Skip to main content

wasefire_protocol/
platform.rs

1// Copyright 2024 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use wasefire_common::platform::Side;
16use wasefire_error::Error;
17use wasefire_wire::Wire;
18#[cfg(feature = "host")]
19use wasefire_wire::Yoke;
20
21use crate::common::{AppletKind, Hexa, Name};
22
23#[derive(Debug)]
24#[cfg(feature = "host")]
25pub enum DynInfo {
26    V3(Yoke<Info3<'static>>),
27    V2(Yoke<_Info2<'static>>),
28    V1(Yoke<_Info1<'static>>),
29    V0(Yoke<_Info0<'static>>),
30}
31
32#[cfg(feature = "host")]
33impl DynInfo {
34    pub fn serial(&self) -> &Hexa<'_> {
35        match self {
36            DynInfo::V3(x) => &x.get().serial,
37            DynInfo::V2(x) => &x.get().serial,
38            DynInfo::V1(x) => &x.get().serial,
39            DynInfo::V0(x) => &x.get().serial,
40        }
41    }
42
43    pub fn applet_kind(&self) -> Option<AppletKind> {
44        match self {
45            DynInfo::V3(x) => Some(x.get().applet_kind),
46            DynInfo::V2(x) => Some(x.get().applet_kind),
47            DynInfo::V1(_) => None,
48            DynInfo::V0(_) => None,
49        }
50    }
51
52    pub fn running_side(&self) -> Option<Side> {
53        match self {
54            DynInfo::V3(x) => Some(x.get().running_side),
55            DynInfo::V2(x) => Some(x.get().running_side),
56            DynInfo::V1(x) => Some(x.get().running_side),
57            DynInfo::V0(_) => None,
58        }
59    }
60
61    pub fn running_name(&self) -> Option<&Name<'_>> {
62        match self {
63            DynInfo::V3(x) => Some(&x.get().running_info.name),
64            DynInfo::V2(_) => None,
65            DynInfo::V1(_) => None,
66            DynInfo::V0(_) => None,
67        }
68    }
69
70    pub fn running_version(&self) -> &Hexa<'_> {
71        match self {
72            DynInfo::V3(x) => &x.get().running_info.version,
73            DynInfo::V2(x) => &x.get().running_version,
74            DynInfo::V1(x) => &x.get().running_version,
75            DynInfo::V0(x) => &x.get().version,
76        }
77    }
78
79    pub fn opposite_name(&self) -> Option<Result<&Name<'_>, Error>> {
80        Some(match self {
81            DynInfo::V3(x) => try { &x.get().opposite_info.as_ref()?.name },
82            DynInfo::V2(_) => return None,
83            DynInfo::V1(_) => return None,
84            DynInfo::V0(_) => return None,
85        })
86    }
87
88    pub fn opposite_version(&self) -> Option<Result<&Hexa<'_>, Error>> {
89        Some(match self {
90            DynInfo::V3(x) => try { &x.get().opposite_info.as_ref()?.version },
91            DynInfo::V2(x) => try { x.get().opposite_version.as_ref()? },
92            DynInfo::V1(x) => try { x.get().opposite_version.as_ref()? },
93            DynInfo::V0(_) => return None,
94        })
95    }
96}
97
98#[derive(Debug, Wire)]
99#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
100pub struct Info3<'a> {
101    pub serial: Hexa<'a>,
102    pub applet_kind: AppletKind,
103    pub running_side: Side,
104    pub running_info: SideInfo0<'a>,
105    pub opposite_info: Result<SideInfo0<'a>, Error>,
106}
107
108/// Information about a platform side.
109#[derive(Debug, Wire)]
110#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
111pub struct SideInfo0<'a> {
112    /// Name of the platform.
113    ///
114    /// This field has no particular interpretation.
115    pub name: Name<'a>,
116
117    /// Version of the platform.
118    ///
119    /// This field is interpreted by lexicographical order.
120    pub version: Hexa<'a>,
121}
122
123#[derive(Debug, Wire)]
124#[cfg(feature = "host")]
125#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
126pub struct _Info2<'a> {
127    pub serial: Hexa<'a>,
128    pub applet_kind: AppletKind,
129    pub running_side: Side,
130    pub running_version: Hexa<'a>,
131    pub opposite_version: Result<Hexa<'a>, Error>,
132}
133
134#[derive(Debug, Wire)]
135#[cfg(feature = "host")]
136#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
137pub struct _Info1<'a> {
138    pub serial: Hexa<'a>,
139    pub running_side: Side,
140    pub running_version: Hexa<'a>,
141    pub opposite_version: Result<Hexa<'a>, Error>,
142}
143
144#[derive(Debug, Wire)]
145#[cfg(feature = "host")]
146#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
147pub struct _Info0<'a> {
148    pub serial: Hexa<'a>,
149    pub version: Hexa<'a>,
150}