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 let result = result.map_err(|e| {
41 error!("{} failed with {:?}", error_msg, &e);
42 e
43 });
44 match result {
45 Ok(_) => StatusCode::UciStatusOk,
46 Err(Error::BadParameters) => StatusCode::UciStatusInvalidParam,
47 Err(Error::MaxSessionsExceeded) => StatusCode::UciStatusMaxSessionsExceeded,
48 Err(Error::CommandRetry) => StatusCode::UciStatusCommandRetry,
49 Err(Error::RegulationUwbOff) => StatusCode::UciStatusRegulationUwbOff,
50 // For other Error, only generic fail can be given.
51 Err(_) => StatusCode::UciStatusFailed,
52 }
53 }
54
option_result_helper<T>(result: Result<T>, error_msg: &str) -> Option<T>55 pub(crate) fn option_result_helper<T>(result: Result<T>, error_msg: &str) -> Option<T> {
56 result
57 .map_err(|e| {
58 error!("{} failed with {:?}", error_msg, &e);
59 e
60 })
61 .ok()
62 }
63
64 #[cfg(test)]
65 mod tests {
66 use super::*;
67 #[test]
test_boolean_result_helper()68 fn test_boolean_result_helper() {
69 let result: Result<i32> = Ok(5);
70 let error_msg = "Error!";
71 let jboolean_result = boolean_result_helper(result, error_msg);
72 assert_eq!(jboolean_result, true.into()); // Should return true
73
74 // Test case 2: Result is Err
75 let result: Result<i32> = Err(Error::BadParameters);
76 let error_msg = "Error!";
77 let jboolean_result = boolean_result_helper(result, error_msg);
78 assert_eq!(jboolean_result, false.into()); // Should return false
79 }
80
81 #[test]
test_byte_result_helper()82 fn test_byte_result_helper() {
83 // Test cases for each Error variant
84 assert_eq!(byte_result_helper(Ok(10), "Test"), u8::from(StatusCode::UciStatusOk) as i8);
85 assert_eq!(
86 byte_result_helper::<i8>(Err(Error::BadParameters), "Test"),
87 u8::from(StatusCode::UciStatusInvalidParam) as i8
88 );
89 assert_eq!(
90 byte_result_helper::<i8>(Err(Error::MaxSessionsExceeded), "Test"),
91 u8::from(StatusCode::UciStatusMaxSessionsExceeded) as i8
92 );
93 assert_eq!(
94 byte_result_helper::<i8>(Err(Error::CommandRetry), "Test"),
95 u8::from(StatusCode::UciStatusCommandRetry) as i8
96 );
97 assert_eq!(
98 byte_result_helper::<i8>(Err(Error::RegulationUwbOff), "Test"),
99 u8::from(StatusCode::UciStatusRegulationUwbOff) as i8
100 );
101
102 // Test case for a generic error
103 assert_eq!(
104 byte_result_helper::<i8>(Err(Error::DuplicatedSessionId), "Test"),
105 u8::from(StatusCode::UciStatusFailed) as i8
106 );
107 }
108
109 #[test]
test_option_result_helper()110 fn test_option_result_helper() {
111 let result: Result<i32> = Ok(42);
112 let optional_result = option_result_helper(result, "Operation");
113 assert_eq!(optional_result, Some(42));
114
115 let result: Result<i32> = Err(Error::BadParameters);
116 let optional_result = option_result_helper(result, "Operation");
117 assert_eq!(optional_result, None);
118 }
119 }
120