• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //! Helper functions and macros
16 
17 use jni::sys::{jboolean, jbyte};
18 use log::error;
19 use uwb_core::error::{Error, Result};
20 use uwb_uci_packets::StatusCode;
21 
boolean_result_helper<T>(result: Result<T>, error_msg: &str) -> jboolean22 pub(crate) fn boolean_result_helper<T>(result: Result<T>, error_msg: &str) -> jboolean {
23     match result {
24         Ok(_) => true,
25         Err(e) => {
26             error!("{} failed with {:?}", error_msg, &e);
27             false
28         }
29     }
30     .into()
31 }
32 
byte_result_helper<T>(result: Result<T>, error_msg: &str) -> jbyte33 pub(crate) fn byte_result_helper<T>(result: Result<T>, error_msg: &str) -> jbyte {
34     // StatusCode do not overflow i8
35     u8::from(result_to_status_code(result, error_msg)) as i8
36 }
37 
38 /// helper function to convert Result to StatusCode
result_to_status_code<T>(result: Result<T>, error_msg: &str) -> StatusCode39 fn result_to_status_code<T>(result: Result<T>, error_msg: &str) -> StatusCode {
40     match result.map_err(|e| {
41         error!("{} failed with {:?}", error_msg, &e);
42         e
43     }) {
44         Ok(_) => StatusCode::UciStatusOk,
45         Err(Error::BadParameters) => StatusCode::UciStatusInvalidParam,
46         Err(Error::MaxSessionsExceeded) => StatusCode::UciStatusMaxSessionsExceeded,
47         Err(Error::CommandRetry) => StatusCode::UciStatusCommandRetry,
48         // For other Error, only generic fail can be given.
49         Err(_) => StatusCode::UciStatusFailed,
50     }
51 }
52 
option_result_helper<T>(result: Result<T>, error_msg: &str) -> Option<T>53 pub(crate) fn option_result_helper<T>(result: Result<T>, error_msg: &str) -> Option<T> {
54     result
55         .map_err(|e| {
56             error!("{} failed with {:?}", error_msg, &e);
57             e
58         })
59         .ok()
60 }
61