openwrt ubus简介

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

UbusBriefTheubusisdesignedforprovidingcommunicationbetweenvariousdaemonsandapplications.ThearchitectureasbelowUbusDaemonMessageDispatchUBUS_MSG_INVOKERegisteredObjectTableUbusConnectionInstanceserverUbusConnectionInstanceclientUbusConnectionInstanceUbusConnectionInstanceObjectInstanceUBUS_MSG_DTTAUBUS_MSG_NOTIFYSubscribedObjectTableUnixSocketUnixSocketRequestReplyCallMethodresponsecall……ObjectsandObjectpathsTheObjectpathsarebindingscannameobjectinstances,andallowapplicationstorefertothem.InOpenWRT,theobjectpathisnamespacelikenetwork.interface.lanMethodsandNotificationsMethodsareoperationsthatcanbeinvokedonanobject,withoptionalinputparametersandoutput.Notificationsarebroadcastsfromtheobjecttoanyinterestedobserversoftheobject.ThenotificationsmaycontainadatapayloadCallingamethodAmethodcallinubusconsistsoftwomessages;AcallmessagesfromprocessAtoprocessBandthereplymessagesfromprocessBtoprocessA.Thesendmessageandreplymessagesarebothroutedthroughtheubusdaemon.Thecallmessagecontainsthemethodarguments.Thereplymessagesmaybeerrormessages,ormaycontainmethodreturneddata.CallProcess1.Thecallmethodmessagescontainstheubusconnectioncontext,thedestinationobjectid,themethodname,themethodarguments.2.Themethodcallmessageissendtotheubusdaemon3.Theubusdaemonlookupthedestinationobjectid,ifaprocessownstheobjectinstance,thenthedaemonwillforwardthemethodcalltothefindprocess.Otherwisetheubusdaemoncreatesanerrormessagesandsendstheerrormessagebacktothemessagecallasreply.4.Thereceivingprocesswillparsetheubusobjectmessages,andfindthecallmethodandargumentsbelongtothemethod.Thenmatchtheobjectmethodsinobjectinstance,iffindmatchedmethod,willinvokethemethodandthensendthereplymessages.5.Ubusdaemonreceivethereplymessageandforwardthereplymessagetotheprocessthatmadethemethodcall.6.ThereplymessagesistransferredasubusblobmessagesstructurewhichisTLV(Type-Length-Value)basedbinarymessagestype.7.Theprocessreceivedthereplymessageshouldparsethemessageandformattohuman-nicemessagetypeasJSONorXML.NotifyNotificationsAnotificationinubusconsistsofasinglemessages,sendbyoneprocesstoanynumberofotherprocesses,whichmeansthenotificationisaunidirectionalbroadcast,noneedexpectedreplymessage.Thenotificationsenderdonotknowthenotificationsrecipients,itjustsendthenotificationontobusTheinterestrecipientsshouldsubscribethesenderobjectwiththebusdaemon.NotificationProcess1.Addnotificationobjectontoubusdaemon2.Thenotificationmessagecontainsubusconnectioncontext,thenotificationsenderobjectID,thenotificationtypeandoptionalargumentswiththetype.3.Anyprocessontheubuscansubscribethenotificationobject.Thebusmayhasalistofsubscribers,whichwillmatchtheobserverswhendaemonhandlethenotificationmessage.4.Theubusdaemoncheckthenotificationanddetermineswhichprocessesareinterestedinit.Thensendthenotificationtoalloftheinterestedprocesses.5.Eachsubscriberprocessreceivingthenotificationdecideswhattodowiththenotificationmessage.Blob_bufstructureonubusBlob_attrExtended(1bit)ID/Type(7bit)Data_len(24bit)DataBlob_msgExtended(1bit)ID/Type(7bit)Data_len(24bit)namelennamedataDataBlob_msgBlob_bufBlob_attrBlob_attrBlob_attrHowtouseubusServerMainprocessM1.DefineaobjectwithsomeabstractmethodsM2.Connecttheserverprocesstoubusdaemonandgetaubus_context,thecontextwillcontainedtheconnectedfd,registeredfdcallbackandanAVLtreetomanageallobjectsinformationwiththisconnectionM3.Usingulooputilitiestoaddtheubus_context,whichistoregistertheconnectedfdintoepollsetM4.AddthedefinedobjectintoubusdM5.ForeverlooptoepollthefdsetWhattodoinmethodhandlerH1.Parsetheblob_attrmsgintoablob_attrtable,whichcaneasyusingbyindexthetablebymsgIDH2.Getthemethodargumentsaccordingtomsgid,thehandlermaybecallmethodinanotherobjectsorinvokeashellscripttodosomeservice,etcH3.Preparetheresponsemsgintoblob_buffandsendtheresponsetoubusdaemon,whichwillforwardtheresponsetorequestclientifnotspecify“no_reply”or”deferred”flagH4.Ifspecify“deferred”flaginreqcontextinthemethodhandler,whichmeanstheserverprocesswillnotexpecttheresponseinthisrequesthandlerandjustcompletethisrequest.#includelibubox/blobmsg_json.h#includelibubus.hstaticstructubus_context*ctx;staticinttest_hello(structubus_context*ctx,structubus_object*obj,structubus_request_data*req,constchar*method,structblob_attr*msg){structhello_request*hreq;structblob_attr*tb[__HELLO_MAX];constchar*format=%sreceivedamessage:%s;constchar*msgstr=(unknown);//H1.Parsetheblob_attrmsg(blob_data(msg))intoablob_attr//table(tb),whichcaneasilyusebymsgIDtoindexthetableblobmsg_parse(hello_policy,ARRAY_SIZE(hello_policy),tb,blob_data(msg),blob_len(msg));//H2.GetmethodargumentsbymsgIDif(tb[HELLO_MSG])msgstr=blobmsg_data(tb[HELLO_MSG]);hreq=calloc(1,sizeof(*hreq)+strlen(format)+strlen(obj-name)+strlen(msgstr)+1);sprintf(hreq-data,format,obj-name,msgstr);//H4.Deferthereplyfortherequest//Thereplywillbemakingintimercallbackubus_defer_request(ctx,req,&hreq-req);hreq-timeout.cb=test_hello_reply;uloop_timeout_set(&hreq-timeout,1000);return0;}//Definehellomethodwithtest_hellohandle//hellopolicytellubusdtheobjectmethodparameterstyp

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功