2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
4 * Author "Loic Collignon" <loic.collignon@iot.bzh>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
25 #ifdef USE_FEATURE_ISOTP
26 #include "../utils/socketcan-isotp.hpp"
29 #ifdef USE_FEATURE_J1939
30 #include "../utils/socketcan-j1939/socketcan-j1939-data.hpp"
33 low_can_subscription_t::low_can_subscription_t()
40 low_can_subscription_t::low_can_subscription_t(struct event_filter_t event_filter)
42 event_filter_{event_filter},
47 low_can_subscription_t::low_can_subscription_t( low_can_subscription_t&& s)
49 event_filter_{s.event_filter_},
51 socket_{std::move(s.socket_)}
54 low_can_subscription_t& low_can_subscription_t::operator=(const low_can_subscription_t& s)
56 socket_ = std::move(s.socket_);
60 low_can_subscription_t::~low_can_subscription_t()
66 low_can_subscription_t::operator bool() const
68 return ((signal_ != nullptr || ! diagnostic_message_.empty()) && ! socket_);
70 afb_event_t low_can_subscription_t::get_event()
76 * @brief Set the event calling the afb_daemon_make_event function to
77 * create it and the checks its validity.
79 * @return int - 0 if OK, -1 if not
81 int low_can_subscription_t::set_event()
83 std::string event_name = get_name();
84 event_ = afb_daemon_make_event(event_name.c_str());
85 if (! afb_event_is_valid(event_))
87 AFB_ERROR("Can't create an event for %s, something goes wrong.", event_name.c_str());
95 * @brief Subscribe to the event member of the object
97 * @param request the subscribe AFB client request which want to
100 * @return int - 0 if OK, -1 if not
102 int low_can_subscription_t::subscribe(afb_req_t request)
104 if(! afb_event_is_valid(event_))
108 return afb_req_subscribe(request, event_);
112 * @brief Unsubscribe to the event member of the object
114 * @param request the unsubscribe AFB client request which want to
117 * @return int - 0 if OK, -1 if not
119 int low_can_subscription_t::unsubscribe(afb_req_t request)
121 return afb_req_unsubscribe(request, event_);
125 * @brief Getter of index of subscription
129 int low_can_subscription_t::get_index() const
135 * @brief Getter of signal of subscription
137 * @return const std::shared_ptr<signal_t> A shared pointer of the signal
139 const std::shared_ptr<signal_t> low_can_subscription_t::get_signal() const
145 * @brief Check if the signal and event are the same that the subscription
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
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
154 return signal_ == signal && event_filter_ == event_filter;
158 * @brief Getter for diagnostic messages of subscription
160 * @return const vector_ptr_diag_msg_t Vector of pointer of diagnostic message
162 const vect_ptr_diag_msg_t low_can_subscription_t::get_diagnostic_message() const
164 return diagnostic_message_;
167 /// @brief Retrieve a diagnostic_message subscribed from a pid
169 /// @param[in] pid - Diagnostic messages PID to search for
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
174 for(const auto& diag: diagnostic_message_)
175 if(diag->get_pid() == pid)
181 /// @brief Retrieve a diagnostic message search from its name
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
186 for(const auto& diag: diagnostic_message_)
187 if(diag->get_name() == name)
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
197 if (signal_ != nullptr)
198 return signal_->get_name();
199 else if (!diagnostic_message_.empty())
200 return "diagnostic_messages";
202 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
206 /// @brief Return name from a diagnostic message from a PID
208 /// @param[in] pid - Diagnostic message PID
209 const std::string low_can_subscription_t::get_name(uint32_t pid) const
211 if (!diagnostic_message_.empty())
212 return get_diagnostic_message(pid)->get_name() ;
214 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
219 * @brief Getter of the frequency of the event_filter
221 * @return float The frequency
223 float low_can_subscription_t::get_frequency() const
225 return event_filter_.frequency;
229 * @brief Getter of the min of the event_filter
231 * @return float The min value filtered
233 float low_can_subscription_t::get_min() const
235 return event_filter_.min;
239 * @brief Getter of the max of the event_filter
241 * @return float The max value filtered
243 float low_can_subscription_t::get_max() const
245 return event_filter_.max;
248 bool low_can_subscription_t::get_promisc() const
250 return event_filter_.promisc;
254 * @brief Getter of the rx_id of the event_filter
256 * @return canid_t The rx_id value
258 canid_t low_can_subscription_t::get_rx_id() const
260 return event_filter_.rx_id;
264 * @brief Getter of the tx_id of the event_filter
266 * @return canid_t The tx_id value
268 canid_t low_can_subscription_t::get_tx_id() const
270 return event_filter_.tx_id;
274 * @brief Getter of the socket of the subscription
276 * @return std::shared_ptr<utils::socketcan_t> Pointer of the socket object
278 std::shared_ptr<utils::socketcan_t> low_can_subscription_t::get_socket()
284 * @brief Setter for the frequency of the event_filter
286 * @param freq The new frequency
288 void low_can_subscription_t::set_frequency(float freq)
290 event_filter_.frequency = freq;
294 * @brief Setter for the min of the event_filter
296 * @param min The new min
298 void low_can_subscription_t::set_min(float min)
300 event_filter_.min = min;
304 * @brief Setter for the max of the event_filter
306 * @param max The new max
308 void low_can_subscription_t::set_max(float max)
310 event_filter_.max = max;
313 void low_can_subscription_t::set_promisc(bool promisc)
315 event_filter_.promisc = promisc;
319 * @brief Setter for the rx_id of the event_filter
321 * @param rx_id The new rx_id
323 void low_can_subscription_t::set_rx_id(canid_t rx_id)
325 event_filter_.rx_id = rx_id;
329 * @brief Setter for the tx_id of the event_filter
331 * @param tx_id The new tx_id
333 void low_can_subscription_t::set_tx_id(canid_t tx_id)
335 event_filter_.tx_id = tx_id;
339 * @brief Setter for the index of the subscription
341 * @param index The new index
343 void low_can_subscription_t::set_index(int index)
349 * @brief Setter for the signal of the subscription
351 * @param signal The new signal
353 void low_can_subscription_t::set_signal(std::shared_ptr<signal_t> signal)
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.
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)
366 if(! subscription.socket_)
368 if(flags & CAN_PROTOCOL)
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);
378 subscription.index_ = (int)subscription.socket_->socket();
380 #ifdef USE_FEATURE_ISOTP
381 else if(flags & ISOTP_PROTOCOL)
383 std::shared_ptr<utils::socketcan_isotp_t> socket = std::make_shared<utils::socketcan_isotp_t>();
384 if(subscription.signal_ )
386 canid_t rx = NO_CAN_ID;
387 canid_t tx = NO_CAN_ID;
388 if(flags & ISOTP_SEND)
390 rx = subscription.get_rx_id();
391 tx = subscription.signal_->get_message()->get_id();
393 else if(flags & ISOTP_RECEIVE)
395 rx = subscription.signal_->get_message()->get_id();
396 tx = subscription.get_tx_id();
398 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), rx, tx);
399 subscription.socket_ = socket;
401 else if(! bus_name.empty())
403 ret = socket->open(bus_name, subscription.get_rx_id(), subscription.get_tx_id());
404 subscription.socket_ = socket;
406 subscription.index_ = (int)subscription.socket_->socket();
409 #ifdef USE_FEATURE_J1939
410 else if(flags & J1939_ADDR_CLAIM_PROTOCOL)
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();
419 else if(flags & J1939_PROTOCOL)
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_)
425 pgn = subscription.signal_->get_message()->get_id();
426 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), pgn);
428 else if(!bus_name.empty())
430 ret = socket->open(bus_name, pgn);
434 socket->define_opt(!j1939_pgn_is_pdu1(pgn),subscription.event_filter_.promisc);
436 subscription.socket_ = socket;
437 subscription.index_ = (int)subscription.socket_->socket();
442 AFB_ERROR("Socket format not supported");
443 return INVALID_SOCKET;
447 ret = subscription.socket_->socket();
453 /// @brief Builds a BCM message head but doesn't set can_frame.
455 /// @returns a bcm_msg with the msg_head parts set and can_frame
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)
459 struct bcm_msg bcm_msg;
460 ::memset(&bcm_msg, 0, sizeof(bcm_msg));
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;
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)
481 if (bcm_msg.msg_head.flags & CAN_FD_FRAME)
482 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cfd;
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;
490 bcm_msg.msg_head.nframes++;
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)
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--;
504 #ifdef USE_FEATURE_J1939
506 * @brief Create a j1939 socket to read message
508 * @param subscription The subscription
509 * @param sig The signal subscribed
510 * @return int 0 if ok else -1
512 int low_can_subscription_t::create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
514 subscription.signal_= sig;
516 // Make sure that socket is opened.
517 if(open_socket(subscription, "", J1939_PROTOCOL) < 0)
525 * @brief Create an iso tp socket to read message
527 * @param subscription The subscription
528 * @param sig The signal subscribed
529 * @return int 0 if ok else -1
531 int low_can_subscription_t::create_rx_filter_isotp(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
533 subscription.signal_= sig;
535 // Make sure that socket is opened.
536 if(open_socket(subscription, "", ISOTP_PROTOCOL|ISOTP_RECEIVE) < 0)
542 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a CAN signal
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)
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;
554 uint32_t length_msg = sig->get_message()->get_length();
555 std::vector<uint8_t> data(length_msg);
560 AFB_ERROR("Error in the length of message with id %d", sig->get_message()->get_id());
564 encoder_t::encode_data(subscription.signal_, data, true, false, true);
568 flags_bcm = SETTIMER|RX_NO_AUTOTIMER|CAN_FD_FRAME;
569 max_dlen = CANFD_MAX_DLEN;
573 flags_bcm = SETTIMER|RX_NO_AUTOTIMER;
574 max_dlen = CAN_MAX_DLEN;
577 cm = can_message_t( max_dlen,
578 sig->get_message()->get_id(),
581 sig->get_message()->get_flags(),
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();
588 struct bcm_msg bcm_msg = subscription.make_bcm_head(RX_SETUP, subscription.signal_->get_message()->get_id(), flags_bcm, timeout, freq);
590 std::vector<canfd_frame> cfd_vect = cm.convert_to_canfd_frame_vector();
592 if(cfd_vect.size() > 1) //multi
594 AFB_ERROR("Not implemented yet");
597 else if(cfd_vect.size() == 1)
599 subscription.add_one_bcm_frame(cfd_vect[0], bcm_msg);
603 AFB_ERROR("No data available");
607 return create_rx_filter_bcm(subscription, bcm_msg);
611 * @brief Create the good socket to read message
612 * depending on the signal
614 * @param sig The signal subscribed
615 * @return 0 if ok else -1
617 int low_can_subscription_t::create_rx_filter(std::shared_ptr<signal_t> sig)
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);
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);
629 AFB_ERROR("Signal can't be j1939 and isotp");
634 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a
635 /// diagnostic message subscription.
637 /// @return 0 if ok else -1
638 int low_can_subscription_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
640 diagnostic_message_.push_back(sig);
642 struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
643 struct timeval timeout = {0, 0};
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);
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.
653 /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed
654 /// to the 8 classics OBD2 functional response ID
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)
659 // Make sure that socket is opened.
660 if(subscription.open_socket(subscription, "", CAN_PROTOCOL) < 0)
663 can_message_t msg = can_message_t();
664 msg.set_bcm_msg(bcm_msg);
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)
670 subscription.socket_->write_message(msg);
671 if(! subscription.socket_)
676 for(uint8_t i = 0; i < 8; i++)
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_)
688 /// @brief Creates a TX_SEND job that is used by the BCM socket to
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)
694 can_message_t *cm = static_cast<can_message_t*>(message);
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);
699 std::vector<canfd_frame> cfd_vect = cm->convert_to_canfd_frame_vector();
701 if(subscription.open_socket(subscription, bus_name, CAN_PROTOCOL) < 0)
704 struct bcm_msg &bcm_cm = cm->get_bcm_msg();
706 if(cfd_vect.size() > 1)
708 AFB_ERROR("Multi frame BCM not implemented");
711 else if(cfd_vect.size() == 1) // raw or fd
713 subscription.add_one_bcm_frame(cfd_vect[0], bcm_cm);
715 if(subscription.socket_->write_message(*cm) < 0)
717 AFB_ERROR("Error write message id : %d", cfd_vect[0].can_id);
723 AFB_ERROR("Error no data available");
727 if(! subscription.socket_.get())
733 #ifdef USE_FEATURE_J1939
735 * @brief Allows to open socket j1939 and send j1939 messsage
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
742 int low_can_subscription_t::j1939_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
744 //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
745 //subscription.add_one_bcm_frame(cfd, bcm_msg);
747 if(subscription.open_socket(subscription, bus_name, J1939_PROTOCOL) < 0)
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)
754 AFB_ERROR("Error write j1939 message");
764 * @brief Allows to open socket isotp and send can messsage
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
771 int low_can_subscription_t::isotp_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
773 //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
774 //subscription.add_one_bcm_frame(cfd, bcm_msg);
776 if(subscription.open_socket(subscription, bus_name, ISOTP_PROTOCOL|ISOTP_SEND) < 0)
779 can_message_t *cm = static_cast<can_message_t*>(message);
780 if(subscription.socket_->write_message(*cm) < 0)
782 AFB_ERROR("Error write iso tp message");