2 * Copyright (C) 2015, 2018 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
4 * Author "Loic Collignon" <loic.collignon@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "low-can-hat.hpp"
20 #include "low-can-apidef.h"
27 #include <wrap-json.h>
28 #include <systemd/sd-event.h>
29 #include "openxc.pb.h"
30 #include "application.hpp"
31 #include "../can/can-encoder.hpp"
32 #include "../can/can-bus.hpp"
33 #include "../can/signals.hpp"
34 #include "../can/message/message.hpp"
35 #include "../utils/signals.hpp"
36 #include "../diagnostic/diagnostic-message.hpp"
37 #include "../utils/openxc-utils.hpp"
38 #include "../utils/signals.hpp"
40 #ifdef USE_FEATURE_J1939
41 #include "../can/message/j1939-message.hpp"
42 #include <linux/can/j1939.h>
44 ///******************************************************************************
46 /// SystemD event loop Callbacks
48 ///*******************************************************************************/
50 void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, uint32_t pid, map_subscription& s)
52 bool is_permanent_recurring_request = false;
54 if( ! can_subscription->get_diagnostic_message().empty() && can_subscription->get_diagnostic_message(pid) != nullptr)
56 DiagnosticRequest diag_req = can_subscription->get_diagnostic_message(pid)->build_diagnostic_request();
57 active_diagnostic_request_t* adr = application_t::instance().get_diagnostic_manager().find_recurring_request(diag_req);
60 is_permanent_recurring_request = adr->get_permanent();
62 if(! is_permanent_recurring_request)
63 application_t::instance().get_diagnostic_manager().cleanup_request(adr, true);
67 if(! is_permanent_recurring_request)
68 on_no_clients(can_subscription, s);
71 void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, map_subscription& s)
73 auto it = s.find(can_subscription->get_index());
77 static void push_n_notify(std::shared_ptr<message_t> m)
79 can_bus_t& cbm = application_t::instance().get_can_bus_manager();
81 std::lock_guard<std::mutex> can_message_lock(cbm.get_can_message_mutex());
82 cbm.push_new_can_message(m);
84 cbm.get_new_can_message_cv().notify_one();
87 int read_message(sd_event_source *event_source, int fd, uint32_t revents, void *userdata)
90 low_can_subscription_t* can_subscription = (low_can_subscription_t*)userdata;
93 if ((revents & EPOLLIN) != 0)
95 utils::signals_manager_t& sm = utils::signals_manager_t::instance();
96 std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
97 if(can_subscription->get_index() != -1)
99 std::shared_ptr<utils::socketcan_t> s = can_subscription->get_socket();
100 if(s->socket() && s->socket() != -1)
102 std::shared_ptr<message_t> message = s->read_message();
104 // Sure we got a valid CAN message ?
105 if (! message->get_id() == 0 && ! message->get_length() == 0 && !(message->get_flags()&INVALID_FLAG))
107 push_n_notify(message);
113 // check if error or hangup
114 if ((revents & (EPOLLERR | EPOLLRDHUP | EPOLLHUP)) != 0)
116 sd_event_source_unref(event_source);
117 can_subscription->get_socket()->close();
123 ///******************************************************************************
125 /// Subscription and unsubscription
127 ///*******************************************************************************/
129 /// @brief This will determine if an event handle needs to be created and checks if
130 /// we got a valid afb_event to get subscribe or unsubscribe. After that launch the subscription or unsubscription
131 /// against the application framework using that event handle.
132 static int subscribe_unsubscribe_signal(afb_req_t request,
134 std::shared_ptr<low_can_subscription_t>& can_subscription,
138 int sub_index = can_subscription->get_index();
139 bool subscription_exists = s.count(sub_index);
144 /* There is no valid request to subscribe so this must be an
145 * internal permanent diagnostic request. Skip the subscription
146 * part and don't register it into the current "low-can"
154 // Event doesn't exist , so let's create it
155 if ((ret = can_subscription->subscribe(request)) < 0)
158 if(! subscription_exists)
159 s[sub_index] = can_subscription;
164 // Unsubscrition part
165 if(! subscription_exists)
167 AFB_NOTICE("There isn't any valid subscriptions for that request.");
170 else if (subscription_exists &&
171 ! afb_event_is_valid(s[sub_index]->get_event()) )
173 AFB_NOTICE("Event isn't valid, no need to unsubscribed.");
177 if( (ret = s[sub_index]->unsubscribe(request)) < 0)
179 s.find(sub_index)->second->set_index(-1);
184 static int add_to_event_loop(std::shared_ptr<low_can_subscription_t>& can_subscription)
186 struct sd_event_source* event_source = nullptr;
187 return ( sd_event_add_io(afb_daemon_get_event_loop(),
189 can_subscription->get_socket()->socket(),
192 can_subscription.get()));
195 static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request,
197 list_ptr_diag_msg_t diagnostic_messages,
198 struct event_filter_t& event_filter,
200 bool perm_rec_diag_req)
203 application_t& app = application_t::instance();
204 diagnostic_manager_t& diag_m = app.get_diagnostic_manager();
206 for(const auto& sig : diagnostic_messages)
208 DiagnosticRequest* diag_req = new DiagnosticRequest(sig->build_diagnostic_request());
209 event_filter.frequency = event_filter.frequency == 0 ? sig->get_frequency() : event_filter.frequency;
210 std::shared_ptr<low_can_subscription_t> can_subscription;
212 auto it = std::find_if(s.begin(), s.end(), [&sig](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub)
214 return (! sub.second->get_diagnostic_message().empty());
216 can_subscription = it != s.end() ?
218 std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
219 // If the requested diagnostic message is not supported by the car then unsubcribe it
220 // no matter what we want, worst case will be a failed unsubscription but at least we won't
221 // poll a PID for nothing.
222 if(sig->get_supported() && subscribe)
224 if (!app.is_engine_on())
225 AFB_WARNING("signal: Engine is off, %s won't received responses until it's on", sig->get_name().c_str());
227 diag_m.add_recurring_request(diag_req, sig->get_name().c_str(), false, sig->get_decoder(), sig->get_callback(), event_filter.frequency, perm_rec_diag_req);
228 if(can_subscription->create_rx_filter(sig) < 0)
230 AFB_DEBUG("Signal: %s subscribed", sig->get_name().c_str());
231 if(it == s.end() && add_to_event_loop(can_subscription) < 0)
233 diag_m.cleanup_request(
234 diag_m.find_recurring_request(*diag_req), true);
235 AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
241 if(sig->get_supported())
243 AFB_DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str());
247 AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
251 int ret = subscribe_unsubscribe_signal(request, subscribe, can_subscription, s);
260 static int subscribe_unsubscribe_signals(afb_req_t request,
262 list_ptr_signal_t signals,
263 struct event_filter_t& event_filter,
267 for(const auto& sig: signals)
269 auto it = std::find_if(s.begin(), s.end(), [&sig, &event_filter](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub)
271 return sub.second->is_signal_subscription_corresponding(sig, event_filter) ;
273 std::shared_ptr<low_can_subscription_t> can_subscription;
275 can_subscription = it->second;
278 can_subscription = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
279 if(can_subscription->create_rx_filter(sig) < 0)
281 if(add_to_event_loop(can_subscription) < 0)
285 if(subscribe_unsubscribe_signal(request, subscribe, can_subscription, s) < 0)
289 AFB_DEBUG("%s Signal: %s %ssubscribed", sig->get_message()->is_fd() ? "FD": "", sig->get_name().c_str(), subscribe ? "":"un");
295 /// @brief subscribe to all signals in the vector signals
297 /// @param[in] afb_req request : contains original request use to subscribe or unsubscribe
298 /// @param[in] subscribe boolean value, which chooses between a subscription operation or an unsubscription
299 /// @param[in] signals - struct containing vectors with signal_t and diagnostic_messages to subscribe
300 /// @param[in] event_filter - stuct containing filter on the signal
302 /// @return Number of correctly subscribed signal
304 static int subscribe_unsubscribe_signals(afb_req_t request,
306 const struct utils::signals_found& signals,
307 struct event_filter_t& event_filter)
310 utils::signals_manager_t& sm = utils::signals_manager_t::instance();
312 std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
313 map_subscription& s = sm.get_subscribed_signals();
315 rets += subscribe_unsubscribe_diagnostic_messages(request, subscribe, signals.diagnostic_messages, event_filter, s, false);
316 rets += subscribe_unsubscribe_signals(request, subscribe, signals.signals, event_filter, s);
321 static event_filter_t generate_filter(json_object* args)
323 event_filter_t event_filter;
324 struct json_object *filter, *obj;
326 // computes the filter
327 if (json_object_object_get_ex(args, "filter", &filter))
329 if (json_object_object_get_ex(filter, "frequency", &obj)
330 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
331 event_filter.frequency = (float)json_object_get_double(obj);
332 if (json_object_object_get_ex(filter, "min", &obj)
333 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
334 event_filter.min = (float)json_object_get_double(obj);
335 if (json_object_object_get_ex(filter, "max", &obj)
336 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
337 event_filter.max = (float)json_object_get_double(obj);
338 if (json_object_object_get_ex(filter, "rx_id", &obj)
339 && (json_object_is_type(obj, json_type_int)))
340 event_filter.rx_id = (canid_t) json_object_get_int(obj);
341 if (json_object_object_get_ex(filter, "tx_id", &obj)
342 && (json_object_is_type(obj, json_type_int)))
343 event_filter.tx_id = (canid_t) json_object_get_int(obj);
349 static int one_subscribe_unsubscribe_events(afb_req_t request, bool subscribe, const std::string& tag, json_object* args)
352 struct utils::signals_found sf;
354 // subscribe or unsubscribe
355 openxc_DynamicField search_key = build_DynamicField(tag);
356 sf = utils::signals_manager_t::instance().find_signals(search_key);
359 #ifdef USE_FEATURE_ISOTP
360 if(sf.signals.size() > 1)
362 sf.signals.remove_if([](std::shared_ptr<signal_t> x){
363 bool isotp = x->get_message()->is_isotp();
365 AFB_NOTICE("ISO TP messages need to be subscribed one by one (rx, tx)");
372 if (sf.signals.empty() && sf.diagnostic_messages.empty())
374 AFB_NOTICE("No signal(s) found for %s.", tag.c_str());
379 event_filter_t event_filter = generate_filter(args);
380 ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
385 static int one_subscribe_unsubscribe_id(afb_req_t request, bool subscribe, const uint32_t& id, json_object *args)
388 std::shared_ptr<message_definition_t> message_definition = application_t::instance().get_message_definition(id);
389 struct utils::signals_found sf;
391 if(message_definition)
392 sf.signals = list_ptr_signal_t(message_definition->get_signals().begin(), message_definition->get_signals().end());
394 if(sf.signals.empty())
396 AFB_NOTICE("No signal(s) found for %d.", id);
401 event_filter_t event_filter = generate_filter(args);
402 ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
409 static int process_one_subscribe_args(afb_req_t request, bool subscribe, json_object *args)
412 json_object *x = nullptr, *event = nullptr, *id = nullptr;
415 // 2 cases : ID(PGN) and event
417 json_object_object_get_ex(args,"event",&event);
418 json_bool test_id = json_object_object_get_ex(args,"id",&id);
420 test_id = json_object_object_get_ex(args,"pgn",&id);
422 if( args == NULL || (id && ((std::string)json_object_get_string(id)).compare("*") == 0))
424 rc = one_subscribe_unsubscribe_events(request, subscribe, "*", args);
430 if (json_object_get_type(event) != json_type_array) // event is set before and check if it's an array
432 rc = one_subscribe_unsubscribe_events(request, subscribe, json_object_get_string(event), args);
434 else // event is set and it's not an array
436 for (int i = 0 ; i < json_object_array_length(event); i++)
438 x = json_object_array_get_idx(event, i);
439 rc2 = one_subscribe_unsubscribe_events(request, subscribe, json_object_get_string(x), args);
441 rc = rc2 >= 0 ? rc + rc2 : rc2;
448 if (json_object_get_type(id) != json_type_array) // id is set before and check if it's an array
450 rc = one_subscribe_unsubscribe_id(request, subscribe, json_object_get_int(id), args);
452 else // event is set and it's not an array
454 for (int i = 0 ; i < json_object_array_length(id); i++)
456 x = json_object_array_get_idx(id, i);
457 rc2 = one_subscribe_unsubscribe_id(request, subscribe, json_object_get_int(x), args);
459 rc = rc2 >= 0 ? rc + rc2 : rc2;
467 static void do_subscribe_unsubscribe(afb_req_t request, bool subscribe)
470 struct json_object *args, *x;
472 args = afb_req_json(request);
473 if (json_object_get_type(args) == json_type_array)
475 for(int i = 0; i < json_object_array_length(args); i++)
477 x = json_object_array_get_idx(args, i);
478 rc += process_one_subscribe_args(request, subscribe, x);
483 rc += process_one_subscribe_args(request, subscribe, args);
487 afb_req_success(request, NULL, NULL);
489 afb_req_fail(request, "error", NULL);
492 void auth(afb_req_t request)
494 afb_req_session_set_LOA(request, 1);
495 afb_req_success(request, NULL, NULL);
498 void subscribe(afb_req_t request)
500 do_subscribe_unsubscribe(request, true);
503 void unsubscribe(afb_req_t request)
505 do_subscribe_unsubscribe(request, false);
509 static int send_frame(struct canfd_frame& cfd, const std::string& bus_name, socket_type type)
516 std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
518 if( cd.count(bus_name) == 0)
520 cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());
524 if(type == socket_type::BCM)
526 return low_can_subscription_t::tx_send(*cd[bus_name], cfd, bus_name);
528 else if(type == socket_type::J1939)
530 return low_can_subscription_t::j1939_send(*cd[bus_name], cfd, bus_name);
537 static int send_message(message_t *message, const std::string& bus_name, uint32_t flags, event_filter_t &event_filter, std::shared_ptr<signal_t> signal)
542 std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
544 if( cd.count(bus_name) == 0)
545 cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
547 cd[bus_name]->set_signal(signal);
550 if(flags&BCM_PROTOCOL)
551 return low_can_subscription_t::tx_send(*cd[bus_name], message, bus_name);
552 #ifdef USE_FEATURE_ISOTP
553 else if(flags&ISOTP_PROTOCOL)
554 return low_can_subscription_t::isotp_send(*cd[bus_name], message, bus_name);
556 #ifdef USE_FEATURE_J1939
557 else if(flags&J1939_PROTOCOL)
558 return low_can_subscription_t::j1939_send(*cd[bus_name], message, bus_name);
565 static void write_raw_frame(afb_req_t request, const std::string& bus_name, message_t *message,
566 struct json_object *can_data, uint32_t flags, event_filter_t &event_filter)
569 struct utils::signals_found sf;
571 utils::signals_manager_t::instance().lookup_signals_by_id(message->get_id(), application_t::instance().get_all_signals(), sf.signals);
573 if( !sf.signals.empty() )
575 AFB_DEBUG("ID WRITE RAW : %d", sf.signals.front()->get_message()->get_id());
576 if(flags & BCM_PROTOCOL)
578 if(sf.signals.front()->get_message()->is_fd())
580 AFB_DEBUG("CANFD_MAX_DLEN");
581 message->set_flags(CAN_FD_FRAME);
582 message->set_maxdlen(CANFD_MAX_DLEN);
586 AFB_DEBUG("CAN_MAX_DLEN");
587 message->set_maxdlen(CAN_MAX_DLEN);
590 if(sf.signals.front()->get_message()->is_isotp())
592 flags = ISOTP_PROTOCOL;
593 message->set_maxdlen(MAX_ISOTP_FRAMES * message->get_maxdlen());
597 #ifdef USE_FEATURE_J1939
598 if(flags&J1939_PROTOCOL)
599 message->set_maxdlen(J1939_MAX_DLEN);
602 if(message->get_length() > 0 && message->get_length() <= message->get_maxdlen())
604 std::vector<uint8_t> data;
605 for (int i = 0 ; i < message->get_length() ; i++)
607 struct json_object *one_can_data = json_object_array_get_idx(can_data, i);
608 data.push_back((json_object_is_type(one_can_data, json_type_int)) ?
609 (uint8_t)json_object_get_int(one_can_data) : 0);
611 message->set_data(data);
615 if(flags&BCM_PROTOCOL)
616 afb_req_fail(request, "Invalid", "Frame BCM");
617 else if(flags&J1939_PROTOCOL)
618 afb_req_fail(request, "Invalid", "Frame J1939");
619 else if(flags&ISOTP_PROTOCOL)
620 afb_req_fail(request, "Invalid", "Frame ISOTP");
622 afb_req_fail(request, "Invalid", "Frame");
627 if(! send_message(message, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name), flags, event_filter, sf.signals.front()))
628 afb_req_success(request, nullptr, "Message correctly sent");
630 afb_req_fail(request, "Error", "sending the message. See the log for more details.");
634 afb_req_fail(request, "Error", "no find id in signals. See the log for more details.");
639 static void write_frame(afb_req_t request, const std::string& bus_name, json_object *json_value, event_filter_t &event_filter)
644 struct json_object *can_data = nullptr;
645 std::vector<uint8_t> data;
647 AFB_DEBUG("JSON content %s", json_object_get_string(json_value));
649 if(!wrap_json_unpack(json_value, "{si, si, so !}",
652 "can_data", &can_data))
654 message = new can_message_t(0, id, length, false, 0, data, 0);
655 write_raw_frame(request, bus_name, message, can_data, BCM_PROTOCOL, event_filter);
657 #ifdef USE_FEATURE_J1939
658 else if(!wrap_json_unpack(json_value, "{si, si, so !}",
663 message = new j1939_message_t(length, data, 0, J1939_NO_NAME, (pgn_t)id, J1939_NO_ADDR);
664 write_raw_frame(request, bus_name, message, can_data, J1939_PROTOCOL, event_filter);
669 afb_req_fail(request, "Invalid", "Frame object malformed");
675 static void write_signal(afb_req_t request, const std::string& name, json_object *json_value, event_filter_t &event_filter)
677 struct canfd_frame cfd;
678 struct utils::signals_found sf;
679 signal_encoder encoder = nullptr;
682 ::memset(&cfd, 0, sizeof(cfd));
684 openxc_DynamicField search_key = build_DynamicField(name);
685 sf = utils::signals_manager_t::instance().find_signals(search_key);
686 openxc_DynamicField dynafield_value = build_DynamicField(json_value);
688 if (sf.signals.empty())
690 afb_req_fail_f(request, "No signal(s) found for %s. Message not sent.", name.c_str());
694 std::shared_ptr<signal_t> sig = sf.signals.front();
695 if(! sig->get_writable())
697 afb_req_fail_f(request, "%s isn't writable. Message not sent.", sig->get_name().c_str());
701 uint64_t value = (encoder = sig->get_encoder()) ?
702 encoder(*sig, dynafield_value, &send) :
703 encoder_t::encode_DynamicField(*sig, dynafield_value, &send);
705 uint32_t flags = INVALID_FLAG;
707 if(sig->get_message()->is_j1939())
708 flags = J1939_PROTOCOL;
709 else if(sig->get_message()->is_isotp())
710 flags = ISOTP_PROTOCOL;
712 flags = BCM_PROTOCOL;
714 // cfd = encoder_t::build_frame(sig, value);
715 message_t *message = encoder_t::build_message(sig, value, false, false);
717 if(! send_message(message, sig->get_message()->get_bus_device_name(), flags, event_filter, sig) && send)
718 afb_req_success(request, nullptr, "Message correctly sent");
720 afb_req_fail(request, "Error", "Sending the message. See the log for more details.");
722 if(sig->get_message()->is_j1939())
723 #ifdef USE_FEATURE_J1939
724 delete (j1939_message_t*) message;
726 afb_req_fail(request, "Warning", "J1939 not implemented in your kernel.");
729 delete (can_message_t*) message;
732 void write(afb_req_t request)
734 struct json_object* args = nullptr, *json_value = nullptr, *name = nullptr;
735 args = afb_req_json(request);
739 event_filter_t event_filter = generate_filter(args);
741 if(json_object_object_get_ex(args,"bus_name",&name))
743 if(json_object_object_get_ex(args,"frame",&json_value))
744 write_frame(request, (std::string)json_object_get_string(name), json_value, event_filter);
746 afb_req_fail(request, "Error", "Request argument malformed");
748 else if(json_object_object_get_ex(args,"signal_name",&name))
750 if(json_object_object_get_ex(args,"signal_value",&json_value))
751 write_signal(request, (std::string)json_object_get_string(name), json_value, event_filter);
753 afb_req_fail(request, "Error", "Request argument malformed");
757 afb_req_fail(request, "Error", "Request argument malformed");
762 afb_req_fail(request, "Error", "Request argument null");
766 static struct json_object *get_signals_value(const std::string& name)
768 struct utils::signals_found sf;
769 struct json_object *ans = nullptr;
771 openxc_DynamicField search_key = build_DynamicField(name);
772 sf = utils::signals_manager_t::instance().find_signals(search_key);
774 if (sf.signals.empty())
776 AFB_WARNING("No signal(s) found for %s.", name.c_str());
779 ans = json_object_new_array();
780 for(const auto& sig: sf.signals)
782 struct json_object *jobj = json_object_new_object();
783 json_object_object_add(jobj, "event", json_object_new_string(sig->get_name().c_str()));
784 json_object_object_add(jobj, "value", json_object_new_double(sig->get_last_value()));
785 json_object_array_add(ans, jobj);
790 void get(afb_req_t request)
793 struct json_object* args = nullptr,
794 *json_name = nullptr;
795 json_object *ans = nullptr;
797 args = afb_req_json(request);
799 // Process about Raw CAN message on CAN bus directly
800 if (args != nullptr &&
801 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string) ))
803 ans = get_signals_value(json_object_get_string(json_name));
809 AFB_ERROR("Request argument malformed. Please use the following syntax:");
814 afb_req_success(request, ans, NULL);
816 afb_req_fail(request, "error", NULL);
820 static struct json_object *list_can_message(const std::string& name)
822 struct utils::signals_found sf;
823 struct json_object *ans = nullptr;
825 openxc_DynamicField search_key = build_DynamicField(name);
826 sf = utils::signals_manager_t::instance().find_signals(search_key);
828 if (sf.signals.empty() && sf.diagnostic_messages.empty())
830 AFB_WARNING("No signal(s) found for %s.", name.c_str());
833 ans = json_object_new_array();
834 for(const auto& sig: sf.signals)
836 json_object_array_add(ans,
837 json_object_new_string(sig->get_name().c_str()));
839 for(const auto& sig: sf.diagnostic_messages)
841 json_object_array_add(ans,
842 json_object_new_string(sig->get_name().c_str()));
848 void list(afb_req_t request)
851 json_object *ans = nullptr;
852 struct json_object* args = nullptr,
853 *json_name = nullptr;
854 args = afb_req_json(request);
856 if ((args != nullptr) &&
857 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string)))
858 name = json_object_get_string(json_name);
862 ans = list_can_message(name);
867 afb_req_success(request, ans, NULL);
869 afb_req_fail(request, "error", NULL);
873 /// @brief Initialize the binding.
875 /// @param[in] service Structure which represent the Application Framework Binder.
877 /// @return Exit code, zero if success.
878 int init_binding(afb_api_t api)
881 application_t& application = application_t::instance();
882 can_bus_t& can_bus_manager = application.get_can_bus_manager();
884 can_bus_manager.set_can_devices();
885 can_bus_manager.start_threads();
886 utils::signals_manager_t& sm = utils::signals_manager_t::instance();
888 /// Initialize Diagnostic manager that will handle obd2 requests.
889 /// We pass by default the first CAN bus device to its Initialization.
890 /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
891 if(application_t::instance().get_diagnostic_manager().initialize())
894 // Add a recurring dignostic message request to get engine speed at all times.
895 openxc_DynamicField search_key = build_DynamicField("diagnostic_messages.engine.speed");
896 struct utils::signals_found sf = sm.find_signals(search_key);
898 if(sf.signals.empty() && sf.diagnostic_messages.size() == 1)
900 afb_req_t request = nullptr;
902 struct event_filter_t event_filter;
903 event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();
905 map_subscription& s = sm.get_subscribed_signals();
907 subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
911 #ifdef USE_FEATURE_J1939
912 std::string j1939_bus;
913 vect_ptr_msg_def_t current_messages_definition = application.get_messages_definition();
914 for(std::shared_ptr<message_definition_t> message_definition: current_messages_definition)
916 if(message_definition->is_j1939())
918 if (j1939_bus == message_definition->get_bus_device_name() )
920 j1939_bus = message_definition->get_bus_device_name();
922 std::shared_ptr<low_can_subscription_t> low_can_j1939 = std::make_shared<low_can_subscription_t>();
923 application.set_subscription_address_claiming(low_can_j1939);
925 ret = low_can_subscription_t::open_socket(*low_can_j1939,
927 J1939_ADDR_CLAIM_PROTOCOL);
931 AFB_ERROR("Error open socket address claiming for j1939 protocol");
934 add_to_event_loop(low_can_j1939);
941 AFB_ERROR("There was something wrong with CAN device Initialization.");