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.
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <linux/can.h>
17 #include <linux/can/raw.h>
19 #include <sys/ioctl.h>
28 static int net_init(char *ifname)
32 struct sockaddr_can addr;
35 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
41 /* Convert interface name to index */
42 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
43 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
44 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
45 perror("SIOCGIFINDEX");
49 /* Open the CAN interface */
50 memset(&addr, 0, sizeof(addr));
51 addr.can_family = AF_CAN;
52 addr.can_ifindex = ifr.ifr_ifindex;
53 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
58 recv_own_msgs = 1; /* 0 = disabled (default), 1 = enabled */
59 setsockopt(s, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
60 &recv_own_msgs, sizeof(recv_own_msgs));
69 int main(int argc, char **argv)
78 printf("syntax: %s IFNAME CAN_ID bap_node \n", argv[0]);
82 can_id = strtoul(argv[2], NULL, 0);
83 node_id = strtoul(argv[3], NULL, 0);
85 bap = vag_bap_rxer_alloc();
87 printf("Out of memory allocating BAP struct.\n");
91 printf("Listening for CAN ID: %x and Node %d\n", can_id, node_id);
95 s = net_init(argv[1]);
107 retval = select(s+1, &rdfs, NULL, NULL, NULL);
108 /* We currently rely on Linux timeout behavior here,
109 * i.e. the timeout now reflects the remaining time */
113 } else if (!retval) {
114 /* Timeout, we NEED to check this first */
115 } else if (FD_ISSET(s, &rdfs)) {
116 struct can_frame frame;
119 ret = read(s, &frame, sizeof(frame));
121 perror("read from CAN socket");
125 if (can_id == frame.can_id) {
126 struct BAP_Frame *bap_frame;
129 bap_frame = vag_bap_handle_can_frame(bap, &frame);
130 if (bap_frame && bap_frame->node == node_id) {
131 mvprintw(bap_frame->port, 0, "");
133 printw("%u. %2i/%-2i .%c%02i --",
137 bap_frame->is_multiframe ? 'm' : 's',
140 /* Limit huge packets */
141 if (bap_frame->len > 20) {
146 for (i = 0; i < bap_frame->len; i++) {
150 printw("%02x", (unsigned char)(bap_frame->data[i]));
156 mvprintw(bap_frame->port, 64, "");
157 for (i = 0; i < bap_frame->len; i++) {
158 unsigned char c = bap_frame->data[i];
159 if (!isprint(c) || c == '\n' || c == '\r') {
169 vag_bap_frame_free(bap_frame);
179 vag_bap_rxer_free(bap);