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, "ERROR: Can set format, wrong format chosen");
147 /// @brief Take a canfd_frame struct to initialize class members
149 /// This is the preferred way to initialize class members.
151 /// @param[in] frame - canfd_frame to convert coming from a read of CAN socket
152 /// @param[in] nbytes - bytes read from socket read operation.
154 /// @return A can_message_t object fully initialized with canfd_frame values.
156 can_message_t can_message_t::convert_from_canfd_frame(const struct canfd_frame& frame, size_t nbytes)
158 uint8_t maxdlen, length, flags = (uint8_t)NULL;
160 can_message_format_t format;
162 std::vector<uint8_t> data;
167 DEBUG(binder_interface, "set_max_data_length: Got an CAN FD frame");
168 maxdlen = CANFD_MAX_DLEN;
171 DEBUG(binder_interface, "set_max_data_length: Got a legacy CAN frame");
172 maxdlen = CAN_MAX_DLEN;
175 ERROR(binder_interface, "set_max_data_length: unsupported CAN frame");
179 if (frame.can_id & CAN_ERR_FLAG)
180 format = can_message_format_t::ERROR;
181 else if (frame.can_id & CAN_EFF_FLAG)
182 format = can_message_format_t::EXTENDED;
184 format = can_message_format_t::STANDARD;
188 case can_message_format_t::STANDARD:
189 id = frame.can_id & CAN_SFF_MASK;
191 case can_message_format_t::EXTENDED:
192 id = frame.can_id & CAN_EFF_MASK;
194 case can_message_format_t::ERROR:
195 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
198 ERROR(binder_interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
202 /* Overwrite length_ if RTR flags is detected.
203 * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
204 if (frame.can_id & CAN_RTR_FLAG)
207 if(frame.len && frame.len <= CAN_MAX_DLC)
210 length = frame.len& 0xF;
213 length = (frame.len > maxdlen) ? maxdlen : frame.len;
219 length = (frame.len > maxdlen) ? maxdlen : frame.len;
221 /* Flags field only present for CAN FD frames*/
222 if(maxdlen == CANFD_MAX_DLEN)
223 flags = frame.flags & 0xF;
225 if (data.capacity() < maxdlen)
226 data.reserve(maxdlen);
230 /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
231 for(i=0;i<maxdlen;i++)
233 data.push_back(frame.data[i]);
236 DEBUG(binder_interface, "convert_from_canfd_frame: Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
237 id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
240 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data);
244 /// @brief Take all initialized class's members and build an
245 /// canfd_frame struct that can be use to send a CAN message over
248 /// @return canfd_frame struct built from class members.
250 canfd_frame can_message_t::convert_to_canfd_frame()
254 if(is_correct_to_send())
256 frame.can_id = get_id();
257 frame.len = get_length();
258 ::memcpy(frame.data, get_data(), length_);
261 ERROR(binder_interface, "can_message_t not correctly initialized to be sent");