Add feature ISO TP (multi frames and peer to peer)
[apps/agl-service-can-low-level.git] / low-can-binding / binding / low-can-cb.cpp
index 2b63084..2c6e2be 100644 (file)
@@ -35,6 +35,7 @@
 #include "../utils/signals.hpp"
 #include "../diagnostic/diagnostic-message.hpp"
 #include "../utils/openxc-utils.hpp"
+#include "../utils/signals.hpp"
 
 #ifdef USE_FEATURE_J1939
        #include "../can/message/j1939-message.hpp"
@@ -101,7 +102,7 @@ int read_message(sd_event_source *event_source, int fd, uint32_t revents, void *
                                std::shared_ptr<message_t> message = s->read_message();
 
                                // Sure we got a valid CAN message ?
-                               if (! message->get_id() == 0 && ! message->get_length() == 0 && message->get_msg_format() != message_format_t::INVALID)
+                               if (! message->get_id() == 0 && ! message->get_length() == 0 && !(message->get_flags()&INVALID_FLAG))
                                {
                                        push_n_notify(message);
                                }
@@ -192,11 +193,11 @@ 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,
-                                                    struct event_filter_t& event_filter,
-                                                    std::map<int, std::shared_ptr<low_can_subscription_t> >& s,
-                                                    bool perm_rec_diag_req)
+                                                        bool subscribe,
+                                                        std::vector<std::shared_ptr<diagnostic_message_t> > diagnostic_messages,
+                                                        struct event_filter_t& event_filter,
+                                                        std::map<int, std::shared_ptr<low_can_subscription_t> >& s,
+                                                        bool perm_rec_diag_req)
 {
        int rets = 0;
        application_t& app = application_t::instance();
@@ -257,10 +258,10 @@ 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,
-                                            struct event_filter_t& event_filter,
-                                            std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
+                                                bool subscribe,
+                                                std::vector<std::shared_ptr<signal_t> > signals,
+                                                struct event_filter_t& event_filter,
+                                                std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
 {
        int rets = 0;
        for(const auto& sig: signals)
@@ -333,6 +334,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;
 }
@@ -514,7 +521,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, socket_type type)
+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())
        {
@@ -525,16 +532,24 @@ static int send_message(message_t *message, const std::string& bus_name, socket_
 
        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(type == socket_type::BCM)
+
+       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(type == socket_type::J1939)
+       else if(flags&J1939_PROTOCOL)
        {
                return low_can_subscription_t::j1939_send(*cd[bus_name], message, bus_name);
        }
@@ -546,52 +561,94 @@ static int send_message(message_t *message, const std::string& bus_name, socket_
 }
 
 
-static void write_raw_frame(afb_req_t request, const std::string& bus_name, message_t *message, struct json_object *can_data, socket_type type)
+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)
 {
-       if((message->get_length() <= 8 && message->get_length() > 0 && type == socket_type::BCM)
+
+       struct utils::signals_found sf;
+
+       utils::signals_manager_t::instance().lookup_signals_by_id(message->get_id(), application_t::instance().get_all_signals(), sf.signals);
+
+       if( !sf.signals.empty() )
+       {
+               AFB_DEBUG("ID WRITE RAW : %d",sf.signals[0]->get_message()->get_id());
+               if(flags&BCM_PROTOCOL)
+               {
+                       if(sf.signals[0]->get_message()->is_fd())
+                       {
+                               AFB_DEBUG("CANFD_MAX_DLEN");
+                               message->set_flags(CAN_FD_FRAME);
+                               message->set_maxdlen(CANFD_MAX_DLEN);
+                       }
+                       else
+                       {
+                               AFB_DEBUG("CAN_MAX_DLEN");
+                               message->set_maxdlen(CAN_MAX_DLEN);
+                       }
+
+                       if(sf.signals[0]->get_message()->is_isotp())
+                       {
+                               flags = ISOTP_PROTOCOL;
+                               message->set_maxdlen(MAX_ISOTP_FRAMES * message->get_maxdlen());
+                       }
+               }
+
 #ifdef USE_FEATURE_J1939
-       || (message->get_length() < J1939_MAX_DLEN && type == socket_type::J1939)
+               if(flags&J1939_PROTOCOL)
+               {
+                       message->set_maxdlen(J1939_MAX_DLEN);
+               }
 #endif
-       )
-       {
 
-               std::vector<uint8_t> data;
-               for (int i = 0 ; i < message->get_length() ; i++)
+               if(message->get_length() > 0 && message->get_length() <= message->get_maxdlen())
                {
-                       struct json_object *one_can_data = json_object_array_get_idx(can_data, i);
-                       data.push_back((json_object_is_type(one_can_data, json_type_int)) ?
-                                       (uint8_t)json_object_get_int(one_can_data) : 0);
+                       std::vector<uint8_t> data;
+                       for (int i = 0 ; i < message->get_length() ; i++)
+                       {
+                               struct json_object *one_can_data = json_object_array_get_idx(can_data, i);
+                               data.push_back((json_object_is_type(one_can_data, json_type_int)) ?
+                                               (uint8_t)json_object_get_int(one_can_data) : 0);
+                       }
+                       message->set_data(data);
                }
-               message->set_data(data);
-       }
-       else
-       {
-               if(type == socket_type::BCM)
+               else
                {
-                       afb_req_fail(request, "Invalid", "Data array must hold 1 to 8 values.");
+                       if(flags&BCM_PROTOCOL)
+                       {
+                               afb_req_fail(request, "Invalid", "Frame BCM");
+                       }
+                       else if(flags&J1939_PROTOCOL)
+                       {
+                               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");
+                       }
+                       return;
                }
-               else if(type == socket_type::J1939)
+
+               if(! send_message(message, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name), flags, event_filter, sf.signals[0]))
                {
-                       afb_req_fail(request, "Invalid", "Data array too large");
+                       afb_req_success(request, nullptr, "Message correctly sent");
                }
                else
                {
-                       afb_req_fail(request, "Invalid", "Invalid socket type");
+                       afb_req_fail(request, "Error", "sending the message. See the log for more details.");
                }
-               return;
-       }
-
-       if(! send_message(message, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name),type))
-       {
-               afb_req_success(request, nullptr, "Message correctly sent");
        }
        else
        {
-               afb_req_fail(request, "Error", "sending the message. See the log for more details.");
+               afb_req_fail(request, "Error", "no find id in signals. See the log for more details.");
        }
+
 }
 
-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;
@@ -602,21 +659,21 @@ static void write_frame(afb_req_t request, const std::string& bus_name, json_obj
        AFB_DEBUG("JSON content %s",json_object_get_string(json_value));
 
        if(!wrap_json_unpack(json_value, "{si, si, so !}",
-                             "can_id", &id,
-                             "can_dlc", &length,
-                             "can_data", &can_data))
+                                 "can_id", &id,
+                                 "can_dlc", &length,
+                                 "can_data", &can_data))
        {
-               message = new can_message_t(CANFD_MAX_DLEN,(uint32_t)id,(uint32_t)length,message_format_t::STANDARD,false,0,data,0);
-               write_raw_frame(request,bus_name,message,can_data,socket_type::BCM);
+               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,event_filter);
        }
 #ifdef USE_FEATURE_J1939
        else if(!wrap_json_unpack(json_value, "{si, si, so !}",
-                             "pgn", &id,
-                             "length", &length,
-                             "data", &can_data))
+                                 "pgn", &id,
+                                 "length", &length,
+                                 "data", &can_data))
        {
-               message = new j1939_message_t(J1939_MAX_DLEN,(uint32_t)length,message_format_t::J1939,data,0,J1939_NO_NAME,(pgn_t)id,J1939_NO_ADDR);
-               write_raw_frame(request,bus_name,message,can_data,socket_type::J1939);
+               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, event_filter);
        }
 #endif
        else
@@ -627,7 +684,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;
@@ -646,7 +703,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[0];
        if(! sig->get_writable())
        {
                afb_req_fail_f(request, "%s isn't writable. Message not sent.", sig->get_name().c_str());
@@ -657,21 +714,25 @@ static void write_signal(afb_req_t request, const std::string& name, json_object
                        encoder(*sig, dynafield_value, &send) :
                        encoder_t::encode_DynamicField(*sig, dynafield_value, &send);
 
-       socket_type type = socket_type::INVALID;
+       uint32_t flags = INVALID_FLAG;
 
        if(sig->get_message()->is_j1939())
        {
-               type = socket_type::J1939;
+               flags = J1939_PROTOCOL;
+       }
+       else if(sig->get_message()->is_isotp())
+       {
+               flags = ISOTP_PROTOCOL;
        }
        else
        {
-               type = socket_type::BCM;
+               flags = BCM_PROTOCOL;
        }
 
 //     cfd = encoder_t::build_frame(sig, value);
-       message_t *message = encoder_t::build_message(sig,value);
+       message_t *message = encoder_t::build_message(sig,value,false,false);
 
-       if(! send_message(message, sig->get_message()->get_bus_device_name(), type) && 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");
        }
@@ -694,25 +755,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)
@@ -879,7 +958,7 @@ int init_binding(afb_api_t api)
 
                        ret = low_can_subscription_t::open_socket(*low_can_j1939,
                                                                                                        message_definition->get_bus_device_name(),
-                                                                                                       socket_type::J1939_ADDR_CLAIM);
+                                                                                                       J1939_ADDR_CLAIM_PROTOCOL);
                        if(ret < 0)
                        {
                                AFB_ERROR("Error open socket address claiming for j1939 protocol");