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 #ifdef USE_FEATURE_J1939
24 #include "../utils/socketcan-j1939/socketcan-j1939-data.hpp"
27 low_can_subscription_t::low_can_subscription_t()
34 low_can_subscription_t::low_can_subscription_t(struct event_filter_t event_filter)
36 event_filter_{event_filter},
41 low_can_subscription_t::low_can_subscription_t( low_can_subscription_t&& s)
43 event_filter_{s.event_filter_},
45 socket_{std::move(s.socket_)}
48 low_can_subscription_t& low_can_subscription_t::operator=(const low_can_subscription_t& s)
50 socket_ = std::move(s.socket_);
54 low_can_subscription_t::~low_can_subscription_t()
60 low_can_subscription_t::operator bool() const
62 return ((signal_ != nullptr || ! diagnostic_message_.empty()) && ! socket_);
64 afb_event_t low_can_subscription_t::get_event()
70 * @brief Set the event calling the afb_daemon_make_event function to
71 * create it and the checks its validity.
73 * @return int - 0 if OK, -1 if not
75 int low_can_subscription_t::set_event()
77 std::string event_name = get_name();
78 event_ = afb_daemon_make_event(event_name.c_str());
79 if (! afb_event_is_valid(event_))
81 AFB_ERROR("Can't create an event for %s, something goes wrong.", event_name.c_str());
89 * @brief Subscribe to the event member of the object
91 * @param request the subscribe AFB client request which want to
94 * @return int - 0 if OK, -1 if not
96 int low_can_subscription_t::subscribe(afb_req_t request)
98 if(! afb_event_is_valid(event_))
105 return afb_req_subscribe(request, event_);
109 * @brief Unsubscribe to the event member of the object
111 * @param request the unsubscribe AFB client request which want to
114 * @return int - 0 if OK, -1 if not
116 int low_can_subscription_t::unsubscribe(afb_req_t request)
118 return afb_req_unsubscribe(request, event_);
121 int low_can_subscription_t::get_index() const
126 const std::shared_ptr<signal_t> low_can_subscription_t::get_signal() const
131 bool low_can_subscription_t::is_signal_subscription_corresponding(const std::shared_ptr<signal_t> signal, const struct event_filter_t& event_filter) const
133 return signal_ == signal && event_filter_ == event_filter;
136 const std::vector<std::shared_ptr<diagnostic_message_t> > low_can_subscription_t::get_diagnostic_message() const
138 return diagnostic_message_;
141 /// @brief Retrieve a diagnostic_message subscribed from a pid
143 /// @param[in] pid - Diagnostic messages PID to search for
145 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
146 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(uint32_t pid) const
148 for(const auto& diag: diagnostic_message_)
150 if(diag->get_pid() == pid)
158 /// @brief Retrieve a diagnostic message search from its name
160 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
161 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(const std::string& name) const
163 for(const auto& diag: diagnostic_message_)
165 if(diag->get_name() == name)
173 /// @brief Return the CAN signal name and empty string if not found
174 /// or no CAN signal subscribed
175 const std::string low_can_subscription_t::get_name() const
177 if (signal_ != nullptr)
178 return signal_->get_name();
179 else if (!diagnostic_message_.empty())
180 return "diagnostic_messages";
182 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
186 /// @brief Return name from a diagnostic message from a PID
188 /// @param[in] pid - Diagnostic message PID
189 const std::string low_can_subscription_t::get_name(uint32_t pid) const
191 if (!diagnostic_message_.empty())
192 return get_diagnostic_message(pid)->get_name() ;
194 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
198 float low_can_subscription_t::get_frequency() const
200 return event_filter_.frequency;
203 float low_can_subscription_t::get_min() const
205 return event_filter_.min;
208 float low_can_subscription_t::get_max() const
210 return event_filter_.max;
213 std::shared_ptr<utils::socketcan_t> low_can_subscription_t::get_socket()
218 void low_can_subscription_t::set_frequency(float freq)
220 event_filter_.frequency = freq;
223 void low_can_subscription_t::set_min(float min)
225 event_filter_.min = min;
228 void low_can_subscription_t::set_max(float max)
230 event_filter_.max = max;
233 void low_can_subscription_t::set_index(int index)
238 /// @brief Based upon which object is a subscribed CAN signal or diagnostic message
239 /// it will open the socket with the required CAN bus device name.
241 /// @return INVALID_SOCKET on failure, else positive integer
242 int low_can_subscription_t::open_socket(low_can_subscription_t &subscription, const std::string& bus_name, socket_type type)
245 if(! subscription.socket_)
249 case socket_type::BCM:
251 if( subscription.signal_ != nullptr)
253 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
254 ret = subscription.socket_->open(subscription.signal_->get_message()->get_bus_device_name());
256 else if (! subscription.diagnostic_message_ .empty())
258 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
259 ret = subscription.socket_->open(application_t::instance().get_diagnostic_manager().get_bus_device_name());
261 else if ( !bus_name.empty())
263 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
264 ret = subscription.socket_->open(bus_name);
266 subscription.index_ = (int)subscription.socket_->socket();
269 #ifdef USE_FEATURE_J1939
270 case socket_type::J1939_ADDR_CLAIM:
272 pgn_t pgn = J1939_NO_PGN;
273 if(!bus_name.empty())
275 std::shared_ptr<utils::socketcan_j1939_addressclaiming_t> socket = std::make_shared<utils::socketcan_j1939_addressclaiming_t>();
276 ret = socket->open(bus_name, pgn);
277 subscription.socket_ = socket;
279 subscription.index_ = (int)subscription.socket_->socket();
282 case socket_type::J1939:
284 pgn_t pgn = J1939_NO_PGN;
285 if(subscription.signal_ != nullptr)
287 pgn = subscription.signal_->get_message()->get_id();
288 std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
289 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), pgn);
290 subscription.socket_ = socket;
292 else if(!bus_name.empty())
294 std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
295 ret = socket->open(bus_name, pgn);
296 subscription.socket_ = socket;
298 subscription.index_ = (int)subscription.socket_->socket();
304 AFB_ERROR("Socket format not supported");
305 return INVALID_SOCKET;
311 ret = subscription.socket_->socket();
317 /// @brief Builds a BCM message head but doesn't set can_frame.
319 /// @returns a bcm_msg with the msg_head parts set and can_frame
321 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)
323 struct bcm_msg bcm_msg;
324 ::memset(&bcm_msg, 0, sizeof(bcm_msg));
326 bcm_msg.msg_head.opcode = opcode;
327 bcm_msg.msg_head.can_id = can_id;
328 bcm_msg.msg_head.flags = flags;
329 bcm_msg.msg_head.ival1.tv_sec = timeout.tv_sec ;
330 bcm_msg.msg_head.ival1.tv_usec = timeout.tv_usec;
331 bcm_msg.msg_head.ival2.tv_sec = frequency_thinning.tv_sec ;
332 bcm_msg.msg_head.ival2.tv_usec = frequency_thinning.tv_usec;
338 /// @brief Take an existing bcm_msg struct and add a can_frame.
339 /// Currently only 1 uniq can_frame can be added, it's not possible to build
340 /// a multiplexed message with several can_frame.
341 void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct bcm_msg& bcm_msg)
345 if (bcm_msg.msg_head.flags & CAN_FD_FRAME)
346 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cfd;
349 cf.can_id = cfd.can_id;
350 cf.can_dlc = cfd.len;
351 ::memcpy(&cf.data, cfd.data, cfd.len);
352 bcm_msg.frames[bcm_msg.msg_head.nframes] = cf;
354 bcm_msg.msg_head.nframes++;
357 /// @brief Take an existing bcm_msg struct and add a can_frame.
358 /// Currently only 1 uniq can_frame can be added, it's not possible to build
359 /// a multiplexed message with several can_frame.
360 void low_can_subscription_t::remove_last_bcm_frame(struct bcm_msg& bcm_msg)
362 struct canfd_frame cf;
363 memset(&cf,0,sizeof(cf));
364 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cf;
365 bcm_msg.msg_head.nframes--;
368 #ifdef USE_FEATURE_J1939
369 int low_can_subscription_t::create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
371 subscription.signal_= sig;
373 // Make sure that socket is opened.
374 if(open_socket(subscription,"",socket_type::J1939) < 0)
382 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a CAN signal
385 /// @return 0 if ok else -1
386 int low_can_subscription_t::create_rx_filter_can(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
390 struct timeval freq, timeout = {0, 0};
391 struct canfd_frame cfd;
392 subscription.signal_= sig;
393 bool is_fd = sig->get_message()->is_fd();
395 std::vector<uint8_t> data;
396 uint32_t length_msg = sig->get_message()->get_length();
400 AFB_ERROR("Error in the length of message with id %d",sig->get_message()->get_id());
404 for(int i = 0; i<length_msg;i++)
409 encoder_t::encode_data(subscription.signal_,data,true,false,true);
415 flags = SETTIMER|RX_NO_AUTOTIMER|CAN_FD_FRAME;
416 cfd.len = CANFD_MAX_DLEN;
417 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);
421 flags = SETTIMER|RX_NO_AUTOTIMER;
422 cfd.len = CAN_MAX_DLEN;
423 cm = can_message_t(CAN_MAX_DLEN,sig->get_message()->get_id(),length_msg,sig->get_message()->get_format(),false,0,data,0);
426 frequency_clock_t f = subscription.event_filter_.frequency == 0 ? subscription.signal_->get_frequency() : frequency_clock_t(subscription.event_filter_.frequency);
427 freq = f.get_timeval_from_period();
429 struct bcm_msg bcm_msg = subscription.make_bcm_head(RX_SETUP, subscription.signal_->get_message()->get_id(), flags, timeout, freq);
431 std::vector<canfd_frame> cfd_vect = cm.convert_to_canfd_frame_vector();
433 if(cfd_vect.size() > 1) //multi
435 AFB_ERROR("Not implemented yet");
438 else if(cfd_vect.size() == 1)
440 canfd_frame cf = cfd_vect[0];
441 for(int i=0;i<cfd.len;i++)
443 cfd.data[i] = cf.data[i];
445 subscription.add_one_bcm_frame(cfd, bcm_msg);
449 AFB_ERROR("No data available");
453 return create_rx_filter_bcm(subscription, bcm_msg);
456 int low_can_subscription_t::create_rx_filter(std::shared_ptr<signal_t> sig)
458 #ifdef USE_FEATURE_J1939
459 if(sig->get_message()->is_j1939())
461 return low_can_subscription_t::create_rx_filter_j1939(*this, sig);
466 return low_can_subscription_t::create_rx_filter_can(*this, sig);
467 #ifdef USE_FEATURE_J1939
473 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a
474 /// diagnostic message subscription.
476 /// @return 0 if ok else -1
477 int low_can_subscription_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
479 diagnostic_message_.push_back(sig);
481 struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
482 //struct timeval timeout = frequency_clock_t(10).get_timeval_from_period();
483 struct timeval timeout = {0,0};
485 struct bcm_msg bcm_msg = make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER|RX_NO_AUTOTIMER|RX_FILTER_ID, timeout, freq);
486 return create_rx_filter_bcm(*this, bcm_msg);
489 /// @brief Create a RX_SETUP receive job used by the BCM socket directly from
490 /// a bcm_msg. The method should not be used directly but rather through the
491 /// two previous method with signal_t or diagnostic_message_t object.
493 /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed
494 /// to the 8 classics OBD2 functional response ID
496 /// @return 0 if ok else -1
497 int low_can_subscription_t::create_rx_filter_bcm(low_can_subscription_t &subscription, struct bcm_msg& bcm_msg)
499 // Make sure that socket is opened.
500 if(subscription.open_socket(subscription,"",socket_type::BCM) < 0)
503 // If it's not an OBD2 CAN ID then just add a simple RX_SETUP job
504 // else monitor all standard 8 CAN OBD2 ID response.
506 can_message_t msg = can_message_t();
508 msg.set_bcm_msg(bcm_msg);
510 if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
512 subscription.socket_->write_message(msg);
513 if(! subscription.socket_)
518 for(uint8_t i = 0; i < 8; i++)
520 bcm_msg.msg_head.can_id = OBD2_FUNCTIONAL_RESPONSE_START + i;
521 msg.set_bcm_msg(bcm_msg);
522 subscription.socket_->write_message(msg);
523 if(! subscription.socket_)
530 /// @brief Creates a TX_SEND job that is used by the BCM socket to
533 /// @return 0 if ok else -1
534 int low_can_subscription_t::tx_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
536 can_message_t *cm = static_cast<can_message_t*>(message);
538 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
539 std::vector<canfd_frame> cfd_vect = cm->convert_to_canfd_frame_vector();
540 for(auto cfd: cfd_vect)
542 subscription.add_one_bcm_frame(cfd, bcm_msg);
545 if(subscription.open_socket(subscription, bus_name,socket_type::BCM) < 0)
550 cm->set_bcm_msg(bcm_msg);
551 subscription.socket_->write_message(*cm);
552 if(! subscription.socket_.get())
560 #ifdef USE_FEATURE_J1939
561 int low_can_subscription_t::j1939_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
563 //struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cfd.can_id);
564 //subscription.add_one_bcm_frame(cfd, bcm_msg);
566 if(subscription.open_socket(subscription, bus_name, socket_type::J1939) < 0)
571 j1939_message_t *jm = static_cast<j1939_message_t*>(message);
572 jm->set_sockname(jm->get_pgn(),J1939_NO_NAME,J1939_NO_ADDR);
573 if(subscription.socket_->write_message(*jm) < 0)
575 AFB_ERROR("Error write j1939 message");