X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=low-can-binding%2Fbinding%2Flow-can-cb.cpp;h=5910f6a2583b8af944546f7c72fca15dbbb53a19;hb=a483f245eb1aae07917c9074568c8cda82855ff6;hp=ec8b2b467e05076e387ed9fa9bf22644276115e8;hpb=9295346830bae064dddb4e0a12a69ddc9c120123;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 ec8b2b46..5910f6a2 100644 --- a/low-can-binding/binding/low-can-cb.cpp +++ b/low-can-binding/binding/low-can-cb.cpp @@ -28,6 +28,7 @@ #include "openxc.pb.h" #include "application.hpp" +#include "../can/can-encoder.hpp" #include "../can/can-bus.hpp" #include "../can/can-signals.hpp" #include "../can/can-message.hpp" @@ -351,6 +352,12 @@ static void do_subscribe_unsubscribe(struct afb_req request, bool subscribe) afb_req_fail(request, "error", NULL); } +void auth(struct afb_req request) +{ + afb_req_session_set_LOA(request, 1); + afb_req_success(request, NULL, NULL); +} + void subscribe(struct afb_req request) { do_subscribe_unsubscribe(request, true); @@ -360,3 +367,96 @@ void unsubscribe(struct afb_req request) { do_subscribe_unsubscribe(request, false); } + +void swrite(struct afb_req request) +{ + int rc = 0; + struct can_frame cf; + struct utils::signals_found sf; + struct json_object* args = nullptr, + *json_name = nullptr, + *json_value = nullptr; + std::map >& cd = application_t::instance().get_can_devices(); + + ::memset(&cf, 0, sizeof(cf)); + + 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) )) + { + cf.can_id = json_object_get_int(json_can_id); + cf.can_dlc = (uint8_t)json_object_get_int(json_can_dlc); + + struct json_object *x; + int n = json_object_array_length(json_can_data); + if(n <= 8) + { + for (int i = 0 ; i < n ; i++) + { + x = json_object_array_get_idx(json_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 bus_name = json_object_get_string(json_name); + const std::string found_device = application_t::instance().get_can_bus_manager().get_can_device_name(bus_name); + if( ! found_device.empty()) + { + if( cd.count(bus_name) == 0) + {cd[bus_name] = std::make_shared(low_can_socket_t());} + rc = cd[bus_name]->tx_send(cf, found_device); + } + } + 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)))) + { + openxc_DynamicField search_key = build_DynamicField(json_object_get_string(json_name)); + sf = utils::signals_manager_t::instance().find_signals(search_key); + + if (sf.can_signals.empty()) + { + AFB_WARNING("No signal(s) found for id %d. Message not sent.", cf.can_id); + rc = -1; + } + else + { + for(const auto& sig: sf.can_signals) + { + cf = encoder_t::build_frame(sig, (uint64_t)json_object_get_double(json_value)); + const std::string bus_name = sig->get_message()->get_bus_name(); + if( cd.count(bus_name) == 0) + {cd[bus_name] = std::make_shared(low_can_socket_t());} + rc = cd[bus_name]->tx_send(cf, sig); + } + } + } + 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); +}