summaryrefslogtreecommitdiffstats
path: root/cython/mobilebackup2.pxi
diff options
context:
space:
mode:
Diffstat (limited to 'cython/mobilebackup2.pxi')
-rw-r--r--cython/mobilebackup2.pxi123
1 files changed, 123 insertions, 0 deletions
diff --git a/cython/mobilebackup2.pxi b/cython/mobilebackup2.pxi
new file mode 100644
index 0000000..4b47e5b
--- /dev/null
+++ b/cython/mobilebackup2.pxi
@@ -0,0 +1,123 @@
1cdef extern from "libimobiledevice/mobilebackup2.h":
2 cdef struct mobilebackup2_client_private:
3 pass
4 ctypedef mobilebackup2_client_private *mobilebackup2_client_t
5
6 ctypedef enum mobilebackup2_error_t:
7 MOBILEBACKUP2_E_SUCCESS = 0
8 MOBILEBACKUP2_E_INVALID_ARG = -1
9 MOBILEBACKUP2_E_PLIST_ERROR = -2
10 MOBILEBACKUP2_E_MUX_ERROR = -3
11 MOBILEBACKUP2_E_SSL_ERROR = -4
12 MOBILEBACKUP2_E_RECEIVE_TIMEOUT = -5
13 MOBILEBACKUP2_E_BAD_VERSION = -6
14 MOBILEBACKUP2_E_REPLY_NOT_OK = -7
15 MOBILEBACKUP2_E_NO_COMMON_VERSION = -8
16 MOBILEBACKUP2_E_UNKNOWN_ERROR = -256
17
18 mobilebackup2_error_t mobilebackup2_client_new(idevice_t device, lockdownd_service_descriptor_t descriptor, mobilebackup2_client_t * client)
19 mobilebackup2_error_t mobilebackup2_client_free(mobilebackup2_client_t client)
20
21 mobilebackup2_error_t mobilebackup2_send_message(mobilebackup2_client_t client, char *message, plist.plist_t options)
22 mobilebackup2_error_t mobilebackup2_receive_message(mobilebackup2_client_t client, plist.plist_t *msg_plist, char **dlmessage)
23 mobilebackup2_error_t mobilebackup2_send_raw(mobilebackup2_client_t client, char *data, uint32_t length, uint32_t *bytes)
24 mobilebackup2_error_t mobilebackup2_receive_raw(mobilebackup2_client_t client, char *data, uint32_t length, uint32_t *bytes)
25 mobilebackup2_error_t mobilebackup2_version_exchange(mobilebackup2_client_t client, double local_versions[], char count, double *remote_version)
26 mobilebackup2_error_t mobilebackup2_send_request(mobilebackup2_client_t client, char *request, char *target_identifier, char *source_identifier, plist.plist_t options)
27 mobilebackup2_error_t mobilebackup2_send_status_response(mobilebackup2_client_t client, int status_code, char *status1, plist.plist_t status2)
28
29cdef class MobileBackup2Error(BaseError):
30 def __init__(self, *args, **kwargs):
31 self._lookup_table = {
32 MOBILEBACKUP2_E_SUCCESS: "Success",
33 MOBILEBACKUP2_E_INVALID_ARG: "Invalid argument",
34 MOBILEBACKUP2_E_PLIST_ERROR: "Property list error",
35 MOBILEBACKUP2_E_MUX_ERROR: "MUX error",
36 MOBILEBACKUP2_E_SSL_ERROR: "SSL error",
37 MOBILEBACKUP2_E_RECEIVE_TIMEOUT: "Receive timeout",
38 MOBILEBACKUP2_E_BAD_VERSION: "Bad version",
39 MOBILEBACKUP2_E_REPLY_NOT_OK: "Reply not OK",
40 MOBILEBACKUP2_E_NO_COMMON_VERSION: "No common version",
41 MOBILEBACKUP2_E_UNKNOWN_ERROR: "Unknown error"
42 }
43 BaseError.__init__(self, *args, **kwargs)
44
45cdef class MobileBackup2Client(PropertyListService):
46 __service_name__ = "com.apple.mobilebackup2"
47 cdef mobilebackup2_client_t _c_client
48
49 def __cinit__(self, iDevice device not None, LockdownServiceDescriptor descriptor, *args, **kwargs):
50 self.handle_error(mobilebackup2_client_new(device._c_dev, descriptor._c_service_descriptor, &self._c_client))
51
52 def __dealloc__(self):
53 cdef mobilebackup2_error_t err
54 if self._c_client is not NULL:
55 err = mobilebackup2_client_free(self._c_client)
56 self.handle_error(err)
57
58 cdef inline BaseError _error(self, int16_t ret):
59 return MobileBackup2Error(ret)
60
61 cpdef send_message(self, bytes message, plist.Node options):
62 self.handle_error(mobilebackup2_send_message(self._c_client, message, options._c_node))
63
64 cpdef tuple receive_message(self):
65 cdef:
66 char* dlmessage = NULL
67 plist.plist_t c_node = NULL
68 mobilebackup2_error_t err
69 err = mobilebackup2_receive_message(self._c_client, &c_node, &dlmessage)
70 try:
71 self.handle_error(err)
72 return (plist.plist_t_to_node(c_node), <bytes>dlmessage)
73 except BaseError, e:
74 if c_node != NULL:
75 plist.plist_free(c_node)
76 if dlmessage != NULL:
77 free(dlmessage)
78 raise
79
80 cpdef int send_raw(self, bytes data, int length):
81 cdef:
82 uint32_t bytes_recvd = 0
83 mobilebackup2_error_t err
84 err = mobilebackup2_send_raw(self._c_client, data, length, &bytes_recvd)
85 try:
86 self.handle_error(err)
87 return <bint>bytes_recvd
88 except BaseError, e:
89 raise
90
91 cpdef int receive_raw(self, bytearray data, int length):
92 cdef:
93 uint32_t bytes_recvd = 0
94 mobilebackup2_error_t err
95 err = mobilebackup2_receive_raw(self._c_client, data, length, &bytes_recvd)
96
97 # Throwing an exception when we test if theres more data to read is excessive
98 if err == -1 and bytes_recvd == 0:
99 return 0
100
101 try:
102 self.handle_error(err)
103 return <bint>bytes_recvd
104 except BaseError, e:
105 raise
106
107 cpdef float version_exchange(self, double[::1] local_versions):
108 cdef:
109 double[::1] temp = None
110 double remote_version = 0.0
111 mobilebackup2_error_t err
112 err = mobilebackup2_version_exchange(self._c_client, &local_versions[0], len(local_versions), &remote_version)
113 try:
114 self.handle_error(err)
115 return <float>remote_version
116 except BaseError, e:
117 raise
118
119 cpdef send_request(self, bytes request, bytes target_identifier, bytes source_identifier, plist.Node options):
120 self.handle_error(mobilebackup2_send_request(self._c_client, request, target_identifier, source_identifier, options._c_node))
121
122 cpdef send_status_response(self, int status_code, bytes status1, plist.Node status2):
123 self.handle_error(mobilebackup2_send_status_response(self._c_client, status_code, status1, status2._c_node))