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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use bitfield::bitfield;
use packet_encoding::{decodable_enum, Decodable, Encodable};

/// The DLC PN frame definition.
mod dlc_parameter_negotiation;
pub use dlc_parameter_negotiation::{
    CreditBasedFlowHandshake, ParameterNegotiationParams, DEFAULT_INITIAL_CREDITS,
};

/// The Flow Control On/Off frame definitions.
mod flow_control;
pub use flow_control::FlowControlParams;

/// The Modem Status frame definition.
mod modem_status;
pub use modem_status::ModemStatusParams;

/// The Not-supported frame definition.
mod non_supported;
pub use non_supported::NonSupportedCommandParams;

/// The Remote Line Status frame definition.
mod remote_line_status;
pub use remote_line_status::{RemoteLineStatusParams, RlsError};

/// The Remote Port Negotiation frame definition.
mod remote_port_negotiation;
pub use remote_port_negotiation::RemotePortNegotiationParams;

/// The Test Command frame definition.
mod test_command;
pub use test_command::TestCommandParams;

use crate::{
    frame::{CommandResponse, FrameParseError},
    DLCI,
};

decodable_enum! {
    /// The supported Multiplexer Commands in RFCOMM. These commands are sent/received
    /// over the Multiplexer Control Channel (DLCI 0) and are 6 bits wide.
    /// See RFCOMM 4.3.
    pub enum MuxCommandMarker<u8, FrameParseError, OutOfRange> {
        ParameterNegotiation = 0b100000,
        Test = 0b001000,
        FlowControlOn = 0b101000,
        FlowControlOff = 0b011000,
        ModemStatus = 0b111000,
        NonSupportedCommand = 0b000100,
        RemotePortNegotiation = 0b100100,
        RemoteLineStatus = 0b010100,
    }
}

/// The minimum size (in bytes) of a MuxCommand. 1 Byte for the Type field,
/// and at least 1 byte for the Length field. See GSM 7.10 Section 5.4.6.1.
const MIN_MUX_COMMAND_SIZE: usize = 2;

/// The MuxCommand Type Field is the first byte in the payload.
/// See GSM 7.10 Section 5.4.6.1.
const MUX_COMMAND_TYPE_IDX: usize = 0;
bitfield! {
    struct TypeField(u8);
    impl Debug;
    pub bool, ea_bit, set_ea_bit: 0;
    pub bool, cr_bit, set_cr_bit: 1;
    pub u8, command_type_raw, set_command_type: 7, 2;
}

impl TypeField {
    fn command_type(&self) -> Result<MuxCommandMarker, FrameParseError> {
        MuxCommandMarker::try_from(self.command_type_raw())
            .or(Err(FrameParseError::UnsupportedMuxCommandType(self.command_type_raw())))
    }

    fn command_response(&self) -> CommandResponse {
        // C/R bit is always set for commands - see GSM 7.10 Section 5.4.6.2.
        if self.cr_bit() {
            CommandResponse::Command
        } else {
            CommandResponse::Response
        }
    }
}

/// The MuxCommand Length Field starts at the second byte in the payload.
/// See GSM 7.10 Section 5.4.6.1.
const MUX_COMMAND_LENGTH_IDX: usize = 1;

/// The length field is represented as multiple E/A padded octets, each 7-bits wide.
/// See GSM 7.10 Section 5.2.1.4.
const MUX_COMMAND_LENGTH_SHIFT: usize = 7;

bitfield! {
    pub struct LengthField(u8);
    impl Debug;
    pub bool, ea_bit, set_ea_bit: 0;
    pub u8, length, set_length: 7, 1;
}

/// The parameters associated with a Mux Command.
#[derive(Clone, Debug, PartialEq)]
pub enum MuxCommandParams {
    ParameterNegotiation(ParameterNegotiationParams),
    FlowControlOn(FlowControlParams),
    FlowControlOff(FlowControlParams),
    ModemStatus(ModemStatusParams),
    NonSupported(NonSupportedCommandParams),
    RemoteLineStatus(RemoteLineStatusParams),
    RemotePortNegotiation(RemotePortNegotiationParams),
    Test(TestCommandParams),
}

impl MuxCommandParams {
    pub fn marker(&self) -> MuxCommandMarker {
        match self {
            Self::ParameterNegotiation(_) => MuxCommandMarker::ParameterNegotiation,
            Self::FlowControlOn(_) => MuxCommandMarker::FlowControlOn,
            Self::FlowControlOff(_) => MuxCommandMarker::FlowControlOff,
            Self::ModemStatus(_) => MuxCommandMarker::ModemStatus,
            Self::NonSupported(_) => MuxCommandMarker::NonSupportedCommand,
            Self::RemoteLineStatus(_) => MuxCommandMarker::RemoteLineStatus,
            Self::RemotePortNegotiation(_) => MuxCommandMarker::RemotePortNegotiation,
            Self::Test(_) => MuxCommandMarker::Test,
        }
    }

    fn decode(command_type: &MuxCommandMarker, buf: &[u8]) -> Result<Self, FrameParseError> {
        let params = match command_type {
            MuxCommandMarker::ParameterNegotiation => {
                Self::ParameterNegotiation(ParameterNegotiationParams::decode(buf)?)
            }
            MuxCommandMarker::FlowControlOn => Self::FlowControlOn(FlowControlParams::decode(buf)?),
            MuxCommandMarker::FlowControlOff => {
                Self::FlowControlOff(FlowControlParams::decode(buf)?)
            }
            MuxCommandMarker::ModemStatus => Self::ModemStatus(ModemStatusParams::decode(buf)?),
            MuxCommandMarker::NonSupportedCommand => {
                Self::NonSupported(NonSupportedCommandParams::decode(buf)?)
            }
            MuxCommandMarker::RemoteLineStatus => {
                Self::RemoteLineStatus(RemoteLineStatusParams::decode(buf)?)
            }
            MuxCommandMarker::RemotePortNegotiation => {
                Self::RemotePortNegotiation(RemotePortNegotiationParams::decode(buf)?)
            }
            MuxCommandMarker::Test => Self::Test(TestCommandParams::decode(buf)?),
        };
        Ok(params)
    }
}

impl Encodable for MuxCommandParams {
    type Error = FrameParseError;

    fn encoded_len(&self) -> usize {
        match self {
            Self::ParameterNegotiation(cmd) => cmd.encoded_len(),
            Self::FlowControlOn(cmd) => cmd.encoded_len(),
            Self::FlowControlOff(cmd) => cmd.encoded_len(),
            Self::ModemStatus(cmd) => cmd.encoded_len(),
            Self::NonSupported(cmd) => cmd.encoded_len(),
            Self::RemoteLineStatus(cmd) => cmd.encoded_len(),
            Self::RemotePortNegotiation(cmd) => cmd.encoded_len(),
            Self::Test(cmd) => cmd.encoded_len(),
        }
    }

    fn encode(&self, buf: &mut [u8]) -> Result<(), FrameParseError> {
        match self {
            Self::ParameterNegotiation(cmd) => cmd.encode(buf),
            Self::FlowControlOn(cmd) => cmd.encode(buf),
            Self::FlowControlOff(cmd) => cmd.encode(buf),
            Self::ModemStatus(cmd) => cmd.encode(buf),
            Self::NonSupported(cmd) => cmd.encode(buf),
            Self::RemoteLineStatus(cmd) => cmd.encode(buf),
            Self::RemotePortNegotiation(cmd) => cmd.encode(buf),
            Self::Test(cmd) => cmd.encode(buf),
        }
    }
}

/// Converts the `length` into a vector of E/A padded octets in conformance with the message
/// format defined in GSM 7.10 Section 5.4.6.1.
fn length_to_ea_format(mut length: usize) -> Vec<u8> {
    if length == 0 {
        // Return single octet with 0 length.
        let mut length_field = LengthField(0);
        length_field.set_ea_bit(true);
        return vec![length_field.0];
    }

    let mut octets = vec![];
    let mask = 0b01111111;
    // Process the length field in chunks of 7 - E/A bit = 0 for all non-last octets.
    while length != 0 {
        let mut length_field = LengthField(0);
        let length_octet = (length & mask) as u8;
        length_field.set_length(length_octet);
        length_field.set_ea_bit(false);
        octets.push(length_field);
        length >>= MUX_COMMAND_LENGTH_SHIFT;
    }
    // Tag the last length octet with E/A = 1, signifying last octet.
    let last_idx = octets.len() - 1;
    octets[last_idx].set_ea_bit(true);

    octets.iter().map(|l| l.0).collect()
}

/// The unique identifier associated with a MuxCommand.
/// Some MuxCommands are associated with a DLCI - we uniquely identify such a command by it's
/// optional DLCI and command type.
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct MuxCommandIdentifier(pub Option<DLCI>, pub MuxCommandMarker);

/// Represents an RFCOMM multiplexer command.
#[derive(Clone, Debug, PartialEq)]
pub struct MuxCommand {
    /// The parameters associated with this MuxCommand - see RFCOMM 4.3 for the supported commands.
    pub params: MuxCommandParams,

    /// Whether this is a command or response Mux frame.
    pub command_response: CommandResponse,
}

impl MuxCommand {
    /// Returns the DLCI that the MuxCommand is referring to.
    ///
    /// Note: This is different from the DLCI that the MuxCommand is sent over. All RFCOMM Mux
    /// Commands are sent over the Mux Control DLCI. The DLCI that the command itself is referring
    /// to is different. Some commands are generic (i.e Test Command) and do not refer to a
    /// particular DLCI; in these cases, None is returned.
    pub fn dlci(&self) -> Option<DLCI> {
        match &self.params {
            MuxCommandParams::ParameterNegotiation(pn) => Some(pn.dlci),
            MuxCommandParams::ModemStatus(status) => Some(status.dlci),
            MuxCommandParams::RemoteLineStatus(rls) => Some(rls.dlci),
            MuxCommandParams::RemotePortNegotiation(rpn) => Some(rpn.dlci),
            _ => None,
        }
    }

    /// Returns the "identifier" for this MuxCommand. Namely, it is the combination
    /// of the (optional) DLCI for this command and its command type.
    pub fn identifier(&self) -> MuxCommandIdentifier {
        MuxCommandIdentifier(self.dlci(), self.params.marker())
    }
}

impl Decodable for MuxCommand {
    type Error = FrameParseError;

    fn decode(buf: &[u8]) -> Result<Self, FrameParseError> {
        if buf.len() < MIN_MUX_COMMAND_SIZE {
            return Err(FrameParseError::BufferTooSmall);
        }

        // Parse the Type Field from the buffer.
        let type_field = TypeField(buf[MUX_COMMAND_TYPE_IDX]);
        let command_response = type_field.command_response();
        let command_type = type_field.command_type()?;

        // Parse the Length Field from the buffer. There can be multiple octets.
        // This implementation supports lengths that can fit into a u64 (8 octets) - in practice,
        // most lengths should fit in 1 octet. The Test Command is the only variable-length command.
        const MAX_LENGTH_OCTETS: usize = 8;
        let mut length: u64 = 0;
        let mut num_length_octets: usize = 0;

        for i in MUX_COMMAND_LENGTH_IDX..buf.len() {
            let length_field = LengthField(buf[i]);
            let length_octet: u64 = length_field.length().into();
            length |= length_octet << (MUX_COMMAND_LENGTH_SHIFT * num_length_octets);
            num_length_octets += 1;

            // A set EA bit indicates last octet of the length.
            if length_field.ea_bit() {
                break;
            }

            if num_length_octets > MAX_LENGTH_OCTETS {
                return Err(FrameParseError::InvalidFrame);
            }
        }

        // Validate that the buffer is large enough given the variable length.
        // 1 byte for Type field, `num_length_octets` bytes for the length field, and `length`
        // bytes for the data.
        let header_len = 1 + num_length_octets;
        let calculated_buf_size: u64 = header_len as u64 + length;
        if (buf.len() as u64) < calculated_buf_size {
            return Err(FrameParseError::BufferTooSmall);
        }

        let params_payload = &buf[header_len..calculated_buf_size as usize];
        let params = MuxCommandParams::decode(&command_type, params_payload)?;

        Ok(Self { params, command_response })
    }
}

impl Encodable for MuxCommand {
    type Error = FrameParseError;

    /// Returns the encoded length of the command.
    /// - 1 Byte for Type field + n bytes to encode the length + `length` bytes for the payload.
    fn encoded_len(&self) -> usize {
        let length = self.params.encoded_len();
        1 + length_to_ea_format(length).len() + length
    }

    fn encode(&self, buf: &mut [u8]) -> Result<(), FrameParseError> {
        if buf.len() < self.encoded_len() {
            return Err(FrameParseError::BufferTooSmall);
        }

        // The CommandResponse bit for the type field is set according to GSM 7.10 Section 5.4.6.2.
        let cr_bit = if self.command_response == CommandResponse::Command { true } else { false };

        // Type Field: E/A = 1 always.
        let mut type_field = TypeField(0);
        type_field.set_ea_bit(true);
        type_field.set_cr_bit(cr_bit);
        type_field.set_command_type(u8::from(&self.params.marker()));
        buf[0] = type_field.0;

        // Length Field.
        let length = self.params.encoded_len();
        let length_octets = length_to_ea_format(length);
        let length_end_idx = MUX_COMMAND_LENGTH_IDX + length_octets.len();
        buf[MUX_COMMAND_LENGTH_IDX..length_end_idx].copy_from_slice(&length_octets);

        // Payload of the MuxCommand.
        self.params.encode(&mut buf[length_end_idx..])?;

        Ok(())
    }
}

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

    use assert_matches::assert_matches;
    use modem_status::ModemStatusSignals;

    #[test]
    fn test_decode_mux_command_empty_buf() {
        let empty_buf = [];
        assert_matches!(MuxCommand::decode(&empty_buf[..]), Err(FrameParseError::BufferTooSmall));
    }

    #[test]
    fn test_decode_mux_command_invalid_command_type() {
        let buf = [
            0b11111101, // Type field, unrecognized command type.
            0b00000001, // Length field, 0 length.
        ];
        assert_matches!(
            MuxCommand::decode(&buf[..]),
            Err(FrameParseError::UnsupportedMuxCommandType(0b111111))
        );
    }

    #[test]
    fn test_decode_mux_command_too_large_length() {
        let buf = [
            0b10000001, // Type field - DLCParameterNegotiation.
            0b00000010, // Length field, 9 octets - this is too large.
            0b00000010, // Length field, octet 2.
            0b00000010, // Length field, octet 3.
            0b00000010, // Length field, octet 4.
            0b00000010, // Length field, octet 5.
            0b00000010, // Length field, octet 6.
            0b00000010, // Length field, octet 7.
            0b00000010, // Length field, octet 8.
            0b00000010, // Length field, octet 9.
            0b00000000, // Length values - shouldn't matter since we error early.
        ];
        assert_matches!(MuxCommand::decode(&buf[..]), Err(FrameParseError::InvalidFrame));
    }

    #[test]
    fn test_decode_mux_command_missing_length_octets() {
        let buf = [
            0b10000001, // Type field - DLCParameterNegotiation.
            0b00000101, // Length field, Length = 2.
            0b00000010, // Length data octet 1.
                        // Missing octet 2 of length.
        ];
        assert_matches!(MuxCommand::decode(&buf[..]), Err(FrameParseError::BufferTooSmall));
    }

    #[test]
    fn test_decode_dlc_parameter_negotiation_command() {
        let buf = [
            0b10000011, // Type field - DLCParameterNegotiation, C/R = Command.
            0b00010001, // Length field - 1 octet. Length = 8.
            0b00000000, // Data Octet1: DLCI of 0 is OK.
            0b11110000, // Data Octet2: SupportedRequest.
            0b00001100, // Data Octet3: Priority = 12.
            0b00000000, // Data Octet4: Ignored.
            0b00010100, // Data Octet5: Max Frame Size Octet 1 = 20.
            0b00000000, // Data Octet6: Max Frame Size Octet 2 = 0.
            0b00000000, // Data Octet7: Ignored
            0b00000001, // Data Octet8: Initial Credits = 1.
        ];
        let expected = MuxCommand {
            params: MuxCommandParams::ParameterNegotiation(ParameterNegotiationParams {
                dlci: DLCI::try_from(0).unwrap(),
                credit_based_flow_handshake: CreditBasedFlowHandshake::SupportedRequest,
                priority: 12,
                max_frame_size: 20,
                initial_credits: 1,
            }),
            command_response: CommandResponse::Command,
        };
        assert_eq!(MuxCommand::decode(&buf[..]).unwrap(), expected);
    }

    #[test]
    fn test_decode_test_command() {
        let buf = [
            0b00100011, // Type field - Test Command, C/R = Command, E/A = 1.
            0b00001001, // Length field - 1 octet. Length = 4.
            0b00000000, // Data Octet1: Test pattern.
            0b00000000, // Data Octet2: Test pattern.
            0b00000000, // Data Octet3: Test pattern.
            0b00000000, // Data Octet4: Test pattern.
        ];
        let expected = MuxCommand {
            params: MuxCommandParams::Test(TestCommandParams {
                test_pattern: vec![0x00, 0x00, 0x00, 0x00],
            }),
            command_response: CommandResponse::Command,
        };
        assert_eq!(MuxCommand::decode(&buf[..]).unwrap(), expected);
    }

    #[test]
    fn test_decode_flow_control_on_command() {
        let buf = [
            0b10100001, // Type field - Test Command, C/R = Response.
            0b00000001, // Length field - 1 octet. Length = 0.
        ];
        let expected = MuxCommand {
            params: MuxCommandParams::FlowControlOn(FlowControlParams {}),
            command_response: CommandResponse::Response,
        };
        assert_eq!(MuxCommand::decode(&buf[..]).unwrap(), expected);
    }

    #[test]
    fn test_decode_flow_control_off_command() {
        let buf = [
            0b01100011, // Type field - Test Command, C/R = Command.
            0b00000001, // Length field - 1 octet. Length = 0.
        ];
        let expected = MuxCommand {
            params: MuxCommandParams::FlowControlOff(FlowControlParams {}),
            command_response: CommandResponse::Command,
        };
        assert_eq!(MuxCommand::decode(&buf[..]).unwrap(), expected);
    }

    #[test]
    fn test_decode_modem_status_command() {
        let buf = [
            0b11100011, // Type field - Test Command, C/R = Command.
            0b00000111, // Length field - 1 octet. Length = 3.
            0b00001111, // Data Octet1: DLCI = 3, E/A = 1, Bit2 = 1 always.
            0b00000000, // Data Octet2: Signals = 0, E/A = 0 -> Break.
            0b00000001, // Data Octet3: E/A = 1, B1 = 0 -> no break.
        ];
        let expected = MuxCommand {
            params: MuxCommandParams::ModemStatus(ModemStatusParams {
                dlci: DLCI::try_from(3).unwrap(),
                signals: ModemStatusSignals(0),
                break_value: None,
            }),
            command_response: CommandResponse::Command,
        };
        assert_eq!(MuxCommand::decode(&buf[..]).unwrap(), expected);
    }

    #[test]
    fn test_decode_not_supported_command_response() {
        let buf = [
            0b00010001, // Type field - Test Command, C/R = 0 (Response).
            0b00000011, // Length field - 1 octet. Length = 1.
            0b00000001, // Data octet 1: Command Type = 0 (unknown).
        ];
        let expected = MuxCommand {
            params: MuxCommandParams::NonSupported(NonSupportedCommandParams {
                cr_bit: false,
                non_supported_command: 0,
            }),
            command_response: CommandResponse::Response,
        };
        assert_eq!(MuxCommand::decode(&buf[..]).unwrap(), expected);
    }

    #[test]
    fn test_decode_rpn_command() {
        let buf = [
            0b10010011, // Type field - Test Command, C/R = Command.
            0b00000011, // Length field - 1 octet. Length = 1.
            0b00011111, // Data octet1: DLCI = 7 is OK, E/A = 1, Bit2 = 1 always.
        ];
        let expected = MuxCommand {
            params: MuxCommandParams::RemotePortNegotiation(RemotePortNegotiationParams {
                dlci: DLCI::try_from(7).unwrap(),
                port_values: None,
            }),
            command_response: CommandResponse::Command,
        };
        assert_eq!(MuxCommand::decode(&buf[..]).unwrap(), expected);
    }

    #[test]
    fn test_decode_rls_command() {
        let buf = [
            0b01010011, // Type field - Test Command, C/R = Command.
            0b00000101, // Length field - 1 octet. Length = 2.
            0b00011111, // Data octet1: DLCI = 7, E/A = 1, Bit2 = 1 always.
            0b00000000, // Data octet2: Bit1 = 0 -> No Status.
        ];
        let expected = MuxCommand {
            params: MuxCommandParams::RemoteLineStatus(RemoteLineStatusParams::new(
                DLCI::try_from(7).unwrap(),
                None,
            )),
            command_response: CommandResponse::Command,
        };
        assert_eq!(MuxCommand::decode(&buf[..]).unwrap(), expected);
    }

    #[test]
    fn test_encode_invalid_buffer() {
        let command = MuxCommand {
            params: MuxCommandParams::RemotePortNegotiation(RemotePortNegotiationParams {
                dlci: DLCI::try_from(5).unwrap(),
                port_values: None,
            }),
            command_response: CommandResponse::Command,
        };
        let mut buf = vec![];
        assert_matches!(command.encode(&mut buf[..]), Err(FrameParseError::BufferTooSmall));
    }

    #[test]
    fn test_encode_rpn_response() {
        let command = MuxCommand {
            params: MuxCommandParams::RemotePortNegotiation(RemotePortNegotiationParams {
                dlci: DLCI::try_from(5).unwrap(),
                port_values: None,
            }),
            command_response: CommandResponse::Response,
        };
        let mut buf = vec![0; command.encoded_len()];
        assert!(command.encode(&mut buf[..]).is_ok());
        let expected = vec![
            0b10010001, // CommandType = RPN, C/R = 0 (Response), E/A = 1.
            0b00000011, // Length = 1, E/A = 1.
            0b00010111, // Payload = RPN Command Params.
        ];
        assert_eq!(buf, expected);
    }

    /// Test Command is the only command with a variable payload length for the parameters.
    /// Tests encoding a Test Command with a very long payload.
    #[test]
    fn test_encode_long_test_command() {
        // 128 byte pattern - requires two octets for length.
        let test_pattern = vec![
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
            0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
            0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
            0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
            0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03,
            0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01,
            0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
            0x0e, 0x0f,
        ];
        let command = MuxCommand {
            params: MuxCommandParams::Test(TestCommandParams {
                test_pattern: test_pattern.clone(),
            }),
            command_response: CommandResponse::Command,
        };
        let mut buf = vec![0; command.encoded_len()];
        assert!(command.encode(&mut buf[..]).is_ok());
        let expected = vec![
            0b00100011, // CommandType = Test, C/R = 1 (Command), E/A = 1.
            0b00000000, // Length Octet1 = 0, E/A = 0.
            0b00000011, // Length Octet2 = 128, E/A = 1.
        ];
        let expected = vec![expected, test_pattern].concat();
        assert_eq!(buf, expected);
    }

    #[test]
    fn test_zero_length_to_ea_format() {
        let length = 0;
        let expected = [0b00000001];
        assert_eq!(length_to_ea_format(length), expected);
    }

    #[test]
    fn test_small_length_to_ea_format() {
        let small_length = 8;
        let expected = [0b00010001];
        assert_eq!(length_to_ea_format(small_length), expected);
    }

    #[test]
    fn test_two_octet_length_to_ea_format() {
        let two_octet_length = 245;
        let expected = [0b11101010, 0b00000011];
        assert_eq!(length_to_ea_format(two_octet_length), expected);
    }

    #[test]
    fn test_multi_octet_length_to_ea_format() {
        let multi_octet_length = 0b100000001100001001000;
        let expected = [0b10010000, 0b01100000, 0b10000001];
        assert_eq!(length_to_ea_format(multi_octet_length), expected);
    }
}