Skip to main content

nv_redfish/computer_system/
bios.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//! Bios
16
17use crate::schema::redfish::bios::Bios as BiosSchema;
18use crate::Error;
19use crate::NvBmc;
20use nv_redfish_core::Bmc;
21use nv_redfish_core::EdmPrimitiveType;
22use nv_redfish_core::NavProperty;
23use std::marker::PhantomData;
24use std::sync::Arc;
25
26/// BIOS.
27///
28/// Provides functions to access BIOS functions.
29pub struct Bios<B: Bmc> {
30    data: Arc<BiosSchema>,
31    _marker: PhantomData<B>,
32}
33
34impl<B: Bmc> Bios<B> {
35    /// Create a new log bios handle.
36    pub(crate) async fn new(
37        bmc: &NvBmc<B>,
38        nav: &NavProperty<BiosSchema>,
39    ) -> Result<Self, Error<B>> {
40        nav.get(bmc.as_ref())
41            .await
42            .map_err(crate::Error::Bmc)
43            .map(|data| Self {
44                data,
45                _marker: PhantomData,
46            })
47    }
48
49    /// Get the raw schema data for the BIOS.
50    #[must_use]
51    pub fn raw(&self) -> Arc<BiosSchema> {
52        self.data.clone()
53    }
54
55    /// Get bios attribute by key value.
56    #[must_use]
57    pub fn attribute<'a>(&'a self, name: &str) -> Option<BiosAttributeRef<'a>> {
58        self.data
59            .attributes
60            .as_ref()
61            .and_then(|attributes| attributes.dynamic_properties.get(name))
62            .map(|v| BiosAttributeRef::new(v.as_ref()))
63    }
64}
65
66/// Reference to a BIOS attribute.
67pub struct BiosAttributeRef<'a> {
68    value: Option<&'a EdmPrimitiveType>,
69}
70
71impl<'a> BiosAttributeRef<'a> {
72    const fn new(value: Option<&'a EdmPrimitiveType>) -> Self {
73        Self { value }
74    }
75
76    /// Returns true if attribute is null.
77    #[must_use]
78    pub const fn is_null(&self) -> bool {
79        self.value.is_none()
80    }
81
82    /// Returns string value of the attribute if attribute is string.
83    #[must_use]
84    pub const fn str_value(&self) -> Option<&str> {
85        match self.value {
86            Some(EdmPrimitiveType::String(v)) => Some(v.as_str()),
87            _ => None,
88        }
89    }
90
91    /// Returns boolean value of the attribute if attribute is bool.
92    #[must_use]
93    pub const fn bool_value(&self) -> Option<bool> {
94        match self.value {
95            Some(EdmPrimitiveType::Bool(v)) => Some(*v),
96            _ => None,
97        }
98    }
99
100    /// Returns integer value of the attribute if attribute is integer.
101    #[must_use]
102    pub const fn integer_value(&self) -> Option<i64> {
103        match self.value {
104            Some(EdmPrimitiveType::Integer(v)) => Some(*v),
105            _ => None,
106        }
107    }
108
109    /// Returns decimal value of the attribute if attribute is decimal.
110    #[must_use]
111    pub const fn decimal_value(&self) -> Option<f64> {
112        match self.value {
113            Some(EdmPrimitiveType::Decimal(v)) => Some(*v),
114            _ => None,
115        }
116    }
117}