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"
24 #ifdef USE_FEATURE_J1939
25 #include "../utils/socketcan-j1939.hpp"
28 low_can_subscription_t::low_can_subscription_t()
35 low_can_subscription_t::low_can_subscription_t(struct event_filter_t event_filter)
37 event_filter_{event_filter},
42 low_can_subscription_t::low_can_subscription_t( low_can_subscription_t&& s)
44 event_filter_{s.event_filter_},
46 socket_{std::move(s.socket_)}
49 low_can_subscription_t& low_can_subscription_t::operator=(const low_can_subscription_t& s)
51 socket_ = std::move(s.socket_);
55 low_can_subscription_t::~low_can_subscription_t()
61 low_can_subscription_t::operator bool() const
63 return ((signal_ != nullptr || ! diagnostic_message_.empty()) && ! socket_);
65 afb_event_t low_can_subscription_t::get_event()
71 * @brief Set the event calling the afb_daemon_make_event function to
72 * create it and the checks its validity.
74 * @return int - 0 if OK, -1 if not
76 int low_can_subscription_t::set_event()
78 std::string event_name = get_name();
79 event_ = afb_daemon_make_event(event_name.c_str());
80 if (! afb_event_is_valid(event_))
82 AFB_ERROR("Can't create an event for %s, something goes wrong.", event_name.c_str());
90 * @brief Subscribe to the event member of the object
92 * @param request the subscribe AFB client request which want to
95 * @return int - 0 if OK, -1 if not
97 int low_can_subscription_t::subscribe(afb_req_t request)
101 return afb_req_subscribe(request, event_);
105 * @brief Unsubscribe to the event member of the object
107 * @param request the unsubscribe AFB client request which want to
110 * @return int - 0 if OK, -1 if not
112 int low_can_subscription_t::unsubscribe(afb_req_t request)
114 return afb_req_unsubscribe(request, event_);
117 int low_can_subscription_t::get_index() const
122 const std::shared_ptr<signal_t> low_can_subscription_t::get_signal() const
127 bool low_can_subscription_t::is_signal_subscription_corresponding(const std::shared_ptr<signal_t> signal, const struct event_filter_t& event_filter) const
129 return signal_ == signal && event_filter_ == event_filter;
132 const std::vector<std::shared_ptr<diagnostic_message_t> > low_can_subscription_t::get_diagnostic_message() const
134 return diagnostic_message_;
137 /// @brief Retrieve a diagnostic_message subscribed from a pid
139 /// @param[in] pid - Diagnostic messages PID to search for
141 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
142 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(uint32_t pid) const
144 for(const auto& diag: diagnostic_message_)
146 if(diag->get_pid() == pid)
154 /// @brief Retrieve a diagnostic message search from its name
156 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
157 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(const std::string& name) const
159 for(const auto& diag: diagnostic_message_)
161 if(diag->get_name() == name)
169 /// @brief Return the CAN signal name and empty string if not found
170 /// or no CAN signal subscribed
171 const std::string low_can_subscription_t::get_name() const
173 if (signal_ != nullptr)
174 return signal_->get_name();
175 else if (!diagnostic_message_.empty())
176 return "diagnostic_messages";
178 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
182 /// @brief Return name from a diagnostic message from a PID
184 /// @param[in] pid - Diagnostic message PID
185 const std::string low_can_subscription_t::get_name(uint32_t pid) const
187 if (!diagnostic_message_.empty())
188 return get_diagnostic_message(pid)->get_name() ;
190 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
194 float low_can_subscription_t::get_frequency() const
196 return event_filter_.frequency;
199 float low_can_subscription_t::get_min() const
201 return event_filter_.min;
204 float low_can_subscription_t::get_max() const
206 return event_filter_.max;
209 std::shared_ptr<utils::socketcan_t> low_can_subscription_t::get_socket()
214 void low_can_subscription_t::set_frequency(float freq)
216 event_filter_.frequency = freq;
219 void low_can_subscription_t::set_min(float min)
221 event_filter_.min = min;
224 void low_can_subscription_t::set_max(float max)
226 event_filter_.max = max;
229 /// @brief Based upon which object is a subscribed CAN signal or diagnostic message
230 /// it will open the socket with the required CAN bus device name.
232 /// @return INVALID_SOCKET on failure, else positive integer
233 int low_can_subscription_t::open_socket(low_can_subscription_t &subscription, const std::string& bus_name)
236 if(! subscription.socket_)
239 #ifdef USE_FEATURE_J1939
240 if((subscription.signal_ != nullptr || !bus_name.empty()) && subscription.signal_->get_message()->is_j1939())
242 name_t name = J1939_NO_NAME;
243 pgn_t pgn = J1939_NO_PGN;
244 uint8_t addr = J1939_NO_ADDR;
245 pgn = subscription.signal_->get_message()->get_id();
246 if( subscription.signal_ != nullptr)
248 std::shared_ptr<utils::socketcan_j1939_t> socket = std::make_shared<utils::socketcan_j1939_t>();
249 ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), name, pgn, addr);
250 subscription.socket_ = socket;
252 else if ( !bus_name.empty())
254 std::shared_ptr<utils::socketcan_j1939_t> socket = std::make_shared<utils::socketcan_j1939_t>();
255 ret = socket->open(bus_name, name, pgn, addr);
256 subscription.socket_ = socket;
258 subscription.index_ = (int)subscription.socket_->socket();
263 if( subscription.signal_ != nullptr)
265 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
266 ret = subscription.socket_->open(subscription.signal_->get_message()->get_bus_device_name());
268 else if (! subscription.diagnostic_message_ .empty())
270 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
271 ret = subscription.socket_->open(application_t::instance().get_diagnostic_manager().get_bus_device_name());
273 else if ( !bus_name.empty())
275 subscription.socket_ = std::make_shared<utils::socketcan_bcm_t>();
276 ret = subscription.socket_->open(bus_name);
278 subscription.index_ = (int)subscription.socket_->socket();
279 #ifdef USE_FEATURE_J1939
286 /// @brief Builds a BCM message head but doesn't set can_frame.
288 /// @returns a bcm_msg with the msg_head parts set and can_frame
290 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)
292 struct bcm_msg bcm_msg;
293 ::memset(&bcm_msg, 0, sizeof(bcm_msg));
295 bcm_msg.msg_head.opcode = opcode;
296 bcm_msg.msg_head.can_id = can_id;
297 bcm_msg.msg_head.flags = flags;
298 bcm_msg.msg_head.ival1.tv_sec = timeout.tv_sec ;
299 bcm_msg.msg_head.ival1.tv_usec = timeout.tv_usec;
300 bcm_msg.msg_head.ival2.tv_sec = frequency_thinning.tv_sec ;
301 bcm_msg.msg_head.ival2.tv_usec = frequency_thinning.tv_usec;
306 /// @brief Take an existing bcm_msg struct and add a can_frame.
307 /// Currently only 1 uniq can_frame can be added, it's not possible to build
308 /// a multiplexed message with several can_frame.
309 void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct bcm_msg& bcm_msg)
313 if (bcm_msg.msg_head.flags & CAN_FD_FRAME)
314 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cfd;
317 cf.can_id = cfd.can_id;
318 cf.can_dlc = cfd.len;
319 ::memcpy(&cf.data, cfd.data, cfd.len);
320 bcm_msg.frames[bcm_msg.msg_head.nframes] = cf;
322 bcm_msg.msg_head.nframes++;
325 #ifdef USE_FEATURE_J1939
326 int low_can_subscription_t::create_rx_filter_j1939(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
328 subscription.signal_= sig;
330 // Make sure that socket is opened.
331 if(open_socket(subscription) < 0)
339 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a CAN signal
342 /// @return 0 if ok else -1
343 int low_can_subscription_t::create_rx_filter_can(low_can_subscription_t &subscription, std::shared_ptr<signal_t> sig)
347 struct timeval freq, timeout = {0, 0};
348 struct canfd_frame cfd;
349 subscription.signal_= sig;
351 if (sig->get_message()->is_fd())
353 flags = SETTIMER|RX_NO_AUTOTIMER|CAN_FD_FRAME;
354 cfd.len = CANFD_MAX_DLEN;
358 flags = SETTIMER|RX_NO_AUTOTIMER;
359 cfd.len = CAN_MAX_DLEN;
361 val = (float)(1 << subscription.signal_->get_bit_size()) - 1;
362 if(! bitfield_encode_float(val,
363 subscription.signal_->get_bit_position(),
364 subscription.signal_->get_bit_size(),
366 subscription.signal_->get_offset(),
371 frequency_clock_t f = subscription.event_filter_.frequency == 0 ? subscription.signal_->get_frequency() : frequency_clock_t(subscription.event_filter_.frequency);
372 freq = f.get_timeval_from_period();
374 struct bcm_msg bcm_msg = subscription.make_bcm_head(RX_SETUP, subscription.signal_->get_message()->get_id(), flags, timeout, freq);
375 subscription.add_one_bcm_frame(cfd, bcm_msg);
377 return create_rx_filter_bcm(subscription, bcm_msg);
380 int low_can_subscription_t::create_rx_filter(std::shared_ptr<signal_t> sig)
382 #ifdef USE_FEATURE_J1939
383 if(sig->get_message()->is_j1939())
385 return low_can_subscription_t::create_rx_filter_j1939(*this, sig);
390 return low_can_subscription_t::create_rx_filter_can(*this, sig);
391 #ifdef USE_FEATURE_J1939
397 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a
398 /// diagnostic message subscription.
400 /// @return 0 if ok else -1
401 int low_can_subscription_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
403 diagnostic_message_.push_back(sig);
405 struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
406 //struct timeval timeout = frequency_clock_t(10).get_timeval_from_period();
407 struct timeval timeout = {0,0};
409 struct bcm_msg bcm_msg = make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER|RX_NO_AUTOTIMER|RX_FILTER_ID, timeout, freq);
410 return create_rx_filter_bcm(*this, bcm_msg);
413 /// @brief Create a RX_SETUP receive job used by the BCM socket directly from
414 /// a bcm_msg. The method should not be used directly but rather through the
415 /// two previous method with signal_t or diagnostic_message_t object.
417 /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed
418 /// to the 8 classics OBD2 functional response ID
420 /// @return 0 if ok else -1
421 int low_can_subscription_t::create_rx_filter_bcm(low_can_subscription_t &subscription, struct bcm_msg& bcm_msg)
423 // Make sure that socket is opened.
424 if(subscription.open_socket(subscription) < 0)
427 // If it's not an OBD2 CAN ID then just add a simple RX_SETUP job
428 // else monitor all standard 8 CAN OBD2 ID response.
430 can_message_t msg = can_message_t();
432 msg.set_bcm_msg(bcm_msg);
434 if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
436 subscription.socket_->write_message(msg);
437 if(! subscription.socket_)
442 for(uint8_t i = 0; i < 8; i++)
444 bcm_msg.msg_head.can_id = OBD2_FUNCTIONAL_RESPONSE_START + i;
445 msg.set_bcm_msg(bcm_msg);
446 subscription.socket_->write_message(msg);
447 if(! subscription.socket_)
454 /// @brief Creates a TX_SEND job that is used by the BCM socket to
457 /// @return 0 if ok else -1
458 int low_can_subscription_t::tx_send(low_can_subscription_t &subscription, message_t *message, const std::string& bus_name)
460 can_message_t *cm = static_cast<can_message_t*>(message);
462 struct bcm_msg bcm_msg = subscription.make_bcm_head(TX_SEND, cm->get_id(),cm->get_flags());
463 canfd_frame cfd = cm->convert_to_canfd_frame();
464 subscription.add_one_bcm_frame(cfd, bcm_msg);
466 if(subscription.open_socket(subscription, bus_name) < 0)
469 cm->set_bcm_msg(bcm_msg);
470 subscription.socket_->write_message(*cm);
471 if(! subscription.socket_.get())