Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ccd01a27 authored by Charlie Boutier's avatar Charlie Boutier Committed by Automerger Merge Worker
Browse files

Merge "Uwb Hal: use fs::write instead of File::write" am: 995445a0

parents f9257b36 995445a0
Loading
Loading
Loading
Loading
+10 −17
Original line number Diff line number Diff line
@@ -6,8 +6,8 @@ use android_hardware_uwb::binder;
use async_trait::async_trait;
use binder::{Result, Strong};

use tokio::fs::File;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::fs::{self, File};
use tokio::io::AsyncReadExt;
use tokio::sync::Mutex;

use std::os::fd::AsRawFd;
@@ -22,7 +22,6 @@ enum State {
        callbacks: Strong<dyn IUwbClientCallback>,
        #[allow(dead_code)]
        tasks: tokio::task::JoinSet<()>,
        write: File,
    },
}

@@ -65,17 +64,11 @@ impl IUwbChipAsyncServer for UwbChip {
    async fn open(&self, callbacks: &Strong<dyn IUwbClientCallback>) -> Result<()> {
        log::debug!("open: {:?}", &self.path);

        let serial = File::open(&self.path)
        let mut serial = File::open(&self.path)
            .await
            .and_then(makeraw)
            .map_err(|_| binder::StatusCode::UNKNOWN_ERROR)?;

        let mut read = serial
            .try_clone()
            .await
            .map_err(|_| binder::StatusCode::UNKNOWN_ERROR)?;
        let write = serial;

        let mut state = self.state.lock().await;

        if let State::Closed = *state {
@@ -88,14 +81,16 @@ impl IUwbChipAsyncServer for UwbChip {
                    const UWB_HEADER_SIZE: usize = 4;

                    let mut buffer = vec![0; UWB_HEADER_SIZE];
                    read.read_exact(&mut buffer[0..UWB_HEADER_SIZE])
                    serial
                        .read_exact(&mut buffer[0..UWB_HEADER_SIZE])
                        .await
                        .unwrap();

                    let length = buffer[3] as usize + UWB_HEADER_SIZE;

                    buffer.resize(length, 0);
                    read.read_exact(&mut buffer[UWB_HEADER_SIZE..length])
                    serial
                        .read_exact(&mut buffer[UWB_HEADER_SIZE..length])
                        .await
                        .unwrap();

@@ -108,7 +103,6 @@ impl IUwbChipAsyncServer for UwbChip {
            *state = State::Opened {
                callbacks: callbacks.clone(),
                tasks,
                write,
            };

            Ok(())
@@ -155,11 +149,10 @@ impl IUwbChipAsyncServer for UwbChip {
    async fn sendUciMessage(&self, data: &[u8]) -> Result<i32> {
        log::debug!("sendUciMessage");

        if let State::Opened { write, .. } = &mut *self.state.lock().await {
            write
                .write(data)
        if let State::Opened { .. } = &mut *self.state.lock().await {
            fs::write(&self.path, data)
                .await
                .map(|written| written as i32)
                .map(|_| data.len() as i32)
                .map_err(|_| binder::StatusCode::UNKNOWN_ERROR.into())
        } else {
            Err(binder::ExceptionCode::ILLEGAL_STATE.into())