summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Patrick Walton2008-10-19 19:35:00 -0700
committerGravatar Jonathan Beck2008-10-25 14:48:03 +0200
commit76816c9fa6cd80fd7050977607403f6f8bbf0eba (patch)
treef3b904bb41cf2bc9cfc378b2ad2325c2e4d1c032
parent37ecfbc308078a47a9307286dce2a50f99a6dcf7 (diff)
downloadlibimobiledevice-76816c9fa6cd80fd7050977607403f6f8bbf0eba.tar.gz
libimobiledevice-76816c9fa6cd80fd7050977607403f6f8bbf0eba.tar.bz2
Draft iphone_get_specific_device() API, fix documentation for iphone_get_device()
-rw-r--r--src/iphone.c106
1 files changed, 72 insertions, 34 deletions
diff --git a/src/iphone.c b/src/iphone.c
index 81d6870..c14d61b 100644
--- a/src/iphone.c
+++ b/src/iphone.c
@@ -29,19 +29,31 @@
29 29
30extern int debug; 30extern int debug;
31 31
32/** Gets a handle to an iPhone 32/**
33 * 33 * Given a USB bus and device number, returns a device handle to the iPhone on
34 * @return A structure with data on the first iPhone it finds. (Or NULL, on 34 * that bus. To aid compatibility with future devices, this function does not
35 * error) 35 * check the vendor and device IDs! To do that, you should use
36 * iphone_get_device() or a system-specific API (e.g. HAL).
37 *
38 * @param bus_n The USB bus number.
39 * @param dev_n The USB device number.
40 * @param device A pointer to a iphone_device_t, which must be set to NULL upon
41 * calling iphone_get_specific_device, which will be filled with a device
42 * descriptor on return.
43 * @return IPHONE_E_SUCCESS if ok, otherwise an error code.
36 */ 44 */
37iphone_error_t iphone_get_device(iphone_device_t * device) 45iphone_error_t iphone_get_specific_device(int bus_n, int dev_n,
46 iphone_device_t *device)
38{ 47{
48 struct usb_bus *bus, *busses;
49 struct usb_device *dev;
50 usbmux_version_header *version;
51 int bytes = 0;
52
39 //check we can actually write in device 53 //check we can actually write in device
40 if (!device || (device && *device)) 54 if (!device || (device && *device))
41 return IPHONE_E_INVALID_ARG; 55 return IPHONE_E_INVALID_ARG;
42 56
43 struct usb_bus *bus, *busses;
44 struct usb_device *dev;
45 iphone_device_t phone = (iphone_device_t) malloc(sizeof(struct iphone_device_int)); 57 iphone_device_t phone = (iphone_device_t) malloc(sizeof(struct iphone_device_int));
46 58
47 // Initialize the struct 59 // Initialize the struct
@@ -55,36 +67,26 @@ iphone_error_t iphone_get_device(iphone_device_t * device)
55 usb_find_devices(); 67 usb_find_devices();
56 busses = usb_get_busses(); 68 busses = usb_get_busses();
57 69
58
59 // Set the device configuration 70 // Set the device configuration
60 for (bus = busses; bus; bus = bus->next) { 71 for (bus = busses; bus; bus = bus->next)
61 for (dev = bus->devices; dev; dev = dev->next) { 72 if (bus->location == bus_n)
62 if (dev->descriptor.idVendor == 0x05ac && 73 for (dev = bus->devices; dev != NULL; dev = dev->next)
63 (dev->descriptor.idProduct == 0x1290 || 74 if (dev->devnum == dev_n) {
64 dev->descriptor.idProduct == 0x1291 || dev->descriptor.idProduct == 0x1292 75 phone->__device = dev;
65 || dev->descriptor.idProduct == 0x1293) 76 phone->device = usb_open(phone->__device);
66 ) { 77 usb_set_configuration(phone->device, 3);
67 phone->__device = dev; 78 usb_claim_interface(phone->device, 1);
68 phone->device = usb_open(phone->__device); 79 goto found;
69 usb_set_configuration(phone->device, 3); 80 }
70 usb_claim_interface(phone->device, 1);
71 break;
72 }
73 }
74 if (phone->__device && phone->device)
75 break;
76 }
77 81
78 // Check to see if we are connected 82 iphone_free_device(phone);
79 if (!phone->device || !phone->__device) { 83 if (debug)
80 iphone_free_device(phone); 84 fprintf(stderr, "iphone_get_specific_device: iPhone not found\n");
81 if (debug) 85 return IPHONE_E_NO_DEVICE;
82 fprintf(stderr, "get_iPhone(): iPhone not found\n"); 86
83 return IPHONE_E_NO_DEVICE; 87found:
84 }
85 // Send the version command to the phone 88 // Send the version command to the phone
86 int bytes = 0; 89 version = version_header();
87 usbmux_version_header *version = version_header();
88 bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800); 90 bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800);
89 if (bytes < 20 && debug) { 91 if (bytes < 20 && debug) {
90 fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n"); 92 fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n");
@@ -131,6 +133,42 @@ iphone_error_t iphone_get_device(iphone_device_t * device)
131 return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad 133 return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad
132} 134}
133 135
136/**
137 * Scans all USB busses and devices for a known AFC-compatible device and
138 * returns a handle to the first such device it finds. Known devices include
139 * those with vendor ID 0x05ac and product ID between 0x1290 and 0x1293
140 * inclusive.
141 *
142 * This function is convenient, but on systems where higher-level abstractions
143 * (such as HAL) are available it may be preferable to use
144 * iphone_get_specific_device instead, because it can deal with multiple
145 * connected devices as well as devices not known to libiphone.
146 *
147 * @param device Upon calling this function, a pointer to a location of type
148 * iphone_device_t, which must have the value NULL. On return, this location
149 * will be filled with a handle to the device.
150 * @return IPHONE_E_SUCCESS if ok, otherwise an error code.
151 */
152iphone_error_t iphone_get_device(iphone_device_t *device)
153{
154 struct usb_bus *bus, *busses;
155 struct usb_device *dev;
156
157 usb_init();
158 usb_find_busses();
159 usb_find_devices();
160
161 for (bus = usb_get_busses(); bus != NULL; bus = bus->next)
162 for (dev = bus->devices; dev != NULL; dev = dev->next)
163 if (dev->descriptor.idVendor == 0x05ac
164 && dev->descriptor.idProduct >= 0x1290
165 && dev->descriptor.idProduct <= 0x1293)
166 return iphone_get_specific_device(bus->location, dev->devnum,
167 device);
168
169 return IPHONE_E_NO_DEVICE;
170}
171
134/** Cleans up an iPhone structure, then frees the structure itself. 172/** Cleans up an iPhone structure, then frees the structure itself.
135 * This is a library-level function; deals directly with the iPhone to tear 173 * This is a library-level function; deals directly with the iPhone to tear
136 * down relations, but otherwise is mostly internal. 174 * down relations, but otherwise is mostly internal.