summaryrefslogtreecommitdiffstats
path: root/src/device.c
blob: 0a886e6c9a901468237e643df3a718802e7a1d17 (plain)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/**
 * 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");
}

static gboolean device_connect(const char *uuid, idevice_t *phone, lockdownd_client_t *client, GError **error) {
    gboolean res = FALSE;

    if (!client || !phone) {
        return res;
    }

    if (IDEVICE_E_SUCCESS != idevice_new(phone, uuid)) {
        *error = g_error_new(device_domain, ENODEV, _("No device found, is it plugged in?"));
        return res;
    }

    if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(*phone, client, "sbmanager")) {
        *error = g_error_new(device_domain, EIO, _("Could not connect to lockdownd!"));
        return res;
    }

    res = TRUE;

    return res;
}

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 (!device_connect(uuid, &phone, &client, error)) {
        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 */
        res = g_file_set_contents (filename, png, pngsize, error);
    } 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
char *device_sbs_save_wallpaper(sbservices_client_t sbc, const char *uuid, GError **error)
{
    char *res = NULL;
    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 */
        char *path;
        char *filename;

	path = g_build_filename (g_get_user_cache_dir (),
				 "libimobiledevice",
				 "wallpaper", NULL);
	g_mkdir_with_parents (path, 0755);
	g_free (path);

	filename = g_strdup_printf ("%s.png", uuid);
	path = g_build_filename (g_get_user_cache_dir (),
				 "libimobiledevice",
				 "wallpaper",
				 filename, NULL);
        g_free (filename);

        if (g_file_set_contents (path, png, pngsize, error) == FALSE) {
          g_free (filename);
          free (png);
          return NULL;
	}
	res = path;
    } 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()
{
    device_info_t device_info = g_new0(struct device_info_int, 1);

    /* initialize default values */
    device_info->home_screen_icon_columns = 4;
    device_info->home_screen_icon_dock_max_count = 4;
    device_info->home_screen_icon_height = 57;
    device_info->home_screen_icon_rows = 4;
    device_info->home_screen_icon_width = 57;
    device_info->icon_folder_columns = 4;
    device_info->icon_folder_max_pages = 1;
    device_info->icon_folder_rows = 3;
    device_info->icon_state_saves = 1;

    return device_info;
}

void device_info_free(device_info_t device_info)
{
    if (device_info) {
        if (device_info->uuid) {
            free(device_info->uuid);
        }
        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, &current_capacity);
    }
    return (guint)current_capacity;
}

gboolean device_poll_battery_capacity(const char *uuid, device_info_t *device_info, GError **error) {
    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 (!device_connect(uuid, &phone, &client, error)) {
        goto leave_cleanup;
    }

    if (!*device_info) {
        /* make new device info */
        *device_info = device_info_new();
    }

    /* 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;
}

static void device_dump_info(device_info_t info) {
    printf("%s: Device Information\n", __func__);

    printf("%s: UUID: %s\n", __func__, info->uuid);
    printf("%s: Name: %s\n", __func__, info->device_name);
    printf("%s: Type: %s\n", __func__, info->device_type);

    printf("%s: Battery\n", __func__);
    printf("%s: PollInterval: %d\n", __func__, info->battery_poll_interval);
    printf("%s: CurrentCapacity: %d\n", __func__, info->battery_capacity);

    printf("%s: HomeScreen Settings\n", __func__);

    printf("%s: IconColumns: %d\n", __func__, info->home_screen_icon_columns);
    printf("%s: IconRows: %d\n", __func__, info->home_screen_icon_rows);
    printf("%s: IconDockMaxCount: %d\n", __func__, info->home_screen_icon_dock_max_count);
    printf("%s: IconWidth: %d\n", __func__, info->home_screen_icon_width);
    printf("%s: IconHeight: %d\n", __func__, info->home_screen_icon_height);

    printf("%s: IconFolder Settings\n", __func__);
    printf("%s: IconWidth: %d\n", __func__, info->home_screen_icon_width);
    printf("%s: IconHeight: %d\n", __func__, info->home_screen_icon_height);
}

gboolean device_get_info(const char *uuid, device_info_t *device_info, GError **error)
{
    uint8_t boolean = FALSE;
    uint64_t integer = 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 (!device_connect(uuid, &phone, &client, error)) {
        goto leave_cleanup;
    }

    if (!*device_info) {
        /* make new device info */
        *device_info = device_info_new();
    }

    (*device_info)->uuid = strdup(uuid);

    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[18][2] = {
            {"iPhone1,1", "iPhone"},
            {"iPhone1,2", "iPhone 3G"},
            {"iPhone2,1", "iPhone 3GS"},
            {"iPhone3,1", "iPhone 4"},
            {"iPhone3,3", "iPhone 4 CDMA"},
            {"iPhone4,1", "iPhone 4S"},
            {"iPad1,1", "iPad"},
            {"iPad2,1", "iPad 2"},
            {"iPad2,2", "iPad 2 3G GSM"},
            {"iPad2,3", "iPad 2 3G CDMA"},
            {"iPad2,4", "iPad"},
            {"iPad3,1", "iPad (3rd Gen)"},
            {"iPad3,2", "iPad 4G LTE (3rd Gen)"},
            {"iPad3,3", "iPad 4G LTE (3rd Gen)"},
            {"iPod1,1", "iPod Touch"},
            {"iPod2,1", "iPod Touch (2G)"},
            {"iPod3,1", "iPod Touch (3G)"},
            {"iPod4,1", "iPod Touch (4G)"}
        };
        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 layout information */
    node = NULL;
    plist_t value = NULL;
    lockdownd_get_value(client, "com.apple.mobile.iTunes", NULL, &node);

    value = plist_dict_get_item(node, "HomeScreenIconColumns");
    plist_get_uint_val(value, &integer);
    (*device_info)->home_screen_icon_columns = (guint)integer;

    value = plist_dict_get_item(node, "HomeScreenIconDockMaxCount");
    plist_get_uint_val(value, &integer);
    (*device_info)->home_screen_icon_dock_max_count = (guint)integer;

    value = plist_dict_get_item(node, "HomeScreenIconHeight");
    plist_get_uint_val(value, &integer);
    (*device_info)->home_screen_icon_height = (guint)integer;

    value = plist_dict_get_item(node, "HomeScreenIconRows");
    plist_get_uint_val(value, &integer);
    (*device_info)->home_screen_icon_rows = (guint)integer;

    value = plist_dict_get_item(node, "HomeScreenIconWidth");
    plist_get_uint_val(value, &integer);
    (*device_info)->home_screen_icon_width = (guint)integer;

    value = plist_dict_get_item(node, "IconFolderColumns");
    plist_get_uint_val(value, &integer);
    (*device_info)->icon_folder_columns = (guint)integer;

    value = plist_dict_get_item(node, "IconFolderMaxPages");
    plist_get_uint_val(value, &integer);
    (*device_info)->icon_folder_max_pages = (guint)integer;

    value = plist_dict_get_item(node, "IconFolderRows");
    plist_get_uint_val(value, &integer);
    (*device_info)->icon_folder_rows = (guint)integer;

    value = plist_dict_get_item(node, "IconStateSaves");
    plist_get_bool_val(value, &boolean);
    (*device_info)->icon_state_saves = (gboolean)boolean;

    /* get battery poll interval */
    value = plist_dict_get_item(node, "BatteryPollInterval");
    plist_get_uint_val(value, &integer);
    (*device_info)->battery_poll_interval = (guint)integer;

    plist_free(node);
    value = NULL;

    /* 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;

    device_dump_info((*device_info));

  leave_cleanup:
    if (client) {
        lockdownd_client_free(client);
    }
    idevice_free(phone);
    g_mutex_unlock(idevice_mutex);

    return res;
}