2 * Copyright 2017 Max Staudt
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License 2 as published
6 * by the Free Software Foundation.
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <linux/can.h>
18 #include <linux/can/raw.h>
20 #include <sys/ioctl.h>
29 static int net_init(char *ifname)
33 struct sockaddr_can addr;
36 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
42 /* Convert interface name to index */
43 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
44 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
45 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
46 perror("SIOCGIFINDEX");
50 /* Open the CAN interface */
51 memset(&addr, 0, sizeof(addr));
52 addr.can_family = AF_CAN;
53 addr.can_ifindex = ifr.ifr_ifindex;
54 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
59 recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */
60 setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
61 &recv_own_msgs, sizeof(recv_own_msgs));
70 int main(int argc, char **argv)
79 printf("syntax: %s IFNAME CAN_ID bap_node \n", argv[0]);
83 can_id = strtoul(argv[2], NULL, 0);
84 node_id = strtoul(argv[3], NULL, 0);
86 bap = vag_bap_rxer_alloc();
88 printf("Out of memory allocating BAP struct.\n");
92 printf("Listening for CAN ID: %x and Node %d\n", can_id, node_id);
96 s = net_init(argv[1]);
108 retval = select(s+1, &rfds, NULL, NULL, NULL);
109 /* We currently rely on Linux timeout behavior here,
110 * i.e. the timeout now reflects the remaining time */
114 } else if (!retval) {
115 /* Timeout, we NEED to check this first */
116 } else if (FD_ISSET(s, &rfds)) {
117 struct can_frame frame;
120 ret = read(s, &frame, sizeof(frame));
122 perror("read from CAN socket");
126 if (can_id == frame.can_id) {
127 struct BAP_Frame *bap_frame;
130 bap_frame = vag_bap_handle_can_frame(bap, &frame);
131 if (bap_frame && bap_frame->node == node_id) {
132 mvprintw(bap_frame->port, 0, "");
134 printw("%u. %2i/%-2i .%c%02i --",
138 bap_frame->is_multiframe ? 'm' : 's',
141 /* Limit huge packets */
142 if (bap_frame->len > 20) {
147 for (i = 0; i < bap_frame->len; i++) {
151 printw("%02x", (unsigned char)(bap_frame->data[i]));
157 mvprintw(bap_frame->port, 64, "");
158 for (i = 0; i < bap_frame->len; i++) {
159 unsigned char c = bap_frame->data[i];
160 if (!isprint(c) || c == '\n' || c == '\r') {
170 vag_bap_frame_free(bap_frame);
180 vag_bap_rxer_free(bap);