Cleaning the code for now unused functions
[apps/agl-service-can-low-level.git] / 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 "../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         : maxdlen_{0},
31          id_{0},
32          length_{0},
33          format_{can_message_format_t::INVALID},
34          rtr_flag_{false},
35          flags_{0},
36          timestamp_{0},
37          sub_id_{-1}
38 {}
39
40 can_message_t::can_message_t(uint8_t maxdlen,
41         uint32_t id,
42         uint8_t length,
43         can_message_format_t format,
44         bool rtr_flag,
45         uint8_t flags,
46         std::vector<uint8_t>& data,
47         uint64_t timestamp)
48         :  maxdlen_{maxdlen},
49         id_{id},
50         length_{length},
51         format_{format},
52         rtr_flag_{rtr_flag},
53         flags_{flags},
54         data_{data},
55         timestamp_{timestamp},
56         sub_id_{-1}
57 {}
58
59 ///
60 /// @brief Retrieve id_ member value.
61 ///
62 /// @return id_ class member
63 ///
64 uint32_t can_message_t::get_id() const
65 {
66         return id_;
67 }
68
69 int can_message_t::get_sub_id() const
70 {
71         return sub_id_;
72 }
73
74 ///
75 /// @brief Retrieve data_ member value.
76 ///
77 /// @return pointer to the first element
78 ///  of class member data_
79 ///
80 const uint8_t* can_message_t::get_data() const
81 {
82         return data_.data();
83 }
84
85 ///
86 /// @brief Retrieve data_ member whole vector
87 ///
88 /// @return the vector as is
89 ///
90 const std::vector<uint8_t> can_message_t::get_data_vector() const
91 {
92         return 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 void can_message_t::set_sub_id(int sub_id)
106 {
107         sub_id_ = sub_id;
108 }
109
110 uint64_t can_message_t::get_timestamp() const
111 {
112         return timestamp_;
113 }
114
115 /// @brief Control whether the object is correctly initialized
116 ///  to be sent over the CAN bus
117 ///
118 /// @return True if object correctly initialized and false if not.
119 bool can_message_t::is_correct_to_send()
120 {
121         if (id_ != 0 && length_ != 0 && format_ != can_message_format_t::INVALID)
122         {
123                 int i;
124                 for(i=0;i<CAN_MESSAGE_SIZE;i++)
125                         if(data_[i] != 0)
126                                 return true;
127         }
128         return false;
129 }
130
131 /// @brief Take a canfd_frame struct to initialize class members
132 ///
133 /// This is the preferred way to initialize class members.
134 ///
135 /// @param[in] frame - canfd_frame to convert coming from a read of CAN socket
136 /// @param[in] nbytes - bytes read from socket read operation.
137 ///
138 /// @return A can_message_t object fully initialized with canfd_frame values.
139 can_message_t can_message_t::convert_from_frame(const struct canfd_frame& frame, size_t nbytes, uint64_t timestamp)
140 {
141         uint8_t maxdlen = 0, length = 0, flags = 0;
142         uint32_t id;
143         can_message_format_t format;
144         bool rtr_flag;
145         std::vector<uint8_t> data;
146
147         switch(nbytes)
148         {
149                 case CANFD_MTU:
150                         AFB_DEBUG("Got an CAN FD frame");
151                         maxdlen = CANFD_MAX_DLEN;
152                         break;
153                 case CAN_MTU:
154                         AFB_DEBUG("Got a legacy CAN frame");
155                         maxdlen = CAN_MAX_DLEN;
156                         break;
157                 default:
158                         AFB_ERROR("unsupported CAN frame");
159                         break;
160         }
161
162         if (frame.can_id & CAN_ERR_FLAG)
163         {
164                 format = can_message_format_t::INVALID;
165                 id = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
166         }
167         else if (frame.can_id & CAN_EFF_FLAG)
168         {
169                 format = can_message_format_t::EXTENDED;
170                 id = frame.can_id & CAN_EFF_MASK;
171         }
172         else
173         {
174                 format = can_message_format_t::STANDARD;
175                 id = frame.can_id & CAN_SFF_MASK;
176         }
177
178         /* Overwrite length_ if RTR flags is detected.
179          * standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
180         if (frame.can_id & CAN_RTR_FLAG)
181         {
182                 rtr_flag = true;
183                 if(frame.len && frame.len <= CAN_MAX_DLC)
184                 {
185                         if(rtr_flag)
186                                 length = frame.len& 0xF;
187                         else
188                         {
189                                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
190                         }
191                 }
192         }
193         else
194         {
195                 length = (frame.len > maxdlen) ? maxdlen : frame.len;
196
197                 /* Flags field only present for CAN FD frames*/
198                 if(maxdlen == CANFD_MAX_DLEN)
199                                 flags = frame.flags & 0xF;
200
201                 if (data.capacity() < maxdlen)
202                         data.reserve(maxdlen);
203                                 int i;
204
205                         data.clear();
206                         /* maxdlen_ is now set at CAN_MAX_DLEN or CANFD_MAX_DLEN, respectively 8 and 64 bytes*/
207                         for(i=0;i<maxdlen;i++)
208                         {
209                                 data.push_back(frame.data[i]);
210                         };
211
212                 AFB_DEBUG("Found id: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
213                                                                 id, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
214         }
215
216         return can_message_t(maxdlen, id, length, format, rtr_flag, flags, data, timestamp);
217 }