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