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. Default to INVALID.
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;
152 /// @brief Control whether the object is correctly initialized
153 /// to be sent over the CAN bus
155 /// @return True if object correctly initialized and false if not.
156 bool can_message_t::is_correct_to_send()
158 if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::INVALID)
161 for(i=0;i<CAN_MESSAGE_SIZE;i++)
168 /// @brief Set format_ member value.
170 /// Preferred way to initialize these members by using
171 /// convert_from_canfd_frame method.
173 /// @param[in] new_format - class member
174 void can_message_t::set_format(const can_message_format_t new_format)
176 if(new_format == can_message_format_t::STANDARD || new_format == can_message_format_t::EXTENDED || new_format == can_message_format_t::INVALID)
177 format_ = new_format;
179 ERROR("Can set format, wrong format chosen");
182 /// @brief Take a canfd_frame struct to initialize class members
184 /// This is the preferred way to initialize class members.
186 /// @param[in] frame - canfd_frame to convert coming from a read of CAN socket
187 /// @param[in] nbytes - bytes read from socket read operation.
189 /// @return A can_message_t object fully initialized with canfd_frame values.
190 can_message_t can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp)
192 uint8_t maxdlen, length, flags = (uint8_t)NULL;
194 can_message_format_t format;
196 std::vector<uint8_t> data;
201 DEBUG("Got an CAN FD frame");
202 maxdlen = CANFD_MAX_DLEN;
205 DEBUG("Got a legacy CAN frame");
206 maxdlen = CAN_MAX_DLEN;
209 ERROR("unsupported CAN frame");
213 if (frame.can_id & CAN_ERR_FLAG)
215 format = can_message_format_t::INVALID;
216 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
218 else if (frame.can_id & CAN_EFF_FLAG)
220 format = can_message_format_t::EXTENDED;
221 id = frame.can_id & CAN_EFF_MASK;
225 format = can_message_format_t::STANDARD;
226 id = frame.can_id & CAN_SFF_MASK;
229 /* Overwrite length_ if RTR flags is detected.
230 * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
231 if (frame.can_id & CAN_RTR_FLAG)
234 if(frame.len && frame.len <= CAN_MAX_DLC)
237 length = frame.len& 0xF;
240 length = (frame.len > maxdlen) ? maxdlen : frame.len;
246 length = (frame.len > maxdlen) ? maxdlen : frame.len;
248 /* Flags field only present for CAN FD frames*/
249 if(maxdlen == CANFD_MAX_DLEN)
250 flags = frame.flags & 0xF;
252 if (data.capacity() < maxdlen)
253 data.reserve(maxdlen);
257 /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
258 for(i=0;i<maxdlen;i++)
260 data.push_back(frame.data[i]);
263 DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
264 id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
267 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp);
270 /// @brief Take a can_frame struct to initialize class members
272 /// This is the preferred way to initialize class members.
274 /// @param[in] frame - can_frame to convert coming from a read of CAN socket
275 /// @param[in] nbytes - bytes read from socket read operation.
277 /// @return A can_message_t object fully initialized with can_frame values.
278 can_message_t can_message_t::convert_from_frame(const struct can_frame& frame, size_t nbytes, uint64_t timestamp)
280 uint8_t maxdlen, length, flags = (uint8_t)NULL;
282 can_message_format_t format;
284 std::vector<uint8_t> data;
286 if(nbytes <= CAN_MTU)
288 DEBUG("Got a legacy CAN frame");
289 maxdlen = CAN_MAX_DLEN;
293 ERROR("unsupported CAN frame");
296 if (frame.can_id & CAN_ERR_FLAG)
298 format = can_message_format_t::INVALID;
299 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
301 else if (frame.can_id & CAN_EFF_FLAG)
303 format = can_message_format_t::EXTENDED;
304 id = frame.can_id & CAN_EFF_MASK;
308 format = can_message_format_t::STANDARD;
309 id = frame.can_id & CAN_SFF_MASK;
312 /* Overwrite length_ if RTR flags is detected.
313 * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
314 if (frame.can_id & CAN_RTR_FLAG)
317 if(frame.can_dlc && frame.can_dlc <= CAN_MAX_DLC)
320 length = frame.can_dlc& 0xF;
323 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
329 length = (frame.can_dlc > maxdlen) ? maxdlen : frame.can_dlc;
331 if (data.capacity() < maxdlen)
332 data.reserve(maxdlen);
336 /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
337 for(i=0;i<maxdlen;i++)
339 data.push_back(frame.data[i]);
342 // DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
343 // id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
346 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp);
349 /// @brief Take all initialized class's members and build an
350 /// canfd_frame struct that can be use to send a CAN message over
353 /// @return canfd_frame struct built from class members.
354 struct canfd_frame can_message_t::convert_to_canfd_frame()
358 if(is_correct_to_send())
360 frame.can_id = get_id();
361 frame.len = get_length();
362 ::memcpy(frame.data, get_data(), length_);
365 ERROR("can_message_t not correctly initialized to be sent");
370 /// @brief Take all initialized class's members and build an
371 /// can_frame struct that can be use to send a CAN message over
374 /// @return can_frame struct built from class members.
375 struct can_frame can_message_t::convert_to_can_frame()
379 if(is_correct_to_send())
381 frame.can_id = get_id();
382 frame.can_dlc = get_length();
383 ::memcpy(frame.data, get_data(), length_);
386 ERROR("can_message_t not correctly initialized to be sent");