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.
18 #include "./j1939-message.hpp"
20 #include "../../binding/low-can-hat.hpp"
24 /// @brief Class constructor
26 /// j1939_message_t class constructor.
28 j1939_message_t::j1939_message_t():
35 j1939_message_t::j1939_message_t(uint8_t length,
36 can_message_format_t format,
37 std::vector<uint8_t>& data,
42 message_t(length, format, data, timestamp),
49 /// @brief Retrieve name_ member value.
51 /// @return name_ class member
53 uint64_t j1939_message_t::get_name() const {
58 /// @brief Retrieve pgn_ member value.
60 /// @return pgn_ class member
62 uint32_t j1939_message_t::get_pgn() const{
67 /// @brief Retrieve addr_ member value.
69 /// @return addr_ class member
71 uint8_t j1939_message_t::get_addr() const{
76 /// @brief Take a sockaddr_can struct and array of data to initialize class members
78 /// This is the preferred way to initialize class members.
80 /// @param[in] addr - sockaddr_can to get pgn, name and addr
81 /// @param[in] data - array of data get from the j1939 socket
82 /// @param[in] nbytes - size of the array of data
83 /// @param[in] timestamp - timestamp of the message
85 /// @return A j1939_message_t object fully initialized with sockaddr_can and data values.
86 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)
89 can_message_format_t format;
90 std::vector<uint8_t> dataVector;
92 if(nbytes > J1939_MAX_DLEN)
94 AFB_DEBUG("Unsupported j1939 frame");
95 format = can_message_format_t::INVALID;
99 AFB_DEBUG("Got a j1939 frame");
100 format = can_message_format_t::J1939;
103 length = (uint8_t) nbytes;
104 dataVector.reserve(length);
107 for(i=0;i<length;i++)
109 dataVector.push_back(data[i]);
112 AFB_DEBUG("Found pgn: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
113 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]);
115 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));
118 /// @brief Test if members pgn_ and length are set.
120 /// @return boolean - true = set - false = not set
121 bool j1939_message_t::is_set()
123 return (pgn_ != 0 && length_ != 0);
126 /// @brief Generate a string with informations about the message
128 /// @return Debug message with informations about members
129 std::string j1939_message_t::get_debug_message()
131 std::string ret = "";
132 ret = ret + "Here is the next j1939 message : pgn " + std::to_string(pgn_) + " length " + std::to_string(length_) + ", data ";
133 for (size_t i = 0; i < data_.size(); i++)
135 ret = ret + std::to_string(data_[i]);
141 /// @brief Retrieve pgn_ member value.
143 /// @return pgn_ class member
145 uint32_t j1939_message_t::get_id() const
147 AFB_WARNING("Prefer method get_pgn() for j1939 messages");
152 struct bcm_msg j1939_message_t::get_bcm_msg()
154 AFB_WARNING("Not implemented");
155 struct bcm_msg bcm_msg;
156 ::memset(&bcm_msg, 0, sizeof(struct bcm_msg));
160 void j1939_message_t::set_bcm_msg(struct bcm_msg bcm_msg)
162 AFB_WARNING("Not implemented");