Fix : wrong attribute type
[apps/agl-service-can-low-level.git] / src / 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/can-message.hpp"
19
20 #include <cstring>
21
22 #include "low-can-binding.hpp"
23
24 /********************************************************************************
25 *
26 *               CanMessage method implementation
27 *
28 *********************************************************************************/
29 /**
30 * @brief Class constructor
31 *
32 * Constructor about can_message_t class.
33 */
34 can_message_t::can_message_t()
35         : id_{0}, rtr_flag_{false}, length_{0}, flags_{0}, format_{CanMessageFormat::ERROR}
36 {}
37
38 /**
39 * @brief Retrieve id_ member value.
40 *
41 * @return uint32_t id_ class member
42 */
43 uint32_t can_message_t::get_id() const
44 {
45         return id_;
46 }
47
48 /**
49 * @brief Retrieve RTR flag member.
50 *
51 * @return bool rtr_flags_ class member
52 */
53 bool can_message_t::get_rtr_flag_() const
54 {
55         return rtr_flag_;
56 }
57
58 /**
59 * @brief Retrieve format_ member value.
60 *
61 * @return CanMessageFormat format_ class member
62 */
63 int can_message_t::get_format() const
64 {
65         if (format_ != CanMessageFormat::STANDARD || format_ != CanMessageFormat::EXTENDED)
66                 return CanMessageFormat::ERROR;
67         return format_;
68 }
69
70 /**
71 * @brief Retrieve format_ member value.
72 *
73 * @return CanMessageFormat format_ class member
74 */
75 uint8_t can_message_t::get_flags() const
76 {
77         return flags_;
78 }
79
80 /**
81 * @brief Retrieve data_ member value.
82 *
83 * @return uint8_t data_ pointer to the first element 
84 *  of class member data_
85 */
86 const uint8_t* can_message_t::get_data() const
87 {
88         return data_.data();
89 }
90
91 /**
92 * @brief Retrieve length_ member value.
93 *
94 * @return uint8_t length_ class member
95 */
96 uint8_t can_message_t::get_length() const
97 {
98         return length_;
99 }
100
101 void can_message_t::set_max_data_length(size_t nbytes)
102 {
103         maxdlen_ = 0;
104
105         switch(nbytes)
106         {
107                 case CANFD_MTU:
108                         DEBUG(binder_interface, "set_max_data_length: Got an CAN FD frame");
109                         maxdlen_ = CANFD_MAX_DLEN;
110                         break;
111                 case CAN_MTU:
112                         DEBUG(binder_interface, "set_max_data_length: Got a legacy CAN frame");
113                         maxdlen_ = CAN_MAX_DLEN;
114                         break;
115                 default:
116                         ERROR(binder_interface, "set_max_data_length: unsupported CAN frame");
117                         break;
118         }
119 }
120
121 /**
122 * @brief Control whether the object is correctly initialized
123 *  to be sent over the CAN bus
124 *
125 * @return true if object correctly initialized and false if not...
126 */
127 bool can_message_t::is_correct_to_send()
128 {
129         if (id_ != 0 && length_ != 0 && format_ != CanMessageFormat::ERROR)
130         {
131                 int i;
132                 for(i=0;i<CAN_MESSAGE_SIZE;i++)
133                         if(data_[i] != 0)
134                                 return true;
135         }
136         return false;
137 }
138
139 /**
140 * @brief Set id_ member value.
141 *
142 * Preferred way to initialize these members by using 
143 * convert_from_canfd_frame method.
144 *
145 * @param uint32_t id_ class member
146 */
147 void can_message_t::set_id_and_format(const uint32_t new_id)
148 {
149         set_format(new_id);
150         switch(format_)
151         {
152                 case CanMessageFormat::STANDARD:
153                         id_ = new_id & CAN_SFF_MASK;
154                         break;
155                 case CanMessageFormat::EXTENDED:
156                         id_ = new_id & CAN_EFF_MASK;
157                         break;
158                 case CanMessageFormat::ERROR:
159                         id_ = new_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
160                         break;
161                 default:
162                         ERROR(binder_interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
163                         break;
164         }
165 }
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 CanMessageFormat format_ class member
174 */
175 void can_message_t::set_format(const CanMessageFormat new_format)
176 {
177         if(new_format == CanMessageFormat::STANDARD || new_format == CanMessageFormat::EXTENDED || new_format == CanMessageFormat::ERROR)
178                 format_ = new_format;
179         else
180                 ERROR(binder_interface, "ERROR: Can set format, wrong format chosen");
181 }
182
183 /**
184 * @brief Set format_ member value. Deducing from the can_id
185 *  of a canfd_frame.
186 *
187 * Preferred way to initialize these members by using 
188 * convert_from_canfd_frame method.
189 *
190 * @param uint32_t can_id integer from a canfd_frame
191 */
192 void can_message_t::set_format(const uint32_t can_id)
193 {
194         if (can_id & CAN_ERR_FLAG)
195                 format_ = CanMessageFormat::ERROR;
196         else if (can_id & CAN_EFF_FLAG)
197                 format_ = CanMessageFormat::EXTENDED;
198         else
199                 format_ = CanMessageFormat::STANDARD;
200 }
201
202 /**
203 * @brief Set format_ member value.
204 *
205 * Preferred way to initialize these members by using 
206 * convert_from_canfd_frame method.
207 *
208 * @param CanMessageFormat format_ class member
209 */
210 void can_message_t::set_flags(const uint8_t flags)
211 {
212         flags_ = flags & 0xF;
213 }
214
215 /**
216 * @brief Set length_ member value.
217 *
218 * Preferred way to initialize these members by using 
219 * convert_from_canfd_frame method.
220 *
221 * @param uint8_t length_ array with a max size of 8 elements.
222 */
223 void can_message_t::set_length(const uint8_t new_length)
224 {
225         if(rtr_flag_)
226                 length_ = new_length & 0xF;
227         else
228         {
229                 length_ = (new_length > maxdlen_) ? maxdlen_ : new_length;
230         }
231 }
232
233 /**
234 * @brief Set data_ member value.
235 *
236 * Preferred way to initialize these members by using 
237 * convert_from_canfd_frame method.
238 *
239 * @param uint8_t data_ array with a max size of 8 elements.
240 */
241 void can_message_t::set_data(const __u8* new_data)
242 {
243                 int i;
244
245                 data_.clear();
246                 /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
247                 for(i=0;i<maxdlen_;i++)
248                 {
249                         data_.push_back(new_data[i]);
250                 }
251 }
252
253 /**
254 * @brief Take a canfd_frame struct to initialize class members
255 *
256 * This is the preferred way to initialize class members.
257 *
258 * @param canfd_frame struct read from can bus device.
259 */
260 void can_message_t::convert_from_canfd_frame(const std::pair<struct canfd_frame&, size_t>args)
261 {
262         // May be it's overkill to assign member of the pair... May be it will change...
263         struct canfd_frame frame = args.first;
264         size_t nbytes = args.second;
265         set_max_data_length(nbytes);
266         set_length(frame.len);
267         set_id_and_format(frame.can_id);
268
269         /* Overwrite lenght_ 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.len && frame.len <= CAN_MAX_DLC)
275                         set_length(frame.len);
276                 return;
277         }
278
279         /* Flags field only present for CAN FD frames*/
280         if(maxdlen_ == CANFD_MAX_DLEN)
281                 set_flags(frame.flags);
282
283         if ( data_.capacity() < maxdlen_)
284                 data_.reserve(maxdlen_);
285         set_data(frame.data);
286
287         DEBUG(binder_interface, "convert_from_canfd_frame: Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", id_, format_, length_,
288                                                         data_[0], data_[1], data_[2], data_[3], data_[4], data_[5], data_[6], data_[7]);
289 }
290
291 /**
292 * @brief Take all initialized class's members and build an 
293 * canfd_frame struct that can be use to send a CAN message over
294 * the bus.
295 *
296 * @return canfd_frame struct built from class members.
297 */
298 canfd_frame can_message_t::convert_to_canfd_frame()
299 {
300         canfd_frame frame;
301
302         if(is_correct_to_send())
303         {
304                 frame.can_id = get_id();
305                 frame.len = get_length();
306                 ::memcpy(frame.data, get_data(), length_);
307         }
308         else
309                 ERROR(binder_interface, "can_message_t not correctly initialized to be sent");
310         
311         return frame;
312 }
313