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