diff options
| author | 2010-06-03 02:18:48 +0200 | |
|---|---|---|
| committer | 2010-06-03 02:18:48 +0200 | |
| commit | 43964b02b5c32ead9d69f3eafc5f22700d0d5436 (patch) | |
| tree | 9e32dc54fe61c22e43f41cd9498d700ac9501b9a /src | |
| parent | 6f70b0a769b78e6b3e121316d302f2a0280fa6b1 (diff) | |
| download | libimobiledevice-43964b02b5c32ead9d69f3eafc5f22700d0d5436.tar.gz libimobiledevice-43964b02b5c32ead9d69f3eafc5f22700d0d5436.tar.bz2 | |
mobilebackup: add internal mobilebackup_send_message() for cleaner code
Again, does not clean up too much, but it'll be helpful when
completing the restore part of the mobilebackup API.
Diffstat (limited to 'src')
| -rw-r--r-- | src/mobilebackup.c | 69 |
1 files changed, 49 insertions, 20 deletions
diff --git a/src/mobilebackup.c b/src/mobilebackup.c index 234ad8f..d71a494 100644 --- a/src/mobilebackup.c +++ b/src/mobilebackup.c | |||
| @@ -152,6 +152,50 @@ mobilebackup_error_t mobilebackup_send(mobilebackup_client_t client, plist_t pli | |||
| 152 | } | 152 | } |
| 153 | 153 | ||
| 154 | /** | 154 | /** |
| 155 | * Sends a backup message plist. | ||
| 156 | * | ||
| 157 | * @param client The connected MobileBackup client to use. | ||
| 158 | * @param message The message to send. This will be inserted into the request | ||
| 159 | * plist as value for BackupMessageTypeKey. If this parameter is NULL, | ||
| 160 | * the plist passed in the options parameter will be sent directly. | ||
| 161 | * @param options Additional options as PLIST_DICT to add to the request. | ||
| 162 | * The BackupMessageTypeKey with the value passed in the message parameter | ||
| 163 | * will be inserted into this plist before sending it. This parameter | ||
| 164 | * can be NULL if message is not NULL. | ||
| 165 | */ | ||
| 166 | static mobilebackup_error_t mobilebackup_send_message(mobilebackup_client_t client, const char *message, plist_t options) | ||
| 167 | { | ||
| 168 | if (!client || !client->parent || (!message && !options)) | ||
| 169 | return MOBILEBACKUP_E_INVALID_ARG; | ||
| 170 | |||
| 171 | if (options && (plist_get_node_type(options) != PLIST_DICT)) { | ||
| 172 | return MOBILEBACKUP_E_INVALID_ARG; | ||
| 173 | } | ||
| 174 | |||
| 175 | mobilebackup_error_t err; | ||
| 176 | |||
| 177 | if (message) { | ||
| 178 | plist_t dict = NULL; | ||
| 179 | if (options) { | ||
| 180 | dict = plist_copy(options); | ||
| 181 | } else { | ||
| 182 | dict = plist_new_dict(); | ||
| 183 | } | ||
| 184 | plist_dict_insert_item(dict, "BackupMessageTypeKey", plist_new_string(message)); | ||
| 185 | |||
| 186 | /* send it as DLMessageProcessMessage */ | ||
| 187 | err = mobilebackup_error(device_link_service_send_process_message(client->parent, dict)); | ||
| 188 | plist_free(dict); | ||
| 189 | } else { | ||
| 190 | err = mobilebackup_error(device_link_service_send_process_message(client->parent, options)); | ||
| 191 | } | ||
| 192 | if (err != MOBILEBACKUP_E_SUCCESS) { | ||
| 193 | debug_info("ERROR: Could not send message '%s' (%d)!", message, err); | ||
| 194 | } | ||
| 195 | return err; | ||
| 196 | } | ||
| 197 | |||
| 198 | /** | ||
| 155 | * Receives a plist from the device and checks if the value for the | 199 | * Receives a plist from the device and checks if the value for the |
| 156 | * BackupMessageTypeKey matches the value passed in the message parameter. | 200 | * BackupMessageTypeKey matches the value passed in the message parameter. |
| 157 | * | 201 | * |
| @@ -252,8 +296,8 @@ mobilebackup_error_t mobilebackup_request_backup(mobilebackup_client_t client, p | |||
| 252 | plist_dict_insert_item(dict, "BackupMessageTypeKey", plist_new_string("BackupMessageBackupRequest")); | 296 | plist_dict_insert_item(dict, "BackupMessageTypeKey", plist_new_string("BackupMessageBackupRequest")); |
| 253 | plist_dict_insert_item(dict, "BackupProtocolVersion", plist_new_string(proto_version)); | 297 | plist_dict_insert_item(dict, "BackupProtocolVersion", plist_new_string(proto_version)); |
| 254 | 298 | ||
| 255 | /* send it as DLMessageProcessMessage */ | 299 | /* send request */ |
| 256 | err = mobilebackup_error(device_link_service_send_process_message(client->parent, dict)); | 300 | err = mobilebackup_send_message(client, NULL, dict); |
| 257 | plist_free(dict); | 301 | plist_free(dict); |
| 258 | dict = NULL; | 302 | dict = NULL; |
| 259 | if (err != MOBILEBACKUP_E_SUCCESS) { | 303 | if (err != MOBILEBACKUP_E_SUCCESS) { |
| @@ -283,7 +327,7 @@ mobilebackup_error_t mobilebackup_request_backup(mobilebackup_client_t client, p | |||
| 283 | goto leave; | 327 | goto leave; |
| 284 | 328 | ||
| 285 | /* BackupMessageBackupReplyOK received, send it back */ | 329 | /* BackupMessageBackupReplyOK received, send it back */ |
| 286 | err = mobilebackup_error(device_link_service_send_process_message(client->parent, dict)); | 330 | err = mobilebackup_send_message(client, NULL, dict); |
| 287 | if (err != MOBILEBACKUP_E_SUCCESS) { | 331 | if (err != MOBILEBACKUP_E_SUCCESS) { |
| 288 | debug_info("ERROR: Could not send BackupReplyOK ACK (%d)", err); | 332 | debug_info("ERROR: Could not send BackupReplyOK ACK (%d)", err); |
| 289 | } | 333 | } |
| @@ -305,20 +349,7 @@ leave: | |||
| 305 | */ | 349 | */ |
| 306 | mobilebackup_error_t mobilebackup_send_backup_file_received(mobilebackup_client_t client) | 350 | mobilebackup_error_t mobilebackup_send_backup_file_received(mobilebackup_client_t client) |
| 307 | { | 351 | { |
| 308 | if (!client || !client->parent) | 352 | return mobilebackup_send_message(client, "kBackupMessageBackupFileReceived", NULL); |
| 309 | return MOBILEBACKUP_E_INVALID_ARG; | ||
| 310 | |||
| 311 | mobilebackup_error_t err; | ||
| 312 | |||
| 313 | /* construct ACK plist */ | ||
| 314 | plist_t dict = plist_new_dict(); | ||
| 315 | plist_dict_insert_item(dict, "BackupMessageTypeKey", plist_new_string("kBackupMessageBackupFileReceived")); | ||
| 316 | |||
| 317 | /* send it as DLMessageProcessMessage */ | ||
| 318 | err = mobilebackup_error(device_link_service_send_process_message(client->parent, dict)); | ||
| 319 | plist_free(dict); | ||
| 320 | |||
| 321 | return err; | ||
| 322 | } | 353 | } |
| 323 | 354 | ||
| 324 | /** | 355 | /** |
| @@ -340,11 +371,9 @@ mobilebackup_error_t mobilebackup_send_error(mobilebackup_client_t client, const | |||
| 340 | 371 | ||
| 341 | /* construct error plist */ | 372 | /* construct error plist */ |
| 342 | plist_t dict = plist_new_dict(); | 373 | plist_t dict = plist_new_dict(); |
| 343 | plist_dict_insert_item(dict, "BackupMessageTypeKey", plist_new_string("BackupMessageError")); | ||
| 344 | plist_dict_insert_item(dict, "BackupErrorReasonKey", plist_new_string(reason)); | 374 | plist_dict_insert_item(dict, "BackupErrorReasonKey", plist_new_string(reason)); |
| 345 | 375 | ||
| 346 | /* send it as DLMessageProcessMessage */ | 376 | err = mobilebackup_send_message(client, "BackupMessageError", dict); |
| 347 | err = mobilebackup_error(device_link_service_send_process_message(client->parent, dict)); | ||
| 348 | plist_free(dict); | 377 | plist_free(dict); |
| 349 | 378 | ||
| 350 | return err; | 379 | return err; |
