2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "can-message.hpp"
22 #include "../binding/low-can-hat.hpp"
25 /// @brief Class constructor
27 /// Constructor about can_message_t class.
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}
33 can_message_t::can_message_t(uint8_t maxdlen,
36 can_message_format_t format,
39 std::vector<uint8_t> data,
52 /// @brief Retrieve id_ member value.
54 /// @return id_ class member
56 uint32_t can_message_t::get_id() const
62 /// @brief Retrieve RTR flag member.
64 /// @return rtr_flags_ class member
66 bool can_message_t::get_rtr_flag_() const
72 /// @brief Retrieve format_ member value.
74 /// @return format_ class member
76 can_message_format_t can_message_t::get_format() const
78 if (format_ != can_message_format_t::STANDARD || format_ != can_message_format_t::EXTENDED)
79 return can_message_format_t::INVALID;
84 /// @brief Retrieve flags_ member value.
86 /// @return flags_ class member
88 uint8_t can_message_t::get_flags() const
94 /// @brief Retrieve data_ member value.
96 /// @return pointer to the first element
97 /// of class member data_
99 const uint8_t* can_message_t::get_data() const
105 /// @brief Retrieve data_ member whole vector
107 /// @return the vector as is
109 const std::vector<uint8_t> can_message_t::get_data_vector() const
115 /// @brief Retrieve length_ member value.
117 /// @return length_ class member
119 uint8_t can_message_t::get_length() const
124 uint64_t can_message_t::get_timestamp() const
129 void can_message_t::set_timestamp(uint64_t timestamp)
131 timestamp_ = timestamp;
135 /// @brief Control whether the object is correctly initialized
136 /// to be sent over the CAN bus
138 /// @return true if object correctly initialized and false if not.
140 bool can_message_t::is_correct_to_send()
142 if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::INVALID)
145 for(i=0;i<CAN_MESSAGE_SIZE;i++)
153 /// @brief Set format_ member value.
155 /// Preferred way to initialize these members by using
156 /// convert_from_canfd_frame method.
158 /// @param[in] new_format - class member
160 void can_message_t::set_format(const can_message_format_t new_format)
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;
165 ERROR(binder_interface, "%s: Can set format, wrong format chosen", __FUNCTION__);
168 /// @brief Take a canfd_frame struct to initialize class members
170 /// This is the preferred way to initialize class members.
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.
175 /// @return A can_message_t object fully initialized with canfd_frame values.
177 can_message_t can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp)
179 uint8_t maxdlen, length, flags = (uint8_t)NULL;
181 can_message_format_t format;
183 std::vector<uint8_t> data;
188 DEBUG(binder_interface, "%s: Got an CAN FD frame", __FUNCTION__);
189 maxdlen = CANFD_MAX_DLEN;
192 DEBUG(binder_interface, "%s: Got a legacy CAN frame", __FUNCTION__);
193 maxdlen = CAN_MAX_DLEN;
196 ERROR(binder_interface, "%s: unsupported CAN frame", __FUNCTION__);
200 if (frame.can_id & CAN_ERR_FLAG)
202 format = can_message_format_t::INVALID;
203 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
205 else if (frame.can_id & CAN_EFF_FLAG)
207 format = can_message_format_t::EXTENDED;
208 id = frame.can_id & CAN_EFF_MASK;
212 format = can_message_format_t::STANDARD;
213 id = frame.can_id & CAN_SFF_MASK;
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)
221 if(frame.len && frame.len <= CAN_MAX_DLC)
224 length = frame.len& 0xF;
227 length = (frame.len > maxdlen) ? maxdlen : frame.len;
233 length = (frame.len > maxdlen) ? maxdlen : frame.len;
235 /* Flags field only present for CAN FD frames*/
236 if(maxdlen == CANFD_MAX_DLEN)
237 flags = frame.flags & 0xF;
239 if (data.capacity() < maxdlen)
240 data.reserve(maxdlen);
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++)
247 data.push_back(frame.data[i]);
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]);
254 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp);
257 can_message_t can_message_t::convert_from_frame(const struct can_frame& frame, size_t nbytes, uint64_t timestamp)
259 uint8_t maxdlen, length, flags = (uint8_t)NULL;
261 can_message_format_t format;
263 std::vector<uint8_t> data;
265 if(nbytes <= CAN_MTU)
267 DEBUG(binder_interface, "%s: Got a legacy CAN frame", __FUNCTION__);
268 maxdlen = CAN_MAX_DLEN;
272 ERROR(binder_interface, "%s: unsupported CAN frame", __FUNCTION__);
275 if (frame.can_id & CAN_ERR_FLAG)
277 format = can_message_format_t::INVALID;
278 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
280 else if (frame.can_id & CAN_EFF_FLAG)
282 format = can_message_format_t::EXTENDED;
283 id = frame.can_id & CAN_EFF_MASK;
287 format = can_message_format_t::STANDARD;
288 id = frame.can_id & CAN_SFF_MASK;
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)
296 if(frame.can_dlc && frame.can_dlc <= CAN_MAX_DLC)
299 length = frame.can_dlc& 0xF;
302 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
308 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
310 if (data.capacity() < maxdlen)
311 data.reserve(maxdlen);
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++)
318 data.push_back(frame.data[i]);
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]);
325 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp);
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
333 /// @return canfd_frame struct built from class members.
335 struct canfd_frame can_message_t::convert_to_canfd_frame()
339 if(is_correct_to_send())
341 frame.can_id = get_id();
342 frame.len = get_length();
343 ::memcpy(frame.data, get_data(), length_);
346 ERROR(binder_interface, "%s: can_message_t not correctly initialized to be sent", __FUNCTION__);
351 struct can_frame can_message_t::convert_to_can_frame()
355 if(is_correct_to_send())
357 frame.can_id = get_id();
358 frame.can_dlc = get_length();
359 ::memcpy(frame.data, get_data(), length_);
362 ERROR(binder_interface, "%s: can_message_t not correctly initialized to be sent", __FUNCTION__);