2 * Copyright (C) 2018, 2019 "IoT.bzh"
3 * Author "Arthur Guyader" <arthur.guyader@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "../../binding/low-can-hat.hpp"
22 #include "../../utils/converter.hpp"
23 #include "j1939-message.hpp"
26 * @brief Construct a new j1939 message t::j1939 message t object
29 j1939_message_t::j1939_message_t():
37 * @brief Construct a new j1939 message t::j1939 message t object
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
48 j1939_message_t::j1939_message_t(uint32_t maxdlen,
50 message_format_t format,
51 std::vector<uint8_t>& data,
56 message_t(maxdlen,length, format, data, timestamp),
63 /// @brief Retrieve name_ member value.
65 /// @return name_ class member
67 uint64_t j1939_message_t::get_name() const {
72 /// @brief Retrieve pgn_ member value.
74 /// @return pgn_ class member
76 uint32_t j1939_message_t::get_pgn() const{
81 /// @brief Retrieve addr_ member value.
83 /// @return addr_ class member
85 uint8_t j1939_message_t::get_addr() const{
90 /// @brief Take a sockaddr_can struct and array of data to initialize class members
92 /// This is the preferred way to initialize class members.
94 /// @param[in] addr - sockaddr_can to get pgn, name and addr
95 /// @param[in] data - array of data get from the j1939 socket
96 /// @param[in] nbytes - size of the array of data
97 /// @param[in] timestamp - timestamp of the message
99 /// @return A j1939_message_t object fully initialized with sockaddr_can and data values.
100 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)
104 message_format_t format;
105 std::vector<uint8_t> data_vector;
107 if(nbytes > J1939_MAX_DLEN)
109 AFB_DEBUG("Unsupported j1939 frame");
110 format = message_format_t::INVALID;
114 //AFB_DEBUG("Got a j1939 frame");
115 format = message_format_t::J1939;
118 length = (uint32_t) nbytes;
119 data_vector.reserve(length);
123 std::string data_string;
124 data_string = converter_t::to_hex(data,length);
126 for(i=0;i<length;i++)
128 data_vector.push_back(data[i]);
131 AFB_DEBUG("Found pgn: %X, format: %X, length: %X, data %s",
132 addr.can_addr.j1939.pgn, (uint8_t)format, length, data_string.c_str());
134 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));
137 /// @brief Test if members pgn_ and length are set.
139 /// @return boolean - true = set - false = not set
140 bool j1939_message_t::is_set()
142 return (pgn_ != 0 && length_ != 0);
145 /// @brief Generate a string with informations about the message
147 /// @return Debug message with informations about members
148 std::string j1939_message_t::get_debug_message()
150 std::string ret = "";
151 ret = ret + "Here is the next j1939 message : pgn " + std::to_string(pgn_) + " length " + std::to_string(length_) + ", data ";
152 for (size_t i = 0; i < data_.size(); i++)
154 ret = ret + std::to_string(data_[i]);
160 /// @brief Retrieve pgn_ member value.
162 /// @return pgn_ class member
164 uint32_t j1939_message_t::get_id() const
166 AFB_DEBUG("Prefer method get_pgn() for j1939 messages");
171 * @brief Return the sockname of the message
173 * @return struct sockaddr_can The sockname of the message
175 struct sockaddr_can j1939_message_t::get_sockname()
181 * @brief Allows to set a sockname at a message to send it after
183 * @param sockname The sockname of the message
185 void j1939_message_t::set_sockname(struct sockaddr_can sockname)
187 sockname_ = sockname;
191 * @brief Allows to generate a sockname for the message
193 * @param pgn The pgn for the sockname
194 * @param name The name for the sockname
195 * @param addr The address for the sockname
197 void j1939_message_t::set_sockname(pgn_t pgn, name_t name, uint8_t addr)
199 memset(&sockname_, 0, sizeof(sockname_));
200 sockname_.can_family = AF_CAN;
201 sockname_.can_ifindex = 0;
203 if(addr <= 0 || addr >= UINT8_MAX )
205 sockname_.can_addr.j1939.addr = J1939_NO_ADDR;
209 sockname_.can_addr.j1939.addr = addr;
212 if(name <= 0 || name >= UINT64_MAX )
214 sockname_.can_addr.j1939.name = J1939_NO_NAME;
218 sockname_.can_addr.j1939.name = name;
221 if(pgn <= 0 || pgn > J1939_PGN_MAX)
223 sockname_.can_addr.j1939.pgn = J1939_NO_PGN;
227 sockname_.can_addr.j1939.pgn = pgn;