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