Mercurial > hg > minimon
comparison minimon.c @ 0:71b9540bdd23
starting, importing Woo's code and adding minimon
| author | Peter Meerwald <pmeerw@pmeerw.net> |
|---|---|
| date | Sat, 07 May 2011 17:29:58 +0200 |
| parents | |
| children | 111d4bbce605 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:71b9540bdd23 |
|---|---|
| 1 #include <stdlib.h> | |
| 2 #include <stdio.h> | |
| 3 #include <string.h> | |
| 4 #include <fcntl.h> | |
| 5 #include <assert.h> | |
| 6 #include "usb.h" | |
| 7 | |
| 8 static const char *progname = "minimon"; | |
| 9 | |
| 10 struct usb_device *find_dev() { | |
| 11 struct usb_bus *bus; | |
| 12 struct usb_device *dev; | |
| 13 | |
| 14 usb_init(); | |
| 15 usb_find_busses(); | |
| 16 usb_find_devices(); | |
| 17 | |
| 18 for (bus = usb_busses; bus; bus = bus->next) { | |
| 19 for (dev = bus->devices; dev; dev = dev->next) { | |
| 20 if (dev->descriptor.idVendor == 0x04e8 && | |
| 21 dev->descriptor.idProduct == 0x2028) { | |
| 22 return dev; | |
| 23 } | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 return NULL; | |
| 28 } | |
| 29 | |
| 30 usb_dev_handle *dev_open(struct usb_device *dev) { | |
| 31 int res = -1; | |
| 32 char buf[256]; | |
| 33 usb_dev_handle *udev; | |
| 34 int numeps = 0; | |
| 35 | |
| 36 udev = usb_open(dev); | |
| 37 if (!udev) { | |
| 38 fprintf(stderr, "%s: failed to open device, exit.\n", progname); | |
| 39 exit(EXIT_FAILURE); | |
| 40 } | |
| 41 | |
| 42 setuid(getuid()); | |
| 43 | |
| 44 res = usb_set_configuration(udev, 1); | |
| 45 | |
| 46 usb_claim_interface(udev, 0); | |
| 47 numeps = dev->config[0].interface[0].altsetting[0].bNumEndpoints; | |
| 48 if (numeps == 0) { | |
| 49 fprintf(stderr, "%s: no endpoints, exit.\n", progname); | |
| 50 exit(EXIT_FAILURE); | |
| 51 } | |
| 52 | |
| 53 strcpy(buf, "** no string **"); | |
| 54 res = usb_get_string_simple(udev, dev->descriptor.iManufacturer, buf, sizeof(buf)); | |
| 55 fprintf(stderr, "usb_get_string_simple => %d, %s\n", res, buf); | |
| 56 | |
| 57 { | |
| 58 int eplist[] = { 0x2, 0x81, 0x83 }; | |
| 59 int eplength = sizeof(eplist)/sizeof(eplist[0]); | |
| 60 int *endpoint = eplist; | |
| 61 int i; | |
| 62 for (i=0; i<eplength; i++) { | |
| 63 res = usb_resetep(udev, *endpoint); | |
| 64 res = usb_clear_halt(udev, *endpoint); | |
| 65 endpoint++; | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 return udev; | |
| 70 } | |
| 71 | |
| 72 | |
| 73 void send_jpeg(FILE *f, usb_dev_handle *udev) { | |
| 74 fseek(f, 0, SEEK_END); | |
| 75 int sz = ftell(f); | |
| 76 fseek(f, 0, SEEK_SET); | |
| 77 | |
| 78 #define URBBUF_MAX 0x20000 | |
| 79 char buf[URBBUF_MAX]; | |
| 80 | |
| 81 #define HDR_LEN 12 | |
| 82 char hdr[HDR_LEN] = {0xa5, 0x5a, 0x18, 0x04, 0xff, 0xff, 0xff, 0xff, 0x48, 0x00, 0x00, 0x00}; | |
| 83 *(int *)(hdr+4) = sz; | |
| 84 | |
| 85 memcpy(buf, hdr, HDR_LEN); | |
| 86 int off = HDR_LEN; | |
| 87 | |
| 88 while(!feof(f)) { | |
| 89 int nr = fread(buf+off, 1, URBBUF_MAX - off, f); | |
| 90 if (nr < 0) break; | |
| 91 // pad | |
| 92 memset(buf + off + nr, 0, URBBUF_MAX - off - nr); | |
| 93 | |
| 94 // write it out chunk by chunk | |
| 95 int timeout = 1000; | |
| 96 int endpoint = 0x2; | |
| 97 int res = usb_bulk_write(udev, endpoint, buf, URBBUF_MAX, timeout); | |
| 98 | |
| 99 assert(res >= 0); | |
| 100 off = 0; // no header on subsequent chunks | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 int main(int argc, char *argv[]) { | |
| 105 if (argc != 2) { | |
| 106 fprintf(stderr, "Usage: %s FILE", progname); | |
| 107 return EXIT_FAILURE; | |
| 108 } | |
| 109 | |
| 110 struct usb_device *dev = find_dev(index); | |
| 111 assert(dev != NULL); | |
| 112 | |
| 113 usb_dev_handle *udev = dev_open(dev); | |
| 114 | |
| 115 FILE *f = fopen(argv[1], "rb"); | |
| 116 if (f == NULL) { | |
| 117 fprintf(stderr, "%s: failed to open file '%s', exit.\n", progname, argv[1]); | |
| 118 exit(EXIT_FAILURE); | |
| 119 } | |
| 120 send_jpeg(f, udev); | |
| 121 fclose(f); | |
| 122 | |
| 123 return EXIT_SUCCESS; | |
| 124 } |
