mobilesync.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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. #define _GNU_SOURCE 1
  23. #define __USE_GNU 1
  24. #include <plist/plist.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include "mobilesync.h"
  29. #include "device_link_service.h"
  30. #include "common/debug.h"
  31. #define MSYNC_VERSION_INT1 300
  32. #define MSYNC_VERSION_INT2 100
  33. #define EMPTY_PARAMETER_STRING "___EmptyParameterString___"
  34. /**
  35. * Convert an #device_link_service_error_t value to an #mobilesync_error_t value.
  36. * Used internally to get correct error codes when using device_link_service stuff.
  37. *
  38. * @param err A #device_link_service_error_t error code
  39. *
  40. * @return A matching #mobilesync_error_t error code,
  41. * MOBILESYNC_E_UNKNOWN_ERROR otherwise.
  42. */
  43. static mobilesync_error_t mobilesync_error(device_link_service_error_t err)
  44. {
  45. switch (err) {
  46. case DEVICE_LINK_SERVICE_E_SUCCESS:
  47. return MOBILESYNC_E_SUCCESS;
  48. case DEVICE_LINK_SERVICE_E_INVALID_ARG:
  49. return MOBILESYNC_E_INVALID_ARG;
  50. case DEVICE_LINK_SERVICE_E_PLIST_ERROR:
  51. return MOBILESYNC_E_PLIST_ERROR;
  52. case DEVICE_LINK_SERVICE_E_MUX_ERROR:
  53. return MOBILESYNC_E_MUX_ERROR;
  54. case DEVICE_LINK_SERVICE_E_BAD_VERSION:
  55. return MOBILESYNC_E_BAD_VERSION;
  56. default:
  57. break;
  58. }
  59. return MOBILESYNC_E_UNKNOWN_ERROR;
  60. }
  61. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_client_new(idevice_t device, lockdownd_service_descriptor_t service,
  62. mobilesync_client_t * client)
  63. {
  64. if (!device || !service || service->port == 0 || !client || *client)
  65. return MOBILESYNC_E_INVALID_ARG;
  66. device_link_service_client_t dlclient = NULL;
  67. mobilesync_error_t ret = mobilesync_error(device_link_service_client_new(device, service, &dlclient));
  68. if (ret != MOBILESYNC_E_SUCCESS) {
  69. return ret;
  70. }
  71. mobilesync_client_t client_loc = (mobilesync_client_t) malloc(sizeof(struct mobilesync_client_private));
  72. client_loc->parent = dlclient;
  73. client_loc->direction = MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER;
  74. client_loc->data_class = NULL;
  75. /* perform handshake */
  76. ret = mobilesync_error(device_link_service_version_exchange(dlclient, MSYNC_VERSION_INT1, MSYNC_VERSION_INT2));
  77. if (ret != MOBILESYNC_E_SUCCESS) {
  78. debug_info("version exchange failed, error %d", ret);
  79. mobilesync_client_free(client_loc);
  80. return ret;
  81. }
  82. *client = client_loc;
  83. return ret;
  84. }
  85. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_client_start_service(idevice_t device, mobilesync_client_t * client, const char* label)
  86. {
  87. mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR;
  88. service_client_factory_start_service(device, MOBILESYNC_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(mobilesync_client_new), &err);
  89. return err;
  90. }
  91. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_client_free(mobilesync_client_t client)
  92. {
  93. if (!client)
  94. return MOBILESYNC_E_INVALID_ARG;
  95. device_link_service_disconnect(client->parent, "All done, thanks for the memories");
  96. mobilesync_error_t err = mobilesync_error(device_link_service_client_free(client->parent));
  97. free(client);
  98. return err;
  99. }
  100. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_receive(mobilesync_client_t client, plist_t * plist)
  101. {
  102. if (!client)
  103. return MOBILESYNC_E_INVALID_ARG;
  104. mobilesync_error_t ret = mobilesync_error(device_link_service_receive(client->parent, plist));
  105. return ret;
  106. }
  107. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_send(mobilesync_client_t client, plist_t plist)
  108. {
  109. if (!client || !plist)
  110. return MOBILESYNC_E_INVALID_ARG;
  111. return mobilesync_error(device_link_service_send(client->parent, plist));
  112. }
  113. LIBIMOBILEDEVICE_API 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)
  114. {
  115. if (!client || client->data_class || !data_class ||
  116. !anchors || !anchors->computer_anchor) {
  117. return MOBILESYNC_E_INVALID_ARG;
  118. }
  119. mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR;
  120. char *response_type = NULL;
  121. char *sync_type_str = NULL;
  122. plist_t msg = NULL;
  123. plist_t response_type_node = NULL;
  124. *error_description = NULL;
  125. msg = plist_new_array();
  126. plist_array_append_item(msg, plist_new_string("SDMessageSyncDataClassWithDevice"));
  127. plist_array_append_item(msg, plist_new_string(data_class));
  128. if (anchors->device_anchor) {
  129. plist_array_append_item(msg, plist_new_string(anchors->device_anchor));
  130. } else {
  131. plist_array_append_item(msg, plist_new_string("---"));
  132. }
  133. plist_array_append_item(msg, plist_new_string(anchors->computer_anchor));
  134. plist_array_append_item(msg, plist_new_uint(computer_data_class_version));
  135. plist_array_append_item(msg, plist_new_string(EMPTY_PARAMETER_STRING));
  136. err = mobilesync_send(client, msg);
  137. if (err != MOBILESYNC_E_SUCCESS) {
  138. goto out;
  139. }
  140. plist_free(msg);
  141. msg = NULL;
  142. err = mobilesync_receive(client, &msg);
  143. if (err != MOBILESYNC_E_SUCCESS) {
  144. goto out;
  145. }
  146. response_type_node = plist_array_get_item(msg, 0);
  147. if (!response_type_node) {
  148. err = MOBILESYNC_E_PLIST_ERROR;
  149. goto out;
  150. }
  151. plist_get_string_val(response_type_node, &response_type);
  152. if (!response_type) {
  153. err = MOBILESYNC_E_PLIST_ERROR;
  154. goto out;
  155. }
  156. // did the device refuse to sync with the computer?
  157. if (!strcmp(response_type, "SDMessageRefuseToSyncDataClassWithComputer")) {
  158. err = MOBILESYNC_E_SYNC_REFUSED;
  159. plist_get_string_val(plist_array_get_item(msg, 2), error_description);
  160. debug_info("Device refused sync: %s", error_description);
  161. goto out;
  162. }
  163. // did the device cancel the session?
  164. if (!strcmp(response_type, "SDMessageCancelSession")) {
  165. err = MOBILESYNC_E_CANCELLED;
  166. plist_get_string_val(plist_array_get_item(msg, 2), error_description);
  167. debug_info("Device cancelled: %s", error_description);
  168. goto out;
  169. }
  170. if (sync_type != NULL) {
  171. plist_t sync_type_node = plist_array_get_item(msg, 4);
  172. if (!sync_type_node) {
  173. err = MOBILESYNC_E_PLIST_ERROR;
  174. goto out;
  175. }
  176. plist_get_string_val(sync_type_node, &sync_type_str);
  177. if (!sync_type_str) {
  178. err = MOBILESYNC_E_PLIST_ERROR;
  179. goto out;
  180. }
  181. if (!strcmp(sync_type_str, "SDSyncTypeFast")) {
  182. *sync_type = MOBILESYNC_SYNC_TYPE_FAST;
  183. } else if (!strcmp(sync_type_str, "SDSyncTypeSlow")) {
  184. *sync_type = MOBILESYNC_SYNC_TYPE_SLOW;
  185. } else if (!strcmp(sync_type_str, "SDSyncTypeReset")) {
  186. *sync_type = MOBILESYNC_SYNC_TYPE_RESET;
  187. } else {
  188. err = MOBILESYNC_E_PLIST_ERROR;
  189. goto out;
  190. }
  191. }
  192. if (device_data_class_version != NULL) {
  193. plist_t device_data_class_version_node = plist_array_get_item(msg, 5);
  194. if (!device_data_class_version_node) {
  195. err = MOBILESYNC_E_PLIST_ERROR;
  196. goto out;
  197. }
  198. plist_get_uint_val(device_data_class_version_node, device_data_class_version);
  199. }
  200. err = MOBILESYNC_E_SUCCESS;
  201. out:
  202. if (sync_type_str) {
  203. free(sync_type_str);
  204. sync_type_str = NULL;
  205. }
  206. if (response_type) {
  207. free(response_type);
  208. response_type = NULL;
  209. }
  210. if (msg) {
  211. plist_free(msg);
  212. msg = NULL;
  213. }
  214. client->data_class = strdup(data_class);
  215. client->direction = MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER;
  216. return err;
  217. }
  218. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_finish(mobilesync_client_t client)
  219. {
  220. if (!client || !client->data_class) {
  221. return MOBILESYNC_E_INVALID_ARG;
  222. }
  223. mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR;
  224. plist_t msg = NULL;
  225. plist_t response_type_node = NULL;
  226. char *response_type = NULL;
  227. msg = plist_new_array();
  228. plist_array_append_item(msg, plist_new_string("SDMessageFinishSessionOnDevice"));
  229. plist_array_append_item(msg, plist_new_string(client->data_class));
  230. err = mobilesync_send(client, msg);
  231. if (err != MOBILESYNC_E_SUCCESS) {
  232. goto out;
  233. }
  234. plist_free(msg);
  235. msg = NULL;
  236. err = mobilesync_receive(client, &msg);
  237. if (err != MOBILESYNC_E_SUCCESS) {
  238. goto out;
  239. }
  240. response_type_node = plist_array_get_item(msg, 0);
  241. if (!response_type_node) {
  242. err = MOBILESYNC_E_PLIST_ERROR;
  243. goto out;
  244. }
  245. plist_get_string_val(response_type_node, &response_type);
  246. if (!response_type) {
  247. err = MOBILESYNC_E_PLIST_ERROR;
  248. goto out;
  249. }
  250. if (!strcmp(response_type, "SDMessageDeviceFinishedSession")) {
  251. err = MOBILESYNC_E_SUCCESS;
  252. }
  253. out:
  254. if (response_type) {
  255. free(response_type);
  256. response_type = NULL;
  257. }
  258. if (msg) {
  259. plist_free(msg);
  260. msg = NULL;
  261. }
  262. free(client->data_class);
  263. client->data_class = NULL;
  264. client->direction = MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER;
  265. return err;
  266. }
  267. static mobilesync_error_t mobilesync_get_records(mobilesync_client_t client, const char *operation)
  268. {
  269. if (!client || !client->data_class || !operation) {
  270. return MOBILESYNC_E_INVALID_ARG;
  271. }
  272. mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR;
  273. plist_t msg = NULL;
  274. msg = plist_new_array();
  275. plist_array_append_item(msg, plist_new_string(operation));
  276. plist_array_append_item(msg, plist_new_string(client->data_class));
  277. err = mobilesync_send(client, msg);
  278. if (msg) {
  279. plist_free(msg);
  280. msg = NULL;
  281. }
  282. return err;
  283. }
  284. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_get_all_records_from_device(mobilesync_client_t client)
  285. {
  286. return mobilesync_get_records(client, "SDMessageGetAllRecordsFromDevice");
  287. }
  288. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_get_changes_from_device(mobilesync_client_t client)
  289. {
  290. return mobilesync_get_records(client, "SDMessageGetChangesFromDevice");
  291. }
  292. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_receive_changes(mobilesync_client_t client, plist_t *entities, uint8_t *is_last_record, plist_t *actions)
  293. {
  294. if (!client || !client->data_class) {
  295. return MOBILESYNC_E_INVALID_ARG;
  296. }
  297. plist_t msg = NULL;
  298. plist_t response_type_node = NULL;
  299. plist_t actions_node = NULL;
  300. char *response_type = NULL;
  301. uint8_t has_more_changes = 0;
  302. mobilesync_error_t err = mobilesync_receive(client, &msg);
  303. if (err != MOBILESYNC_E_SUCCESS) {
  304. goto out;
  305. }
  306. response_type_node = plist_array_get_item(msg, 0);
  307. if (!response_type_node) {
  308. err = MOBILESYNC_E_PLIST_ERROR;
  309. goto out;
  310. }
  311. plist_get_string_val(response_type_node, &response_type);
  312. if (!response_type) {
  313. err = MOBILESYNC_E_PLIST_ERROR;
  314. goto out;
  315. }
  316. if (!strcmp(response_type, "SDMessageCancelSession")) {
  317. char *reason = NULL;
  318. err = MOBILESYNC_E_CANCELLED;
  319. plist_get_string_val(plist_array_get_item(msg, 2), &reason);
  320. debug_info("Device cancelled: %s", reason);
  321. free(reason);
  322. goto out;
  323. }
  324. if (entities != NULL) {
  325. *entities = plist_copy(plist_array_get_item(msg, 2));
  326. }
  327. if (is_last_record != NULL) {
  328. plist_get_bool_val(plist_array_get_item(msg, 3), &has_more_changes);
  329. *is_last_record = (has_more_changes > 0 ? 0 : 1);
  330. }
  331. if (actions != NULL) {
  332. actions_node = plist_array_get_item(msg, 4);
  333. if (plist_get_node_type(actions_node) == PLIST_DICT)
  334. *actions = plist_copy(actions_node);
  335. else
  336. *actions = NULL;
  337. }
  338. out:
  339. if (response_type) {
  340. free(response_type);
  341. response_type = NULL;
  342. }
  343. if (msg) {
  344. plist_free(msg);
  345. msg = NULL;
  346. }
  347. return err;
  348. }
  349. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_clear_all_records_on_device(mobilesync_client_t client)
  350. {
  351. if (!client || !client->data_class) {
  352. return MOBILESYNC_E_INVALID_ARG;
  353. }
  354. mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR;
  355. plist_t msg = NULL;
  356. plist_t response_type_node = NULL;
  357. char *response_type = NULL;
  358. msg = plist_new_array();
  359. plist_array_append_item(msg, plist_new_string("SDMessageClearAllRecordsOnDevice"));
  360. plist_array_append_item(msg, plist_new_string(client->data_class));
  361. plist_array_append_item(msg, plist_new_string(EMPTY_PARAMETER_STRING));
  362. err = mobilesync_send(client, msg);
  363. if (err != MOBILESYNC_E_SUCCESS) {
  364. goto out;
  365. }
  366. plist_free(msg);
  367. msg = NULL;
  368. err = mobilesync_receive(client, &msg);
  369. if (err != MOBILESYNC_E_SUCCESS) {
  370. goto out;
  371. }
  372. response_type_node = plist_array_get_item(msg, 0);
  373. if (!response_type_node) {
  374. err = MOBILESYNC_E_PLIST_ERROR;
  375. goto out;
  376. }
  377. plist_get_string_val(response_type_node, &response_type);
  378. if (!response_type) {
  379. err = MOBILESYNC_E_PLIST_ERROR;
  380. goto out;
  381. }
  382. if (!strcmp(response_type, "SDMessageCancelSession")) {
  383. char *reason = NULL;
  384. err = MOBILESYNC_E_CANCELLED;
  385. plist_get_string_val(plist_array_get_item(msg, 2), &reason);
  386. debug_info("Device cancelled: %s", reason);
  387. free(reason);
  388. goto out;
  389. }
  390. if (strcmp(response_type, "SDMessageDeviceWillClearAllRecords")) {
  391. err = MOBILESYNC_E_PLIST_ERROR;
  392. }
  393. out:
  394. if (response_type) {
  395. free(response_type);
  396. response_type = NULL;
  397. }
  398. if (msg) {
  399. plist_free(msg);
  400. msg = NULL;
  401. }
  402. return err;
  403. }
  404. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_acknowledge_changes_from_device(mobilesync_client_t client)
  405. {
  406. if (!client || !client->data_class) {
  407. return MOBILESYNC_E_INVALID_ARG;
  408. }
  409. plist_t msg = NULL;
  410. mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR;
  411. msg = plist_new_array();
  412. plist_array_append_item(msg, plist_new_string("SDMessageAcknowledgeChangesFromDevice"));
  413. plist_array_append_item(msg, plist_new_string(client->data_class));
  414. err = mobilesync_send(client, msg);
  415. plist_free(msg);
  416. return err;
  417. }
  418. static plist_t create_process_changes_message(const char *data_class, plist_t entities, uint8_t more_changes, plist_t actions)
  419. {
  420. plist_t msg = plist_new_array();
  421. plist_array_append_item(msg, plist_new_string("SDMessageProcessChanges"));
  422. plist_array_append_item(msg, plist_new_string(data_class));
  423. plist_array_append_item(msg, plist_copy(entities));
  424. plist_array_append_item(msg, plist_new_bool(more_changes));
  425. if (actions)
  426. plist_array_append_item(msg, plist_copy(actions));
  427. else
  428. plist_array_append_item(msg, plist_new_string(EMPTY_PARAMETER_STRING));
  429. return msg;
  430. }
  431. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_ready_to_send_changes_from_computer(mobilesync_client_t client)
  432. {
  433. if (!client || !client->data_class) {
  434. return MOBILESYNC_E_INVALID_ARG;
  435. }
  436. if (client->direction != MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER) {
  437. return MOBILESYNC_E_WRONG_DIRECTION;
  438. }
  439. plist_t msg = NULL;
  440. plist_t response_type_node = NULL;
  441. char *response_type = NULL;
  442. mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR;
  443. err = mobilesync_receive(client, &msg);
  444. if (err != MOBILESYNC_E_SUCCESS) {
  445. goto out;
  446. }
  447. response_type_node = plist_array_get_item(msg, 0);
  448. if (!response_type_node) {
  449. err = MOBILESYNC_E_PLIST_ERROR;
  450. goto out;
  451. }
  452. plist_get_string_val(response_type_node, &response_type);
  453. if (!response_type) {
  454. err = MOBILESYNC_E_PLIST_ERROR;
  455. goto out;
  456. }
  457. if (!strcmp(response_type, "SDMessageCancelSession")) {
  458. char *reason = NULL;
  459. err = MOBILESYNC_E_CANCELLED;
  460. plist_get_string_val(plist_array_get_item(msg, 2), &reason);
  461. debug_info("Device cancelled: %s", reason);
  462. free(reason);
  463. goto out;
  464. }
  465. if (strcmp(response_type, "SDMessageDeviceReadyToReceiveChanges") != 0) {
  466. err = MOBILESYNC_E_NOT_READY;
  467. goto out;
  468. }
  469. err = mobilesync_error(device_link_service_send_ping(client->parent, "Preparing to get changes for device"));
  470. if (err != MOBILESYNC_E_SUCCESS) {
  471. goto out;
  472. }
  473. client->direction = MOBILESYNC_SYNC_DIR_COMPUTER_TO_DEVICE;
  474. err = MOBILESYNC_E_SUCCESS;
  475. out:
  476. if (response_type) {
  477. free(response_type);
  478. response_type = NULL;
  479. }
  480. if (msg) {
  481. plist_free(msg);
  482. msg = NULL;
  483. }
  484. return err;
  485. }
  486. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_send_changes(mobilesync_client_t client, plist_t entities, uint8_t is_last_record, plist_t actions)
  487. {
  488. if (!client || !client->data_class || !entities) {
  489. return MOBILESYNC_E_INVALID_ARG;
  490. }
  491. if (plist_get_node_type(entities) != PLIST_DICT) {
  492. return MOBILESYNC_E_INVALID_ARG;
  493. }
  494. if (client->direction != MOBILESYNC_SYNC_DIR_COMPUTER_TO_DEVICE) {
  495. return MOBILESYNC_E_WRONG_DIRECTION;
  496. }
  497. mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR;
  498. plist_t msg = NULL;
  499. msg = create_process_changes_message(client->data_class, entities, (is_last_record > 0 ? 0 : 1), actions);
  500. err = mobilesync_send(client, msg);
  501. if (msg) {
  502. plist_free(msg);
  503. msg = NULL;
  504. }
  505. return err;
  506. }
  507. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_remap_identifiers(mobilesync_client_t client, plist_t *mapping)
  508. {
  509. if (!client || !client->data_class) {
  510. return MOBILESYNC_E_INVALID_ARG;
  511. }
  512. if (client->direction == MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER) {
  513. return MOBILESYNC_E_WRONG_DIRECTION;
  514. }
  515. plist_t msg = NULL;
  516. plist_t response_type_node = NULL;
  517. char *response_type = NULL;
  518. mobilesync_error_t err = mobilesync_receive(client, &msg);
  519. if (err != MOBILESYNC_E_SUCCESS) {
  520. goto out;
  521. }
  522. response_type_node = plist_array_get_item(msg, 0);
  523. if (!response_type_node) {
  524. err = MOBILESYNC_E_PLIST_ERROR;
  525. goto out;
  526. }
  527. plist_get_string_val(response_type_node, &response_type);
  528. if (!response_type) {
  529. err = MOBILESYNC_E_PLIST_ERROR;
  530. goto out;
  531. }
  532. if (!strcmp(response_type, "SDMessageCancelSession")) {
  533. char *reason = NULL;
  534. err = MOBILESYNC_E_CANCELLED;
  535. plist_get_string_val(plist_array_get_item(msg, 2), &reason);
  536. debug_info("Device cancelled: %s", reason);
  537. free(reason);
  538. goto out;
  539. }
  540. if (strcmp(response_type, "SDMessageRemapRecordIdentifiers") != 0) {
  541. err = MOBILESYNC_E_PLIST_ERROR;
  542. goto out;
  543. }
  544. if (mapping != NULL) {
  545. plist_t map = plist_array_get_item(msg, 2);
  546. if (plist_get_node_type(map) == PLIST_DICT) {
  547. *mapping = plist_copy(map);
  548. } else {
  549. *mapping = NULL;
  550. }
  551. }
  552. err = MOBILESYNC_E_SUCCESS;
  553. out:
  554. if (response_type) {
  555. free(response_type);
  556. response_type = NULL;
  557. }
  558. if (msg) {
  559. plist_free(msg);
  560. msg = NULL;
  561. }
  562. return err;
  563. }
  564. LIBIMOBILEDEVICE_API mobilesync_error_t mobilesync_cancel(mobilesync_client_t client, const char* reason)
  565. {
  566. if (!client || !client->data_class || !reason) {
  567. return MOBILESYNC_E_INVALID_ARG;
  568. }
  569. mobilesync_error_t err = MOBILESYNC_E_UNKNOWN_ERROR;
  570. plist_t msg = NULL;
  571. msg = plist_new_array();
  572. plist_array_append_item(msg, plist_new_string("SDMessageCancelSession"));
  573. plist_array_append_item(msg, plist_new_string(client->data_class));
  574. plist_array_append_item(msg, plist_new_string(reason));
  575. err = mobilesync_send(client, msg);
  576. plist_free(msg);
  577. msg = NULL;
  578. free(client->data_class);
  579. client->data_class = NULL;
  580. client->direction = MOBILESYNC_SYNC_DIR_DEVICE_TO_COMPUTER;
  581. return err;
  582. }
  583. LIBIMOBILEDEVICE_API mobilesync_anchors_t mobilesync_anchors_new(const char *device_anchor, const char *computer_anchor)
  584. {
  585. mobilesync_anchors_t anchors = (mobilesync_anchors_t) malloc(sizeof(mobilesync_anchors));
  586. if (device_anchor != NULL) {
  587. anchors->device_anchor = strdup(device_anchor);
  588. } else {
  589. anchors->device_anchor = NULL;
  590. }
  591. if (computer_anchor != NULL) {
  592. anchors->computer_anchor = strdup(computer_anchor);
  593. } else {
  594. anchors->computer_anchor = NULL;
  595. }
  596. return anchors;
  597. }
  598. LIBIMOBILEDEVICE_API void mobilesync_anchors_free(mobilesync_anchors_t anchors)
  599. {
  600. if (anchors->device_anchor != NULL) {
  601. free(anchors->device_anchor);
  602. anchors->device_anchor = NULL;
  603. }
  604. if (anchors->computer_anchor != NULL) {
  605. free(anchors->computer_anchor);
  606. anchors->computer_anchor = NULL;
  607. }
  608. free(anchors);
  609. anchors = NULL;
  610. }
  611. LIBIMOBILEDEVICE_API plist_t mobilesync_actions_new()
  612. {
  613. return plist_new_dict();
  614. }
  615. LIBIMOBILEDEVICE_API void mobilesync_actions_add(plist_t actions, ...)
  616. {
  617. if (!actions)
  618. return;
  619. va_list args;
  620. va_start(args, actions);
  621. char *arg = va_arg(args, char*);
  622. while (arg) {
  623. char *key = strdup(arg);
  624. if (!strcmp(key, "SyncDeviceLinkEntityNamesKey")) {
  625. char **entity_names = va_arg(args, char**);
  626. int entity_names_length = va_arg(args, int);
  627. int i = 0;
  628. plist_t array = plist_new_array();
  629. for (i = 0; i < entity_names_length; i++) {
  630. plist_array_append_item(array, plist_new_string(entity_names[i]));
  631. }
  632. plist_dict_set_item(actions, key, array);
  633. } else if (!strcmp(key, "SyncDeviceLinkAllRecordsOfPulledEntityTypeSentKey")) {
  634. int link_records = va_arg(args, int);
  635. plist_dict_set_item(actions, key, plist_new_bool(link_records));
  636. }
  637. free(key);
  638. key = NULL;
  639. arg = va_arg(args, char*);
  640. }
  641. va_end(args);
  642. }
  643. LIBIMOBILEDEVICE_API void mobilesync_actions_free(plist_t actions)
  644. {
  645. if (actions) {
  646. plist_free(actions);
  647. actions = NULL;
  648. }
  649. }