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