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
//! Contains the DeviceNetworkHighLevel struct that wraps the low-level
//! complicated facade sub-system and which is exposed by the IDeviceNetworkHighLevel
//! trait.

use super::facade_complicatedsubsystem::FacadeComplicatedSubSystem;
use super::facade_idevicenetworklowlevel_trait::IDeviceNetworkLowLevel;
use super::facade_idevicenetworkhighlevel_trait::IDeviceNetworkHighLevel;

/// This struct wraps the IDeviceNetworkLowLevel interface and implements
/// the high level IDeviceNetworkHighLevel interface, which is a simpler
/// interface.  All calls on the high level interface are forwarded to the
/// appropriate low level interface.
/// Part of the "Facade" pattern example.
pub struct DeviceNetworkHighLevel {
    low_level_system: Box<dyn IDeviceNetworkLowLevel>,
}

impl DeviceNetworkHighLevel {
    /// Constructor.
    pub fn new() -> Box<dyn IDeviceNetworkHighLevel> {
        Box::new(DeviceNetworkHighLevel {
            low_level_system : FacadeComplicatedSubSystem::new(),
        })
    }
}

impl IDeviceNetworkHighLevel for DeviceNetworkHighLevel {
    fn num_chains(&self) -> usize {
        self.low_level_system.get_num_chains()
    }

    fn get_idcodes(&mut self, chain_index: usize) -> Vec<u32> {
        let mut idcodes: Vec<u32> = vec![];
        if self.low_level_system.lock_device_chain(chain_index) {
            idcodes = self.low_level_system.get_idcodes(chain_index);
            self.low_level_system.unlock_device_chain(chain_index);
        }

        idcodes
    }

    fn enable_devices_in_device_chain(&mut self, chain_index: usize, select_mask: u32) {
        if self.low_level_system.lock_device_chain(chain_index) {
            self.low_level_system.enable_devices_in_device_chain(chain_index, select_mask);
            self.low_level_system.unlock_device_chain(chain_index);
        }
    }

    fn disable_devices_in_device_chain(&mut self, chain_index: usize) {
        if self.low_level_system.lock_device_chain(chain_index) {
            self.low_level_system.reset_device_chain(chain_index);
            self.low_level_system.unlock_device_chain(chain_index);
        }
    }
}