From 8634569d0ee2bfcfa7fcdef30446977c1542c2f8 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Wed, 29 Oct 2014 15:22:59 +0100 Subject: [PATCH] Update candump-mmap --- candump-mmap.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/candump-mmap.c b/candump-mmap.c index 1032473..dca80e9 100644 --- a/candump-mmap.c +++ b/candump-mmap.c @@ -26,8 +26,7 @@ #define FRAME_NR (BLOCK_NR*(BLOCK_SIZE/FRAME_SIZE)) void *rx_ring_buffer; -int hdrlen; -int current; +int current_frame; int init_mmap_socket(const char *dev) { @@ -39,8 +38,6 @@ int init_mmap_socket(const char *dev) int val = TPACKET_V2; CHECK(setsockopt(sock, SOL_PACKET, PACKET_VERSION, &val, sizeof(val))); - socklen_t len = sizeof(hdrlen); - CHECK(getsockopt(sock, SOL_PACKET, PACKET_HDRLEN, &hdrlen, &len)); strncpy (ifr.ifr_name, dev, sizeof(ifr.ifr_name)); CHECK(ioctl(sock, SIOCGIFINDEX, &ifr)); @@ -61,29 +58,28 @@ int init_mmap_socket(const char *dev) rx_ring_buffer = (char*)CHECKPTR(mmap(0, BLOCK_SIZE*BLOCK_NR, PROT_READ|PROT_WRITE, MAP_SHARED, sock, 0)); - current = 0; + current_frame = 0; return sock; } void can_rx_mmap(int sock) { - volatile struct tpacket2_hdr *hdr = rx_ring_buffer + current*FRAME_SIZE; + volatile struct tpacket2_hdr *hdr = rx_ring_buffer + current_frame*FRAME_SIZE; + /* Wait for a message to arrive. If the task wake-up overhead + * is too high, poll() can be commented out and the + * application will busy-wait here. */ while (hdr->tp_status == TP_STATUS_KERNEL) { struct pollfd pfd = {.fd = sock, .revents = 0, .events = POLLIN|POLLRDNORM|POLLERR }; - /* Wait for a message to arrive. If the task wake-up - * overhead is too high, poll() can be commented out - * and the application will busy-wait here. */ CHECK(poll(&pfd, 1, -1)); } struct can_frame *cf = (void*)hdr + hdr->tp_mac; - printf("Frame #%d received\n", current); print_can_frame(cf); hdr->tp_status = 0; /* Mark the frame as processed */ - current = (current + 1) % FRAME_NR; + current_frame = (current_frame + 1) % FRAME_NR; } int main(int argc, char *argv[]) -- 2.39.2