+ }
+}
+
+/// @brief Initialize the binding.
+///
+/// @param[in] service Structure which represent the Application Framework Binder.
+///
+/// @return Exit code, zero if success.
+int init_binding(afb_api_t api)
+{
+ int ret = 1;
+ application_t& application = application_t::instance();
+ can_bus_t& can_bus_manager = application.get_can_bus_manager();
+
+ can_bus_manager.set_can_devices();
+ can_bus_manager.start_threads();
+ utils::signals_manager_t& sm = utils::signals_manager_t::instance();
+
+ /// Initialize Diagnostic manager that will handle obd2 requests.
+ /// We pass by default the first CAN bus device to its Initialization.
+ /// TODO: be able to choose the CAN bus device that will be use as Diagnostic bus.
+ if(application_t::instance().get_diagnostic_manager().initialize())
+ ret = 0;
+
+ // Add a recurring dignostic message request to get engine speed at all times.
+ openxc_DynamicField search_key = build_DynamicField("diagnostic_messages.engine.speed");
+ struct utils::signals_found sf = sm.find_signals(search_key);
+
+ if(sf.signals.empty() && sf.diagnostic_messages.size() == 1)
+ {
+ afb_req_t request = nullptr;
+
+ struct event_filter_t event_filter;
+ event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();
+
+ std::map<int, std::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
+
+ subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
+ }
+
+
+#ifdef USE_FEATURE_J1939
+ std::vector<std::shared_ptr<message_definition_t>> current_messages_definition = application.get_messages_definition();
+ for(std::shared_ptr<message_definition_t> message_definition: current_messages_definition)
+ {
+ if(message_definition->is_j1939())
+ {
+ std::shared_ptr<low_can_subscription_t> low_can_j1939 = std::make_shared<low_can_subscription_t>();
+
+ application.set_subscription_address_claiming(low_can_j1939);
+
+ ret = low_can_subscription_t::open_socket(*low_can_j1939,
+ message_definition->get_bus_device_name(),
+ socket_type::J1939_ADDR_CLAIM);
+ if(ret < 0)
+ {
+ AFB_ERROR("Error open socket address claiming for j1939 protocol");
+ return -1;
+ }
+
+// std::shared_ptr<low_can_subscription_t> saddrclaim = application.get_subscription_address_claiming();
+
+ add_to_event_loop(low_can_j1939);
+ break;
+ }
+ }
+#endif
+
+ if(ret)
+ {
+ AFB_ERROR("There was something wrong with CAN device Initialization.");
+ }
+
+ return ret;