Change subscribed signals search to check filters as well
[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 = event_filter.frequency == 0 ? 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 static int subscribe_unsubscribe_can_signals(struct afb_req request,
237                                                                                         bool subscribe,
238                                                                                         std::vector<std::shared_ptr<can_signal_t> > can_signals,
239                                                                                         struct event_filter_t& event_filter,
240                                                                                         std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
241 {
242         int rets = 0;
243         for(const auto& sig: can_signals)
244         {
245                 auto it =  std::find_if(s.begin(), s.end(), [&sig, &event_filter](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub){ return sub.second->is_signal_subscription_corresponding(sig, event_filter) ; });
246                 std::shared_ptr<low_can_subscription_t> can_subscription;
247                 if(it != s.end())
248                 {
249                          can_subscription = it->second;
250                 }
251                 else
252                 {
253                          can_subscription = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
254                         if(can_subscription->create_rx_filter(sig) < 0)
255                                 {return -1;}
256                         if(add_to_event_loop(can_subscription) < 0)
257                                 {return -1;}
258                 }
259
260                 if(subscribe_unsubscribe_signal(request, subscribe, can_subscription, s) < 0)
261                         {return -1;}
262
263                 rets++;
264                 AFB_DEBUG("signal: %s subscribed", sig->get_name().c_str());
265         }
266         return rets;
267 }
268
269 ///
270 /// @brief subscribe to all signals in the vector signals
271 ///
272 /// @param[in] afb_req request : contain original request use to subscribe or unsubscribe
273 /// @param[in] subscribe boolean value used to chose between a subscription operation or an unsubscription
274 /// @param[in] signals -  struct containing vectors with can_signal_t and diagnostic_messages to subscribe
275 ///
276 /// @return Number of correctly subscribed signal
277 ///
278 static int subscribe_unsubscribe_signals(struct afb_req request,
279                                                                                 bool subscribe,
280                                                                                 const struct utils::signals_found& signals,
281                                                                                 struct event_filter_t& event_filter)
282 {
283         int rets = 0;
284         utils::signals_manager_t& sm = utils::signals_manager_t::instance();
285
286         std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
287         std::map<int, std::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
288
289         rets += subscribe_unsubscribe_diagnostic_messages(request, subscribe, signals.diagnostic_messages, event_filter, s);
290         rets += subscribe_unsubscribe_can_signals(request, subscribe, signals.can_signals, event_filter, s);
291
292         return rets;
293 }
294
295 static int one_subscribe_unsubscribe(struct afb_req request,
296                                                                         bool subscribe,
297                                                                         const std::string& tag,
298                                                                         json_object* args)
299 {
300         int ret = 0;
301         struct event_filter_t event_filter;
302         struct json_object  *filter, *obj;
303         struct utils::signals_found sf;
304
305         // computes the filter
306         if (json_object_object_get_ex(args, "filter", &filter))
307         {
308                 if (json_object_object_get_ex(filter, "frequency", &obj)
309                 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
310                         {event_filter.frequency = (float)json_object_get_double(obj);}
311                 if (json_object_object_get_ex(filter, "min", &obj)
312                 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
313                         {event_filter.min = (float)json_object_get_double(obj);}
314                 if (json_object_object_get_ex(filter, "max", &obj)
315                 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
316                         {event_filter.max = (float)json_object_get_double(obj);}
317         }
318
319         // subscribe or unsubscribe
320         openxc_DynamicField search_key = build_DynamicField(tag);
321         sf = utils::signals_manager_t::instance().find_signals(search_key);
322         if (sf.can_signals.empty() && sf.diagnostic_messages.empty())
323         {
324                 AFB_NOTICE("No signal(s) found for %s.", tag.c_str());
325                 ret = -1;
326         }
327         else
328                 {ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);}
329
330         return ret;
331 }
332 static int process_one_subscribe_args(struct afb_req request, bool subscribe, json_object *args)
333 {
334         int rc = 0, rc2=0;
335         json_object *x = nullptr, *event = nullptr;
336         if(args == NULL ||
337                 !json_object_object_get_ex(args, "event", &event))
338         {
339                 rc = one_subscribe_unsubscribe(request, subscribe, "*", args);
340         }
341         else if (json_object_get_type(event) != json_type_array)
342         {
343                 rc = one_subscribe_unsubscribe(request, subscribe, json_object_get_string(event), args);
344         }
345         else
346         {
347                 for (int i = 0 ; i < json_object_array_length(event); i++)
348                 {
349                         x = json_object_array_get_idx(event, i);
350                         rc2 = one_subscribe_unsubscribe(request, subscribe, json_object_get_string(x), args);
351                         if (rc >= 0)
352                                 rc = rc2 >= 0 ? rc + rc2 : rc2;
353                 }
354         }
355         return rc;
356 }
357
358 static void do_subscribe_unsubscribe(struct afb_req request, bool subscribe)
359 {
360         int rc = 0;
361         struct json_object *args, *x;
362
363         args = afb_req_json(request);
364         if (json_object_get_type(args) == json_type_array)
365         {
366                 for(int i = 0; i < json_object_array_length(args); i++)
367                 {
368                         x = json_object_array_get_idx(args, i);
369                         rc += process_one_subscribe_args(request, subscribe, x);
370                 }
371         }
372         else
373         {
374                 rc += process_one_subscribe_args(request, subscribe, args);
375         }
376
377         if (rc >= 0)
378                 afb_req_success(request, NULL, NULL);
379         else
380                 afb_req_fail(request, "error", NULL);
381 }
382
383 void auth(struct afb_req request)
384 {
385         afb_req_session_set_LOA(request, 1);
386         afb_req_success(request, NULL, NULL);
387 }
388
389 void subscribe(struct afb_req request)
390 {
391         do_subscribe_unsubscribe(request, true);
392 }
393
394 void unsubscribe(struct afb_req request)
395 {
396         do_subscribe_unsubscribe(request, false);
397 }
398
399 static int send_frame(const std::string& bus_name, const struct can_frame& cf)
400 {
401         std::map<std::string, std::shared_ptr<low_can_socket_t> >& cd = application_t::instance().get_can_devices();
402
403         if( cd.count(bus_name) == 0)
404                 {cd[bus_name] = std::make_shared<low_can_socket_t>(low_can_socket_t());}
405
406         return cd[bus_name]->tx_send(cf, bus_name);
407 }
408
409 static int write_raw_frame(const std::string& bus_name, uint32_t can_id, uint8_t can_dlc, struct json_object* can_data)
410 {
411         int rc = 0;
412         struct can_frame cf;
413
414         ::memset(&cf, 0, sizeof(cf));
415
416         cf.can_id = can_id;
417         cf.can_dlc = can_dlc;
418
419         struct json_object *x;
420         int n = json_object_array_length(can_data);
421         if(n <= 8)
422         {
423                 for (int i = 0 ; i < n ; i++)
424                 {
425                         x = json_object_array_get_idx(can_data, i);
426                         cf.data[i] = json_object_get_type(x) == json_type_int ? (uint8_t)json_object_get_int(x) : 0;
427                 }
428         }
429
430         const std::string found_device = application_t::instance().get_can_bus_manager().get_can_device_name(bus_name);
431         if( ! found_device.empty())
432         {
433                 rc = send_frame(found_device, cf);
434         }
435
436         return rc;
437 }
438 static int write_signal(const std::string& name, uint64_t value)
439 {
440         int rc = 0;
441         struct can_frame cf;
442         struct utils::signals_found sf;
443
444         ::memset(&cf, 0, sizeof(cf));
445
446         openxc_DynamicField search_key = build_DynamicField(name);
447         sf = utils::signals_manager_t::instance().find_signals(search_key);
448
449         if (sf.can_signals.empty())
450         {
451                 AFB_WARNING("No signal(s) found for %s. Message not sent.", name.c_str());
452                 rc = -1;
453         }
454         else
455         {
456                 for(const auto& sig: sf.can_signals)
457                 {
458                         if(sig->get_writable())
459                         {
460                                 cf = encoder_t::build_frame(sig, value);
461                                 const std::string bus_name = sig->get_message()->get_bus_device_name();
462                                 rc = send_frame(bus_name, cf);
463                         }
464                         else
465                         {
466                                 AFB_WARNING("%s isn't writable. Message not sent.", sig->get_name().c_str());
467                                 return -1;
468                         }
469                 }
470         }
471
472         return rc;
473 }
474
475 void write(struct afb_req request)
476 {
477         int rc = 0;
478         struct json_object* args = nullptr,
479                 *json_name = nullptr,
480                 *json_value = nullptr;
481
482         args = afb_req_json(request);
483
484         // Process about Raw CAN message on CAN bus directly
485         if (args != NULL &&
486                 (json_object_object_get_ex(args, "bus_name", &json_name) && json_object_is_type(json_name, json_type_string) ) &&
487                 (json_object_object_get_ex(args, "frame", &json_value) && json_object_is_type(json_value, json_type_object) ))
488         {
489                 struct json_object* json_can_id = nullptr,
490                         *json_can_dlc = nullptr,
491                         *json_can_data = nullptr;
492
493                 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))) &&
494                         (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))) &&
495                         (json_object_object_get_ex(json_value, "can_data", &json_can_data) && json_object_is_type(json_can_data, json_type_array) ))
496                 {
497                         rc = write_raw_frame(json_object_get_string(json_name),
498                                 json_object_get_int(json_can_id),
499                                 (uint8_t)json_object_get_int(json_can_dlc),
500                                 json_can_data);
501                 }
502                 else
503                 {
504                         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]}");
505                         rc = -1;
506                 }
507         }
508         // Search signal then encode value.
509         else if(args != NULL &&
510                 (json_object_object_get_ex(args, "signal_name", &json_name) && json_object_is_type(json_name, json_type_string)) &&
511                 (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))))
512         {
513                 rc = write_signal(json_object_get_string(json_name),
514                         (uint64_t)json_object_get_double(json_value));
515         }
516         else
517         {
518                 AFB_ERROR("Request argument malformed. Please use the following syntax:");
519                 rc = -1;
520         }
521
522         if (rc >= 0)
523                 afb_req_success(request, NULL, NULL);
524         else
525                 afb_req_fail(request, "error", NULL);
526 }
527
528 static struct json_object *get_signals_value(const std::string& name)
529 {
530         struct utils::signals_found sf;
531         struct json_object *ans = nullptr;
532
533         openxc_DynamicField search_key = build_DynamicField(name);
534         sf = utils::signals_manager_t::instance().find_signals(search_key);
535
536         if (sf.can_signals.empty())
537         {
538                 AFB_WARNING("No signal(s) found for %s.", name.c_str());
539                 return NULL;
540         }
541         ans = json_object_new_array();
542         for(const auto& sig: sf.can_signals)
543         {
544                 struct json_object *jobj = json_object_new_object();
545                 json_object_object_add(jobj, "event", json_object_new_string(sig->get_name().c_str()));
546                 json_object_object_add(jobj, "value", json_object_new_double(sig->get_last_value()));
547                 json_object_array_add(ans, jobj);
548         }
549
550         return ans;
551 }
552 void get(struct afb_req request)
553 {
554         int rc = 0;
555         struct json_object* args = nullptr,
556                 *json_name = nullptr;
557         json_object *ans = nullptr;
558
559         args = afb_req_json(request);
560
561         // Process about Raw CAN message on CAN bus directly
562         if (args != nullptr &&
563                 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string) ))
564         {
565                 ans = get_signals_value(json_object_get_string(json_name));
566                 if (!ans)
567                         rc = -1;
568         }
569         else
570         {
571                 AFB_ERROR("Request argument malformed. Please use the following syntax:");
572                 rc = -1;
573         }
574
575         if (rc >= 0)
576                 afb_req_success(request, ans, NULL);
577         else
578                 afb_req_fail(request, "error", NULL);
579 }
580
581
582 static struct json_object *list_can_message(const std::string& name)
583 {
584         struct utils::signals_found sf;
585         struct json_object *ans = nullptr;
586
587         openxc_DynamicField search_key = build_DynamicField(name);
588         sf = utils::signals_manager_t::instance().find_signals(search_key);
589
590         if (sf.can_signals.empty() && sf.diagnostic_messages.empty())
591         {
592                 AFB_WARNING("No signal(s) found for %s.", name.c_str());
593                 return NULL;
594         }
595         ans = json_object_new_array();
596         for(const auto& sig: sf.can_signals)
597         {
598                 json_object_array_add(ans,
599                         json_object_new_string(sig->get_name().c_str()));
600         }
601         for(const auto& sig: sf.diagnostic_messages)
602         {
603                 json_object_array_add(ans,
604                         json_object_new_string(sig->get_name().c_str()));
605         }
606
607         return ans;
608 }
609
610 void list(struct afb_req request)
611 {
612         int rc = 0;
613         json_object *ans = nullptr;
614         struct json_object* args = nullptr,
615                 *json_name = nullptr;
616         args = afb_req_json(request);
617         const char *name;
618         if ((args != nullptr) &&
619                 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string)))
620         {
621                 name = json_object_get_string(json_name);
622         }
623         else
624         {
625                 name = "*";
626         }
627
628         ans = list_can_message(name);
629         if (!ans)
630                 rc = -1;
631
632         if (rc >= 0)
633                 afb_req_success(request, ans, NULL);
634         else
635                 afb_req_fail(request, "error", NULL);
636 }
637
638 /// @brief Initialize the binding.
639 ///
640 /// @param[in] service Structure which represent the Application Framework Binder.
641 ///
642 /// @return Exit code, zero if success.
643 int initv2()
644 {
645         can_bus_t& can_bus_manager = application_t::instance().get_can_bus_manager();
646
647         can_bus_manager.set_can_devices();
648         can_bus_manager.start_threads();
649
650         /// Initialize Diagnostic manager that will handle obd2 requests.
651         /// We pass by default the first CAN bus device to its Initialization.
652         /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
653         if(application_t::instance().get_diagnostic_manager().initialize())
654                 return 0;
655
656         AFB_ERROR("There was something wrong with CAN device Initialization.");
657         return 1;
658 }