aboutsummaryrefslogtreecommitdiff
path: root/rust/netsim-cxx/src/devices/device.rs
blob: 0d20b89fa13ecb47de847126d6c95fca4145b21c (plain)
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
// Device.rs

use protobuf::Message;

use crate::devices::chip;
use crate::devices::chip::Chip;
use crate::devices::chip::ChipIdentifier;
use crate::devices::facades::FacadeIdentifier;
use crate::proto::common::ChipKind as ProtoChipKind;
use crate::proto::model::Device as ProtoDevice;
use crate::proto::model::Orientation as ProtoOrientation;
use crate::proto::model::Position as ProtoPosition;
use std::collections::HashMap;

pub type DeviceIdentifier = i32;

pub struct Device {
    pub id: DeviceIdentifier,
    pub guid: String,
    name: String,
    visible: bool,
    pub position: ProtoPosition,
    orientation: ProtoOrientation,
    pub chips: HashMap<ChipIdentifier, Chip>,
}
impl Device {
    pub fn new(id: DeviceIdentifier, guid: String, name: String) -> Self {
        Device {
            id,
            guid,
            name,
            visible: true,
            position: ProtoPosition::new(),
            orientation: ProtoOrientation::new(),
            chips: HashMap::new(),
        }
    }
}

pub struct AddChipResult {
    pub device_id: DeviceIdentifier,
    pub chip_id: ChipIdentifier,
    pub facade_id: FacadeIdentifier,
}

impl Device {
    pub fn get(&self) -> ProtoDevice {
        let mut device = ProtoDevice::new();
        device.id = self.id;
        device.name = self.name.clone();
        device.visible = self.visible;
        device.position = protobuf::MessageField::from(Some(self.position.clone()));
        device.orientation = protobuf::MessageField::from(Some(self.orientation.clone()));
        for chip in self.chips.values() {
            device.chips.push(chip.get());
        }
        device
    }

    /// Patch a device and its chips.
    pub fn patch(&mut self, patch: &ProtoDevice) {
        // TODO visible should be State
        self.visible = patch.visible;
        if patch.position.is_some() {
            self.position.clone_from(&patch.position);
        }
        if patch.orientation.is_some() {
            self.orientation.clone_from(&patch.orientation);
        }
        // iterate over patched ProtoChip entries and patch matching chip
        for patch_chip in patch.chips.iter() {
            // Allow default chip kind of BLUETOOTH
            let patch_chip_kind = patch_chip.kind.enum_value_or(ProtoChipKind::BLUETOOTH);
            let patch_chip_name = &patch_chip.name;
            // Find the matching chip and patch the proto chip
            for chip in self.chips.values_mut() {
                if chip.name.eq(patch_chip_name) && chip.kind == patch_chip_kind {
                    chip.patch(patch_chip);
                    break; // next proto chip
                }
            }
        }
    }

    /// Remove a chip from a device.
    pub fn remove_chip(&mut self, chip_id: ChipIdentifier) {
        if let Some(chip) = self.chips.get_mut(&chip_id) {
            chip.remove();
        } else {
            eprintln!("RemoveChip id {chip_id} not found");
        }
        self.chips.remove(&chip_id);
    }

    pub fn add_chip(
        &mut self,
        device_name: &str,
        chip_kind: ProtoChipKind,
        chip_name: &str,
        chip_manufacturer: &str,
        chip_product_name: &str,
    ) -> Option<AddChipResult> {
        for chip in self.chips.values() {
            if chip.kind == chip_kind && chip.name == chip_name {
                eprintln!("Device::AddChip - duplicate at id {}, skipping.", chip.id);
                return None;
            }
        }
        let chip = chip::chip_new(
            self.id,
            chip_kind,
            chip_name,
            device_name,
            chip_manufacturer,
            chip_product_name,
        );
        let chip_id = chip.id;
        let facade_id = chip.facade_id;
        self.chips.insert(chip.id, chip);
        Some(AddChipResult { device_id: self.id, chip_id, facade_id })
    }

    /// Reset a device to its default state.
    pub fn reset(&mut self) {
        self.visible = true;
        self.position.clear();
        self.orientation.clear();
        for chp in self.chips.values_mut() {
            chp.reset();
        }
    }

    /// Remove all chips from a device.
    /// Called at shutdown.
    #[allow(dead_code)]
    pub fn remove(&mut self) {
        for (_, chip) in self.chips.iter_mut() {
            chip.remove();
        }
    }
}