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()
33 format_{can_message_format_t::INVALID},
40 can_message_t::can_message_t(uint8_t maxdlen,
43 can_message_format_t format,
46 std::vector<uint8_t>& data,
55 timestamp_{timestamp},
60 /// @brief Retrieve id_ member value.
62 /// @return id_ class member
64 uint32_t can_message_t::get_id() const
69 int can_message_t::get_sub_id() const
75 /// @brief Retrieve RTR flag member.
77 /// @return rtr_flags_ class member
79 bool can_message_t::get_rtr_flag_() const
85 /// @brief Retrieve format_ member value.
87 /// @return format_ class member
89 can_message_format_t can_message_t::get_format() const
91 if (format_ != can_message_format_t::STANDARD || format_ != can_message_format_t::EXTENDED)
92 return can_message_format_t::INVALID;
97 /// @brief Retrieve flags_ member value.
99 /// @return flags_ class member
101 uint8_t can_message_t::get_flags() const
107 /// @brief Retrieve data_ member value.
109 /// @return pointer to the first element
110 /// of class member data_
112 const uint8_t* can_message_t::get_data() const
118 /// @brief Retrieve data_ member whole vector
120 /// @return the vector as is
122 const std::vector<uint8_t> can_message_t::get_data_vector() const
128 /// @brief Retrieve length_ member value.
130 /// @return length_ class member
132 uint8_t can_message_t::get_length() const
137 void can_message_t::set_sub_id(int sub_id)
142 uint64_t can_message_t::get_timestamp() const
147 void can_message_t::set_timestamp(uint64_t timestamp)
149 timestamp_ = timestamp;
153 /// @brief Control whether the object is correctly initialized
154 /// to be sent over the CAN bus
156 /// @return true if object correctly initialized and false if not.
158 bool can_message_t::is_correct_to_send()
160 if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::INVALID)
163 for(i=0;i<CAN_MESSAGE_SIZE;i++)
171 /// @brief Set format_ member value.
173 /// Preferred way to initialize these members by using
174 /// convert_from_canfd_frame method.
176 /// @param[in] new_format - class member
178 void can_message_t::set_format(const can_message_format_t new_format)
180 if(new_format == can_message_format_t::STANDARD || new_format == can_message_format_t::EXTENDED || new_format == can_message_format_t::INVALID)
181 format_ = new_format;
183 ERROR(binder_interface, "%s: Can set format, wrong format chosen", __FUNCTION__);
186 /// @brief Take a canfd_frame struct to initialize class members
188 /// This is the preferred way to initialize class members.
190 /// @param[in] frame - canfd_frame to convert coming from a read of CAN socket
191 /// @param[in] nbytes - bytes read from socket read operation.
193 /// @return A can_message_t object fully initialized with canfd_frame values.
195 can_message_t can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp)
197 uint8_t maxdlen, length, flags = (uint8_t)NULL;
199 can_message_format_t format;
201 std::vector<uint8_t> data;
206 DEBUG(binder_interface, "%s: Got an CAN FD frame", __FUNCTION__);
207 maxdlen = CANFD_MAX_DLEN;
210 DEBUG(binder_interface, "%s: Got a legacy CAN frame", __FUNCTION__);
211 maxdlen = CAN_MAX_DLEN;
214 ERROR(binder_interface, "%s: unsupported CAN frame", __FUNCTION__);
218 if (frame.can_id & CAN_ERR_FLAG)
220 format = can_message_format_t::INVALID;
221 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
223 else if (frame.can_id & CAN_EFF_FLAG)
225 format = can_message_format_t::EXTENDED;
226 id = frame.can_id & CAN_EFF_MASK;
230 format = can_message_format_t::STANDARD;
231 id = frame.can_id & CAN_SFF_MASK;
234 /* Overwrite length_ if RTR flags is detected.
235 * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
236 if (frame.can_id & CAN_RTR_FLAG)
239 if(frame.len && frame.len <= CAN_MAX_DLC)
242 length = frame.len& 0xF;
245 length = (frame.len > maxdlen) ? maxdlen : frame.len;
251 length = (frame.len > maxdlen) ? maxdlen : frame.len;
253 /* Flags field only present for CAN FD frames*/
254 if(maxdlen == CANFD_MAX_DLEN)
255 flags = frame.flags & 0xF;
257 if (data.capacity() < maxdlen)
258 data.reserve(maxdlen);
262 /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
263 for(i=0;i<maxdlen;i++)
265 data.push_back(frame.data[i]);
268 DEBUG(binder_interface, "%s: Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", __FUNCTION__,
269 id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
272 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp);
275 can_message_t can_message_t::convert_from_frame(const struct can_frame& frame, size_t nbytes, uint64_t timestamp)
277 uint8_t maxdlen, length, flags = (uint8_t)NULL;
279 can_message_format_t format;
281 std::vector<uint8_t> data;
283 if(nbytes <= CAN_MTU)
285 DEBUG(binder_interface, "%s: Got a legacy CAN frame", __FUNCTION__);
286 maxdlen = CAN_MAX_DLEN;
290 ERROR(binder_interface, "%s: unsupported CAN frame", __FUNCTION__);
293 if (frame.can_id & CAN_ERR_FLAG)
295 format = can_message_format_t::INVALID;
296 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
298 else if (frame.can_id & CAN_EFF_FLAG)
300 format = can_message_format_t::EXTENDED;
301 id = frame.can_id & CAN_EFF_MASK;
305 format = can_message_format_t::STANDARD;
306 id = frame.can_id & CAN_SFF_MASK;
309 /* Overwrite length_ if RTR flags is detected.
310 * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
311 if (frame.can_id & CAN_RTR_FLAG)
314 if(frame.can_dlc && frame.can_dlc <= CAN_MAX_DLC)
317 length = frame.can_dlc& 0xF;
320 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
326 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
328 if (data.capacity() < maxdlen)
329 data.reserve(maxdlen);
333 /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
334 for(i=0;i<maxdlen;i++)
336 data.push_back(frame.data[i]);
339 // DEBUG(binder_interface, "%s: Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", __FUNCTION__,
340 // id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
343 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp);
347 /// @brief Take all initialized class's members and build an
348 /// canfd_frame struct that can be use to send a CAN message over
351 /// @return canfd_frame struct built from class members.
353 struct canfd_frame can_message_t::convert_to_canfd_frame()
357 if(is_correct_to_send())
359 frame.can_id = get_id();
360 frame.len = get_length();
361 ::memcpy(frame.data, get_data(), length_);
364 ERROR(binder_interface, "%s: can_message_t not correctly initialized to be sent", __FUNCTION__);
369 struct can_frame can_message_t::convert_to_can_frame()
373 if(is_correct_to_send())
375 frame.can_id = get_id();
376 frame.can_dlc = get_length();
377 ::memcpy(frame.data, get_data(), length_);
380 ERROR(binder_interface, "%s: can_message_t not correctly initialized to be sent", __FUNCTION__);