diff options
| author | 2010-05-13 05:59:36 -0400 | |
|---|---|---|
| committer | 2010-05-13 05:59:36 -0400 | |
| commit | a57e39d7180ae1c7ce28105fb0c735121dec6f0d (patch) | |
| tree | f435bddb3658cb310cb9f5e57b20775f32ec26c7 /src/libirecovery.c | |
| parent | 2e8feabfe1ec82e75434663d5e3128e1769f34c5 (diff) | |
| download | libirecovery-a57e39d7180ae1c7ce28105fb0c735121dec6f0d.tar.gz libirecovery-a57e39d7180ae1c7ce28105fb0c735121dec6f0d.tar.bz2 | |
Began work. Added basic Makefile and implemented irecv_init(), irecv_open(), irecv_close(), and irecv_exit()
Needs to be tested on MacOSX and Windows, and Makefile needs to be updated for these platforms.
Diffstat (limited to 'src/libirecovery.c')
| -rw-r--r-- | src/libirecovery.c | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/libirecovery.c b/src/libirecovery.c new file mode 100644 index 0000000..dfc0aea --- /dev/null +++ b/src/libirecovery.c | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | /** | ||
| 2 | * iRecovery - Utility for DFU 2.0, WTF and Recovery Mode | ||
| 3 | * Copyright (C) 2008 - 2009 westbaer | ||
| 4 | * | ||
| 5 | * This program is free software: you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation, either version 3 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | **/ | ||
| 18 | |||
| 19 | #include <stdio.h> | ||
| 20 | #include <stdlib.h> | ||
| 21 | #include <string.h> | ||
| 22 | #include <libusb-1.0/libusb.h> | ||
| 23 | |||
| 24 | #include "libirecovery.h" | ||
| 25 | |||
| 26 | static unsigned int debug = 1; | ||
| 27 | |||
| 28 | int irecv_init(irecv_device** p_device) { | ||
| 29 | struct libusb_context* usb_context = NULL; | ||
| 30 | |||
| 31 | libusb_init(&usb_context); | ||
| 32 | if (debug) | ||
| 33 | libusb_set_debug(usb_context, 3); | ||
| 34 | |||
| 35 | irecv_device* device = (irecv_device*) malloc(sizeof(irecv_device)); | ||
| 36 | if (device == NULL) { | ||
| 37 | *p_device = NULL; | ||
| 38 | return IRECV_ERROR_OUT_OF_MEMORY; | ||
| 39 | } | ||
| 40 | memset(device, '\0', sizeof(irecv_device)); | ||
| 41 | device->context = usb_context; | ||
| 42 | |||
| 43 | *p_device = device; | ||
| 44 | return IRECV_SUCCESS; | ||
| 45 | } | ||
| 46 | |||
| 47 | int irecv_open(irecv_device* device) { | ||
| 48 | int i = 0; | ||
| 49 | int usb_device_count = 0; | ||
| 50 | struct libusb_device* usb_device = NULL; | ||
| 51 | struct libusb_device** usb_device_list = NULL; | ||
| 52 | struct libusb_device_handle* usb_handle = NULL; | ||
| 53 | struct libusb_device_descriptor usb_descriptor; | ||
| 54 | |||
| 55 | if (device == NULL || device->context == NULL) { | ||
| 56 | return IRECV_ERROR_NO_DEVICE; | ||
| 57 | } | ||
| 58 | |||
| 59 | usb_device_count = libusb_get_device_list(device->context, &usb_device_list); | ||
| 60 | for (i = 0; i < usb_device_count; i++) { | ||
| 61 | usb_device = usb_device_list[i]; | ||
| 62 | libusb_get_device_descriptor(usb_device, &usb_descriptor); | ||
| 63 | if (usb_descriptor.idVendor == kAppleVendorId) { | ||
| 64 | |||
| 65 | libusb_open(usb_device, &usb_handle); | ||
| 66 | if (usb_handle == NULL) { | ||
| 67 | libusb_free_device_list(usb_device_list, 1); | ||
| 68 | return IRECV_ERROR_UNABLE_TO_CONNECT; | ||
| 69 | } | ||
| 70 | |||
| 71 | libusb_free_device_list(usb_device_list, 1); | ||
| 72 | device->mode = usb_descriptor.idProduct; | ||
| 73 | device->handle = usb_handle; | ||
| 74 | return IRECV_SUCCESS; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | return IRECV_ERROR_NO_DEVICE; | ||
| 79 | } | ||
| 80 | |||
| 81 | int irecv_close(irecv_device* device) { | ||
| 82 | if (device != NULL) { | ||
| 83 | if (device->handle != NULL) { | ||
| 84 | libusb_close(device->handle); | ||
| 85 | device->handle = NULL; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | return IRECV_SUCCESS; | ||
| 90 | } | ||
| 91 | |||
| 92 | int irecv_exit(irecv_device* device) { | ||
| 93 | if (device != NULL) { | ||
| 94 | if (device->handle != NULL) { | ||
| 95 | libusb_close(device->handle); | ||
| 96 | device->handle = NULL; | ||
| 97 | } | ||
| 98 | |||
| 99 | if (device->context != NULL) { | ||
| 100 | libusb_exit(device->context); | ||
| 101 | device->context = NULL; | ||
| 102 | } | ||
| 103 | |||
| 104 | free(device); | ||
| 105 | device = NULL; | ||
| 106 | } | ||
| 107 | |||
| 108 | return IRECV_SUCCESS; | ||
| 109 | } | ||
