2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include "can-signals.hpp"
22 #include "../binding/configuration.hpp"
23 #include "../utils/signals.hpp"
24 #include "can-decoder.hpp"
25 #include "can-message.hpp"
26 #include "can-bus.hpp"
27 #include "../diagnostic/diagnostic-message.hpp"
28 #include "canutil/write.h"
30 std::string can_signal_t::prefix_ = "messages";
32 can_signal_t::can_signal_t(
33 std::string generic_name,
40 frequency_clock_t frequency,
42 bool force_send_changed,
43 std::map<uint8_t, std::string> states,
45 SignalDecoder decoder,
46 SignalEncoder encoder,
49 generic_name_{ generic_name }
50 , bit_position_{ bit_position }
51 , bit_size_{ bit_size }
54 , min_value_{min_value}
55 , max_value_{max_value}
56 , frequency_{frequency}
57 , send_same_{send_same}
58 , force_send_changed_{force_send_changed}
67 /*can_signal_t::can_signal_t(const can_signal_t& b)
69 generic_name_{ b.generic_name_}
70 , bit_position_{ b.bit_position_}
71 , bit_size_{ b.bit_size_}
72 , factor_{ b.factor_ }
74 , min_value_{b.min_value_}
75 , max_value_{b.max_value_}
76 , frequency_{b.frequency_}
77 , send_same_{b.send_same_}
78 , force_send_changed_{b.force_send_changed_}
80 , writable_{b.writable_}
81 , decoder_{b.decoder_}
82 , encoder_{b.encoder_}
83 , received_{b.received_}
84 , last_value_{b.last_value_}
87 utils::socketcan_bcm_t can_signal_t::get_socket() const
92 can_message_definition_t* can_signal_t::get_message() const
97 const std::string can_signal_t::get_generic_name() const
102 const std::string can_signal_t::get_name() const
104 return prefix_ + "." + generic_name_;
107 const std::string can_signal_t::get_prefix() const
112 uint8_t can_signal_t::get_bit_position() const
114 return bit_position_;
117 uint8_t can_signal_t::get_bit_size() const
122 float can_signal_t::get_factor() const
127 float can_signal_t::get_offset() const
132 float can_signal_t::get_min_value() const
137 float can_signal_t::get_max_value() const
142 frequency_clock_t& can_signal_t::get_frequency()
147 bool can_signal_t::get_send_same() const
152 bool can_signal_t::get_force_send_changed() const
154 return force_send_changed_;
157 const std::map<uint8_t, std::string>& can_signal_t::get_states() const
162 const std::string can_signal_t::get_states(uint8_t value)
164 if (value < states_.size())
165 return states_[value];
166 return std::string();
169 size_t can_signal_t::get_state_count() const
171 return states_.size();
174 bool can_signal_t::get_writable() const
179 SignalDecoder& can_signal_t::get_decoder()
184 SignalEncoder& can_signal_t::get_encoder()
189 bool can_signal_t::get_received() const
193 float can_signal_t::get_last_value() const
198 void can_signal_t::set_parent(can_message_definition_t* parent)
203 void can_signal_t::set_prefix(std::string val)
208 void can_signal_t::set_received(bool r)
213 void can_signal_t::set_last_value(float val)
218 void can_signal_t::set_timestamp(uint64_t timestamp)
220 frequency_.tick(timestamp);
223 /// @brief Create a RX_SETUP receive job used by the BCM socket.
225 /// @return 0 if ok else -1
226 int can_signal_t::create_rx_filter()
228 // Make sure that socket has been opened.
231 get_message()->get_bus_name());
233 uint32_t can_id = get_message()->get_id();
235 struct utils::simple_bcm_msg bcm_msg;
236 struct can_frame cfd;
238 memset(&cfd, 0, sizeof(cfd));
239 memset(&bcm_msg.msg_head, 0, sizeof(bcm_msg.msg_head));
240 float val = (float)(1 << bit_size_)-1;
241 struct timeval freq = frequency_.get_timeval_from_period();
243 bcm_msg.msg_head.opcode = RX_SETUP;
244 bcm_msg.msg_head.can_id = can_id;
245 bcm_msg.msg_head.flags = SETTIMER|RX_NO_AUTOTIMER;
246 bcm_msg.msg_head.ival2.tv_sec = freq.tv_sec ;
247 bcm_msg.msg_head.ival2.tv_usec = freq.tv_usec;
248 bcm_msg.msg_head.nframes = 1;
249 bitfield_encode_float(val,
257 bcm_msg.frames = cfd;
259 if(socket_ << bcm_msg)