Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaves committed Apr 23, 2021
1 parent 0ed5741 commit f634185
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 188 deletions.
95 changes: 47 additions & 48 deletions src/bluetooth/bluetooth_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,69 @@ use std::time::{Duration, Instant};
use async_std::future::timeout;
use async_std::task::block_on;

use rustable::Adapter;
use rustable::gatt;
use gatt::server::{AppWorker, Application};
use rustable::gatt;
use rustable::Adapter;

use btutils::timing::TimeService;
use btutils::messaging::{MsgChannelServ, ServerOptions};
use crate::{Error, LedMsg, Receiver};
use btutils::messaging::{MsgChannelServ, ServerOptions};
use btutils::timing::TimeService;

pub struct BluetoothReceiver {
app: AppWorker,
time: TimeService,
msg: MsgChannelServ
app: AppWorker,
time: TimeService,
msg: MsgChannelServ,
}

impl BluetoothReceiver {
pub async fn new(hci: u8) -> Result<Self, Error> {
let hci = Adapter::new(hci).await?;
let mut app = Application::new(&hci, "/io/maves/bt_recv");
let mut options = ServerOptions::new();
options.target_lt = Duration::from_millis(100);
let msg = MsgChannelServ::new(&mut app, &options);
let time = TimeService::new(&mut app);
let app = app.register().await.expect("Err unimplementd");
let hci = Adapter::new(hci).await?;
let mut app = Application::new(&hci, "/io/maves/bt_recv");
let mut options = ServerOptions::new();
options.target_lt = Duration::from_millis(100);
let msg = MsgChannelServ::new(&mut app, &options);
let time = TimeService::new(&mut app);
let app = app.register().await.expect("Err unimplementd");

Ok(Self {
msg,
time,
app
})
}
pub async fn shutdown(self) -> Result<(), Error> {
self.app.unregister().await.expect("Err unimplemented!");
Ok(())
}
Ok(Self { msg, time, app })
}
pub async fn shutdown(self) -> Result<(), Error> {
self.app.unregister().await.expect("Err unimplemented!");
Ok(())
}
}

impl Receiver for BluetoothReceiver {
fn cur_time(&self) -> u64 {
self.time.get_time()
self.time.get_time()
}
fn recv_to(&mut self, to_dur: Duration) -> Result<Vec<LedMsg>, Error> {
let deadline = Instant::now() + to_dur;
loop {
let to_dur = deadline.saturating_duration_since(Instant::now());
return match block_on(timeout(to_dur, self.msg.recv_msg())) {
Ok(Ok(data)) => match LedMsg::deserialize(&data, self.cur_time()) {
Ok(msgs) => Ok(msgs),
Err(_) => continue,
}
Ok(Err(_)) => Err(Error::Unrecoverable("BT message service has panicked!".into())),
Err(_) => Err(Error::Timeout("Message reception timed out.".into()))

};
}
let deadline = Instant::now() + to_dur;
loop {
let to_dur = deadline.saturating_duration_since(Instant::now());
return match block_on(timeout(to_dur, self.msg.recv_msg())) {
Ok(Ok(data)) => match LedMsg::deserialize(&data, self.cur_time()) {
Ok(msgs) => Ok(msgs),
Err(_) => continue,
},
Ok(Err(_)) => Err(Error::Unrecoverable(
"BT message service has panicked!".into(),
)),
Err(_) => Err(Error::Timeout("Message reception timed out.".into())),
};
}
}
fn recv(&mut self) -> Result<Vec<LedMsg>, Error> {
loop {
return match block_on(self.msg.recv_msg()) {
Ok(data) => match LedMsg::deserialize(&data, self.cur_time()) {
Ok(msgs) => Ok(msgs),
Err(_) => continue,
},
Err(_) => Err(Error::Unrecoverable("BT message service has panicked!".into())),
}
}
loop {
return match block_on(self.msg.recv_msg()) {
Ok(data) => match LedMsg::deserialize(&data, self.cur_time()) {
Ok(msgs) => Ok(msgs),
Err(_) => continue,
},
Err(_) => Err(Error::Unrecoverable(
"BT message service has panicked!".into(),
)),
};
}
}
}
151 changes: 75 additions & 76 deletions src/bluetooth/bluetooth_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,97 +3,96 @@ use futures::future::join;

use crate::{Error, LedMsg, Sender as LECPSender};

use rustable::{MAC, Adapter};

use btutils::timing::{TIME_SERV, TimeClient};
use btutils::messaging::{SERV_UUID as MSG_SERV, MsgChannelClient, ClientOptions as MsgOptions};
use rustable::{Adapter, MAC};

use btutils::messaging::{ClientOptions as MsgOptions, MsgChannelClient, SERV_UUID as MSG_SERV};
use btutils::timing::{TimeClient, TIME_SERV};

use std::time::{Duration, Instant};

use super::ECP_UUID;

pub struct BluetoothSender {
last_sync: Instant,
time: TimeClient,
msg: MsgChannelClient
last_sync: Instant,
time: TimeClient,
msg: MsgChannelClient,
}

impl BluetoothSender {
pub async fn new(hci: u8, mac: MAC) -> Result<Self, Error> {
let hci = Adapter::new(hci).await?;
let dev = hci.get_device(mac).await?;
let ecp_serv = dev.get_service(ECP_UUID).await?;
let includes = ecp_serv.get_includes().await?;
let mut time_serv = None;
let mut msg_serv = None;
for serv in includes {
if serv.uuid() == TIME_SERV {
time_serv = Some(serv);
} else if serv.uuid() == MSG_SERV {
msg_serv = Some(serv);
}
if time_serv.is_some() && msg_serv.is_some() {
break;
}
}
let (time_serv, msg_serv) = match (time_serv, msg_serv) {
(Some(s0), Some(s1)) => (s0, s1),
_ => return Err(rustable::Error::UnknownServ(ECP_UUID).into())
};

let time = TimeClient::from_service(time_serv).await.map_err(|_|
rustable::Error::UnknownServ(TIME_SERV)
)?;
let mut msg_options = MsgOptions::new(mac);
msg_options.target_lt = Duration::from_millis(100);
let msg = MsgChannelClient::from_service(msg_serv, msg_options).await.map_err(|_|
rustable::Error::UnknownServ(MSG_SERV)
)?;
let synced = time.do_client_sync().await.expect("Err unimplemented!");
if !synced {
unimplemented!("What to do if time sync fails?")
}
let last_sync = Instant::now();
Ok(Self {
time,
msg,
last_sync,
})
let hci = Adapter::new(hci).await?;
let dev = hci.get_device(mac).await?;
let ecp_serv = dev.get_service(ECP_UUID).await?;
let includes = ecp_serv.get_includes().await?;
let mut time_serv = None;
let mut msg_serv = None;
for serv in includes {
if serv.uuid() == TIME_SERV {
time_serv = Some(serv);
} else if serv.uuid() == MSG_SERV {
msg_serv = Some(serv);
}
if time_serv.is_some() && msg_serv.is_some() {
break;
}
}
let (time_serv, msg_serv) = match (time_serv, msg_serv) {
(Some(s0), Some(s1)) => (s0, s1),
_ => return Err(rustable::Error::UnknownServ(ECP_UUID).into()),
};

let time = TimeClient::from_service(time_serv)
.await
.map_err(|_| rustable::Error::UnknownServ(TIME_SERV))?;
let mut msg_options = MsgOptions::new(mac);
msg_options.target_lt = Duration::from_millis(100);
let msg = MsgChannelClient::from_service(msg_serv, msg_options)
.await
.map_err(|_| rustable::Error::UnknownServ(MSG_SERV))?;
let synced = time.do_client_sync().await.expect("Err unimplemented!");
if !synced {
unimplemented!("What to do if time sync fails?")
}
let last_sync = Instant::now();
Ok(Self {
time,
msg,
last_sync,
})
}
pub fn do_time_sync(&self) -> Result<(), Error> {
let synced = block_on(self.time.do_client_sync()).expect("Err unimplemented!");
if !synced {
unimplemented!();
} else {
Ok(())
}
}
pub fn shutdown(self) -> Result<(), Error> {
let t_shut = self.time.shutdown();
let m_shut = self.msg.shutdown();
let (res1, res2) = block_on(join(t_shut, m_shut));
res1.expect("Err unimplemented!");
res2.expect("Err unimplemented!");
Ok(())
}
pub fn do_time_sync(&self) -> Result<(), Error> {
let synced = block_on(self.time.do_client_sync()).expect("Err unimplemented!");
if !synced {
unimplemented!();
} else {
Ok(())
}
}
pub fn shutdown(self) -> Result<(), Error> {
let t_shut = self.time.shutdown();
let m_shut = self.msg.shutdown();
let (res1, res2) = block_on(join(t_shut, m_shut));
res1.expect("Err unimplemented!");
res2.expect("Err unimplemented!");
Ok(())
}
}

impl LECPSender for BluetoothSender {
fn send(&mut self, msgs: &[LedMsg]) -> Result<(), Error> {
let mut out_buf = [0; 512];
let mtu = self.msg.get_out_mtu();
let out_buf = &mut out_buf[..mtu as usize];
let mut msgs_sent = 0;
while msgs_sent < msgs.len() {
let to_send = &msgs[msgs_sent..];
let (sent, used) = LedMsg::serialize(to_send, out_buf, self.get_time());
msgs_sent += sent;
block_on(self.msg.send_msg(&out_buf[..used])).expect("Err unimplemented");
}
Ok(())
let mut out_buf = [0; 512];
let mtu = self.msg.get_out_mtu();
let out_buf = &mut out_buf[..mtu as usize];
let mut msgs_sent = 0;
while msgs_sent < msgs.len() {
let to_send = &msgs[msgs_sent..];
let (sent, used) = LedMsg::serialize(to_send, out_buf, self.get_time());
msgs_sent += sent;
block_on(self.msg.send_msg(&out_buf[..used])).expect("Err unimplemented");
}
Ok(())
}
fn get_time(&self) -> u64 {
self.time.get_time()
}
fn get_time(&self) -> u64 {
self.time.get_time()
}
}
3 changes: 1 addition & 2 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// Defines the `Color` and `ColorMap` that are used to set colors on the
/// Defines the `Color` and `ColorMap` that are used to set colors on the
/// receiver.
use std::ops::{Deref, DerefMut, Mul, MulAssign};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Color {
Expand Down
Loading

0 comments on commit f634185

Please sign in to comment.