rusty_libimobiledevice 0.1.10

An ergonomic library to communicate with iOS devices
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// jkcoxson

use std::ffi::{c_uint, CString};

use crate::{
    bindings as unsafe_bindings, error::MobileSyncError, idevice::Device,
    services::lockdownd::LockdowndService,
};

use plist_plus::{Plist, PlistType};

pub struct MobileSyncClient<'a> {
    pub(crate) pointer: unsafe_bindings::mobilesync_client_t,
    phantom: std::marker::PhantomData<&'a Device>,
}

pub struct MobileSyncAnchor {
    device_anchor: String,
    computer_anchor: String,
}

impl MobileSyncClient<'_> {
    /// Creates a new mobile sync service from a lockdown service
    /// # Arguments
    /// * `device` - The device to connect to
    /// * `descriptor` - The lockdown service to connect on
    /// # Returns
    /// A struct containing the handle to the connection
    ///
    /// ***Verified:*** False
    pub fn new(device: Device, descriptor: LockdowndService) -> Result<Self, MobileSyncError> {
        let mut pointer: unsafe_bindings::mobilesync_client_t = std::ptr::null_mut();
        let result = unsafe {
            unsafe_bindings::mobilesync_client_new(device.pointer, descriptor.pointer, &mut pointer)
        }
        .into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok(MobileSyncClient {
            pointer,
            phantom: std::marker::PhantomData,
        })
    }

    /// Starts a new connection and adds a mobile sync to it
    /// # Arguments
    /// * `device` - The device to connect to
    /// * `label` - The label for the connection
    /// # Returns
    /// A struct containing the handle to the connection
    ///
    /// ***Verified:*** False
    pub fn start_service(
        device: Device,
        label: impl Into<String>,
    ) -> Result<Self, MobileSyncError> {
        let label_c_string = CString::new(label.into()).unwrap();
        let mut pointer: unsafe_bindings::mobilesync_client_t = std::ptr::null_mut();
        let result = unsafe {
            unsafe_bindings::mobilesync_client_start_service(
                device.pointer,
                &mut pointer,
                label_c_string.as_ptr(),
            )
        }
        .into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok(MobileSyncClient {
            pointer,
            phantom: std::marker::PhantomData,
        })
    }

    /// Receives a message from the service.
    /// Blocks until a full plist has been received
    /// # Arguments
    /// *none*
    /// # Returns
    /// A plist containing the message
    ///
    /// ***Verified:*** False
    pub fn recieve(&self) -> Result<Plist, MobileSyncError> {
        let mut plist: unsafe_bindings::plist_t = std::ptr::null_mut();
        let result =
            unsafe { unsafe_bindings::mobilesync_receive(self.pointer, &mut plist) }.into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok(plist.into())
    }

    /// Sends a message to the service
    /// # Arguments
    /// * `message` - The message to send
    /// # Returns
    /// *none*
    ///
    /// ***Verified:*** False
    pub fn send(&self, message: Plist) -> Result<(), MobileSyncError> {
        let result =
            unsafe { unsafe_bindings::mobilesync_send(self.pointer, message.get_pointer()) }.into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok(())
    }

    /// Starts the syncing of data
    /// # Arguments
    /// * `data_class` - The identifiers to sync
    /// * `anchors` - The sync anchors to base off of
    /// * `computer_data_class_version` - The class version on the host
    /// * `sync_type` - The type of sync to perform
    /// # Returns
    /// *none*
    ///
    /// ***Verified:*** False
    pub fn start(
        &self,
        data_class: impl Into<String>,
        anchors: Vec<MobileSyncAnchor>,
        computer_data_class_version: u64,
        sync_type: MobileSyncType,
    ) -> Result<(), (String, MobileSyncError)> {
        let data_class_c_string = CString::new(data_class.into()).unwrap();

        let mut anchor_ptrs = Vec::new();
        for i in anchors {
            anchor_ptrs.push(unsafe_bindings::mobilesync_anchors_t::from(i));
        }
        anchor_ptrs.push(std::ptr::null_mut());

        let mut device_data_class_version = 0;

        let mut error_description = std::ptr::null_mut();

        let result = unsafe {
            unsafe_bindings::mobilesync_start(
                self.pointer,
                data_class_c_string.as_ptr(),
                anchor_ptrs[0],
                computer_data_class_version,
                &mut sync_type.into(),
                &mut device_data_class_version,
                &mut error_description,
            )
        }
        .into();

        if result != MobileSyncError::Success {
            return Err((
                unsafe { CString::from_raw(error_description) }
                    .into_string()
                    .unwrap(),
                result,
            ));
        }

        Ok(())
    }

    /// Cancels a sync request
    /// # Arguments
    /// * `reason` - The reason for cancelling the sync
    /// # Returns
    /// *none*
    ///
    /// ***Verified:*** False
    pub fn cancel(&self, reason: impl Into<String>) -> Result<(), MobileSyncError> {
        let reason_c_string = CString::new(reason.into()).unwrap();

        let result =
            unsafe { unsafe_bindings::mobilesync_cancel(self.pointer, reason_c_string.as_ptr()) }
                .into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok(())
    }

    /// Finishes the current sync
    /// # Arguments
    /// *none*
    /// # Returns
    /// *none*
    ///
    /// ***Verified:*** False
    pub fn finish(&self) -> Result<(), MobileSyncError> {
        let result = unsafe { unsafe_bindings::mobilesync_finish(self.pointer) }.into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok(())
    }

    /// Gets all sync records from the device
    /// # Arguments
    /// *none*
    /// # Returns
    /// The data, whether it's the end of the data and the anchors
    ///
    /// ***Verified:*** False
    pub fn get_all_records_from_device(&self) -> Result<(Plist, bool, Plist), MobileSyncError> {
        let result =
            unsafe { unsafe_bindings::mobilesync_get_all_records_from_device(self.pointer) }.into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        self.receive_changes()
    }

    /// Gets all the changes from the device
    /// # Arguments
    /// *none*
    /// # Returns
    /// The data, whether it's the end of the data and the anchors
    ///
    /// ***Verified:*** False
    pub fn get_changes_from_device(&self) -> Result<(Plist, bool, Plist), MobileSyncError> {
        let result =
            unsafe { unsafe_bindings::mobilesync_get_changes_from_device(self.pointer) }.into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        self.receive_changes()
    }

    /// Clears the records on the device
    /// # Arguments
    /// *none*
    /// # Returns
    /// *none*
    ///
    /// ***Verified:*** False
    pub fn clear_all_records_on_device(&self) -> Result<(), MobileSyncError> {
        let result =
            unsafe { unsafe_bindings::mobilesync_clear_all_records_on_device(self.pointer) }.into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok(())
    }

    /// Receive changes from the device
    /// # Arguments
    /// *none* Returns
    /// The data, whether it's the end of the data and the anchors
    ///
    /// ***Verified:*** False
    pub fn receive_changes(&self) -> Result<(Plist, bool, Plist), MobileSyncError> {
        let mut plist: unsafe_bindings::plist_t = std::ptr::null_mut();
        let mut has_more_changes = 0;
        let mut anchor: unsafe_bindings::plist_t = std::ptr::null_mut();

        let result = unsafe {
            unsafe_bindings::mobilesync_receive_changes(
                self.pointer,
                &mut plist,
                &mut has_more_changes,
                &mut anchor,
            )
        }
        .into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok((plist.into(), has_more_changes != 0, anchor.into()))
    }

    /// Acknoledge the changes from the device to continue sync
    /// # Arguments
    /// *none*
    /// # Returns
    /// *none*
    ///
    /// ***Verified:*** False
    pub fn acknowledge_changes_from_device(&self) -> Result<(), MobileSyncError> {
        let result =
            unsafe { unsafe_bindings::mobilesync_acknowledge_changes_from_device(self.pointer) }
                .into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok(())
    }

    /// Tells the client that the host is ready to send changes
    /// # Arguments
    /// *none*
    /// # Returns
    /// *none*
    ///
    /// ***Verified:*** False
    pub fn ready_to_send_changes_from_computer(&self) -> Result<(), MobileSyncError> {
        let result = unsafe {
            unsafe_bindings::mobilesync_ready_to_send_changes_from_computer(self.pointer)
        }
        .into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok(())
    }

    /// Send changes to the device
    /// # Arguments
    /// * `entities` - The changes to send in a plist
    /// * `is_lanst` - Tells the device if it's the last change
    /// * `actions` - Additional actions the device should perform
    ///
    /// ***Verified:*** False
    pub fn send_changes(
        &self,
        entities: Plist,
        is_last: bool,
        actions: Option<Plist>,
    ) -> Result<(), MobileSyncError> {
        let actions = actions
            .map(|x| x.get_pointer())
            .unwrap_or(std::ptr::null_mut());

        let result = unsafe {
            unsafe_bindings::mobilesync_send_changes(
                self.pointer,
                entities.get_pointer(),
                is_last.into(),
                actions,
            )
        }
        .into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok(())
    }

    /// Remaps the identifiers on the device
    /// # Arguments
    /// * `mapping` - The new mappings the device should use
    /// # Returns
    /// *none*
    ///
    /// ***Verified:*** False
    pub fn remap_identifiers(&self, mapping: Plist) -> Result<(), MobileSyncError> {
        if mapping.plist_type != PlistType::Array {
            return Err(MobileSyncError::InvalidArg);
        }

        let result = unsafe {
            unsafe_bindings::mobilesync_remap_identifiers(self.pointer, &mut mapping.get_pointer())
        }
        .into();

        if result != MobileSyncError::Success {
            return Err(result);
        }

        Ok(())
    }
}

impl MobileSyncAnchor {
    pub fn new(device_anchor: impl Into<String>, computer_anchor: impl Into<String>) -> Self {
        MobileSyncAnchor {
            device_anchor: device_anchor.into(),
            computer_anchor: computer_anchor.into(),
        }
    }
}

impl From<MobileSyncAnchor> for unsafe_bindings::mobilesync_anchors_t {
    fn from(anchor: MobileSyncAnchor) -> Self {
        let device_anchor = CString::new(anchor.device_anchor).unwrap().into_raw();
        let computer_anchor = CString::new(anchor.computer_anchor).unwrap().into_raw();

        let x = unsafe_bindings::mobilesync_anchors {
            device_anchor,
            computer_anchor,
        };
        Box::into_raw(Box::new(x))
    }
}

pub enum MobileSyncType {
    Fast,
    Slow,
    Reset,
}

impl From<MobileSyncType> for c_uint {
    fn from(type_: MobileSyncType) -> Self {
        match type_ {
            MobileSyncType::Fast => 0,
            MobileSyncType::Slow => 1,
            MobileSyncType::Reset => 2,
        }
    }
}

impl Drop for MobileSyncClient<'_> {
    fn drop(&mut self) {
        unsafe {
            unsafe_bindings::mobilesync_client_free(self.pointer);
        }
    }
}