From 88077fb4b383cf7f35093b6cc0d2d9d86c6f1bf3 Mon Sep 17 00:00:00 2001 From: Arthur Guyader Date: Mon, 29 Jul 2019 12:19:13 +0200 Subject: Add feature to build messages and fix some functions Allows to build a message (J1939,BCM) with a signal and a value. Bug-AGL: SPEC-2386 Signed-off-by: Arthur Guyader Change-Id: Iadca13a927ff83f713f39da441c88356695a1285 --- low-can-binding/can/message/j1939-message.cpp | 77 ++++++++++++++++++++------- 1 file changed, 57 insertions(+), 20 deletions(-) (limited to 'low-can-binding/can/message/j1939-message.cpp') diff --git a/low-can-binding/can/message/j1939-message.cpp b/low-can-binding/can/message/j1939-message.cpp index dad8e7f1..08fc1302 100644 --- a/low-can-binding/can/message/j1939-message.cpp +++ b/low-can-binding/can/message/j1939-message.cpp @@ -16,14 +16,16 @@ */ #include +#include +#include +#include #include "../../binding/low-can-hat.hpp" #include "j1939-message.hpp" -/// -/// @brief Class constructor -/// -/// j1939_message_t class constructor. -/// +/** + * @brief Construct a new j1939 message t::j1939 message t object + * + */ j1939_message_t::j1939_message_t(): message_t(), name_{0}, @@ -31,14 +33,27 @@ j1939_message_t::j1939_message_t(): addr_{0} {} -j1939_message_t::j1939_message_t(uint8_t length, +/** + * @brief Construct a new j1939 message t::j1939 message t object + * + * @param maxdlen The max length of the message + * @param length The length of the message + * @param format The format of the message + * @param data The vector data of the message + * @param timestamp The timetamp of the message + * @param name The name of the message + * @param pgn The PGN of the message + * @param addr The address of the message + */ +j1939_message_t::j1939_message_t(uint32_t maxdlen, + uint32_t length, message_format_t format, std::vector& data, uint64_t timestamp, name_t name, pgn_t pgn, uint8_t addr): - message_t(length, format, data, timestamp), + message_t(maxdlen,length, format, data, timestamp), name_{name}, pgn_{pgn}, addr_{addr} @@ -71,6 +86,23 @@ uint8_t j1939_message_t::get_addr() const{ return addr_; } +/** + * @brief Convert hex data to string + * + * @param data An array of data + * @param length The length of the data + * @return std::string The string data + */ +std::string to_hex( uint8_t data[], const size_t length) +{ + std::stringstream stream; + stream << std::hex << std::setfill('0'); + for(int i = 0; i < length; i++) + { + stream << std::hex << ((int) data[i]); + } + return stream.str(); +} /// @brief Take a sockaddr_can struct and array of data to initialize class members /// @@ -84,9 +116,10 @@ uint8_t j1939_message_t::get_addr() const{ /// @return A j1939_message_t object fully initialized with sockaddr_can and data values. std::shared_ptr j1939_message_t::convert_from_addr(struct sockaddr_can& addr, uint8_t (&data)[128],size_t nbytes, uint64_t timestamp) { - uint8_t length = 0; - message_format_t format; - std::vector dataVector; + int i; + uint32_t length = 0; + message_format_t format; + std::vector data_vector; if(nbytes > J1939_MAX_DLEN) { @@ -95,23 +128,27 @@ std::shared_ptr j1939_message_t::convert_from_addr(struct socka } else { - AFB_DEBUG("Got a j1939 frame"); + //AFB_DEBUG("Got a j1939 frame"); format = message_format_t::J1939; } - length = (uint8_t) nbytes; - dataVector.reserve(length); - int i; - dataVector.clear(); + length = (uint32_t) nbytes; + data_vector.reserve(length); + + data_vector.clear(); + + std::string data_string; + data_string = to_hex(data,length); + for(i=0;i(j1939_message_t(length, format, dataVector, timestamp,addr.can_addr.j1939.name,addr.can_addr.j1939.pgn,addr.can_addr.j1939.addr)); + return std::make_shared(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)); } /// @brief Test if members pgn_ and length are set. @@ -143,7 +180,7 @@ std::string j1939_message_t::get_debug_message() /// uint32_t j1939_message_t::get_id() const { - AFB_WARNING("Prefer method get_pgn() for j1939 messages"); + AFB_DEBUG("Prefer method get_pgn() for j1939 messages"); return get_pgn(); } -- cgit 1.2.3-korg