quick shell script hack for tethered shooting with qiv
[alphamote.git] / download.c
blob4042357922f679a98ac753730919f526b42c63db
1 /* This file is generated with usbsnoop2libusb.pl from a usbsnoop log file. */
2 /* Latest version of the script should be in http://iki.fi/lindi/usb/usbsnoop2libusb.pl */
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <signal.h>
8 #include <ctype.h>
9 #include <usb.h>
10 #include <getopt.h>
11 #include "alphamote.h"
13 void print_bytes(char *bytes, int len) {
14 int i;
15 if (len > 0) {
16 for (i=0; i<len; i++) {
17 printf("%02x ", (int)((unsigned char)bytes[i]));
19 printf("\"");
20 for (i=0; i<len; i++) {
21 printf("%c", isprint(bytes[i]) ? bytes[i] : '.');
23 printf("\"");
28 int main(int argc, char **argv) {
29 int ret, c;
30 char buf[65535];
31 char *filename = "/tmp/foo.jpg";
33 // command line options
34 opterr = 0;
35 while ((c = getopt(argc, argv, "o:")) != -1) {
36 switch (c) {
37 case 'o':
38 if (optarg != NULL) {
39 filename = optarg;
41 break;
45 printf("filename: %s", filename);
47 init_usb();
49 // retrieving image
50 usleep(50*1000);
51 memcpy(buf, "\x10\x00\x00\x00\x01\x00\x08\x10\xfa\x06\x00\x00\x01\xc0\xff\xff", 0x0000010);
52 ret = usb_bulk_write(devh, 0x00000001, buf, 0x0000010, 1000);
53 printf("5469 bulk write returned %d, bytes: ", ret);
54 print_bytes(buf, ret);
55 printf("\n");
56 usleep(6*1000);
57 // 5470 contains the filename
58 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
59 printf("5470 bulk read returned %d, bytes: ", ret);
60 print_bytes(buf, ret);
61 printf("\n");
62 usleep(2*1000);
63 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
64 printf("5471 bulk read returned %d, bytes: ", ret);
65 print_bytes(buf, ret);
66 printf("\n");
67 usleep(4*1000);
68 ret = usb_interrupt_read(devh, 0x00000083, buf, 0x0000008, 1000);
69 printf("5472 interrupt read returned %d, bytes: ", ret);
70 print_bytes(buf, ret);
71 printf("\n");
72 usleep(16*1000);
73 memcpy(buf, "\x10\x00\x00\x00\x01\x00\x09\x10\xfb\x06\x00\x00\x01\xc0\xff\xff", 0x0000010);
74 ret = usb_bulk_write(devh, 0x00000001, buf, 0x0000010, 1000);
75 printf("5473 bulk write returned %d, bytes: ", ret);
76 print_bytes(buf, ret);
77 printf("\n");
78 usleep(5*1000);
79 FILE *out;
80 out = fopen("/tmp/foo.jpg", "w"); // TODO - error handling
81 // here, the actual image data starts (ignore 12 byte ptp headers)
82 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 4932);
83 int j;
84 for (j = 12; j < ret; j++) {
85 fputc(buf[j], out);
88 while (ret > 0) {
89 // something to read left
90 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 4932);
91 // append to file
92 int i;
93 for (i = 0; i < ret; i++) {
94 fputc(buf[i], out);
97 fclose(out);
98 ret = usb_release_interface(devh, 0);
99 assert(ret == 0);
100 ret = usb_close(devh);
101 assert(ret == 0);
102 return 0;