1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/**
* device.c
* Device communication functions.
*
* Copyright (C) 2009-2010 Nikias Bassen <nikias@gmx.li>
* Copyright (C) 2009-2010 Martin Szulecki <opensuse@sukimashita.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more profile.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h> /* for GETTEXT_PACKAGE */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <plist/plist.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/lockdown.h>
#include <libimobiledevice/sbservices.h>
#include "device.h"
static GMutex *idevice_mutex = NULL;
static GQuark device_domain = 0;
void device_init()
{
idevice_mutex = g_mutex_new();
device_domain = g_quark_from_string("libimobiledevice");
}
sbservices_client_t device_sbs_new(const char *uuid, uint32_t *osversion, GError **error)
{
sbservices_client_t sbc = NULL;
idevice_t phone = NULL;
lockdownd_client_t client = NULL;
uint16_t port = 0;
printf("%s: %s\n", __func__, uuid);
g_mutex_lock(idevice_mutex);
if (IDEVICE_E_SUCCESS != idevice_new(&phone, uuid)) {
if (error)
*error = g_error_new(device_domain, ENODEV, _("No device found, is it plugged in?"));
g_mutex_unlock(idevice_mutex);
return sbc;
}
if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "sbmanager")) {
if (error)
*error = g_error_new(device_domain, EIO, _("Could not connect to lockdownd!"));
goto leave_cleanup;
}
plist_t version = NULL;
if (osversion && lockdownd_get_value(client, NULL, "ProductVersion", &version) == LOCKDOWN_E_SUCCESS) {
if (plist_get_node_type(version) == PLIST_STRING) {
char *version_string = NULL;
plist_get_string_val(version, &version_string);
if (version_string) {
/* parse version */
int maj = 0;
int min = 0;
int rev = 0;
sscanf(version_string, "%d.%d.%d", &maj, &min, &rev);
free(version_string);
*osversion = ((maj & 0xFF) << 24) + ((min & 0xFF) << 16) + ((rev & 0xFF) << 8);
}
}
}
if ((lockdownd_start_service(client, "com.apple.springboardservices", &port) != LOCKDOWN_E_SUCCESS) || !port) {
if (error)
*error = g_error_new(device_domain, EIO, _("Could not start com.apple.springboardservices service! Remind that this feature is only supported in OS 3.1 and later!"));
goto leave_cleanup;
}
if (sbservices_client_new(phone, port, &sbc) != SBSERVICES_E_SUCCESS) {
if (error)
*error = g_error_new(device_domain, EIO, _("Could not connect to springboardservices!"));
goto leave_cleanup;
}
leave_cleanup:
if (client) {
lockdownd_client_free(client);
}
idevice_free(phone);
g_mutex_unlock(idevice_mutex);
return sbc;
}
void device_sbs_free(sbservices_client_t sbc)
{
if (sbc) {
sbservices_client_free(sbc);
}
}
gboolean device_sbs_get_iconstate(sbservices_client_t sbc, plist_t *iconstate, const char *format_version, GError **error)
{
plist_t iconstate_loc = NULL;
gboolean ret = FALSE;
*iconstate = NULL;
if (sbc) {
sbservices_error_t err;
#ifdef HAVE_LIBIMOBILEDEVICE_1_1
err = sbservices_get_icon_state(sbc, &iconstate_loc, format_version);
#else
err = sbservices_get_icon_state(sbc, &iconstate_loc);
#endif
if (err != SBSERVICES_E_SUCCESS || !iconstate_loc) {
if (error)
*error = g_error_new(device_domain, EIO, _("Could not get icon state!"));
goto leave_cleanup;
}
if (plist_get_node_type(iconstate_loc) != PLIST_ARRAY) {
if (error)
*error = g_error_new(device_domain, EIO, _("icon state is not an array as expected!"));
goto leave_cleanup;
}
*iconstate = iconstate_loc;
ret = TRUE;
}
leave_cleanup:
if ((ret == FALSE) && iconstate_loc) {
plist_free(iconstate_loc);
}
return ret;
}
gboolean device_sbs_save_icon(sbservices_client_t sbc, char *display_identifier, char *filename, GError **error)
{
gboolean res = FALSE;
char *png = NULL;
uint64_t pngsize = 0;
if ((sbservices_get_icon_pngdata(sbc, display_identifier, &png, &pngsize) == SBSERVICES_E_SUCCESS) && (pngsize > 0)) {
/* save png icon to disk */
FILE *f = fopen(filename, "w");
fwrite(png, 1, pngsize, f);
fclose(f);
res = TRUE;
} else {
if (error)
*error = g_error_new(device_domain, EIO, _("Could not get icon png data for '%s'"), display_identifier);
}
if (png) {
free(png);
}
return res;
}
gboolean device_sbs_set_iconstate(sbservices_client_t sbc, plist_t iconstate, GError **error)
{
gboolean result = FALSE;
printf("About to upload new iconstate...\n");
if (sbc) {
if (sbservices_set_icon_state(sbc, iconstate) != SBSERVICES_E_SUCCESS) {
if (error)
*error = g_error_new(device_domain, EIO, _("Could not set new icon state!"));
goto leave_cleanup;
}
printf("Successfully uploaded new iconstate\n");
result = TRUE;
}
leave_cleanup:
return result;
}
#ifdef HAVE_LIBIMOBILEDEVICE_1_1
gboolean device_sbs_save_wallpaper(sbservices_client_t sbc, const char *filename, GError **error)
{
gboolean res = FALSE;
char *png = NULL;
uint64_t pngsize = 0;
if ((sbservices_get_home_screen_wallpaper_pngdata(sbc, &png, &pngsize) == SBSERVICES_E_SUCCESS) && (pngsize > 0)) {
/* save png icon to disk */
FILE *f = fopen(filename, "w");
fwrite(png, 1, pngsize, f);
fclose(f);
res = TRUE;
} else {
if (error)
*error = g_error_new(device_domain, EIO, _("Could not get wallpaper png data"));
}
if (png) {
free(png);
}
return res;
}
#endif
device_info_t device_info_new()
{
return g_new0(struct device_info_int, 1);
}
void device_info_free(device_info_t device_info)
{
if (device_info) {
if (device_info->device_name) {
free(device_info->device_name);
}
if (device_info->device_type) {
free(device_info->device_type);
}
free(device_info);
}
}
static guint battery_info_get_current_capacity(plist_t battery_info)
{
uint64_t current_capacity = 0;
plist_t node = NULL;
if (battery_info == NULL)
return (guint)current_capacity;
node = plist_dict_get_item(battery_info, "BatteryCurrentCapacity");
if (node != NULL) {
plist_get_uint_val(node, ¤t_capacity);
}
return (guint)current_capacity;
}
gboolean device_get_info(const char *uuid, device_info_t *device_info, GError **error)
{
uint64_t interval = 60;
plist_t node = NULL;
idevice_t phone = NULL;
lockdownd_client_t client = NULL;
gboolean res = FALSE;
printf("%s: %s\n", __func__, uuid);
if (!device_info) {
return res;
}
printf("%s\n", __func__);
g_mutex_lock(idevice_mutex);
if (IDEVICE_E_SUCCESS != idevice_new(&phone, uuid)) {
*error = g_error_new(device_domain, ENODEV, _("No device found, is it plugged in?"));
goto leave_cleanup;
}
if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "sbmanager")) {
*error = g_error_new(device_domain, EIO, _("Could not connect to lockdownd!"));
goto leave_cleanup;
}
if (!*device_info) {
/* make new device info */
*device_info = device_info_new();
}
if ((*device_info)->device_name) {
free((*device_info)->device_name);
(*device_info)->device_name = NULL;
}
if ((*device_info)->device_type) {
free((*device_info)->device_type);
(*device_info)->device_type = NULL;
}
/* get device name */
lockdownd_get_device_name(client, &((*device_info)->device_name));
/* get device type */
lockdownd_get_value(client, NULL, "ProductType", &node);
if (node) {
char *devtype = NULL;
const char *devtypes[8][2] = {
{"iPhone1,1", "iPhone"},
{"iPhone1,2", "iPhone 3G"},
{"iPhone2,1", "iPhone 3GS"},
{"iPhone3,1", "iPhone 4"},
{"iPad1,1", "iPad"},
{"iPod1,1", "iPod Touch"},
{"iPod2,1", "iPod touch (2G)"},
{"iPod3,1", "iPod Touch (3G)"}
};
plist_get_string_val(node, &devtype);
if (devtype) {
int i;
for (i = 0; i < 6; i++) {
if (g_str_equal(devtypes[i][0], devtype)) {
(*device_info)->device_type = g_strdup(devtypes[i][1]);
break;
}
}
}
plist_free(node);
}
/* get battery poll interval */
node = NULL;
lockdownd_get_value(client, "com.apple.mobile.iTunes", "BatteryPollInterval", &node);
plist_get_uint_val(node, &interval);
(*device_info)->battery_poll_interval = (guint)interval;
plist_free(node);
/* get current battery capacity */
node = NULL;
lockdownd_get_value(client, "com.apple.mobile.battery", NULL, &node);
(*device_info)->battery_capacity = battery_info_get_current_capacity(node);
plist_free(node);
res = TRUE;
leave_cleanup:
if (client) {
lockdownd_client_free(client);
}
idevice_free(phone);
g_mutex_unlock(idevice_mutex);
return res;
}
|