atdecc 0.2.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
640
641
642
643
644
645
646
647
648
649
650
//! The entity model a talker or listener entity serves: its descriptors,
//! built from a few settings and encoded as READ_DESCRIPTOR answers them
//! (IEEE 1722.1-2021, 7.2), with the current names, formats, sampling
//! rate and clock source.
//!
//! One configuration with one audio unit, one AVB interface and one clock
//! domain. Each audio stream has a stream port of its own on the audio
//! unit, with an audio cluster of one channel for each of the stream's
//! channels: an output's mapped to them by a static map, an input's by
//! dynamic mappings controllers change, as Milan has inputs' (the entity
//! keeps those); a media clock stream, CRF, has none. The clock sources are the internal clock, then the stream inputs
//! the model offers as clock sources.

use alloc::string::String;
use alloc::vec::Vec;

use avb_net::MacAddress;

use crate::aecp::{AaStatus, AaTlv, AddressAccessPdu};
use crate::aem::NAME_LENGTH;
use crate::descriptor::{
    ClockSourceType, DescriptorType, InterfaceFlags, MemoryObjectType, StreamFlags,
};
use crate::id::{ClockIdentity, EntityId, EntityModelId};
use crate::stream_format::StreamFormat;

/// No localized string (7.3.6).
const NO_STRING: u16 = 0xffff;
/// The audio cluster format of PCM samples (Table 7-37, MBLA).
const MBLA: u8 = 0x40;
/// The IDENTIFY control type (7.3.4, Avnu's OUI-24 90-E0-F0).
pub const IDENTIFY_CONTROL_TYPE: u64 = 0x90e0_f000_0000_0001;
/// CONTROL_LINEAR_UINT8 (Table 7-41).
const LINEAR_UINT8: u16 = 0x0001;
/// The clock source location type of the internal clock: the entity.
const LOCATION_ENTITY: u16 = 0x0000;

/// A stream input or output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StreamModel {
    pub name: String,
    /// The formats it can take, the first the default.
    pub formats: Vec<StreamFormat>,
    pub current_format: StreamFormat,
    /// Its channels, each an audio cluster; none for a media clock.
    pub channels: u16,
}

impl StreamModel {
    /// Sent in SR class B rather than A, as its format decides: a frame
    /// each class B interval, as a stream crossing a Wi-Fi hop sends
    /// (AVB Wireless profile, 4).
    pub fn class_b(&self) -> bool {
        self.current_format.is_class_b()
    }
}

/// The interface's PTP Instance as the BTCA ranks it (802.1AS-2020
/// 8.6.2), as its AVB_INTERFACE reports it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ClockQuality {
    pub priority1: u8,
    pub clock_class: u8,
    pub clock_accuracy: u8,
    pub offset_scaled_log_variance: u16,
    pub priority2: u8,
}

impl ClockQuality {
    /// A PTP Instance that is not grandmaster capable (8.6.2.1, 8.6.2.2).
    pub const NOT_GRANDMASTER_CAPABLE: Self = Self {
        priority1: 255,
        clock_class: 255,
        clock_accuracy: 0xfe,
        offset_scaled_log_variance: 0x436a,
        priority2: 248,
    };

    /// A grandmaster capable one of `priority1`, its clock otherwise
    /// unknown (8.6.2.2 to 8.6.2.5). Milan asks 248 of a talker
    /// (4.2.6.2.1).
    pub const fn grandmaster_capable(priority1: u8) -> Self {
        Self {
            priority1,
            clock_class: 248,
            clock_accuracy: 0xfe,
            offset_scaled_log_variance: 0x436a,
            priority2: 248,
        }
    }

    /// Of a PTP Instance of `priority1`: grandmaster capable below 255.
    pub const fn of_priority1(priority1: u8) -> Self {
        if priority1 == 255 {
            Self::NOT_GRANDMASTER_CAPABLE
        } else {
            Self::grandmaster_capable(priority1)
        }
    }

    pub const fn is_grandmaster_capable(&self) -> bool {
        self.priority1 < 255
    }
}

/// What a talker or listener entity is.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EndpointModel {
    pub entity_id: EntityId,
    pub entity_model_id: EntityModelId,
    pub entity_name: String,
    pub group_name: String,
    pub vendor_name: String,
    pub model_name: String,
    pub firmware_version: String,
    pub serial_number: String,
    pub mac: MacAddress,
    pub interface_name: String,
    /// The interface's gPTP clock identity.
    pub clock_identity: ClockIdentity,
    pub clock_quality: ClockQuality,
    pub sampling_rates: Vec<u32>,
    pub current_sampling_rate: u32,
    pub outputs: Vec<StreamModel>,
    pub inputs: Vec<StreamModel>,
    /// The clock domain's current clock source: 0 the internal clock,
    /// then those of `clock_source_inputs`, in order.
    pub clock_source: u16,
    /// The stream inputs that are clock sources too, beside the internal
    /// clock: those whose media clock the entity can follow.
    pub clock_source_inputs: Vec<u16>,
    /// The PNG image controllers show the entity with: a MEMORY_OBJECT
    /// of PNG_ENTITY, its octets read through Address Access.
    pub logo: Option<Vec<u8>>,
    /// The PNG image of its vendor, as `logo`: a MEMORY_OBJECT of
    /// PNG_MANUFACTURER, after the logo's.
    pub vendor_logo: Option<Vec<u8>>,
}

/// Where the logo's octets start, as Address Access reads them.
pub const LOGO_ADDRESS: u64 = 0x1000_0000;
/// Where the vendor logo's octets start.
pub const VENDOR_LOGO_ADDRESS: u64 = 0x2000_0000;

/// A memory object the entity has: what it holds, its name, where its
/// octets start and the octets.
struct MemoryObject<'a> {
    memory_object_type: MemoryObjectType,
    name: &'static str,
    address: u64,
    data: &'a [u8],
}

/// The most octets of a memory object one Address Access response
/// carries, in a TLV of its own.
pub const MEMORY_SEGMENT: u16 = (AddressAccessPdu::MAX_TLVS_LEN - AaTlv::HEADER_LEN) as u16;

/// Fixed-size AEM text: UTF-8, zero padded, cut at a character boundary.
pub(crate) fn name_field(text: &str) -> [u8; NAME_LENGTH] {
    let mut field = [0; NAME_LENGTH];
    let mut end = text.len().min(NAME_LENGTH);
    while !text.is_char_boundary(end) {
        end -= 1;
    }
    field[..end].copy_from_slice(&text.as_bytes()[..end]);
    field
}

/// A descriptor being written: its type and index, then fields in order.
struct Writer(Vec<u8>);

impl Writer {
    fn new(descriptor_type: DescriptorType, index: u16) -> Self {
        let mut bytes = Vec::with_capacity(320);
        bytes.extend_from_slice(&descriptor_type.0.to_be_bytes());
        bytes.extend_from_slice(&index.to_be_bytes());
        Writer(bytes)
    }

    fn u8(&mut self, value: u8) -> &mut Self {
        self.0.push(value);
        self
    }

    fn u16(&mut self, value: u16) -> &mut Self {
        self.0.extend_from_slice(&value.to_be_bytes());
        self
    }

    fn u32(&mut self, value: u32) -> &mut Self {
        self.0.extend_from_slice(&value.to_be_bytes());
        self
    }

    fn u64(&mut self, value: u64) -> &mut Self {
        self.0.extend_from_slice(&value.to_be_bytes());
        self
    }

    fn bytes(&mut self, value: &[u8]) -> &mut Self {
        self.0.extend_from_slice(value);
        self
    }

    fn name(&mut self, text: &str) -> &mut Self {
        self.bytes(&name_field(text))
    }

    /// Zero padding up to `length` octets in all.
    fn pad_to(&mut self, length: usize) -> &mut Self {
        if self.0.len() < length {
            self.0.resize(length, 0);
        }
        self
    }

    fn done(&mut self) -> Vec<u8> {
        core::mem::take(&mut self.0)
    }
}

/// Which stream port, cluster and map belong to which audio stream:
/// inputs' ports first, then outputs'.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Port {
    pub(crate) input: bool,
    /// The stream port's index, counting its way's audio streams.
    pub(crate) index: u16,
    /// The stream's index.
    pub(crate) stream: u16,
    base_cluster: u16,
    /// Its channels, each a cluster.
    pub(crate) channels: u16,
    /// Its map, by AUDIO_MAP index; none for an input's, whose mappings
    /// are dynamic.
    map: Option<u16>,
}

impl EndpointModel {
    pub(crate) fn ports(&self) -> Vec<Port> {
        let mut ports = Vec::new();
        let mut cluster = 0;
        let mut map = 0;
        for (input, streams) in [(true, &self.inputs), (false, &self.outputs)] {
            let audio = streams
                .iter()
                .enumerate()
                .filter(|(_, stream)| !stream.current_format.is_clock() && stream.channels > 0);
            for (index, (stream, model)) in audio.enumerate() {
                ports.push(Port {
                    input,
                    index: index as u16,
                    stream: stream as u16,
                    base_cluster: cluster,
                    channels: model.channels,
                    map: (!input).then_some(map),
                });
                cluster += model.channels;
                if !input {
                    map += 1;
                }
            }
        }
        ports
    }

    /// How many clock sources the clock domain has.
    pub(crate) fn clock_sources(&self) -> u16 {
        1 + self.clock_source_inputs.len() as u16
    }

    /// The stream input clock source `source` follows, none for the
    /// internal clock.
    pub fn clock_source_input(&self, source: u16) -> Option<u16> {
        let at = usize::from(source.checked_sub(1)?);
        self.clock_source_inputs.get(at).copied()
    }

    /// The descriptor types the configuration counts, with how many of
    /// each.
    pub fn descriptor_counts(&self) -> Vec<(DescriptorType, u16)> {
        let mut counts = Vec::from([(DescriptorType::AUDIO_UNIT, 1)]);
        if !self.inputs.is_empty() {
            counts.push((DescriptorType::STREAM_INPUT, self.inputs.len() as u16));
        }
        if !self.outputs.is_empty() {
            counts.push((DescriptorType::STREAM_OUTPUT, self.outputs.len() as u16));
        }
        counts.extend([
            (DescriptorType::AVB_INTERFACE, 1),
            (DescriptorType::CLOCK_SOURCE, self.clock_sources()),
            (DescriptorType::LOCALE, 1),
            (DescriptorType::CONTROL, 1),
            (DescriptorType::CLOCK_DOMAIN, 1),
        ]);
        let memory_objects = self.memory_objects().count() as u16;
        if memory_objects > 0 {
            counts.push((DescriptorType::MEMORY_OBJECT, memory_objects));
        }
        counts
    }

    /// Its memory objects, by MEMORY_OBJECT index: the logo, then the
    /// vendor logo, of those it has.
    fn memory_objects(&self) -> impl Iterator<Item = MemoryObject<'_>> {
        [
            (
                MemoryObjectType::PNG_ENTITY,
                "Logo",
                LOGO_ADDRESS,
                &self.logo,
            ),
            (
                MemoryObjectType::PNG_MANUFACTURER,
                "Vendor logo",
                VENDOR_LOGO_ADDRESS,
                &self.vendor_logo,
            ),
        ]
        .into_iter()
        .filter_map(|(memory_object_type, name, address, data)| {
            Some(MemoryObject {
                memory_object_type,
                name,
                address,
                data: data.as_deref()?,
            })
        })
    }

    /// Whether it has memory for Address Access to read.
    pub(crate) fn has_memory(&self) -> bool {
        self.memory_objects().next().is_some()
    }

    /// The length of the memory object at MEMORY_OBJECT `index`.
    pub(crate) fn memory_length(&self, index: u16) -> Option<u64> {
        let object = self.memory_objects().nth(usize::from(index))?;
        Some(object.data.len() as u64)
    }

    /// `length` octets of its memory objects from `address`, as Address
    /// Access reads them: of the object starting last at or before it.
    pub(crate) fn memory(&self, address: u64, length: u16) -> Result<&[u8], AaStatus> {
        let object = self
            .memory_objects()
            .filter(|object| object.address <= address)
            .max_by_key(|object| object.address)
            .ok_or(AaStatus::ADDRESS_TOO_LOW)?;
        let start =
            usize::try_from(address - object.address).map_err(|_| AaStatus::ADDRESS_TOO_HIGH)?;
        let end = start
            .checked_add(usize::from(length))
            .filter(|end| *end <= object.data.len())
            .ok_or(AaStatus::ADDRESS_TOO_HIGH)?;
        Ok(&object.data[start..end])
    }

    /// The localized strings: the vendor and model names.
    fn strings(&self) -> [&str; 7] {
        [&self.vendor_name, &self.model_name, "", "", "", "", ""]
    }

    /// The descriptor of `descriptor_type` at `index`, as READ_DESCRIPTOR
    /// answers it, or `None` when the entity has none.
    pub fn descriptor(
        &self,
        descriptor_type: DescriptorType,
        index: u16,
        entity: &EntityState,
    ) -> Option<Vec<u8>> {
        let ports = self.ports();
        let mut writer = Writer::new(descriptor_type, index);
        let bytes = match descriptor_type {
            DescriptorType::ENTITY if index == 0 => writer
                .u64(self.entity_id.0)
                .u64(self.entity_model_id.0)
                .u32(entity.capabilities)
                .u16(self.outputs.len() as u16)
                .u16(entity.talker_capabilities)
                .u16(self.inputs.len() as u16)
                .u16(entity.listener_capabilities)
                .u32(0)
                .u32(entity.available_index)
                .u64(0)
                .name(&self.entity_name)
                .u16(0) // vendor_name_string: strings 0
                .u16(1) // model_name_string: strings 1
                .name(&self.firmware_version)
                .name(&self.group_name)
                .name(&self.serial_number)
                .u16(1) // configurations_count
                .u16(0) // current_configuration
                .done(),
            DescriptorType::CONFIGURATION if index == 0 => {
                let counts = self.descriptor_counts();
                writer
                    .name("Configuration")
                    .u16(NO_STRING)
                    .u16(counts.len() as u16)
                    .u16(74);
                for (kind, count) in counts {
                    writer.u16(kind.0).u16(count);
                }
                writer.done()
            }
            DescriptorType::AUDIO_UNIT if index == 0 => {
                let inputs = ports.iter().filter(|port| port.input).count() as u16;
                let outputs = ports.iter().filter(|port| !port.input).count() as u16;
                writer
                    .name("Audio")
                    .u16(NO_STRING)
                    .u16(0) // clock_domain_index
                    .u16(inputs)
                    .u16(0)
                    .u16(outputs)
                    .u16(0)
                    .pad_to(136)
                    .u32(self.current_sampling_rate)
                    .u16(144)
                    .u16(self.sampling_rates.len() as u16);
                for rate in &self.sampling_rates {
                    writer.u32(*rate);
                }
                writer.done()
            }
            DescriptorType::STREAM_INPUT | DescriptorType::STREAM_OUTPUT => {
                let input = descriptor_type == DescriptorType::STREAM_INPUT;
                let streams = if input { &self.inputs } else { &self.outputs };
                let stream = streams.get(usize::from(index))?;
                // The classes its formats go in.
                let mut flags = StreamFlags::empty();
                if stream.formats.iter().any(|format| !format.is_class_b()) {
                    flags |= StreamFlags::CLASS_A;
                }
                if stream.formats.iter().any(|format| format.is_class_b()) {
                    flags |= StreamFlags::CLASS_B;
                }
                if input && self.clock_source_inputs.contains(&index) {
                    flags |= StreamFlags::CLOCK_SYNC_SOURCE;
                }
                writer
                    .name(&stream.name)
                    .u16(NO_STRING)
                    .u16(0) // clock_domain_index
                    .u16(flags.0)
                    .u64(stream.current_format.0)
                    .u16(138)
                    .u16(stream.formats.len() as u16)
                    .pad_to(126) // backup talkers, none
                    .u16(0) // avb_interface_index
                    .u32(0) // buffer_length
                    .u16(138 + 8 * stream.formats.len() as u16)
                    .u16(0) // number_of_redundant_streams
                    .u16(0); // timing
                for format in &stream.formats {
                    writer.u64(format.0);
                }
                writer.done()
            }
            DescriptorType::AVB_INTERFACE if index == 0 => {
                let quality = self.clock_quality;
                let mut flags = InterfaceFlags::GPTP_SUPPORTED | InterfaceFlags::SRP_SUPPORTED;
                if quality.is_grandmaster_capable() {
                    flags |= InterfaceFlags::GPTP_GRANDMASTER_SUPPORTED;
                }
                writer
                    .name(&self.interface_name)
                    .u16(NO_STRING)
                    .bytes(&self.mac.0)
                    .u16(flags.0)
                    .u64(self.clock_identity.0)
                    .u8(quality.priority1)
                    .u8(quality.clock_class)
                    .u16(quality.offset_scaled_log_variance)
                    .u8(quality.clock_accuracy)
                    .u8(quality.priority2)
                    .u8(0) // domain_number
                    .u8(0xfd) // log_sync_interval, -3
                    .u8(0) // log_announce_interval
                    .u8(0) // log_pdelay_interval
                    .u16(1) // port_number
                    .pad_to(110)
                    .done()
            }
            DescriptorType::CLOCK_SOURCE if index < self.clock_sources() => {
                if index == 0 {
                    writer
                        .name("Internal")
                        .u16(NO_STRING)
                        .u16(0)
                        .u16(ClockSourceType::INTERNAL.0)
                        .u64(0)
                        .u16(LOCATION_ENTITY)
                        .u16(0)
                } else {
                    let input = self.clock_source_input(index)?;
                    let name = self.inputs.get(usize::from(input))?.name.as_str();
                    writer
                        .name(name)
                        .u16(NO_STRING)
                        .u16(0)
                        .u16(ClockSourceType::INPUT_STREAM.0)
                        .u64(0)
                        .u16(DescriptorType::STREAM_INPUT.0)
                        .u16(input)
                };
                writer.pad_to(94).done()
            }
            DescriptorType::CLOCK_DOMAIN if index == 0 => {
                writer
                    .name("Clock Domain")
                    .u16(NO_STRING)
                    .u16(self.clock_source)
                    .u16(76)
                    .u16(self.clock_sources());
                for source in 0..self.clock_sources() {
                    writer.u16(source);
                }
                writer.pad_to(88).done()
            }
            DescriptorType::LOCALE if index == 0 => writer
                .name("en")
                .u16(1) // number_of_strings
                .u16(0) // base_strings
                .pad_to(80)
                .done(),
            DescriptorType::STRINGS if index == 0 => {
                for text in self.strings() {
                    writer.name(text);
                }
                writer.pad_to(460).done()
            }
            DescriptorType::STREAM_PORT_INPUT | DescriptorType::STREAM_PORT_OUTPUT => {
                let input = descriptor_type == DescriptorType::STREAM_PORT_INPUT;
                let port = ports
                    .iter()
                    .find(|port| port.input == input && port.index == index)?;
                writer
                    .u16(0) // clock_domain_index
                    .u16(0) // port_flags
                    .u16(0) // number_of_controls
                    .u16(0) // base_control
                    .u16(port.channels)
                    .u16(port.base_cluster)
                    .u16(u16::from(port.map.is_some())) // number_of_maps
                    .u16(port.map.unwrap_or(0))
                    .pad_to(28)
                    .done()
            }
            DescriptorType::AUDIO_CLUSTER => {
                let port = ports.iter().find(|port| {
                    (port.base_cluster..port.base_cluster + port.channels).contains(&index)
                })?;
                let channel = index - port.base_cluster + 1;
                let mut name = String::from("Channel ");
                push_number(&mut name, channel);
                writer
                    .name(&name)
                    .u16(NO_STRING)
                    .u16(0xffff) // signal_type: none
                    .u16(0) // signal_index
                    .u16(0) // signal_output
                    .u32(0) // path_latency
                    .u32(0) // block_latency
                    .u16(1) // channel_count
                    .u8(MBLA)
                    .pad_to(100)
                    .done()
            }
            DescriptorType::AUDIO_MAP => {
                let port = ports.iter().find(|port| port.map == Some(index))?;
                writer.u16(8).u16(port.channels);
                for channel in 0..port.channels {
                    writer.u16(port.stream).u16(channel).u16(channel).u16(0);
                }
                writer.pad_to(24).done()
            }
            DescriptorType::CONTROL if index == 0 => writer
                .name("Identify")
                .u16(NO_STRING)
                .u32(0) // block_latency
                .u32(0) // control_latency
                .u16(0) // control_domain
                .u16(LINEAR_UINT8)
                .u64(IDENTIFY_CONTROL_TYPE)
                .u32(0) // reset_time
                .u16(104) // values_offset
                .u16(1) // number_of_values
                .u16(DescriptorType::ENTITY.0) // signal_type
                .u16(0) // signal_index
                .u16(0) // signal_output
                .u8(0) // minimum
                .u8(255) // maximum
                .u8(255) // step
                .u8(0) // default
                .u8(if entity.identifying { 255 } else { 0 })
                .u16(0) // unit
                .u16(NO_STRING)
                .pad_to(212)
                .done(),
            DescriptorType::MEMORY_OBJECT => {
                let object = self.memory_objects().nth(usize::from(index))?;
                let length = object.data.len() as u64;
                writer
                    .name(object.name)
                    .u16(NO_STRING)
                    .u16(object.memory_object_type.0)
                    .u16(DescriptorType::ENTITY.0) // target_descriptor_type
                    .u16(0) // target_descriptor_index
                    .u64(object.address) // start_address
                    .u64(length) // maximum_length
                    .u64(length)
                    .u64(u64::from(MEMORY_SEGMENT)) // maximum_segment_length
                    .done()
            }
            _ => return None,
        };
        Some(bytes)
    }
}

/// The parts of the ENTITY descriptor that come from the running entity
/// rather than its model.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct EntityState {
    pub capabilities: u32,
    pub talker_capabilities: u16,
    pub listener_capabilities: u16,
    pub available_index: u32,
    pub identifying: bool,
}

/// Appends the decimal digits of `number`.
fn push_number(text: &mut String, number: u16) {
    let mut digits = [0u8; 5];
    let mut place = digits.len();
    let mut rest = number;
    loop {
        place -= 1;
        digits[place] = b'0' + (rest % 10) as u8;
        rest /= 10;
        if rest == 0 {
            break;
        }
    }
    for digit in &digits[place..] {
        text.push(char::from(*digit));
    }
}