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
use std::{
    collections::HashMap,
    fmt::{self, Display},
    str::FromStr,
};

use anyhow::{anyhow, Result};
use gaia_ccsds_c2a::access::cmd::schema::{from_tlmcmddb, CommandSchema};
use itertools::Itertools;

use crate::proto::tmtc_generic_c2a as proto;
use crate::satconfig;

struct TcoName {
    prefix: String,
    component: String,
    command: String,
}

impl Display for TcoName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.prefix, self.component, self.command)
    }
}

impl FromStr for TcoName {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut parts = s.split('.');
        let prefix = parts.next().ok_or(())?;
        let component = parts.next().ok_or(())?;
        let command = parts.next().ok_or(())?;
        if parts.next().is_some() {
            return Err(());
        }
        Ok(Self {
            prefix: prefix.to_string(),
            component: component.to_string(),
            command: command.to_string(),
        })
    }
}

#[derive(Debug, Clone)]
pub struct FatCommandSchema<'a> {
    pub apid: u16,
    pub command_id: u16,
    pub destination_type: u8,
    pub execution_type: u8,
    pub has_time_indicator: bool,
    pub schema: &'a CommandSchema,
}

#[derive(Debug, Clone)]
struct CommandSchemaWithId {
    apid: u16,
    command_id: u16,
    schema: CommandSchema,
}

#[derive(Debug, Clone)]
pub struct Registry {
    prefix_map: satconfig::CommandPrefixMap,
    schema_map: HashMap<(String, String), CommandSchemaWithId>,
}

impl Registry {
    pub fn build_command_prefix_schema_map(&self) -> HashMap<String, proto::CommandPrefixSchema> {
        self.prefix_map
            .iter()
            .map(|(prefix, subsystem_map)| {
                let prefix = prefix.to_string();
                let subsystems = subsystem_map
                    .iter()
                    .map(|(component_name, subsystem)| {
                        let component_name = component_name.to_string();
                        let command_subsystem_schema = proto::CommandSubsystemSchema {
                            metadata: Some(proto::CommandSubsystemSchemaMetadata {
                                destination_type: subsystem.destination_type as u32,
                                execution_type: subsystem.execution_type as u32,
                            }),
                            has_time_indicator: subsystem.has_time_indicator,
                        };
                        (component_name, command_subsystem_schema)
                    })
                    .collect();
                let command_prefix_schema = proto::CommandPrefixSchema {
                    metadata: Some(proto::CommandPrefixSchemaMetadata {}),
                    subsystems,
                };
                (prefix, command_prefix_schema)
            })
            .collect()
    }

    pub fn build_command_component_schema_map(
        &self,
    ) -> HashMap<String, proto::CommandComponentSchema> {
        self.schema_map
            .iter()
            .sorted_by_key(|&((component_name, _), _)| component_name)
            .group_by(|&((component_name, _), schema_with_id)| {
                (component_name, schema_with_id.apid)
            })
            .into_iter()
            .map(|((component_name, apid), group)| {
                let command_schema_map = group
                    .map(|((_, command_name), schema_with_id)| {
                        let trailer_parameter = if schema_with_id.schema.has_trailer_parameter {
                            Some(proto::CommandParameterSchema {
                                metadata: Some(proto::CommandParameterSchemaMetadata {}),
                                data_type: proto::CommandParameterDataType::CmdParameterBytes
                                    .into(),
                            })
                        } else {
                            None
                        };
                        let parameters = schema_with_id
                            .schema
                            .sized_parameters
                            .iter()
                            .map(|param| {
                                let data_type = match param {
                                    structpack::NumericField::Integral(_) => {
                                        proto::CommandParameterDataType::CmdParameterInteger
                                    }
                                    structpack::NumericField::Floating(_) => {
                                        proto::CommandParameterDataType::CmdParameterDouble
                                    }
                                };
                                proto::CommandParameterSchema {
                                    metadata: Some(proto::CommandParameterSchemaMetadata {}),
                                    data_type: data_type.into(),
                                }
                            })
                            .chain(trailer_parameter)
                            .collect();
                        let command_name = command_name.to_string();
                        let command_schema = proto::CommandSchema {
                            metadata: Some(proto::CommandSchemaMetadata {
                                id: schema_with_id.command_id as u32,
                            }),
                            parameters,
                        };
                        (command_name, command_schema)
                    })
                    .collect();
                let command_component_schema = proto::CommandComponentSchema {
                    metadata: Some(proto::CommandComponentSchemaMetadata { apid: apid as u32 }),
                    commands: command_schema_map,
                };
                (component_name.to_string(), command_component_schema)
            })
            .collect()
    }

    pub fn lookup(&self, tco_name: &str) -> Option<FatCommandSchema> {
        let TcoName {
            prefix,
            component,
            command,
        } = tco_name.parse().ok()?;
        let satconfig::CommandSubsystem {
            has_time_indicator,
            destination_type,
            execution_type,
        } = self.prefix_map.get(&prefix)?.get(&component)?;
        let CommandSchemaWithId {
            apid,
            command_id,
            schema,
        } = self.schema_map.get(&(component, command))?;
        Some(FatCommandSchema {
            apid: *apid,
            command_id: *command_id,
            destination_type: *destination_type,
            execution_type: *execution_type,
            has_time_indicator: *has_time_indicator,
            schema,
        })
    }

    pub fn from_tlmcmddb_with_satconfig(
        db: &tlmcmddb::Database,
        apid_map: &HashMap<String, u16>,
        prefix_map: satconfig::CommandPrefixMap,
    ) -> Result<Self> {
        let schema_map = from_tlmcmddb(db)
            .flatten()
            .map(|schema| {
                let (metadata, schema) = schema?;
                let component = metadata.component_name;
                let cmddb_name = metadata.command_name;
                let apid = *apid_map
                    .get(&component)
                    .ok_or_else(|| anyhow!("APID is not defined for {component}"))?;
                let schema_with_id = CommandSchemaWithId {
                    apid,
                    command_id: metadata.cmd_id,
                    schema,
                };
                Ok(((component, cmddb_name), schema_with_id))
            })
            .collect::<Result<_>>()?;
        Ok(Self {
            schema_map,
            prefix_map,
        })
    }
}