From 38f9b3de40b1d1d8e9ed5db0fee40bdaa763d577 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Wed, 12 Jul 2017 17:40:36 +0200 Subject: Implements new verbs to write on CAN bus This will open a new BCM socket not dedicated to a subscription use to that mean. Change-Id: Idf353015f731e32d6eb7c7856bd73167c66e790f Signed-off-by: Romain Forlot --- low-can-binding/binding/low-can-cb.cpp | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'low-can-binding/binding/low-can-cb.cpp') diff --git a/low-can-binding/binding/low-can-cb.cpp b/low-can-binding/binding/low-can-cb.cpp index ec8b2b4..e25d6ea 100644 --- a/low-can-binding/binding/low-can-cb.cpp +++ b/low-can-binding/binding/low-can-cb.cpp @@ -360,3 +360,63 @@ 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, *json_can_socket, *json_can_id, *json_can_dlc, *json_can_data; + + ::memset(&cf, 0, sizeof(cf)); + + args = afb_req_json(request); + if (args == NULL || ( + ((!json_object_object_get_ex(args, "canbus_name", &json_can_socket)) && json_object_get_type(json_can_socket) == json_type_string) && + ((!json_object_object_get_ex(args, "can_id", &json_can_id)) && json_object_get_type(json_can_id) == json_type_int) && + ((!json_object_object_get_ex(args, "can_dlc", &json_can_dlc)) && json_object_get_type(json_can_dlc) == json_type_int))) + { + cf.can_id = json_object_get_int(json_can_id); + cf.can_dlc = (uint8_t)json_object_get_int(json_can_dlc); + openxc_DynamicField search_key = build_DynamicField((double)cf.can_id); + sf = utils::signals_manager_t::instance().find_signals(search_key); + } + + if((args == NULL || !json_object_object_get_ex(args, "can_data", &json_can_data)) && json_object_get_type(json_can_data) == json_type_array) + { + 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; + } + } + } + + if (sf.can_signals.empty() && sf.diagnostic_messages.empty()) + { + AFB_WARNING("No signal(s) found for id %d. Message not sent.", cf.can_id); + rc = -1; + } + else + { + std::map >& cd = application_t::instance().get_can_devices(); + const char* can_socket = json_object_get_string(json_can_socket); + for(const auto& sig: sf.can_signals) + { + if (sig->get_message()->get_bus_name().c_str() == can_socket) + { + rc = cd[can_socket]->tx_send(cf, sig); + } + } + } + + if (rc >= 0) + afb_req_success(request, NULL, NULL); + else + afb_req_fail(request, "error", NULL); +} -- cgit 1.2.3-korg