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