Constructor making mandatory to pass a can_bus_dev_t
[apps/agl-service-can-low-level.git] / src / can / 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/can-message.hpp"
19
20 #include <cstring>
21
22 #include "low-can-binding.hpp"
23
24 /********************************************************************************
25 *
26 *               CanMessage method implementation
27 *
28 *********************************************************************************/
29 /**
30 * @brief Class constructor
31 *
32 * Constructor about can_message_t class.
33 */
34 can_message_t::can_message_t()
35         : maxdlen_{0}, id_{0}, length_{0}, format_{can_message_format_t::ERROR}, rtr_flag_{false}, flags_{0}
36 {}
37
38 can_message_t::can_message_t(uint8_t maxdlen, uint32_t id, uint8_t length, can_message_format_t format, bool rtr_flag_, uint8_t flags, std::vector<uint8_t> data)
39         :  maxdlen_{0}, id_{0}, length_{0}, format_{can_message_format_t::ERROR}, rtr_flag_{false}, flags_{0}, data_{data}
40 {}
41
42 /**
43 * @brief Retrieve id_ member value.
44 *
45 * @return id_ class member
46 */
47 uint32_t can_message_t::get_id() const
48 {
49         return id_;
50 }
51
52 /**
53 * @brief Retrieve RTR flag member.
54 *
55 * @return rtr_flags_ class member
56 */
57 bool can_message_t::get_rtr_flag_() const
58 {
59         return rtr_flag_;
60 }
61
62 /**
63 * @brief Retrieve format_ member value.
64 *
65 * @return format_ class member
66 */
67 can_message_format_t can_message_t::get_format() const
68 {
69         if (format_ != can_message_format_t::STANDARD || format_ != can_message_format_t::EXTENDED)
70                 return can_message_format_t::ERROR;
71         return format_;
72 }
73
74 /**
75 * @brief Retrieve flags_ member value.
76 *
77 * @return flags_ class member 
78 */
79 uint8_t can_message_t::get_flags() const
80 {
81         return flags_;
82 }
83
84 /**
85 * @brief Retrieve data_ member value.
86 *
87 * @return pointer to the first element 
88 *  of class member data_
89 */
90 const uint8_t* can_message_t::get_data() const
91 {
92         return data_.data();
93 }
94
95 /**
96 * @brief Retrieve length_ member value.
97 *
98 * @return length_ class member
99 */
100 uint8_t can_message_t::get_length() const
101 {
102         return length_;
103 }
104
105 /**
106 * @brief Control whether the object is correctly initialized
107 *  to be sent over the CAN bus
108 *
109 * @return true if object correctly initialized and false if not.
110 */
111 bool can_message_t::is_correct_to_send()
112 {
113         if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::ERROR)
114         {
115                 int i;
116                 for(i=0;i<CAN_MESSAGE_SIZE;i++)
117                         if(data_[i] != 0)
118                                 return true;
119         }
120         return false;
121 }
122
123 /**
124 * @brief Set format_ member value.
125 *
126 * Preferred way to initialize these members by using 
127 * convert_from_canfd_frame method.
128 *
129 * @param[in] new_format - class member
130 */
131 void can_message_t::set_format(const can_message_format_t new_format)
132 {
133         if(new_format == can_message_format_t::STANDARD || new_format == can_message_format_t::EXTENDED || new_format == can_message_format_t::ERROR)
134                 format_ = new_format;
135         else
136                 ERROR(binder_interface, "ERROR: Can set format, wrong format chosen");
137 }
138
139 /**
140 * @brief Take a canfd_frame struct to initialize class members
141 *
142 * This is the preferred way to initialize class members.
143 *
144 * @param[in] args - struct read from can bus device.
145 */
146 can_message_t can_message_t::convert_to_canfd_frame(const struct canfd_frame& frame, size_t nbytes)
147 {
148         uint8_t maxdlen, length, flags = (uint8_t)NULL;
149         uint32_t id;
150         can_message_format_t format;
151         bool rtr_flag;
152         std::vector<uint8_t> data;
153
154         switch(nbytes)
155         {
156                 case CANFD_MTU:
157                         DEBUG(binder_interface, "set_max_data_length: Got an CAN FD frame");
158                         maxdlen = CANFD_MAX_DLEN;
159                         break;
160                 case CAN_MTU:
161                         DEBUG(binder_interface, "set_max_data_length: Got a legacy CAN frame");
162                         maxdlen = CAN_MAX_DLEN;
163                         break;
164                 default:
165                         ERROR(binder_interface, "set_max_data_length: unsupported CAN frame");
166                         break;
167         }
168
169         if (frame.can_id & CAN_ERR_FLAG)
170                 format = can_message_format_t::ERROR;
171         else if (frame.can_id & CAN_EFF_FLAG)
172                 format = can_message_format_t::EXTENDED;
173         else
174                 format = can_message_format_t::STANDARD;
175
176         switch(format)
177         {
178                 case can_message_format_t::STANDARD:
179                         id = frame.can_id & CAN_SFF_MASK;
180                         break;
181                 case can_message_format_t::EXTENDED:
182                         id = frame.can_id & CAN_EFF_MASK;
183                         break;
184                 case can_message_format_t::ERROR:
185                         id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
186                         break;
187                 default:
188                         ERROR(binder_interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
189                         break;
190         }
191
192         /* Overwrite length_ if RTR flags is detected.
193          * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
194         if (frame.can_id & CAN_RTR_FLAG)
195         {
196                 rtr_flag = true;
197                 if(frame.len && frame.len <= CAN_MAX_DLC)
198                 {
199                         if(rtr_flag)
200                                 length = frame.len& 0xF;
201                         else
202                         {
203                                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
204                         }       
205                 }
206         }
207         else
208         {
209                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
210
211                 /* Flags field only present for CAN FD frames*/
212                 if(maxdlen == CANFD_MAX_DLEN)
213                                 flags = frame.flags & 0xF;
214
215                 if (data.capacity() < maxdlen)
216                         data.reserve(maxdlen);
217                                 int i;
218
219                         data.clear();
220                         /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
221                         for(i=0;i<maxdlen;i++)
222                         {
223                                 data.push_back(frame.data[i]);
224                         };
225
226                 DEBUG(binder_interface, "convert_from_canfd_frame: Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X", 
227                                                                 id, format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
228         }
229
230         return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data);
231 }
232
233 /**
234 * @brief Take all initialized class's members and build an 
235 * canfd_frame struct that can be use to send a CAN message over
236 * the bus.
237 *
238 * @return canfd_frame struct built from class members.
239 */
240 canfd_frame can_message_t::convert_to_canfd_frame()
241 {
242         canfd_frame frame;
243
244         if(is_correct_to_send())
245         {
246                 frame.can_id = get_id();
247                 frame.len = get_length();
248                 ::memcpy(frame.data, get_data(), length_);
249         }
250         else
251                 ERROR(binder_interface, "can_message_t not correctly initialized to be sent");
252
253         return frame;
254 }
255
256 /********************************************************************************
257 *
258 *               can_message_definition_t method implementation
259 *
260 *********************************************************************************/
261
262 can_message_definition_t::can_message_definition_t(can_bus_dev_t& cbd)
263         : bus_{cbd}, last_value_(CAN_MESSAGE_SIZE)
264 {}
265
266 uint32_t can_message_definition_t::get_id() const
267 {
268         return id_;
269 }