diff options
Diffstat (limited to 'cython/mobilesync.pxi')
-rw-r--r-- | cython/mobilesync.pxi | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/cython/mobilesync.pxi b/cython/mobilesync.pxi new file mode 100644 index 0000000..23f0005 --- /dev/null +++ b/cython/mobilesync.pxi | |||
@@ -0,0 +1,164 @@ | |||
1 | cdef extern from "libimobiledevice/mobilesync.h": | ||
2 | cdef struct mobilesync_client_private: | ||
3 | pass | ||
4 | ctypedef mobilesync_client_private *mobilesync_client_t | ||
5 | ctypedef enum mobilesync_error_t: | ||
6 | MOBILESYNC_E_SUCCESS = 0 | ||
7 | MOBILESYNC_E_INVALID_ARG = -1 | ||
8 | MOBILESYNC_E_PLIST_ERROR = -2 | ||
9 | MOBILESYNC_E_MUX_ERROR = -3 | ||
10 | MOBILESYNC_E_SSL_ERROR = -4 | ||
11 | MOBILESYNC_E_RECEIVE_TIMEOUT = -5 | ||
12 | MOBILESYNC_E_BAD_VERSION = -6 | ||
13 | MOBILESYNC_E_SYNC_REFUSED = -7 | ||
14 | MOBILESYNC_E_CANCELLED = -8 | ||
15 | MOBILESYNC_E_WRONG_DIRECTION = -9 | ||
16 | MOBILESYNC_E_NOT_READY = -10 | ||
17 | MOBILESYNC_E_UNKNOWN_ERROR = -256 | ||
18 | |||
19 | ctypedef enum mobilesync_sync_type_t: | ||
20 | MOBILESYNC_SYNC_TYPE_FAST | ||
21 | MOBILESYNC_SYNC_TYPE_SLOW | ||
22 | MOBILESYNC_SYNC_TYPE_RESET | ||
23 | |||
24 | ctypedef struct mobilesync_anchors: | ||
25 | char *device_anchor | ||
26 | char *host_anchor | ||
27 | ctypedef mobilesync_anchors *mobilesync_anchors_t | ||
28 | |||
29 | mobilesync_error_t mobilesync_client_new(idevice_t device, lockdownd_service_descriptor_t service, mobilesync_client_t * client) | ||
30 | mobilesync_error_t mobilesync_client_free(mobilesync_client_t client) | ||
31 | mobilesync_error_t mobilesync_receive(mobilesync_client_t client, plist.plist_t *plist) | ||
32 | mobilesync_error_t mobilesync_send(mobilesync_client_t client, plist.plist_t plist) | ||
33 | |||
34 | mobilesync_error_t mobilesync_start(mobilesync_client_t client, 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) | ||
35 | mobilesync_error_t mobilesync_cancel(mobilesync_client_t client, char* reason) | ||
36 | mobilesync_error_t mobilesync_finish(mobilesync_client_t client) | ||
37 | |||
38 | mobilesync_error_t mobilesync_get_all_records_from_device(mobilesync_client_t client) | ||
39 | mobilesync_error_t mobilesync_get_changes_from_device(mobilesync_client_t client) | ||
40 | mobilesync_error_t mobilesync_receive_changes(mobilesync_client_t client, plist.plist_t *entities, uint8_t *is_last_record, plist.plist_t *actions) | ||
41 | mobilesync_error_t mobilesync_acknowledge_changes_from_device(mobilesync_client_t client) | ||
42 | |||
43 | mobilesync_error_t mobilesync_ready_to_send_changes_from_computer(mobilesync_client_t client) | ||
44 | mobilesync_error_t mobilesync_send_changes(mobilesync_client_t client, plist.plist_t changes, uint8_t is_last_record, plist.plist_t actions) | ||
45 | mobilesync_error_t mobilesync_remap_identifiers(mobilesync_client_t client, plist.plist_t *mapping) | ||
46 | |||
47 | mobilesync_anchors_t mobilesync_anchors_new(char *device_anchor, char *computer_anchor) | ||
48 | void mobilesync_anchors_free(mobilesync_anchors_t anchors) | ||
49 | |||
50 | plist.plist_t mobilesync_actions_new() | ||
51 | void mobilesync_actions_add(plist.plist_t actions, ...) | ||
52 | void mobilesync_actions_free(plist.plist_t actions) | ||
53 | |||
54 | SYNC_TYPE_FAST = MOBILESYNC_SYNC_TYPE_FAST | ||
55 | SYNC_TYPE_SLOW = MOBILESYNC_SYNC_TYPE_SLOW | ||
56 | SYNC_TYPE_RESET = MOBILESYNC_SYNC_TYPE_RESET | ||
57 | |||
58 | cdef class MobileSyncError(BaseError): | ||
59 | def __init__(self, *args, **kwargs): | ||
60 | self._lookup_table = { | ||
61 | MOBILESYNC_E_SUCCESS: "Success", | ||
62 | MOBILESYNC_E_INVALID_ARG: "Invalid argument", | ||
63 | MOBILESYNC_E_PLIST_ERROR: "Property list error", | ||
64 | MOBILESYNC_E_MUX_ERROR: "MUX error", | ||
65 | MOBILESYNC_E_SSL_ERROR: "SSL error", | ||
66 | MOBILESYNC_E_RECEIVE_TIMEOUT: "Receive timeout", | ||
67 | MOBILESYNC_E_BAD_VERSION: "Bad version", | ||
68 | MOBILESYNC_E_SYNC_REFUSED: "Sync refused", | ||
69 | MOBILESYNC_E_CANCELLED: "Sync cancelled", | ||
70 | MOBILESYNC_E_WRONG_DIRECTION: "Wrong sync direction", | ||
71 | MOBILESYNC_E_NOT_READY: "Not ready to receive changes", | ||
72 | MOBILESYNC_E_UNKNOWN_ERROR: "Unknown error" | ||
73 | } | ||
74 | BaseError.__init__(self, *args, **kwargs) | ||
75 | |||
76 | cdef class MobileSyncClient(DeviceLinkService): | ||
77 | __service_name__ = "com.apple.mobilesync" | ||
78 | cdef mobilesync_client_t _c_client | ||
79 | |||
80 | def __cinit__(self, iDevice device not None, LockdownServiceDescriptor descriptor, *args, **kwargs): | ||
81 | self.handle_error(mobilesync_client_new(device._c_dev, descriptor._c_service_descriptor, &(self._c_client))) | ||
82 | |||
83 | def __dealloc__(self): | ||
84 | cdef mobilesync_error_t err | ||
85 | if self._c_client is not NULL: | ||
86 | err = mobilesync_client_free(self._c_client) | ||
87 | self.handle_error(err) | ||
88 | |||
89 | cpdef tuple start(self, bytes data_class, bytes device_anchor, bytes host_anchor): | ||
90 | cdef: | ||
91 | mobilesync_anchors_t anchors = NULL | ||
92 | mobilesync_sync_type_t sync_type | ||
93 | uint64_t computer_data_class_version = 1 | ||
94 | uint64_t device_data_class_version | ||
95 | char* error_description = NULL | ||
96 | |||
97 | if device_anchor is None: | ||
98 | anchors = mobilesync_anchors_new(NULL, host_anchor) | ||
99 | else: | ||
100 | anchors = mobilesync_anchors_new(device_anchor, host_anchor) | ||
101 | |||
102 | try: | ||
103 | self.handle_error(mobilesync_start(self._c_client, data_class, anchors, computer_data_class_version, &sync_type, &device_data_class_version, &error_description)) | ||
104 | return (sync_type, <bint>computer_data_class_version, <bint>device_data_class_version, <bytes>error_description) | ||
105 | except Exception, e: | ||
106 | raise | ||
107 | finally: | ||
108 | mobilesync_anchors_free(anchors) | ||
109 | |||
110 | cpdef finish(self): | ||
111 | self.handle_error(mobilesync_finish(self._c_client)) | ||
112 | |||
113 | cpdef cancel(self, bytes reason): | ||
114 | self.handle_error(mobilesync_cancel(self._c_client, reason)) | ||
115 | |||
116 | cpdef get_all_records_from_device(self): | ||
117 | self.handle_error(mobilesync_get_all_records_from_device(self._c_client)) | ||
118 | |||
119 | cpdef get_changes_from_device(self): | ||
120 | self.handle_error(mobilesync_get_changes_from_device(self._c_client)) | ||
121 | |||
122 | cpdef tuple receive_changes(self): | ||
123 | cdef: | ||
124 | plist.plist_t entities = NULL | ||
125 | uint8_t is_last_record = 0 | ||
126 | plist.plist_t actions = NULL | ||
127 | try: | ||
128 | self.handle_error(mobilesync_receive_changes(self._c_client, &entities, &is_last_record, &actions)) | ||
129 | return (plist.plist_t_to_node(entities), <bint>is_last_record, plist.plist_t_to_node(actions)) | ||
130 | except Exception, e: | ||
131 | if entities != NULL: | ||
132 | plist.plist_free(entities) | ||
133 | if actions != NULL: | ||
134 | plist.plist_free(actions) | ||
135 | raise | ||
136 | |||
137 | cpdef acknowledge_changes_from_device(self): | ||
138 | self.handle_error(mobilesync_acknowledge_changes_from_device(self._c_client)) | ||
139 | |||
140 | cpdef ready_to_send_changes_from_computer(self): | ||
141 | self.handle_error(mobilesync_ready_to_send_changes_from_computer(self._c_client)) | ||
142 | |||
143 | cpdef send_changes(self, plist.Node changes, bint is_last_record, plist.Node actions): | ||
144 | self.handle_error(mobilesync_send_changes(self._c_client, changes._c_node, is_last_record, actions._c_node)) | ||
145 | |||
146 | cpdef remap_identifiers(self): | ||
147 | cdef plist.plist_t remapping = NULL | ||
148 | |||
149 | try: | ||
150 | self.handle_error(mobilesync_remap_identifiers(self._c_client, &remapping)) | ||
151 | return plist.plist_t_to_node(remapping) | ||
152 | except Exception, e: | ||
153 | if remapping != NULL: | ||
154 | plist.plist_free(remapping) | ||
155 | raise | ||
156 | |||
157 | cdef int16_t _send(self, plist.plist_t node): | ||
158 | return mobilesync_send(self._c_client, node) | ||
159 | |||
160 | cdef int16_t _receive(self, plist.plist_t* node): | ||
161 | return mobilesync_receive(self._c_client, node) | ||
162 | |||
163 | cdef inline BaseError _error(self, int16_t ret): | ||
164 | return MobileSyncError(ret) | ||