+
+static int send_frame(const std::string& bus_name, const struct can_frame& cf)
+{
+ std::map<std::string, std::shared_ptr<low_can_socket_t> >& cd = application_t::instance().get_can_devices();
+
+ if( cd.count(bus_name) == 0)
+ {cd[bus_name] = std::make_shared<low_can_socket_t>(low_can_socket_t());}
+
+ return cd[bus_name]->tx_send(cf, bus_name);
+}
+
+static int write_raw_frame(const std::string& bus_name, uint32_t can_id, uint8_t can_dlc, struct json_object* can_data)
+{
+ int rc = 0;
+ struct can_frame cf;
+
+ ::memset(&cf, 0, sizeof(cf));
+
+ cf.can_id = can_id;
+ cf.can_dlc = can_dlc;
+
+ struct json_object *x;
+ size_t n = json_object_array_length(can_data);
+ if(n <= 8)
+ {
+ for (int i = 0 ; i < n ; i++)
+ {
+ x = json_object_array_get_idx(can_data, i);
+ cf.data[i] = json_object_get_type(x) == json_type_int ? (uint8_t)json_object_get_int(x) : 0;
+ }
+ }
+
+ const std::string found_device = application_t::instance().get_can_bus_manager().get_can_device_name(bus_name);
+ if( ! found_device.empty())
+ {
+ rc = send_frame(found_device, cf);
+ }
+
+ return rc;
+}
+static int write_signal(const std::string& name, uint64_t value)
+{
+ int rc = 0;
+ struct can_frame cf;
+ struct utils::signals_found sf;
+
+ ::memset(&cf, 0, sizeof(cf));
+
+ openxc_DynamicField search_key = build_DynamicField(name);
+ sf = utils::signals_manager_t::instance().find_signals(search_key);
+
+ if (sf.can_signals.empty())
+ {
+ AFB_WARNING("No signal(s) found for %s. Message not sent.", name.c_str());
+ rc = -1;
+ }
+ else
+ {
+ for(const auto& sig: sf.can_signals)
+ {
+ if(sig->get_writable())
+ {
+ cf = encoder_t::build_frame(sig, value);
+ const std::string bus_name = sig->get_message()->get_bus_device_name();
+ rc = send_frame(bus_name, cf);
+ }
+ else
+ {
+ AFB_WARNING("%s isn't writable. Message not sent.", sig->get_name().c_str());
+ return -1;
+ }
+ }
+ }
+
+ return rc;
+}
+
+void write(afb_req_t request)
+{
+ int rc = 0;
+ struct json_object* args = nullptr,
+ *json_name = nullptr,
+ *json_value = nullptr;
+
+ args = afb_req_json(request);
+
+ // Process about Raw CAN message on CAN bus directly
+ if (args != NULL &&
+ (json_object_object_get_ex(args, "bus_name", &json_name) && json_object_is_type(json_name, json_type_string) ) &&
+ (json_object_object_get_ex(args, "frame", &json_value) && json_object_is_type(json_value, json_type_object) ))
+ {
+ struct json_object* json_can_id = nullptr,
+ *json_can_dlc = nullptr,
+ *json_can_data = nullptr;
+
+ if( (json_object_object_get_ex(json_value, "can_id", &json_can_id) && (json_object_is_type(json_can_id, json_type_double) || json_object_is_type(json_can_id, json_type_int))) &&
+ (json_object_object_get_ex(json_value, "can_dlc", &json_can_dlc) && (json_object_is_type(json_can_dlc, json_type_double) || json_object_is_type(json_can_dlc, json_type_int))) &&
+ (json_object_object_get_ex(json_value, "can_data", &json_can_data) && json_object_is_type(json_can_data, json_type_array) ))
+ {
+ rc = write_raw_frame(json_object_get_string(json_name),
+ json_object_get_int(json_can_id),
+ (uint8_t)json_object_get_int(json_can_dlc),
+ json_can_data);
+ }
+ else
+ {
+ AFB_ERROR("Frame object malformed (must be \n \"frame\": {\"can_id\": int, \"can_dlc\": int, \"can_data\": [ int, int , int, int ,int , int ,int ,int]}");
+ rc = -1;
+ }
+ }
+ // Search signal then encode value.
+ else if(args != NULL &&
+ (json_object_object_get_ex(args, "signal_name", &json_name) && json_object_is_type(json_name, json_type_string)) &&
+ (json_object_object_get_ex(args, "signal_value", &json_value) && (json_object_is_type(json_value, json_type_double) || json_object_is_type(json_value, json_type_int))))
+ {
+ rc = write_signal(json_object_get_string(json_name),
+ (uint64_t)json_object_get_double(json_value));
+ }
+ else
+ {
+ AFB_ERROR("Request argument malformed. Please use the following syntax:");
+ rc = -1;
+ }
+
+ if (rc >= 0)
+ afb_req_success(request, NULL, NULL);
+ else
+ afb_req_fail(request, "error", NULL);
+}
+
+static struct json_object *get_signals_value(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.can_signals.empty())
+ {
+ AFB_WARNING("No signal(s) found for %s.", name.c_str());
+ return NULL;
+ }
+ ans = json_object_new_array();
+ for(const auto& sig: sf.can_signals)
+ {
+ struct json_object *jobj = json_object_new_object();
+ json_object_object_add(jobj, "event", json_object_new_string(sig->get_name().c_str()));
+ json_object_object_add(jobj, "value", json_object_new_double(sig->get_last_value()));
+ json_object_array_add(ans, jobj);
+ }
+
+ return ans;
+}
+void get(afb_req_t request)
+{
+ int rc = 0;
+ struct json_object* args = nullptr,
+ *json_name = nullptr;
+ json_object *ans = nullptr;
+
+ args = afb_req_json(request);
+
+ // Process about Raw CAN message on CAN bus directly
+ if (args != nullptr &&
+ (json_object_object_get_ex(args, "event", &json_name) && json_object_is_type(json_name, json_type_string) ))
+ {
+ ans = get_signals_value(json_object_get_string(json_name));
+ if (!ans)
+ rc = -1;
+ }
+ else
+ {
+ AFB_ERROR("Request argument malformed. Please use the following syntax:");
+ rc = -1;
+ }
+
+ if (rc >= 0)
+ afb_req_success(request, ans, NULL);
+ else
+ afb_req_fail(request, "error", NULL);
+}
+
+
+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.can_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.can_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)
+{
+ uint32_t ret = 1;
+ can_bus_t& can_bus_manager = application_t::instance().get_can_bus_manager();
+
+ can_bus_manager.set_can_devices();
+ can_bus_manager.start_threads();
+
+ /// 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 = utils::signals_manager_t::instance().find_signals(search_key);
+
+ if(sf.can_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();
+
+ utils::signals_manager_t& sm = utils::signals_manager_t::instance();
+ 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);
+ }
+
+ if(ret)
+ AFB_ERROR("There was something wrong with CAN device Initialization.");
+
+ return ret;
+}