Skip to contentSkip to conversation

Extended CAN frame filtering

Xenomai-core Jan Kiszka 2 participants 10 messages Feb 14, 2007 – Feb 18, 2007

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

Hi Wolfgang,

unless I messed something up, the first patch aligns the implementation of Socket-CAN filters in Xenomai with their current specification. Right now, if you set a filter on a standard frame ID, you will also receive extended frames with the same ID. In contrast, when the extended bit is set, only extended frames are received, not standard frames with the same ID (that's again spec-conforming).

drivers/can/rtcan_raw_filter.c +4 -4
@@ -55,11 +55,11 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
 					  can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-	recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-				 CAN_EFF_FLAG);
+    if (filter->can_id & CAN_EFF_FLAG)
+	recv_filter->can_mask = filter->can_mask & CAN_EFF_MASK;
     else
-	recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
+	recv_filter->can_mask = filter->can_mask & CAN_SFF_MASK;
+    recv_filter->can_mask |= CAN_EFF_FLAG;
 
     recv_filter->can_id = filter->can_id & recv_filter->can_mask;
 } 

However, I wonder if this behaviour is useful. You can now either set a filter for extended frames or standard frame, not for both frame type, just varying on the ID length. If we consider EFF just as another bit of the CAN ID, we could take this into account for the mask:

drivers/can/rtcan_raw_filter.c +5 -4
@@ -55,11 +55,12 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
 					  can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-	recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-				 CAN_EFF_FLAG);
+    if (filter->can_id & CAN_EFF_FLAG)
+	recv_filter->can_mask = filter->can_mask &
+				(CAN_EFF_MASK | CAN_EFF_FLAG);
     else
-	recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
+	recv_filter->can_mask = filter->can_mask &
+				(CAN_SFF_MASK | CAN_EFF_FLAG);
 
     recv_filter->can_id = filter->can_id & recv_filter->can_mask;
 }

Note: this alternative patch would also require a patch to rtdm/rtcan.h.

Actually, the second variant was what I intuitively expected. What is the behaviour of non-RT Socket-CAN here?

Jan

PS: A few lines above those hunks is still a "#if 0" code block. Either make this configurable or please clean it up.

#1 lore raw

Hi Jan,

Quoted 54 lines · Jan Kiszka

Hi Wolfgang,

unless I messed something up, the first patch aligns the implementation of Socket-CAN filters in Xenomai with their current specification. Right now, if you set a filter on a standard frame ID, you will also receive extended frames with the same ID. In contrast, when the extended bit is set, only extended frames are received, not standard frames with the same ID (that's again spec-conforming).

drivers/can/rtcan_raw_filter.c +4 -4
@@ -55,11 +55,11 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
 					  can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-	recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-				 CAN_EFF_FLAG);
+    if (filter->can_id & CAN_EFF_FLAG)
+	recv_filter->can_mask = filter->can_mask & CAN_EFF_MASK;
     else
-	recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
+	recv_filter->can_mask = filter->can_mask & CAN_SFF_MASK;
+    recv_filter->can_mask |= CAN_EFF_FLAG;
 
     recv_filter->can_id = filter->can_id & recv_filter->can_mask;
 } 

However, I wonder if this behaviour is useful. You can now either set a filter for extended frames or standard frame, not for both frame type, just varying on the ID length. If we consider EFF just as another bit of the CAN ID, we could take this into account for the mask:

drivers/can/rtcan_raw_filter.c +5 -4
@@ -55,11 +55,12 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
 					  can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-	recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-				 CAN_EFF_FLAG);
+    if (filter->can_id & CAN_EFF_FLAG)
+	recv_filter->can_mask = filter->can_mask &
+				(CAN_EFF_MASK | CAN_EFF_FLAG);
     else
-	recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
+	recv_filter->can_mask = filter->can_mask &
+				(CAN_SFF_MASK | CAN_EFF_FLAG);
 
     recv_filter->can_id = filter->can_id & recv_filter->can_mask;
 }
Quoted 4 lines

Note: this alternative patch would also require a patch to rtdm/rtcan.h.

Actually, the second variant was what I intuitively expected. What is the behaviour of non-RT Socket-CAN here?

Hm, good question. I'm going to ask on the Socket-CAN-ML later today.

Quoted 6 lines

Jan

PS: A few lines above those hunks is still a "#if 0" code block. Either make this configurable or please clean it up.

OK.

Wolfgang.

#2 lore raw
In reply to Jan Kiszka
Quoted 54 lines · Jan Kiszka

Hi Wolfgang,

unless I messed something up, the first patch aligns the implementation of Socket-CAN filters in Xenomai with their current specification. Right now, if you set a filter on a standard frame ID, you will also receive extended frames with the same ID. In contrast, when the extended bit is set, only extended frames are received, not standard frames with the same ID (that's again spec-conforming).

drivers/can/rtcan_raw_filter.c +4 -4
@@ -55,11 +55,11 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
 					  can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-	recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-				 CAN_EFF_FLAG);
+    if (filter->can_id & CAN_EFF_FLAG)
+	recv_filter->can_mask = filter->can_mask & CAN_EFF_MASK;
     else
-	recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
+	recv_filter->can_mask = filter->can_mask & CAN_SFF_MASK;
+    recv_filter->can_mask |= CAN_EFF_FLAG;
 
     recv_filter->can_id = filter->can_id & recv_filter->can_mask;
 } 

However, I wonder if this behaviour is useful. You can now either set a filter for extended frames or standard frame, not for both frame type, just varying on the ID length. If we consider EFF just as another bit of the CAN ID, we could take this into account for the mask:

drivers/can/rtcan_raw_filter.c +5 -4
@@ -55,11 +55,12 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
 					  can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-	recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-				 CAN_EFF_FLAG);
+    if (filter->can_id & CAN_EFF_FLAG)
+	recv_filter->can_mask = filter->can_mask &
+				(CAN_EFF_MASK | CAN_EFF_FLAG);
     else
-	recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
+	recv_filter->can_mask = filter->can_mask &
+				(CAN_SFF_MASK | CAN_EFF_FLAG);
 
     recv_filter->can_id = filter->can_id & recv_filter->can_mask;
 }
Quoted 4 lines

Note: this alternative patch would also require a patch to rtdm/rtcan.h.

Actually, the second variant was what I intuitively expected. What is the behaviour of non-RT Socket-CAN here?

I checked the filter related code of Socket-CAN. They use a more sophisticated filtering scheme:

if (filter.can_id & 0x40000000) [CAN_RTR_FLAG]

    accept if ((can_id & filter->can_mask) == filter->can_id)
  else if (filter.can_id & 0x20000000) [CAN_INV_FILTER]
    accept if ((can_id & filter->can_mask) != filter->can_id)
  else if (filter->can_mask == 0)
    accept all messages
  else if filter.can_id & 0x80000000) [CAN_EFF_FLAG]
    accept if (can_id == filter->can_id)
  else
    accept if (can_id == filter->can_id)

In other words, you can define a positive or negative filter using id and mask, accept all messages or require an exact id match for standard or extended frames. Well, we need something compatible and for this reason I triggered a discussion on the Socket-CAN-ML.

Wolfgang.

#3 lore raw
Quoted 72 lines · Wolfgang Grandegger
Quoted 53 lines · Jan Kiszka

Hi Wolfgang,

unless I messed something up, the first patch aligns the implementation of Socket-CAN filters in Xenomai with their current specification. Right now, if you set a filter on a standard frame ID, you will also receive extended frames with the same ID. In contrast, when the extended bit is set, only extended frames are received, not standard frames with the same ID (that's again spec-conforming).

drivers/can/rtcan_raw_filter.c (Arbeitskopie) renamed from drivers/can/rtcan_raw_filter.c (Revision 2178) +4 -4
@@ -55,11 +55,11 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
                       can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-    recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-                 CAN_EFF_FLAG);
+    if (filter->can_id & CAN_EFF_FLAG)
+    recv_filter->can_mask = filter->can_mask & CAN_EFF_MASK;
     else
-    recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
+    recv_filter->can_mask = filter->can_mask & CAN_SFF_MASK;
+    recv_filter->can_mask |= CAN_EFF_FLAG;
 
     recv_filter->can_id = filter->can_id & recv_filter->can_mask;
 }

However, I wonder if this behaviour is useful. You can now either set a filter for extended frames or standard frame, not for both frame type, just varying on the ID length. If we consider EFF just as another bit of the CAN ID, we could take this into account for the mask:

drivers/can/rtcan_raw_filter.c (Arbeitskopie) renamed from drivers/can/rtcan_raw_filter.c (Revision 2178) +5 -4
@@ -55,11 +55,12 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
                       can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-    recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-                 CAN_EFF_FLAG);
+    if (filter->can_id & CAN_EFF_FLAG)
+    recv_filter->can_mask = filter->can_mask &
+                (CAN_EFF_MASK | CAN_EFF_FLAG);
     else
-    recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
+    recv_filter->can_mask = filter->can_mask &
+                (CAN_SFF_MASK | CAN_EFF_FLAG);
 
     recv_filter->can_id = filter->can_id & recv_filter->can_mask;
 }
Quoted 4 lines

Note: this alternative patch would also require a patch to rtdm/rtcan.h.

Actually, the second variant was what I intuitively expected. What is the behaviour of non-RT Socket-CAN here?

I checked the filter related code of Socket-CAN. They use a more sophisticated filtering scheme:

if (filter.can_id & 0x40000000) [CAN_RTR_FLAG]

    accept if ((can_id & filter->can_mask) == filter->can_id)
  else if (filter.can_id & 0x20000000) [CAN_INV_FILTER]
    accept if ((can_id & filter->can_mask) != filter->can_id)
  else if (filter->can_mask == 0)
    accept all messages
  else if filter.can_id & 0x80000000) [CAN_EFF_FLAG]
    accept if (can_id == filter->can_id)
  else
    accept if (can_id == filter->can_id)
Quoted 1 line

In other words, you can define a positive or negative filter using id and mask, accept all messages or require an exact id match for standard or extended frames. Well, we need something compatible and for this reason I triggered a discussion on the Socket-CAN-ML.

Attached is a patch adding the CAN_INV_FILTER feature. It also updates the doc. Now the filter scheme should be compatible with Socket-CAN. If it's OK, I will check it in (or you can do it).

Wolfgang.

rtdm/rtcan.h +22 -13
Index: include/rtdm/rtcan.h
===================================================================
@@ -441,28 +441,38 @@ typedef enum CAN_STATE can_state_t;
 
 #define CAN_STATE_OPERATING(state) ((state) < CAN_STATE_BUS_OFF)
 
+/** Invert CAN filter definition */
+#define CAN_INV_FILTER      CAN_ERR_FLAG
+
 /**
  * Filter for reception of CAN messages.
  *
  * This filter works as follows:
  * A received CAN ID is AND'ed bitwise with @c can_mask and then compared to
- * @c can_id. If this comparison is true the message will be received by the
- * socket.
+ * @c can_id. This also includes the @ref CAN_EFF_FLAG and @ref CAN_RTR_FLAG
+ * of @ref CAN_xxx_FLAG. If this comparison is true, the message will be
+ * received by the socket. The logic can be inverted with the @c can_id flag
+ * @ref CAN_INV_FILTER :
+ *
+ * @code
+ * if (can_id & CAN_INV_FILTER) {
+ *    if ((received_can_id & can_mask) != (can_id & ~CAN_INV_FILTER))
+ *       accept-message;
+ * } else {
+ *    if ((received_can_id & can_mask) == can_id)
+ *       accept-message;
+ * }
+ * @endcode
  *
- * Multiple filters can be arranged in a filter list and set with 
- * @ref Sockopts. If one of these filters matches a CAN ID upon reception 
+ * Multiple filters can be arranged in a filter list and set with
+ * @ref Sockopts. If one of these filters matches a CAN ID upon reception
  * of a CAN frame, this frame is accepted.
  *
- * @note Only @ref CAN_EFF_FLAG of @ref CAN_xxx_FLAG "CAN ID flags" is
- * valid for @c can_id and none for @c can_mask. This means that the RTR bit
- * is not taken into account while filtering messages.
- *
- * Extended IDs are received only if @ref CAN_EFF_FLAG is set in
- * @c can_id. If it is cleared only standard IDs are accepted.
  */
 typedef struct can_filter {
 
-    /** CAN ID which must match with incoming IDs after passing the mask */
+    /** CAN ID which must match with incoming IDs after passing the mask.
+     *  The filter logic can be inverted with the flag @ref CAN_INV_FILTER. */
     uint32_t    can_id;
 
     /** Mask which is applied to incoming IDs. See @ref CAN_xxx_MASK
@@ -470,12 +480,11 @@ typedef struct can_filter {
     uint32_t    can_mask;
 } can_filter_t;
 
-
 /**
  * Socket address structure for the CAN address family
  */
 struct sockaddr_can {
-    /** CAN address family, must be @c AF_CAN */    
+    /** CAN address family, must be @c AF_CAN */
     sa_family_t  can_family;
     /** Interface index of CAN controller. See @ref SIOCGIFINDEX. */
     int          can_ifindex;
drivers/can/rtcan_module.c +10 -7
Index: ksrc/drivers/can/rtcan_module.c
===================================================================
@@ -262,11 +262,11 @@ static int rtcan_read_proc_filter(char *
     rtdm_lockctx_t lock_ctx;
     RTCAN_PROC_PRINT_VARS(80);
 
-    /*  fd __CAN_ID__ _CAN_Mask_ MatchCount
-     *   3 0x12345678 0x12345678 1234567890
+    /*  fd __CAN_ID__ _CAN_Mask_ Inv MatchCount
+     *   3 0x12345678 0x12345678  no 1234567890
      */
-    
-    if (!RTCAN_PROC_PRINT("fd __CAN_ID__ _CAN_Mask_ MatchCount\n"))
+
+    if (!RTCAN_PROC_PRINT("fd __CAN_ID__ _CAN_Mask_ Inv MatchCount\n"))
         goto done;
 
     rtdm_lock_get_irqsave(&rtcan_recv_list_lock, lock_ctx);
@@ -275,10 +275,13 @@ static int rtcan_read_proc_filter(char *
     while (recv_listener != NULL) {
 	context = rtcan_socket_context(recv_listener->sock);
 
-	if (!RTCAN_PROC_PRINT("%2d 0x%08x 0x%08x %10d\n",
-			      context->fd, 
+	if (!RTCAN_PROC_PRINT("%2d 0x%08x 0x%08x %s %10d\n",
+			      context->fd,
 			      recv_listener->can_filter.can_id,
-			      recv_listener->can_filter.can_mask,
+			      recv_listener->can_filter.can_mask &
+			      ~CAN_INV_FILTER,
+			      (recv_listener->can_filter.can_mask &
+			       CAN_INV_FILTER) ? "yes" : " no",
 			      recv_listener->match_count))
 	    break;
 	recv_listener = recv_listener->next;
drivers/can/rtcan_raw.c +4 -1
Index: ksrc/drivers/can/rtcan_raw.c
===================================================================
@@ -67,7 +67,10 @@ static struct rtdm_device rtcan_proto_ra
 
 static inline int rtcan_accept_msg(uint32_t can_id, can_filter_t *filter)
 {
-    return ((can_id & filter->can_mask) == filter->can_id);
+    if ((filter->can_mask & CAN_INV_FILTER))
+	return ((can_id & filter->can_mask) != filter->can_id);
+    else
+	return ((can_id & filter->can_mask) == filter->can_id);
 }
 
 
drivers/can/rtcan_raw_filter.c +7 -7
Index: ksrc/drivers/can/rtcan_raw_filter.c
===================================================================
@@ -55,13 +55,13 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
 					  can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-	recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-				 CAN_EFF_FLAG);
-    else
-	recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
-
-    recv_filter->can_id = filter->can_id & recv_filter->can_mask;
+    if (filter->can_id & CAN_INV_FILTER) {
+	recv_filter->can_id = filter->can_id & ~CAN_INV_FILTER;
+	recv_filter->can_mask = filter->can_mask | CAN_INV_FILTER;
+    } else {
+	recv_filter->can_id = filter->can_id;
+	recv_filter->can_mask = filter->can_mask & ~CAN_INV_FILTER;
+    }
 }
 
 
author
#4 lore raw

Wolfgang Grandegger wrote:
...

rtdm/rtcan.h

Quoted 14 lines

Attached is a patch adding the CAN_INV_FILTER feature. It also updates the doc. Now the filter scheme should be compatible with Socket-CAN. If it's OK, I will check it in (or you can do it).

Wolfgang.

------------------------------------------------------------------------

Index: include/rtdm/rtcan.h
===================================================================

rtdm/rtcan.h +0 -0

Please increment the profile revision (2 spots: #define and documentation text).

rtdm/rtcan.h typedef enum CAN_STATE can_state_t; · line 446

Quoted 7 lines
rtdm/rtcan.h +3 -0
@@ -441,28 +441,38 @@ typedef enum CAN_STATE can_state_t;
 
 #define CAN_STATE_OPERATING(state) ((state) < CAN_STATE_BUS_OFF)
 
+/** Invert CAN filter definition */
+#define CAN_INV_FILTER      CAN_ERR_FLAG
+

I would move this close to the other CAN ID flags and explain explicitly which one is valid in which context. Both are sharing the same bit, so confusion can arise.

Quoted 11 lines
rtdm/rtcan.h +4 -2
 /**
  * Filter for reception of CAN messages.
  *
  * This filter works as follows:
  * A received CAN ID is AND'ed bitwise with @c can_mask and then compared to
- * @c can_id. If this comparison is true the message will be received by the
- * socket.
+ * @c can_id. This also includes the @ref CAN_EFF_FLAG and @ref CAN_RTR_FLAG
+ * of @ref CAN_xxx_FLAG. If this comparison is true, the message will be
+ * received by the socket. The logic can be inverted with the @c can_id flag
+ * @ref CAN_INV_FILTER :

rtdm/rtcan.h typedef enum CAN_STATE can_state_t; · line 465

Quoted 10 lines
rtdm/rtcan.h +10 -0
+ *
+ * @code
+ * if (can_id & CAN_INV_FILTER) {
+ *    if ((received_can_id & can_mask) != (can_id & ~CAN_INV_FILTER))
+ *       accept-message;
+ * } else {
+ *    if ((received_can_id & can_mask) == can_id)
+ *       accept-message;
+ * }
+ * @endcode

Nice and helpful!

Quoted 101 lines
rtdm/rtcan.h +5 -11
  *
- * Multiple filters can be arranged in a filter list and set with 
- * @ref Sockopts. If one of these filters matches a CAN ID upon reception 
+ * Multiple filters can be arranged in a filter list and set with
+ * @ref Sockopts. If one of these filters matches a CAN ID upon reception
  * of a CAN frame, this frame is accepted.
  *
- * @note Only @ref CAN_EFF_FLAG of @ref CAN_xxx_FLAG "CAN ID flags" is
- * valid for @c can_id and none for @c can_mask. This means that the RTR bit
- * is not taken into account while filtering messages.
- *
- * Extended IDs are received only if @ref CAN_EFF_FLAG is set in
- * @c can_id. If it is cleared only standard IDs are accepted.
  */
 typedef struct can_filter {
 
-    /** CAN ID which must match with incoming IDs after passing the mask */
+    /** CAN ID which must match with incoming IDs after passing the mask.
+     *  The filter logic can be inverted with the flag @ref CAN_INV_FILTER. */
     uint32_t    can_id;
 
     /** Mask which is applied to incoming IDs. See @ref CAN_xxx_MASK
@@ -470,12 +480,11 @@ typedef struct can_filter {
     uint32_t    can_mask;
 } can_filter_t;
 
-
 /**
  * Socket address structure for the CAN address family
  */
 struct sockaddr_can {
-    /** CAN address family, must be @c AF_CAN */    
+    /** CAN address family, must be @c AF_CAN */
     sa_family_t  can_family;
     /** Interface index of CAN controller. See @ref SIOCGIFINDEX. */
     int          can_ifindex;

Index: ksrc/drivers/can/rtcan_module.c
===================================================================

drivers/can/rtcan_module.c +10 -7
@@ -262,11 +262,11 @@ static int rtcan_read_proc_filter(char *
     rtdm_lockctx_t lock_ctx;
     RTCAN_PROC_PRINT_VARS(80);
 
-    /*  fd __CAN_ID__ _CAN_Mask_ MatchCount
-     *   3 0x12345678 0x12345678 1234567890
+    /*  fd __CAN_ID__ _CAN_Mask_ Inv MatchCount
+     *   3 0x12345678 0x12345678  no 1234567890
      */
-    
-    if (!RTCAN_PROC_PRINT("fd __CAN_ID__ _CAN_Mask_ MatchCount\n"))
+
+    if (!RTCAN_PROC_PRINT("fd __CAN_ID__ _CAN_Mask_ Inv MatchCount\n"))
         goto done;
 
     rtdm_lock_get_irqsave(&rtcan_recv_list_lock, lock_ctx);
@@ -275,10 +275,13 @@ static int rtcan_read_proc_filter(char *
     while (recv_listener != NULL) {
 	context = rtcan_socket_context(recv_listener->sock);
 
-	if (!RTCAN_PROC_PRINT("%2d 0x%08x 0x%08x %10d\n",
-			      context->fd, 
+	if (!RTCAN_PROC_PRINT("%2d 0x%08x 0x%08x %s %10d\n",
+			      context->fd,
 			      recv_listener->can_filter.can_id,
-			      recv_listener->can_filter.can_mask,
+			      recv_listener->can_filter.can_mask &
+			      ~CAN_INV_FILTER,
+			      (recv_listener->can_filter.can_mask &
+			       CAN_INV_FILTER) ? "yes" : " no",
 			      recv_listener->match_count))
 	    break;
 	recv_listener = recv_listener->next;

Index: ksrc/drivers/can/rtcan_raw.c
===================================================================

drivers/can/rtcan_raw.c +4 -1
@@ -67,7 +67,10 @@ static struct rtdm_device rtcan_proto_ra
 
 static inline int rtcan_accept_msg(uint32_t can_id, can_filter_t *filter)
 {
-    return ((can_id & filter->can_mask) == filter->can_id);
+    if ((filter->can_mask & CAN_INV_FILTER))
+	return ((can_id & filter->can_mask) != filter->can_id);
+    else
+	return ((can_id & filter->can_mask) == filter->can_id);
 }
 
 

Index: ksrc/drivers/can/rtcan_raw_filter.c
===================================================================

drivers/can/rtcan_raw_filter.c +0 -4
@@ -55,13 +55,13 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
 					  can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-	recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-				 CAN_EFF_FLAG);
-    else

drivers/can/rtcan_raw_filter.c void rtcan_raw_print_filter(struct rtcan · line 64

Quoted 10 lines
drivers/can/rtcan_raw_filter.c +7 -3
-	recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
-
-    recv_filter->can_id = filter->can_id & recv_filter->can_mask;
+    if (filter->can_id & CAN_INV_FILTER) {
+	recv_filter->can_id = filter->can_id & ~CAN_INV_FILTER;
+	recv_filter->can_mask = filter->can_mask | CAN_INV_FILTER;
+    } else {
+	recv_filter->can_id = filter->can_id;
+	recv_filter->can_mask = filter->can_mask & ~CAN_INV_FILTER;
+    }

Why do you push CAN_INV_FILTER internally into the mask instead of keeping it in the filter's ID - as the pseudo code above states?

Jan

#5 lore raw
Quoted 23 lines · Jan Kiszka

Wolfgang Grandegger wrote:
...

rtdm/rtcan.h

Quoted 13 lines

Attached is a patch adding the CAN_INV_FILTER feature. It also updates the doc. Now the filter scheme should be compatible with Socket-CAN. If it's OK, I will check it in (or you can do it).

Wolfgang.

------------------------------------------------------------------------

Index: include/rtdm/rtcan.h
===================================================================

rtdm/rtcan.h +0 -0

Please increment the profile revision (2 spots: #define and documentation text).

Quoted 4 lines
rtdm/rtcan.h +0 -0
@@ -441,28 +441,38 @@ typedef enum CAN_STATE can_state_t;
 
 #define CAN_STATE_OPERATING(state) ((state) < CAN_STATE_BUS_OFF)
 
Quoted 7 lines

rtdm/rtcan.h typedef enum CAN_STATE can_state_t; · line 446

Quoted 3 lines
rtdm/rtcan.h +3 -0
+/** Invert CAN filter definition */
+#define CAN_INV_FILTER      CAN_ERR_FLAG
+

I would move this close to the other CAN ID flags and explain explicitly which one is valid in which context. Both are sharing the same bit, so confusion can arise.

I already thought about it but was unsure if it's less confusing. OK, one more pro, I will change it.

Quoted 131 lines
Quoted 12 lines
rtdm/rtcan.h +4 -2
 /**
  * Filter for reception of CAN messages.
  *
  * This filter works as follows:
  * A received CAN ID is AND'ed bitwise with @c can_mask and then compared to
- * @c can_id. If this comparison is true the message will be received by the
- * socket.
+ * @c can_id. This also includes the @ref CAN_EFF_FLAG and @ref CAN_RTR_FLAG
+ * of @ref CAN_xxx_FLAG. If this comparison is true, the message will be
+ * received by the socket. The logic can be inverted with the @c can_id flag
+ * @ref CAN_INV_FILTER :

rtdm/rtcan.h typedef enum CAN_STATE can_state_t; · line 465

Quoted 10 lines
rtdm/rtcan.h +10 -0
+ *
+ * @code
+ * if (can_id & CAN_INV_FILTER) {
+ *    if ((received_can_id & can_mask) != (can_id & ~CAN_INV_FILTER))
+ *       accept-message;
+ * } else {
+ *    if ((received_can_id & can_mask) == can_id)
+ *       accept-message;
+ * }
+ * @endcode

Nice and helpful!

Quoted 106 lines
rtdm/rtcan.h +5 -11
  *
- * Multiple filters can be arranged in a filter list and set with 
- * @ref Sockopts. If one of these filters matches a CAN ID upon reception 
+ * Multiple filters can be arranged in a filter list and set with
+ * @ref Sockopts. If one of these filters matches a CAN ID upon reception
  * of a CAN frame, this frame is accepted.
  *
- * @note Only @ref CAN_EFF_FLAG of @ref CAN_xxx_FLAG "CAN ID flags" is
- * valid for @c can_id and none for @c can_mask. This means that the RTR bit
- * is not taken into account while filtering messages.
- *
- * Extended IDs are received only if @ref CAN_EFF_FLAG is set in
- * @c can_id. If it is cleared only standard IDs are accepted.
  */
 typedef struct can_filter {
 
-    /** CAN ID which must match with incoming IDs after passing the mask */
+    /** CAN ID which must match with incoming IDs after passing the mask.
+     *  The filter logic can be inverted with the flag @ref CAN_INV_FILTER. */
     uint32_t    can_id;
 
     /** Mask which is applied to incoming IDs. See @ref CAN_xxx_MASK
@@ -470,12 +480,11 @@ typedef struct can_filter {
     uint32_t    can_mask;
 } can_filter_t;
 
-
 /**
  * Socket address structure for the CAN address family
  */
 struct sockaddr_can {
-    /** CAN address family, must be @c AF_CAN */    
+    /** CAN address family, must be @c AF_CAN */
     sa_family_t  can_family;
     /** Interface index of CAN controller. See @ref SIOCGIFINDEX. */
     int          can_ifindex;

Index: ksrc/drivers/can/rtcan_module.c
===================================================================

drivers/can/rtcan_module.c +10 -7
@@ -262,11 +262,11 @@ static int rtcan_read_proc_filter(char *
     rtdm_lockctx_t lock_ctx;
     RTCAN_PROC_PRINT_VARS(80);
 
-    /*  fd __CAN_ID__ _CAN_Mask_ MatchCount
-     *   3 0x12345678 0x12345678 1234567890
+    /*  fd __CAN_ID__ _CAN_Mask_ Inv MatchCount
+     *   3 0x12345678 0x12345678  no 1234567890
      */
-    
-    if (!RTCAN_PROC_PRINT("fd __CAN_ID__ _CAN_Mask_ MatchCount\n"))
+
+    if (!RTCAN_PROC_PRINT("fd __CAN_ID__ _CAN_Mask_ Inv MatchCount\n"))
         goto done;
 
     rtdm_lock_get_irqsave(&rtcan_recv_list_lock, lock_ctx);
@@ -275,10 +275,13 @@ static int rtcan_read_proc_filter(char *
     while (recv_listener != NULL) {
 	context = rtcan_socket_context(recv_listener->sock);
 
-	if (!RTCAN_PROC_PRINT("%2d 0x%08x 0x%08x %10d\n",
-			      context->fd, 
+	if (!RTCAN_PROC_PRINT("%2d 0x%08x 0x%08x %s %10d\n",
+			      context->fd,
 			      recv_listener->can_filter.can_id,
-			      recv_listener->can_filter.can_mask,
+			      recv_listener->can_filter.can_mask &
+			      ~CAN_INV_FILTER,
+			      (recv_listener->can_filter.can_mask &
+			       CAN_INV_FILTER) ? "yes" : " no",
 			      recv_listener->match_count))
 	    break;
 	recv_listener = recv_listener->next;

Index: ksrc/drivers/can/rtcan_raw.c
===================================================================

drivers/can/rtcan_raw.c +4 -1
@@ -67,7 +67,10 @@ static struct rtdm_device rtcan_proto_ra
 
 static inline int rtcan_accept_msg(uint32_t can_id, can_filter_t *filter)
 {
-    return ((can_id & filter->can_mask) == filter->can_id);
+    if ((filter->can_mask & CAN_INV_FILTER))
+	return ((can_id & filter->can_mask) != filter->can_id);
+    else
+	return ((can_id & filter->can_mask) == filter->can_id);
 }
 
 

Index: ksrc/drivers/can/rtcan_raw_filter.c
===================================================================

drivers/can/rtcan_raw_filter.c +2 -7
@@ -55,13 +55,13 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
 					  can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-	recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-				 CAN_EFF_FLAG);
-    else
-	recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
-
-    recv_filter->can_id = filter->can_id & recv_filter->can_mask;
+    if (filter->can_id & CAN_INV_FILTER) {
+	recv_filter->can_id = filter->can_id & ~CAN_INV_FILTER;
Quoted 8 lines

drivers/can/rtcan_raw_filter.c void rtcan_raw_print_filter(struct rtcan · line 64

Quoted 5 lines
drivers/can/rtcan_raw_filter.c +5 -0
+	recv_filter->can_mask = filter->can_mask | CAN_INV_FILTER;
+    } else {
+	recv_filter->can_id = filter->can_id;
+	recv_filter->can_mask = filter->can_mask & ~CAN_INV_FILTER;
+    }

Why do you push CAN_INV_FILTER internally into the mask instead of keeping it in the filter's ID - as the pseudo code above states?

To simplify the filter calculation. It actually avoids the expression (filter->can_id & ~CAN_INV_FILTER). As this is in a very frequently called function, I think it's worth the trick.

Wolfgang.

author
#6 lore raw
Quoted 26 lines · Wolfgang Grandegger
Quoted 17 lines
Quoted 17 lines

Index: ksrc/drivers/can/rtcan_raw_filter.c
===================================================================

drivers/can/rtcan_raw_filter.c (working copy) renamed from drivers/can/rtcan_raw_filter.c (revision 2193) +2 -7
@@ -55,13 +55,13 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
                       can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-    recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-                 CAN_EFF_FLAG);
-    else
-    recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
-
-    recv_filter->can_id = filter->can_id & recv_filter->can_mask;
+    if (filter->can_id & CAN_INV_FILTER) {
+    recv_filter->can_id = filter->can_id & ~CAN_INV_FILTER;
Quoted 8 lines

drivers/can/rtcan_raw_filter.c (working copy) void rtcan_raw_print_filter(struct rtcan · line 64

Quoted 5 lines
drivers/can/rtcan_raw_filter.c (working copy) renamed from drivers/can/rtcan_raw_filter.c (revision 2193) +5 -0
+    recv_filter->can_mask = filter->can_mask | CAN_INV_FILTER;
+    } else {
+    recv_filter->can_id = filter->can_id;
+    recv_filter->can_mask = filter->can_mask & ~CAN_INV_FILTER;
+    }

Why do you push CAN_INV_FILTER internally into the mask instead of keeping it in the filter's ID - as the pseudo code above states?

Quoted 4 lines

To simplify the filter calculation. It actually avoids the expression (filter->can_id & ~CAN_INV_FILTER). As this is in a very frequently called function, I think it's worth the trick.

Ack. I missed that point on first run.

Jan

#7 lore raw
Quoted 25 lines · Jan Kiszka
Quoted 24 lines · Wolfgang Grandegger
Quoted 24 lines
Quoted 12 lines

Index: ksrc/drivers/can/rtcan_raw_filter.c
===================================================================

drivers/can/rtcan_raw_filter.c (working copy) renamed from drivers/can/rtcan_raw_filter.c (revision 2193) +0 -4
@@ -55,13 +55,13 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
                       can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-    recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-                 CAN_EFF_FLAG);
-    else

drivers/can/rtcan_raw_filter.c (working copy) void rtcan_raw_print_filter(struct rtcan · line 64

Quoted 10 lines
drivers/can/rtcan_raw_filter.c (working copy) renamed from drivers/can/rtcan_raw_filter.c (revision 2193) +7 -3
-    recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
-
-    recv_filter->can_id = filter->can_id & recv_filter->can_mask;
+    if (filter->can_id & CAN_INV_FILTER) {
+    recv_filter->can_id = filter->can_id & ~CAN_INV_FILTER;
+    recv_filter->can_mask = filter->can_mask | CAN_INV_FILTER;
+    } else {
+    recv_filter->can_id = filter->can_id;
+    recv_filter->can_mask = filter->can_mask & ~CAN_INV_FILTER;
+    }

Why do you push CAN_INV_FILTER internally into the mask instead of keeping it in the filter's ID - as the pseudo code above states?

Quoted 6 lines
Quoted 4 lines

To simplify the filter calculation. It actually avoids the expression (filter->can_id & ~CAN_INV_FILTER). As this is in a very frequently called function, I think it's worth the trick.

Ack. I missed that point on first run.

Attached is a revised patch including change log entry. As this patch just extends the existing filter capabilities, it could be applied to trunk and the 2.3.x branch as well.

Wolfgang.

rtdm/rtcan.h +28 -16
Index: include/rtdm/rtcan.h
===================================================================
@@ -289,9 +289,15 @@ typedef can_id_t can_err_mask_t;
  * @anchor CAN_xxx_FLAG @name CAN ID flags
  * Flags within a CAN ID indicating special CAN frame attributes
  * @{ */
-#define CAN_EFF_FLAG  0x80000000 /**< extended frame           */
-#define CAN_RTR_FLAG  0x40000000 /**< remote transmission flag */
-#define CAN_ERR_FLAG  0x20000000 /**< error frame (see @ref Errors) */
+/** Extended frame */
+#define CAN_EFF_FLAG  0x80000000
+/** Remote transmission frame */
+#define CAN_RTR_FLAG  0x40000000
+/** Error frame (see @ref Errors) */
+#define CAN_ERR_FLAG  0x20000000
+/** Invert CAN filter definition (used for struct can_filter) */
+#define CAN_INV_FILTER CAN_ERR_FLAG
+
 /** @} */
 
 
@@ -446,23 +452,30 @@ typedef enum CAN_STATE can_state_t;
  *
  * This filter works as follows:
  * A received CAN ID is AND'ed bitwise with @c can_mask and then compared to
- * @c can_id. If this comparison is true the message will be received by the
- * socket.
+ * @c can_id. This also includes the @ref CAN_EFF_FLAG and @ref CAN_RTR_FLAG
+ * of @ref CAN_xxx_FLAG. If this comparison is true, the message will be
+ * received by the socket. The logic can be inverted with the @c can_id flag
+ * @ref CAN_INV_FILTER :
+ *
+ * @code
+ * if (can_id & CAN_INV_FILTER) {
+ *    if ((received_can_id & can_mask) != (can_id & ~CAN_INV_FILTER))
+ *       accept-message;
+ * } else {
+ *    if ((received_can_id & can_mask) == can_id)
+ *       accept-message;
+ * }
+ * @endcode
  *
- * Multiple filters can be arranged in a filter list and set with 
- * @ref Sockopts. If one of these filters matches a CAN ID upon reception 
+ * Multiple filters can be arranged in a filter list and set with
+ * @ref Sockopts. If one of these filters matches a CAN ID upon reception
  * of a CAN frame, this frame is accepted.
  *
- * @note Only @ref CAN_EFF_FLAG of @ref CAN_xxx_FLAG "CAN ID flags" is
- * valid for @c can_id and none for @c can_mask. This means that the RTR bit
- * is not taken into account while filtering messages.
- *
- * Extended IDs are received only if @ref CAN_EFF_FLAG is set in
- * @c can_id. If it is cleared only standard IDs are accepted.
  */
 typedef struct can_filter {
 
-    /** CAN ID which must match with incoming IDs after passing the mask */
+    /** CAN ID which must match with incoming IDs after passing the mask.
+     *  The filter logic can be inverted with the flag @ref CAN_INV_FILTER. */
     uint32_t    can_id;
 
     /** Mask which is applied to incoming IDs. See @ref CAN_xxx_MASK
@@ -470,12 +483,11 @@ typedef struct can_filter {
     uint32_t    can_mask;
 } can_filter_t;
 
-
 /**
  * Socket address structure for the CAN address family
  */
 struct sockaddr_can {
-    /** CAN address family, must be @c AF_CAN */    
+    /** CAN address family, must be @c AF_CAN */
     sa_family_t  can_family;
     /** Interface index of CAN controller. See @ref SIOCGIFINDEX. */
     int          can_ifindex;
ChangeLog +6 -1
Index: ChangeLog
===================================================================
@@ -1,3 +1,9 @@
+2007-02-17  Wolfgang Grandegger  <[email protected]>
+
+	* ksrc/drivers/can/rtcan_raw.c, ksrc/drivers/can/rtcan_raw_filter.c,
+	ksrc/drivers/can/rtcan_module.c, include/rtdm/rtcan.h: The CAN filter
+	definition can now be inverted with the can_id flag CAN_INV_FILTER.
+
 2007-02-16  Philippe Gerum  <[email protected]>
 
 	* ksrc/skins/native/queue.c (rt_queue_delete): 
@@ -57,7 +63,6 @@
 
 	* ksrc/nucleus/pipe.c (xnpipe_write): Use regular copy_from_user()
 	and check return value.
-
 2007-02-07  Philippe Gerum  <[email protected]>
 
 	* ksrc/arch/i386/patches: Upgrade to 2.6.19-1.7-01.
drivers/can/rtcan_module.c +10 -7
Index: ksrc/drivers/can/rtcan_module.c
===================================================================
@@ -262,11 +262,11 @@ static int rtcan_read_proc_filter(char *
     rtdm_lockctx_t lock_ctx;
     RTCAN_PROC_PRINT_VARS(80);
 
-    /*  fd __CAN_ID__ _CAN_Mask_ MatchCount
-     *   3 0x12345678 0x12345678 1234567890
+    /*  fd __CAN_ID__ _CAN_Mask_ Inv MatchCount
+     *   3 0x12345678 0x12345678  no 1234567890
      */
-    
-    if (!RTCAN_PROC_PRINT("fd __CAN_ID__ _CAN_Mask_ MatchCount\n"))
+
+    if (!RTCAN_PROC_PRINT("fd __CAN_ID__ _CAN_Mask_ Inv MatchCount\n"))
         goto done;
 
     rtdm_lock_get_irqsave(&rtcan_recv_list_lock, lock_ctx);
@@ -275,10 +275,13 @@ static int rtcan_read_proc_filter(char *
     while (recv_listener != NULL) {
 	context = rtcan_socket_context(recv_listener->sock);
 
-	if (!RTCAN_PROC_PRINT("%2d 0x%08x 0x%08x %10d\n",
-			      context->fd, 
+	if (!RTCAN_PROC_PRINT("%2d 0x%08x 0x%08x %s %10d\n",
+			      context->fd,
 			      recv_listener->can_filter.can_id,
-			      recv_listener->can_filter.can_mask,
+			      recv_listener->can_filter.can_mask &
+			      ~CAN_INV_FILTER,
+			      (recv_listener->can_filter.can_mask &
+			       CAN_INV_FILTER) ? "yes" : " no",
 			      recv_listener->match_count))
 	    break;
 	recv_listener = recv_listener->next;
drivers/can/rtcan_raw.c +4 -1
Index: ksrc/drivers/can/rtcan_raw.c
===================================================================
@@ -67,7 +67,10 @@ static struct rtdm_device rtcan_proto_ra
 
 static inline int rtcan_accept_msg(uint32_t can_id, can_filter_t *filter)
 {
-    return ((can_id & filter->can_mask) == filter->can_id);
+    if ((filter->can_mask & CAN_INV_FILTER))
+	return ((can_id & filter->can_mask) != filter->can_id);
+    else
+	return ((can_id & filter->can_mask) == filter->can_id);
 }
 
 
drivers/can/rtcan_raw_filter.c +7 -7
Index: ksrc/drivers/can/rtcan_raw_filter.c
===================================================================
@@ -55,13 +55,13 @@ void rtcan_raw_print_filter(struct rtcan
 static inline void rtcan_raw_mount_filter(can_filter_t *recv_filter,
 					  can_filter_t *filter)
 {
-   if (filter->can_id & CAN_EFF_FLAG)
-	recv_filter->can_mask = ((filter->can_mask & CAN_EFF_MASK) |
-				 CAN_EFF_FLAG);
-    else
-	recv_filter->can_mask = (filter->can_mask & CAN_SFF_MASK);
-
-    recv_filter->can_id = filter->can_id & recv_filter->can_mask;
+    if (filter->can_id & CAN_INV_FILTER) {
+	recv_filter->can_id = filter->can_id & ~CAN_INV_FILTER;
+	recv_filter->can_mask = filter->can_mask | CAN_INV_FILTER;
+    } else {
+	recv_filter->can_id = filter->can_id;
+	recv_filter->can_mask = filter->can_mask & ~CAN_INV_FILTER;
+    }
 }
 
 
author
#8 lore raw
Quoted 4 lines · Wolfgang Grandegger

...
Attached is a revised patch including change log entry. As this patch just extends the existing filter capabilities, it could be applied to trunk and the 2.3.x branch as well.

You are just forgetting the other changes to the filter mechanism you implemented.

Thanks for the patch, I just merged your patch into trunk. For v2.3.x, I applied my first patch I sent on Wednesday to align it to what was specified on release of 2.3. No breakage inside the stable series unless something really forces us.

Jan

#9 lore raw
Quoted 6 lines · Jan Kiszka
Quoted 4 lines · Wolfgang Grandegger

...
Attached is a revised patch including change log entry. As this patch just extends the existing filter capabilities, it could be applied to trunk and the 2.3.x branch as well.

Quoted 7 lines

You are just forgetting the other changes to the filter mechanism you implemented.

Thanks for the patch, I just merged your patch into trunk. For v2.3.x, I applied my first patch I sent on Wednesday to align it to what was specified on release of 2.3. No breakage inside the stable series unless something really forces us.

OK, thanks.

Wolfgang.