1 /*
2  * Copyright (C) 2022 The Android Open Source Project
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  */
16 
17 use crate::sys::*;
18 use alloc::alloc::AllocError;
19 use core::ffi::FromBytesWithNulError;
20 use core::num::TryFromIntError;
21 use tipc::TipcError;
22 use trusty_sys::Error;
23 
24 #[derive(Debug, Eq, PartialEq)]
25 pub enum HwkeyError {
26     Generic,
27     NotValid,
28     BadLen,
29     NotImplemented,
30     NotFound,
31     AlreadyExists,
32     AllocError,
33     InvalidCmdResponse,
34     OutOfBounds,
35     System(Error),
36     Tipc(TipcError),
37 }
38 
39 impl HwkeyError {
from_hwkey_rc(rc: u32) -> Result<(), Self>40     pub(crate) fn from_hwkey_rc(rc: u32) -> Result<(), Self> {
41         #[allow(non_upper_case_globals)]
42         match rc.into() {
43             hwkey_err_HWKEY_NO_ERROR => Ok(()),
44             hwkey_err_HWKEY_ERR_GENERIC => Err(Self::Generic),
45             hwkey_err_HWKEY_ERR_NOT_VALID => Err(Self::NotValid),
46             hwkey_err_HWKEY_ERR_BAD_LEN => Err(Self::BadLen),
47             hwkey_err_HWKEY_ERR_NOT_IMPLEMENTED => Err(Self::NotImplemented),
48             hwkey_err_HWKEY_ERR_NOT_FOUND => Err(Self::NotFound),
49             hwkey_err_HWKEY_ERR_ALREADY_EXISTS => Err(Self::AlreadyExists),
50             _ => Err(Self::Generic),
51         }
52     }
53 }
54 
55 impl From<TipcError> for HwkeyError {
from(e: TipcError) -> Self56     fn from(e: TipcError) -> Self {
57         Self::Tipc(e)
58     }
59 }
60 
61 impl From<Error> for HwkeyError {
from(e: Error) -> Self62     fn from(e: Error) -> Self {
63         Self::System(e)
64     }
65 }
66 
67 impl From<TryFromIntError> for HwkeyError {
from(_err: TryFromIntError) -> Self68     fn from(_err: TryFromIntError) -> Self {
69         HwkeyError::OutOfBounds
70     }
71 }
72 
73 impl From<AllocError> for HwkeyError {
from(_err: AllocError) -> Self74     fn from(_err: AllocError) -> Self {
75         Self::AllocError
76     }
77 }
78 
79 impl From<FromBytesWithNulError> for HwkeyError {
from(_err: FromBytesWithNulError) -> Self80     fn from(_err: FromBytesWithNulError) -> Self {
81         Self::NotValid
82     }
83 }
84