Debian Codegraph @

src/device_link_service.c

raw
1 /*
2 * device_link_service.c
3 * DeviceLink service implementation.
4 *
5 * Copyright (c) 2010-2019 Nikias Bassen, All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
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
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <string.h>
26 #include <stdlib.h>
27 #include "device_link_service.h"
28 #include "property_list_service.h"
29 #include "common/debug.h"
30
31 static 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 }
51
52 /**
53 * Internally used function to extract the message string from a DL* message
54 * plist.
55 *
56 * @param dl_msg The DeviceLink property list to parse.
57 * @param message A pointer that will be set to a newly allocated char*
58 * containing the DLMessage* string from the given plist. It is up to
59 * the caller to free the allocated memory. If this parameter is NULL
60 * it will be ignored.
61 *
62 * @return 1 if the given plist is a DL* message, or 0 if the plist does not
63 * contain any DL* message.
64 */
65 static int device_link_service_get_message(plist_t dl_msg, char **message)
66 {
67 plist_t cmd = NULL;
68 char *cmd_str = NULL;
69
70 /* sanity check */
71 if ((plist_get_node_type(dl_msg) != PLIST_ARRAY) || (plist_array_get_size(dl_msg) < 1)) {
72 return 0;
73 }
74
75 /* get dl command */
76 cmd = plist_array_get_item(dl_msg, 0);
77 if (!cmd || (plist_get_node_type(cmd) != PLIST_STRING)) {
78 return 0;
79 }
80
81 plist_get_string_val(cmd, &cmd_str);
82 if (!cmd_str) {
83 return 0;
84 }
85
86 if ((strlen(cmd_str) < 9) || (strncmp(cmd_str, "DL", 2) != 0)) {
87 free(cmd_str);
88 return 0;
89 }
90
91 if (message)
92 *message = cmd_str;
93
94 /* we got a DL* message */
95 return 1;
96 }
97
98 /**
99 * Creates a new device link service client.
100 *
101 * @param device The device to connect to.
102 * @param service The service descriptor returned by lockdownd_start_service.
103 * @param client Reference that will point to a newly allocated
104 * device_link_service_client_t upon successful return.
105 *
106 * @return DEVICE_LINK_SERVICE_E_SUCCESS on success,
107 * DEVICE_LINK_SERVICE_E_INVALID_ARG when one of the parameters is invalid,
108 * or DEVICE_LINK_SERVICE_E_MUX_ERROR when the connection failed.
109 */
110 device_link_service_error_t device_link_service_client_new(idevice_t device, lockdownd_service_descriptor_t service, device_link_service_client_t *client)
111 {
112 if (!device || !service || service->port == 0 || !client || *client) {
113 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
114 }
115
116 property_list_service_client_t plistclient = NULL;
117 device_link_service_error_t err = device_link_error(property_list_service_client_new(device, service, &plistclient));
118 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
119 return err;
120 }
121
122 /* create client object */
123 device_link_service_client_t client_loc = (device_link_service_client_t) malloc(sizeof(struct device_link_service_client_private));
124 client_loc->parent = plistclient;
125
126 /* all done, return success */
127 *client = client_loc;
128 return DEVICE_LINK_SERVICE_E_SUCCESS;
129 }
130
131 /**
132 * Frees a device link service client.
133 *
134 * @param client The device_link_service_client_t to free.
135 *
136 * @return DEVICE_LINK_SERVICE_E_SUCCESS on success,
137 * DEVICE_LINK_SERVICE_E_INVALID_ARG when one of client or client->parent
138 * is invalid, or DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR when the was an error
139 * freeing the parent property_list_service client.
140 */
141 device_link_service_error_t device_link_service_client_free(device_link_service_client_t client)
142 {
143 if (!client)
144 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
145
146 device_link_service_error_t err = device_link_error(property_list_service_client_free(client->parent));
147 free(client);
148
149 return err;
150 }
151
152 /**
153 * Performs the DLMessageVersionExchange with the connected device.
154 * This should be the first operation to be executed by an implemented
155 * device link service client.
156 *
157 * @param client The device_link_service client to use.
158 * @param version_major The major version number to check.
159 * @param version_minor The minor version number to check.
160 *
161 * @return DEVICE_LINK_SERVICE_E_SUCCESS on success,
162 * DEVICE_LINK_SERVICE_E_INVALID_ARG when client is NULL,
163 * DEVICE_LINK_SERVICE_E_MUX_ERROR when a communication error occurs,
164 * DEVICE_LINK_SERVICE_E_PLIST_ERROR when the received plist has not the
165 * expected contents, DEVICE_LINK_SERVICE_E_BAD_VERSION when the version
166 * given by the device is larger than the given version,
167 * or DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR otherwise.
168 */
169 device_link_service_error_t device_link_service_version_exchange(device_link_service_client_t client, uint64_t version_major, uint64_t version_minor)
170 {
171 if (!client)
172 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
173
174 device_link_service_error_t err = DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR;
175
176 /* perform version exchange */
177 plist_t array = NULL;
178 char *msg = NULL;
179
180 /* receive DLMessageVersionExchange from device */
181 err = device_link_error(property_list_service_receive_plist(client->parent, &array));
182 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
183 debug_info("Did not receive initial message from device!");
184 goto leave;
185 }
186 device_link_service_get_message(array, &msg);
187 if (!msg || strcmp(msg, "DLMessageVersionExchange") != 0) {
188 debug_info("Did not receive DLMessageVersionExchange from device!");
189 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR;
190 goto leave;
191 }
192 free(msg);
193 msg = NULL;
194
195 /* get major and minor version number */
196 if (plist_array_get_size(array) < 3) {
197 debug_info("DLMessageVersionExchange has unexpected format!");
198 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR;
199 goto leave;
200 }
201 plist_t maj = plist_array_get_item(array, 1);
202 plist_t min = plist_array_get_item(array, 2);
203 uint64_t vmajor = 0;
204 uint64_t vminor = 0;
205 if (maj) {
206 plist_get_uint_val(maj, &vmajor);
207 }
208 if (min) {
209 plist_get_uint_val(min, &vminor);
210 }
211 if (vmajor > version_major) {
212 debug_info("Version mismatch: device=(%lld,%lld) > expected=(%lld,%lld)", vmajor, vminor, version_major, version_minor);
213 err = DEVICE_LINK_SERVICE_E_BAD_VERSION;
214 goto leave;
215 } else if ((vmajor == version_major) && (vminor > version_minor)) {
216 debug_info("WARNING: Version mismatch: device=(%lld,%lld) > expected=(%lld,%lld)", vmajor, vminor, version_major, version_minor);
217 err = DEVICE_LINK_SERVICE_E_BAD_VERSION;
218 goto leave;
219 }
220 plist_free(array);
221
222 /* version is ok, send reply */
223 array = plist_new_array();
224 plist_array_append_item(array, plist_new_string("DLMessageVersionExchange"));
225 plist_array_append_item(array, plist_new_string("DLVersionsOk"));
226 plist_array_append_item(array, plist_new_uint(version_major));
227 err = device_link_error(property_list_service_send_binary_plist(client->parent, array));
228 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
229 debug_info("Error when sending DLVersionsOk");
230 goto leave;
231 }
232 plist_free(array);
233
234 /* receive DeviceReady message */
235 array = NULL;
236 err = device_link_error(property_list_service_receive_plist(client->parent, &array));
237 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
238 debug_info("Error when receiving DLMessageDeviceReady!");
239 goto leave;
240 }
241 device_link_service_get_message(array, &msg);
242 if (!msg || strcmp(msg, "DLMessageDeviceReady") != 0) {
243 debug_info("Did not get DLMessageDeviceReady!");
244 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR;
245 goto leave;
246 }
247 err = DEVICE_LINK_SERVICE_E_SUCCESS;
248
249 leave:
250 if (msg) {
251 free(msg);
252 }
253 if (array) {
254 plist_free(array);
255 }
256 return err;
257 }
258
259 /**
260 * Performs a disconnect with the connected device link service client.
261 *
262 * @param client The device link service client to disconnect.
263 * @param message Optional message to send send to the device or NULL.
264 *
265 * @return DEVICE_LINK_SERVICE_E_SUCCESS on success,
266 * DEVICE_LINK_SERVICE_E_INVALID_ARG if client is NULL,
267 * or DEVICE_LINK_SERVICE_E_MUX_ERROR when there's an error when sending
268 * the the disconnect message.
269 */
270 device_link_service_error_t device_link_service_disconnect(device_link_service_client_t client, const char *message)
271 {
272 if (!client)
273 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
274
275 plist_t array = plist_new_array();
276 plist_array_append_item(array, plist_new_string("DLMessageDisconnect"));
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___"));
281
282 device_link_service_error_t err = device_link_error(property_list_service_send_binary_plist(client->parent, array));
283 plist_free(array);
284
285 return err;
286 }
287
288 /**
289 * Sends a DLMessagePing plist.
290 *
291 * @param client The device link service client to use.
292 * @param message String to send as ping message.
293 *
294 * @return DEVICE_LINK_SERVICE_E_SUCCESS on success,
295 * DEVICE_LINK_SERVICE_E_INVALID_ARG if client or message is invalid,
296 * or DEVICE_LINK_SERVICE_E_MUX_ERROR if the DLMessagePing plist could
297 * not be sent.
298 */
299 device_link_service_error_t device_link_service_send_ping(device_link_service_client_t client, const char *message)
300 {
301 if (!client || !client->parent || !message)
302 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
303
304 plist_t array = plist_new_array();
305 plist_array_append_item(array, plist_new_string("DLMessagePing"));
306 plist_array_append_item(array, plist_new_string(message));
307
308 device_link_service_error_t err = device_link_error(property_list_service_send_binary_plist(client->parent, array));
309 plist_free(array);
310
311 return err;
312 }
313
314 /**
315 * Sends a DLMessageProcessMessage plist.
316 *
317 * @param client The device link service client to use.
318 * @param message PLIST_DICT to send.
319 *
320 * @return DEVICE_LINK_SERVICE_E_SUCCESS on success,
321 * DEVICE_LINK_SERVICE_E_INVALID_ARG if client or message is invalid or
322 * message is not a PLIST_DICT, or DEVICE_LINK_SERVICE_E_MUX_ERROR if
323 * the DLMessageProcessMessage plist could not be sent.
324 */
325 device_link_service_error_t device_link_service_send_process_message(device_link_service_client_t client, plist_t message)
326 {
327 if (!client || !client->parent || !message)
328 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
329
330 if (plist_get_node_type(message) != PLIST_DICT)
331 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
332
333 plist_t array = plist_new_array();
334 plist_array_append_item(array, plist_new_string("DLMessageProcessMessage"));
335 plist_array_append_item(array, plist_copy(message));
336
337 device_link_service_error_t err = device_link_error(property_list_service_send_binary_plist(client->parent, array));
338 plist_free(array);
339
340 return err;
341 }
342
343 /**
344 * Receives a DL* message plist
345 *
346 * @param client The connected device link service client used for receiving.
347 * @param msg_plist Pointer to a plist that will be set to the contents of the
348 * message plist upon successful return.
349 * @param dlmessage A pointer that will be set to a newly allocated char*
350 * containing the DL* string from the given plist. It is up to the caller
351 * to free the allocated memory. If this parameter is NULL
352 * it will be ignored.
353 *
354 * @return DEVICE_LINK_SERVICE_E_SUCCESS if a DL* message was received,
355 * DEVICE_LINK_SERVICE_E_INVALID_ARG if client or message is invalid,
356 * DEVICE_LINK_SERVICE_E_PLIST_ERROR if the received plist is invalid
357 * or is not a DL* message plist, or DEVICE_LINK_SERVICE_E_MUX_ERROR if
358 * receiving from the device failed.
359 */
360 device_link_service_error_t device_link_service_receive_message(device_link_service_client_t client, plist_t *msg_plist, char **dlmessage)
361 {
362 if (!client || !client->parent || !msg_plist)
363 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
364
365 *msg_plist = NULL;
366 device_link_service_error_t err = device_link_error(property_list_service_receive_plist(client->parent, msg_plist));
367 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
368 return err;
369 }
370
371 if (!device_link_service_get_message(*msg_plist, dlmessage)) {
372 debug_info("Did not receive a DL* message as expected!");
373 return DEVICE_LINK_SERVICE_E_PLIST_ERROR;
374 }
375 return DEVICE_LINK_SERVICE_E_SUCCESS;
376 }
377
378 /**
379 * Receives a DLMessageProcessMessage plist.
380 *
381 * @param client The connected device link service client used for receiving.
382 * @param message Pointer to a plist that will be set to the contents of the
383 * message contents upon successful return.
384 *
385 * @return DEVICE_LINK_SERVICE_E_SUCCESS when a DLMessageProcessMessage was
386 * received, DEVICE_LINK_SERVICE_E_INVALID_ARG when client or message is
387 * invalid, DEVICE_LINK_SERVICE_E_PLIST_ERROR if the received plist is
388 * invalid or is not a DLMessageProcessMessage,
389 * or DEVICE_LINK_SERVICE_E_MUX_ERROR if receiving from device fails.
390 */
391 device_link_service_error_t device_link_service_receive_process_message(device_link_service_client_t client, plist_t *message)
392 {
393 if (!client || !client->parent || !message)
394 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
395
396 plist_t pmsg = NULL;
397 device_link_service_error_t err = device_link_error(property_list_service_receive_plist(client->parent, &pmsg));
398 if (err != DEVICE_LINK_SERVICE_E_SUCCESS) {
399 return err;
400 }
401
402 err = DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR;
403
404 char *msg = NULL;
405 device_link_service_get_message(pmsg, &msg);
406 if (!msg || strcmp(msg, "DLMessageProcessMessage") != 0) {
407 debug_info("Did not receive DLMessageProcessMessage as expected!");
408 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR;
409 goto leave;
410 }
411
412 if (plist_array_get_size(pmsg) != 2) {
413 debug_info("Malformed plist received for DLMessageProcessMessage");
414 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR;
415 goto leave;
416 }
417
418 plist_t msg_loc = plist_array_get_item(pmsg, 1);
419 if (msg_loc) {
420 *message = plist_copy(msg_loc);
421 err = DEVICE_LINK_SERVICE_E_SUCCESS;
422 } else {
423 *message = NULL;
424 err = DEVICE_LINK_SERVICE_E_PLIST_ERROR;
425 }
426
427 leave:
428 if (msg)
429 free(msg);
430 if (pmsg)
431 plist_free(pmsg);
432
433 return err;
434 }
435
436 /**
437 * Generic device link service send function.
438 *
439 * @param client The device link service client to use for sending
440 * @param plist The property list to send
441 *
442 * @return DEVICE_LINK_SERVICE_E_SUCCESS on success,
443 * DEVICE_LINK_SERVICE_E_INVALID_ARG when client or plist is NULL,
444 * or DEVICE_LINK_SERVICE_E_MUX_ERROR when the given property list could
445 * not be sent.
446 */
447 device_link_service_error_t device_link_service_send(device_link_service_client_t client, plist_t plist)
448 {
449 if (!client || !plist) {
450 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
451 }
452 return device_link_error(property_list_service_send_binary_plist(client->parent, plist));
453 }
454
455 /* Generic device link service receive function.
456 *
457 * @param client The device link service client to use for sending
458 * @param plist Pointer that will point to the property list received upon
459 * successful return.
460 *
461 * @return DEVICE_LINK_SERVICE_E_SUCCESS on success,
462 * DEVICE_LINK_SERVICE_E_INVALID_ARG when client or plist is NULL,
463 * or DEVICE_LINK_SERVICE_E_MUX_ERROR when no property list could be
464 * received.
465 */
466 device_link_service_error_t device_link_service_receive(device_link_service_client_t client, plist_t *plist)
467 {
468 if (!client || !plist || (plist && *plist)) {
469 return DEVICE_LINK_SERVICE_E_INVALID_ARG;
470 }
471
472 return device_link_error(property_list_service_receive_plist(client->parent, plist));
473 }
474