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