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