encoder: use contructor to initialize the vector
[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 encode
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         float factor_v = factor ? sig->get_factor() : 1;
39         float offset_v = offset ? sig->get_offset() : 0;
40
41         int new_start_byte = 0;
42         int new_end_byte = 0;
43         uint8_t new_start_bit = 0;
44         uint8_t new_end_bit = 0;
45
46         converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
47         std::vector<uint8_t> data_signal(new_end_byte - new_start_byte + 1, 0xFF);
48
49         if(filter)
50         {
51                 uint8_t mask_first_v = static_cast<uint8_t>(0xFF >> new_start_bit);
52                 uint8_t mask_last_v = static_cast<uint8_t>(0xFF << (7 - new_end_bit));
53
54                 if(new_start_byte == new_end_byte)
55                 {
56                         data_signal[0] = mask_first_v & mask_last_v;
57                 }
58                 else
59                 {
60                         data_signal[0] = mask_first_v;
61                         data_signal[new_end_byte - new_start_byte] = mask_last_v;
62                 }
63         }
64         else
65         {
66                 bitfield_encode_float(sig->get_last_value(),
67                                       new_start_bit,
68                                       bit_size,
69                                       factor_v,
70                                       offset_v,
71                                       data_signal.data(),
72                                       bit_size);
73         }
74
75         for(size_t i = new_start_byte; i <= new_end_byte ; i++)
76                 data[i] = data[i] | data_signal[i-new_start_byte];
77 }
78
79 /**
80  * @brief Allows to build a multi frame message with correct data to be send
81  *
82  * @param signal The CAN signal to write, including the bit position and bit size.
83  * @param value The encoded integer value to write in the CAN signal.
84  * @param message A multi frame message to complete
85  * @param factor If true that will use the factor of the signal else 1
86  * @param offset If true that will use the offset of the signal else 0
87  * @return message_t*  The message that is generated
88  */
89 message_t* encoder_t::build_frame(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message, bool factor, bool offset)
90 {
91         signal->set_last_value(static_cast<float>(value));
92         std::vector<uint8_t> data(message->get_length(), 0);
93
94         for(const auto& sig: signal->get_message()->get_signals())
95                 encode_data(sig, data, false, factor, offset);
96
97         message->set_data(data);
98         return message;
99 }
100
101 /**
102  * @brief Allows to build a message_t with correct data to be send
103  *
104  * @param signal The CAN signal to write, including the bit position and bit size.
105  * @param value The encoded integer value to write in the CAN signal.
106  * @param factor If true that will use the factor of the signal else 1
107  * @param offset If true that will use the offset of the signal else 0
108  * @return message_t* The message that is generated
109  */
110 message_t* encoder_t::build_message(const std::shared_ptr<signal_t>& signal, uint64_t value, bool factor, bool offset)
111 {
112         message_t *message;
113         std::vector<uint8_t> data;
114         switch(signal->get_message()->get_flags())
115         {
116                 case CAN_PROTOCOL_WITH_FD_FRAME:
117                         message = new can_message_t(CANFD_MAX_DLEN,
118                                                     signal->get_message()->get_id(),
119                                                     CANFD_MAX_DLEN,
120                                                     false,
121                                                     signal->get_message()->get_flags(),
122                                                     data,
123                                                     0);
124                         return build_frame(signal, value, message, factor, offset);
125 #ifdef USE_FEATURE_J1939
126                 case J1939_PROTOCOL:
127                         message = new j1939_message_t(signal->get_message()->get_length(),
128                                                       data,
129                                                       0,
130                                                       J1939_NO_NAME,
131                                                       signal->get_message()->get_id(),
132                                                       J1939_NO_ADDR);
133                         return build_frame(signal, value, message, factor, offset);
134 #endif
135                 case CAN_PROTOCOL:
136                         message = new can_message_t(CAN_MAX_DLEN,
137                                                     signal->get_message()->get_id(),
138                                                     CAN_MAX_DLEN,
139                                                     false,
140                                                     signal->get_message()->get_flags(),
141                                                     data,
142                                                     0);
143                         return build_frame(signal, value, message, factor, offset);
144                 default:
145                         message = new can_message_t(CAN_MAX_DLEN,
146                                                     signal->get_message()->get_id(),
147                                                     CAN_MAX_DLEN,
148                                                     false,
149                                                     signal->get_message()->get_flags(),
150                                                     data,
151                                                     0);
152                         return build_frame(signal, value, message, factor, offset);
153         }
154
155 }
156
157 /// @brief Encode a boolean into an integer, fit for a CAN signal bitfield.
158 ///
159 /// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
160 /// bool*) that takes care of creating the DynamicField object for you with the
161 /// boolean value.
162 ///
163 /// @param[in] signal  - The CAN signal to encode this value for..
164 /// @param[in] value - The boolean value to encode
165 /// @param[out] send - An output argument that will be set to false if the value should
166 ///     not be sent for any reason.
167 ///
168 /// @return Returns the encoded integer. If 'send' is changed to false, the field could
169 /// not be encoded and the return value is undefined.
170 ///
171 uint64_t encoder_t::encode_boolean(const signal_t& signal, bool value, bool* send)
172 {
173         return encode_number(signal, float(value), send);
174 }
175 /// @brief Encode a float into an integer, fit for a CAN signal's bitfield.
176 ///
177 /// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
178 /// bool*) that takes care of creating the DynamicField object for you with the
179 /// float value.
180 ///
181 /// @param[in] signal  - The CAN signal to encode this value for.
182 /// @param[in] value - The float value to encode.
183 /// @param[out] send - This output argument will always be set to false, so the caller will
184 ///      know not to publish this value to the pipeline.
185 ///
186 /// @return Returns the encoded integer. If 'send' is changed to false, the field could
187 /// not be encoded and the return value is undefined.
188 ///
189 uint64_t encoder_t::encode_number(const signal_t& signal, float value, bool* send)
190 {
191         return float_to_fixed_point(value, signal.get_factor(), signal.get_offset());
192 }
193
194 /// @brief Encode a string into an integer, fit for a CAN signal's bitfield.
195 ///
196 /// Be aware that the behavior is undefined if there are multiple values assigned
197 /// to a single state. See https://github.com/openxc/vi-firmware/issues/185.
198 ///
199 /// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
200 /// bool*) that takes care of creating the DynamicField object for you with the
201 /// string state value.
202 ///
203 /// @param[in] signal  - The details of the signal that contains the state mapping.
204 /// @param[in] value - The string state value to encode.
205 /// @param[out] send - An output argument that will be set to false if the value should
206 ///     not be sent for any reason.
207 ///
208 /// @return Returns the encoded integer. If 'send' is changed to false, the field could
209 /// not be encoded and the return value is undefined.
210 ///
211 uint64_t encoder_t::encode_state(const signal_t& signal, const std::string& state, bool* send)
212 {
213         uint64_t value = 0;
214         if(state == "")
215         {
216                 AFB_DEBUG("Can't write state of "" -- not sending");
217                 *send = false;
218         }
219         else
220         {
221                 uint64_t signal_state = signal.get_states(state);
222                 if(signal_state != -1) {
223                         value = signal_state;
224                 } else {
225                         AFB_DEBUG("Couldn't find a valid signal state for %s", state.c_str());
226                         *send = false;
227                 }
228         }
229         return value;
230 }
231
232 /// @brief Parse a signal from a CAN message and apply any required
233 /// transforations to get a human readable value.
234 ///
235 /// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
236 /// will be passed to the decoder before returning.
237 ///
238 /// @param[in] signal - The details of the signal to decode and forward.
239 /// @param[in] value - The numerical value that will be converted to a boolean.
240 /// @param[out] send - An output parameter that will be flipped to false if the value could
241 ///      not be decoded.
242 ///
243 /// @return The decoder returns an openxc_DynamicField, which may contain a number,
244 /// string or boolean. If 'send' is false, the return value is undefined.
245 ///
246 uint64_t encoder_t::encode_DynamicField( signal_t& signal, const openxc_DynamicField& field, bool* send)
247 {
248         uint64_t value = 0;
249         switch(field.type) {
250                 case openxc_DynamicField_Type_STRING:
251                         value = encode_state(signal, field.string_value, send);
252                         break;
253                 case openxc_DynamicField_Type_NUM:
254                         value = encode_number(signal, (float)field.numeric_value, send);
255                         break;
256                 case openxc_DynamicField_Type_BOOL:
257                         value = encode_boolean(signal, field.boolean_value, send);
258                         break;
259                 default:
260                         AFB_DEBUG("Dynamic field didn't have a value, can't encode");
261                         *send = false;
262                         break;
263         }
264         return value;
265 }