From fd3978709abbb3ed46caaf48c1d2f45b2c74fa89 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Tue, 16 May 2017 16:36:51 +0200 Subject: Make and use a single function to read incoming CAN messages Change-Id: I5d3f2a8628ace464ad25989346cc76682c591bd8 Signed-off-by: Romain Forlot --- CAN-binder/low-can-binding/binding/low-can-cb.cpp | 53 ++++++++++++++-------- CAN-binder/low-can-binding/binding/low-can-hat.hpp | 3 +- 2 files changed, 36 insertions(+), 20 deletions(-) (limited to 'CAN-binder/low-can-binding/binding') diff --git a/CAN-binder/low-can-binding/binding/low-can-cb.cpp b/CAN-binder/low-can-binding/binding/low-can-cb.cpp index d2987791..4faae852 100644 --- a/CAN-binder/low-can-binding/binding/low-can-cb.cpp +++ b/CAN-binder/low-can-binding/binding/low-can-cb.cpp @@ -52,33 +52,50 @@ void on_no_clients(std::string message) } } -int read_can_signal(sd_event_source *s, int fd, uint32_t revents, void *userdata) +static void push_n_notify(const can_message_t cm) { - can_signal_t* sig= (can_signal_t*)userdata; - sig->read_socket(); - - /* check if error or hangup */ - if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0) - { - sd_event_source_unref(s); - sig->get_socket().close(); - sig->create_rx_filter(); - } - return 0; + can_bus_t& cbm = configuration_t::instance().get_can_bus_manager(); + std::lock_guard can_message_lock(cbm.get_can_message_mutex()); + { cbm.push_new_can_message(cm); } + cbm.get_new_can_message_cv().notify_one(); } -int read_diagnostic_message(sd_event_source *s, int fd, uint32_t revents, void *userdata) +int read_message(sd_event_source *s, int fd, uint32_t revents, void *userdata) { + can_message_t cm; + can_signal_t* sig; diagnostic_manager_t& diag_m = configuration_t::instance().get_diagnostic_manager(); - diag_m.read_socket(); + + if(userdata != nullptr) + { + sig = (can_signal_t*)userdata; + utils::socketcan_bcm_t s = sig->get_socket(); + s >> cm; + } + else + { + utils::socketcan_bcm_t s = diag_m.get_socket(); + s >> cm; + } + + push_n_notify(cm); /* check if error or hangup */ if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0) { sd_event_source_unref(s); - diag_m.get_socket().close(); - diag_m.cleanup_active_requests(true); - ERROR(binder_interface, "%s: Error on diagnostic manager socket, cancelling active requests.", __FUNCTION__); + if(userdata != nullptr) + { + sig->get_socket().close(); + sig->create_rx_filter(); + NOTICE(binder_interface, "%s: Recreated RX_SETUP BCM job for signal: %s", __FUNCTION__, sig->get_name().c_str()); + } + else + { + diag_m.get_socket().close(); + diag_m.cleanup_active_requests(true); + ERROR(binder_interface, "%s: Error on diagnostic manager socket, cancelling active requests.", __FUNCTION__); + } return -1; } @@ -207,7 +224,7 @@ static int subscribe_unsubscribe_signals(struct afb_req request, bool subscribe, return -1; } struct sd_event_source* e_source; - sd_event_add_io(afb_daemon_get_event_loop(binder_interface->daemon), &e_source, sig->get_socket().socket(), EPOLLIN, read_can_signal, sig.get()); + sd_event_add_io(afb_daemon_get_event_loop(binder_interface->daemon), &e_source, sig->get_socket().socket(), EPOLLIN, read_message, sig.get()); rets++; DEBUG(binder_interface, "%s: signal: %s subscribed", __FUNCTION__, sig->get_name().c_str()); } diff --git a/CAN-binder/low-can-binding/binding/low-can-hat.hpp b/CAN-binder/low-can-binding/binding/low-can-hat.hpp index 57c0a536..b3384d77 100644 --- a/CAN-binder/low-can-binding/binding/low-can-hat.hpp +++ b/CAN-binder/low-can-binding/binding/low-can-hat.hpp @@ -32,8 +32,7 @@ extern "C" struct afb_binding_interface; extern const struct afb_binding_interface *binder_interface; void on_no_clients(std::string message); -int read_can_signal(sd_event_source *s, int fd, uint32_t revents, void *userdata); -int read_diagnostic_message(sd_event_source *s, int fd, uint32_t revents, void *userdata); +int read_message(sd_event_source *s, int fd, uint32_t revents, void *userdata); void subscribe(struct afb_req request); void unsubscribe(struct afb_req request); \ No newline at end of file -- cgit 1.2.3-korg