1、ZBus-the lightweight and flexible Zephyr message busRodrigo Peixoto,Edge-UFAL/Citriniorodrigopex1MotivationThread AThread BONE-TO-ONEFIFOLIFOStackMessage queueMailboxPipe2MotivationThread AThread BThread CThread DFIFOLIFOStackMessage queueMailboxPipeONE-TO-MANY3MotivationThread AThread BThread CThre
2、ad DThread EMANY-TO-MANYFIFOLIFOStackMessage queueMailboxPipe4Solution ideaMessage busThread AThread BThread CThread DThread EFIFOLIFOStackMessage queueMailboxPipeBus5Bus topologies6ONE-TO-ONEONE-TO-MANYMANY-TO-MANYEmbedded systems challenges Memory constraints Processing limitations Battery-powered
3、 devices7 ZBusChan 1Chan 2Chan 3Chan N8Thread AThread BThread CSubscribersAsynchronous ZBusChan 1Chan 2Chan 3Chan NDecoupled9TimeSpaceSynchronizationThread AThread BThread CCallback DCallback ESubscribersListenersAsynchronousSynchronousTimeSpaceSynchronizationDecoupledCoupled ZBusChan 1Chan 2Chan 3C
4、han N1011ThreadReadChannelAvailable actions12ThreadPublishNotify*ChannelAvailable actionsSubscriber Notification queue Listener Callbackexecuteput13Channel AChannel BMessage:“Hi!”Subscribers:Thread 2Listeners:listener1()listener2()Channel CChannel DBusEvent DispatcherThread 2Thread 1void listener1()
5、void listener2()125346Virtual Distributed Event DispatcherVDED is the bus logic responsible for sending notifications about message publications to the channels observers.There is no central entity that acts as an event dispatcher on ZBus.14Virtual Distributed Event Dispatcher ZBusThread 1Chan AThre
6、ad 2BroadcastexampleListener 1Listener 2Thread 3Thread 415Virtual Distributed Event Dispatcher16Virtual Distributed Event Dispatcher ZBusThread 1Chan AThread 2Chan BThread 4Thread 3High priority contextLow priority context1718ThreadClaim/FinishChannelAvailable actionsExample19ExampleButton20Example2
7、1StorageButtonExample22StorageBluetoothButtonExample23StorageBluetoothMockButtonExample24StorageBluetoothMockButtonNB-IoTExample25StorageBluetoothMockButtonNB-IoTUsage considerationsPROSPromotes event-driven architectureUnified way to make threads talk and share dataCode decoupling(time,space,and sy
8、nchronization)*Promotes reuseIncrease testability(+controllability+observability)Extensible(claim/finish+user_data)CONSTake time to master itToo many possibilitiesNot for intensive byte streamingNo delivery guarantees for subscribers26ZBus Features Backlog 27ZBus async APIs28 Run inside an ISR could
9、 be possible Would avoid using work queues Dedicated ZBus thread ZBus omni subscriber29 The omni subscriber will listen to all the channels It can be used to extend the bus featuresIntegration and samples30 Make ZBus an Input subsys event distributor backend Add samples(Bluetooth,Sensors,FSM,etc.)ZB
10、us for multi-core31ZBus for multi-target32TARGET#1(ARM)TARGET#2(RISCV)BluetoothZBus desktop33+ZBus Discord Channel 34ZBus Roadmap topic at Zephyr Discord channelTips and tricks35 Avoid excessive use of them,they are running during the publishing processListeners36 Think of them as an ISR.They must r
11、un as quickly as possible Do not sleep inside listeners.It will increase the publishing latencyListeners37 Use zbus_chan_const_msg inside listeners.The channels are already locked!Use a work queue or separated thread instead of executing something heavy inside a listener Do not use subscribers when
12、losses and duplications cannot be toleratedSubscribers38 PR with confirmed channels sample submitted Use listeners in conjunction with message queues Take care with publishing loopsUndesired loops39 Avoid loops on the bus diagram Observer 1Chan AObserver 2Chan BUndesired loops40 Take care with chain
13、ed publishing loops Observer 1Chan AObserver 3Chan BObserver 2Observer 4Chan CChan D Do not use ZBus functions inside an ISRISR41 Postpone that by using work queues insteadExtras42 The channels can be used as a concurrent property system Isolate the hardware code using channels Use channels as modules interface in/outQuestions&Answers43Thank you!