| 1 |
/* |
| 2 |
* mobilesync.c |
| 3 |
* Contains functions for the built-in MobileSync client. |
| 4 |
* |
| 5 |
* Copyright (c) 2010 Bryan Forbes All Rights Reserved. |
| 6 |
* Copyright (c) 2009 Jonathan Beck All Rights Reserved. |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Lesser General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2.1 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with this library; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
#define _GNU_SOURCE 1 |
| 27 |
#define __USE_GNU 1 |
| 28 |
#include <plist/plist.h> |
| 29 |
#include <string.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <stdio.h> |
| 32 |
|
| 33 |
#include "mobilesync.h" |
| 34 |
#include "device_link_service.h" |
| 35 |
#include "common/debug.h" |
| 36 |
|
| 37 |
#define MSYNC_VERSION_INT1 400 |
| 38 |
#define MSYNC_VERSION_INT2 100 |
| 39 |
|
| 40 |
#define EMPTY_PARAMETER_STRING "___EmptyParameterString___" |
| 41 |
|
| 42 |
/** |
| 43 |
* Convert an #device_link_service_error_t value to an #mobilesync_error_t value. |
| 44 |
* Used internally to get correct error codes when using device_link_service stuff. |
| 45 |
* |
| 46 |
* @param err A #device_link_service_error_t error code |
| 47 |
* |
| 48 |
* @return A matching #mobilesync_error_t error code, |
| 49 |
* MOBILESYNC_E_UNKNOWN_ERROR otherwise. |
| 50 |
*/ |
| 51 |
static mobilesync_error_t mobilesync_error(device_link_service_error_t err) |
| 52 |
{ |
| 53 |
switch (err) { |
| 54 |
case DEVICE_LINK_SERVICE_E_SUCCESS: |
| 55 |
return MOBILESYNC_E_SUCCESS; |
| 56 |
case DEVICE_LINK_SERVICE_E_INVALID_ARG: |
| 57 |
return MOBILESYNC_E_INVALID_ARG; |
| 58 |
case DEVICE_LINK_SERVICE_E_PLIST_ERROR: |
| 59 |
return MOBILESYNC_E_PLIST_ERROR; |
| 60 |
case DEVICE_LINK_SERVICE_E_MUX_ERROR: |
| 61 |
return MOBILESYNC_E_MUX_ERROR; |
| 62 |
case DEVICE_LINK_SERVICE_E_SSL_ERROR: |
| 63 |
return MOBILESYNC_E_SSL_ERROR; |
| 64 |
case DEVICE_LINK_SERVICE_E_RECEIVE_TIMEOUT: |
| 65 |
return MOBILESYNC_E_RECEIVE_TIMEOUT; |
| 66 |
case DEVICE_LINK_SERVICE_E_BAD_VERSION: |
| 67 |
return MOBILESYNC_E_BAD_VERSION; |
| 68 |
default: |
| 69 |
break; |
| 70 |
} |
| 71 |
return MOBILESYNC_E_UNKNOWN_ERROR; |
| 72 |
} |
| 73 |
|
| 74 |
mobilesync_error_t mobilesync_client_new(idevice_t device, lockdownd_service_descriptor_t service, |
| 75 |
mobilesync_client_t * client) |
| 76 |
{ |
| 77 |
if (!device || !service || service->port == 0 || !client || *client) |
| 78 |
return MOBILESYNC_E_INVALID_ARG; |
| 79 |
|
| 80 |
device_link_service_client_t dlclient = NULL; |
| 81 |
mobilesync_error_t ret = mobilesync_error(device_link_service_client_new(device, service, &dlclient)); |
| 82 |
if (ret != MOBILESYNC_E_SUCCESS) { |
| 83 |
return ret; |
| 84 |
} |
| 85 |
|
| 86 |
mobilesync_client_t client_loc = (mobilesync_client_t) malloc(sizeof(struct mobilesync_client_private)); |
| 87 |
client_loc->parent = dlclient; |
| 88 |
client_loc->direction = MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER; |
| 89 |
client_loc->data_class = NULL; |
| 90 |
|
| 91 |
/* perform handshake */ |
| 92 |
ret = mobilesync_error(device_link_service_version_exchange(dlclient, MSYNC_VERSION_INT1, MSYNC_VERSION_INT2)); |
| 93 |
if (ret != MOBILESYNC_E_SUCCESS) { |
| 94 |
debug_info("version exchange failed, error %d", ret); |
| 95 |
mobilesync_client_free(client_loc); |
| 96 |
return ret; |
| 97 |
} |
| 98 |
|
| 99 |
*client = client_loc; |
| 100 |
|
| 101 |
return ret; |
| 102 |
} |
| 103 |
|
| 104 |
mobilesync_error_t mobilesync_client_start_service(idevice_t device, mobilesync_client_t * client, const char* label) |
| 105 |
{ |
| 106 |
mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR; |
| 107 |
service_client_factory_start_service(device, MOBILESYNC_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(mobilesync_client_new), &err); |
| 108 |
return err; |
| 109 |
} |
| 110 |
|
| 111 |
mobilesync_error_t mobilesync_client_free(mobilesync_client_t client) |
| 112 |
{ |
| 113 |
if (!client) |
| 114 |
return MOBILESYNC_E_INVALID_ARG; |
| 115 |
device_link_service_disconnect(client->parent, "All done, thanks for the memories"); |
| 116 |
mobilesync_error_t err = mobilesync_error(device_link_service_client_free(client->parent)); |
| 117 |
free(client); |
| 118 |
return err; |
| 119 |
} |
| 120 |
|
| 121 |
mobilesync_error_t mobilesync_receive(mobilesync_client_t client, plist_t * plist) |
| 122 |
{ |
| 123 |
if (!client) |
| 124 |
return MOBILESYNC_E_INVALID_ARG; |
| 125 |
mobilesync_error_t ret = mobilesync_error(device_link_service_receive(client->parent, plist)); |
| 126 |
return ret; |
| 127 |
} |
| 128 |
|
| 129 |
mobilesync_error_t mobilesync_send(mobilesync_client_t client, plist_t plist) |
| 130 |
{ |
| 131 |
if (!client || !plist) |
| 132 |
return MOBILESYNC_E_INVALID_ARG; |
| 133 |
return mobilesync_error(device_link_service_send(client->parent, plist)); |
| 134 |
} |
| 135 |
|
| 136 |
mobilesync_error_t mobilesync_start(mobilesync_client_t client, const char *data_class, mobilesync_anchors_t anchors, uint64_t computer_data_class_version, mobilesync_sync_type_t *sync_type, uint64_t *device_data_class_version, char** error_description) |
| 137 |
{ |
| 138 |
if (!client || client->data_class || !data_class || |
| 139 |
!anchors || !anchors->computer_anchor) { |
| 140 |
return MOBILESYNC_E_INVALID_ARG; |
| 141 |
} |
| 142 |
|
| 143 |
mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR; |
| 144 |
char *response_type = NULL; |
| 145 |
char *sync_type_str = NULL; |
| 146 |
plist_t msg = NULL; |
| 147 |
plist_t response_type_node = NULL; |
| 148 |
|
| 149 |
*error_description = NULL; |
| 150 |
|
| 151 |
msg = plist_new_array(); |
| 152 |
plist_array_append_item(msg, plist_new_string("SDMessageSyncDataClassWithDevice")); |
| 153 |
plist_array_append_item(msg, plist_new_string(data_class)); |
| 154 |
if (anchors->device_anchor) { |
| 155 |
plist_array_append_item(msg, plist_new_string(anchors->device_anchor)); |
| 156 |
} else { |
| 157 |
plist_array_append_item(msg, plist_new_string("---")); |
| 158 |
} |
| 159 |
plist_array_append_item(msg, plist_new_string(anchors->computer_anchor)); |
| 160 |
plist_array_append_item(msg, plist_new_uint(computer_data_class_version)); |
| 161 |
plist_array_append_item(msg, plist_new_string(EMPTY_PARAMETER_STRING)); |
| 162 |
|
| 163 |
err = mobilesync_send(client, msg); |
| 164 |
|
| 165 |
if (err != MOBILESYNC_E_SUCCESS) { |
| 166 |
goto out; |
| 167 |
} |
| 168 |
|
| 169 |
plist_free(msg); |
| 170 |
msg = NULL; |
| 171 |
|
| 172 |
err = mobilesync_receive(client, &msg); |
| 173 |
|
| 174 |
if (err != MOBILESYNC_E_SUCCESS) { |
| 175 |
goto out; |
| 176 |
} |
| 177 |
|
| 178 |
response_type_node = plist_array_get_item(msg, 0); |
| 179 |
if (!response_type_node) { |
| 180 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 181 |
goto out; |
| 182 |
} |
| 183 |
|
| 184 |
plist_get_string_val(response_type_node, &response_type); |
| 185 |
if (!response_type) { |
| 186 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 187 |
goto out; |
| 188 |
} |
| 189 |
|
| 190 |
// did the device refuse to sync with the computer? |
| 191 |
if (!strcmp(response_type, "SDMessageRefuseToSyncDataClassWithComputer")) { |
| 192 |
err = MOBILESYNC_E_SYNC_REFUSED; |
| 193 |
plist_get_string_val(plist_array_get_item(msg, 2), error_description); |
| 194 |
debug_info("Device refused sync: %s", error_description); |
| 195 |
goto out; |
| 196 |
} |
| 197 |
|
| 198 |
// did the device cancel the session? |
| 199 |
if (!strcmp(response_type, "SDMessageCancelSession")) { |
| 200 |
err = MOBILESYNC_E_CANCELLED; |
| 201 |
plist_get_string_val(plist_array_get_item(msg, 2), error_description); |
| 202 |
debug_info("Device cancelled: %s", error_description); |
| 203 |
goto out; |
| 204 |
} |
| 205 |
|
| 206 |
if (sync_type != NULL) { |
| 207 |
plist_t sync_type_node = plist_array_get_item(msg, 4); |
| 208 |
if (!sync_type_node) { |
| 209 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 210 |
goto out; |
| 211 |
} |
| 212 |
|
| 213 |
plist_get_string_val(sync_type_node, &sync_type_str); |
| 214 |
if (!sync_type_str) { |
| 215 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 216 |
goto out; |
| 217 |
} |
| 218 |
|
| 219 |
if (!strcmp(sync_type_str, "SDSyncTypeFast")) { |
| 220 |
*sync_type = MOBILESYNC_SYNC_TYPE_FAST; |
| 221 |
} else if (!strcmp(sync_type_str, "SDSyncTypeSlow")) { |
| 222 |
*sync_type = MOBILESYNC_SYNC_TYPE_SLOW; |
| 223 |
} else if (!strcmp(sync_type_str, "SDSyncTypeReset")) { |
| 224 |
*sync_type = MOBILESYNC_SYNC_TYPE_RESET; |
| 225 |
} else { |
| 226 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 227 |
goto out; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
if (device_data_class_version != NULL) { |
| 232 |
plist_t device_data_class_version_node = plist_array_get_item(msg, 5); |
| 233 |
if (!device_data_class_version_node) { |
| 234 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 235 |
goto out; |
| 236 |
} |
| 237 |
|
| 238 |
plist_get_uint_val(device_data_class_version_node, device_data_class_version); |
| 239 |
} |
| 240 |
|
| 241 |
err = MOBILESYNC_E_SUCCESS; |
| 242 |
|
| 243 |
out: |
| 244 |
if (sync_type_str) { |
| 245 |
free(sync_type_str); |
| 246 |
sync_type_str = NULL; |
| 247 |
} |
| 248 |
if (response_type) { |
| 249 |
free(response_type); |
| 250 |
response_type = NULL; |
| 251 |
} |
| 252 |
if (msg) { |
| 253 |
plist_free(msg); |
| 254 |
msg = NULL; |
| 255 |
} |
| 256 |
|
| 257 |
client->data_class = strdup(data_class); |
| 258 |
client->direction = MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER; |
| 259 |
return err; |
| 260 |
} |
| 261 |
|
| 262 |
mobilesync_error_t mobilesync_finish(mobilesync_client_t client) |
| 263 |
{ |
| 264 |
if (!client || !client->data_class) { |
| 265 |
return MOBILESYNC_E_INVALID_ARG; |
| 266 |
} |
| 267 |
|
| 268 |
mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR; |
| 269 |
|
| 270 |
plist_t msg = NULL; |
| 271 |
plist_t response_type_node = NULL; |
| 272 |
char *response_type = NULL; |
| 273 |
|
| 274 |
msg = plist_new_array(); |
| 275 |
plist_array_append_item(msg, plist_new_string("SDMessageFinishSessionOnDevice")); |
| 276 |
plist_array_append_item(msg, plist_new_string(client->data_class)); |
| 277 |
|
| 278 |
err = mobilesync_send(client, msg); |
| 279 |
|
| 280 |
if (err != MOBILESYNC_E_SUCCESS) { |
| 281 |
goto out; |
| 282 |
} |
| 283 |
|
| 284 |
plist_free(msg); |
| 285 |
msg = NULL; |
| 286 |
|
| 287 |
err = mobilesync_receive(client, &msg); |
| 288 |
|
| 289 |
if (err != MOBILESYNC_E_SUCCESS) { |
| 290 |
goto out; |
| 291 |
} |
| 292 |
|
| 293 |
response_type_node = plist_array_get_item(msg, 0); |
| 294 |
if (!response_type_node) { |
| 295 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 296 |
goto out; |
| 297 |
} |
| 298 |
|
| 299 |
plist_get_string_val(response_type_node, &response_type); |
| 300 |
if (!response_type) { |
| 301 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 302 |
goto out; |
| 303 |
} |
| 304 |
|
| 305 |
if (!strcmp(response_type, "SDMessageDeviceFinishedSession")) { |
| 306 |
err = MOBILESYNC_E_SUCCESS; |
| 307 |
} |
| 308 |
|
| 309 |
out: |
| 310 |
if (response_type) { |
| 311 |
free(response_type); |
| 312 |
response_type = NULL; |
| 313 |
} |
| 314 |
if (msg) { |
| 315 |
plist_free(msg); |
| 316 |
msg = NULL; |
| 317 |
} |
| 318 |
|
| 319 |
free(client->data_class); |
| 320 |
client->data_class = NULL; |
| 321 |
client->direction = MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER; |
| 322 |
return err; |
| 323 |
} |
| 324 |
|
| 325 |
static mobilesync_error_t mobilesync_get_records(mobilesync_client_t client, const char *operation) |
| 326 |
{ |
| 327 |
if (!client || !client->data_class || !operation) { |
| 328 |
return MOBILESYNC_E_INVALID_ARG; |
| 329 |
} |
| 330 |
|
| 331 |
mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR; |
| 332 |
plist_t msg = NULL; |
| 333 |
|
| 334 |
msg = plist_new_array(); |
| 335 |
plist_array_append_item(msg, plist_new_string(operation)); |
| 336 |
plist_array_append_item(msg, plist_new_string(client->data_class)); |
| 337 |
|
| 338 |
err = mobilesync_send(client, msg); |
| 339 |
|
| 340 |
if (msg) { |
| 341 |
plist_free(msg); |
| 342 |
msg = NULL; |
| 343 |
} |
| 344 |
return err; |
| 345 |
} |
| 346 |
|
| 347 |
mobilesync_error_t mobilesync_get_all_records_from_device(mobilesync_client_t client) |
| 348 |
{ |
| 349 |
return mobilesync_get_records(client, "SDMessageGetAllRecordsFromDevice"); |
| 350 |
} |
| 351 |
|
| 352 |
mobilesync_error_t mobilesync_get_changes_from_device(mobilesync_client_t client) |
| 353 |
{ |
| 354 |
return mobilesync_get_records(client, "SDMessageGetChangesFromDevice"); |
| 355 |
} |
| 356 |
|
| 357 |
mobilesync_error_t mobilesync_receive_changes(mobilesync_client_t client, plist_t *entities, uint8_t *is_last_record, plist_t *actions) |
| 358 |
{ |
| 359 |
if (!client || !client->data_class) { |
| 360 |
return MOBILESYNC_E_INVALID_ARG; |
| 361 |
} |
| 362 |
|
| 363 |
plist_t msg = NULL; |
| 364 |
plist_t response_type_node = NULL; |
| 365 |
plist_t actions_node = NULL; |
| 366 |
char *response_type = NULL; |
| 367 |
uint8_t has_more_changes = 0; |
| 368 |
|
| 369 |
mobilesync_error_t err = mobilesync_receive(client, &msg); |
| 370 |
if (err != MOBILESYNC_E_SUCCESS) { |
| 371 |
goto out; |
| 372 |
} |
| 373 |
|
| 374 |
response_type_node = plist_array_get_item(msg, 0); |
| 375 |
if (!response_type_node) { |
| 376 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 377 |
goto out; |
| 378 |
} |
| 379 |
|
| 380 |
plist_get_string_val(response_type_node, &response_type); |
| 381 |
if (!response_type) { |
| 382 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 383 |
goto out; |
| 384 |
} |
| 385 |
|
| 386 |
if (!strcmp(response_type, "SDMessageCancelSession")) { |
| 387 |
char *reason = NULL; |
| 388 |
err = MOBILESYNC_E_CANCELLED; |
| 389 |
plist_get_string_val(plist_array_get_item(msg, 2), &reason); |
| 390 |
debug_info("Device cancelled: %s", reason); |
| 391 |
free(reason); |
| 392 |
goto out; |
| 393 |
} |
| 394 |
|
| 395 |
if (entities != NULL) { |
| 396 |
*entities = plist_copy(plist_array_get_item(msg, 2)); |
| 397 |
} |
| 398 |
|
| 399 |
if (is_last_record != NULL) { |
| 400 |
plist_get_bool_val(plist_array_get_item(msg, 3), &has_more_changes); |
| 401 |
*is_last_record = (has_more_changes > 0 ? 0 : 1); |
| 402 |
} |
| 403 |
|
| 404 |
if (actions != NULL) { |
| 405 |
actions_node = plist_array_get_item(msg, 4); |
| 406 |
if (plist_get_node_type(actions_node) == PLIST_DICT) |
| 407 |
*actions = plist_copy(actions_node); |
| 408 |
else |
| 409 |
*actions = NULL; |
| 410 |
} |
| 411 |
|
| 412 |
out: |
| 413 |
if (response_type) { |
| 414 |
free(response_type); |
| 415 |
response_type = NULL; |
| 416 |
} |
| 417 |
if (msg) { |
| 418 |
plist_free(msg); |
| 419 |
msg = NULL; |
| 420 |
} |
| 421 |
return err; |
| 422 |
} |
| 423 |
|
| 424 |
mobilesync_error_t mobilesync_clear_all_records_on_device(mobilesync_client_t client) |
| 425 |
{ |
| 426 |
if (!client || !client->data_class) { |
| 427 |
return MOBILESYNC_E_INVALID_ARG; |
| 428 |
} |
| 429 |
|
| 430 |
mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR; |
| 431 |
plist_t msg = NULL; |
| 432 |
plist_t response_type_node = NULL; |
| 433 |
char *response_type = NULL; |
| 434 |
|
| 435 |
msg = plist_new_array(); |
| 436 |
plist_array_append_item(msg, plist_new_string("SDMessageClearAllRecordsOnDevice")); |
| 437 |
plist_array_append_item(msg, plist_new_string(client->data_class)); |
| 438 |
plist_array_append_item(msg, plist_new_string(EMPTY_PARAMETER_STRING)); |
| 439 |
|
| 440 |
err = mobilesync_send(client, msg); |
| 441 |
|
| 442 |
if (err != MOBILESYNC_E_SUCCESS) { |
| 443 |
goto out; |
| 444 |
} |
| 445 |
|
| 446 |
plist_free(msg); |
| 447 |
msg = NULL; |
| 448 |
|
| 449 |
err = mobilesync_receive(client, &msg); |
| 450 |
|
| 451 |
if (err != MOBILESYNC_E_SUCCESS) { |
| 452 |
goto out; |
| 453 |
} |
| 454 |
|
| 455 |
response_type_node = plist_array_get_item(msg, 0); |
| 456 |
if (!response_type_node) { |
| 457 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 458 |
goto out; |
| 459 |
} |
| 460 |
|
| 461 |
plist_get_string_val(response_type_node, &response_type); |
| 462 |
if (!response_type) { |
| 463 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 464 |
goto out; |
| 465 |
} |
| 466 |
|
| 467 |
if (!strcmp(response_type, "SDMessageCancelSession")) { |
| 468 |
char *reason = NULL; |
| 469 |
err = MOBILESYNC_E_CANCELLED; |
| 470 |
plist_get_string_val(plist_array_get_item(msg, 2), &reason); |
| 471 |
debug_info("Device cancelled: %s", reason); |
| 472 |
free(reason); |
| 473 |
goto out; |
| 474 |
} |
| 475 |
|
| 476 |
if (strcmp(response_type, "SDMessageDeviceWillClearAllRecords") != 0) { |
| 477 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 478 |
} |
| 479 |
|
| 480 |
out: |
| 481 |
if (response_type) { |
| 482 |
free(response_type); |
| 483 |
response_type = NULL; |
| 484 |
} |
| 485 |
if (msg) { |
| 486 |
plist_free(msg); |
| 487 |
msg = NULL; |
| 488 |
} |
| 489 |
|
| 490 |
return err; |
| 491 |
} |
| 492 |
|
| 493 |
mobilesync_error_t mobilesync_acknowledge_changes_from_device(mobilesync_client_t client) |
| 494 |
{ |
| 495 |
if (!client || !client->data_class) { |
| 496 |
return MOBILESYNC_E_INVALID_ARG; |
| 497 |
} |
| 498 |
|
| 499 |
plist_t msg = NULL; |
| 500 |
mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR; |
| 501 |
|
| 502 |
msg = plist_new_array(); |
| 503 |
plist_array_append_item(msg, plist_new_string("SDMessageAcknowledgeChangesFromDevice")); |
| 504 |
plist_array_append_item(msg, plist_new_string(client->data_class)); |
| 505 |
|
| 506 |
err = mobilesync_send(client, msg); |
| 507 |
plist_free(msg); |
| 508 |
return err; |
| 509 |
} |
| 510 |
|
| 511 |
static plist_t create_process_changes_message(const char *data_class, plist_t entities, uint8_t more_changes, plist_t actions) |
| 512 |
{ |
| 513 |
plist_t msg = plist_new_array(); |
| 514 |
plist_array_append_item(msg, plist_new_string("SDMessageProcessChanges")); |
| 515 |
plist_array_append_item(msg, plist_new_string(data_class)); |
| 516 |
plist_array_append_item(msg, plist_copy(entities)); |
| 517 |
plist_array_append_item(msg, plist_new_bool(more_changes)); |
| 518 |
|
| 519 |
if (actions) |
| 520 |
plist_array_append_item(msg, plist_copy(actions)); |
| 521 |
else |
| 522 |
plist_array_append_item(msg, plist_new_string(EMPTY_PARAMETER_STRING)); |
| 523 |
|
| 524 |
return msg; |
| 525 |
} |
| 526 |
|
| 527 |
mobilesync_error_t mobilesync_ready_to_send_changes_from_computer(mobilesync_client_t client) |
| 528 |
{ |
| 529 |
if (!client || !client->data_class) { |
| 530 |
return MOBILESYNC_E_INVALID_ARG; |
| 531 |
} |
| 532 |
|
| 533 |
if (client->direction != MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER) { |
| 534 |
return MOBILESYNC_E_WRONG_DIRECTION; |
| 535 |
} |
| 536 |
|
| 537 |
plist_t msg = NULL; |
| 538 |
plist_t response_type_node = NULL; |
| 539 |
char *response_type = NULL; |
| 540 |
mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR; |
| 541 |
|
| 542 |
err = mobilesync_receive(client, &msg); |
| 543 |
if (err != MOBILESYNC_E_SUCCESS) { |
| 544 |
goto out; |
| 545 |
} |
| 546 |
|
| 547 |
response_type_node = plist_array_get_item(msg, 0); |
| 548 |
if (!response_type_node) { |
| 549 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 550 |
goto out; |
| 551 |
} |
| 552 |
|
| 553 |
plist_get_string_val(response_type_node, &response_type); |
| 554 |
if (!response_type) { |
| 555 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 556 |
goto out; |
| 557 |
} |
| 558 |
|
| 559 |
if (!strcmp(response_type, "SDMessageCancelSession")) { |
| 560 |
char *reason = NULL; |
| 561 |
err = MOBILESYNC_E_CANCELLED; |
| 562 |
plist_get_string_val(plist_array_get_item(msg, 2), &reason); |
| 563 |
debug_info("Device cancelled: %s", reason); |
| 564 |
free(reason); |
| 565 |
goto out; |
| 566 |
} |
| 567 |
|
| 568 |
if (strcmp(response_type, "SDMessageDeviceReadyToReceiveChanges") != 0) { |
| 569 |
err = MOBILESYNC_E_NOT_READY; |
| 570 |
goto out; |
| 571 |
} |
| 572 |
|
| 573 |
err = mobilesync_error(device_link_service_send_ping(client->parent, "Preparing to get changes for device")); |
| 574 |
if (err != MOBILESYNC_E_SUCCESS) { |
| 575 |
goto out; |
| 576 |
} |
| 577 |
|
| 578 |
client->direction = MOBILESYNC_SYNC_DIR_COMPUTER_TO_DEVICE; |
| 579 |
err = MOBILESYNC_E_SUCCESS; |
| 580 |
|
| 581 |
out: |
| 582 |
if (response_type) { |
| 583 |
free(response_type); |
| 584 |
response_type = NULL; |
| 585 |
} |
| 586 |
if (msg) { |
| 587 |
plist_free(msg); |
| 588 |
msg = NULL; |
| 589 |
} |
| 590 |
|
| 591 |
return err; |
| 592 |
} |
| 593 |
|
| 594 |
mobilesync_error_t mobilesync_send_changes(mobilesync_client_t client, plist_t entities, uint8_t is_last_record, plist_t actions) |
| 595 |
{ |
| 596 |
if (!client || !client->data_class || !entities) { |
| 597 |
return MOBILESYNC_E_INVALID_ARG; |
| 598 |
} |
| 599 |
|
| 600 |
if (plist_get_node_type(entities) != PLIST_DICT) { |
| 601 |
return MOBILESYNC_E_INVALID_ARG; |
| 602 |
} |
| 603 |
|
| 604 |
if (client->direction != MOBILESYNC_SYNC_DIR_COMPUTER_TO_DEVICE) { |
| 605 |
return MOBILESYNC_E_WRONG_DIRECTION; |
| 606 |
} |
| 607 |
|
| 608 |
mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR; |
| 609 |
plist_t msg = NULL; |
| 610 |
|
| 611 |
msg = create_process_changes_message(client->data_class, entities, (is_last_record > 0 ? 0 : 1), actions); |
| 612 |
err = mobilesync_send(client, msg); |
| 613 |
|
| 614 |
if (msg) { |
| 615 |
plist_free(msg); |
| 616 |
msg = NULL; |
| 617 |
} |
| 618 |
|
| 619 |
return err; |
| 620 |
} |
| 621 |
|
| 622 |
mobilesync_error_t mobilesync_remap_identifiers(mobilesync_client_t client, plist_t *mapping) |
| 623 |
{ |
| 624 |
if (!client || !client->data_class) { |
| 625 |
return MOBILESYNC_E_INVALID_ARG; |
| 626 |
} |
| 627 |
|
| 628 |
if (client->direction == MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER) { |
| 629 |
return MOBILESYNC_E_WRONG_DIRECTION; |
| 630 |
} |
| 631 |
|
| 632 |
plist_t msg = NULL; |
| 633 |
plist_t response_type_node = NULL; |
| 634 |
char *response_type = NULL; |
| 635 |
|
| 636 |
mobilesync_error_t err = mobilesync_receive(client, &msg); |
| 637 |
if (err != MOBILESYNC_E_SUCCESS) { |
| 638 |
goto out; |
| 639 |
} |
| 640 |
|
| 641 |
response_type_node = plist_array_get_item(msg, 0); |
| 642 |
if (!response_type_node) { |
| 643 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 644 |
goto out; |
| 645 |
} |
| 646 |
|
| 647 |
plist_get_string_val(response_type_node, &response_type); |
| 648 |
if (!response_type) { |
| 649 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 650 |
goto out; |
| 651 |
} |
| 652 |
|
| 653 |
if (!strcmp(response_type, "SDMessageCancelSession")) { |
| 654 |
char *reason = NULL; |
| 655 |
err = MOBILESYNC_E_CANCELLED; |
| 656 |
plist_get_string_val(plist_array_get_item(msg, 2), &reason); |
| 657 |
debug_info("Device cancelled: %s", reason); |
| 658 |
free(reason); |
| 659 |
goto out; |
| 660 |
} |
| 661 |
|
| 662 |
if (strcmp(response_type, "SDMessageRemapRecordIdentifiers") != 0) { |
| 663 |
err = MOBILESYNC_E_PLIST_ERROR; |
| 664 |
goto out; |
| 665 |
} |
| 666 |
|
| 667 |
if (mapping != NULL) { |
| 668 |
plist_t map = plist_array_get_item(msg, 2); |
| 669 |
if (plist_get_node_type(map) == PLIST_DICT) { |
| 670 |
*mapping = plist_copy(map); |
| 671 |
} else { |
| 672 |
*mapping = NULL; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
err = MOBILESYNC_E_SUCCESS; |
| 677 |
|
| 678 |
out: |
| 679 |
if (response_type) { |
| 680 |
free(response_type); |
| 681 |
response_type = NULL; |
| 682 |
} |
| 683 |
if (msg) { |
| 684 |
plist_free(msg); |
| 685 |
msg = NULL; |
| 686 |
} |
| 687 |
|
| 688 |
return err; |
| 689 |
} |
| 690 |
|
| 691 |
mobilesync_error_t mobilesync_cancel(mobilesync_client_t client, const char* reason) |
| 692 |
{ |
| 693 |
if (!client || !client->data_class || !reason) { |
| 694 |
return MOBILESYNC_E_INVALID_ARG; |
| 695 |
} |
| 696 |
|
| 697 |
mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR; |
| 698 |
plist_t msg = NULL; |
| 699 |
|
| 700 |
msg = plist_new_array(); |
| 701 |
plist_array_append_item(msg, plist_new_string("SDMessageCancelSession")); |
| 702 |
plist_array_append_item(msg, plist_new_string(client->data_class)); |
| 703 |
plist_array_append_item(msg, plist_new_string(reason)); |
| 704 |
|
| 705 |
err = mobilesync_send(client, msg); |
| 706 |
|
| 707 |
plist_free(msg); |
| 708 |
msg = NULL; |
| 709 |
|
| 710 |
free(client->data_class); |
| 711 |
client->data_class = NULL; |
| 712 |
client->direction = MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER; |
| 713 |
|
| 714 |
return err; |
| 715 |
} |
| 716 |
|
| 717 |
mobilesync_anchors_t mobilesync_anchors_new(const char *device_anchor, const char *computer_anchor) |
| 718 |
{ |
| 719 |
mobilesync_anchors_t anchors = (mobilesync_anchors_t) malloc(sizeof(mobilesync_anchors)); |
| 720 |
if (device_anchor != NULL) { |
| 721 |
anchors->device_anchor = strdup(device_anchor); |
| 722 |
} else { |
| 723 |
anchors->device_anchor = NULL; |
| 724 |
} |
| 725 |
if (computer_anchor != NULL) { |
| 726 |
anchors->computer_anchor = strdup(computer_anchor); |
| 727 |
} else { |
| 728 |
anchors->computer_anchor = NULL; |
| 729 |
} |
| 730 |
|
| 731 |
return anchors; |
| 732 |
} |
| 733 |
|
| 734 |
void mobilesync_anchors_free(mobilesync_anchors_t anchors) |
| 735 |
{ |
| 736 |
if (anchors->device_anchor != NULL) { |
| 737 |
free(anchors->device_anchor); |
| 738 |
anchors->device_anchor = NULL; |
| 739 |
} |
| 740 |
if (anchors->computer_anchor != NULL) { |
| 741 |
free(anchors->computer_anchor); |
| 742 |
anchors->computer_anchor = NULL; |
| 743 |
} |
| 744 |
free(anchors); |
| 745 |
anchors = NULL; |
| 746 |
} |
| 747 |
|
| 748 |
plist_t mobilesync_actions_new(void) |
| 749 |
{ |
| 750 |
return plist_new_dict(); |
| 751 |
} |
| 752 |
|
| 753 |
void mobilesync_actions_add(plist_t actions, ...) |
| 754 |
{ |
| 755 |
if (!actions) |
| 756 |
return; |
| 757 |
va_list args; |
| 758 |
va_start(args, actions); |
| 759 |
char *arg = va_arg(args, char*); |
| 760 |
while (arg) { |
| 761 |
char *key = strdup(arg); |
| 762 |
if (!strcmp(key, "SyncDeviceLinkEntityNamesKey")) { |
| 763 |
char **entity_names = va_arg(args, char**); |
| 764 |
int entity_names_length = va_arg(args, int); |
| 765 |
int i = 0; |
| 766 |
|
| 767 |
plist_t array = plist_new_array(); |
| 768 |
|
| 769 |
for (i = 0; i < entity_names_length; i++) { |
| 770 |
plist_array_append_item(array, plist_new_string(entity_names[i])); |
| 771 |
} |
| 772 |
|
| 773 |
plist_dict_set_item(actions, key, array); |
| 774 |
} else if (!strcmp(key, "SyncDeviceLinkAllRecordsOfPulledEntityTypeSentKey")) { |
| 775 |
int link_records = va_arg(args, int); |
| 776 |
plist_dict_set_item(actions, key, plist_new_bool(link_records)); |
| 777 |
} |
| 778 |
free(key); |
| 779 |
key = NULL; |
| 780 |
arg = va_arg(args, char*); |
| 781 |
} |
| 782 |
va_end(args); |
| 783 |
} |
| 784 |
|
| 785 |
void mobilesync_actions_free(plist_t actions) |
| 786 |
{ |
| 787 |
if (actions) { |
| 788 |
plist_free(actions); |
| 789 |
actions = NULL; |
| 790 |
} |
| 791 |
} |