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.
18 #include "can-encoder.hpp"
20 #include "canutil/write.h"
21 #include "../utils/openxc-utils.hpp"
22 #include "message-definition.hpp"
23 #include "../utils/converter.hpp"
26 * @brief Allows to encode data for a signal
28 * @param sig The signal to know its location
29 * @param data The data to encod
30 * @param filter If true that will generate the filter BCM for the signal
31 * @param factor If true that will use the factor of the signal else 1
32 * @param offset If true that will use the offset of the signal else 0
34 void encoder_t::encode_data(std::shared_ptr<signal_t> sig, std::vector<uint8_t> &data, bool filter, bool factor, bool offset)
36 uint32_t bit_size = sig->get_bit_size();
37 uint32_t bit_position = sig->get_bit_position();
38 int new_start_byte = 0;
40 uint8_t new_start_bit = 0;
41 uint8_t new_end_bit = 0;
43 converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
45 int len_signal_bytes_tmp = new_end_byte - new_start_byte + 1;
47 uint8_t len_signal_bytes = 0;
48 if(len_signal_bytes_tmp > 255)
50 AFB_ERROR("Error signal %s too long",sig->get_name().c_str());
54 len_signal_bytes = (uint8_t) len_signal_bytes_tmp;
57 if(new_start_bit > 255)
59 AFB_ERROR("Error signal %s too long",sig->get_name().c_str());
62 uint8_t new_bit_size = 0;
65 AFB_ERROR("Error signal %s to long bit size",sig->get_name().c_str());
69 new_bit_size = (uint8_t) bit_size;
72 uint8_t data_signal[len_signal_bytes] = {0};
76 factor_v = sig->get_factor();
82 offset_v = sig->get_offset();
89 for(int i=0;i<new_bit_size;i++)
91 int mask = 1 << ((i%8)+new_start_bit);
96 AFB_ERROR("Error mask too large");
100 mask_v = (uint8_t) mask;
106 data_signal[j] = tmp;
115 bitfield_encode_float( sig->get_last_value(),
124 for(size_t i = new_start_byte; i <= new_end_byte ; i++)
126 data[i] = data[i] | data_signal[i-new_start_byte];
131 * @brief Allows to build a multi frame message with correct data to be send
133 * @param signal The CAN signal to write, including the bit position and bit size.
134 * @param value The encoded integer value to write in the CAN signal.
135 * @param message A multi frame message to complete
136 * @param factor If true that will use the factor of the signal else 1
137 * @param offset If true that will use the offset of the signal else 0
138 * @return message_t* The message that is generated
140 message_t* encoder_t::build_frame(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message, bool factor, bool offset)
142 signal->set_last_value((float)value);
143 std::vector<uint8_t> data;
144 for(int i = 0; i<message->get_length();i++)
149 for(const auto& sig: signal->get_message()->get_signals())
151 encode_data(sig,data,false,factor,offset);
153 message->set_data(data);
158 * @brief Allows to build a message_t with correct data to be send
160 * @param signal The CAN signal to write, including the bit position and bit size.
161 * @param value The encoded integer value to write in the CAN signal.
162 * @param factor If true that will use the factor of the signal else 1
163 * @param offset If true that will use the offset of the signal else 0
164 * @return message_t* The message that is generated
166 message_t* encoder_t::build_message(const std::shared_ptr<signal_t>& signal, uint64_t value, bool factor, bool offset)
169 std::vector<uint8_t> data;
170 if(signal->get_message()->is_fd())
172 message = new can_message_t( CANFD_MAX_DLEN,
173 signal->get_message()->get_id(),
176 signal->get_message()->get_flags(),
180 return build_frame(signal,value,message, factor, offset);
182 #ifdef USE_FEATURE_J1939
183 else if(signal->get_message()->is_j1939())
185 message = new j1939_message_t( signal->get_message()->get_length(),
189 signal->get_message()->get_id(),
191 return build_frame(signal,value,message, factor, offset);
196 message = new can_message_t(CAN_MAX_DLEN,
197 signal->get_message()->get_id(),
200 signal->get_message()->get_flags(),
203 return build_frame(signal,value,message, factor, offset);
209 * @brief Allows to build a single frame message with correct data to be send
211 * @param signal The CAN signal to write, including the bit position and bit size.
212 * @param value The encoded integer value to write in the CAN signal.
213 * @param message A single frame message to complete
214 * @return message_t* The message that is generated
216 message_t* encoder_t::build_one_frame_message(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message)
218 signal->set_last_value((float)value);
219 uint8_t data_tab[message->get_length()];
220 std::vector<uint8_t> data;
222 for(const auto& sig: signal->get_message()->get_signals())
224 float last_value = sig->get_last_value();
225 bitfield_encode_float(last_value,
226 sig->get_bit_position(),
231 (uint8_t)message->get_length());
234 for (size_t i = 0; i < (uint8_t) message->get_length(); i++)
236 data.push_back(data_tab[i]);
239 message->set_data(data);
244 * @brief Allows to build a multi frame message with correct data to be send
246 * @param signal The CAN signal to write, including the bit position and bit size.
247 * @param value The encoded integer value to write in the CAN signal.
248 * @param message A multi frame message to complete
249 * @return message_t* The message that is generated
251 message_t* encoder_t::build_multi_frame_message(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message)
253 signal->set_last_value((float)value);
254 std::vector<uint8_t> data;
256 uint32_t msgs_len = signal->get_message()->get_length(); // multi frame - number of bytes
257 int number_of_frame = (int) msgs_len / 8;
259 uint8_t data_tab[number_of_frame][8] = {0};
261 for(const auto& sig: signal->get_message()->get_signals())
264 int frame_position = (int) sig->get_bit_position() / 64;
265 float last_value = sig->get_last_value();
266 uint8_t bit_position = sig->get_bit_position() - ((uint8_t)(64 * frame_position));
268 bitfield_encode_float(last_value,
273 data_tab[frame_position],
277 for (size_t i = 0; i < number_of_frame; i++)
279 for(size_t j = 0; j < 8 ; j++)
281 data.push_back(data_tab[i][j]);
285 message->set_data(data);
290 * @brief Allows to build a message_t with correct data to be send
292 * @param signal The CAN signal to write, including the bit position and bit size.
293 * @param value The encoded integer value to write in the CAN signal.
294 * @return message_t* The message that is generated
296 message_t* encoder_t::build_message(const std::shared_ptr<signal_t>& signal, uint64_t value)
299 std::vector<uint8_t> data;
300 if(signal->get_message()->is_fd())
302 message = new can_message_t(CANFD_MAX_DLEN,signal->get_message()->get_id(),CANFD_MAX_DLEN,signal->get_message()->get_format(),false,0,data,0);
303 return build_one_frame_message(signal,value,message);
305 #ifdef USE_FEATURE_J1939
306 else if(signal->get_message()->is_j1939())
308 message = new j1939_message_t(J1939_MAX_DLEN,signal->get_message()->get_length(),signal->get_message()->get_format(),data,0,J1939_NO_NAME,signal->get_message()->get_id(),J1939_NO_ADDR);
309 return build_multi_frame_message(signal,value,message);
314 message = new can_message_t(CAN_MAX_DLEN,signal->get_message()->get_id(),CAN_MAX_DLEN,signal->get_message()->get_format(),false,0,data,0);
315 return build_one_frame_message(signal,value,message);
319 /// @brief Encode a boolean into an integer, fit for a CAN signal bitfield.
321 /// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
322 /// bool*) that takes care of creating the DynamicField object for you with the
325 /// @param[in] signal - The CAN signal to encode this value for..
326 /// @param[in] value - The boolean value to encode
327 /// @param[out] send - An output argument that will be set to false if the value should
328 /// not be sent for any reason.
330 /// @return Returns the encoded integer. If 'send' is changed to false, the field could
331 /// not be encoded and the return value is undefined.
333 uint64_t encoder_t::encode_boolean(const signal_t& signal, bool value, bool* send)
335 return encode_number(signal, float(value), send);
337 /// @brief Encode a float into an integer, fit for a CAN signal's bitfield.
339 /// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
340 /// bool*) that takes care of creating the DynamicField object for you with the
343 /// @param[in] signal - The CAN signal to encode this value for.
344 /// @param[in] value - The float value to encode.
345 /// @param[out] send - This output argument will always be set to false, so the caller will
346 /// know not to publish this value to the pipeline.
348 /// @return Returns the encoded integer. If 'send' is changed to false, the field could
349 /// not be encoded and the return value is undefined.
351 uint64_t encoder_t::encode_number(const signal_t& signal, float value, bool* send)
353 return float_to_fixed_point(value, signal.get_factor(), signal.get_offset());
356 /// @brief Encode a string into an integer, fit for a CAN signal's bitfield.
358 /// Be aware that the behavior is undefined if there are multiple values assigned
359 /// to a single state. See https://github.com/openxc/vi-firmware/issues/185.
361 /// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
362 /// bool*) that takes care of creating the DynamicField object for you with the
363 /// string state value.
365 /// @param[in] signal - The details of the signal that contains the state mapping.
366 /// @param[in] value - The string state value to encode.
367 /// @param[out] send - An output argument that will be set to false if the value should
368 /// not be sent for any reason.
370 /// @return Returns the encoded integer. If 'send' is changed to false, the field could
371 /// not be encoded and the return value is undefined.
373 uint64_t encoder_t::encode_state(const signal_t& signal, const std::string& state, bool* send)
378 AFB_DEBUG("Can't write state of "" -- not sending");
383 uint64_t signal_state = signal.get_states(state);
384 if(signal_state != -1) {
385 value = signal_state;
387 AFB_DEBUG("Couldn't find a valid signal state for %s", state.c_str());
394 /// @brief Parse a signal from a CAN message and apply any required
395 /// transforations to get a human readable value.
397 /// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
398 /// will be passed to the decoder before returning.
400 /// @param[in] signal - The details of the signal to decode and forward.
401 /// @param[in] value - The numerical value that will be converted to a boolean.
402 /// @param[out] send - An output parameter that will be flipped to false if the value could
405 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
406 /// string or boolean. If 'send' is false, the return value is undefined.
408 uint64_t encoder_t::encode_DynamicField( signal_t& signal, const openxc_DynamicField& field, bool* send)
412 case openxc_DynamicField_Type_STRING:
413 value = encode_state(signal, field.string_value, send);
415 case openxc_DynamicField_Type_NUM:
416 value = encode_number(signal, (float)field.numeric_value, send);
418 case openxc_DynamicField_Type_BOOL:
419 value = encode_boolean(signal, field.boolean_value, send);
422 AFB_DEBUG("Dynamic field didn't have a value, can't encode");