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 //! A simple example for the usage of the uwb_core library.
16
17 use log::debug;
18
19 use uwb_core::error::{Error as UwbError, Result as UwbResult};
20 use uwb_core::service::{
21 default_runtime, NopUwbServiceCallback, UwbServiceBuilder, UwbServiceCallbackSendBuilder,
22 };
23 use uwb_core::uci::{NopUciHal, NopUciLoggerFactory};
24
main()25 fn main() {
26 env_logger::init();
27
28 // The UwbService needs an outlived Tokio Runtime.
29 let runtime = default_runtime().unwrap();
30 // Initialize the UWB service.
31 let service = UwbServiceBuilder::new()
32 .runtime_handle(runtime.handle().to_owned())
33 .callback_builder(UwbServiceCallbackSendBuilder::new(NopUwbServiceCallback {}))
34 .uci_hal(NopUciHal {})
35 .uci_logger_factory(NopUciLoggerFactory {})
36 .build()
37 .unwrap();
38
39 // Call the public methods of UWB service under tokio runtime.
40 let result: UwbResult<()> = service.enable();
41
42 // Enumerate the error code for backward-compatibility.
43 // WARNING: Modifying or removing the current fields are prohibited in general,
44 // unless we could confirm that there is no client using the modified field.
45 if let Err(err) = result {
46 match err {
47 UwbError::BadParameters => {}
48 UwbError::MaxSessionsExceeded => {}
49 UwbError::MaxRrRetryReached => {}
50 UwbError::ProtocolSpecific => {}
51 UwbError::RemoteRequest => {}
52 UwbError::Timeout => {}
53 UwbError::CommandRetry => {}
54 UwbError::DuplicatedSessionId => {}
55 UwbError::RegulationUwbOff => {}
56 UwbError::Unknown => {}
57
58 // UwbError is non_exhaustive so we need to add a wild branch here.
59 // With this wild branch, adding a new enum field doesn't break the build.
60 _ => debug!("Received unknown error: {:?}", err),
61 }
62 }
63 }
64