X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Futils%2Fopenxc-utils.cpp;h=e454121b6fb92d338bc789c9b86705b0b08fbd09;hb=2daa033bd69b9ede497afa4355a5fdbb119895eb;hp=52b49d290615338d1367d5b8de079feebd3a8f0b;hpb=a58d40b5ae336a54408201963b065ee049b43acd;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 52b49d29..e454121b 100644 --- a/low-can-binding/utils/openxc-utils.cpp +++ b/low-can-binding/utils/openxc-utils.cpp @@ -18,7 +18,7 @@ #include "openxc-utils.hpp" -#include "../configuration.hpp" +#include "../binding/application.hpp" /// /// @brief Build a specific VehicleMessage containing a DiagnosticResponse. @@ -28,18 +28,20 @@ /// and put into the DiagnosticResponse of the VehicleMessage. /// @param[in] parsed_value - raw parsed value of the payload from CAN message /// -/// @return a vehicle message including simple message that will be convert into +/// @return a vehicle message including simple message that will be convert into /// a JSON object before being pushed to the subscribers /// -openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* request, const DiagnosticResponse& response, float parsed_value) +const openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* request, const DiagnosticResponse& response, float parsed_value) { openxc_VehicleMessage message; + application_t& app = application_t::instance(); message.has_type = true; message.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_DIAGNOSTIC; message.has_diagnostic_response = true; message.diagnostic_response.has_bus = true; - message.diagnostic_response.bus = configuration_t::instance().get_diagnostic_manager().get_can_bus_dev()->get_address(); + message.diagnostic_response.bus = app.get_can_bus_manager().get_can_device_index( + app.get_diagnostic_manager().get_bus_name()); message.diagnostic_response.has_message_id = true; if(request->get_id() != OBD2_FUNCTIONAL_BROADCAST_ID) @@ -84,25 +86,47 @@ openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* request, return message; } +/// +/// @brief Build a specific VehicleMessage containing a SimpleMessage with associated timestamp +/// +/// @param[in] message - simple message to include into openxc_VehicleMessage +/// @param[in] timestamp - timestamp from ioctl when reading the socket +/// +/// @return a vehicle message including simple message that will be convert into +/// a JSON object before being pushed to the subscribers +/// +const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message, uint64_t timestamp) +{ + openxc_VehicleMessage v; + + v.has_type = true, + v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE; + v.has_simple_message = true; + v.simple_message = message; + v.has_timestamp = true; + v.timestamp = timestamp; + + return v; +} /// /// @brief Build a specific VehicleMessage containing a SimpleMessage. /// /// @param[in] message - simple message to include into openxc_VehicleMessage /// -/// @return a vehicle message including simple message that will be convert into +/// @return a vehicle message including simple message that will be convert into /// a JSON object before being pushed to the subscribers /// -openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message) +const openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message) { openxc_VehicleMessage v; - + v.has_type = true, v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE; v.has_simple_message = true; v.simple_message = message; v.has_timestamp = true; - v.timestamp = system_time_ms(); + v.timestamp = system_time_us(); return v; } @@ -138,14 +162,14 @@ bool is_valid(const openxc_VehicleMessage& v) /// @brief Build an openxc_SimpleMessage associating a name to an openxc_DynamicField /// /// @param[in] name - const string reference name to assign to the created SimpleMessage -/// this will set has_name member to true and assign name to the name member. Maximum size for name is +/// this will set has_name member to true and assign name to the name member. Maximum size for name is /// set to 100 char. /// @param[in] value - const reference with DynamicField to assign to SimpleMessage /// value. /// /// @return an openxc_SimpleMessage struct initialized with name and value provided. /// -openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value) +const openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value) { openxc_SimpleMessage s; @@ -157,6 +181,32 @@ openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_D 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 /// @@ -165,17 +215,39 @@ openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_D /// /// @return openxc_DynamicField initialized with a string value. /// -openxc_DynamicField build_DynamicField(const std::string& value) +const openxc_DynamicField build_DynamicField(const char* value) { openxc_DynamicField d; d.has_type = true; d.type = openxc_DynamicField_Type_STRING; - + + d.has_string_value = true; + d.has_numeric_value = false; + d.has_boolean_value = false; + ::strncpy(d.string_value, value, 100); + + return d; +} + +/// +/// @brief Build an openxc_DynamicField with a string value +/// +/// @param[in] value - const string reference value to assign to builded +/// openxc_DynamicField. +/// +/// @return openxc_DynamicField initialized with a string value. +/// +const openxc_DynamicField build_DynamicField(const std::string& value) +{ + openxc_DynamicField d; + d.has_type = true; + d.type = openxc_DynamicField_Type_STRING; + d.has_string_value = true; d.has_numeric_value = false; d.has_boolean_value = false; ::strncpy(d.string_value, value.c_str(), 100); - + return d; } @@ -188,17 +260,17 @@ openxc_DynamicField build_DynamicField(const std::string& value) /// /// @return openxc_DynamicField initialized with a double value. /// -openxc_DynamicField build_DynamicField(double value) +const openxc_DynamicField build_DynamicField(double value) { openxc_DynamicField d; d.has_type = true; d.type = openxc_DynamicField_Type_NUM; - + d.has_string_value = false; d.has_numeric_value = true; d.has_boolean_value = false; d.numeric_value = value; - + return d; } @@ -209,20 +281,43 @@ openxc_DynamicField build_DynamicField(double value) /// /// @return openxc_DynamicField initialized with a boolean value. /// -openxc_DynamicField build_DynamicField(bool value) +const openxc_DynamicField build_DynamicField(bool value) { openxc_DynamicField d; d.has_type = true; d.type = openxc_DynamicField_Type_BOOL; - + d.has_string_value = false; d.has_numeric_value = false; d.has_boolean_value = true; d.boolean_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) + { + *ret = v_msg.simple_message.value.boolean_value; + return 0; + } + + return -1; +} + +double get_numerical_from_DynamicField(const openxc_VehicleMessage& v_msg) +{ + return (v_msg.has_simple_message && v_msg.simple_message.has_value && v_msg.simple_message.value.has_numeric_value) ? + v_msg.simple_message.value.numeric_value : -1.0; +} + +const std::string get_string_from_DynamicField(const openxc_VehicleMessage& v_msg) +{ +return (v_msg.has_simple_message && v_msg.simple_message.has_value && v_msg.simple_message.value.has_string_value) ? + v_msg.simple_message.value.string_value : ""; +} + /// /// @brief Extract the simple message value from an openxc_VehicleMessage /// and return it. If there isn't SimpleMessage in the VehicleMessage then @@ -233,11 +328,11 @@ openxc_DynamicField build_DynamicField(bool value) /// /// @return A simpleMessage from the provided VehicleMessage. /// -openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg) +const openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg) { if (v_msg.has_simple_message) return v_msg.simple_message; - + openxc_SimpleMessage s_msg = { false, "", false, build_DynamicField(false), false, build_DynamicField(false)}; return s_msg; } @@ -262,10 +357,10 @@ void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value) /// /// @brief Make a JSON object from a SimpleMessage /// -/// @param[in] s_msg - const reference to an openxc_SimpleMessage +/// @param[in] s_msg - const reference to an openxc_SimpleMessage /// struct to convert into a json object. /// @param[out] json - pointer with the DynamicField converted into json object -/// +/// /// @return True if SimpleMessage has been transformed into json object /// and false if not. In such case, a json object is returned { "error": "error msg"} /// @@ -279,4 +374,29 @@ 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; -} \ No newline at end of file +} + +/// +/// @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; +}