6cfcb7188bb4256a6951c6a3e3734e19b5061396
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-subscription.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-subscription.hpp"
20 #include "application.hpp"
21 #include "canutil/write.h"
22 #include "../utils/socketcan-bcm.hpp"
23 #include "../can/can-encoder.hpp"
24
25 #ifdef USE_FEATURE_ISOTP
26 #include "../utils/socketcan-isotp.hpp"
27 #endif
28
29 #ifdef USE_FEATURE_J1939
30 #include "../utils/socketcan-j1939/socketcan-j1939-data.hpp"
31 #endif
32
33 low_can_subscription_t::low_can_subscription_t()
34         : index_{-1},
35         event_filter_{},
36         event_{},
37         socket_{}
38 {}
39
40 low_can_subscription_t::low_can_subscription_t(struct event_filter_t event_filter)
41         : index_{-1},
42           event_filter_{event_filter},
43           event_{},
44           socket_{}
45  {}
46
47 low_can_subscription_t::low_can_subscription_t( low_can_subscription_t&& s)
48         : index_{s.index_},
49         event_filter_{s.event_filter_},
50         event_{},
51         socket_{std::move(s.socket_)}
52 {}
53
54 low_can_subscription_t& low_can_subscription_t::operator=(const low_can_subscription_t& s)
55 {
56         socket_ = std::move(s.socket_);
57         return *this;
58 }
59
60 low_can_subscription_t::~low_can_subscription_t()
61 {
62         if(socket_)
63                 socket_->close();
64 }
65
66 low_can_subscription_t::operator bool() const
67 {
68         return ((signal_ != nullptr || ! diagnostic_message_.empty()) && ! socket_);
69 }
70 afb_event_t low_can_subscription_t::get_event()
71 {
72         return event_;
73 }
74
75 /**
76  * @brief Set the event calling the afb_daemon_make_event function to
77  * create it and the checks its validity.
78  *
79  * @return int - 0 if OK, -1 if not
80  */
81 int low_can_subscription_t::set_event()
82 {
83         std::string event_name = get_name();
84         event_ = afb_daemon_make_event(event_name.c_str());
85         if (! afb_event_is_valid(event_))
86         {
87                 AFB_ERROR("Can't create an event for %s, something goes wrong.", event_name.c_str());
88                 return -1;
89         }
90
91         return 0;
92 }
93
94 /**
95  * @brief Subscribe to the event member of the object
96  *
97  * @param request the subscribe AFB client request which want to
98  * subscribe
99  *
100  * @return int - 0 if OK, -1 if not
101  */
102 int low_can_subscription_t::subscribe(afb_req_t request)
103 {
104         if(! afb_event_is_valid(event_))
105                 if(set_event() < 0)
106                         return -1;
107
108         return afb_req_subscribe(request, event_);
109 }
110
111 /**
112  * @brief Unsubscribe to the event member of the object
113  *
114  * @param request the unsubscribe AFB client request which want to
115  * unsubscribe
116  *
117  * @return int - 0 if OK, -1 if not
118  */
119 int low_can_subscription_t::unsubscribe(afb_req_t request)
120 {
121         return afb_req_unsubscribe(request, event_);
122 }
123
124 /**
125  * @brief Getter of index of subscription
126  *
127  * @return int Index
128  */
129 int low_can_subscription_t::get_index() const
130 {
131         return index_;
132 }
133
134 /**
135  * @brief Getter of signal of subscription
136  *
137  * @return const std::shared_ptr<signal_t> A shared pointer of the signal
138  */
139 const std::shared_ptr<signal_t> low_can_subscription_t::get_signal() const
140 {
141         return signal_;
142 }
143
144 /**
145  * @brief Check if the signal and event are the same that the subscription
146  *
147  * @param signal the signal compared
148  * @param event_filter the event_filter compared
149  * @return true if they are equal
150  * @return false if they are not equal
151  */
152 bool low_can_subscription_t::is_signal_subscription_corresponding(const std::shared_ptr<signal_t> signal, const struct event_filter_t& event_filter) const
153 {
154         return signal_ == signal && event_filter_ == event_filter;
155 }
156
157 /**
158  * @brief Getter for diagnostic messages of subscription
159  *
160  * @return const vector_ptr_diag_msg_t Vector of pointer of diagnostic message
161  */
162 const vect_ptr_diag_msg_t low_can_subscription_t::get_diagnostic_message() const
163 {
164         return diagnostic_message_;
165 }
166
167 /// @brief Retrieve a diagnostic_message subscribed from a pid
168 ///
169 /// @param[in] pid - Diagnostic messages PID to search for
170 ///
171 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
172 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(uint32_t pid) const
173 {
174         for(const auto& diag: diagnostic_message_)
175                 if(diag->get_pid() == pid)
176                         return diag;
177
178         return nullptr;
179 }
180
181 /// @brief Retrieve a diagnostic message search from its name
182 ///
183 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
184 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(const std::string& name) const
185 {
186         for(const auto& diag: diagnostic_message_)
187                 if(diag->get_name() == name)
188                         return diag;
189
190         return nullptr;
191 }
192
193 /// @brief Return the CAN signal name and empty string if not found
194 /// or no CAN signal subscribed
195 const std::string low_can_subscription_t::get_name() const
196 {
197         if (signal_ != nullptr)
198                 return signal_->get_name();
199         else if (!diagnostic_message_.empty())
200                 return "diagnostic_messages";
201
202         AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
203         return "";
204 }
205
206 /// @brief Return name from a diagnostic message from a PID
207 ///
208 /// @param[in] pid - Diagnostic message PID
209 const std::string low_can_subscription_t::get_name(uint32_t pid) const
210 {
211         if (!diagnostic_message_.empty())
212                 return get_diagnostic_message(pid)->get_name() ;
213
214         AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
215         return "";
216 }
217
218 /**
219  * @brief Getter of the frequency of the event_filter
220  *
221  * @return float The frequency
222  */
223 float low_can_subscription_t::get_frequency() const
224 {
225         return event_filter_.frequency;
226 }
227
228 /**
229  * @brief Getter of the min of the event_filter
230  *
231  * @return float The min value filtered
232  */
233 float low_can_subscription_t::get_min() const
234 {
235         return event_filter_.min;
236 }
237
238 /**
239  * @brief Getter of the max of the event_filter
240  *
241  * @return float The max value filtered
242  */
243 float low_can_subscription_t::get_max() const
244 {
245         return event_filter_.max;
246 }
247
248 bool low_can_subscription_t::get_promisc() const
249 {
250         return event_filter_.promisc;
251 }
252
253 /**
254  * @brief Getter of the rx_id of the event_filter
255  *
256  * @return canid_t The rx_id value
257  */
258 canid_t low_can_subscription_t::get_rx_id() const
259 {
260         return event_filter_.rx_id;
261 }
262
263 /**
264  * @brief Getter of the tx_id of the event_filter
265  *
266  * @return canid_t The tx_id value
267  */
268 canid_t low_can_subscription_t::get_tx_id() const
269 {
270         return event_filter_.tx_id;
271 }
272
273 /**
274  * @brief Getter of the socket of the subscription
275  *
276  * @return std::shared_ptr<utils::socketcan_t> Pointer of the socket object
277  */
278 std::shared_ptr<utils::socketcan_t> low_can_subscription_t::get_socket()
279 {
280         return socket_;
281 }
282
283 /**
284  * @brief Setter for the frequency of the event_filter
285  *
286  * @param freq The new frequency
287  */
288 void low_can_subscription_t::set_frequency(float freq)
289 {
290         event_filter_.frequency = freq;
291 }
292
293 /**
294  * @brief Setter for the min of the event_filter
295  *
296  * @param min The new min
297  */
298 void low_can_subscription_t::set_min(float min)
299 {
300         event_filter_.min = min;
301 }
302
303 /**
304  * @brief Setter for the max of the event_filter
305  *
306  * @param max The new max
307  */
308 void low_can_subscription_t::set_max(float max)
309 {
310         event_filter_.max = max;
311 }
312
313 void low_can_subscription_t::set_promisc(bool promisc)
314 {
315         event_filter_.promisc = promisc;
316 }
317
318 /**
319  * @brief Setter for the rx_id of the event_filter
320  *
321  * @param rx_id The new rx_id
322  */
323 void low_can_subscription_t::set_rx_id(canid_t rx_id)
324 {
325         event_filter_.rx_id = rx_id;
326 }
327
328 /**
329  * @brief Setter for the tx_id of the event_filter
330  *
331  * @param tx_id The new tx_id
332  */
333 void low_can_subscription_t::set_tx_id(canid_t tx_id)
334 {
335         event_filter_.tx_id = tx_id;
336 }
337
338 /**
339  * @brief Setter for the index of the subscription
340  *
341  * @param index The new index
342  */
343 void low_can_subscription_t::set_index(int index)
344 {
345         index_ = index;
346 }
347
348 /**
349  * @brief Setter for the signal of the subscription
350  *
351  * @param signal The new signal
352  */
353 void low_can_subscription_t::set_signal(std::shared_ptr<signal_t> signal)
354 {
355         signal_ = signal;
356 }
357
358
359 /// @brief Based upon which object is a subscribed CAN signal or diagnostic message
360 /// it will open the socket with the required CAN bus device name.
361 ///
362 /// @return INVALID_SOCKET on failure, else positive integer
363 int low_can_subscription_t::open_socket(low_can_subscription_t &subscription, const std::string& bus_name,  uint32_t flags)
364 {
365         int ret = -1;
366         if(! subscription.socket_)
367         {
368                 if(flags & CAN_PROTOCOL)
369                 {
370                         subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
371                         if( subscription.signal_ )
372                                 ret = subscription.socket_->open(subscription.signal_->get_message()->get_bus_device_name());
373                         else if(! subscription.diagnostic_message_.empty())
374                                 ret = subscription.socket_->open(application_t::instance().get_diagnostic_manager().get_bus_device_name());
375                         else if(! bus_name.empty())
376                                 ret = subscription.socket_->open(bus_name);
377
378                         subscription.index_ = (int)subscription.socket_->socket();
379                 }
380 #ifdef USE_FEATURE_ISOTP
381                 else if(flags & ISOTP_PROTOCOL)
382                 {
383                         std::shared_ptr<utils::socketcan_isotp_t> socket = std::make_shared<utils::socketcan_isotp_t>();
384                         if(subscription.signal_ )
385                         {
386                                 canid_t rx = NO_CAN_ID;
387                                 canid_t tx = NO_CAN_ID;
388                                 if(flags & ISOTP_SEND)
389                                 {
390                                         rx = subscription.get_rx_id();
391                                         tx = subscription.signal_->get_message()->get_id();
392                                 }
393                                 else if(flags & ISOTP_RECEIVE)
394                                 {
395                                         rx = subscription.signal_->get_message()->get_id();
396                                         tx = subscription.get_tx_id();
397                                 }
398                                 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), rx, tx);
399                                 subscription.socket_ = socket;
400                         }
401                         else if(! bus_name.empty())
402                         {
403                                 ret = socket->open(bus_name, subscription.get_rx_id(), subscription.get_tx_id());
404                                 subscription.socket_ = socket;
405                         }
406                         subscription.index_ = (int)subscription.socket_->socket();
407                 }
408 #endif
409 #ifdef USE_FEATURE_J1939
410                 else if(flags & J1939_ADDR_CLAIM_PROTOCOL)
411                 {
412                         pgn_t pgn = J1939_NO_PGN;
413                         std::shared_ptr<utils::socketcan_j1939_addressclaiming_t> socket = std::make_shared<utils::socketcan_j1939_addressclaiming_t>();
414                         if(!bus_name.empty())
415                                 ret = socket->open(bus_name, pgn);
416                         subscription.socket_ = socket;
417                         subscription.index_ = (int)subscription.socket_->socket();
418                 }
419                 else if(flags & J1939_PROTOCOL)
420                 {
421                         pgn_t pgn = J1939_NO_PGN;
422                         std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
423                         if(subscription.signal_)
424                         {
425                                 pgn = subscription.signal_->get_message()->get_id();
426                                 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), pgn);
427                         }
428                         else if(!bus_name.empty())
429                         {
430                                 ret = socket->open(bus_name, pgn);
431                         }
432
433                         if(ret)
434                                 socket->define_opt(!j1939_pgn_is_pdu1(pgn),subscription.event_filter_.promisc);
435
436                         subscription.socket_ = socket;
437                         subscription.index_ = (int)subscription.socket_->socket();
438                 }
439 #endif
440                 else
441                 {
442                         AFB_ERROR("Socket format not supported");
443                         return INVALID_SOCKET;
444                 }
445         }
446         else{
447                 ret = subscription.socket_->socket();
448         }
449         return ret;
450 }
451
452
453 /// @brief Builds a BCM message head but doesn't set can_frame.
454 ///
455 /// @returns a bcm_msg with the msg_head parts set and can_frame
456 /// zeroed.
457 struct bcm_msg low_can_subscription_t::make_bcm_head(uint32_t opcode, uint32_t can_id, uint32_t flags, const struct timeval& timeout, const struct timeval& frequency_thinning)
458 {
459         struct bcm_msg bcm_msg;
460         ::memset(&bcm_msg, 0, sizeof(bcm_msg));
461
462         bcm_msg.msg_head.opcode  = opcode;
463         bcm_msg.msg_head.can_id  = can_id;
464         bcm_msg.msg_head.flags = flags;
465         bcm_msg.msg_head.ival1.tv_sec = timeout.tv_sec ;
466         bcm_msg.msg_head.ival1.tv_usec = timeout.tv_usec;
467         bcm_msg.msg_head.ival2.tv_sec = frequency_thinning.tv_sec ;
468         bcm_msg.msg_head.ival2.tv_usec = frequency_thinning.tv_usec;
469
470
471         return bcm_msg;
472 }
473
474 /// @brief Take an existing bcm_msg struct and add a can_frame.
475 /// Currently only 1 uniq can_frame can be added, it's not possible to build
476 /// a multiplexed message with several can_frame.
477 void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct bcm_msg& bcm_msg)
478 {
479         struct can_frame cf;
480
481         if (bcm_msg.msg_head.flags & CAN_FD_FRAME)
482                 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cfd;
483         else
484         {
485                 cf.can_id = cfd.can_id;
486                 cf.can_dlc = cfd.len;
487                 ::memcpy(&cf.data, cfd.data, cfd.len);
488                 bcm_msg.frames[bcm_msg.msg_head.nframes] = cf;
489         }
490         bcm_msg.msg_head.nframes++;
491 }
492
493 /// @brief Take an existing bcm_msg struct and add a can_frame.
494 /// Currently only 1 uniq can_frame can be added, it's not possible to build
495 /// a multiplexed message with several can_frame.
496 void low_can_subscription_t::remove_last_bcm_frame(struct bcm_msg& bcm_msg)
497 {
498         struct canfd_frame cf;
499         memset(&cf, 0, sizeof(cf));
500         bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cf;
501         bcm_msg.msg_head.nframes--;
502 }
503
504 #ifdef USE_FEATURE_J1939
505 /**
506  * @brief Create a j1939 socket to read message
507  *
508  * @param subscription The subscription
509  * @param sig The signal subscribed
510  * @return int 0 if ok else -1
511  */
512 int low_can_subscription_t::create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
513 {
514         subscription.signal_= sig;
515
516         // Make sure that socket is opened.
517         if(open_socket(subscription, "", J1939_PROTOCOL) < 0)
518                         return -1;
519
520         return 0;
521 }
522 #endif
523
524 /**
525  * @brief Create an iso tp socket to read message
526  *
527  * @param subscription The subscription
528  * @param sig The signal subscribed
529  * @return int 0 if ok else -1
530  */
531 int low_can_subscription_t::create_rx_filter_isotp(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
532 {
533         subscription.signal_= sig;
534
535         // Make sure that socket is opened.
536         if(open_socket(subscription, "", ISOTP_PROTOCOL|ISOTP_RECEIVE) < 0)
537                         return -1;
538
539         return 0;
540 }
541
542 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a CAN signal
543 /// subscription
544 ///
545 /// @return 0 if ok else -1
546 int low_can_subscription_t::create_rx_filter_can(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
547 {
548         uint32_t flags_bcm;
549         struct timeval freq, timeout = {0, 0};
550         subscription.signal_= sig;
551         bool is_fd = sig->get_message()->is_fd();
552         uint32_t max_dlen = 0;
553
554         uint32_t length_msg = sig->get_message()->get_length();
555         std::vector<uint8_t> data(length_msg);
556         can_message_t cm;
557
558         if(! length_msg)
559         {
560                 AFB_ERROR("Error in the length of message with id %d", sig->get_message()->get_id());
561                 return -1;
562         }
563
564         encoder_t::encode_data(subscription.signal_, data, true, false, true);
565
566         if (is_fd)
567         {
568                 flags_bcm = SETTIMER|RX_NO_AUTOTIMER|CAN_FD_FRAME;
569                 max_dlen = CANFD_MAX_DLEN;
570         }
571         else
572         {
573                 flags_bcm = SETTIMER|RX_NO_AUTOTIMER;
574                 max_dlen = CAN_MAX_DLEN;
575         }
576
577         cm = can_message_t( max_dlen,
578                             sig->get_message()->get_id(),
579                             length_msg,
580                             false,
581                             sig->get_message()->get_flags(),
582                             data,
583                             0);
584
585         frequency_clock_t f = subscription.event_filter_.frequency == 0 ? subscription.signal_->get_frequency() : frequency_clock_t(subscription.event_filter_.frequency);
586         freq = f.get_timeval_from_period();
587
588         struct bcm_msg bcm_msg = subscription.make_bcm_head(RX_SETUP, subscription.signal_->get_message()->get_id(), flags_bcm, timeout, freq);
589
590         std::vector<canfd_frame> cfd_vect = cm.convert_to_canfd_frame_vector();
591
592         if(cfd_vect.size() > 1) //multi
593         {
594                 AFB_ERROR("Not implemented yet");
595                 return -1;
596         }
597         else if(cfd_vect.size() == 1)
598         {
599                 subscription.add_one_bcm_frame(cfd_vect[0], bcm_msg);
600         }
601         else
602         {
603                 AFB_ERROR("No data available");
604                 return -1;
605         }
606
607         return create_rx_filter_bcm(subscription, bcm_msg);
608 }
609
610 /**
611  * @brief Create the good socket to read message
612  * depending on the signal
613  *
614  * @param sig The signal subscribed
615  * @return  0 if ok else -1
616  */
617 int low_can_subscription_t::create_rx_filter(std::shared_ptr<signal_t> sig)
618 {
619         if(!sig->get_message()->is_isotp() && !sig->get_message()->is_j1939())
620                 return low_can_subscription_t::create_rx_filter_can(*this, sig);
621 #ifdef USE_FEATURE_ISOTP
622         else if(sig->get_message()->is_isotp())
623                 return low_can_subscription_t::create_rx_filter_isotp(*this, sig);
624 #endif
625 #ifdef USE_FEATURE_J1939
626         else if(sig->get_message()->is_j1939())
627                 return low_can_subscription_t::create_rx_filter_j1939(*this, sig);
628 #endif
629         AFB_ERROR("Signal can't be j1939 and isotp");
630         return -1;
631 }
632
633
634 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a
635 /// diagnostic message subscription.
636 ///
637 /// @return 0 if ok else -1
638 int low_can_subscription_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
639 {
640         diagnostic_message_.push_back(sig);
641
642         struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
643         struct timeval timeout = {0, 0};
644
645         struct bcm_msg bcm_msg =  make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER | RX_NO_AUTOTIMER | RX_FILTER_ID, timeout, freq);
646         return create_rx_filter_bcm(*this, bcm_msg);
647 }
648
649 /// @brief Create a RX_SETUP receive job used by the BCM socket directly from
650 /// a bcm_msg. The method should not be used directly but rather through the
651 /// two previous method with signal_t or diagnostic_message_t object.
652 ///
653 /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed
654 /// to the 8 classics OBD2 functional response ID
655 ///
656 /// @return 0 if ok else -1
657 int low_can_subscription_t::create_rx_filter_bcm(low_can_subscription_t &subscription, struct bcm_msg& bcm_msg)
658 {
659         // Make sure that socket is opened.
660         if(subscription.open_socket(subscription, "", CAN_PROTOCOL) < 0)
661                 return -1;
662
663         can_message_t msg = can_message_t();
664         msg.set_bcm_msg(bcm_msg);
665
666         // If it's not an OBD2 CAN ID then just add a simple RX_SETUP job
667         // else monitor all standard 8 CAN OBD2 ID response.
668         if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
669         {
670                 subscription.socket_->write_message(msg);
671                 if(! subscription.socket_)
672                         return -1;
673         }
674         else
675         {
676                 for(uint8_t i = 0; i < 8; i++)
677                 {
678                         bcm_msg.msg_head.can_id = OBD2_FUNCTIONAL_RESPONSE_START + i;
679                         msg.set_bcm_msg(bcm_msg);
680                         subscription.socket_->write_message(msg);
681                         if(! subscription.socket_)
682                                 return -1;
683                 }
684         }
685         return 0;
686 }
687
688 /// @brief Creates a TX_SEND job that is used by the BCM socket to
689 /// send a message
690 ///
691 /// @return 0 if ok else -1
692 int low_can_subscription_t::tx_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
693 {
694         can_message_t *cm = static_cast<can_message_t*>(message);
695
696         struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cm->get_id(), cm->get_flags()|TX_CP_CAN_ID); // TX_CP_CAN_ID -> copy in cfd the id of bcm
697         cm->set_bcm_msg(bcm_msg);
698
699         std::vector<canfd_frame> cfd_vect = cm->convert_to_canfd_frame_vector();
700
701         if(subscription.open_socket(subscription, bus_name, CAN_PROTOCOL) < 0)
702                 return -1;
703
704         struct bcm_msg &bcm_cm = cm->get_bcm_msg();
705
706         if(cfd_vect.size() > 1)
707         {
708                 AFB_ERROR("Multi frame BCM not implemented");
709                 return -1;
710         }
711         else if(cfd_vect.size() == 1) // raw or fd
712         {
713                 subscription.add_one_bcm_frame(cfd_vect[0], bcm_cm);
714
715                 if(subscription.socket_->write_message(*cm) < 0)
716                 {
717                         AFB_ERROR("Error write message id : %d", cfd_vect[0].can_id);
718                         return -1;
719                 }
720         }
721         else // error
722         {
723                 AFB_ERROR("Error no data available");
724                 return -1;
725         }
726
727         if(! subscription.socket_.get())
728                 return -1;
729
730         return 0;
731 }
732
733 #ifdef USE_FEATURE_J1939
734 /**
735  * @brief Allows to open socket j1939 and send j1939 messsage
736  *
737  * @param subscription The subscription
738  * @param message The j1939 message to send
739  * @param bus_name The bus name where to send message
740  * @return int  0 if ok else -1
741  */
742 int low_can_subscription_t::j1939_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
743 {
744         //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
745         //subscription.add_one_bcm_frame(cfd, bcm_msg);
746
747         if(subscription.open_socket(subscription, bus_name, J1939_PROTOCOL) < 0)
748                 return -1;
749
750         j1939_message_t *jm = static_cast<j1939_message_t*>(message);
751         jm->set_sockname(jm->get_pgn(), J1939_NO_NAME, J1939_NO_ADDR);
752         if(subscription.socket_->write_message(*jm) < 0)
753         {
754                 AFB_ERROR("Error write j1939 message");
755                 return -1;
756         }
757
758         return 0;
759 }
760 #endif
761
762
763 /**
764  * @brief Allows to open socket isotp and send can messsage
765  *
766  * @param subscription The subscription
767  * @param message The can message to send
768  * @param bus_name The bus name where to send message
769  * @return int  0 if ok else -1
770  */
771 int low_can_subscription_t::isotp_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
772 {
773         //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
774         //subscription.add_one_bcm_frame(cfd, bcm_msg);
775
776         if(subscription.open_socket(subscription, bus_name, ISOTP_PROTOCOL|ISOTP_SEND) < 0)
777                 return -1;
778
779         can_message_t *cm = static_cast<can_message_t*>(message);
780         if(subscription.socket_->write_message(*cm) < 0)
781         {
782                 AFB_ERROR("Error write iso tp message");
783                 return -1;
784         }
785
786         return 0;
787 }