diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2019-11-26 16:19:53 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2019-11-28 16:11:48 +0100 |
commit | 46a6d2ed70bf66ad8255513191d2bb7b26cd32bb (patch) | |
tree | 007195a0717c4b59ed6f695ef1954f519f484fb4 | |
parent | 1fa52a04542ed648b5a2a15b2f3d252f33202a33 (diff) |
Add new decoders bytes for signal of long size
This commit adds the decoder bytes. It allows to
return a sequence of bytes in hexadecimal form.
Bug-AGL : SPEC-2780
Bug-AGL: SPEC-2976
Change-Id: I27180774f044c48a9d7baa2739b15a2e85b8b2e2
Signed-off-by: Arthur Guyader <arthur.guyader@iot.bzh>
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | libs/openxc-message-format/gen/cpp/openxc.pb.h | 8 | ||||
-rw-r--r-- | low-can-binding/can/can-decoder.cpp | 91 | ||||
-rw-r--r-- | low-can-binding/can/can-decoder.hpp | 1 | ||||
-rw-r--r-- | low-can-binding/utils/openxc-utils.cpp | 54 | ||||
-rw-r--r-- | low-can-binding/utils/openxc-utils.hpp | 1 |
5 files changed, 153 insertions, 2 deletions
diff --git a/libs/openxc-message-format/gen/cpp/openxc.pb.h b/libs/openxc-message-format/gen/cpp/openxc.pb.h index 09735ee7..30b0818d 100644 --- a/libs/openxc-message-format/gen/cpp/openxc.pb.h +++ b/libs/openxc-message-format/gen/cpp/openxc.pb.h @@ -13,6 +13,8 @@ extern "C" { #endif +#define MAX_ISOTP_BYTES 4095 + /* Enum definitions */ typedef enum _openxc_VehicleMessage_Type { openxc_VehicleMessage_Type_CAN = 1, @@ -73,7 +75,8 @@ typedef enum _openxc_DiagnosticRequest_DecodedType { typedef enum _openxc_DynamicField_Type { openxc_DynamicField_Type_STRING = 1, openxc_DynamicField_Type_NUM = 2, - openxc_DynamicField_Type_BOOL = 3 + openxc_DynamicField_Type_BOOL = 3, + openxc_DynamicField_Type_BYTES = 4 } openxc_DynamicField_Type; /* Struct definitions */ @@ -163,6 +166,9 @@ typedef struct _openxc_DynamicField { double numeric_value; bool has_boolean_value; bool boolean_value; + uint8_t bytes_value[MAX_ISOTP_BYTES]; + uint32_t length_array; + bool has_bytes_value; } openxc_DynamicField; typedef struct _openxc_NetworkOperatorSettings_NetworkDescriptor { diff --git a/low-can-binding/can/can-decoder.cpp b/low-can-binding/can/can-decoder.cpp index 7d6ae2a1..c19614c0 100644 --- a/low-can-binding/can/can-decoder.cpp +++ b/low-can-binding/can/can-decoder.cpp @@ -88,6 +88,97 @@ float decoder_t::parse_signal_bitfield(signal_t& signal, std::shared_ptr<message signal.get_offset()); } + +/// @brief Decode and return string bytes (hex) for a CAN signal's. +/// +/// This is an implementation of the Signal type signature, and can be +/// used directly in the signal_t.decoder field. +/// +/// @param[in] signal - The details of the signal. +/// @param[in] message - The message with data to decode. +/// @param[out] send - An output argument that will be set to false if the value should +/// not be sent for any reason. +/// +/// @return Returns a DynamicField with a string value of bytes (hex) +/// +openxc_DynamicField decoder_t::decode_bytes(signal_t& signal, std::shared_ptr<message_t> message, bool* send) +{ + int i=0; + openxc_DynamicField decoded_value; + std::vector<uint8_t> data = message->get_data_vector(); + uint32_t length = message->get_length(); + uint32_t bit_position = signal.get_bit_position(); + uint32_t bit_size = signal.get_bit_size(); + std::vector<uint8_t> new_data = std::vector<uint8_t>(); + new_data.reserve(bit_size << 3); + + int new_start_byte = 0; + int new_end_byte = 0; + int new_start_bit = 0; + int new_end_bit = 0; + + converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit); + + if(new_end_byte >= length) + { + new_end_byte = length-1; + } + + if(new_start_byte >= length) + { + AFB_ERROR("Error in description of signals"); + return decoded_value; + } + + uint8_t first = data[new_start_byte]; + int mask_first = 0; + for(i=new_start_bit;i<8;i++) + { + mask_first = mask_first | (1 << i); + } + + uint8_t mask_first_v = 0; + if(mask_first > 255) + { + AFB_ERROR("Error mask decode bytes"); + } + else + { + mask_first_v = (uint8_t)mask_first; + } + + data[new_start_byte]=first&mask_first_v; + + uint8_t last = data[new_end_byte]; + int mask_last = 0; + for(i=0;i<=new_end_bit;i++) + { + mask_last = mask_last | (1 << (7-i)); + } + + uint8_t mask_last_v = 0; + if(mask_last > 255) + { + AFB_ERROR("Error mask decode bytes"); + } + else + { + mask_last_v = (uint8_t)mask_last; + } + + data[new_end_byte]=last&mask_last_v; + + + for(i=new_start_byte;i<=new_end_byte;i++) + { + new_data.push_back(data[i]); + } + + decoded_value = build_DynamicField(new_data); + + return decoded_value; +} + /// @brief Wraps a raw CAN signal value in a DynamicField without modification. /// /// This is an implementation of the Signal type signature, and can be diff --git a/low-can-binding/can/can-decoder.hpp b/low-can-binding/can/can-decoder.hpp index 78de8ee1..a5e3acbe 100644 --- a/low-can-binding/can/can-decoder.hpp +++ b/low-can-binding/can/can-decoder.hpp @@ -30,6 +30,7 @@ public: static openxc_DynamicField decode_boolean(signal_t& signal, std::shared_ptr<message_t> message, bool* send); static openxc_DynamicField decode_ignore(signal_t& signal, std::shared_ptr<message_t> message, bool* send); static openxc_DynamicField decode_noop(signal_t& signal, std::shared_ptr<message_t> message, bool* send); + static openxc_DynamicField decode_bytes(signal_t& signal, std::shared_ptr<message_t> message, bool* send); static openxc_DynamicField translate_signal(signal_t& signal, std::shared_ptr<message_t> message, bool* send); diff --git a/low-can-binding/utils/openxc-utils.cpp b/low-can-binding/utils/openxc-utils.cpp index c06beabb..db19d1f3 100644 --- a/low-can-binding/utils/openxc-utils.cpp +++ b/low-can-binding/utils/openxc-utils.cpp @@ -17,9 +17,10 @@ */ #include "openxc-utils.hpp" - +#include "converter.hpp" #include "../binding/application.hpp" + /// /// @brief Build a specific VehicleMessage containing a DiagnosticResponse. /// @@ -210,6 +211,40 @@ const openxc_DynamicField build_DynamicField(json_object* value) } } +const openxc_DynamicField build_DynamicField(std::vector<uint8_t> &array) +{ + openxc_DynamicField d; + d.has_type = true; + d.type = openxc_DynamicField_Type_BYTES; + + d.has_string_value = false; + d.has_numeric_value = false; + d.has_boolean_value = false; + d.has_bytes_value = true; + + + size_t size = array.size(); + + if(size > 2040) + { + AFB_ERROR("Error to generate array dynamic field, too large data"); + return d; + } + else + { + d.length_array = (uint32_t) size; + } + + + for(int i=0;i<size;i++) + { + d.bytes_value[i] = array[i]; + } + + return d; +} + + /// /// @brief Build an openxc_DynamicField with a string value /// @@ -227,6 +262,7 @@ const openxc_DynamicField build_DynamicField(const char* value) d.has_string_value = true; d.has_numeric_value = false; d.has_boolean_value = false; + d.has_bytes_value = false; ::strncpy(d.string_value, value, 100); return d; @@ -249,6 +285,7 @@ const openxc_DynamicField build_DynamicField(const std::string& value) d.has_string_value = true; d.has_numeric_value = false; d.has_boolean_value = false; + d.has_bytes_value = false; ::strncpy(d.string_value, value.c_str(), 100); return d; @@ -272,6 +309,7 @@ const openxc_DynamicField build_DynamicField(double value) d.has_string_value = false; d.has_numeric_value = true; d.has_boolean_value = false; + d.has_bytes_value = false; d.numeric_value = value; return d; @@ -293,8 +331,10 @@ const openxc_DynamicField build_DynamicField(bool value) d.has_string_value = false; d.has_numeric_value = false; d.has_boolean_value = true; + d.has_bytes_value = false; d.boolean_value = value; + return d; } @@ -350,11 +390,23 @@ const openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value) { if(field.has_numeric_value) + { json_object_object_add(value, "value", json_object_new_double(field.numeric_value)); + } else if(field.has_boolean_value) + { json_object_object_add(value, "value", json_object_new_boolean(field.boolean_value)); + } else if(field.has_string_value) + { json_object_object_add(value, "value", json_object_new_string(field.string_value)); + } + else if(field.has_bytes_value) + { + std::string s = converter_t::to_hex(field.bytes_value,field.length_array); + + json_object_object_add(value, "value", json_object_new_string(s.c_str())); + } } /// diff --git a/low-can-binding/utils/openxc-utils.hpp b/low-can-binding/utils/openxc-utils.hpp index a84bc350..61f365b0 100644 --- a/low-can-binding/utils/openxc-utils.hpp +++ b/low-can-binding/utils/openxc-utils.hpp @@ -37,6 +37,7 @@ const openxc_DynamicField build_DynamicField(const char* value); const openxc_DynamicField build_DynamicField(const std::string& value); const openxc_DynamicField build_DynamicField(double value); const openxc_DynamicField build_DynamicField(bool value); +const openxc_DynamicField build_DynamicField(std::vector<uint8_t> &array); int get_bool_from_DynamicField(const openxc_VehicleMessage& v_msg, bool& ret); double get_numerical_from_DynamicField(const openxc_VehicleMessage& v_msg); |