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_))
111 return afb_req_subscribe(request, event_);
115 * @brief Unsubscribe to the event member of the object
117 * @param request the unsubscribe AFB client request which want to
120 * @return int - 0 if OK, -1 if not
122 int low_can_subscription_t::unsubscribe(afb_req_t request)
124 return afb_req_unsubscribe(request, event_);
128 * @brief Getter of index of subscription
132 int low_can_subscription_t::get_index() const
138 * @brief Getter of signal of subscription
140 * @return const std::shared_ptr<signal_t> A shared pointer of the signal
142 const std::shared_ptr<signal_t> low_can_subscription_t::get_signal() const
148 * @brief Check if the signal and event are the same that the subscription
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
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
157 return signal_ == signal && event_filter_ == event_filter;
161 * @brief Getter for diagnostic messages of subscription
163 * @return const vector_ptr_diag_msg_t Vector of pointer of diagnostic message
165 const vect_ptr_diag_msg_t low_can_subscription_t::get_diagnostic_message() const
167 return diagnostic_message_;
170 /// @brief Retrieve a diagnostic_message subscribed from a pid
172 /// @param[in] pid - Diagnostic messages PID to search for
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
177 for(const auto& diag: diagnostic_message_)
179 if(diag->get_pid() == pid)
187 /// @brief Retrieve a diagnostic message search from its name
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
192 for(const auto& diag: diagnostic_message_)
194 if(diag->get_name() == name)
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
206 if (signal_ != nullptr)
207 return signal_->get_name();
208 else if (!diagnostic_message_.empty())
209 return "diagnostic_messages";
211 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
215 /// @brief Return name from a diagnostic message from a PID
217 /// @param[in] pid - Diagnostic message PID
218 const std::string low_can_subscription_t::get_name(uint32_t pid) const
220 if (!diagnostic_message_.empty())
221 return get_diagnostic_message(pid)->get_name() ;
223 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
228 * @brief Getter of the frequency of the event_filter
230 * @return float The frequency
232 float low_can_subscription_t::get_frequency() const
234 return event_filter_.frequency;
238 * @brief Getter of the min of the event_filter
240 * @return float The min value filtered
242 float low_can_subscription_t::get_min() const
244 return event_filter_.min;
248 * @brief Getter of the max of the event_filter
250 * @return float The max value filtered
252 float low_can_subscription_t::get_max() const
254 return event_filter_.max;
257 bool low_can_subscription_t::get_promisc() const
259 return event_filter_.promisc;
263 * @brief Getter of the rx_id of the event_filter
265 * @return canid_t The rx_id value
267 canid_t low_can_subscription_t::get_rx_id() const
269 return event_filter_.rx_id;
273 * @brief Getter of the tx_id of the event_filter
275 * @return canid_t The tx_id value
277 canid_t low_can_subscription_t::get_tx_id() const
279 return event_filter_.tx_id;
283 * @brief Getter of the socket of the subscription
285 * @return std::shared_ptr<utils::socketcan_t> Pointer of the socket object
287 std::shared_ptr<utils::socketcan_t> low_can_subscription_t::get_socket()
293 * @brief Setter for the frequency of the event_filter
295 * @param freq The new frequency
297 void low_can_subscription_t::set_frequency(float freq)
299 event_filter_.frequency = freq;
303 * @brief Setter for the min of the event_filter
305 * @param min The new min
307 void low_can_subscription_t::set_min(float min)
309 event_filter_.min = min;
313 * @brief Setter for the max of the event_filter
315 * @param max The new max
317 void low_can_subscription_t::set_max(float max)
319 event_filter_.max = max;
322 void low_can_subscription_t::set_promisc(bool promisc)
324 event_filter_.promisc = promisc;
328 * @brief Setter for the rx_id of the event_filter
330 * @param rx_id The new rx_id
332 void low_can_subscription_t::set_rx_id(canid_t rx_id)
334 event_filter_.rx_id = rx_id;
338 * @brief Setter for the tx_id of the event_filter
340 * @param tx_id The new tx_id
342 void low_can_subscription_t::set_tx_id(canid_t tx_id)
344 event_filter_.tx_id = tx_id;
348 * @brief Setter for the index of the subscription
350 * @param index The new index
352 void low_can_subscription_t::set_index(int index)
358 * @brief Setter for the signal of the subscription
360 * @param signal The new signal
362 void low_can_subscription_t::set_signal(std::shared_ptr<signal_t> signal)
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.
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)
375 if(! subscription.socket_)
377 if(flags & CAN_PROTOCOL)
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);
387 subscription.index_ = (int)subscription.socket_->socket();
389 #ifdef USE_FEATURE_ISOTP
390 else if(flags & ISOTP_PROTOCOL)
392 std::shared_ptr<utils::socketcan_isotp_t> socket = std::make_shared<utils::socketcan_isotp_t>();
393 if(subscription.signal_ )
395 canid_t rx = NO_CAN_ID;
396 canid_t tx = NO_CAN_ID;
397 if(flags & ISOTP_SEND)
399 rx = subscription.get_rx_id();
400 tx = subscription.signal_->get_message()->get_id();
402 else if(flags & ISOTP_RECEIVE)
404 rx = subscription.signal_->get_message()->get_id();
405 tx = subscription.get_tx_id();
407 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), rx, tx);
408 subscription.socket_ = socket;
410 else if(! bus_name.empty())
412 ret = socket->open(bus_name, subscription.get_rx_id(), subscription.get_tx_id());
413 subscription.socket_ = socket;
415 subscription.index_ = (int)subscription.socket_->socket();
418 #ifdef USE_FEATURE_J1939
419 else if(flags & J1939_ADDR_CLAIM_PROTOCOL)
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())
425 ret = socket->open(bus_name, pgn);
427 subscription.socket_ = socket;
428 subscription.index_ = (int)subscription.socket_->socket();
430 else if(flags & J1939_PROTOCOL)
432 pgn_t pgn = J1939_NO_PGN;
433 std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
434 if(subscription.signal_)
436 pgn = subscription.signal_->get_message()->get_id();
437 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), pgn);
439 else if(!bus_name.empty())
441 ret = socket->open(bus_name, pgn);
445 socket->define_opt(!j1939_pgn_is_pdu1(pgn),subscription.event_filter_.promisc);
447 subscription.socket_ = socket;
448 subscription.index_ = (int)subscription.socket_->socket();
453 AFB_ERROR("Socket format not supported");
454 return INVALID_SOCKET;
458 ret = subscription.socket_->socket();
464 /// @brief Builds a BCM message head but doesn't set can_frame.
466 /// @returns a bcm_msg with the msg_head parts set and can_frame
468 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)
470 struct bcm_msg bcm_msg;
471 ::memset(&bcm_msg, 0, sizeof(bcm_msg));
473 bcm_msg.msg_head.opcode = opcode;
474 bcm_msg.msg_head.can_id = can_id;
475 bcm_msg.msg_head.flags = flags;
476 bcm_msg.msg_head.ival1.tv_sec = timeout.tv_sec ;
477 bcm_msg.msg_head.ival1.tv_usec = timeout.tv_usec;
478 bcm_msg.msg_head.ival2.tv_sec = frequency_thinning.tv_sec ;
479 bcm_msg.msg_head.ival2.tv_usec = frequency_thinning.tv_usec;
485 /// @brief Take an existing bcm_msg struct and add a can_frame.
486 /// Currently only 1 uniq can_frame can be added, it's not possible to build
487 /// a multiplexed message with several can_frame.
488 void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct bcm_msg& bcm_msg)
492 if (bcm_msg.msg_head.flags & CAN_FD_FRAME)
493 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cfd;
496 cf.can_id = cfd.can_id;
497 cf.can_dlc = cfd.len;
498 ::memcpy(&cf.data, cfd.data, cfd.len);
499 bcm_msg.frames[bcm_msg.msg_head.nframes] = cf;
501 bcm_msg.msg_head.nframes++;
504 /// @brief Take an existing bcm_msg struct and add a can_frame.
505 /// Currently only 1 uniq can_frame can be added, it's not possible to build
506 /// a multiplexed message with several can_frame.
507 void low_can_subscription_t::remove_last_bcm_frame(struct bcm_msg& bcm_msg)
509 struct canfd_frame cf;
510 memset(&cf, 0, sizeof(cf));
511 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cf;
512 bcm_msg.msg_head.nframes--;
515 #ifdef USE_FEATURE_J1939
517 * @brief Create a j1939 socket to read message
519 * @param subscription The subscription
520 * @param sig The signal subscribed
521 * @return int 0 if ok else -1
523 int low_can_subscription_t::create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
525 subscription.signal_= sig;
527 // Make sure that socket is opened.
528 if(open_socket(subscription, "", J1939_PROTOCOL) < 0)
536 * @brief Create an iso tp socket to read message
538 * @param subscription The subscription
539 * @param sig The signal subscribed
540 * @return int 0 if ok else -1
542 int low_can_subscription_t::create_rx_filter_isotp(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
544 subscription.signal_= sig;
546 // Make sure that socket is opened.
547 if(open_socket(subscription, "", ISOTP_PROTOCOL|ISOTP_RECEIVE) < 0)
553 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a CAN signal
556 /// @return 0 if ok else -1
557 int low_can_subscription_t::create_rx_filter_can(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
560 struct timeval freq, timeout = {0, 0};
561 subscription.signal_= sig;
562 bool is_fd = sig->get_message()->is_fd();
563 uint32_t max_dlen = 0;
565 uint32_t length_msg = sig->get_message()->get_length();
566 std::vector<uint8_t> data(length_msg);
571 AFB_ERROR("Error in the length of message with id %d", sig->get_message()->get_id());
575 encoder_t::encode_data(subscription.signal_, data, true, false, true);
579 flags_bcm = SETTIMER|RX_NO_AUTOTIMER|CAN_FD_FRAME;
580 max_dlen = CANFD_MAX_DLEN;
584 flags_bcm = SETTIMER|RX_NO_AUTOTIMER;
585 max_dlen = CAN_MAX_DLEN;
588 cm = can_message_t( max_dlen,
589 sig->get_message()->get_id(),
592 sig->get_message()->get_flags(),
596 frequency_clock_t f = subscription.event_filter_.frequency == 0 ? subscription.signal_->get_frequency() : frequency_clock_t(subscription.event_filter_.frequency);
597 freq = f.get_timeval_from_period();
599 struct bcm_msg bcm_msg = subscription.make_bcm_head(RX_SETUP, subscription.signal_->get_message()->get_id(), flags_bcm, timeout, freq);
601 std::vector<canfd_frame> cfd_vect = cm.convert_to_canfd_frame_vector();
603 if(cfd_vect.size() > 1) //multi
605 AFB_ERROR("Not implemented yet");
608 else if(cfd_vect.size() == 1)
610 subscription.add_one_bcm_frame(cfd_vect[0], bcm_msg);
614 AFB_ERROR("No data available");
618 return create_rx_filter_bcm(subscription, bcm_msg);
622 * @brief Create the good socket to read message
623 * depending on the signal
625 * @param sig The signal subscribed
626 * @return 0 if ok else -1
628 int low_can_subscription_t::create_rx_filter(std::shared_ptr<signal_t> sig)
630 if(!sig->get_message()->is_isotp() && !sig->get_message()->is_j1939())
632 return low_can_subscription_t::create_rx_filter_can(*this, sig);
634 #ifdef USE_FEATURE_ISOTP
635 else if(sig->get_message()->is_isotp())
637 return low_can_subscription_t::create_rx_filter_isotp(*this, sig);
640 #ifdef USE_FEATURE_J1939
641 else if(sig->get_message()->is_j1939())
643 return low_can_subscription_t::create_rx_filter_j1939(*this, sig);
648 AFB_ERROR("Signal can't be j1939 and isotp");
654 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a
655 /// diagnostic message subscription.
657 /// @return 0 if ok else -1
658 int low_can_subscription_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
660 diagnostic_message_.push_back(sig);
662 struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
663 struct timeval timeout = {0, 0};
665 struct bcm_msg bcm_msg = make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER | RX_NO_AUTOTIMER | RX_FILTER_ID, timeout, freq);
666 return create_rx_filter_bcm(*this, bcm_msg);
669 /// @brief Create a RX_SETUP receive job used by the BCM socket directly from
670 /// a bcm_msg. The method should not be used directly but rather through the
671 /// two previous method with signal_t or diagnostic_message_t object.
673 /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed
674 /// to the 8 classics OBD2 functional response ID
676 /// @return 0 if ok else -1
677 int low_can_subscription_t::create_rx_filter_bcm(low_can_subscription_t &subscription, struct bcm_msg& bcm_msg)
679 // Make sure that socket is opened.
680 if(subscription.open_socket(subscription, "", CAN_PROTOCOL) < 0)
683 can_message_t msg = can_message_t();
684 msg.set_bcm_msg(bcm_msg);
686 // If it's not an OBD2 CAN ID then just add a simple RX_SETUP job
687 // else monitor all standard 8 CAN OBD2 ID response.
688 if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
690 subscription.socket_->write_message(msg);
691 if(! subscription.socket_)
696 for(uint8_t i = 0; i < 8; i++)
698 bcm_msg.msg_head.can_id = OBD2_FUNCTIONAL_RESPONSE_START + i;
699 msg.set_bcm_msg(bcm_msg);
700 subscription.socket_->write_message(msg);
701 if(! subscription.socket_)
708 /// @brief Creates a TX_SEND job that is used by the BCM socket to
711 /// @return 0 if ok else -1
712 int low_can_subscription_t::tx_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
714 can_message_t *cm = static_cast<can_message_t*>(message);
716 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
717 cm->set_bcm_msg(bcm_msg);
719 std::vector<canfd_frame> cfd_vect = cm->convert_to_canfd_frame_vector();
721 if(subscription.open_socket(subscription, bus_name, CAN_PROTOCOL) < 0)
724 struct bcm_msg &bcm_cm = cm->get_bcm_msg();
726 if(cfd_vect.size() > 1)
728 AFB_ERROR("Multi frame BCM not implemented");
731 else if(cfd_vect.size() == 1) // raw or fd
733 subscription.add_one_bcm_frame(cfd_vect[0], bcm_cm);
735 if(subscription.socket_->write_message(*cm) < 0)
737 AFB_ERROR("Error write message id : %d", cfd_vect[0].can_id);
743 AFB_ERROR("Error no data available");
747 if(! subscription.socket_.get())
753 #ifdef USE_FEATURE_J1939
755 * @brief Allows to open socket j1939 and send j1939 messsage
757 * @param subscription The subscription
758 * @param message The j1939 message to send
759 * @param bus_name The bus name where to send message
760 * @return int 0 if ok else -1
762 int low_can_subscription_t::j1939_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
764 //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
765 //subscription.add_one_bcm_frame(cfd, bcm_msg);
767 if(subscription.open_socket(subscription, bus_name, J1939_PROTOCOL) < 0)
770 j1939_message_t *jm = static_cast<j1939_message_t*>(message);
771 jm->set_sockname(jm->get_pgn(), J1939_NO_NAME, J1939_NO_ADDR);
772 if(subscription.socket_->write_message(*jm) < 0)
774 AFB_ERROR("Error write j1939 message");
784 * @brief Allows to open socket isotp and send can messsage
786 * @param subscription The subscription
787 * @param message The can message to send
788 * @param bus_name The bus name where to send message
789 * @return int 0 if ok else -1
791 int low_can_subscription_t::isotp_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
793 //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
794 //subscription.add_one_bcm_frame(cfd, bcm_msg);
796 if(subscription.open_socket(subscription, bus_name, ISOTP_PROTOCOL|ISOTP_SEND) < 0)
799 can_message_t *cm = static_cast<can_message_t*>(message);
800 if(subscription.socket_->write_message(*cm) < 0)
802 AFB_ERROR("Error write iso tp message");