diff options
| author | 2010-04-09 16:52:30 -0500 | |
|---|---|---|
| committer | 2012-03-20 23:25:55 +0100 | |
| commit | bea5efe442daeab05d5d7a2e9d9e7b934ba6e684 (patch) | |
| tree | 0346eebd799517c0976e640db7241d0c72cf7d95 /cython/lockdown.pxi | |
| parent | acac4f819ccafa6f6bb945626f2e21ec2b75074b (diff) | |
| download | libimobiledevice-bea5efe442daeab05d5d7a2e9d9e7b934ba6e684.tar.gz libimobiledevice-bea5efe442daeab05d5d7a2e9d9e7b934ba6e684.tar.bz2 | |
Implemented hierarchy suggested by Martin S.
Implemented new BaseService constructors.
Moved LockdownClient to lockdown.pxi.
Implemented more of the afc interface.
Diffstat (limited to 'cython/lockdown.pxi')
| -rw-r--r-- | cython/lockdown.pxi | 254 |
1 files changed, 254 insertions, 0 deletions
diff --git a/cython/lockdown.pxi b/cython/lockdown.pxi new file mode 100644 index 0000000..76f84b1 --- /dev/null +++ b/cython/lockdown.pxi | |||
| @@ -0,0 +1,254 @@ | |||
| 1 | cdef extern from "libimobiledevice/lockdown.h": | ||
| 2 | ctypedef enum lockdownd_error_t: | ||
| 3 | LOCKDOWN_E_SUCCESS = 0 | ||
| 4 | LOCKDOWN_E_INVALID_ARG = -1 | ||
| 5 | LOCKDOWN_E_INVALID_CONF = -2 | ||
| 6 | LOCKDOWN_E_PLIST_ERROR = -3 | ||
| 7 | LOCKDOWN_E_PAIRING_FAILED = -4 | ||
| 8 | LOCKDOWN_E_SSL_ERROR = -5 | ||
| 9 | LOCKDOWN_E_DICT_ERROR = -6 | ||
| 10 | LOCKDOWN_E_START_SERVICE_FAILED = -7 | ||
| 11 | LOCKDOWN_E_NOT_ENOUGH_DATA = -8 | ||
| 12 | LOCKDOWN_E_SET_VALUE_PROHIBITED = -9 | ||
| 13 | LOCKDOWN_E_GET_VALUE_PROHIBITED = -10 | ||
| 14 | LOCKDOWN_E_REMOVE_VALUE_PROHIBITED = -11 | ||
| 15 | LOCKDOWN_E_MUX_ERROR = -12 | ||
| 16 | LOCKDOWN_E_ACTIVATION_FAILED = -13 | ||
| 17 | LOCKDOWN_E_PASSWORD_PROTECTED = -14 | ||
| 18 | LOCKDOWN_E_NO_RUNNING_SESSION = -15 | ||
| 19 | LOCKDOWN_E_INVALID_HOST_ID = -16 | ||
| 20 | LOCKDOWN_E_INVALID_SERVICE = -17 | ||
| 21 | LOCKDOWN_E_INVALID_ACTIVATION_RECORD = -18 | ||
| 22 | LOCKDOWN_E_UNKNOWN_ERROR = -256 | ||
| 23 | |||
| 24 | lockdownd_error_t lockdownd_client_new(idevice_t device, lockdownd_client_t *client, char *label) | ||
| 25 | lockdownd_error_t lockdownd_client_new_with_handshake(idevice_t device, lockdownd_client_t *client, char *label) | ||
| 26 | lockdownd_error_t lockdownd_client_free(lockdownd_client_t client) | ||
| 27 | |||
| 28 | lockdownd_error_t lockdownd_query_type(lockdownd_client_t client, char **tp) | ||
| 29 | lockdownd_error_t lockdownd_get_value(lockdownd_client_t client, char *domain, char *key, plist.plist_t *value) | ||
| 30 | lockdownd_error_t lockdownd_set_value(lockdownd_client_t client, char *domain, char *key, plist.plist_t value) | ||
| 31 | lockdownd_error_t lockdownd_remove_value(lockdownd_client_t client, char *domain, char *key) | ||
| 32 | lockdownd_error_t lockdownd_start_service(lockdownd_client_t client, char *service, uint16_t *port) | ||
| 33 | lockdownd_error_t lockdownd_start_session(lockdownd_client_t client, char *host_id, char **session_id, int *ssl_enabled) | ||
| 34 | lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client, char *session_id) | ||
| 35 | lockdownd_error_t lockdownd_send(lockdownd_client_t client, plist.plist_t plist) | ||
| 36 | lockdownd_error_t lockdownd_receive(lockdownd_client_t client, plist.plist_t *plist) | ||
| 37 | lockdownd_error_t lockdownd_pair(lockdownd_client_t client, lockdownd_pair_record_t pair_record) | ||
| 38 | lockdownd_error_t lockdownd_validate_pair(lockdownd_client_t client, lockdownd_pair_record_t pair_record) | ||
| 39 | lockdownd_error_t lockdownd_unpair(lockdownd_client_t client, lockdownd_pair_record_t pair_record) | ||
| 40 | lockdownd_error_t lockdownd_activate(lockdownd_client_t client, plist.plist_t activation_record) | ||
| 41 | lockdownd_error_t lockdownd_deactivate(lockdownd_client_t client) | ||
| 42 | lockdownd_error_t lockdownd_enter_recovery(lockdownd_client_t client) | ||
| 43 | lockdownd_error_t lockdownd_goodbye(lockdownd_client_t client) | ||
| 44 | |||
| 45 | cdef class LockdownError(BaseError): | ||
| 46 | def __init__(self, *args, **kwargs): | ||
| 47 | self._lookup_table = { | ||
| 48 | LOCKDOWN_E_SUCCESS: "Success", | ||
| 49 | LOCKDOWN_E_INVALID_ARG: "Invalid argument", | ||
| 50 | LOCKDOWN_E_INVALID_CONF: "Invalid configuration", | ||
| 51 | LOCKDOWN_E_PLIST_ERROR: "Property list error", | ||
| 52 | LOCKDOWN_E_PAIRING_FAILED: "Pairing failed", | ||
| 53 | LOCKDOWN_E_SSL_ERROR: "SSL error", | ||
| 54 | LOCKDOWN_E_DICT_ERROR: "Dict error", | ||
| 55 | LOCKDOWN_E_START_SERVICE_FAILED: "Start service failed", | ||
| 56 | LOCKDOWN_E_NOT_ENOUGH_DATA: "Not enough data", | ||
| 57 | LOCKDOWN_E_SET_VALUE_PROHIBITED: "Set value prohibited", | ||
| 58 | LOCKDOWN_E_GET_VALUE_PROHIBITED: "Get value prohibited", | ||
| 59 | LOCKDOWN_E_REMOVE_VALUE_PROHIBITED: "Remove value prohibited", | ||
| 60 | LOCKDOWN_E_MUX_ERROR: "MUX Error", | ||
| 61 | LOCKDOWN_E_ACTIVATION_FAILED: "Activation failed", | ||
| 62 | LOCKDOWN_E_PASSWORD_PROTECTED: "Password protected", | ||
| 63 | LOCKDOWN_E_NO_RUNNING_SESSION: "No running session", | ||
| 64 | LOCKDOWN_E_INVALID_HOST_ID: "Invalid host ID", | ||
| 65 | LOCKDOWN_E_INVALID_SERVICE: "Invalid service", | ||
| 66 | LOCKDOWN_E_INVALID_ACTIVATION_RECORD: "Invalid activation record", | ||
| 67 | LOCKDOWN_E_UNKNOWN_ERROR: "Unknown error" | ||
| 68 | } | ||
| 69 | BaseError.__init__(self, *args, **kwargs) | ||
| 70 | |||
| 71 | cdef class LockdownPairRecord: | ||
| 72 | #def __cinit__(self, bytes device_certificate, bytes host_certificate, bytes host_id, bytes root_certificate, *args, **kwargs): | ||
| 73 | property device_certificate: | ||
| 74 | def __get__(self): | ||
| 75 | cdef bytes result = self._c_record.device_certificate | ||
| 76 | return result | ||
| 77 | property host_certificate: | ||
| 78 | def __get__(self): | ||
| 79 | cdef bytes result = self._c_record.host_certificate | ||
| 80 | return result | ||
| 81 | property host_id: | ||
| 82 | def __get__(self): | ||
| 83 | cdef bytes result = self._c_record.host_id | ||
| 84 | return result | ||
| 85 | property root_certificate: | ||
| 86 | def __get__(self): | ||
| 87 | cdef bytes result = self._c_record.root_certificate | ||
| 88 | return result | ||
| 89 | |||
| 90 | cdef class LockdownClient(PropertyListService): | ||
| 91 | def __cinit__(self, iDevice device not None, bytes label="", bool handshake=True, *args, **kwargs): | ||
| 92 | cdef: | ||
| 93 | iDevice dev = device | ||
| 94 | lockdownd_error_t err | ||
| 95 | char* c_label = NULL | ||
| 96 | if label: | ||
| 97 | c_label = label | ||
| 98 | if handshake: | ||
| 99 | err = lockdownd_client_new_with_handshake(dev._c_dev, &self._c_client, c_label) | ||
| 100 | else: | ||
| 101 | err = lockdownd_client_new(dev._c_dev, &self._c_client, c_label) | ||
| 102 | self.handle_error(err) | ||
| 103 | |||
| 104 | self.device = dev | ||
| 105 | |||
| 106 | def __dealloc__(self): | ||
| 107 | cdef lockdownd_error_t err | ||
| 108 | if self._c_client is not NULL: | ||
| 109 | err = lockdownd_client_free(self._c_client) | ||
| 110 | self.handle_error(err) | ||
| 111 | |||
| 112 | cpdef bytes query_type(self): | ||
| 113 | cdef: | ||
| 114 | lockdownd_error_t err | ||
| 115 | char* c_type = NULL | ||
| 116 | bytes result | ||
| 117 | err = lockdownd_query_type(self._c_client, &c_type) | ||
| 118 | try: | ||
| 119 | self.handle_error(err) | ||
| 120 | except BaseError, e: | ||
| 121 | raise | ||
| 122 | finally: | ||
| 123 | if c_type != NULL: | ||
| 124 | result = c_type | ||
| 125 | free(c_type) | ||
| 126 | |||
| 127 | return result | ||
| 128 | |||
| 129 | cpdef plist.Node get_value(self, bytes domain=None, bytes key=None): | ||
| 130 | cdef: | ||
| 131 | lockdownd_error_t err | ||
| 132 | plist.plist_t c_node = NULL | ||
| 133 | char* c_domain = NULL | ||
| 134 | char* c_key = NULL | ||
| 135 | if domain is not None: | ||
| 136 | c_domain = domain | ||
| 137 | if key is not None: | ||
| 138 | c_key = key | ||
| 139 | err = lockdownd_get_value(self._c_client, c_domain, c_key, &c_node) | ||
| 140 | try: | ||
| 141 | self.handle_error(err) | ||
| 142 | except BaseError, e: | ||
| 143 | if c_node != NULL: | ||
| 144 | plist_free(c_node) | ||
| 145 | raise | ||
| 146 | |||
| 147 | return plist.plist_t_to_node(c_node) | ||
| 148 | |||
| 149 | cpdef set_value(self, bytes domain, bytes key, object value): | ||
| 150 | cdef plist.plist_t c_node = plist.native_to_plist_t(value) | ||
| 151 | try: | ||
| 152 | self.handle_error(lockdownd_set_value(self._c_client, domain, key, c_node)) | ||
| 153 | except BaseError, e: | ||
| 154 | raise | ||
| 155 | finally: | ||
| 156 | if c_node != NULL: | ||
| 157 | plist_free(c_node) | ||
| 158 | |||
| 159 | cpdef remove_value(self, bytes domain, bytes key): | ||
| 160 | self.handle_error(lockdownd_remove_value(self._c_client, domain, key)) | ||
| 161 | |||
| 162 | cpdef uint16_t start_service(self, object service): | ||
| 163 | cdef: | ||
| 164 | char* c_service_name = NULL | ||
| 165 | uint16_t port = 0 | ||
| 166 | |||
| 167 | if hasattr(service, '__service_name__') and \ | ||
| 168 | service.__service_name__ is not None \ | ||
| 169 | and isinstance(service.__service_name__, basestring): | ||
| 170 | c_service_name = <bytes>service.__service_name__ | ||
| 171 | elif isinstance(service, basestring): | ||
| 172 | c_service_name = <bytes>service | ||
| 173 | else: | ||
| 174 | raise TypeError("LockdownClient.start_service() takes a BaseService or string as its first argument") | ||
| 175 | |||
| 176 | try: | ||
| 177 | self.handle_error(lockdownd_start_service(self._c_client, c_service_name, &port)) | ||
| 178 | except BaseError, e: | ||
| 179 | raise | ||
| 180 | |||
| 181 | return port | ||
| 182 | |||
| 183 | cpdef object get_service_client(self, object service_class): | ||
| 184 | cdef: | ||
| 185 | uint16_t port = 0 | ||
| 186 | object result | ||
| 187 | |||
| 188 | if not hasattr(service_class, '__service_name__') and \ | ||
| 189 | not service_class.__service_name__ is not None \ | ||
| 190 | and not isinstance(service_class.__service_name__, basestring): | ||
| 191 | raise TypeError("LockdownClient.get_service_client() takes a BaseService as its first argument") | ||
| 192 | |||
| 193 | port = self.start_service(service_class) | ||
| 194 | return service_class(self.device, port) | ||
| 195 | |||
| 196 | cpdef tuple start_session(self, bytes host_id): | ||
| 197 | cdef: | ||
| 198 | lockdownd_error_t err | ||
| 199 | char* c_session_id = NULL | ||
| 200 | bint ssl_enabled | ||
| 201 | bytes session_id | ||
| 202 | err = lockdownd_start_session(self._c_client, host_id, &c_session_id, &ssl_enabled) | ||
| 203 | try: | ||
| 204 | self.handle_error(err) | ||
| 205 | except BaseError, e: | ||
| 206 | raise | ||
| 207 | finally: | ||
| 208 | if c_session_id != NULL: | ||
| 209 | session_id = c_session_id | ||
| 210 | free(c_session_id) | ||
| 211 | |||
| 212 | return (session_id, ssl_enabled) | ||
| 213 | |||
| 214 | cpdef stop_session(self, bytes session_id): | ||
| 215 | self.handle_error(lockdownd_stop_session(self._c_client, session_id)) | ||
| 216 | |||
| 217 | cpdef pair(self, object pair_record=None): | ||
| 218 | cdef lockdownd_pair_record_t c_pair_record = NULL | ||
| 219 | if pair_record is not None: | ||
| 220 | c_pair_record = (<LockdownPairRecord>pair_record)._c_record | ||
| 221 | self.handle_error(lockdownd_pair(self._c_client, c_pair_record)) | ||
| 222 | |||
| 223 | cpdef validate_pair(self, object pair_record=None): | ||
| 224 | cdef lockdownd_pair_record_t c_pair_record = NULL | ||
| 225 | if pair_record is not None: | ||
| 226 | c_pair_record = (<LockdownPairRecord>pair_record)._c_record | ||
| 227 | self.handle_error(lockdownd_validate_pair(self._c_client, c_pair_record)) | ||
| 228 | |||
| 229 | cpdef unpair(self, object pair_record=None): | ||
| 230 | cdef lockdownd_pair_record_t c_pair_record = NULL | ||
| 231 | if pair_record is not None: | ||
| 232 | c_pair_record = (<LockdownPairRecord>pair_record)._c_record | ||
| 233 | self.handle_error(lockdownd_unpair(self._c_client, c_pair_record)) | ||
| 234 | |||
| 235 | cpdef activate(self, plist.Node activation_record): | ||
| 236 | self.handle_error(lockdownd_activate(self._c_client, activation_record._c_node)) | ||
| 237 | |||
| 238 | cpdef deactivate(self): | ||
| 239 | self.handle_error(lockdownd_deactivate(self._c_client)) | ||
| 240 | |||
| 241 | cpdef enter_recovery(self): | ||
| 242 | self.handle_error(lockdownd_enter_recovery(self._c_client)) | ||
| 243 | |||
| 244 | cpdef goodbye(self): | ||
| 245 | self.handle_error(lockdownd_goodbye(self._c_client)) | ||
| 246 | |||
| 247 | cdef inline int16_t _send(self, plist.plist_t node): | ||
| 248 | return lockdownd_send(self._c_client, node) | ||
| 249 | |||
| 250 | cdef inline int16_t _receive(self, plist.plist_t* node): | ||
| 251 | return lockdownd_receive(self._c_client, node) | ||
| 252 | |||
| 253 | cdef inline BaseError _error(self, int16_t ret): | ||
| 254 | return LockdownError(ret) | ||
