Replace all enum types with masks
[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 /// @brief Control whether the object is correctly initialized
59 ///  to be sent over the CAN bus
60 ///
61 /// @return True if object correctly initialized and false if not.
62 bool can_message_t::is_correct_to_send()
63 {
64         if (id_ != 0 && length_ != 0 && !(flags_&INVALID_FLAG))
65         {
66                 int i;
67                 for(i=0;i<length_;i++)
68                         if(data_[i] != 0)
69                                 return true;
70         }
71         return false;
72 }
73
74 /// @brief Take a canfd_frame struct to initialize class members
75 ///
76 /// This is the preferred way to initialize class members.
77 ///
78 /// @param[in] frame - canfd_frame to convert coming from a read of CAN socket
79 /// @param[in] nbytes - bytes read from socket read operation.
80 ///
81 /// @return A can_message_t object fully initialized with canfd_frame values.
82 std::shared_ptr<can_message_t> can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp)
83 {
84         uint32_t maxdlen = 0, length = 0;
85         uint32_t flags = 0;
86         uint32_t id;
87         bool rtr_flag;
88         std::vector<uint8_t> data;
89
90         switch(nbytes)
91         {
92                 case CANFD_MTU:
93                         AFB_DEBUG("Got an CAN FD frame");
94                         maxdlen = CANFD_MAX_DLEN;
95                         break;
96                 case CAN_MTU:
97                         AFB_DEBUG("Got a legacy CAN frame");
98                         maxdlen = CAN_MAX_DLEN;
99                         break;
100                 default:
101                         AFB_ERROR("unsupported CAN frame");
102                         break;
103         }
104
105         if (frame.can_id & CAN_ERR_FLAG)
106         {
107                 flags = flags|INVALID_FLAG;
108                 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
109         }
110         else if (frame.can_id & CAN_EFF_FLAG)
111         {
112                 flags = flags|EXTENDED_ID;
113                 id = frame.can_id & CAN_EFF_MASK;
114         }
115         else
116         {
117                 flags = flags|STANDARD_ID;
118                 id = frame.can_id & CAN_SFF_MASK;
119         }
120
121         /* Overwrite length_ if RTR flags is detected.
122          * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
123         if (frame.can_id & CAN_RTR_FLAG)
124         {
125                 rtr_flag = true;
126                 if(frame.len && frame.len <= CAN_MAX_DLC)
127                 {
128                         if(rtr_flag)
129                                 length = frame.len& 0xF;
130                         else
131                         {
132                                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
133                         }
134                 }
135         }
136         else
137         {
138                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
139
140                 /* Flags field only present for CAN FD frames*/
141                 /*if(maxdlen == CANFD_MAX_DLEN)
142                                 flags = frame.flags & 0xF;*/
143
144                 if (data.capacity() < maxdlen)
145                         data.reserve(maxdlen);
146                                 int i;
147
148                         data.clear();
149                         /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
150                         for(i=0;i<maxdlen;i++)
151                         {
152                                 data.push_back(frame.data[i]);
153                         };
154
155                 AFB_DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
156                                                                 id, (uint32_t)flags, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
157         }
158
159         return std::make_shared<can_message_t>(can_message_t(maxdlen, id, length, rtr_flag, flags, data, timestamp));
160 }
161
162 /// @brief Take all initialized class members and build a
163 /// canfd_frame struct that can be use to send a CAN message over
164 /// the bus.
165 ///
166 /// @return canfd_frame struct built from class members.
167 struct canfd_frame can_message_t::convert_to_canfd_frame()
168 {
169         canfd_frame frame;
170
171         if(is_correct_to_send())
172         {
173                 frame.can_id = get_id();
174                 frame.len = (uint8_t) get_length();
175                 ::memcpy(frame.data, get_data(), length_);
176         }
177         else
178                 AFB_ERROR("can_message_t not correctly initialized to be sent");
179
180         return frame;
181 }
182
183 /// @brief Take all initialized class members and build a
184 /// canfd_frame struct that can be use to send a CAN message over
185 /// the bus.
186 ///
187 /// @return canfd_frame struct built from class members.
188 struct std::vector<canfd_frame> can_message_t::convert_to_canfd_frame_vector()
189 {
190         std::vector<canfd_frame> ret;
191         if(is_correct_to_send())
192         {
193                 if(flags_ & CAN_FD_FRAME)
194                 {
195                         int i=0;
196                         do
197                         {
198                                 canfd_frame frame;
199                                 frame.can_id = id_;
200                                 frame.len = 64;
201                                 std::vector<uint8_t> data = get_data_vector((i*64),(i*64)+63);
202                                 if(data.size()<64)
203                                 {
204                                         ::memset(frame.data,0,sizeof(frame.data));
205                                         ::memcpy(frame.data,data.data(),data.size());
206                                 }
207                                 else
208                                 {
209                                         ::memcpy(frame.data,data.data(),64);
210                                 }
211                                 ret.push_back(frame);
212                                 i++;
213                         } while (i<(length_ >> 6));
214                 }
215                 else
216                 {
217                         int i=0;
218                         do
219                         {
220                                 canfd_frame frame;
221                                 frame.can_id = id_;
222                                 frame.len = 8;
223                                 std::vector<uint8_t> data = get_data_vector(i*8,(i*8)+7);
224                                 if(data.size()<8)
225                                 {
226                                         ::memset(frame.data,0,sizeof(frame.data));
227                                         ::memcpy(frame.data,data.data(),data.size());
228                                 }
229                                 else
230                                 {
231                                         ::memset(frame.data,0,sizeof(frame.data));
232                                         ::memcpy(frame.data,data.data(),8);
233                                 }
234                                 ret.push_back(frame);
235                                 i++;
236                         } while (i<(length_ >> 3));
237                 }
238         }
239         else
240                 AFB_ERROR("can_message_t not correctly initialized to be sent");
241
242         return ret;
243 }
244
245 /// @brief Take all initialized class members and build a
246 /// can_frame struct that can be use to send a CAN message over
247 /// the bus.
248 ///
249 /// @return can_frame struct built from class members.
250 struct can_frame can_message_t::convert_to_can_frame()
251 {
252         can_frame frame;
253
254         if(is_correct_to_send())
255         {
256                 frame.can_id = get_id();
257                 frame.can_dlc = (uint8_t) get_length();
258                 ::memcpy(frame.data, get_data(), length_);
259         }
260         else
261                 AFB_ERROR("can_message_t not correctly initialized to be sent");
262
263         return frame;
264 }
265
266 bool can_message_t::is_set()
267 {
268         return (id_ != 0 && length_ != 0);
269 }
270
271 std::string can_message_t::get_debug_message()
272 {
273         std::string ret = "";
274         ret = ret + "Here is the next can message : id " + std::to_string(id_)  + " length " + std::to_string(length_) + ", data ";
275         for (size_t i = 0; i < data_.size(); i++)
276         {
277                 ret = ret + std::to_string(data_[i]);
278         }
279
280         return ret;
281 }
282
283 struct bcm_msg& can_message_t::get_bcm_msg()
284 {
285         return bcm_msg_;
286 }
287
288 void can_message_t::set_bcm_msg(struct bcm_msg bcm_msg)
289 {
290         bcm_msg_ = bcm_msg;
291 }