Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
currentleak authored Oct 21, 2024
1 parent bed3947 commit 150fc50
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
31 changes: 16 additions & 15 deletions canfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,49 @@

int main(int argc, char **argv)
{
int s, i;
int fdSocketCAN, i;
int nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;

printf("CAN Sockets Receive Filter Demo\r\n");

if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
if ((fdSocketCAN = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("Socket");
return 1;
return -1;
}

if(argc == 2)
strcpy(ifr.ifr_name, argv[1]);
else strcpy(ifr.ifr_name, "vcan0" );

ioctl(s, SIOCGIFINDEX, &ifr);
ioctl(fdSocketCAN, SIOCGIFINDEX, &ifr);

memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;

if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
if (bind(fdSocketCAN, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Bind");
return 1;
return -1;
}

struct can_filter rfilter[1];
/// A filter matches, when <received_can_id> & mask == can_id & mask
struct can_filter rfilter[1]; // filtres pour 2 ID

rfilter[0].can_id = 0x550;
rfilter[0].can_id = 0x550;
rfilter[0].can_mask = 0xFF0;
//rfilter[1].can_id = 0x200;
//rfilter[1].can_mask = 0x700;
rfilter[1].can_id = 0x480;
rfilter[1].can_mask = 0xFF0;

setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
setsockopt(fdSocketCAN, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));

nbytes = read(s, &frame, sizeof(struct can_frame));
nbytes = read(fdSocketCAN, &frame, sizeof(struct can_frame));

if (nbytes < 0) {
perror("Read");
return 1;
return -1;
}

printf("0x%03X [%d] ",frame.can_id, frame.can_dlc);
Expand All @@ -66,9 +67,9 @@ int main(int argc, char **argv)

printf("\r\n");

if (close(s) < 0) {
if (close(fdSocketCAN) < 0) {
perror("Close");
return 1;
return -1;
}

return 0;
Expand Down
26 changes: 14 additions & 12 deletions canreceive.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

int main(int argc, char *argv[]) //char **argv)
{
int s, i;
int fdSocketCAN, i;
int nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
Expand All @@ -30,9 +30,9 @@ int main(int argc, char *argv[]) //char **argv)
protocole de socket.
la fonction retourne un descripteur de fichier.
*/
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
if ((fdSocketCAN = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("Socket");
return 1;
return -1;
}

/*
Expand All @@ -44,37 +44,39 @@ int main(int argc, char *argv[]) //char **argv)
strcpy(ifr.ifr_name, argv[1]);
else strcpy(ifr.ifr_name, "vcan0" );

ioctl(s, SIOCGIFINDEX, &ifr);
ioctl(fdSocketCAN, SIOCGIFINDEX, &ifr);
/* Alternativement, zéro comme index d'interface, permet de récupérer les paquets de toutes les interfaces CAN.
Avec l'index de l'interface, maintenant lier le socket à l'interface CAN
*/

/*
Alternativement, zéro comme index d'interface, permet de récupérer les paquets de toutes les interfaces CAN.
Avec l'index de l'interface, maintenant lier le socket à l'interface CAN
*/
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;

if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
if (bind(fdSocketCAN, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Bind");
return 1;
return -1;
}

// appel système read(). Cela bloquera jusqu'à ce qu'une trame soit disponible
nbytes = read(s, &frame, sizeof(struct can_frame));
nbytes = read(fdSocketCAN, &frame, sizeof(struct can_frame));

if (nbytes < 0) {
perror("Read");
return 1;
return -1;
}

printf("0x%03X [%d] ",frame.can_id, frame.can_dlc);
for (i = 0; i < frame.can_dlc; i++)
printf("%02X ",frame.data[i]);
printf("\r\n");

if (close(s) < 0) {
if (close(fdSocketCAN) < 0) {
perror("Close");
return 1;
return -1;
}

return 0;
Expand Down
26 changes: 14 additions & 12 deletions cantransmit.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

int main(int argc, char **argv)
{
int s;
int fdSocketCAN;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;
Expand All @@ -29,9 +29,9 @@ int main(int argc, char **argv)
protocole de socket.
la fonction retourne un descripteur de fichier.
*/
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { // Création du socket CAN, de type RAW
if ((fdSocketCAN = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { // Création du socket CAN, de type RAW
perror("Socket");
return 1;
return -1;
}

/*
Expand All @@ -43,19 +43,21 @@ int main(int argc, char **argv)
strcpy(ifr.ifr_name, argv[1]);
else strcpy(ifr.ifr_name, "vcan0" ); // par défaut l'interface can0

ioctl(s, SIOCGIFINDEX, &ifr);
ioctl(fdSocketCAN, SIOCGIFINDEX, &ifr);
/* Alternativement, zéro comme index d'interface, permet de récupérer les paquets de toutes les interfaces CAN.
Avec l'index de l'interface, maintenant lier le socket à l'interface CAN
*/

/*
Alternativement, zéro comme index d'interface, permet de récupérer les paquets de toutes les interfaces CAN.
Avec l'index de l'interface, maintenant lier le socket à l'interface CAN
*/
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;

if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
if (bind(fdSocketCAN, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Bind");
return 1;
return -1;
}

/*
Expand All @@ -66,14 +68,14 @@ int main(int argc, char **argv)
frame.can_dlc = 7; // nombre d'octets de données
sprintf(frame.data, "616-TGE"); // données

if (write(s, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame)) {
if (write(fdSocketCAN, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame)) {
perror("Write");
return 1;
return -1;
}

if (close(s) < 0) {
if (close(fdSocketCAN) < 0) {
perror("Close");
return 1;
return -1;
}

return 0;
Expand Down

0 comments on commit 150fc50

Please sign in to comment.