1 // Copyright 2021, 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 //! Rootcanal HAL
16 //! This connects to "rootcanal" which provides a simulated
17 //! Nfc chip as well as a simulated environment.
18 
19 use log::{debug, LevelFilter};
20 use logger::{self, Config};
21 use nfc_rnci::api::NciApi;
22 
23 /// Result type
24 type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
25 
26 /// The NFC response callback
nfc_callback(kind: u16, val: &[u8])27 pub fn nfc_callback(kind: u16, val: &[u8]) {
28     debug!("Callback#{} -> {:?}", kind, val);
29 }
30 
31 #[tokio::main]
main() -> Result<()>32 async fn main() -> Result<()> {
33     let set_tlvs: [u8; 10] = [3, 0xa1, 1, 0x1e, 0xa2, 1, 0x19, 0x80, 1, 0x01];
34     let get_tlvs: [u8; 3] = [2, 0x52, 0x80];
35     logger::init(Config::default().with_tag_on_device("lnfc").with_max_level(LevelFilter::Trace));
36 
37     let mut nci = NciApi::new();
38     nci.nfc_enable(nfc_callback).await;
39     nci.nfc_init().await?;
40     let lmrts = nci.nfc_get_lmrt_size().await;
41     debug!("LMRT size:{}", lmrts);
42     let status = nci.nfc_set_config(&set_tlvs).await?;
43     debug!("SET_CONFIG status:{}", status);
44     let status = nci.nfc_get_config(&get_tlvs).await?;
45     debug!("GET_CONFIG status:{}", status);
46     nci.nfc_disable().await;
47     nci.nfc_enable(nfc_callback).await;
48     nci.nfc_init().await?;
49     let status = nci.nfc_get_config(&get_tlvs).await?;
50     debug!("GET_CONFIG status:{}", status);
51     nci.nfc_disable().await;
52     Ok(())
53 }
54