Be able to write raw frame & encode value to sent
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-cb.cpp
index 97b6d20..5910f6a 100644 (file)
@@ -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"
@@ -105,7 +106,7 @@ static int make_subscription_unsubscription(struct afb_req request, std::shared_
        /* Make the subscription or unsubscription to the event */
        if (((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s[can_subscription->get_index()]->get_event())) < 0)
        {
-               ERROR("Operation goes wrong for signal: %s", can_subscription->get_name().c_str());
+               AFB_ERROR("Operation goes wrong for signal: %s", can_subscription->get_name().c_str());
                return -1;
        }
        return 0;
@@ -118,7 +119,7 @@ static int create_event_handle(std::shared_ptr<low_can_subscription_t>& can_subs
        s[sub_index] = can_subscription;
        if (!afb_event_is_valid(s[sub_index]->get_event()))
        {
-               ERROR("Can't create an event for %s, something goes wrong.", can_subscription->get_name().c_str());
+               AFB_ERROR("Can't create an event for %s, something goes wrong.", can_subscription->get_name().c_str());
                return -1;
        }
        return 0;
@@ -136,7 +137,7 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe,
        {
                if (!afb_event_is_valid(s[sub_index]->get_event()) && !subscribe)
                {
-                       NOTICE("Event isn't valid, no need to unsubscribed.");
+                       AFB_NOTICE("Event isn't valid, no need to unsubscribed.");
                        ret = -1;
                }
                ret = 0;
@@ -192,22 +193,22 @@ static int subscribe_unsubscribe_diagnostic_messages(struct afb_req request, boo
                        diag_m.add_recurring_request(diag_req, sig->get_name().c_str(), false, sig->get_decoder(), sig->get_callback(), event_filter.frequency);
                        if(can_subscription->create_rx_filter(sig) < 0)
                                {return -1;}
-                       DEBUG("Signal: %s subscribed", sig->get_name().c_str());
+                       AFB_DEBUG("Signal: %s subscribed", sig->get_name().c_str());
                        if(it == s.end() && add_to_event_loop(can_subscription) < 0)
                        {
                                diag_m.cleanup_request(
                                        diag_m.find_recurring_request(*diag_req), true);
-                               WARNING("signal: %s isn't supported. Canceling operation.",  sig->get_name().c_str());
+                               AFB_WARNING("signal: %s isn't supported. Canceling operation.",  sig->get_name().c_str());
                                return -1;
                        }
                }
                else
                {
                        if(sig->get_supported())
-                       {DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str());}
+                       {AFB_DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str());}
                        else
                        {
-                               WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
+                               AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
                                return -1;
                        }
                }
@@ -221,6 +222,7 @@ static int subscribe_unsubscribe_diagnostic_messages(struct afb_req request, boo
        return rets;
 }
 
+// TODO: Create separate subscrition object if event_filter isn't the same.
 static int subscribe_unsubscribe_can_signals(struct afb_req request, bool subscribe, std::vector<std::shared_ptr<can_signal_t> > can_signals, struct event_filter_t& event_filter, std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
 {
        int rets = 0;
@@ -245,7 +247,7 @@ static int subscribe_unsubscribe_can_signals(struct afb_req request, bool subscr
                        {return -1;}
 
                rets++;
-               DEBUG("signal: %s subscribed", sig->get_name().c_str());
+               AFB_DEBUG("signal: %s subscribed", sig->get_name().c_str());
        }
        return rets;
 }
@@ -307,9 +309,12 @@ static int one_subscribe_unsubscribe(struct afb_req request, bool subscribe, con
        openxc_DynamicField search_key = build_DynamicField(tag);
        sf = utils::signals_manager_t::instance().find_signals(search_key);
        if (sf.can_signals.empty() && sf.diagnostic_messages.empty())
-               NOTICE("No signal(s) found for %s.", tag.c_str());
+       {
+               AFB_NOTICE("No signal(s) found for %s.", tag.c_str());
+               ret = -1;
+       }
        else
-               ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);
+               {ret = subscribe_unsubscribe_signals(request, subscribe, sf, event_filter);}
 
        return ret;
 }
@@ -347,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);
@@ -356,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<std::string, std::shared_ptr<low_can_socket_t> >& 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>(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>(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);
+}