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 /// can_message_t class constructor.
29 can_message_t::can_message_t()
35 can_message_t::can_message_t(uint32_t maxdlen,
38 message_format_t format,
41 std::vector<uint8_t>& data,
43 : message_t(maxdlen, length, format, data, timestamp, flags),
49 /// @brief Retrieve id_ member value.
51 /// @return id_ class member
53 uint32_t can_message_t::get_id() const
59 /// @brief Control whether the object is correctly initialized
60 /// to be sent over the CAN bus
62 /// @return True if object correctly initialized and false if not.
63 bool can_message_t::is_correct_to_send()
65 if (id_ != 0 && length_ != 0 && format_ != message_format_t::INVALID)
68 for(i=0;i<length_;i++)
75 /// @brief Take a canfd_frame struct to initialize class members
77 /// This is the preferred way to initialize class members.
79 /// @param[in] frame - canfd_frame to convert coming from a read of CAN socket
80 /// @param[in] nbytes - bytes read from socket read operation.
82 /// @return A can_message_t object fully initialized with canfd_frame values.
83 std::shared_ptr<can_message_t> can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp)
85 uint32_t maxdlen = 0, length = 0;
88 message_format_t format;
90 std::vector<uint8_t> data;
95 AFB_DEBUG("Got an CAN FD frame");
96 maxdlen = CANFD_MAX_DLEN;
99 AFB_DEBUG("Got a legacy CAN frame");
100 maxdlen = CAN_MAX_DLEN;
103 AFB_ERROR("unsupported CAN frame");
107 if (frame.can_id & CAN_ERR_FLAG)
109 format = message_format_t::INVALID;
110 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
112 else if (frame.can_id & CAN_EFF_FLAG)
114 format = message_format_t::EXTENDED;
115 id = frame.can_id & CAN_EFF_MASK;
119 format = message_format_t::STANDARD;
120 id = frame.can_id & CAN_SFF_MASK;
123 /* Overwrite length_ if RTR flags is detected.
124 * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
125 if (frame.can_id & CAN_RTR_FLAG)
128 if(frame.len && frame.len <= CAN_MAX_DLC)
131 length = frame.len& 0xF;
134 length = (frame.len > maxdlen) ? maxdlen : frame.len;
140 length = (frame.len > maxdlen) ? maxdlen : frame.len;
142 /* Flags field only present for CAN FD frames*/
143 if(maxdlen == CANFD_MAX_DLEN)
144 flags = frame.flags & 0xF;
146 if (data.capacity() < maxdlen)
147 data.reserve(maxdlen);
151 /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
152 for(i=0;i<maxdlen;i++)
154 data.push_back(frame.data[i]);
157 AFB_DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
158 id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
161 return std::make_shared<can_message_t>(can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp));
164 /// @brief Take all initialized class members and build a
165 /// canfd_frame struct that can be use to send a CAN message over
168 /// @return canfd_frame struct built from class members.
169 struct canfd_frame can_message_t::convert_to_canfd_frame()
173 if(is_correct_to_send())
175 frame.can_id = get_id();
176 frame.len = (uint8_t) get_length();
177 ::memcpy(frame.data, get_data(), length_);
180 AFB_ERROR("can_message_t not correctly initialized to be sent");
185 /// @brief Take all initialized class members and build a
186 /// canfd_frame struct that can be use to send a CAN message over
189 /// @return canfd_frame struct built from class members.
190 struct std::vector<canfd_frame> can_message_t::convert_to_canfd_frame_vector()
192 std::vector<canfd_frame> ret;
193 if(is_correct_to_send())
195 if(flags_ & CAN_FD_FRAME)
203 std::vector<uint8_t> data = get_data_vector((i*64),(i*64)+63);
206 ::memset(frame.data,0,sizeof(frame.data));
207 ::memcpy(frame.data,data.data(),data.size());
211 ::memcpy(frame.data,data.data(),64);
213 ret.push_back(frame);
215 } while (i<(length_ >> 6));
225 std::vector<uint8_t> data = get_data_vector(i*8,(i*8)+7);
228 ::memset(frame.data,0,sizeof(frame.data));
229 ::memcpy(frame.data,data.data(),data.size());
233 ::memset(frame.data,0,sizeof(frame.data));
234 ::memcpy(frame.data,data.data(),8);
236 ret.push_back(frame);
238 } while (i<(length_ >> 3));
242 AFB_ERROR("can_message_t not correctly initialized to be sent");
247 /// @brief Take all initialized class members and build a
248 /// can_frame struct that can be use to send a CAN message over
251 /// @return can_frame struct built from class members.
252 struct can_frame can_message_t::convert_to_can_frame()
256 if(is_correct_to_send())
258 frame.can_id = get_id();
259 frame.can_dlc = (uint8_t) get_length();
260 ::memcpy(frame.data, get_data(), length_);
263 AFB_ERROR("can_message_t not correctly initialized to be sent");
268 bool can_message_t::is_set()
270 return (id_ != 0 && length_ != 0);
273 std::string can_message_t::get_debug_message()
275 std::string ret = "";
276 ret = ret + "Here is the next can message : id " + std::to_string(id_) + " length " + std::to_string(length_) + ", data ";
277 for (size_t i = 0; i < data_.size(); i++)
279 ret = ret + std::to_string(data_[i]);
285 struct bcm_msg can_message_t::get_bcm_msg()
290 void can_message_t::set_bcm_msg(struct bcm_msg bcm_msg)