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