summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/NotificationProxy.c99
1 files changed, 49 insertions, 50 deletions
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 @@
24#include <stdlib.h> 24#include <stdlib.h>
25#include <arpa/inet.h> 25#include <arpa/inet.h>
26#include <plist/plist.h> 26#include <plist/plist.h>
27
27#include "NotificationProxy.h" 28#include "NotificationProxy.h"
28#include "iphone.h" 29#include "iphone.h"
29#include "utils.h" 30#include "utils.h"
@@ -60,24 +61,24 @@ static void np_unlock(np_client_t client)
60 * @param client NP to send data to 61 * @param client NP to send data to
61 * @param dict plist to send 62 * @param dict plist to send
62 * 63 *
63 * @return IPHONE_E_SUCCESS or an error code. 64 * @return NP_E_SUCCESS or an error code.
64 */ 65 */
65static iphone_error_t np_plist_send(np_client_t client, plist_t dict) 66static np_error_t np_plist_send(np_client_t client, plist_t dict)
66{ 67{
67 char *XML_content = NULL; 68 char *XML_content = NULL;
68 uint32_t length = 0; 69 uint32_t length = 0;
69 uint32_t nlen = 0; 70 uint32_t nlen = 0;
70 int bytes = 0; 71 int bytes = 0;
71 iphone_error_t res = IPHONE_E_UNKNOWN_ERROR; 72 np_error_t res = NP_E_UNKNOWN_ERROR;
72 73
73 if (!client || !dict) { 74 if (!client || !dict) {
74 return IPHONE_E_INVALID_ARG; 75 return NP_E_INVALID_ARG;
75 } 76 }
76 77
77 plist_to_xml(dict, &XML_content, &length); 78 plist_to_xml(dict, &XML_content, &length);
78 79
79 if (!XML_content || length == 0) { 80 if (!XML_content || length == 0) {
80 return IPHONE_E_PLIST_ERROR; 81 return NP_E_PLIST_ERROR;
81 } 82 }
82 83
83 nlen = htonl(length); 84 nlen = htonl(length);
@@ -86,7 +87,7 @@ static iphone_error_t np_plist_send(np_client_t client, plist_t dict)
86 usbmuxd_send(client->sfd, XML_content, length, (uint32_t*)&bytes); 87 usbmuxd_send(client->sfd, XML_content, length, (uint32_t*)&bytes);
87 if (bytes > 0) { 88 if (bytes > 0) {
88 if ((uint32_t)bytes == length) { 89 if ((uint32_t)bytes == length) {
89 res = IPHONE_E_SUCCESS; 90 res = NP_E_SUCCESS;
90 } else { 91 } else {
91 log_debug_msg("%s: ERROR: Could not send all data (%d of %d)!\n", __func__, bytes, length); 92 log_debug_msg("%s: ERROR: Could not send all data (%d of %d)!\n", __func__, bytes, length);
92 } 93 }
@@ -109,19 +110,19 @@ static iphone_error_t np_plist_send(np_client_t client, plist_t dict)
109 * 110 *
110 * @return A handle to the newly-connected client or NULL upon error. 111 * @return A handle to the newly-connected client or NULL upon error.
111 */ 112 */
112iphone_error_t np_new_client ( iphone_device_t device, int dst_port, np_client_t *client ) 113np_error_t np_client_new(iphone_device_t device, int dst_port, np_client_t *client)
113{ 114{
114 //makes sure thread environment is available 115 /* makes sure thread environment is available */
115 if (!g_thread_supported()) 116 if (!g_thread_supported())
116 g_thread_init(NULL); 117 g_thread_init(NULL);
117 118
118 if (!device) 119 if (!device)
119 return IPHONE_E_INVALID_ARG; 120 return NP_E_INVALID_ARG;
120 121
121 // Attempt connection 122 /* Attempt connection */
122 int sfd = usbmuxd_connect(device->handle, dst_port); 123 int sfd = usbmuxd_connect(device->handle, dst_port);
123 if (sfd < 0) { 124 if (sfd < 0) {
124 return IPHONE_E_UNKNOWN_ERROR; //ret; 125 return NP_E_UNKNOWN_ERROR;
125 } 126 }
126 127
127 np_client_t client_loc = (np_client_t) malloc(sizeof(struct np_client_int)); 128 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
132 client_loc->notifier = NULL; 133 client_loc->notifier = NULL;
133 134
134 *client = client_loc; 135 *client = client_loc;
135 return IPHONE_E_SUCCESS; 136 return NP_E_SUCCESS;
136} 137}
137 138
138/** Disconnects an NP client from the phone. 139/** Disconnects an NP client from the phone.
139 * 140 *
140 * @param client The client to disconnect. 141 * @param client The client to disconnect.
141 */ 142 */
142iphone_error_t np_free_client ( np_client_t client ) 143np_error_t np_client_free(np_client_t client)
143{ 144{
144 if (!client) 145 if (!client)
145 return IPHONE_E_INVALID_ARG; 146 return NP_E_INVALID_ARG;
146 147
147 usbmuxd_disconnect(client->sfd); 148 usbmuxd_disconnect(client->sfd);
148 client->sfd = -1; 149 client->sfd = -1;
@@ -155,7 +156,7 @@ iphone_error_t np_free_client ( np_client_t client )
155 } 156 }
156 free(client); 157 free(client);
157 158
158 return IPHONE_E_SUCCESS; 159 return NP_E_SUCCESS;
159} 160}
160 161
161/** Sends a notification to the device's Notification Proxy. 162/** Sends a notification to the device's Notification Proxy.
@@ -167,10 +168,10 @@ iphone_error_t np_free_client ( np_client_t client )
167 * @param client The client to send to 168 * @param client The client to send to
168 * @param notification The notification message to send 169 * @param notification The notification message to send
169 */ 170 */
170iphone_error_t np_post_notification( np_client_t client, const char *notification ) 171np_error_t np_post_notification(np_client_t client, const char *notification)
171{ 172{
172 if (!client || !notification) { 173 if (!client || !notification) {
173 return IPHONE_E_INVALID_ARG; 174 return NP_E_INVALID_ARG;
174 } 175 }
175 np_lock(client); 176 np_lock(client);
176 177
@@ -180,7 +181,7 @@ iphone_error_t np_post_notification( np_client_t client, const char *notificatio
180 plist_add_sub_key_el(dict, "Name"); 181 plist_add_sub_key_el(dict, "Name");
181 plist_add_sub_string_el(dict, notification); 182 plist_add_sub_string_el(dict, notification);
182 183
183 iphone_error_t res = np_plist_send(client, dict); 184 np_error_t res = np_plist_send(client, dict);
184 plist_free(dict); 185 plist_free(dict);
185 186
186 dict = plist_new_dict(); 187 dict = plist_new_dict();
@@ -190,7 +191,7 @@ iphone_error_t np_post_notification( np_client_t client, const char *notificatio
190 res = np_plist_send(client, dict); 191 res = np_plist_send(client, dict);
191 plist_free(dict); 192 plist_free(dict);
192 193
193 if (res != IPHONE_E_SUCCESS) { 194 if (res != NP_E_SUCCESS) {
194 log_debug_msg("%s: Error sending XML plist to device!\n", __func__); 195 log_debug_msg("%s: Error sending XML plist to device!\n", __func__);
195 } 196 }
196 197
@@ -203,10 +204,10 @@ iphone_error_t np_post_notification( np_client_t client, const char *notificatio
203 * @param client The client to send to 204 * @param client The client to send to
204 * @param notification The notifications that should be observed. 205 * @param notification The notifications that should be observed.
205 */ 206 */
206iphone_error_t np_observe_notification( np_client_t client, const char *notification ) 207np_error_t np_observe_notification( np_client_t client, const char *notification )
207{ 208{
208 if (!client || !notification) { 209 if (!client || !notification) {
209 return IPHONE_E_INVALID_ARG; 210 return NP_E_INVALID_ARG;
210 } 211 }
211 np_lock(client); 212 np_lock(client);
212 213
@@ -216,8 +217,8 @@ iphone_error_t np_observe_notification( np_client_t client, const char *notifica
216 plist_add_sub_key_el(dict, "Name"); 217 plist_add_sub_key_el(dict, "Name");
217 plist_add_sub_string_el(dict, notification); 218 plist_add_sub_string_el(dict, notification);
218 219
219 iphone_error_t res = np_plist_send(client, dict); 220 np_error_t res = np_plist_send(client, dict);
220 if (res != IPHONE_E_SUCCESS) { 221 if (res != NP_E_SUCCESS) {
221 log_debug_msg("%s: Error sending XML plist to device!\n", __func__); 222 log_debug_msg("%s: Error sending XML plist to device!\n", __func__);
222 } 223 }
223 plist_free(dict); 224 plist_free(dict);
@@ -245,14 +246,14 @@ iphone_error_t np_observe_notification( np_client_t client, const char *notifica
245 * terminating NULL entry. However this parameter can be NULL; in this case, 246 * terminating NULL entry. However this parameter can be NULL; in this case,
246 * the default set of notifications will be used. 247 * the default set of notifications will be used.
247 */ 248 */
248iphone_error_t np_observe_notifications( np_client_t client, const char **notification_spec ) 249np_error_t np_observe_notifications(np_client_t client, const char **notification_spec)
249{ 250{
250 int i = 0; 251 int i = 0;
251 iphone_error_t res = IPHONE_E_UNKNOWN_ERROR; 252 np_error_t res = NP_E_UNKNOWN_ERROR;
252 const char **notifications = notification_spec; 253 const char **notifications = notification_spec;
253 254
254 if (!client) { 255 if (!client) {
255 return IPHONE_E_INVALID_ARG; 256 return NP_E_INVALID_ARG;
256 } 257 }
257 258
258 if (!notifications) { 259 if (!notifications) {
@@ -261,7 +262,7 @@ iphone_error_t np_observe_notifications( np_client_t client, const char **notifi
261 262
262 while (notifications[i]) { 263 while (notifications[i]) {
263 res = np_observe_notification(client, notifications[i]); 264 res = np_observe_notification(client, notifications[i]);
264 if (res != IPHONE_E_SUCCESS) { 265 if (res != NP_E_SUCCESS) {
265 break; 266 break;
266 } 267 }
267 i++; 268 i++;
@@ -277,24 +278,22 @@ iphone_error_t np_observe_notifications( np_client_t client, const char **notifi
277 * @param notification Pointer to a buffer that will be allocated and filled 278 * @param notification Pointer to a buffer that will be allocated and filled
278 * with the notification that has been received. 279 * with the notification that has been received.
279 * 280 *
280 * @return IPHONE_E_SUCCESS if a notification has been received, 281 * @return 0 if a notification has been received or nothing has been received,
281 * IPHONE_E_TIMEOUT if nothing has been received,
282 * or an error value if an error occured. 282 * or an error value if an error occured.
283 * 283 *
284 * @note You probably want to check out np_set_notify_callback 284 * @note You probably want to check out np_set_notify_callback
285 * @see np_set_notify_callback 285 * @see np_set_notify_callback
286 */ 286 */
287static iphone_error_t np_get_notification( np_client_t client, char **notification ) 287static int np_get_notification(np_client_t client, char **notification)
288{ 288{
289 uint32_t bytes = 0; 289 uint32_t bytes = 0;
290 iphone_error_t res; 290 int res = 0;
291 uint32_t pktlen = 0; 291 uint32_t pktlen = 0;
292 char *XML_content = NULL; 292 char *XML_content = NULL;
293 plist_t dict = NULL; 293 plist_t dict = NULL;
294 294
295 if (!client || client->sfd < 0 || *notification) { 295 if (!client || client->sfd < 0 || *notification)
296 return IPHONE_E_INVALID_ARG; 296 return -1;
297 }
298 297
299 np_lock(client); 298 np_lock(client);
300 299
@@ -302,7 +301,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati
302 log_debug_msg("NotificationProxy: initial read=%i\n", bytes); 301 log_debug_msg("NotificationProxy: initial read=%i\n", bytes);
303 if (bytes < 4) { 302 if (bytes < 4) {
304 log_debug_msg("NotificationProxy: no notification received!\n"); 303 log_debug_msg("NotificationProxy: no notification received!\n");
305 res = IPHONE_E_TIMEOUT; 304 res = 0;
306 } else { 305 } else {
307 if ((char)pktlen == 0) { 306 if ((char)pktlen == 0) {
308 pktlen = ntohl(pktlen); 307 pktlen = ntohl(pktlen);
@@ -312,7 +311,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati
312 311
313 usbmuxd_recv_timeout(client->sfd, XML_content, pktlen, &bytes, 1000); 312 usbmuxd_recv_timeout(client->sfd, XML_content, pktlen, &bytes, 1000);
314 if (bytes <= 0) { 313 if (bytes <= 0) {
315 res = IPHONE_E_UNKNOWN_ERROR; 314 res = -1;
316 } else { 315 } else {
317 log_debug_msg("NotificationProxy: received data:\n"); 316 log_debug_msg("NotificationProxy: received data:\n");
318 log_debug_buffer(XML_content, pktlen); 317 log_debug_buffer(XML_content, pktlen);
@@ -320,7 +319,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati
320 plist_from_xml(XML_content, bytes, &dict); 319 plist_from_xml(XML_content, bytes, &dict);
321 if (!dict) { 320 if (!dict) {
322 np_unlock(client); 321 np_unlock(client);
323 return IPHONE_E_PLIST_ERROR; 322 return -2;
324 } 323 }
325 324
326 plist_t cmd_key_node = plist_find_node_by_key(dict, "Command"); 325 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
345 plist_get_string_val(name_value_node, &name_value); 344 plist_get_string_val(name_value_node, &name_value);
346 } 345 }
347 346
348 res = IPHONE_E_PLIST_ERROR; 347 res = -2;
349 if (name_key && name_value && !strcmp(name_key, "Name")) { 348 if (name_key && name_value && !strcmp(name_key, "Name")) {
350 *notification = name_value; 349 *notification = name_value;
351 log_debug_msg("%s: got notification %s\n", __func__, name_value); 350 log_debug_msg("%s: got notification %s\n", __func__, name_value);
352 res = IPHONE_E_SUCCESS; 351 res = 0;
353 } 352 }
354 free(name_key); 353 free(name_key);
355 } else if (cmd_value && !strcmp(cmd_value, "ProxyDeath")) { 354 } else if (cmd_value && !strcmp(cmd_value, "ProxyDeath")) {
356 log_debug_msg("%s: ERROR: NotificationProxy died!\n", __func__); 355 log_debug_msg("%s: ERROR: NotificationProxy died!\n", __func__);
357 res = IPHONE_E_UNKNOWN_ERROR; 356 res = -1;
358 } else if (cmd_value) { 357 } else if (cmd_value) {
359 log_debug_msg("%d: unknown NotificationProxy command '%s' received!\n", __func__); 358 log_debug_msg("%d: unknown NotificationProxy command '%s' received!\n", __func__);
360 res = IPHONE_E_UNKNOWN_ERROR; 359 res = -1;
361 } else { 360 } else {
362 res = IPHONE_E_PLIST_ERROR; 361 res = -2;
363 } 362 }
364 if (cmd_value) { 363 if (cmd_value) {
365 free(cmd_value); 364 free(cmd_value);
@@ -370,7 +369,7 @@ static iphone_error_t np_get_notification( np_client_t client, char **notificati
370 XML_content = NULL; 369 XML_content = NULL;
371 } 370 }
372 } else { 371 } else {
373 res = IPHONE_E_UNKNOWN_ERROR; 372 res = -1;
374 } 373 }
375 } 374 }
376 375
@@ -416,15 +415,15 @@ gpointer np_notifier( gpointer arg )
416 * @param notify_cb pointer to a callback function or NULL to de-register a 415 * @param notify_cb pointer to a callback function or NULL to de-register a
417 * previously set callback function 416 * previously set callback function
418 * 417 *
419 * @return IPHONE_E_SUCCESS when the callback was successfully registered, 418 * @return NP_E_SUCCESS when the callback was successfully registered,
420 * or an error value when an error occured. 419 * or an error value when an error occured.
421 */ 420 */
422iphone_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify_cb ) 421np_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify_cb )
423{ 422{
424 if (!client) { 423 if (!client)
425 return IPHONE_E_INVALID_ARG; 424 return NP_E_INVALID_ARG;
426 } 425
427 iphone_error_t res = IPHONE_E_UNKNOWN_ERROR; 426 np_error_t res = NP_E_UNKNOWN_ERROR;
428 427
429 np_lock(client); 428 np_lock(client);
430 if (client->notifier) { 429 if (client->notifier) {
@@ -444,7 +443,7 @@ iphone_error_t np_set_notify_callback( np_client_t client, np_notify_cb_t notify
444 443
445 client->notifier = g_thread_create(np_notifier, npt, TRUE, NULL); 444 client->notifier = g_thread_create(np_notifier, npt, TRUE, NULL);
446 if (client->notifier) { 445 if (client->notifier) {
447 res = IPHONE_E_SUCCESS; 446 res = NP_E_SUCCESS;
448 } 447 }
449 } 448 }
450 } else { 449 } else {