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 "../low-can-binding.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::ERROR}, 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::ERROR;
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 length_ member value.
105 /// @return length_ class member
107 uint8_t can_message_t::get_length() const
113 /// @brief Control whether the object is correctly initialized
114 /// to be sent over the CAN bus
116 /// @return true if object correctly initialized and false if not.
118 bool can_message_t::is_correct_to_send()
120 if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::ERROR)
123 for(i=0;i<CAN_MESSAGE_SIZE;i++)
131 /// @brief Set format_ member value.
133 /// Preferred way to initialize these members by using
134 /// convert_from_canfd_frame method.
136 /// @param[in] new_format - class member
138 void can_message_t::set_format(const can_message_format_t new_format)
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;
143 ERROR(binder_interface, "%s: Can set format, wrong format chosen", __FUNCTION__);
146 /// @brief Take a canfd_frame struct to initialize class members
148 /// This is the preferred way to initialize class members.
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.
153 /// @return A can_message_t object fully initialized with canfd_frame values.
155 can_message_t can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes)
157 uint8_t maxdlen, length, flags = (uint8_t)NULL;
159 can_message_format_t format;
161 std::vector<uint8_t> data;
166 DEBUG(binder_interface, "%s: Got an CAN FD frame", __FUNCTION__);
167 maxdlen = CANFD_MAX_DLEN;
170 DEBUG(binder_interface, "%s: Got a legacy CAN frame", __FUNCTION__);
171 maxdlen = CAN_MAX_DLEN;
174 ERROR(binder_interface, "%s: unsupported CAN frame", __FUNCTION__);
178 if (frame.can_id & CAN_ERR_FLAG)
180 format = can_message_format_t::ERROR;
181 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
183 else if (frame.can_id & CAN_EFF_FLAG)
185 format = can_message_format_t::EXTENDED;
186 id = frame.can_id & CAN_EFF_MASK;
190 format = can_message_format_t::STANDARD;
191 id = frame.can_id & CAN_SFF_MASK;
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)
199 if(frame.len && frame.len <= CAN_MAX_DLC)
202 length = frame.len& 0xF;
205 length = (frame.len > maxdlen) ? maxdlen : frame.len;
211 length = (frame.len > maxdlen) ? maxdlen : frame.len;
213 /* Flags field only present for CAN FD frames*/
214 if(maxdlen == CANFD_MAX_DLEN)
215 flags = frame.flags & 0xF;
217 if (data.capacity() < maxdlen)
218 data.reserve(maxdlen);
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++)
225 data.push_back(frame.data[i]);
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]);
232 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data);
235 can_message_t can_message_t::convert_from_frame(const struct can_frame& frame, size_t nbytes)
237 uint8_t maxdlen, length, flags = (uint8_t)NULL;
239 can_message_format_t format;
241 std::vector<uint8_t> data;
243 if(nbytes <= CAN_MTU)
245 DEBUG(binder_interface, "%s: Got a legacy CAN frame", __FUNCTION__);
246 maxdlen = CAN_MAX_DLEN;
250 ERROR(binder_interface, "%s: unsupported CAN frame", __FUNCTION__);
253 if (frame.can_id & CAN_ERR_FLAG)
255 format = can_message_format_t::ERROR;
256 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
258 else if (frame.can_id & CAN_EFF_FLAG)
260 format = can_message_format_t::EXTENDED;
261 id = frame.can_id & CAN_EFF_MASK;
265 format = can_message_format_t::STANDARD;
266 id = frame.can_id & CAN_SFF_MASK;
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)
274 if(frame.can_dlc && frame.can_dlc <= CAN_MAX_DLC)
277 length = frame.can_dlc& 0xF;
280 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
286 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
288 if (data.capacity() < maxdlen)
289 data.reserve(maxdlen);
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++)
296 data.push_back(frame.data[i]);
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]);
303 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data);
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
311 /// @return canfd_frame struct built from class members.
313 struct canfd_frame can_message_t::convert_to_canfd_frame()
317 if(is_correct_to_send())
319 frame.can_id = get_id();
320 frame.len = get_length();
321 ::memcpy(frame.data, get_data(), length_);
324 ERROR(binder_interface, "%s: can_message_t not correctly initialized to be sent", __FUNCTION__);
329 struct can_frame can_message_t::convert_to_can_frame()
333 if(is_correct_to_send())
335 frame.can_id = get_id();
336 frame.can_dlc = get_length();
337 ::memcpy(frame.data, get_data(), length_);
340 ERROR(binder_interface, "%s: can_message_t not correctly initialized to be sent", __FUNCTION__);