summaryrefslogtreecommitdiffstats
path: root/src/iphone.c
blob: 4a5484877f5ab65193845ea28c43590e91f3800c (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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/* 
 * iphone.c
 * Device discovery and communication interface.
 *
 * Copyright (c) 2008 Zach C. All Rights Reserved.
 * Copyright (c) 2009 Nikias Bassen. 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 <stdlib.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>

#include <usbmuxd.h>
#include "iphone.h"
#include "utils.h"

static iphone_event_cb_t event_cb = NULL;

static void usbmux_event_cb(const usbmuxd_event_t *event, void *user_data)
{
	iphone_event_t ev;

	ev.event = event->event;
	ev.uuid = event->device.uuid;
	ev.conn_type = CONNECTION_USBMUXD;

	if (event_cb) {
		event_cb(&ev, user_data);
	}
}

/**
 * Register a callback function that will be called when device add/remove
 * events occur.
 *
 * @param callback Callback function to call.
 * @param user_data Application-specific data passed as parameter
 *   to the registered callback function.
 *
 * @return IPHONE_E_SUCCESS on success or an error value when an error occured.
 */
iphone_error_t iphone_event_subscribe(iphone_event_cb_t callback, void *user_data)
{
	event_cb = callback;
	int res = usbmuxd_subscribe(usbmux_event_cb, user_data);
        if (res != 0) {
		event_cb = NULL;
		log_debug_msg("%s: Error %d when subscribing usbmux event callback!\n", __func__, res);
		return IPHONE_E_UNKNOWN_ERROR;
	}
	return IPHONE_E_SUCCESS;
}

/**
 * Release the event callback function that has been registered with
 *  iphone_event_subscribe().
 *
 * @return IPHONE_E_SUCCESS on success or an error value when an error occured.
 */
iphone_error_t iphone_event_unsubscribe()
{
	event_cb = NULL;
	int res = usbmuxd_unsubscribe();
	if (res != 0) {
		log_debug_msg("%s: Error %d when unsubscribing usbmux event callback!\n", __func__, res);
		return IPHONE_E_UNKNOWN_ERROR;
	}
	return IPHONE_E_SUCCESS;
}

/**
 * Get a list of currently available devices.
 *
 * @param devices List of uuids of devices that are currently available.
 *   This list is terminated by a NULL pointer.
 * @param count Number of devices found.
 *
 * @return IPHONE_E_SUCCESS on success or an error value when an error occured.
 */
iphone_error_t iphone_get_device_list(char ***devices, int *count)
{
	usbmuxd_device_info_t *dev_list;

	*devices = NULL;
	*count = 0;

	if (usbmuxd_get_device_list(&dev_list) < 0) {
		log_debug_msg("%s: ERROR: usbmuxd is not running!\n", __func__);
		return IPHONE_E_NO_DEVICE;
	}

	char **newlist = NULL;
	int i, newcount = 0;

	for (i = 0; dev_list[i].handle > 0; i++) {
		newlist = realloc(*devices, sizeof(char*) * (newcount+1));
		newlist[newcount++] = strdup(dev_list[i].uuid);
		*devices = newlist;
	}
	usbmuxd_device_list_free(&dev_list);

	*count = newcount;
	newlist = realloc(*devices, sizeof(char*) * (newcount+1));
	newlist[newcount] = NULL;
	*devices = newlist;

	return IPHONE_E_SUCCESS;
}

/**
 * Free a list of device uuids.
 *
 * @param devices List of uuids to free.
 *
 * @return Always returnes IPHONE_E_SUCCESS.
 */
iphone_error_t iphone_device_list_free(char **devices)
{
	if (devices) {
		int i = 0;
		while (devices[i++]) {
			free(devices[i]);
		}
		free(devices);
	}
	return IPHONE_E_SUCCESS;
}

/**
 * Creates an iphone_device_t structure for the device specified by uuid,
 *  if the device is available.
 *
 * @note The resulting iphone_device_t structure has to be freed with
 * iphone_device_free() if it is no longer used.
 *
 * @param device Upon calling this function, a pointer to a location of type
 *  iphone_device_t. On successful return, this location will be populated.
 * @param uuid The UUID to match.
 *
 * @return IPHONE_E_SUCCESS if ok, otherwise an error code.
 */
iphone_error_t iphone_device_new(iphone_device_t * device, const char *uuid)
{
	usbmuxd_device_info_t muxdev;
	int res = usbmuxd_get_device_by_uuid(uuid, &muxdev);
	if (res > 0) {
		iphone_device_t phone = (iphone_device_t) malloc(sizeof(struct iphone_device_int));
		phone->uuid = strdup(muxdev.uuid);
		phone->conn_type = CONNECTION_USBMUXD;
		phone->conn_data = (void*)muxdev.handle;
		*device = phone;
		return IPHONE_E_SUCCESS;
	}
	/* other connection types could follow here */

	return IPHONE_E_NO_DEVICE;
}

/** 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 device A pointer to an iPhone structure.
 */
iphone_error_t iphone_device_free(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->uuid);

	if (device->conn_type == CONNECTION_USBMUXD) {
		device->conn_data = 0;
	}
	if (device->conn_data) {
		free(device->conn_data);
	}
	free(device);
	return ret;
}

/**
 * Set up a connection to the given device.
 *
 * @param device The device to connect to.
 * @param dst_port The destination port to connect to.
 * @param connection Pointer to an iphone_connection_t that will be filled
 *   with the necessary data of the connection.
 *
 * @return IPHONE_E_SUCCESS if ok, otherwise an error code.
 */
iphone_error_t iphone_device_connect(iphone_device_t device, uint16_t dst_port, iphone_connection_t *connection)
{
	if (!device) {
		return IPHONE_E_INVALID_ARG;
	}

	if (device->conn_type == CONNECTION_USBMUXD) {
		int sfd = usbmuxd_connect((uint32_t)(device->conn_data), dst_port);
		if (sfd < 0) {
			log_debug_msg("%s: ERROR: Connecting to usbmuxd failed: %d (%s)\n", __func__, sfd, strerror(-sfd));
			return IPHONE_E_UNKNOWN_ERROR;
		}
		iphone_connection_t new_connection = (iphone_connection_t)malloc(sizeof(struct iphone_connection_int));
		new_connection->type = CONNECTION_USBMUXD;
		new_connection->data = (void*)sfd;
		*connection = new_connection;
		return IPHONE_E_SUCCESS;
	} else {
		log_debug_msg("%s: Unknown connection type %d\n", __func__, device->conn_type);
	}

	return IPHONE_E_UNKNOWN_ERROR;
}

/**
 * Disconnect from the device and clean up the connection structure.
 *
 * @param connection The connection to close.
 *
 * @return IPHONE_E_SUCCESS if ok, otherwise an error code.
 */
iphone_error_t iphone_device_disconnect(iphone_connection_t connection)
{
	if (!connection) {
		return IPHONE_E_INVALID_ARG;
	}
	iphone_error_t result = IPHONE_E_UNKNOWN_ERROR;
	if (connection->type == CONNECTION_USBMUXD) {
		usbmuxd_disconnect((int)(connection->data));
		result = IPHONE_E_SUCCESS;
	} else {
		log_debug_msg("%s: Unknown connection type %d\n", __func__, connection->type);
	}
	free(connection);
	return result;
}

/**
 * Send data to a device via the given connection.
 *
 * @param connection The connection to send data over.
 * @param data Buffer with data to send.
 * @param len Size of the buffer to send.
 * @param sent_bytes Pointer to an uint32_t that will be filled
 *   with the number of bytes actually sent.
 *
 * @return IPHONE_E_SUCCESS if ok, otherwise an error code.
 */
iphone_error_t iphone_device_send(iphone_connection_t connection, const char *data, uint32_t len, uint32_t *sent_bytes)
{
	if (!connection || !data) {
		return IPHONE_E_INVALID_ARG;
	}

	if (connection->type == CONNECTION_USBMUXD) {
		int res = usbmuxd_send((int)(connection->data), data, len, sent_bytes);
		if (res < 0) {
			log_debug_msg("%s: ERROR: usbmuxd_send returned %d (%s)\n", __func__, res, strerror(-res));
			return IPHONE_E_UNKNOWN_ERROR;
		}
		return IPHONE_E_SUCCESS;
	} else {
		log_debug_msg("%s: Unknown connection type %d\n", __func__, connection->type);
	}
	return IPHONE_E_UNKNOWN_ERROR;
}

/**
 * Receive data from a device via the given connection.
 * This function will return after the given timeout even if no data has been
 * received.
 *
 * @param connection The connection to receive data from.
 * @param data Buffer that will be filled with the received data.
 *   This buffer has to be large enough to hold len bytes.
 * @param len Buffer size or number of bytes to receive.
 * @param recv_bytes Number of bytes actually received.
 * @param timeout Timeout in milliseconds after which this function should
 *   return even if no data has been received.
 *
 * @return IPHONE_E_SUCCESS if ok, otherwise an error code.
 */
iphone_error_t iphone_device_recv_timeout(iphone_connection_t connection, char *data, uint32_t len, uint32_t *recv_bytes, unsigned int timeout)
{
	if (!connection) {
		return IPHONE_E_INVALID_ARG;
	}

	if (connection->type == CONNECTION_USBMUXD) {
		int res = usbmuxd_recv_timeout((int)(connection->data), data, len, recv_bytes, timeout);
		if (res < 0) {
			log_debug_msg("%s: ERROR: usbmuxd_recv_timeout returned %d (%s)\n", __func__, res, strerror(-res));
			return IPHONE_E_UNKNOWN_ERROR;
		}
		return IPHONE_E_SUCCESS;
	} else {
		log_debug_msg("%s: Unknown connection type %d\n", __func__, connection->type);
	}
	return IPHONE_E_UNKNOWN_ERROR;
}

/**
 * Receive data from a device via the given connection.
 * This function is like iphone_device_recv_timeout, but with a predefined
 *  reasonable timeout.
 *
 * @param connection The connection to receive data from.
 * @param data Buffer that will be filled with the received data.
 *   This buffer has to be large enough to hold len bytes.
 * @param len Buffer size or number of bytes to receive.
 * @param recv_bytes Number of bytes actually received.
 *
 * @return IPHONE_E_SUCCESS if ok, otherwise an error code.
 */
iphone_error_t iphone_device_recv(iphone_connection_t connection, char *data, uint32_t len, uint32_t *recv_bytes)
{
	if (!connection) {
		return -EINVAL;
	}

	if (connection->type == CONNECTION_USBMUXD) {
		int res = usbmuxd_recv((int)(connection->data), data, len, recv_bytes);
		if (res < 0) {
			log_debug_msg("%s: ERROR: usbmuxd_recv returned %d (%s)\n", __func__, res, strerror(-res));
			return IPHONE_E_UNKNOWN_ERROR;
		}

		return IPHONE_E_SUCCESS;
	} else {
		log_debug_msg("%s: Unknown connection type %d\n", __func__, connection->type);
	}
	return IPHONE_E_UNKNOWN_ERROR;
}

/**
 * Sends a plist over the given connection.
 * Internally used generic plist send function.
 *
 * @param connection The connection to use for sending.
 *        Can be NULL if ssl_session is non-NULL.
 * @param plist plist to send
 * @param binary 1 = send binary plist, 0 = send xml plist
 * @param ssl_session If set to NULL, the communication will be unencrypted.
 *        For encrypted communication, pass a valid and properly initialized
 *        gnutls_session_t. connection is ignored when ssl_session is non-NULL.
 *
 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when one or more
 *      parameters are invalid, IPHONE_E_PLIST_ERROR when dict is not a valid
 *      plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs.
 */
static iphone_error_t internal_plist_send(iphone_connection_t connection, plist_t plist, int binary, gnutls_session_t ssl_session)
{
	iphone_error_t res = IPHONE_E_UNKNOWN_ERROR;
	char *content = NULL;
	uint32_t length = 0;
	uint32_t nlen = 0;
	int bytes = 0;

	if ((!connection && !ssl_session) || !plist) {
		return IPHONE_E_INVALID_ARG;
	}

	if (binary) {
		plist_to_bin(plist, &content, &length);
	} else {
		plist_to_xml(plist, &content, &length);
	}

	if (!content || length == 0) {
		return IPHONE_E_PLIST_ERROR;
	}

	nlen = htonl(length);
	log_debug_msg("%s: sending %d bytes\n", __func__, length);
	if (ssl_session) {
		bytes = gnutls_record_send(ssl_session, (const char*)&nlen, sizeof(nlen));
	} else {
		iphone_device_send(connection, (const char*)&nlen, sizeof(nlen), (uint32_t*)&bytes);
	}
	if (bytes == sizeof(nlen)) {
		if (ssl_session) {
			bytes = gnutls_record_send(ssl_session, content, length);
		} else {
			iphone_device_send(connection, content, length, (uint32_t*)&bytes);
		}
		if (bytes > 0) {
			log_debug_msg("%s: sent %d bytes\n", __func__, bytes);
			log_debug_buffer(content, bytes);
			if ((uint32_t)bytes == length) {
				res = IPHONE_E_SUCCESS;
			} else {
				log_debug_msg("%s: ERROR: Could not send all data (%d of %d)!\n", __func__, bytes, length);
			}
		}
	}
	if (bytes <= 0) {
		log_debug_msg("%s: ERROR: sending to device failed.\n", __func__);
	}

	free(content);

	return res;
}

/**
 * Sends an XML plist over the given connection.
 *
 * @param connection The connection to send data over
 * @param plist plist to send
 *
 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection
 *      or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist,
 *      or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs.
 */
iphone_error_t iphone_device_send_xml_plist(iphone_connection_t connection, plist_t plist)
{
	return internal_plist_send(connection, plist, 0, NULL);
}

/**
 * Sends a binary plist over the given connection.
 *
 * @param connection The connection to send data over
 * @param plist plist to send
 *
 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection
 *      or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist,
 *      or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs.
 */
iphone_error_t iphone_device_send_binary_plist(iphone_connection_t connection, plist_t plist)
{
	return internal_plist_send(connection, plist, 1, NULL);
}

/**
 * Sends an encrypted XML plist.
 *
 * @param ssl_session Valid and properly initialized gnutls_session_t.
 * @param plist plist to send
 *
 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session
 *      or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist,
 *      or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs.
 */
iphone_error_t iphone_device_send_encrypted_xml_plist(gnutls_session_t ssl_session, plist_t plist)
{
	return internal_plist_send(NULL, plist, 0, ssl_session);
}

/**
 * Sends an encrypted binary plist.
 *
 * @param ssl_session Valid and properly initialized gnutls_session_t.
 * @param plist plist to send
 *
 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session
 *      or plist is NULL, IPHONE_E_PLIST_ERROR when dict is not a valid plist,
 *      or IPHONE_E_UNKNOWN_ERROR when an unspecified error occurs.
 */
iphone_error_t iphone_device_send_encrypted_binary_plist(gnutls_session_t ssl_session, plist_t plist)
{
	return internal_plist_send(NULL, plist, 1, ssl_session);
}

/**
 * Receives a plist over the given connection. 
 * Internally used generic plist send function.
 *
 * @param connection The connection to receive data on
 * @param plist pointer to a plist_t that will point to the received plist
 *              upon successful return
 * @param timeout Maximum time in milliseconds to wait for data.
 * @param ssl_session If set to NULL, the communication will be unencrypted.
 *        For encrypted communication, pass a valid and properly initialized
 *        gnutls_session_t.
 *
 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection
 *    or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be
 *    converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified
 *    error occurs.
 */
static iphone_error_t internal_plist_recv_timeout(iphone_connection_t connection, plist_t *plist, unsigned int timeout, gnutls_session_t ssl_session)
{
	iphone_error_t res = IPHONE_E_UNKNOWN_ERROR;
	uint32_t pktlen = 0;
	uint32_t bytes = 0;

	if ((!connection && !ssl_session) || !plist) {
		return IPHONE_E_INVALID_ARG;
	}

	if (ssl_session) {
		bytes = gnutls_record_recv(ssl_session, (char*)&pktlen, sizeof(pktlen));
	} else {
		iphone_device_recv_timeout(connection, (char*)&pktlen, sizeof(pktlen), &bytes, timeout);
	}
	log_debug_msg("%s: initial read=%i\n", __func__, bytes);
	if (bytes < 4) {
		log_debug_msg("%s: initial read failed!\n", __func__);
		return IPHONE_E_NOT_ENOUGH_DATA;
	} else {
		if ((char)pktlen == 0) { /* prevent huge buffers */
			uint32_t curlen = 0;
			char *content = NULL;
			pktlen = ntohl(pktlen);
			log_debug_msg("%s: %d bytes following\n", __func__, pktlen);
			content = (char*)malloc(pktlen);

			while (curlen < pktlen) {
				if (ssl_session) {
					bytes = gnutls_record_recv(ssl_session, content+curlen, pktlen-curlen);
				} else {
					iphone_device_recv(connection, content+curlen, pktlen-curlen, &bytes);
				}
				if (bytes <= 0) {
					res = IPHONE_E_UNKNOWN_ERROR;
					break;
				}
				log_debug_msg("%s: received %d bytes\n", __func__, bytes);
				curlen += bytes;
			}
			log_debug_buffer(content, pktlen);
			if (!memcmp(content, "bplist00", 8)) {
				plist_from_bin(content, pktlen, plist);
			} else {
				plist_from_xml(content, pktlen, plist);
			}
			if (*plist) {
				res = IPHONE_E_SUCCESS;
			} else {
				res = IPHONE_E_PLIST_ERROR;
			}
			free(content);
			content = NULL;
		} else {
			res = IPHONE_E_UNKNOWN_ERROR;
		}
	}
	return res;
}

/**
 * Receives a plist over the given connection with specified timeout.
 * Binary or XML plists are automatically handled.
 *
 * @param connection The connection to receive data on
 * @param plist pointer to a plist_t that will point to the received plist
 *              upon successful return
 * @param timeout Maximum time in milliseconds to wait for data.
 *
 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection
 *    or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be
 *    converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified
 *    error occurs.
 */
iphone_error_t iphone_device_receive_plist_with_timeout(iphone_connection_t connection, plist_t *plist, unsigned int timeout)
{
	return internal_plist_recv_timeout(connection, plist, timeout, NULL);
}

/**
 * Receives a plist over the given connection.
 * Binary or XML plists are automatically handled.
 *
 * This function is like iphone_device_receive_plist_with_timeout
 *   using a timeout of 10 seconds.
 * @see iphone_device_receive_plist_with_timeout
 *
 * @param connection The connection to receive data on
 * @param plist pointer to a plist_t that will point to the received plist
 *              upon successful return
 *
 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when connection
 *    or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be
 *    converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified
 *    error occurs.
 */
iphone_error_t iphone_device_receive_plist(iphone_connection_t connection, plist_t *plist)
{
	return internal_plist_recv_timeout(connection, plist, 10000, NULL);
}

/**
 * Receives an encrypted plist with specified timeout.
 * Binary or XML plists are automatically handled.
 *
 * @param ssl_session Valid and properly initialized gnutls_session_t.
 * @param plist pointer to a plist_t that will point to the received plist
 *              upon successful return
 * @param timeout Maximum time in milliseconds to wait for data.
 *
 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session
 *    or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be
 *    converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified
 *    error occurs.
 */
iphone_error_t iphone_device_receive_encrypted_plist_with_timeout(gnutls_session_t ssl_session, plist_t *plist, unsigned int timeout)
{
	return internal_plist_recv_timeout(NULL, plist, timeout, ssl_session);
}

/**
 * Receives an encrypted plist.
 * Binary or XML plists are automatically handled.
 * This function is like iphone_device_receive_encrypted_plist_with_timeout
 *   with a timeout value of 10 seconds.
 *
 * @param ssl_session Valid and properly initialized gnutls_session_t.
 * @param connection The connection to receive data on
 * @param plist pointer to a plist_t that will point to the received plist
 *              upon successful return
 *
 * @return IPHONE_E_SUCCESS on success, IPHONE_E_INVALID_ARG when ssl_session
 *    or *plist is NULL, IPHONE_E_PLIST_ERROR when the received data cannot be
 *    converted to a plist, or IPHONE_E_UNKNOWN_ERROR when an unspecified
 *    error occurs.
 */
iphone_error_t iphone_device_receive_encrypted_plist(gnutls_session_t ssl_session, plist_t *plist)
{
	return internal_plist_recv_timeout(NULL, plist, 10000, ssl_session);
}

iphone_error_t iphone_device_get_handle(iphone_device_t device, uint32_t *handle)
{
	if (!device)
		return IPHONE_E_INVALID_ARG;

	if (device->conn_type == CONNECTION_USBMUXD) {
		*handle = (uint32_t)device->conn_data;
		return IPHONE_E_SUCCESS;
	} else {
		log_debug_msg("%s: Unknown connection type %d\n", __func__, device->conn_type);
	}
	return IPHONE_E_UNKNOWN_ERROR;
}

iphone_error_t iphone_device_get_uuid(iphone_device_t device, char **uuid)
{
	if (!device)
		return IPHONE_E_INVALID_ARG;

	*uuid = strdup(device->uuid);
	return IPHONE_E_SUCCESS;
}