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, uint32_t id, uint8_t length, can_message_format_t format, bool rtr_flag, uint8_t flags, std::vector<uint8_t> data)
34 : maxdlen_{maxdlen}, id_{id}, length_{length}, format_{format}, rtr_flag_{rtr_flag}, flags_{flags}, data_{data}
38 * @brief Retrieve id_ member value.
40 * @return id_ class member
42 uint32_t can_message_t::get_id() const
48 * @brief Retrieve RTR flag member.
50 * @return rtr_flags_ class member
52 bool can_message_t::get_rtr_flag_() const
58 * @brief Retrieve format_ member value.
60 * @return format_ class member
62 can_message_format_t can_message_t::get_format() const
64 if (format_ != can_message_format_t::STANDARD || format_ != can_message_format_t::EXTENDED)
65 return can_message_format_t::ERROR;
70 * @brief Retrieve flags_ member value.
72 * @return flags_ class member
74 uint8_t can_message_t::get_flags() const
80 * @brief Retrieve data_ member value.
82 * @return pointer to the first element
83 * of class member data_
85 const uint8_t* can_message_t::get_data() const
91 * @brief Retrieve length_ member value.
93 * @return length_ class member
95 uint8_t can_message_t::get_length() const
101 * @brief Control whether the object is correctly initialized
102 * to be sent over the CAN bus
104 * @return true if object correctly initialized and false if not.
106 bool can_message_t::is_correct_to_send()
108 if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::ERROR)
111 for(i=0;i<CAN_MESSAGE_SIZE;i++)
119 * @brief Set format_ member value.
121 * Preferred way to initialize these members by using
122 * convert_from_canfd_frame method.
124 * @param[in] new_format - class member
126 void can_message_t::set_format(const can_message_format_t new_format)
128 if(new_format == can_message_format_t::STANDARD || new_format == can_message_format_t::EXTENDED || new_format == can_message_format_t::ERROR)
129 format_ = new_format;
131 ERROR(binder_interface, "ERROR: Can set format, wrong format chosen");
135 * @brief Take a canfd_frame struct to initialize class members
137 * This is the preferred way to initialize class members.
139 * @param[in] frame - canfd_frame to convert coming from a read of CAN socket
140 * @param[in] nbyte - bytes read from socket read operation.
142 * @return A can_message_t object fully initialized with canfd_frame values.
144 can_message_t can_message_t::convert_from_canfd_frame(const struct canfd_frame& frame, size_t nbytes)
146 uint8_t maxdlen, length, flags = (uint8_t)NULL;
148 can_message_format_t format;
150 std::vector<uint8_t> data;
155 DEBUG(binder_interface, "set_max_data_length: Got an CAN FD frame");
156 maxdlen = CANFD_MAX_DLEN;
159 DEBUG(binder_interface, "set_max_data_length: Got a legacy CAN frame");
160 maxdlen = CAN_MAX_DLEN;
163 ERROR(binder_interface, "set_max_data_length: unsupported CAN frame");
167 if (frame.can_id & CAN_ERR_FLAG)
168 format = can_message_format_t::ERROR;
169 else if (frame.can_id & CAN_EFF_FLAG)
170 format = can_message_format_t::EXTENDED;
172 format = can_message_format_t::STANDARD;
176 case can_message_format_t::STANDARD:
177 id = frame.can_id & CAN_SFF_MASK;
179 case can_message_format_t::EXTENDED:
180 id = frame.can_id & CAN_EFF_MASK;
182 case can_message_format_t::ERROR:
183 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
186 ERROR(binder_interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
190 /* Overwrite length_ if RTR flags is detected.
191 * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
192 if (frame.can_id & CAN_RTR_FLAG)
195 if(frame.len && frame.len <= CAN_MAX_DLC)
198 length = frame.len& 0xF;
201 length = (frame.len > maxdlen) ? maxdlen : frame.len;
207 length = (frame.len > maxdlen) ? maxdlen : frame.len;
209 /* Flags field only present for CAN FD frames*/
210 if(maxdlen == CANFD_MAX_DLEN)
211 flags = frame.flags & 0xF;
213 if (data.capacity() < maxdlen)
214 data.reserve(maxdlen);
218 /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
219 for(i=0;i<maxdlen;i++)
221 data.push_back(frame.data[i]);
224 DEBUG(binder_interface, "convert_from_canfd_frame: Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
225 id, format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
228 return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data);
232 * @brief Take all initialized class's members and build an
233 * canfd_frame struct that can be use to send a CAN message over
236 * @return canfd_frame struct built from class members.
238 canfd_frame can_message_t::convert_to_canfd_frame()
242 if(is_correct_to_send())
244 frame.can_id = get_id();
245 frame.len = get_length();
246 ::memcpy(frame.data, get_data(), length_);
249 ERROR(binder_interface, "can_message_t not correctly initialized to be sent");