X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Fcan%2Fmessage%2Fj1939-message.cpp;h=0d2320dc1da609764857f8970889011f9b9de649;hb=22e1fe8db6686ee6ae32cfe58a10aad9f7dfb3a7;hp=dad8e7f1bf0cbcb87a45a2faa8852b0e2944fb19;hpb=f9b67aecdc07d31def64175e0f58e9c0a113f94c;p=apps%2Fagl-service-can-low-level.git diff --git a/low-can-binding/can/message/j1939-message.cpp b/low-can-binding/can/message/j1939-message.cpp index dad8e7f1..0d2320dc 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,20 +180,67 @@ 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(); } - -struct bcm_msg j1939_message_t::get_bcm_msg() +/** + * @brief Return the sockname of the message + * + * @return struct sockaddr_can The sockname of the message + */ +struct sockaddr_can j1939_message_t::get_sockname() { - AFB_WARNING("Not implemented"); - struct bcm_msg bcm_msg; - ::memset(&bcm_msg, 0, sizeof(struct bcm_msg)); - return bcm_msg; + return sockname_; } -void j1939_message_t::set_bcm_msg(struct bcm_msg bcm_msg) +/** + * @brief Allows to set a sockname at a message to send it after + * + * @param sockname The sockname of the message + */ +void j1939_message_t::set_sockname(struct sockaddr_can sockname) { - AFB_WARNING("Not implemented"); + sockname_ = sockname; } + +/** + * @brief Allows to generate a sockname for the message + * + * @param pgn The pgn for the sockname + * @param name The name for the sockname + * @param addr The address for the sockname + */ +void j1939_message_t::set_sockname(pgn_t pgn, name_t name, uint8_t addr) +{ + memset(&sockname_, 0, sizeof(sockname_)); + sockname_.can_family = AF_CAN; + sockname_.can_ifindex = 0; + + if(addr <= 0 || addr >= UINT8_MAX ) + { + sockname_.can_addr.j1939.addr = J1939_NO_ADDR; + } + else + { + sockname_.can_addr.j1939.addr = addr; + } + + if(name <= 0 || name >= UINT64_MAX ) + { + sockname_.can_addr.j1939.name = J1939_NO_NAME; + } + else + { + sockname_.can_addr.j1939.name = name; + } + + if(pgn <= 0 || pgn > J1939_PGN_MAX) + { + sockname_.can_addr.j1939.pgn = J1939_NO_PGN; + } + else + { + sockname_.can_addr.j1939.pgn = pgn; + } +} \ No newline at end of file