Skip to content

Commit

Permalink
fix(SourceDevice): implement source device client for channel communi…
Browse files Browse the repository at this point in the history
…cation
  • Loading branch information
ShadowApex committed Jul 10, 2024
1 parent a379d14 commit 9a1dabe
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 319 deletions.
212 changes: 27 additions & 185 deletions src/dbus/interface/source/iio_imu.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
use std::error::Error;

use crate::iio::device::Device;
use tokio::sync::mpsc::Sender;
use crate::{iio::device::Device, input::source::client::SourceDeviceClient};
use zbus::{fdo, Connection};
use zbus_macros::interface;

use crate::input::source::{iio::get_dbus_path, SourceCommand};
use crate::input::source::iio::get_dbus_path;

/// DBusInterface exposing information about a HIDRaw device
pub struct SourceIioImuInterface {
info: Device,
tx: Sender<SourceCommand>,
_info: Device,
source_device: SourceDeviceClient,
}

impl SourceIioImuInterface {
pub fn new(info: Device, tx: Sender<SourceCommand>) -> SourceIioImuInterface {
SourceIioImuInterface { info, tx }
pub fn new(info: Device, source_device: SourceDeviceClient) -> SourceIioImuInterface {
SourceIioImuInterface {
_info: info,
source_device,
}
}

/// Creates a new instance of the source hidraw interface on DBus. Returns
/// a structure with information about the source device.
pub async fn listen_on_dbus(
conn: Connection,
info: Device,
tx: Sender<SourceCommand>,
source_device: SourceDeviceClient,
) -> Result<(), Box<dyn Error>> {
let Some(id) = info.id.clone() else {
return Err("Failed to get ID of IIO device".into());
};
let path = get_dbus_path(id);

let iface = SourceIioImuInterface::new(info, tx);
let iface = SourceIioImuInterface::new(info, source_device);
tokio::task::spawn(async move {
log::debug!("Starting dbus interface: {path}");
let result = conn.object_server().at(path.clone(), iface).await;
Expand All @@ -48,263 +50,103 @@ impl SourceIioImuInterface {
impl SourceIioImuInterface {
#[zbus(property)]
async fn accel_sample_rate(&self) -> fdo::Result<f64> {
let (tx, rx) = std::sync::mpsc::channel();
if let Err(e) = self
.tx
.send(SourceCommand::GetSampleRate("accel".to_string(), tx))
.await
{
return Err(fdo::Error::Failed(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(fdo::Error::Failed(
"Channel closed with no response.".to_string(),
));
};
match response {
match self.source_device.get_sample_rate("accel").await {
Ok(result) => Ok(result),
Err(e) => Err(fdo::Error::Failed(e.to_string())),
}
}

#[zbus(property)]
async fn accel_sample_rates_avail(&self) -> fdo::Result<Vec<f64>> {
let (tx, rx) = std::sync::mpsc::channel();
if let Err(e) = self
.tx
.send(SourceCommand::GetSampleRatesAvail("accel".to_string(), tx))
.await
{
return Err(fdo::Error::Failed(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(fdo::Error::Failed(
"Channel closed with no response.".to_string(),
));
};
match response {
match self.source_device.get_sample_rates_avail("accel").await {
Ok(result) => Ok(result),
Err(e) => Err(fdo::Error::Failed(e.to_string())),
}
}

#[zbus(property)]
async fn angvel_sample_rate(&self) -> fdo::Result<f64> {
let (tx, rx) = std::sync::mpsc::channel();
if let Err(e) = self
.tx
.send(SourceCommand::GetSampleRate("gyro".to_string(), tx))
.await
{
return Err(fdo::Error::Failed(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(fdo::Error::Failed(
"Channel closed with no response.".to_string(),
));
};
match response {
match self.source_device.get_sample_rate("gyro").await {
Ok(result) => Ok(result),
Err(e) => Err(fdo::Error::Failed(e.to_string())),
}
}

#[zbus(property)]
async fn angvel_sample_rates_avail(&self) -> fdo::Result<Vec<f64>> {
let (tx, rx) = std::sync::mpsc::channel();
if let Err(e) = self
.tx
.send(SourceCommand::GetSampleRatesAvail("gyro".to_string(), tx))
.await
{
return Err(fdo::Error::Failed(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(fdo::Error::Failed(
"Channel closed with no response.".to_string(),
));
};
match response {
match self.source_device.get_sample_rates_avail("gyro").await {
Ok(result) => Ok(result),
Err(e) => Err(fdo::Error::Failed(e.to_string())),
}
}

#[zbus(property)]
async fn set_accel_sample_rate(&self, sample_rate: f64) -> zbus::Result<()> {
let (tx, rx) = std::sync::mpsc::channel();

if let Err(e) = self
.tx
.send(SourceCommand::SetSampleRate(
"accel".to_string(),
sample_rate,
tx,
))
match self
.source_device
.set_sample_rate("accel", sample_rate)
.await
{
return Err(zbus::Error::Failure(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(zbus::Error::Failure(
"Channel closed with no response.".to_string(),
));
};
match response {
Ok(result) => Ok(result),
Err(e) => Err(zbus::Error::Failure(e.to_string())),
}
}

#[zbus(property)]
async fn set_angvel_sample_rate(&self, sample_rate: f64) -> zbus::Result<()> {
let (tx, rx) = std::sync::mpsc::channel();

if let Err(e) = self
.tx
.send(SourceCommand::SetSampleRate(
"gyro".to_string(),
sample_rate,
tx,
))
match self
.source_device
.set_sample_rate("gyro", sample_rate)
.await
{
return Err(zbus::Error::Failure(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(zbus::Error::Failure(
"Channel closed with no response.".to_string(),
));
};
match response {
Ok(result) => Ok(result),
Err(e) => Err(zbus::Error::Failure(e.to_string())),
}
}
//
#[zbus(property)]
async fn accel_scale(&self) -> fdo::Result<f64> {
let (tx, rx) = std::sync::mpsc::channel();
if let Err(e) = self
.tx
.send(SourceCommand::GetScale("accel".to_string(), tx))
.await
{
return Err(fdo::Error::Failed(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(fdo::Error::Failed(
"Channel closed with no response.".to_string(),
));
};
match response {
match self.source_device.get_scale("accel").await {
Ok(result) => Ok(result),
Err(e) => Err(fdo::Error::Failed(e.to_string())),
}
}

#[zbus(property)]
async fn accel_scales_avail(&self) -> fdo::Result<Vec<f64>> {
let (tx, rx) = std::sync::mpsc::channel();
if let Err(e) = self
.tx
.send(SourceCommand::GetScalesAvail("accel".to_string(), tx))
.await
{
return Err(fdo::Error::Failed(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(fdo::Error::Failed(
"Channel closed with no response.".to_string(),
));
};
match response {
match self.source_device.get_scales_available("accel").await {
Ok(result) => Ok(result),
Err(e) => Err(fdo::Error::Failed(e.to_string())),
}
}

#[zbus(property)]
async fn angvel_scale(&self) -> fdo::Result<f64> {
let (tx, rx) = std::sync::mpsc::channel();
if let Err(e) = self
.tx
.send(SourceCommand::GetScale("gyro".to_string(), tx))
.await
{
return Err(fdo::Error::Failed(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(fdo::Error::Failed(
"Channel closed with no response.".to_string(),
));
};
match response {
match self.source_device.get_scale("gyro").await {
Ok(result) => Ok(result),
Err(e) => Err(fdo::Error::Failed(e.to_string())),
}
}

#[zbus(property)]
async fn angvel_scales_avail(&self) -> fdo::Result<Vec<f64>> {
let (tx, rx) = std::sync::mpsc::channel();
if let Err(e) = self
.tx
.send(SourceCommand::GetScalesAvail("gyro".to_string(), tx))
.await
{
return Err(fdo::Error::Failed(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(fdo::Error::Failed(
"Channel closed with no response.".to_string(),
));
};
match response {
match self.source_device.get_scales_available("gyro").await {
Ok(result) => Ok(result),
Err(e) => Err(fdo::Error::Failed(e.to_string())),
}
}

#[zbus(property)]
async fn set_accel_scale(&self, scale: f64) -> zbus::Result<()> {
let (tx, rx) = std::sync::mpsc::channel();

if let Err(e) = self
.tx
.send(SourceCommand::SetScale("accel".to_string(), scale, tx))
.await
{
return Err(zbus::Error::Failure(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(zbus::Error::Failure(
"Channel closed with no response.".to_string(),
));
};
match response {
match self.source_device.set_scale("accel", scale).await {
Ok(result) => Ok(result),
Err(e) => Err(zbus::Error::Failure(e.to_string())),
}
}

#[zbus(property)]
async fn set_angvel_scale(&self, scale: f64) -> zbus::Result<()> {
let (tx, rx) = std::sync::mpsc::channel();

if let Err(e) = self
.tx
.send(SourceCommand::SetScale("gyro".to_string(), scale, tx))
.await
{
return Err(zbus::Error::Failure(e.to_string()));
}
let Ok(response) = rx.recv() else {
return Err(zbus::Error::Failure(
"Channel closed with no response.".to_string(),
));
};
match response {
match self.source_device.set_scale("gyro", scale).await {
Ok(result) => Ok(result),
Err(e) => Err(zbus::Error::Failure(e.to_string())),
}
Expand Down
Loading

0 comments on commit 9a1dabe

Please sign in to comment.