summaryrefslogtreecommitdiffstats
path: root/low-can-binding
diff options
context:
space:
mode:
authorArthur Guyader <arthur.guyader@iot.bzh>2019-12-13 15:23:27 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2020-01-09 16:25:36 +0100
commite8c14d2baa8841f13beefaa1fb662a280ea8eb96 (patch)
treea063203c055c957aac8ea60dbe440c90f6d200cd /low-can-binding
parent74acfee2361a04f5ea389b31c4defecc30d8fc6b (diff)
decoder: Add new decoders: ascii, date, time
Change-Id: I392bff2c1f4062aee186875335786fcda9cc0649 Signed-off-by: Arthur Guyader <arthur.guyader@iot.bzh> Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'low-can-binding')
-rw-r--r--low-can-binding/can/can-decoder.cpp61
-rw-r--r--low-can-binding/can/can-decoder.hpp3
-rw-r--r--low-can-binding/utils/converter.cpp19
-rw-r--r--low-can-binding/utils/converter.hpp1
4 files changed, 82 insertions, 2 deletions
diff --git a/low-can-binding/can/can-decoder.cpp b/low-can-binding/can/can-decoder.cpp
index 488fdfb8..15341b1c 100644
--- a/low-can-binding/can/can-decoder.cpp
+++ b/low-can-binding/can/can-decoder.cpp
@@ -195,6 +195,67 @@ openxc_DynamicField decoder_t::decode_bytes(signal_t& signal, std::shared_ptr<me
return decoded_value;
}
+
+/// @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_ascii(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
+{
+ std::string ret_s = "";
+ openxc_DynamicField openxc_bytes = decode_bytes(signal,message,send);
+ if(!openxc_bytes.has_bytes_value)
+ {
+ AFB_ERROR("Error no bytes value to translate to ascii");
+ }
+ ret_s = converter_t::to_ascii(openxc_bytes.bytes_value,openxc_bytes.length_array);
+ openxc_DynamicField ret = build_DynamicField(ret_s);
+ return ret;
+}
+
+//edit
+openxc_DynamicField decoder_t::decode_date(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
+{
+ float value = decoder_t::parse_signal_bitfield(signal, message);
+ AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
+ openxc_DynamicField decoded_value = build_DynamicField(value);
+
+ // Don't send if they is no changes
+ if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
+ {
+ *send = false;
+ }
+ signal.set_last_value(value);
+
+ return decoded_value;
+}
+
+//edit
+openxc_DynamicField decoder_t::decode_time(signal_t& signal, std::shared_ptr<message_t> message, bool* send)
+{
+ float value = decoder_t::parse_signal_bitfield(signal, message);
+ AFB_DEBUG("Decoded message from parse_signal_bitfield: %f", value);
+ openxc_DynamicField decoded_value = build_DynamicField(value);
+
+ // Don't send if they is no changes
+ if ((signal.get_last_value() == value && !signal.get_send_same()) || !*send )
+ {
+ *send = false;
+ }
+ signal.set_last_value(value);
+
+ 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 2f7823ad..fd246f3c 100644
--- a/low-can-binding/can/can-decoder.hpp
+++ b/low-can-binding/can/can-decoder.hpp
@@ -32,6 +32,9 @@ public:
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 decode_ascii(signal_t& signal, std::shared_ptr<message_t> message, bool* send);
+ static openxc_DynamicField decode_date(signal_t& signal, std::shared_ptr<message_t> message, bool* send);
+ static openxc_DynamicField decode_time(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/converter.cpp b/low-can-binding/utils/converter.cpp
index c6c2f5e1..56625742 100644
--- a/low-can-binding/utils/converter.cpp
+++ b/low-can-binding/utils/converter.cpp
@@ -22,11 +22,11 @@
#include <climits>
/**
- * @brief Convert hex data to string
+ * @brief Convert data to hex string
*
* @param data An array of data
* @param length The length of the data
- * @return std::string The string data
+ * @return std::string The hex string
*/
std::string converter_t::to_hex(const uint8_t data[], const size_t length)
{
@@ -39,6 +39,21 @@ std::string converter_t::to_hex(const uint8_t data[], const size_t length)
}
/**
+ * @brief Convert data to ascii string
+ *
+ * @param data An array of data
+ * @param length The length of the data
+ * @return std::string The ascii string
+ */
+std::string converter_t::to_ascii(const uint8_t data[], const size_t length)
+{
+ std::stringstream stream;
+ for(int i = 0; i < length; i++)
+ stream << ((char) data[i]);
+ return stream.str();
+}
+
+/**
* @brief Translate bit_position and bit_size
*
*
diff --git a/low-can-binding/utils/converter.hpp b/low-can-binding/utils/converter.hpp
index 45e29e9a..e8a40f5b 100644
--- a/low-can-binding/utils/converter.hpp
+++ b/low-can-binding/utils/converter.hpp
@@ -23,6 +23,7 @@ class converter_t
{
public:
static std::string to_hex(const uint8_t data[], const size_t length);
+ static std::string to_ascii(const uint8_t data[], const size_t length);
static void signal_to_bits_bytes(unsigned int bit_position,
unsigned int bit_size,
int &new_start_byte,