diff options
Diffstat (limited to 'src/device_link_service.c')
-rw-r--r-- | src/device_link_service.c | 103 |
1 files changed, 57 insertions, 46 deletions
diff --git a/src/device_link_service.c b/src/device_link_service.c index 007223e..66c2461 100644 --- a/src/device_link_service.c +++ b/src/device_link_service.c @@ -2,7 +2,7 @@ * 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 @@ -18,12 +18,37 @@ * License along with this library; if not, write to the Free Software * 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 "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 * plist. @@ -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; } @@ -89,18 +114,15 @@ device_link_service_error_t device_link_service_client_new(idevice_t device, loc } property_list_service_client_t plistclient = NULL; - if (property_list_service_client_new(device, service, &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 */ device_link_service_client_t client_loc = (device_link_service_client_t) malloc(sizeof(struct device_link_service_client_private)); client_loc->parent = plistclient; - /* enable SSL if requested */ - if (service->ssl_enabled) - property_list_service_enable_ssl(client_loc->parent); - /* all done, return success */ *client = client_loc; return DEVICE_LINK_SERVICE_E_SUCCESS; @@ -121,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; } /** @@ -157,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; @@ -203,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; @@ -258,11 +279,9 @@ device_link_service_error_t device_link_service_disconnect(device_link_service_c 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; } @@ -286,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; } @@ -317,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; } @@ -348,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)) { @@ -378,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; @@ -432,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. @@ -455,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)); } |