36be668cfcbac0a1423aeb2369dafd1eb0f0961e
[apps/agl-service-can-low-level.git] / low-can-binding / can / can-encoder.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include "can-encoder.hpp"
19
20 #include "canutil/write.h"
21 #include "../utils/openxc-utils.hpp"
22 #include "message-definition.hpp"
23 #include "../utils/converter.hpp"
24
25 /**
26  * @brief Allows to encode data for a signal
27  *
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
33  */
34 void encoder_t::encode_data(std::shared_ptr<signal_t> sig, std::vector<uint8_t> &data, bool filter, bool factor, bool offset)
35 {
36         uint32_t bit_size = sig->get_bit_size();
37         uint32_t bit_position = sig->get_bit_position();
38         int new_start_byte = 0;
39         int new_end_byte = 0;
40         uint8_t new_start_bit = 0;
41         uint8_t new_end_bit = 0;
42
43         converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
44
45         int len_signal_bytes_tmp = new_end_byte - new_start_byte + 1;
46
47         uint8_t len_signal_bytes = 0;
48         if(len_signal_bytes_tmp > 255)
49         {
50                 AFB_ERROR("Error signal %s too long", sig->get_name().c_str());
51         }
52         else
53         {
54                 len_signal_bytes = (uint8_t) len_signal_bytes_tmp;
55         }
56 /*
57         if(new_start_bit > 255)
58         {
59                 AFB_ERROR("Error signal %s too long", sig->get_name().c_str());
60         }
61 */
62         uint8_t new_bit_size = 0;
63         if(bit_size > 255)
64         {
65                 AFB_ERROR("Error signal %s to long bit size", sig->get_name().c_str());
66         }
67         else
68         {
69                 new_bit_size = (uint8_t) bit_size;
70         }
71
72         uint8_t data_signal[len_signal_bytes] = {0};
73         float factor_v = 1;
74         if(factor)
75         {
76                 factor_v = sig->get_factor();
77         }
78
79         float offset_v = 0;
80         if(factor)
81         {
82                 offset_v = sig->get_offset();
83         }
84
85         if(filter)
86         {
87                 uint8_t tmp = 0;
88                 int j=0;
89                 for(int i=0;i<new_bit_size;i++)
90                 {
91                         int mask = 1 << ((i%8)+new_start_bit);
92
93                         uint8_t mask_v = 0;
94                         if(mask > 255)
95                         {
96                                 AFB_ERROR("Error mask too large");
97                         }
98                         else
99                         {
100                                 mask_v = (uint8_t) mask;
101                         }
102                         tmp = tmp|mask_v;
103
104                         if(i%8 == 7)
105                         {
106                                 data_signal[j] = tmp;
107                                 tmp = 0;
108                                 j++;
109                         }
110                 }
111                 data_signal[j]=tmp;
112         }
113         else
114         {
115                 bitfield_encode_float(  sig->get_last_value(),
116                                                 new_start_bit,
117                                                 new_bit_size,
118                                                 factor_v,
119                                                 offset_v,
120                                                 data_signal,
121                                                 len_signal_bytes);
122         }
123
124         for(size_t i = new_start_byte; i <= new_end_byte ; i++)
125         {
126                 data[i] = data[i] | data_signal[i-new_start_byte];
127         }
128 }
129
130 /**
131  * @brief Allows to build a multi frame message with correct data to be send
132  *
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
139  */
140 message_t* encoder_t::build_frame(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message, bool factor, bool offset)
141 {
142         signal->set_last_value((float)value);
143         std::vector<uint8_t> data;
144         for(int i = 0; i<message->get_length();i++)
145         {
146                 data.push_back(0);
147         }
148
149         for(const auto& sig: signal->get_message()->get_signals())
150         {
151                 encode_data(sig, data, false, factor, offset);
152         }
153         message->set_data(data);
154         return message;
155 }
156
157 /**
158  * @brief Allows to build a message_t with correct data to be send
159  *
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
165  */
166 message_t* encoder_t::build_message(const std::shared_ptr<signal_t>& signal, uint64_t value, bool factor, bool offset)
167 {
168         message_t *message;
169         std::vector<uint8_t> data;
170         switch(signal->get_message()->get_flags())
171         {
172                 case CAN_PROTOCOL_WITH_FD_FRAME:
173                         message = new can_message_t(CANFD_MAX_DLEN,
174                                                     signal->get_message()->get_id(),
175                                                     CANFD_MAX_DLEN,
176                                                     false,
177                                                     signal->get_message()->get_flags(),
178                                                     data,
179                                                     0);
180                         return build_frame(signal, value, message, factor, offset);
181 #ifdef USE_FEATURE_J1939
182                 case J1939_PROTOCOL:
183                         message = new j1939_message_t(signal->get_message()->get_length(),
184                                                       data,
185                                                       0,
186                                                       J1939_NO_NAME,
187                                                       signal->get_message()->get_id(),
188                                                       J1939_NO_ADDR);
189                         return build_frame(signal, value, message, factor, offset);
190 #endif
191                 case CAN_PROTOCOL:
192                         message = new can_message_t(CAN_MAX_DLEN,
193                                                     signal->get_message()->get_id(),
194                                                     CAN_MAX_DLEN,
195                                                     false,
196                                                     signal->get_message()->get_flags(),
197                                                     data,
198                                                     0);
199                         return build_frame(signal, value, message, factor, offset);
200                 default:
201                         message = new can_message_t(CAN_MAX_DLEN,
202                                                     signal->get_message()->get_id(),
203                                                     CAN_MAX_DLEN,
204                                                     false,
205                                                     signal->get_message()->get_flags(),
206                                                     data,
207                                                     0);
208                         return build_frame(signal, value, message, factor, offset);
209         }
210
211 }
212
213
214 /**
215  * @brief Allows to build a single frame message with correct data to be send
216  *
217  * @param signal The CAN signal to write, including the bit position and bit size.
218  * @param value The encoded integer value to write in the CAN signal.
219  * @param message A single frame message to complete
220  * @return message_t*  The message that is generated
221  */
222 message_t* encoder_t::build_one_frame_message(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message)
223 {
224         signal->set_last_value((float)value);
225         uint8_t data_tab[message->get_length()];
226         std::vector<uint8_t> data;
227
228         for(const auto& sig: signal->get_message()->get_signals())
229         {
230                 float last_value = sig->get_last_value();
231                 bitfield_encode_float(last_value,
232                                         sig->get_bit_position(),
233                                         sig->get_bit_size(),
234                                         sig->get_factor(),
235                                         sig->get_offset(),
236                                         data_tab,
237                                         (uint8_t)message->get_length());
238         }
239
240         for (size_t i = 0; i < (uint8_t) message->get_length(); i++)
241         {
242                 data.push_back(data_tab[i]);
243         }
244
245         message->set_data(data);
246         return message;
247 }
248
249 /**
250  * @brief Allows to build a multi frame message with correct data to be send
251  *
252  * @param signal The CAN signal to write, including the bit position and bit size.
253  * @param value The encoded integer value to write in the CAN signal.
254  * @param message A multi frame message to complete
255  * @return message_t*  The message that is generated
256  */
257 message_t* encoder_t::build_multi_frame_message(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message)
258 {
259         signal->set_last_value((float)value);
260         std::vector<uint8_t> data;
261
262         uint32_t msgs_len = signal->get_message()->get_length(); // multi frame - number of bytes
263         int number_of_frame = (int) msgs_len / 8;
264
265         uint8_t data_tab[number_of_frame][8] = {0};
266
267         for(const auto& sig: signal->get_message()->get_signals())
268         {
269
270                 int frame_position = (int) sig->get_bit_position() / 64;
271                 float last_value = sig->get_last_value();
272                 uint8_t bit_position = sig->get_bit_position() - ((uint8_t)(64 * frame_position));
273
274                 bitfield_encode_float(last_value,
275                                         bit_position,
276                                         sig->get_bit_size(),
277                                         sig->get_factor(),
278                                         sig->get_offset(),
279                                         data_tab[frame_position],
280                                         8);
281         }
282
283         for (size_t i = 0; i < number_of_frame; i++)
284         {
285                 for(size_t j = 0; j < 8 ; j++)
286                 {
287                         data.push_back(data_tab[i][j]);
288                 }
289         }
290
291         message->set_data(data);
292         return message;
293 }
294
295 /**
296  * @brief Allows to build a message_t with correct data to be send
297  *
298  * @param signal The CAN signal to write, including the bit position and bit size.
299  * @param value The encoded integer value to write in the CAN signal.
300  * @return message_t* The message that is generated
301  */
302 message_t* encoder_t::build_message(const std::shared_ptr<signal_t>& signal, uint64_t value)
303 {
304         message_t *message;
305         std::vector<uint8_t> data;
306
307         switch(signal->get_message()->get_flags())
308         {
309                 case CAN_PROTOCOL_WITH_FD_FRAME:
310                         message = new can_message_t(CANFD_MAX_DLEN,
311                                                 signal->get_message()->get_id(),
312                                                 CANFD_MAX_DLEN,
313                                                 false,
314                                                 signal->get_message()->get_flags(),
315                                                 data,
316                                                 0);
317                         return build_one_frame_message(signal, value, message);
318 #ifdef USE_FEATURE_J1939
319                 case J1939_PROTOCOL:
320                         message = new j1939_message_t(signal->get_message()->get_length(),
321                                                 data,
322                                                 0,
323                                                 J1939_NO_NAME,
324                                                 signal->get_message()->get_id(),
325                                                 J1939_NO_ADDR);
326                         return build_multi_frame_message(signal, value, message);
327 #endif
328         case CAN_PROTOCOL:
329                 message = new can_message_t(CAN_MAX_DLEN,
330                                             signal->get_message()->get_id(),
331                                             CAN_MAX_DLEN,
332                                             false,
333                                             signal->get_message()->get_flags(),
334                                             data,
335                                             0);
336                 return build_one_frame_message(signal, value, message);
337         default:
338                 message = new can_message_t(CAN_MAX_DLEN,
339                                             signal->get_message()->get_id(),
340                                             CAN_MAX_DLEN,
341                                             false,
342                                             signal->get_message()->get_flags(),
343                                             data,
344                                             0);
345                 return build_one_frame_message(signal, value, message);
346         }
347 }
348
349 /// @brief Encode a boolean into an integer, fit for a CAN signal bitfield.
350 ///
351 /// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
352 /// bool*) that takes care of creating the DynamicField object for you with the
353 /// boolean value.
354 ///
355 /// @param[in] signal  - The CAN signal to encode this value for..
356 /// @param[in] value - The boolean value to encode
357 /// @param[out] send - An output argument that will be set to false if the value should
358 ///     not be sent for any reason.
359 ///
360 /// @return Returns the encoded integer. If 'send' is changed to false, the field could
361 /// not be encoded and the return value is undefined.
362 ///
363 uint64_t encoder_t::encode_boolean(const signal_t& signal, bool value, bool* send)
364 {
365         return encode_number(signal, float(value), send);
366 }
367 /// @brief Encode a float into an integer, fit for a CAN signal's bitfield.
368 ///
369 /// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
370 /// bool*) that takes care of creating the DynamicField object for you with the
371 /// float value.
372 ///
373 /// @param[in] signal  - The CAN signal to encode this value for.
374 /// @param[in] value - The float value to encode.
375 /// @param[out] send - This output argument will always be set to false, so the caller will
376 ///      know not to publish this value to the pipeline.
377 ///
378 /// @return Returns the encoded integer. If 'send' is changed to false, the field could
379 /// not be encoded and the return value is undefined.
380 ///
381 uint64_t encoder_t::encode_number(const signal_t& signal, float value, bool* send)
382 {
383         return float_to_fixed_point(value, signal.get_factor(), signal.get_offset());
384 }
385
386 /// @brief Encode a string into an integer, fit for a CAN signal's bitfield.
387 ///
388 /// Be aware that the behavior is undefined if there are multiple values assigned
389 /// to a single state. See https://github.com/openxc/vi-firmware/issues/185.
390 ///
391 /// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
392 /// bool*) that takes care of creating the DynamicField object for you with the
393 /// string state value.
394 ///
395 /// @param[in] signal  - The details of the signal that contains the state mapping.
396 /// @param[in] value - The string state value to encode.
397 /// @param[out] send - An output argument that will be set to false if the value should
398 ///     not be sent for any reason.
399 ///
400 /// @return Returns the encoded integer. If 'send' is changed to false, the field could
401 /// not be encoded and the return value is undefined.
402 ///
403 uint64_t encoder_t::encode_state(const signal_t& signal, const std::string& state, bool* send)
404 {
405         uint64_t value = 0;
406         if(state == "")
407         {
408                 AFB_DEBUG("Can't write state of "" -- not sending");
409                 *send = false;
410         }
411         else
412         {
413                 uint64_t signal_state = signal.get_states(state);
414                 if(signal_state != -1) {
415                         value = signal_state;
416                 } else {
417                         AFB_DEBUG("Couldn't find a valid signal state for %s", state.c_str());
418                         *send = false;
419                 }
420         }
421         return value;
422 }
423
424 /// @brief Parse a signal from a CAN message and apply any required
425 /// transforations to get a human readable value.
426 ///
427 /// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
428 /// will be passed to the decoder before returning.
429 ///
430 /// @param[in] signal - The details of the signal to decode and forward.
431 /// @param[in] value - The numerical value that will be converted to a boolean.
432 /// @param[out] send - An output parameter that will be flipped to false if the value could
433 ///      not be decoded.
434 ///
435 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
436 /// string or boolean. If 'send' is false, the return value is undefined.
437 ///
438 uint64_t encoder_t::encode_DynamicField( signal_t& signal, const openxc_DynamicField& field, bool* send)
439 {
440         uint64_t value = 0;
441         switch(field.type) {
442                 case openxc_DynamicField_Type_STRING:
443                         value = encode_state(signal, field.string_value, send);
444                         break;
445                 case openxc_DynamicField_Type_NUM:
446                         value = encode_number(signal, (float)field.numeric_value, send);
447                         break;
448                 case openxc_DynamicField_Type_BOOL:
449                         value = encode_boolean(signal, field.boolean_value, send);
450                         break;
451                 default:
452                         AFB_DEBUG("Dynamic field didn't have a value, can't encode");
453                         *send = false;
454                         break;
455         }
456         return value;
457 }