Mercurial > hg > toybox
annotate toys/other/lsusb.c @ 902:36993c59a3d3
Tighten up lsusb, default to "y".
author  Rob Landley <rob@landley.net> 

date  Tue, 14 May 2013 20:42:54 0500 
parents  edd8e6fd418d 
children 
rev  line source 

900
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

1 /* lsusb.c  list available USB devices 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

2 * 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

3 * Copyright 2013 Andre Renaud <andre@bluewatersys.com> 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

4 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

5 USE_LSUSB(NEWTOY(lsusb, NULL, TOYFLAG_USRTOYFLAG_BIN)) 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

6 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

7 config LSUSB 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

8 bool "lsusb" 
902
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

9 default y 
900
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

10 help 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

11 usage: lsusb 
902
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

12 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

13 List USB hosts/devices. 
900
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

14 */ 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

15 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

16 #include "toys.h" 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

17 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

18 static int list_device(struct dirtree *new) 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

19 { 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

20 FILE *file; 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

21 char *name; 
902
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

22 int busnum = 0, devnum = 0, pid = 0, vid = 0; 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

23 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

24 if (!new>parent) return DIRTREE_RECURSE; 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

25 if (new>name[0] == '.') return 0; 
900
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

26 name = dirtree_path(new, 0); 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

27 snprintf(toybuf, sizeof(toybuf), "%s/%s", name, "/uevent"); 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

28 file = fopen(toybuf, "r"); 
902
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

29 if (file) { 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

30 int count = 0; 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

31 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

32 while (fgets(toybuf, sizeof(toybuf), file)) 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

33 if (sscanf(toybuf, "BUSNUM=%u\n", &busnum) 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

34  sscanf(toybuf, "DEVNUM=%u\n", &devnum) 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

35  sscanf(toybuf, "PRODUCT=%x/%x/", &pid, &vid)) count++; 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

36 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

37 if (count == 3) 
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

38 printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid); 
900
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

39 fclose(file); 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

40 } 
902
36993c59a3d3
Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents:
900
diff
changeset

41 free(name); 
900
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

42 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

43 return 0; 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

44 } 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

45 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

46 void lsusb_main(void) 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

47 { 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

48 dirtree_read("/sys/bus/usb/devices/", list_device); 
edd8e6fd418d
Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff
changeset

49 } 