Skip to content

Commit 36f13d0

Browse files
authored
Merge pull request #2 from yezzfusl/test
Implement CAN Message Filtering and Decoding.
2 parents 0482f3b + eff1b3f commit 36f13d0

File tree

12 files changed

+55
-1
lines changed

12 files changed

+55
-1
lines changed

can_analyzer

296 Bytes
Binary file not shown.

obj/can_decode.o

1.72 KB
Binary file not shown.

obj/can_filter.o

1.12 KB
Binary file not shown.

obj/can_interface.o

200 Bytes
Binary file not shown.

obj/main.o

264 Bytes
Binary file not shown.

src/can_decode.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "can_decode.h"
2+
#include <stdio.h>
3+
4+
void decode_can_message(const struct can_frame *frame) {
5+
printf("Decoded message: ");
6+
7+
switch (frame->can_id) {
8+
case 0x123:
9+
printf("Engine Temperature: %d°C\n", frame->data[0]);
10+
break;
11+
case 0x456:
12+
printf("Vehicle Speed: %d km/h\n", frame->data[0]);
13+
break;
14+
default:
15+
printf("Unknown message type\n");
16+
}
17+
}

src/can_decode.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef CAN_DECODE_H
2+
#define CAN_DECODE_H
3+
4+
#include <linux/can.h>
5+
6+
void decode_can_message(const struct can_frame *frame);
7+
8+
#endif

src/can_filter.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "can_filter.h"
2+
3+
int apply_can_filter(const struct can_frame *frame, const struct can_filter *filter) {
4+
return (frame->can_id & filter->can_mask) == (filter->can_id & filter->can_mask);
5+
}

src/can_filter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef CAN_FILTER_H
2+
#define CAN_FILTER_H
3+
4+
#include <linux/can.h>
5+
6+
int apply_can_filter(const struct can_frame *frame, const struct can_filter *filter);
7+
8+
#endif

src/can_interface.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ int can_receive(int socket_fd, struct can_frame *frame) {
5050
void can_close(int socket_fd) {
5151
close(socket_fd);
5252
}
53+
54+
void set_can_filter(int socket_fd, struct can_filter *filter) {
55+
setsockopt(socket_fd, SOL_CAN_RAW, CAN_RAW_FILTER, filter, sizeof(struct can_filter));
56+
}

0 commit comments

Comments
 (0)