From d11abfb48218a37d9c66831ebec8b0a736d5385f Mon Sep 17 00:00:00 2001 From: Martin Szulecki Date: Sat, 25 Jul 2009 02:20:03 +0200 Subject: Update NotificationProxy API and introduce new error codes --- dev/main.c | 14 ++--- include/libiphone/notification_proxy.h | 34 +++++++----- src/NotificationProxy.c | 99 +++++++++++++++++----------------- 3 files changed, 77 insertions(+), 70 deletions(-) diff --git a/dev/main.c b/dev/main.c index 46c5557..33c60f9 100644 --- a/dev/main.c +++ b/dev/main.c @@ -46,11 +46,11 @@ static void perform_notification(iphone_device_t phone, lockdownd_client_t clien lockdownd_start_service(client, "com.apple.mobile.notification_proxy", &nport); if (nport) { printf("::::::::::::::: np was started ::::::::::::\n"); - np_new_client(phone, nport, &np); + np_client_new(phone, nport, &np); if (np) { printf("::::::::: PostNotification %s\n", notification); np_post_notification(np, notification); - np_free_client(np); + np_client_free(np); } } else { printf("::::::::::::::: np was NOT started ::::::::::::\n"); @@ -108,7 +108,7 @@ int main(int argc, char *argv[]) lockdownd_start_service(client, "com.apple.mobile.notification_proxy", &npp); if (npp) { printf("Notification Proxy started.\n"); - np_new_client(phone, npp, &gnp); + np_client_new(phone, npp, &gnp); } else { printf("ERROR: Notification proxy could not be started.\n"); } @@ -225,17 +225,19 @@ int main(int argc, char *argv[]) printf("XXX sleeping\n"); sleep(5); - //perform_notification(phone, control, NP_SYNC_DID_FINISH); - printf("XXX unlocking file\n"); afc_lock_file(afc, lockfile, AFC_LOCK_UN); printf("XXX closing file\n"); afc_close_file(afc, lockfile); + + printf("XXX sleeping\n"); + sleep(5); + //perform_notification(phone, client, NP_SYNC_DID_FINISH); } if (gnp) { - np_free_client(gnp); + np_client_free(gnp); gnp = NULL; } diff --git a/include/libiphone/notification_proxy.h b/include/libiphone/notification_proxy.h index 14d1b39..520ccd1 100644 --- a/include/libiphone/notification_proxy.h +++ b/include/libiphone/notification_proxy.h @@ -27,16 +27,21 @@ extern "C" { #include -struct np_client_int; -typedef struct np_client_int *np_client_t; +/* Error Codes */ +#define NP_E_SUCCESS 0 +#define NP_E_INVALID_ARG -1 +#define NP_E_PLIST_ERROR -2 + +#define NP_E_UNKNOWN_ERROR -256 + +typedef int16_t np_error_t; -// NotificationProxy related -// notifications for use with post_notification (client --> device) +/* Notification IDs for use with post_notification (client --> device) */ #define NP_SYNC_WILL_START "com.apple.itunes-mobdev.syncWillStart" #define NP_SYNC_DID_START "com.apple.itunes-mobdev.syncDidStart" #define NP_SYNC_DID_FINISH "com.apple.itunes-mobdev.syncDidFinish" -// notifications for use with observe_notification (device --> client) +/* Notification IDs for use with observe_notification (device --> client) */ #define NP_SYNC_CANCEL_REQUEST "com.apple.itunes-client.syncCancelRequest" #define NP_SYNC_SUSPEND_REQUEST "com.apple.itunes-client.syncSuspendRequest" #define NP_SYNC_RESUME_REQUEST "com.apple.itunes-client.syncResumeRequest" @@ -48,17 +53,18 @@ typedef struct np_client_int *np_client_t; #define NP_APP_UNINSTALLED "com.apple.mobile.application_uninstalled" #define NP_ITDBPREP_DID_END "com.apple.itdbprep.notification.didEnd" -iphone_error_t np_new_client ( iphone_device_t device, int dst_port, np_client_t *client ); -iphone_error_t np_free_client ( np_client_t client ); - -iphone_error_t np_post_notification ( np_client_t client, const char *notification ); - -iphone_error_t np_observe_notification ( np_client_t client, const char *notification ); -iphone_error_t np_observe_notifications ( np_client_t client, const char **notification_spec ); +struct np_client_int; +typedef struct np_client_int *np_client_t; -typedef void (*np_notify_cb_t) ( const char *notification ); +typedef void (*np_notify_cb_t) (const char *notification); -iphone_error_t np_set_notify_callback ( np_client_t client, np_notify_cb_t notify_cb ); +/* Interface */ +np_error_t np_client_new(iphone_device_t device, int dst_port, np_client_t *client); +np_error_t np_client_free(np_client_t client); +np_error_t np_post_notification(np_client_t client, const char *notification); +np_error_t np_observe_notification(np_client_t client, const char *notification); +np_error_t np_observe_notifications(np_client_t client, const char **notification_spec); +np_error_t np_set_notify_callback(np_client_t client, np_notify_cb_t notify_cb); #ifdef __cplusplus } diff --git a/src/NotificationProxy.c b/src/NotificationProxy.c index 1bbdc6d..7e52405 100644 --- a/src/NotificationProxy.c +++ b/src/NotificationProxy.c @@ -24,6 +24,7 @@ #include #include #include + #include "NotificationProxy.h" #include "iphone.h" #include "utils.h" @@ -60,24 +61,24 @@ static void np_unlock(np_client_t client) * @param client NP to send data to * @param dict plist to send * - * @return IPHONE_E_SUCCESS or an error code. + * @return NP_E_SUCCESS or an error code. */ -static iphone_error_t np_plist_send(np_client_t client, plist_t dict) +static np_error_t np_plist_send(np_client_t client, plist_t dict) { char *XML_content = NULL; uint32_t length = 0; uint32_t nlen = 0; int bytes = 0; - iphone_error_t res = IPHONE_E_UNKNOWN_ERROR; + np_error_t res = NP_E_UNKNOWN_ERROR; if (!client || !dict) { - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; } plist_to_xml(dict, &XML_content, &length); if (!XML_content || length == 0) { - return IPHONE_E_PLIST_ERROR; + return NP_E_PLIST_ERROR; } nlen = htonl(length); @@ -86,7 +87,7 @@ static iphone_error_t np_plist_send(np_client_t client, plist_t dict) usbmuxd_send(client->sfd, XML_content, length, (uint32_t*)&bytes); if (bytes > 0) { if ((uint32_t)bytes == length) { - res = IPHONE_E_SUCCESS; + res = NP_E_SUCCESS; } else { log_debug_msg("%s: ERROR: Could not send all data (%d of %d)!\n", __func__, bytes, length); } @@ -109,19 +110,19 @@ static iphone_error_t np_plist_send(np_client_t client, plist_t dict) * * @return A handle to the newly-connected client or NULL upon error. */ -iphone_error_t np_new_client ( iphone_device_t device, int dst_port, np_client_t *client ) +np_error_t np_client_new(iphone_device_t device, int dst_port, np_client_t *client) { - //makes sure thread environment is available + /* makes sure thread environment is available */ if (!g_thread_supported()) g_thread_init(NULL); if (!device) - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; - // Attempt connection + /* Attempt connection */ int sfd = usbmuxd_connect(device->handle, dst_port); if (sfd < 0) { - return IPHONE_E_UNKNOWN_ERROR; //ret; + return NP_E_UNKNOWN_ERROR; } np_client_t client_loc = (np_client_t) malloc(sizeof(struct np_client_int)); @@ -132,17 +133,17 @@ iphone_error_t np_new_client ( iphone_device_t device, int dst_port, np_client_t client_loc->notifier = NULL; *client = client_loc; - return IPHONE_E_SUCCESS; + return NP_E_SUCCESS; } /** Disconnects an NP client from the phone. * * @param client The client to disconnect. */ -iphone_error_t np_free_client ( np_client_t client ) +np_error_t np_client_free(np_client_t client) { if (!client) - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; usbmuxd_disconnect(client->sfd); client->sfd = -1; @@ -155,7 +156,7 @@ iphone_error_t np_free_client ( np_client_t client ) } free(client); - return IPHONE_E_SUCCESS; + return NP_E_SUCCESS; } /** Sends a notification to the device's Notification Proxy. @@ -167,10 +168,10 @@ iphone_error_t np_free_client ( np_client_t client ) * @param client The client to send to * @param notification The notification message to send */ -iphone_error_t np_post_notification( np_client_t client, const char *notification ) +np_error_t np_post_notification(np_client_t client, const char *notification) { if (!client || !notification) { - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; } np_lock(client); @@ -180,7 +181,7 @@ iphone_error_t np_post_notification( np_client_t client, const char *notificatio plist_add_sub_key_el(dict, "Name"); plist_add_sub_string_el(dict, notification); - iphone_error_t res = np_plist_send(client, dict); + np_error_t res = np_plist_send(client, dict); plist_free(dict); dict = plist_new_dict(); @@ -190,7 +191,7 @@ iphone_error_t np_post_notification( np_client_t client, const char *notificatio res = np_plist_send(client, dict); plist_free(dict); - if (res != IPHONE_E_SUCCESS) { + if (res != NP_E_SUCCESS) { log_debug_msg("%s: Error sending XML plist to device!\n", __func__); } @@ -203,10 +204,10 @@ iphone_error_t np_post_notification( np_client_t client, const char *notificatio * @param client The client to send to * @param notification The notifications that should be observed. */ -iphone_error_t np_observe_notification( np_client_t client, const char *notification ) +np_error_t np_observe_notification( np_client_t client, const char *notification ) { if (!client || !notification) { - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; } np_lock(client); @@ -216,8 +217,8 @@ iphone_error_t np_observe_notification( np_client_t client, const char *notifica plist_add_sub_key_el(dict, "Name"); plist_add_sub_string_el(dict, notification); - iphone_error_t res = np_plist_send(client, dict); - if (res != IPHONE_E_SUCCESS) { + np_error_t res = np_plist_send(client, dict); + if (res != NP_E_SUCCESS) { log_debug_msg("%s: Error sending XML plist to device!\n", __func__); } plist_free(dict); @@ -245,14 +246,14 @@ iphone_error_t np_observe_notification( np_client_t client, const char *notifica * terminating NULL entry. However this parameter can be NULL; in this case, * the default set of notifications will be used. */ -iphone_error_t np_observe_notifications( np_client_t client, const char **notification_spec ) +np_error_t np_observe_notifications(np_client_t client, const char **notification_spec) { int i = 0; - iphone_error_t res = IPHONE_E_UNKNOWN_ERROR; + np_error_t res = NP_E_UNKNOWN_ERROR; const char **notifications = notification_spec; if (!client) { - return IPHONE_E_INVALID_ARG; + return NP_E_INVALID_ARG; } if (!notifications) { @@ -261,7 +262,7 @@ iphone_error_t np_observe_notifications( np_client_t client, const char **notifi while (notifications[i]) { res = np_observe_notification(client, notifications[i]); - if (res != IPHONE_E_SUCCESS) { + if (res != NP_E_SUCCESS) { break; } i++; @@ -277,24 +278,22 @@ iphone_error_t np_observe_notifications( np_client_t client, const char **notifi * @param notification Pointer to a buffer that will be allocated and filled * with the notification that has been received. * - * @return IPHONE_E_SUCCESS if a notification has been received, - * IPHONE_E_TIMEOUT if nothing has been received, + * @return 0 if a notification has been received or nothing has been received, * or an error value if an error occured. * * @note You probably want to check out np_set_notify_callback * @see np_set_notify_callback */ -static iphone_error_t np_get_notification( np_client_t client, char **notification ) +static int np_get_notification(np_client_t client, char **notification) { uint32_t bytes = 0; - iphone_error_t res; + int res = 0; uint32_t pktlen = 0; char *XML_content = NULL; plist_t dict = NULL; - if (!client || client->sfd < 0 || *notification) { - return IPHONE_E_INVALID_ARG; - } + if (!client || client->sfd < 0 || *notification) + return -1; np_lock(client); @@ -302,7 +301,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati log_debug_msg("NotificationProxy: initial read=%i\n", bytes); if (bytes < 4) { log_debug_msg("NotificationProxy: no notification received!\n"); - res = IPHONE_E_TIMEOUT; + res = 0; } else { if ((char)pktlen == 0) { pktlen = ntohl(pktlen); @@ -312,7 +311,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati usbmuxd_recv_timeout(client->sfd, XML_content, pktlen, &bytes, 1000); if (bytes <= 0) { - res = IPHONE_E_UNKNOWN_ERROR; + res = -1; } else { log_debug_msg("NotificationProxy: received data:\n"); log_debug_buffer(XML_content, pktlen); @@ -320,7 +319,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati plist_from_xml(XML_content, bytes, &dict); if (!dict) { np_unlock(client); - return IPHONE_E_PLIST_ERROR; + return -2; } plist_t cmd_key_node = plist_find_node_by_key(dict, "Command"); @@ -345,21 +344,21 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati plist_get_string_val(name_value_node, &name_value); } - res = IPHONE_E_PLIST_ERROR; + res = -2; if (name_key && name_value && !strcmp(name_key, "Name")) { *notification = name_value; log_debug_msg("%s: got notification %s\n", __func__, name_value); - res = IPHONE_E_SUCCESS; + res = 0; } free(name_key); } else if (cmd_value && !strcmp(cmd_value, "ProxyDeath")) { log_debug_msg("%s: ERROR: NotificationProxy died!\n", __func__); - res = IPHONE_E_UNKNOWN_ERROR; + res = -1; } else if (cmd_value) { log_debug_msg("%d: unknown NotificationProxy command '%s' received!\n", __func__); - res = IPHONE_E_UNKNOWN_ERROR; + res = -1; } else { - res = IPHONE_E_PLIST_ERROR; + res = -2; } if (cmd_value) { free(cmd_value); @@ -370,7 +369,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati XML_content = NULL; } } else { - res = IPHONE_E_UNKNOWN_ERROR; + res = -1; } } @@ -416,15 +415,15 @@ gpointer np_notifier( gpointer arg ) * @param notify_cb pointer to a callback function or NULL to de-register a * previously set callback function * - * @return IPHONE_E_SUCCESS when the callback was successfully registered, + * @return NP_E_SUCCESS when the callback was successfully registered, * or an error value when an error occured. */ -iphone_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify_cb ) +np_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify_cb ) { - if (!client) { - return IPHONE_E_INVALID_ARG; - } - iphone_error_t res = IPHONE_E_UNKNOWN_ERROR; + if (!client) + return NP_E_INVALID_ARG; + + np_error_t res = NP_E_UNKNOWN_ERROR; np_lock(client); if (client->notifier) { @@ -444,7 +443,7 @@ iphone_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify client->notifier = g_thread_create(np_notifier, npt, TRUE, NULL); if (client->notifier) { - res = IPHONE_E_SUCCESS; + res = NP_E_SUCCESS; } } } else { -- cgit v1.1-32-gdbae