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"
23 low_can_subscription_t::low_can_subscription_t()
30 low_can_subscription_t::low_can_subscription_t(struct event_filter_t event_filter)
32 event_filter_{event_filter},
37 low_can_subscription_t::low_can_subscription_t( low_can_subscription_t&& s)
39 event_filter_{s.event_filter_},
41 socket_{std::move(s.socket_)}
44 low_can_subscription_t& low_can_subscription_t::operator=(const low_can_subscription_t& s)
46 socket_ = std::move(s.socket_);
50 low_can_subscription_t::~low_can_subscription_t()
55 low_can_subscription_t::operator bool() const
57 return ((can_signal_ != nullptr || ! diagnostic_message_.empty()) && ! socket_);
59 afb_event_t low_can_subscription_t::get_event()
65 * @brief Set the event calling the afb_daemon_make_event function to
66 * create it and the checks its validity.
68 * @return int - 0 if OK, -1 if not
70 int low_can_subscription_t::set_event()
72 std::string event_name = get_name();
73 event_ = afb_daemon_make_event(event_name.c_str());
74 if (! afb_event_is_valid(event_))
76 AFB_ERROR("Can't create an event for %s, something goes wrong.", event_name.c_str());
84 * @brief Subscribe to the event member of the object
86 * @param request the subscribe AFB client request which want to
89 * @return int - 0 if OK, -1 if not
91 int low_can_subscription_t::subscribe(afb_req_t request)
95 return afb_req_subscribe(request, event_);
99 * @brief Unsubscribe to the event member of the object
101 * @param request the unsubscribe AFB client request which want to
104 * @return int - 0 if OK, -1 if not
106 int low_can_subscription_t::unsubscribe(afb_req_t request)
108 return afb_req_unsubscribe(request, event_);
111 int low_can_subscription_t::get_index() const
116 const std::shared_ptr<can_signal_t> low_can_subscription_t::get_can_signal() const
121 bool low_can_subscription_t::is_signal_subscription_corresponding(const std::shared_ptr<can_signal_t> can_signal, const struct event_filter_t& event_filter) const
123 return can_signal_ == can_signal && event_filter_ == event_filter;
126 const std::vector<std::shared_ptr<diagnostic_message_t> > low_can_subscription_t::get_diagnostic_message() const
128 return diagnostic_message_;
131 /// @brief Retrieve a diagnostic_message subscribed from a pid
133 /// @param[in] pid - Diagnostic messages PID to search for
135 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
136 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(uint32_t pid) const
138 for(const auto& diag: diagnostic_message_)
140 if(diag->get_pid() == pid)
148 /// @brief Retrieve a diagnostic message search from its name
150 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
151 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(const std::string& name) const
153 for(const auto& diag: diagnostic_message_)
155 if(diag->get_name() == name)
163 /// @brief Return the CAN signal name and empty string if not found
164 /// or no CAN signal subscribed
165 const std::string low_can_subscription_t::get_name() const
167 if (can_signal_ != nullptr)
168 return can_signal_->get_name();
169 else if (!diagnostic_message_.empty())
170 return "diagnostic_messages";
172 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
176 /// @brief Return name from a diagnostic message from a PID
178 /// @param[in] pid - Diagnostic message PID
179 const std::string low_can_subscription_t::get_name(uint32_t pid) const
181 if (!diagnostic_message_.empty())
182 return get_diagnostic_message(pid)->get_name() ;
184 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
188 float low_can_subscription_t::get_frequency() const
190 return event_filter_.frequency;
193 float low_can_subscription_t::get_min() const
195 return event_filter_.min;
198 float low_can_subscription_t::get_max() const
200 return event_filter_.max;
203 std::shared_ptr<utils::socketcan_t> low_can_subscription_t::get_socket()
208 void low_can_subscription_t::set_frequency(float freq)
210 event_filter_.frequency = freq;
213 void low_can_subscription_t::set_min(float min)
215 event_filter_.min = min;
218 void low_can_subscription_t::set_max(float max)
220 event_filter_.max = max;
222 /// @brief Based upon which object is a subscribed CAN signal or diagnostic message
223 /// it will open the socket with the required CAN bus device name.
225 /// @return INVALID_SOCKET on failure, else positive integer
226 int low_can_subscription_t::open_socket(const std::string& bus_name)
231 if( can_signal_ != nullptr)
232 {ret = socket_->open(can_signal_->get_message()->get_bus_device_name());}
233 else if (! diagnostic_message_ .empty())
234 {ret = socket_->open(application_t::instance().get_diagnostic_manager().get_bus_device_name());}
235 else if ( ! bus_name.empty())
236 { ret = socket_->open(bus_name);}
237 index_ = (int)socket_->socket();
242 /// @brief Builds a BCM message head but doesn't set can_frame.
244 /// @returns a bcm_msg with the msg_head parts set and can_frame
246 struct utils::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) const
248 struct utils::bcm_msg bcm_msg;
249 ::memset(&bcm_msg, 0, sizeof(bcm_msg));
251 bcm_msg.msg_head.opcode = opcode;
252 bcm_msg.msg_head.can_id = can_id;
253 bcm_msg.msg_head.flags = flags;
254 bcm_msg.msg_head.ival1.tv_sec = timeout.tv_sec ;
255 bcm_msg.msg_head.ival1.tv_usec = timeout.tv_usec;
256 bcm_msg.msg_head.ival2.tv_sec = frequency_thinning.tv_sec ;
257 bcm_msg.msg_head.ival2.tv_usec = frequency_thinning.tv_usec;
262 /// @brief Take an existing bcm_msg struct and add a can_frame.
263 /// Currently only 1 uniq can_frame can be added, it's not possible to build
264 /// a multiplexed message with several can_frame.
265 void low_can_subscription_t::add_one_bcm_frame(struct canfd_frame& cfd, struct utils::bcm_msg& bcm_msg) const
269 if (bcm_msg.msg_head.flags & CAN_FD_FRAME)
270 bcm_msg.fd_frames[bcm_msg.msg_head.nframes] = cfd;
273 cf.can_id = cfd.can_id;
274 cf.can_dlc = cfd.len;
275 ::memcpy(&cf.data, cfd.data, cfd.len);
276 bcm_msg.frames[bcm_msg.msg_head.nframes] = cf;
278 bcm_msg.msg_head.nframes++;
281 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a CAN signal
284 /// @return 0 if ok else -1
285 int low_can_subscription_t::create_rx_filter(std::shared_ptr<can_signal_t> sig)
289 struct timeval freq, timeout = {0, 0};
290 struct canfd_frame cfd;
293 if (sig->get_message()->is_fd())
295 flags = SETTIMER|RX_NO_AUTOTIMER|CAN_FD_FRAME;
296 cfd.len = CANFD_MAX_DLEN;
300 flags = SETTIMER|RX_NO_AUTOTIMER;
301 cfd.len = CAN_MAX_DLEN;
303 val = (float)(1 << can_signal_->get_bit_size()) - 1;
304 if(! bitfield_encode_float(val,
305 can_signal_->get_bit_position(),
306 can_signal_->get_bit_size(),
308 can_signal_->get_offset(),
313 frequency_clock_t f = event_filter_.frequency == 0 ? can_signal_->get_frequency() : frequency_clock_t(event_filter_.frequency);
314 freq = f.get_timeval_from_period();
316 utils::bcm_msg bcm_msg = make_bcm_head(RX_SETUP, can_signal_->get_message()->get_id(), flags, timeout, freq);
317 add_one_bcm_frame(cfd, bcm_msg);
319 return create_rx_filter(bcm_msg);
322 /// @brief Create a RX_SETUP receive job used by the BCM socket directly from
323 /// a bcm_msg. The method should not be used directly but rather through the
324 /// two previous method with can_signal_t or diagnostic_message_t object.
326 /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed
327 /// to the 8 classics OBD2 functional response ID
329 /// @return 0 if ok else -1
330 int low_can_subscription_t::create_rx_filter(utils::bcm_msg& bcm_msg)
332 // Make sure that socket is opened.
333 if(open_socket() < 0)
336 // If it's not an OBD2 CAN ID then just add a simple RX_SETUP job
337 // else monitor all standard 8 CAN OBD2 ID response.
338 if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
340 socket_->write_message(bcm_msg);
346 for(uint8_t i = 0; i < 8; i++)
348 bcm_msg.msg_head.can_id = OBD2_FUNCTIONAL_RESPONSE_START + i;
350 socket_->write_message(bcm_msg);
359 /// @brief Create a RX_SETUP receive job to be used by the BCM socket for a
360 /// diagnostic message subscription.
362 /// @return 0 if ok else -1
363 int low_can_subscription_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
365 diagnostic_message_.push_back(sig);
367 struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
368 //struct timeval timeout = frequency_clock_t(10).get_timeval_from_period();
369 struct timeval timeout = {0,0};
371 utils::bcm_msg bcm_msg = make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER|RX_NO_AUTOTIMER|RX_FILTER_ID, timeout, freq);
372 return create_rx_filter(bcm_msg);
375 /// @brief Creates a TX_SEND job that is used by the BCM socket to
378 /// @return 0 if ok else -1
379 int low_can_subscription_t::tx_send(struct canfd_frame& cfd, const std::string& bus_name)
381 struct utils::bcm_msg bcm_msg = make_bcm_head(TX_SEND, cfd.can_id);
382 add_one_bcm_frame(cfd, bcm_msg);
384 if(open_socket(bus_name) < 0)
387 socket_->write_message(bcm_msg);