atdecc 0.1.0

IEEE 1722.1 ATDECC (AVDECC) with Milan: frames, controller and entity state machines, without I/O
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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! This Mac's own AVB entities, through Apple's AudioVideoBridging
//! framework. macOS never shows its AVB stack the frames a program writes
//! to BPF, so commands to an entity advertised from this Mac go through
//! the framework instead, and its answers come back as the frames the
//! entity would have sent.
//!
//! The framework answers AECP commands without putting anything on the
//! wire and sends ACMP ones on the wire as well, where BPF sees the
//! copies; those carry the framework's own sequence IDs, so the
//! controller takes them for another controller's traffic. Unsolicited
//! notifications reach only a handler registered for the controller.

#![allow(unsafe_code)]

use std::collections::{HashSet, VecDeque};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::{Arc, Mutex};

use avb_net::MacAddress;
use block2::RcBlock;
use objc2::rc::{Allocated, Retained};
use objc2::runtime::{AnyClass, AnyObject, Bool, NSObject};
use objc2::{AnyThread, DefinedClass, define_class, msg_send, sel};
use objc2_foundation::{NSData, NSError, NSString};

use crate::acmp::{AcmpFlags, AcmpMessageType, AcmpStatus, Acmpdu};
use crate::aecp::{AecpHeader, AecpMessageType, AemPdu, VendorUniquePdu};
use crate::id::{EntityId, StreamId};
use crate::pdu::{self, Pdu};

#[link(name = "AudioVideoBridging", kind = "framework")]
unsafe extern "C" {}

/// How long a vendor unique command waits for its answer, in
/// milliseconds; the controller retries on its own.
const VENDOR_UNIQUE_TIMEOUT: i64 = 250;

/// Room for any frame.
const FRAME: usize = 1536;

/// How long after sending an ACMP command through the framework its copy
/// on the wire is taken for it.
const ECHO_WINDOW: std::time::Duration = std::time::Duration::from_secs(2);

/// Whether AVB is on for interface `name`, as Audio MIDI Setup's Network
/// Device Browser turns it on and off; `None` where the framework is
/// missing.
pub(super) fn avb_enabled(name: &str) -> Option<bool> {
    let base = AnyClass::get(c"AVBInterface")?;
    let ns_name = NSString::from_str(name);
    // SAFETY: the selector and its types are AVBInterface's, as the
    // framework's headers declare them.
    let enabled: Bool = unsafe { msg_send![base, isAVBEnabledOnInterfaceNamed: &*ns_name] };
    Some(enabled.as_bool())
}

/// The framework's view of one interface, sending commands to this Mac's
/// own entities and turning their answers into frames. [`super::Driver`]
/// uses it; a program running a controller in a loop of its own can too.
pub struct Local {
    _interface: Retained<AnyObject>,
    aecp: Retained<AnyObject>,
    acmp: Retained<AnyObject>,
    /// The interface's own address, where this Mac's entities are.
    mac: Retained<AnyObject>,
    controller: EntityId,
    sender: Sender<Vec<u8>>,
    answers: Receiver<Vec<u8>>,
    /// Commands sent and not yet answered or timed out.
    waiting: Arc<AtomicUsize>,
    /// The entities commands went to, whose unsolicited notifications are
    /// passed on.
    entities: Arc<Mutex<HashSet<EntityId>>>,
    handler: Option<Retained<Responses>>,
    /// The framework sends vendor unique commands (macOS 14 and later).
    vendor_unique: bool,
    /// The controller IDs the framework puts on the ACMP commands it sends
    /// on the wire for this controller, learned from its answers and from
    /// the copies seen.
    framework_controllers: Arc<Mutex<HashSet<EntityId>>>,
    /// The ACMP commands sent through the framework lately, to tell their
    /// copies on the wire before the framework's own ID is known.
    recent: Mutex<VecDeque<(std::time::Instant, Acmpdu)>>,
}

impl Local {
    /// The framework on interface `name`, for controller `controller`, or
    /// `None` where it is missing or AVB is off on the interface.
    pub fn open(name: &str, controller: EntityId) -> Option<Self> {
        let base = AnyClass::get(c"AVBInterface")?;
        let ethernet = AnyClass::get(c"AVBEthernetInterface")?;
        let ns_name = NSString::from_str(name);
        // SAFETY: the selectors and their types are AVBInterface's, as the
        // framework's headers declare them.
        unsafe {
            let enabled: Bool = msg_send![base, isAVBEnabledOnInterfaceNamed: &*ns_name];
            if !enabled.as_bool() {
                return None;
            }
            let allocated: Allocated<AnyObject> = msg_send![ethernet, alloc];
            let interface: Option<Retained<AnyObject>> =
                msg_send![allocated, initWithInterfaceName: &*ns_name];
            let interface = interface?;
            let aecp: Option<Retained<AnyObject>> = msg_send![&*interface, aecp];
            let acmp: Option<Retained<AnyObject>> = msg_send![&*interface, acmp];
            let mac: Option<Retained<AnyObject>> =
                msg_send![base, macAddressForInterfaceNamed: &*ns_name];
            let (aecp, acmp, mac) = (aecp?, acmp?, mac?);
            let vendor_unique: Bool = msg_send![
                &*aecp,
                respondsToSelector: sel!(sendVendorUniqueCommand:toMACAddress:expectResponseWithinTimeout:completionHandler:)
            ];
            let (sender, answers) = mpsc::channel();
            let entities = Arc::new(Mutex::new(HashSet::new()));
            let handler = Responses::new(sender.clone(), entities.clone());
            let registered: Bool = msg_send![
                &*aecp,
                setResponseHandler: &*handler,
                forControllerEntityID: controller.0
            ];
            Some(Self {
                _interface: interface,
                aecp,
                acmp,
                mac,
                controller,
                sender,
                answers,
                waiting: Arc::new(AtomicUsize::new(0)),
                framework_controllers: Arc::new(Mutex::new(HashSet::new())),
                recent: Mutex::new(VecDeque::new()),
                entities,
                handler: registered.as_bool().then_some(handler),
                vendor_unique: vendor_unique.as_bool(),
            })
        }
    }

    /// Sends `bytes` through the framework when they are a command to an
    /// entity `is_local` says is on this Mac, returning whether it did.
    pub fn send(&self, bytes: &[u8], is_local: impl Fn(EntityId) -> bool) -> bool {
        let (target, sent) = match pdu::decode(bytes) {
            Ok(Pdu::Aem(aem))
                if aem.header.message_type == AecpMessageType::AEM_COMMAND
                    && is_local(aem.header.target_entity_id) =>
            {
                (aem.header.target_entity_id, self.send_aem(&aem))
            }
            Ok(Pdu::VendorUnique(command))
                if self.vendor_unique
                    && command.header.message_type == AecpMessageType::VENDOR_UNIQUE_COMMAND
                    && is_local(command.header.target_entity_id) =>
            {
                (
                    command.header.target_entity_id,
                    self.send_vendor_unique(&command),
                )
            }
            Ok(Pdu::Acmp(command))
                if command.message_type.is_command() && is_local(addressed(&command)) =>
            {
                (addressed(&command), self.send_acmp(&command))
            }
            _ => return false,
        };
        if sent && let Ok(mut entities) = self.entities.lock() {
            entities.insert(target);
        }
        sent
    }

    /// Whether commands wait for their answers.
    pub fn waiting(&self) -> bool {
        self.waiting.load(Ordering::Relaxed) > 0
    }

    /// The next answer, as the frame's octets after the Ethernet header.
    pub fn poll(&self) -> Option<Vec<u8>> {
        self.answers.try_recv().ok()
    }

    fn send_aem(&self, command: &AemPdu<'_>) -> bool {
        let Some(class) = AnyClass::get(c"AVB17221AECPAEMMessage") else {
            return false;
        };
        let header = command.header;
        let command_type = command.command_type;
        let answers = self.sender.clone();
        let waiting = self.waiting.clone();
        let completion = RcBlock::new(move |error: *mut NSError, response: *mut AnyObject| {
            waiting.fetch_sub(1, Ordering::Relaxed);
            // SAFETY: the framework hands over a valid response or none.
            let Some(response) = (unsafe { answered(error, response, 1) }) else {
                return;
            };
            // SAFETY: an AVB17221AECPAEMMessage's properties.
            let (status, payload) = unsafe {
                let status: u8 = msg_send![response, status];
                let data: Option<Retained<NSData>> = msg_send![response, commandSpecificData];
                (status, data.map(|data| data.to_vec()).unwrap_or_default())
            };
            let answer = AemPdu {
                header: AecpHeader {
                    message_type: AecpMessageType::AEM_RESPONSE,
                    status,
                    ..header
                },
                unsolicited: false,
                controller_request: false,
                command_type,
                payload: &payload,
            };
            let mut out = [0; FRAME];
            if let Ok(length) = answer.encode(&mut out) {
                let _ = answers.send(out[..length].to_vec());
            }
        });
        self.waiting.fetch_add(1, Ordering::Relaxed);
        // SAFETY: the selectors and their types are the framework's; it
        // copies the block.
        let sent: Bool = unsafe {
            let message: Retained<AnyObject> = msg_send![class, commandMessage];
            let _: () = msg_send![&*message, setMessageType: AecpMessageType::AEM_COMMAND.0];
            let _: () = msg_send![&*message, setCommandType: command_type.0];
            let _: () = msg_send![&*message, setTargetEntityID: header.target_entity_id.0];
            let _: () = msg_send![&*message, setControllerEntityID: header.controller_entity_id.0];
            let _: () = msg_send![&*message, setSequenceID: header.sequence_id];
            let _: () = msg_send![&*message, setUnsolicited: Bool::new(command.unsolicited)];
            let data = NSData::with_bytes(command.payload);
            let _: () = msg_send![&*message, setCommandSpecificData: &*data];
            msg_send![
                &*self.aecp,
                sendCommand: &*message,
                toMACAddress: &*self.mac,
                completionHandler: &*completion
            ]
        };
        self.sent(sent)
    }

    fn send_vendor_unique(&self, command: &VendorUniquePdu<'_>) -> bool {
        let Some(class) = AnyClass::get(c"AVB17221AECPVendorMessage") else {
            return false;
        };
        let header = command.header;
        let protocol_id = command.protocol_id;
        let answers = self.sender.clone();
        let waiting = self.waiting.clone();
        let completion = RcBlock::new(move |error: *mut NSError, response: *mut AnyObject| {
            waiting.fetch_sub(1, Ordering::Relaxed);
            // SAFETY: the framework hands over a valid response or none.
            let Some(response) = (unsafe { answered(error, response, 7) }) else {
                return;
            };
            // SAFETY: an AVB17221AECPVendorMessage's properties.
            let (status, payload) = unsafe {
                let status: u8 = msg_send![response, status];
                let data: Option<Retained<NSData>> = msg_send![response, protocolSpecificData];
                (status, data.map(|data| data.to_vec()).unwrap_or_default())
            };
            let answer = VendorUniquePdu {
                header: AecpHeader {
                    message_type: AecpMessageType::VENDOR_UNIQUE_RESPONSE,
                    status,
                    ..header
                },
                protocol_id,
                payload: &payload,
            };
            let mut out = [0; FRAME];
            if let Ok(length) = answer.encode(&mut out) {
                let _ = answers.send(out[..length].to_vec());
            }
        });
        let mut id = [0; 8];
        id[2..].copy_from_slice(&protocol_id);
        self.waiting.fetch_add(1, Ordering::Relaxed);
        // SAFETY: as in send_aem; the class has a plain init.
        let sent: Bool = unsafe {
            let allocated: Allocated<AnyObject> = msg_send![class, alloc];
            let message: Retained<AnyObject> = msg_send![allocated, init];
            let _: () = msg_send![
                &*message,
                setMessageType: AecpMessageType::VENDOR_UNIQUE_COMMAND.0
            ];
            let _: () = msg_send![&*message, setTargetEntityID: header.target_entity_id.0];
            let _: () = msg_send![&*message, setControllerEntityID: header.controller_entity_id.0];
            let _: () = msg_send![&*message, setSequenceID: header.sequence_id];
            let _: () = msg_send![&*message, setProtocolID: u64::from_be_bytes(id)];
            let data = NSData::with_bytes(command.payload);
            let _: () = msg_send![&*message, setProtocolSpecificData: &*data];
            msg_send![
                &*self.aecp,
                sendVendorUniqueCommand: &*message,
                toMACAddress: &*self.mac,
                expectResponseWithinTimeout: VENDOR_UNIQUE_TIMEOUT,
                completionHandler: &*completion
            ]
        };
        self.sent(sent)
    }

    fn send_acmp(&self, command: &Acmpdu) -> bool {
        let (Some(class), Some(mac_class)) = (
            AnyClass::get(c"AVB17221ACMPMessage"),
            AnyClass::get(c"AVBMACAddress"),
        ) else {
            return false;
        };
        let sent_command = *command;
        let answers = self.sender.clone();
        let waiting = self.waiting.clone();
        let framework_controllers = self.framework_controllers.clone();
        let expected = command.message_type.response().0;
        let completion = RcBlock::new(move |error: *mut NSError, response: *mut AnyObject| {
            waiting.fetch_sub(1, Ordering::Relaxed);
            // SAFETY: the framework hands over a valid response or none.
            let Some(response) = (unsafe { answered(error, response, expected) }) else {
                return;
            };
            // SAFETY: an AVB17221ACMPMessage's properties.
            let answer = unsafe {
                let destination: Option<Retained<AnyObject>> = msg_send![response, destinationMAC];
                let destination = destination
                    .and_then(|mac| {
                        let data: Option<Retained<NSData>> = msg_send![&*mac, dataRepresentation];
                        data
                    })
                    .and_then(|data| <[u8; 6]>::try_from(data.to_vec().as_slice()).ok())
                    .map_or(sent_command.stream_dest_mac, MacAddress);
                let message_type: u8 = msg_send![response, messageType];
                let status: u8 = msg_send![response, status];
                let stream_id: u64 = msg_send![response, streamID];
                let talker: u64 = msg_send![response, talkerEntityID];
                let listener: u64 = msg_send![response, listenerEntityID];
                let talker_unique_id: u16 = msg_send![response, talkerUniqueID];
                let listener_unique_id: u16 = msg_send![response, listenerUniqueID];
                let connection_count: u16 = msg_send![response, connectionCount];
                let flags: u16 = msg_send![response, flags];
                let vlan: u16 = msg_send![response, vlanID];
                let controller: u64 = msg_send![response, controllerEntityID];
                if controller != 0 && controller != sent_command.controller_entity_id.0 {
                    framework_controllers
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .insert(EntityId(controller));
                }
                Acmpdu {
                    message_type: AcmpMessageType(message_type),
                    status: AcmpStatus(status),
                    stream_id: StreamId(stream_id),
                    talker_entity_id: EntityId(talker),
                    listener_entity_id: EntityId(listener),
                    talker_unique_id,
                    listener_unique_id,
                    stream_dest_mac: destination,
                    connection_count,
                    flags: AcmpFlags(flags),
                    stream_vlan_id: vlan,
                    ..sent_command
                }
            };
            let mut out = [0; FRAME];
            if let Ok(length) = answer.encode(&mut out) {
                let _ = answers.send(out[..length].to_vec());
            }
        });
        self.waiting.fetch_add(1, Ordering::Relaxed);
        {
            let mut recent = self
                .recent
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let now = std::time::Instant::now();
            recent.retain(|(at, _)| now.duration_since(*at) < ECHO_WINDOW);
            recent.push_back((now, *command));
        }
        // SAFETY: as in send_aem; the message class has a plain init, and
        // the address is six octets.
        let sent: Bool = unsafe {
            let allocated: Allocated<AnyObject> = msg_send![class, alloc];
            let message: Retained<AnyObject> = msg_send![allocated, init];
            let allocated: Allocated<AnyObject> = msg_send![mac_class, alloc];
            let destination: Retained<AnyObject> =
                msg_send![allocated, initWithBytes: command.stream_dest_mac.0.as_ptr()];
            let _: () = msg_send![&*message, setMessageType: command.message_type.0];
            let _: () = msg_send![&*message, setStreamID: command.stream_id.0];
            let _: () = msg_send![&*message, setControllerEntityID: command.controller_entity_id.0];
            let _: () = msg_send![&*message, setTalkerEntityID: command.talker_entity_id.0];
            let _: () = msg_send![&*message, setListenerEntityID: command.listener_entity_id.0];
            let _: () = msg_send![&*message, setTalkerUniqueID: command.talker_unique_id];
            let _: () = msg_send![&*message, setListenerUniqueID: command.listener_unique_id];
            let _: () = msg_send![&*message, setDestinationMAC: &*destination];
            let _: () = msg_send![&*message, setConnectionCount: command.connection_count];
            let _: () = msg_send![&*message, setSequenceID: command.sequence_id];
            let _: () = msg_send![&*message, setFlags: command.flags.0];
            let _: () = msg_send![&*message, setVlanID: command.stream_vlan_id];
            msg_send![
                &*self.acmp,
                sendACMPCommandMessage: &*message,
                completionHandler: &*completion
            ]
        };
        self.sent(sent)
    }

    /// Whether `bytes`, received from this Mac, are the framework's copy
    /// of an ACMP command it sent on the wire for this controller, under a
    /// controller ID of its own: one known from its answers, or a command
    /// just sent through it, whose ID is learned then.
    pub fn echoes(&self, bytes: &[u8]) -> bool {
        let Ok(Pdu::Acmp(copy)) = pdu::decode(bytes) else {
            return false;
        };
        if copy.message_type.is_response() {
            return false;
        }
        let mut controllers = self
            .framework_controllers
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if controllers.contains(&copy.controller_entity_id) {
            return true;
        }
        let mut recent = self
            .recent
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let now = std::time::Instant::now();
        recent.retain(|(at, _)| now.duration_since(*at) < ECHO_WINDOW);
        let Some(position) = recent.iter().position(|(_, sent)| {
            sent.message_type == copy.message_type
                && sent.talker_entity_id == copy.talker_entity_id
                && sent.talker_unique_id == copy.talker_unique_id
                && sent.listener_entity_id == copy.listener_entity_id
                && sent.listener_unique_id == copy.listener_unique_id
        }) else {
            return false;
        };
        recent.remove(position);
        if copy.controller_entity_id != self.controller {
            controllers.insert(copy.controller_entity_id);
        }
        true
    }

    /// Whether the framework took a command; one it did not take will not
    /// be answered.
    fn sent(&self, sent: Bool) -> bool {
        if !sent.as_bool() {
            self.waiting.fetch_sub(1, Ordering::Relaxed);
        }
        sent.as_bool()
    }
}

impl Drop for Local {
    fn drop(&mut self) {
        if self.handler.is_some() {
            // SAFETY: AVB17221AECPInterface's method.
            unsafe {
                let _: () = msg_send![
                    &*self.aecp,
                    removeResponseHandlerForControllerEntityID: self.controller.0
                ];
            }
        }
    }
}

/// The entity an ACMP command goes to: TX commands to the talker, RX ones
/// to the listener.
fn addressed(command: &Acmpdu) -> EntityId {
    match command.message_type {
        AcmpMessageType::CONNECT_TX_COMMAND
        | AcmpMessageType::DISCONNECT_TX_COMMAND
        | AcmpMessageType::GET_TX_STATE_COMMAND
        | AcmpMessageType::GET_TX_CONNECTION_COMMAND => command.talker_entity_id,
        _ => command.listener_entity_id,
    }
}

/// The response a completion handler got, when it is one: the framework
/// passes an error of code 0 along with answers, and on a timeout a
/// nonzero one, perhaps with the command itself.
///
/// # Safety
///
/// `error` and `response` are null or valid objects.
unsafe fn answered<'a>(
    error: *mut NSError,
    response: *mut AnyObject,
    message_type: u8,
) -> Option<&'a AnyObject> {
    // SAFETY: the caller's.
    let (error, response) = unsafe { (error.as_ref(), response.as_ref()?) };
    if error.is_some_and(|error| error.code() != 0) {
        return None;
    }
    // SAFETY: every AVB message has a messageType.
    let found: u8 = unsafe { msg_send![response, messageType] };
    (found == message_type).then_some(response)
}

struct Ivars {
    answers: Sender<Vec<u8>>,
    entities: Arc<Mutex<HashSet<EntityId>>>,
}

define_class!(
    // SAFETY: NSObject has no subclassing requirements, and Responses
    // does not implement Drop.
    #[unsafe(super(NSObject))]
    #[name = "AtdeccLocalEntityResponses"]
    #[ivars = Ivars]
    struct Responses;

    /// The AVB17221AECPClient methods the framework calls with the
    /// responses for the controller.
    impl Responses {
        #[unsafe(method(AECPDidReceiveCommand:onInterface:))]
        fn received_command(&self, _message: *mut AnyObject, _interface: *mut AnyObject) -> Bool {
            Bool::NO
        }

        #[unsafe(method(AECPDidReceiveResponse:onInterface:))]
        fn received_response(&self, message: *mut AnyObject, _interface: *mut AnyObject) -> Bool {
            // SAFETY: the framework passes a valid message.
            if let Some(message) = unsafe { message.as_ref() } {
                self.unsolicited(message);
            }
            Bool::YES
        }
    }
);

impl Responses {
    fn new(answers: Sender<Vec<u8>>, entities: Arc<Mutex<HashSet<EntityId>>>) -> Retained<Self> {
        let this = Self::alloc().set_ivars(Ivars { answers, entities });
        // SAFETY: NSObject's init.
        unsafe { msg_send![super(this), init] }
    }

    /// Passes on an unsolicited notification from one of this Mac's
    /// entities; solicited answers come to their command's handler.
    fn unsolicited(&self, message: &AnyObject) {
        let Some(class) = AnyClass::get(c"AVB17221AECPAEMMessage") else {
            return;
        };
        // SAFETY: NSObject's isKindOfClass:, then an
        // AVB17221AECPAEMMessage's properties.
        unsafe {
            let aem: Bool = msg_send![message, isKindOfClass: class];
            if !aem.as_bool() {
                return;
            }
            let unsolicited: Bool = msg_send![message, isUnsolicited];
            let target: u64 = msg_send![message, targetEntityID];
            let known = self
                .ivars()
                .entities
                .lock()
                .is_ok_and(|entities| entities.contains(&EntityId(target)));
            if !unsolicited.as_bool() || !known {
                return;
            }
            let status: u8 = msg_send![message, status];
            let controller: u64 = msg_send![message, controllerEntityID];
            let sequence_id: u16 = msg_send![message, sequenceID];
            let command_type: u16 = msg_send![message, commandType];
            let data: Option<Retained<NSData>> = msg_send![message, commandSpecificData];
            let payload = data.map(|data| data.to_vec()).unwrap_or_default();
            let answer = AemPdu {
                header: AecpHeader {
                    message_type: AecpMessageType::AEM_RESPONSE,
                    status,
                    target_entity_id: EntityId(target),
                    controller_entity_id: EntityId(controller),
                    sequence_id,
                },
                unsolicited: true,
                controller_request: false,
                command_type: crate::aecp::AemCommandType(command_type),
                payload: &payload,
            };
            let mut out = [0; FRAME];
            if let Ok(length) = answer.encode(&mut out) {
                let _ = self.ivars().answers.send(out[..length].to_vec());
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_framework_links_and_its_classes_are_found() {
        for name in [
            c"AVBInterface",
            c"AVBEthernetInterface",
            c"AVBMACAddress",
            c"AVB17221AECPAEMMessage",
            c"AVB17221AECPVendorMessage",
            c"AVB17221ACMPMessage",
        ] {
            assert!(AnyClass::get(name).is_some(), "{name:?}");
        }
        assert!(Local::open("no-such-interface", EntityId(1)).is_none());
    }

    #[test]
    fn acmp_commands_go_to_the_talker_or_the_listener() {
        let command = |message_type| Acmpdu {
            message_type,
            talker_entity_id: EntityId(1),
            listener_entity_id: EntityId(2),
            ..Acmpdu::default()
        };
        assert_eq!(
            addressed(&command(AcmpMessageType::GET_TX_STATE_COMMAND)),
            EntityId(1)
        );
        assert_eq!(
            addressed(&command(AcmpMessageType::DISCONNECT_TX_COMMAND)),
            EntityId(1)
        );
        assert_eq!(
            addressed(&command(AcmpMessageType::CONNECT_RX_COMMAND)),
            EntityId(2)
        );
        assert_eq!(
            addressed(&command(AcmpMessageType::GET_RX_STATE_COMMAND)),
            EntityId(2)
        );
    }
}