mobilebackup2.pxi 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. cdef extern from "libimobiledevice/mobilebackup2.h":
  2. cdef struct mobilebackup2_client_private:
  3. pass
  4. ctypedef mobilebackup2_client_private *mobilebackup2_client_t
  5. ctypedef enum mobilebackup2_error_t:
  6. MOBILEBACKUP2_E_SUCCESS = 0
  7. MOBILEBACKUP2_E_INVALID_ARG = -1
  8. MOBILEBACKUP2_E_PLIST_ERROR = -2
  9. MOBILEBACKUP2_E_MUX_ERROR = -3
  10. MOBILEBACKUP2_E_BAD_VERSION = -4
  11. MOBILEBACKUP2_E_REPLY_NOT_OK = -5
  12. MOBILEBACKUP2_E_NO_COMMON_VERSION = -6
  13. MOBILEBACKUP2_E_UNKNOWN_ERROR = -256
  14. mobilebackup2_error_t mobilebackup2_client_new(idevice_t device, lockdownd_service_descriptor_t descriptor, mobilebackup2_client_t * client)
  15. mobilebackup2_error_t mobilebackup2_client_free(mobilebackup2_client_t client)
  16. mobilebackup2_error_t mobilebackup2_send_message(mobilebackup2_client_t client, char *message, plist.plist_t options)
  17. mobilebackup2_error_t mobilebackup2_receive_message(mobilebackup2_client_t client, plist.plist_t *msg_plist, char **dlmessage)
  18. mobilebackup2_error_t mobilebackup2_send_raw(mobilebackup2_client_t client, char *data, uint32_t length, uint32_t *bytes)
  19. mobilebackup2_error_t mobilebackup2_receive_raw(mobilebackup2_client_t client, char *data, uint32_t length, uint32_t *bytes)
  20. mobilebackup2_error_t mobilebackup2_version_exchange(mobilebackup2_client_t client, double local_versions[], char count, double *remote_version)
  21. mobilebackup2_error_t mobilebackup2_send_request(mobilebackup2_client_t client, char *request, char *target_identifier, char *source_identifier, plist.plist_t options)
  22. mobilebackup2_error_t mobilebackup2_send_status_response(mobilebackup2_client_t client, int status_code, char *status1, plist.plist_t status2)
  23. cdef class MobileBackup2Error(BaseError):
  24. def __init__(self, *args, **kwargs):
  25. self._lookup_table = {
  26. MOBILEBACKUP2_E_SUCCESS: "Success",
  27. MOBILEBACKUP2_E_INVALID_ARG: "Invalid argument",
  28. MOBILEBACKUP2_E_PLIST_ERROR: "Property list error",
  29. MOBILEBACKUP2_E_MUX_ERROR: "MUX error",
  30. MOBILEBACKUP2_E_BAD_VERSION: "Bad version",
  31. MOBILEBACKUP2_E_REPLY_NOT_OK: "Reply not OK",
  32. MOBILEBACKUP2_E_NO_COMMON_VERSION: "No common version",
  33. MOBILEBACKUP2_E_UNKNOWN_ERROR: "Unknown error"
  34. }
  35. BaseError.__init__(self, *args, **kwargs)
  36. cdef class MobileBackup2Client(PropertyListService):
  37. __service_name__ = "com.apple.mobilebackup2"
  38. cdef mobilebackup2_client_t _c_client
  39. def __cinit__(self, iDevice device not None, LockdownServiceDescriptor descriptor, *args, **kwargs):
  40. self.handle_error(mobilebackup2_client_new(device._c_dev, descriptor._c_service_descriptor, &self._c_client))
  41. def __dealloc__(self):
  42. cdef mobilebackup2_error_t err
  43. if self._c_client is not NULL:
  44. err = mobilebackup2_client_free(self._c_client)
  45. self.handle_error(err)
  46. cdef inline BaseError _error(self, int16_t ret):
  47. return MobileBackup2Error(ret)
  48. cdef send_message(self, bytes message, plist.Node options):
  49. self.handle_error(mobilebackup2_send_message(self._c_client, message, options._c_node))
  50. cdef tuple receive_message(self):
  51. cdef:
  52. char* dlmessage = NULL
  53. plist.plist_t c_node = NULL
  54. mobilebackup2_error_t err
  55. err = mobilebackup2_receive_message(self._c_client, &c_node, &dlmessage)
  56. try:
  57. self.handle_error(err)
  58. return (plist.plist_t_to_node(c_node), <bytes>dlmessage)
  59. except BaseError, e:
  60. if c_node != NULL:
  61. plist.plist_free(c_node)
  62. if dlmessage != NULL:
  63. free(dlmessage)
  64. raise
  65. cdef int send_raw(self, bytes data, int length):
  66. cdef:
  67. uint32_t bytes = 0
  68. mobilebackup2_error_t err
  69. err = mobilebackup2_send_raw(self._c_client, data, length, &bytes)
  70. try:
  71. self.handle_error(err)
  72. return <bint>bytes
  73. except BaseError, e:
  74. raise
  75. cdef int receive_raw(self, bytes data, int length):
  76. cdef:
  77. uint32_t bytes = 0
  78. mobilebackup2_error_t err
  79. err = mobilebackup2_receive_raw(self._c_client, data, length, &bytes)
  80. try:
  81. self.handle_error(err)
  82. return <bint>bytes
  83. except BaseError, e:
  84. raise
  85. cdef float version_exchange(self, double[::1] local_versions):
  86. cdef:
  87. double[::1] temp = None
  88. double remote_version = 0.0
  89. mobilebackup2_error_t err
  90. err = mobilebackup2_version_exchange(self._c_client, &local_versions[0], len(local_versions), &remote_version)
  91. try:
  92. self.handle_error(err)
  93. return <float>remote_version
  94. except BaseError, e:
  95. raise
  96. cdef send_request(self, bytes request, bytes target_identifier, bytes source_identifier, plist.Node options):
  97. self.handle_error(mobilebackup2_send_request(self._c_client, request, target_identifier, source_identifier, options._c_node))
  98. cdef send_status_response(self, int status_code, bytes status1, plist.Node status2):
  99. self.handle_error(mobilebackup2_send_status_response(self._c_client, status_code, status1, status2._c_node))