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

#![doc = include_str!("../README.md")]

//! ## Process events for control surface
//!
//! Tascam FW-1884, FW-1082, and FE-8 have control surface, in which the hardware make no
//! superficial change to the surface according to user operation. Instead, the surface notifies
//! the operation to system.
//!
//! In FE-8, the image consists of 32 quadlets. Asynchronous notification is simply sent for one
//! of quadlet to which user operation effects.
//!
//! In FW-1884 and FW-1082, the state of surface is expressed as an image which consists of 64
//! quadlets. One of the quadlets is multiplexed to data block in isochronous packet as well as
//! PCM frame in order.
//!
//! FW-1884 and FW-1082 has shift button to divert some buttons. Furthermore, FW-1082 has some
//! rotaries and buttons which change their role according to encoder mode. The module includes
//! two stuffs to abstract the above design; surface state and machine state. The former is used
//! used to parse surface imageg and detect event and operate LED. The latter is used to monitor
//! current state of each surface item by handling the event, and generate normalized events. It's
//! task of runtime implementation to prepare converter between the machine event and application
//! specific message such as ALSA Sequencer and Open Sound Control.
//!
//! The relationship between the constrol surface, surface state, machine state, and message
//! converter is illustrated in below diagram:
//!
//! ```text
//!                       ++====================================================++
//!                       ||                  Service runtime                   ||
//!                       ||                                                    ||
//! ++==========++  surface image    +---------+   machine event   +---------+  ||
//! ||          || ----------------> |         | ----------------> |         |  ||
//! || surface  ||        ||         | surface |                   | machine |  ||
//! || hardware ||  LED operation    |  state  |   machine event   |  state  |  ||
//! ||          || <---------------- |         | <---------------- |         |  ||
//! ++==========++        ||         +---------+                   +---------+  ||
//!                       ||                                         ^    |     ||
//!                       ||                                      machine event ||
//!                       ||                                         |    v     ||
//!                       ||                                       +---------+  ||
//!                       ||                                       |         |  ||
//!                       ||                                       | message |  ||
//!                       ||                                       |converter|  ||
//!                       ||                                       |         |  ||
//!                       ||                                       +---------+  ||
//!                       ||                                         ^    |     ||
//!                       ||                                   specific message ||
//!                       ||                                         |    |     ||
//!                       ++=========================================|====|=====++
//!                                                                  |    |
//!                                     Inter process communication  |    |
//!                                     (ALSA Sequencer, OSC, etc.)  |    v
//!                                                             ++=============++
//!                                                             || application ||
//!                                                             ++=============++
//! ```

pub mod asynch;
pub mod isoch;

pub mod config_rom;

use {
    glib::{Error, FileError},
    hinawa::{prelude::*, *},
};

const BASE_OFFSET: u64 = 0xffff00000000;
const HW_INFO_REGISTER_OFFSET: u64 = 0x00;
const HW_INFO_FPGA_OFFSET: u64 = 0x04;
const HW_INFO_ARM_OFFSET: u64 = 0x08;
const HW_INFO_HW_OFFSET: u64 = 0x0c;
const LED_OFFSET: u64 = 0x0404;

fn read_quadlet(
    req: &mut FwReq,
    node: &mut FwNode,
    offset: u64,
    frames: &mut [u8],
    timeout_ms: u32,
) -> Result<(), Error> {
    req.transaction_sync(
        node,
        FwTcode::ReadQuadletRequest,
        BASE_OFFSET + offset,
        4,
        frames,
        timeout_ms,
    )
}

fn write_quadlet(
    req: &mut FwReq,
    node: &FwNode,
    offset: u64,
    frames: &mut [u8],
    timeout_ms: u32,
) -> Result<(), Error> {
    req.transaction_sync(
        node,
        FwTcode::WriteQuadletRequest,
        BASE_OFFSET + offset,
        4,
        frames,
        timeout_ms,
    )
}

/// Information of hardware.
#[derive(Debug, Default, Copy, Clone)]
pub struct HardwareInformation {
    pub register: u32,
    pub fpga: u32,
    pub arm: u32,
    pub hardware: u32,
}

/// The protocol implementaion commonly available to Tascam FireWire models.
#[derive(Debug, Default)]
pub struct HardwareInformationProtocol;

/// The trait for oepration of hardware information.
impl HardwareInformationProtocol {
    pub fn read_hardware_information(
        req: &mut FwReq,
        node: &mut FwNode,
        info: &mut HardwareInformation,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let mut quads = [0; 4];
        read_quadlet(req, node, HW_INFO_REGISTER_OFFSET, &mut quads, timeout_ms)
            .map(|_| info.register = u32::from_be_bytes(quads))?;
        read_quadlet(req, node, HW_INFO_FPGA_OFFSET, &mut quads, timeout_ms)
            .map(|_| info.fpga = u32::from_be_bytes(quads))?;
        read_quadlet(req, node, HW_INFO_ARM_OFFSET, &mut quads, timeout_ms)
            .map(|_| info.arm = u32::from_be_bytes(quads))?;
        read_quadlet(req, node, HW_INFO_HW_OFFSET, &mut quads, timeout_ms)
            .map(|_| info.hardware = u32::from_be_bytes(quads))?;
        Ok(())
    }
}

/// The specification of hardware image.
pub trait TascamHardwareImageSpecification {
    const IMAGE_QUADLET_COUNT: usize;

    fn create_hardware_image() -> Vec<u32> {
        vec![0; Self::IMAGE_QUADLET_COUNT]
    }
}

/// Items of surface.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum MachineItem {
    // Channel section.
    Master,
    Ol(usize),
    Rec(usize),
    Signal(usize),
    Rotary(usize),
    Select(usize),
    Solo(usize),
    Mute(usize),
    Input(usize),
    Func(usize),
    Pfl,

    // Global section.
    Read,
    Wrt,
    Tch,
    Latch,
    Wheel,
    Shuttle,
    Computer,
    Clock,
    Up,
    Left,
    Down,
    Right,
    NudgeLeft,
    NudgeRight,
    LocateLeft,
    LocateRight,
    Set,
    In,
    Out,

    // Encoder section.
    Flip,
    Pan, // has bool value in FW-1884, has u16 value in FW-1082.
    Aux(usize),
    EncoderMode, // FW-1082 only.

    // Equalizer section.
    High,
    HighMid,
    LowMid,
    Low,
    Recall,
    Gain,
    Freq,
    Q,

    // Bank section.
    Bank,

    // Transport section.
    Rew,
    Fwd,
    Stop,
    Play,
    Record,

    // Shortcut section.
    Panel,
    Save,
    Revert,
    AllSafe,
    ClrSolo,
    Markers,
    Loop,
    Cut,
    Del,
    Copy,
    Paste,
    Alt,
    Cmd,
    Undo,
    Shift,
    Ctrl,
}

impl Default for MachineItem {
    fn default() -> Self {
        Self::Master
    }
}

impl std::fmt::Display for MachineItem {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::Master => write!(f, "master"),
            Self::Ol(ch) => write!(f, "ol {}", ch),
            Self::Rec(ch) => write!(f, "rec {}", ch),
            Self::Signal(ch) => write!(f, "signal {}", ch),
            Self::Rotary(ch) => write!(f, "rotary {}", ch),
            Self::Select(ch) => write!(f, "select {}", ch),
            Self::Solo(ch) => write!(f, "solo {}", ch),
            Self::Mute(ch) => write!(f, "mute {}", ch),
            Self::Input(ch) => write!(f, "input {}", ch),
            Self::Func(ch) => write!(f, "func {}", ch),
            Self::Pfl => write!(f, "pfl"),
            Self::Read => write!(f, "read"),
            Self::Wrt => write!(f, "wrt"),
            Self::Tch => write!(f, "tch"),
            Self::Latch => write!(f, "latch"),
            Self::Wheel => write!(f, "wheel"),
            Self::Shuttle => write!(f, "Shuttle"),
            Self::Computer => write!(f, "computer"),
            Self::Clock => write!(f, "clock"),
            Self::Up => write!(f, "up"),
            Self::Left => write!(f, "left"),
            Self::Down => write!(f, "down"),
            Self::Right => write!(f, "right"),
            Self::NudgeLeft => write!(f, "nudge left"),
            Self::NudgeRight => write!(f, "nudge right"),
            Self::LocateLeft => write!(f, "locate left"),
            Self::LocateRight => write!(f, "locate right"),
            Self::Set => write!(f, "set"),
            Self::In => write!(f, "in"),
            Self::Out => write!(f, "out"),
            Self::Flip => write!(f, "flip"),
            Self::Pan => write!(f, "pan"),
            Self::Aux(ch) => write!(f, "aux {}", ch),
            Self::EncoderMode => write!(f, "encoder model"),
            Self::High => write!(f, "high"),
            Self::HighMid => write!(f, "high-mid"),
            Self::LowMid => write!(f, "low-mid"),
            Self::Low => write!(f, "low"),
            Self::Recall => write!(f, "recall"),
            Self::Gain => write!(f, "gain"),
            Self::Freq => write!(f, "freq"),
            Self::Q => write!(f, "q"),
            Self::Bank => write!(f, "bank"),
            Self::Rew => write!(f, "rew"),
            Self::Fwd => write!(f, "fwd"),
            Self::Stop => write!(f, "stop"),
            Self::Play => write!(f, "play"),
            Self::Record => write!(f, "record"),
            Self::Panel => write!(f, "panel"),
            Self::Save => write!(f, "save"),
            Self::Revert => write!(f, "revert"),
            Self::AllSafe => write!(f, "all safe"),
            Self::ClrSolo => write!(f, "clr solo"),
            Self::Markers => write!(f, "markers"),
            Self::Loop => write!(f, "loop"),
            Self::Cut => write!(f, "cut"),
            Self::Del => write!(f, "del"),
            Self::Copy => write!(f, "copy"),
            Self::Paste => write!(f, "paste"),
            Self::Alt => write!(f, "alt"),
            Self::Cmd => write!(f, "cmd"),
            Self::Undo => write!(f, "undo"),
            Self::Shift => write!(f, "shift"),
            Self::Ctrl => write!(f, "ctrl"),
        }
    }
}

/// The state machine of control surface.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct MachineState {
    /// The boolean value of each item.
    bool_items: Vec<bool>,
    /// The u16 value of each item.
    u16_items: Vec<u16>,
    /// Between 0-3.
    bank: u16,
    /// One of Rew, Fwd, Stop, Play, and Record.
    transport: MachineItem,
}

/// The event of state machine.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ItemValue {
    Bool(bool),
    U16(u16),
}

const BANK_MIN: u16 = 0;
const BANK_MAX: u16 = 3;

/// The trait for operation of state machine.
pub trait MachineStateOperation {
    const BOOL_ITEMS: &'static [MachineItem];
    const U16_ITEMS: &'static [MachineItem];
    const HAS_TRANSPORT: bool;
    const HAS_BANK: bool;

    const BANK_MIN: u16 = BANK_MIN;
    const BANK_MAX: u16 = BANK_MAX;

    const U16_ITEM_MIN: u16 = 0;
    const U16_ITEM_MAX: u16 = 0xffffu16;

    const TRANSPORT_ITEMS: [MachineItem; 5] = [
        MachineItem::Rew,
        MachineItem::Fwd,
        MachineItem::Stop,
        MachineItem::Play,
        MachineItem::Record,
    ];

    const EQ_BAND_ITEMS: [MachineItem; 4] = [
        MachineItem::High,
        MachineItem::HighMid,
        MachineItem::LowMid,
        MachineItem::Low,
    ];

    fn create_machine_state() -> MachineState {
        MachineState {
            bool_items: vec![false; Self::BOOL_ITEMS.len()],
            u16_items: vec![0; Self::U16_ITEMS.len()],
            bank: 0,
            transport: MachineItem::Stop,
        }
    }

    fn get_machine_current_values(state: &MachineState) -> Vec<(MachineItem, ItemValue)> {
        let mut machine_values = Vec::new();

        Self::BOOL_ITEMS
            .iter()
            .zip(&state.bool_items)
            .for_each(|(&item, &value)| machine_values.push((item, ItemValue::Bool(value))));

        Self::U16_ITEMS
            .iter()
            .zip(&state.u16_items)
            .for_each(|(&item, &value)| machine_values.push((item, ItemValue::U16(value))));

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

        if Self::HAS_TRANSPORT {
            Self::TRANSPORT_ITEMS.iter().for_each(|&item| {
                machine_values.push((item, ItemValue::Bool(item.eq(&state.transport))));
            });
        }

        machine_values
    }

    fn change_machine_value(
        state: &mut MachineState,
        input: &(MachineItem, ItemValue),
    ) -> Vec<(MachineItem, ItemValue)> {
        let mut outputs = Vec::new();

        if let ItemValue::Bool(value) = input.1 {
            // Normal items.
            let _ = Self::BOOL_ITEMS
                .iter()
                .zip(&mut state.bool_items)
                .find(|(i, v)| input.0.eq(i) && !value.eq(v))
                .map(|(_, v)| {
                    *v = value;
                    outputs.push((input.0, ItemValue::Bool(*v)));
                });

            // One of transport items should be enabled.
            if Self::HAS_TRANSPORT
                && Self::TRANSPORT_ITEMS
                    .iter()
                    .find(|i| input.0.eq(i))
                    .is_some()
            {
                if input.0 != state.transport {
                    outputs.push((state.transport, ItemValue::Bool(false)));
                    outputs.push((input.0, ItemValue::Bool(true)));
                    state.transport = input.0;
                }
            }

            // None of, or one of equalizer band items should be enabled.
            if Self::EQ_BAND_ITEMS.iter().find(|i| input.0.eq(i)).is_some() {
                if value {
                    Self::BOOL_ITEMS
                        .iter()
                        .zip(&mut state.bool_items)
                        .filter(|(i, v)| {
                            !input.0.eq(i)
                                && **v
                                && Self::EQ_BAND_ITEMS.iter().find(|item| item.eq(i)).is_some()
                        })
                        .for_each(|(i, v)| {
                            *v = false;
                            outputs.push((*i, ItemValue::Bool(*v)));
                        });
                }
            }
        } else if let ItemValue::U16(value) = input.1 {
            let _ = Self::U16_ITEMS
                .iter()
                .zip(&mut state.u16_items)
                .find(|(i, v)| input.0.eq(i) && !value.eq(v))
                .map(|(_, v)| {
                    *v = value;
                    outputs.push((input.0, ItemValue::U16(*v)));
                });

            if Self::HAS_BANK && input.0 == MachineItem::Bank {
                if state.bank != value && value <= Self::BANK_MAX {
                    state.bank = value;
                    outputs.push((MachineItem::Bank, ItemValue::U16(state.bank)));
                }
            }
        }

        outputs
    }
}

/// The trait to operate LED in surface.
pub trait TascamSurfaceLedOperation<T> {
    fn operate_leds(
        state: &mut T,
        machine_value: &(MachineItem, ItemValue),
        req: &mut FwReq,
        node: &mut FwNode,
        timeout_ms: u32,
    ) -> Result<(), Error>;

    fn clear_leds(
        state: &mut T,
        req: &mut FwReq,
        node: &mut FwNode,
        timeout_ms: u32,
    ) -> Result<(), Error>;
}

/// The trait to operate local state of surface.
pub trait TascamSurfaceStateOperation<T> {
    /// Initialize the state.
    fn init(state: &mut T);

    /// Peek machine value from image and event.
    fn peek(
        state: &T,
        image: &[u32],
        index: u32,
        before: u32,
        after: u32,
    ) -> Vec<(MachineItem, ItemValue)>;

    /// Ack the machine value returned from peek method.
    fn ack(state: &mut T, machine_value: &(MachineItem, ItemValue));
}

/// Common state of surface.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct TascamSurfaceCommonState {
    stateful_items: Vec<bool>,
    enabled_leds: LedState,
}

/// The trait to express specification of normal LEDs.
pub trait TascamSurfaceLedNormalSpecification {
    const NORMAL_LEDS: &'static [(&'static [MachineItem], &'static [u16])];
}

impl<O> TascamSurfaceLedOperation<TascamSurfaceCommonState> for O
where
    O: TascamSurfaceLedNormalSpecification,
{
    fn operate_leds(
        state: &mut TascamSurfaceCommonState,
        machine_value: &(MachineItem, ItemValue),
        req: &mut FwReq,
        node: &mut FwNode,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        if let ItemValue::Bool(value) = machine_value.1 {
            if let Some((_, positions)) = Self::NORMAL_LEDS.iter().find(|(items, _)| {
                if items.len() == 1 {
                    machine_value.0.eq(&items[0])
                } else {
                    items.iter().find(|i| machine_value.0.eq(i)).is_some()
                }
            }) {
                operate_led_cached(
                    &mut state.enabled_leds,
                    req,
                    node,
                    positions[0],
                    value,
                    timeout_ms,
                )?;
            }
        }

        Ok(())
    }

    fn clear_leds(
        state: &mut TascamSurfaceCommonState,
        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 specification for common state of surface.
pub trait TascamSurfaceStateCommonSpecification {
    /// The surface items to be stateful.
    const STATEFUL_ITEMS: &'static [(SurfaceBoolValue, MachineItem)];
    /// The surface items to be stateless.
    const STATELESS_ITEMS: &'static [(SurfaceBoolValue, MachineItem)];
    /// The surface rotaries.
    const ROTARIES: &'static [(SurfaceU16Value, MachineItem)];
    /// The surface faders.
    const FADERS: &'static [(SurfaceBoolValue, SurfaceU16Value, MachineItem)];
}

impl<O> TascamSurfaceStateOperation<TascamSurfaceCommonState> for O
where
    O: TascamSurfaceStateCommonSpecification,
{
    fn init(state: &mut TascamSurfaceCommonState) {
        state.stateful_items = vec![Default::default(); Self::STATEFUL_ITEMS.len()];
    }

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

        Self::STATEFUL_ITEMS
            .iter()
            .zip(&state.stateful_items)
            .filter(|((bool_val, _), _)| {
                detect_stateful_bool_action(bool_val, index, before, after)
            })
            .for_each(|((_, item), &s)| machine_values.push((*item, ItemValue::Bool(!s))));

        Self::STATELESS_ITEMS
            .iter()
            .filter(|(bool_val, _)| detect_bool_action(bool_val, index, before, after))
            .for_each(|(bool_val, item)| {
                let value = detect_bool_value(bool_val, before);
                machine_values.push((*item, ItemValue::Bool(value)));
            });

        Self::ROTARIES
            .iter()
            .filter(|(u16_val, _)| detect_u16_action(u16_val, index, before, after))
            .for_each(|(u16_val, item)| {
                let value = detect_u16_value(u16_val, after);
                machine_values.push((*item, ItemValue::U16(value)));
            });

        Self::FADERS
            .iter()
            .filter(|(bool_val, _, _)| detect_bool_action(bool_val, index, before, after))
            .for_each(|(_, u16_val, item)| {
                let value = detect_u16_value_in_image(u16_val, image);
                machine_values.push((*item, ItemValue::U16(value)));
            });

        machine_values
    }

    fn ack(state: &mut TascamSurfaceCommonState, machine_value: &(MachineItem, ItemValue)) {
        if let ItemValue::Bool(val) = machine_value.1 {
            Self::STATEFUL_ITEMS
                .iter()
                .zip(&mut state.stateful_items)
                .find(|((_, item), _)| machine_value.0.eq(item))
                .map(|((_, _), s)| *s = val);
        }
    }
}

/// Boolean value in surface image.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct SurfaceBoolValue(usize, u32); // index, mask.

/// U16 value in surface image.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct SurfaceU16Value(usize, u32, usize); // index, mask, shift

fn detect_stateful_bool_action(
    bool_val: &SurfaceBoolValue,
    index: u32,
    before: u32,
    after: u32,
) -> bool {
    bool_val.0 == index as usize && (before ^ after) & bool_val.1 > 0 && before & bool_val.1 > 0
}

fn detect_bool_action(bool_val: &SurfaceBoolValue, index: u32, before: u32, after: u32) -> bool {
    bool_val.0 == index as usize && (before ^ after) & bool_val.1 > 0
}

fn detect_bool_value(bool_val: &SurfaceBoolValue, before: u32) -> bool {
    before & bool_val.1 > 0
}

fn detect_u16_action(u16_val: &SurfaceU16Value, index: u32, before: u32, after: u32) -> bool {
    u16_val.0 == index as usize && (before ^ after) & u16_val.1 > 0
}

fn detect_u16_value(u16_val: &SurfaceU16Value, after: u32) -> u16 {
    ((after & u16_val.1) >> u16_val.2) as u16
}

fn detect_u16_value_in_image(u16_val: &SurfaceU16Value, image: &[u32]) -> u16 {
    ((image[u16_val.0] & u16_val.1) >> u16_val.2) as u16
}

#[derive(Default, Debug, Clone, PartialEq, Eq)]
struct LedState(Vec<u16>);

fn operate_led(
    req: &mut FwReq,
    node: &mut FwNode,
    pos: u16,
    enable: bool,
    timeout_ms: u32,
) -> Result<(), Error> {
    let mut frame = [0; 4];
    frame[0..2].copy_from_slice(&(enable as u16).to_be_bytes());
    frame[2..4].copy_from_slice(&pos.to_be_bytes());
    write_quadlet(req, node, LED_OFFSET, &mut frame, timeout_ms)
}

fn operate_led_cached(
    state: &mut LedState,
    req: &mut FwReq,
    node: &mut FwNode,
    pos: u16,
    enable: bool,
    timeout_ms: u32,
) -> Result<(), Error> {
    operate_led(req, node, pos, enable, timeout_ms).map(|_| {
        if !enable {
            state.0.retain(|&p| p != pos);
        } else if state.0.iter().find(|&p| *p == pos).is_none() {
            state.0.push(pos);
        }
    })
}

fn clear_leds(
    state: &mut LedState,
    req: &mut FwReq,
    node: &mut FwNode,
    timeout_ms: u32,
) -> Result<(), Error> {
    let cache = state.0.to_vec();
    cache
        .iter()
        .try_for_each(|&pos| operate_led_cached(state, req, node, pos, false, timeout_ms))
}

/// The trait for operation of FireWire LED.
pub trait FireWireLedOperation {
    const POSITIONS: &'static [u16];

    fn operate_firewire_led(
        req: &mut FwReq,
        node: &mut FwNode,
        enable: bool,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        operate_led(req, node, Self::POSITIONS[0], enable, timeout_ms)
    }
}