m3dev

cortex m3 debug tools -- superceded by mdebug
git clone http://frotz.net/git/m3dev.git
Log | Files | Refs | README | LICENSE

usbmon.c (2882B)


      1 /* usbmon.c
      2  *
      3  * Copyright 2011 Brian Swetland <swetland@frotz.net>
      4  * 
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <unistd.h>
     21 #include <fcntl.h>
     22 #include <string.h>
     23 #include <errno.h>
     24 
     25 #include <sys/ioctl.h>
     26 #include <linux/ioctl.h>
     27 
     28 typedef unsigned long long u64;
     29 typedef signed long long s64;
     30 typedef int s32;
     31 typedef unsigned short u16;
     32 
     33 #include "usbmon.h"
     34 
     35 static char _xfer[4] = "SICB";
     36 
     37 int main(int argc, char **argv) 
     38 {
     39 	unsigned char data[4096];
     40 	struct usbmon_packet hdr;
     41 	struct usbmon_get arg;
     42 	unsigned char filter_dev[128];
     43 	int fd, r, n;
     44 
     45 	memset(filter_dev, 0, sizeof(filter_dev));
     46 
     47 	fd = open("/dev/usbmon0", O_RDONLY);
     48 	if (fd < 0) 
     49 		return -1;
     50 
     51 	argc--;
     52 	argv++;
     53 	while (argc--) {
     54 		if (argv[0][0] == '-') {
     55 			switch(argv[0][1]) {
     56 			case 'x':
     57 				r = atoi(argv[0] + 2);
     58 				if ((r < 0) || (r > 127))
     59 					continue;
     60 				filter_dev[r] = 1;
     61 				break;
     62 			}
     63 		}
     64 		argv++;
     65 	}
     66 
     67 	arg.hdr = &hdr;
     68 	arg.data = data;
     69 	for (;;) {
     70 		arg.alloc = sizeof(data);
     71 		r = ioctl(fd, MON_IOCX_GET, &arg);
     72 		if (r < 0)
     73 			break;
     74 		if (filter_dev[hdr.devnum])
     75 			continue;
     76 		printf("%d.%03d.%03d %c %c%c %04x",
     77 			hdr.busnum, hdr.devnum, hdr.epnum & 0x7F,
     78 			hdr.type,
     79 			_xfer[hdr.xfer], (hdr.epnum & 0x80) ? 'i' : 'o',
     80 #if 0
     81 			hdr.flag_setup ? hdr.flag_setup : ' ',
     82 			hdr.flag_data ? hdr.flag_data : ' ',
     83 #endif
     84 			hdr.length);
     85 		if (hdr.type == 'S') {
     86 			if (hdr.xfer == 2) {
     87 				printf(" %02x %02x %02x%02x %02x%02x %02x%02x\n",
     88 					hdr.s.setup[0], hdr.s.setup[1],
     89 					hdr.s.setup[3], hdr.s.setup[2],
     90 					hdr.s.setup[5], hdr.s.setup[4],
     91 					hdr.s.setup[7], hdr.s.setup[6]);
     92 			} else {
     93 				goto dumpdata;
     94 			}
     95 	
     96 		} else {
     97 			switch (hdr.status) {
     98 			case 0:
     99 				printf(" OK\n");
    100 				break;
    101 			case -EPIPE:
    102 				printf(" STALLED\n");
    103 				break;
    104 			case -ENODEV:
    105 				printf(" DISCONNECTED\n");
    106 				break;
    107 			case -ETIMEDOUT:
    108 				printf(" TIMEDOUT\n");
    109 				break;
    110 			default:
    111 				printf(" %s\n", strerror(-hdr.status));
    112 			}
    113 		}
    114 		if (!hdr.len_cap) 
    115 			continue;
    116 		printf("                   ");
    117 dumpdata:
    118 		if (hdr.len_cap > sizeof(data))
    119 			hdr.len_cap = sizeof(data);
    120 		for (n = 0; n < hdr.len_cap; n++) 
    121 			printf((n & 3) ? "%02x" : " %02x",data[n]);
    122 		printf(" ");
    123 		for (n = 0; n < hdr.len_cap; n++)
    124 			putchar(((data[n] < 0x20) || (data[n] > 0x7F)) ? '.' : data[n]);
    125 		printf("\n");
    126 	}
    127 	return 0;	
    128 }
    129