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.
19 #include "../../binding/low-can-hat.hpp"
20 #include "j1939-message.hpp"
23 /// @brief Class constructor
25 /// j1939_message_t class constructor.
27 j1939_message_t::j1939_message_t():
34 j1939_message_t::j1939_message_t(uint8_t length,
35 message_format_t format,
36 std::vector<uint8_t>& data,
41 message_t(length, format, data, timestamp),
48 /// @brief Retrieve name_ member value.
50 /// @return name_ class member
52 uint64_t j1939_message_t::get_name() const {
57 /// @brief Retrieve pgn_ member value.
59 /// @return pgn_ class member
61 uint32_t j1939_message_t::get_pgn() const{
66 /// @brief Retrieve addr_ member value.
68 /// @return addr_ class member
70 uint8_t j1939_message_t::get_addr() const{
75 /// @brief Take a sockaddr_can struct and array of data to initialize class members
77 /// This is the preferred way to initialize class members.
79 /// @param[in] addr - sockaddr_can to get pgn, name and addr
80 /// @param[in] data - array of data get from the j1939 socket
81 /// @param[in] nbytes - size of the array of data
82 /// @param[in] timestamp - timestamp of the message
84 /// @return A j1939_message_t object fully initialized with sockaddr_can and data values.
85 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)
88 message_format_t format;
89 std::vector<uint8_t> dataVector;
91 if(nbytes > J1939_MAX_DLEN)
93 AFB_DEBUG("Unsupported j1939 frame");
94 format = message_format_t::INVALID;
98 AFB_DEBUG("Got a j1939 frame");
99 format = message_format_t::J1939;
102 length = (uint8_t) nbytes;
103 dataVector.reserve(length);
106 for(i=0;i<length;i++)
108 dataVector.push_back(data[i]);
111 AFB_DEBUG("Found pgn: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
112 addr.can_addr.j1939.pgn, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
114 return std::make_shared<j1939_message_t>(j1939_message_t(length, format, dataVector, timestamp,addr.can_addr.j1939.name,addr.can_addr.j1939.pgn,addr.can_addr.j1939.addr));
117 /// @brief Test if members pgn_ and length are set.
119 /// @return boolean - true = set - false = not set
120 bool j1939_message_t::is_set()
122 return (pgn_ != 0 && length_ != 0);
125 /// @brief Generate a string with informations about the message
127 /// @return Debug message with informations about members
128 std::string j1939_message_t::get_debug_message()
130 std::string ret = "";
131 ret = ret + "Here is the next j1939 message : pgn " + std::to_string(pgn_) + " length " + std::to_string(length_) + ", data ";
132 for (size_t i = 0; i < data_.size(); i++)
134 ret = ret + std::to_string(data_[i]);
140 /// @brief Retrieve pgn_ member value.
142 /// @return pgn_ class member
144 uint32_t j1939_message_t::get_id() const
146 AFB_WARNING("Prefer method get_pgn() for j1939 messages");
151 struct bcm_msg j1939_message_t::get_bcm_msg()
153 AFB_WARNING("Not implemented");
154 struct bcm_msg bcm_msg;
155 ::memset(&bcm_msg, 0, sizeof(struct bcm_msg));
159 void j1939_message_t::set_bcm_msg(struct bcm_msg bcm_msg)
161 AFB_WARNING("Not implemented");