diff options
Diffstat (limited to 'src/device_link_service.c')
-rw-r--r-- | src/device_link_service.c | 129 |
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 @@ - /* +/* * device_link_service.c * DeviceLink service implementation. - * - * Copyright (c) 2010 Nikias Bassen, All Rights Reserved. + * + * Copyright (c) 2010-2019 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 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif #include <string.h> #include <stdlib.h> #include "device_link_service.h" #include "property_list_service.h" -#include "debug.h" +#include "common/debug.h" + +static device_link_service_error_t device_link_error(property_list_service_error_t err) +{ + switch (err) { + case PROPERTY_LIST_SERVICE_E_SUCCESS: + return DEVICE_LINK_SERVICE_E_SUCCESS; + case PROPERTY_LIST_SERVICE_E_INVALID_ARG: + return DEVICE_LINK_SERVICE_E_INVALID_ARG; + case PROPERTY_LIST_SERVICE_E_PLIST_ERROR: + return DEVICE_LINK_SERVICE_E_PLIST_ERROR; + case PROPERTY_LIST_SERVICE_E_MUX_ERROR: + return DEVICE_LINK_SERVICE_E_MUX_ERROR; + case PROPERTY_LIST_SERVICE_E_SSL_ERROR: + return DEVICE_LINK_SERVICE_E_SSL_ERROR; + case PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT: + return DEVICE_LINK_SERVICE_E_RECEIVE_TIMEOUT; + default: + break; + } + return DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR; +} /** * 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) return 0; } - if ((strlen(cmd_str) < 9) || (strncmp(cmd_str, "DL", 2))) { + if ((strlen(cmd_str) < 9) || (strncmp(cmd_str, "DL", 2) != 0)) { free(cmd_str); return 0; } @@ -74,7 +99,7 @@ static int device_link_service_get_message(plist_t dl_msg, char **message) * Creates a new device link service client. * * @param device The device to connect to. - * @param port Port on device to connect to. + * @param service The service descriptor returned by lockdownd_start_service. * @param client Reference that will point to a newly allocated * device_link_service_client_t upon successful return. * @@ -82,15 +107,16 @@ static int device_link_service_get_message(plist_t dl_msg, char **message) * DEVICE_LINK_SERVICE_E_INVALID_ARG when one of the parameters is invalid, * or DEVICE_LINK_SERVICE_E_MUX_ERROR when the connection failed. */ -device_link_service_error_t device_link_service_client_new(idevice_t device, uint16_t port, device_link_service_client_t *client) +device_link_service_error_t device_link_service_client_new(idevice_t device, lockdownd_service_descriptor_t service, device_link_service_client_t *client) { - if (!device || port == 0 || !client || *client) { + if (!device || !service || service->port == 0 || !client || *client) { return DEVICE_LINK_SERVICE_E_INVALID_ARG; } property_list_service_client_t plistclient = NULL; - if (property_list_service_client_new(device, port, &plistclient) != PROPERTY_LIST_SERVICE_E_SUCCESS) { - return DEVICE_LINK_SERVICE_E_MUX_ERROR; + device_link_service_error_t err = device_link_error(property_list_service_client_new(device, service, &plistclient)); + if (err != DEVICE_LINK_SERVICE_E_SUCCESS) { + return err; } /* create client object */ @@ -117,11 +143,10 @@ device_link_service_error_t device_link_service_client_free(device_link_service_ if (!client) return DEVICE_LINK_SERVICE_E_INVALID_ARG; - if (property_list_service_client_free(client->parent) != PROPERTY_LIST_SERVICE_E_SUCCESS) { - return DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR; - } + device_link_service_error_t err = device_link_error(property_list_service_client_free(client->parent)); free(client); - return DEVICE_LINK_SERVICE_E_SUCCESS; + + return err; } /** @@ -145,7 +170,7 @@ device_link_service_error_t device_link_service_version_exchange(device_link_ser { if (!client) return DEVICE_LINK_SERVICE_E_INVALID_ARG; - + device_link_service_error_t err = DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR; /* perform version exchange */ @@ -153,13 +178,13 @@ device_link_service_error_t device_link_service_version_exchange(device_link_ser char *msg = NULL; /* receive DLMessageVersionExchange from device */ - if (property_list_service_receive_plist(client->parent, &array) != PROPERTY_LIST_SERVICE_E_SUCCESS) { + err = device_link_error(property_list_service_receive_plist(client->parent, &array)); + if (err != DEVICE_LINK_SERVICE_E_SUCCESS) { debug_info("Did not receive initial message from device!"); - err = DEVICE_LINK_SERVICE_E_MUX_ERROR; goto leave; } device_link_service_get_message(array, &msg); - if (!msg || strcmp(msg, "DLMessageVersionExchange")) { + if (!msg || strcmp(msg, "DLMessageVersionExchange") != 0) { debug_info("Did not receive DLMessageVersionExchange from device!"); err = DEVICE_LINK_SERVICE_E_PLIST_ERROR; goto leave; @@ -199,22 +224,22 @@ device_link_service_error_t device_link_service_version_exchange(device_link_ser plist_array_append_item(array, plist_new_string("DLMessageVersionExchange")); plist_array_append_item(array, plist_new_string("DLVersionsOk")); plist_array_append_item(array, plist_new_uint(version_major)); - if (property_list_service_send_binary_plist(client->parent, array) != PROPERTY_LIST_SERVICE_E_SUCCESS) { + err = device_link_error(property_list_service_send_binary_plist(client->parent, array)); + if (err != DEVICE_LINK_SERVICE_E_SUCCESS) { debug_info("Error when sending DLVersionsOk"); - err = DEVICE_LINK_SERVICE_E_MUX_ERROR; goto leave; } plist_free(array); /* receive DeviceReady message */ array = NULL; - if (property_list_service_receive_plist(client->parent, &array) != PROPERTY_LIST_SERVICE_E_SUCCESS) { + err = device_link_error(property_list_service_receive_plist(client->parent, &array)); + if (err != DEVICE_LINK_SERVICE_E_SUCCESS) { debug_info("Error when receiving DLMessageDeviceReady!"); - err = DEVICE_LINK_SERVICE_E_MUX_ERROR; goto leave; } device_link_service_get_message(array, &msg); - if (!msg || strcmp(msg, "DLMessageDeviceReady")) { + if (!msg || strcmp(msg, "DLMessageDeviceReady") != 0) { debug_info("Did not get DLMessageDeviceReady!"); err = DEVICE_LINK_SERVICE_E_PLIST_ERROR; goto leave; @@ -235,26 +260,28 @@ leave: * Performs a disconnect with the connected device link service client. * * @param client The device link service client to disconnect. - * + * @param message Optional message to send send to the device or NULL. + * * @return DEVICE_LINK_SERVICE_E_SUCCESS on success, * DEVICE_LINK_SERVICE_E_INVALID_ARG if client is NULL, * or DEVICE_LINK_SERVICE_E_MUX_ERROR when there's an error when sending * the the disconnect message. */ -device_link_service_error_t device_link_service_disconnect(device_link_service_client_t client) +device_link_service_error_t device_link_service_disconnect(device_link_service_client_t client, const char *message) { if (!client) return DEVICE_LINK_SERVICE_E_INVALID_ARG; plist_t array = plist_new_array(); plist_array_append_item(array, plist_new_string("DLMessageDisconnect")); - plist_array_append_item(array, plist_new_string("All done, thanks for the memories")); + if (message) + plist_array_append_item(array, plist_new_string(message)); + else + plist_array_append_item(array, plist_new_string("___EmptyParameterString___")); - device_link_service_error_t err = DEVICE_LINK_SERVICE_E_SUCCESS; - if (property_list_service_send_binary_plist(client->parent, array) != PROPERTY_LIST_SERVICE_E_SUCCESS) { - err = DEVICE_LINK_SERVICE_E_MUX_ERROR; - } + device_link_service_error_t err = device_link_error(property_list_service_send_binary_plist(client->parent, array)); plist_free(array); + return err; } @@ -278,11 +305,9 @@ device_link_service_error_t device_link_service_send_ping(device_link_service_cl plist_array_append_item(array, plist_new_string("DLMessagePing")); plist_array_append_item(array, plist_new_string(message)); - device_link_service_error_t err = DEVICE_LINK_SERVICE_E_SUCCESS; - if (property_list_service_send_binary_plist(client->parent, array) != PROPERTY_LIST_SERVICE_E_SUCCESS) { - err = DEVICE_LINK_SERVICE_E_MUX_ERROR; - } + device_link_service_error_t err = device_link_error(property_list_service_send_binary_plist(client->parent, array)); plist_free(array); + return err; } @@ -309,11 +334,9 @@ device_link_service_error_t device_link_service_send_process_message(device_link plist_array_append_item(array, plist_new_string("DLMessageProcessMessage")); plist_array_append_item(array, plist_copy(message)); - device_link_service_error_t err = DEVICE_LINK_SERVICE_E_SUCCESS; - if (property_list_service_send_binary_plist(client->parent, array) != PROPERTY_LIST_SERVICE_E_SUCCESS) { - err = DEVICE_LINK_SERVICE_E_MUX_ERROR; - } + device_link_service_error_t err = device_link_error(property_list_service_send_binary_plist(client->parent, array)); plist_free(array); + return err; } @@ -340,8 +363,9 @@ device_link_service_error_t device_link_service_receive_message(device_link_serv return DEVICE_LINK_SERVICE_E_INVALID_ARG; *msg_plist = NULL; - if (property_list_service_receive_plist(client->parent, msg_plist) != PROPERTY_LIST_SERVICE_E_SUCCESS) { - return DEVICE_LINK_SERVICE_E_MUX_ERROR; + device_link_service_error_t err = device_link_error(property_list_service_receive_plist(client->parent, msg_plist)); + if (err != DEVICE_LINK_SERVICE_E_SUCCESS) { + return err; } 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 return DEVICE_LINK_SERVICE_E_INVALID_ARG; plist_t pmsg = NULL; - if (property_list_service_receive_plist(client->parent, &pmsg) != PROPERTY_LIST_SERVICE_E_SUCCESS) { - return DEVICE_LINK_SERVICE_E_MUX_ERROR; + device_link_service_error_t err = device_link_error(property_list_service_receive_plist(client->parent, &pmsg)); + if (err != DEVICE_LINK_SERVICE_E_SUCCESS) { + return err; } - device_link_service_error_t err = DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR; + err = DEVICE_LINK_SERVICE_E_UNKNOWN_ERROR; char *msg = NULL; device_link_service_get_message(pmsg, &msg); - if (!msg || strcmp(msg, "DLMessageProcessMessage")) { + if (!msg || strcmp(msg, "DLMessageProcessMessage") != 0) { debug_info("Did not receive DLMessageProcessMessage as expected!"); err = DEVICE_LINK_SERVICE_E_PLIST_ERROR; goto leave; @@ -424,10 +449,7 @@ device_link_service_error_t device_link_service_send(device_link_service_client_ if (!client || !plist) { return DEVICE_LINK_SERVICE_E_INVALID_ARG; } - if (property_list_service_send_binary_plist(client->parent, plist) != PROPERTY_LIST_SERVICE_E_SUCCESS) { - return DEVICE_LINK_SERVICE_E_MUX_ERROR; - } - return DEVICE_LINK_SERVICE_E_SUCCESS; + return device_link_error(property_list_service_send_binary_plist(client->parent, plist)); } /* Generic device link service receive function. @@ -447,9 +469,6 @@ device_link_service_error_t device_link_service_receive(device_link_service_clie return DEVICE_LINK_SERVICE_E_INVALID_ARG; } - if (property_list_service_receive_plist(client->parent, plist) != PROPERTY_LIST_SERVICE_E_SUCCESS) { - return DEVICE_LINK_SERVICE_E_MUX_ERROR; - } - return DEVICE_LINK_SERVICE_E_SUCCESS; + return device_link_error(property_list_service_receive_plist(client->parent, plist)); } |