added quick README
[alphamote.git] / info.c
blob0edfbb38534bd470abe272df771241b85b908c94
2 /* This file is generated with usbsnoop2libusb.pl from a usbsnoop log file. */
3 /* Latest version of the script should be in http://iki.fi/lindi/usb/usbsnoop2libusb.pl */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <assert.h>
8 #include <signal.h>
9 #include <ctype.h>
10 #include <usb.h>
11 #if 0
12 #include <linux/usbdevice_fs.h>
13 #define LIBUSB_AUGMENT
14 #include "libusb_augment.h"
15 #endif
17 struct usb_dev_handle *devh;
19 void release_usb_device(int dummy) {
20 int ret;
21 ret = usb_release_interface(devh, 0);
22 if (!ret)
23 printf("failed to release interface: %d\n", ret);
24 usb_close(devh);
25 if (!ret)
26 printf("failed to close interface: %d\n", ret);
27 exit(1);
30 struct usb_device *find_device(int vendor, int product) {
31 struct usb_bus *bus;
33 for (bus = usb_get_busses(); bus; bus = bus->next) {
34 struct usb_device *dev;
36 for (dev = bus->devices; dev; dev = dev->next) {
37 if (dev->descriptor.idVendor == vendor
38 && dev->descriptor.idProduct == product)
39 return dev;
42 return NULL;
45 void print_bytes(char *bytes, int len) {
46 int i;
47 if (len > 0) {
48 printf("00: ");
49 for (i=0; i<len; i++) {
50 printf("%02x ", (int)((unsigned char)bytes[i]));
51 if ((i + 1) % 10 == 0) {
52 printf("\n");
53 printf("%02d: ", (i / 10) + 1);
56 printf("\n");
60 void print_interpretation(char *buf) {
61 // byte 35 and 36 are 100 * fstop
62 long fstop_scaled = buf[35] & 255;
63 fstop_scaled |= (buf[36] << 8);
64 double fstop;
65 fstop = (fstop_scaled / 100.0);
66 printf("f%2.1f\n", fstop);
68 if (buf[95] == 1) {
69 // if byte 95 is 1
70 // byte 93 and 94 are 1/time
71 printf("using 1/s\n");
72 long time = (buf[93] & 255) | (buf[94] << 8);
73 printf("1/%ds\n", time);
75 else {
76 // byte 95 and 96 are 10*s (or bulb if 0)
77 long time = (buf[95] & 255) | (buf[96] << 8);
78 if (time != 0) {
79 double time_s;
80 time_s = (time / 10.0);
81 printf("%2.1fs\n", time_s);
83 else {
84 printf("bulb\n");
87 // byte 41 identifies the focusing type
88 if (buf[41] == 1) {
89 printf("M\n");
91 else if (buf[41] == 4) {
92 printf("AF-C\n");
94 else if (buf[41] == 5) {
95 printf("AF-A\n");
97 else if (buf[41] == 2) {
98 printf("AF-S\n");
101 // bytes 71/72 identify drive/bracketing type
102 if (buf[71] == 1) {
103 printf("Singleshot\n");
105 else if (buf[71] == 2) {
106 printf("Continuous drive (high)\n");
108 else if (buf[71] == 0x12) {
109 printf("Continuous drive (low)\n");
111 else if (buf[71] == 4) {
112 printf("Timer (10s)\n");
114 else if (buf[71] == 5) {
115 printf("Timer (2s)\n");
117 else if (buf[71] == '\x37' && buf[72] == '\x83') {
118 printf("Continuous bracketing (0.3 f-stops, 3 pictures)\n");
120 else if (buf[71] == '\x37' && buf[72] == '\x85') {
121 printf("Continuous bracketing (0.3 f-stops, 5 pictures)\n");
123 else if (buf[71] == '\x57' && buf[72] == '\x83') {
124 printf("Continuous bracketing (0.5 f-stops, 3 pictures)\n");
126 else if (buf[71] == '\x57' && buf[72] == '\x85') {
127 printf("Continuous bracketing (0.5 f-stops, 5 pictures)\n");
129 else if (buf[71] == '\x77' && buf[72] == '\x83') {
130 printf("Continuous bracketing (0.7 f-stops, 3 pictures)\n");
132 else if (buf[71] == '\x77' && buf[72] == '\x85') {
133 printf("Continuous bracketing (0.7 f-stops, 5 pictures)\n");
135 else if (buf[71] == '\x47') {
136 printf("Continuous bracketing (2 f-stops, 3 pictures)\n");
138 else if (buf[71] == '\x29') {
139 printf("DR-+ bracketing (high)\n");
141 else if (buf[71] == '\x19') {
142 printf("DR-+ bracketing (low)\n");
144 else if (buf[71] == '\x36' && buf[72] == '\x83') {
145 printf("Single bracketing (0.3 f-stops, 3 pictures)\n");
147 else if (buf[71] == '\x36' && buf[72] == '\x85') {
148 printf("Single bracketing (0.3 f-stops, 5 pictures)\n");
150 else if (buf[71] == '\x56' && buf[72] == '\x83') {
151 printf("Single bracketing (0.5 f-stops, 3 pictures)\n");
153 else if (buf[71] == '\x56' && buf[72] == '\x85') {
154 printf("Single bracketing (0.5 f-stops, 5 pictures)\n");
156 else if (buf[71] == '\x76' && buf[72] == '\x83') {
157 printf("Single bracketing (0.7 f-stops, 3 pictures)\n");
159 else if (buf[71] == '\x76' && buf[72] == '\x85') {
160 printf("Single bracketing (0.7 f-stops, 5 pictures)\n");
162 else if (buf[71] == '\x46') {
163 printf("Single bracketing (2 f-stops, 3 pictures)\n");
165 else if (buf[71] == '\x28') {
166 printf("WB bracketing (high)\n");
168 else if (buf[71] == '\x18') {
169 printf("WB bracketing (low)\n");
171 else if (buf[71] == '\x0a') {
172 printf("Remote control\n");
176 int main(int argc, char **argv) {
177 int ret, vendor, product;
178 struct usb_device *dev;
179 char buf[65535], *endptr;
181 usb_init();
182 usb_find_busses();
183 usb_find_devices();
185 vendor = 0x054c;
186 product = 0x02e7;
187 dev = find_device(vendor, product);
188 assert(dev);
190 devh = usb_open(dev);
191 assert(devh);
193 signal(SIGTERM, release_usb_device);
195 ret = usb_get_driver_np(devh, 0, buf, sizeof(buf));
196 if (ret == 0) {
197 printf("interface 0 already claimed by driver \"%s\", attempting to detach it\n", buf);
198 ret = usb_detach_kernel_driver_np(devh, 0);
199 printf("usb_detach_kernel_driver_np returned %d\n", ret);
201 ret = usb_claim_interface(devh, 0);
202 if (ret != 0) {
203 printf("claim failed with error %d\n", ret);
204 exit(1);
207 ret = usb_set_altinterface(devh, 0);
208 assert(ret >= 0);
210 ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
211 usleep(4*1000);
212 ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
213 usleep(3*1000);
214 ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000027);
215 usleep(4*1000);
216 ret = usb_release_interface(devh, 0);
217 if (ret != 0) printf("failed to release interface before set_configuration: %d\n", ret);
218 ret = usb_set_configuration(devh, 0x0000001);
219 ret = usb_claim_interface(devh, 0);
220 if (ret != 0) printf("claim after set_configuration failed with error %d\n", ret);
221 ret = usb_set_altinterface(devh, 0);
223 // get info (bulk read 62)
224 usleep(100*1000);
225 memcpy(buf, "\x0c\x00\x00\x00\x01\x00\x08\x92\x0f\x00\x00\x00", 0x000000c);
226 ret = usb_bulk_write(devh, 0x00000001, buf, 0x000000c, 1000);
227 usleep(4*1000);
228 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
229 int i;
230 print_interpretation(buf);
231 printf("\n");
232 print_bytes(buf, ret);
233 usleep(4*1000);
234 ret = usb_bulk_read(devh, 0x00000082, buf, 0x0000200, 1030);
235 ret = usb_release_interface(devh, 0);
236 assert(ret == 0);
237 ret = usb_close(devh);
238 assert(ret == 0);
239 return 0;