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_);
127 int low_can_subscription_t::get_index() const
132 const std::shared_ptr<signal_t> low_can_subscription_t::get_signal() const
137 bool low_can_subscription_t::is_signal_subscription_corresponding(const std::shared_ptr<signal_t> signal, const struct event_filter_t& event_filter) const
139 return signal_ == signal && event_filter_ == event_filter;
142 const std::vector<std::shared_ptr<diagnostic_message_t> > low_can_subscription_t::get_diagnostic_message() const
144 return diagnostic_message_;
147 /// @brief Retrieve a diagnostic_message subscribed from a pid
149 /// @param[in] pid - Diagnostic messages PID to search for
151 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
152 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(uint32_t pid) const
154 for(const auto& diag: diagnostic_message_)
156 if(diag->get_pid() == pid)
164 /// @brief Retrieve a diagnostic message search from its name
166 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
167 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(const std::string& name) const
169 for(const auto& diag: diagnostic_message_)
171 if(diag->get_name() == name)
179 /// @brief Return the CAN signal name and empty string if not found
180 /// or no CAN signal subscribed
181 const std::string low_can_subscription_t::get_name() const
183 if (signal_ != nullptr)
184 return signal_->get_name();
185 else if (!diagnostic_message_.empty())
186 return "diagnostic_messages";
188 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
192 /// @brief Return name from a diagnostic message from a PID
194 /// @param[in] pid - Diagnostic message PID
195 const std::string low_can_subscription_t::get_name(uint32_t pid) const
197 if (!diagnostic_message_.empty())
198 return get_diagnostic_message(pid)->get_name() ;
200 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
204 float low_can_subscription_t::get_frequency() const
206 return event_filter_.frequency;
209 float low_can_subscription_t::get_min() const
211 return event_filter_.min;
214 float low_can_subscription_t::get_max() const
216 return event_filter_.max;
219 canid_t low_can_subscription_t::get_rx_id() const
221 return event_filter_.rx_id;
224 canid_t low_can_subscription_t::get_tx_id() const
226 return event_filter_.tx_id;
229 std::shared_ptr<utils::socketcan_t> low_can_subscription_t::get_socket()
234 void low_can_subscription_t::set_frequency(float freq)
236 event_filter_.frequency = freq;
239 void low_can_subscription_t::set_min(float min)
241 event_filter_.min = min;
244 void low_can_subscription_t::set_max(float max)
246 event_filter_.max = max;
249 void low_can_subscription_t::set_rx_id(canid_t rx_id)
251 event_filter_.rx_id = rx_id;
254 void low_can_subscription_t::set_tx_id(canid_t tx_id)
256 event_filter_.tx_id = tx_id;
259 void low_can_subscription_t::set_index(int index)
264 void low_can_subscription_t::set_signal(std::shared_ptr<signal_t> signal)
270 /// @brief Based upon which object is a subscribed CAN signal or diagnostic message
271 /// it will open the socket with the required CAN bus device name.
273 /// @return INVALID_SOCKET on failure, else positive integer
274 int low_can_subscription_t::open_socket(low_can_subscription_t &subscription, const std::string& bus_name, uint32_t flags)
277 if(! subscription.socket_)
279 if(flags&BCM_PROTOCOL)
281 if( subscription.signal_ != nullptr)
283 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
284 ret = subscription.socket_->open(subscription.signal_->get_message()->get_bus_device_name());
286 else if (! subscription.diagnostic_message_ .empty())
288 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
289 ret = subscription.socket_->open(application_t::instance().get_diagnostic_manager().get_bus_device_name());
291 else if ( !bus_name.empty())
293 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
294 ret = subscription.socket_->open(bus_name);
296 subscription.index_ = (int)subscription.socket_->socket();
298 #ifdef USE_FEATURE_ISOTP
299 else if(flags&ISOTP_PROTOCOL)
301 if(subscription.signal_ != nullptr)
303 canid_t rx = NO_CAN_ID;
304 canid_t tx = NO_CAN_ID;
307 rx = subscription.get_rx_id();
308 tx = subscription.signal_->get_message()->get_id();
310 else if(flags&ISOTP_RECEIVE)
312 rx = subscription.signal_->get_message()->get_id();
313 tx = subscription.get_tx_id();
315 std::shared_ptr<utils::socketcan_isotp_t> socket = std::make_shared<utils::socketcan_isotp_t>();
316 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(),rx,tx);
317 subscription.socket_ = socket;
319 else if(!bus_name.empty())
321 std::shared_ptr<utils::socketcan_isotp_t> socket = std::make_shared<utils::socketcan_isotp_t>();
322 ret = socket->open(bus_name, subscription.get_rx_id(),subscription.get_tx_id());
323 subscription.socket_ = socket;
325 subscription.index_ = (int)subscription.socket_->socket();
328 #ifdef USE_FEATURE_J1939
329 else if(flags&J1939_ADDR_CLAIM_PROTOCOL)
331 pgn_t pgn = J1939_NO_PGN;
332 if(!bus_name.empty())
334 std::shared_ptr<utils::socketcan_j1939_addressclaiming_t> socket = std::make_shared<utils::socketcan_j1939_addressclaiming_t>();
335 ret = socket->open(bus_name, pgn);
336 subscription.socket_ = socket;
338 subscription.index_ = (int)subscription.socket_->socket();
340 else if(flags&J1939_PROTOCOL)
342 pgn_t pgn = J1939_NO_PGN;
343 if(subscription.signal_ != nullptr)
345 pgn = subscription.signal_->get_message()->get_id();
346 std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
347 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), pgn);
348 subscription.socket_ = socket;
350 else if(!bus_name.empty())
352 std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
353 ret = socket->open(bus_name, pgn);
354 subscription.socket_ = socket;
356 subscription.index_ = (int)subscription.socket_->socket();
361 AFB_ERROR("Socket format not supported");
362 return INVALID_SOCKET;
366 ret = subscription.socket_->socket();
372 /// @brief Builds a BCM message head but doesn't set can_frame.
374 /// @returns a bcm_msg with the msg_head parts set and can_frame
376 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)
378 struct bcm_msg bcm_msg;
379 ::memset(&bcm_msg, 0, sizeof(bcm_msg));
381 bcm_msg.msg_head.opcode = opcode;
382 bcm_msg.msg_head.can_id = can_id;
383 bcm_msg.msg_head.flags = flags;
384 bcm_msg.msg_head.ival1.tv_sec = timeout.tv_sec ;
385 bcm_msg.msg_head.ival1.tv_usec = timeout.tv_usec;
386 bcm_msg.msg_head.ival2.tv_sec = frequency_thinning.tv_sec ;
387 bcm_msg.msg_head.ival2.tv_usec = frequency_thinning.tv_usec;
393 /// @brief Take an existing bcm_msg struct and add a can_frame.
394 /// Currently only 1 uniq can_frame can be added, it's not possible to build
395 /// a multiplexed message with several can_frame.
396 void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct bcm_msg& bcm_msg)
400 if (bcm_msg.msg_head.flags & CAN_FD_FRAME)
401 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cfd;
404 cf.can_id = cfd.can_id;
405 cf.can_dlc = cfd.len;
406 ::memcpy(&cf.data, cfd.data, cfd.len);
407 bcm_msg.frames[bcm_msg.msg_head.nframes] = cf;
409 bcm_msg.msg_head.nframes++;
412 /// @brief Take an existing bcm_msg struct and add a can_frame.
413 /// Currently only 1 uniq can_frame can be added, it's not possible to build
414 /// a multiplexed message with several can_frame.
415 void low_can_subscription_t::remove_last_bcm_frame(struct bcm_msg& bcm_msg)
417 struct canfd_frame cf;
418 memset(&cf,0,sizeof(cf));
419 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cf;
420 bcm_msg.msg_head.nframes--;
423 #ifdef USE_FEATURE_J1939
424 int low_can_subscription_t::create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
426 subscription.signal_= sig;
428 // Make sure that socket is opened.
429 if(open_socket(subscription,"",J1939_PROTOCOL) < 0)
437 int low_can_subscription_t::create_rx_filter_isotp(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
439 subscription.signal_= sig;
441 // Make sure that socket is opened.
442 if(open_socket(subscription,"",ISOTP_PROTOCOL|ISOTP_RECEIVE) < 0)
449 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a CAN signal
452 /// @return 0 if ok else -1
453 int low_can_subscription_t::create_rx_filter_can(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
457 struct timeval freq, timeout = {0, 0};
458 struct canfd_frame cfd;
459 subscription.signal_= sig;
460 bool is_fd = sig->get_message()->is_fd();
462 std::vector<uint8_t> data;
463 uint32_t length_msg = sig->get_message()->get_length();
467 AFB_ERROR("Error in the length of message with id %d",sig->get_message()->get_id());
471 for(int i = 0; i<length_msg;i++)
476 encoder_t::encode_data(subscription.signal_,data,true,false,true);
482 flags_bcm = SETTIMER|RX_NO_AUTOTIMER|CAN_FD_FRAME;
483 cfd.len = CANFD_MAX_DLEN;
484 cm = can_message_t( CANFD_MAX_DLEN,
485 sig->get_message()->get_id(),
488 sig->get_message()->get_flags(),
494 flags_bcm = SETTIMER|RX_NO_AUTOTIMER;
495 cfd.len = CAN_MAX_DLEN;
496 cm = can_message_t( CAN_MAX_DLEN,
497 sig->get_message()->get_id(),
500 sig->get_message()->get_flags(),
505 frequency_clock_t f = subscription.event_filter_.frequency == 0 ? subscription.signal_->get_frequency() : frequency_clock_t(subscription.event_filter_.frequency);
506 freq = f.get_timeval_from_period();
508 struct bcm_msg bcm_msg = subscription.make_bcm_head(RX_SETUP, subscription.signal_->get_message()->get_id(), flags_bcm, timeout, freq);
510 std::vector<canfd_frame> cfd_vect = cm.convert_to_canfd_frame_vector();
512 if(cfd_vect.size() > 1) //multi
514 AFB_ERROR("Not implemented yet");
517 else if(cfd_vect.size() == 1)
519 canfd_frame cf = cfd_vect[0];
520 for(int i=0;i<cfd.len;i++)
522 cfd.data[i] = cf.data[i];
524 subscription.add_one_bcm_frame(cfd, bcm_msg);
528 AFB_ERROR("No data available");
532 return create_rx_filter_bcm(subscription, bcm_msg);
535 int low_can_subscription_t::create_rx_filter(std::shared_ptr<signal_t> sig)
537 if(!sig->get_message()->is_isotp() && !sig->get_message()->is_j1939())
539 return low_can_subscription_t::create_rx_filter_can(*this, sig);
541 #ifdef USE_FEATURE_ISOTP
542 else if(sig->get_message()->is_isotp())
544 return low_can_subscription_t::create_rx_filter_isotp(*this,sig);
547 #ifdef USE_FEATURE_J1939
548 else if(sig->get_message()->is_j1939())
550 return low_can_subscription_t::create_rx_filter_j1939(*this, sig);
555 AFB_ERROR("Signal can't be j1939 and isotp");
561 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a
562 /// diagnostic message subscription.
564 /// @return 0 if ok else -1
565 int low_can_subscription_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
567 diagnostic_message_.push_back(sig);
569 struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
570 //struct timeval timeout = frequency_clock_t(10).get_timeval_from_period();
571 struct timeval timeout = {0,0};
573 struct bcm_msg bcm_msg = make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER|RX_NO_AUTOTIMER|RX_FILTER_ID, timeout, freq);
574 return create_rx_filter_bcm(*this, bcm_msg);
577 /// @brief Create a RX_SETUP receive job used by the BCM socket directly from
578 /// a bcm_msg. The method should not be used directly but rather through the
579 /// two previous method with signal_t or diagnostic_message_t object.
581 /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed
582 /// to the 8 classics OBD2 functional response ID
584 /// @return 0 if ok else -1
585 int low_can_subscription_t::create_rx_filter_bcm(low_can_subscription_t &subscription, struct bcm_msg& bcm_msg)
587 // Make sure that socket is opened.
588 if(subscription.open_socket(subscription,"",BCM_PROTOCOL) < 0)
591 // If it's not an OBD2 CAN ID then just add a simple RX_SETUP job
592 // else monitor all standard 8 CAN OBD2 ID response.
594 can_message_t msg = can_message_t();
596 msg.set_bcm_msg(bcm_msg);
598 if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
600 subscription.socket_->write_message(msg);
601 if(! subscription.socket_)
606 for(uint8_t i = 0; i < 8; i++)
608 bcm_msg.msg_head.can_id = OBD2_FUNCTIONAL_RESPONSE_START + i;
609 msg.set_bcm_msg(bcm_msg);
610 subscription.socket_->write_message(msg);
611 if(! subscription.socket_)
618 /// @brief Creates a TX_SEND job that is used by the BCM socket to
621 /// @return 0 if ok else -1
622 int low_can_subscription_t::tx_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
624 can_message_t *cm = static_cast<can_message_t*>(message);
626 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
627 cm->set_bcm_msg(bcm_msg);
629 std::vector<canfd_frame> cfd_vect = cm->convert_to_canfd_frame_vector();
631 if(subscription.open_socket(subscription, bus_name, BCM_PROTOCOL) < 0)
636 struct bcm_msg &bcm_cm = cm->get_bcm_msg();
640 if(cfd_vect.size() > 1)
642 AFB_ERROR("Multi frame BCM not implemented");
645 else if(cfd_vect.size() == 1) // raw or fd
647 subscription.add_one_bcm_frame(cfd_vect[0], bcm_cm);
649 if(subscription.socket_->write_message(*cm) < 0)
651 AFB_ERROR("Error write message id : %d",cfd_vect[0].can_id);
657 AFB_ERROR("Error no data available");
661 if(! subscription.socket_.get())
669 #ifdef USE_FEATURE_J1939
670 int low_can_subscription_t::j1939_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
672 //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
673 //subscription.add_one_bcm_frame(cfd, bcm_msg);
675 if(subscription.open_socket(subscription, bus_name, J1939_PROTOCOL) < 0)
680 j1939_message_t *jm = static_cast<j1939_message_t*>(message);
681 jm->set_sockname(jm->get_pgn(),J1939_NO_NAME,J1939_NO_ADDR);
682 if(subscription.socket_->write_message(*jm) < 0)
684 AFB_ERROR("Error write j1939 message");
693 int low_can_subscription_t::isotp_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
695 //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
696 //subscription.add_one_bcm_frame(cfd, bcm_msg);
698 if(subscription.open_socket(subscription, bus_name, ISOTP_PROTOCOL|ISOTP_SEND) < 0)
703 can_message_t *cm = static_cast<can_message_t*>(message);
704 if(subscription.socket_->write_message(*cm) < 0)
706 AFB_ERROR("Error write iso tp message");