diff options
Diffstat (limited to 'src/NotificationProxy.c')
| -rw-r--r-- | src/NotificationProxy.c | 162 |
1 files changed, 54 insertions, 108 deletions
diff --git a/src/NotificationProxy.c b/src/NotificationProxy.c index cba12b9..b73b521 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 | */ |
| 69 | static np_error_t np_plist_send(np_client_t client, plist_t dict) | 68 | static 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. |
| @@ -185,13 +160,13 @@ np_error_t np_post_notification(np_client_t client, const char *notification) | |||
| 185 | plist_dict_insert_item(dict,"Command", plist_new_string("PostNotification")); | 160 | plist_dict_insert_item(dict,"Command", plist_new_string("PostNotification")); |
| 186 | plist_dict_insert_item(dict,"Name", plist_new_string(notification)); | 161 | plist_dict_insert_item(dict,"Name", plist_new_string(notification)); |
| 187 | 162 | ||
| 188 | np_error_t res = np_plist_send(client, dict); | 163 | np_error_t res = iphone_to_np_error(iphone_device_send_xml_plist(client->connection, dict)); |
| 189 | plist_free(dict); | 164 | plist_free(dict); |
| 190 | 165 | ||
| 191 | dict = plist_new_dict(); | 166 | dict = plist_new_dict(); |
| 192 | plist_dict_insert_item(dict,"Command", plist_new_string("Shutdown")); | 167 | plist_dict_insert_item(dict,"Command", plist_new_string("Shutdown")); |
| 193 | 168 | ||
| 194 | res = np_plist_send(client, dict); | 169 | res = iphone_to_np_error(iphone_device_send_xml_plist(client->connection, dict)); |
| 195 | plist_free(dict); | 170 | plist_free(dict); |
| 196 | 171 | ||
| 197 | if (res != NP_E_SUCCESS) { | 172 | if (res != NP_E_SUCCESS) { |
| @@ -221,7 +196,7 @@ np_error_t np_observe_notification( np_client_t client, const char *notification | |||
| 221 | plist_dict_insert_item(dict,"Command", plist_new_string("ObserveNotification")); | 196 | plist_dict_insert_item(dict,"Command", plist_new_string("ObserveNotification")); |
| 222 | plist_dict_insert_item(dict,"Name", plist_new_string(notification)); | 197 | plist_dict_insert_item(dict,"Name", plist_new_string(notification)); |
| 223 | 198 | ||
| 224 | np_error_t res = np_plist_send(client, dict); | 199 | np_error_t res = iphone_to_np_error(iphone_device_send_xml_plist(client->connection, dict)); |
| 225 | if (res != NP_E_SUCCESS) { | 200 | if (res != NP_E_SUCCESS) { |
| 226 | log_debug_msg("%s: Error sending XML plist to device!\n", __func__); | 201 | log_debug_msg("%s: Error sending XML plist to device!\n", __func__); |
| 227 | } | 202 | } |
| @@ -281,10 +256,7 @@ np_error_t np_observe_notifications(np_client_t client, const char **notificatio | |||
| 281 | */ | 256 | */ |
| 282 | static int np_get_notification(np_client_t client, char **notification) | 257 | static int np_get_notification(np_client_t client, char **notification) |
| 283 | { | 258 | { |
| 284 | uint32_t bytes = 0; | ||
| 285 | int res = 0; | 259 | int res = 0; |
| 286 | uint32_t pktlen = 0; | ||
| 287 | char *XML_content = NULL; | ||
| 288 | plist_t dict = NULL; | 260 | plist_t dict = NULL; |
| 289 | 261 | ||
| 290 | if (!client || !client->connection || *notification) | 262 | if (!client || !client->connection || *notification) |
| @@ -292,72 +264,46 @@ static int np_get_notification(np_client_t client, char **notification) | |||
| 292 | 264 | ||
| 293 | np_lock(client); | 265 | np_lock(client); |
| 294 | 266 | ||
| 295 | iphone_device_recv_timeout(client->connection, (char*)&pktlen, sizeof(pktlen), &bytes, 500); | 267 | iphone_device_receive_plist_with_timeout(client->connection, &dict, 500); |
| 296 | log_debug_msg("NotificationProxy: initial read=%i\n", bytes); | 268 | if (!dict) { |
| 297 | if (bytes < 4) { | ||
| 298 | log_debug_msg("NotificationProxy: no notification received!\n"); | 269 | log_debug_msg("NotificationProxy: no notification received!\n"); |
| 299 | res = 0; | 270 | res = 0; |
| 300 | } else { | 271 | } else { |
| 301 | if ((char)pktlen == 0) { | 272 | char *cmd_value = NULL; |
| 302 | pktlen = ntohl(pktlen); | 273 | plist_t cmd_value_node = plist_dict_get_item(dict, "Command"); |
| 303 | log_debug_msg("NotificationProxy: %d bytes following\n", pktlen); | 274 | |
| 304 | XML_content = (char*)malloc(pktlen); | 275 | if (plist_get_node_type(cmd_value_node) == PLIST_STRING) { |
| 305 | log_debug_msg("pointer %p\n", XML_content); | 276 | plist_get_string_val(cmd_value_node, &cmd_value); |
| 306 | 277 | } | |
| 307 | iphone_device_recv_timeout(client->connection, XML_content, pktlen, &bytes, 1000); | 278 | |
| 308 | if (bytes <= 0) { | 279 | if (cmd_value && !strcmp(cmd_value, "RelayNotification")) { |
| 309 | res = -1; | 280 | char *name_value = NULL; |
| 310 | } else { | 281 | plist_t name_value_node = plist_dict_get_item(dict, "Name"); |
| 311 | log_debug_msg("NotificationProxy: received data:\n"); | 282 | |
| 312 | log_debug_buffer(XML_content, pktlen); | 283 | if (plist_get_node_type(name_value_node) == PLIST_STRING) { |
| 313 | 284 | plist_get_string_val(name_value_node, &name_value); | |
| 314 | plist_from_xml(XML_content, bytes, &dict); | ||
| 315 | if (!dict) { | ||
| 316 | np_unlock(client); | ||
| 317 | return -2; | ||
| 318 | } | ||
| 319 | |||
| 320 | char *cmd_value = NULL; | ||
| 321 | plist_t cmd_value_node = plist_dict_get_item(dict, "Command"); | ||
| 322 | |||
| 323 | if (plist_get_node_type(cmd_value_node) == PLIST_STRING) { | ||
| 324 | plist_get_string_val(cmd_value_node, &cmd_value); | ||
| 325 | } | ||
| 326 | |||
| 327 | if (cmd_value && !strcmp(cmd_value, "RelayNotification")) { | ||
| 328 | char *name_value = NULL; | ||
| 329 | plist_t name_value_node = plist_dict_get_item(dict, "Name"); | ||
| 330 | |||
| 331 | if (plist_get_node_type(name_value_node) == PLIST_STRING) { | ||
| 332 | plist_get_string_val(name_value_node, &name_value); | ||
| 333 | } | ||
| 334 | |||
| 335 | res = -2; | ||
| 336 | if (name_value_node && name_value) { | ||
| 337 | *notification = name_value; | ||
| 338 | log_debug_msg("%s: got notification %s\n", __func__, name_value); | ||
| 339 | res = 0; | ||
| 340 | } | ||
| 341 | } else if (cmd_value && !strcmp(cmd_value, "ProxyDeath")) { | ||
| 342 | log_debug_msg("%s: ERROR: NotificationProxy died!\n", __func__); | ||
| 343 | res = -1; | ||
| 344 | } else if (cmd_value) { | ||
| 345 | log_debug_msg("%d: unknown NotificationProxy command '%s' received!\n", __func__); | ||
| 346 | res = -1; | ||
| 347 | } else { | ||
| 348 | res = -2; | ||
| 349 | } | ||
| 350 | if (cmd_value) { | ||
| 351 | free(cmd_value); | ||
| 352 | } | ||
| 353 | plist_free(dict); | ||
| 354 | dict = NULL; | ||
| 355 | free(XML_content); | ||
| 356 | XML_content = NULL; | ||
| 357 | } | 285 | } |
| 358 | } else { | 286 | |
| 287 | res = -2; | ||
| 288 | if (name_value_node && name_value) { | ||
| 289 | *notification = name_value; | ||
| 290 | log_debug_msg("%s: got notification %s\n", __func__, name_value); | ||
| 291 | res = 0; | ||
| 292 | } | ||
| 293 | } else if (cmd_value && !strcmp(cmd_value, "ProxyDeath")) { | ||
| 294 | log_debug_msg("%s: ERROR: NotificationProxy died!\n", __func__); | ||
| 359 | res = -1; | 295 | res = -1; |
| 296 | } else if (cmd_value) { | ||
| 297 | log_debug_msg("%d: unknown NotificationProxy command '%s' received!\n", __func__); | ||
| 298 | res = -1; | ||
| 299 | } else { | ||
| 300 | res = -2; | ||
| 301 | } | ||
| 302 | if (cmd_value) { | ||
| 303 | free(cmd_value); | ||
| 360 | } | 304 | } |
| 305 | plist_free(dict); | ||
| 306 | dict = NULL; | ||
| 361 | } | 307 | } |
| 362 | 308 | ||
| 363 | np_unlock(client); | 309 | np_unlock(client); |
