#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)
{
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));
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[])