Add gitlab issue/merge request templates
[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 std::shared_ptr<message_definition_t> low_can_subscription_t::get_message_definition()
284 {
285         return message_;
286 }
287
288 /**
289  * @brief Setter for the frequency of the event_filter
290  *
291  * @param freq The new frequency
292  */
293 void low_can_subscription_t::set_frequency(float freq)
294 {
295         event_filter_.frequency = freq;
296 }
297
298 /**
299  * @brief Setter for the min of the event_filter
300  *
301  * @param min The new min
302  */
303 void low_can_subscription_t::set_min(float min)
304 {
305         event_filter_.min = min;
306 }
307
308 /**
309  * @brief Setter for the max of the event_filter
310  *
311  * @param max The new max
312  */
313 void low_can_subscription_t::set_max(float max)
314 {
315         event_filter_.max = max;
316 }
317
318 void low_can_subscription_t::set_promisc(bool promisc)
319 {
320         event_filter_.promisc = promisc;
321 }
322
323 /**
324  * @brief Setter for the rx_id of the event_filter
325  *
326  * @param rx_id The new rx_id
327  */
328 void low_can_subscription_t::set_rx_id(canid_t rx_id)
329 {
330         event_filter_.rx_id = rx_id;
331 }
332
333 /**
334  * @brief Setter for the tx_id of the event_filter
335  *
336  * @param tx_id The new tx_id
337  */
338 void low_can_subscription_t::set_tx_id(canid_t tx_id)
339 {
340         event_filter_.tx_id = tx_id;
341 }
342
343 /**
344  * @brief Setter for the index of the subscription
345  *
346  * @param index The new index
347  */
348 void low_can_subscription_t::set_index(int index)
349 {
350         index_ = index;
351 }
352
353 /**
354  * @brief Setter for the signal of the subscription
355  *
356  * @param signal The new signal
357  */
358 void low_can_subscription_t::set_signal(std::shared_ptr<signal_t> signal)
359 {
360         signal_ = signal;
361 }
362
363 void low_can_subscription_t::set_message_definition(std::shared_ptr<message_definition_t> message)
364 {
365         message_ = message;
366 }
367
368 /// @brief Based upon which object is a subscribed CAN signal or diagnostic message
369 /// it will open the socket with the required CAN bus device name.
370 ///
371 /// @return INVALID_SOCKET on failure, else positive integer
372 int low_can_subscription_t::open_socket(low_can_subscription_t &subscription, const std::string& bus_name,  uint32_t flags)
373 {
374         int ret = -1;
375         if(! subscription.socket_)
376         {
377                 if(flags & CAN_PROTOCOL)
378                 {
379                         subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
380                         if( subscription.signal_ )
381                                 ret = subscription.socket_->open(subscription.signal_->get_message()->get_bus_device_name());
382                         else if(! subscription.diagnostic_message_.empty())
383                                 ret = subscription.socket_->open(application_t::instance().get_diagnostic_manager().get_bus_device_name());
384                         else if(! bus_name.empty())
385                                 ret = subscription.socket_->open(bus_name);
386
387                         subscription.index_ = (int)subscription.socket_->socket();
388                 }
389 #ifdef USE_FEATURE_ISOTP
390                 else if(flags & ISOTP_PROTOCOL)
391                 {
392                         std::shared_ptr<utils::socketcan_isotp_t> socket = std::make_shared<utils::socketcan_isotp_t>();
393                         if(subscription.signal_ )
394                         {
395                                 canid_t rx = NO_CAN_ID;
396                                 canid_t tx = NO_CAN_ID;
397                                 if(flags & ISOTP_SEND)
398                                 {
399                                         rx = subscription.get_rx_id();
400                                         tx = subscription.signal_->get_message()->get_id();
401                                 }
402                                 else if(flags & ISOTP_RECEIVE)
403                                 {
404                                         rx = subscription.signal_->get_message()->get_id();
405                                         tx = subscription.get_tx_id();
406                                 }
407                                 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), rx, tx);
408                                 subscription.socket_ = socket;
409                         }
410                         else if(! bus_name.empty())
411                         {
412                                 ret = socket->open(bus_name, subscription.get_rx_id(), subscription.get_tx_id());
413                                 subscription.socket_ = socket;
414                         }
415                         subscription.index_ = (int)subscription.socket_->socket();
416                 }
417 #endif
418 #ifdef USE_FEATURE_J1939
419                 else if(flags & J1939_ADDR_CLAIM_PROTOCOL)
420                 {
421                         pgn_t pgn = J1939_NO_PGN;
422                         std::shared_ptr<utils::socketcan_j1939_addressclaiming_t> socket = std::make_shared<utils::socketcan_j1939_addressclaiming_t>();
423                         if(!bus_name.empty())
424                                 ret = socket->open(bus_name, pgn);
425                         subscription.socket_ = socket;
426                         subscription.index_ = (int)subscription.socket_->socket();
427                 }
428                 else if(flags & J1939_PROTOCOL)
429                 {
430                         pgn_t pgn = J1939_NO_PGN;
431                         std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
432                         if(subscription.signal_)
433                         {
434                                 pgn = subscription.signal_->get_message()->get_id();
435                                 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), pgn);
436                         }
437                         else if(!bus_name.empty())
438                         {
439                                 ret = socket->open(bus_name, pgn);
440                         }
441
442                         if(ret)
443                                 socket->define_opt(!j1939_pgn_is_pdu1(pgn),subscription.event_filter_.promisc);
444
445                         subscription.socket_ = socket;
446                         subscription.index_ = (int)subscription.socket_->socket();
447                 }
448 #endif
449                 else
450                 {
451                         AFB_ERROR("Socket format not supported");
452                         return INVALID_SOCKET;
453                 }
454         }
455         else{
456                 ret = subscription.socket_->socket();
457         }
458         return ret;
459 }
460
461
462 /// @brief Builds a BCM message head but doesn't set can_frame.
463 ///
464 /// @returns a bcm_msg with the msg_head parts set and can_frame
465 /// zeroed.
466 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)
467 {
468         struct bcm_msg bcm_msg;
469         ::memset(&bcm_msg, 0, sizeof(bcm_msg));
470
471         bcm_msg.msg_head.opcode  = opcode;
472         bcm_msg.msg_head.can_id  = can_id;
473         bcm_msg.msg_head.flags = flags;
474         bcm_msg.msg_head.ival1.tv_sec = timeout.tv_sec ;
475         bcm_msg.msg_head.ival1.tv_usec = timeout.tv_usec;
476         bcm_msg.msg_head.ival2.tv_sec = frequency_thinning.tv_sec ;
477         bcm_msg.msg_head.ival2.tv_usec = frequency_thinning.tv_usec;
478
479
480         return bcm_msg;
481 }
482
483 /// @brief Take an existing bcm_msg struct and add a can_frame.
484 /// Currently only 1 uniq can_frame can be added, it's not possible to build
485 /// a multiplexed message with several can_frame.
486 void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct bcm_msg& bcm_msg)
487 {
488         struct can_frame cf;
489
490         if (bcm_msg.msg_head.flags & CAN_FD_FRAME)
491                 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cfd;
492         else
493         {
494                 cf.can_id = cfd.can_id;
495                 cf.can_dlc = cfd.len;
496                 ::memcpy(&cf.data, cfd.data, cfd.len);
497                 bcm_msg.frames[bcm_msg.msg_head.nframes] = cf;
498         }
499         bcm_msg.msg_head.nframes++;
500 }
501
502 /// @brief Take an existing bcm_msg struct and add a can_frame.
503 /// Currently only 1 uniq can_frame can be added, it's not possible to build
504 /// a multiplexed message with several can_frame.
505 void low_can_subscription_t::remove_last_bcm_frame(struct bcm_msg& bcm_msg)
506 {
507         struct canfd_frame cf;
508         memset(&cf, 0, sizeof(cf));
509         bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cf;
510         bcm_msg.msg_head.nframes--;
511 }
512
513 #ifdef USE_FEATURE_J1939
514 /**
515  * @brief Create a j1939 socket to read message
516  *
517  * @param subscription The subscription
518  * @param sig The signal subscribed
519  * @return int 0 if ok else -1
520  */
521 int low_can_subscription_t::create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
522 {
523         subscription.signal_= sig;
524
525         // Make sure that socket is opened.
526         if(open_socket(subscription, "", J1939_PROTOCOL) < 0)
527                         return -1;
528
529         return 0;
530 }
531 #endif
532
533 /**
534  * @brief Create an iso tp socket to read message
535  *
536  * @param subscription The subscription
537  * @param sig The signal subscribed
538  * @return int 0 if ok else -1
539  */
540 int low_can_subscription_t::create_rx_filter_isotp(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
541 {
542         subscription.signal_= sig;
543
544         // Make sure that socket is opened.
545         if(open_socket(subscription, "", ISOTP_PROTOCOL|ISOTP_RECEIVE) < 0)
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         subscription.signal_= sig;
560         bool is_fd = sig->get_message()->is_fd();
561         uint32_t max_dlen = 0;
562
563         uint32_t length_msg = sig->get_message()->get_length();
564         std::vector<uint8_t> data(length_msg);
565         can_message_t cm;
566
567         if(! length_msg)
568         {
569                 AFB_ERROR("Error in the length of message with id %d", sig->get_message()->get_id());
570                 return -1;
571         }
572
573         encoder_t::encode_data(subscription.signal_, data, true, false, true);
574
575         if (is_fd)
576         {
577                 flags_bcm = SETTIMER|RX_NO_AUTOTIMER|CAN_FD_FRAME;
578                 max_dlen = CANFD_MAX_DLEN;
579         }
580         else
581         {
582                 flags_bcm = SETTIMER|RX_NO_AUTOTIMER;
583                 max_dlen = CAN_MAX_DLEN;
584         }
585
586         cm = can_message_t( max_dlen,
587                             sig->get_message()->get_id(),
588                             length_msg,
589                             false,
590                             sig->get_message()->get_flags(),
591                             data,
592                             0);
593
594         frequency_clock_t f = subscription.event_filter_.frequency == 0 ? subscription.signal_->get_frequency() : frequency_clock_t(subscription.event_filter_.frequency);
595         freq = f.get_timeval_from_period();
596
597         struct bcm_msg bcm_msg = subscription.make_bcm_head(RX_SETUP, subscription.signal_->get_message()->get_id(), flags_bcm, timeout, freq);
598
599         std::vector<canfd_frame> cfd_vect = cm.convert_to_canfd_frame_vector();
600
601         if(cfd_vect.size() > 1) //multi
602         {
603                 AFB_ERROR("Not implemented yet");
604                 return -1;
605         }
606         else if(cfd_vect.size() == 1)
607         {
608                 subscription.add_one_bcm_frame(cfd_vect[0], bcm_msg);
609         }
610         else
611         {
612                 AFB_ERROR("No data available");
613                 return -1;
614         }
615
616         return create_rx_filter_bcm(subscription, bcm_msg);
617 }
618
619
620 int low_can_subscription_t::create_rx_filter(std::shared_ptr<message_definition_t> msg)
621 {
622         std::shared_ptr<signal_t> signal_message =
623                 std::make_shared<signal_t>(signal_t{msg->get_name(),
624                                                     0,
625                                                     msg->get_length() * 8,
626                                                     1.00000f,
627                                                     0.00000f,
628                                                     0,
629                                                     0,
630                                                     frequency_clock_t(0.00000f),
631                                                     true,
632                                                     false,
633                                                     {},
634                                                     true,
635                                                     nullptr,
636                                                     nullptr,
637                                                     false,
638                                                     std::make_pair<bool, int>(false, 0),
639                                                     static_cast<sign_t>(0),
640                                                     -1,
641                                                     ""});
642
643         signal_message->set_parent(msg);
644         return create_rx_filter(signal_message);
645 }
646
647
648 /**
649  * @brief Create the good socket to read message
650  * depending on the signal
651  *
652  * @param sig The signal subscribed
653  * @return  0 if ok else -1
654  */
655 int low_can_subscription_t::create_rx_filter(std::shared_ptr<signal_t> sig)
656 {
657         if(!sig->get_message()->is_isotp() && !sig->get_message()->is_j1939())
658                 return low_can_subscription_t::create_rx_filter_can(*this, sig);
659 #ifdef USE_FEATURE_ISOTP
660         else if(sig->get_message()->is_isotp())
661                 return low_can_subscription_t::create_rx_filter_isotp(*this, sig);
662 #endif
663 #ifdef USE_FEATURE_J1939
664         else if(sig->get_message()->is_j1939())
665                 return low_can_subscription_t::create_rx_filter_j1939(*this, sig);
666 #endif
667         AFB_ERROR("Signal can't be created (check config)");
668         return -1;
669 }
670
671
672 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a
673 /// diagnostic message subscription.
674 ///
675 /// @return 0 if ok else -1
676 int low_can_subscription_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
677 {
678         diagnostic_message_.push_back(sig);
679
680         struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
681         struct timeval timeout = {0, 0};
682
683         struct bcm_msg bcm_msg =  make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER | RX_NO_AUTOTIMER | RX_FILTER_ID, timeout, freq);
684         return create_rx_filter_bcm(*this, bcm_msg);
685 }
686
687 /// @brief Create a RX_SETUP receive job used by the BCM socket directly from
688 /// a bcm_msg. The method should not be used directly but rather through the
689 /// two previous method with signal_t or diagnostic_message_t object.
690 ///
691 /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed
692 /// to the 8 classics OBD2 functional response ID
693 ///
694 /// @return 0 if ok else -1
695 int low_can_subscription_t::create_rx_filter_bcm(low_can_subscription_t &subscription, struct bcm_msg& bcm_msg)
696 {
697         // Make sure that socket is opened.
698         if(subscription.open_socket(subscription, "", CAN_PROTOCOL) < 0)
699                 return -1;
700
701         can_message_t msg = can_message_t();
702         msg.set_bcm_msg(bcm_msg);
703
704         // If it's not an OBD2 CAN ID then just add a simple RX_SETUP job
705         // else monitor all standard 8 CAN OBD2 ID response.
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, CAN_PROTOCOL) < 0)
740                 return -1;
741
742         struct bcm_msg &bcm_cm = cm->get_bcm_msg();
743
744         if(cfd_vect.size() > 1)
745         {
746                 AFB_ERROR("Multi frame BCM not implemented");
747                 return -1;
748         }
749         else if(cfd_vect.size() == 1) // raw or fd
750         {
751                 subscription.add_one_bcm_frame(cfd_vect[0], bcm_cm);
752
753                 if(subscription.socket_->write_message(*cm) < 0)
754                 {
755                         AFB_ERROR("Error write message id : %d", cfd_vect[0].can_id);
756                         return -1;
757                 }
758         }
759         else // error
760         {
761                 AFB_ERROR("Error no data available");
762                 return -1;
763         }
764
765         if(! subscription.socket_.get())
766                 return -1;
767
768         return 0;
769 }
770
771 #ifdef USE_FEATURE_J1939
772 /**
773  * @brief Allows to open socket j1939 and send j1939 messsage
774  *
775  * @param subscription The subscription
776  * @param message The j1939 message to send
777  * @param bus_name The bus name where to send message
778  * @return int  0 if ok else -1
779  */
780 int low_can_subscription_t::j1939_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
781 {
782         //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
783         //subscription.add_one_bcm_frame(cfd, bcm_msg);
784
785         if(subscription.open_socket(subscription, bus_name, J1939_PROTOCOL) < 0)
786                 return -1;
787
788         j1939_message_t *jm = static_cast<j1939_message_t*>(message);
789         jm->set_sockname(jm->get_pgn(), J1939_NO_NAME, J1939_NO_ADDR);
790         if(subscription.socket_->write_message(*jm) < 0)
791         {
792                 AFB_ERROR("Error write j1939 message");
793                 return -1;
794         }
795
796         return 0;
797 }
798 #endif
799
800
801 /**
802  * @brief Allows to open socket isotp and send can messsage
803  *
804  * @param subscription The subscription
805  * @param message The can message to send
806  * @param bus_name The bus name where to send message
807  * @return int  0 if ok else -1
808  */
809 int low_can_subscription_t::isotp_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
810 {
811         //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
812         //subscription.add_one_bcm_frame(cfd, bcm_msg);
813
814         if(subscription.open_socket(subscription, bus_name, ISOTP_PROTOCOL|ISOTP_SEND) < 0)
815                 return -1;
816
817         can_message_t *cm = static_cast<can_message_t*>(message);
818         if(subscription.socket_->write_message(*cm) < 0)
819         {
820                 AFB_ERROR("Error write iso tp message");
821                 return -1;
822         }
823
824         return 0;
825 }