summaryrefslogtreecommitdiffstats
path: root/src/device_link_service.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/device_link_service.c')
-rw-r--r--src/device_link_service.c129
1 files changed, 74 insertions, 55 deletions
diff --git a/src/device_link_service.c b/src/device_link_service.c
index 6083d80..66c2461 100644
--- a/src/device_link_service.c
+++ b/src/device_link_service.c
@@ -1,28 +1,53 @@
1 /* 1/*
2 * device_link_service.c 2 * device_link_service.c
3 * DeviceLink service implementation. 3 * DeviceLink service implementation.
4 * 4 *
5 * Copyright (c) 2010 Nikias Bassen, All Rights Reserved. 5 * Copyright (c) 2010-2019 Nikias Bassen, All Rights Reserved.
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public 8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version. 10 * version 2.1 of the License, or (at your option) any later version.
11 * 11 *
12 * This library is distributed in the hope that it will be useful, 12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details. 15 * Lesser General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU Lesser General Public 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 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 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
21#include <string.h> 25#include <string.h>
22#include <stdlib.h> 26#include <stdlib.h>
23#include "device_link_service.h" 27#include "device_link_service.h"
24#include "property_list_service.h" 28#include "property_list_service.h"
25#include "debug.h" 29#include "common/debug.h"
30
31static device_link_service_error_t device_link_error(property_list_service_error_t err)
32{
33 switch (err) {
34 case PROPERTY_LIST_SERVICE_E_SUCCESS:
35 return DEVICE_LINK_SERVICE_E_SUCCESS;
36 case PROPERTY_LIST_SERVICE_E_INVALID_ARG:
37 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
38 case PROPERTY_LIST_SERVICE_E_PLIST_ERROR:
39 return DEVICE_LINK_SERVICE_E_PLIST_ERROR;
40 case PROPERTY_LIST_SERVICE_E_MUX_ERROR:
41 return DEVICE_LINK_SERVICE_E_MUX_ERROR;
42 case PROPERTY_LIST_SERVICE_E_SSL_ERROR:
43 return DEVICE_LINK_SERVICE_E_SSL_ERROR;
44 case PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT:
45 return DEVICE_LINK_SERVICE_E_RECEIVE_TIMEOUT;
46 default:
47 break;
48 }
49 return DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR;
50}
26 51
27/** 52/**
28 * Internally used function to extract the message string from a DL* message 53 * Internally used function to extract the message string from a DL* message
@@ -58,7 +83,7 @@ static int device_link_service_get_message(plist_t dl_msg, char **message)
58 return 0; 83 return 0;
59 } 84 }
60 85
61 if ((strlen(cmd_str) < 9) || (strncmp(cmd_str, "DL", 2))) { 86 if ((strlen(cmd_str) < 9) || (strncmp(cmd_str, "DL", 2) != 0)) {
62 free(cmd_str); 87 free(cmd_str);
63 return 0; 88 return 0;
64 } 89 }
@@ -74,7 +99,7 @@ static int device_link_service_get_message(plist_t dl_msg, char **message)
74 * Creates a new device link service client. 99 * Creates a new device link service client.
75 * 100 *
76 * @param device The device to connect to. 101 * @param device The device to connect to.
77 * @param port Port on device to connect to. 102 * @param service The service descriptor returned by lockdownd_start_service.
78 * @param client Reference that will point to a newly allocated 103 * @param client Reference that will point to a newly allocated
79 * device_link_service_client_t upon successful return. 104 * device_link_service_client_t upon successful return.
80 * 105 *
@@ -82,15 +107,16 @@ static int device_link_service_get_message(plist_t dl_msg, char **message)
82 * DEVICE_LINK_SERVICE_E_INVALID_ARG when one of the parameters is invalid, 107 * DEVICE_LINK_SERVICE_E_INVALID_ARG when one of the parameters is invalid,
83 * or DEVICE_LINK_SERVICE_E_MUX_ERROR when the connection failed. 108 * or DEVICE_LINK_SERVICE_E_MUX_ERROR when the connection failed.
84 */ 109 */
85device_link_service_error_t device_link_service_client_new(idevice_t device, uint16_t port, device_link_service_client_t *client) 110device_link_service_error_t device_link_service_client_new(idevice_t device, lockdownd_service_descriptor_t service, device_link_service_client_t *client)
86{ 111{
87 if (!device || port == 0 || !client || *client) { 112 if (!device || !service || service->port == 0 || !client || *client) {
88 return DEVICE_LINK_SERVICE_E_INVALID_ARG; 113 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
89 } 114 }
90 115
91 property_list_service_client_t plistclient = NULL; 116 property_list_service_client_t plistclient = NULL;
92 if (property_list_service_client_new(device, port, &plistclient) != PROPERTY_LIST_SERVICE_E_SUCCESS) { 117 device_link_service_error_t err = device_link_error(property_list_service_client_new(device, service, &plistclient));
93 return DEVICE_LINK_SERVICE_E_MUX_ERROR; 118 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
119 return err;
94 } 120 }
95 121
96 /* create client object */ 122 /* create client object */
@@ -117,11 +143,10 @@ device_link_service_error_t device_link_service_client_free(device_link_service_
117 if (!client) 143 if (!client)
118 return DEVICE_LINK_SERVICE_E_INVALID_ARG; 144 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
119 145
120 if (property_list_service_client_free(client->parent) != PROPERTY_LIST_SERVICE_E_SUCCESS) { 146 device_link_service_error_t err = device_link_error(property_list_service_client_free(client->parent));
121 return DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR;
122 }
123 free(client); 147 free(client);
124 return DEVICE_LINK_SERVICE_E_SUCCESS; 148
149 return err;
125} 150}
126 151
127/** 152/**
@@ -145,7 +170,7 @@ device_link_service_error_t device_link_service_version_exchange(device_link_ser
145{ 170{
146 if (!client) 171 if (!client)
147 return DEVICE_LINK_SERVICE_E_INVALID_ARG; 172 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
148 173
149 device_link_service_error_t err = DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR; 174 device_link_service_error_t err = DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR;
150 175
151 /* perform version exchange */ 176 /* perform version exchange */
@@ -153,13 +178,13 @@ device_link_service_error_t device_link_service_version_exchange(device_link_ser
153 char *msg = NULL; 178 char *msg = NULL;
154 179
155 /* receive DLMessageVersionExchange from device */ 180 /* receive DLMessageVersionExchange from device */
156 if (property_list_service_receive_plist(client->parent, &array) != PROPERTY_LIST_SERVICE_E_SUCCESS) { 181 err = device_link_error(property_list_service_receive_plist(client->parent, &array));
182 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
157 debug_info("Did not receive initial message from device!"); 183 debug_info("Did not receive initial message from device!");
158 err = DEVICE_LINK_SERVICE_E_MUX_ERROR;
159 goto leave; 184 goto leave;
160 } 185 }
161 device_link_service_get_message(array, &msg); 186 device_link_service_get_message(array, &msg);
162 if (!msg || strcmp(msg, "DLMessageVersionExchange")) { 187 if (!msg || strcmp(msg, "DLMessageVersionExchange") != 0) {
163 debug_info("Did not receive DLMessageVersionExchange from device!"); 188 debug_info("Did not receive DLMessageVersionExchange from device!");
164 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR; 189 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR;
165 goto leave; 190 goto leave;
@@ -199,22 +224,22 @@ device_link_service_error_t device_link_service_version_exchange(device_link_ser
199 plist_array_append_item(array, plist_new_string("DLMessageVersionExchange")); 224 plist_array_append_item(array, plist_new_string("DLMessageVersionExchange"));
200 plist_array_append_item(array, plist_new_string("DLVersionsOk")); 225 plist_array_append_item(array, plist_new_string("DLVersionsOk"));
201 plist_array_append_item(array, plist_new_uint(version_major)); 226 plist_array_append_item(array, plist_new_uint(version_major));
202 if (property_list_service_send_binary_plist(client->parent, array) != PROPERTY_LIST_SERVICE_E_SUCCESS) { 227 err = device_link_error(property_list_service_send_binary_plist(client->parent, array));
228 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
203 debug_info("Error when sending DLVersionsOk"); 229 debug_info("Error when sending DLVersionsOk");
204 err = DEVICE_LINK_SERVICE_E_MUX_ERROR;
205 goto leave; 230 goto leave;
206 } 231 }
207 plist_free(array); 232 plist_free(array);
208 233
209 /* receive DeviceReady message */ 234 /* receive DeviceReady message */
210 array = NULL; 235 array = NULL;
211 if (property_list_service_receive_plist(client->parent, &array) != PROPERTY_LIST_SERVICE_E_SUCCESS) { 236 err = device_link_error(property_list_service_receive_plist(client->parent, &array));
237 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
212 debug_info("Error when receiving DLMessageDeviceReady!"); 238 debug_info("Error when receiving DLMessageDeviceReady!");
213 err = DEVICE_LINK_SERVICE_E_MUX_ERROR;
214 goto leave; 239 goto leave;
215 } 240 }
216 device_link_service_get_message(array, &msg); 241 device_link_service_get_message(array, &msg);
217 if (!msg || strcmp(msg, "DLMessageDeviceReady")) { 242 if (!msg || strcmp(msg, "DLMessageDeviceReady") != 0) {
218 debug_info("Did not get DLMessageDeviceReady!"); 243 debug_info("Did not get DLMessageDeviceReady!");
219 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR; 244 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR;
220 goto leave; 245 goto leave;
@@ -235,26 +260,28 @@ leave:
235 * Performs a disconnect with the connected device link service client. 260 * Performs a disconnect with the connected device link service client.
236 * 261 *
237 * @param client The device link service client to disconnect. 262 * @param client The device link service client to disconnect.
238 * 263 * @param message Optional message to send send to the device or NULL.
264 *
239 * @return DEVICE_LINK_SERVICE_E_SUCCESS on success, 265 * @return DEVICE_LINK_SERVICE_E_SUCCESS on success,
240 * DEVICE_LINK_SERVICE_E_INVALID_ARG if client is NULL, 266 * DEVICE_LINK_SERVICE_E_INVALID_ARG if client is NULL,
241 * or DEVICE_LINK_SERVICE_E_MUX_ERROR when there's an error when sending 267 * or DEVICE_LINK_SERVICE_E_MUX_ERROR when there's an error when sending
242 * the the disconnect message. 268 * the the disconnect message.
243 */ 269 */
244device_link_service_error_t device_link_service_disconnect(device_link_service_client_t client) 270device_link_service_error_t device_link_service_disconnect(device_link_service_client_t client, const char *message)
245{ 271{
246 if (!client) 272 if (!client)
247 return DEVICE_LINK_SERVICE_E_INVALID_ARG; 273 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
248 274
249 plist_t array = plist_new_array(); 275 plist_t array = plist_new_array();
250 plist_array_append_item(array, plist_new_string("DLMessageDisconnect")); 276 plist_array_append_item(array, plist_new_string("DLMessageDisconnect"));
251 plist_array_append_item(array, plist_new_string("All done, thanks for the memories")); 277 if (message)
278 plist_array_append_item(array, plist_new_string(message));
279 else
280 plist_array_append_item(array, plist_new_string("___EmptyParameterString___"));
252 281
253 device_link_service_error_t err = DEVICE_LINK_SERVICE_E_SUCCESS; 282 device_link_service_error_t err = device_link_error(property_list_service_send_binary_plist(client->parent, array));
254 if (property_list_service_send_binary_plist(client->parent, array) != PROPERTY_LIST_SERVICE_E_SUCCESS) {
255 err = DEVICE_LINK_SERVICE_E_MUX_ERROR;
256 }
257 plist_free(array); 283 plist_free(array);
284
258 return err; 285 return err;
259} 286}
260 287
@@ -278,11 +305,9 @@ device_link_service_error_t device_link_service_send_ping(device_link_service_cl
278 plist_array_append_item(array, plist_new_string("DLMessagePing")); 305 plist_array_append_item(array, plist_new_string("DLMessagePing"));
279 plist_array_append_item(array, plist_new_string(message)); 306 plist_array_append_item(array, plist_new_string(message));
280 307
281 device_link_service_error_t err = DEVICE_LINK_SERVICE_E_SUCCESS; 308 device_link_service_error_t err = device_link_error(property_list_service_send_binary_plist(client->parent, array));
282 if (property_list_service_send_binary_plist(client->parent, array) != PROPERTY_LIST_SERVICE_E_SUCCESS) {
283 err = DEVICE_LINK_SERVICE_E_MUX_ERROR;
284 }
285 plist_free(array); 309 plist_free(array);
310
286 return err; 311 return err;
287} 312}
288 313
@@ -309,11 +334,9 @@ device_link_service_error_t device_link_service_send_process_message(device_link
309 plist_array_append_item(array, plist_new_string("DLMessageProcessMessage")); 334 plist_array_append_item(array, plist_new_string("DLMessageProcessMessage"));
310 plist_array_append_item(array, plist_copy(message)); 335 plist_array_append_item(array, plist_copy(message));
311 336
312 device_link_service_error_t err = DEVICE_LINK_SERVICE_E_SUCCESS; 337 device_link_service_error_t err = device_link_error(property_list_service_send_binary_plist(client->parent, array));
313 if (property_list_service_send_binary_plist(client->parent, array) != PROPERTY_LIST_SERVICE_E_SUCCESS) {
314 err = DEVICE_LINK_SERVICE_E_MUX_ERROR;
315 }
316 plist_free(array); 338 plist_free(array);
339
317 return err; 340 return err;
318} 341}
319 342
@@ -340,8 +363,9 @@ device_link_service_error_t device_link_service_receive_message(device_link_serv
340 return DEVICE_LINK_SERVICE_E_INVALID_ARG; 363 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
341 364
342 *msg_plist = NULL; 365 *msg_plist = NULL;
343 if (property_list_service_receive_plist(client->parent, msg_plist) != PROPERTY_LIST_SERVICE_E_SUCCESS) { 366 device_link_service_error_t err = device_link_error(property_list_service_receive_plist(client->parent, msg_plist));
344 return DEVICE_LINK_SERVICE_E_MUX_ERROR; 367 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
368 return err;
345 } 369 }
346 370
347 if (!device_link_service_get_message(*msg_plist, dlmessage)) { 371 if (!device_link_service_get_message(*msg_plist, dlmessage)) {
@@ -370,15 +394,16 @@ device_link_service_error_t device_link_service_receive_process_message(device_l
370 return DEVICE_LINK_SERVICE_E_INVALID_ARG; 394 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
371 395
372 plist_t pmsg = NULL; 396 plist_t pmsg = NULL;
373 if (property_list_service_receive_plist(client->parent, &pmsg) != PROPERTY_LIST_SERVICE_E_SUCCESS) { 397 device_link_service_error_t err = device_link_error(property_list_service_receive_plist(client->parent, &pmsg));
374 return DEVICE_LINK_SERVICE_E_MUX_ERROR; 398 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
399 return err;
375 } 400 }
376 401
377 device_link_service_error_t err = DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR; 402 err = DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR;
378 403
379 char *msg = NULL; 404 char *msg = NULL;
380 device_link_service_get_message(pmsg, &msg); 405 device_link_service_get_message(pmsg, &msg);
381 if (!msg || strcmp(msg, "DLMessageProcessMessage")) { 406 if (!msg || strcmp(msg, "DLMessageProcessMessage") != 0) {
382 debug_info("Did not receive DLMessageProcessMessage as expected!"); 407 debug_info("Did not receive DLMessageProcessMessage as expected!");
383 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR; 408 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR;
384 goto leave; 409 goto leave;
@@ -424,10 +449,7 @@ device_link_service_error_t device_link_service_send(device_link_service_client_
424 if (!client || !plist) { 449 if (!client || !plist) {
425 return DEVICE_LINK_SERVICE_E_INVALID_ARG; 450 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
426 } 451 }
427 if (property_list_service_send_binary_plist(client->parent, plist) != PROPERTY_LIST_SERVICE_E_SUCCESS) { 452 return device_link_error(property_list_service_send_binary_plist(client->parent, plist));
428 return DEVICE_LINK_SERVICE_E_MUX_ERROR;
429 }
430 return DEVICE_LINK_SERVICE_E_SUCCESS;
431} 453}
432 454
433/* Generic device link service receive function. 455/* Generic device link service receive function.
@@ -447,9 +469,6 @@ device_link_service_error_t device_link_service_receive(device_link_service_clie
447 return DEVICE_LINK_SERVICE_E_INVALID_ARG; 469 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
448 } 470 }
449 471
450 if (property_list_service_receive_plist(client->parent, plist) != PROPERTY_LIST_SERVICE_E_SUCCESS) { 472 return device_link_error(property_list_service_receive_plist(client->parent, plist));
451 return DEVICE_LINK_SERVICE_E_MUX_ERROR;
452 }
453 return DEVICE_LINK_SERVICE_E_SUCCESS;
454} 473}
455 474