summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChih-Yu Huang <akahuang@google.com>2022-03-04 14:09:25 +0900
committerChih-Yu Huang <akahuang@google.com>2022-03-04 19:38:35 +0900
commit932da8d01a3aebe386c647ffbcfce7a2237f0c93 (patch)
tree7204625b6a0654cacdc74d9e6ab042faa7ea6cd3 /src
parentc1e6af3ca32d539eec77ea0ed7e56e9c1da50d45 (diff)
downloaduwb-932da8d01a3aebe386c647ffbcfce7a2237f0c93.tar.gz
Add Dispatcher trait
Bug: 216552887 Test: libuwb_uci_rust_tests Change-Id: Ia6739f470fe6239b0ffdae65b78067a6ccfe608f
Diffstat (limited to 'src')
-rw-r--r--src/rust/uci/mod.rs37
1 files changed, 27 insertions, 10 deletions
diff --git a/src/rust/uci/mod.rs b/src/rust/uci/mod.rs
index a5749e2..d083d44 100644
--- a/src/rust/uci/mod.rs
+++ b/src/rust/uci/mod.rs
@@ -44,7 +44,7 @@ pub type UciResponseHandle = oneshot::Sender<UciResponse>;
type SyncUwbAdaptation = Arc<dyn UwbAdaptation + Send + Sync>;
// Commands sent from JNI.
-#[derive(Debug)]
+#[derive(Debug, PartialEq, Eq)]
pub enum JNICommand {
// Blocking UCI commands
UciGetCapsInfo,
@@ -455,15 +455,23 @@ impl<T: EventManager> Driver<T> {
}
}
+pub trait Dispatcher {
+ fn send_jni_command(&self, cmd: JNICommand) -> Result<()>;
+ fn block_on_jni_command(&self, cmd: JNICommand) -> Result<UciResponse>;
+ fn exit(&mut self) -> Result<()>;
+ fn get_device_info(&self) -> &Option<GetDeviceInfoRspPacket>;
+ fn set_device_info(&mut self, device_info: Option<GetDeviceInfoRspPacket>);
+}
+
// Controller for sending tasks for the native thread to handle.
-pub struct Dispatcher {
+pub struct DispatcherImpl {
cmd_sender: mpsc::UnboundedSender<(JNICommand, Option<UciResponseHandle>)>,
join_handle: task::JoinHandle<Result<()>>,
runtime: Runtime,
- pub device_info: Option<GetDeviceInfoRspPacket>,
+ device_info: Option<GetDeviceInfoRspPacket>,
}
-impl Dispatcher {
+impl DispatcherImpl {
pub fn new<T: 'static + EventManager + Send + Sync>(event_manager: T) -> Result<Self> {
let (rsp_sender, rsp_receiver) = mpsc::unbounded_channel::<HalCallback>();
// TODO when simplifying constructors, avoid spare runtime
@@ -501,17 +509,19 @@ impl Dispatcher {
.build()?;
let join_handle =
runtime.spawn(drive(adaptation, event_manager, cmd_receiver, rsp_receiver));
- Ok(Dispatcher { cmd_sender, join_handle, runtime, device_info: None })
+ Ok(DispatcherImpl { cmd_sender, join_handle, runtime, device_info: None })
}
+}
- pub fn send_jni_command(&self, cmd: JNICommand) -> Result<()> {
+impl Dispatcher for DispatcherImpl {
+ fn send_jni_command(&self, cmd: JNICommand) -> Result<()> {
self.cmd_sender.send((cmd, None))?;
Ok(())
}
// TODO: Consider implementing these separate for different commands so we can have more
// specific return types.
- pub fn block_on_jni_command(&self, cmd: JNICommand) -> Result<UciResponse> {
+ fn block_on_jni_command(&self, cmd: JNICommand) -> Result<UciResponse> {
let (tx, rx) = oneshot::channel();
self.cmd_sender.send((cmd, Some(tx)))?;
let ret = self.runtime.block_on(rx)?;
@@ -519,7 +529,7 @@ impl Dispatcher {
Ok(ret)
}
- pub fn exit(&mut self) -> Result<()> {
+ fn exit(&mut self) -> Result<()> {
self.send_jni_command(JNICommand::Exit)?;
match self.runtime.block_on(&mut self.join_handle) {
Err(err) if err.is_panic() => {
@@ -529,6 +539,13 @@ impl Dispatcher {
_ => Ok(()),
}
}
+
+ fn get_device_info(&self) -> &Option<GetDeviceInfoRspPacket> {
+ &self.device_info
+ }
+ fn set_device_info(&mut self, device_info: Option<GetDeviceInfoRspPacket>) {
+ self.device_info = device_info;
+ }
}
#[cfg(test)]
@@ -540,7 +557,7 @@ mod tests {
fn setup_dispatcher(
config_fn: fn(&mut Arc<MockUwbAdaptation>, &mut MockEventManager),
- ) -> Result<Dispatcher> {
+ ) -> Result<DispatcherImpl> {
// TODO: Remove this once we call it somewhere real.
logger::init(
logger::Config::default().with_tag_on_device("uwb").with_min_level(log::Level::Debug),
@@ -552,7 +569,7 @@ mod tests {
config_fn(&mut mock_adaptation, &mut mock_event_manager);
- Dispatcher::new_for_testing(
+ DispatcherImpl::new_for_testing(
mock_event_manager,
mock_adaptation as SyncUwbAdaptation,
rsp_receiver,