Minor improvements
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-cb.cpp
1 /*
2  * Copyright (C) 2015, 2018 "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 <wrap-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/signals.hpp"
35 #include "../can/message/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(std::shared_ptr<message_t> m)
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(m);
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
87
88         if ((revents & EPOLLIN) != 0)
89         {
90                 std::shared_ptr<utils::socketcan_t> s = can_subscription->get_socket();
91                 std::shared_ptr<message_t> message = s->read_message();
92
93                 // Sure we got a valid CAN message ?
94                 if (! message->get_id() == 0 && ! message->get_length() == 0)
95                 {
96                         push_n_notify(message);
97                 }
98         }
99
100         // check if error or hangup
101         if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
102         {
103                 sd_event_source_unref(event_source);
104                 can_subscription->get_socket()->close();
105         }
106
107         return 0;
108 }
109
110 ///******************************************************************************
111 ///
112 ///             Subscription and unsubscription
113 ///
114 ///*******************************************************************************/
115
116 /// @brief This will determine if an event handle needs to be created and checks if
117 /// we got a valid afb_event to get subscribe or unsubscribe. After that launch the subscription or unsubscription
118 /// against the application framework using that event handle.
119 static int subscribe_unsubscribe_signal(afb_req_t request,
120                                         bool subscribe,
121                                         std::shared_ptr<low_can_subscription_t>& can_subscription,
122                                         std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
123 {
124         int ret = 0;
125         int sub_index = can_subscription->get_index();
126         bool subscription_exists = s.count(sub_index);
127
128         // Susbcription part
129         if(subscribe)
130         {
131                 /* There is no valid request to subscribe so this must be an
132                  * internal permanent diagnostic request. Skip the subscription
133                  * part and don't register it into the current "low-can"
134                  * subsciptions.
135                  */
136                 if(! request)
137                 {
138                         return 0;
139                 }
140
141                 // Event doesn't exist , so let's create it
142                 if (! subscription_exists &&
143                     (ret = can_subscription->subscribe(request)) < 0)
144                         return ret;
145
146                 if(! subscription_exists)
147                                 s[sub_index] = can_subscription;
148
149                 return ret;
150         }
151
152         // Unsubscrition part
153         if(! subscription_exists)
154         {
155                 AFB_NOTICE("There isn't any valid subscriptions for that request.");
156                 return ret;
157         }
158         else if (subscription_exists &&
159                  ! afb_event_is_valid(s[sub_index]->get_event()) )
160         {
161                 AFB_NOTICE("Event isn't valid, no need to unsubscribed.");
162                 return ret;
163         }
164
165         if( (ret = s[sub_index]->unsubscribe(request)) < 0)
166                 return ret;
167         s.erase(sub_index);
168
169         return ret;
170 }
171
172 static int add_to_event_loop(std::shared_ptr<low_can_subscription_t>& can_subscription)
173 {
174                 struct sd_event_source* event_source = nullptr;
175                 return ( sd_event_add_io(afb_daemon_get_event_loop(),
176                         &event_source,
177                         can_subscription->get_socket()->socket(),
178                         EPOLLIN,
179                         read_message,
180                         can_subscription.get()));
181 }
182
183 static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request,
184                                                      bool subscribe,
185                                                      std::vector<std::shared_ptr<diagnostic_message_t> > diagnostic_messages,
186                                                      struct event_filter_t& event_filter,
187                                                      std::map<int, std::shared_ptr<low_can_subscription_t> >& s,
188                                                      bool perm_rec_diag_req)
189 {
190         int rets = 0;
191         application_t& app = application_t::instance();
192         diagnostic_manager_t& diag_m = app.get_diagnostic_manager();
193
194         for(const auto& sig : diagnostic_messages)
195         {
196                 DiagnosticRequest* diag_req = new DiagnosticRequest(sig->build_diagnostic_request());
197                 event_filter.frequency = event_filter.frequency == 0 ? sig->get_frequency() : event_filter.frequency;
198                 std::shared_ptr<low_can_subscription_t> can_subscription;
199
200                 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());});
201                 can_subscription = it != s.end() ?
202                         it->second :
203                         std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
204                 // If the requested diagnostic message is not supported by the car then unsubcribe it
205                 // no matter what we want, worst case will be a failed unsubscription but at least we won't
206                 // poll a PID for nothing.
207                 if(sig->get_supported() && subscribe)
208                 {
209                         if (!app.isEngineOn())
210                                 AFB_WARNING("signal: Engine is off, %s won't received responses until it's on",  sig->get_name().c_str());
211
212                         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);
213                         if(can_subscription->create_rx_filter(sig) < 0)
214                                 {return -1;}
215                         AFB_DEBUG("Signal: %s subscribed", sig->get_name().c_str());
216                         if(it == s.end() && add_to_event_loop(can_subscription) < 0)
217                         {
218                                 diag_m.cleanup_request(
219                                         diag_m.find_recurring_request(*diag_req), true);
220                                 AFB_WARNING("signal: %s isn't supported. Canceling operation.",  sig->get_name().c_str());
221                                 return -1;
222                         }
223                 }
224                 else
225                 {
226                         if(sig->get_supported())
227                         {AFB_DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str());}
228                         else
229                         {
230                                 AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
231                                 return -1;
232                         }
233                 }
234                 int ret = subscribe_unsubscribe_signal(request, subscribe, can_subscription, s);
235                 if(ret < 0)
236                         return ret;
237
238                 rets++;
239         }
240
241         return rets;
242 }
243
244 static int subscribe_unsubscribe_signals(afb_req_t request,
245                                              bool subscribe,
246                                              std::vector<std::shared_ptr<signal_t> > signals,
247                                              struct event_filter_t& event_filter,
248                                              std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
249 {
250         int rets = 0;
251         for(const auto& sig: signals)
252         {
253                 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) ; });
254                 std::shared_ptr<low_can_subscription_t> can_subscription;
255                 if(it != s.end())
256                         {can_subscription = it->second;}
257                 else
258                 {
259                         can_subscription = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
260                         if(can_subscription->create_rx_filter(sig) < 0)
261                                 {return -1;}
262                         if(add_to_event_loop(can_subscription) < 0)
263                                 {return -1;}
264                 }
265
266                 if(subscribe_unsubscribe_signal(request, subscribe, can_subscription, s) < 0)
267                         {return -1;}
268
269                 rets++;
270                 AFB_DEBUG("%s Signal: %s %ssubscribed", sig->get_message()->is_fd() ? "FD": "", sig->get_name().c_str(), subscribe ? "":"un");
271         }
272         return rets;
273 }
274
275 ///
276 /// @brief subscribe to all signals in the vector signals
277 ///
278 /// @param[in] afb_req request : contains original request use to subscribe or unsubscribe
279 /// @param[in] subscribe boolean value, which chooses between a subscription operation or an unsubscription
280 /// @param[in] signals -  struct containing vectors with signal_t and diagnostic_messages to subscribe
281 ///
282 /// @return Number of correctly subscribed signal
283 ///
284 static int subscribe_unsubscribe_signals(afb_req_t request,
285                                          bool subscribe,
286                                          const struct utils::signals_found& signals,
287                                          struct event_filter_t& event_filter)
288 {
289         int rets = 0;
290         utils::signals_manager_t& sm = utils::signals_manager_t::instance();
291
292         std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
293         std::map<int, std::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
294
295         rets += subscribe_unsubscribe_diagnostic_messages(request, subscribe, signals.diagnostic_messages, event_filter, s, false);
296         rets += subscribe_unsubscribe_signals(request, subscribe, signals.signals, event_filter, s);
297
298         return rets;
299 }
300
301 static event_filter_t generate_filter(json_object* args)
302 {
303         event_filter_t event_filter;
304         struct json_object  *filter, *obj;
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         return event_filter;
320 }
321
322
323 static int one_subscribe_unsubscribe_events(afb_req_t request, bool subscribe, const std::string& tag, json_object* args)
324 {
325         int ret = 0;
326         struct utils::signals_found sf;
327
328         // subscribe or unsubscribe
329         openxc_DynamicField search_key = build_DynamicField(tag);
330         sf = utils::signals_manager_t::instance().find_signals(search_key);
331         if (sf.signals.empty() && sf.diagnostic_messages.empty())
332         {
333                 AFB_NOTICE("No signal(s) found for %s.", tag.c_str());
334                 ret = -1;
335         }
336         else
337         {
338                 event_filter_t event_filter = generate_filter(args);
339                 ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
340         }
341         return ret;
342 }
343
344 static int one_subscribe_unsubscribe_id(afb_req_t request, bool subscribe, const uint32_t& id ,json_object *args)
345 {
346         int ret = 0;
347         std::shared_ptr<message_definition_t> message_definition = application_t::instance().get_message_definition(id);
348         struct utils::signals_found sf;
349
350         if(message_definition)
351         {
352                 sf.signals = message_definition->get_signals();
353         }
354
355         if(sf.signals.empty())
356         {
357                 AFB_NOTICE("No signal(s) found for %d.", id);
358                 ret = -1;
359         }
360         else
361         {
362                 event_filter_t event_filter = generate_filter(args);
363                 ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
364         }
365         return ret;
366 }
367
368
369 static int process_one_subscribe_args(afb_req_t request, bool subscribe, json_object *args)
370 {
371         int rc = 0, rc2=0;
372         json_object *x = nullptr, *event = nullptr, *id = nullptr;
373
374
375         // 2 cases : ID(PGN) and event
376
377         json_bool test_event = json_object_object_get_ex(args,"event",&event);
378         json_bool test_id = json_object_object_get_ex(args,"id",&id);
379         if(!test_id)
380         {
381                 json_object_object_get_ex(args,"pgn",&id);
382         }
383
384         if(     args == NULL || (!test_event && !id))
385         {
386                 rc = one_subscribe_unsubscribe_events(request, subscribe, "*", args);
387         }
388         else
389         {
390                 if(event)
391                 {
392                         if (json_object_get_type(event) != json_type_array) // event is set before and check if it's an array
393                         {
394                                 rc = one_subscribe_unsubscribe_events(request, subscribe, json_object_get_string(event), args);
395                         }
396                         else // event is set and it's not an array
397                         {
398                                 for (int i = 0 ; i < json_object_array_length(event); i++)
399                                 {
400                                         x = json_object_array_get_idx(event, i);
401                                         rc2 = one_subscribe_unsubscribe_events(request, subscribe, json_object_get_string(x), args);
402                                         if (rc >= 0)
403                                                 rc = rc2 >= 0 ? rc + rc2 : rc2;
404                                 }
405                         }
406                 }
407
408                 if(id)
409                 {
410                         if (json_object_get_type(id) != json_type_array) // id is set before and check if it's an array
411                         {
412                                 rc = one_subscribe_unsubscribe_id(request, subscribe, json_object_get_int(id), args);
413                         }
414                         else // event is set and it's not an array
415                         {
416                                 for (int i = 0 ; i < json_object_array_length(id); i++)
417                                 {
418                                         x = json_object_array_get_idx(id, i);
419                                         rc2 = one_subscribe_unsubscribe_id(request, subscribe, json_object_get_int(x), args);
420                                         if (rc >= 0)
421                                                 rc = rc2 >= 0 ? rc + rc2 : rc2;
422                                 }
423                         }
424                 }
425         }
426         return rc;
427 }
428
429 static void do_subscribe_unsubscribe(afb_req_t request, bool subscribe)
430 {
431         int rc = 0;
432         struct json_object *args, *x;
433
434         args = afb_req_json(request);
435         if (json_object_get_type(args) == json_type_array)
436         {
437                 for(int i = 0; i < json_object_array_length(args); i++)
438                 {
439                         x = json_object_array_get_idx(args, i);
440                         rc += process_one_subscribe_args(request, subscribe, x);
441                 }
442         }
443         else
444         {
445                 rc += process_one_subscribe_args(request, subscribe, args);
446         }
447
448         if (rc >= 0)
449                 afb_req_success(request, NULL, NULL);
450         else
451                 afb_req_fail(request, "error", NULL);
452 }
453
454 void auth(afb_req_t request)
455 {
456         afb_req_session_set_LOA(request, 1);
457         afb_req_success(request, NULL, NULL);
458 }
459
460 void subscribe(afb_req_t request)
461 {
462         do_subscribe_unsubscribe(request, true);
463 }
464
465 void unsubscribe(afb_req_t request)
466 {
467         do_subscribe_unsubscribe(request, false);
468 }
469
470 static int send_frame(struct canfd_frame& cfd, const std::string& bus_name)
471 {
472         if(bus_name.empty()) {
473                 return -1;
474         }
475
476         std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
477
478         if( cd.count(bus_name) == 0)
479                 {cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());}
480
481         return low_can_subscription_t::tx_send(*cd[bus_name], cfd, bus_name);
482 }
483
484 static void write_raw_frame(afb_req_t request, const std::string& bus_name, json_object *json_value)
485 {
486         struct canfd_frame cfd;
487         struct json_object *can_data = nullptr;
488
489         ::memset(&cfd, 0, sizeof(cfd));
490
491         if(wrap_json_unpack(json_value, "{si, si, so !}",
492                               "can_id", &cfd.can_id,
493                               "can_dlc", &cfd.len,
494                               "can_data", &can_data))
495         {
496                 afb_req_fail(request, "Invalid", "Frame object malformed");
497                 return;
498         }
499
500         if(cfd.len <= 8 && cfd.len > 0)
501         {
502                 for (int i = 0 ; i < cfd.len ; i++)
503                 {
504                         struct json_object *one_can_data = json_object_array_get_idx(can_data, i);
505                         cfd.data[i] = (json_object_is_type(one_can_data, json_type_int)) ?
506                                         (uint8_t)json_object_get_int(one_can_data) : 0;
507                 }
508         }
509         else
510         {
511                 afb_req_fail(request, "Invalid", "Data array must hold 1 to 8 values.");
512                 return;
513         }
514
515         if(! send_frame(cfd, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name)))
516                 afb_req_success(request, nullptr, "Message correctly sent");
517         else
518                 afb_req_fail(request, "Error", "sending the message. See the log for more details.");
519 }
520
521 static void write_signal(afb_req_t request, const std::string& name, json_object *json_value)
522 {
523         struct canfd_frame cfd;
524         struct utils::signals_found sf;
525         signal_encoder encoder = nullptr;
526         bool send = true;
527
528         ::memset(&cfd, 0, sizeof(cfd));
529
530         openxc_DynamicField search_key = build_DynamicField(name);
531         sf = utils::signals_manager_t::instance().find_signals(search_key);
532         openxc_DynamicField dynafield_value = build_DynamicField(json_value);
533
534         if (sf.signals.empty())
535         {
536                 afb_req_fail_f(request, "No signal(s) found for %s. Message not sent.", name.c_str());
537                 return;
538         }
539
540         std::shared_ptr<signal_t>& sig = sf.signals[0];
541         if(! sig->get_writable())
542         {
543                 afb_req_fail_f(request, "%s isn't writable. Message not sent.", sig->get_name().c_str());
544                 return;
545         }
546
547         uint64_t value = (encoder = sig->get_encoder()) ?
548                         encoder(*sig, dynafield_value, &send) :
549                         encoder_t::encode_DynamicField(*sig, dynafield_value, &send);
550
551         cfd = encoder_t::build_frame(sig, value);
552         if(! send_frame(cfd, sig->get_message()->get_bus_device_name()) && send)
553                 afb_req_success(request, nullptr, "Message correctly sent");
554         else
555                 afb_req_fail(request, "Error", "Sending the message. See the log for more details.");
556 }
557
558 void write(afb_req_t request)
559 {
560         struct json_object* args = nullptr, *json_value = nullptr;
561         const char *name = nullptr;
562
563         args = afb_req_json(request);
564
565         // Process about Raw CAN message on CAN bus directly
566         if (args != NULL && ! wrap_json_unpack(args, "{ss, so !}",
567                                                "bus_name", &name,
568                                                "frame", &json_value))
569                 write_raw_frame(request, name, json_value);
570
571         // Search signal then encode value.
572         else if(args != NULL &&
573                 ! wrap_json_unpack(args, "{ss, so !}",
574                                    "signal_name", &name,
575                                    "signal_value", &json_value))
576                 write_signal(request, std::string(name), json_value);
577         else
578                 afb_req_fail(request, "Error", "Request argument malformed");
579 }
580
581 static struct json_object *get_signals_value(const std::string& name)
582 {
583         struct utils::signals_found sf;
584         struct json_object *ans = nullptr;
585
586         openxc_DynamicField search_key = build_DynamicField(name);
587         sf = utils::signals_manager_t::instance().find_signals(search_key);
588
589         if (sf.signals.empty())
590         {
591                 AFB_WARNING("No signal(s) found for %s.", name.c_str());
592                 return NULL;
593         }
594         ans = json_object_new_array();
595         for(const auto& sig: sf.signals)
596         {
597                 struct json_object *jobj = json_object_new_object();
598                 json_object_object_add(jobj, "event", json_object_new_string(sig->get_name().c_str()));
599                 json_object_object_add(jobj, "value", json_object_new_double(sig->get_last_value()));
600                 json_object_array_add(ans, jobj);
601         }
602
603         return ans;
604 }
605 void get(afb_req_t request)
606 {
607         int rc = 0;
608         struct json_object* args = nullptr,
609                 *json_name = nullptr;
610         json_object *ans = nullptr;
611
612         args = afb_req_json(request);
613
614         // Process about Raw CAN message on CAN bus directly
615         if (args != nullptr &&
616                 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string) ))
617         {
618                 ans = get_signals_value(json_object_get_string(json_name));
619                 if (!ans)
620                         rc = -1;
621         }
622         else
623         {
624                 AFB_ERROR("Request argument malformed. Please use the following syntax:");
625                 rc = -1;
626         }
627
628         if (rc >= 0)
629                 afb_req_success(request, ans, NULL);
630         else
631                 afb_req_fail(request, "error", NULL);
632 }
633
634
635 static struct json_object *list_can_message(const std::string& name)
636 {
637         struct utils::signals_found sf;
638         struct json_object *ans = nullptr;
639
640         openxc_DynamicField search_key = build_DynamicField(name);
641         sf = utils::signals_manager_t::instance().find_signals(search_key);
642
643         if (sf.signals.empty() && sf.diagnostic_messages.empty())
644         {
645                 AFB_WARNING("No signal(s) found for %s.", name.c_str());
646                 return NULL;
647         }
648         ans = json_object_new_array();
649         for(const auto& sig: sf.signals)
650         {
651                 json_object_array_add(ans,
652                         json_object_new_string(sig->get_name().c_str()));
653         }
654         for(const auto& sig: sf.diagnostic_messages)
655         {
656                 json_object_array_add(ans,
657                         json_object_new_string(sig->get_name().c_str()));
658         }
659
660         return ans;
661 }
662
663 void list(afb_req_t request)
664 {
665         int rc = 0;
666         json_object *ans = nullptr;
667         struct json_object* args = nullptr,
668                 *json_name = nullptr;
669         args = afb_req_json(request);
670         const char *name;
671         if ((args != nullptr) &&
672                 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string)))
673         {
674                 name = json_object_get_string(json_name);
675         }
676         else
677         {
678                 name = "*";
679         }
680
681         ans = list_can_message(name);
682         if (!ans)
683                 rc = -1;
684
685         if (rc >= 0)
686                 afb_req_success(request, ans, NULL);
687         else
688                 afb_req_fail(request, "error", NULL);
689 }
690
691 /// @brief Initialize the binding.
692 ///
693 /// @param[in] service Structure which represent the Application Framework Binder.
694 ///
695 /// @return Exit code, zero if success.
696 int init_binding(afb_api_t api)
697 {
698         uint32_t ret = 1;
699         can_bus_t& can_bus_manager = application_t::instance().get_can_bus_manager();
700
701         can_bus_manager.set_can_devices();
702         can_bus_manager.start_threads();
703         utils::signals_manager_t& sm = utils::signals_manager_t::instance();
704
705         /// Initialize Diagnostic manager that will handle obd2 requests.
706         /// We pass by default the first CAN bus device to its Initialization.
707         /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
708         if(application_t::instance().get_diagnostic_manager().initialize())
709                 ret = 0;
710
711         // Add a recurring dignostic message request to get engine speed at all times.
712         openxc_DynamicField search_key = build_DynamicField("diagnostic_messages.engine.speed");
713         struct utils::signals_found sf = sm.find_signals(search_key);
714
715         if(sf.signals.empty() && sf.diagnostic_messages.size() == 1)
716         {
717                 afb_req_t request = nullptr;
718
719                 struct event_filter_t event_filter;
720                 event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();
721
722                 std::map<int, std::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
723
724                 subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
725         }
726
727         if(ret)
728                 AFB_ERROR("There was something wrong with CAN device Initialization.");
729
730         return ret;
731 }