firewire-dice-protocols 0.3.0

Implementation of protocols defined by TC Applied Technologies for ASICs of Digital Interface Communication Engine (DICE) as well as hardware vendors.
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
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (c) 2024 Takashi Sakamoto

//! Protocol specific to Weiss Engineering AV/C models.
//!
//! The module includes structure, enumeration, and trait and its implementation for protocol
//! defined by Weiss Engineering.
//!
//! MAN301 includes two units in the root directory of its configuration ROM. The first unit
//! expresses AV/C protocol, and the second unit expresses TCAT protocol.
//!
//! ```text
//! spdif-opt-input-1/2  ---+
//! spdif-coax-input-1/2 --(or)--> digital-input-1/2 -----------------> stream-output-1/2
//! aesebu-xlr-input-1/2 ---+             |
//!                                       v
//! stream-input-1/2 --------------------(or)--+----------------------> spdif-coax-output-1/2
//!                                            +----------------------> aesebu-xlr-output-1/2
//!                                            +--analog-output-1/2 --> analog-xlr-output-1/2
//!                                                       +-----------> analog-coax-output-1/2
//! ```

use {
    super::*,
    crate::{tcelectronic::*, *},
    glib::{Error, FileError, IsA},
    hinawa::{
        prelude::{FwFcpExt, FwFcpExtManual},
        FwFcp, FwNode,
    },
    ta1394_avc_general::{general::*, *},
};

/// Protocol implementation specific to MAN301.
#[derive(Default, Debug)]
pub struct WeissMan301Protocol;

impl TcatOperation for WeissMan301Protocol {}

// clock caps: 44100 48000 88200 96000 176400 192000
// clock source names: AES/EBU (XLR)\S/PDIF (RCA)\S/PDIF (TOS)\Unused\Unused\Unused\Unused\Word Clock (BNC)\Unused\Unused\Unused\Unused\Internal\\
impl TcatGlobalSectionSpecification for WeissMan301Protocol {}

/// The implementation of AV/C transaction.
#[derive(Default, Debug)]
pub struct WeissAvc(FwFcp);

impl Ta1394Avc<Error> for WeissAvc {
    fn transaction(&self, command_frame: &[u8], timeout_ms: u32) -> Result<Vec<u8>, Error> {
        let mut resp = vec![0; Self::FRAME_SIZE];
        self.0
            .avc_transaction(&command_frame, &mut resp, timeout_ms)
            .map(|len| {
                resp.truncate(len);
                resp
            })
    }
}

fn from_avc_err(err: Ta1394AvcError<Error>) -> Error {
    match err {
        Ta1394AvcError::CmdBuild(cause) => Error::new(FileError::Inval, &cause.to_string()),
        Ta1394AvcError::CommunicationFailure(cause) => cause,
        Ta1394AvcError::RespParse(cause) => Error::new(FileError::Io, &cause.to_string()),
    }
}

impl WeissAvc {
    /// Bind FCP protocol to the given node for AV/C operation.
    pub fn bind(&self, node: &impl IsA<FwNode>) -> Result<(), Error> {
        self.0.bind(node)
    }

    /// Request AV/C control operation and wait for response.
    pub fn control<O: AvcOp + AvcControl>(
        &self,
        addr: &AvcAddr,
        op: &mut O,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        Ta1394Avc::<Error>::control(self, addr, op, timeout_ms).map_err(|err| from_avc_err(err))
    }

    /// Request AV/C status operation and wait for response.
    pub fn status<O: AvcOp + AvcStatus>(
        &self,
        addr: &AvcAddr,
        op: &mut O,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        Ta1394Avc::<Error>::status(self, addr, op, timeout_ms).map_err(|err| from_avc_err(err))
    }
}

/// The content of command to operate parameter.
#[derive(Debug)]
pub struct WeissAvcParamCmd {
    /// The numeric identifier of parameter.
    pub numeric_id: u32,
    /// The value of parameter.
    pub value: u32,
    /// For future use.
    pub reserved: [u32; 4],
    op: TcAvcCmd,
}

impl Default for WeissAvcParamCmd {
    fn default() -> Self {
        let mut op = TcAvcCmd::new(&WEISS_OUI);
        op.class_id = 1;
        op.sequence_id = u8::MAX;
        op.command_id = 0x8002;
        Self {
            numeric_id: u32::MAX,
            value: u32::MAX,
            reserved: [u32::MAX; 4],
            op: TcAvcCmd::new(&WEISS_OUI),
        }
    }
}

impl AvcOp for WeissAvcParamCmd {
    const OPCODE: u8 = VendorDependent::OPCODE;
}

fn build_param_command_status_data(cmd: &mut WeissAvcParamCmd) -> Result<(), AvcCmdBuildError> {
    cmd.op.arguments.resize(24, u8::MAX);
    serialize_u32(&cmd.numeric_id, &mut cmd.op.arguments[..4]);
    (0..4).for_each(|i| {
        let pos = 8 + i * 4;
        serialize_u32(&cmd.reserved[i], &mut cmd.op.arguments[pos..(pos + 4)]);
    });
    Ok(())
}

fn build_param_command_control_data(cmd: &mut WeissAvcParamCmd) -> Result<(), AvcCmdBuildError> {
    cmd.op.arguments.resize(24, u8::MIN);
    serialize_u32(&cmd.numeric_id, &mut cmd.op.arguments[..4]);
    serialize_u32(&cmd.value, &mut cmd.op.arguments[4..8]);
    (0..4).for_each(|i| {
        let pos = 8 + i * 4;
        serialize_u32(&cmd.reserved[i], &mut cmd.op.arguments[pos..(pos + 4)]);
    });
    Ok(())
}

fn parse_param_command_response_data(cmd: &mut WeissAvcParamCmd) -> Result<(), AvcRespParseError> {
    if cmd.op.arguments.len() < 24 {
        Err(AvcRespParseError::TooShortResp(24))?
    }

    deserialize_u32(&mut cmd.numeric_id, &cmd.op.arguments[..4]);
    deserialize_u32(&mut cmd.value, &cmd.op.arguments[4..8]);
    (0..4).for_each(|i| {
        let pos = 8 + i * 4;
        deserialize_u32(&mut cmd.reserved[i], &cmd.op.arguments[pos..(pos + 4)]);
    });

    Ok(())
}

impl AvcStatus for WeissAvcParamCmd {
    fn build_operands(&mut self, addr: &AvcAddr) -> Result<Vec<u8>, AvcCmdBuildError> {
        build_param_command_status_data(self)?;
        AvcStatus::build_operands(&mut self.op, addr)
    }

    fn parse_operands(&mut self, addr: &AvcAddr, operands: &[u8]) -> Result<(), AvcRespParseError> {
        AvcStatus::parse_operands(&mut self.op, addr, operands)?;
        parse_param_command_response_data(self)
    }
}

impl AvcControl for WeissAvcParamCmd {
    fn build_operands(&mut self, addr: &AvcAddr) -> Result<Vec<u8>, AvcCmdBuildError> {
        build_param_command_control_data(self)?;
        AvcControl::build_operands(&mut self.op, addr)
    }

    fn parse_operands(&mut self, addr: &AvcAddr, operands: &[u8]) -> Result<(), AvcRespParseError> {
        AvcControl::parse_operands(&mut self.op, addr, operands)?;
        parse_param_command_response_data(self)
    }
}

impl WeissMan301Protocol {
    const PARAM_ID_DIGITAL_CAPTURE_SOURCE: u32 = 0;
    const PARAM_ID_DIGITAL_OUTPUT_MODE: u32 = 1;
    const PARAM_ID_WORD_CLOCK_OUTPUT_HALF_RATE: u32 = 2;
    const PARAM_ID_AESEBU_XLR_OUTPUT_MUTE: u32 = 3;
    const PARAM_ID_SPDIF_COAXIAL_OUTPUT_MUTE: u32 = 4;
    const PARAM_ID_ANALOG_OUTPUT_POLARITY_INVERSION: u32 = 5;
    const PARAM_ID_ANALOG_OUTPUT_FILTER_TYPE: u32 = 6;
    const PARAM_ID_ANALOG_OUTPUT_MUTE: u32 = 7;
    const PARAM_ID_ANALOG_OUTPUT_LEVEL: u32 = 8;
}

/// Convert between parameter and command.
pub trait WeissAvcParamConvert<T> {
    /// Build data for status command.
    fn build_status_data(cmd: &mut WeissAvcParamCmd) -> Result<(), Error>;
    /// Build data for control command.
    fn build_control_data(param: &T, cmd: &mut WeissAvcParamCmd) -> Result<(), Error>;
    /// Parse data for response.
    fn parse_response_data(param: &mut T, cmd: &WeissAvcParamCmd) -> Result<(), Error>;
}

/// The source of digital input captured for output packet stream.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum WeissAvcDigitalCaptureSource {
    /// AES/EBU signal in XLR input interface.
    AesebuXlr,
    /// S/PDIF signal in coaxial input interface.
    SpdifCoaxial,
    /// S/PDIF signal in optical input interface.
    SpdifOptical,
}

impl Default for WeissAvcDigitalCaptureSource {
    fn default() -> Self {
        Self::AesebuXlr
    }
}

impl WeissAvcParamConvert<WeissAvcDigitalCaptureSource> for WeissMan301Protocol {
    fn build_status_data(cmd: &mut WeissAvcParamCmd) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_DIGITAL_CAPTURE_SOURCE;
        cmd.value = u32::MAX;
        Ok(())
    }

    fn build_control_data(
        param: &WeissAvcDigitalCaptureSource,
        cmd: &mut WeissAvcParamCmd,
    ) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_DIGITAL_CAPTURE_SOURCE;
        cmd.value = match param {
            WeissAvcDigitalCaptureSource::SpdifOptical => 2,
            WeissAvcDigitalCaptureSource::SpdifCoaxial => 1,
            WeissAvcDigitalCaptureSource::AesebuXlr => 0,
        };
        Ok(())
    }

    fn parse_response_data(
        param: &mut WeissAvcDigitalCaptureSource,
        cmd: &WeissAvcParamCmd,
    ) -> Result<(), Error> {
        assert_eq!(cmd.numeric_id, Self::PARAM_ID_DIGITAL_CAPTURE_SOURCE);
        *param = match cmd.value {
            2 => WeissAvcDigitalCaptureSource::SpdifOptical,
            1 => WeissAvcDigitalCaptureSource::SpdifCoaxial,
            _ => WeissAvcDigitalCaptureSource::AesebuXlr,
        };
        Ok(())
    }
}

/// The mode of AES/EBU XLR output and S/PDIF coaxial output. When enabled, it is in "dual wire"
/// mode at 176.4/192.0 kHz; i.e. the former is for the left audio channel, and the latter is for
/// right audio channel.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct WeissAvcDigitalOutputMode(pub bool);

impl WeissAvcParamConvert<WeissAvcDigitalOutputMode> for WeissMan301Protocol {
    fn build_status_data(cmd: &mut WeissAvcParamCmd) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_DIGITAL_OUTPUT_MODE;
        cmd.value = u32::MAX;
        Ok(())
    }

    fn build_control_data(
        param: &WeissAvcDigitalOutputMode,
        cmd: &mut WeissAvcParamCmd,
    ) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_DIGITAL_OUTPUT_MODE;
        cmd.value = if param.0 { 1 } else { 0 };
        Ok(())
    }

    fn parse_response_data(
        param: &mut WeissAvcDigitalOutputMode,
        cmd: &WeissAvcParamCmd,
    ) -> Result<(), Error> {
        assert_eq!(cmd.numeric_id, Self::PARAM_ID_DIGITAL_OUTPUT_MODE);
        param.0 = cmd.value > 0;
        Ok(())
    }
}

/// The mode of word clock at dual wire mode. When enabled, the output of BNC for word clock is at
/// half of sampling rate.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct WeissAvcWordClockOutputHalfRate(pub bool);

impl WeissAvcParamConvert<WeissAvcWordClockOutputHalfRate> for WeissMan301Protocol {
    fn build_status_data(cmd: &mut WeissAvcParamCmd) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_WORD_CLOCK_OUTPUT_HALF_RATE;
        cmd.value = u32::MAX;
        Ok(())
    }

    fn build_control_data(
        param: &WeissAvcWordClockOutputHalfRate,
        cmd: &mut WeissAvcParamCmd,
    ) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_WORD_CLOCK_OUTPUT_HALF_RATE;
        cmd.value = if param.0 { 1 } else { 0 };
        Ok(())
    }

    fn parse_response_data(
        param: &mut WeissAvcWordClockOutputHalfRate,
        cmd: &WeissAvcParamCmd,
    ) -> Result<(), Error> {
        assert_eq!(cmd.numeric_id, Self::PARAM_ID_WORD_CLOCK_OUTPUT_HALF_RATE);
        param.0 = cmd.value > 0;
        Ok(())
    }
}

/// Mute XLR output.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct WeissAvcAesebuXlrOutputMute(pub bool);

impl WeissAvcParamConvert<WeissAvcAesebuXlrOutputMute> for WeissMan301Protocol {
    fn build_status_data(cmd: &mut WeissAvcParamCmd) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_AESEBU_XLR_OUTPUT_MUTE;
        cmd.value = u32::MAX;
        Ok(())
    }

    fn build_control_data(
        param: &WeissAvcAesebuXlrOutputMute,
        cmd: &mut WeissAvcParamCmd,
    ) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_AESEBU_XLR_OUTPUT_MUTE;
        cmd.value = if param.0 { 0 } else { 1 };
        Ok(())
    }

    fn parse_response_data(
        param: &mut WeissAvcAesebuXlrOutputMute,
        cmd: &WeissAvcParamCmd,
    ) -> Result<(), Error> {
        assert_eq!(cmd.numeric_id, Self::PARAM_ID_AESEBU_XLR_OUTPUT_MUTE);
        param.0 = cmd.value == 0;
        Ok(())
    }
}

/// Mute coaxial output.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct WeissAvcSpdifCoaxialOutputMute(pub bool);

impl WeissAvcParamConvert<WeissAvcSpdifCoaxialOutputMute> for WeissMan301Protocol {
    fn build_status_data(cmd: &mut WeissAvcParamCmd) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_SPDIF_COAXIAL_OUTPUT_MUTE;
        cmd.value = u32::MAX;
        Ok(())
    }

    fn build_control_data(
        param: &WeissAvcSpdifCoaxialOutputMute,
        cmd: &mut WeissAvcParamCmd,
    ) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_SPDIF_COAXIAL_OUTPUT_MUTE;
        cmd.value = if param.0 { 0 } else { 1 };
        Ok(())
    }

    fn parse_response_data(
        param: &mut WeissAvcSpdifCoaxialOutputMute,
        cmd: &WeissAvcParamCmd,
    ) -> Result<(), Error> {
        assert_eq!(cmd.numeric_id, Self::PARAM_ID_SPDIF_COAXIAL_OUTPUT_MUTE);
        param.0 = cmd.value == 0;
        Ok(())
    }
}

/// Invert polarity of analog output.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct WeissAvcAnalogOutputPolarityInversion(pub bool);

impl WeissAvcParamConvert<WeissAvcAnalogOutputPolarityInversion> for WeissMan301Protocol {
    fn build_status_data(cmd: &mut WeissAvcParamCmd) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_ANALOG_OUTPUT_POLARITY_INVERSION;
        cmd.value = u32::MAX;
        Ok(())
    }

    fn build_control_data(
        param: &WeissAvcAnalogOutputPolarityInversion,
        cmd: &mut WeissAvcParamCmd,
    ) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_ANALOG_OUTPUT_POLARITY_INVERSION;
        cmd.value = if param.0 { 1 } else { 0 };
        Ok(())
    }

    fn parse_response_data(
        param: &mut WeissAvcAnalogOutputPolarityInversion,
        cmd: &WeissAvcParamCmd,
    ) -> Result<(), Error> {
        assert_eq!(
            cmd.numeric_id,
            Self::PARAM_ID_ANALOG_OUTPUT_POLARITY_INVERSION
        );
        param.0 = cmd.value > 0;
        Ok(())
    }
}

/// The type of oversampling filter in analog output.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum WeissAvcAnalogOutputFilterType {
    A,
    B,
}

impl Default for WeissAvcAnalogOutputFilterType {
    fn default() -> Self {
        Self::A
    }
}

impl WeissAvcParamConvert<WeissAvcAnalogOutputFilterType> for WeissMan301Protocol {
    fn build_status_data(cmd: &mut WeissAvcParamCmd) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_ANALOG_OUTPUT_FILTER_TYPE;
        cmd.value = u32::MAX;
        Ok(())
    }

    fn build_control_data(
        param: &WeissAvcAnalogOutputFilterType,
        cmd: &mut WeissAvcParamCmd,
    ) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_ANALOG_OUTPUT_FILTER_TYPE;
        cmd.value = if param.eq(&WeissAvcAnalogOutputFilterType::B) {
            1
        } else {
            0
        };
        Ok(())
    }

    fn parse_response_data(
        param: &mut WeissAvcAnalogOutputFilterType,
        cmd: &WeissAvcParamCmd,
    ) -> Result<(), Error> {
        assert_eq!(cmd.numeric_id, Self::PARAM_ID_ANALOG_OUTPUT_FILTER_TYPE);
        *param = if cmd.value > 0 {
            WeissAvcAnalogOutputFilterType::B
        } else {
            WeissAvcAnalogOutputFilterType::A
        };
        Ok(())
    }
}

/// Mute analog output.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct WeissAvcAnalogOutputMute(pub bool);

impl WeissAvcParamConvert<WeissAvcAnalogOutputMute> for WeissMan301Protocol {
    fn build_status_data(cmd: &mut WeissAvcParamCmd) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_ANALOG_OUTPUT_MUTE;
        cmd.value = u32::MAX;
        Ok(())
    }

    fn build_control_data(
        param: &WeissAvcAnalogOutputMute,
        cmd: &mut WeissAvcParamCmd,
    ) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_ANALOG_OUTPUT_MUTE;
        cmd.value = if param.0 { 0 } else { 1 };
        Ok(())
    }

    fn parse_response_data(
        param: &mut WeissAvcAnalogOutputMute,
        cmd: &WeissAvcParamCmd,
    ) -> Result<(), Error> {
        assert_eq!(cmd.numeric_id, Self::PARAM_ID_ANALOG_OUTPUT_MUTE);
        param.0 = cmd.value == 0;
        Ok(())
    }
}

/// The level of analog output.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum WeissAvcAnalogOutputLevel {
    /// 0 dB.
    Zero,
    /// -10 dB.
    NegativeTen,
    /// -20 dB.
    NegativeTwenty,
    /// -30 dB.
    NegativeThirty,
}

impl Default for WeissAvcAnalogOutputLevel {
    fn default() -> Self {
        Self::Zero
    }
}

impl WeissAvcParamConvert<WeissAvcAnalogOutputLevel> for WeissMan301Protocol {
    fn build_status_data(cmd: &mut WeissAvcParamCmd) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_ANALOG_OUTPUT_LEVEL;
        cmd.value = u32::MAX;
        Ok(())
    }

    fn build_control_data(
        param: &WeissAvcAnalogOutputLevel,
        cmd: &mut WeissAvcParamCmd,
    ) -> Result<(), Error> {
        cmd.numeric_id = Self::PARAM_ID_ANALOG_OUTPUT_LEVEL;
        cmd.value = match param {
            WeissAvcAnalogOutputLevel::NegativeThirty => 3,
            WeissAvcAnalogOutputLevel::NegativeTwenty => 2,
            WeissAvcAnalogOutputLevel::NegativeTen => 1,
            WeissAvcAnalogOutputLevel::Zero => 0,
        };
        Ok(())
    }

    fn parse_response_data(
        param: &mut WeissAvcAnalogOutputLevel,
        cmd: &WeissAvcParamCmd,
    ) -> Result<(), Error> {
        assert_eq!(cmd.numeric_id, Self::PARAM_ID_ANALOG_OUTPUT_LEVEL);
        *param = match cmd.value {
            3 => WeissAvcAnalogOutputLevel::NegativeThirty,
            2 => WeissAvcAnalogOutputLevel::NegativeTwenty,
            1 => WeissAvcAnalogOutputLevel::NegativeTen,
            _ => WeissAvcAnalogOutputLevel::Zero,
        };
        Ok(())
    }
}

/// The operation for parameters in Weiss AV/C protocol.
pub trait WeissAvcParamOperation<T>: WeissAvcParamConvert<T> {
    /// Cache current state of parameter.
    fn cache_param(fcp: &WeissAvc, param: &mut T, timeout_ms: u32) -> Result<(), Error>;
    /// Update the state of parameter.
    fn update_param(fcp: &WeissAvc, param: &mut T, timeout_ms: u32) -> Result<(), Error>;
}

impl<T> WeissAvcParamOperation<T> for WeissMan301Protocol
where
    WeissMan301Protocol: WeissAvcParamConvert<T>,
{
    fn cache_param(fcp: &WeissAvc, param: &mut T, timeout_ms: u32) -> Result<(), Error> {
        let mut cmd = WeissAvcParamCmd::default();
        Self::build_status_data(&mut cmd)?;
        fcp.status(&AvcAddr::Unit, &mut cmd, timeout_ms)?;
        Self::parse_response_data(param, &cmd)?;
        Ok(())
    }

    fn update_param(fcp: &WeissAvc, param: &mut T, timeout_ms: u32) -> Result<(), Error> {
        let mut cmd = WeissAvcParamCmd::default();
        Self::build_control_data(param, &mut cmd)?;
        fcp.control(&AvcAddr::Unit, &mut cmd, timeout_ms)?;
        Self::parse_response_data(param, &cmd)?;
        Ok(())
    }
}

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

    #[test]
    fn weiss_avc_param_command_operands() {
        let operands = [
            0x00, 0x1c, 0x6a, 0x01, 0x7f, 0x80, 0x02, 0x76, 0x54, 0x32, 0x10, 0xfe, 0xdc, 0xba,
            0x98, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
            0xff, 0xff, 0xff,
        ];
        let mut op = WeissAvcParamCmd::default();
        AvcStatus::parse_operands(&mut op, &AvcAddr::Unit, &operands).unwrap();
        assert_eq!(op.op.class_id, 0x01);
        assert_eq!(op.op.sequence_id, 0x7f);
        assert_eq!(op.op.command_id, 0x8002);
        assert_eq!(op.op.arguments, &operands[7..]);
        assert_eq!(op.numeric_id, 0x76543210);
        assert_eq!(op.value, 0xfedcba98);

        let target = AvcStatus::build_operands(&mut op, &AvcAddr::Unit).unwrap();
        assert_eq!(&target[..4], &operands[..4]);
        // The value of sequence_id field is never matched.
        assert_eq!(&target[5..], &operands[5..]);

        let target = AvcControl::build_operands(&mut op, &AvcAddr::Unit).unwrap();
        assert_eq!(&target[..4], &operands[..4]);
        // The value of sequence_id field is never matched.
        assert_eq!(&target[5..], &operands[5..]);

        let mut op = WeissAvcParamCmd::default();
        AvcControl::parse_operands(&mut op, &AvcAddr::Unit, &operands).unwrap();
        assert_eq!(op.op.class_id, 0x01);
        assert_eq!(op.op.sequence_id, 0x7f);
        assert_eq!(op.op.command_id, 0x8002);
        assert_eq!(op.op.arguments, &operands[7..]);
        assert_eq!(op.numeric_id, 0x76543210);
        assert_eq!(op.value, 0xfedcba98);
    }
}