afb-apiset: Fix start of apis
[src/app-framework-binder.git] / docs / afb-migration-to-binding-v3.md
1 Migration to binding V3
2 =======================
3
4 The ***binding*** interface evolved from version 1 to version 2
5 for the following reasons:
6
7 - integration of the security requirements within the bindings
8 - simplification of the API (after developer feedbacks)
9 - removal of obscure features and cleanup
10
11 The ***binder*** can run ***bindings*** v1, v2 and/or v3 in any combination.  
12 Thus moving from v1 or v2 to v3 is not enforced at this time. But ...
13
14 In the face to face meeting in Karlsruhe it was decided to remove support
15 of bindings v1 and to deprecate the use of bindings v2.
16
17 So at the end, **IT IS HIGHLY NEEDED TO SWITCH TO VERSION 3**
18
19 This guide covers the migration of bindings from version 2 to version 3.
20
21 The migration from version 1 is not treated here because bindings version 1
22 are very old and probably do not exist anymore. If needed you can refer
23 to the old [guide to migrate bindings from v1 to v2](legacy/afb-migration-v1-to-v2.html).
24
25
26 Differences between version 2 and version 3
27 -------------------------------------------
28
29 ### in v3 all is api
30
31 The version 3 introduces the concept of "API" that gather what was called before
32 the daemon and the service. This is the new concept that predates the 2 others.
33
34 The concept of API is intended to allow the definition of multiple APIs
35 by a same "binding" (a dynamically loaded library).
36
37 Because there is potentially several "API", the functions that were without
38 context in bindings version 2 need now to tell what API is consumer.
39
40 To be compatible with version 2, bindings v3 still have a default hidden
41 context: the default API named **afbBindingV3root**.
42
43 To summarize, the functions of class **daemon** and **service** use the default
44 hidden API.
45
46 It is encouraged to avoid use of functions of class **daemon** and **service**.
47 You should replace these implicit calls to explicit **api** calls that 
48 reference **afbBindingV3root**.
49
50 Same thing for the logging macros: **AFB_ERROR**, **AFB_WARNING**,
51 **AFB_NOTICE**, **AFB_INFO**, **AFB_DEBUG** that becomes respectively
52 **AFB_API_ERROR**, **AFB_API_WARNING**, **AFB_API_NOTICE**, **AFB_API_INFO**,
53 **AFB_API_DEBUG**.
54
55 Example of 2 equivalent writes:
56
57 ```C
58         AFB_NOTICE("send stress event");
59         afb_daemon_broadcast_event(stressed_event, NULL);
60 ```
61
62 or 
63
64 ```C
65         AFB_API_NOTICE(afbBindingV3root, "send stress event");
66         afb_api_broadcast_event(afbBindingV3root, stressed_event, NULL);
67 ```
68
69 ### the reply mechanism predates success and fail
70
71 ### subcall has more power
72
73 Task list for the migration
74 ---------------------------
75
76 This task list is:
77
78 1. Use the automatic migration procedure described below
79 2. Adapt the functions **preinit**, **init** and **onevent**
80 3. Consider use of the new reply
81 4. Consider use of the new (sub)call
82 5. Consider use of event handlers
83
84 The remaining chapters explain these task with more details.
85
86 Automatic migration!
87 --------------------
88
89 A tiny **sed** script is intended to perform a first pass on the code that
90 you want to upgrade. It can be done using **curl** and applied using **sed**
91 as below.
92
93 ```bash
94 BASE=https://git.automotivelinux.org/src/app-framework-binder/plain
95 SED=migration-to-binding-v3.sed
96 curl -o $SED $BASE/docs/$SED
97 sed -i -f $SED file1 file2 file3...
98 ```
99
100 You can also follow
101 [this link](https://git.automotivelinux.org/src/app-framework-binder/plain/docs/migration-to-binding-v3.sed)
102 and save the file.
103
104 This automatic action does most of the boring job but not all the job.
105 The remaining of this guide explains the missing part.
106
107 Adapt the functions preinit, init and onevent
108 ----------------------------------------------
109
110 The signature of the functions **preinit**, **init** and **onevent** changed
111 to include the target api.
112
113 The functions of the v2:
114
115 ```C
116 int (*preinit)();
117 int (*init)();
118 void (*onevent)(const char *event, struct json_object *object);
119 ```
120
121 Gain a new first argument of type **afb_api_t** as below:
122
123 ```C
124 int (*preinit)(afb_api_t api);
125 int (*init)(afb_api_t api);
126 void (*onevent)(afb_api_t api, const char *event, struct json_object *object);
127 ```
128
129 For the migration, it is enough to just add the new argument without
130 using it.
131
132 Consider use of the new reply
133 -----------------------------
134
135 The v3 allows error reply with JSON object. To achieve it, an unified
136 reply function's family is introduced:
137
138 ```C
139 void afb_req_reply(afb_req_t req, json_object *obj, const char *error, const char *info);
140 void afb_req_reply_v(afb_req_t req, json_object *obj, const char *error, const char *info, va_list args);
141 void afb_req_reply_f(afb_req_t req, json_object *obj, const char *error, const char *info, ...);
142 ```
143
144 The functions **success** and **fail** are still supported.
145 These functions are now implemented as the following macros:
146
147
148 ```C
149 #define afb_req_success(r,o,i)          afb_req_reply(r,o,NULL,i)
150 #define afb_req_success_f(r,o,...)      afb_req_reply_f(r,o,NULL,__VA_ARGS__)
151 #define afb_req_success_v(r,o,f,v)      afb_req_reply_v(r,o,NULL,f,v)
152 #define afb_req_fail(r,e,i)             afb_req_reply(r,NULL,e,i)
153 #define afb_req_fail_f(r,e,...)         afb_req_reply_f(r,NULL,e,__VA_ARGS__)
154 #define afb_req_fail_v(r,e,f,v)         afb_req_reply_v(r,NULL,e,f,v)
155 ```
156
157 This is a decision of the developer to switch to the new family
158 **afb_req_reply** or to keep the good old functions **afb_req_fail**
159 adn **afb_req_success**.
160
161 Consider use of the new (sub)call
162 ---------------------------------
163
164 The new call and subcall (the functions **afb_api_call**, **afb_api_call_sync**,
165 **afb_req_subcall** and **afb_req_subcall_sync**) functions are redesigned
166 to better fit the new reply behaviour. In most case the developer will benefit
167 of the new behavior that directly gives result and error without enforcing
168 to parse the JSON object result.
169
170 The subcall functions are also fully redesigned to allow precise handling
171 of the context and event subscriptions. The new design allows you to specify:
172
173  - whether the subcall is made in the session of the caller or in the session
174    of the service
175  - whether the credentials to use are those of the caller or those of the
176    service
177  - whether the caller or the service or both or none will receive the
178    eventually events during the subcall.
179
180 See [calls](reference-v3/func-api.html#calls-and-job-functions) and
181 [subcalls](reference-v3/func-req.html#subcall-functions).
182
183 The table below list the changes to apply:
184
185 | Name in Version 2      | New name of Version 3
186 |:----------------------:|:----------------------------------------------------:
187 | afb_req_subcall        | afb_req_subcall_legacy
188 | afb_req_subcall_sync   | afb_req_subcall_sync_legacy
189 | afb_service_call       | afb_service_call_legacy
190 | afb_service_call_sync  | afb_service_call_sync_legacy
191 | afb_req_subcall_req    | afb_req_subcall_req (same but obsolete)
192
193
194 Consider use of event handlers
195 ------------------------------
196
197 Binding V3 brings new ways of handling event in services. You can register
198 functions that will handle specific events and that accept closure arguments.
199
200 See [**afb_api_event_handler_add** and **afb_api_event_handler_del**](reference-v3/func-api.html#event-functions)