X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Futils%2Fopenxc-utils.cpp;h=c06beabb6ef39f2a9f5190a8bddc969038189394;hb=3d8c0459bcfde576581246b6d2e0e0d5595ac638;hp=3b404f0905e7577790534fe3124a132f9aec9c7a;hpb=e2af4a871c6a8bcab4733193948833aed9c5ceb8;p=apps%2Fagl-service-can-low-level.git diff --git a/low-can-binding/utils/openxc-utils.cpp b/low-can-binding/utils/openxc-utils.cpp index 3b404f09..c06beabb 100644 --- a/low-can-binding/utils/openxc-utils.cpp +++ b/low-can-binding/utils/openxc-utils.cpp @@ -34,6 +34,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 +99,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 +122,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 +144,7 @@ openxc_VehicleMessage build_VehicleMessage() { openxc_VehicleMessage v; - ::memset(&v, 0, sizeof(openxc_VehicleMessage)); + ::memset(&v, 0, sizeof(v)); return v; } @@ -181,6 +184,32 @@ 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; + } +} + /// /// @brief Build an openxc_DynamicField with a string value /// @@ -349,3 +378,28 @@ 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[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, json_object* json) +{ + if(jsonify_simple(get_simple_message(v_msg), json)) + { + if(v_msg.has_timestamp) + { + json_object_object_add(json, "timestamp", json_object_new_int64(v_msg.timestamp)); + return true; + } + return true; + } + json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'")); + return false; +}