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