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-hat.hpp"
21 #include "low-can-subscription.hpp"
22 #include "application.hpp"
23 #include "canutil/write.h"
25 ///******************************************************************************
27 /// low_can_subscription_t object
29 ///*******************************************************************************/
31 low_can_subscription_t::low_can_subscription_t()
33 event_filter_{event_filter_t()},
37 low_can_subscription_t::low_can_subscription_t(struct event_filter_t event_filter)
38 : event_filter_{event_filter}
41 low_can_subscription_t::low_can_subscription_t( low_can_subscription_t&& s)
43 event_filter_{s.event_filter_},
44 socket_{std::move(s.socket_)}
47 low_can_subscription_t& low_can_subscription_t::operator=(const low_can_subscription_t& s)
49 socket_ = std::move(s.socket_);
53 low_can_subscription_t::~low_can_subscription_t()
58 low_can_subscription_t::operator bool() const
60 return ((can_signal_ != nullptr || ! diagnostic_message_.empty()) && afb_event_is_valid(event_));
63 struct afb_event& low_can_subscription_t::get_event()
68 int low_can_subscription_t::get_index() const
73 const std::shared_ptr<can_signal_t> low_can_subscription_t::get_can_signal() const
78 const std::vector<std::shared_ptr<diagnostic_message_t> > low_can_subscription_t::get_diagnostic_message() const
80 return diagnostic_message_;
83 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(uint32_t pid) const
85 for(const auto& diag: diagnostic_message_)
87 if(diag->get_pid() == pid)
95 const std::shared_ptr<diagnostic_message_t> low_can_subscription_t::get_diagnostic_message(const std::string& name) const
97 for(const auto& diag: diagnostic_message_)
99 if(diag->get_name() == name)
107 const std::string low_can_subscription_t::get_name() const
109 if (can_signal_ != nullptr)
110 return can_signal_->get_name();
115 const std::string low_can_subscription_t::get_name(uint32_t pid) const
117 if (!diagnostic_message_.empty())
118 return get_diagnostic_message(pid)->get_name() ;
123 float low_can_subscription_t::get_frequency() const
125 return event_filter_.frequency;
128 float low_can_subscription_t::get_min() const
130 return event_filter_.min;
133 float low_can_subscription_t::get_max() const
135 return event_filter_.max;
138 utils::socketcan_bcm_t& low_can_subscription_t::get_socket()
143 void low_can_subscription_t::set_event(struct afb_event event)
148 void low_can_subscription_t::set_frequency(float freq)
150 event_filter_.frequency = freq;
153 void low_can_subscription_t::set_min(float min)
155 event_filter_.min = min;
158 void low_can_subscription_t::set_max(float max)
160 event_filter_.max = max;
163 int low_can_subscription_t::open_socket()
168 if( can_signal_ != nullptr)
169 {ret = socket_.open(can_signal_->get_message()->get_bus_device_name());}
170 else if (! diagnostic_message_ .empty())
171 {ret = socket_.open(application_t::instance().get_diagnostic_manager().get_bus_device_name());}
172 index_ = (int)socket_.socket();
177 struct utils::simple_bcm_msg low_can_subscription_t::make_bcm_head(uint32_t can_id, uint32_t flags, const struct timeval& timeout, const struct timeval& frequency_thinning) const
179 struct utils::simple_bcm_msg bcm_msg;
181 memset(&bcm_msg, 0, sizeof(bcm_msg));
183 bcm_msg.msg_head.opcode = RX_SETUP;
184 bcm_msg.msg_head.can_id = can_id;
185 bcm_msg.msg_head.flags = flags;
186 bcm_msg.msg_head.ival1.tv_sec = timeout.tv_sec ;
187 bcm_msg.msg_head.ival1.tv_usec = timeout.tv_usec;
188 bcm_msg.msg_head.ival2.tv_sec = frequency_thinning.tv_sec ;
189 bcm_msg.msg_head.ival2.tv_usec = frequency_thinning.tv_usec;
194 void low_can_subscription_t::add_bcm_frame(const struct can_frame& cfd, struct utils::simple_bcm_msg& bcm_msg) const
196 for(int i=0; i < CAN_MAX_DLEN; i++)
200 bcm_msg.msg_head.nframes = 1;
201 bcm_msg.frames = cfd;
207 /// @brief Create a RX_SETUP receive job used by the BCM socket.
209 /// @return 0 if ok else -1
210 int low_can_subscription_t::create_rx_filter()
213 if ( can_signal_ != nullptr)
214 {ret = create_rx_filter(can_signal_);}
215 else if (! diagnostic_message_ .empty())
216 {ret = create_rx_filter(diagnostic_message_.front());}
221 /// @brief Create a RX_SETUP receive job used by the BCM socket.
223 /// @return 0 if ok else -1
224 int low_can_subscription_t::create_rx_filter(std::shared_ptr<can_signal_t> sig)
228 struct can_frame cfd;
229 memset(&cfd, 0, sizeof(cfd));
231 float val = (float)(1 << can_signal_->get_bit_size()) - 1;
232 bitfield_encode_float(val,
233 can_signal_->get_bit_position(),
234 can_signal_->get_bit_size(),
235 can_signal_->get_factor(),
236 can_signal_->get_offset(),
240 struct timeval freq, timeout = {0, 0};
241 frequency_clock_t f = std::isnan(event_filter_.frequency) ? can_signal_->get_frequency() : frequency_clock_t(event_filter_.frequency);
242 freq = f.get_timeval_from_period();
244 utils::simple_bcm_msg bcm_msg = make_bcm_head(can_signal_->get_message()->get_id(), SETTIMER|RX_NO_AUTOTIMER, timeout, freq);
245 add_bcm_frame(cfd, bcm_msg);
247 return create_rx_filter(bcm_msg);
250 /// @brief Create a RX_SETUP receive job used by the BCM socket.
252 /// @return 0 if ok else -1
253 int low_can_subscription_t::create_rx_filter(std::shared_ptr<diagnostic_message_t> sig)
255 diagnostic_message_.push_back(sig);
257 struct timeval freq = frequency_clock_t(event_filter_.frequency).get_timeval_from_period();
258 //struct timeval timeout = frequency_clock_t(10).get_timeval_from_period();
259 struct timeval timeout = {0,0};
261 utils::simple_bcm_msg bcm_msg = make_bcm_head(OBD2_FUNCTIONAL_BROADCAST_ID, SETTIMER|RX_NO_AUTOTIMER|RX_FILTER_ID, timeout, freq);
262 return create_rx_filter(bcm_msg);
265 /// @brief Create a RX_SETUP receive job used by the BCM socket.
267 /// @return 0 if ok else -1
268 int low_can_subscription_t::create_rx_filter(utils::simple_bcm_msg& bcm_msg)
270 // Make sure that socket has been opened.
271 if(open_socket() < 0)
274 // If it isn't an OBD2 CAN ID then just add a simple RX_SETUP job
275 // else monitor all standard 8 CAN OBD2 ID response.
276 if(bcm_msg.msg_head.can_id != OBD2_FUNCTIONAL_BROADCAST_ID)
284 for(uint8_t i = 0; i < 8; i++)
286 bcm_msg.msg_head.can_id = OBD2_FUNCTIONAL_RESPONSE_START + i;