X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Fbinding%2Flow-can-cb.cpp;h=f200b192ea5ed1fc963218c69f59e5a8e275b237;hb=97a08a7e37835f0e8619d22c83243bee26a2bff2;hp=c5a0138ec1305b54eb650f794577981a4c9733e1;hpb=1b3655d85cfb9cc143c9e7f0d3506e6b004f835e;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 c5a0138e..f200b192 100644 --- a/low-can-binding/binding/low-can-cb.cpp +++ b/low-can-binding/binding/low-can-cb.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015, 2016 "IoT.bzh" + * Copyright (C) 2015, 2018 "IoT.bzh" * Author "Romain Forlot" * Author "Loic Collignon" * @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include "openxc.pb.h" @@ -109,35 +109,6 @@ int read_message(sd_event_source *event_source, int fd, uint32_t revents, void * /// ///*******************************************************************************/ -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 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()); - return -1; - } - return 0; -} - -static int create_event_handle(std::shared_ptr& can_subscription, - std::map >& s) -{ - int sub_index = can_subscription->get_index(); - can_subscription->set_event(afb_daemon_make_event(can_subscription->get_name().c_str())); - s[sub_index] = can_subscription; - if (!afb_event_is_valid(s[sub_index]->get_event())) - { - AFB_ERROR("Can't create an event for %s, something goes wrong.", can_subscription->get_name().c_str()); - return -1; - } - return 0; -} - /// @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. @@ -146,30 +117,52 @@ static int subscribe_unsubscribe_signal(afb_req_t request, std::shared_ptr& can_subscription, std::map >& s) { - int ret = -1; + int ret = 0; int sub_index = can_subscription->get_index(); + bool subscription_exists = s.count(sub_index); - if (can_subscription && s.find(sub_index) != s.end()) + // Susbcription part + if(subscribe) { - if (!afb_event_is_valid(s[sub_index]->get_event()) && !subscribe) + /* There is no valid request to subscribe so this must be an + * internal permanent diagnostic request. Skip the subscription + * part and don't register it into the current "low-can" + * subsciptions. + */ + if(! request) { - AFB_NOTICE("Event isn't valid, no need to unsubscribed."); - ret = -1; + return 0; } - ret = 0; + + // Event doesn't exist , so let's create it + if (! subscription_exists && + (ret = can_subscription->subscribe(request)) < 0) + return ret; + + if(! subscription_exists) + s[sub_index] = can_subscription; + + return ret; } - else + + // Unsubscrition part + if(! subscription_exists) + { + AFB_NOTICE("There isn't any valid subscriptions for that request."); + return ret; + } + else if (subscription_exists && + ! afb_event_is_valid(s[sub_index]->get_event()) ) { - /* Event doesn't exist , so let's create it */ - s[sub_index] = can_subscription; - ret = create_event_handle(can_subscription, s); + AFB_NOTICE("Event isn't valid, no need to unsubscribed."); + return ret; } - // Checks if the event handler is correctly created, if it is, it - // performs the subscription or unsubscription operations. - if (ret < 0) + if( (ret = s[sub_index]->unsubscribe(request)) < 0) return ret; - return make_subscription_unsubscription(request, can_subscription, s, subscribe); + s.erase(sub_index); + + return ret; } static int add_to_event_loop(std::shared_ptr& can_subscription) @@ -256,9 +249,7 @@ static int subscribe_unsubscribe_can_signals(afb_req_t request, auto it = std::find_if(s.begin(), s.end(), [&sig, &event_filter](std::pair > sub){ return sub.second->is_signal_subscription_corresponding(sig, event_filter) ; }); std::shared_ptr can_subscription; if(it != s.end()) - { - can_subscription = it->second; - } + {can_subscription = it->second;} else { can_subscription = std::make_shared(low_can_subscription_t(event_filter)); @@ -272,7 +263,7 @@ static int subscribe_unsubscribe_can_signals(afb_req_t request, {return -1;} rets++; - AFB_DEBUG("signal: %s subscribed", sig->get_name().c_str()); + AFB_DEBUG("%s Signal: %s %ssubscribed", sig->get_message()->is_fd() ? "FD": "", sig->get_name().c_str(), subscribe ? "":"un"); } return rets; } @@ -407,133 +398,115 @@ 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) +static int send_frame(struct canfd_frame& cfd, const std::string& bus_name) { - std::map >& cd = application_t::instance().get_can_devices(); + if(bus_name.empty()) { + return -1; + } + + std::map >& cd = application_t::instance().get_can_devices(); if( cd.count(bus_name) == 0) - {cd[bus_name] = std::make_shared(low_can_socket_t());} + {cd[bus_name] = std::make_shared(low_can_subscription_t());} - return cd[bus_name]->tx_send(cf, bus_name); + return cd[bus_name]->tx_send(cfd, 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 canfd_frame cfd; + struct json_object *can_data = nullptr; - ::memset(&cf, 0, sizeof(cf)); + ::memset(&cfd, 0, sizeof(cfd)); - cf.can_id = can_id; - cf.can_dlc = can_dlc; + if(wrap_json_unpack(json_value, "{si, si, so !}", + "can_id", &cfd.can_id, + "can_dlc", &cfd.len, + "can_data", &can_data)) + { + afb_req_fail(request, "Invalid", "Frame object malformed"); + return; + } - struct json_object *x; - size_t n = json_object_array_length(can_data); - if(n <= 8) + if(cfd.len <= 8 && cfd.len > 0) { - for (int i = 0 ; i < n ; i++) + for (int i = 0 ; i < cfd.len ; i++) { - 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; + struct json_object *one_can_data = json_object_array_get_idx(can_data, i); + cfd.data[i] = (json_object_is_type(one_can_data, json_type_int)) ? + (uint8_t)json_object_get_int(one_can_data) : 0; } } - - const std::string found_device = application_t::instance().get_can_bus_manager().get_can_device_name(bus_name); - if( ! found_device.empty()) + else { - rc = send_frame(found_device, cf); + afb_req_fail(request, "Invalid", "Data array must hold 1 to 8 values."); + return; } - return rc; + if(! send_frame(cfd, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name))) + afb_req_success(request, nullptr, "Message correctly sent"); + else + afb_req_fail(request, "Error", "sending the message. See the log for more details."); } -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 canfd_frame cfd; struct utils::signals_found sf; + signal_encoder encoder = nullptr; + bool send = true; - ::memset(&cf, 0, sizeof(cf)); + ::memset(&cfd, 0, sizeof(cfd)); 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); + + cfd = encoder_t::build_frame(sig, value); + if(! send_frame(cfd, sig->get_message()->get_bus_device_name()) && 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(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)