3 <link rel="stylesheet" type="text/css" href="doc.css">
7 <a name="HOWTO.WRITE.a.PLUGIN.for.AFB-DAEMON"></a>
8 <h1>HOWTO WRITE a PLUGIN for AFB-DAEMON</h1>
16 <li><a href="#HOWTO.WRITE.a.PLUGIN.for.AFB-DAEMON">HOWTO WRITE a PLUGIN for AFB-DAEMON</a>
18 <li><a href="#Summary">Summary</a>
20 <li><a href="#Nature.of.a.plugin">Nature of a plugin</a></li>
21 <li><a href="#Kinds.of.plugins">Kinds of plugins</a>
23 <li><a href="#Application.plugins">Application plugins</a></li>
24 <li><a href="#Service.plugins">Service plugins</a></li>
27 <li><a href="#Live.cycle.of.a.plugin.within.afb-daemon">Live cycle of a plugin within afb-daemon</a></li>
28 <li><a href="#Content.of.a.plugin">Content of a plugin</a>
30 <li><a href="#The.name.of.the.plugin">The name of the plugin</a></li>
31 <li><a href="#Names.of.verbs">Names of verbs</a></li>
32 <li><a href="#The.initialisation.function">The initialisation function</a></li>
33 <li><a href="#Functions.implementing.verbs">Functions implementing verbs</a>
39 <li><a href="#The.Tic-Tac-Toe.example">The Tic-Tac-Toe example</a></li>
40 <li><a href="#Choosing.names">Choosing names</a>
42 <li><a href="#Names.for.API..plugin.">Names for API (plugin)</a></li>
43 <li><a href="#Names.for.verbs">Names for verbs</a></li>
44 <li><a href="#Names.for.arguments">Names for arguments</a></li>
45 <li><a href="#Forging.names.widely.available">Forging names widely available</a></li>
48 <li><a href="#Options.to.set.when.compiling.plugins">Options to set when compiling plugins</a></li>
49 <li><a href="#Header.files.to.include">Header files to include</a></li>
50 <li><a href="#Writing.a.synchronous.verb.implementation">Writing a synchronous verb implementation</a>
52 <li><a href="#The.incoming.request">The incoming request</a></li>
53 <li><a href="#Associating.a.context.to.the.session">Associating a context to the session</a></li>
54 <li><a href="#Sending.the.reply.to.a.request">Sending the reply to a request</a></li>
57 <li><a href="#Getting.argument.of.invocation">Getting argument of invocation</a>
59 <li><a href="#Basic.functions.for.querying.arguments">Basic functions for querying arguments</a></li>
60 <li><a href="#Arguments.for.received.files">Arguments for received files</a></li>
61 <li><a href="#Arguments.as.a.JSON.object">Arguments as a JSON object</a></li>
64 <li><a href="#Initialisation.of.the.plugin.and.declaration.of.verbs">Initialisation of the plugin and declaration of verbs</a></li>
65 <li><a href="#Sending.messages.to.the.log.system">Sending messages to the log system</a>
67 <li><a href="#Verbs.for.logging.messages">Verbs for logging messages</a></li>
68 <li><a href="#Managing.verbosity">Managing verbosity</a></li>
69 <li><a href="#Output.format.and.destination">Output format and destination</a></li>
72 <li><a href="#Sending.events">Sending events</a></li>
73 <li><a href="#Writing.an.asynchronous.verb.implementation">Writing an asynchronous verb implementation</a></li>
74 <li><a href="#How.to.build.a.plugin">How to build a plugin</a>
76 <li><a href="#Example.for.cmake.meta.build.system">Example for cmake meta build system</a></li>
77 <li><a href="#Exporting.the.function.pluginAfbV1Register">Exporting the function pluginAfbV1Register</a></li>
78 <li><a href="#Building.within.yocto">Building within yocto</a></li>
85 <a name="Summary"></a>
88 <p>The binder afb-daemon serves files through
89 the HTTP protocol and offers access to API’s through
90 HTTP or WebSocket protocol.</p>
92 <p>The plugins are used to add API’s to afb-daemon.
93 This part describes how to write a plugin for afb-daemon.
94 Excepting this summary, this part is intended to be read
97 <p>Before going into details, through a tiny example,
98 a short overview plugins basis is needed.</p>
100 <a name="Nature.of.a.plugin"></a>
101 <h3>Nature of a plugin</h3>
103 <p>A plugin is a separate piece of code made of a shared library.
104 The plugin is loaded and activated by afb-daemon when afb-daemon
107 <p>Technically, a plugin is not linked to any library of afb-daemon.</p>
109 <a name="Kinds.of.plugins"></a>
110 <h3>Kinds of plugins</h3>
112 <p>There is two kinds of plugins: application plugins and service
115 <a name="Application.plugins"></a>
116 <h4>Application plugins</h4>
118 <p>Application plugins are intended to be instanciated for each
119 application: when an application using that plugin is started,
120 its binder starts a new instance of the plugin.</p>
122 <p>It means that the application plugins mainly have only one
123 context to manage for one client.</p>
125 <a name="Service.plugins"></a>
126 <h4>Service plugins</h4>
128 <p>Service plugins are intended to be instanciated only one time
129 only and connected to many clients.</p>
131 <p>So either it does not manage context at all or otherwise,
132 if it manages context, it should be able to manage one context
135 <p>In details, it may be useful to have service plugins at a user
138 <a name="Live.cycle.of.a.plugin.within.afb-daemon"></a>
139 <h3>Live cycle of a plugin within afb-daemon</h3>
141 <p>The plugins are loaded and activated when afb-daemon starts.</p>
143 <p>At start, the plugin initialise itself.
144 If it fails to initialise then afb-daemon stops.</p>
146 <p>Conversely, if it success to initialize, it must declare
147 a name, that must be unique, and a list of API’s verbs.</p>
149 <p>When initialized, the functions implementing the API’s verbs
150 of the plugin are activated on call.</p>
152 <p>At the end, nothing special is done by afb-daemon.
153 Consequently, developpers of plugins should use ‘atexit’
154 or ‘on_exit’ during initialisation if they need to
155 perform specific actions when stopping.</p>
157 <a name="Content.of.a.plugin"></a>
158 <h3>Content of a plugin</h3>
160 <p>For afb-daemon, a plugin contains 2 different
161 things: names and functions.</p>
163 <p>There is two kind of names:
164 - the name of the plugin,
165 - the names of the verbs.</p>
167 <p>There is two kind of functions:
168 - the initialisation function
169 - functions implementing verbs</p>
171 <p>Afb-daemon translates the name of the method that is
172 invoked to a pair of API and verb names. For example,
173 the method named <strong>foo/bar</strong> translated to the API
174 name <strong>foo</strong> and the verb name <strong>bar</strong>.
175 To serve it, afb-daemon search the plugin that record
176 the name <strong>foo</strong> and if it also recorded the verb <strong>bar</strong>,
177 it calls the implementation function declared for this verb.</p>
179 <p>Afb-daemon make no distinction between lower case
180 and upper case when searching for a method.
181 Thus, The names <strong>TicTacToe/Board</strong> and <strong>tictactoe/borad</strong>
184 <a name="The.name.of.the.plugin"></a>
185 <h4>The name of the plugin</h4>
187 <p>The name of the plugin is also known as the name
188 of the API that defines the plugin.</p>
190 <p>This name is also known as the prefix.</p>
192 <p>The name of a plugin MUST be unique within afb-daemon.</p>
194 <p>For example, when a client of afb-daemon
195 calls a method named <strong>foo/bar</strong>. Afb-daemon
196 extracts the prefix <strong>foo</strong> and the suffix <strong>bar</strong>.
197 <strong>foo</strong> is the API name and must match a plugin name,
198 the plugin that implements the verb <strong>bar</strong>.</p>
200 <a name="Names.of.verbs"></a>
201 <h4>Names of verbs</h4>
203 <p>Each plugin exposes a set of verbs that can be called
204 by client of afb-daemon.</p>
206 <p>The name of a verb MUST be unique within a plugin.</p>
208 <p>Plugins link verbs to functions that are called
209 when clients emit requests for that verb.</p>
211 <p>For example, when a client of afb-daemon
212 calls a method named <strong>foo/bar</strong>.</p>
214 <a name="The.initialisation.function"></a>
215 <h4>The initialisation function</h4>
217 <p>The initialisation function serves several purposes.</p>
220 <li><p>It allows afb-daemon to check the version
221 of the plugin using the name of the initialisation
222 functions that it found. Currently, the initialisation
223 function is named <strong>pluginAfbV1Register</strong>. It identifies
224 the first version of plugins.</p></li>
225 <li><p>It allows the plugin to initialise itself.</p></li>
226 <li><p>It serves to the plugin to declare names, descriptions,
227 requirements and implmentations of the verbs that it exposes.</p></li>
231 <a name="Functions.implementing.verbs"></a>
232 <h4>Functions implementing verbs</h4>
234 <p>When a method is called, afb-daemon constructs a request
235 object and pass it to the implementation function for verb
236 within the plugin of the API.</p>
238 <p>An implementation function receives a request object that
239 is used to get arguments of the request, to send
240 answer, to store session data.</p>
242 <p>A plugin MUST send an answer to the request.</p>
244 <p>But it is not mandatory to send the answer
245 before to return from the implementing function.
246 This behaviour is important for implementing
247 asynchronous actions.</p>
249 <p>Implementation functions that always reply to the request
250 before returning are named <em>synchronous implementations</em>.
251 Those that don’t always reply to the request before
252 returning are named <em>asynchronous implementations</em>.</p>
254 <p>Asynchronous implementations typically initiate an
255 asynchronous action and record to send the reply
256 on completion of this action.</p>
258 <a name="The.Tic-Tac-Toe.example"></a>
259 <h2>The Tic-Tac-Toe example</h2>
261 <p>This part explains how to write an afb-plugin.
262 For the sake of being practical we will use many
263 examples from the tic-tac-toe example.
264 This plugin example is in <em>plugins/samples/tic-tac-toe.c</em>.</p>
266 <p>This plugin is named <strong><em>tictactoe</em></strong>.</p>
268 <a name="Choosing.names"></a>
269 <h2>Choosing names</h2>
271 <p>The designer of a plugin must defines names for its plugin
272 (or its API) and for the verbs of its API. He also
273 must defines names for arguments given by name.</p>
275 <p>While forging names, the designer should take into account
276 the rules for making valid names and some rules that make
277 the names easy to use across plaforms.</p>
279 <p>The names and strings used ALL are UTF-8 encoded.</p>
281 <a name="Names.for.API..plugin."></a>
282 <h3>Names for API (plugin)</h3>
284 <p>The names of the API are checked.
285 All characters are authorised except:</p>
288 <li>the control characters (\u0000 .. \u001f)</li>
289 <li>the characters of the set { ‘ ’, ‘“’, ‘#’, ‘%’, ‘&’,
290 ‘’‘, ’/‘, ’?‘, ’`‘, ’\x7f' }</li>
294 <p>In other words the set of forbidden characters is
295 { \u0000..\u0020, \u0022, \u0023, \u0025..\u0027,
296 \u002f, \u003f, \u0060, \u007f }.</p>
298 <p>Afb-daemon make no distinction between lower case
299 and upper case when searching for an API by its name.</p>
301 <a name="Names.for.verbs"></a>
302 <h3>Names for verbs</h3>
304 <p>The names of the verbs are not checked.</p>
306 <p>However, the validity rules for verb’s names are the
307 same as for API’s names except that the dot (.) character
310 <p>Afb-daemon make no distinction between lower case
311 and upper case when searching for an API by its name.</p>
313 <a name="Names.for.arguments"></a>
314 <h3>Names for arguments</h3>
316 <p>The names for arguments are not restricted and can be
319 <p>The arguments are searched with the case sensitive
320 string comparison. Thus the names “index” and “Index”
321 are not the same.</p>
323 <a name="Forging.names.widely.available"></a>
324 <h3>Forging names widely available</h3>
326 <p>The key names of javascript object can be almost
327 anything using the arrayed notation:</p>
329 <pre><code>object[key] = value
332 <p>That is not the case with the dot notation:</p>
334 <pre><code>object.key = value
337 <p>Using the dot notation, the key must be a valid javascript
340 <p>For this reason, the chosen names should better be
341 valid javascript identifier.</p>
343 <p>It is also a good practice, even for arguments, to not
344 rely on the case sensitivity and to avoid the use of
345 names different only by the case.</p>
347 <a name="Options.to.set.when.compiling.plugins"></a>
348 <h2>Options to set when compiling plugins</h2>
350 <p>Afb-daemon provides a configuration file for <em>pkg-config</em>.
351 Typing the command</p>
353 <pre><code>pkg-config --cflags afb-daemon
356 <p>will print the flags to use for compiling, like this:</p>
358 <pre><code>$ pkg-config --cflags afb-daemon
359 -I/opt/local/include -I/usr/include/json-c
362 <p>For linking, you should use</p>
364 <pre><code>$ pkg-config --libs afb-daemon
368 <p>As you see, afb-daemon automatically includes dependency to json-c.
369 This is done through the <strong>Requires</strong> keyword of pkg-config.</p>
371 <p>If this behaviour is a problem, let us know.</p>
373 <a name="Header.files.to.include"></a>
374 <h2>Header files to include</h2>
376 <p>The plugin <em>tictactoe</em> has the following lines for its includes:</p>
378 <pre><code>#define _GNU_SOURCE
379 #include <stdio.h>
380 #include <string.h>
381 #include <json-c/json.h>
382 #include <afb/afb-plugin.h>
385 <p>The header <em>afb/afb-plugin.h</em> includes all the features that a plugin
386 needs except two foreign header that must be included by the plugin
390 <li><em>json-c/json.h</em>: this header must be include to handle json objects;</li>
391 <li><em>systemd/sd-event.h</em>: this must be include to access the main loop;</li>
392 <li><em>systemd/sd-bus.h</em>: this may be include to use dbus connections.</li>
396 <p>The <em>tictactoe</em> plugin does not use systemd features so it is not included.</p>
398 <p>When including <em>afb/afb-plugin.h</em>, the macro <strong>_GNU_SOURCE</strong> must be
401 <a name="Writing.a.synchronous.verb.implementation"></a>
402 <h2>Writing a synchronous verb implementation</h2>
404 <p>The verb <strong>tictactoe/board</strong> is a synchronous implementation.
405 Here is its listing:</p>
410 static void board(struct afb_req req)
413 struct json_object *description;
415 /* retrieves the context for the session */
416 board = board_of_req(req);
417 INFO(afbitf, "method 'board' called for boardid %d", board->id);
419 /* describe the board */
420 description = describe(board);
422 /* send the board's description */
423 afb_req_success(req, description, NULL);
427 <p>This examples show many aspects of writing a synchronous
428 verb implementation. Let summarize it:</p>
431 <li><p>The function <strong>board_of_req</strong> retrieves the context stored
432 for the plugin: the board.</p></li>
433 <li><p>The macro <strong>INFO</strong> sends a message of kind <em>INFO</em>
434 to the logging system. The global variable named <strong>afbitf</strong>
435 used represents the interface to afb-daemon.</p></li>
436 <li><p>The function <strong>describe</strong> creates a json_object representing
438 <li><p>The function <strong>afb_req_success</strong> sends the reply, attaching to
439 it the object <em>description</em>.</p></li>
443 <a name="The.incoming.request"></a>
444 <h3>The incoming request</h3>
446 <p>For any implementation, the request is received by a structure of type
447 <strong>struct afb_req</strong>.</p>
449 <blockquote><p>Note that this is a PLAIN structure, not a pointer to a structure.</p></blockquote>
451 <p>The definition of <strong>struct afb_req</strong> is:</p>
454 * Describes the request by plugins from afb-daemon
457 const struct afb_req_itf *itf; /* the interfacing functions */
458 void *closure; /* the closure for functions */
462 <p>It contains two pointers: one, <em>itf</em>, points to the functions needed
463 to handle the internal request represented by the second pointer, <em>closure</em>.</p>
465 <blockquote><p>The structure must never be used directly.
466 Insted, use the intended functions provided
467 by afb-daemon and described here.</p></blockquote>
469 <p><em>req</em> is used to get arguments of the request, to send
470 answer, to store session data.</p>
472 <p>This object and its interface is defined and documented
473 in the file names <em>afb/afb-req-itf.h</em></p>
475 <p>The above example uses 2 times the request object <em>req</em>.</p>
477 <p>The first time, it is used for retrieving the board attached to
478 the session of the request.</p>
480 <p>The second time, it is used to send the reply: an object that
481 describes the current board.</p>
483 <a name="Associating.a.context.to.the.session"></a>
484 <h3>Associating a context to the session</h3>
486 <p>When the plugin <em>tic-tac-toe</em> receives a request, it musts regain
487 the board that describes the game associated to the session.</p>
489 <p>For a plugin, having data associated to a session is a common case.
490 This data is called the context of the plugin for the session.
491 For the plugin <em>tic-tac-toe</em>, the context is the board.</p>
493 <p>The requests <em>afb_req</em> offer four functions for
494 storing and retrieving the context associated to the session.</p>
496 <p>These functions are:</p>
499 <li><p><strong>afb_req_context_get</strong>:
500 retrieves the context data stored for the plugin.</p></li>
501 <li><p><strong>afb_req_context_set</strong>:
502 store the context data of the plugin.</p></li>
503 <li><p><strong>afb_req_context</strong>:
504 retrieves the context data of the plugin,
505 if needed, creates the context and store it.</p></li>
506 <li><p><strong>afb_req_context_clear</strong>:
507 reset the stored data.</p></li>
511 <p>The plugin <em>tictactoe</em> use a convenient function to retrieve
512 its context: the board. This function is <em>board_of_req</em>:</p>
515 * retrieves the board of the request
517 static inline struct board *board_of_req(struct afb_req req)
519 return afb_req_context(req, (void*)get_new_board, (void*)release_board);
523 <p>The function <strong>afb_req_context</strong> ensure an existing context
524 for the session of the request.
525 Its two last arguments are functions. Here, the casts are required
526 to avoid a warning when compiling.</p>
528 <p>Here is the definition of the function <strong>afb_req_context</strong></p>
531 * Gets the pointer stored by the plugin for the session of 'req'.
532 * If the stored pointer is NULL, indicating that no pointer was
533 * already stored, afb_req_context creates a new context by calling
534 * the function 'create_context' and stores it with the freeing function
537 static inline void *afb_req_context(struct afb_req req, void *(*create_context)(), void (*free_context)(void*))
539 void *result = afb_req_context_get(req);
540 if (result == NULL) {
541 result = create_context();
542 afb_req_context_set(req, result, free_context);
548 <p>The second argument if the function that creates the context.
549 For the plugin <em>tic-tac-toe</em> it is the function <strong>get_new_board</strong>.
550 The function <strong>get_new_board</strong> creates a new board and set its
551 count of use to 1. The boards are counting their count of use
552 to free there ressources when no more used.</p>
554 <p>The third argument if the function that frees the context.
555 For the plugin <em>tic-tac-toe</em> it is the function <strong>release_board</strong>.
556 The function <strong>release_board</strong> decrease the the count of use of
557 the board given as argument. If the use count decrease to zero,
558 the board data are freed.</p>
560 <p>The definition of the other functions for dealing with contexts are:</p>
563 * Gets the pointer stored by the plugin for the session of 'req'.
564 * When the plugin has not yet recorded a pointer, NULL is returned.
566 void *afb_req_context_get(struct afb_req req);
569 * Stores for the plugin the pointer 'context' to the session of 'req'.
570 * The function 'free_context' will be called when the session is closed
571 * or if plugin stores an other pointer.
573 void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*));
576 * Frees the pointer stored by the plugin for the session of 'req'
577 * and sets it to NULL.
579 * Shortcut for: afb_req_context_set(req, NULL, NULL)
581 static inline void afb_req_context_clear(struct afb_req req)
583 afb_req_context_set(req, NULL, NULL);
587 <a name="Sending.the.reply.to.a.request"></a>
588 <h3>Sending the reply to a request</h3>
590 <p>Two kinds of replies can be made: successful replies and
593 <blockquote><p>Sending a reply to a request must be done at most one time.</p></blockquote>
595 <p>The two functions to send a reply of kind “success” are
596 <strong>afb_req_success</strong> and <strong>afb_req_success_f</strong>.</p>
599 * Sends a reply of kind success to the request 'req'.
600 * The status of the reply is automatically set to "success".
601 * Its send the object 'obj' (can be NULL) with an
602 * informationnal comment 'info (can also be NULL).
604 void afb_req_success(struct afb_req req, struct json_object *obj, const char *info);
607 * Same as 'afb_req_success' but the 'info' is a formatting
608 * string followed by arguments.
610 void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...);
613 <p>The two functions to send a reply of kind “failure” are
614 <strong>afb_req_fail</strong> and <strong>afb_req_fail_f</strong>.</p>
617 * Sends a reply of kind failure to the request 'req'.
618 * The status of the reply is set to 'status' and an
619 * informationnal comment 'info' (can also be NULL) can be added.
621 * Note that calling afb_req_fail("success", info) is equivalent
622 * to call afb_req_success(NULL, info). Thus even if possible it
623 * is strongly recommanded to NEVER use "success" for status.
625 void afb_req_fail(struct afb_req req, const char *status, const char *info);
628 * Same as 'afb_req_fail' but the 'info' is a formatting
629 * string followed by arguments.
631 void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...);
634 <a name="Getting.argument.of.invocation"></a>
635 <h2>Getting argument of invocation</h2>
637 <p>Many verbs expect arguments. Afb-daemon let plugins
638 retrieve their arguments by name not by position.</p>
640 <p>Arguments are given by the requests either through HTTP
641 or through WebSockets.</p>
643 <p>For example, the verb <strong>join</strong> of the plugin <strong>tic-tac-toe</strong>
644 expects one argument: the <em>boardid</em> to join. Here is an extract:</p>
649 static void join(struct afb_req req)
651 struct board *board, *new_board;
654 /* retrieves the context for the session */
655 board = board_of_req(req);
656 INFO(afbitf, "method 'join' called for boardid %d", board->id);
658 /* retrieves the argument */
659 id = afb_req_value(req, "boardid");
665 <p>The function <strong>afb_req_value</strong> search in the request <em>req</em>
666 for an argument whose name is given. When no argument of the
667 given name was passed, <strong>afb_req_value</strong> returns NULL.</p>
669 <blockquote><p>The search is case sensitive. So the name <em>boardid</em> is not the
670 same name than <em>BoardId</em>. But this must not be assumed so two
671 expected names of argument should not differ only by case.</p></blockquote>
673 <a name="Basic.functions.for.querying.arguments"></a>
674 <h3>Basic functions for querying arguments</h3>
676 <p>The function <strong>afb_req_value</strong> is defined as below:</p>
679 * Gets from the request 'req' the string value of the argument of 'name'.
680 * Returns NULL if when there is no argument of 'name'.
681 * Returns the value of the argument of 'name' otherwise.
683 * Shortcut for: afb_req_get(req, name).value
685 static inline const char *afb_req_value(struct afb_req req, const char *name)
687 return afb_req_get(req, name).value;
691 <p>It is defined as a shortcut to call the function <strong>afb_req_get</strong>.
692 That function is defined as below:</p>
695 * Gets from the request 'req' the argument of 'name'.
696 * Returns a PLAIN structure of type 'struct afb_arg'.
697 * When the argument of 'name' is not found, all fields of result are set to NULL.
698 * When the argument of 'name' is found, the fields are filled,
699 * in particular, the field 'result.name' is set to 'name'.
701 * There is a special name value: the empty string.
702 * The argument of name "" is defined only if the request was made using
703 * an HTTP POST of Content-Type "application/json". In that case, the
704 * argument of name "" receives the value of the body of the HTTP request.
706 struct afb_arg afb_req_get(struct afb_req req, const char *name);
709 <p>That function takes 2 parameters: the request and the name
710 of the argument to retrieve. It returns a PLAIN structure of
711 type <strong>struct afb_arg</strong>.</p>
713 <p>There is a special name that is defined when the request is
714 of type HTTP/POST with a Content-Type being application/json.
715 This name is <strong>“”</strong> (the empty string). In that case, the value
716 of this argument of empty name is the string received as a body
717 of the post and is supposed to be a JSON string.</p>
719 <p>The definition of <strong>struct afb_arg</strong> is:</p>
722 * Describes an argument (or parameter) of a request
725 const char *name; /* name of the argument or NULL if invalid */
726 const char *value; /* string representation of the value of the argument */
727 /* original filename of the argument if path != NULL */
728 const char *path; /* if not NULL, path of the received file for the argument */
729 /* when the request is finalized this file is removed */
733 <p>The structure returns the data arguments that are known for the
734 request. This data include a field named <strong>path</strong>. This <strong>path</strong>
735 can be accessed using the function <strong>afb_req_path</strong> defined as
739 * Gets from the request 'req' the path for file attached to the argument of 'name'.
740 * Returns NULL if when there is no argument of 'name' or when there is no file.
741 * Returns the path of the argument of 'name' otherwise.
743 * Shortcut for: afb_req_get(req, name).path
745 static inline const char *afb_req_path(struct afb_req req, const char *name)
747 return afb_req_get(req, name).path;
751 <p>The path is only defined for HTTP/POST requests that send file.</p>
753 <a name="Arguments.for.received.files"></a>
754 <h3>Arguments for received files</h3>
756 <p>As it is explained just above, clients can send files using
757 HTTP/POST requests.</p>
759 <p>Received files are attached to a arguments. For example, the
760 following HTTP fragment (from test/sample-post.html)
761 will send an HTTP/POST request to the method
762 <strong>post/upload-image</strong> with 2 arguments named <em>file</em> and
765 <pre><code><h2>Sample Post File</h2>
766 <form enctype="multipart/form-data">
767 <input type="file" name="file" />
768 <input type="hidden" name="hidden" value="bollobollo" />
770 <button formmethod="POST" formaction="api/post/upload-image">Post File</button>
774 <p>In that case, the argument named <strong>file</strong> has its value and its
775 path defined and not NULL.</p>
777 <p>The value is the name of the file as it was
778 set by the HTTP client and is generally the filename on the
781 <p>The path is the path of the file saved on the temporary local storage
782 area of the application. This is a randomly generated and unic filename
783 not linked in any way with the original filename on the client.</p>
785 <p>The plugin can use the file at the given path the way that it wants:
786 read, write, remove, copy, rename…
787 But when the reply is sent and the query is terminated, the file at
788 this path is destroyed if it still exist.</p>
790 <a name="Arguments.as.a.JSON.object"></a>
791 <h3>Arguments as a JSON object</h3>
793 <p>Plugins can get all the arguments as one single object.
794 This feature is provided by the function <strong>afb_req_json</strong>
795 that is defined as below:</p>
798 * Gets from the request 'req' the json object hashing the arguments.
799 * The returned object must not be released using 'json_object_put'.
801 struct json_object *afb_req_json(struct afb_req req);
804 <p>It returns a json object. This object depends on how the request was
808 <li><p>For HTTP requests, this is an object whose keys are the names of the
809 arguments and whose values are either a string for common arguments or
810 an object like { “file”: “…”, “path”: “…” }</p></li>
811 <li><p>For WebSockets requests, the returned object is the object
812 given by the client transparently transported.</p></li>
816 <blockquote><p>In fact, for Websockets requests, the function <strong>afb_req_value</strong>
817 can be seen as a shortcut to
818 <strong><em>json_object_get_string(json_object_object_get(afb_req_json(req), name))</em></strong></p></blockquote>
820 <a name="Initialisation.of.the.plugin.and.declaration.of.verbs"></a>
821 <h2>Initialisation of the plugin and declaration of verbs</h2>
823 <p>To be active, the verbs of the plugin should be declared to
824 afb-daemon. And even more, the plugin itself must be recorded.</p>
826 <p>The mechanism for doing this is very simple: when afb-need starts,
827 it loads the plugins that are listed in its argument or configuration.</p>
829 <p>Loading a plugin follows the following steps:</p>
832 <li><p>It loads the plugin using <em>dlopen</em>.</p></li>
833 <li><p>It searchs for the symbol named <strong>pluginAfbV1Register</strong> using <em>dlsym</em>.
834 This symbol is assumed to be the exported initialisation function of the plugin.</p></li>
835 <li><p>It build an interface object for the plugin.</p></li>
836 <li><p>It calls the found function <strong>pluginAfbV1Register</strong> and pass it the pointer
837 to its interface.</p></li>
838 <li><p>The function <strong>pluginAfbV1Register</strong> setup the plugin, initialize it.</p></li>
839 <li><p>The function <strong>pluginAfbV1Register</strong> returns the pointer to a structure
840 that describes the plugin: its version, its name (prefix or API name), and the
841 list of its verbs.</p></li>
842 <li><p>Afb-daemon checks that the returned version and name can be managed.
843 If it can manage it, the plugin and its verbs are recorded and can be used
844 when afb-daemon finishes it initialisation.</p></li>
848 <p>Here is the listing of the function <strong>pluginAfbV1Register</strong> of the plugin
849 <em>tic-tac-toe</em>:</p>
852 * activation function for registering the plugin called by afb-daemon
854 const struct AFB_plugin *pluginAfbV1Register(const struct AFB_interface *itf)
856 afbitf = itf; // records the interface for accessing afb-daemon
857 return &plugin_description; // returns the description of the plugin
861 <p>This is a very small function because the <em>tic-tac-toe</em> plugin doesn’t have initialisation step.
862 It merely record the daemon’s interface and returns its descritption.</p>
864 <p>The variable <strong>afbitf</strong> is a variable global to the plugin. It records the
865 interface to afb-daemon and is used for logging and pushing events.
866 Here is its declaration:</p>
869 * the interface to afb-daemon
871 const struct AFB_interface *afbitf;
874 <p>The description of the plugin is defined as below.</p>
877 * array of the verbs exported to afb-daemon
879 static const struct AFB_verb_desc_v1 plugin_verbs[] = {
880 /* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL SHORT DESCRIPTION */
881 { .name= "new", .session= AFB_SESSION_NONE, .callback= new, .info= "Starts a new game" },
882 { .name= "play", .session= AFB_SESSION_NONE, .callback= play, .info= "Tells the server to play" },
883 { .name= "move", .session= AFB_SESSION_NONE, .callback= move, .info= "Tells the client move" },
884 { .name= "board", .session= AFB_SESSION_NONE, .callback= board, .info= "Get the current board" },
885 { .name= "level", .session= AFB_SESSION_NONE, .callback= level, .info= "Set the server level" },
886 { .name= "join", .session= AFB_SESSION_CHECK,.callback= join, .info= "Join a board" },
887 { .name= "undo", .session= AFB_SESSION_NONE, .callback= undo, .info= "Undo the last move" },
888 { .name= "wait", .session= AFB_SESSION_NONE, .callback= wait, .info= "Wait for a change" },
889 { .name= NULL } /* marker for end of the array */
893 * description of the plugin for afb-daemon
895 static const struct AFB_plugin plugin_description =
897 /* description conforms to VERSION 1 */
898 .type= AFB_PLUGIN_VERSION_1,
899 .v1= { /* fills the v1 field of the union when AFB_PLUGIN_VERSION_1 */
900 .prefix= "tictactoe", /* the API name (or plugin name or prefix) */
901 .info= "Sample tac-tac-toe game", /* short description of of the plugin */
902 .verbs = plugin_verbs /* the array describing the verbs of the API */
907 <p>The structure <strong>plugin_description</strong> describes the plugin.
908 It declares the type and version of the plugin, its name, a description
909 and a list of its verbs.</p>
911 <p>The list of verbs is an array of structures describing the verbs and terminated by a marker:
912 a verb whose name is NULL.</p>
914 <p>The description of the verbs for this version is made of 4 fields:</p>
917 <li><p>the name of the verbs,</p></li>
918 <li><p>the session management flags,</p></li>
919 <li><p>the implementation function to be call for the verb,</p></li>
920 <li><p>a short description.</p></li>
924 <p>The structure describing verbs is defined as follows:</p>
927 * Description of one verb of the API provided by the plugin
928 * This enumeration is valid for plugins of type 1
930 struct AFB_verb_desc_v1
932 const char *name; /* name of the verb */
933 enum AFB_session_v1 session; /* authorisation and session requirements of the verb */
934 void (*callback)(struct afb_req req); /* callback function implementing the verb */
935 const char *info; /* textual description of the verb */
939 <p>For technical reasons, the enumeration <strong>enum AFB_session_v1</strong> is not exactly an
940 enumeration but the wrapper of constant definitions that can be mixed using bitwise or
941 (the C operator |).</p>
943 <p>The constants that can bit mixed are:</p>
948 <th>Constant name </th>
954 <td><strong>AFB_SESSION_CREATE</strong> </td>
955 <td> Equals to AFB_SESSION_LOA_EQ_0|AFB_SESSION_RENEW</td>
958 <td><strong>AFB_SESSION_CLOSE</strong> </td>
959 <td> Closes the session after the reply and set the LOA to 0</td>
962 <td><strong>AFB_SESSION_RENEW</strong> </td>
963 <td> Refreshes the token of authentification</td>
966 <td><strong>AFB_SESSION_CHECK</strong> </td>
967 <td> Just requires the token authentification</td>
970 <td><strong>AFB_SESSION_LOA_LE_0</strong> </td>
971 <td> Requires the current LOA to be lesser then or equal to 0</td>
974 <td><strong>AFB_SESSION_LOA_LE_1</strong> </td>
975 <td> Requires the current LOA to be lesser then or equal to 1</td>
978 <td><strong>AFB_SESSION_LOA_LE_2</strong> </td>
979 <td> Requires the current LOA to be lesser then or equal to 2</td>
982 <td><strong>AFB_SESSION_LOA_LE_3</strong> </td>
983 <td> Requires the current LOA to be lesser then or equal to 3</td>
986 <td><strong>AFB_SESSION_LOA_GE_0</strong> </td>
987 <td> Requires the current LOA to be greater then or equal to 0</td>
990 <td><strong>AFB_SESSION_LOA_GE_1</strong> </td>
991 <td> Requires the current LOA to be greater then or equal to 1</td>
994 <td><strong>AFB_SESSION_LOA_GE_2</strong> </td>
995 <td> Requires the current LOA to be greater then or equal to 2</td>
998 <td><strong>AFB_SESSION_LOA_GE_3</strong> </td>
999 <td> Requires the current LOA to be greater then or equal to 3</td>
1002 <td><strong>AFB_SESSION_LOA_EQ_0</strong> </td>
1003 <td> Requires the current LOA to be equal to 0</td>
1006 <td><strong>AFB_SESSION_LOA_EQ_1</strong> </td>
1007 <td> Requires the current LOA to be equal to 1</td>
1010 <td><strong>AFB_SESSION_LOA_EQ_2</strong> </td>
1011 <td> Requires the current LOA to be equal to 2</td>
1014 <td><strong>AFB_SESSION_LOA_EQ_3</strong> </td>
1015 <td> Requires the current LOA to be equal to 3</td>
1021 <p>If any of this flags is set, afb-daemon requires the token authentification
1022 as if the flag <strong>AFB_SESSION_CHECK</strong> had been set.</p>
1024 <p>The special value <strong>AFB_SESSION_NONE</strong> is zero and can be used to avoid any check.</p>
1026 <blockquote><p>Note that <strong>AFB_SESSION_CREATE</strong> and <strong>AFB_SESSION_CLOSE</strong> might be removed in later versions.</p></blockquote>
1028 <a name="Sending.messages.to.the.log.system"></a>
1029 <h2>Sending messages to the log system</h2>
1031 <p>Afb-daemon provides 4 levels of verbosity and 5 verbs for logging messages.</p>
1033 <p>The verbosity is managed. Options allow the change the verbosity of afb-daemon
1034 and the verbosity of the plugins can be set plugin by plugin.</p>
1036 <p>The verbs for logging messages are defined as macros that test the
1037 verbosity level and that call the real logging function only if the
1038 message must be output. This avoid evaluation of arguments of the
1039 formatting messages if the message must not be output.</p>
1041 <a name="Verbs.for.logging.messages"></a>
1042 <h3>Verbs for logging messages</h3>
1044 <p>The 5 logging verbs are:</p>
1050 <th style="text-align:center;"> Verbosity </th>
1052 <th style="text-align:center;"> syslog level</th>
1058 <td style="text-align:center;"> 0 </td>
1059 <td> Error conditions </td>
1060 <td style="text-align:center;"> 3</td>
1064 <td style="text-align:center;"> 1 </td>
1065 <td> Warning conditions </td>
1066 <td style="text-align:center;"> 4</td>
1070 <td style="text-align:center;"> 1 </td>
1071 <td> Normal but significant condition </td>
1072 <td style="text-align:center;"> 5</td>
1076 <td style="text-align:center;"> 2 </td>
1077 <td> Informational </td>
1078 <td style="text-align:center;"> 6</td>
1082 <td style="text-align:center;"> 3 </td>
1083 <td> Debug-level messages </td>
1084 <td style="text-align:center;"> 7</td>
1090 <p>You can note that the 2 verbs <strong>WARNING</strong> and <strong>INFO</strong> have the same level
1091 of verbosity. But they don’t have the same <em>syslog level</em>. It means that
1092 they are output with a different level on the logging system.</p>
1094 <p>All of these verbs have the same signature:</p>
1096 <pre><code>void ERROR(const struct AFB_interface *afbitf, const char *message, ...);
1099 <p>The first argument <strong>afbitf</strong> is the interface to afb daemon that the
1100 plugin received at its initialisation when <strong>pluginAfbV1Register</strong> was called.</p>
1102 <p>The second argument <strong>message</strong> is a formatting string compatible with printf/sprintf.</p>
1104 <p>The remaining arguments are arguments of the formating message like for printf.</p>
1106 <a name="Managing.verbosity"></a>
1107 <h3>Managing verbosity</h3>
1109 <p>Depending on the level of verbosity, the messages are output or not.
1110 The following table explains what messages will be output depending
1111 ont the verbosity level.</p>
1116 <th style="text-align:center;">Level of verbosity </th>
1117 <th> Outputed macro</th>
1122 <td style="text-align:center;">0 </td>
1126 <td style="text-align:center;">1 </td>
1127 <td> ERROR + WARNING + NOTICE</td>
1130 <td style="text-align:center;">2 </td>
1131 <td> ERROR + WARNING + NOTICE + INFO</td>
1134 <td style="text-align:center;">3 </td>
1135 <td> ERROR + WARNING + NOTICE + INFO + DEBUG</td>
1141 <a name="Output.format.and.destination"></a>
1142 <h3>Output format and destination</h3>
1144 <p>The syslog level is used for forging a prefix to the message.
1145 The prefixes are:</p>
1150 <th style="text-align:center;">syslog level </th>
1156 <td style="text-align:center;">0 </td>
1157 <td> <0> EMERGENCY</td>
1160 <td style="text-align:center;">1 </td>
1164 <td style="text-align:center;">2 </td>
1165 <td> <2> CRITICAL</td>
1168 <td style="text-align:center;">3 </td>
1172 <td style="text-align:center;">4 </td>
1173 <td> <4> WARNING</td>
1176 <td style="text-align:center;">5 </td>
1177 <td> <5> NOTICE</td>
1180 <td style="text-align:center;">6 </td>
1184 <td style="text-align:center;">7 </td>
1191 <p>The message is issued to the standard error.
1192 The final destination of the message depends on how the systemd service
1193 was configured through the variable <strong>StandardError</strong>: It can be
1194 journal, syslog or kmsg. (See man sd-daemon).</p>
1196 <a name="Sending.events"></a>
1197 <h2>Sending events</h2>
1199 <a name="Writing.an.asynchronous.verb.implementation"></a>
1200 <h2>Writing an asynchronous verb implementation</h2>
1202 <a name="How.to.build.a.plugin"></a>
1203 <h2>How to build a plugin</h2>
1205 <p>Afb-daemon provides a <em>pkg-config</em> configuration file that can be
1206 queried by the name <strong>afb-daemon</strong>.
1207 This configuration file provides data that should be used
1208 for compiling plugins. Examples:</p>
1210 <pre><code>$ pkg-config --cflags afb-daemon
1211 $ pkg-config --libs afb-daemon
1214 <a name="Example.for.cmake.meta.build.system"></a>
1215 <h3>Example for cmake meta build system</h3>
1217 <p>This example is the extract for building the plugin <em>afm-main</em> using <em>CMAKE</em>.</p>
1219 <pre><code>pkg_check_modules(afb afb-daemon)
1221 message(STATUS "Creation afm-main-plugin for AFB-DAEMON")
1222 add_library(afm-main-plugin MODULE afm-main-plugin.c)
1223 target_compile_options(afm-main-plugin PRIVATE ${afb_CFLAGS})
1224 target_include_directories(afm-main-plugin PRIVATE ${afb_INCLUDE_DIRS})
1225 target_link_libraries(afm-main-plugin utils ${afb_LIBRARIES})
1226 set_target_properties(afm-main-plugin PROPERTIES
1228 LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-plugin.export-map"
1230 install(TARGETS afm-main-plugin LIBRARY DESTINATION ${plugin_dir})
1232 message(STATUS "Not creating the plugin for AFB-DAEMON")
1236 <p>Let now describe some of these lines.</p>
1238 <pre><code>pkg_check_modules(afb afb-daemon)
1241 <p>This first lines searches to the <em>pkg-config</em> configuration file for
1242 <strong>afb-daemon</strong>. Resulting data are stored in the following variables:</p>
1254 <td> Set to 1 if afb-daemon plugin development files exist</td>
1257 <td>afb_LIBRARIES </td>
1258 <td> Only the libraries (w/o the ‘-l’) for compiling afb-daemon plugins</td>
1261 <td>afb_LIBRARY_DIRS </td>
1262 <td> The paths of the libraries (w/o the ‘-L’) for compiling afb-daemon plugins</td>
1265 <td>afb_LDFLAGS </td>
1266 <td> All required linker flags for compiling afb-daemon plugins</td>
1269 <td>afb_INCLUDE_DIRS </td>
1270 <td> The ‘-I’ preprocessor flags (w/o the ‘-I’) for compiling afb-daemon plugins</td>
1273 <td>afb_CFLAGS </td>
1274 <td> All required cflags for compiling afb-daemon plugins</td>
1280 <p>If development files are found, the plugin can be added to the set of
1281 target to build.</p>
1283 <pre><code>add_library(afm-main-plugin MODULE afm-main-plugin.c)
1286 <p>This line asks to create a shared library having only the
1287 source file afm-main-plugin.c (that is compiled).
1288 The default name of the created shared object is
1289 <strong>libafm-main-plugin.so</strong>.</p>
1291 <pre><code>set_target_properties(afm-main-plugin PROPERTIES
1293 LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/afm-main-plugin.export-map"
1297 <p>This lines are doing two things:</p>
1300 <li><p>It renames the built library from <strong>libafm-main-plugin.so</strong> to <strong>afm-main-plugin.so</strong>
1301 by removing the implicitely added prefix <em>lib</em>. This step is not mandatory
1302 at all because afb-daemon doesn’t check names of files when loading it.
1303 The only convention that use afb-daemon is that extension is <strong>.so</strong>
1304 but this convention is used only when afb-daemon discovers plugin
1305 from a directory hierarchy.</p></li>
1306 <li><p>It applies a version script at link to only export the conventional name
1307 of the entry point: <strong>pluginAfbV1Register</strong>. See below. By default, the linker
1308 that creates the shared object exports all the public symbols (C functions that
1309 are not <strong>static</strong>).</p></li>
1313 <p>Next line are:</p>
1315 <pre><code>target_include_directories(afm-main-plugin PRIVATE ${afb_INCLUDE_DIRS})
1316 target_link_libraries(afm-main-plugin utils ${afb_LIBRARIES})
1319 <p>As you can see it uses the variables computed by <strong><em>pkg_check_modules(afb afb-daemon)</em></strong>
1320 to configure the compiler and the linker.</p>
1322 <a name="Exporting.the.function.pluginAfbV1Register"></a>
1323 <h3>Exporting the function pluginAfbV1Register</h3>
1325 <p>The function <strong>pluginAfbV1Register</strong> must be exported. This can be achieved
1326 using a version script when linking. Here is the version script that is
1327 used for <em>tic-tac-toe</em> (plugins/samples/export.map).</p>
1329 <pre><code>{ global: pluginAfbV1Register; local: *; };
1332 <p>This sample <a href="https://sourceware.org/binutils/docs-2.26/ld/VERSION.html#VERSION">version script</a>
1333 exports as global the symbol <em>pluginAfbV1Register</em> and hides any
1336 <p>This version script is added to the link options using the
1337 option <strong>–version-script=export.map</strong> is given directly to the
1338 linker or using th option <strong>-Wl,–version-script=export.map</strong>
1339 when the option is given to the C compiler.</p>
1341 <a name="Building.within.yocto"></a>
1342 <h3>Building within yocto</h3>
1344 <p>Adding a dependency to afb-daemon is enough. See below:</p>
1346 <pre><code>DEPENDS += " afb-daemon "