+ afb_req_fail(request, "Invalid", "Frame object malformed");
+ return;
+ }
+ delete message;
+}
+
+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.signals.empty())
+ {
+ afb_req_fail_f(request, "No signal(s) found for %s. Message not sent.", name.c_str());
+ return;
+ }
+
+ std::shared_ptr<signal_t>& sig = sf.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);
+
+ socket_type type = socket_type::INVALID;
+
+ if(sig->get_message()->is_j1939())
+ {
+ type = socket_type::J1939;
+ }
+ else
+ {
+ type = socket_type::BCM;
+ }
+
+// cfd = encoder_t::build_frame(sig, value);
+ message_t *message = encoder_t::build_message(sig,value);
+
+ if(! send_message(message, sig->get_message()->get_bus_device_name(), type) && send)
+ {
+ afb_req_success(request, nullptr, "Message correctly sent");
+ }
+ else
+ {
+ afb_req_fail(request, "Error", "Sending the message. See the log for more details.");
+ }
+
+ if(sig->get_message()->is_j1939())
+ {
+#ifdef USE_FEATURE_J1939
+ delete (j1939_message_t*) message;
+#endif
+ }
+ else
+ {
+ delete (can_message_t*) message;
+ }
+}
+
+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_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.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.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.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)