diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2019-06-25 17:12:37 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2019-06-26 17:55:05 +0200 |
commit | 9f0d5da859bfe7778394f35baf48fbe77f1ed7d9 (patch) | |
tree | 1171c20714c22f7a433283fe821732a59f2836eb /low-can-binding/binding/low-can-cb.cpp | |
parent | ed4d7bde0c6dd6d7f4812f2601e1fa2f681ed960 (diff) |
Use subscription's sockets as shared_ptr
This change is made to leverage C++ to read and write the different socket
classes depending on CAN protocol used.
Bug-AGL: SPEC-2386
Change-Id: I5e25e271fc82e9627f836aeb43b2af5ef25db83a
Signed-off-by: Stephane Desneux <stephane.desneux@iot.bzh>
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'low-can-binding/binding/low-can-cb.cpp')
-rw-r--r-- | low-can-binding/binding/low-can-cb.cpp | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/low-can-binding/binding/low-can-cb.cpp b/low-can-binding/binding/low-can-cb.cpp index 750baeff..6b869073 100644 --- a/low-can-binding/binding/low-can-cb.cpp +++ b/low-can-binding/binding/low-can-cb.cpp @@ -70,12 +70,12 @@ void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, std s.erase(it); } -static void push_n_notify(const can_message_t& cm) +static void push_n_notify(std::shared_ptr<can_message_t> m) { can_bus_t& cbm = application_t::instance().get_can_bus_manager(); { std::lock_guard<std::mutex> 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) { - std::shared_ptr<can_message_t> cm; - utils::socketcan_bcm_t& s = can_subscription->get_socket(); - cm = s.read_message(); + std::shared_ptr<utils::socketcan_t> s = can_subscription->get_socket(); + std::shared_ptr<can_message_t> 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; } @@ -170,7 +174,7 @@ static int add_to_event_loop(std::shared_ptr<low_can_subscription_t>& 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())); |