X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Fbinding%2Flow-can-cb.cpp;h=2b630845e4ca03134a5b01a1535304c87c3c6fac;hb=refs%2Fchanges%2F42%2F22142%2F3;hp=b6a18ebb4de787415d16a15b8a0fa72d54d0b04b;hpb=c88f115014d9e75e4d1cc0db9762f11ca8532afb;p=apps%2Fagl-service-can-low-level.git diff --git a/low-can-binding/binding/low-can-cb.cpp b/low-can-binding/binding/low-can-cb.cpp index b6a18ebb..2b630845 100644 --- a/low-can-binding/binding/low-can-cb.cpp +++ b/low-can-binding/binding/low-can-cb.cpp @@ -26,7 +26,6 @@ #include #include #include - #include "openxc.pb.h" #include "application.hpp" #include "../can/can-encoder.hpp" @@ -37,6 +36,10 @@ #include "../diagnostic/diagnostic-message.hpp" #include "../utils/openxc-utils.hpp" +#ifdef USE_FEATURE_J1939 + #include "../can/message/j1939-message.hpp" + #include +#endif ///****************************************************************************** /// /// SystemD event loop Callbacks @@ -82,18 +85,27 @@ static void push_n_notify(std::shared_ptr m) int read_message(sd_event_source *event_source, int fd, uint32_t revents, void *userdata) { + low_can_subscription_t* can_subscription = (low_can_subscription_t*)userdata; if ((revents & EPOLLIN) != 0) { - std::shared_ptr s = can_subscription->get_socket(); - std::shared_ptr message = s->read_message(); - - // Sure we got a valid CAN message ? - if (! message->get_id() == 0 && ! message->get_length() == 0) + utils::signals_manager_t& sm = utils::signals_manager_t::instance(); + std::lock_guard subscribed_signals_lock(sm.get_subscribed_signals_mutex()); + if(can_subscription->get_index() != -1) { - push_n_notify(message); + std::shared_ptr s = can_subscription->get_socket(); + if(s->socket() && s->socket() != -1) + { + std::shared_ptr message = s->read_message(); + + // Sure we got a valid CAN message ? + if (! message->get_id() == 0 && ! message->get_length() == 0 && message->get_msg_format() != message_format_t::INVALID) + { + push_n_notify(message); + } + } } } @@ -139,8 +151,7 @@ static int subscribe_unsubscribe_signal(afb_req_t request, } // Event doesn't exist , so let's create it - if (! subscription_exists && - (ret = can_subscription->subscribe(request)) < 0) + if ((ret = can_subscription->subscribe(request)) < 0) return ret; if(! subscription_exists) @@ -164,8 +175,8 @@ static int subscribe_unsubscribe_signal(afb_req_t request, if( (ret = s[sub_index]->unsubscribe(request)) < 0) return ret; + s.find(sub_index)->second->set_index(-1); s.erase(sub_index); - return ret; } @@ -197,7 +208,10 @@ static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request, event_filter.frequency = event_filter.frequency == 0 ? sig->get_frequency() : event_filter.frequency; std::shared_ptr can_subscription; - auto it = std::find_if(s.begin(), s.end(), [&sig](std::pair > sub){ return (! sub.second->get_diagnostic_message().empty());}); + auto it = std::find_if(s.begin(), s.end(), [&sig](std::pair > sub) + { + return (! sub.second->get_diagnostic_message().empty()); + }); can_subscription = it != s.end() ? it->second : std::make_shared(low_can_subscription_t(event_filter)); @@ -224,7 +238,9 @@ static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request, else { if(sig->get_supported()) - {AFB_DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str());} + { + AFB_DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str()); + } else { AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str()); @@ -237,7 +253,6 @@ static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request, rets++; } - return rets; } @@ -250,7 +265,10 @@ static int subscribe_unsubscribe_signals(afb_req_t request, int rets = 0; for(const auto& sig: signals) { - auto it = std::find_if(s.begin(), s.end(), [&sig, &event_filter](std::pair > sub){ return sub.second->is_signal_subscription_corresponding(sig, event_filter) ; }); + auto it = std::find_if(s.begin(), s.end(), [&sig, &event_filter](std::pair > sub) + { + return sub.second->is_signal_subscription_corresponding(sig, event_filter) ; + }); std::shared_ptr can_subscription; if(it != s.end()) {can_subscription = it->second;} @@ -374,14 +392,14 @@ static int process_one_subscribe_args(afb_req_t request, bool subscribe, json_ob // 2 cases : ID(PGN) and event - json_bool test_event = json_object_object_get_ex(args,"event",&event); + json_object_object_get_ex(args,"event",&event); json_bool test_id = json_object_object_get_ex(args,"id",&id); if(!test_id) { - json_object_object_get_ex(args,"pgn",&id); + test_id = json_object_object_get_ex(args,"pgn",&id); } - if( args == NULL || (!test_event && !id)) + if( args == NULL || (id && ((std::string)json_object_get_string(id)).compare("*") == 0)) { rc = one_subscribe_unsubscribe_events(request, subscribe, "*", args); } @@ -467,55 +485,146 @@ void unsubscribe(afb_req_t request) do_subscribe_unsubscribe(request, false); } -static int send_frame(struct canfd_frame& cfd, const std::string& bus_name) +/* +static int send_frame(struct canfd_frame& cfd, const std::string& bus_name, socket_type type) { - if(bus_name.empty()) { + if(bus_name.empty()) + { return -1; } std::map >& cd = application_t::instance().get_can_devices(); if( cd.count(bus_name) == 0) - {cd[bus_name] = std::make_shared(low_can_subscription_t());} + { + cd[bus_name] = std::make_shared(low_can_subscription_t()); + } - return low_can_subscription_t::tx_send(*cd[bus_name], cfd, bus_name); -} -static void write_raw_frame(afb_req_t request, const std::string& bus_name, json_object *json_value) + 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, socket_type type) { - struct canfd_frame cfd; - struct json_object *can_data = nullptr; + if(bus_name.empty()) + { + return -1; + } - ::memset(&cfd, 0, sizeof(cfd)); + std::map >& cd = application_t::instance().get_can_devices(); - if(wrap_json_unpack(json_value, "{si, si, so !}", - "can_id", &cfd.can_id, - "can_dlc", &cfd.len, - "can_data", &can_data)) + if( cd.count(bus_name) == 0) { - afb_req_fail(request, "Invalid", "Frame object malformed"); - return; + cd[bus_name] = std::make_shared(low_can_subscription_t()); } - if(cfd.len <= 8 && cfd.len > 0) + + if(type == socket_type::BCM) + { + return low_can_subscription_t::tx_send(*cd[bus_name], message, bus_name); + } +#ifdef USE_FEATURE_J1939 + else if(type == socket_type::J1939) { - for (int i = 0 ; i < cfd.len ; i++) + 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, socket_type type) +{ + if((message->get_length() <= 8 && message->get_length() > 0 && type == socket_type::BCM) +#ifdef USE_FEATURE_J1939 + || (message->get_length() < J1939_MAX_DLEN && type == socket_type::J1939) +#endif + ) + { + + std::vector data; + for (int i = 0 ; i < message->get_length() ; 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; + 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 { - afb_req_fail(request, "Invalid", "Data array must hold 1 to 8 values."); + if(type == socket_type::BCM) + { + afb_req_fail(request, "Invalid", "Data array must hold 1 to 8 values."); + } + else if(type == socket_type::J1939) + { + afb_req_fail(request, "Invalid", "Data array too large"); + } + else + { + afb_req_fail(request, "Invalid", "Invalid socket type"); + } return; } - if(! send_frame(cfd, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name))) + if(! send_message(message, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name),type)) + { 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_frame(afb_req_t request, const std::string& bus_name, json_object *json_value) +{ + message_t *message; + int id; + int length; + struct json_object *can_data = nullptr; + std::vector 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(CANFD_MAX_DLEN,(uint32_t)id,(uint32_t)length,message_format_t::STANDARD,false,0,data,0); + write_raw_frame(request,bus_name,message,can_data,socket_type::BCM); + } +#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(J1939_MAX_DLEN,(uint32_t)length,message_format_t::J1939,data,0,J1939_NO_NAME,(pgn_t)id,J1939_NO_ADDR); + write_raw_frame(request,bus_name,message,can_data,socket_type::J1939); + } +#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) @@ -548,11 +657,39 @@ static void write_signal(afb_req_t request, const std::string& name, json_object 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) + 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) @@ -566,7 +703,7 @@ void write(afb_req_t request) if (args != NULL && ! wrap_json_unpack(args, "{ss, so !}", "bus_name", &name, "frame", &json_value)) - write_raw_frame(request, name, json_value); + write_frame(request, name, json_value); // Search signal then encode value. else if(args != NULL && @@ -683,9 +820,13 @@ void list(afb_req_t request) rc = -1; if (rc >= 0) + { afb_req_success(request, ans, NULL); + } else + { afb_req_fail(request, "error", NULL); + } } /// @brief Initialize the binding. @@ -695,11 +836,13 @@ void list(afb_req_t request) /// @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(); + 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. @@ -709,7 +852,7 @@ int init_binding(afb_api_t api) // 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); + struct utils::signals_found sf = sm.find_signals(search_key); if(sf.signals.empty() && sf.diagnostic_messages.size() == 1) { @@ -718,14 +861,43 @@ int init_binding(afb_api_t api) 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 >& s = sm.get_subscribed_signals(); subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true); } + +#ifdef USE_FEATURE_J1939 + std::vector> current_messages_definition = application.get_messages_definition(); + for(std::shared_ptr message_definition: current_messages_definition) + { + if(message_definition->is_j1939()) + { + std::shared_ptr low_can_j1939 = std::make_shared(); + + application.set_subscription_address_claiming(low_can_j1939); + + ret = low_can_subscription_t::open_socket(*low_can_j1939, + message_definition->get_bus_device_name(), + socket_type::J1939_ADDR_CLAIM); + if(ret < 0) + { + AFB_ERROR("Error open socket address claiming for j1939 protocol"); + return -1; + } + +// std::shared_ptr 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; }