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