X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Fbinding%2Flow-can-cb.cpp;h=3cb61c6c1a9ac2b01754d5cb607e7009a9469aeb;hb=99682db67048b828705c9afe14d53b8305a07392;hp=76520f45dd3cc5e6415d67c629ee3b3173f4da9c;hpb=438bf4912a1174c63fac77fe72e3b320c589f431;p=apps%2Fagl-service-can-low-level.git diff --git a/low-can-binding/binding/low-can-cb.cpp b/low-can-binding/binding/low-can-cb.cpp index 76520f45..3cb61c6c 100644 --- a/low-can-binding/binding/low-can-cb.cpp +++ b/low-can-binding/binding/low-can-cb.cpp @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include "openxc.pb.h" @@ -109,13 +109,13 @@ int read_message(sd_event_source *event_source, int fd, uint32_t revents, void * /// ///*******************************************************************************/ -static int make_subscription_unsubscription(struct afb_req request, - std::shared_ptr& can_subscription, - std::map >& s, - bool subscribe) +static int make_subscription_unsubscription(afb_req_t request, + std::shared_ptr& can_subscription, + std::map >& s, + bool subscribe) { - /* Make the subscription or unsubscription to the event (if request contents are not null) */ - if(request.itf && request.closure && + /* Make the subscription or unsubscription to the event (if request is not null) */ + if(request && ((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s[can_subscription->get_index()]->get_event())) < 0) { AFB_ERROR("Operation goes wrong for signal: %s", can_subscription->get_name().c_str()); @@ -125,7 +125,7 @@ static int make_subscription_unsubscription(struct afb_req request, } static int create_event_handle(std::shared_ptr& can_subscription, - std::map >& s) + std::map >& s) { int sub_index = can_subscription->get_index(); can_subscription->set_event(afb_daemon_make_event(can_subscription->get_name().c_str())); @@ -138,13 +138,13 @@ static int create_event_handle(std::shared_ptr& can_subs return 0; } -/// @brief Will determine if it is needed or not to create the event handle and checks it to be sure that -/// we got a valid afb_event to get subscribe or unsubscribe. Then launch the subscription or unsubscription +/// @brief This will determine if an event handle needs to be created and checks if +/// we got a valid afb_event to get subscribe or unsubscribe. After that launch the subscription or unsubscription /// against the application framework using that event handle. -static int subscribe_unsubscribe_signal(struct afb_req request, - bool subscribe, - std::shared_ptr& can_subscription, - std::map >& s) +static int subscribe_unsubscribe_signal(afb_req_t request, + bool subscribe, + std::shared_ptr& can_subscription, + std::map >& s) { int ret = -1; int sub_index = can_subscription->get_index(); @@ -161,13 +161,12 @@ static int subscribe_unsubscribe_signal(struct afb_req request, else { /* Event doesn't exist , so let's create it */ - can_subscription->set_event({nullptr, nullptr}); s[sub_index] = can_subscription; ret = create_event_handle(can_subscription, s); } - // Check whether or not the event handler has been correctly created and - // make the subscription/unsubscription operation is so. + // Checks if the event handler is correctly created, if it is, it + // performs the subscription or unsubscription operations. if (ret < 0) return ret; return make_subscription_unsubscription(request, can_subscription, s, subscribe); @@ -184,12 +183,12 @@ static int add_to_event_loop(std::shared_ptr& can_subscr can_subscription.get())); } -static int subscribe_unsubscribe_diagnostic_messages(struct afb_req request, - bool subscribe, - std::vector > diagnostic_messages, - struct event_filter_t& event_filter, - std::map >& s, - bool perm_rec_diag_req) +static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request, + bool subscribe, + std::vector > diagnostic_messages, + struct event_filter_t& event_filter, + std::map >& s, + bool perm_rec_diag_req) { int rets = 0; application_t& app = application_t::instance(); @@ -205,8 +204,8 @@ static int subscribe_unsubscribe_diagnostic_messages(struct afb_req request, can_subscription = it != s.end() ? it->second : std::make_shared(low_can_subscription_t(event_filter)); - // If the requested diagnostic message isn't supported by the car then unsubcribe it - // no matter what we want, worse case will be a fail unsubscription but at least we don't + // If the requested diagnostic message is not supported by the car then unsubcribe it + // no matter what we want, worst case will be a failed unsubscription but at least we won't // poll a PID for nothing. if(sig->get_supported() && subscribe) { @@ -245,11 +244,11 @@ static int subscribe_unsubscribe_diagnostic_messages(struct afb_req request, return rets; } -static int subscribe_unsubscribe_can_signals(struct afb_req request, - bool subscribe, - std::vector > can_signals, - struct event_filter_t& event_filter, - std::map >& s) +static int subscribe_unsubscribe_can_signals(afb_req_t request, + bool subscribe, + std::vector > can_signals, + struct event_filter_t& event_filter, + std::map >& s) { int rets = 0; for(const auto& sig: can_signals) @@ -281,16 +280,16 @@ static int subscribe_unsubscribe_can_signals(struct afb_req request, /// /// @brief subscribe to all signals in the vector signals /// -/// @param[in] afb_req request : contain original request use to subscribe or unsubscribe -/// @param[in] subscribe boolean value used to chose between a subscription operation or an unsubscription +/// @param[in] afb_req request : contains original request use to subscribe or unsubscribe +/// @param[in] subscribe boolean value, which chooses between a subscription operation or an unsubscription /// @param[in] signals - struct containing vectors with can_signal_t and diagnostic_messages to subscribe /// /// @return Number of correctly subscribed signal /// -static int subscribe_unsubscribe_signals(struct afb_req request, - bool subscribe, - const struct utils::signals_found& signals, - struct event_filter_t& event_filter) +static int subscribe_unsubscribe_signals(afb_req_t request, + bool subscribe, + const struct utils::signals_found& signals, + struct event_filter_t& event_filter) { int rets = 0; utils::signals_manager_t& sm = utils::signals_manager_t::instance(); @@ -304,10 +303,10 @@ static int subscribe_unsubscribe_signals(struct afb_req request, return rets; } -static int one_subscribe_unsubscribe(struct afb_req request, - bool subscribe, - const std::string& tag, - json_object* args) +static int one_subscribe_unsubscribe(afb_req_t request, + bool subscribe, + const std::string& tag, + json_object* args) { int ret = 0; struct event_filter_t event_filter; @@ -341,7 +340,7 @@ static int one_subscribe_unsubscribe(struct afb_req request, return ret; } -static int process_one_subscribe_args(struct afb_req request, bool subscribe, json_object *args) +static int process_one_subscribe_args(afb_req_t request, bool subscribe, json_object *args) { int rc = 0, rc2=0; json_object *x = nullptr, *event = nullptr; @@ -367,7 +366,7 @@ static int process_one_subscribe_args(struct afb_req request, bool subscribe, js return rc; } -static void do_subscribe_unsubscribe(struct afb_req request, bool subscribe) +static void do_subscribe_unsubscribe(afb_req_t request, bool subscribe) { int rc = 0; struct json_object *args, *x; @@ -392,24 +391,28 @@ static void do_subscribe_unsubscribe(struct afb_req request, bool subscribe) afb_req_fail(request, "error", NULL); } -void auth(struct afb_req request) +void auth(afb_req_t request) { afb_req_session_set_LOA(request, 1); afb_req_success(request, NULL, NULL); } -void subscribe(struct afb_req request) +void subscribe(afb_req_t request) { do_subscribe_unsubscribe(request, true); } -void unsubscribe(struct afb_req request) +void unsubscribe(afb_req_t request) { do_subscribe_unsubscribe(request, false); } static int send_frame(const std::string& bus_name, const struct can_frame& cf) { + if(bus_name.empty()) { + return -1; + } + std::map >& cd = application_t::instance().get_can_devices(); if( cd.count(bus_name) == 0) @@ -418,123 +421,104 @@ static int send_frame(const std::string& bus_name, const struct can_frame& cf) return cd[bus_name]->tx_send(cf, bus_name); } -static int write_raw_frame(const std::string& bus_name, uint32_t can_id, uint8_t can_dlc, struct json_object* can_data) +static void write_raw_frame(afb_req_t request, const std::string& bus_name, json_object *json_value) { - int rc = 0; struct can_frame cf; + struct json_object *json_can_data = nullptr; ::memset(&cf, 0, sizeof(cf)); - cf.can_id = can_id; - cf.can_dlc = can_dlc; - - struct json_object *x; - size_t n = json_object_array_length(can_data); - if(n <= 8) + if(! wrap_json_unpack(json_value, "{sF, sF, so !}", + "can_id", &cf.can_id, + "can_dlc", &cf.can_dlc, + "can_data", &json_can_data)) { - for (int i = 0 ; i < n ; i++) + struct json_object *one_can_data; + size_t n = json_object_array_length(json_can_data); + + if(n <= 8 && n > 0) + { + for (int i = 0 ; i < n ; i++) + { + one_can_data = json_object_array_get_idx(json_can_data, i); + cf.data[i] = json_object_get_type(one_can_data) == json_type_int ? (uint8_t)json_object_get_int(one_can_data) : 0; + } + } + else { - x = json_object_array_get_idx(can_data, i); - cf.data[i] = json_object_get_type(x) == json_type_int ? (uint8_t)json_object_get_int(x) : 0; + afb_req_fail(request, "Error", "Data array must hold 1 to 8 values."); + return; } - } - const std::string found_device = application_t::instance().get_can_bus_manager().get_can_device_name(bus_name); - if( ! found_device.empty()) - { - rc = send_frame(found_device, cf); + if(! send_frame(application_t::instance().get_can_bus_manager().get_can_device_name(bus_name), cf)) + afb_req_success(request, nullptr, "Message correctly sent"); + else + afb_req_fail(request, "Error", "sending the message. See the log for more details."); + + return; } - return rc; + afb_req_fail(request, "Error", "Frame object malformed (must be \n \"frame\": {\"can_id\": int, \"can_dlc\": int, \"can_data\": [ int, int , int, int ,int , int ,int ,int]}"); } -static int write_signal(const std::string& name, uint64_t value) + +static void write_signal(afb_req_t request, const std::string& name, json_object *json_value) { - int rc = 0; struct can_frame cf; struct utils::signals_found sf; + signal_encoder encoder = nullptr; + bool send = true; ::memset(&cf, 0, sizeof(cf)); openxc_DynamicField search_key = build_DynamicField(name); sf = utils::signals_manager_t::instance().find_signals(search_key); + openxc_DynamicField dynafield_value = build_DynamicField(json_value); if (sf.can_signals.empty()) { - AFB_WARNING("No signal(s) found for %s. Message not sent.", name.c_str()); - rc = -1; + afb_req_fail_f(request, "No signal(s) found for %s. Message not sent.", name.c_str()); + return; } - else + + std::shared_ptr& sig = sf.can_signals[0]; + if(! sig->get_writable()) { - for(const auto& sig: sf.can_signals) - { - if(sig->get_writable()) - { - cf = encoder_t::build_frame(sig, value); - const std::string bus_name = sig->get_message()->get_bus_device_name(); - rc = send_frame(bus_name, cf); - } - else - { - AFB_WARNING("%s isn't writable. Message not sent.", sig->get_name().c_str()); - return -1; - } - } + afb_req_fail_f(request, "%s isn't writable. Message not sent.", sig->get_name().c_str()); + return; } - return rc; + uint64_t value = (encoder = sig->get_encoder()) ? + encoder(*sig, dynafield_value, &send) : + encoder_t::encode_DynamicField(*sig, dynafield_value, &send); + + if(! send_frame(sig->get_message()->get_bus_device_name(), encoder_t::build_frame(sig, value)) && + send) + afb_req_success(request, nullptr, "Message correctly sent"); + else + afb_req_fail(request, "Error", "Sending the message. See the log for more details."); } -void write(struct afb_req request) +void write(afb_req_t request) { - int rc = 0; - struct json_object* args = nullptr, - *json_name = nullptr, - *json_value = nullptr; + struct json_object* args = nullptr, *json_value = nullptr; + const char *name = nullptr; args = afb_req_json(request); // Process about Raw CAN message on CAN bus directly - if (args != NULL && - (json_object_object_get_ex(args, "bus_name", &json_name) && json_object_is_type(json_name, json_type_string) ) && - (json_object_object_get_ex(args, "frame", &json_value) && json_object_is_type(json_value, json_type_object) )) - { - struct json_object* json_can_id = nullptr, - *json_can_dlc = nullptr, - *json_can_data = nullptr; + if (args != NULL && ! wrap_json_unpack(args, "{ss, so !}", + "bus_name", &name, + "frame", &json_value)) + write_raw_frame(request, name, json_value); - if( (json_object_object_get_ex(json_value, "can_id", &json_can_id) && (json_object_is_type(json_can_id, json_type_double) || json_object_is_type(json_can_id, json_type_int))) && - (json_object_object_get_ex(json_value, "can_dlc", &json_can_dlc) && (json_object_is_type(json_can_dlc, json_type_double) || json_object_is_type(json_can_dlc, json_type_int))) && - (json_object_object_get_ex(json_value, "can_data", &json_can_data) && json_object_is_type(json_can_data, json_type_array) )) - { - rc = write_raw_frame(json_object_get_string(json_name), - json_object_get_int(json_can_id), - (uint8_t)json_object_get_int(json_can_dlc), - json_can_data); - } - else - { - AFB_ERROR("Frame object malformed (must be \n \"frame\": {\"can_id\": int, \"can_dlc\": int, \"can_data\": [ int, int , int, int ,int , int ,int ,int]}"); - rc = -1; - } - } // Search signal then encode value. else if(args != NULL && - (json_object_object_get_ex(args, "signal_name", &json_name) && json_object_is_type(json_name, json_type_string)) && - (json_object_object_get_ex(args, "signal_value", &json_value) && (json_object_is_type(json_value, json_type_double) || json_object_is_type(json_value, json_type_int)))) - { - rc = write_signal(json_object_get_string(json_name), - (uint64_t)json_object_get_double(json_value)); - } + ! wrap_json_unpack(args, "{ss, so !}", + "signal_name", &name, + "signal_value", &json_value)) + write_signal(request, std::string(name), json_value); else - { - AFB_ERROR("Request argument malformed. Please use the following syntax:"); - rc = -1; - } - - if (rc >= 0) - afb_req_success(request, NULL, NULL); - else - afb_req_fail(request, "error", NULL); + afb_req_fail(request, "Error", "Request argument malformed"); } static struct json_object *get_signals_value(const std::string& name) @@ -561,7 +545,7 @@ static struct json_object *get_signals_value(const std::string& name) return ans; } -void get(struct afb_req request) +void get(afb_req_t request) { int rc = 0; struct json_object* args = nullptr, @@ -619,7 +603,7 @@ static struct json_object *list_can_message(const std::string& name) return ans; } -void list(struct afb_req request) +void list(afb_req_t request) { int rc = 0; json_object *ans = nullptr; @@ -652,7 +636,7 @@ void list(struct afb_req request) /// @param[in] service Structure which represent the Application Framework Binder. /// /// @return Exit code, zero if success. -int initv2() +int init_binding(afb_api_t api) { uint32_t ret = 1; can_bus_t& can_bus_manager = application_t::instance().get_can_bus_manager(); @@ -666,15 +650,13 @@ int initv2() if(application_t::instance().get_diagnostic_manager().initialize()) ret = 0; - // Add a recurring dignostic message request to get engine speed continuously + // Add a recurring dignostic message request to get engine speed at all times. openxc_DynamicField search_key = build_DynamicField("diagnostic_messages.engine.speed"); struct utils::signals_found sf = utils::signals_manager_t::instance().find_signals(search_key); if(sf.can_signals.empty() && sf.diagnostic_messages.size() == 1) { - struct afb_req request; - request.itf = nullptr; - request.closure = nullptr; + afb_req_t request = nullptr; struct event_filter_t event_filter; event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();