Fails write request if CAN signal not writable
[apps/low-level-can-service.git] / low-can-binding / binding / low-can-cb.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  * Author "Loic Collignon" <loic.collignon@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include "low-can-hat.hpp"
20 #include "low-can-apidef.h"
21
22 #include <map>
23 #include <queue>
24 #include <mutex>
25 #include <vector>
26 #include <thread>
27 #include <json-c/json.h>
28 #include <systemd/sd-event.h>
29
30 #include "openxc.pb.h"
31 #include "application.hpp"
32 #include "../can/can-encoder.hpp"
33 #include "../can/can-bus.hpp"
34 #include "../can/can-signals.hpp"
35 #include "../can/can-message.hpp"
36 #include "../utils/signals.hpp"
37 #include "../diagnostic/diagnostic-message.hpp"
38 #include "../utils/openxc-utils.hpp"
39
40 ///******************************************************************************
41 ///
42 ///             SystemD event loop Callbacks
43 ///
44 ///*******************************************************************************/
45
46 void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, uint32_t pid, std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
47 {
48         if( ! can_subscription->get_diagnostic_message().empty() && can_subscription->get_diagnostic_message(pid) != nullptr)
49         {
50                 DiagnosticRequest diag_req = can_subscription->get_diagnostic_message(pid)->build_diagnostic_request();
51                 active_diagnostic_request_t* adr = application_t::instance().get_diagnostic_manager().find_recurring_request(diag_req);
52                 if( adr != nullptr)
53                         application_t::instance().get_diagnostic_manager().cleanup_request(adr, true);
54         }
55
56         on_no_clients(can_subscription, s);
57 }
58
59 void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
60 {
61         auto it = s.find(can_subscription->get_index());
62         s.erase(it);
63 }
64
65 static void push_n_notify(const can_message_t& cm)
66 {
67         can_bus_t& cbm = application_t::instance().get_can_bus_manager();
68         {
69                 std::lock_guard<std::mutex> can_message_lock(cbm.get_can_message_mutex());
70                 cbm.push_new_can_message(cm);
71         }
72         cbm.get_new_can_message_cv().notify_one();
73 }
74
75 int read_message(sd_event_source *event_source, int fd, uint32_t revents, void *userdata)
76 {
77         low_can_subscription_t* can_subscription = (low_can_subscription_t*)userdata;
78         if ((revents & EPOLLIN) != 0)
79         {
80                 can_message_t cm;
81                 utils::socketcan_bcm_t& s = can_subscription->get_socket();
82                 s >> cm;
83
84                 // Sure we got a valid CAN message ?
85                 if(! cm.get_id() == 0 && ! cm.get_length() == 0)
86                         {push_n_notify(cm);}
87         }
88
89         // check if error or hangup
90         if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
91         {
92                 sd_event_source_unref(event_source);
93                 can_subscription->get_socket().close();
94         }
95         return 0;
96 }
97
98 ///******************************************************************************
99 ///
100 ///             Subscription and unsubscription
101 ///
102 ///*******************************************************************************/
103
104 static int make_subscription_unsubscription(struct afb_req request,
105                                                                                         std::shared_ptr<low_can_subscription_t>& can_subscription,
106                                                                                         std::map<int, std::shared_ptr<low_can_subscription_t> >& s,
107                                                                                         bool subscribe)
108 {
109         /* Make the subscription or unsubscription to the event */
110         if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s[can_subscription->get_index()]->get_event())) < 0)
111         {
112                 AFB_ERROR("Operation goes wrong for signal: %s", can_subscription->get_name().c_str());
113                 return -1;
114         }
115         return 0;
116 }
117
118 static int create_event_handle(std::shared_ptr<low_can_subscription_t>& can_subscription,
119                                                         std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
120 {
121         int sub_index = can_subscription->get_index();
122         can_subscription->set_event(afb_daemon_make_event(can_subscription->get_name().c_str()));
123         s[sub_index] = can_subscription;
124         if (!afb_event_is_valid(s[sub_index]->get_event()))
125         {
126                 AFB_ERROR("Can't create an event for %s, something goes wrong.", can_subscription->get_name().c_str());
127                 return -1;
128         }
129         return 0;
130 }
131
132 /// @brief Will determine if it is needed or not to create the event handle and checks it to be sure that
133 /// we got a valid afb_event to get subscribe or unsubscribe. Then launch the subscription or unsubscription
134 /// against the application framework using that event handle.
135 static int subscribe_unsubscribe_signal(struct afb_req request,
136                                                                                 bool subscribe,
137                                                                                 std::shared_ptr<low_can_subscription_t>& can_subscription,
138                                                                                 std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
139 {
140         int ret = -1;
141         int sub_index = can_subscription->get_index();
142
143         if (can_subscription && s.find(sub_index) != s.end())
144         {
145                 if (!afb_event_is_valid(s[sub_index]->get_event()) && !subscribe)
146                 {
147                         AFB_NOTICE("Event isn't valid, no need to unsubscribed.");
148                         ret = -1;
149                 }
150                 ret = 0;
151         }
152         else
153         {
154                 /* Event doesn't exist , so let's create it */
155                 can_subscription->set_event({nullptr, nullptr});
156                 s[sub_index] = can_subscription;
157                 ret = create_event_handle(can_subscription, s);
158         }
159
160         // Check whether or not the event handler has been correctly created and
161         // make the subscription/unsubscription operation is so.
162         if (ret < 0)
163                 return ret;
164         return make_subscription_unsubscription(request, can_subscription, s, subscribe);
165 }
166
167 static int add_to_event_loop(std::shared_ptr<low_can_subscription_t>& can_subscription)
168 {
169                 struct sd_event_source* event_source = nullptr;
170                 return ( sd_event_add_io(afb_daemon_get_event_loop(),
171                         &event_source,
172                         can_subscription->get_socket().socket(),
173                         EPOLLIN,
174                         read_message,
175                         can_subscription.get()));
176 }
177
178 static int subscribe_unsubscribe_diagnostic_messages(struct afb_req request,
179                                                                                                         bool subscribe,
180                                                                                                         std::vector<std::shared_ptr<diagnostic_message_t> > diagnostic_messages,
181                                                                                                         struct event_filter_t& event_filter,
182                                                                                                         std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
183 {
184         int rets = 0;
185         application_t& app = application_t::instance();
186         diagnostic_manager_t& diag_m = app.get_diagnostic_manager();
187
188         for(const auto& sig : diagnostic_messages)
189         {
190                 DiagnosticRequest* diag_req = new DiagnosticRequest(sig->build_diagnostic_request());
191                 event_filter.frequency = std::isnan(event_filter.frequency) ? sig->get_frequency() : event_filter.frequency;
192                 std::shared_ptr<low_can_subscription_t> can_subscription;
193
194                 auto it =  std::find_if(s.begin(), s.end(), [&sig](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub){ return (! sub.second->get_diagnostic_message().empty());});
195                 can_subscription = it != s.end() ?
196                         it->second :
197                         std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
198                 // If the requested diagnostic message isn't supported by the car then unsubcribe it
199                 // no matter what we want, worse case will be a fail unsubscription but at least we don't
200                 // poll a PID for nothing.
201                 //TODO: Adding callback requesting ignition status:     diag_req, sig.c_str(), false, diagnostic_message_t::decode_obd2_response, diagnostic_message_t::check_ignition_status, frequency);
202                 if(sig->get_supported() && subscribe)
203                 {
204                         diag_m.add_recurring_request(diag_req, sig->get_name().c_str(), false, sig->get_decoder(), sig->get_callback(), event_filter.frequency);
205                         if(can_subscription->create_rx_filter(sig) < 0)
206                                 {return -1;}
207                         AFB_DEBUG("Signal: %s subscribed", sig->get_name().c_str());
208                         if(it == s.end() && add_to_event_loop(can_subscription) < 0)
209                         {
210                                 diag_m.cleanup_request(
211                                         diag_m.find_recurring_request(*diag_req), true);
212                                 AFB_WARNING("signal: %s isn't supported. Canceling operation.",  sig->get_name().c_str());
213                                 return -1;
214                         }
215                 }
216                 else
217                 {
218                         if(sig->get_supported())
219                         {AFB_DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str());}
220                         else
221                         {
222                                 AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
223                                 return -1;
224                         }
225                 }
226                 int ret = subscribe_unsubscribe_signal(request, subscribe, can_subscription, s);
227                 if(ret < 0)
228                         return ret;
229
230                 rets++;
231         }
232
233         return rets;
234 }
235
236 // TODO: Create separate subscrition object if event_filter isn't the same.
237 static int subscribe_unsubscribe_can_signals(struct afb_req request,
238                                                                                         bool subscribe,
239                                                                                         std::vector<std::shared_ptr<can_signal_t> > can_signals,
240                                                                                         struct event_filter_t& event_filter,
241                                                                                         std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
242 {
243         int rets = 0;
244         for(const auto& sig: can_signals)
245         {
246                 auto it =  std::find_if(s.begin(), s.end(), [&sig](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub){ return sub.second->get_can_signal() == sig; });
247                 std::shared_ptr<low_can_subscription_t> can_subscription;
248                 if(it != s.end())
249                 {
250                          can_subscription = it->second;
251                 }
252                 else
253                 {
254                          can_subscription = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
255                         if(can_subscription->create_rx_filter(sig) < 0)
256                                 {return -1;}
257                         if(add_to_event_loop(can_subscription) < 0)
258                                 {return -1;}
259                 }
260
261                 if(subscribe_unsubscribe_signal(request, subscribe, can_subscription, s) < 0)
262                         {return -1;}
263
264                 rets++;
265                 AFB_DEBUG("signal: %s subscribed", sig->get_name().c_str());
266         }
267         return rets;
268 }
269
270 ///
271 /// @brief subscribe to all signals in the vector signals
272 ///
273 /// @param[in] afb_req request : contain original request use to subscribe or unsubscribe
274 /// @param[in] subscribe boolean value used to chose between a subscription operation or an unsubscription
275 /// @param[in] signals -  struct containing vectors with can_signal_t and diagnostic_messages to subscribe
276 ///
277 /// @return Number of correctly subscribed signal
278 ///
279 static int subscribe_unsubscribe_signals(struct afb_req request,
280                                                                                 bool subscribe,
281                                                                                 const struct utils::signals_found& signals,
282                                                                                 struct event_filter_t& event_filter)
283 {
284         int rets = 0;
285         utils::signals_manager_t& sm = utils::signals_manager_t::instance();
286
287         std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
288         std::map<int, std::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
289
290         rets += subscribe_unsubscribe_diagnostic_messages(request, subscribe, signals.diagnostic_messages, event_filter, s);
291         rets += subscribe_unsubscribe_can_signals(request, subscribe, signals.can_signals, event_filter, s);
292
293         return rets;
294 }
295
296 static int one_subscribe_unsubscribe(struct afb_req request,
297                                                                         bool subscribe,
298                                                                         const std::string& tag,
299                                                                         json_object* args)
300 {
301         int ret = 0;
302         struct event_filter_t event_filter;
303         struct json_object  *filter, *obj;
304         struct utils::signals_found sf;
305
306         // computes the filter
307         if (json_object_object_get_ex(args, "filter", &filter))
308         {
309                 if (json_object_object_get_ex(filter, "frequency", &obj)
310                 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
311                         {event_filter.frequency = (float)json_object_get_double(obj);}
312                 if (json_object_object_get_ex(filter, "min", &obj)
313                 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
314                         {event_filter.min = (float)json_object_get_double(obj);}
315                 if (json_object_object_get_ex(filter, "max", &obj)
316                 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
317                         {event_filter.max = (float)json_object_get_double(obj);}
318         }
319
320         // subscribe or unsubscribe
321         openxc_DynamicField search_key = build_DynamicField(tag);
322         sf = utils::signals_manager_t::instance().find_signals(search_key);
323         if (sf.can_signals.empty() && sf.diagnostic_messages.empty())
324         {
325                 AFB_NOTICE("No signal(s) found for %s.", tag.c_str());
326                 ret = -1;
327         }
328         else
329                 {ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);}
330
331         return ret;
332 }
333 static int process_one_subscribe_args(struct afb_req request, bool subscribe, json_object *args)
334 {
335         int rc = 0, rc2=0;
336         json_object *x = nullptr, *event = nullptr;
337         if(args == NULL ||
338                 !json_object_object_get_ex(args, "event", &event))
339         {
340                 rc = one_subscribe_unsubscribe(request, subscribe, "*", args);
341         }
342         else if (json_object_get_type(event) != json_type_array)
343         {
344                 rc = one_subscribe_unsubscribe(request, subscribe, json_object_get_string(event), args);
345         }
346         else
347         {
348                 for (int i = 0 ; i < json_object_array_length(event); i++)
349                 {
350                         x = json_object_array_get_idx(event, i);
351                         rc2 = one_subscribe_unsubscribe(request, subscribe, json_object_get_string(x), args);
352                         if (rc >= 0)
353                                 rc = rc2 >= 0 ? rc + rc2 : rc2;
354                 }
355         }
356         return rc;
357 }
358
359 static void do_subscribe_unsubscribe(struct afb_req request, bool subscribe)
360 {
361         int rc = 0;
362         struct json_object *args, *x;
363
364         args = afb_req_json(request);
365         if (json_object_get_type(args) == json_type_array)
366         {
367                 for(int i = 0; i < json_object_array_length(args); i++)
368                 {
369                         x = json_object_array_get_idx(args, i);
370                         rc += process_one_subscribe_args(request, subscribe, x);
371                 }
372         }
373         else
374         {
375                 rc += process_one_subscribe_args(request, subscribe, args);
376         }
377
378         if (rc >= 0)
379                 afb_req_success(request, NULL, NULL);
380         else
381                 afb_req_fail(request, "error", NULL);
382 }
383
384 void auth(struct afb_req request)
385 {
386         afb_req_session_set_LOA(request, 1);
387         afb_req_success(request, NULL, NULL);
388 }
389
390 void subscribe(struct afb_req request)
391 {
392         do_subscribe_unsubscribe(request, true);
393 }
394
395 void unsubscribe(struct afb_req request)
396 {
397         do_subscribe_unsubscribe(request, false);
398 }
399
400 static int send_frame(const std::string& bus_name, const struct can_frame& cf)
401 {
402         std::map<std::string, std::shared_ptr<low_can_socket_t> >& cd = application_t::instance().get_can_devices();
403
404         if( cd.count(bus_name) == 0)
405                 {cd[bus_name] = std::make_shared<low_can_socket_t>(low_can_socket_t());}
406
407         return cd[bus_name]->tx_send(cf, bus_name);
408 }
409
410 static int write_raw_frame(const std::string& bus_name, uint32_t can_id, uint8_t can_dlc, struct json_object* can_data)
411 {
412         int rc = 0;
413         struct can_frame cf;
414
415         ::memset(&cf, 0, sizeof(cf));
416
417         cf.can_id = can_id;
418         cf.can_dlc = can_dlc;
419
420         struct json_object *x;
421         int n = json_object_array_length(can_data);
422         if(n <= 8)
423         {
424                 for (int i = 0 ; i < n ; i++)
425                 {
426                         x = json_object_array_get_idx(can_data, i);
427                         cf.data[i] = json_object_get_type(x) == json_type_int ? (uint8_t)json_object_get_int(x) : 0;
428                 }
429         }
430
431         const std::string found_device = application_t::instance().get_can_bus_manager().get_can_device_name(bus_name);
432         if( ! found_device.empty())
433         {
434                 rc = send_frame(found_device, cf);
435         }
436
437         return rc;
438 }
439 static int write_signal(const std::string& name, uint64_t value)
440 {
441         int rc = 0;
442         struct can_frame cf;
443         struct utils::signals_found sf;
444
445         ::memset(&cf, 0, sizeof(cf));
446
447         openxc_DynamicField search_key = build_DynamicField(name);
448         sf = utils::signals_manager_t::instance().find_signals(search_key);
449
450         if (sf.can_signals.empty())
451         {
452                 AFB_WARNING("No signal(s) found for %s. Message not sent.", name.c_str());
453                 rc = -1;
454         }
455         else
456         {
457                 for(const auto& sig: sf.can_signals)
458                 {
459                         if(sig->get_writable())
460                         {
461                                 cf = encoder_t::build_frame(sig, value);
462                                 const std::string bus_name = sig->get_message()->get_bus_device_name();
463                                 rc = send_frame(bus_name, cf);
464                         }
465                         else
466                         {
467                                 AFB_WARNING("%s isn't writable. Message not sent.", sig->get_name().c_str());
468                                 return -1;
469                         }
470                 }
471         }
472
473         return rc;
474 }
475
476 void write(struct afb_req request)
477 {
478         int rc = 0;
479         struct json_object* args = nullptr,
480                 *json_name = nullptr,
481                 *json_value = nullptr;
482
483         args = afb_req_json(request);
484
485         // Process about Raw CAN message on CAN bus directly
486         if (args != NULL &&
487                 (json_object_object_get_ex(args, "bus_name", &json_name) && json_object_is_type(json_name, json_type_string) ) &&
488                 (json_object_object_get_ex(args, "frame", &json_value) && json_object_is_type(json_value, json_type_object) ))
489         {
490                 struct json_object* json_can_id = nullptr,
491                         *json_can_dlc = nullptr,
492                         *json_can_data = nullptr;
493
494                 if( (json_object_object_get_ex(json_value, "can_id", &json_can_id) && (json_object_is_type(json_can_id, json_type_double) || json_object_is_type(json_can_id, json_type_int))) &&
495                         (json_object_object_get_ex(json_value, "can_dlc", &json_can_dlc) && (json_object_is_type(json_can_dlc, json_type_double) || json_object_is_type(json_can_dlc, json_type_int))) &&
496                         (json_object_object_get_ex(json_value, "can_data", &json_can_data) && json_object_is_type(json_can_data, json_type_array) ))
497                 {
498                         rc = write_raw_frame(json_object_get_string(json_name),
499                                 json_object_get_int(json_can_id),
500                                 (uint8_t)json_object_get_int(json_can_dlc),
501                                 json_can_data);
502                 }
503                 else
504                 {
505                         AFB_ERROR("Frame object malformed (must be \n \"frame\": {\"can_id\": int, \"can_dlc\": int, \"can_data\": [ int, int , int, int ,int , int ,int ,int]}");
506                         rc = -1;
507                 }
508         }
509         // Search signal then encode value.
510         else if(args != NULL &&
511                 (json_object_object_get_ex(args, "signal_name", &json_name) && json_object_is_type(json_name, json_type_string)) &&
512                 (json_object_object_get_ex(args, "signal_value", &json_value) && (json_object_is_type(json_value, json_type_double) || json_object_is_type(json_value, json_type_int))))
513         {
514                 rc = write_signal(json_object_get_string(json_name),
515                         (uint64_t)json_object_get_double(json_value));
516         }
517         else
518         {
519                 AFB_ERROR("Request argument malformed. Please use the following syntax:");
520                 rc = -1;
521         }
522
523         if (rc >= 0)
524                 afb_req_success(request, NULL, NULL);
525         else
526                 afb_req_fail(request, "error", NULL);
527 }
528
529 static struct json_object *get_signals_value(const std::string& name)
530 {
531         struct utils::signals_found sf;
532         struct json_object *ans = nullptr;
533
534         openxc_DynamicField search_key = build_DynamicField(name);
535         sf = utils::signals_manager_t::instance().find_signals(search_key);
536
537         if (sf.can_signals.empty())
538         {
539                 AFB_WARNING("No signal(s) found for %s.", name.c_str());
540                 return NULL;
541         }
542         ans = json_object_new_array();
543         for(const auto& sig: sf.can_signals)
544         {
545                 struct json_object *jobj = json_object_new_object();
546                 json_object_object_add(jobj, "event", json_object_new_string(sig->get_name().c_str()));
547                 json_object_object_add(jobj, "value", json_object_new_double(sig->get_last_value()));
548                 json_object_array_add(ans, jobj);
549         }
550
551         return ans;
552 }
553 void get(struct afb_req request)
554 {
555         int rc = 0;
556         struct json_object* args = nullptr,
557                 *json_name = nullptr;
558         json_object *ans = nullptr;
559
560         args = afb_req_json(request);
561
562         // Process about Raw CAN message on CAN bus directly
563         if (args != nullptr &&
564                 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string) ))
565         {
566                 ans = get_signals_value(json_object_get_string(json_name));
567                 if (!ans)
568                         rc = -1;
569         }
570         else
571         {
572                 AFB_ERROR("Request argument malformed. Please use the following syntax:");
573                 rc = -1;
574         }
575
576         if (rc >= 0)
577                 afb_req_success(request, ans, NULL);
578         else
579                 afb_req_fail(request, "error", NULL);
580 }
581
582
583 static struct json_object *list_can_message(const std::string& name)
584 {
585         struct utils::signals_found sf;
586         struct json_object *ans = nullptr;
587
588         openxc_DynamicField search_key = build_DynamicField(name);
589         sf = utils::signals_manager_t::instance().find_signals(search_key);
590
591         if (sf.can_signals.empty() && sf.diagnostic_messages.empty())
592         {
593                 AFB_WARNING("No signal(s) found for %s.", name.c_str());
594                 return NULL;
595         }
596         ans = json_object_new_array();
597         for(const auto& sig: sf.can_signals)
598         {
599                 json_object_array_add(ans,
600                         json_object_new_string(sig->get_name().c_str()));
601         }
602         for(const auto& sig: sf.diagnostic_messages)
603         {
604                 json_object_array_add(ans,
605                         json_object_new_string(sig->get_name().c_str()));
606         }
607
608         return ans;
609 }
610
611 void list(struct afb_req request)
612 {
613         int rc = 0;
614         json_object *ans = nullptr;
615         struct json_object* args = nullptr,
616                 *json_name = nullptr;
617         args = afb_req_json(request);
618         const char *name;
619         if ((args != nullptr) &&
620                 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string)))
621         {
622                 name = json_object_get_string(json_name);
623         }
624         else
625         {
626                 name = "*";
627         }
628
629         ans = list_can_message(name);
630         if (!ans)
631                 rc = -1;
632
633         if (rc >= 0)
634                 afb_req_success(request, ans, NULL);
635         else
636                 afb_req_fail(request, "error", NULL);
637 }
638
639 /// @brief Initialize the binding.
640 ///
641 /// @param[in] service Structure which represent the Application Framework Binder.
642 ///
643 /// @return Exit code, zero if success.
644 int initv2()
645 {
646         can_bus_t& can_bus_manager = application_t::instance().get_can_bus_manager();
647
648         can_bus_manager.set_can_devices();
649         can_bus_manager.start_threads();
650
651         /// Initialize Diagnostic manager that will handle obd2 requests.
652         /// We pass by default the first CAN bus device to its Initialization.
653         /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
654         if(application_t::instance().get_diagnostic_manager().initialize())
655                 return 0;
656
657         AFB_ERROR("There was something wrong with CAN device Initialization.");
658         return 1;
659 }