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()
36 can_message_t::can_message_t(uint32_t maxdlen,
39 message_format_t format,
42 std::vector<uint8_t>& data,
44 : message_t(maxdlen, length, format, data, timestamp),
51 /// @brief Retrieve id_ member value.
53 /// @return id_ class member
55 uint32_t can_message_t::get_id() const
61 /// @brief Control whether the object is correctly initialized
62 /// to be sent over the CAN bus
64 /// @return True if object correctly initialized and false if not.
65 bool can_message_t::is_correct_to_send()
67 if (id_ != 0 && length_ != 0 && format_ != message_format_t::INVALID)
70 for(i=0;i<CAN_MESSAGE_SIZE;i++)
77 /// @brief Take a canfd_frame struct to initialize class members
79 /// This is the preferred way to initialize class members.
81 /// @param[in] frame - canfd_frame to convert coming from a read of CAN socket
82 /// @param[in] nbytes - bytes read from socket read operation.
84 /// @return A can_message_t object fully initialized with canfd_frame values.
85 std::shared_ptr<can_message_t> can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp)
87 uint32_t maxdlen = 0, length = 0;
90 message_format_t format;
92 std::vector<uint8_t> data;
97 AFB_DEBUG("Got an CAN FD frame");
98 maxdlen = CANFD_MAX_DLEN;
101 AFB_DEBUG("Got a legacy CAN frame");
102 maxdlen = CAN_MAX_DLEN;
105 AFB_ERROR("unsupported CAN frame");
109 if (frame.can_id & CAN_ERR_FLAG)
111 format = message_format_t::INVALID;
112 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
114 else if (frame.can_id & CAN_EFF_FLAG)
116 format = message_format_t::EXTENDED;
117 id = frame.can_id & CAN_EFF_MASK;
121 format = message_format_t::STANDARD;
122 id = frame.can_id & CAN_SFF_MASK;
125 /* Overwrite length_ if RTR flags is detected.
126 * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
127 if (frame.can_id & CAN_RTR_FLAG)
130 if(frame.len && frame.len <= CAN_MAX_DLC)
133 length = frame.len& 0xF;
136 length = (frame.len > maxdlen) ? maxdlen : frame.len;
142 length = (frame.len > maxdlen) ? maxdlen : frame.len;
144 /* Flags field only present for CAN FD frames*/
145 if(maxdlen == CANFD_MAX_DLEN)
146 flags = frame.flags & 0xF;
148 if (data.capacity() < maxdlen)
149 data.reserve(maxdlen);
153 /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
154 for(i=0;i<maxdlen;i++)
156 data.push_back(frame.data[i]);
159 AFB_DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
160 id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
163 return std::make_shared<can_message_t>(can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp));
166 /// @brief Take all initialized class members and build a
167 /// canfd_frame struct that can be use to send a CAN message over
170 /// @return canfd_frame struct built from class members.
171 struct canfd_frame can_message_t::convert_to_canfd_frame()
175 if(is_correct_to_send())
177 frame.can_id = get_id();
178 frame.len = (uint8_t) get_length();
179 ::memcpy(frame.data, get_data(), length_);
182 AFB_ERROR("can_message_t not correctly initialized to be sent");
187 /// @brief Take all initialized class members and build a
188 /// can_frame struct that can be use to send a CAN message over
191 /// @return can_frame struct built from class members.
192 struct can_frame can_message_t::convert_to_can_frame()
196 if(is_correct_to_send())
198 frame.can_id = get_id();
199 frame.can_dlc = (uint8_t) get_length();
200 ::memcpy(frame.data, get_data(), length_);
203 AFB_ERROR("can_message_t not correctly initialized to be sent");
208 bool can_message_t::is_set()
210 return (id_ != 0 && length_ != 0);
213 std::string can_message_t::get_debug_message()
215 std::string ret = "";
216 ret = ret + "Here is the next can message : id " + std::to_string(id_) + " length " + std::to_string(length_) + ", data ";
217 for (size_t i = 0; i < data_.size(); i++)
219 ret = ret + std::to_string(data_[i]);
225 struct bcm_msg can_message_t::get_bcm_msg()
230 void can_message_t::set_bcm_msg(struct bcm_msg bcm_msg)
235 uint32_t can_message_t::get_flags()