+
+
+static struct json_object *list_can_message(const std::string& name)
+{
+ struct utils::signals_found sf;
+ struct json_object *ans = nullptr;
+
+ openxc_DynamicField search_key = build_DynamicField(name);
+ sf = utils::signals_manager_t::instance().find_signals(search_key);
+
+ if (sf.signals.empty() && sf.diagnostic_messages.empty())
+ {
+ AFB_WARNING("No signal(s) found for %s.", name.c_str());
+ return NULL;
+ }
+ ans = json_object_new_array();
+ for(const auto& sig: sf.signals)
+ {
+ json_object_array_add(ans,
+ json_object_new_string(sig->get_name().c_str()));
+ }
+ for(const auto& sig: sf.diagnostic_messages)
+ {
+ json_object_array_add(ans,
+ json_object_new_string(sig->get_name().c_str()));
+ }
+
+ return ans;
+}
+
+void list(afb_req_t request)
+{
+ int rc = 0;
+ json_object *ans = nullptr;
+ struct json_object* args = nullptr,
+ *json_name = nullptr;
+ args = afb_req_json(request);
+ const char *name;
+ if ((args != nullptr) &&
+ (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string)))
+ {
+ name = json_object_get_string(json_name);
+ }
+ else
+ {
+ name = "*";
+ }
+
+ ans = list_can_message(name);
+ if (!ans)
+ rc = -1;
+
+ if (rc >= 0)
+ {
+ afb_req_success(request, ans, NULL);
+ }
+ else
+ {
+ afb_req_fail(request, "error", NULL);
+ }
+}
+
+/// @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();
+
+ map_subscription& s = sm.get_subscribed_signals();
+
+ subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
+ }
+
+
+#ifdef USE_FEATURE_J1939
+ vect_ptr_msg_def_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(),
+ J1939_ADDR_CLAIM_PROTOCOL);
+ 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;
+}