Abi
This page presents some idea for an embedded middleware named ABI based on publish/subscribe like Ivy.
General idea
- Give an easy way to allow software components to exchange data with minimum delay nor execution overhead.
- The components don't need to know each other, they only need the message format.
- Each subscriber set a callback function that will be called when new data are sent.
Implementation
Message definition
The messages are describe in conf/messages.xml like any other messages in paparazzi. Name are unique, IDs starts from 0. "type" field should be a real C type. Field name might be useless.
<class name="airborne"> <message name="DATA" id="0"> <field name="a" type="float"/> <field name="b" type="struct bla"/> </message> ... </class>
Code generation
The generated code will be in var/include/abi_messages.h. Bind and Send functions are generated, as well as callback type definition. A linked list is used to store the binded callbacks for each message. The head of the list is in an array to allow a fast access.
// Message IDs #define DATA_ID 0 // Callbacks typedef void (*abi_callback)(void); // Generic callback definition typedef void (*abi_callbackDATA)(const float * a, const struct bla * b); // Specific callback for DATA message (arguments are const to prevent modifying them) // Array and linked list #define ABI_MESSAGE_NB <highest id of the messages in airborne class + 1> struct _abi_event_struct { abi_callback cb; struct _abi_event_struct * next; }; typedef struct _abi_event_struct * abi_event; #ifdef ABI_C #define EXTERN #else #define EXTERN extern #endif EXTERN abi_event abi_queues[ABI_MESSAGE_NB]; // Magic trick to avoid generating .c file #define ABI_FOREACH(head,el) for(el=head; el; el=el->next) #define ABI_PREPEND(head,new) { (add)->next = head; head = add; } // Bind and Send for each messages static inline void AbiBindMsgDATA(abi_event ev, abi_callbackDATA cb) { ev->cb = (abi_callback)cb; // Store callback ABI_PREPEND(abi_queues[DATA_ID],ev); // add to the head of the list (because I'm lazy) } static inline void AbiSendMsgDATA(float * a, struct bla * b) { // Call all callback functions abi_event e; ABI_FOREACH(abi_queues[DATA_ID],e) { abi_callbackDATA cb = (abi_callbackDATA)(e->cb); // C black magic cb(a,b); } }
Airborne code for main AP
This part of the code should be written and called only once in a .c file (like main.c, but it can be somewhere else)
#define ABI_C 1 #include "abi_messages.h"
Airborne code for subscriber
Include header and declare an abi_event as a global variable. Write the callback function with the proper prototype.
#include "abi_messages.h" abi_event ev; void data_cb(const float * a, const struct bla * b) { // do something here }
In the initialization function (or later) call the binding functions for the message you want to receive.
AbiBindMsgDATA(ev, data_cb);
Airborne code for publisher
Include header of course.
#include "abi_messages.h"
In any function, call the send function.
float var = 2.; struct bla s; AbiSendMsgDATA(&var,&s);
That's it !