DAB RC3 Fixes follows appfw API break.
[apps/low-level-can-service.git] / low-can-binding / can / can-message.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-message.hpp"
19
20 #include <cstring>
21
22 #include "../binding/low-can-hat.hpp"
23
24 ///
25 /// @brief Class constructor
26 ///
27 /// Constructor about can_message_t class.
28 ///
29 can_message_t::can_message_t()
30         : maxdlen_{0},
31          id_{0},
32          length_{0},
33          format_{can_message_format_t::INVALID},
34          rtr_flag_{false},
35          flags_{0},
36          timestamp_{0},
37          sub_id_{-1}
38 {}
39
40 can_message_t::can_message_t(uint8_t maxdlen,
41         uint32_t id,
42         uint8_t length,
43         can_message_format_t format,
44         bool rtr_flag,
45         uint8_t flags,
46         std::vector<uint8_t>& data,
47         uint64_t timestamp)
48         :  maxdlen_{maxdlen},
49         id_{id},
50         length_{length},
51         format_{format},
52         rtr_flag_{rtr_flag},
53         flags_{flags},
54         data_{data},
55         timestamp_{timestamp},
56         sub_id_{-1}
57 {}
58
59 ///
60 /// @brief Retrieve id_ member value.
61 ///
62 /// @return id_ class member
63 ///
64 uint32_t can_message_t::get_id() const
65 {
66         return id_;
67 }
68
69 int can_message_t::get_sub_id() const
70 {
71         return sub_id_;
72 }
73
74 ///
75 /// @brief Retrieve RTR flag member.
76 ///
77 /// @return rtr_flags_ class member
78 ///
79 bool can_message_t::get_rtr_flag_() const
80 {
81         return rtr_flag_;
82 }
83
84 ///
85 /// @brief Retrieve format_ member value.
86 ///
87 /// @return format_ class member. Default to INVALID.
88 ///
89 can_message_format_t can_message_t::get_format() const
90 {
91         if (format_ != can_message_format_t::STANDARD || format_ != can_message_format_t::EXTENDED)
92                 return can_message_format_t::INVALID;
93         return format_;
94 }
95
96 ///
97 /// @brief Retrieve flags_ member value.
98 ///
99 /// @return flags_ class member
100 ///
101 uint8_t can_message_t::get_flags() const
102 {
103         return flags_;
104 }
105
106 ///
107 /// @brief Retrieve data_ member value.
108 ///
109 /// @return pointer to the first element
110 ///  of class member data_
111 ///
112 const uint8_t* can_message_t::get_data() const
113 {
114         return data_.data();
115 }
116
117 ///
118 /// @brief Retrieve data_ member whole vector
119 ///
120 /// @return the vector as is
121 ///
122 const std::vector<uint8_t> can_message_t::get_data_vector() const
123 {
124         return data_;
125 }
126
127 ///
128 /// @brief Retrieve length_ member value.
129 ///
130 /// @return length_ class member
131 ///
132 uint8_t can_message_t::get_length() const
133 {
134         return length_;
135 }
136
137 void can_message_t::set_sub_id(int sub_id)
138 {
139         sub_id_ = sub_id;
140 }
141
142 uint64_t can_message_t::get_timestamp() const
143 {
144         return timestamp_;
145 }
146
147 void can_message_t::set_timestamp(uint64_t timestamp)
148 {
149         timestamp_ = timestamp;
150 }
151
152 /// @brief Control whether the object is correctly initialized
153 ///  to be sent over the CAN bus
154 ///
155 /// @return True if object correctly initialized and false if not.
156 bool can_message_t::is_correct_to_send()
157 {
158         if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::INVALID)
159         {
160                 int i;
161                 for(i=0;i<CAN_MESSAGE_SIZE;i++)
162                         if(data_[i] != 0)
163                                 return true;
164         }
165         return false;
166 }
167
168 /// @brief Set format_ member value.
169 ///
170 /// Preferred way to initialize these members by using
171 /// convert_from_canfd_frame method.
172 ///
173 /// @param[in] new_format - class member
174 void can_message_t::set_format(const can_message_format_t new_format)
175 {
176         if(new_format == can_message_format_t::STANDARD || new_format == can_message_format_t::EXTENDED || new_format == can_message_format_t::INVALID)
177                 format_ = new_format;
178         else
179                 AFB_ERROR("Can set format, wrong format chosen");
180 }
181
182 /// @brief Take a canfd_frame struct to initialize class members
183 ///
184 /// This is the preferred way to initialize class members.
185 ///
186 /// @param[in] frame - canfd_frame to convert coming from a read of CAN socket
187 /// @param[in] nbytes - bytes read from socket read operation.
188 ///
189 /// @return A can_message_t object fully initialized with canfd_frame values.
190 can_message_t can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp)
191 {
192         uint8_t maxdlen, length, flags = (uint8_t)NULL;
193         uint32_t id;
194         can_message_format_t format;
195         bool rtr_flag;
196         std::vector<uint8_t> data;
197
198         switch(nbytes)
199         {
200                 case CANFD_MTU:
201                         AFB_DEBUG("Got an CAN FD frame");
202                         maxdlen = CANFD_MAX_DLEN;
203                         break;
204                 case CAN_MTU:
205                         AFB_DEBUG("Got a legacy CAN frame");
206                         maxdlen = CAN_MAX_DLEN;
207                         break;
208                 default:
209                         AFB_ERROR("unsupported CAN frame");
210                         break;
211         }
212
213         if (frame.can_id & CAN_ERR_FLAG)
214         {
215                 format = can_message_format_t::INVALID;
216                 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
217         }
218         else if (frame.can_id & CAN_EFF_FLAG)
219         {
220                 format = can_message_format_t::EXTENDED;
221                 id = frame.can_id & CAN_EFF_MASK;
222         }
223         else
224         {
225                 format = can_message_format_t::STANDARD;
226                 id = frame.can_id & CAN_SFF_MASK;
227         }
228
229         /* Overwrite length_ if RTR flags is detected.
230          * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
231         if (frame.can_id & CAN_RTR_FLAG)
232         {
233                 rtr_flag = true;
234                 if(frame.len && frame.len <= CAN_MAX_DLC)
235                 {
236                         if(rtr_flag)
237                                 length = frame.len& 0xF;
238                         else
239                         {
240                                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
241                         }
242                 }
243         }
244         else
245         {
246                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
247
248                 /* Flags field only present for CAN FD frames*/
249                 if(maxdlen == CANFD_MAX_DLEN)
250                                 flags = frame.flags & 0xF;
251
252                 if (data.capacity() < maxdlen)
253                         data.reserve(maxdlen);
254                                 int i;
255
256                         data.clear();
257                         /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
258                         for(i=0;i<maxdlen;i++)
259                         {
260                                 data.push_back(frame.data[i]);
261                         };
262
263                 AFB_DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
264                                                                 id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
265         }
266
267         return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp);
268 }
269
270 /// @brief Take a can_frame struct to initialize class members
271 ///
272 /// This is the preferred way to initialize class members.
273 ///
274 /// @param[in] frame - can_frame to convert coming from a read of CAN socket
275 /// @param[in] nbytes - bytes read from socket read operation.
276 ///
277 /// @return A can_message_t object fully initialized with can_frame values.
278 can_message_t can_message_t::convert_from_frame(const struct can_frame& frame, size_t nbytes, uint64_t timestamp)
279 {
280         uint8_t maxdlen, length, flags = (uint8_t)NULL;
281         uint32_t id;
282         can_message_format_t format;
283         bool rtr_flag;
284         std::vector<uint8_t> data;
285
286         if(nbytes <= CAN_MTU)
287         {
288                         AFB_DEBUG("Got a legacy CAN frame");
289                         maxdlen = CAN_MAX_DLEN;
290         }
291         else
292         {
293                         AFB_ERROR("unsupported CAN frame");
294         }
295
296         if (frame.can_id & CAN_ERR_FLAG)
297         {
298                 format = can_message_format_t::INVALID;
299                 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
300         }
301         else if (frame.can_id & CAN_EFF_FLAG)
302         {
303                 format = can_message_format_t::EXTENDED;
304                 id = frame.can_id & CAN_EFF_MASK;
305         }
306         else
307         {
308                 format = can_message_format_t::STANDARD;
309                 id = frame.can_id & CAN_SFF_MASK;
310         }
311
312         /* Overwrite length_ if RTR flags is detected.
313          * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
314         if (frame.can_id & CAN_RTR_FLAG)
315         {
316                 rtr_flag = true;
317                 if(frame.can_dlc && frame.can_dlc <= CAN_MAX_DLC)
318                 {
319                         if(rtr_flag)
320                                 length = frame.can_dlc& 0xF;
321                         else
322                         {
323                                 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
324                         }
325                 }
326         }
327         else
328         {
329                 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
330
331                 if (data.capacity() < maxdlen)
332                         data.reserve(maxdlen);
333                                 int i;
334
335                         data.clear();
336                         /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
337                         for(i=0;i<maxdlen;i++)
338                         {
339                                 data.push_back(frame.data[i]);
340                         };
341
342 //              AFB_DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
343 //                                                              id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
344         }
345
346         return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp);
347 }
348
349 /// @brief Take all initialized class's members and build an
350 /// canfd_frame struct that can be use to send a CAN message over
351 /// the bus.
352 ///
353 /// @return canfd_frame struct built from class members.
354 struct canfd_frame can_message_t::convert_to_canfd_frame()
355 {
356         canfd_frame frame;
357
358         if(is_correct_to_send())
359         {
360                 frame.can_id = get_id();
361                 frame.len = get_length();
362                 ::memcpy(frame.data, get_data(), length_);
363         }
364         else
365                 AFB_ERROR("can_message_t not correctly initialized to be sent");
366
367         return frame;
368 }
369
370 /// @brief Take all initialized class's members and build an
371 /// can_frame struct that can be use to send a CAN message over
372 /// the bus.
373 ///
374 /// @return can_frame struct built from class members.
375 struct can_frame can_message_t::convert_to_can_frame()
376 {
377         can_frame frame;
378
379         if(is_correct_to_send())
380         {
381                 frame.can_id = get_id();
382                 frame.can_dlc = get_length();
383                 ::memcpy(frame.data, get_data(), length_);
384         }
385         else
386                 AFB_ERROR("can_message_t not correctly initialized to be sent");
387
388         return frame;
389 }