summaryrefslogtreecommitdiffstats
path: root/src/iphone.c
blob: 0e179e7f29678c7e8f435299ba79e9295dfaef9b (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
/* 
 * iphone.c
 * Functions for creating and initializing iPhone structures.
 *
 * Copyright (c) 2008 Zach C. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 */

#include "iphone.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libiphone/libiphone.h>

/**
 * Retrieves a list of connected devices from usbmuxd and matches their
 * UUID with the given UUID. If the given UUID is NULL then the first
 * device reported by usbmuxd is used.
 *
 * @param device Upon calling this function, a pointer to a location of type
 *  iphone_device_t, which must have the value NULL. On return, this location
 *  will be filled with a handle to the device.
 * @param uuid The UUID to match.
 *
 * @return IPHONE_E_SUCCESS if ok, otherwise an error code.
 */
iphone_error_t iphone_get_device_by_uuid(iphone_device_t * device, const char *uuid)
{
	iphone_device_t phone;
	uint32_t handle = 0;
	char *serial_number = malloc(41);
	usbmuxd_scan_result *dev_list = NULL;
	int i;

	if (usbmuxd_scan(&dev_list) < 0) {
		log_debug_msg("%s: usbmuxd_scan returned an error, is usbmuxd running?\n", __func__);
	}
	if (dev_list && dev_list[0].handle > 0) {
		if (!uuid) {
			// select first device found if no UUID specified
			handle = dev_list[0].handle;
			strcpy(serial_number, dev_list[0].serial_number);
		} else {
			// otherwise walk through the list
			for (i = 0; dev_list[i].handle > 0; i++) {
				log_debug_msg("%s: device handle=%d, uuid=%s\n", __func__, dev_list[i].handle, dev_list[i].serial_number);
				if (strcasecmp(uuid, dev_list[i].serial_number) == 0) {
					handle = dev_list[i].handle;
					strcpy(serial_number, dev_list[i].serial_number);
					break;
				}
			}
		}
		free(dev_list);

		if (handle > 0) {
			phone = (iphone_device_t) malloc(sizeof(struct iphone_device_int));
			phone->handle = handle;
			phone->serial_number = serial_number;
			*device = phone;
			return IPHONE_E_SUCCESS;
		}
	}

	return IPHONE_E_NO_DEVICE;
}

/**
 * This function has the purpose to retrieve a handle to the first
 *  attached iPhone/iPod reported by usbmuxd.
 *
 * @param Upon calling this function, a pointer to a location of type
 *  iphone_device_t, which must have the value NULL. On return, this location
 *  will be filled with a handle to the device.
 *
 * @return IPHONE_E_SUCCESS if ok, otherwise an error code.
 */
iphone_error_t iphone_get_device(iphone_device_t * device)
{
	return iphone_get_device_by_uuid(device, NULL);
}

uint32_t iphone_get_device_handle(iphone_device_t device)
{
	if (device) {
		return device->handle;
	} else {
		return 0;
	}
}

char* iphone_get_uuid(iphone_device_t device)
{
	if (device) {
		return device->serial_number;
	} else {
		return NULL;
	}
}

/** Cleans up an iPhone structure, then frees the structure itself.  
 * This is a library-level function; deals directly with the iPhone to tear
 *  down relations, but otherwise is mostly internal.
 * 
 * @param phone A pointer to an iPhone structure.
 */
iphone_error_t iphone_free_device(iphone_device_t device)
{
	if (!device)
		return IPHONE_E_INVALID_ARG;
	iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;

	ret = IPHONE_E_SUCCESS;

	free(device->serial_number);
	free(device);
	return ret;
}