summaryrefslogtreecommitdiffstats
path: root/src/NotificationProxy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/NotificationProxy.c')
-rw-r--r--src/NotificationProxy.c47
1 files changed, 22 insertions, 25 deletions
diff --git a/src/NotificationProxy.c b/src/NotificationProxy.c
index d8bcc34..6fc048c 100644
--- a/src/NotificationProxy.c
+++ b/src/NotificationProxy.c
@@ -21,6 +21,7 @@
21 21
22#include <string.h> 22#include <string.h>
23#include <stdio.h> 23#include <stdio.h>
24#include <stdlib.h>
24#include <arpa/inet.h> 25#include <arpa/inet.h>
25#include <plist/plist.h> 26#include <plist/plist.h>
26#include "NotificationProxy.h" 27#include "NotificationProxy.h"
@@ -79,9 +80,9 @@ static iphone_error_t np_plist_send(iphone_np_client_t client, plist_t dict)
79 } 80 }
80 81
81 nlen = htonl(length); 82 nlen = htonl(length);
82 iphone_mux_send(client->connection, (const char*)&nlen, sizeof(nlen), (uint32_t*)&bytes); 83 usbmuxd_send(client->sfd, (const char*)&nlen, sizeof(nlen), (uint32_t*)&bytes);
83 if (bytes == sizeof(nlen)) { 84 if (bytes == sizeof(nlen)) {
84 iphone_mux_send(client->connection, XML_content, length, (uint32_t*)&bytes); 85 usbmuxd_send(client->sfd, XML_content, length, (uint32_t*)&bytes);
85 if (bytes > 0) { 86 if (bytes > 0) {
86 if ((uint32_t)bytes == length) { 87 if ((uint32_t)bytes == length) {
87 res = IPHONE_E_SUCCESS; 88 res = IPHONE_E_SUCCESS;
@@ -107,26 +108,24 @@ static iphone_error_t np_plist_send(iphone_np_client_t client, plist_t dict)
107 * 108 *
108 * @return A handle to the newly-connected client or NULL upon error. 109 * @return A handle to the newly-connected client or NULL upon error.
109 */ 110 */
110iphone_error_t iphone_np_new_client ( iphone_device_t device, int src_port, int dst_port, iphone_np_client_t *client ) 111iphone_error_t iphone_np_new_client ( iphone_device_t device, int dst_port, iphone_np_client_t *client )
111{ 112{
112 int ret = IPHONE_E_SUCCESS;
113
114 //makes sure thread environment is available 113 //makes sure thread environment is available
115 if (!g_thread_supported()) 114 if (!g_thread_supported())
116 g_thread_init(NULL); 115 g_thread_init(NULL);
117 iphone_np_client_t client_loc = (iphone_np_client_t) malloc(sizeof(struct iphone_np_client_int));
118 116
119 if (!device) 117 if (!device)
120 return IPHONE_E_INVALID_ARG; 118 return IPHONE_E_INVALID_ARG;
121 119
122 // Attempt connection 120 // Attempt connection
123 client_loc->connection = NULL; 121 int sfd = usbmuxd_connect(device->handle, dst_port);
124 ret = iphone_mux_new_client(device, src_port, dst_port, &client_loc->connection); 122 if (sfd < 0) {
125 if (IPHONE_E_SUCCESS != ret || !client_loc->connection) { 123 return IPHONE_E_UNKNOWN_ERROR; //ret;
126 free(client_loc);
127 return ret;
128 } 124 }
129 125
126 iphone_np_client_t client_loc = (iphone_np_client_t) malloc(sizeof(struct iphone_np_client_int));
127 client_loc->sfd = sfd;
128
130 client_loc->mutex = g_mutex_new(); 129 client_loc->mutex = g_mutex_new();
131 130
132 client_loc->notifier = NULL; 131 client_loc->notifier = NULL;
@@ -144,13 +143,11 @@ iphone_error_t iphone_np_free_client ( iphone_np_client_t client )
144 if (!client) 143 if (!client)
145 return IPHONE_E_INVALID_ARG; 144 return IPHONE_E_INVALID_ARG;
146 145
147 if (client->connection) { 146 usbmuxd_disconnect(client->sfd);
148 iphone_mux_free_client(client->connection); 147 client->sfd = -1;
149 client->connection = NULL; 148 if (client->notifier) {
150 if (client->notifier) { 149 log_debug_msg("joining np callback\n");
151 log_debug_msg("joining np callback\n"); 150 g_thread_join(client->notifier);
152 g_thread_join(client->notifier);
153 }
154 } 151 }
155 if (client->mutex) { 152 if (client->mutex) {
156 g_mutex_free(client->mutex); 153 g_mutex_free(client->mutex);
@@ -295,13 +292,13 @@ iphone_error_t iphone_np_get_notification( iphone_np_client_t client, char **not
295 char *XML_content = NULL; 292 char *XML_content = NULL;
296 plist_t dict = NULL; 293 plist_t dict = NULL;
297 294
298 if (!client || !client->connection || *notification) { 295 if (!client || client->sfd < 0 || *notification) {
299 return IPHONE_E_INVALID_ARG; 296 return IPHONE_E_INVALID_ARG;
300 } 297 }
301 298
302 np_lock(client); 299 np_lock(client);
303 300
304 iphone_mux_recv_timeout(client->connection, (char*)&pktlen, sizeof(pktlen), &bytes, 500); 301 usbmuxd_recv_timeout(client->sfd, (char*)&pktlen, sizeof(pktlen), &bytes, 500);
305 log_debug_msg("NotificationProxy: initial read=%i\n", bytes); 302 log_debug_msg("NotificationProxy: initial read=%i\n", bytes);
306 if (bytes < 4) { 303 if (bytes < 4) {
307 log_debug_msg("NotificationProxy: no notification received!\n"); 304 log_debug_msg("NotificationProxy: no notification received!\n");
@@ -313,7 +310,7 @@ iphone_error_t iphone_np_get_notification( iphone_np_client_t client, char **not
313 XML_content = (char*)malloc(pktlen); 310 XML_content = (char*)malloc(pktlen);
314 log_debug_msg("pointer %p\n", XML_content); 311 log_debug_msg("pointer %p\n", XML_content);
315 312
316 iphone_mux_recv_timeout(client->connection, XML_content, pktlen, &bytes, 1000); 313 usbmuxd_recv_timeout(client->sfd, XML_content, pktlen, &bytes, 1000);
317 if (bytes <= 0) { 314 if (bytes <= 0) {
318 res = IPHONE_E_UNKNOWN_ERROR; 315 res = IPHONE_E_UNKNOWN_ERROR;
319 } else { 316 } else {
@@ -393,7 +390,7 @@ gpointer iphone_np_notifier( gpointer arg )
393 if (!npt) return NULL; 390 if (!npt) return NULL;
394 391
395 log_debug_msg("%s: starting callback.\n", __func__); 392 log_debug_msg("%s: starting callback.\n", __func__);
396 while (npt->client->connection) { 393 while (npt->client->sfd >= 0) {
397 iphone_np_get_notification(npt->client, &notification); 394 iphone_np_get_notification(npt->client, &notification);
398 if (notification) { 395 if (notification) {
399 npt->cbfunc(notification); 396 npt->cbfunc(notification);
@@ -432,11 +429,11 @@ iphone_error_t iphone_np_set_notify_callback( iphone_np_client_t client, iphone_
432 np_lock(client); 429 np_lock(client);
433 if (client->notifier) { 430 if (client->notifier) {
434 log_debug_msg("%s: callback already set, removing\n"); 431 log_debug_msg("%s: callback already set, removing\n");
435 iphone_umux_client_t conn = client->connection; 432 int conn = client->sfd;
436 client->connection = NULL; 433 client->sfd = -1;
437 g_thread_join(client->notifier); 434 g_thread_join(client->notifier);
438 client->notifier = NULL; 435 client->notifier = NULL;
439 client->connection = conn; 436 client->sfd = conn;
440 } 437 }
441 438
442 if (notify_cb) { 439 if (notify_cb) {