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