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_socket_t::low_can_socket_t()
29 low_can_socket_t::low_can_socket_t(struct event_filter_t event_filter)
30 : event_filter_{event_filter}
33 low_can_socket_t::low_can_socket_t( low_can_socket_t&& s)
35 event_filter_{s.event_filter_},
36 socket_{std::move(s.socket_)}
39 low_can_socket_t& low_can_socket_t::operator=(const low_can_socket_t& s)
41 socket_ = std::move(s.socket_);
45 low_can_socket_t::~low_can_socket_t()
50 low_can_socket_t::operator bool() const
52 return ((can_signal_ != nullptr || ! diagnostic_message_.empty()) && ! socket_);
55 int low_can_socket_t::get_index() const
60 const std::shared_ptr<can_signal_t> low_can_socket_t::get_can_signal() const
65 bool low_can_socket_t::is_signal_subscription_corresponding(const std::shared_ptr<can_signal_t> can_signal, const struct event_filter_t& event_filter) const
67 return can_signal_ == can_signal && event_filter_ == event_filter;
70 const std::vector<std::shared_ptr<diagnostic_message_t> > low_can_socket_t::get_diagnostic_message() const
72 return diagnostic_message_;
75 /// @brief Retrieve a diagnostic_message subscribed from a pid
77 /// @param[in] pid - Diagnostic messages PID to search for
79 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
80 const std::shared_ptr<diagnostic_message_t> low_can_socket_t::get_diagnostic_message(uint32_t pid) const
82 for(const auto& diag: diagnostic_message_)
84 if(diag->get_pid() == pid)
92 /// @brief Retrieve a diagnostic message search from its name
94 /// @return shared_ptr diagnostic_message_ if found and nullptr if not found
95 const std::shared_ptr<diagnostic_message_t> low_can_socket_t::get_diagnostic_message(const std::string& name) const
97 for(const auto& diag: diagnostic_message_)
99 if(diag->get_name() == name)
107 /// @brief Return the CAN signal name and empty string if not found
108 /// or no CAN signal subscribed
109 const std::string low_can_socket_t::get_name() const
111 if (can_signal_ != nullptr)
112 return can_signal_->get_name();
113 else if (!diagnostic_message_.empty())
114 return "diagnostic_messages";
116 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
120 /// @brief Return name from a diagnostic message from a PID
122 /// @param[in] pid - Diagnostic message PID
123 const std::string low_can_socket_t::get_name(uint32_t pid) const
125 if (!diagnostic_message_.empty())
126 return get_diagnostic_message(pid)->get_name() ;
128 AFB_WARNING("No diagnostics messages nor CAN signals registered in that subscription. Name empty ! It's a bug to be reported.");
132 float low_can_socket_t::get_frequency() const
134 return event_filter_.frequency;
137 float low_can_socket_t::get_min() const
139 return event_filter_.min;
142 float low_can_socket_t::get_max() const
144 return event_filter_.max;
147 utils::socketcan_bcm_t& low_can_socket_t::get_socket()
152 void low_can_socket_t::set_frequency(float freq)
154 event_filter_.frequency = freq;
157 void low_can_socket_t::set_min(float min)
159 event_filter_.min = min;
162 void low_can_socket_t::set_max(float max)
164 event_filter_.max = max;
166 /// @brief Based upon which object is subscribed CAN signal or diagnostic message
167 /// this will open the socket with the required CAN bus device name.
169 /// @return INVALID_SOCKET on failure else positive integer
170 int low_can_socket_t::open_socket(const std::string& bus_name)
175 if( can_signal_ != nullptr)
176 {ret = socket_.open(can_signal_->get_message()->get_bus_device_name());}
177 else if (! diagnostic_message_ .empty())
178 {ret = socket_.open(application_t::instance().get_diagnostic_manager().get_bus_device_name());}
179 else if ( ! bus_name.empty())
180 { ret = socket_.open(bus_name);}
181 index_ = (int)socket_.socket();
186 /// @brief Build a BCM message head but don't set can_frame.
188 /// @return a simple_bcm_msg with the msg_head parts set and can_frame
190 struct utils::simple_bcm_msg low_can_socket_t::make_bcm_head(uint32_t opcode, uint32_t can_id, uint32_t flags, const struct timeval& timeout, const struct timeval& frequency_thinning) const
192 struct utils::simple_bcm_msg bcm_msg;
193 ::memset(&bcm_msg, 0, sizeof(bcm_msg));
195 bcm_msg.msg_head.opcode = opcode;
196 bcm_msg.msg_head.can_id = can_id;
197 bcm_msg.msg_head.flags = flags;
198 bcm_msg.msg_head.ival1.tv_sec = timeout.tv_sec ;
199 bcm_msg.msg_head.ival1.tv_usec = timeout.tv_usec;
200 bcm_msg.msg_head.ival2.tv_sec = frequency_thinning.tv_sec ;
201 bcm_msg.msg_head.ival2.tv_usec = frequency_thinning.tv_usec;
206 /// @brief Take an existing simple_bcm_msg struct and add a can_frame to it.
207 /// Only possible for now to add 1 uniq can_frame, it isn't possible to build
208 /// a multiplexed message with several can_frame.
209 void low_can_socket_t::add_bcm_frame(const struct can_frame& cf, struct utils::simple_bcm_msg& bcm_msg) const
211 for(int i=0; i < CAN_MAX_DLEN; i++)
215 bcm_msg.msg_head.nframes = 1;
222 /// @brief Create a RX_SETUP receive job used by the BCM socket for a CAN signal
225 /// @return 0 if ok else -1
226 int low_can_socket_t::create_rx_filter(std::shared_ptr<can_signal_t> sig)
230 struct can_frame cfd;
231 memset(&cfd, 0, sizeof(cfd));
233 float val = (float)(1 << can_signal_->get_bit_size()) - 1;
234 bitfield_encode_float(val,
235 can_signal_->get_bit_position(),
236 can_signal_->get_bit_size(),
237 can_signal_->get_factor(),
238 can_signal_->get_offset(),
242 struct timeval freq, timeout = {0, 0};
243 frequency_clock_t f = event_filter_.frequency == 0 ? can_signal_->get_frequency() : frequency_clock_t(event_filter_.frequency);
244 freq = f.get_timeval_from_period();
246 utils::simple_bcm_msg bcm_msg = make_bcm_head(RX_SETUP, can_signal_->get_message()->get_id(), SETTIMER|RX_NO_AUTOTIMER, timeout, freq);
247 add_bcm_frame(cfd, bcm_msg);
249 return create_rx_filter(bcm_msg);
252 /// @brief Create a RX_SETUP receive job used by the BCM socket for a
253 /// diagnostic message subscription.
255 /// @return 0 if ok else -1
256 int low_can_socket_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
258 diagnostic_message_.push_back(sig);
260 struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
261 //struct timeval timeout = frequency_clock_t(10).get_timeval_from_period();
262 struct timeval timeout = {0,0};
264 utils::simple_bcm_msg bcm_msg = make_bcm_head(RX_SETUP, OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER|RX_NO_AUTOTIMER|RX_FILTER_ID, timeout, freq);
265 return create_rx_filter(bcm_msg);
268 /// @brief Create a RX_SETUP receive job used by the BCM socket directly from
269 /// a simple_bcm_msg. You will not use this method directly but rather use the
270 /// two previous method with can_signal_t or diagnostic_message_t object.
272 /// If the CAN arbitration ID is the OBD2 functional broadcast id the subscribed
273 /// to the 8 classics OBD2 functional response ID
275 /// @return 0 if ok else -1
276 int low_can_socket_t::create_rx_filter(utils::simple_bcm_msg& bcm_msg)
278 // Make sure that socket has been opened.
279 if(open_socket() < 0)
282 // If it isn't an OBD2 CAN ID then just add a simple RX_SETUP job
283 // else monitor all standard 8 CAN OBD2 ID response.
284 if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
292 for(uint8_t i = 0; i < 8; i++)
294 bcm_msg.msg_head.can_id = OBD2_FUNCTIONAL_RESPONSE_START + i;
305 /// @brief Create a TX_SEND job used by the BCM socket to
306 /// simply send message
308 /// @return 0 if ok else -1
309 int low_can_socket_t::tx_send(const struct can_frame& cf, std::shared_ptr<can_signal_t> sig)
313 utils::simple_bcm_msg bcm_msg = make_bcm_head(TX_SEND);
314 add_bcm_frame(cf, bcm_msg);
316 if(open_socket() < 0)
326 /// @brief Create a TX_SEND job used by the BCM socket to
327 /// simply send message
329 /// @return 0 if ok else -1
330 int low_can_socket_t::tx_send(const struct can_frame& cf, const std::string& bus_name)
332 can_signal_ = nullptr;
334 utils::simple_bcm_msg bcm_msg = make_bcm_head(TX_SEND);
335 add_bcm_frame(cf, bcm_msg);
337 if(open_socket(bus_name) < 0)