diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-02-17 17:03:53 +0000 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-02-20 11:14:55 +0000 |
commit | d9237e07858fb243eb35f78ded3664aec69f524d (patch) | |
tree | dce18989baaaa2836e4c9f00ddcc40c3072eb15e /src/low-can-binding.cpp | |
parent | be97b88567b831ddcd85295dfe890e53d0ad1bda (diff) |
Separated can_bus and can_bus_dev object.
Now there are as many thread reading as can bus device
Change-Id: I65928b39714f3a0e76e588de3db10d091ad5093d
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'src/low-can-binding.cpp')
-rw-r--r-- | src/low-can-binding.cpp | 150 |
1 files changed, 84 insertions, 66 deletions
diff --git a/src/low-can-binding.cpp b/src/low-can-binding.cpp index b895adc..7899797 100644 --- a/src/low-can-binding.cpp +++ b/src/low-can-binding.cpp @@ -40,7 +40,9 @@ #include <afb/afb-binding.h> #include <afb/afb-service-itf.h> -#include "obd2.hpp" +//#include "obd2.hpp" +#include "can-utils.hpp" +#include "can-signals.hpp" /* * Interface between the daemon and the binding @@ -56,21 +58,22 @@ static const struct afb_binding_interface *interface; /* * TBF TBF TBF * called on an event on the CAN bus - */ -static int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) + static int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) { openxc_CanMessage can_message; can_message = openxc_CanMessage_init_default; /* read available data */ + /* if ((revents & EPOLLIN) != 0) { read_can(&can_message); send_event(); } - +*/ /* check if error or hangup */ +/* if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0) { sd_event_source_unref(s); @@ -80,7 +83,7 @@ static int on_event(sd_event_source *s, int fd, uint32_t revents, void *userdata return 0; } - +*/ /* * USELESS SINCE THREADS SEPARATION * @@ -120,45 +123,68 @@ static int connect_to_event_loop(CanBus &CanBus_handler) * *********************************************************************************/ -static int subscribe_unsubscribe_signal(struct afb_req request, int subscribe, std::map<CanSignal, struct afb_event>::iterator *s_sig) +static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, std::vector<CanSignal>::const_iterator& sig_i) { - if (!afb_event_is_valid(s_sig->second)) { - if (!subscribe) - return 1; - sig->event = afb_daemon_make_event(afbitf->daemon, s_sig->first->genericName); - if (!afb_event_is_valid(s_sig->second)) { - return 0; - } - } - - if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s_sig->second)) < 0) { - return 0; + int ret; + + const auto& ss_i = subscribed_signals.find(sig_i); + if (ss_i != subscribed_signals.end()) + { + if(!afb_event_is_valid(ss_i->second)) + { + if(!subscribe) + { + NOTICE(interface, "Event isn't valid, it can't be unsubscribed."); + ret = 1; + } + else + { + ss_i->second = afb_daemon_make_event(afbitf->daemon, ss_i->first.genericName); + if (!afb_event_is_valid(ss_i->second)) + { + ERROR(interface, "Can't create an event, something goes wrong."); + ret = 0; + } + } + } + } + else + { + subscribed_signals[sig_i] = afb_daemon_make_event(afbitf->daemon, sig_i.genericName); + if (!afb_event_is_valid(ss_i->second)) + { + ERROR(interface, "Can't create an event, something goes wrong."); + ret = 0; + } + } + + if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, subscribed_signals[sig_i])) < 0) + { + ERROR(interface, "Operation goes wrong for signal: %s", sig_i.genericName); + ret = 0; } - - return 1; + else + ret = 1; + + return ret; } -static int subscribe_unsubscribe_signals(struct afb_req request, int subscribe, std:vector<CanSignal> *signals) +static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, const std:vector<CanSignal>& signals) { std::vector<CanSignal>::iterator signal_i; std::map <CanSignal, struct afb_event>::iterator s_signal_i; - - for(signal_i=signals.begin(); signal_i != signals.end(); signal_i++) + int ret; + + // TODO: lock the subscribed_signals when insert/remove + for(const auto& signal_i : signals) { - s_signal_i = subscribed_signals.find(signal_i); - if(s_signal_i != subscribed_signals.end()) - subscribe_unsubscribe_signal(request, subscribe, s_signal_i); - else - { - std::map <CanSignal, struct afb_event>::iterator it = subscribed_signals.begin(); - it = subscribed_signals.insert(it, std::pair<CanSignal, struct afb_event>(signal_i, NULL); - subscribe_unsubscribe_signal(request, subscribe, it); - } - return 0; + ret = subscribe_unsubscribe_signal(request, subscribe, signal_i); + if(ret == 0) + return ret; } } -static int subscribe_unsubscribe_all(struct afb_req request, int subscribe) +static int subscribe_unsubscribe_all(struct afb_req request, bool subscribe) { int i, n, e; @@ -169,25 +195,32 @@ static int subscribe_unsubscribe_all(struct afb_req request, int subscribe) return e == 0; } -static int subscribe_unsubscribe_name(struct afb_req request, int subscribe, const char *name) +static int subscribe_unsubscribe_name(struct afb_req request, bool subscribe, const char *name) { - std::vector <CanSignal> *sig; - - if (0 == strcmp(name, "*")) - return subscribe_unsubscribe_all(request, subscribe); - - if(obd2_handler_c.is_obd2_signal(name)) + std::vector<CanSignal> sig; + int ret = 0; + if (!strcmp(name, "*")) + ret = subscribe_unsubscribe_all(request, subscribe); else - find_can_signals(name, sig); - if (sig == NULL) { - return 0; - } - - return subscribe_unsubscribe_sig(request, subscribe, sig); + { + //if(obd2_handler_c.is_obd2_signal(name)) + if(false) + { + // TODO + } + else + { + sig = find_can_signals(name); + if (sig.empty()) + ret = 0; + } + ret = subscribe_unsubscribe_signals(request, subscribe, sig); + } + return ret; } -static void subscribe_unsubscribe(struct afb_req request, int subscribe) +static void subscribe_unsubscribe(struct afb_req request, bool subscribe) { int ok, i, n; struct json_object *args, *a, *x; @@ -218,12 +251,12 @@ static void subscribe_unsubscribe(struct afb_req request, int subscribe) static void subscribe(struct afb_req request) { - subscribe_unsubscribe(request, 1); + subscribe_unsubscribe(request, true); } static void unsubscribe(struct afb_req request) { - subscribe_unsubscribe(request, 0); + subscribe_unsubscribe(request, false); } static const struct afb_verb_desc_v1 verbs[]= { @@ -258,25 +291,10 @@ const struct afb_binding *afbBindingV1Register (const struct afb_binding_interfa int afbBindingV1ServiceInit(struct afb_service service) { std::ifstream fd_conf; - std::string fd_conf_content; - json_object jo_canbus; - - /* Open JSON conf file */ - jo_canbus = json_object_new_object(); - fd_conf = afb_daemon_rootdir_open_locale(interface->daemon, "canbus.json", O_RDONLY, NULL); - if (fd_conf) - { - fd_conf.seekg(0, std::ios::end); - fd_conf_content.resize(fd_conf.tellg()); - fd_conf.seekg(0, std::ios::beg); - fd_conf.read(&fd_conf_content[0], fd_conf_content.size()); - fd_conf.close(); - } - - jo_canbus = json_tokener_parse(&fd_conf_content); + fd_conf = afb_daemon_rootdir_open_locale(interface->daemon, "can_bus.json", O_RDONLY, NULL); /* Open CAN socket */ - can_bus_t CanBus_handler(interface, json_object_get_string(json_object_object_get(jo_canbus, "deviceName")); + can_bus_t can_bus_handler(interface, )); CanBus_handler.open(); CanBus_handler.start_threads(); } |