summaryrefslogtreecommitdiffstats
path: root/src/mobilesync.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mobilesync.c')
-rw-r--r--src/mobilesync.c148
1 files changed, 148 insertions, 0 deletions
diff --git a/src/mobilesync.c b/src/mobilesync.c
new file mode 100644
index 0000000..64c06d5
--- /dev/null
+++ b/src/mobilesync.c
@@ -0,0 +1,148 @@
1/*
2 * mobilesync.c
3 * Contains functions for the built-in MobileSync client.
4 *
5 * Copyright (c) 2009 Jonathan Beck All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <plist/plist.h>
23#include <string.h>
24#include <stdlib.h>
25#include <arpa/inet.h>
26
27#include "mobilesync.h"
28#include "device_link_service.h"
29#include "debug.h"
30
31#define MSYNC_VERSION_INT1 100
32#define MSYNC_VERSION_INT2 100
33
34/**
35 * Convert an device_link_service_error_t value to an mobilesync_error_t value.
36 * Used internally to get correct error codes when using device_link_service stuff.
37 *
38 * @param err An device_link_service_error_t error code
39 *
40 * @return A matching mobilesync_error_t error code,
41 * MOBILESYNC_E_UNKNOWN_ERROR otherwise.
42 */
43static mobilesync_error_t mobilesync_error(device_link_service_error_t err)
44{
45 switch (err) {
46 case DEVICE_LINK_SERVICE_E_SUCCESS:
47 return MOBILESYNC_E_SUCCESS;
48 case DEVICE_LINK_SERVICE_E_INVALID_ARG:
49 return MOBILESYNC_E_INVALID_ARG;
50 case DEVICE_LINK_SERVICE_E_PLIST_ERROR:
51 return MOBILESYNC_E_PLIST_ERROR;
52 case DEVICE_LINK_SERVICE_E_MUX_ERROR:
53 return MOBILESYNC_E_MUX_ERROR;
54 case DEVICE_LINK_SERVICE_E_BAD_VERSION:
55 return MOBILESYNC_E_BAD_VERSION;
56 default:
57 break;
58 }
59 return MOBILESYNC_E_UNKNOWN_ERROR;
60}
61
62mobilesync_error_t mobilesync_client_new(iphone_device_t device, uint16_t port,
63 mobilesync_client_t * client)
64{
65 if (!device || port == 0 || !client || *client)
66 return MOBILESYNC_E_INVALID_ARG;
67
68 device_link_service_client_t dlclient = NULL;
69 mobilesync_error_t ret = mobilesync_error(device_link_service_client_new(device, port, &dlclient));
70 if (ret != MOBILESYNC_E_SUCCESS) {
71 return ret;
72 }
73
74 mobilesync_client_t client_loc = (mobilesync_client_t) malloc(sizeof(struct mobilesync_client_int));
75 client_loc->parent = dlclient;
76
77 /* perform handshake */
78 ret = mobilesync_error(device_link_service_version_exchange(dlclient, MSYNC_VERSION_INT1, MSYNC_VERSION_INT2));
79 if (ret != MOBILESYNC_E_SUCCESS) {
80 debug_info("version exchange failed, error %d", ret);
81 mobilesync_client_free(client_loc);
82 return ret;
83 }
84
85 *client = client_loc;
86
87 return ret;
88}
89
90mobilesync_error_t mobilesync_client_free(mobilesync_client_t client)
91{
92 if (!client)
93 return MOBILESYNC_E_INVALID_ARG;
94 device_link_service_disconnect(client->parent);
95 mobilesync_error_t err = mobilesync_error(device_link_service_client_free(client->parent));
96 free(client);
97 return err;
98}
99
100/** Polls the iPhone for MobileSync data.
101 *
102 * @param client The MobileSync client
103 * @param plist A pointer to the location where the plist should be stored
104 *
105 * @return an error code
106 */
107mobilesync_error_t mobilesync_recv(mobilesync_client_t client, plist_t * plist)
108{
109 if (!client)
110 return MOBILESYNC_E_INVALID_ARG;
111 mobilesync_error_t ret = mobilesync_error(device_link_service_receive(client->parent, plist));
112#ifndef STRIP_DEBUG_CODE
113 if (ret != MOBILESYNC_E_SUCCESS) {
114 return ret;
115 }
116 char *XMLContent = NULL;
117 uint32_t length = 0;
118 plist_to_xml(*plist, &XMLContent, &length);
119 debug_info("plist size: %i\nbuffer :\n%s", length, XMLContent);
120 free(XMLContent);
121#endif
122 return ret;
123}
124
125/** Sends MobileSync data to the iPhone
126 *
127 * @note This function is low-level and should only be used if you need to send
128 * a new type of message.
129 *
130 * @param client The MobileSync client
131 * @param plist The location of the plist to send
132 *
133 * @return an error code
134 */
135mobilesync_error_t mobilesync_send(mobilesync_client_t client, plist_t plist)
136{
137 if (!client || !plist)
138 return MOBILESYNC_E_INVALID_ARG;
139
140#ifndef STRIP_DEBUG_CODE
141 char *XMLContent = NULL;
142 uint32_t length = 0;
143 plist_to_xml(plist, &XMLContent, &length);
144 debug_info("plist size: %i\nbuffer :\n%s", length, XMLContent);
145 free(XMLContent);
146#endif
147 return mobilesync_error(device_link_service_send(client->parent, plist));
148}