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}
33 can_message_t::can_message_t(uint8_t maxdlen,
36 can_message_format_t format,
39 std::vector<uint8_t> data)
50 /// @brief Retrieve id_ member value.
52 /// @return id_ class member
54 uint32_t can_message_t::get_id() const
60 /// @brief Retrieve RTR flag member.
62 /// @return rtr_flags_ class member
64 bool can_message_t::get_rtr_flag_() const
70 /// @brief Retrieve format_ member value.
72 /// @return format_ class member
74 can_message_format_t can_message_t::get_format() const
76 if (format_ != can_message_format_t::STANDARD || format_ != can_message_format_t::EXTENDED)
77 return can_message_format_t::INVALID;
82 /// @brief Retrieve flags_ member value.
84 /// @return flags_ class member
86 uint8_t can_message_t::get_flags() const
92 /// @brief Retrieve data_ member value.
94 /// @return pointer to the first element
95 /// of class member data_
97 const uint8_t* can_message_t::get_data() const
103 /// @brief Retrieve data_ member whole vector
105 /// @return the vector as is
107 const std::vector<uint8_t> can_message_t::get_data_vector() const
113 /// @brief Retrieve length_ member value.
115 /// @return length_ class member
117 uint8_t can_message_t::get_length() const
123 /// @brief Control whether the object is correctly initialized
124 /// to be sent over the CAN bus
126 /// @return true if object correctly initialized and false if not.
128 bool can_message_t::is_correct_to_send()
130 if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::INVALID)
133 for(i=0;i<CAN_MESSAGE_SIZE;i++)
141 /// @brief Set format_ member value.
143 /// Preferred way to initialize these members by using
144 /// convert_from_canfd_frame method.
146 /// @param[in] new_format - class member
148 void can_message_t::set_format(const can_message_format_t new_format)
150 if(new_format == can_message_format_t::STANDARD || new_format == can_message_format_t::EXTENDED || new_format == can_message_format_t::INVALID)
151 format_ = new_format;
153 ERROR(binder_interface, "%s: Can set format, wrong format chosen", __FUNCTION__);
156 /// @brief Take a canfd_frame struct to initialize class members
158 /// This is the preferred way to initialize class members.
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.
163 /// @return A can_message_t object fully initialized with canfd_frame values.
165 can_message_t can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes)
167 uint8_t maxdlen, length, flags = (uint8_t)NULL;
169 can_message_format_t format;
171 std::vector<uint8_t> data;
176 DEBUG(binder_interface, "%s: Got an CAN FD frame", __FUNCTION__);
177 maxdlen = CANFD_MAX_DLEN;
180 DEBUG(binder_interface, "%s: Got a legacy CAN frame", __FUNCTION__);
181 maxdlen = CAN_MAX_DLEN;
184 ERROR(binder_interface, "%s: unsupported CAN frame", __FUNCTION__);
188 if (frame.can_id & CAN_ERR_FLAG)
190 format = can_message_format_t::INVALID;
191 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
193 else if (frame.can_id & CAN_EFF_FLAG)
195 format = can_message_format_t::EXTENDED;
196 id = frame.can_id & CAN_EFF_MASK;
200 format = can_message_format_t::STANDARD;
201 id = frame.can_id & CAN_SFF_MASK;
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)
209 if(frame.len && frame.len <= CAN_MAX_DLC)
212 length = frame.len& 0xF;
215 length = (frame.len > maxdlen) ? maxdlen : frame.len;
221 length = (frame.len > maxdlen) ? maxdlen : frame.len;
223 /* Flags field only present for CAN FD frames*/
224 if(maxdlen == CANFD_MAX_DLEN)
225 flags = frame.flags & 0xF;
227 if (data.capacity() < maxdlen)
228 data.reserve(maxdlen);
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++)
235 data.push_back(frame.data[i]);
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]);
242 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data);
245 can_message_t can_message_t::convert_from_frame(const struct can_frame& frame, size_t nbytes)
247 uint8_t maxdlen, length, flags = (uint8_t)NULL;
249 can_message_format_t format;
251 std::vector<uint8_t> data;
253 if(nbytes <= CAN_MTU)
255 DEBUG(binder_interface, "%s: Got a legacy CAN frame", __FUNCTION__);
256 maxdlen = CAN_MAX_DLEN;
260 ERROR(binder_interface, "%s: unsupported CAN frame", __FUNCTION__);
263 if (frame.can_id & CAN_ERR_FLAG)
265 format = can_message_format_t::INVALID;
266 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
268 else if (frame.can_id & CAN_EFF_FLAG)
270 format = can_message_format_t::EXTENDED;
271 id = frame.can_id & CAN_EFF_MASK;
275 format = can_message_format_t::STANDARD;
276 id = frame.can_id & CAN_SFF_MASK;
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)
284 if(frame.can_dlc && frame.can_dlc <= CAN_MAX_DLC)
287 length = frame.can_dlc& 0xF;
290 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
296 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
298 if (data.capacity() < maxdlen)
299 data.reserve(maxdlen);
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++)
306 data.push_back(frame.data[i]);
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]);
313 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data);
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
321 /// @return canfd_frame struct built from class members.
323 struct canfd_frame can_message_t::convert_to_canfd_frame()
327 if(is_correct_to_send())
329 frame.can_id = get_id();
330 frame.len = get_length();
331 ::memcpy(frame.data, get_data(), length_);
334 ERROR(binder_interface, "%s: can_message_t not correctly initialized to be sent", __FUNCTION__);
339 struct can_frame can_message_t::convert_to_can_frame()
343 if(is_correct_to_send())
345 frame.can_id = get_id();
346 frame.can_dlc = get_length();
347 ::memcpy(frame.data, get_data(), length_);
350 ERROR(binder_interface, "%s: can_message_t not correctly initialized to be sent", __FUNCTION__);