1 // Copyright 2022, The Android Open Source Project
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
15 //! Defines error type for uci_hal_android
16
17 use android_hardware_uwb::binder::{ExceptionCode, Status as BinderStatus, StatusCode};
18 use uwb_core::error::Error as UwbCoreError;
19
20 /// Union of the different errors with into implementations that project the error to the nearest
21 /// equivalent in each error type.
22 #[derive(Debug, thiserror::Error, PartialEq, Eq)]
23 pub enum Error {
24 /// uwb_core::error::Error
25 #[error("UwbCore error: {0:?}")]
26 UwbCoreError(#[from] UwbCoreError),
27 /// android_hardware_uwb::binder::StatusCode
28 #[error("Binder StatusCode error: {0:?}")]
29 StatusCode(#[from] StatusCode),
30 /// android_hardware_uwb::binder::Status
31 #[error("Binder Status error: {0:?}")]
32 BinderStatus(#[from] BinderStatus),
33 }
34
35 /// The From traits allow conversion of Result types and ? macro.
36 impl From<Error> for BinderStatus {
from(error: Error) -> BinderStatus37 fn from(error: Error) -> BinderStatus {
38 match error {
39 Error::BinderStatus(a) => a,
40 Error::StatusCode(StatusCode::OK) => BinderStatus::ok(),
41 Error::StatusCode(e) => {
42 BinderStatus::new_exception(status_code_to_exception_code(e), None)
43 }
44 Error::UwbCoreError(e) => {
45 BinderStatus::new_exception(uwb_core_error_to_exception_code(e), None)
46 }
47 }
48 }
49 }
50
51 impl From<Error> for UwbCoreError {
from(error: Error) -> UwbCoreError52 fn from(error: Error) -> UwbCoreError {
53 match error {
54 Error::BinderStatus(e) => exception_code_to_uwb_error(e.exception_code()),
55 Error::StatusCode(e) => status_code_to_uwb_core_error(e),
56 Error::UwbCoreError(a) => a,
57 }
58 }
59 }
60
status_code_to_exception_code(status_code: StatusCode) -> ExceptionCode61 fn status_code_to_exception_code(status_code: StatusCode) -> ExceptionCode {
62 match status_code {
63 // StatusCode::OK should not be reached from a Result type.
64 StatusCode::OK => ExceptionCode::NONE,
65 StatusCode::NO_MEMORY => ExceptionCode::TRANSACTION_FAILED,
66 StatusCode::INVALID_OPERATION => ExceptionCode::ILLEGAL_ARGUMENT,
67 StatusCode::BAD_VALUE => ExceptionCode::ILLEGAL_ARGUMENT,
68 StatusCode::BAD_TYPE => ExceptionCode::ILLEGAL_ARGUMENT,
69 StatusCode::NAME_NOT_FOUND => ExceptionCode::ILLEGAL_ARGUMENT,
70 StatusCode::PERMISSION_DENIED => ExceptionCode::SECURITY,
71 StatusCode::NO_INIT => ExceptionCode::ILLEGAL_ARGUMENT,
72 StatusCode::ALREADY_EXISTS => ExceptionCode::ILLEGAL_ARGUMENT,
73 StatusCode::DEAD_OBJECT => ExceptionCode::ILLEGAL_ARGUMENT,
74 StatusCode::FAILED_TRANSACTION => ExceptionCode::TRANSACTION_FAILED,
75 StatusCode::BAD_INDEX => ExceptionCode::ILLEGAL_ARGUMENT,
76 StatusCode::NOT_ENOUGH_DATA => ExceptionCode::ILLEGAL_ARGUMENT,
77 StatusCode::WOULD_BLOCK => ExceptionCode::TRANSACTION_FAILED,
78 StatusCode::TIMED_OUT => ExceptionCode::TRANSACTION_FAILED,
79 StatusCode::UNKNOWN_TRANSACTION => ExceptionCode::ILLEGAL_ARGUMENT,
80 StatusCode::FDS_NOT_ALLOWED => ExceptionCode::ILLEGAL_ARGUMENT,
81 StatusCode::UNEXPECTED_NULL => ExceptionCode::ILLEGAL_ARGUMENT,
82 _ => ExceptionCode::TRANSACTION_FAILED,
83 }
84 }
85
status_code_to_uwb_core_error(status_code: StatusCode) -> UwbCoreError86 fn status_code_to_uwb_core_error(status_code: StatusCode) -> UwbCoreError {
87 match status_code {
88 // StatusCode::OK should not be reached from a Result type.
89 StatusCode::OK => UwbCoreError::Unknown,
90 StatusCode::NO_MEMORY => UwbCoreError::Unknown,
91 StatusCode::INVALID_OPERATION => UwbCoreError::BadParameters,
92 StatusCode::BAD_VALUE => UwbCoreError::BadParameters,
93 StatusCode::BAD_TYPE => UwbCoreError::BadParameters,
94 StatusCode::NAME_NOT_FOUND => UwbCoreError::BadParameters,
95 StatusCode::PERMISSION_DENIED => UwbCoreError::BadParameters,
96 StatusCode::NO_INIT => UwbCoreError::BadParameters,
97 StatusCode::ALREADY_EXISTS => UwbCoreError::Unknown,
98 StatusCode::DEAD_OBJECT => UwbCoreError::Unknown,
99 StatusCode::FAILED_TRANSACTION => UwbCoreError::Unknown,
100 StatusCode::BAD_INDEX => UwbCoreError::BadParameters,
101 StatusCode::NOT_ENOUGH_DATA => UwbCoreError::BadParameters,
102 StatusCode::WOULD_BLOCK => UwbCoreError::Unknown,
103 StatusCode::TIMED_OUT => UwbCoreError::Timeout,
104 StatusCode::UNKNOWN_TRANSACTION => UwbCoreError::BadParameters,
105 StatusCode::FDS_NOT_ALLOWED => UwbCoreError::Unknown,
106 StatusCode::UNEXPECTED_NULL => UwbCoreError::Unknown,
107 _ => UwbCoreError::Unknown,
108 }
109 }
110
uwb_core_error_to_exception_code(uwb_core_error: UwbCoreError) -> ExceptionCode111 fn uwb_core_error_to_exception_code(uwb_core_error: UwbCoreError) -> ExceptionCode {
112 match uwb_core_error {
113 UwbCoreError::BadParameters => ExceptionCode::ILLEGAL_ARGUMENT,
114 _ => ExceptionCode::TRANSACTION_FAILED,
115 }
116 }
117
exception_code_to_uwb_error(exception_code: ExceptionCode) -> UwbCoreError118 fn exception_code_to_uwb_error(exception_code: ExceptionCode) -> UwbCoreError {
119 match exception_code {
120 ExceptionCode::ILLEGAL_ARGUMENT
121 | ExceptionCode::ILLEGAL_STATE
122 | ExceptionCode::UNSUPPORTED_OPERATION
123 | ExceptionCode::NULL_POINTER => UwbCoreError::BadParameters,
124 _ => UwbCoreError::Unknown,
125 }
126 }
127 /// Result type associated with Error:
128 pub type Result<T> = std::result::Result<T, Error>;
129
130 #[cfg(test)]
131 mod tests {
132 use super::*;
133
134 use android_hardware_uwb::binder::ExceptionCode;
135 use uwb_core::error::Error as UwbCoreError;
136
137 #[test]
test_uwb_core_error_to_exception_code()138 fn test_uwb_core_error_to_exception_code() {
139 let mut exception = uwb_core_error_to_exception_code(UwbCoreError::BadParameters);
140 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
141
142 exception = uwb_core_error_to_exception_code(UwbCoreError::ForeignFunctionInterface);
143 assert_eq!(exception, ExceptionCode::TRANSACTION_FAILED);
144 }
145
146 #[test]
test_exception_code_to_uwb_error()147 fn test_exception_code_to_uwb_error() {
148 let mut error = exception_code_to_uwb_error(ExceptionCode::ILLEGAL_ARGUMENT);
149 assert_eq!(error, UwbCoreError::BadParameters);
150
151 error = exception_code_to_uwb_error(ExceptionCode::ILLEGAL_STATE);
152 assert_eq!(error, UwbCoreError::BadParameters);
153
154 error = exception_code_to_uwb_error(ExceptionCode::UNSUPPORTED_OPERATION);
155 assert_eq!(error, UwbCoreError::BadParameters);
156
157 error = exception_code_to_uwb_error(ExceptionCode::NULL_POINTER);
158 assert_eq!(error, UwbCoreError::BadParameters);
159 }
160
161 #[test]
test_status_code_to_exception_code()162 fn test_status_code_to_exception_code() {
163 let mut exception = status_code_to_exception_code(StatusCode::OK);
164 assert_eq!(exception, ExceptionCode::NONE);
165
166 exception = status_code_to_exception_code(StatusCode::NO_MEMORY);
167 assert_eq!(exception, ExceptionCode::TRANSACTION_FAILED);
168
169 exception = status_code_to_exception_code(StatusCode::INVALID_OPERATION);
170 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
171
172 exception = status_code_to_exception_code(StatusCode::BAD_VALUE);
173 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
174
175 exception = status_code_to_exception_code(StatusCode::BAD_TYPE);
176 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
177
178 exception = status_code_to_exception_code(StatusCode::NAME_NOT_FOUND);
179 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
180
181 exception = status_code_to_exception_code(StatusCode::PERMISSION_DENIED);
182 assert_eq!(exception, ExceptionCode::SECURITY);
183
184 exception = status_code_to_exception_code(StatusCode::NO_INIT);
185 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
186
187 exception = status_code_to_exception_code(StatusCode::ALREADY_EXISTS);
188 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
189
190 exception = status_code_to_exception_code(StatusCode::DEAD_OBJECT);
191 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
192
193 exception = status_code_to_exception_code(StatusCode::FAILED_TRANSACTION);
194 assert_eq!(exception, ExceptionCode::TRANSACTION_FAILED);
195
196 exception = status_code_to_exception_code(StatusCode::BAD_INDEX);
197 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
198
199 exception = status_code_to_exception_code(StatusCode::NOT_ENOUGH_DATA);
200 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
201
202 exception = status_code_to_exception_code(StatusCode::WOULD_BLOCK);
203 assert_eq!(exception, ExceptionCode::TRANSACTION_FAILED);
204
205 exception = status_code_to_exception_code(StatusCode::TIMED_OUT);
206 assert_eq!(exception, ExceptionCode::TRANSACTION_FAILED);
207
208 exception = status_code_to_exception_code(StatusCode::UNKNOWN_TRANSACTION);
209 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
210
211 exception = status_code_to_exception_code(StatusCode::FDS_NOT_ALLOWED);
212 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
213
214 exception = status_code_to_exception_code(StatusCode::UNEXPECTED_NULL);
215 assert_eq!(exception, ExceptionCode::ILLEGAL_ARGUMENT);
216 }
217
218 #[test]
test_status_code_to_uwb_core_error()219 fn test_status_code_to_uwb_core_error() {
220 let mut error = status_code_to_uwb_core_error(StatusCode::OK);
221 assert_eq!(error, UwbCoreError::Unknown);
222
223 error = status_code_to_uwb_core_error(StatusCode::NO_MEMORY);
224 assert_eq!(error, UwbCoreError::Unknown);
225
226 error = status_code_to_uwb_core_error(StatusCode::BAD_VALUE);
227 assert_eq!(error, UwbCoreError::BadParameters);
228
229 error = status_code_to_uwb_core_error(StatusCode::BAD_TYPE);
230 assert_eq!(error, UwbCoreError::BadParameters);
231
232 error = status_code_to_uwb_core_error(StatusCode::NAME_NOT_FOUND);
233 assert_eq!(error, UwbCoreError::BadParameters);
234
235 error = status_code_to_uwb_core_error(StatusCode::PERMISSION_DENIED);
236 assert_eq!(error, UwbCoreError::BadParameters);
237
238 error = status_code_to_uwb_core_error(StatusCode::NO_INIT);
239 assert_eq!(error, UwbCoreError::BadParameters);
240
241 error = status_code_to_uwb_core_error(StatusCode::ALREADY_EXISTS);
242 assert_eq!(error, UwbCoreError::Unknown);
243
244 error = status_code_to_uwb_core_error(StatusCode::DEAD_OBJECT);
245 assert_eq!(error, UwbCoreError::Unknown);
246
247 error = status_code_to_uwb_core_error(StatusCode::FAILED_TRANSACTION);
248 assert_eq!(error, UwbCoreError::Unknown);
249
250 error = status_code_to_uwb_core_error(StatusCode::BAD_INDEX);
251 assert_eq!(error, UwbCoreError::BadParameters);
252
253 error = status_code_to_uwb_core_error(StatusCode::NOT_ENOUGH_DATA);
254 assert_eq!(error, UwbCoreError::BadParameters);
255
256 error = status_code_to_uwb_core_error(StatusCode::WOULD_BLOCK);
257 assert_eq!(error, UwbCoreError::Unknown);
258
259 error = status_code_to_uwb_core_error(StatusCode::TIMED_OUT);
260 assert_eq!(error, UwbCoreError::Timeout);
261
262 error = status_code_to_uwb_core_error(StatusCode::UNKNOWN_TRANSACTION);
263 assert_eq!(error, UwbCoreError::BadParameters);
264
265 error = status_code_to_uwb_core_error(StatusCode::FDS_NOT_ALLOWED);
266 assert_eq!(error, UwbCoreError::Unknown);
267
268 error = status_code_to_uwb_core_error(StatusCode::UNEXPECTED_NULL);
269 assert_eq!(error, UwbCoreError::Unknown);
270 }
271 }
272