]> rtime.felk.cvut.cz Git - sojka/can-syscalls-examples.git/commitdiff
Update candump-mmap
authorMichal Sojka <sojkam1@fel.cvut.cz>
Wed, 29 Oct 2014 14:22:59 +0000 (15:22 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Wed, 29 Oct 2014 14:22:59 +0000 (15:22 +0100)
candump-mmap.c

index 10324739350bf5860a1c3442abeb492c6b596f07..dca80e900444b10c596e38cda22b3dde45489e79 100644 (file)
@@ -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[])