summaryrefslogtreecommitdiffstats
path: root/src/MobileSync.c
diff options
context:
space:
mode:
authorGravatar Matt Colyer2009-07-26 19:34:22 -0700
committerGravatar Matt Colyer2009-07-26 19:34:22 -0700
commiteea538c94f01f8054f69f059614f19400187a472 (patch)
tree209a12dc8c8eaece15b8153d15e689c8c2147ab6 /src/MobileSync.c
parent8ebfd7d8eea89bb27e4e6dbb1f37fd90d98b439c (diff)
parent19c9750d670435ce430f0fc85a55faf127bdfbf9 (diff)
downloadlibimobiledevice-eea538c94f01f8054f69f059614f19400187a472.tar.gz
libimobiledevice-eea538c94f01f8054f69f059614f19400187a472.tar.bz2
Merge commit 'martin-s/martin'
[#46 state:resolved]
Diffstat (limited to 'src/MobileSync.c')
-rw-r--r--src/MobileSync.c55
1 files changed, 30 insertions, 25 deletions
diff --git a/src/MobileSync.c b/src/MobileSync.c
index b9a1cb0..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
32iphone_error_t mobilesync_new_client(iphone_device_t device, int dst_port, 34mobilesync_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
118iphone_error_t mobilesync_free_client(mobilesync_client_t client) 123mobilesync_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 */
134iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist) 139mobilesync_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);
@@ -161,7 +166,7 @@ iphone_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist)
161 char *XMLContent = NULL; 166 char *XMLContent = NULL;
162 uint32_t length = 0; 167 uint32_t length = 0;
163 plist_to_xml(*plist, &XMLContent, &length); 168 plist_to_xml(*plist, &XMLContent, &length);
164 log_dbg_msg(DBGMASK_MOBILESYNC, "Recv msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent); 169 log_dbg_msg(DBGMASK_MOBILESYNC, "%s: plist size: %i\nbuffer :\n%s\n", __func__, length, XMLContent);
165 free(XMLContent); 170 free(XMLContent);
166 171
167 return ret; 172 return ret;
@@ -177,15 +182,15 @@ 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 */
180iphone_error_t mobilesync_send(mobilesync_client_t client, plist_t plist) 185mobilesync_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;
187 plist_to_xml(plist, &XMLContent, &length); 192 plist_to_xml(plist, &XMLContent, &length);
188 log_dbg_msg(DBGMASK_MOBILESYNC, "Send msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent); 193 log_dbg_msg(DBGMASK_MOBILESYNC, "%s: plist size: %i\nbuffer :\n%s\n", __func__, length, XMLContent);
189 free(XMLContent); 194 free(XMLContent);
190 195
191 char *content = NULL; 196 char *content = NULL;
@@ -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