aboutsummaryrefslogtreecommitdiffstats
path: root/docs/afb-migration-v1-to-v2.md
blob: 9a43454e992411af0075b89cb09750f3c50f665d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
[LEGACY] Migration from binding V1 to binding V2
=======================================

> LEGACY!!! IT IS NOT EXPECTED THAT YOU STILL NEED THIS GUIDE.
>
> THIS GUIDE WILL BE REMOVED IN A NEAR FUTURE


The ***binding*** interface evolved from version 1 to version 2
for the following reasons:

- integration of the security requirements within the bindings
- simplification of the API (after developer feedbacks)
- removal of obscure features, cleanup

The ***binder*** can run ***bindings*** v1 and/or v2 in any combination.  
Thus moving from v1 to v2 is not enforced, there is no real need.

More, it is possible to write a dual ***binding***:

- a ***binding*** that implements the version 1 and the version 2.

However, IT IS HIGHLY RECOMMENDED TO SWITCH TO ONLY VERSION 2:

- any new development SHOULD start using ***binding*** V2
- existing ***bindings*** SHOULD migrate to the version 2

This guide covers the migration of bindings from version 1 to version 2.

It also explains some of the rationale taken when migrating from version 1 to version 2.

In the future, if ***binding*** api evolves to fresh versions (3, 4, ...)
it might be necessarily to write bindings implementing more than
just one version.  
For example:

- a ***binding*** being v2 AND v3 will resolve the issue of running on older and newer version of AGL. 

This should always be possible even if more complicated.

Important things to known when migrating
----------------------------------------

One of the most important change when migrating from v1 to v2 is
that many functions use an hidden *common* variable.  
This affects the functions of the following classes:

- functions of class **daemon**:
  - functions starting with **afb_daemon_...**
  - functions for logging: **ERROR**, **WARNING**, **NOTICE**, **INFO**, **DEBUG**
- functions of class **service**:
  - functions starting with **afb_service_...**
- callback functions:
  - the register function (that is removed)
  - the service init function
  - the onevent function

For these functions, the first parameter is now implicit.

Let takes an example.  
For ***binding*** v1 you had to write:

```C
        afb_daemon_broadcast_event(afbitf->daemon, reason, description);
```

For ***binding*** v2, you simply write:

```C
        afb_daemon_broadcast_event(reason, description);
```

This simplification is possible because the header files included for the bindings
now provide a common variable for storing the **daemon** and **service** data.

As a programmer, you shouldn't care much about that hidden variable.  
It simplifies the job, that's all and that is the reason of the change.

An other important difference is between the version 1 and the version 2 is
on how the ***binding***'s **API** is documented.  
The version 2 emphasis the **OpenAPI v3** description of the **API**.  
For this reason, to avoid duplication of descriptions, only one description is expected:

- The **OpenAPI** one.

Task list for the migration
---------------------------

This task list is:

1. Enforce use of binding v2 by setting **AFB_BINDING_VERSION**
1. Rewrite the main structure and the list of exported verbs
1. Adapt the init and callback functions
1. Removes the first parameter of functions of classes **daemon** and **service**
1. Consider where to emit logs for requests
1. Take care of store/unstore changes
1. Consider use of synchronous (sub)call requests
1. Optionally, removes explicit struct

The remaining chapters explain these task with more details.

Enforce use of binding v2 by setting AFB_BINDING_VERSION
--------------------------------------------------------

By defining **AFB_BINDING_VERSION** to **2** you switch to version 2.  
This is done as below.

```C
#define AFB_BINDING_VERSION 2
#include <afb/afb-binding.h>
```

After that you will get many errors when compiling.

Rewrite the main structures and the list of exported verbs
---------------------------------------------------------

The structures describing the ***binding** changed from version 1 to version 2.

The structure for describing verbs changed to include security
requirements.

In version 1 it was:

```C
struct afb_verb_desc_v1
{
       const char *name;                       /* name of the verb */
       enum afb_session_flags_v1 session;      /* authorization and session requirements of the verb */
       void (*callback)(struct afb_req req);   /* callback function implementing the verb */
       const char *info;                       /* textual description of the verb */
};
```

In version 2 it becomes:

```C
struct afb_verb_v2
{
        const char *verb;                       /* name of the verb */
        void (*callback)(struct afb_req req);   /* callback function implementing the verb */
        const struct afb_auth *auth;            /* required authorization */
        uint32_t session;                       /* authorization and session requirements of the verb */
};

```

The migration of instances of that structure requires the following actions:

- rename field **name** to **verb**
- remove field **info**
- adapt field **session** if needed
- set field **auth** to NULL

Example:

```C
 { .name= "new",   .session= AFB_SESSION_NONE, .callback= new,   .info= "Starts a new game" }
```

Becomes

```C
 { .verb = "new",   .session = AFB_SESSION_NONE, .callback = new, .auth = NULL }
```

The field **auth** can be set to a value describing the requested
authorization.

The main describing structure also changed.

In version 1 it was:

```C
struct afb_binding_desc_v1
{
       const char *info;                       /* textual information about the binding */
       const char *prefix;                     /* required prefix name for the binding */
       const struct afb_verb_desc_v1 *verbs;   /* array of descriptions of verbs terminated by a NULL name */
};
```

In version 2 it becomes:

```C
struct afb_binding_v2
{
        const char *api;                        /* api name for the binding */
        const char *specification;              /* textual specification of the binding */
        const struct afb_verb_v2 *verbs;        /* array of descriptions of verbs terminated by a NULL name */
        int (*preinit)();                       /* callback at load of the binding */
        int (*init)();                          /* callback for starting the service */
        void (*onevent)(const char *event, struct json_object *object); /* callback for handling events */
        unsigned noconcurrency: 1;              /* avoids concurrent requests to verbs */
};
```

The migration of instances of that structure requires the following actions:

- declare, explore, name the structure as ```const struct afb_binding_v2 afbBindingV2```
- rename the field **prefix** to **api**
- remove the field **info**
- setup the fields **preinit**, **init**, **onevent** according to the next section
- set the field **noconcurrency** to the right value:
  - to 1 if you want to avoid concurrent calls to verbs.
  - to 0 if you allow concurrent calls to verbs.

Example:

```C
static const struct afb_binding plugin_desc = {
        .type = AFB_BINDING_VERSION_1,
        .v1 = {
                .info = "Minimal Hello World Sample",
                .prefix = "hello",
                .verbs = verbs
        }
```

Becomes:

```C
const struct afb_binding_v2 afbBindingV2 = {
        .api = "hello",
        .specification = NULL,
        .verbs = verbs,
        .preinit = preinit,
        .init = init
};
```

The **binder** now relies only on the exported names
to deduce the type of the binding.  
This make the main structure more simple.

Adapt the init and callback functions
-------------------------------------

The ***bindings*** version 1 defined 3 exported functions:

- **afbBindingV1Register**
- **afbBindingV1ServiceInit**
- **afbBindingV1ServiceEvent**

These function should not be exported any more and there definition changed.

The function **afbBindingV1Register** is no more used to describe the binding.  
When a binding has to take actions when it is loaded, it must set the field
**preinit** of the structure **afbBindingV2**.  
This field, this preinit, might be used to check features at load.  
When it returns a negative number, the ***binder*** stops before initializing any ***binding***.

The function **afbBindingV1ServiceInit** is replaced by the field **init**
of the structure **afbBindingV2**.  
The init function should return 0 in case of success or a negative error code
in case of problem.  
It is called during initialization of services.

The function **afbBindingV1ServiceEvent**is replaced by the field **onevent**
of the structure **afbBindingV2**.

The two functions **afbBindingV1Register** and **afbBindingV1ServiceInit**,
were taking as parameter the ***binder*** interface and the service interface respectively.  
These interfaces are now managed hiddenly for the **binding** by the **binder**.  
So the variable that ***bindings*** version used to store the ***binder*** interface
and the service interface are no more needed and can be removed.

Example:

```C
const struct afb_binding_interface *interface;
struct afb_service service;

static const struct afb_binding plugin_desc = {
        .type = AFB_BINDING_VERSION_1,
        .v1 = {
                .info = "Minimal Hello World Sample",
                .prefix = "hello",
                .verbs = verbs
        }
}

const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
{
        interface = itf;
        NOTICE(interface, "binding register");
        return &plugin_desc;
}

int afbBindingV1ServiceInit(struct afb_service svc)
{
        service = svc;
        NOTICE(interface, "binding init");
        return 0;
}

void afbBindingV1ServiceEvent(const char *event, struct json_object *object)
{
        NOTICE(interface, "onevent %s", event);
}
```

Becomes:

```C
static int preinit()
{
        AFB_NOTICE("binding preinit (was register)");
        return 0;
}

static int init()
{
        AFB_NOTICE("binding init");
        return 0;
}

static void onevent(const char *event, struct json_object *object)
{
        AFB_NOTICE("onevent %s", event);
}

const struct afb_binding_v2 afbBindingV2 = {
        .api = "hello",
        .specification = NULL,
        .verbs = verbs,
        .preinit = preinit,
        .init = init,
        .onevent = onevent
};
```

The two functions **afbBindingV1Register** and **afbBindingV1ServiceInit**,
were taking as parameter the ***binder*** interface and the service interface respectively.  
These interfaces are now managed hiddenly for the **binding** by the **binder**.  
So the variable that ***bindings*** version used to store the ***binder*** interface
and the service interface are no more needed and can be removed.

On the above example the following lines were removed:

```C
const struct afb_binding_interface *interface;
struct afb_service service;

        interface = itf;

        service = svc;
```

Removes the first parameter of functions of classes **daemon** and **service**
------------------------------------------------------------------------------

As explained before, many functions loose there first
arguments, this are the functions of the following classes:

- functions of class **daemon**:
  - functions starting with **afb_daemon_...**
  - functions for logging: **ERROR**, **WARNING**, **NOTICE**, **INFO**, **DEBUG**
- functions of class **service**:
  - functions starting with **afb_service_...**
- callback functions:
  - the register function (that is removed)
  - the service init function
  - the onevent function

For these functions, the first parameter is now implicit.

Example:

```C
        afb_daemon_broadcast_event(afbitf->daemon, reason, description);
```

Becomes:

```C
        afb_daemon_broadcast_event(reason, description);
```

Also, to avoid possible conflicts, we introduced prefixed logging functions:  
the macros 

- **ERROR**
- **WARNING**
- **NOTICE**
- **INFO**
- **DEBUG** 

have now a prefixed version: 

- **AFB\_ERROR**
- **AFB\_WARNING**
- **AFB\_NOTICE**
- **AFB\_INFO**
- **AFB\_DEBUG**

It is now recommended to use the prefixed version.

Example:

```C
        NOTICE(interface, "hello plugin comes to live");
```

Become:

```C
        NOTICE("hello plugin comes to live");
```

or, better:

```C
        AFB_NOTICE("hello plugin comes to live");
```

To remove definition of the un-prefixed versions of logging macros:

- **ERROR**
- **WARNING**
- **NOTICE**
- **INFO**
- **DEBUG** 

and just define 

- **AFB_BINDING_PRAGMA_NO_VERBOSE_UNPREFIX**

before to include **afb/afb-binding.h**.

```C
#define AFB_BINDING_PRAGMA_NO_VERBOSE_UNPREFIX
#define AFB_BINDING_VERSION 2
#include <afb/afb-binding.h>
```

Consider where to emit logs for requests
----------------------------------------

The ***bindings*** v2 now allows to emit log messages associated to ***requests***.  
This feature is valuable when debugging because it allows to return
side information associated to a ***request***.

The defined macros for logging to requests are: 

- **AFB_REQ_ERROR**
- **AFB_REQ_WARNING**
- **AFB_REQ_NOTICE**
- **AFB_REQ_INFO**
- **AFB_REQ_DEBUG**

We encourage the use of these new logging facilities everywhere it makes sense.

Example:

```C
        INFO(afbitf, "method 'new' called for boardid %d", board->id);
```

Might become:

```C
        AFB_REQ_INFO(req, "method 'new' called for boardid %d", board->id);
```

Take care of store/unstore change
---------------------------------

For efficiency, the version 2 redefined how storing/un-storing of
requests works.  
Storing request is needed for asynchronous handling of requests.

For ***bindings*** version, the signature of the functions were:

```C
struct afb_req *afb_req_store(struct afb_req req);
struct afb_req afb_req_unstore(struct afb_req *req);
```

For version 2 it becomes

```C
struct afb_stored_req *afb_req_store(struct afb_req req);
struct afb_req afb_req_unstore(struct afb_stored_req *sreq);
```

Where the structure ```struct afb_stored_req``` is opaque.

It should require few code change.

Also check the following chapter that explain that asynchronous (sub)calls
can be replaced by synchronous one, avoiding the need to store/unstore
requests.

Consider use of synchronous (sub)call requests
----------------------------------------------

***Bindings*** can emit requests for themselves (calls) or for
their clients (subcalls).  
With ***bindings*** version 2 comes also synchronous requests for both cases.

So when migrating to bindings version 2, a developer can consider
to replace the asynchronous requests (with asynchronous call back)
by synchronous ones.

See functions ***afb_service_call_sync*** and ***afb_req_subcall_sync***.

Optionally, removes explicit struct
-----------------------------------

The new definitions now includes **typedefs** for common
structures, as shown on below sample:

```C
typedef struct afb_daemon               afb_daemon;
typedef struct afb_event                afb_event;
typedef struct afb_arg                  afb_arg;
typedef struct afb_req                  afb_req;
typedef struct afb_service              afb_service;
```

So you can remove the keyword **struct** if it bores you.

Example:

```C
static void verb(struct afb_req req)
{
        ...
}
```

Might become:

```C
static void verb(afb_req req)
{
        ...
}
```

Example of migration
--------------------

The first ***binding*** that migrated from v1 to v2 was the sample **HelloWorld**.  
Here is shown the differences between the version 1 and the version 2.

```diff
diff --git a/bindings/samples/HelloWorld.c b/bindings/samples/HelloWorld.c
index c6fa779..505aee3 100644
--- a/bindings/samples/HelloWorld.c
+++ b/bindings/samples/HelloWorld.c
@@ -21,9 +21,9 @@

 #include <json-c/json.h>

+#define AFB_BINDING_VERSION 2
 #include <afb/afb-binding.h>

-const struct afb_binding_interface *interface;
 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

 struct event
@@ -79,7 +80,7 @@ static int event_add(const char *tag, const char *name)
         strcpy(e->tag, tag);

         /* make the event */
-    e->event = afb_daemon_make_event(interface->daemon, name);
+    e->event = afb_daemon_make_event(name);
         if (!e->event.closure) { free(e); return -1; }

         /* link */
@@ -140,7 +141,7 @@ static void pingBug (struct afb_req request)
 static void pingEvent(struct afb_req request)
 {
         json_object *query = afb_req_json(request);
-    afb_daemon_broadcast_event(interface->daemon, "event", json_object_get(query));
+    afb_daemon_broadcast_event("event", json_object_get(query));
         ping(request, json_object_get(query), "event");
 }

@@ -288,38 +289,43 @@ static void exitnow (struct afb_req request)
         exit(0);
 }

+static int preinit()
+{
+    AFB_NOTICE("hello binding comes to live");
+    return 0;
+}
+
+static int init()
+{
+    AFB_NOTICE("hello binding starting");
+    return 0;
+}
+
 // NOTE: this sample does not use session to keep test a basic as possible
 //       in real application most APIs should be protected with AFB_SESSION_CHECK
-static const struct afb_verb_desc_v1 verbs[]= {
-  {"ping"     , AFB_SESSION_NONE, pingSample  , "Ping Application Framework"},
-  {"pingfail" , AFB_SESSION_NONE, pingFail    , "Fails"},
-  {"pingnull" , AFB_SESSION_NONE, pingNull    , "Return NULL"},
-  {"pingbug"  , AFB_SESSION_NONE, pingBug     , "Do a Memory Violation"},
-  {"pingJson" , AFB_SESSION_NONE, pingJson    , "Return a JSON object"},
-  {"pingevent", AFB_SESSION_NONE, pingEvent   , "Send an event"},
-  {"subcall",   AFB_SESSION_NONE, subcall     , "Call api/verb(args)"},
-  {"subcallsync",   AFB_SESSION_NONE, subcallsync     , "Call api/verb(args)"},
-  {"eventadd",  AFB_SESSION_NONE, eventadd    , "adds the event of 'name' for the 'tag'"},
-  {"eventdel",  AFB_SESSION_NONE, eventdel    , "deletes the event of 'tag'"},
-  {"eventsub",  AFB_SESSION_NONE, eventsub    , "subscribes to the event of 'tag'"},
-  {"eventunsub",AFB_SESSION_NONE, eventunsub  , "unsubscribes to the event of 'tag'"},
-  {"eventpush", AFB_SESSION_NONE, eventpush   , "pushs the event of 'tag' with the 'data'"},
-  {"exit",      AFB_SESSION_NONE, exitnow     , "exits from afb-daemon"},
-  {NULL}
+static const struct afb_verb_v2 verbs[]= {
+  { "ping"     ,    pingSample , NULL, AFB_SESSION_NONE },
+  { "pingfail" ,    pingFail   , NULL, AFB_SESSION_NONE },
+  { "pingnull" ,    pingNull   , NULL, AFB_SESSION_NONE },
+  { "pingbug"  ,    pingBug    , NULL, AFB_SESSION_NONE },
+  { "pingJson" ,    pingJson   , NULL, AFB_SESSION_NONE },
+  { "pingevent",    pingEvent  , NULL, AFB_SESSION_NONE },
+  { "subcall",      subcall    , NULL, AFB_SESSION_NONE },
+  { "subcallsync",  subcallsync, NULL, AFB_SESSION_NONE },
+  { "eventadd",     eventadd   , NULL, AFB_SESSION_NONE },
+  { "eventdel",     eventdel   , NULL, AFB_SESSION_NONE },
+  { "eventsub",     eventsub   , NULL, AFB_SESSION_NONE },
+  { "eventunsub",   eventunsub , NULL, AFB_SESSION_NONE },
+  { "eventpush",    eventpush  , NULL, AFB_SESSION_NONE },
+  { "exit",         exitnow    , NULL, AFB_SESSION_NONE },
+  { NULL}
 };

-static const struct afb_binding plugin_desc = {
-    .type = AFB_BINDING_VERSION_1,
-    .v1 = {
-        .info = "Minimal Hello World Sample",
-        .prefix = "hello",
-        .verbs = verbs
-    }
+const struct afb_binding_v2 afbBindingV2 = {
+    .api = "hello",
+    .specification = NULL,
+    .verbs = verbs,
+    .preinit = preinit,
+    .init = init
 };

-const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
-{
-    interface = itf;
-    NOTICE(interface, "hello plugin comes to live");
-    return &plugin_desc;
-}
```