eculocate

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 2f517e991d9f2a5ac1d6f8d37593bcb978d91c4b
parent ef1b608ac2e6c46419c7acc9cdc685cb8e8b3c2e
Author: Daniel Barlow <dan@telent.net>
Date:   Fri, 23 Jan 2026 19:17:19 +0000

remove tabs

Diffstat:
Mecu-esp32/src/wifi.rs | 228++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 114 insertions(+), 114 deletions(-)

diff --git a/ecu-esp32/src/wifi.rs b/ecu-esp32/src/wifi.rs @@ -63,37 +63,37 @@ fn dns_records(my_ip_address: [u8;4], serial: Option<usize>) -> Vec<mdns::Resour use crate::mdns::RData as R; let (hostname, ptr_name) = - match serial { - None => ( - String::from("eculocate.local"), - String::from("Bike._keihin._udp.local") - ), - Some(number) => ( - format!("eculocate{number}.local"), - format!("Bike ({number})._keihin._udp.local") - ) - }; + match serial { + None => ( + String::from("eculocate.local"), + String::from("Bike._keihin._udp.local") + ), + Some(number) => ( + format!("eculocate{number}.local"), + format!("Bike ({number})._keihin._udp.local") + ) + }; let records = [ - Resource::new(&hostname, R::A(my_ip_address)), - Resource::new( - &ptr_name, - R::Srv { - target: String::from(hostname), port: 5000, - weight: 0, priority: 0 - } - ), - Resource::new(&ptr_name, R::Txt(["txtvers=1".into()].into())), - Resource::new("_keihin._udp.local", R::Ptr(ptr_name)), - Resource::new(DISCOVERY_NAME, R::Ptr("_keihin._udp.local".into())) + Resource::new(&hostname, R::A(my_ip_address)), + Resource::new( + &ptr_name, + R::Srv { + target: String::from(hostname), port: 5000, + weight: 0, priority: 0 + } + ), + Resource::new(&ptr_name, R::Txt(["txtvers=1".into()].into())), + Resource::new("_keihin._udp.local", R::Ptr(ptr_name)), + Resource::new(DISCOVERY_NAME, R::Ptr("_keihin._udp.local".into())) ]; Vec::from(records) } pub async fn start_wifi(spawner: &Spawner, - wifi_peripheral: esp_hal::peripherals::WIFI<'static>, - flash_peripheral: esp_hal::peripherals::FLASH<'static>, + wifi_peripheral: esp_hal::peripherals::WIFI<'static>, + flash_peripheral: esp_hal::peripherals::FLASH<'static>, ecu: &mut crate::ecu::Ecu<'_>) { @@ -134,7 +134,7 @@ pub async fn start_wifi(spawner: &Spawner, loop { if let Some(config) = stack.config_v4() { println!("Got IP address: {}", config.address); - my_address = config.address.address().octets(); + my_address = config.address.address().octets(); break; } Timer::after(Duration::from_millis(500)).await; @@ -143,19 +143,19 @@ pub async fn start_wifi(spawner: &Spawner, let mut serial = None; join( - join( + join( crate::streamer::loop_udp_thing(stack, ecu), - run_tcp_handler(stack, flash_peripheral), - ), + run_tcp_handler(stack, flash_peripheral), + ), async { loop { - let mdns_thread = mdns::responder( - stack, - dns_records(my_address, serial) - ); - mdns_thread.await; - serial = match serial { None => Some(2), Some(n) => Some(n+1) }; - info!("probe failed, restarting with incremented serial {:?}", serial); + let mdns_thread = mdns::responder( + stack, + dns_records(my_address, serial) + ); + mdns_thread.await; + serial = match serial { None => Some(2), Some(n) => Some(n+1) }; + info!("probe failed, restarting with incremented serial {:?}", serial); Timer::after(Duration::from_millis(5000)).await; }; } @@ -217,23 +217,23 @@ async fn net_task(mut runner: Runner<'static, WifiDevice<'static>>) { } async fn read_all(socket: &mut embassy_net::tcp::TcpSocket<'_>, - buf: &mut [u8], - len: usize) -> usize { + buf: &mut [u8], + len: usize) -> usize { let mut n = 0; while n < len { - match socket.read(&mut buf[n..len]).await { - Ok(0) => { - warn!("read EOF"); - break; - }, - Ok(count) => { - n = n + count; - } - Err(e) => { - warn!("read err {:?}", e); - return 0; // ew - } - } + match socket.read(&mut buf[n..len]).await { + Ok(0) => { + warn!("read EOF"); + break; + }, + Ok(count) => { + n = n + count; + } + Err(e) => { + warn!("read err {:?}", e); + return 0; // ew + } + } } n } @@ -242,18 +242,18 @@ const ERASE_BLOCK_BYTES : usize = 4096; const ED25519_SIGNATURE_BYTES : usize = 64; async fn write_ota(ota : &mut Ota<FlashStorage<'_>>, - sock: &mut embassy_net::tcp::TcpSocket<'_>) { + sock: &mut embassy_net::tcp::TcpSocket<'_>) { let mut buf = [0u8; ERASE_BLOCK_BYTES + ED25519_SIGNATURE_BYTES]; let mut buf_offset = 0; let mut signed = true; let len = { - let mut len_bytes = [0u8; 4]; - if read_all(sock, &mut len_bytes, 4).await == 4 { - u32::from_be_bytes(len_bytes) - } else { - 0 - } + let mut len_bytes = [0u8; 4]; + if read_all(sock, &mut len_bytes, 4).await == 4 { + u32::from_be_bytes(len_bytes) + } else { + 0 + } }; let verifying_key_bytes = include_bytes!("../../pubkey.bin"); @@ -267,51 +267,51 @@ async fn write_ota(ota : &mut Ota<FlashStorage<'_>>, let mut remaining = len; while remaining > 0 { - // info!("remaining {}, offset {}", remaining, buf_offset); - match sock.read(&mut buf[buf_offset..]).await { + // info!("remaining {}, offset {}", remaining, buf_offset); + match sock.read(&mut buf[buf_offset..]).await { Ok(0) => { info!("unexpected eof on read"); break; } - Ok(chunksize) => { + Ok(chunksize) => { // info!("read {} at {}, {} remaining", chunksize, buf_offset, remaining); buf_offset += chunksize; remaining -= chunksize as u32; } - Err(x) => { - warn!("error reading ota image: {:?}", x); - break; - } + Err(x) => { + warn!("error reading ota image: {:?}", x); + break; + } } - if remaining <= 0 || buf_offset == ED25519_SIGNATURE_BYTES + ERASE_BLOCK_BYTES { - let (sig, chunk) = buf.split_at(ED25519_SIGNATURE_BYTES); - let signature: Signature = Signature::try_from(sig).expect("sig"); - signed = signed && verifying_key.verify(chunk, &signature).is_ok(); + if remaining <= 0 || buf_offset == ED25519_SIGNATURE_BYTES + ERASE_BLOCK_BYTES { + let (sig, chunk) = buf.split_at(ED25519_SIGNATURE_BYTES); + let signature: Signature = Signature::try_from(sig).expect("sig"); + signed = signed && verifying_key.verify(chunk, &signature).is_ok(); - if signed { - if let Err(_) = ota.ota_write_chunk(&buf[ED25519_SIGNATURE_BYTES..buf_offset]) { + if signed { + if let Err(_) = ota.ota_write_chunk(&buf[ED25519_SIGNATURE_BYTES..buf_offset]) { break; - } - } else { - // accept the rest of the update, just don't flash it. - warn!("signature invalid with {remaining} left"); - } - buf_offset = 0; - } + } + } else { + // accept the rest of the update, just don't flash it. + warn!("signature invalid with {remaining} left"); + } + buf_offset = 0; + } } if remaining == 0 { - info!("wrote all bytes"); - info!("write {:?}", sock.write(b"BYE\n").await); - let _ = sock.flush().await; - sock.close(); - if signed && !ota.ota_flush(false, false).is_ok() { - error!("ota failed"); - } - Timer::after_millis(1000).await; - esp_hal::system::software_reset(); + info!("wrote all bytes"); + info!("write {:?}", sock.write(b"BYE\n").await); + let _ = sock.flush().await; + sock.close(); + if signed && !ota.ota_flush(false, false).is_ok() { + error!("ota failed"); + } + Timer::after_millis(1000).await; + esp_hal::system::software_reset(); } else { - warn!("stream ended early or flash failed with {} bytes left", remaining); + warn!("stream ended early or flash failed with {} bytes left", remaining); } } @@ -329,37 +329,37 @@ async fn run_tcp_handler(stack : Stack<'_>, flash_peripheral: esp_hal::periphera loop { - let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); - socket.set_timeout(Some(Duration::from_secs(10))); + let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); + socket.set_timeout(Some(Duration::from_secs(10))); - if let Err(e) = socket.accept(5000).await { - warn!("error accepting connection: {:?}", e); - continue; - } + if let Err(e) = socket.accept(5000).await { + warn!("error accepting connection: {:?}", e); + continue; + } info!("Received connection from {:?}", socket.remote_endpoint()); - let n = read_all(&mut socket, &mut buf, 4).await; - - if n == 4 { - info!("got a thing"); - match &buf[0..4] { - b"ROM0" => { - info!("OTA request"); - write_ota(&mut ota, &mut socket).await; - }, - b"KEY0" => { - info!("sessionkey"); - if read_all(&mut socket, &mut buf, 96).await == 64 { - let (_sig, key) = buf.split_at(64); - register_session_key(&key); - } - }, - _ => { - warn!("unrecognised command"); - } - - } - } + let n = read_all(&mut socket, &mut buf, 4).await; + + if n == 4 { + info!("got a thing"); + match &buf[0..4] { + b"ROM0" => { + info!("OTA request"); + write_ota(&mut ota, &mut socket).await; + }, + b"KEY0" => { + info!("sessionkey"); + if read_all(&mut socket, &mut buf, 96).await == 64 { + let (_sig, key) = buf.split_at(64); + register_session_key(&key); + } + }, + _ => { + warn!("unrecognised command"); + } + + } + } } }