+
+/*
+static int send_frame(struct canfd_frame& cfd, const std::string& bus_name, socket_type type)
+{
+ if(bus_name.empty())
+ {
+ return -1;
+ }
+
+ std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
+
+ if( cd.count(bus_name) == 0)
+ {
+ cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());
+ }
+
+
+ if(type == socket_type::BCM)
+ {
+ return low_can_subscription_t::tx_send(*cd[bus_name], cfd, bus_name);
+ }
+ else if(type == socket_type::J1939)
+ {
+ return low_can_subscription_t::j1939_send(*cd[bus_name], cfd, bus_name);
+ }
+ else{
+ return -1;
+ }
+}
+*/
+static int send_message(message_t *message, const std::string& bus_name, uint32_t flags, event_filter_t &event_filter, std::shared_ptr<signal_t> signal)
+{
+ if(bus_name.empty())
+ return -1;
+
+ std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
+
+ if( cd.count(bus_name) == 0)
+ cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
+
+ cd[bus_name]->set_signal(signal);
+
+
+ if(flags&CAN_PROTOCOL)
+ return low_can_subscription_t::tx_send(*cd[bus_name], message, bus_name);
+#ifdef USE_FEATURE_ISOTP
+ else if(flags&ISOTP_PROTOCOL)
+ return low_can_subscription_t::isotp_send(*cd[bus_name], message, bus_name);
+#endif
+#ifdef USE_FEATURE_J1939
+ else if(flags&J1939_PROTOCOL)
+ return low_can_subscription_t::j1939_send(*cd[bus_name], message, bus_name);
+#endif
+ else
+ return -1;
+}
+
+
+static void write_raw_frame(afb_req_t request, const std::string& bus_name, message_t *message,
+ struct json_object *can_data, uint32_t flags, event_filter_t &event_filter)
+{
+
+ struct utils::signals_found sf;
+
+ utils::signals_manager_t::instance().lookup_signals_by_id(message->get_id(), application_t::instance().get_all_signals(), sf.signals);
+
+ if( !sf.signals.empty() )
+ {
+ AFB_DEBUG("ID WRITE RAW : %d", sf.signals.front()->get_message()->get_id());
+ if(flags & CAN_PROTOCOL)
+ {
+ if(sf.signals.front()->get_message()->is_fd())
+ {
+ AFB_DEBUG("CANFD_MAX_DLEN");
+ message->set_flags(CAN_PROTOCOL_WITH_FD_FRAME);
+ message->set_maxdlen(CANFD_MAX_DLEN);
+ }
+ else
+ {
+ AFB_DEBUG("CAN_MAX_DLEN");
+ message->set_maxdlen(CAN_MAX_DLEN);
+ }
+
+ if(sf.signals.front()->get_message()->is_isotp())
+ {
+ flags = ISOTP_PROTOCOL;
+ message->set_maxdlen(MAX_ISOTP_FRAMES * message->get_maxdlen());
+ }
+ }
+
+#ifdef USE_FEATURE_J1939
+ if(flags&J1939_PROTOCOL)
+ message->set_maxdlen(J1939_MAX_DLEN);
+#endif
+
+ if(message->get_length() > 0 && message->get_length() <= message->get_maxdlen())
+ {
+ std::vector<uint8_t> data;
+ for (int i = 0 ; i < message->get_length() ; i++)
+ {
+ struct json_object *one_can_data = json_object_array_get_idx(can_data, i);
+ data.push_back((json_object_is_type(one_can_data, json_type_int)) ?
+ (uint8_t)json_object_get_int(one_can_data) : 0);
+ }
+ message->set_data(data);
+ }
+ else
+ {
+ if(flags&CAN_PROTOCOL)
+ afb_req_fail(request, "Invalid", "Frame BCM");
+ else if(flags&J1939_PROTOCOL)
+ afb_req_fail(request, "Invalid", "Frame J1939");
+ else if(flags&ISOTP_PROTOCOL)
+ afb_req_fail(request, "Invalid", "Frame ISOTP");
+ else
+ afb_req_fail(request, "Invalid", "Frame");
+
+ return;
+ }
+
+ if(! send_message(message, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name), flags, event_filter, sf.signals.front()))
+ afb_req_success(request, nullptr, "Message correctly sent");
+ else
+ afb_req_fail(request, "Error", "sending the message. See the log for more details.");
+ }
+ else
+ {
+ afb_req_fail(request, "Error", "no find id in signals. See the log for more details.");
+ }
+
+}
+
+static void write_frame(afb_req_t request, const std::string& bus_name, json_object *json_value, event_filter_t &event_filter)
+{
+ message_t *message;
+ uint32_t id;
+ uint32_t length;
+ struct json_object *can_data = nullptr;
+ std::vector<uint8_t> data;
+
+ AFB_DEBUG("JSON content %s", json_object_get_string(json_value));
+
+ if(!wrap_json_unpack(json_value, "{si, si, so !}",
+ "can_id", &id,
+ "can_dlc", &length,
+ "can_data", &can_data))
+ {
+ message = new can_message_t(0, id, length, false, 0, data, 0);
+ write_raw_frame(request, bus_name, message, can_data, CAN_PROTOCOL, event_filter);
+ }
+#ifdef USE_FEATURE_J1939
+ else if(!wrap_json_unpack(json_value, "{si, si, so !}",
+ "pgn", &id,
+ "length", &length,
+ "data", &can_data))
+ {
+ message = new j1939_message_t(length, data, 0, J1939_NO_NAME, (pgn_t)id, J1939_NO_ADDR);
+ write_raw_frame(request, bus_name, message, can_data, J1939_PROTOCOL, event_filter);
+ }
+#endif
+ else
+ {
+ 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, event_filter_t &event_filter)
+{
+ 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.front();
+ 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);
+
+ uint32_t flags = INVALID_FLAG;
+
+ if(sig->get_message()->is_j1939())
+ flags = J1939_PROTOCOL;
+ else if(sig->get_message()->is_isotp())
+ flags = ISOTP_PROTOCOL;
+ else
+ flags = CAN_PROTOCOL;
+
+// cfd = encoder_t::build_frame(sig, value);
+ message_t *message = encoder_t::build_message(sig, value, false, false);
+
+ if(! send_message(message, sig->get_message()->get_bus_device_name(), flags, event_filter, sig) && 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;
+#else
+ afb_req_fail(request, "Warning", "J1939 not implemented in your kernel.");
+#endif
+ else
+ delete (can_message_t*) message;
+}
+
+void write(afb_req_t request)
+{
+ struct json_object* args = nullptr, *json_value = nullptr, *name = nullptr;
+ args = afb_req_json(request);
+
+ if(args != NULL)
+ {
+ event_filter_t event_filter = generate_filter(args);
+
+ if(json_object_object_get_ex(args,"bus_name",&name))
+ {
+ if(json_object_object_get_ex(args,"frame",&json_value))
+ write_frame(request, (std::string)json_object_get_string(name), json_value, event_filter);
+ else
+ afb_req_fail(request, "Error", "Request argument malformed");
+ }
+ else if(json_object_object_get_ex(args,"signal_name",&name))
+ {
+ if(json_object_object_get_ex(args,"signal_value",&json_value))
+ write_signal(request, (std::string)json_object_get_string(name), json_value, event_filter);
+ else
+ afb_req_fail(request, "Error", "Request argument malformed");
+ }
+ else
+ {
+ afb_req_fail(request, "Error", "Request argument malformed");
+ }
+ }
+ else
+ {
+ afb_req_fail(request, "Error", "Request argument 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.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)
+ 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 = 0;
+ application_t& application = application_t::instance();
+ can_bus_t& can_bus_manager = application.get_can_bus_manager();
+
+ if(application.get_message_set().empty())
+ {
+ AFB_ERROR("No message_set defined");
+ return -1;
+ }
+
+ can_bus_manager.start_threads();
+ utils::signals_manager_t& sm = utils::signals_manager_t::instance();
+
+ if (application.get_diagnostic_manager().is_initialized())
+ {
+ // 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
+ std::string j1939_bus;
+ 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())
+ {
+ if (j1939_bus == message_definition->get_bus_device_name() )
+ continue;
+ j1939_bus = message_definition->get_bus_device_name();
+
+ 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,
+ j1939_bus,
+ J1939_ADDR_CLAIM_PROTOCOL);
+
+ if(ret < 0)
+ {
+ AFB_ERROR("Error open socket address claiming for j1939 protocol");
+ return -1;
+ }
+ add_to_event_loop(low_can_j1939);
+ break;
+ }
+ }
+#endif
+
+ if(ret)
+ AFB_ERROR("There was something wrong with CAN device Initialization.");
+
+ return ret;
+}
+
+int load_config(afb_api_t api)
+{
+ int ret = 0;
+ CtlConfigT *ctlConfig;
+ const char *dirList = getenv("CONTROL_CONFIG_PATH");
+ std::string bindingDirPath = GetBindingDirPath(api);
+ std::string filepath = bindingDirPath + "/etc";
+
+ if (!dirList)
+ dirList=CONTROL_CONFIG_PATH;
+
+ filepath.append(":");
+ filepath.append(dirList);
+ const char *configPath = CtlConfigSearch(api, filepath.c_str(), "control");
+
+ if (!configPath)
+ {
+ AFB_ERROR_V3("CtlPreInit: No control-%s* config found invalid JSON %s ", GetBinderName(), filepath.c_str());
+ return -1;
+ }
+
+ // create one API per file
+ ctlConfig = CtlLoadMetaData(api, configPath);
+ if (!ctlConfig)
+ {
+ AFB_ERROR_V3("CtrlPreInit No valid control config file in:\n-- %s", configPath);
+ return -1;
+ }
+
+ // Save the config in the api userdata field
+ afb_api_set_userdata(api, ctlConfig);
+
+ setExternalData(ctlConfig, (void*) &application_t::instance());
+ ret= CtlLoadSections(api, ctlConfig, ctlSections_);
+
+ return ret;
+}