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