X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Futils%2Fopenxc-utils.cpp;h=5ca70b88f58d371770983d39ecb99266b07319e7;hb=05e0dcd47f5cbe62de594c801f1c460622847716;hp=52b49d290615338d1367d5b8de079feebd3a8f0b;hpb=9e444ade872bc436cf12bc12d03c3a5d51ac0b9e;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..5ca70b88 100644 --- a/low-can-binding/utils/openxc-utils.cpp +++ b/low-can-binding/utils/openxc-utils.cpp @@ -17,8 +17,9 @@ */ #include "openxc-utils.hpp" +#include "converter.hpp" +#include "../binding/application.hpp" -#include "../configuration.hpp" /// /// @brief Build a specific VehicleMessage containing a DiagnosticResponse. @@ -28,18 +29,21 @@ /// 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; + ::memset(&message, 0, sizeof(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 +88,49 @@ 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; + ::memset(&v, 0, sizeof(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; - + ::memset(&v, 0, sizeof(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; } @@ -117,7 +145,7 @@ openxc_VehicleMessage build_VehicleMessage() { openxc_VehicleMessage v; - ::memset(&v, 0, sizeof(openxc_VehicleMessage)); + ::memset(&v, 0, sizeof(v)); return v; } @@ -138,14 +166,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 +185,87 @@ 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; + } +} + +const openxc_DynamicField build_DynamicField(std::vector &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 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; +}