can: Add big endian CAN frame layout handle
[apps/agl-service-can-low-level.git] / low-can-binding / can / message / j1939-message.cpp
1 /*
2  * Copyright (C) 2018, 2019 "IoT.bzh"
3  * Author "Arthur Guyader" <arthur.guyader@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 <cstring>
19 #include <iomanip>
20 #include <net/if.h>
21 #include "../../binding/low-can-hat.hpp"
22 #include "../../utils/converter.hpp"
23 #include "j1939-message.hpp"
24
25 /**
26  * @brief Construct a new j1939 message t::j1939 message t object
27  *
28  */
29 j1939_message_t::j1939_message_t():
30         message_t(),
31         name_{0},
32         pgn_{0},
33         addr_{0}
34 {}
35
36 /**
37  * @brief Construct a new j1939 message t::j1939 message t object
38  *
39  * @param length The length of the message
40  * @param format The format of the message
41  * @param data The vector data of the message
42  * @param timestamp The timetamp of the message
43  * @param name The name of the message
44  * @param pgn The PGN of the message
45  * @param addr The address of the message
46  */
47 j1939_message_t::j1939_message_t(uint32_t length,
48         std::vector<uint8_t>& data,
49         uint64_t timestamp,
50         name_t name,
51         pgn_t pgn,
52         uint8_t addr):
53         message_t(J1939_MAX_DLEN, length, J1939_PROTOCOL, data, timestamp),
54         name_{name},
55         pgn_{pgn},
56         addr_{addr}
57 {}
58
59 ///
60 /// @brief Retrieve name_ member value.
61 ///
62 /// @return name_ class member
63 ///
64 uint64_t j1939_message_t::get_name() const {
65         return name_;
66 }
67
68 ///
69 /// @brief Retrieve pgn_ member value.
70 ///
71 /// @return pgn_ class member
72 ///
73 uint32_t j1939_message_t::get_pgn() const{
74         return pgn_;
75 }
76
77 ///
78 /// @brief Retrieve addr_ member value.
79 ///
80 /// @return addr_ class member
81 ///
82 uint8_t j1939_message_t::get_addr() const{
83         return addr_;
84 }
85
86 /**
87  * @brief Convert hex data to string
88  *
89  * @param data An array of data
90  * @param length The length of the data
91  * @return std::string The string data
92  */
93 std::string to_hex( uint8_t data[], const size_t length)
94 {
95     std::stringstream stream;
96     stream << std::hex << std::setfill('0');
97     for(int i = 0; i < length; i++)
98     {
99         stream << std::hex << ((int) data[i]);
100     }
101     return stream.str();
102 }
103
104 /// @brief Take a sockaddr_can struct and array of data to initialize class members
105 ///
106 /// This is the preferred way to initialize class members.
107 ///
108 /// @param[in] addr - sockaddr_can to get pgn, name and addr
109 /// @param[in] data - array of data get from the j1939 socket
110 /// @param[in] nbytes - size of the array of data
111 /// @param[in] timestamp - timestamp of the message
112 ///
113 /// @return A j1939_message_t object fully initialized with sockaddr_can and data values.
114 std::shared_ptr<j1939_message_t> j1939_message_t::convert_from_addr(struct sockaddr_can& addr, uint8_t (&data)[128], size_t nbytes, uint64_t timestamp)
115 {
116         int i;
117         uint32_t length = 0;
118         std::vector<uint8_t> data_vector;
119
120         if(nbytes > J1939_MAX_DLEN)
121         {
122                 AFB_DEBUG("Unsupported j1939 frame");
123                 return std::make_shared<j1939_message_t>(j1939_message_t());
124         }
125
126         length = (uint32_t) nbytes;
127         data_vector.reserve(length);
128
129         data_vector.clear();
130
131         std::string data_string;
132         data_string = converter_t::to_hex(data, length);
133
134         for(i=0;i<length;i++)
135         {
136                 data_vector.push_back(data[i]);
137         };
138
139         AFB_DEBUG("Found pgn: %X, length: %X, data %s",
140                                                         addr.can_addr.j1939.pgn, length, data_string.c_str());
141
142         return std::make_shared<j1939_message_t>(j1939_message_t(length, data_vector, timestamp, addr.can_addr.j1939.name, addr.can_addr.j1939.pgn, addr.can_addr.j1939.addr));
143 }
144
145 /// @brief Test if members pgn_ and length are set.
146 ///
147 /// @return boolean - true = set - false = not set
148 bool j1939_message_t::is_set()
149 {
150         return (pgn_ != 0 && length_ != 0);
151 }
152
153 /// @brief Generate a string with informations about the message
154 ///
155 /// @return Debug message with informations about members
156 std::string j1939_message_t::get_debug_message()
157 {
158         std::string ret = "";
159         ret = ret + "Here is the next j1939 message : pgn " + std::to_string(pgn_)  + " length " + std::to_string(length_) + ", data ";
160         for (size_t i = 0; i < data_.size(); i++)
161         {
162                 ret = ret + std::to_string(data_[i]);
163         }
164         return ret;
165 }
166
167 ///
168 /// @brief Retrieve pgn_ member value.
169 ///
170 /// @return pgn_ class member
171 ///
172 uint32_t j1939_message_t::get_id() const
173 {
174         AFB_DEBUG("Prefer method get_pgn() for j1939 messages");
175         return get_pgn();
176 }
177
178 void j1939_message_t::set_id(const canid_t id)
179 {
180         pgn_ = id;
181 }
182
183
184 /**
185  * @brief Return the sockname of the message
186  *
187  * @return struct sockaddr_can The sockname of the message
188  */
189 struct sockaddr_can j1939_message_t::get_sockname()
190 {
191         return sockname_;
192 }
193
194 /**
195  * @brief Allows to set a sockname at a message to send it after
196  *
197  * @param sockname The sockname of the message
198  */
199 void j1939_message_t::set_sockname(struct sockaddr_can sockname)
200 {
201         sockname_ = sockname;
202 }
203
204 /**
205  * @brief Allows to generate a sockname for the message
206  *
207  * @param pgn The pgn for the sockname
208  * @param name The name for the sockname
209  * @param addr The address for the sockname
210  */
211 void j1939_message_t::set_sockname(pgn_t pgn, name_t name, uint8_t addr)
212 {
213         memset(&sockname_, 0, sizeof(sockname_));
214         sockname_.can_family = AF_CAN;
215         sockname_.can_ifindex = 0;
216
217         if(addr <= 0 || addr >= UINT8_MAX )
218         {
219                 sockname_.can_addr.j1939.addr = J1939_NO_ADDR;
220         }
221         else
222         {
223                 sockname_.can_addr.j1939.addr = addr;
224         }
225
226         if(name <= 0 || name >= UINT64_MAX )
227         {
228                 sockname_.can_addr.j1939.name = J1939_NO_NAME;
229         }
230         else
231         {
232                 sockname_.can_addr.j1939.name = name;
233         }
234
235         if(pgn <= 0 || pgn > J1939_PGN_MAX)
236         {
237                 sockname_.can_addr.j1939.pgn = J1939_NO_PGN;
238         }
239         else
240         {
241                 sockname_.can_addr.j1939.pgn = pgn;
242         }
243 }