Skip to main content

tss_esapi/constants/return_code/
layer.rs

1// Copyright 2022 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3use crate::{
4    constants::tss::{
5        TSS2_ESYS_RC_LAYER, TSS2_FEATURE_RC_LAYER, TSS2_MU_RC_LAYER, TSS2_RESMGR_RC_LAYER,
6        TSS2_RESMGR_TPM_RC_LAYER, TSS2_SYS_RC_LAYER, TSS2_TCTI_RC_LAYER, TSS2_TPM_RC_LAYER,
7    },
8    Error, Result, WrapperErrorKind,
9};
10
11use log::error;
12use num_derive::{FromPrimitive, ToPrimitive};
13use num_traits::{FromPrimitive, ToPrimitive};
14use std::convert::TryFrom;
15
16/// Enum representing the TSS layer of a
17/// return code.
18#[derive(FromPrimitive, ToPrimitive, Copy, Clone, Debug, PartialEq, Eq, Hash)]
19#[repr(u8)]
20pub enum ReturnCodeLayer {
21    Tpm = u32::to_le_bytes(TSS2_TPM_RC_LAYER)[2],
22    Feature = u32::to_le_bytes(TSS2_FEATURE_RC_LAYER)[2],
23    Esys = u32::to_le_bytes(TSS2_ESYS_RC_LAYER)[2],
24    Sys = u32::to_le_bytes(TSS2_SYS_RC_LAYER)[2],
25    Mu = u32::to_le_bytes(TSS2_MU_RC_LAYER)[2],
26    Tcti = u32::to_le_bytes(TSS2_TCTI_RC_LAYER)[2],
27    ResMgr = u32::to_le_bytes(TSS2_RESMGR_RC_LAYER)[2],
28    ResMgrTpm = u32::to_le_bytes(TSS2_RESMGR_TPM_RC_LAYER)[2],
29}
30
31impl TryFrom<u8> for ReturnCodeLayer {
32    type Error = Error;
33
34    fn try_from(value: u8) -> Result<Self> {
35        ReturnCodeLayer::from_u8(value).ok_or_else(|| {
36            error!("{:#02X} is not valid ReturnCodeLayer", value);
37            Error::local_error(WrapperErrorKind::InvalidParam)
38        })
39    }
40}
41
42impl From<ReturnCodeLayer> for u8 {
43    fn from(return_code_layer: ReturnCodeLayer) -> u8 {
44        // The values are well defined so unwrap cannot panic.
45        return_code_layer.to_u8().unwrap()
46    }
47}