diff options
Diffstat (limited to 'src/SBServices.c')
-rw-r--r-- | src/SBServices.c | 139 |
1 files changed, 109 insertions, 30 deletions
diff --git a/src/SBServices.c b/src/SBServices.c index 1296245..69c7425 100644 --- a/src/SBServices.c +++ b/src/SBServices.c @@ -26,8 +26,8 @@ #include <plist/plist.h> #include "SBServices.h" -#include "iphone.h" -#include "utils.h" +#include "property_list_service.h" +#include "debug.h" /** Locks an sbservices client, done for thread safety stuff. * @@ -35,7 +35,7 @@ */ static void sbs_lock(sbservices_client_t client) { - log_debug_msg("SBServices: Locked\n"); + debug_info("SBServices: Locked"); g_mutex_lock(client->mutex); } @@ -45,11 +45,48 @@ static void sbs_lock(sbservices_client_t client) */ static void sbs_unlock(sbservices_client_t client) { - log_debug_msg("SBServices: Unlocked\n"); + debug_info("SBServices: Unlocked"); g_mutex_unlock(client->mutex); } -sbservices_error_t sbservices_client_new(iphone_device_t device, int dst_port, sbservices_client_t *client) +/** + * Convert a property_list_service_error_t value to a sbservices_error_t value. + * Used internally to get correct error codes. + * + * @param err A property_list_service_error_t error code + * + * @return A matching sbservices_error_t error code, + * SBSERVICES_E_UNKNOWN_ERROR otherwise. + */ +static sbservices_error_t sbservices_error(property_list_service_error_t err) +{ + switch (err) { + case PROPERTY_LIST_SERVICE_E_SUCCESS: + return SBSERVICES_E_SUCCESS; + case PROPERTY_LIST_SERVICE_E_INVALID_ARG: + return SBSERVICES_E_INVALID_ARG; + case PROPERTY_LIST_SERVICE_E_PLIST_ERROR: + return SBSERVICES_E_PLIST_ERROR; + case PROPERTY_LIST_SERVICE_E_MUX_ERROR: + return SBSERVICES_E_CONN_FAILED; + default: + break; + } + return SBSERVICES_E_UNKNOWN_ERROR; +} + +/** + * Creates a new sbservices client. + * + * @param device The device to connect to. + * @param port The port on device to connect to. + * @param client Pointer that will point to a newly allocated + * sbservices_client_t upon successful return. + * + * @return SBSERVICES_E_SUCCESS on success, SBSERVICES_E_INVALID_ARG when + * client is NULL, or an SBSERVICES_E_* error code otherwise. + */ +sbservices_error_t sbservices_client_new(iphone_device_t device, uint16_t port, sbservices_client_t *client) { /* makes sure thread environment is available */ if (!g_thread_supported()) @@ -58,38 +95,56 @@ sbservices_error_t sbservices_client_new(iphone_device_t device, int dst_port, s if (!device) return SBSERVICES_E_INVALID_ARG; - /* Attempt connection */ - iphone_connection_t connection = NULL; - if (iphone_device_connect(device, dst_port, &connection) != IPHONE_E_SUCCESS) { - return SBSERVICES_E_CONN_FAILED; + property_list_service_client_t plistclient = NULL; + sbservices_error_t err = sbservices_error(property_list_service_client_new(device, port, &plistclient)); + if (err != SBSERVICES_E_SUCCESS) { + return err; } sbservices_client_t client_loc = (sbservices_client_t) malloc(sizeof(struct sbservices_client_int)); - client_loc->connection = connection; + client_loc->parent = plistclient; client_loc->mutex = g_mutex_new(); *client = client_loc; return SBSERVICES_E_SUCCESS; } +/** + * Frees an sbservices client. + * + * @param client The sbservices client to free. + * + * @return SBSERVICES_E_SUCCESS on success, SBSERVICES_E_INVALID_ARG when + * client is NULL, or an SBSERVICES_E_* error code otherwise. + */ sbservices_error_t sbservices_client_free(sbservices_client_t client) { if (!client) return SBSERVICES_E_INVALID_ARG; - iphone_device_disconnect(client->connection); - client->connection = NULL; + sbservices_error_t err = sbservices_error(property_list_service_client_free(client->parent)); + client->parent = NULL; if (client->mutex) { g_mutex_free(client->mutex); } free(client); - return SBSERVICES_E_SUCCESS; + return err; } +/** + * Gets the icon state of the connected device. + * + * @param client The connected sbservices client to use. + * @param state Pointer that will point to a newly allocated plist containing + * the current icon state. It is up to the caller to free the memory. + * + * @return SBSERVICES_E_SUCCESS on success, SBSERVICES_E_INVALID_ARG when + * client or state is invalid, or an SBSERVICES_E_* error code otherwise. + */ sbservices_error_t sbservices_get_icon_state(sbservices_client_t client, plist_t *state) { - if (!client || !client->connection || !state) + if (!client || !client->parent || !state) return SBSERVICES_E_INVALID_ARG; sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR; @@ -99,17 +154,17 @@ sbservices_error_t sbservices_get_icon_state(sbservices_client_t client, plist_t sbs_lock(client); - if (iphone_device_send_binary_plist(client->connection, dict) != IPHONE_E_SUCCESS) { - log_debug_msg("%s: could not send plist\n", __func__); + res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict)); + if (res != SBSERVICES_E_SUCCESS) { + debug_info("could not send plist, error %d", res); goto leave_unlock; } plist_free(dict); dict = NULL; - if (iphone_device_receive_plist(client->connection, state) == IPHONE_E_SUCCESS) { - res = SBSERVICES_E_SUCCESS; - } else { - log_debug_msg("%s: could not get icon state!\n", __func__); + res = sbservices_error(property_list_service_receive_plist(client->parent, state)); + if (res != SBSERVICES_E_SUCCESS) { + debug_info("could not get icon state, error %d", res); if (*state) { plist_free(*state); *state = NULL; @@ -124,9 +179,18 @@ leave_unlock: return res; } +/** + * Sets the icon state of the connected device. + * + * @param client The connected sbservices client to use. + * @param newstate A plist containing the new iconstate. + * + * @return SBSERVICES_E_SUCCESS on success, SBSERVICES_E_INVALID_ARG when + * client or newstate is NULL, or an SBSERVICES_E_* error code otherwise. + */ sbservices_error_t sbservices_set_icon_state(sbservices_client_t client, plist_t newstate) { - if (!client || !client->connection || !newstate) + if (!client || !client->parent || !newstate) return SBSERVICES_E_INVALID_ARG; sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR; @@ -137,13 +201,12 @@ sbservices_error_t sbservices_set_icon_state(sbservices_client_t client, plist_t sbs_lock(client); - if (iphone_device_send_binary_plist(client->connection, dict) != IPHONE_E_SUCCESS) { - log_debug_msg("%s: could not send plist\n", __func__); - goto leave_unlock; + res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict)); + if (res != SBSERVICES_E_SUCCESS) { + debug_info("could not send plist, error %d", res); } // NO RESPONSE -leave_unlock: if (dict) { plist_free(dict); } @@ -151,9 +214,24 @@ leave_unlock: return res; } +/** + * Get the icon of the specified app as PNG data. + * + * @param client The connected sbservices client to use. + * @param bundleId The bundle identifier of the app to retrieve the icon for. + * @param pngdata Pointer that will point to a newly allocated buffer + * containing the PNG data upon successful return. It is up to the caller + * to free the memory. + * @param pngsize Pointer to a uint64_t that will be set to the size of the + * buffer pngdata points to upon successful return. + * + * @return SBSERVICES_E_SUCCESS on success, SBSERVICES_E_INVALID_ARG when + * client, bundleId, or pngdata are invalid, or an SBSERVICES_E_* error + * code otherwise. + */ sbservices_error_t sbservices_get_icon_pngdata(sbservices_client_t client, const char *bundleId, char **pngdata, uint64_t *pngsize) { - if (!client || !client->connection || !pngdata) + if (!client || !client->parent || !bundleId || !pngdata) return SBSERVICES_E_INVALID_ARG; sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR; @@ -164,19 +242,20 @@ sbservices_error_t sbservices_get_icon_pngdata(sbservices_client_t client, const sbs_lock(client); - if (iphone_device_send_binary_plist(client->connection, dict) != IPHONE_E_SUCCESS) { - log_debug_msg("%s: could not send plist\n", __func__); + res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict)); + if (res != SBSERVICES_E_SUCCESS) { + debug_info("could not send plist, error %d", res); goto leave_unlock; } plist_free(dict); dict = NULL; - if (iphone_device_receive_plist(client->connection, &dict) == IPHONE_E_SUCCESS) { + res = sbservices_error(property_list_service_receive_plist(client->parent, &dict)); + if (res == SBSERVICES_E_SUCCESS) { plist_t node = plist_dict_get_item(dict, "pngData"); if (node) { plist_get_data_val(node, pngdata, pngsize); } - res = SBSERVICES_E_SUCCESS; } leave_unlock: |