Add gitlab issue/merge request templates
[apps/agl-service-can-low-level.git] / low-can-binding / can / message / can-message.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include "./can-message.hpp"
19
20 #include <cstring>
21
22 #include "../../binding/low-can-hat.hpp"
23
24 ///
25 /// @brief Class constructor
26 ///
27 /// can_message_t class constructor.
28 ///
29 can_message_t::can_message_t()
30         : message_t(),
31          id_{0},
32          rtr_flag_{false}
33 {}
34
35 can_message_t::can_message_t(uint32_t maxdlen,
36         uint32_t id,
37         uint32_t length,
38         bool rtr_flag,
39         uint32_t flags,
40         std::vector<uint8_t>& data,
41         uint64_t timestamp)
42         : message_t(maxdlen, length, flags, data, timestamp),
43         id_{id},
44         rtr_flag_{rtr_flag}
45 {}
46
47 ///
48 /// @brief Retrieve id_ member value.
49 ///
50 /// @return id_ class member
51 ///
52 uint32_t can_message_t::get_id() const
53 {
54         return id_;
55 }
56
57
58 void can_message_t::set_id(const canid_t id)
59 {
60         id_ = id;
61 }
62
63 /// @brief Control whether the object is correctly initialized
64 ///  to be sent over the CAN bus
65 ///
66 /// @return True if object correctly initialized and false if not.
67 bool can_message_t::is_correct_to_send()
68 {
69         if (id_ != 0 && length_ != 0 && !(flags_&INVALID_FLAG))
70         {
71                 int i;
72                 for(i=0;i<length_;i++)
73                         if(data_[i] != 0)
74                                 return true;
75         }
76         return false;
77 }
78
79 /// @brief Take a canfd_frame struct to initialize class members
80 ///
81 /// This is the preferred way to initialize class members.
82 ///
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.
85 ///
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)
88 {
89         uint32_t maxdlen = 0, length = 0;
90         uint32_t flags = 0;
91         uint32_t id;
92         bool rtr_flag;
93         std::vector<uint8_t> data;
94
95         switch(nbytes)
96         {
97                 case CANFD_MTU:
98                         AFB_DEBUG("Got an CAN FD frame");
99                         maxdlen = CANFD_MAX_DLEN;
100                         break;
101                 case CAN_MTU:
102                         AFB_DEBUG("Got a legacy CAN frame");
103                         maxdlen = CAN_MAX_DLEN;
104                         break;
105                 default:
106                         AFB_ERROR("unsupported CAN frame");
107                         break;
108         }
109
110         if (frame.can_id & CAN_ERR_FLAG)
111         {
112                 flags = flags|INVALID_FLAG;
113                 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
114         }
115         else if (frame.can_id & CAN_EFF_FLAG)
116         {
117                 id = frame.can_id & CAN_EFF_MASK;
118         }
119         else
120         {
121                 id = frame.can_id & CAN_SFF_MASK;
122         }
123
124         /* Overwrite length_ if RTR flags is detected.
125          * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
126         if (frame.can_id & CAN_RTR_FLAG)
127         {
128                 rtr_flag = true;
129                 if(frame.len && frame.len <= CAN_MAX_DLC)
130                 {
131                         if(rtr_flag)
132                                 length = frame.len& 0xF;
133                         else
134                                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
135                 }
136         }
137         else
138         {
139                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
140
141                 /* Flags field only present for CAN FD frames*/
142                 /*if(maxdlen == CANFD_MAX_DLEN)
143                                 flags = frame.flags & 0xF;*/
144
145                 if (data.capacity() < maxdlen)
146                         data.reserve(maxdlen);
147                                 int i;
148
149                         data.clear();
150                         /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
151                         for(i=0;i<maxdlen;i++)
152                         {
153                                 data.push_back(frame.data[i]);
154                         };
155
156                 AFB_DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
157                                                                 id, (uint32_t)flags, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
158         }
159
160         return std::make_shared<can_message_t>(can_message_t(maxdlen, id, length, rtr_flag, flags, data, timestamp));
161 }
162
163 /// @brief Take all initialized class members and build a
164 /// canfd_frame struct that can be use to send a CAN message over
165 /// the bus.
166 ///
167 /// @return canfd_frame struct built from class members.
168 struct canfd_frame can_message_t::convert_to_canfd_frame()
169 {
170         canfd_frame frame;
171
172         if(is_correct_to_send())
173         {
174                 frame.can_id = get_id();
175                 frame.len = (uint8_t) get_length();
176                 ::memcpy(frame.data, get_data(), length_);
177         }
178         else
179         {
180                 AFB_ERROR("can_message_t not correctly initialized to be sent");
181         }
182
183         return frame;
184 }
185
186 /// @brief Take all initialized class members and build a
187 /// canfd_frame struct that can be use to send a CAN message over
188 /// the bus.
189 ///
190 /// @return canfd_frame struct built from class members.
191 struct std::vector<canfd_frame> can_message_t::convert_to_canfd_frame_vector()
192 {
193         std::vector<canfd_frame> ret;
194         int i = 0;
195
196         if(is_correct_to_send())
197         {
198                 while (i < length_)
199                 {
200                         canfd_frame frame;
201                         frame.can_id = id_;
202                         if(flags_ & CAN_FD_FRAME)
203                         {
204                                 frame.len = (length_ - (i * CANFD_MAX_DLC)) > CANFD_MAX_DLC - 1 ? CANFD_MAX_DLC : (uint8_t)(length_ - (i * CANFD_MAX_DLC));
205                                 ::memcpy(frame.data, &data_.data()[i*CANFD_MAX_DLC], frame.len);
206                                 i += CANFD_MAX_DLC;
207                         }
208                         else
209                         {
210                                 frame.len = (length_ - (i * CAN_MAX_DLC)) > CAN_MAX_DLC - 1 ? CAN_MAX_DLC : (uint8_t)(length_ - (i * CAN_MAX_DLC));
211                                 ::memcpy(frame.data, &data_.data()[i*CAN_MAX_DLEN], frame.len);
212                                 i += CAN_MAX_DLC;
213                         }
214                         ret.push_back(frame);
215                 }
216         }
217         else
218         {
219                 AFB_ERROR("can_message_t not correctly initialized to be sent");
220         }
221
222         return ret;
223 }
224
225 /// @brief Take all initialized class members and build a
226 /// can_frame struct that can be use to send a CAN message over
227 /// the bus.
228 ///
229 /// @return can_frame struct built from class members.
230 struct can_frame can_message_t::convert_to_can_frame()
231 {
232         can_frame frame;
233
234         if(is_correct_to_send())
235         {
236                 frame.can_id = get_id();
237                 frame.can_dlc = (uint8_t) get_length();
238                 ::memcpy(frame.data, get_data(), length_);
239         }
240         else
241         {
242                 AFB_ERROR("can_message_t not correctly initialized to be sent");
243         }
244
245         return frame;
246 }
247
248 bool can_message_t::is_set()
249 {
250         return (id_ != 0 && length_ != 0);
251 }
252
253 std::string can_message_t::get_debug_message()
254 {
255         std::string ret = "";
256         ret = ret + "Here is the next can message : id " + std::to_string(id_)  + " length " + std::to_string(length_) + ", data ";
257         for (size_t i = 0; i < data_.size(); i++)
258                 ret = ret + std::to_string(data_[i]);
259
260         return ret;
261 }
262
263 struct bcm_msg& can_message_t::get_bcm_msg()
264 {
265         return bcm_msg_;
266 }
267
268 void can_message_t::set_bcm_msg(struct bcm_msg bcm_msg)
269 {
270         bcm_msg_ = bcm_msg;
271 }