firewire-tascam-protocols 0.2.0

Protocol implementation for TASCAM FireWire series
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
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (c) 2021 Takashi Sakamoto

//! Protocols defined for Tascam for FireWire series with isochronous communication.
//!
//! The module includes protocol implementation defined by Tascam for FireWire series with
//! isochronous communication.

pub mod fw1082;
pub mod fw1804;
pub mod fw1884;

use super::*;

/// Operation to cache whole parameters at once.
pub trait TascamIsochWhollyCachableParamsOperation<T> {
    /// Cache whole parameters.
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut T,
        timeout_ms: u32,
    ) -> Result<(), Error>;
}

/// Operation to update whole parameters at once.
pub trait TascamIsochWhollyUpdatableParamsOperation<T> {
    /// Update whole parameters.
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &T,
        timeout_ms: u32,
    ) -> Result<(), Error>;
}

/// Operation to update the part of parameters.
pub trait TascamIsochPartiallyUpdatableParamsOperation<T> {
    /// Update the part of parameters.
    fn update_partially(
        req: &mut FwReq,
        node: &mut FwNode,
        params: &mut T,
        update: T,
        timeout_ms: u32,
    ) -> Result<(), Error>;
}

/// Operation to parse parameters in the image of hardware state.
pub trait TascamIsochImageParamsOperation<T> {
    /// Parse the image of hardware state.
    fn parse_image(params: &mut T, image: &[u32]);
}

/// Signal source of sampling clock.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ClkSrc {
    /// Internal oscillator.
    Internal,
    /// Word clock signal from BNC input interface.
    Wordclock,
    /// S/PDIF signal from coaxial input interface.
    Spdif,
    /// ADAT signal from optical input interface.
    Adat,
}

impl Default for ClkSrc {
    fn default() -> Self {
        Self::Internal
    }
}

/// Nominal frequency of media clock.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ClkRate {
    /// At 44.1 kHz.
    R44100,
    /// At 48.0 kHz.
    R48000,
    /// At 88.2 kHz.
    R88200,
    /// At 96.0 kHz.
    R96000,
}

impl Default for ClkRate {
    fn default() -> Self {
        Self::R44100
    }
}

/// The parameters of sampling and media clock.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct TascamClockParameters {
    /// The source of sampling clock.
    pub sampling_clock_source: ClkSrc,
    /// The rate of media clock.
    pub media_clock_rate: ClkRate,
}

/// The specification of sampling and media clocks.
pub trait TascamIsochClockSpecification {
    const SAMPLING_CLOCK_SOURCES: &'static [ClkSrc];
}

const CLOCK_STATUS_OFFSET: u64 = 0x0228;

const CLOCK_SOURCES: [(ClkSrc, u8); 4] = [
    (ClkSrc::Internal, 0x01),
    (ClkSrc::Wordclock, 0x02),
    (ClkSrc::Spdif, 0x03),
    (ClkSrc::Adat, 0x04),
];

const CLOCK_RATES: [(ClkRate, u8); 4] = [
    (ClkRate::R44100, 0x01),
    (ClkRate::R48000, 0x02),
    (ClkRate::R88200, 0x03),
    (ClkRate::R96000, 0x04),
];

impl<O> TascamIsochWhollyCachableParamsOperation<TascamClockParameters> for O
where
    O: TascamIsochClockSpecification,
{
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut TascamClockParameters,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut frame = [0; 4];
        read_quadlet(req, node, CLOCK_STATUS_OFFSET, &mut frame, timeout_ms)?;
        let src = CLOCK_SOURCES
            .iter()
            .find_map(|&(src, val)| if val == frame[3] { Some(src) } else { None })
            .ok_or_else(|| {
                let msg = format!("Unexpected value for source of clock: {}", frame[3]);
                Error::new(FileError::Io, &msg)
            })?;
        Self::SAMPLING_CLOCK_SOURCES
            .iter()
            .find_map(|s| if src.eq(s) { Some(src) } else { None })
            .ok_or_else(|| {
                let msg = "Unsupported source of sampling clock";
                Error::new(FileError::Inval, &msg)
            })
            .map(|src| states.sampling_clock_source = src)?;
        CLOCK_RATES
            .iter()
            .find_map(|&(rate, val)| if val == frame[1] { Some(rate) } else { None })
            .ok_or_else(|| {
                let label = format!("Unexpected value for rate of clock: {}", frame[1]);
                Error::new(FileError::Io, &label)
            })
            .map(|rate| states.media_clock_rate = rate)
    }
}

impl<O> TascamIsochWhollyUpdatableParamsOperation<TascamClockParameters> for O
where
    O: TascamIsochClockSpecification,
{
    /// Update whole parameters.
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        params: &TascamClockParameters,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let _ = Self::SAMPLING_CLOCK_SOURCES
            .iter()
            .find(|s| params.sampling_clock_source.eq(s))
            .ok_or_else(|| {
                let msg = "Unsupported source of sampling clock";
                Error::new(FileError::Inval, &msg)
            })?;
        let mut frame = [0; 4];
        frame[3] = CLOCK_SOURCES
            .iter()
            .find_map(|&(s, val)| {
                if params.sampling_clock_source.eq(&s) {
                    Some(val)
                } else {
                    None
                }
            })
            .unwrap();
        frame[2] = CLOCK_RATES
            .iter()
            .find_map(|&(r, val)| {
                if params.media_clock_rate.eq(&r) {
                    Some(val)
                } else {
                    None
                }
            })
            .unwrap();
        write_quadlet(req, node, CLOCK_STATUS_OFFSET, &mut frame, timeout_ms)
    }
}

/// The parameters of threshold to detect input signal. The value is between 1 and 0x7fff. The dB
/// level can be calculated by below formula.
///
/// ```text
/// level = 20 * log10(value / 0x7fff)
/// ```
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct TascamInputDetectionThreshold {
    /// For signal detection itself.
    pub signal: u16,
    /// For over-level detection.
    pub over_level: u16,
}

/// The specification of input detection.
pub trait TascamIsochInputDetectionSpecification {
    /// The minimum value of threshold to detect input signal.
    const INPUT_SIGNAL_THRESHOLD_MIN: u16 = 1;
    /// The maximum value of threshold to detect input signal.
    const INPUT_SIGNAL_THRESHOLD_MAX: u16 = 0x7fff;
}

const INPUT_THRESHOLD_OFFSET: u64 = 0x0230;

impl<O> TascamIsochWhollyCachableParamsOperation<TascamInputDetectionThreshold> for O
where
    O: TascamIsochInputDetectionSpecification,
{
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut TascamInputDetectionThreshold,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut quads = [0; 4];
        read_quadlet(req, node, INPUT_THRESHOLD_OFFSET, &mut quads, timeout_ms).map(|_| {
            let quad = u32::from_be_bytes(quads);
            let val = (quad & 0x0000ffff) as u16;
            states.signal = val.clamp(
                Self::INPUT_SIGNAL_THRESHOLD_MIN,
                Self::INPUT_SIGNAL_THRESHOLD_MAX,
            );

            let val = (quad >> 16) as u16;
            states.over_level = val.clamp(
                Self::INPUT_SIGNAL_THRESHOLD_MIN,
                Self::INPUT_SIGNAL_THRESHOLD_MAX,
            );
        })
    }
}

impl<O> TascamIsochWhollyUpdatableParamsOperation<TascamInputDetectionThreshold> for O
where
    O: TascamIsochInputDetectionSpecification,
{
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        params: &TascamInputDetectionThreshold,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        if params.signal > Self::INPUT_SIGNAL_THRESHOLD_MAX
            || params.signal < Self::INPUT_SIGNAL_THRESHOLD_MIN
        {
            let msg = format!(
                "Argument should be greater than {} and less than {}, but {}",
                Self::INPUT_SIGNAL_THRESHOLD_MIN,
                Self::INPUT_SIGNAL_THRESHOLD_MAX,
                params.signal
            );
            Err(Error::new(FileError::Inval, &msg))?;
        }

        if params.over_level > Self::INPUT_SIGNAL_THRESHOLD_MAX
            || params.over_level < Self::INPUT_SIGNAL_THRESHOLD_MIN
        {
            let msg = format!(
                "Argument should be greater than {} and less than {}, but {}",
                Self::INPUT_SIGNAL_THRESHOLD_MIN,
                Self::INPUT_SIGNAL_THRESHOLD_MAX,
                params.over_level
            );
            Err(Error::new(FileError::Inval, &msg))?;
        }

        let quad = ((params.over_level as u32) << 16) | (params.signal as u32);

        write_quadlet(
            req,
            node,
            INPUT_THRESHOLD_OFFSET,
            &mut quad.to_be_bytes(),
            timeout_ms,
        )
    }
}

const ISOCH_IMAGE_QUADLET_COUNT: usize = 64;

/// Mode of output mixer.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum MixerMode {
    /// From stream input 0/1 only.
    Computer,
    /// From monitor outputs 0/1 only.
    Inputs,
    /// Multiplex signals from stream input 0/1 and monitor output 0/1.
    Both,
}

impl Default for MixerMode {
    fn default() -> Self {
        Self::Computer
    }
}

/// State of meter.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct IsochMeterState {
    /// The value of monitor knob.
    pub monitor: i16,
    /// The value of solo knob.
    pub solo: Option<i16>,
    /// Detected signal levels for inputs.
    pub inputs: Vec<i32>,
    /// Detected signal levels for outputs.
    pub outputs: Vec<i32>,
    /// Detected rate of sampling clock.
    pub rate: Option<ClkRate>,
    /// detected source of sampling clock.
    pub src: Option<ClkSrc>,
    /// Detected signal levels of mixer outputs.
    pub mixer_meters: [i32; 2],
    /// Detected signal levels of monitor outputs.
    pub monitor_meters: [i32; 2],
    /// The mode of mixer.
    pub mixer_mode: MixerMode,
}

/// The specification of metering.
pub trait TascamIsochMeterSpecification: TascamHardwareImageSpecification {
    const INPUT_COUNT: usize;
    const OUTPUT_COUNT: usize;
    const HAS_SOLO: bool;

    const ROTARY_MIN: i16 = 0;
    const ROTARY_MAX: i16 = 1023;
    const ROTARY_STEP: i16 = 2;

    const LEVEL_MIN: i32 = 0;
    const LEVEL_MAX: i32 = 0x7fffff00;
    const LEVEL_STEP: i32 = 0x100;

    fn create_meter_state() -> IsochMeterState {
        IsochMeterState {
            monitor: Default::default(),
            solo: if Self::HAS_SOLO {
                Some(Default::default())
            } else {
                None
            },
            inputs: vec![Default::default(); Self::INPUT_COUNT],
            outputs: vec![Default::default(); Self::OUTPUT_COUNT],
            rate: Default::default(),
            src: Default::default(),
            mixer_meters: Default::default(),
            monitor_meters: Default::default(),
            mixer_mode: Default::default(),
        }
    }
}

impl<O> TascamIsochImageParamsOperation<IsochMeterState> for O
where
    O: TascamIsochMeterSpecification,
{
    fn parse_image(state: &mut IsochMeterState, image: &[u32]) {
        assert_eq!(image.len(), Self::IMAGE_QUADLET_COUNT);

        let monitor = (image[5] & 0x0000ffff) as i16;
        if (state.monitor - monitor).abs() > Self::ROTARY_STEP {
            state.monitor = monitor;
        }

        if let Some(solo) = &mut state.solo {
            let val = ((image[4] >> 16) & 0x0000ffff) as i16;
            if (*solo - val).abs() > Self::ROTARY_STEP {
                *solo = val;
            }
        }

        state
            .inputs
            .iter_mut()
            .take(Self::INPUT_COUNT)
            .enumerate()
            .for_each(|(i, input)| {
                let pos = if Self::INPUT_COUNT == 10 && i >= 8 {
                    i + 16
                } else {
                    i
                } + 16;
                *input = image[pos] as i32;
            });

        state
            .outputs
            .iter_mut()
            .take(Self::OUTPUT_COUNT)
            .enumerate()
            .for_each(|(i, output)| {
                let pos = if Self::OUTPUT_COUNT == 4 && i >= 2 {
                    i + 16
                } else {
                    i
                } + 34;
                *output = image[pos] as i32;
            });

        let bits = (image[52] & 0x0000000f) as u8;
        state.src = match bits {
            0x04 => Some(ClkSrc::Adat),
            0x03 => Some(ClkSrc::Spdif),
            0x02 => Some(ClkSrc::Wordclock),
            0x01 => Some(ClkSrc::Internal),
            _ => None,
        };

        let bits = ((image[52] >> 8) & 0x000000ff) as u8;
        state.rate = match bits {
            0x82 => Some(ClkRate::R96000),
            0x81 => Some(ClkRate::R88200),
            0x02 => Some(ClkRate::R48000),
            0x01 => Some(ClkRate::R44100),
            _ => None,
        };

        state
            .mixer_meters
            .iter_mut()
            .enumerate()
            .for_each(|(i, m)| {
                *m = image[i + 54] as i32;
            });

        state
            .monitor_meters
            .iter_mut()
            .enumerate()
            .for_each(|(i, m)| {
                *m = image[i + 57] as i32;
            });

        if image[59] > 0 && image[59] < 4 {
            state.mixer_mode = match image[59] {
                2 => MixerMode::Inputs,
                1 => MixerMode::Computer,
                _ => MixerMode::Both,
            };
        }
    }
}

fn serialize_config_flag<T: Copy + Eq>(
    option: &T,
    flags: &[(T, u32, u32)],
    val: &mut u32,
) -> Result<(), Error> {
    let mask = flags.iter().fold(0, |mask, (_, _, flag)| mask | flag);
    *val &= !mask;
    let (_, _, flag) = flags.iter().find(|(o, _, _)| option.eq(o)).unwrap();
    *val |= flag;
    Ok(())
}

fn deserialize_config_flag<T: Copy + Eq>(
    option: &mut T,
    flags: &[(T, u32, u32)],
    val: u32,
) -> Result<(), Error> {
    let mask = flags.iter().fold(0, |mask, (_, flag, _)| mask | flag);
    flags
        .iter()
        .find(|&(_, flag, _)| val & mask == *flag)
        .ok_or_else(|| {
            let msg = format!("No flag detected: val: 0x{:08x} mask: 0x{:08x}", val, mask);
            Error::new(FileError::Nxio, &msg)
        })
        .map(|&(opt, _, _)| *option = opt)
}

fn read_config(
    req: &mut FwReq,
    node: &mut FwNode,
    config: &mut u32,
    timeout_ms: u32,
) -> Result<(), Error> {
    let mut quads = [0; 4];
    read_quadlet(req, node, CONFIG_FLAG_OFFSET, &mut quads, timeout_ms).map(|_| {
        *config = u32::from_be_bytes(quads);
    })
}

fn write_config(
    req: &mut FwReq,
    node: &mut FwNode,
    config: u32,
    timeout_ms: u32,
) -> Result<(), Error> {
    write_quadlet(
        req,
        node,
        CONFIG_FLAG_OFFSET,
        &mut config.to_be_bytes(),
        timeout_ms,
    )
}

/// Source of output coaxial interface.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CoaxialOutputSource {
    /// A pair in stream inputs.
    StreamInputPair,
    /// Mirror of analog output 0 and 1.
    AnalogOutputPair0,
}

impl Default for CoaxialOutputSource {
    fn default() -> Self {
        Self::StreamInputPair
    }
}

/// The specification of coaxial output interface.
pub trait TascamIsochCoaxialOutputSpecification {}

const COAXIAL_OUTPUT_SOURCES: [(CoaxialOutputSource, u32, u32); 2] = [
    (CoaxialOutputSource::StreamInputPair, 0x00000002, 0x00020000),
    (
        CoaxialOutputSource::AnalogOutputPair0,
        0x00000000,
        0x00000200,
    ),
];

impl<O> TascamIsochWhollyCachableParamsOperation<CoaxialOutputSource> for O
where
    O: TascamIsochCoaxialOutputSpecification,
{
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut CoaxialOutputSource,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut config = 0;
        read_config(req, node, &mut config, timeout_ms)?;
        deserialize_config_flag(states, &COAXIAL_OUTPUT_SOURCES, config)
    }
}

impl<O> TascamIsochWhollyUpdatableParamsOperation<CoaxialOutputSource> for O
where
    O: TascamIsochCoaxialOutputSpecification,
{
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &CoaxialOutputSource,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut config = 0;
        serialize_config_flag(states, &COAXIAL_OUTPUT_SOURCES, &mut config)?;
        write_config(req, node, config, timeout_ms)
    }
}

const CONFIG_FLAG_OFFSET: u64 = 0x022c;

/// Source of S/PDIF input.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SpdifCaptureSource {
    /// To coaxial interface.
    Coaxial,
    /// To optical interface.
    Optical,
}

impl Default for SpdifCaptureSource {
    fn default() -> Self {
        Self::Coaxial
    }
}

/// Source of optical output.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum OpticalOutputSource {
    /// 4 pairs in stream inputs.
    StreamInputPairs,
    /// Mirror of coaxial output 0 and 1.
    CoaxialOutputPair0,
    /// Analog input 0 and 1.
    AnalogInputPair0,
    /// Mirror of analog output 0, 1, 2, 3, 4, 5, 6, 7, and 8.
    AnalogOutputPairs,
}

impl Default for OpticalOutputSource {
    fn default() -> Self {
        Self::StreamInputPairs
    }
}

/// The parameters of digital input and output interfaces.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct TascamOpticalIfaceParameters {
    /// The input interface from which the S/PDIF signal is captured.
    pub capture_source: SpdifCaptureSource,
    /// The source signal of optical output interface.
    pub output_source: OpticalOutputSource,
}

/// The specification of digital interfaces.
pub trait TascamIsochOpticalIfaceSpecification {
    const OPTICAL_OUTPUT_SOURCES: &'static [(OpticalOutputSource, u32, u32)];
}

const SPDIF_CAPTURE_SOURCES: &[(SpdifCaptureSource, u32, u32)] = &[
    (SpdifCaptureSource::Coaxial, 0x00000000, 0x00010000),
    (SpdifCaptureSource::Optical, 0x00000001, 0x00000100),
];

impl<O> TascamIsochWhollyCachableParamsOperation<TascamOpticalIfaceParameters> for O
where
    O: TascamIsochOpticalIfaceSpecification,
{
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut TascamOpticalIfaceParameters,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut config = 0;
        read_config(req, node, &mut config, timeout_ms)?;
        deserialize_config_flag(&mut states.capture_source, &SPDIF_CAPTURE_SOURCES, config)?;
        deserialize_config_flag(
            &mut states.output_source,
            &Self::OPTICAL_OUTPUT_SOURCES,
            config,
        )?;
        Ok(())
    }
}

impl<O> TascamIsochWhollyUpdatableParamsOperation<TascamOpticalIfaceParameters> for O
where
    O: TascamIsochOpticalIfaceSpecification,
{
    /// Update whole parameters.
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &TascamOpticalIfaceParameters,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut config = 0;
        serialize_config_flag(&states.capture_source, &SPDIF_CAPTURE_SOURCES, &mut config)?;
        serialize_config_flag(
            &states.output_source,
            &Self::OPTICAL_OUTPUT_SOURCES,
            &mut config,
        )?;
        write_config(req, node, config, timeout_ms)
    }
}

/// State of console.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct IsochConsoleState {
    /// Whether to enable host mode.
    pub host_mode: bool,

    /// Whether to assign master fader to analog output 1/2.
    pub master_fader_assign: bool,
}

/// The specification of console.
pub trait TascamIsochConsoleSpecification: TascamHardwareImageSpecification {}

const MASTER_FADER_ASSIGNS: [(bool, u32, u32); 2] = [
    (false, 0x00000040, 0x00400000),
    (true, 0x00000000, 0x00004000),
];

impl<O> TascamIsochWhollyCachableParamsOperation<IsochConsoleState> for O
where
    O: TascamIsochConsoleSpecification,
{
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut IsochConsoleState,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut config = 0;
        read_config(req, node, &mut config, timeout_ms)?;
        deserialize_config_flag(
            &mut states.master_fader_assign,
            &MASTER_FADER_ASSIGNS,
            config,
        )
    }
}

impl<O> TascamIsochWhollyUpdatableParamsOperation<IsochConsoleState> for O
where
    O: TascamIsochConsoleSpecification,
{
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &IsochConsoleState,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut config = 0;
        serialize_config_flag(
            &states.master_fader_assign,
            &MASTER_FADER_ASSIGNS,
            &mut config,
        )?;
        write_config(req, node, config, timeout_ms)
    }
}

impl<O> TascamIsochImageParamsOperation<IsochConsoleState> for O
where
    O: TascamIsochConsoleSpecification + TascamHardwareImageSpecification,
{
    fn parse_image(params: &mut IsochConsoleState, image: &[u32]) {
        assert_eq!(image.len(), Self::IMAGE_QUADLET_COUNT);
        params.host_mode = (image[5] & 0xff000000) != 0xff000000;
    }
}

/// The parameters of input.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct IsochRackInputParameters {
    /// Gain of signal to stereo monitor. The value is between 0 and 0x7fff.
    pub gains: [i16; 18],
    /// L/R balance to stereo monitor. The value is between 0 and 255.
    pub balances: [u8; 18],
    /// Whether to mute.
    pub mutes: [bool; 18],
}

const RACK_STATE_SIZE: usize = 72;

fn serialize_input_params(params: &IsochRackInputParameters, raw: &mut [u8; RACK_STATE_SIZE]) {
    (0..18).for_each(|i| {
        let val = ((i as u32) << 24) | ((params.mutes[i] as u32) << 16) | (params.gains[i] as u32);
        let pos = i * 4;
        raw[pos..(pos + 4)].copy_from_slice(&val.to_be_bytes());
    });
}

#[cfg(test)]
fn deserialize_input_params(params: &mut IsochRackInputParameters, raw: &[u8; RACK_STATE_SIZE]) {
    (0..RACK_STATE_SIZE).step_by(4).for_each(|pos| {
        let mut quad = [0; 4];
        quad.copy_from_slice(&raw[pos..(pos + 4)]);
        let val = u32::from_be_bytes(quad);
        let i = (val >> 24) as usize;
        let muted = (val & 0x00ff0000) > 0;
        let gain = (val & 0x0000ffff) as i16;
        if i < params.gains.len() {
            params.gains[i] = gain;
            params.mutes[i] = muted;
        }
    });
}

/// The specification of rack input.
pub trait TascamIsochRackInputSpecification {
    /// The number of input channels.
    const CHANNEL_COUNT: usize = 18;

    /// The minimum value of gain.
    const INPUT_GAIN_MIN: i16 = 0;
    /// The maximum value of gain.
    const INPUT_GAIN_MAX: i16 = 0x7fff;
    /// The step value of gain.
    const INPUT_GAIN_STEP: i16 = 0x100;

    /// The minimum value of L/R balance.
    const INPUT_BALANCE_MIN: u8 = 0;
    /// The maximum value of L/R balance.
    const INPUT_BALANCE_MAX: u8 = 255;
    /// The step value of L/R balance.
    const INPUT_BALANCE_STEP: u8 = 1;

    fn create_input_parameters() -> IsochRackInputParameters {
        let mut params = IsochRackInputParameters::default();
        params.gains.fill(Self::INPUT_GAIN_MAX);
        params
            .balances
            .iter_mut()
            .enumerate()
            .for_each(|(i, balance)| {
                *balance = if i % 2 > 0 { u8::MAX } else { u8::MIN };
            });
        params.mutes.fill(false);
        params
    }
}

const INPUT_OFFSET: u64 = 0x0408;

fn write_input_quadlet(
    req: &mut FwReq,
    node: &mut FwNode,
    quad: &mut [u8],
    timeout_ms: u32,
) -> Result<(), Error> {
    assert_eq!(quad.len(), 4);

    write_quadlet(req, node, INPUT_OFFSET, quad, timeout_ms)
}

impl<O> TascamIsochWhollyUpdatableParamsOperation<IsochRackInputParameters> for O
where
    O: TascamIsochRackInputSpecification,
{
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &IsochRackInputParameters,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut raw = [0; RACK_STATE_SIZE];
        serialize_input_params(states, &mut raw);
        (0..RACK_STATE_SIZE).step_by(4).try_for_each(|pos| {
            write_input_quadlet(req, node, &mut raw[pos..(pos + 4)], timeout_ms)
        })
    }
}

impl<O> TascamIsochPartiallyUpdatableParamsOperation<IsochRackInputParameters> for O
where
    O: TascamIsochRackInputSpecification,
{
    fn update_partially(
        req: &mut FwReq,
        node: &mut FwNode,
        params: &mut IsochRackInputParameters,
        update: IsochRackInputParameters,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut old = [0; RACK_STATE_SIZE];
        serialize_input_params(params, &mut old);

        let mut new = [0; RACK_STATE_SIZE];
        serialize_input_params(&update, &mut new);

        (0..RACK_STATE_SIZE)
            .step_by(4)
            .try_for_each(|pos| {
                if old[pos..(pos + 4)] != new[pos..(pos + 4)] {
                    write_input_quadlet(req, node, &mut new[pos..(pos + 4)], timeout_ms)
                } else {
                    Ok(())
                }
            })
            .map(|_| *params = update)
    }
}

/// State of surface in isoch models.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct TascamSurfaceIsochState {
    shifted: bool,
    shifted_items: Vec<bool>,
    bank: u16,
    enabled_leds: LedState,
}

/// The trait to express specification of LEDS for isochronous models.
pub trait TascamSurfaceLedIsochSpecification {
    const BANK_LEDS: [&'static [u16]; 4];
}

impl<O> TascamSurfaceLedOperation<TascamSurfaceIsochState> for O
where
    O: TascamSurfaceLedIsochSpecification,
{
    fn operate_leds(
        state: &mut TascamSurfaceIsochState,
        machine_value: &(MachineItem, ItemValue),
        req: &mut FwReq,
        node: &mut FwNode,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        if let (MachineItem::Bank, ItemValue::U16(value)) = machine_value {
            Self::BANK_LEDS
                .iter()
                .enumerate()
                .try_for_each(|(i, positions)| {
                    let enable = *value == i as u16;
                    operate_led_cached(
                        &mut state.enabled_leds,
                        req,
                        node,
                        positions[0],
                        enable,
                        timeout_ms,
                    )
                })?;
        }

        Ok(())
    }

    fn clear_leds(
        state: &mut TascamSurfaceIsochState,
        req: &mut FwReq,
        node: &mut FwNode,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        clear_leds(&mut state.enabled_leds, req, node, timeout_ms)
    }
}

/// The trait to express state of surface specific to isochronous models.
pub trait TascamSurfaceStateIsochSpecification {
    const SHIFT_ITEM: SurfaceBoolValue;
    const SHIFTED_ITEMS: &'static [(SurfaceBoolValue, [MachineItem; 2])];
    const BANK_CURSORS: [SurfaceBoolValue; 2];
}

impl<O> TascamSurfaceStateOperation<TascamSurfaceIsochState> for O
where
    O: TascamSurfaceStateIsochSpecification,
{
    fn init(state: &mut TascamSurfaceIsochState) {
        state.shifted = false;
        state.shifted_items = vec![false; Self::SHIFTED_ITEMS.len()];
        state.bank = 0;
    }

    fn peek(
        state: &TascamSurfaceIsochState,
        _: &[u32],
        index: u32,
        before: u32,
        after: u32,
    ) -> Vec<(MachineItem, ItemValue)> {
        let mut machine_values = Vec::new();

        let shifted = if detect_bool_action(&Self::SHIFT_ITEM, index, before, after) {
            let shifted = detect_bool_value(&Self::SHIFT_ITEM, before);
            machine_values.push((MachineItem::Shift, ItemValue::Bool(shifted)));
            shifted
        } else {
            state.shifted
        };

        if shifted != state.shifted {
            let prev_idx = state.shifted as usize;
            let curr_idx = shifted as usize;

            Self::SHIFTED_ITEMS
                .iter()
                .zip(&state.shifted_items)
                .filter(|(_, &s)| s)
                .for_each(|((_, pairs), _)| {
                    machine_values.push((pairs[prev_idx], ItemValue::Bool(false)));
                    machine_values.push((pairs[curr_idx], ItemValue::Bool(true)));
                });
        }

        Self::SHIFTED_ITEMS
            .iter()
            .filter(|(bool_val, _)| detect_bool_action(bool_val, index, before, after))
            .for_each(|(bool_val, pairs)| {
                let value = detect_bool_value(bool_val, before);
                machine_values.push((pairs[shifted as usize], ItemValue::Bool(value)));
            });

        Self::BANK_CURSORS
            .iter()
            .enumerate()
            .filter(|(_, bool_val)| detect_bool_action(bool_val, index, before, after))
            .for_each(|(idx, bool_val)| {
                let is_right = idx > 0;
                let push_event = detect_bool_value(bool_val, before);
                if push_event {
                    let mut bank = state.bank;

                    if !is_right && bank > BANK_MIN {
                        bank -= 1;
                    } else if is_right && bank < BANK_MAX {
                        bank += 1;
                    }

                    if bank != state.bank {
                        machine_values.push((MachineItem::Bank, ItemValue::U16(bank)));
                    }
                }
            });

        machine_values
    }

    fn ack(state: &mut TascamSurfaceIsochState, machine_value: &(MachineItem, ItemValue)) {
        match machine_value {
            &(MachineItem::Shift, ItemValue::Bool(value)) => state.shifted = value,
            &(MachineItem::Bank, ItemValue::U16(value)) => state.bank = value,
            _ => (),
        }
    }
}

/// The trait for operation specific to isoch models.
trait SurfaceImageIsochOperation {
    const SHIFT_ITEM: SurfaceBoolValue;
    const SHIFTED_ITEMS: &'static [(SurfaceBoolValue, [MachineItem; 2])];
    const BANK_CURSORS: [SurfaceBoolValue; 2];

    fn initialize_surface_isoch_state(state: &mut TascamSurfaceIsochState) {
        state.shifted = false;
        state.shifted_items = vec![false; Self::SHIFTED_ITEMS.len()];
        state.bank = 0;
    }

    fn decode_surface_image_isoch(
        machine_values: &mut Vec<(MachineItem, ItemValue)>,
        state: &TascamSurfaceIsochState,
        index: u32,
        before: u32,
        after: u32,
    ) {
        let shifted = if detect_bool_action(&Self::SHIFT_ITEM, index, before, after) {
            let shifted = detect_bool_value(&Self::SHIFT_ITEM, before);
            machine_values.push((MachineItem::Shift, ItemValue::Bool(shifted)));
            shifted
        } else {
            state.shifted
        };

        if shifted != state.shifted {
            let prev_idx = state.shifted as usize;
            let curr_idx = shifted as usize;

            Self::SHIFTED_ITEMS
                .iter()
                .zip(&state.shifted_items)
                .filter(|(_, &s)| s)
                .for_each(|((_, pairs), _)| {
                    machine_values.push((pairs[prev_idx], ItemValue::Bool(false)));
                    machine_values.push((pairs[curr_idx], ItemValue::Bool(true)));
                });
        }

        Self::SHIFTED_ITEMS
            .iter()
            .filter(|(bool_val, _)| detect_bool_action(bool_val, index, before, after))
            .for_each(|(bool_val, pairs)| {
                let value = detect_bool_value(bool_val, before);
                machine_values.push((pairs[shifted as usize], ItemValue::Bool(value)));
            });

        Self::BANK_CURSORS
            .iter()
            .enumerate()
            .filter(|(_, bool_val)| detect_bool_action(bool_val, index, before, after))
            .for_each(|(idx, bool_val)| {
                let is_right = idx > 0;
                let push_event = detect_bool_value(bool_val, before);
                if push_event {
                    let mut bank = state.bank;

                    if !is_right && bank > BANK_MIN {
                        bank -= 1;
                    } else if is_right && bank < BANK_MAX {
                        bank += 1;
                    }

                    if bank != state.bank {
                        machine_values.push((MachineItem::Bank, ItemValue::U16(bank)));
                    }
                }
            });
    }

    fn feedback_to_surface_isoch(
        state: &mut TascamSurfaceIsochState,
        machine_value: &(MachineItem, ItemValue),
    ) {
        match machine_value {
            &(MachineItem::Shift, ItemValue::Bool(value)) => state.shifted = value,
            &(MachineItem::Bank, ItemValue::U16(value)) => state.bank = value,
            _ => (),
        }
    }
}

/// The trait for operation of bank LEDs in surface.
trait SurfaceBankLedOperation {
    const BANK_LEDS: [&'static [u16]; 4];

    fn operate_bank_leds(
        state: &mut LedState,
        req: &mut FwReq,
        node: &mut FwNode,
        bank: u16,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        Self::BANK_LEDS
            .iter()
            .enumerate()
            .try_for_each(|(i, positions)| {
                let enable = bank == i as u16;
                let pos = positions[0];
                operate_led_cached(state, req, node, pos, enable, timeout_ms)
            })
    }
}

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

    #[test]
    fn config_flag_serdes() {
        #[derive(Debug, Copy, Clone, PartialEq, Eq)]
        enum Flag {
            A,
            B,
            C,
            N,
        }
        const TABLE: &[(Flag, u32, u32)] = &[
            (Flag::A, 0x00000001, 0x10000000),
            (Flag::B, 0x00000020, 0x20000000),
            (Flag::C, 0x00000004, 0x00040000),
        ];

        let mut param = Flag::N;
        deserialize_config_flag(&mut param, &TABLE, 0x00000001).unwrap();
        assert_eq!(param, Flag::A);

        deserialize_config_flag(&mut param, &TABLE, 0x00000000).unwrap_err();

        let mut val = 0;
        serialize_config_flag(&param, &TABLE, &mut val).unwrap();
        assert_eq!(val, 0x10000000);
    }

    #[test]
    fn rack_input_params_serdes() {
        let orig = IsochRackInputParameters::default();
        let mut raw = [0; RACK_STATE_SIZE];
        serialize_input_params(&orig, &mut raw);

        let mut target = IsochRackInputParameters::default();
        deserialize_input_params(&mut target, &raw);

        assert_eq!(target, orig);
    }
}