aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur Guyader <arthur.guyader@iot.bzh>2019-12-16 16:54:07 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2020-01-09 16:25:36 +0100
commite28ae5f481c3a0d78bb7d6c21416dc38be541a4d (patch)
tree1ef30b4274ba82cdc585d7f81bbb0d1f8c258a8b
parent8c1df02d734676f79807e3a4682fb96ea88c5cb5 (diff)
openxc-utils : Add json value for DynamicField
Change-Id: I93d31768804d4e30994897487e9fd312a56c547b 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.h6
-rw-r--r--low-can-binding/utils/openxc-utils.cpp40
-rw-r--r--low-can-binding/utils/openxc-utils.hpp1
3 files changed, 35 insertions, 12 deletions
diff --git a/libs/openxc-message-format/gen/cpp/openxc.pb.h b/libs/openxc-message-format/gen/cpp/openxc.pb.h
index 30b0818d..8b1cb19a 100644
--- a/libs/openxc-message-format/gen/cpp/openxc.pb.h
+++ b/libs/openxc-message-format/gen/cpp/openxc.pb.h
@@ -4,6 +4,7 @@
#ifndef PB_OPENXC_PB_H_INCLUDED
#define PB_OPENXC_PB_H_INCLUDED
#include <pb.h>
+#include <json-c/json.h>
#if PB_PROTO_HEADER_VERSION != 30
#error Regenerate this file with the current version of nanopb generator.
@@ -76,7 +77,8 @@ typedef enum _openxc_DynamicField_Type {
openxc_DynamicField_Type_STRING = 1,
openxc_DynamicField_Type_NUM = 2,
openxc_DynamicField_Type_BOOL = 3,
- openxc_DynamicField_Type_BYTES = 4
+ openxc_DynamicField_Type_BYTES = 4,
+ openxc_DynamicField_Type_JSON = 5
} openxc_DynamicField_Type;
/* Struct definitions */
@@ -169,6 +171,8 @@ typedef struct _openxc_DynamicField {
uint8_t bytes_value[MAX_ISOTP_BYTES];
uint32_t length_array;
bool has_bytes_value;
+ json_object* json_value;
+ bool has_json_value;
} openxc_DynamicField;
typedef struct _openxc_NetworkOperatorSettings_NetworkDescriptor {
diff --git a/low-can-binding/utils/openxc-utils.cpp b/low-can-binding/utils/openxc-utils.cpp
index 5ca70b88..8d331003 100644
--- a/low-can-binding/utils/openxc-utils.cpp
+++ b/low-can-binding/utils/openxc-utils.cpp
@@ -235,7 +235,6 @@ const openxc_DynamicField build_DynamicField(std::vector<uint8_t> &array)
d.length_array = (uint32_t) size;
}
-
for(int i=0;i<size;i++)
d.bytes_value[i] = array[i];
@@ -261,6 +260,7 @@ const openxc_DynamicField build_DynamicField(const char* value)
d.has_numeric_value = false;
d.has_boolean_value = false;
d.has_bytes_value = false;
+ d.has_json_value = false;
::strncpy(d.string_value, value, 100);
return d;
@@ -284,6 +284,7 @@ const openxc_DynamicField build_DynamicField(const std::string& value)
d.has_numeric_value = false;
d.has_boolean_value = false;
d.has_bytes_value = false;
+ d.has_json_value = false;
::strncpy(d.string_value, value.c_str(), 100);
return d;
@@ -308,6 +309,7 @@ const openxc_DynamicField build_DynamicField(double value)
d.has_numeric_value = true;
d.has_boolean_value = false;
d.has_bytes_value = false;
+ d.has_json_value = false;
d.numeric_value = value;
return d;
@@ -330,8 +332,31 @@ const openxc_DynamicField build_DynamicField(bool value)
d.has_numeric_value = false;
d.has_boolean_value = true;
d.has_bytes_value = false;
+ d.has_json_value = false;
d.boolean_value = value;
+ return d;
+}
+
+///
+/// @brief Build an openxc_DynamicField with a json value
+///
+/// @param[in] value - json value to assign to builded openxc_DynamicField.
+///
+/// @return openxc_DynamicField initialized with a json value.
+///
+const openxc_DynamicField build_DynamicField_json(json_object *value)
+{
+ openxc_DynamicField d;
+ d.has_type = true;
+ d.type = openxc_DynamicField_Type_JSON;
+
+ d.has_string_value = false;
+ d.has_numeric_value = false;
+ d.has_boolean_value = false;
+ d.has_bytes_value = false;
+ d.has_json_value = true;
+ d.json_value = value;
return d;
}
@@ -388,22 +413,15 @@ 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()));
- }
+ json_object_object_add(value, "value", json_object_new_string(converter_t::to_hex(field.bytes_value, field.length_array).c_str()));
+ else if(field.has_json_value)
+ json_object_object_add(value, "signals", json_object_new_string(json_object_get_string(field.json_value)));
}
///
diff --git a/low-can-binding/utils/openxc-utils.hpp b/low-can-binding/utils/openxc-utils.hpp
index 675dc2f1..cf04d220 100644
--- a/low-can-binding/utils/openxc-utils.hpp
+++ b/low-can-binding/utils/openxc-utils.hpp
@@ -39,6 +39,7 @@ 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);
+const openxc_DynamicField build_DynamicField_json(json_object *value);
int get_bool_from_DynamicField(const openxc_VehicleMessage& v_msg, bool& ret);
double get_numerical_from_DynamicField(const openxc_VehicleMessage& v_msg);