summaryrefslogtreecommitdiffstats
path: root/low-can-binding/binding/low-can-cb.cpp
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-07-12 17:40:36 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-07-21 12:02:10 +0200
commit38f9b3de40b1d1d8e9ed5db0fee40bdaa763d577 (patch)
tree65a4357036ae32acefb725d1217c89aea3502c53 /low-can-binding/binding/low-can-cb.cpp
parentbe9884ab7d3c138e0adae6abd5a3a0dc4350a9fd (diff)
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 <romain.forlot@iot.bzh>
Diffstat (limited to 'low-can-binding/binding/low-can-cb.cpp')
-rw-r--r--low-can-binding/binding/low-can-cb.cpp60
1 files changed, 60 insertions, 0 deletions
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<std::string, std::shared_ptr<low_can_socket_t> >& 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);
+}