summaryrefslogtreecommitdiffstats
path: root/src/NotificationProxy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/NotificationProxy.c')
-rw-r--r--src/NotificationProxy.c162
1 files changed, 54 insertions, 108 deletions
diff --git a/src/NotificationProxy.c b/src/NotificationProxy.c
index 160ac4a..e2c1faa 100644
--- a/src/NotificationProxy.c
+++ b/src/NotificationProxy.c
@@ -56,53 +56,28 @@ static void np_unlock(np_client_t client)
56} 56}
57 57
58/** 58/**
59 * Sends an xml plist to the device using the connection specified in client. 59 * Convert an iphone_error_t value to an np_error_t value.
60 * This function is only used internally. 60 * Used internally to get correct error codes when using plist helper
61 * functions.
61 * 62 *
62 * @param client NP to send data to 63 * @param err An iphone_error_t error code
63 * @param dict plist to send
64 * 64 *
65 * @return NP_E_SUCCESS on success, NP_E_INVALID_ARG when client or dict 65 * @return A matching np_error_t error code,
66 * are NULL, NP_E_PLIST_ERROR when dict is not a valid plist, 66 * NP_E_UNKNOWN_ERROR otherwise.
67 * or NP_E_UNKNOWN_ERROR when an unspecified error occurs.
68 */ 67 */
69static np_error_t np_plist_send(np_client_t client, plist_t dict) 68static np_error_t iphone_to_np_error(iphone_error_t err)
70{ 69{
71 char *XML_content = NULL; 70 switch (err) {
72 uint32_t length = 0; 71 case IPHONE_E_SUCCESS:
73 uint32_t nlen = 0; 72 return NP_E_SUCCESS;
74 int bytes = 0; 73 case IPHONE_E_INVALID_ARG:
75 np_error_t res = NP_E_UNKNOWN_ERROR; 74 return NP_E_INVALID_ARG;
76 75 case IPHONE_E_PLIST_ERROR:
77 if (!client || !dict) { 76 return NP_E_PLIST_ERROR;
78 return NP_E_INVALID_ARG; 77 default:
79 } 78 break;
80
81 plist_to_xml(dict, &XML_content, &length);
82
83 if (!XML_content || length == 0) {
84 return NP_E_PLIST_ERROR;
85 }
86
87 nlen = htonl(length);
88 iphone_device_send(client->connection, (const char*)&nlen, sizeof(nlen), (uint32_t*)&bytes);
89 if (bytes == sizeof(nlen)) {
90 iphone_device_send(client->connection, XML_content, length, (uint32_t*)&bytes);
91 if (bytes > 0) {
92 if ((uint32_t)bytes == length) {
93 res = NP_E_SUCCESS;
94 } else {
95 log_debug_msg("%s: ERROR: Could not send all data (%d of %d)!\n", __func__, bytes, length);
96 }
97 }
98 }
99 if (bytes <= 0) {
100 log_debug_msg("%s: ERROR: sending to device failed.\n", __func__);
101 } 79 }
102 80 return NP_E_UNKNOWN_ERROR;
103 free(XML_content);
104
105 return res;
106} 81}
107 82
108/** Makes a connection to the NP service on the phone. 83/** Makes a connection to the NP service on the phone.
@@ -189,13 +164,13 @@ np_error_t np_post_notification(np_client_t client, const char *notification)
189 plist_dict_insert_item(dict,"Command", plist_new_string("PostNotification")); 164 plist_dict_insert_item(dict,"Command", plist_new_string("PostNotification"));
190 plist_dict_insert_item(dict,"Name", plist_new_string(notification)); 165 plist_dict_insert_item(dict,"Name", plist_new_string(notification));
191 166
192 np_error_t res = np_plist_send(client, dict); 167 np_error_t res = iphone_to_np_error(iphone_device_send_xml_plist(client->connection, dict));
193 plist_free(dict); 168 plist_free(dict);
194 169
195 dict = plist_new_dict(); 170 dict = plist_new_dict();
196 plist_dict_insert_item(dict,"Command", plist_new_string("Shutdown")); 171 plist_dict_insert_item(dict,"Command", plist_new_string("Shutdown"));
197 172
198 res = np_plist_send(client, dict); 173 res = iphone_to_np_error(iphone_device_send_xml_plist(client->connection, dict));
199 plist_free(dict); 174 plist_free(dict);
200 175
201 if (res != NP_E_SUCCESS) { 176 if (res != NP_E_SUCCESS) {
@@ -225,7 +200,7 @@ np_error_t np_observe_notification( np_client_t client, const char *notification
225 plist_dict_insert_item(dict,"Command", plist_new_string("ObserveNotification")); 200 plist_dict_insert_item(dict,"Command", plist_new_string("ObserveNotification"));
226 plist_dict_insert_item(dict,"Name", plist_new_string(notification)); 201 plist_dict_insert_item(dict,"Name", plist_new_string(notification));
227 202
228 np_error_t res = np_plist_send(client, dict); 203 np_error_t res = iphone_to_np_error(iphone_device_send_xml_plist(client->connection, dict));
229 if (res != NP_E_SUCCESS) { 204 if (res != NP_E_SUCCESS) {
230 log_debug_msg("%s: Error sending XML plist to device!\n", __func__); 205 log_debug_msg("%s: Error sending XML plist to device!\n", __func__);
231 } 206 }
@@ -297,10 +272,7 @@ np_error_t np_observe_notifications(np_client_t client, const char **notificatio
297 */ 272 */
298static int np_get_notification(np_client_t client, char **notification) 273static int np_get_notification(np_client_t client, char **notification)
299{ 274{
300 uint32_t bytes = 0;
301 int res = 0; 275 int res = 0;
302 uint32_t pktlen = 0;
303 char *XML_content = NULL;
304 plist_t dict = NULL; 276 plist_t dict = NULL;
305 277
306 if (!client || !client->connection || *notification) 278 if (!client || !client->connection || *notification)
@@ -308,72 +280,46 @@ static int np_get_notification(np_client_t client, char **notification)
308 280
309 np_lock(client); 281 np_lock(client);
310 282
311 iphone_device_recv_timeout(client->connection, (char*)&pktlen, sizeof(pktlen), &bytes, 500); 283 iphone_device_receive_plist_with_timeout(client->connection, &dict, 500);
312 log_debug_msg("NotificationProxy: initial read=%i\n", bytes); 284 if (!dict) {
313 if (bytes < 4) {
314 log_debug_msg("NotificationProxy: no notification received!\n"); 285 log_debug_msg("NotificationProxy: no notification received!\n");
315 res = 0; 286 res = 0;
316 } else { 287 } else {
317 if ((char)pktlen == 0) { 288 char *cmd_value = NULL;
318 pktlen = ntohl(pktlen); 289 plist_t cmd_value_node = plist_dict_get_item(dict, "Command");
319 log_debug_msg("NotificationProxy: %d bytes following\n", pktlen); 290
320 XML_content = (char*)malloc(pktlen); 291 if (plist_get_node_type(cmd_value_node) == PLIST_STRING) {
321 log_debug_msg("pointer %p\n", XML_content); 292 plist_get_string_val(cmd_value_node, &cmd_value);
322 293 }
323 iphone_device_recv_timeout(client->connection, XML_content, pktlen, &bytes, 1000); 294
324 if (bytes <= 0) { 295 if (cmd_value && !strcmp(cmd_value, "RelayNotification")) {
325 res = -1; 296 char *name_value = NULL;
326 } else { 297 plist_t name_value_node = plist_dict_get_item(dict, "Name");
327 log_debug_msg("NotificationProxy: received data:\n"); 298
328 log_debug_buffer(XML_content, pktlen); 299 if (plist_get_node_type(name_value_node) == PLIST_STRING) {
329 300 plist_get_string_val(name_value_node, &name_value);
330 plist_from_xml(XML_content, bytes, &dict);
331 if (!dict) {
332 np_unlock(client);
333 return -2;
334 }
335
336 char *cmd_value = NULL;
337 plist_t cmd_value_node = plist_dict_get_item(dict, "Command");
338
339 if (plist_get_node_type(cmd_value_node) == PLIST_STRING) {
340 plist_get_string_val(cmd_value_node, &cmd_value);
341 }
342
343 if (cmd_value && !strcmp(cmd_value, "RelayNotification")) {
344 char *name_value = NULL;
345 plist_t name_value_node = plist_dict_get_item(dict, "Name");
346
347 if (plist_get_node_type(name_value_node) == PLIST_STRING) {
348 plist_get_string_val(name_value_node, &name_value);
349 }
350
351 res = -2;
352 if (name_value_node && name_value) {
353 *notification = name_value;
354 log_debug_msg("%s: got notification %s\n", __func__, name_value);
355 res = 0;
356 }
357 } else if (cmd_value && !strcmp(cmd_value, "ProxyDeath")) {
358 log_debug_msg("%s: ERROR: NotificationProxy died!\n", __func__);
359 res = -1;
360 } else if (cmd_value) {
361 log_debug_msg("%d: unknown NotificationProxy command '%s' received!\n", __func__);
362 res = -1;
363 } else {
364 res = -2;
365 }
366 if (cmd_value) {
367 free(cmd_value);
368 }
369 plist_free(dict);
370 dict = NULL;
371 free(XML_content);
372 XML_content = NULL;
373 } 301 }
374 } else { 302
303 res = -2;
304 if (name_value_node && name_value) {
305 *notification = name_value;
306 log_debug_msg("%s: got notification %s\n", __func__, name_value);
307 res = 0;
308 }
309 } else if (cmd_value && !strcmp(cmd_value, "ProxyDeath")) {
310 log_debug_msg("%s: ERROR: NotificationProxy died!\n", __func__);
375 res = -1; 311 res = -1;
312 } else if (cmd_value) {
313 log_debug_msg("%d: unknown NotificationProxy command '%s' received!\n", __func__);
314 res = -1;
315 } else {
316 res = -2;
317 }
318 if (cmd_value) {
319 free(cmd_value);
376 } 320 }
321 plist_free(dict);
322 dict = NULL;
377 } 323 }
378 324
379 np_unlock(client); 325 np_unlock(client);