Rename some of the classes removing can- prefix
[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 int one_subscribe_unsubscribe(afb_req_t request,
302                                      bool subscribe,
303                                      const std::string& tag,
304                                      json_object* args)
305 {
306         int ret = 0;
307         struct event_filter_t event_filter;
308         struct json_object  *filter, *obj;
309         struct utils::signals_found sf;
310
311         // computes the filter
312         if (json_object_object_get_ex(args, "filter", &filter))
313         {
314                 if (json_object_object_get_ex(filter, "frequency", &obj)
315                 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
316                         {event_filter.frequency = (float)json_object_get_double(obj);}
317                 if (json_object_object_get_ex(filter, "min", &obj)
318                 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
319                         {event_filter.min = (float)json_object_get_double(obj);}
320                 if (json_object_object_get_ex(filter, "max", &obj)
321                 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
322                         {event_filter.max = (float)json_object_get_double(obj);}
323         }
324
325         // subscribe or unsubscribe
326         openxc_DynamicField search_key = build_DynamicField(tag);
327         sf = utils::signals_manager_t::instance().find_signals(search_key);
328         if (sf.signals.empty() && sf.diagnostic_messages.empty())
329         {
330                 AFB_NOTICE("No signal(s) found for %s.", tag.c_str());
331                 ret = -1;
332         }
333         else
334                 {ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);}
335
336         return ret;
337 }
338 static int process_one_subscribe_args(afb_req_t request, bool subscribe, json_object *args)
339 {
340         int rc = 0, rc2=0;
341         json_object *x = nullptr, *event = nullptr;
342         if(args == NULL ||
343                 !json_object_object_get_ex(args, "event", &event))
344         {
345                 rc = one_subscribe_unsubscribe(request, subscribe, "*", args);
346         }
347         else if (json_object_get_type(event) != json_type_array)
348         {
349                 rc = one_subscribe_unsubscribe(request, subscribe, json_object_get_string(event), args);
350         }
351         else
352         {
353                 for (int i = 0 ; i < json_object_array_length(event); i++)
354                 {
355                         x = json_object_array_get_idx(event, i);
356                         rc2 = one_subscribe_unsubscribe(request, subscribe, json_object_get_string(x), args);
357                         if (rc >= 0)
358                                 rc = rc2 >= 0 ? rc + rc2 : rc2;
359                 }
360         }
361         return rc;
362 }
363
364 static void do_subscribe_unsubscribe(afb_req_t request, bool subscribe)
365 {
366         int rc = 0;
367         struct json_object *args, *x;
368
369         args = afb_req_json(request);
370         if (json_object_get_type(args) == json_type_array)
371         {
372                 for(int i = 0; i < json_object_array_length(args); i++)
373                 {
374                         x = json_object_array_get_idx(args, i);
375                         rc += process_one_subscribe_args(request, subscribe, x);
376                 }
377         }
378         else
379         {
380                 rc += process_one_subscribe_args(request, subscribe, args);
381         }
382
383         if (rc >= 0)
384                 afb_req_success(request, NULL, NULL);
385         else
386                 afb_req_fail(request, "error", NULL);
387 }
388
389 void auth(afb_req_t request)
390 {
391         afb_req_session_set_LOA(request, 1);
392         afb_req_success(request, NULL, NULL);
393 }
394
395 void subscribe(afb_req_t request)
396 {
397         do_subscribe_unsubscribe(request, true);
398 }
399
400 void unsubscribe(afb_req_t request)
401 {
402         do_subscribe_unsubscribe(request, false);
403 }
404
405 static int send_frame(struct canfd_frame& cfd, const std::string& bus_name)
406 {
407         if(bus_name.empty()) {
408                 return -1;
409         }
410
411         std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
412
413         if( cd.count(bus_name) == 0)
414                 {cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());}
415
416         return cd[bus_name]->tx_send(*cd[bus_name], cfd, bus_name);
417 }
418
419 static void write_raw_frame(afb_req_t request, const std::string& bus_name, json_object *json_value)
420 {
421         struct canfd_frame cfd;
422         struct json_object *can_data = nullptr;
423
424         ::memset(&cfd, 0, sizeof(cfd));
425
426         if(wrap_json_unpack(json_value, "{si, si, so !}",
427                               "can_id", &cfd.can_id,
428                               "can_dlc", &cfd.len,
429                               "can_data", &can_data))
430         {
431                 afb_req_fail(request, "Invalid", "Frame object malformed");
432                 return;
433         }
434
435         if(cfd.len <= 8 && cfd.len > 0)
436         {
437                 for (int i = 0 ; i < cfd.len ; i++)
438                 {
439                         struct json_object *one_can_data = json_object_array_get_idx(can_data, i);
440                         cfd.data[i] = (json_object_is_type(one_can_data, json_type_int)) ?
441                                         (uint8_t)json_object_get_int(one_can_data) : 0;
442                 }
443         }
444         else
445         {
446                 afb_req_fail(request, "Invalid", "Data array must hold 1 to 8 values.");
447                 return;
448         }
449
450         if(! send_frame(cfd, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name)))
451                 afb_req_success(request, nullptr, "Message correctly sent");
452         else
453                 afb_req_fail(request, "Error", "sending the message. See the log for more details.");
454 }
455
456 static void write_signal(afb_req_t request, const std::string& name, json_object *json_value)
457 {
458         struct canfd_frame cfd;
459         struct utils::signals_found sf;
460         signal_encoder encoder = nullptr;
461         bool send = true;
462
463         ::memset(&cfd, 0, sizeof(cfd));
464
465         openxc_DynamicField search_key = build_DynamicField(name);
466         sf = utils::signals_manager_t::instance().find_signals(search_key);
467         openxc_DynamicField dynafield_value = build_DynamicField(json_value);
468
469         if (sf.signals.empty())
470         {
471                 afb_req_fail_f(request, "No signal(s) found for %s. Message not sent.", name.c_str());
472                 return;
473         }
474
475         std::shared_ptr<signal_t>& sig = sf.signals[0];
476         if(! sig->get_writable())
477         {
478                 afb_req_fail_f(request, "%s isn't writable. Message not sent.", sig->get_name().c_str());
479                 return;
480         }
481
482         uint64_t value = (encoder = sig->get_encoder()) ?
483                         encoder(*sig, dynafield_value, &send) :
484                         encoder_t::encode_DynamicField(*sig, dynafield_value, &send);
485
486         cfd = encoder_t::build_frame(sig, value);
487         if(! send_frame(cfd, sig->get_message()->get_bus_device_name()) && send)
488                 afb_req_success(request, nullptr, "Message correctly sent");
489         else
490                 afb_req_fail(request, "Error", "Sending the message. See the log for more details.");
491 }
492
493 void write(afb_req_t request)
494 {
495         struct json_object* args = nullptr, *json_value = nullptr;
496         const char *name = nullptr;
497
498         args = afb_req_json(request);
499
500         // Process about Raw CAN message on CAN bus directly
501         if (args != NULL && ! wrap_json_unpack(args, "{ss, so !}",
502                                                "bus_name", &name,
503                                                "frame", &json_value))
504                 write_raw_frame(request, name, json_value);
505
506         // Search signal then encode value.
507         else if(args != NULL &&
508                 ! wrap_json_unpack(args, "{ss, so !}",
509                                    "signal_name", &name,
510                                    "signal_value", &json_value))
511                 write_signal(request, std::string(name), json_value);
512         else
513                 afb_req_fail(request, "Error", "Request argument malformed");
514 }
515
516 static struct json_object *get_signals_value(const std::string& name)
517 {
518         struct utils::signals_found sf;
519         struct json_object *ans = nullptr;
520
521         openxc_DynamicField search_key = build_DynamicField(name);
522         sf = utils::signals_manager_t::instance().find_signals(search_key);
523
524         if (sf.signals.empty())
525         {
526                 AFB_WARNING("No signal(s) found for %s.", name.c_str());
527                 return NULL;
528         }
529         ans = json_object_new_array();
530         for(const auto& sig: sf.signals)
531         {
532                 struct json_object *jobj = json_object_new_object();
533                 json_object_object_add(jobj, "event", json_object_new_string(sig->get_name().c_str()));
534                 json_object_object_add(jobj, "value", json_object_new_double(sig->get_last_value()));
535                 json_object_array_add(ans, jobj);
536         }
537
538         return ans;
539 }
540 void get(afb_req_t request)
541 {
542         int rc = 0;
543         struct json_object* args = nullptr,
544                 *json_name = nullptr;
545         json_object *ans = nullptr;
546
547         args = afb_req_json(request);
548
549         // Process about Raw CAN message on CAN bus directly
550         if (args != nullptr &&
551                 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string) ))
552         {
553                 ans = get_signals_value(json_object_get_string(json_name));
554                 if (!ans)
555                         rc = -1;
556         }
557         else
558         {
559                 AFB_ERROR("Request argument malformed. Please use the following syntax:");
560                 rc = -1;
561         }
562
563         if (rc >= 0)
564                 afb_req_success(request, ans, NULL);
565         else
566                 afb_req_fail(request, "error", NULL);
567 }
568
569
570 static struct json_object *list_can_message(const std::string& name)
571 {
572         struct utils::signals_found sf;
573         struct json_object *ans = nullptr;
574
575         openxc_DynamicField search_key = build_DynamicField(name);
576         sf = utils::signals_manager_t::instance().find_signals(search_key);
577
578         if (sf.signals.empty() && sf.diagnostic_messages.empty())
579         {
580                 AFB_WARNING("No signal(s) found for %s.", name.c_str());
581                 return NULL;
582         }
583         ans = json_object_new_array();
584         for(const auto& sig: sf.signals)
585         {
586                 json_object_array_add(ans,
587                         json_object_new_string(sig->get_name().c_str()));
588         }
589         for(const auto& sig: sf.diagnostic_messages)
590         {
591                 json_object_array_add(ans,
592                         json_object_new_string(sig->get_name().c_str()));
593         }
594
595         return ans;
596 }
597
598 void list(afb_req_t request)
599 {
600         int rc = 0;
601         json_object *ans = nullptr;
602         struct json_object* args = nullptr,
603                 *json_name = nullptr;
604         args = afb_req_json(request);
605         const char *name;
606         if ((args != nullptr) &&
607                 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string)))
608         {
609                 name = json_object_get_string(json_name);
610         }
611         else
612         {
613                 name = "*";
614         }
615
616         ans = list_can_message(name);
617         if (!ans)
618                 rc = -1;
619
620         if (rc >= 0)
621                 afb_req_success(request, ans, NULL);
622         else
623                 afb_req_fail(request, "error", NULL);
624 }
625
626 /// @brief Initialize the binding.
627 ///
628 /// @param[in] service Structure which represent the Application Framework Binder.
629 ///
630 /// @return Exit code, zero if success.
631 int init_binding(afb_api_t api)
632 {
633         uint32_t ret = 1;
634         can_bus_t& can_bus_manager = application_t::instance().get_can_bus_manager();
635
636         can_bus_manager.set_can_devices();
637         can_bus_manager.start_threads();
638
639         /// Initialize Diagnostic manager that will handle obd2 requests.
640         /// We pass by default the first CAN bus device to its Initialization.
641         /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
642         if(application_t::instance().get_diagnostic_manager().initialize())
643                 ret = 0;
644
645         // Add a recurring dignostic message request to get engine speed at all times.
646         openxc_DynamicField search_key = build_DynamicField("diagnostic_messages.engine.speed");
647         struct utils::signals_found sf = utils::signals_manager_t::instance().find_signals(search_key);
648
649         if(sf.signals.empty() && sf.diagnostic_messages.size() == 1)
650         {
651                 afb_req_t request = nullptr;
652
653                 struct event_filter_t event_filter;
654                 event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();
655
656                 utils::signals_manager_t& sm = utils::signals_manager_t::instance();
657                 std::map<int, std::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
658
659                 subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
660         }
661
662         if(ret)
663                 AFB_ERROR("There was something wrong with CAN device Initialization.");
664
665         return ret;
666 }