Add gitlab issue/merge request templates
[apps/agl-service-can-low-level.git] / low-can-binding / utils / openxc-utils.cpp
index 3b404f0..68c8a65 100644 (file)
  */
 
 #include "openxc-utils.hpp"
-
+#include "converter.hpp"
 #include "../binding/application.hpp"
 
+
 ///
 /// @brief Build a specific VehicleMessage containing a DiagnosticResponse.
 ///
@@ -34,6 +35,7 @@
 const openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* request, const DiagnosticResponse& response, float parsed_value)
 {
        openxc_VehicleMessage message;
+       ::memset(&message, 0, sizeof(message));
        application_t& app = application_t::instance();
 
        message.has_type = true;
@@ -98,6 +100,7 @@ const openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* re
 const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message, uint64_t timestamp)
 {
        openxc_VehicleMessage v;
+       ::memset(&v, 0, sizeof(v));
 
        v.has_type = true,
        v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
@@ -120,6 +123,7 @@ const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& mes
 const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message)
 {
        openxc_VehicleMessage v;
+       ::memset(&v, 0, sizeof(v));
 
        v.has_type = true,
        v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
@@ -141,7 +145,7 @@ openxc_VehicleMessage build_VehicleMessage()
 {
        openxc_VehicleMessage v;
 
-       ::memset(&v, 0, sizeof(openxc_VehicleMessage));
+       ::memset(&v, 0, sizeof(v));
        return v;
 }
 
@@ -181,6 +185,63 @@ const openxc_SimpleMessage build_SimpleMessage(const std::string& name, const op
        return s;
 }
 
+/// @brief Build an openxc_DynamicField with a json object
+///
+/// @param[in] value - const json_object pointer to assign to convert in an
+///  openxc_DynamicField.
+///
+/// @return openxc_DynamicField initialized with a json object.
+///
+const openxc_DynamicField build_DynamicField(json_object* value)
+{
+       switch(json_object_get_type(value))
+       {
+               case json_type_string:
+                       return build_DynamicField(json_object_get_string(value));
+               case json_type_double:
+                       return build_DynamicField(json_object_get_double(value));
+               case json_type_int:
+                       return build_DynamicField(json_object_get_double(value));
+               case json_type_boolean:
+                       return build_DynamicField((bool)json_object_get_boolean(value));
+               default:
+                       openxc_DynamicField d;
+                       ::memset(&d, 0, sizeof(openxc_DynamicField));
+                       return d;
+       }
+}
+
+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
 ///
@@ -198,6 +259,8 @@ 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;
+       d.has_json_value = false;
        ::strncpy(d.string_value, value, 100);
 
        return d;
@@ -220,6 +283,8 @@ 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;
+       d.has_json_value = false;
        ::strncpy(d.string_value, value.c_str(), 100);
 
        return d;
@@ -243,6 +308,8 @@ 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.has_json_value = false;
        d.numeric_value = value;
 
        return d;
@@ -264,11 +331,36 @@ 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.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;
+}
+
 int get_bool_from_DynamicField(const openxc_VehicleMessage& v_msg, bool* ret)
 {
        if(v_msg.has_simple_message && v_msg.simple_message.has_value && v_msg.simple_message.value.has_boolean_value)
@@ -326,6 +418,10 @@ void jsonify_DynamicField(const openxc_DynamicField& field, json_object* 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)
+               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_get(field.json_value));
 }
 
 ///
@@ -349,3 +445,31 @@ bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json)
        json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'"));
        return false;
 }
+
+///
+/// @brief Make a JSON object from a VehicleMessage
+///
+/// @param[in] v_msg - const reference to an openxc_VehicleMessage
+/// struct to convert into a json object.
+/// @param[in] sig - signal reference to the subscription of openxc_VehicleMessage,
+/// to get more informations about it
+/// @param[out] json - pointer with the DynamicField converted into json object
+///
+/// @return True if VehicleMessage has been transformed into json object
+///  and false if not. In such case, a json object is returned { "error": "error msg"}
+///
+bool jsonify_vehicle(const openxc_VehicleMessage& v_msg, std::shared_ptr<signal_t> sig, json_object* json)
+{
+       if(jsonify_simple(get_simple_message(v_msg), json))
+       {
+               if(sig != nullptr && sig->get_unit() != "")
+                       json_object_object_add(json, "unit", json_object_new_string(sig->get_unit().c_str()));
+
+               if(v_msg.has_timestamp)
+                       json_object_object_add(json, "timestamp", json_object_new_int64(v_msg.timestamp));
+
+               return true;
+       }
+       json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'"));
+       return false;
+}