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