X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Fbinding%2Flow-can-cb.cpp;h=8edd1daa290db4389e9c95726074b33a9e490ca2;hb=2f60d294b3fa4e243fa67a738f9b82a0b428a7fc;hp=098f071563ba366e980f57d1e847b2181a097710;hpb=ea35eabeadce57e4f5015797fea530c5bb219fff;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 098f0715..8edd1daa 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" * @@ -31,8 +31,8 @@ #include "application.hpp" #include "../can/can-encoder.hpp" #include "../can/can-bus.hpp" -#include "../can/can-signals.hpp" -#include "../can/can-message.hpp" +#include "../can/signals.hpp" +#include "../can/message/message.hpp" #include "../utils/signals.hpp" #include "../diagnostic/diagnostic-message.hpp" #include "../utils/openxc-utils.hpp" @@ -70,12 +70,12 @@ void on_no_clients(std::shared_ptr can_subscription, std s.erase(it); } -static void push_n_notify(const can_message_t& cm) +static void push_n_notify(std::shared_ptr m) { can_bus_t& cbm = application_t::instance().get_can_bus_manager(); { std::lock_guard can_message_lock(cbm.get_can_message_mutex()); - cbm.push_new_can_message(cm); + cbm.push_new_can_message(m); } cbm.get_new_can_message_cv().notify_one(); } @@ -83,23 +83,27 @@ static void push_n_notify(const can_message_t& cm) int read_message(sd_event_source *event_source, int fd, uint32_t revents, void *userdata) { low_can_subscription_t* can_subscription = (low_can_subscription_t*)userdata; + + if ((revents & EPOLLIN) != 0) { - can_message_t cm; - utils::socketcan_bcm_t& s = can_subscription->get_socket(); - s >> cm; + std::shared_ptr s = can_subscription->get_socket(); + std::shared_ptr message = s->read_message(); // Sure we got a valid CAN message ? - if(! cm.get_id() == 0 && ! cm.get_length() == 0) - {push_n_notify(cm);} + if (! message->get_id() == 0 && ! message->get_length() == 0) + { + push_n_notify(message); + } } // check if error or hangup if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0) { sd_event_source_unref(event_source); - can_subscription->get_socket().close(); + can_subscription->get_socket()->close(); } + return 0; } @@ -109,35 +113,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 +121,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) { - /* Event doesn't exist , so let's create it */ - s[sub_index] = can_subscription; - ret = create_event_handle(can_subscription, s); + 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()) ) + { + 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) @@ -177,7 +174,7 @@ static int add_to_event_loop(std::shared_ptr& can_subscr struct sd_event_source* event_source = nullptr; return ( sd_event_add_io(afb_daemon_get_event_loop(), &event_source, - can_subscription->get_socket().socket(), + can_subscription->get_socket()->socket(), EPOLLIN, read_message, can_subscription.get())); @@ -244,21 +241,19 @@ static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request, return rets; } -static int subscribe_unsubscribe_can_signals(afb_req_t request, +static int subscribe_unsubscribe_signals(afb_req_t request, bool subscribe, - std::vector > can_signals, + std::vector > signals, struct event_filter_t& event_filter, std::map >& s) { int rets = 0; - for(const auto& sig: can_signals) + for(const auto& sig: signals) { 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 +267,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; } @@ -282,7 +277,7 @@ static int subscribe_unsubscribe_can_signals(afb_req_t request, /// /// @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 +/// @param[in] signals - struct containing vectors with signal_t and diagnostic_messages to subscribe /// /// @return Number of correctly subscribed signal /// @@ -298,7 +293,7 @@ static int subscribe_unsubscribe_signals(afb_req_t request, std::map >& s = sm.get_subscribed_signals(); rets += subscribe_unsubscribe_diagnostic_messages(request, subscribe, signals.diagnostic_messages, event_filter, s, false); - rets += subscribe_unsubscribe_can_signals(request, subscribe, signals.can_signals, event_filter, s); + rets += subscribe_unsubscribe_signals(request, subscribe, signals.signals, event_filter, s); return rets; } @@ -330,7 +325,7 @@ static int one_subscribe_unsubscribe(afb_req_t request, // subscribe or unsubscribe openxc_DynamicField search_key = build_DynamicField(tag); sf = utils::signals_manager_t::instance().find_signals(search_key); - if (sf.can_signals.empty() && sf.diagnostic_messages.empty()) + if (sf.signals.empty() && sf.diagnostic_messages.empty()) { AFB_NOTICE("No signal(s) found for %s.", tag.c_str()); ret = -1; @@ -413,12 +408,12 @@ static int send_frame(struct canfd_frame& cfd, const std::string& bus_name) return -1; } - std::map >& cd = application_t::instance().get_can_devices(); + 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(cfd, bus_name); + return cd[bus_name]->tx_send(*cd[bus_name], cfd, bus_name); } static void write_raw_frame(afb_req_t request, const std::string& bus_name, json_object *json_value) @@ -471,13 +466,13 @@ static void write_signal(afb_req_t request, const std::string& name, json_object sf = utils::signals_manager_t::instance().find_signals(search_key); openxc_DynamicField dynafield_value = build_DynamicField(json_value); - if (sf.can_signals.empty()) + if (sf.signals.empty()) { afb_req_fail_f(request, "No signal(s) found for %s. Message not sent.", name.c_str()); return; } - std::shared_ptr& sig = sf.can_signals[0]; + std::shared_ptr& sig = sf.signals[0]; if(! sig->get_writable()) { afb_req_fail_f(request, "%s isn't writable. Message not sent.", sig->get_name().c_str()); @@ -526,13 +521,13 @@ static struct json_object *get_signals_value(const std::string& name) openxc_DynamicField search_key = build_DynamicField(name); sf = utils::signals_manager_t::instance().find_signals(search_key); - if (sf.can_signals.empty()) + if (sf.signals.empty()) { AFB_WARNING("No signal(s) found for %s.", name.c_str()); return NULL; } ans = json_object_new_array(); - for(const auto& sig: sf.can_signals) + for(const auto& sig: sf.signals) { struct json_object *jobj = json_object_new_object(); json_object_object_add(jobj, "event", json_object_new_string(sig->get_name().c_str())); @@ -580,13 +575,13 @@ static struct json_object *list_can_message(const std::string& name) openxc_DynamicField search_key = build_DynamicField(name); sf = utils::signals_manager_t::instance().find_signals(search_key); - if (sf.can_signals.empty() && sf.diagnostic_messages.empty()) + if (sf.signals.empty() && sf.diagnostic_messages.empty()) { AFB_WARNING("No signal(s) found for %s.", name.c_str()); return NULL; } ans = json_object_new_array(); - for(const auto& sig: sf.can_signals) + for(const auto& sig: sf.signals) { json_object_array_add(ans, json_object_new_string(sig->get_name().c_str())); @@ -651,7 +646,7 @@ int init_binding(afb_api_t api) 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) + if(sf.signals.empty() && sf.diagnostic_messages.size() == 1) { afb_req_t request = nullptr;