diff options
| author | 2009-07-25 02:08:12 +0200 | |
|---|---|---|
| committer | 2009-07-25 02:08:12 +0200 | |
| commit | 40a0abaa1c7f2196d40276b0689b18a04e468acd (patch) | |
| tree | aee97118e637ccc7b8ab638a77a154b6bbed4f4b | |
| parent | 66695e6b7db9457d5ecfe6f4f6624dc195e5274d (diff) | |
| download | libimobiledevice-40a0abaa1c7f2196d40276b0689b18a04e468acd.tar.gz libimobiledevice-40a0abaa1c7f2196d40276b0689b18a04e468acd.tar.bz2 | |
Update MobileSync API and introduce mobilesync error codes
| -rw-r--r-- | dev/msyncclient.c | 12 | ||||
| -rw-r--r-- | include/libiphone/mobilesync.h | 20 | ||||
| -rw-r--r-- | src/MobileSync.c | 51 | ||||
| -rw-r--r-- | src/MobileSync.h | 4 |
4 files changed, 48 insertions, 39 deletions
diff --git a/dev/msyncclient.c b/dev/msyncclient.c index 5eb105d..7651002 100644 --- a/dev/msyncclient.c +++ b/dev/msyncclient.c | |||
| @@ -28,12 +28,12 @@ | |||
| 28 | #include <libiphone/lockdown.h> | 28 | #include <libiphone/lockdown.h> |
| 29 | #include <libiphone/mobilesync.h> | 29 | #include <libiphone/mobilesync.h> |
| 30 | 30 | ||
| 31 | static iphone_error_t mobilesync_get_all_contacts(mobilesync_client_t client) | 31 | static mobilesync_error_t mobilesync_get_all_contacts(mobilesync_client_t client) |
| 32 | { | 32 | { |
| 33 | if (!client) | 33 | if (!client) |
| 34 | return IPHONE_E_INVALID_ARG; | 34 | return MOBILESYNC_E_INVALID_ARG; |
| 35 | 35 | ||
| 36 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 36 | mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR; |
| 37 | plist_t array = NULL; | 37 | plist_t array = NULL; |
| 38 | 38 | ||
| 39 | array = plist_new_array(); | 39 | array = plist_new_array(); |
| @@ -124,7 +124,6 @@ static iphone_error_t mobilesync_get_all_contacts(mobilesync_client_t client) | |||
| 124 | plist_free(array); | 124 | plist_free(array); |
| 125 | array = NULL; | 125 | array = NULL; |
| 126 | 126 | ||
| 127 | |||
| 128 | return ret; | 127 | return ret; |
| 129 | } | 128 | } |
| 130 | 129 | ||
| @@ -137,7 +136,6 @@ int main(int argc, char *argv[]) | |||
| 137 | if (argc > 1 && !strcasecmp(argv[1], "--debug")) | 136 | if (argc > 1 && !strcasecmp(argv[1], "--debug")) |
| 138 | iphone_set_debug_mask(DBGMASK_MOBILESYNC); | 137 | iphone_set_debug_mask(DBGMASK_MOBILESYNC); |
| 139 | 138 | ||
| 140 | |||
| 141 | if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) { | 139 | if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) { |
| 142 | printf("No iPhone found, is it plugged in?\n"); | 140 | printf("No iPhone found, is it plugged in?\n"); |
| 143 | return -1; | 141 | return -1; |
| @@ -152,10 +150,10 @@ int main(int argc, char *argv[]) | |||
| 152 | 150 | ||
| 153 | if (port) { | 151 | if (port) { |
| 154 | mobilesync_client_t msync = NULL; | 152 | mobilesync_client_t msync = NULL; |
| 155 | mobilesync_new_client(phone, port, &msync); | 153 | mobilesync_client_new(phone, port, &msync); |
| 156 | if (msync) { | 154 | if (msync) { |
| 157 | mobilesync_get_all_contacts(msync); | 155 | mobilesync_get_all_contacts(msync); |
| 158 | mobilesync_free_client(msync); | 156 | mobilesync_client_free(msync); |
| 159 | } | 157 | } |
| 160 | } else { | 158 | } else { |
| 161 | printf("Start service failure.\n"); | 159 | printf("Start service failure.\n"); |
diff --git a/include/libiphone/mobilesync.h b/include/libiphone/mobilesync.h index 128be45..f8fa711 100644 --- a/include/libiphone/mobilesync.h +++ b/include/libiphone/mobilesync.h | |||
| @@ -27,14 +27,24 @@ extern "C" { | |||
| 27 | 27 | ||
| 28 | #include <libiphone/libiphone.h> | 28 | #include <libiphone/libiphone.h> |
| 29 | 29 | ||
| 30 | /* Error Codes */ | ||
| 31 | #define MOBILESYNC_E_SUCCESS 0 | ||
| 32 | #define MOBILESYNC_E_INVALID_ARG -1 | ||
| 33 | #define MOBILESYNC_E_PLIST_ERROR -2 | ||
| 34 | #define MOBILESYNC_E_MUX_ERROR -3 | ||
| 35 | #define MOBILESYNC_E_BAD_VERSION -4 | ||
| 36 | |||
| 37 | #define MOBILESYNC_E_UNKNOWN_ERROR -256 | ||
| 38 | |||
| 39 | typedef int16_t mobilesync_error_t; | ||
| 40 | |||
| 30 | struct mobilesync_client_int; | 41 | struct mobilesync_client_int; |
| 31 | typedef struct mobilesync_client_int *mobilesync_client_t; | 42 | typedef struct mobilesync_client_int *mobilesync_client_t; |
| 32 | 43 | ||
| 33 | iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port, | 44 | mobilesync_error_t mobilesync_client_new(iphone_device_t device, int dst_port, mobilesync_client_t * client); |
| 34 | mobilesync_client_t * client); | 45 | mobilesync_error_t mobilesync_client_free(mobilesync_client_t client); |
| 35 | iphone_error_t mobilesync_free_client(mobilesync_client_t client); | 46 | mobilesync_error_t mobilesync_recv(mobilesync_client_t client, plist_t *plist); |
| 36 | iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t *plist); | 47 | mobilesync_error_t mobilesync_send(mobilesync_client_t client, plist_t plist); |
| 37 | iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist); | ||
| 38 | 48 | ||
| 39 | #ifdef __cplusplus | 49 | #ifdef __cplusplus |
| 40 | } | 50 | } |
diff --git a/src/MobileSync.c b/src/MobileSync.c index f544400..4463251 100644 --- a/src/MobileSync.c +++ b/src/MobileSync.c | |||
| @@ -19,25 +19,27 @@ | |||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ | 20 | */ |
| 21 | 21 | ||
| 22 | #include "MobileSync.h" | ||
| 23 | #include <plist/plist.h> | 22 | #include <plist/plist.h> |
| 24 | #include <string.h> | 23 | #include <string.h> |
| 25 | #include <stdlib.h> | 24 | #include <stdlib.h> |
| 26 | #include <arpa/inet.h> | 25 | #include <arpa/inet.h> |
| 27 | 26 | ||
| 27 | #include "MobileSync.h" | ||
| 28 | #include "iphone.h" | ||
| 29 | #include "utils.h" | ||
| 28 | 30 | ||
| 29 | #define MSYNC_VERSION_INT1 100 | 31 | #define MSYNC_VERSION_INT1 100 |
| 30 | #define MSYNC_VERSION_INT2 100 | 32 | #define MSYNC_VERSION_INT2 100 |
| 31 | 33 | ||
| 32 | iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port, | 34 | mobilesync_error_t mobilesync_client_new(iphone_device_t device, int dst_port, |
| 33 | mobilesync_client_t * client) | 35 | mobilesync_client_t * client) |
| 34 | { | 36 | { |
| 35 | if (!device || dst_port == 0 || !client || *client) | 37 | if (!device || dst_port == 0 || !client || *client) |
| 36 | return IPHONE_E_INVALID_ARG; | 38 | return MOBILESYNC_E_INVALID_ARG; |
| 37 | 39 | ||
| 38 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 40 | mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR; |
| 39 | 41 | ||
| 40 | // Attempt connection | 42 | /* Attempt connection */ |
| 41 | int sfd = usbmuxd_connect(device->handle, dst_port); | 43 | int sfd = usbmuxd_connect(device->handle, dst_port); |
| 42 | if (sfd < 0) { | 44 | if (sfd < 0) { |
| 43 | return ret; | 45 | return ret; |
| @@ -46,10 +48,10 @@ iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port, | |||
| 46 | mobilesync_client_t client_loc = (mobilesync_client_t) malloc(sizeof(struct mobilesync_client_int)); | 48 | mobilesync_client_t client_loc = (mobilesync_client_t) malloc(sizeof(struct mobilesync_client_int)); |
| 47 | client_loc->sfd = sfd; | 49 | client_loc->sfd = sfd; |
| 48 | 50 | ||
| 49 | //perform handshake | 51 | /* perform handshake */ |
| 50 | plist_t array = NULL; | 52 | plist_t array = NULL; |
| 51 | 53 | ||
| 52 | //first receive version | 54 | /* first receive version */ |
| 53 | ret = mobilesync_recv(client_loc, &array); | 55 | ret = mobilesync_recv(client_loc, &array); |
| 54 | 56 | ||
| 55 | plist_t msg_node = plist_find_node_by_string(array, "DLMessageVersionExchange"); | 57 | plist_t msg_node = plist_find_node_by_string(array, "DLMessageVersionExchange"); |
| @@ -86,17 +88,20 @@ iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port, | |||
| 86 | plist_t rep_node = plist_find_node_by_string(array, "DLMessageDeviceReady"); | 88 | plist_t rep_node = plist_find_node_by_string(array, "DLMessageDeviceReady"); |
| 87 | 89 | ||
| 88 | if (rep_node) { | 90 | if (rep_node) { |
| 89 | ret = IPHONE_E_SUCCESS; | 91 | ret = MOBILESYNC_E_SUCCESS; |
| 90 | *client = client_loc; | 92 | *client = client_loc; |
| 91 | } | 93 | } |
| 94 | else | ||
| 95 | { | ||
| 96 | ret = MOBILESYNC_E_BAD_VERSION; | ||
| 97 | } | ||
| 92 | plist_free(array); | 98 | plist_free(array); |
| 93 | array = NULL; | 99 | array = NULL; |
| 94 | |||
| 95 | } | 100 | } |
| 96 | } | 101 | } |
| 97 | 102 | ||
| 98 | if (IPHONE_E_SUCCESS != ret) | 103 | if (MOBILESYNC_E_SUCCESS != ret) |
| 99 | mobilesync_free_client(client_loc); | 104 | mobilesync_client_free(client_loc); |
| 100 | 105 | ||
| 101 | return ret; | 106 | return ret; |
| 102 | } | 107 | } |
| @@ -115,13 +120,13 @@ static void mobilesync_disconnect(mobilesync_client_t client) | |||
| 115 | array = NULL; | 120 | array = NULL; |
| 116 | } | 121 | } |
| 117 | 122 | ||
| 118 | iphone_error_t mobilesync_free_client(mobilesync_client_t client) | 123 | mobilesync_error_t mobilesync_client_free(mobilesync_client_t client) |
| 119 | { | 124 | { |
| 120 | if (!client) | 125 | if (!client) |
| 121 | return IPHONE_E_INVALID_ARG; | 126 | return IPHONE_E_INVALID_ARG; |
| 122 | 127 | ||
| 123 | mobilesync_disconnect(client); | 128 | mobilesync_disconnect(client); |
| 124 | return usbmuxd_disconnect(client->sfd); | 129 | return (usbmuxd_disconnect(client->sfd) == 0 ? MOBILESYNC_E_SUCCESS: MOBILESYNC_E_MUX_ERROR); |
| 125 | } | 130 | } |
| 126 | 131 | ||
| 127 | /** Polls the iPhone for MobileSync data. | 132 | /** Polls the iPhone for MobileSync data. |
| @@ -131,11 +136,11 @@ iphone_error_t mobilesync_free_client(mobilesync_client_t client) | |||
| 131 | * | 136 | * |
| 132 | * @return an error code | 137 | * @return an error code |
| 133 | */ | 138 | */ |
| 134 | iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist) | 139 | mobilesync_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist) |
| 135 | { | 140 | { |
| 136 | if (!client || !plist || (plist && *plist)) | 141 | if (!client || !plist || (plist && *plist)) |
| 137 | return IPHONE_E_INVALID_ARG; | 142 | return MOBILESYNC_E_INVALID_ARG; |
| 138 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 143 | mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR; |
| 139 | char *receive = NULL; | 144 | char *receive = NULL; |
| 140 | uint32_t datalen = 0, bytes = 0, received_bytes = 0; | 145 | uint32_t datalen = 0, bytes = 0, received_bytes = 0; |
| 141 | 146 | ||
| @@ -145,14 +150,14 @@ iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist) | |||
| 145 | receive = (char *) malloc(sizeof(char) * datalen); | 150 | receive = (char *) malloc(sizeof(char) * datalen); |
| 146 | 151 | ||
| 147 | /* fill buffer and request more packets if needed */ | 152 | /* fill buffer and request more packets if needed */ |
| 148 | while ((received_bytes < datalen) && (ret == IPHONE_E_SUCCESS)) { | 153 | while ((received_bytes < datalen) && (ret == MOBILESYNC_E_SUCCESS)) { |
| 149 | ret = usbmuxd_recv(client->sfd, receive + received_bytes, datalen - received_bytes, &bytes); | 154 | ret = usbmuxd_recv(client->sfd, receive + received_bytes, datalen - received_bytes, &bytes); |
| 150 | received_bytes += bytes; | 155 | received_bytes += bytes; |
| 151 | } | 156 | } |
| 152 | 157 | ||
| 153 | if (ret != IPHONE_E_SUCCESS) { | 158 | if (ret != MOBILESYNC_E_SUCCESS) { |
| 154 | free(receive); | 159 | free(receive); |
| 155 | return ret; | 160 | return MOBILESYNC_E_MUX_ERROR; |
| 156 | } | 161 | } |
| 157 | 162 | ||
| 158 | plist_from_bin(receive, received_bytes, plist); | 163 | plist_from_bin(receive, received_bytes, plist); |
| @@ -177,10 +182,10 @@ iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist) | |||
| 177 | * | 182 | * |
| 178 | * @return an error code | 183 | * @return an error code |
| 179 | */ | 184 | */ |
| 180 | iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist) | 185 | mobilesync_error_t mobilesync_send(mobilesync_client_t client, plist_t plist) |
| 181 | { | 186 | { |
| 182 | if (!client || !plist) | 187 | if (!client || !plist) |
| 183 | return IPHONE_E_INVALID_ARG; | 188 | return MOBILESYNC_E_INVALID_ARG; |
| 184 | 189 | ||
| 185 | char *XMLContent = NULL; | 190 | char *XMLContent = NULL; |
| 186 | uint32_t length = 0; | 191 | uint32_t length = 0; |
| @@ -195,7 +200,7 @@ iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist) | |||
| 195 | 200 | ||
| 196 | char *real_query; | 201 | char *real_query; |
| 197 | int bytes; | 202 | int bytes; |
| 198 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | 203 | mobilesync_error_t ret = MOBILESYNC_E_UNKNOWN_ERROR; |
| 199 | 204 | ||
| 200 | real_query = (char *) malloc(sizeof(char) * (length + 4)); | 205 | real_query = (char *) malloc(sizeof(char) * (length + 4)); |
| 201 | length = htonl(length); | 206 | length = htonl(length); |
| @@ -204,6 +209,6 @@ iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist) | |||
| 204 | 209 | ||
| 205 | ret = usbmuxd_send(client->sfd, real_query, ntohl(length) + sizeof(length), (uint32_t*)&bytes); | 210 | ret = usbmuxd_send(client->sfd, real_query, ntohl(length) + sizeof(length), (uint32_t*)&bytes); |
| 206 | free(real_query); | 211 | free(real_query); |
| 207 | return ret; | 212 | return (ret == 0 ? MOBILESYNC_E_SUCCESS: MOBILESYNC_E_MUX_ERROR); |
| 208 | } | 213 | } |
| 209 | 214 | ||
diff --git a/src/MobileSync.h b/src/MobileSync.h index 5279ce0..6347399 100644 --- a/src/MobileSync.h +++ b/src/MobileSync.h | |||
| @@ -21,12 +21,8 @@ | |||
| 21 | #ifndef MOBILESYNC_H | 21 | #ifndef MOBILESYNC_H |
| 22 | #define MOBILESYNC_H | 22 | #define MOBILESYNC_H |
| 23 | 23 | ||
| 24 | #include "iphone.h" | ||
| 25 | #include "utils.h" | ||
| 26 | #include "libiphone/mobilesync.h" | 24 | #include "libiphone/mobilesync.h" |
| 27 | 25 | ||
| 28 | #include <plist/plist.h> | ||
| 29 | |||
| 30 | struct mobilesync_client_int { | 26 | struct mobilesync_client_int { |
| 31 | int sfd; | 27 | int sfd; |
| 32 | }; | 28 | }; |
