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_J1939
26 #include "../utils/socketcan-j1939/socketcan-j1939-data.hpp"
29 low_can_subscription_t::low_can_subscription_t()
36 low_can_subscription_t::low_can_subscription_t(struct event_filter_t event_filter)
38 event_filter_{event_filter},
43 low_can_subscription_t::low_can_subscription_t( low_can_subscription_t&& s)
45 event_filter_{s.event_filter_},
47 socket_{std::move(s.socket_)}
50 low_can_subscription_t& low_can_subscription_t::operator=(const low_can_subscription_t& s)
52 socket_ = std::move(s.socket_);
56 low_can_subscription_t::~low_can_subscription_t()
62 low_can_subscription_t::operator bool() const
64 return ((signal_ != nullptr || ! diagnostic_message_.empty()) && ! socket_);
66 afb_event_t low_can_subscription_t::get_event()
72 * @brief Set the event calling the afb_daemon_make_event function to
73 * create it and the checks its validity.
75 * @return int - 0 if OK, -1 if not
77 int low_can_subscription_t::set_event()
79 std::string event_name = get_name();
80 event_ = afb_daemon_make_event(event_name.c_str());
81 if (! afb_event_is_valid(event_))
83 AFB_ERROR("Can't create an event for %s, something goes wrong.", event_name.c_str());
91 * @brief Subscribe to the event member of the object
93 * @param request the subscribe AFB client request which want to
96 * @return int - 0 if OK, -1 if not
98 int low_can_subscription_t::subscribe(afb_req_t request)
100 if(! afb_event_is_valid(event_))
107 return afb_req_subscribe(request, event_);
111 * @brief Unsubscribe to the event member of the object
113 * @param request the unsubscribe AFB client request which want to
116 * @return int - 0 if OK, -1 if not
118 int low_can_subscription_t::unsubscribe(afb_req_t request)
120 return afb_req_unsubscribe(request, event_);
123 int low_can_subscription_t::get_index() const
128 const std::shared_ptr<signal_t> low_can_subscription_t::get_signal() const
133 bool low_can_subscription_t::is_signal_subscription_corresponding(const std::shared_ptr<signal_t> signal, const struct event_filter_t& event_filter) const
135 return signal_ == signal && event_filter_ == event_filter;
138 const std::vector<std::shared_ptr<diagnostic_message_t> > low_can_subscription_t::get_diagnostic_message() const
140 return diagnostic_message_;
143 /// @brief Retrieve a diagnostic_message subscribed from a pid
145 /// @param[in] pid - Diagnostic messages PID to search for
147 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
148 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(uint32_t pid) const
150 for(const auto& diag: diagnostic_message_)
152 if(diag->get_pid() == pid)
160 /// @brief Retrieve a diagnostic message search from its name
162 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
163 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(const std::string& name) const
165 for(const auto& diag: diagnostic_message_)
167 if(diag->get_name() == name)
175 /// @brief Return the CAN signal name and empty string if not found
176 /// or no CAN signal subscribed
177 const std::string low_can_subscription_t::get_name() const
179 if (signal_ != nullptr)
180 return signal_->get_name();
181 else if (!diagnostic_message_.empty())
182 return "diagnostic_messages";
184 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
188 /// @brief Return name from a diagnostic message from a PID
190 /// @param[in] pid - Diagnostic message PID
191 const std::string low_can_subscription_t::get_name(uint32_t pid) const
193 if (!diagnostic_message_.empty())
194 return get_diagnostic_message(pid)->get_name() ;
196 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
200 float low_can_subscription_t::get_frequency() const
202 return event_filter_.frequency;
205 float low_can_subscription_t::get_min() const
207 return event_filter_.min;
210 float low_can_subscription_t::get_max() const
212 return event_filter_.max;
215 std::shared_ptr<utils::socketcan_t> low_can_subscription_t::get_socket()
220 void low_can_subscription_t::set_frequency(float freq)
222 event_filter_.frequency = freq;
225 void low_can_subscription_t::set_min(float min)
227 event_filter_.min = min;
230 void low_can_subscription_t::set_max(float max)
232 event_filter_.max = max;
235 void low_can_subscription_t::set_index(int index)
240 /// @brief Based upon which object is a subscribed CAN signal or diagnostic message
241 /// it will open the socket with the required CAN bus device name.
243 /// @return INVALID_SOCKET on failure, else positive integer
244 int low_can_subscription_t::open_socket(low_can_subscription_t &subscription, const std::string& bus_name, socket_type type)
247 if(! subscription.socket_)
251 case socket_type::BCM:
253 if( subscription.signal_ != nullptr)
255 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
256 ret = subscription.socket_->open(subscription.signal_->get_message()->get_bus_device_name());
258 else if (! subscription.diagnostic_message_ .empty())
260 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
261 ret = subscription.socket_->open(application_t::instance().get_diagnostic_manager().get_bus_device_name());
263 else if ( !bus_name.empty())
265 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
266 ret = subscription.socket_->open(bus_name);
268 subscription.index_ = (int)subscription.socket_->socket();
271 #ifdef USE_FEATURE_J1939
272 case socket_type::J1939_ADDR_CLAIM:
274 pgn_t pgn = J1939_NO_PGN;
275 if(!bus_name.empty())
277 std::shared_ptr<utils::socketcan_j1939_addressclaiming_t> socket = std::make_shared<utils::socketcan_j1939_addressclaiming_t>();
278 ret = socket->open(bus_name, pgn);
279 subscription.socket_ = socket;
281 subscription.index_ = (int)subscription.socket_->socket();
284 case socket_type::J1939:
286 pgn_t pgn = J1939_NO_PGN;
287 if(subscription.signal_ != nullptr)
289 pgn = subscription.signal_->get_message()->get_id();
290 std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
291 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), pgn);
292 subscription.socket_ = socket;
294 else if(!bus_name.empty())
296 std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
297 ret = socket->open(bus_name, pgn);
298 subscription.socket_ = socket;
300 subscription.index_ = (int)subscription.socket_->socket();
306 AFB_ERROR("Socket format not supported");
307 return INVALID_SOCKET;
313 ret = subscription.socket_->socket();
319 /// @brief Builds a BCM message head but doesn't set can_frame.
321 /// @returns a bcm_msg with the msg_head parts set and can_frame
323 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)
325 struct bcm_msg bcm_msg;
326 ::memset(&bcm_msg, 0, sizeof(bcm_msg));
328 bcm_msg.msg_head.opcode = opcode;
329 bcm_msg.msg_head.can_id = can_id;
330 bcm_msg.msg_head.flags = flags;
331 bcm_msg.msg_head.ival1.tv_sec = timeout.tv_sec ;
332 bcm_msg.msg_head.ival1.tv_usec = timeout.tv_usec;
333 bcm_msg.msg_head.ival2.tv_sec = frequency_thinning.tv_sec ;
334 bcm_msg.msg_head.ival2.tv_usec = frequency_thinning.tv_usec;
340 /// @brief Take an existing bcm_msg struct and add a can_frame.
341 /// Currently only 1 uniq can_frame can be added, it's not possible to build
342 /// a multiplexed message with several can_frame.
343 void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct bcm_msg& bcm_msg)
347 if (bcm_msg.msg_head.flags & CAN_FD_FRAME)
348 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cfd;
351 cf.can_id = cfd.can_id;
352 cf.can_dlc = cfd.len;
353 ::memcpy(&cf.data, cfd.data, cfd.len);
354 bcm_msg.frames[bcm_msg.msg_head.nframes] = cf;
356 bcm_msg.msg_head.nframes++;
359 /// @brief Take an existing bcm_msg struct and add a can_frame.
360 /// Currently only 1 uniq can_frame can be added, it's not possible to build
361 /// a multiplexed message with several can_frame.
362 void low_can_subscription_t::remove_last_bcm_frame(struct bcm_msg& bcm_msg)
364 struct canfd_frame cf;
365 memset(&cf,0,sizeof(cf));
366 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cf;
367 bcm_msg.msg_head.nframes--;
370 #ifdef USE_FEATURE_J1939
371 int low_can_subscription_t::create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
373 subscription.signal_= sig;
375 // Make sure that socket is opened.
376 if(open_socket(subscription,"",socket_type::J1939) < 0)
384 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a CAN signal
387 /// @return 0 if ok else -1
388 int low_can_subscription_t::create_rx_filter_can(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
392 struct timeval freq, timeout = {0, 0};
393 struct canfd_frame cfd;
394 subscription.signal_= sig;
395 bool is_fd = sig->get_message()->is_fd();
397 std::vector<uint8_t> data;
398 uint32_t length_msg = sig->get_message()->get_length();
402 AFB_ERROR("Error in the length of message with id %d",sig->get_message()->get_id());
406 for(int i = 0; i<length_msg;i++)
411 encoder_t::encode_data(subscription.signal_,data,true,false,true);
417 flags = SETTIMER|RX_NO_AUTOTIMER|CAN_FD_FRAME;
418 cfd.len = CANFD_MAX_DLEN;
419 cm = can_message_t(CANFD_MAX_DLEN,sig->get_message()->get_id(),length_msg,sig->get_message()->get_format(),false,CAN_FD_FRAME,data,0);
423 flags = SETTIMER|RX_NO_AUTOTIMER;
424 cfd.len = CAN_MAX_DLEN;
425 cm = can_message_t(CAN_MAX_DLEN,sig->get_message()->get_id(),length_msg,sig->get_message()->get_format(),false,0,data,0);
428 frequency_clock_t f = subscription.event_filter_.frequency == 0 ? subscription.signal_->get_frequency() : frequency_clock_t(subscription.event_filter_.frequency);
429 freq = f.get_timeval_from_period();
431 struct bcm_msg bcm_msg = subscription.make_bcm_head(RX_SETUP, subscription.signal_->get_message()->get_id(), flags, timeout, freq);
433 std::vector<canfd_frame> cfd_vect = cm.convert_to_canfd_frame_vector();
435 if(cfd_vect.size() > 1) //multi
437 AFB_ERROR("Not implemented yet");
440 else if(cfd_vect.size() == 1)
442 canfd_frame cf = cfd_vect[0];
443 for(int i=0;i<cfd.len;i++)
445 cfd.data[i] = cf.data[i];
447 subscription.add_one_bcm_frame(cfd, bcm_msg);
451 AFB_ERROR("No data available");
455 return create_rx_filter_bcm(subscription, bcm_msg);
458 int low_can_subscription_t::create_rx_filter(std::shared_ptr<signal_t> sig)
460 #ifdef USE_FEATURE_J1939
461 if(sig->get_message()->is_j1939())
463 return low_can_subscription_t::create_rx_filter_j1939(*this, sig);
468 return low_can_subscription_t::create_rx_filter_can(*this, sig);
469 #ifdef USE_FEATURE_J1939
475 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a
476 /// diagnostic message subscription.
478 /// @return 0 if ok else -1
479 int low_can_subscription_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
481 diagnostic_message_.push_back(sig);
483 struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
484 //struct timeval timeout = frequency_clock_t(10).get_timeval_from_period();
485 struct timeval timeout = {0,0};
487 struct bcm_msg bcm_msg = make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER|RX_NO_AUTOTIMER|RX_FILTER_ID, timeout, freq);
488 return create_rx_filter_bcm(*this, bcm_msg);
491 /// @brief Create a RX_SETUP receive job used by the BCM socket directly from
492 /// a bcm_msg. The method should not be used directly but rather through the
493 /// two previous method with signal_t or diagnostic_message_t object.
495 /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed
496 /// to the 8 classics OBD2 functional response ID
498 /// @return 0 if ok else -1
499 int low_can_subscription_t::create_rx_filter_bcm(low_can_subscription_t &subscription, struct bcm_msg& bcm_msg)
501 // Make sure that socket is opened.
502 if(subscription.open_socket(subscription,"",socket_type::BCM) < 0)
505 // If it's not an OBD2 CAN ID then just add a simple RX_SETUP job
506 // else monitor all standard 8 CAN OBD2 ID response.
508 can_message_t msg = can_message_t();
510 msg.set_bcm_msg(bcm_msg);
512 if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
514 subscription.socket_->write_message(msg);
515 if(! subscription.socket_)
520 for(uint8_t i = 0; i < 8; i++)
522 bcm_msg.msg_head.can_id = OBD2_FUNCTIONAL_RESPONSE_START + i;
523 msg.set_bcm_msg(bcm_msg);
524 subscription.socket_->write_message(msg);
525 if(! subscription.socket_)
532 /// @brief Creates a TX_SEND job that is used by the BCM socket to
535 /// @return 0 if ok else -1
536 int low_can_subscription_t::tx_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
538 can_message_t *cm = static_cast<can_message_t*>(message);
540 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
541 cm->set_bcm_msg(bcm_msg);
543 std::vector<canfd_frame> cfd_vect = cm->convert_to_canfd_frame_vector();
545 if(subscription.open_socket(subscription, bus_name,socket_type::BCM) < 0)
550 struct bcm_msg &bcm_cm = cm->get_bcm_msg();
554 if(cfd_vect.size() > 1)
556 AFB_ERROR("Multi frame BCM not implemented");
559 else if(cfd_vect.size() == 1) // raw or fd
561 subscription.add_one_bcm_frame(cfd_vect[0], bcm_cm);
563 if(subscription.socket_->write_message(*cm) < 0)
565 AFB_ERROR("Error write message id : %d",cfd_vect[0].can_id);
571 AFB_ERROR("Error no data available");
575 if(! subscription.socket_.get())
583 #ifdef USE_FEATURE_J1939
584 int low_can_subscription_t::j1939_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
586 //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
587 //subscription.add_one_bcm_frame(cfd, bcm_msg);
589 if(subscription.open_socket(subscription, bus_name, socket_type::J1939) < 0)
594 j1939_message_t *jm = static_cast<j1939_message_t*>(message);
595 jm->set_sockname(jm->get_pgn(),J1939_NO_NAME,J1939_NO_ADDR);
596 if(subscription.socket_->write_message(*jm) < 0)
598 AFB_ERROR("Error write j1939 message");