Add feature to build messages and fix some functions
[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          flags_{0}
34 {}
35
36 can_message_t::can_message_t(uint32_t maxdlen,
37         uint32_t id,
38         uint32_t length,
39         message_format_t format,
40         bool rtr_flag,
41         uint32_t flags,
42         std::vector<uint8_t>& data,
43         uint64_t timestamp)
44         : message_t(maxdlen, length, format, data, timestamp),
45         id_{id},
46         rtr_flag_{rtr_flag},
47         flags_{flags}
48 {}
49
50 ///
51 /// @brief Retrieve id_ member value.
52 ///
53 /// @return id_ class member
54 ///
55 uint32_t can_message_t::get_id() const
56 {
57         return id_;
58 }
59
60
61 /// @brief Control whether the object is correctly initialized
62 ///  to be sent over the CAN bus
63 ///
64 /// @return True if object correctly initialized and false if not.
65 bool can_message_t::is_correct_to_send()
66 {
67         if (id_ != 0 && length_ != 0 && format_ != message_format_t::INVALID)
68         {
69                 int i;
70                 for(i=0;i<CAN_MESSAGE_SIZE;i++)
71                         if(data_[i] != 0)
72                                 return true;
73         }
74         return false;
75 }
76
77 /// @brief Take a canfd_frame struct to initialize class members
78 ///
79 /// This is the preferred way to initialize class members.
80 ///
81 /// @param[in] frame - canfd_frame to convert coming from a read of CAN socket
82 /// @param[in] nbytes - bytes read from socket read operation.
83 ///
84 /// @return A can_message_t object fully initialized with canfd_frame values.
85 std::shared_ptr<can_message_t> can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp)
86 {
87         uint32_t maxdlen = 0, length = 0;
88         uint8_t flags = 0;
89         uint32_t id;
90         message_format_t format;
91         bool rtr_flag;
92         std::vector<uint8_t> data;
93
94         switch(nbytes)
95         {
96                 case CANFD_MTU:
97                         AFB_DEBUG("Got an CAN FD frame");
98                         maxdlen = CANFD_MAX_DLEN;
99                         break;
100                 case CAN_MTU:
101                         AFB_DEBUG("Got a legacy CAN frame");
102                         maxdlen = CAN_MAX_DLEN;
103                         break;
104                 default:
105                         AFB_ERROR("unsupported CAN frame");
106                         break;
107         }
108
109         if (frame.can_id & CAN_ERR_FLAG)
110         {
111                 format = message_format_t::INVALID;
112                 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
113         }
114         else if (frame.can_id & CAN_EFF_FLAG)
115         {
116                 format = message_format_t::EXTENDED;
117                 id = frame.can_id & CAN_EFF_MASK;
118         }
119         else
120         {
121                 format = message_format_t::STANDARD;
122                 id = frame.can_id & CAN_SFF_MASK;
123         }
124
125         /* Overwrite length_ if RTR flags is detected.
126          * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
127         if (frame.can_id & CAN_RTR_FLAG)
128         {
129                 rtr_flag = true;
130                 if(frame.len && frame.len <= CAN_MAX_DLC)
131                 {
132                         if(rtr_flag)
133                                 length = frame.len& 0xF;
134                         else
135                         {
136                                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
137                         }
138                 }
139         }
140         else
141         {
142                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
143
144                 /* Flags field only present for CAN FD frames*/
145                 if(maxdlen == CANFD_MAX_DLEN)
146                                 flags = frame.flags & 0xF;
147
148                 if (data.capacity() < maxdlen)
149                         data.reserve(maxdlen);
150                                 int i;
151
152                         data.clear();
153                         /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
154                         for(i=0;i<maxdlen;i++)
155                         {
156                                 data.push_back(frame.data[i]);
157                         };
158
159                 AFB_DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
160                                                                 id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
161         }
162
163         return std::make_shared<can_message_t>(can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp));
164 }
165
166 /// @brief Take all initialized class members and build a
167 /// canfd_frame struct that can be use to send a CAN message over
168 /// the bus.
169 ///
170 /// @return canfd_frame struct built from class members.
171 struct canfd_frame can_message_t::convert_to_canfd_frame()
172 {
173         canfd_frame frame;
174
175         if(is_correct_to_send())
176         {
177                 frame.can_id = get_id();
178                 frame.len = (uint8_t) get_length();
179                 ::memcpy(frame.data, get_data(), length_);
180         }
181         else
182                 AFB_ERROR("can_message_t not correctly initialized to be sent");
183
184         return frame;
185 }
186
187 /// @brief Take all initialized class members and build a
188 /// can_frame struct that can be use to send a CAN message over
189 /// the bus.
190 ///
191 /// @return can_frame struct built from class members.
192 struct can_frame can_message_t::convert_to_can_frame()
193 {
194         can_frame frame;
195
196         if(is_correct_to_send())
197         {
198                 frame.can_id = get_id();
199                 frame.can_dlc = (uint8_t) get_length();
200                 ::memcpy(frame.data, get_data(), length_);
201         }
202         else
203                 AFB_ERROR("can_message_t not correctly initialized to be sent");
204
205         return frame;
206 }
207
208 bool can_message_t::is_set()
209 {
210         return (id_ != 0 && length_ != 0);
211 }
212
213 std::string can_message_t::get_debug_message()
214 {
215         std::string ret = "";
216     ret = ret + "Here is the next can message : id " + std::to_string(id_)  + " length " + std::to_string(length_) + ", data ";
217     for (size_t i = 0; i < data_.size(); i++)
218     {
219         ret = ret + std::to_string(data_[i]);
220     }
221
222     return ret;
223 }
224
225 struct bcm_msg can_message_t::get_bcm_msg()
226 {
227         return bcm_msg_;
228 }
229
230 void can_message_t::set_bcm_msg(struct bcm_msg bcm_msg)
231 {
232         bcm_msg_ = bcm_msg;
233 }
234
235 uint32_t can_message_t::get_flags()
236 {
237         return flags_;
238 }