summaryrefslogtreecommitdiffstats
path: root/src/mobilebackup.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mobilebackup.c')
-rw-r--r--src/mobilebackup.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/src/mobilebackup.c b/src/mobilebackup.c
new file mode 100644
index 0000000..5b81c7f
--- /dev/null
+++ b/src/mobilebackup.c
@@ -0,0 +1,131 @@
1/*
2 * mobilebackup.c
3 * Contains functions for the built-in MobileBackup client.
4 *
5 * Copyright (c) 2009 Martin Szulecki 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 "mobilebackup.h"
28#include "device_link_service.h"
29#include "debug.h"
30
31#define MBACKUP_VERSION_INT1 100
32#define MBACKUP_VERSION_INT2 0
33
34/**
35 * Convert an device_link_service_error_t value to an mobilebackup_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 mobilebackup_error_t error code,
41 * MOBILEBACKUP_E_UNKNOWN_ERROR otherwise.
42 */
43static mobilebackup_error_t mobilebackup_error(device_link_service_error_t err)
44{
45 switch (err) {
46 case DEVICE_LINK_SERVICE_E_SUCCESS:
47 return MOBILEBACKUP_E_SUCCESS;
48 case DEVICE_LINK_SERVICE_E_INVALID_ARG:
49 return MOBILEBACKUP_E_INVALID_ARG;
50 case DEVICE_LINK_SERVICE_E_PLIST_ERROR:
51 return MOBILEBACKUP_E_PLIST_ERROR;
52 case DEVICE_LINK_SERVICE_E_MUX_ERROR:
53 return MOBILEBACKUP_E_MUX_ERROR;
54 case DEVICE_LINK_SERVICE_E_BAD_VERSION:
55 return MOBILEBACKUP_E_BAD_VERSION;
56 default:
57 break;
58 }
59 return MOBILEBACKUP_E_UNKNOWN_ERROR;
60}
61
62mobilebackup_error_t mobilebackup_client_new(iphone_device_t device, uint16_t port,
63 mobilebackup_client_t * client)
64{
65 if (!device || port == 0 || !client || *client)
66 return MOBILEBACKUP_E_INVALID_ARG;
67
68 device_link_service_client_t dlclient = NULL;
69 mobilebackup_error_t ret = mobilebackup_error(device_link_service_client_new(device, port, &dlclient));
70 if (ret != MOBILEBACKUP_E_SUCCESS) {
71 return ret;
72 }
73
74 mobilebackup_client_t client_loc = (mobilebackup_client_t) malloc(sizeof(struct mobilebackup_client_int));
75 client_loc->parent = dlclient;
76
77 /* perform handshake */
78 ret = mobilebackup_error(device_link_service_version_exchange(dlclient, MBACKUP_VERSION_INT1, MBACKUP_VERSION_INT2));
79 if (ret != MOBILEBACKUP_E_SUCCESS) {
80 debug_info("version exchange failed, error %d", ret);
81 mobilebackup_client_free(client_loc);
82 return ret;
83 }
84
85 *client = client_loc;
86
87 return ret;
88}
89
90mobilebackup_error_t mobilebackup_client_free(mobilebackup_client_t client)
91{
92 if (!client)
93 return MOBILEBACKUP_E_INVALID_ARG;
94 device_link_service_disconnect(client->parent);
95 mobilebackup_error_t err = mobilebackup_error(device_link_service_client_free(client->parent));
96 free(client);
97 return err;
98}
99
100/** Polls the iPhone for MobileBackup data.
101 *
102 * @param client The MobileBackup client
103 * @param plist A pointer to the location where the plist should be stored
104 *
105 * @return an error code
106 */
107mobilebackup_error_t mobilebackup_receive(mobilebackup_client_t client, plist_t * plist)
108{
109 if (!client)
110 return MOBILEBACKUP_E_INVALID_ARG;
111 mobilebackup_error_t ret = mobilebackup_error(device_link_service_receive(client->parent, plist));
112 return ret;
113}
114
115/** Sends MobileBackup data to the iPhone
116 *
117 * @note This function is low-level and should only be used if you need to send
118 * a new type of message.
119 *
120 * @param client The MobileBackup client
121 * @param plist The location of the plist to send
122 *
123 * @return an error code
124 */
125mobilebackup_error_t mobilebackup_send(mobilebackup_client_t client, plist_t plist)
126{
127 if (!client || !plist)
128 return MOBILEBACKUP_E_INVALID_ARG;
129 return mobilebackup_error(device_link_service_send(client->parent, plist));
130}
131