accessory-access-sys 0.1.1

FFI to Apple's AccessoryAccess framework, through swift-bridge. Use the `accessory-access` crate instead.
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! FFI to Apple's [AccessoryAccess] framework, through swift-bridge. Use the
//! `accessory-access` crate instead; this one holds its `unsafe`.
//!
//! Signatures are Apple's, as far as swift-bridge can carry them, the way
//! `virtualization-sys`'s are:
//!
//! - Classes keep Apple's names; a nested type joins them
//!   (`AAUSBAccessoryMatchingCriteria.InterfaceMatchingOption` is
//!   [`AAUSBAccessoryMatchingCriteriaInterfaceMatchingOption`]). Members are
//!   methods `aa_<member>`; initializers and class members are free functions
//!   `aa_<type>_init[_<labels>]` and `aa_<type>_<member>`.
//! - Foundation's objects are objc2's, through `apple-bridge-foundation`, and cross as
//!   pointers. A `[String: any Sendable]` is a property list dictionary.
//! - IOUSBHost's `IOUSBHostDevice` is `io-usb-host-sys`'s, crossing by
//!   address.
//! - A listener is the trait [`AAUSBAccessoryListener`], which Swift holds
//!   while it is registered: AccessoryAccess knows a listener by its object,
//!   so the bridge keeps one per Rust listener, by a key Rust gives.
//! - Enums keep Apple's cases, plus `Unknown(rawValue)` for a case newer than
//!   this crate.
//!
//! The glue's names differ from the other bridges': all link into one binary,
//! and swift-bridge names symbols for each bridge's types.
//!
//! macOS only: the build fails elsewhere.
//!
//! [AccessoryAccess]: https://developer.apple.com/documentation/accessoryaccess

use std::collections::BTreeMap;
use std::ffi::c_void;
use std::ptr;
use std::sync::Arc;

use apple_bridge_foundation::PropertyListValue;
use apple_bridge_foundation::objc::{
  NSArray, NSData, NSObject, borrowed, borrowed_or_nil, ns_array, ns_array_objects, ns_data_bytes,
  ns_property_list_dictionary, retained, retained_or_nil,
};
pub use apple_bridge_foundation::objc::{NSError, Retained};
use dispatch2::DispatchQueue;
use io_usb_host_sys::IOUSBHostDevice;

pub use self::ffi::{
  AAErrorCode, AAUSBAccessory, AAUSBAccessoryManager, AAUSBAccessoryMatchingCriteria,
  AAUSBAccessoryMatchingCriteriaInterfaceMatchingOption, aa_error_code_init_raw_value, aa_error_code_raw_value,
  aa_error_domain, aa_usb_accessory_manager_shared,
};

// No doc comments inside the module: swift-bridge can't parse them. Shared
// enums are renamed on the Swift side, where Apple's names are taken.
//
// A function with a Foundation pointer takes the object it is a member of by
// value: swift-bridge 0.1.59 releases an object a free function borrows.
#[swift_bridge::bridge]
mod ffi {
  // Foundation.swift

  enum AccessoryAccessNil {
    Nil,
  }

  // Error.swift

  #[swift_bridge(swift_name = "BridgedAAErrorCode")]
  enum AAErrorCode {
    InternalError,
    AccessoryListenerAlreadyRegistered,
    AccessoryNotAccessible,
    InvalidAccessoryState,
    Unknown(isize),
  }

  extern "Swift" {
    fn aa_error_domain() -> String;
    fn aa_error_code_init_raw_value(raw_value: isize) -> AAErrorCode;
    fn aa_error_code_raw_value(code: AAErrorCode) -> isize;
  }

  // USBAccessory.swift

  extern "Swift" {
    type AAUSBAccessory;

    fn bridge_retained(self: &AAUSBAccessory) -> AAUSBAccessory;
    fn bridge_borrowed(self: &AAUSBAccessory) -> *const c_void;
    fn ns_description(self: &AAUSBAccessory) -> String;
    fn aa_usb_accessory_bridge_from(object: *const c_void) -> AAUSBAccessory;
    fn aa_registry_id(self: &AAUSBAccessory) -> u64;
    fn aa_usb_accessory_device_descriptor_data(this: AAUSBAccessory) -> *mut c_void;
    fn aa_usb_accessory_configuration_descriptor_data(this: AAUSBAccessory) -> usize;
    fn aa_usb_accessory_open(
      this: AAUSBAccessory,
      service_queue: usize,
      completion_handler: Box<dyn FnOnce(usize, usize)>,
    );
    fn aa_usb_accessory_close(this: AAUSBAccessory, completion_handler: Box<dyn FnOnce(usize)>);
  }

  // USBAccessoryListener.swift

  extern "Rust" {
    type AAUSBAccessoryListenerBridge;

    fn usb_accessory_did_connect(self: &AAUSBAccessoryListenerBridge, usb_accessory: AAUSBAccessory);
    fn usb_accessory_did_disconnect(self: &AAUSBAccessoryListenerBridge, usb_accessory: AAUSBAccessory);
  }

  // USBAccessoryManager.swift

  extern "Swift" {
    type AAUSBAccessoryManager;

    fn aa_usb_accessory_manager_shared() -> AAUSBAccessoryManager;
    fn bridge_retained(self: &AAUSBAccessoryManager) -> AAUSBAccessoryManager;
    fn ns_description(self: &AAUSBAccessoryManager) -> String;
    fn aa_usb_accessory_manager_register_listener(
      this: AAUSBAccessoryManager,
      listener: AAUSBAccessoryListenerBridge,
      key: usize,
      matching_criteria: *const c_void,
      completion_handler: Box<dyn FnOnce(usize, usize)>,
    );
    fn aa_usb_accessory_manager_unregister_listener(
      this: AAUSBAccessoryManager,
      listener: AAUSBAccessoryListenerBridge,
      key: usize,
      completion_handler: Box<dyn FnOnce()>,
    );
  }

  // USBAccessoryMatchingCriteria.swift

  #[swift_bridge(swift_name = "BridgedInterfaceMatchingOption")]
  enum AAUSBAccessoryMatchingCriteriaInterfaceMatchingOption {
    MatchAll,
    MatchAny,
    Unknown(isize),
  }

  extern "Swift" {
    type AAUSBAccessoryMatchingCriteria;

    fn aa_usb_accessory_matching_criteria_init_device_matching_dictionary(
      dictionary: *const c_void,
    ) -> Result<AAUSBAccessoryMatchingCriteria, AccessoryAccessNil>;
    fn aa_usb_accessory_matching_criteria_init_device_matching_dictionary_interface_matching_dictionaries_interface_matching_option(
      device_matching_dictionary: usize,
      interface_matching_dictionaries: *const c_void,
      interface_matching_option: AAUSBAccessoryMatchingCriteriaInterfaceMatchingOption,
    ) -> Result<AAUSBAccessoryMatchingCriteria, AccessoryAccessNil>;
    fn aa_usb_accessory_matching_criteria_init_device_matching_criteria(
      device_matching_criteria: *const c_void,
    ) -> AAUSBAccessoryMatchingCriteria;
    fn aa_usb_accessory_matching_criteria_init_device_matching_criteria_interface_matching_criteria_interface_matching_option(
      device_matching_criteria: usize,
      interface_matching_criteria: *const c_void,
      interface_matching_option: AAUSBAccessoryMatchingCriteriaInterfaceMatchingOption,
    ) -> AAUSBAccessoryMatchingCriteria;
    fn bridge_retained(self: &AAUSBAccessoryMatchingCriteria) -> AAUSBAccessoryMatchingCriteria;
    fn bridge_borrowed(self: &AAUSBAccessoryMatchingCriteria) -> *const c_void;
    fn ns_description(self: &AAUSBAccessoryMatchingCriteria) -> String;
  }
}

// Foundation.swift

/// An object of a class this bridge declares, as an `NSObject`, for as long
/// as `owner`, which holds it.
///
/// # Safety
///
/// `pointer` is what `bridge_borrowed()` returned for an object `owner`
/// holds.
unsafe fn bridge_object<O>(_owner: &O, pointer: *const c_void) -> &NSObject {
  // SAFETY: the caller's: `owner` keeps the object alive while it is
  // borrowed, and every AccessoryAccess class is an `NSObject`.
  unsafe { &*pointer.cast::<NSObject>() }
}

/// An optional dispatch queue, lent to Swift by address.
fn borrowed_queue_or_nil(queue: Option<&DispatchQueue>) -> usize {
  queue.map_or(0, |queue| ptr::from_ref(queue).expose_provenance())
}

/// What a completion handler failed with, which Swift handed over by
/// address: `0` for nothing.
fn completed_error(error: usize) -> Option<Retained<NSError>> {
  // SAFETY: `bridging(retainedError:)`, a retained `NSError` or `0`.
  unsafe { retained_or_nil(error) }
}

// USBAccessory.swift

// SAFETY: Apple declares `AAUSBAccessory` `Sendable`.
unsafe impl Send for AAUSBAccessory {}

// SAFETY: as for `Send`.
unsafe impl Sync for AAUSBAccessory {}

impl AAUSBAccessory {
  pub fn aa_device_descriptor_data(&self) -> Vec<u8> {
    // SAFETY: `deviceDescriptorData`, a retained `NSData`.
    ns_data_bytes(&*unsafe { retained::<NSData>(ffi::aa_usb_accessory_device_descriptor_data(self.bridge_retained())) })
  }

  pub fn aa_configuration_descriptor_data(&self) -> Option<Vec<u8>> {
    // SAFETY: `configurationDescriptorData`, a retained `NSData` or `0`.
    unsafe {
      retained_or_nil::<NSData>(ffi::aa_usb_accessory_configuration_descriptor_data(
        self.bridge_retained(),
      ))
    }
    .map(|data| ns_data_bytes(&data))
  }

  /// `service_queue` `None` is `nil`. The device crosses to
  /// `io-usb-host-sys`'s bridge.
  pub fn aa_open(
    &self,
    service_queue: Option<&DispatchQueue>,
    completion_handler: impl FnOnce(Result<IOUSBHostDevice, Retained<NSError>>) + Send + 'static,
  ) {
    ffi::aa_usb_accessory_open(
      self.bridge_retained(),
      borrowed_queue_or_nil(service_queue),
      Box::new(move |device, error| {
        // SAFETY: `bridging(retainedAddress:)`, a retained `IOUSBHostDevice`,
        // or `0` when it failed.
        let device: Option<Retained<NSObject>> = unsafe { retained_or_nil(device) };

        completion_handler(match device {
          // SAFETY: an `IOUSBHostDevice`, which `device` keeps alive for the
          // call.
          Some(device) => Ok(unsafe { io_usb_host_sys::iousbhost_device_bridge_from(borrowed(&*device)) }),
          None => Err(completed_error(error).expect("Swift passes a device or an error")),
        });
      }),
    );
  }

  pub fn aa_close(&self, completion_handler: impl FnOnce(Option<Retained<NSError>>) + Send + 'static) {
    ffi::aa_usb_accessory_close(
      self.bridge_retained(),
      Box::new(move |error| completion_handler(completed_error(error))),
    );
  }
}

// USBAccessoryListener.swift

/// `AAUSBAccessoryListener`, which Rust implements. AccessoryAccess calls it
/// on an arbitrary thread.
pub trait AAUSBAccessoryListener: Send + Sync {
  fn usb_accessory_did_connect(&self, usb_accessory: AAUSBAccessory);

  fn usb_accessory_did_disconnect(&self, usb_accessory: AAUSBAccessory);
}

/// An [`AAUSBAccessoryListener`] as Swift holds it.
pub struct AAUSBAccessoryListenerBridge {
  listener: Arc<dyn AAUSBAccessoryListener>,
}

impl AAUSBAccessoryListenerBridge {
  fn usb_accessory_did_connect(&self, usb_accessory: AAUSBAccessory) {
    self.listener.usb_accessory_did_connect(usb_accessory);
  }

  fn usb_accessory_did_disconnect(&self, usb_accessory: AAUSBAccessory) {
    self.listener.usb_accessory_did_disconnect(usb_accessory);
  }
}

// USBAccessoryManager.swift

// SAFETY: Apple declares `AAUSBAccessoryManager` `Sendable`.
unsafe impl Send for AAUSBAccessoryManager {}

// SAFETY: as for `Send`.
unsafe impl Sync for AAUSBAccessoryManager {}

impl AAUSBAccessoryManager {
  /// `key` names `listener` to the bridge, which passes AccessoryAccess the
  /// same object for each listener with the same key.
  pub fn aa_register_listener(
    &self,
    listener: Arc<dyn AAUSBAccessoryListener>,
    key: usize,
    matching_criteria: &[&AAUSBAccessoryMatchingCriteria],
    completion_handler: impl FnOnce(Result<Vec<AAUSBAccessory>, Retained<NSError>>) + Send + 'static,
  ) {
    let matching_criteria: Vec<_> = matching_criteria
      .iter()
      .map(|criteria| criteria.bridge_object())
      .collect();

    ffi::aa_usb_accessory_manager_register_listener(
      self.bridge_retained(),
      AAUSBAccessoryListenerBridge { listener },
      key,
      borrowed(&*ns_array(&matching_criteria)),
      Box::new(move |accessories, error| {
        // SAFETY: `bridging(retainedAddress:)`, a retained `NSArray` of
        // `AAUSBAccessory`s, which Swift passes even with an error: taken
        // first, so it is released either way.
        let accessories =
          unsafe { retained_or_nil::<NSArray<NSObject>>(accessories) }.expect("Swift passes the accessories");

        if let Some(error) = completed_error(error) {
          return completion_handler(Err(error));
        }

        completion_handler(Ok(
          ns_array_objects(&accessories)
            .iter()
            .map(|accessory| ffi::aa_usb_accessory_bridge_from(borrowed(&**accessory)))
            .collect(),
        ));
      }),
    );
  }

  /// `key` names `listener` as [`Self::aa_register_listener`] did.
  pub fn aa_unregister_listener(
    &self,
    listener: Arc<dyn AAUSBAccessoryListener>,
    key: usize,
    completion_handler: impl FnOnce() + Send + 'static,
  ) {
    ffi::aa_usb_accessory_manager_unregister_listener(
      self.bridge_retained(),
      AAUSBAccessoryListenerBridge { listener },
      key,
      Box::new(completion_handler),
    );
  }
}

// USBAccessoryMatchingCriteria.swift

/// `AAUSBAccessoryMatchingCriteria.DeviceMatchingCriteria`: each `None` is
/// `nil`.
pub struct AAUSBAccessoryMatchingCriteriaDeviceMatchingCriteria {
  pub vendor_id: Option<isize>,
  pub product_id: Option<isize>,
  pub device_class: Option<isize>,
  pub device_sub_class: Option<isize>,
  pub device_protocol: Option<isize>,
  pub speed: Option<isize>,
}

impl AAUSBAccessoryMatchingCriteriaDeviceMatchingCriteria {
  /// Its non-`nil` fields by their Swift names, as Swift reads it.
  fn bridge_dictionary(&self) -> BTreeMap<String, PropertyListValue> {
    criteria_dictionary([
      ("vendorID", self.vendor_id),
      ("productID", self.product_id),
      ("deviceClass", self.device_class),
      ("deviceSubClass", self.device_sub_class),
      ("deviceProtocol", self.device_protocol),
      ("speed", self.speed),
    ])
  }
}

/// `AAUSBAccessoryMatchingCriteria.InterfaceMatchingCriteria`: each `None` is
/// `nil`.
pub struct AAUSBAccessoryMatchingCriteriaInterfaceMatchingCriteria {
  pub vendor_id: Option<isize>,
  pub product_id: Option<isize>,
  pub bcd_device: Option<isize>,
  pub interface_number: Option<isize>,
  pub configuration_value: Option<isize>,
  pub interface_class: Option<isize>,
  pub interface_sub_class: Option<isize>,
  pub interface_protocol: Option<isize>,
  pub speed: Option<isize>,
}

impl AAUSBAccessoryMatchingCriteriaInterfaceMatchingCriteria {
  /// Its non-`nil` fields by their Swift names, as Swift reads it.
  fn bridge_dictionary(&self) -> BTreeMap<String, PropertyListValue> {
    criteria_dictionary([
      ("vendorID", self.vendor_id),
      ("productID", self.product_id),
      ("bcdDevice", self.bcd_device),
      ("interfaceNumber", self.interface_number),
      ("configurationValue", self.configuration_value),
      ("interfaceClass", self.interface_class),
      ("interfaceSubClass", self.interface_sub_class),
      ("interfaceProtocol", self.interface_protocol),
      ("speed", self.speed),
    ])
  }
}

/// A matching criteria struct's non-`nil` fields.
fn criteria_dictionary<const N: usize>(fields: [(&str, Option<isize>); N]) -> BTreeMap<String, PropertyListValue> {
  fields
    .into_iter()
    .filter_map(|(name, value)| Some((name.to_string(), PropertyListValue::Integer(value? as i64))))
    .collect()
}

pub fn aa_usb_accessory_matching_criteria_init_device_matching_dictionary(
  dictionary: &BTreeMap<String, PropertyListValue>,
) -> Option<AAUSBAccessoryMatchingCriteria> {
  ffi::aa_usb_accessory_matching_criteria_init_device_matching_dictionary(borrowed(&*ns_property_list_dictionary(
    dictionary,
  )))
  .ok()
}

pub fn aa_usb_accessory_matching_criteria_init_device_matching_dictionary_interface_matching_dictionaries_interface_matching_option(
  device_matching_dictionary: Option<&BTreeMap<String, PropertyListValue>>,
  interface_matching_dictionaries: &[BTreeMap<String, PropertyListValue>],
  interface_matching_option: AAUSBAccessoryMatchingCriteriaInterfaceMatchingOption,
) -> Option<AAUSBAccessoryMatchingCriteria> {
  let device_matching_dictionary = device_matching_dictionary.map(ns_property_list_dictionary);
  let interface_matching_dictionaries: Vec<_> = interface_matching_dictionaries
    .iter()
    .map(ns_property_list_dictionary)
    .collect();
  let interface_matching_dictionaries: Vec<_> = interface_matching_dictionaries
    .iter()
    .map(|dictionary| &**dictionary)
    .collect();

  ffi::aa_usb_accessory_matching_criteria_init_device_matching_dictionary_interface_matching_dictionaries_interface_matching_option(
    borrowed_or_nil(device_matching_dictionary.as_deref()),
    borrowed(&*ns_array(&interface_matching_dictionaries)),
    interface_matching_option,
  )
  .ok()
}

pub fn aa_usb_accessory_matching_criteria_init_device_matching_criteria(
  device_matching_criteria: &AAUSBAccessoryMatchingCriteriaDeviceMatchingCriteria,
) -> AAUSBAccessoryMatchingCriteria {
  ffi::aa_usb_accessory_matching_criteria_init_device_matching_criteria(borrowed(&*ns_property_list_dictionary(
    &device_matching_criteria.bridge_dictionary(),
  )))
}

pub fn aa_usb_accessory_matching_criteria_init_device_matching_criteria_interface_matching_criteria_interface_matching_option(
  device_matching_criteria: Option<&AAUSBAccessoryMatchingCriteriaDeviceMatchingCriteria>,
  interface_matching_criteria: &[AAUSBAccessoryMatchingCriteriaInterfaceMatchingCriteria],
  interface_matching_option: AAUSBAccessoryMatchingCriteriaInterfaceMatchingOption,
) -> AAUSBAccessoryMatchingCriteria {
  let device_matching_criteria =
    device_matching_criteria.map(|criteria| ns_property_list_dictionary(&criteria.bridge_dictionary()));
  let interface_matching_criteria: Vec<_> = interface_matching_criteria
    .iter()
    .map(|criteria| ns_property_list_dictionary(&criteria.bridge_dictionary()))
    .collect();
  let interface_matching_criteria: Vec<_> = interface_matching_criteria
    .iter()
    .map(|criteria| &**criteria)
    .collect();

  ffi::aa_usb_accessory_matching_criteria_init_device_matching_criteria_interface_matching_criteria_interface_matching_option(
    borrowed_or_nil(device_matching_criteria.as_deref()),
    borrowed(&*ns_array(&interface_matching_criteria)),
    interface_matching_option,
  )
}

// SAFETY: Apple declares `AAUSBAccessoryMatchingCriteria` `Sendable`.
unsafe impl Send for AAUSBAccessoryMatchingCriteria {}

// SAFETY: as for `Send`.
unsafe impl Sync for AAUSBAccessoryMatchingCriteria {}

impl AAUSBAccessoryMatchingCriteria {
  fn bridge_object(&self) -> &NSObject {
    // SAFETY: this one's own object.
    unsafe { bridge_object(self, self.bridge_borrowed()) }
  }
}