j1939: Follow updates of the kernel 84/23384/3
authorArthur GUYADER <arthur.guyader@iot.bzh>
Thu, 12 Dec 2019 17:12:01 +0000 (18:12 +0100)
committerRomain Forlot <romain.forlot@iot.bzh>
Thu, 9 Jan 2020 15:25:36 +0000 (16:25 +0100)
This commit patch j1939 feature after the update of the
kernel from linux-can-next to mainline.
Now promisc option removes filter of the bind.
The option is now a parameter in the event_filter.
The feature recv_own isn't available, you can't receive
on the same socket an emit frame from you.
Not a big problem because receive and send method use two
different sockets in the implementation.

Change-Id: I5ac410177c1512090827870300b8aa1679477b84
Signed-off-by: Arthur GUYADER <arthur.guyader@iot.bzh>
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
low-can-binding/binding/low-can-cb.cpp
low-can-binding/binding/low-can-subscription.cpp
low-can-binding/binding/low-can-subscription.hpp
low-can-binding/utils/socketcan-j1939/socketcan-j1939-addressclaiming.cpp
low-can-binding/utils/socketcan-j1939/socketcan-j1939-data.cpp
low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp
low-can-binding/utils/socketcan-j1939/socketcan-j1939.hpp

index 5e03c75..5d5a517 100644 (file)
@@ -314,6 +314,9 @@ 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, "promisc", &obj)
+                       && (json_object_is_type(obj, json_type_boolean)))
+                       event_filter.promisc = (bool)json_object_get_boolean(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);
index 3cbcb6c..4bdf318 100644 (file)
@@ -254,6 +254,11 @@ float low_can_subscription_t::get_max() const
        return event_filter_.max;
 }
 
+bool low_can_subscription_t::get_promisc() const
+{
+       return event_filter_.promisc;
+}
+
 /**
  * @brief Getter of the rx_id of the event_filter
  *
@@ -314,6 +319,11 @@ void low_can_subscription_t::set_max(float max)
        event_filter_.max = max;
 }
 
+void low_can_subscription_t::set_promisc(bool promisc)
+{
+       event_filter_.promisc = promisc;
+}
+
 /**
  * @brief Setter for the rx_id of the event_filter
  *
@@ -409,30 +419,32 @@ int low_can_subscription_t::open_socket(low_can_subscription_t &subscription, co
                else if(flags & J1939_ADDR_CLAIM_PROTOCOL)
                {
                        pgn_t pgn = J1939_NO_PGN;
+                       std::shared_ptr<utils::socketcan_j1939_addressclaiming_t> socket = std::make_shared<utils::socketcan_j1939_addressclaiming_t>();
                        if(!bus_name.empty())
                        {
-                               std::shared_ptr<utils::socketcan_j1939_addressclaiming_t> socket = std::make_shared<utils::socketcan_j1939_addressclaiming_t>();
                                ret = socket->open(bus_name, pgn);
-                               subscription.socket_ = socket;
                        }
+                       subscription.socket_ = socket;
                        subscription.index_ = (int)subscription.socket_->socket();
                }
                else if(flags & J1939_PROTOCOL)
                {
                        pgn_t pgn = J1939_NO_PGN;
+                       std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
                        if(subscription.signal_)
                        {
                                pgn = subscription.signal_->get_message()->get_id();
-                               std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
                                ret = socket->open(subscription.signal_->get_message()->get_bus_device_name(), pgn);
-                               subscription.socket_ = socket;
                        }
                        else if(!bus_name.empty())
                        {
-                               std::shared_ptr<utils::socketcan_j1939_data_t> socket = std::make_shared<utils::socketcan_j1939_data_t>();
                                ret = socket->open(bus_name, pgn);
-                               subscription.socket_ = socket;
                        }
+
+                       if(ret)
+                               socket->define_opt(!j1939_pgn_is_pdu1(pgn),subscription.event_filter_.promisc);
+
+                       subscription.socket_ = socket;
                        subscription.index_ = (int)subscription.socket_->socket();
                }
 #endif
index fa6b795..972b6d6 100644 (file)
@@ -35,12 +35,13 @@ struct event_filter_t
        float frequency; ///< frequency - Maximum frequency which will be received and pushed to a subscribed event.
        float min; ///< min - Minimum value that the signal doesn't have to go below to be pushed.
        float max; ///< max - Maximum value that the signal doesn't have to go above to be pushed.
+       bool promisc; //<promisc - Active mode promisc on socket that permit this feature (j1939)
        canid_t rx_id; ///< rx_id - RX_ID for ISO_TP protocol
        canid_t tx_id; ///< tx_id - TX_ID for ISO_TP protocol
 
-       event_filter_t() : frequency{0}, min{-__FLT_MAX__}, max{__FLT_MAX__}, rx_id{NO_CAN_ID}, tx_id{NO_CAN_ID} {};
+       event_filter_t() : frequency{0}, min{-__FLT_MAX__}, max{__FLT_MAX__}, promisc{false}, rx_id{NO_CAN_ID}, tx_id{NO_CAN_ID} {};
        bool operator==(const event_filter_t& ext) const {
-               return frequency == ext.frequency && min == ext.min && max == ext.max && rx_id == ext.rx_id && tx_id == ext.tx_id;
+               return frequency == ext.frequency && min == ext.min && max == ext.max && promisc == ext.promisc && rx_id == ext.rx_id && tx_id == ext.tx_id;
        }
 };
 
@@ -87,6 +88,7 @@ public:
        float get_frequency() const;
        float get_min() const;
        float get_max() const;
+       bool get_promisc() const;
        canid_t get_rx_id() const;
        canid_t get_tx_id() const;
        std::shared_ptr<utils::socketcan_t> get_socket();
@@ -95,6 +97,7 @@ public:
        void set_min(float min);
        void set_max(float max);
        void set_index(int index);
+       void set_promisc(bool promisc);
        void set_rx_id(canid_t rx_id);
        void set_tx_id(canid_t tx_id);
        void set_signal(std::shared_ptr<signal_t> signal);
index ebb3974..1ba590b 100644 (file)
@@ -50,7 +50,7 @@ namespace utils
                {
                        if(jm->get_addr() >= J1939_IDLE_ADDR)
                        {
-                               AFB_DEBUG("Get invalid address claiming by name : %x",(unsigned int)jm->get_name());
+                               AFB_DEBUG("Get invalid address claiming by name (maybe logout) : %x",(unsigned int)jm->get_name());
                                return invalid_message;
                        }
 
index 08c1375..2be275d 100644 (file)
@@ -31,15 +31,7 @@ namespace utils
         */
        int socketcan_j1939_data_t::open(std::string device_name, pgn_t pgn)
        {
-               int ret = socketcan_j1939_t::open(device_name, htole64(J1939_NAME_ECU), pgn, J1939_NO_ADDR);
-               if(ret >= 0)
-               {
-                       if(tx_address_.can_addr.j1939.pgn != J1939_NO_PGN)
-                               add_filter(J1939_NO_NAME, tx_address_.can_addr.j1939.pgn, J1939_NO_ADDR, J1939_NO_NAME, J1939_NO_PGN, J1939_NO_ADDR);
-
-                       define_opt();
-               }
-               return ret;
+               return socketcan_j1939_t::open(device_name, htole64(J1939_NAME_ECU), pgn, J1939_NO_ADDR);
        }
 
        /**
index 90e6821..0e5a671 100644 (file)
@@ -87,18 +87,16 @@ namespace utils
        /**
         * @brief Define some parameters on the socket
         *
-        * @param promisc - Allows to accept all packets that the socket receives even if they are not addressed directly to it
-        * @param recv_own_msgs - Allows you to receive your own packets
         * @param broadcast - Allows to write message with address brodcast (255)
+        * @param promisc - Allows to accept all packets that the socket receives even if they are not addressed directly to it
         */
-       void socketcan_j1939_t::define_opt(bool promisc, bool recv_own_msgs, bool broadcast)
+       void socketcan_j1939_t::define_opt(bool broadcast, bool promisc)
        {
-               int promisc_i = promisc ? 1 : 0;
-               //int recv_own_msgs_i = recv_own_msgs ? 1 : 0;
                int broadcast_i = broadcast ? 1 : 0;
+               int promisc_i = promisc ? 1 : 0;
 
-               setopt(SOL_CAN_J1939, SO_J1939_PROMISC, &promisc_i, sizeof(promisc_i));
                setopt(SOL_SOCKET, SO_BROADCAST, &broadcast_i, sizeof(broadcast_i));
+               setopt(SOL_CAN_J1939, SO_J1939_PROMISC, &promisc_i, sizeof(promisc_i));
        }
 
 
index 1ae8d1c..c68500f 100644 (file)
 #define J1939_NAME_ECU 0x1234
 #endif
 
+#define J1939_CAN_ID CAN_EFF_FLAG
+#define J1939_CAN_MASK (CAN_EFF_FLAG | CAN_RTR_FLAG)
+
+
+// PDU 1 = NO BROADCAST
+// PDU 2 = BRODCAST
+static inline bool j1939_pgn_is_pdu1(pgn_t pgn)
+{
+       return (pgn & 0xff00) < 0xf000;
+}
+
 namespace utils
 {
 
@@ -54,6 +65,7 @@ namespace utils
                        virtual std::shared_ptr<message_t> read_message(int flag);
                        virtual int write_message(message_t& obj);
                        virtual int write_j1939_message(pgn_t pgn, std::vector<uint8_t> &data, uint32_t len_data);
+                       void define_opt(bool broadcast = true, bool promisc = false);
 
                protected:
                        struct ifreq ifr_;
@@ -61,7 +73,7 @@ namespace utils
                        static std::condition_variable signal_address_claiming_;
                        void define_tx_address(std::string device_name, name_t name, pgn_t pgn, uint8_t addr);
                        int add_filter(name_t name, pgn_t pgn, uint8_t addr, name_t name_mask, pgn_t pgn_mask, uint8_t addr_mask);
-                       void define_opt(bool promisc = true, bool recv_own_msgs = true, bool broadcast = true);
+
 
        };
 }