X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Flow-can-binding.cpp;h=f60790fc8e629e321da69098fdd6834ac5ee8632;hb=4e2d038c42d6995d69239455ec4fbad9931fcfd5;hp=2fee11465d1beebece96e6b9037fc5b250555acd;hpb=92bc628a7b7cbf141c2bbc7e4ea3eeac62cd30dd;p=apps%2Flow-level-can-service.git diff --git a/src/low-can-binding.cpp b/src/low-can-binding.cpp index 2fee114..f60790f 100644 --- a/src/low-can-binding.cpp +++ b/src/low-can-binding.cpp @@ -19,167 +19,149 @@ #include "low-can-binding.hpp" #include +#include #include #include -#include +#include #include #include -#include -#include "timer.hpp" #include "openxc.pb.h" -#include "can-utils.hpp" -#include "can-signals.hpp" -#include "openxc-utils.hpp" +#include "configuration.hpp" +#include "can/can-bus.hpp" +#include "can/can-signals.hpp" +#include "can/can-message.hpp" +#include "utils/timer.hpp" +#include "utils/signals.hpp" +#include "obd2/obd2-signals.hpp" +#include "utils/openxc-utils.hpp" extern "C" { - #include #include }; -/* - * Interface between the daemon and the binding - */ +// Interface between the daemon and the binding const struct afb_binding_interface *binder_interface; -/* - * CAN bus handler pointer. This is the object that will be use to - * initialize each CAN devices specified into the configuration file - * - * It is used by the reading thread also because of its can_message_q_ queue - * that store CAN messages read from the socket. - */ -can_bus_t *can_bus_handler; - /******************************************************************************** * -* Event management +* Subscription and unsubscription * *********************************************************************************/ -int can_frame_received(sd_event_source *s, int fd, uint32_t revents, void *userdata) -{ - can_bus_dev_t *can_bus_dev = (can_bus_dev_t*)userdata; - /* Notify reading thread that there is something to read */ - if ((revents & EPOLLIN) != 0) { - new_can_frame.notify_one(); +static int make_subscription_unsubscription(struct afb_req request, const std::string& sig_name, std::map& s, bool subscribe) +{ + /* Make the subscription or unsubscription to the event */ + if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s[sig_name])) < 0) + { + ERROR(binder_interface, "Operation goes wrong for signal: %s", sig_name.c_str()); + return 0; } + return 1; + +} - /* check if error or hangup and reopen the socket and event_loop. - * socket is protected by a mutex */ - if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0) +static int create_event_handle(const std::string& sig_name, std::map& s) +{ + s[sig_name] = afb_daemon_make_event(binder_interface->daemon, sig_name.c_str()); + if (!afb_event_is_valid(s[sig_name])) { - std::lock_guard can_frame_lock(can_frame_mutex); - sd_event_source_unref(s); - can_bus_dev->close(); - can_bus_dev->open(); - can_bus_dev->start_reading(*can_bus_handler); - can_bus_dev->event_loop_connection(); + ERROR(binder_interface, "Can't create an event for %s, something goes wrong.", sig_name.c_str()); + return 0; } - - return 0; + return 1; } -/******************************************************************************** -* -* Subscription and unsubscription -* -*********************************************************************************/ -static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, const CanSignal& sig) +static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, const std::string& sig) { int ret; - // TODO: lock the subscribed_signals when insert/remove - const auto& ss_i = subscribed_signals.find(sig.genericName); - if (ss_i != subscribed_signals.end()) + std::lock_guard subscribed_signals_lock(get_subscribed_signals_mutex()); + std::map& s = get_subscribed_signals(); + if (s.find(sig) != s.end() && !afb_event_is_valid(s[sig])) { - if(!afb_event_is_valid(ss_i->second)) + if(!subscribe) { - if(!subscribe) - { - NOTICE(binder_interface, "Event isn't valid, it can't be unsubscribed."); - ret = 1; - } - else - { - ss_i->second = afb_daemon_make_event(binder_interface->daemon, ss_i->first.c_str()); - if (!afb_event_is_valid(ss_i->second)) - { - ERROR(binder_interface, "Can't create an event, something goes wrong."); - ret = 0; - } - } + NOTICE(binder_interface, "Event isn't valid, it can't be unsubscribed."); + ret = -1; } - } - else - { - subscribed_signals[sig.genericName] = afb_daemon_make_event(binder_interface->daemon, sig.genericName); - if (!afb_event_is_valid(ss_i->second)) + else { - ERROR(binder_interface, "Can't create an event, something goes wrong."); - ret = 0; + /* Event it isn't valid annymore, recreate it */ + ret = create_event_handle(sig, s); } } - - if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, subscribed_signals[sig.genericName])) < 0) + else { - ERROR(binder_interface, "Operation goes wrong for signal: %s", sig.genericName); - ret = 0; + /* Event doesn't exist , so let's create it */ + struct afb_event empty_event = {nullptr, nullptr}; + subscribed_signals[sig] = empty_event; + ret = create_event_handle(sig, s); } - else - ret = 1; - - return ret; + + /* Check whether or not the event handler has been correctly created and + * make the subscription/unsubscription operation is so. + */ + if (ret <= 0) + return ret; + return make_subscription_unsubscription(request, sig, s, subscribe); } -static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std::vector& signals) +/** + * @fn static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std::vector& signals) + * @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] can_signal_t vector with can_signal_t to subscribe + * + * @return Number of correctly subscribed signal + */ +static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std::vector& signals) { - int ret = 0; + int rets = 0; - for(const auto& signal_i : signals) + //TODO: Implement way to dynamically call the right function no matter + // how much signals types we have. + + for(const std::string& sig : signals) { - ret = subscribe_unsubscribe_signal(request, subscribe, signal_i); - if(ret == 0) - return ret; - } - return ret; -} + int ret; + if (active_diagnostic_request_t::is_diagnostic_signal(sig) && subscribe) + { + std::vector found; + configuration_t::instance().find_obd2_signals(build_DynamicField(sig), found); + int frequency = found.front()->get_frequency(); + DiagnosticRequest* diag_req = new DiagnosticRequest(found.front()->build_diagnostic_request()); + configuration_t::instance().get_diagnostic_manager().add_recurring_request( + diag_req, sig.c_str(), false, obd2_signal_t::decode_obd2_response, nullptr, (float)frequency); + //TODO: Adding callback requesting ignition status: diag_req, sig.c_str(), false, obd2_signal_t::decode_obd2_response, obd2_signal_t::check_ignition_status, frequency); + } -static int subscribe_unsubscribe_all(struct afb_req request, bool subscribe) -{ - int e = 0; + ret = subscribe_unsubscribe_signal(request, subscribe, sig); + if(ret <= 0) + return ret; - //for (const auto& sig : SIGNALS) - // e += !subscribe_unsubscribe_signals(request, subscribe, sig); - e += !subscribe_unsubscribe_signals(request, subscribe, getSignals()); - - return e == 0; + rets++; + DEBUG(binder_interface, "Signal: %s subscribed", sig.c_str()); + } + return rets; } static int subscribe_unsubscribe_name(struct afb_req request, bool subscribe, const char *name) { - std::vector sig; + std::vector signals; int ret = 0; - if (!::strcmp(name, "*")) - ret = subscribe_unsubscribe_all(request, subscribe); - else - { - //if(obd2_handler_c.is_obd2_signal(name)) - if(false) - { - // TODO - } - else - { - openxc_DynamicField search_key = build_DynamicField(name); - sig = find_can_signals(search_key); - if (sig.empty()) - ret = 0; - } - ret = subscribe_unsubscribe_signals(request, subscribe, sig); - } + openxc_DynamicField search_key = build_DynamicField(std::string(name)); + signals = find_signals(search_key); + if (signals.empty()) + ret = 0; + + ret = subscribe_unsubscribe_signals(request, subscribe, signals); + NOTICE(binder_interface, "Subscribed/unsubscribe correctly to %d/%d signal(s).", ret, (int)signals.size()); + return ret; } @@ -191,7 +173,7 @@ static void subscribe_unsubscribe(struct afb_req request, bool subscribe) /* makes the subscription/unsubscription */ args = afb_req_json(request); if (args == NULL || !json_object_object_get_ex(args, "event", &a)) { - ok = subscribe_unsubscribe_all(request, subscribe); + ok = subscribe_unsubscribe_name(request, subscribe, "*"); } else if (json_object_get_type(a) != json_type_array) { ok = subscribe_unsubscribe_name(request, subscribe, json_object_get_string(a)); } else { @@ -233,8 +215,8 @@ extern "C" static const struct afb_binding binding_desc { AFB_BINDING_VERSION_1, { - "CAN bus service", - "can", + "Low level CAN bus service", + "low-can", verbs } }; @@ -248,26 +230,25 @@ extern "C" /** * @brief Initialize the binding. - * + * * @param[in] service Structure which represent the Application Framework Binder. - * + * * @return Exit code, zero if success. */ int afbBindingV1ServiceInit(struct afb_service service) { - int fd_conf; - fd_conf = afb_daemon_rootdir_open_locale(binder_interface->daemon, "can_bus.json", O_RDONLY, NULL); + can_bus_t& can_bus_manager = configuration_t::instance().get_can_bus_manager(); - /* Initialize the CAN bus handler */ - can_bus_t cbh(fd_conf); - can_bus_handler = &cbh; + /// Initialize CAN socket + if(can_bus_manager.init_can_dev() == 0) + can_bus_manager.start_threads(); - /* Open CAN socket */ - if(can_bus_handler->init_can_dev() == 0) - { - can_bus_handler->start_threads(); + /// Initialize Diagnostic manager that will handle obd2 requests. + /// We pass by default the first CAN bus device to its Initialization. + /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus. + if(configuration_t::instance().get_diagnostic_manager().initialize(can_bus_manager.get_can_devices().front())) return 0; - } + ERROR(binder_interface, "There was something wrong with CAN device Initialization. Check your config file maybe"); return 1; }