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,
40 std::vector<uint8_t>& data,
42 : message_t(maxdlen, length, flags, data, timestamp),
48 /// @brief Retrieve id_ member value.
50 /// @return id_ class member
52 uint32_t can_message_t::get_id() const
58 void can_message_t::set_id(const canid_t id)
63 /// @brief Control whether the object is correctly initialized
64 /// to be sent over the CAN bus
66 /// @return True if object correctly initialized and false if not.
67 bool can_message_t::is_correct_to_send()
69 if (id_ != 0 && length_ != 0 && !(flags_&INVALID_FLAG))
72 for(i=0;i<length_;i++)
79 /// @brief Take a canfd_frame struct to initialize class members
81 /// This is the preferred way to initialize class members.
83 /// @param[in] frame - canfd_frame to convert coming from a read of CAN socket
84 /// @param[in] nbytes - bytes read from socket read operation.
86 /// @return A can_message_t object fully initialized with canfd_frame values.
87 std::shared_ptr<can_message_t> can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp)
89 uint32_t maxdlen = 0, length = 0;
93 std::vector<uint8_t> data;
98 AFB_DEBUG("Got an CAN FD frame");
99 maxdlen = CANFD_MAX_DLEN;
102 AFB_DEBUG("Got a legacy CAN frame");
103 maxdlen = CAN_MAX_DLEN;
106 AFB_ERROR("unsupported CAN frame");
110 if (frame.can_id & CAN_ERR_FLAG)
112 flags = flags|INVALID_FLAG;
113 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
115 else if (frame.can_id & CAN_EFF_FLAG)
117 flags = flags|EXTENDED_ID;
118 id = frame.can_id & CAN_EFF_MASK;
122 flags = flags|STANDARD_ID;
123 id = frame.can_id & CAN_SFF_MASK;
126 /* Overwrite length_ if RTR flags is detected.
127 * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
128 if (frame.can_id & CAN_RTR_FLAG)
131 if(frame.len && frame.len <= CAN_MAX_DLC)
134 length = frame.len& 0xF;
137 length = (frame.len > maxdlen) ? maxdlen : frame.len;
143 length = (frame.len > maxdlen) ? maxdlen : frame.len;
145 /* Flags field only present for CAN FD frames*/
146 /*if(maxdlen == CANFD_MAX_DLEN)
147 flags = frame.flags & 0xF;*/
149 if (data.capacity() < maxdlen)
150 data.reserve(maxdlen);
154 /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
155 for(i=0;i<maxdlen;i++)
157 data.push_back(frame.data[i]);
160 AFB_DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
161 id, (uint32_t)flags, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
164 return std::make_shared<can_message_t>(can_message_t(maxdlen, id, length, rtr_flag, flags, data, timestamp));
167 /// @brief Take all initialized class members and build a
168 /// canfd_frame struct that can be use to send a CAN message over
171 /// @return canfd_frame struct built from class members.
172 struct canfd_frame can_message_t::convert_to_canfd_frame()
176 if(is_correct_to_send())
178 frame.can_id = get_id();
179 frame.len = (uint8_t) get_length();
180 ::memcpy(frame.data, get_data(), length_);
183 AFB_ERROR("can_message_t not correctly initialized to be sent");
188 /// @brief Take all initialized class members and build a
189 /// canfd_frame struct that can be use to send a CAN message over
192 /// @return canfd_frame struct built from class members.
193 struct std::vector<canfd_frame> can_message_t::convert_to_canfd_frame_vector()
195 std::vector<canfd_frame> ret;
196 if(is_correct_to_send())
198 if(flags_ & CAN_FD_FRAME)
206 std::vector<uint8_t> data = get_data_vector((i*64),(i*64)+63);
209 ::memset(frame.data,0,sizeof(frame.data));
210 ::memcpy(frame.data,data.data(),data.size());
214 ::memcpy(frame.data,data.data(),64);
216 ret.push_back(frame);
218 } while (i<(length_ >> 6));
228 std::vector<uint8_t> data = get_data_vector(i*8,(i*8)+7);
231 ::memset(frame.data,0,sizeof(frame.data));
232 ::memcpy(frame.data,data.data(),data.size());
236 ::memset(frame.data,0,sizeof(frame.data));
237 ::memcpy(frame.data,data.data(),8);
239 ret.push_back(frame);
241 } while (i<(length_ >> 3));
245 AFB_ERROR("can_message_t not correctly initialized to be sent");
250 /// @brief Take all initialized class members and build a
251 /// can_frame struct that can be use to send a CAN message over
254 /// @return can_frame struct built from class members.
255 struct can_frame can_message_t::convert_to_can_frame()
259 if(is_correct_to_send())
261 frame.can_id = get_id();
262 frame.can_dlc = (uint8_t) get_length();
263 ::memcpy(frame.data, get_data(), length_);
266 AFB_ERROR("can_message_t not correctly initialized to be sent");
271 bool can_message_t::is_set()
273 return (id_ != 0 && length_ != 0);
276 std::string can_message_t::get_debug_message()
278 std::string ret = "";
279 ret = ret + "Here is the next can message : id " + std::to_string(id_) + " length " + std::to_string(length_) + ", data ";
280 for (size_t i = 0; i < data_.size(); i++)
282 ret = ret + std::to_string(data_[i]);
288 struct bcm_msg& can_message_t::get_bcm_msg()
293 void can_message_t::set_bcm_msg(struct bcm_msg bcm_msg)