Skip to content

Commit

Permalink
Bigger firmware update packets, batch provider device manager request…
Browse files Browse the repository at this point in the history
…s so we can handle multiple providers at once
  • Loading branch information
JaciBrunning committed Jun 10, 2024
1 parent 2bd40cf commit da5cd5d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
6 changes: 3 additions & 3 deletions grapple-hook/src-tauri/src/devices/device_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ impl DeviceManager {
let send = super::SendWrapper(self.send.get(domain).unwrap().clone(), self.replies_waiting.get(domain).unwrap().clone());

let device = match (&id, device_type) {
(DeviceId::Dfu(..), DeviceType::Grapple(GrappleModelId::LaserCan)) => Box::new(FirmwareUpgradeDevice::<LaserCan>::new(send, info_arc.clone())),
(DeviceId::Dfu(..), DeviceType::Grapple(GrappleModelId::LaserCan)) => Box::new(FirmwareUpgradeDevice::<LaserCan>::new(send, info_arc.clone(), 8)),
(DeviceId::Serial(..), DeviceType::Grapple(GrappleModelId::LaserCan)) => LaserCan::maybe_gate(send, info_arc.clone(), LaserCan::new).await,
(DeviceId::Dfu(..), DeviceType::Grapple(GrappleModelId::FlexiCAN)) => Box::new(FirmwareUpgradeDevice::<FlexiCan>::new(send, info_arc.clone())),
(DeviceId::Dfu(..), DeviceType::Grapple(GrappleModelId::FlexiCAN)) => Box::new(FirmwareUpgradeDevice::<FlexiCan>::new(send, info_arc.clone(), 64)),
(DeviceId::Serial(..), DeviceType::Grapple(GrappleModelId::FlexiCAN)) => FlexiCan::maybe_gate(send, info_arc.clone(), FlexiCan::new).await,
(DeviceId::Dfu(..), DeviceType::Grapple(GrappleModelId::MitoCANdria)) => Box::new(FirmwareUpgradeDevice::<Mitocandria>::new(send, info_arc.clone())),
(DeviceId::Dfu(..), DeviceType::Grapple(GrappleModelId::MitoCANdria)) => Box::new(FirmwareUpgradeDevice::<Mitocandria>::new(send, info_arc.clone(), 64)),
(DeviceId::Serial(..), DeviceType::Grapple(GrappleModelId::MitoCANdria)) => Mitocandria::maybe_gate(send, info_arc.clone(), Mitocandria::new).await,
_ => unreachable!()
};
Expand Down
20 changes: 10 additions & 10 deletions grapple-hook/src-tauri/src/devices/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,34 +176,33 @@ pub struct FirmwareUpgradeDevice<T: FirmwareValidatingDevice> {
info: SharedInfo,
progress: Arc<RwLock<Option<f64>>>,
ack: Arc<Notify>,
chunk_size: usize,
_t: PhantomData<T>
}

impl<T: FirmwareValidatingDevice> FirmwareUpgradeDevice<T> {
pub fn new(sender: SendWrapper, info: SharedInfo) -> Self {
Self { sender, info, progress: Arc::new(RwLock::new(None)), ack: Arc::new(Notify::new()), _t: PhantomData }
pub fn new(sender: SendWrapper, info: SharedInfo, chunk_size: usize) -> Self {
Self { sender, info, progress: Arc::new(RwLock::new(None)), ack: Arc::new(Notify::new()), chunk_size, _t: PhantomData }
}

pub async fn field_upgrade_worker(sender: SendWrapper, id: u8, data: &[u8], progress: Arc<RwLock<Option<f64>>>, ack: Arc<Notify>) -> anyhow::Result<()> {
pub async fn field_upgrade_worker(sender: SendWrapper, id: u8, data: &[u8], progress: Arc<RwLock<Option<f64>>>, ack: Arc<Notify>, chunk_size: usize) -> anyhow::Result<()> {
*progress.write().await = Some(0.0);
let chunks = data.chunks(8);
let chunks = data.chunks(chunk_size);
let nchunks = chunks.len();
for (i, chunk) in chunks.enumerate() {
info!("Chunk {}", i);
let mut c = [0u8; 8];
c[0..chunk.len()].copy_from_slice(chunk);
info!("Chunk {} (len: {})", i, chunk.len());

sender.send(TaggedGrappleMessage::new(
id,
GrappleDeviceMessage::FirmwareUpdate(
GrappleFirmwareMessage::UpdatePart(AsymmetricCow(Cow::<Payload>::Borrowed(Into::into(c.as_ref()))).into_static())
GrappleFirmwareMessage::UpdatePart(AsymmetricCow(Cow::<Payload>::Borrowed(Into::into(chunk))).into_static())
)
)).await?;
tokio::time::timeout(Duration::from_millis(1000), ack.notified()).await?;
*progress.write().await = Some((i + 1) as f64 / (nchunks as f64) * 100.0);
}

*progress.write().await = Some(100.0);
*progress.write().await = Some(100.0);
sender.send(TaggedGrappleMessage::new(
id,
GrappleDeviceMessage::FirmwareUpdate(
Expand Down Expand Up @@ -234,10 +233,11 @@ impl<T: FirmwareValidatingDevice + Send + Sync> FirmwareUpgradeDevice<T> {
let progress = self.progress.clone();
let id = self.info.read().await.require_device_id()?;
let notify = self.ack.clone();
let chunk_size = self.chunk_size;

tokio::task::spawn(async move {
let data = data;
Self::field_upgrade_worker(sender, id, &data[..], progress, notify).await.ok();
Self::field_upgrade_worker(sender, id, &data[..], progress, notify, chunk_size).await.ok();
});
Ok(())
}
Expand Down
15 changes: 10 additions & 5 deletions grapple-hook/src/providers/ProviderManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ export default function ProviderManagerComponent(props: ProviderManagerProps) {
rpc<ProviderManagerRequest, ProviderManagerResponse, "providers">(invoke, "providers", {})
.then((providers) => {
setProviders(providers);
for (let provider of Object.keys(providers)) {
rpc<DeviceManagerRequest, DeviceManagerResponse, "devices">(device_manager_rpc(providers[provider].address), "devices", {})
.then(ds => setDevices(update(devices, { [provider]: { $set: ds } })))
.catch(addError)
}
let futs = Promise.all(Object.keys(providers).map(provider => rpc<DeviceManagerRequest, DeviceManagerResponse, "devices">(device_manager_rpc(providers[provider].address), "devices", {}).catch(addError)));
futs.then(vals => {
let devs = update(devices, {});
vals.forEach((v, i) => {
if (v) {
devs = update(devs, { [Object.keys(providers)[i]]: { $set: v } });
}
});
setDevices(devs);
});
})
.catch(addError)
}, 500);
Expand Down

0 comments on commit da5cd5d

Please sign in to comment.