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"
28 #include <wrap-json.h>
29 #include <systemd/sd-event.h>
30 #include <ctl-config.h>
31 #include "openxc.pb.h"
32 #include "application.hpp"
33 #include "../can/can-encoder.hpp"
34 #include "../can/can-bus.hpp"
35 #include "../can/signals.hpp"
36 #include "../can/message/message.hpp"
37 #include "../utils/signals.hpp"
38 #include "../diagnostic/diagnostic-message.hpp"
39 #include "../utils/openxc-utils.hpp"
40 #include "../utils/signals.hpp"
42 #ifdef USE_FEATURE_J1939
43 #include "../can/message/j1939-message.hpp"
44 #include <linux/can/j1939.h>
47 ///*****************************************************************************
49 /// Controller Definitions and Callbacks
51 ///****************************************************************************/
53 int config_low_can(afb_api_t apiHandle, CtlSectionT *section, json_object *json_obj)
55 AFB_DEBUG("Config %s", json_object_to_json_string(json_obj));
56 CtlConfigT *ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle);
57 int active_message_set = 0;
58 json_object *dev_mapping = nullptr;
59 const char *diagnotic_bus = nullptr;
61 if(! ctrlConfig || ! ctrlConfig->external)
64 application_t *application = (application_t*) ctrlConfig->external;
66 if(wrap_json_unpack(json_obj, "{si, s?s}",
67 "active_message_set", &active_message_set,
68 "diagnostic_bus", &diagnotic_bus))
71 application->set_active_message_set((uint8_t)active_message_set);
73 if(wrap_json_unpack(json_obj, "{so}",
74 "dev-mapping", &dev_mapping))
77 application->get_can_bus_manager().set_can_devices(dev_mapping);
79 /// Initialize Diagnostic manager that will handle obd2 requests.
80 /// We pass by default the first CAN bus device to its Initialization.
81 if(! diagnotic_bus || application_t::instance().get_diagnostic_manager().initialize(diagnotic_bus))
82 AFB_WARNING("Diagnostic Manager: not initialized");
87 CtlSectionT ctlSections_[] = {
88 [0]={.key="config" , .uid="config", .info=nullptr,
89 .loadCB=config_low_can,
92 [1]={.key="plugins" , .uid="plugins", .info=nullptr,
96 [2]={.key=nullptr , .uid=nullptr, .info=nullptr,
102 ///*****************************************************************************
104 /// Subscription and unsubscription
106 ///****************************************************************************/
108 /// @brief This will determine if an event handle needs to be created and checks if
109 /// we got a valid afb_event to get subscribe or unsubscribe. After that launch the subscription or unsubscription
110 /// against the application framework using that event handle.
111 static int subscribe_unsubscribe_signal(afb_req_t request,
113 std::shared_ptr<low_can_subscription_t>& can_subscription,
117 int sub_index = can_subscription->get_index();
118 bool subscription_exists = s.count(sub_index);
123 /* There is no valid request to subscribe so this must be an
124 * internal permanent diagnostic request. Skip the subscription
125 * part and don't register it into the current "low-can"
133 // Event doesn't exist , so let's create it
134 if ((ret = can_subscription->subscribe(request)) < 0)
137 if(! subscription_exists)
138 s[sub_index] = can_subscription;
143 // Unsubscrition part
144 if(! subscription_exists)
146 AFB_NOTICE("There isn't any valid subscriptions for that request.");
149 else if (subscription_exists &&
150 ! afb_event_is_valid(s[sub_index]->get_event()) )
152 AFB_NOTICE("Event isn't valid, no need to unsubscribed.");
156 if( (ret = s[sub_index]->unsubscribe(request)) < 0)
158 s.find(sub_index)->second->set_index(-1);
163 static int add_to_event_loop(std::shared_ptr<low_can_subscription_t>& can_subscription)
165 struct sd_event_source* event_source = nullptr;
166 return ( sd_event_add_io(afb_daemon_get_event_loop(),
168 can_subscription->get_socket()->socket(),
171 can_subscription.get()));
174 static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request,
176 list_ptr_diag_msg_t diagnostic_messages,
177 struct event_filter_t& event_filter,
179 bool perm_rec_diag_req)
182 application_t& app = application_t::instance();
183 diagnostic_manager_t& diag_m = app.get_diagnostic_manager();
185 for(const auto& sig : diagnostic_messages)
187 DiagnosticRequest* diag_req = new DiagnosticRequest(sig->build_diagnostic_request());
188 event_filter.frequency = event_filter.frequency == 0 ? sig->get_frequency() : event_filter.frequency;
189 std::shared_ptr<low_can_subscription_t> can_subscription;
191 auto it = std::find_if(s.begin(), s.end(), [&sig](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub)
193 return (! sub.second->get_diagnostic_message().empty());
195 can_subscription = it != s.end() ?
197 std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
198 // If the requested diagnostic message is not supported by the car then unsubcribe it
199 // no matter what we want, worst case will be a failed unsubscription but at least we won't
200 // poll a PID for nothing.
201 if(sig->get_supported() && subscribe)
203 if (!app.is_engine_on())
204 AFB_WARNING("signal: Engine is off, %s won't received responses until it's on", sig->get_name().c_str());
206 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);
207 if(can_subscription->create_rx_filter(sig) < 0)
209 AFB_DEBUG("Signal: %s subscribed", sig->get_name().c_str());
210 if(it == s.end() && add_to_event_loop(can_subscription) < 0)
212 diag_m.cleanup_request(
213 diag_m.find_recurring_request(*diag_req), true);
214 AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
220 if(sig->get_supported())
222 AFB_DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str());
226 AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
230 int ret = subscribe_unsubscribe_signal(request, subscribe, can_subscription, s);
239 static int subscribe_unsubscribe_signals(afb_req_t request,
241 list_ptr_signal_t signals,
242 struct event_filter_t& event_filter,
246 for(const auto& sig: signals)
248 auto it = std::find_if(s.begin(), s.end(), [&sig, &event_filter](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub)
250 return sub.second->is_signal_subscription_corresponding(sig, event_filter) ;
252 std::shared_ptr<low_can_subscription_t> can_subscription;
254 can_subscription = it->second;
257 can_subscription = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
258 if(can_subscription->create_rx_filter(sig) < 0)
260 if(add_to_event_loop(can_subscription) < 0)
264 if(subscribe_unsubscribe_signal(request, subscribe, can_subscription, s) < 0)
268 AFB_DEBUG("%s Signal: %s %ssubscribed", sig->get_message()->is_fd() ? "FD": "", sig->get_name().c_str(), subscribe ? "":"un");
274 /// @brief subscribe to all signals in the vector signals
276 /// @param[in] afb_req request : contains original request use to subscribe or unsubscribe
277 /// @param[in] subscribe boolean value, which chooses between a subscription operation or an unsubscription
278 /// @param[in] signals - struct containing vectors with signal_t and diagnostic_messages to subscribe
279 /// @param[in] event_filter - stuct containing filter on the signal
281 /// @return Number of correctly subscribed signal
283 static int subscribe_unsubscribe_signals(afb_req_t request,
285 const struct utils::signals_found& signals,
286 struct event_filter_t& event_filter)
289 utils::signals_manager_t& sm = utils::signals_manager_t::instance();
291 std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
292 map_subscription& s = sm.get_subscribed_signals();
294 rets += subscribe_unsubscribe_diagnostic_messages(request, subscribe, signals.diagnostic_messages, event_filter, s, false);
295 rets += subscribe_unsubscribe_signals(request, subscribe, signals.signals, event_filter, s);
300 static event_filter_t generate_filter(json_object* args)
302 event_filter_t event_filter;
303 struct json_object *filter, *obj;
305 // computes the filter
306 if (json_object_object_get_ex(args, "filter", &filter))
308 if (json_object_object_get_ex(filter, "frequency", &obj)
309 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
310 event_filter.frequency = (float)json_object_get_double(obj);
311 if (json_object_object_get_ex(filter, "min", &obj)
312 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
313 event_filter.min = (float)json_object_get_double(obj);
314 if (json_object_object_get_ex(filter, "max", &obj)
315 && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
316 event_filter.max = (float)json_object_get_double(obj);
317 if (json_object_object_get_ex(filter, "rx_id", &obj)
318 && (json_object_is_type(obj, json_type_int)))
319 event_filter.rx_id = (canid_t) json_object_get_int(obj);
320 if (json_object_object_get_ex(filter, "tx_id", &obj)
321 && (json_object_is_type(obj, json_type_int)))
322 event_filter.tx_id = (canid_t) json_object_get_int(obj);
328 static int one_subscribe_unsubscribe_events(afb_req_t request, bool subscribe, const std::string& tag, json_object* args)
331 struct utils::signals_found sf;
333 // subscribe or unsubscribe
334 openxc_DynamicField search_key = build_DynamicField(tag);
335 sf = utils::signals_manager_t::instance().find_signals(search_key);
338 #ifdef USE_FEATURE_ISOTP
339 if(sf.signals.size() > 1)
341 sf.signals.remove_if([](std::shared_ptr<signal_t> x){
342 bool isotp = x->get_message()->is_isotp();
344 AFB_NOTICE("ISO TP messages need to be subscribed one by one (rx, tx)");
351 if (sf.signals.empty() && sf.diagnostic_messages.empty())
353 AFB_NOTICE("No signal(s) found for %s.", tag.c_str());
358 event_filter_t event_filter = generate_filter(args);
359 ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
364 static int one_subscribe_unsubscribe_id(afb_req_t request, bool subscribe, const uint32_t& id, json_object *args)
367 std::shared_ptr<message_definition_t> message_definition = application_t::instance().get_message_definition(id);
368 struct utils::signals_found sf;
370 if(message_definition)
371 sf.signals = list_ptr_signal_t(message_definition->get_signals().begin(), message_definition->get_signals().end());
373 if(sf.signals.empty())
375 AFB_NOTICE("No signal(s) found for %d.", id);
380 event_filter_t event_filter = generate_filter(args);
381 ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
388 static int process_one_subscribe_args(afb_req_t request, bool subscribe, json_object *args)
391 json_object *x = nullptr, *event = nullptr, *id = nullptr;
394 // 2 cases : ID(PGN) and event
396 json_object_object_get_ex(args,"event",&event);
397 json_object_object_get_ex(args,"id",&id) || json_object_object_get_ex(args,"pgn",&id);
399 if( args == NULL || (id && ((std::string)json_object_get_string(id)).compare("*") == 0))
401 rc = one_subscribe_unsubscribe_events(request, subscribe, "*", args);
407 if (json_object_get_type(event) != json_type_array) // event is set before and check if it's an array
409 rc = one_subscribe_unsubscribe_events(request, subscribe, json_object_get_string(event), args);
411 else // event is set and it's not an array
413 for (int i = 0 ; i < json_object_array_length(event); i++)
415 x = json_object_array_get_idx(event, i);
416 rc2 = one_subscribe_unsubscribe_events(request, subscribe, json_object_get_string(x), args);
418 rc = rc2 >= 0 ? rc + rc2 : rc2;
425 if (json_object_get_type(id) != json_type_array) // id is set before and check if it's an array
427 rc = one_subscribe_unsubscribe_id(request, subscribe, json_object_get_int(id), args);
429 else // event is set and it's not an array
431 for (int i = 0 ; i < json_object_array_length(id); i++)
433 x = json_object_array_get_idx(id, i);
434 rc2 = one_subscribe_unsubscribe_id(request, subscribe, json_object_get_int(x), args);
436 rc = rc2 >= 0 ? rc + rc2 : rc2;
444 static void do_subscribe_unsubscribe(afb_req_t request, bool subscribe)
447 struct json_object *args, *x;
449 args = afb_req_json(request);
450 if (json_object_get_type(args) == json_type_array)
452 for(int i = 0; i < json_object_array_length(args); i++)
454 x = json_object_array_get_idx(args, i);
455 rc += process_one_subscribe_args(request, subscribe, x);
460 rc += process_one_subscribe_args(request, subscribe, args);
464 afb_req_success(request, NULL, NULL);
466 afb_req_fail(request, "error", NULL);
469 void auth(afb_req_t request)
471 afb_req_session_set_LOA(request, 1);
472 afb_req_success(request, NULL, NULL);
475 void subscribe(afb_req_t request)
477 do_subscribe_unsubscribe(request, true);
480 void unsubscribe(afb_req_t request)
482 do_subscribe_unsubscribe(request, false);
486 static int send_frame(struct canfd_frame& cfd, const std::string& bus_name, socket_type type)
493 std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
495 if( cd.count(bus_name) == 0)
497 cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());
501 if(type == socket_type::BCM)
503 return low_can_subscription_t::tx_send(*cd[bus_name], cfd, bus_name);
505 else if(type == socket_type::J1939)
507 return low_can_subscription_t::j1939_send(*cd[bus_name], cfd, bus_name);
514 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)
519 std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
521 if( cd.count(bus_name) == 0)
522 cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
524 cd[bus_name]->set_signal(signal);
527 if(flags&CAN_PROTOCOL)
528 return low_can_subscription_t::tx_send(*cd[bus_name], message, bus_name);
529 #ifdef USE_FEATURE_ISOTP
530 else if(flags&ISOTP_PROTOCOL)
531 return low_can_subscription_t::isotp_send(*cd[bus_name], message, bus_name);
533 #ifdef USE_FEATURE_J1939
534 else if(flags&J1939_PROTOCOL)
535 return low_can_subscription_t::j1939_send(*cd[bus_name], message, bus_name);
542 static void write_raw_frame(afb_req_t request, const std::string& bus_name, message_t *message,
543 struct json_object *can_data, uint32_t flags, event_filter_t &event_filter)
546 struct utils::signals_found sf;
548 utils::signals_manager_t::instance().lookup_signals_by_id(message->get_id(), application_t::instance().get_all_signals(), sf.signals);
550 if( !sf.signals.empty() )
552 AFB_DEBUG("ID WRITE RAW : %d", sf.signals.front()->get_message()->get_id());
553 if(flags & CAN_PROTOCOL)
555 if(sf.signals.front()->get_message()->is_fd())
557 AFB_DEBUG("CANFD_MAX_DLEN");
558 message->set_flags(CAN_PROTOCOL_WITH_FD_FRAME);
559 message->set_maxdlen(CANFD_MAX_DLEN);
563 AFB_DEBUG("CAN_MAX_DLEN");
564 message->set_maxdlen(CAN_MAX_DLEN);
567 if(sf.signals.front()->get_message()->is_isotp())
569 flags = ISOTP_PROTOCOL;
570 message->set_maxdlen(MAX_ISOTP_FRAMES * message->get_maxdlen());
574 #ifdef USE_FEATURE_J1939
575 if(flags&J1939_PROTOCOL)
576 message->set_maxdlen(J1939_MAX_DLEN);
579 if(message->get_length() > 0 && message->get_length() <= message->get_maxdlen())
581 std::vector<uint8_t> data;
582 for (int i = 0 ; i < message->get_length() ; i++)
584 struct json_object *one_can_data = json_object_array_get_idx(can_data, i);
585 data.push_back((json_object_is_type(one_can_data, json_type_int)) ?
586 (uint8_t)json_object_get_int(one_can_data) : 0);
588 message->set_data(data);
592 if(flags&CAN_PROTOCOL)
593 afb_req_fail(request, "Invalid", "Frame BCM");
594 else if(flags&J1939_PROTOCOL)
595 afb_req_fail(request, "Invalid", "Frame J1939");
596 else if(flags&ISOTP_PROTOCOL)
597 afb_req_fail(request, "Invalid", "Frame ISOTP");
599 afb_req_fail(request, "Invalid", "Frame");
604 if(! send_message(message, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name), flags, event_filter, sf.signals.front()))
605 afb_req_success(request, nullptr, "Message correctly sent");
607 afb_req_fail(request, "Error", "sending the message. See the log for more details.");
611 afb_req_fail(request, "Error", "no find id in signals. See the log for more details.");
616 static void write_frame(afb_req_t request, const std::string& bus_name, json_object *json_value, event_filter_t &event_filter)
621 struct json_object *can_data = nullptr;
622 std::vector<uint8_t> data;
624 AFB_DEBUG("JSON content %s", json_object_get_string(json_value));
626 if(!wrap_json_unpack(json_value, "{si, si, so !}",
629 "can_data", &can_data))
631 message = new can_message_t(0, id, length, false, 0, data, 0);
632 write_raw_frame(request, bus_name, message, can_data, CAN_PROTOCOL, event_filter);
634 #ifdef USE_FEATURE_J1939
635 else if(!wrap_json_unpack(json_value, "{si, si, so !}",
640 message = new j1939_message_t(length, data, 0, J1939_NO_NAME, (pgn_t)id, J1939_NO_ADDR);
641 write_raw_frame(request, bus_name, message, can_data, J1939_PROTOCOL, event_filter);
646 afb_req_fail(request, "Invalid", "Frame object malformed");
652 static void write_signal(afb_req_t request, const std::string& name, json_object *json_value, event_filter_t &event_filter)
654 struct canfd_frame cfd;
655 struct utils::signals_found sf;
656 signal_encoder encoder = nullptr;
659 ::memset(&cfd, 0, sizeof(cfd));
661 openxc_DynamicField search_key = build_DynamicField(name);
662 sf = utils::signals_manager_t::instance().find_signals(search_key);
663 openxc_DynamicField dynafield_value = build_DynamicField(json_value);
665 if (sf.signals.empty())
667 afb_req_fail_f(request, "No signal(s) found for %s. Message not sent.", name.c_str());
671 std::shared_ptr<signal_t> sig = sf.signals.front();
672 if(! sig->get_writable())
674 afb_req_fail_f(request, "%s isn't writable. Message not sent.", sig->get_name().c_str());
678 uint64_t value = (encoder = sig->get_encoder()) ?
679 encoder(*sig, dynafield_value, &send) :
680 encoder_t::encode_DynamicField(*sig, dynafield_value, &send);
682 uint32_t flags = INVALID_FLAG;
684 if(sig->get_message()->is_j1939())
685 flags = J1939_PROTOCOL;
686 else if(sig->get_message()->is_isotp())
687 flags = ISOTP_PROTOCOL;
689 flags = CAN_PROTOCOL;
691 // cfd = encoder_t::build_frame(sig, value);
692 message_t *message = encoder_t::build_message(sig, value, false, false);
694 if(! send_message(message, sig->get_message()->get_bus_device_name(), flags, event_filter, sig) && send)
695 afb_req_success(request, nullptr, "Message correctly sent");
697 afb_req_fail(request, "Error", "Sending the message. See the log for more details.");
699 if(sig->get_message()->is_j1939())
700 #ifdef USE_FEATURE_J1939
701 delete (j1939_message_t*) message;
703 afb_req_fail(request, "Warning", "J1939 not implemented in your kernel.");
706 delete (can_message_t*) message;
709 void write(afb_req_t request)
711 struct json_object* args = nullptr, *json_value = nullptr, *name = nullptr;
712 args = afb_req_json(request);
716 event_filter_t event_filter = generate_filter(args);
718 if(json_object_object_get_ex(args,"bus_name",&name))
720 if(json_object_object_get_ex(args,"frame",&json_value))
721 write_frame(request, (std::string)json_object_get_string(name), json_value, event_filter);
723 afb_req_fail(request, "Error", "Request argument malformed");
725 else if(json_object_object_get_ex(args,"signal_name",&name))
727 if(json_object_object_get_ex(args,"signal_value",&json_value))
728 write_signal(request, (std::string)json_object_get_string(name), json_value, event_filter);
730 afb_req_fail(request, "Error", "Request argument malformed");
734 afb_req_fail(request, "Error", "Request argument malformed");
739 afb_req_fail(request, "Error", "Request argument null");
743 static struct json_object *get_signals_value(const std::string& name)
745 struct utils::signals_found sf;
746 struct json_object *ans = nullptr;
748 openxc_DynamicField search_key = build_DynamicField(name);
749 sf = utils::signals_manager_t::instance().find_signals(search_key);
751 if (sf.signals.empty())
753 AFB_WARNING("No signal(s) found for %s.", name.c_str());
756 ans = json_object_new_array();
757 for(const auto& sig: sf.signals)
759 struct json_object *jobj = json_object_new_object();
760 json_object_object_add(jobj, "event", json_object_new_string(sig->get_name().c_str()));
761 json_object_object_add(jobj, "value", json_object_new_double(sig->get_last_value()));
762 json_object_array_add(ans, jobj);
767 void get(afb_req_t request)
770 struct json_object* args = nullptr,
771 *json_name = nullptr;
772 json_object *ans = nullptr;
774 args = afb_req_json(request);
776 // Process about Raw CAN message on CAN bus directly
777 if (args != nullptr &&
778 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string) ))
780 ans = get_signals_value(json_object_get_string(json_name));
786 AFB_ERROR("Request argument malformed. Please use the following syntax:");
791 afb_req_success(request, ans, NULL);
793 afb_req_fail(request, "error", NULL);
797 static struct json_object *list_can_message(const std::string& name)
799 struct utils::signals_found sf;
800 struct json_object *ans = nullptr;
802 openxc_DynamicField search_key = build_DynamicField(name);
803 sf = utils::signals_manager_t::instance().find_signals(search_key);
805 if (sf.signals.empty() && sf.diagnostic_messages.empty())
807 AFB_WARNING("No signal(s) found for %s.", name.c_str());
810 ans = json_object_new_array();
811 for(const auto& sig: sf.signals)
813 json_object_array_add(ans,
814 json_object_new_string(sig->get_name().c_str()));
816 for(const auto& sig: sf.diagnostic_messages)
818 json_object_array_add(ans,
819 json_object_new_string(sig->get_name().c_str()));
825 void list(afb_req_t request)
828 json_object *ans = nullptr;
829 struct json_object* args = nullptr,
830 *json_name = nullptr;
831 args = afb_req_json(request);
833 if ((args != nullptr) &&
834 (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string)))
835 name = json_object_get_string(json_name);
839 ans = list_can_message(name);
844 afb_req_success(request, ans, NULL);
846 afb_req_fail(request, "error", NULL);
850 /// @brief Initialize the binding.
852 /// @param[in] service Structure which represent the Application Framework Binder.
854 /// @return Exit code, zero if success.
855 int init_binding(afb_api_t api)
858 application_t& application = application_t::instance();
859 can_bus_t& can_bus_manager = application.get_can_bus_manager();
861 if(application.get_message_set().empty())
863 AFB_ERROR("No message_set defined");
867 can_bus_manager.start_threads();
868 utils::signals_manager_t& sm = utils::signals_manager_t::instance();
870 if (application.get_diagnostic_manager().is_initialized())
872 // Add a recurring dignostic message request to get engine speed at all times.
873 openxc_DynamicField search_key = build_DynamicField("diagnostic_messages.engine.speed");
874 struct utils::signals_found sf = sm.find_signals(search_key);
876 if(sf.signals.empty() && sf.diagnostic_messages.size() == 1)
878 afb_req_t request = nullptr;
880 struct event_filter_t event_filter;
881 event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();
883 map_subscription& s = sm.get_subscribed_signals();
885 subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
889 #ifdef USE_FEATURE_J1939
890 std::string j1939_bus;
891 vect_ptr_msg_def_t current_messages_definition = application.get_messages_definition();
892 for(std::shared_ptr<message_definition_t> message_definition: current_messages_definition)
894 if(message_definition->is_j1939())
896 if (j1939_bus == message_definition->get_bus_device_name() )
898 j1939_bus = message_definition->get_bus_device_name();
900 std::shared_ptr<low_can_subscription_t> low_can_j1939 = std::make_shared<low_can_subscription_t>();
901 application.set_subscription_address_claiming(low_can_j1939);
903 ret = low_can_subscription_t::open_socket(*low_can_j1939,
905 J1939_ADDR_CLAIM_PROTOCOL);
909 AFB_ERROR("Error open socket address claiming for j1939 protocol");
912 add_to_event_loop(low_can_j1939);
919 AFB_ERROR("There was something wrong with CAN device Initialization.");
924 int load_config(afb_api_t api)
927 CtlConfigT *ctlConfig;
928 const char *dirList = getenv("CONTROL_CONFIG_PATH");
929 std::string bindingDirPath = GetBindingDirPath(api);
930 std::string filepath = bindingDirPath + "/etc";
933 dirList=CONTROL_CONFIG_PATH;
935 filepath.append(":");
936 filepath.append(dirList);
937 const char *configPath = CtlConfigSearch(api, filepath.c_str(), "control");
941 AFB_ERROR_V3("CtlPreInit: No control-%s* config found invalid JSON %s ", GetBinderName(), filepath.c_str());
945 // create one API per file
946 ctlConfig = CtlLoadMetaData(api, configPath);
949 AFB_ERROR_V3("CtrlPreInit No valid control config file in:\n-- %s", configPath);
953 // Save the config in the api userdata field
954 afb_api_set_userdata(api, ctlConfig);
956 setExternalData(ctlConfig, (void*) &application_t::instance());
957 ret= CtlLoadSections(api, ctlConfig, ctlSections_);