diff options
Diffstat (limited to 'cython/imobiledevice.pyx')
| -rw-r--r-- | cython/imobiledevice.pyx | 79 |
1 files changed, 71 insertions, 8 deletions
diff --git a/cython/imobiledevice.pyx b/cython/imobiledevice.pyx index 9d2e13d..8da2296 100644 --- a/cython/imobiledevice.pyx +++ b/cython/imobiledevice.pyx @@ -7,7 +7,10 @@ cdef class BaseError(Exception): property message: def __get__(self): - return self._lookup_table[self._c_errcode] + if self._c_errcode in self._lookup_table: + return self._lookup_table[self._c_errcode] + else: + return "Unknown error ({0})".format(self._c_errcode) property code: def __get__(self): @@ -35,8 +38,12 @@ cdef extern from "libimobiledevice/libimobiledevice.h": IDEVICE_E_UNKNOWN_ERROR = -2 IDEVICE_E_NO_DEVICE = -3 IDEVICE_E_NOT_ENOUGH_DATA = -4 - IDEVICE_E_BAD_HEADER = -5 IDEVICE_E_SSL_ERROR = -6 + IDEVICE_E_TIMEOUT = -7 + cdef enum idevice_options: + IDEVICE_LOOKUP_USBMUX = 1 << 1 + IDEVICE_LOOKUP_NETWORK = 1 << 2 + IDEVICE_LOOKUP_PREFER_NETWORK = 1 << 3 ctypedef void (*idevice_event_cb_t) (const_idevice_event_t event, void *user_data) cdef extern idevice_error_t idevice_event_subscribe(idevice_event_cb_t callback, void *user_data) cdef extern idevice_error_t idevice_event_unsubscribe() @@ -44,6 +51,7 @@ cdef extern from "libimobiledevice/libimobiledevice.h": idevice_error_t idevice_device_list_free(char **devices) void idevice_set_debug_level(int level) idevice_error_t idevice_new(idevice_t *device, char *udid) + idevice_error_t idevice_new_with_options(idevice_t *device, const char *udid, idevice_options options); idevice_error_t idevice_free(idevice_t device) idevice_error_t idevice_get_udid(idevice_t device, char** udid) idevice_error_t idevice_get_handle(idevice_t device, uint32_t *handle) @@ -61,8 +69,8 @@ cdef class iDeviceError(BaseError): IDEVICE_E_UNKNOWN_ERROR: 'Unknown error', IDEVICE_E_NO_DEVICE: 'No device', IDEVICE_E_NOT_ENOUGH_DATA: 'Not enough data', - IDEVICE_E_BAD_HEADER: 'Bad header', - IDEVICE_E_SSL_ERROR: 'SSL Error' + IDEVICE_E_SSL_ERROR: 'SSL Error', + IDEVICE_E_TIMEOUT: 'Connection timeout' } BaseError.__init__(self, *args, **kwargs) @@ -86,7 +94,7 @@ cdef class iDeviceEvent: def __get__(self): return self._c_event.conn_type -cdef void idevice_event_cb(const_idevice_event_t c_event, void *user_data) with gil: +cdef void idevice_event_cb(const_idevice_event_t c_event, void *user_data) noexcept: cdef iDeviceEvent event = iDeviceEvent.__new__(iDeviceEvent) event._c_event = c_event (<object>user_data)(event) @@ -125,12 +133,42 @@ cdef class iDeviceConnection(Base): def __init__(self, *args, **kwargs): raise TypeError("iDeviceConnection cannot be instantiated. Please use iDevice.connect()") + cpdef bytes receive_timeout(self, uint32_t max_len, unsigned int timeout): + cdef: + uint32_t bytes_received + char* c_data = <char *>malloc(max_len) + bytes result + + try: + self.handle_error(idevice_connection_receive_timeout(self._c_connection, c_data, max_len, &bytes_received, timeout)) + result = c_data[:bytes_received] + return result + except BaseError, e: + raise + finally: + free(c_data) + + cpdef bytes receive(self, max_len): + cdef: + uint32_t bytes_received + char* c_data = <char *>malloc(max_len) + bytes result + + try: + self.handle_error(idevice_connection_receive(self._c_connection, c_data, max_len, &bytes_received)) + result = c_data[:bytes_received] + return result + except BaseError, e: + raise + finally: + free(c_data) + cpdef disconnect(self): cdef idevice_error_t err err = idevice_disconnect(self._c_connection) self.handle_error(err) - cdef inline BaseError _error(self, int16_t ret): + cdef BaseError _error(self, int16_t ret): return iDeviceError(ret) from libc.stdlib cimport * @@ -138,7 +176,7 @@ from libc.stdlib cimport * cdef class iDevice(Base): def __cinit__(self, object udid=None, *args, **kwargs): cdef char* c_udid = NULL - if isinstance(udid, basestring): + if isinstance(udid, (str, bytes)): c_udid = <bytes>udid elif udid is not None: raise TypeError("iDevice's constructor takes a string or None as the udid argument") @@ -148,7 +186,7 @@ cdef class iDevice(Base): if self._c_dev is not NULL: self.handle_error(idevice_free(self._c_dev)) - cdef inline BaseError _error(self, int16_t ret): + cdef BaseError _error(self, int16_t ret): return iDeviceError(ret) cpdef iDeviceConnection connect(self, uint16_t port): @@ -210,12 +248,29 @@ cdef class PropertyListService(BaseService): plist.plist_free(c_node) raise + cpdef object receive_with_timeout(self, int timeout_ms): + cdef: + plist.plist_t c_node = NULL + int16_t err + err = self._receive_with_timeout(&c_node, timeout_ms) + try: + self.handle_error(err) + + return plist.plist_t_to_node(c_node) + except BaseError, e: + if c_node != NULL: + plist.plist_free(c_node) + raise + cdef int16_t _send(self, plist.plist_t node): raise NotImplementedError("send is not implemented") cdef int16_t _receive(self, plist.plist_t* c_node): raise NotImplementedError("receive is not implemented") + cdef int16_t _receive_with_timeout(self, plist.plist_t* c_node, int timeout_ms): + raise NotImplementedError("receive_with_timeout is not implemented") + cdef class DeviceLinkService(PropertyListService): pass @@ -224,8 +279,16 @@ include "mobilesync.pxi" include "notification_proxy.pxi" include "sbservices.pxi" include "mobilebackup.pxi" +include "mobilebackup2.pxi" include "afc.pxi" include "file_relay.pxi" include "screenshotr.pxi" include "installation_proxy.pxi" include "mobile_image_mounter.pxi" +include "webinspector.pxi" +include "heartbeat.pxi" +include "diagnostics_relay.pxi" +include "misagent.pxi" +include "house_arrest.pxi" +include "restore.pxi" +include "debugserver.pxi" |
