atdecc 0.1.2

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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
//! A controller and two entities, a talker and a listener, on one
//! network in memory: the controller finds and reads both, binds the
//! listener to the talker, the listener probes the talker for its stream,
//! and unbinding undoes it.

use std::collections::VecDeque;
use std::time::Duration;

use atdecc::adp::{Adpdu, EntityCapabilities};
use atdecc::aecp::{AaMode, AaStatus, AaTlv, AddressAccessPdu, AecpHeader, AecpMessageType};
use atdecc::controller::{Config, Controller, Event, Outcome, Refusal};
use atdecc::descriptor::{DescriptorType, MemoryObjectDescriptor, MemoryObjectType};
use atdecc::entity::{
    EndpointModel, Entity, EntityEvent, LOGO_ADDRESS, MEMORY_SEGMENT, StreamModel,
    VENDOR_LOGO_ADDRESS,
};
use atdecc::model::EnumerationState;
use atdecc::stream_format::StreamFormat;
use atdecc::{
    ADP_ACMP_MULTICAST, AcmpMessageType, AcmpStatus, Acmpdu, AemStatus, ClockIdentity, EntityId,
    EntityModelId, Instant, MacAddress, StreamId,
};

const CONTROLLER_MAC: MacAddress = MacAddress([0x02, 0, 0, 0, 0, 1]);
const HOST_MAC: MacAddress = MacAddress([0x9c, 0x6b, 0x00, 0x30, 0x9a, 0x2b]);
const TALKER: EntityId = EntityId(0x9c6b_00ff_0030_9a2b);
const LISTENER: EntityId = EntityId(0x9c6b_00ff_0130_9a2b);
const AAF_48K_8CH: StreamFormat = StreamFormat(0x0205_0220_0200_6000);
const STREAM: StreamId = StreamId(0x9c6b_0030_9a2b_0000);
const DESTINATION: MacAddress = MacAddress([0x91, 0xe0, 0xf0, 0x00, 0x12, 0x34]);

fn stream(name: &str) -> StreamModel {
    StreamModel {
        name: name.into(),
        formats: vec![AAF_48K_8CH],
        current_format: AAF_48K_8CH,
        channels: 8,
    }
}

fn model(entity_id: EntityId, name: &str, talker: bool) -> EndpointModel {
    EndpointModel {
        entity_id,
        entity_model_id: EntityModelId(0x8c1f_6436_c000_0002),
        entity_name: name.into(),
        group_name: String::new(),
        vendor_name: "Scramble Tools".into(),
        model_name: "Host endpoint".into(),
        firmware_version: "0.1.0".into(),
        serial_number: String::new(),
        mac: HOST_MAC,
        interface_name: "enp2s0".into(),
        clock_identity: ClockIdentity(0x9c6b_00ff_fe30_9a2b),
        sampling_rates: vec![48_000],
        current_sampling_rate: 48_000,
        outputs: if talker {
            vec![stream("Host out")]
        } else {
            vec![]
        },
        inputs: if talker {
            vec![]
        } else {
            vec![stream("Host in")]
        },
        clock_source: 0,
        clock_source_inputs: if talker { vec![] } else { vec![0] },
        logo: None,
        vendor_logo: None,
    }
}

struct Network {
    now: Instant,
    controller: Controller,
    talker: Entity,
    listener: Entity,
    entity_events: Vec<EntityEvent>,
    controller_events: Vec<Event>,
    /// Every frame the entities sent.
    sent: Vec<Vec<u8>>,
    /// A logo the talker's Address Access commands are answered from in
    /// place, as ESP32 endpoints answer them, in a response as long as the
    /// command, instead of by the talker.
    in_place: Option<Vec<u8>>,
    /// The status the listener's unbind answers carry in place of its
    /// own, as a listener outside Milan answers.
    unbind_status: Option<AcmpStatus>,
}

impl Network {
    fn new() -> Self {
        let mut talker = Entity::new(model(TALKER, "Talker", true), &[(STREAM, DESTINATION)]);
        let mut listener = Entity::new(model(LISTENER, "Listener", false), &[]);
        talker.start(Instant::ZERO);
        listener.start(Instant::ZERO);
        Network {
            now: Instant::ZERO,
            controller: Controller::new(Config::new(EntityId(0x0200_00ff_fe00_0001))),
            talker,
            listener,
            entity_events: Vec::new(),
            controller_events: Vec::new(),
            sent: Vec::new(),
            in_place: None,
            unbind_status: None,
        }
    }

    /// Runs everyone for `millis`, handing each frame to whoever it is for.
    fn run(&mut self, millis: u64) {
        let until = self.now + Duration::from_millis(millis);
        while self.now < until {
            self.now += Duration::from_millis(5);
            let now = self.now;
            self.controller.handle_timeout(now);
            self.talker.handle_timeout(now);
            self.listener.handle_timeout(now);
            // Frames in flight: their source, destination and octets.
            let mut frames: VecDeque<(MacAddress, MacAddress, Vec<u8>)> = VecDeque::new();
            loop {
                let mut out = [0; 1500];
                while let Ok(Some(transmit)) = self.controller.poll_transmit(&mut out) {
                    frames.push_back((
                        CONTROLLER_MAC,
                        transmit.destination,
                        out[..transmit.length].to_vec(),
                    ));
                }
                for entity in [&mut self.talker, &mut self.listener] {
                    while let Some((destination, bytes)) = entity.poll_transmit() {
                        self.sent.push(bytes.clone());
                        frames.push_back((HOST_MAC, destination, bytes));
                    }
                }
                if let Some(status) = self.unbind_status {
                    for (_, _, bytes) in &mut frames {
                        refuse_unbind(bytes, status);
                    }
                }
                let Some((source, destination, bytes)) = frames.pop_front() else {
                    break;
                };
                if destination == HOST_MAC
                    && let Some(answer) = self.answer_in_place(&bytes)
                {
                    frames.push_back((HOST_MAC, CONTROLLER_MAC, answer));
                    continue;
                }
                let multicast = destination == ADP_ACMP_MULTICAST;
                if multicast || destination == CONTROLLER_MAC {
                    let _ = self.controller.handle_frame(now, source, &bytes);
                }
                if multicast || destination == HOST_MAC {
                    self.talker.handle_frame(now, source, &bytes);
                    self.listener.handle_frame(now, source, &bytes);
                }
            }
            for entity in [&mut self.talker, &mut self.listener] {
                self.entity_events
                    .extend(std::iter::from_fn(|| entity.poll_event()));
            }
            self.controller_events
                .extend(std::iter::from_fn(|| self.controller.poll_event()));
        }
    }
}

/// Gives an unbind answer in `bytes` the status `status`.
fn refuse_unbind(bytes: &mut Vec<u8>, status: AcmpStatus) {
    let Ok(mut acmpdu) = Acmpdu::decode(bytes) else {
        return;
    };
    if acmpdu.message_type != AcmpMessageType::DISCONNECT_RX_RESPONSE {
        return;
    }
    acmpdu.status = status;
    let mut out = [0; 128];
    let length = acmpdu.encode(&mut out).unwrap();
    *bytes = out[..length].to_vec();
}

impl Network {
    /// The answer to an Address Access READ of the talker's logo, written
    /// over the command, as long as it: with the octets read only where
    /// the command had room for them.
    fn answer_in_place(&self, command: &[u8]) -> Option<Vec<u8>> {
        let logo = self.in_place.as_ref()?;
        let pdu = AddressAccessPdu::decode(command).ok()?;
        if pdu.header.message_type != AecpMessageType::ADDRESS_ACCESS_COMMAND
            || pdu.header.target_entity_id != TALKER
        {
            return None;
        }
        let mut answer = command.to_vec();
        answer[1] = (answer[1] & 0xf0) | AecpMessageType::ADDRESS_ACCESS_RESPONSE.0;
        // tlv_count, then the one TLV's mode and length, address and room.
        let word = u16::from_be_bytes([answer[24], answer[25]]);
        let length = usize::from(word & 0x0fff);
        let address = u64::from_be_bytes(answer[26..34].try_into().unwrap());
        let start = usize::try_from(address - LOGO_ADDRESS).unwrap();
        let room = answer.len().saturating_sub(34).min(length);
        answer[34..34 + room].copy_from_slice(&logo[start..start + room]);
        Some(answer)
    }
}

#[test]
fn a_controller_reads_both_entities() {
    let mut network = Network::new();
    network.controller.discover(None);
    network.run(3000);
    for (entity_id, name, stream) in [
        (
            TALKER,
            "Talker",
            (DescriptorType::STREAM_OUTPUT, "Host out"),
        ),
        (
            LISTENER,
            "Listener",
            (DescriptorType::STREAM_INPUT, "Host in"),
        ),
    ] {
        let model = network.controller.model(entity_id).expect("read");
        assert_eq!(model.state, EnumerationState::Complete, "{name}");
        assert_eq!(model.entity_name(), Some(name));
        assert_eq!(model.name_of(stream.0, 0), Some(stream.1));
        let milan = model.milan.expect("GET_MILAN_INFO answered");
        assert_eq!(milan.specification_version, Some([1, 3, 0, 0]));
        let info = model.stream_info(stream.0, 0).expect("GET_STREAM_INFO");
        assert_eq!(info.stream_format, AAF_48K_8CH);
    }
    let talker = network.controller.model(TALKER).unwrap();
    let output = talker
        .stream_info(DescriptorType::STREAM_OUTPUT, 0)
        .unwrap();
    assert_eq!(
        (output.stream_id, output.stream_dest_mac),
        (STREAM, DESTINATION)
    );
}

#[test]
fn binding_probes_the_talker_and_unbinding_undoes_it() {
    let mut network = Network::new();
    network.controller.discover(None);
    network.run(3000);
    let now = network.now;
    let bind = network.controller.connect(now, (TALKER, 0), (LISTENER, 0));
    network.run(1000);
    assert!(
        network
            .controller_events
            .contains(&Event::CommandFinished(bind, Outcome::Done)),
        "{:?}",
        network.controller_events
    );
    assert!(network.entity_events.iter().any(|event| matches!(
        event,
        EntityEvent::InputBound { index: 0, binding } if binding.talker == TALKER
    )));
    let settled = network.entity_events.iter().find_map(|event| match event {
        EntityEvent::InputSettled { index: 0, stream } => Some(*stream),
        _ => None,
    });
    let settled = settled.expect("the probe found the stream");
    assert_eq!(
        (settled.stream_id, settled.destination),
        (STREAM, DESTINATION)
    );
    let binding = network
        .controller
        .model(LISTENER)
        .and_then(|model| model.binding(0))
        .copied()
        .expect("the controller knows the binding");
    assert_eq!(binding.talker_stream(), Some((TALKER, 0)));

    let now = network.now;
    let unbind = network.controller.disconnect(now, (LISTENER, 0));
    network.run(1000);
    assert!(
        network
            .controller_events
            .contains(&Event::CommandFinished(unbind, Outcome::Done))
    );
    assert!(
        network
            .entity_events
            .contains(&EntityEvent::InputUnbound { index: 0 })
    );
    assert_eq!(network.listener.input_binding(0), None);
    // The listener lets go of the talker, as one outside Milan keeps the
    // connection the probe made until told.
    assert_eq!(network.disconnects(), [(TALKER, 0, LISTENER, 0)]);
}

/// Stopping and starting a stream are sent as AEM commands for the
/// stream named, and finish with the entity's answer: this entity does
/// not implement them.
#[test]
fn streaming_commands_finish_with_the_answer() {
    let mut network = Network::new();
    network.controller.discover(None);
    network.run(3000);
    let now = network.now;
    let stop = network
        .controller
        .stop_streaming(now, TALKER, DescriptorType::STREAM_OUTPUT, 0);
    let start = network
        .controller
        .start_streaming(now, TALKER, DescriptorType::STREAM_OUTPUT, 0);
    network.run(1000);
    let refused = Outcome::Refused(Refusal::Aem(AemStatus::NOT_IMPLEMENTED));
    for command in [stop, start] {
        assert!(
            network
                .controller_events
                .contains(&Event::CommandFinished(command, refused)),
            "{:?}",
            network.controller_events
        );
    }
}

/// A listener that answers an unbind NOT_CONNECTED, as one outside Milan
/// does where its talker had let go of it, is asked how it stands, so the
/// controller no longer holds it bound.
#[test]
fn a_refused_unbind_reads_the_binding_again() {
    let mut network = Network::new();
    network.controller.discover(None);
    network.run(3000);
    let now = network.now;
    network.controller.connect(now, (TALKER, 0), (LISTENER, 0));
    network.run(1000);
    let bound = |network: &Network| {
        network
            .controller
            .model(LISTENER)
            .and_then(|model| model.binding(0))
            .and_then(|binding| binding.talker_stream())
    };
    assert_eq!(bound(&network), Some((TALKER, 0)));

    network.unbind_status = Some(AcmpStatus::NOT_CONNECTED);
    let now = network.now;
    let unbind = network.controller.disconnect(now, (LISTENER, 0));
    network.run(1000);
    let refused = Outcome::Refused(Refusal::Acmp(AcmpStatus::NOT_CONNECTED));
    assert!(
        network
            .controller_events
            .contains(&Event::CommandFinished(unbind, refused)),
        "{:?}",
        network.controller_events
    );
    assert_eq!(network.listener.input_binding(0), None);
    assert_eq!(bound(&network), None);
}

/// A listener leaving, or handing over to an entity that does not carry
/// an input on, lets go of the talker that input was bound to.
#[test]
fn a_listener_leaving_lets_go_of_its_talker() {
    let mut network = Network::new();
    network.controller.discover(None);
    network.run(3000);
    let now = network.now;
    network.controller.connect(now, (TALKER, 0), (LISTENER, 0));
    network.run(1000);
    assert!(network.disconnects().is_empty());

    // Carried on, the input keeps its talker.
    network.listener.release_inputs(&[0]);
    network.run(100);
    assert!(network.disconnects().is_empty());
    network.listener.release_inputs(&[]);
    network.run(100);
    assert_eq!(network.disconnects(), [(TALKER, 0, LISTENER, 0)]);

    network.sent.clear();
    network.listener.depart();
    network.run(100);
    assert_eq!(network.disconnects(), [(TALKER, 0, LISTENER, 0)]);
}

impl Network {
    /// The DISCONNECT_TX commands the entities sent: talker and output,
    /// listener and input.
    fn disconnects(&self) -> Vec<(EntityId, u16, EntityId, u16)> {
        self.sent
            .iter()
            .filter_map(|bytes| atdecc::Acmpdu::decode(bytes).ok())
            .filter(|acmpdu| acmpdu.message_type == atdecc::AcmpMessageType::DISCONNECT_TX_COMMAND)
            .map(|acmpdu| {
                (
                    acmpdu.talker_entity_id,
                    acmpdu.talker_unique_id,
                    acmpdu.listener_entity_id,
                    acmpdu.listener_unique_id,
                )
            })
            .collect()
    }
}

#[test]
fn a_restored_binding_probes_the_talker_again() {
    let mut network = Network::new();
    network.controller.discover(None);
    network.run(3000);
    // The listener starts again bound as it was, without a controller.
    let mut listener = Entity::new(model(LISTENER, "Listener", false), &[]);
    listener.start(network.now);
    let binding = atdecc::entity::InputBinding {
        talker: TALKER,
        talker_unique_id: 0,
        controller: EntityId(0),
        flags: atdecc::AcmpFlags::empty(),
    };
    listener.restore_binding(network.now, 0, binding);
    network.listener = listener;
    network.run(1000);
    assert!(
        network
            .entity_events
            .contains(&EntityEvent::InputBound { index: 0, binding })
    );
    let settled = network.entity_events.iter().find_map(|event| match event {
        EntityEvent::InputSettled { index: 0, stream } => Some(*stream),
        _ => None,
    });
    assert_eq!(
        settled.map(|stream| (stream.stream_id, stream.destination)),
        Some((STREAM, DESTINATION))
    );
    assert_eq!(network.listener.input_binding(0), Some(binding));
}

#[test]
fn a_talker_without_an_address_is_probed_again_until_it_has_one() {
    let mut network = Network::new();
    let mut talker = Entity::new(
        model(TALKER, "Talker", true),
        &[(STREAM, atdecc::entity::UNADDRESSED)],
    );
    talker.start(network.now);
    network.talker = talker;
    network.controller.discover(None);
    network.run(3000);
    let now = network.now;
    network.controller.connect(now, (TALKER, 0), (LISTENER, 0));
    network.run(1000);
    let settled = |network: &Network| {
        network.entity_events.iter().find_map(|event| match event {
            EntityEvent::InputSettled { index: 0, stream } => Some(*stream),
            _ => None,
        })
    };
    assert_eq!(settled(&network), None, "no address to settle on yet");
    // The address comes, and the next probe finds it.
    network.talker.set_output_destination(0, DESTINATION);
    network.run(5000);
    assert_eq!(
        settled(&network).map(|stream| stream.destination),
        Some(DESTINATION)
    );
}

/// An entity's AVB_INTERFACE counters, link up and down and grandmaster
/// changes, as it is given them, read by a controller.
#[test]
fn a_controller_reads_an_entitys_interface_counters() {
    let mut network = Network::new();
    let mut counters = [0; 32];
    counters[0] = 2;
    counters[1] = 1;
    counters[5] = 3;
    network
        .talker
        .set_counters(DescriptorType::AVB_INTERFACE, 0, counters);
    network.controller.discover(None);
    network.run(3000);
    let model = network.controller.model(TALKER).expect("read");
    let read = model
        .counters(DescriptorType::AVB_INTERFACE, 0)
        .expect("GET_COUNTERS answered");
    assert_eq!(read.valid, 0b10_0011);
    assert_eq!(&read.block[..6], &[2, 1, 0, 0, 0, 3]);
}

#[test]
fn a_controller_reads_an_entitys_avb_lite_status() {
    use atdecc::ClockIdentity as Clock;
    use atdecc::lite::{FallbackReason, LiteFlags, LiteStatus, PtpProfile};

    let mut network = Network::new();
    let status = LiteStatus {
        interface: 0,
        flags: LiteFlags::CAPABLE.union(LiteFlags::ACTIVE),
        fallback_reason: FallbackReason::PDELAY_UNANSWERED,
        ptp_profile: PtpProfile::AVB_LITE_PTP,
        ptp_domain: 0,
        media_vlan_id: 2,
        unicast_fanout_limit: 2,
        link_speed: 1000,
        committed_egress: 0,
        grandmaster: Clock(0),
        offset_from_grandmaster: 0,
    };
    network.talker.set_lite_status(status);
    network.controller.discover(None);
    network.run(3000);
    let model = network.controller.model(TALKER).expect("read");
    assert_eq!(model.lite_supported, Some(true));
    assert_eq!(model.lite_status(0), Some(&status));
    // The listener has no AVB Lite to report.
    let listener = network.controller.model(LISTENER).expect("read");
    assert_ne!(listener.lite_supported, Some(true));
    assert_eq!(listener.lite_status(0), None);
}

#[test]
fn a_controller_allows_escalation_with_set_lite_config() {
    use atdecc::ClockIdentity as Clock;
    use atdecc::aecp::VendorUniquePdu;
    use atdecc::lite::{
        FallbackReason, LiteConfigFlags, LiteFlags, LiteStatus, PtpProfile, encode_set_lite_config,
    };

    let mut network = Network::new();
    let status = LiteStatus {
        interface: 0,
        flags: LiteFlags::CAPABLE.union(LiteFlags::ACTIVE),
        fallback_reason: FallbackReason::ENDPOINT_TLV,
        ptp_profile: PtpProfile::AVB_LITE_PTP,
        ptp_domain: 0,
        media_vlan_id: 2,
        unicast_fanout_limit: 2,
        link_speed: 1000,
        committed_egress: 0,
        grandmaster: Clock(0),
        offset_from_grandmaster: 0,
    };
    network.talker.set_lite_status(status);
    let now = network.now;
    let controller = MacAddress([0x02, 0, 0, 0, 0, 1]);
    let set = |talker: &mut Entity, interface: u16, flags: u8| {
        let mut out = [0; 64];
        let length = encode_set_lite_config(
            TALKER,
            EntityId(0x0200_00ff_fe00_0001),
            9,
            interface,
            LiteConfigFlags(flags),
            &mut out,
        )
        .unwrap();
        talker.handle_frame(now, controller, &out[..length]);
        let (to, response) = talker.poll_transmit().expect("an answer");
        assert_eq!(to, controller);
        let response = VendorUniquePdu::decode(&response).unwrap();
        (response.header.status, response.payload.to_vec())
    };
    // Allowed: the status says so, and the daemon hears of it.
    let (status_code, payload) = set(&mut network.talker, 0, 0x01);
    assert_eq!(status_code, 0);
    assert_eq!(payload, [0x00, 0x01, 0x00, 0x00, 0x01, 0, 0, 0]);
    assert!(
        network
            .talker
            .lite_status()
            .unwrap()
            .flags
            .contains(LiteFlags::ESCALATION_ALLOWED)
    );
    assert_eq!(
        network.talker.poll_event(),
        Some(EntityEvent::LiteConfigChanged {
            escalation_allowed: true
        })
    );
    // A reserved bit, or an interface it does not have, changes nothing.
    assert_eq!(set(&mut network.talker, 0, 0x03).0, 7);
    assert_eq!(set(&mut network.talker, 1, 0x00).0, 2);
    assert!(
        network
            .talker
            .lite_status()
            .unwrap()
            .flags
            .contains(LiteFlags::ESCALATION_ALLOWED)
    );
    assert_eq!(network.talker.poll_event(), None);
}

#[test]
fn a_controller_sets_an_entitys_lite_config() {
    use atdecc::ClockIdentity as Clock;
    use atdecc::controller::Outcome;
    use atdecc::lite::{FallbackReason, LiteFlags, LiteStatus, PtpProfile};

    let mut network = Network::new();
    network.talker.set_lite_status(LiteStatus {
        interface: 0,
        flags: LiteFlags::CAPABLE.union(LiteFlags::ACTIVE),
        fallback_reason: FallbackReason::ENDPOINT_TLV,
        ptp_profile: PtpProfile::AVB_LITE_PTP,
        ptp_domain: 0,
        media_vlan_id: 2,
        unicast_fanout_limit: 2,
        link_speed: 1000,
        committed_egress: 0,
        grandmaster: Clock(0),
        offset_from_grandmaster: 0,
    });
    network.controller.discover(None);
    network.run(3000);
    let now = network.now;
    let command = network.controller.set_lite_config(now, TALKER, 0, true);
    network.run(1000);
    assert!(
        network.controller_events.iter().any(
            |event| matches!(event, Event::CommandFinished(id, Outcome::Done) if *id == command)
        )
    );
    let model = network.controller.model(TALKER).expect("read");
    assert!(
        model
            .lite_status(0)
            .unwrap()
            .flags
            .contains(LiteFlags::ESCALATION_ALLOWED)
    );
}

#[test]
fn the_sampling_rate_and_stream_formats_move_together() {
    use atdecc::descriptor::SamplingRate;

    // AAF, 8 channels of 32-bit, at 96 kHz: 12 samples a frame.
    const AAF_96K_8CH: StreamFormat = StreamFormat(0x0207_0220_0200_c000);
    let mut network = Network::new();
    let mut model = model(TALKER, "Talker", true);
    model.sampling_rates = vec![48_000, 96_000];
    model.outputs[0].formats = vec![AAF_48K_8CH, AAF_96K_8CH];
    let mut talker = Entity::new(model, &[(STREAM, DESTINATION)]);
    talker.start(network.now);
    network.talker = talker;
    network.controller.discover(None);
    network.run(3000);

    // The rate goes to 96 kHz, and the stream with it.
    let now = network.now;
    network.controller.set_sampling_rate(
        now,
        TALKER,
        DescriptorType::AUDIO_UNIT,
        0,
        SamplingRate(96_000),
    );
    network.run(1000);
    assert_eq!(network.talker.model().current_sampling_rate, 96_000);
    assert_eq!(
        network.talker.model().outputs[0].current_format,
        AAF_96K_8CH
    );
    assert!(
        network
            .entity_events
            .contains(&EntityEvent::StreamFormatChanged {
                descriptor_type: DescriptorType::STREAM_OUTPUT,
                index: 0,
                format: AAF_96K_8CH,
            })
    );

    // A stream format at 48 kHz brings the rate back.
    let now = network.now;
    network.controller.set_stream_format(
        now,
        TALKER,
        DescriptorType::STREAM_OUTPUT,
        0,
        AAF_48K_8CH,
    );
    network.run(1000);
    assert_eq!(network.talker.model().current_sampling_rate, 48_000);
    assert!(
        network
            .entity_events
            .contains(&EntityEvent::SamplingRateChanged(48_000))
    );
    // The controller hears both, unsolicited or in answers.
    let read = network.controller.model(TALKER).unwrap();
    assert_eq!(
        read.audio_units()
            .next()
            .map(|unit| unit.current_sampling_rate.0),
        Some(48_000)
    );
    assert_eq!(
        read.streams(false)
            .next()
            .map(|stream| stream.current_format),
        Some(AAF_48K_8CH)
    );
}

/// A stream whose channels take more than a frame at the new rate moves
/// to its packing of the most channels that fit there.
#[test]
fn a_new_rate_too_fast_for_a_streams_channels_takes_fewer() {
    use atdecc::descriptor::SamplingRate;

    // AAF of 32-bit at 48 kHz, 16 channels; at 192 kHz, 24 samples a
    // frame, 8 and 2 channels.
    const AAF_48K_16CH: StreamFormat = StreamFormat(0x0205_0220_0400_6000);
    const AAF_192K_8CH: StreamFormat = StreamFormat(0x0209_0220_0201_8000);
    const AAF_192K_2CH: StreamFormat = StreamFormat(0x0209_0220_0081_8000);
    let mut network = Network::new();
    let mut model = model(TALKER, "Talker", true);
    model.sampling_rates = vec![48_000, 192_000];
    model.outputs[0].formats = vec![AAF_48K_16CH, AAF_192K_2CH, AAF_192K_8CH];
    model.outputs[0].current_format = AAF_48K_16CH;
    let mut talker = Entity::new(model, &[(STREAM, DESTINATION)]);
    talker.start(network.now);
    network.talker = talker;
    network.controller.discover(None);
    network.run(3000);

    let now = network.now;
    network.controller.set_sampling_rate(
        now,
        TALKER,
        DescriptorType::AUDIO_UNIT,
        0,
        SamplingRate(192_000),
    );
    network.run(1000);
    assert_eq!(network.talker.model().current_sampling_rate, 192_000);
    assert_eq!(
        network.talker.model().outputs[0].current_format,
        AAF_192K_8CH
    );
}

/// A media clock output, CRF, has no stream port, clusters or map: the
/// audio output after it has the one port, mapped to its own stream, and
/// a new rate moves the audio alone.
#[test]
fn a_media_clock_stream_has_no_port_and_keeps_its_rate() {
    use atdecc::adp::TalkerCapabilities;
    use atdecc::descriptor::SamplingRate;

    const CRF_48K: StreamFormat = StreamFormat(0x0410_6001_0000_bb80);
    const AAF_96K_8CH: StreamFormat = StreamFormat(0x0207_0220_0200_c000);
    let mut network = Network::new();
    let mut model = model(TALKER, "Talker", true);
    model.sampling_rates = vec![48_000, 96_000];
    model.outputs[0].formats = vec![AAF_48K_8CH, AAF_96K_8CH];
    model.outputs.insert(
        0,
        StreamModel {
            name: "Media Clock Out 1".into(),
            formats: vec![CRF_48K],
            current_format: CRF_48K,
            channels: 0,
        },
    );
    let clock_stream = StreamId(0x9c6b_0030_9a2b_0001);
    let mut talker = Entity::new(model, &[(clock_stream, DESTINATION), (STREAM, DESTINATION)]);
    talker.start(network.now);
    network.talker = talker;
    network.controller.discover(None);
    network.run(3000);

    let read = network.controller.model(TALKER).expect("read");
    assert_eq!(read.state, EnumerationState::Complete);
    assert_eq!(read.streams(false).count(), 2);
    // It advertises both kinds it sends.
    let advertised = network.controller.entity(TALKER).expect("discovered");
    assert_eq!(
        advertised.adp.talker_capabilities,
        TalkerCapabilities::IMPLEMENTED
            | TalkerCapabilities::AUDIO_SOURCE
            | TalkerCapabilities::MEDIA_CLOCK_SOURCE
    );
    let ports: Vec<_> = read.stream_ports(false).collect();
    assert_eq!(ports.len(), 1, "the audio output's alone");
    let streams: Vec<u16> = read
        .static_mappings(&ports[0])
        .map(|mapping| mapping.stream_index)
        .collect();
    assert_eq!(streams, [1; 8]);

    let now = network.now;
    network.controller.set_sampling_rate(
        now,
        TALKER,
        DescriptorType::AUDIO_UNIT,
        0,
        SamplingRate(96_000),
    );
    network.run(1000);
    let outputs = &network.talker.model().outputs;
    assert_eq!(
        (outputs[0].current_format, outputs[1].current_format),
        (CRF_48K, AAF_96K_8CH)
    );
}

/// A listener whose media clock input is a clock source: the controller
/// reads it as one, after the internal clock, flagged on its input alone,
/// and picking it tells the entity.
#[test]
fn a_media_clock_input_is_a_clock_source() {
    use atdecc::adp::ListenerCapabilities;
    use atdecc::descriptor::{ClockSourceType, StreamFlags};

    const CRF_48K: StreamFormat = StreamFormat(0x0410_6001_0000_bb80);
    let mut network = Network::new();
    let mut model = model(LISTENER, "Listener", false);
    model.inputs.push(StreamModel {
        name: "Media Clock In 1".into(),
        formats: vec![CRF_48K],
        current_format: CRF_48K,
        channels: 0,
    });
    model.clock_source_inputs = vec![1];
    let mut listener = Entity::new(model, &[]);
    listener.start(network.now);
    network.listener = listener;
    network.controller.discover(None);
    network.run(3000);

    let advertised = network.controller.entity(LISTENER).expect("discovered");
    assert_eq!(
        advertised.adp.listener_capabilities,
        ListenerCapabilities::IMPLEMENTED
            | ListenerCapabilities::AUDIO_SINK
            | ListenerCapabilities::MEDIA_CLOCK_SINK
    );
    let read = network.controller.model(LISTENER).expect("read");
    let sources: Vec<(String, ClockSourceType, DescriptorType, u16)> = read
        .clock_sources()
        .map(|source| {
            (
                source.object_name.to_owned(),
                source.clock_source_type,
                source.location_type,
                source.location_index,
            )
        })
        .collect();
    assert_eq!(sources.len(), 2);
    assert_eq!(sources[0].1, ClockSourceType::INTERNAL);
    assert_eq!(
        sources[1],
        (
            "Media Clock In 1".to_owned(),
            ClockSourceType::INPUT_STREAM,
            DescriptorType::STREAM_INPUT,
            1
        )
    );
    let synced: Vec<bool> = read
        .streams(true)
        .map(|stream| stream.stream_flags.contains(StreamFlags::CLOCK_SYNC_SOURCE))
        .collect();
    assert_eq!(synced, [false, true]);

    let now = network.now;
    network.controller.set_clock_source(now, LISTENER, 0, 1);
    network.run(500);
    assert!(
        network
            .entity_events
            .contains(&EntityEvent::ClockSourceChanged(1))
    );
    assert_eq!(network.listener.model().clock_source, 1);
}

/// A talker whose streams change takes over from itself: the controller
/// reads its new model without it ever going offline or restarting.
#[test]
fn a_talker_taking_over_from_itself_is_read_again_in_place() {
    let mut network = Network::new();
    network.controller.discover(None);
    network.run(3000);
    assert_eq!(
        network
            .controller
            .model(TALKER)
            .unwrap()
            .streams(false)
            .count(),
        1
    );
    network.controller_events.clear();

    let mut model = model(TALKER, "Talker", true);
    model.entity_model_id = EntityModelId(0x8c1f_6436_c0f3_59f0);
    model.outputs.push(stream("Host out 2"));
    let second = StreamId(0x9c6b_0030_9a2b_0001);
    let mut talker = Entity::new(model, &[(STREAM, DESTINATION), (second, DESTINATION)]);
    talker.take_over(network.now, &network.talker);
    talker.start(network.now);
    network.talker = talker;
    network.run(2000);

    let events = &network.controller_events;
    assert!(
        !events
            .iter()
            .any(|event| matches!(event, Event::EntityOffline(..) | Event::EntityRestarted(_))),
        "{events:?}"
    );
    assert!(events.contains(&Event::EntityChanged(TALKER)), "{events:?}");
    let read = network.controller.model(TALKER).unwrap();
    assert_eq!(read.state, EnumerationState::Complete);
    assert_eq!(read.streams(false).count(), 2);
}

/// An entity's input bound to its own output probes its own talker and
/// finds its stream, when its frames come back to it, as the daemon hands
/// them.
#[test]
fn an_input_bound_to_its_own_output_settles() {
    let mut network = Network::new();
    let mut model = model(TALKER, "Talker", true);
    model.inputs.push(stream("Host in"));
    model.clock_source_inputs = vec![0];
    let mut talker = Entity::new(model, &[(STREAM, DESTINATION)]);
    talker.start(network.now);
    network.talker = talker;
    network.controller.discover(None);
    network.run(3000);
    let now = network.now;
    let bind = network.controller.connect(now, (TALKER, 0), (TALKER, 0));
    network.run(1000);
    assert!(
        network
            .controller_events
            .contains(&Event::CommandFinished(bind, Outcome::Done)),
        "{:?}",
        network.controller_events
    );
    let settled = network.entity_events.iter().find_map(|event| match event {
        EntityEvent::InputSettled { index: 0, stream } => Some(*stream),
        _ => None,
    });
    assert_eq!(
        settled.map(|stream| (stream.stream_id, stream.destination)),
        Some((STREAM, DESTINATION))
    );
}

/// A listener hearing its own probe, as entities that hear their own
/// frames do, leaves it to the talker it names to answer.
#[test]
fn a_listener_does_not_answer_its_own_probe() {
    use atdecc::acmp::{AcmpMessageType, Acmpdu};

    let mut listener = Entity::new(model(LISTENER, "Listener", false), &[]);
    let now = Instant::ZERO;
    listener.start(now);
    let bind = Acmpdu::command(
        AcmpMessageType::BIND_RX_COMMAND,
        EntityId(0x0200_00ff_fe00_0001),
        (TALKER, 0),
        (LISTENER, 0),
        1,
    );
    let mut out = [0; 128];
    let length = bind.encode(&mut out).unwrap();
    listener.handle_frame(now, CONTROLLER_MAC, &out[..length]);
    listener.handle_timeout(now);
    let probe = std::iter::from_fn(|| listener.poll_transmit())
        .filter_map(|(_, bytes)| Acmpdu::decode(&bytes).ok())
        .find(|acmpdu| acmpdu.message_type == AcmpMessageType::PROBE_TX_COMMAND)
        .expect("the probe");
    let length = probe.encode(&mut out).unwrap();
    listener.handle_frame(now, HOST_MAC, &out[..length]);
    let answers: Vec<Acmpdu> = std::iter::from_fn(|| listener.poll_transmit())
        .filter_map(|(_, bytes)| Acmpdu::decode(&bytes).ok())
        .collect();
    assert!(answers.is_empty(), "{answers:?}");
}

/// A listener's input port has dynamic mappings, one to one at first: a
/// controller reads them, moves a channel to another cluster, and is
/// refused one that maps a cluster twice.
#[test]
fn a_listener_maps_its_inputs_dynamically() {
    use atdecc::aem::AudioMapping;

    let mut network = Network::new();
    network.controller.discover(None);
    network.run(3000);
    let port = (DescriptorType::STREAM_PORT_INPUT, 0);
    let read = |network: &Network| -> Vec<(u16, u16)> {
        network
            .controller
            .model(LISTENER)
            .and_then(|model| model.dynamic_mappings(port.0, port.1))
            .map(|mappings| {
                mappings
                    .map(|mapping| (mapping.stream_channel, mapping.cluster_offset))
                    .collect()
            })
            .unwrap_or_default()
    };
    assert_eq!(
        read(&network),
        (0..8).map(|channel| (channel, channel)).collect::<Vec<_>>()
    );
    let mapping = |stream_channel, cluster_offset| AudioMapping {
        stream_index: 0,
        stream_channel,
        cluster_offset,
        cluster_channel: 0,
    };
    // Channel 0 to cluster 7, which first lets go of channel 7.
    let now = network.now;
    network
        .controller
        .remove_audio_mappings(now, LISTENER, port, &[mapping(7, 7)]);
    network.run(500);
    let now = network.now;
    let added = network
        .controller
        .add_audio_mappings(now, LISTENER, port, &[mapping(0, 7)]);
    network.run(500);
    assert!(
        network
            .controller_events
            .contains(&Event::CommandFinished(added, Outcome::Done))
    );
    let mut expected: Vec<(u16, u16)> = (0..7).map(|channel| (channel, channel)).collect();
    expected.push((0, 7));
    expected.sort_by_key(|&(channel, cluster)| (channel, cluster));
    let mut got = read(&network);
    got.sort_by_key(|&(channel, cluster)| (channel, cluster));
    assert_eq!(got, expected);
    assert_eq!(
        network
            .entity_events
            .iter()
            .filter(|event| matches!(event, EntityEvent::MappingsChanged { index: 0 }))
            .count(),
        2
    );
    assert_eq!(network.listener.input_mappings(0).map(<[_]>::len), Some(8));
    // Cluster 1 takes channel 1 already.
    let now = network.now;
    let refused = network
        .controller
        .add_audio_mappings(now, LISTENER, port, &[mapping(2, 1)]);
    network.run(500);
    assert!(network.controller_events.iter().any(|event| matches!(
        event,
        Event::CommandFinished(command, Outcome::Refused(_)) if *command == refused
    )));
}

/// A listener whose streams changed carries an input that runs on into
/// the entity taking over: its binding and the stream its probe found,
/// so it listens on without probing again.
#[test]
fn a_kept_input_carries_its_binding_and_stream() {
    let mut network = Network::new();
    network.controller.discover(None);
    network.run(3000);
    let now = network.now;
    network.controller.connect(now, (TALKER, 0), (LISTENER, 0));
    network.run(1000);
    let found = network.listener.input_stream(0).expect("settled");

    let mut model = model(LISTENER, "Listener", false);
    model.entity_model_id = EntityModelId(0x8c1f_6436_c0f3_59f0);
    model.inputs.push(stream("Host in 2"));
    let mut listener = Entity::new(model, &[]);
    listener.take_over(network.now, &network.listener);
    listener.carry_streams(&network.listener, &[], &[0]);
    assert_eq!(listener.input_stream(0), Some(found));
    assert_eq!(
        listener.input_binding(0).map(|binding| binding.talker),
        Some(TALKER)
    );
    assert_eq!(listener.input_binding(1), None);
}

/// A stream output's SR class follows its format: class B's own, a frame
/// each 250 µs, sends it in class B, as its stream info says, and its
/// max transit time follows from one class's default to the other's,
/// telling the controller; one a controller set stays.
#[test]
fn an_outputs_class_follows_its_format() {
    use atdecc::aem::StreamInfoFlags;
    use atdecc::descriptor::StreamFlags;

    // AAF, 8 channels of 32-bit at 48 kHz: 12 samples a frame, class B's.
    const CLASS_B_48K_8CH: StreamFormat = StreamFormat(0x0205_0220_0200_c000);
    let mut network = Network::new();
    let mut model = model(TALKER, "Talker", true);
    model.outputs[0].formats = vec![CLASS_B_48K_8CH, AAF_48K_8CH];
    model.outputs[0].current_format = CLASS_B_48K_8CH;
    let mut talker = Entity::new(model, &[(STREAM, DESTINATION)]);
    talker.start(network.now);
    network.talker = talker;
    network.controller.discover(None);
    network.run(3000);
    let read = network.controller.model(TALKER).unwrap();
    let output = read.streams(false).next().unwrap();
    assert_eq!(
        output.stream_flags,
        StreamFlags::CLASS_A | StreamFlags::CLASS_B
    );
    let info = read.stream_info(DescriptorType::STREAM_OUTPUT, 0).unwrap();
    assert!(info.flags.contains(StreamInfoFlags::CLASS_B));
    assert_eq!(network.talker.max_transit_time(0), Some(12_000_000));

    // Class A's format takes it to class A, and class A's default.
    let now = network.now;
    network.controller.set_stream_format(
        now,
        TALKER,
        DescriptorType::STREAM_OUTPUT,
        0,
        AAF_48K_8CH,
    );
    network.run(1000);
    assert!(!network.talker.model().outputs[0].class_b());
    assert_eq!(network.talker.max_transit_time(0), Some(2_000_000));
    assert!(network.talker.max_transit_time_is_default(0));
    assert!(
        network
            .entity_events
            .contains(&EntityEvent::MaxTransitTimeChanged {
                index: 0,
                nanoseconds: 2_000_000,
            })
    );
    let read = network.controller.model(TALKER).unwrap();
    assert_eq!(read.max_transit_time(0), Some(2_000_000));

    // A max transit time a controller set stays through the next change.
    let now = network.now;
    network
        .controller
        .set_max_transit_time(now, TALKER, 0, 5_000_000);
    network.run(1000);
    let now = network.now;
    network.controller.set_stream_format(
        now,
        TALKER,
        DescriptorType::STREAM_OUTPUT,
        0,
        CLASS_B_48K_8CH,
    );
    network.run(1000);
    assert!(network.talker.model().outputs[0].class_b());
    assert_eq!(network.talker.max_transit_time(0), Some(5_000_000));
}

/// Asks `entity` for its memory with one Address Access TLV: the status,
/// and the octets the response's TLV carries.
fn address_access(entity: &mut Entity, tlv: AaTlv<'_>) -> (AaStatus, Vec<u8>) {
    let mut tlvs = [0; 600];
    let length = tlv.encode(&mut tlvs).unwrap();
    let command = AddressAccessPdu {
        header: AecpHeader {
            message_type: AecpMessageType::ADDRESS_ACCESS_COMMAND,
            status: 0,
            target_entity_id: TALKER,
            controller_entity_id: EntityId(0x0200_00ff_fe00_0001),
            sequence_id: 3,
        },
        tlv_count: 1,
        tlvs: &tlvs[..length],
    };
    let mut out = [0; 1500];
    let length = command.encode(&mut out).unwrap();
    entity.handle_frame(Instant::ZERO, CONTROLLER_MAC, &out[..length]);
    let (destination, bytes) = entity.poll_transmit().expect("an answer");
    assert_eq!(destination, CONTROLLER_MAC);
    assert!(entity.poll_transmit().is_none());
    let response = AddressAccessPdu::decode(&bytes).unwrap();
    assert_eq!(
        response.header.message_type,
        AecpMessageType::ADDRESS_ACCESS_RESPONSE
    );
    assert_eq!(response.header.sequence_id, 3);
    // Every response's TLVs read as a response's, a refusal's too.
    let tlvs: Vec<AaTlv<'_>> = response.tlvs().map(Result::unwrap).collect();
    (
        response.status(),
        tlvs.first()
            .map(|tlv| tlv.data.to_vec())
            .unwrap_or_default(),
    )
}

#[test]
fn an_entity_gives_its_logos_through_address_access() {
    let logo: Vec<u8> = (0..1000).map(|at| at as u8).collect();
    let vendor_logo: Vec<u8> = (0..300).map(|at| (at * 7) as u8).collect();
    let mut entity = Entity::new(
        EndpointModel {
            logo: Some(logo.clone()),
            vendor_logo: Some(vendor_logo.clone()),
            ..model(TALKER, "Talker", true)
        },
        &[(STREAM, DESTINATION)],
    );
    entity.start(Instant::ZERO);
    let adp = loop {
        if let Some(adp) = std::iter::from_fn(|| entity.poll_transmit())
            .find_map(|(_, bytes)| Adpdu::decode(&bytes).ok())
        {
            break adp;
        }
        let at = entity.poll_timeout().expect("it advertises");
        entity.handle_timeout(at);
    };
    assert!(
        adp.entity_capabilities
            .contains(EntityCapabilities::ADDRESS_ACCESS_SUPPORTED)
    );
    while entity.poll_transmit().is_some() {}
    let read = |address: u64, length: u16| AaTlv {
        mode: AaMode::READ,
        length,
        address,
        data: &[],
    };
    assert_eq!(
        address_access(&mut entity, read(LOGO_ADDRESS + 500, 400)),
        (AaStatus::SUCCESS, logo[500..900].to_vec())
    );
    // The most one response carries, to the logo's end.
    assert_eq!(
        address_access(&mut entity, read(LOGO_ADDRESS + 498, MEMORY_SEGMENT)),
        (AaStatus::SUCCESS, logo[498..].to_vec())
    );
    assert_eq!(
        address_access(&mut entity, read(LOGO_ADDRESS + 900, 200)).0,
        AaStatus::ADDRESS_TOO_HIGH
    );
    // The vendor logo, at its own address, and nothing between the two.
    assert_eq!(
        address_access(&mut entity, read(VENDOR_LOGO_ADDRESS + 100, 200)),
        (AaStatus::SUCCESS, vendor_logo[100..].to_vec())
    );
    assert_eq!(
        address_access(&mut entity, read(LOGO_ADDRESS + 1000, 1)).0,
        AaStatus::ADDRESS_TOO_HIGH
    );
    assert_eq!(
        address_access(&mut entity, read(LOGO_ADDRESS - 1, 10)).0,
        AaStatus::ADDRESS_TOO_LOW
    );
    assert_eq!(
        address_access(&mut entity, read(LOGO_ADDRESS, MEMORY_SEGMENT + 1)).0,
        AaStatus::TLV_INVALID
    );
    let write = AaTlv {
        mode: AaMode::WRITE,
        length: 2,
        address: LOGO_ADDRESS,
        data: &[1, 2],
    };
    assert_eq!(address_access(&mut entity, write).0, AaStatus::UNSUPPORTED);
    assert_eq!(
        logo,
        entity.model().logo.clone().unwrap(),
        "nothing written"
    );

    // Without a logo there is no memory to read.
    let mut plain = Entity::new(model(TALKER, "Talker", true), &[(STREAM, DESTINATION)]);
    plain.start(Instant::ZERO);
    while plain.poll_transmit().is_some() {}
    assert_eq!(
        address_access(&mut plain, read(LOGO_ADDRESS, 10)).0,
        AaStatus::NOT_IMPLEMENTED
    );
}

#[test]
fn a_controller_reads_the_logos() {
    let mut network = Network::new();
    // More than one read holds, and less.
    let logo: Vec<u8> = (0..1200).map(|at| (at % 251) as u8).collect();
    let vendor_logo = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a];
    network.talker = Entity::new(
        EndpointModel {
            logo: Some(logo.clone()),
            vendor_logo: Some(vendor_logo.clone()),
            ..model(TALKER, "Talker", true)
        },
        &[(STREAM, DESTINATION)],
    );
    network.talker.start(Instant::ZERO);
    network.controller.discover(None);
    network.run(3000);
    let model = network.controller.model(TALKER).expect("read");
    assert_eq!(model.state, EnumerationState::Complete);
    let descriptor = model
        .descriptor(DescriptorType::MEMORY_OBJECT, 0)
        .expect("its MEMORY_OBJECT");
    assert_eq!(descriptor.len(), 108);
    let field = |at: usize| u64::from_be_bytes(descriptor[at..at + 8].try_into().unwrap());
    // memory_object_type, then the descriptor it is for.
    assert_eq!(descriptor[70..76], [0x00, 0x0a, 0x00, 0x00, 0x00, 0x00]);
    assert_eq!(field(76), LOGO_ADDRESS);
    assert_eq!(field(84), logo.len() as u64);
    assert_eq!(field(92), logo.len() as u64);
    assert_eq!(field(100), u64::from(MEMORY_SEGMENT));
    let vendor = model
        .descriptor(DescriptorType::MEMORY_OBJECT, 1)
        .and_then(|bytes| MemoryObjectDescriptor::decode(bytes).ok())
        .expect("the vendor logo's");
    assert_eq!(
        vendor.memory_object_type,
        MemoryObjectType::PNG_MANUFACTURER
    );
    assert_eq!(vendor.start_address, VENDOR_LOGO_ADDRESS);
    // Both read through Address Access.
    assert_eq!(model.image(MemoryObjectType::PNG_ENTITY), Some(&logo[..]));
    assert_eq!(
        model.image(MemoryObjectType::PNG_MANUFACTURER),
        Some(&vendor_logo[..])
    );
    // The listener, without one, has none.
    let listener = network.controller.model(LISTENER).unwrap();
    assert!(
        listener
            .descriptor(DescriptorType::MEMORY_OBJECT, 0)
            .is_none()
    );
    assert_eq!(listener.image(MemoryObjectType::PNG_ENTITY), None);
}

/// An entity that answers a READ in place, as ESP32 endpoints do, gives
/// its logo too, as the controller's READs have room for it.
#[test]
fn a_logo_answered_in_place_is_read() {
    let mut network = Network::new();
    let logo: Vec<u8> = (0..1000).map(|at| (at % 241) as u8).collect();
    network.talker = Entity::new(
        EndpointModel {
            logo: Some(logo.clone()),
            ..model(TALKER, "Talker", true)
        },
        &[(STREAM, DESTINATION)],
    );
    network.talker.start(Instant::ZERO);
    network.in_place = Some(logo.clone());
    network.controller.discover(None);
    network.run(3000);
    let model = network.controller.model(TALKER).expect("read");
    assert_eq!(model.image(MemoryObjectType::PNG_ENTITY), Some(&logo[..]));
}

#[test]
fn a_vendor_logo_alone_is_the_first_memory_object() {
    let mut network = Network::new();
    let vendor_logo = vec![1, 2, 3];
    network.talker = Entity::new(
        EndpointModel {
            vendor_logo: Some(vendor_logo.clone()),
            ..model(TALKER, "Talker", true)
        },
        &[(STREAM, DESTINATION)],
    );
    network.talker.start(Instant::ZERO);
    network.controller.discover(None);
    network.run(3000);
    let model = network.controller.model(TALKER).expect("read");
    let objects: Vec<MemoryObjectDescriptor<'_>> = model.memory_objects().collect();
    assert_eq!(objects.len(), 1);
    assert_eq!(objects[0].index, 0);
    assert_eq!(
        objects[0].memory_object_type,
        MemoryObjectType::PNG_MANUFACTURER
    );
    assert_eq!(objects[0].maximum_segment_length, Some(502));
    assert_eq!(
        model.image(MemoryObjectType::PNG_MANUFACTURER),
        Some(&vendor_logo[..])
    );
    assert_eq!(model.image(MemoryObjectType::PNG_ENTITY), None);
}

#[test]
fn an_entity_advertises_again_as_its_link_comes_up() {
    let mut entity = Entity::new(model(TALKER, "Talker", true), &[(STREAM, DESTINATION)]);
    let advertisements = |entity: &mut Entity| {
        std::iter::from_fn(|| entity.poll_transmit())
            .filter(|(_, bytes)| Adpdu::decode(bytes).is_ok())
            .count()
    };
    let mut now = Instant::ZERO;
    entity.link_up(now);
    assert_eq!(entity.poll_timeout(), None, "nothing before it starts");
    entity.start(now);
    // Its first advertisement goes out as the link goes down, to no one.
    now += Duration::from_millis(500);
    entity.handle_timeout(now);
    assert_eq!(advertisements(&mut entity), 1);
    // Up again a few seconds later: it advertises within the moment
    // ENTITY_DISCOVER gives, not 10 s after the first.
    now += Duration::from_secs(4);
    entity.link_up(now);
    now += Duration::from_millis(500);
    entity.handle_timeout(now);
    assert_eq!(advertisements(&mut entity), 1);
}