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 super::*;
18 
19 use core::{array::TryFromSliceError, num::TryFromIntError};
20 use tipc::TipcError;
21 use trusty_std::alloc::AllocError;
22 use trusty_sys::Error;
23 
24 /// Errors that the HwWsk client and service may encounter.
25 #[derive(Debug, Eq, PartialEq)]
26 pub enum HwWskError {
27     /// The requested command or specified parameter is not supported.
28     NotSupported,
29     /// A generic error received by the client as a response from the service.
30     Generic,
31     /// An invalid command or command parameter specified.
32     NotValid,
33     /// An unexpected or unaccepted buffer or data length.
34     BadLen,
35     /// An integer overflow error or bad cast.
36     OutOfBounds,
37     /// An allocation failure that may be due to resource exhaustion.
38     AllocError,
39     /// The client receives a response from the service that is invalid.
40     InvalidCmdResponse,
41     /// A conversion from a slice to an array fails.
42     ConversionError,
43     /// Some tipc error.
44     Tipc(TipcError),
45     /// Some other system error.
46     System(Error),
47 }
48 
49 impl HwWskError {
from_status(rc: u32) -> Result<(), Self>50     pub(crate) fn from_status(rc: u32) -> Result<(), Self> {
51         #[allow(non_upper_case_globals)]
52         match rc.into() {
53             hwwsk_err_HWWSK_NO_ERROR => Ok(()),
54             hwwsk_err_HWWSK_ERR_INVALID_ARGS => Err(HwWskError::NotValid),
55             hwwsk_err_HWWSK_ERR_NOT_SUPPORTED => Err(HwWskError::NotSupported),
56             hwwsk_err_HWWSK_ERR_BAD_LEN => Err(HwWskError::BadLen),
57             _ => Err(HwWskError::Generic),
58         }
59     }
60 }
61 
62 impl From<TipcError> for HwWskError {
from(err: TipcError) -> Self63     fn from(err: TipcError) -> Self {
64         HwWskError::Tipc(err)
65     }
66 }
67 
68 impl From<Error> for HwWskError {
from(err: Error) -> Self69     fn from(err: Error) -> Self {
70         HwWskError::System(err)
71     }
72 }
73 
74 impl From<TryFromIntError> for HwWskError {
from(_err: TryFromIntError) -> Self75     fn from(_err: TryFromIntError) -> Self {
76         HwWskError::OutOfBounds
77     }
78 }
79 
80 impl From<AllocError> for HwWskError {
from(_err: AllocError) -> Self81     fn from(_err: AllocError) -> Self {
82         HwWskError::AllocError
83     }
84 }
85 
86 impl From<TryFromSliceError> for HwWskError {
from(_err: TryFromSliceError) -> Self87     fn from(_err: TryFromSliceError) -> Self {
88         HwWskError::ConversionError
89     }
90 }
91