UWB: Cleanup in default HAL

Resolve comments on I8ad8072fe01c8d1f466d8debf0ccf5831b3eddf8

Bug: 302005209
Test: m android.hardware.uwb-service
Change-Id: I1c4348fa7548441555c46cac88c2e5787d764f03
diff --git a/uwb/aidl/default/Android.bp b/uwb/aidl/default/Android.bp
index 2b7ef57..916646c 100644
--- a/uwb/aidl/default/Android.bp
+++ b/uwb/aidl/default/Android.bp
@@ -15,6 +15,7 @@
     prefer_rlib: true,
     rustlibs: [
         "android.hardware.uwb-V1-rust",
+        "liblibc",
         "liblogger",
         "liblog_rust",
         "libbinder_rs",
diff --git a/uwb/aidl/default/src/uwb_chip.rs b/uwb/aidl/default/src/uwb_chip.rs
index b63aabe..2b8e481 100644
--- a/uwb/aidl/default/src/uwb_chip.rs
+++ b/uwb/aidl/default/src/uwb_chip.rs
@@ -6,7 +6,6 @@
 use async_trait::async_trait;
 use binder::{DeathRecipient, IBinder, Result, Strong};
 
-use log::info;
 use std::sync::Arc;
 use tokio::io::unix::AsyncFd;
 use tokio::select;
@@ -16,12 +15,13 @@
 use std::fs::{File, OpenOptions};
 use std::io::{self, Read, Write};
 use std::os::fd::AsRawFd;
+use std::os::unix::fs::OpenOptionsExt;
 
 enum State {
     Closed,
     Opened {
         callbacks: Strong<dyn IUwbClientCallback>,
-        _handle: tokio::task::JoinHandle<()>,
+        handle: tokio::task::JoinHandle<()>,
         serial: File,
         death_recipient: DeathRecipient,
         token: CancellationToken,
@@ -46,12 +46,12 @@
 
 impl State {
     /// Terminate the reader task.
-    #[allow(dead_code)]
-    fn close(&mut self) -> Result<()> {
-        if let State::Opened { ref mut token, ref callbacks, ref mut death_recipient, .. } = *self {
+    async fn close(&mut self) -> Result<()> {
+        if let State::Opened { ref mut token, ref callbacks, ref mut death_recipient, ref mut handle, .. } = *self {
             log::info!("waiting for task cancellation");
             callbacks.as_binder().unlink_to_death(death_recipient)?;
             token.cancel();
+            handle.await.unwrap();
             log::info!("task successfully cancelled");
             callbacks.onHalEvent(UwbEvent::CLOSE_CPLT, UwbStatus::OK)?;
             *self = State::Closed;
@@ -69,11 +69,6 @@
     cfmakeraw(&mut attrs);
     tcsetattr(fd, SetArg::TCSANOW, &attrs)?;
 
-    // Configure the file descriptor as non blocking.
-    use nix::fcntl::*;
-    let flags = OFlag::from_bits(fcntl(fd, FcntlArg::F_GETFL)?).unwrap();
-    fcntl(fd, FcntlArg::F_SETFL(flags | OFlag::O_NONBLOCK))?;
-
     Ok(file)
 }
 
@@ -114,6 +109,7 @@
             .read(true)
             .write(true)
             .create(false)
+            .custom_flags(libc::O_NONBLOCK)
             .open(&self.path)
             .and_then(makeraw)
             .map_err(|_| binder::StatusCode::UNKNOWN_ERROR)?;
@@ -122,7 +118,10 @@
         let mut death_recipient = DeathRecipient::new(move || {
             let mut state = state_death_recipient.blocking_lock();
             log::info!("Uwb service has died");
-            state.close().unwrap();
+            if let State::Opened { ref mut token, .. } = *state {
+                token.cancel();
+                *state = State::Closed;
+            }
         });
 
         callbacks.as_binder().link_to_death(&mut death_recipient)?;
@@ -137,7 +136,7 @@
             .map_err(|_| binder::StatusCode::UNKNOWN_ERROR)?;
 
         let join_handle = tokio::task::spawn(async move {
-            info!("UCI reader task started");
+            log::info!("UCI reader task started");
             let mut reader = AsyncFd::new(reader).unwrap();
 
             loop {
@@ -159,7 +158,7 @@
                     // the OS will only notify Tokio when the file descriptor
                     // transitions from not-ready to ready. For this to work
                     // you should first try to read or write and only poll for
-                    // readiness if that fails with an error of 
+                    // readiness if that fails with an error of
                     // std::io::ErrorKind::WouldBlock.
                     match reader.get_mut().read(&mut buffer) {
                         Ok(0) => {
@@ -173,7 +172,7 @@
 
                     let mut guard = select! {
                         _ = cloned_token.cancelled() => {
-                            info!("task is cancelled!");
+                            log::info!("task is cancelled!");
                             return;
                         },
                         result = reader.readable() => result.unwrap()
@@ -199,7 +198,7 @@
 
         *state = State::Opened {
             callbacks: callbacks.clone(),
-            _handle: join_handle,
+            handle: join_handle,
             serial,
             death_recipient,
             token,
@@ -214,7 +213,7 @@
         let mut state = self.state.lock().await;
 
         if matches!(*state, State::Opened { .. }) {
-            state.close()
+            state.close().await
         } else {
             Err(binder::ExceptionCode::ILLEGAL_STATE.into())
         }