+
+static int send_frame(struct canfd_frame& cfd, const std::string& bus_name)
+{
+ if(bus_name.empty()) {
+ return -1;
+ }
+
+ 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(cfd, bus_name);
+}
+
+static void write_raw_frame(afb_req_t request, const std::string& bus_name, json_object *json_value)
+{
+ struct canfd_frame cfd;
+ struct json_object *can_data = nullptr;
+
+ ::memset(&cfd, 0, sizeof(cfd));
+
+ if(wrap_json_unpack(json_value, "{si, si, so !}",
+ "can_id", &cfd.can_id,
+ "can_dlc", &cfd.len,
+ "can_data", &can_data))
+ {
+ afb_req_fail(request, "Invalid", "Frame object malformed");
+ return;
+ }
+
+ if(cfd.len <= 8 && cfd.len > 0)
+ {
+ for (int i = 0 ; i < cfd.len ; i++)
+ {
+ struct json_object *one_can_data = json_object_array_get_idx(can_data, i);
+ cfd.data[i] = (json_object_is_type(one_can_data, json_type_int)) ?
+ (uint8_t)json_object_get_int(one_can_data) : 0;
+ }
+ }
+ else
+ {
+ afb_req_fail(request, "Invalid", "Data array must hold 1 to 8 values.");
+ return;
+ }
+
+ if(! send_frame(cfd, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name)))
+ afb_req_success(request, nullptr, "Message correctly sent");
+ else
+ afb_req_fail(request, "Error", "sending the message. See the log for more details.");
+}
+
+static void write_signal(afb_req_t request, const std::string& name, json_object *json_value)
+{
+ struct canfd_frame cfd;
+ struct utils::signals_found sf;
+ signal_encoder encoder = nullptr;
+ bool send = true;
+
+ ::memset(&cfd, 0, sizeof(cfd));
+
+ openxc_DynamicField search_key = build_DynamicField(name);
+ sf = utils::signals_manager_t::instance().find_signals(search_key);
+ openxc_DynamicField dynafield_value = build_DynamicField(json_value);
+
+ if (sf.can_signals.empty())
+ {
+ afb_req_fail_f(request, "No signal(s) found for %s. Message not sent.", name.c_str());
+ return;
+ }
+
+ std::shared_ptr<can_signal_t>& sig = sf.can_signals[0];
+ if(! sig->get_writable())
+ {
+ afb_req_fail_f(request, "%s isn't writable. Message not sent.", sig->get_name().c_str());
+ return;
+ }
+
+ uint64_t value = (encoder = sig->get_encoder()) ?
+ encoder(*sig, dynafield_value, &send) :
+ encoder_t::encode_DynamicField(*sig, dynafield_value, &send);
+
+ cfd = encoder_t::build_frame(sig, value);
+ if(! send_frame(cfd, sig->get_message()->get_bus_device_name()) && send)
+ afb_req_success(request, nullptr, "Message correctly sent");
+ else
+ afb_req_fail(request, "Error", "Sending the message. See the log for more details.");
+}
+
+void write(afb_req_t request)
+{
+ struct json_object* args = nullptr, *json_value = nullptr;
+ const char *name = nullptr;
+
+ args = afb_req_json(request);
+
+ // Process about Raw CAN message on CAN bus directly
+ if (args != NULL && ! wrap_json_unpack(args, "{ss, so !}",
+ "bus_name", &name,
+ "frame", &json_value))
+ write_raw_frame(request, name, json_value);
+
+ // Search signal then encode value.
+ else if(args != NULL &&
+ ! wrap_json_unpack(args, "{ss, so !}",
+ "signal_name", &name,
+ "signal_value", &json_value))
+ write_signal(request, std::string(name), json_value);
+ else
+ afb_req_fail(request, "Error", "Request argument malformed");
+}
+
+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;
+}