Remove file socketcan-j1939 that was not removed before
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-cb.cpp
index c677b17..fd57867 100644 (file)
@@ -47,7 +47,7 @@
 ///
 ///*******************************************************************************/
 
-void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, uint32_t pid, std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
+void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, uint32_t pid, map_subscription& s)
 {
        bool is_permanent_recurring_request = false;
 
@@ -68,7 +68,7 @@ void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, uin
                on_no_clients(can_subscription, s);
 }
 
-void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
+void on_no_clients(std::shared_ptr<low_can_subscription_t> can_subscription, map_subscription& s)
 {
        auto it = s.find(can_subscription->get_index());
        s.erase(it);
@@ -132,7 +132,7 @@ int read_message(sd_event_source *event_source, int fd, uint32_t revents, void *
 static int subscribe_unsubscribe_signal(afb_req_t request,
                                        bool subscribe,
                                        std::shared_ptr<low_can_subscription_t>& can_subscription,
-                                       std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
+                                       map_subscription& s)
 {
        int ret = 0;
        int sub_index = can_subscription->get_index();
@@ -194,9 +194,9 @@ static int add_to_event_loop(std::shared_ptr<low_can_subscription_t>& can_subscr
 
 static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request,
                                                         bool subscribe,
-                                                        std::vector<std::shared_ptr<diagnostic_message_t> > diagnostic_messages,
+                                                        list_ptr_diag_msg_t diagnostic_messages,
                                                         struct event_filter_t& event_filter,
-                                                        std::map<int, std::shared_ptr<low_can_subscription_t> >& s,
+                                                        map_subscription& s,
                                                         bool perm_rec_diag_req)
 {
        int rets = 0;
@@ -259,9 +259,9 @@ static int subscribe_unsubscribe_diagnostic_messages(afb_req_t request,
 
 static int subscribe_unsubscribe_signals(afb_req_t request,
                                                 bool subscribe,
-                                                std::vector<std::shared_ptr<signal_t> > signals,
+                                                list_ptr_signal_t signals,
                                                 struct event_filter_t& event_filter,
-                                                std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
+                                                map_subscription& s)
 {
        int rets = 0;
        for(const auto& sig: signals)
@@ -297,6 +297,7 @@ static int subscribe_unsubscribe_signals(afb_req_t request,
 /// @param[in] afb_req request : contains original request use to subscribe or unsubscribe
 /// @param[in] subscribe boolean value, which chooses between a subscription operation or an unsubscription
 /// @param[in] signals -  struct containing vectors with signal_t and diagnostic_messages to subscribe
+/// @param[in] event_filter - stuct containing filter on the signal
 ///
 /// @return Number of correctly subscribed signal
 ///
@@ -309,7 +310,7 @@ static int subscribe_unsubscribe_signals(afb_req_t request,
        utils::signals_manager_t& sm = utils::signals_manager_t::instance();
 
        std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
-       std::map<int, std::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
+       map_subscription& s = sm.get_subscribed_signals();
 
        rets += subscribe_unsubscribe_diagnostic_messages(request, subscribe, signals.diagnostic_messages, event_filter, s, false);
        rets += subscribe_unsubscribe_signals(request, subscribe, signals.signals, event_filter, s);
@@ -334,6 +335,12 @@ static event_filter_t generate_filter(json_object* args)
                if (json_object_object_get_ex(filter, "max", &obj)
                && (json_object_is_type(obj, json_type_double) || json_object_is_type(obj, json_type_int)))
                        {event_filter.max = (float)json_object_get_double(obj);}
+               if (json_object_object_get_ex(filter, "rx_id", &obj)
+               && (json_object_is_type(obj, json_type_int)))
+                       {event_filter.rx_id = (canid_t) json_object_get_int(obj);}
+               if (json_object_object_get_ex(filter, "tx_id", &obj)
+               && (json_object_is_type(obj, json_type_int)))
+                       {event_filter.tx_id = (canid_t) json_object_get_int(obj);}
        }
        return event_filter;
 }
@@ -347,6 +354,22 @@ static int one_subscribe_unsubscribe_events(afb_req_t request, bool subscribe, c
        // subscribe or unsubscribe
        openxc_DynamicField search_key = build_DynamicField(tag);
        sf = utils::signals_manager_t::instance().find_signals(search_key);
+
+
+#ifdef USE_FEATURE_ISOTP
+       if(sf.signals.size() > 1)
+       {
+               sf.signals.remove_if([](std::shared_ptr<signal_t> x){
+                       bool isotp = x->get_message()->is_isotp();
+                       if(isotp)
+                       {
+                               AFB_NOTICE("ISO TP messages need to be subscribed one by one (rx, tx)");
+                       }
+                       return isotp;
+               });
+       }
+#endif
+
        if (sf.signals.empty() && sf.diagnostic_messages.empty())
        {
                AFB_NOTICE("No signal(s) found for %s.", tag.c_str());
@@ -368,7 +391,7 @@ static int one_subscribe_unsubscribe_id(afb_req_t request, bool subscribe, const
 
        if(message_definition)
        {
-               sf.signals = message_definition->get_signals();
+               sf.signals = list_ptr_signal_t(message_definition->get_signals().begin(),message_definition->get_signals().end());
        }
 
        if(sf.signals.empty())
@@ -515,7 +538,7 @@ static int send_frame(struct canfd_frame& cfd, const std::string& bus_name, sock
        }
 }
 */
-static int send_message(message_t *message, const std::string& bus_name, uint32_t flags)
+static int send_message(message_t *message, const std::string& bus_name, uint32_t flags, event_filter_t &event_filter, std::shared_ptr<signal_t> signal)
 {
        if(bus_name.empty())
        {
@@ -526,14 +549,22 @@ static int send_message(message_t *message, const std::string& bus_name, uint32_
 
        if( cd.count(bus_name) == 0)
        {
-               cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());
+               cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
        }
 
+       cd[bus_name]->set_signal(signal);
+
 
        if(flags&BCM_PROTOCOL)
        {
                return low_can_subscription_t::tx_send(*cd[bus_name], message, bus_name);
        }
+#ifdef USE_FEATURE_ISOTP
+       else if(flags&ISOTP_PROTOCOL)
+       {
+               return low_can_subscription_t::isotp_send(*cd[bus_name], message, bus_name);
+       }
+#endif
 #ifdef USE_FEATURE_J1939
        else if(flags&J1939_PROTOCOL)
        {
@@ -547,7 +578,8 @@ static int send_message(message_t *message, const std::string& bus_name, uint32_
 }
 
 
-static void write_raw_frame(afb_req_t request, const std::string& bus_name, message_t *message, struct json_object *can_data, uint32_t flags)
+static void write_raw_frame(afb_req_t request, const std::string& bus_name, message_t *message,
+                                                       struct json_object *can_data, uint32_t flags, event_filter_t &event_filter)
 {
 
        struct utils::signals_found sf;
@@ -556,10 +588,10 @@ static void write_raw_frame(afb_req_t request, const std::string& bus_name, mess
 
        if( !sf.signals.empty() )
        {
-               AFB_DEBUG("ID WRITE RAW : %d",sf.signals[0]->get_message()->get_id());
+               AFB_DEBUG("ID WRITE RAW : %d",sf.signals.front()->get_message()->get_id());
                if(flags&BCM_PROTOCOL)
                {
-                       if(sf.signals[0]->get_message()->is_fd())
+                       if(sf.signals.front()->get_message()->is_fd())
                        {
                                AFB_DEBUG("CANFD_MAX_DLEN");
                                message->set_flags(CAN_FD_FRAME);
@@ -570,17 +602,22 @@ static void write_raw_frame(afb_req_t request, const std::string& bus_name, mess
                                AFB_DEBUG("CAN_MAX_DLEN");
                                message->set_maxdlen(CAN_MAX_DLEN);
                        }
+
+                       if(sf.signals.front()->get_message()->is_isotp())
+                       {
+                               flags = ISOTP_PROTOCOL;
+                               message->set_maxdlen(MAX_ISOTP_FRAMES * message->get_maxdlen());
+                       }
+               }
+
+#ifdef USE_FEATURE_J1939
+               if(flags&J1939_PROTOCOL)
+               {
+                       message->set_maxdlen(J1939_MAX_DLEN);
                }
+#endif
 
-               if((message->get_length()> 0 && (
-               ((flags&BCM_PROTOCOL) && (
-                               (message->get_length() <= CANFD_MAX_DLEN * MAX_BCM_CAN_FRAMES && message->get_flags() & CAN_FD_FRAME)
-                       ||      (message->get_length() <= CAN_MAX_DLEN * MAX_BCM_CAN_FRAMES && !(message->get_flags() & CAN_FD_FRAME))
-               ))
-       #ifdef USE_FEATURE_J1939
-               || (message->get_length() <= J1939_MAX_DLEN && flags&J1939_PROTOCOL)
-       #endif
-               )))
+               if(message->get_length() > 0 && message->get_length() <= message->get_maxdlen())
                {
                        std::vector<uint8_t> data;
                        for (int i = 0 ; i < message->get_length() ; i++)
@@ -601,6 +638,10 @@ static void write_raw_frame(afb_req_t request, const std::string& bus_name, mess
                        {
                                afb_req_fail(request, "Invalid", "Frame J1939");
                        }
+                       else if(flags&ISOTP_PROTOCOL)
+                       {
+                               afb_req_fail(request, "Invalid", "Frame ISOTP");
+                       }
                        else
                        {
                                afb_req_fail(request, "Invalid", "Frame");
@@ -608,7 +649,7 @@ static void write_raw_frame(afb_req_t request, const std::string& bus_name, mess
                        return;
                }
 
-               if(! send_message(message, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name),flags))
+               if(! send_message(message, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name), flags, event_filter, sf.signals.front()))
                {
                        afb_req_success(request, nullptr, "Message correctly sent");
                }
@@ -624,7 +665,7 @@ static void write_raw_frame(afb_req_t request, const std::string& bus_name, mess
 
 }
 
-static void write_frame(afb_req_t request, const std::string& bus_name, json_object *json_value)
+static void write_frame(afb_req_t request, const std::string& bus_name, json_object *json_value, event_filter_t &event_filter)
 {
        message_t *message;
        int id;
@@ -640,7 +681,7 @@ static void write_frame(afb_req_t request, const std::string& bus_name, json_obj
                                  "can_data", &can_data))
        {
                message = new can_message_t(0,(uint32_t)id,(uint32_t)length,false,0,data,0);
-               write_raw_frame(request,bus_name,message,can_data,BCM_PROTOCOL);
+               write_raw_frame(request,bus_name,message,can_data,BCM_PROTOCOL,event_filter);
        }
 #ifdef USE_FEATURE_J1939
        else if(!wrap_json_unpack(json_value, "{si, si, so !}",
@@ -649,7 +690,7 @@ static void write_frame(afb_req_t request, const std::string& bus_name, json_obj
                                  "data", &can_data))
        {
                message = new j1939_message_t((uint32_t)length,data,0,J1939_NO_NAME,(pgn_t)id,J1939_NO_ADDR);
-               write_raw_frame(request,bus_name,message,can_data,J1939_PROTOCOL);
+               write_raw_frame(request,bus_name,message,can_data,J1939_PROTOCOL, event_filter);
        }
 #endif
        else
@@ -660,7 +701,7 @@ static void write_frame(afb_req_t request, const std::string& bus_name, json_obj
        delete message;
 }
 
-static void write_signal(afb_req_t request, const std::string& name, json_object *json_value)
+static void write_signal(afb_req_t request, const std::string& name, json_object *json_value, event_filter_t &event_filter)
 {
        struct canfd_frame cfd;
        struct utils::signals_found sf;
@@ -679,7 +720,7 @@ static void write_signal(afb_req_t request, const std::string& name, json_object
                return;
        }
 
-       std::shared_ptr<signal_t>& sig = sf.signals[0];
+       std::shared_ptr<signal_t> sig = sf.signals.front();
        if(! sig->get_writable())
        {
                afb_req_fail_f(request, "%s isn't writable. Message not sent.", sig->get_name().c_str());
@@ -696,6 +737,10 @@ static void write_signal(afb_req_t request, const std::string& name, json_object
        {
                flags = J1939_PROTOCOL;
        }
+       else if(sig->get_message()->is_isotp())
+       {
+               flags = ISOTP_PROTOCOL;
+       }
        else
        {
                flags = BCM_PROTOCOL;
@@ -704,7 +749,7 @@ static void write_signal(afb_req_t request, const std::string& name, json_object
 //     cfd = encoder_t::build_frame(sig, value);
        message_t *message = encoder_t::build_message(sig,value,false,false);
 
-       if(! send_message(message, sig->get_message()->get_bus_device_name(), flags) && send)
+       if(! send_message(message, sig->get_message()->get_bus_device_name(), flags, event_filter, sig) && send)
        {
                afb_req_success(request, nullptr, "Message correctly sent");
        }
@@ -727,25 +772,43 @@ static void write_signal(afb_req_t request, const std::string& name, json_object
 
 void write(afb_req_t request)
 {
-       struct json_object* args = nullptr, *json_value = nullptr;
-       const char *name = nullptr;
-
+       struct json_object* args = nullptr, *json_value = nullptr, *name = nullptr;
        args = afb_req_json(request);
 
-       // Process about Raw CAN message on CAN bus directly
-       if (args != NULL && ! wrap_json_unpack(args, "{ss, so !}",
-                                                  "bus_name", &name,
-                                                  "frame", &json_value))
-               write_frame(request, name, json_value);
-
-       // Search signal then encode value.
-       else if(args != NULL &&
-               ! wrap_json_unpack(args, "{ss, so !}",
-                                  "signal_name", &name,
-                                  "signal_value", &json_value))
-               write_signal(request, std::string(name), json_value);
+       if(args != NULL)
+       {
+               event_filter_t event_filter = generate_filter(args);
+
+               if(json_object_object_get_ex(args,"bus_name",&name))
+               {
+                       if(json_object_object_get_ex(args,"frame",&json_value))
+                       {
+                               write_frame(request, (std::string)json_object_get_string(name), json_value, event_filter);
+                       }
+                       else
+                       {
+                               afb_req_fail(request, "Error", "Request argument malformed");
+                       }
+               }
+               else if(json_object_object_get_ex(args,"signal_name",&name))
+               {
+                       if(json_object_object_get_ex(args,"signal_value",&json_value))
+                       {
+                               write_signal(request, (std::string)json_object_get_string(name), json_value, event_filter);
+                       }
+                       else
+                       {
+                               afb_req_fail(request, "Error", "Request argument malformed");
+                       }
+               }
+               else {
+                       afb_req_fail(request, "Error", "Request argument malformed");
+               }
+       }
        else
-               afb_req_fail(request, "Error", "Request argument malformed");
+       {
+               afb_req_fail(request, "Error", "Request argument null");
+       }
 }
 
 static struct json_object *get_signals_value(const std::string& name)
@@ -894,14 +957,14 @@ int init_binding(afb_api_t api)
                struct event_filter_t event_filter;
                event_filter.frequency = sf.diagnostic_messages.front()->get_frequency();
 
-               std::map<int, std::shared_ptr<low_can_subscription_t> >& s = sm.get_subscribed_signals();
+               map_subscription& s = sm.get_subscribed_signals();
 
                subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
        }
 
 
 #ifdef USE_FEATURE_J1939
-       std::vector<std::shared_ptr<message_definition_t>> current_messages_definition = application.get_messages_definition();
+       vect_ptr_msg_def_t current_messages_definition = application.get_messages_definition();
        for(std::shared_ptr<message_definition_t> message_definition: current_messages_definition)
        {
                if(message_definition->is_j1939())