Skip to content

Commit

Permalink
chore(*): replace fugit with embassy-time (#67)
Browse files Browse the repository at this point in the history
* Use latest atat, with new urc channel

* Replace fugit & fugit-timer with embassy-time

* Adjust number of URC subscribers
  • Loading branch information
MathiasKoch authored Sep 12, 2023
1 parent 3020a8a commit 7517cdf
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 141 deletions.
4 changes: 2 additions & 2 deletions ublox-short-range/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ defmt = { version = "0.3" }
embedded-hal = "=1.0.0-rc.1"
embedded-io = "0.5"
embedded-nal = "0.6.0"
fugit = { version = "0.3", features = ["defmt"] }
fugit-timer = "0.1.2"

embassy-time = "0.1"

[dev-dependencies]
embedded-io = "0.4"
Expand Down
21 changes: 21 additions & 0 deletions ublox-short-range/src/blocking_timer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use embassy_time::{Duration, Instant};

pub struct BlockingTimer {
expires_at: Instant,
}

impl BlockingTimer {
pub fn after(duration: Duration) -> Self {
Self {
expires_at: Instant::now() + duration,
}
}

pub fn wait(self) {
loop {
if self.expires_at <= Instant::now() {
break;
}
}
}
}
Empty file.
119 changes: 34 additions & 85 deletions ublox-short-range/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::str::FromStr;

use crate::{
blocking_timer::BlockingTimer,
command::{
custom_digest::EdmDigester,
data_mode::{
Expand Down Expand Up @@ -34,9 +35,9 @@ use crate::{
};
use atat::{blocking::AtatClient, AtatUrcChannel, UrcSubscription};
use defmt::{debug, error, trace};
use embassy_time::{Duration, Instant};
use embedded_hal::digital::OutputPin;
use embedded_nal::{nb, IpAddr, Ipv4Addr, SocketAddr};
use fugit::ExtU32;
use embedded_nal::{IpAddr, Ipv4Addr, SocketAddr};
use ublox_sockets::{
udp_listener::UdpListener, AnySocket, SocketHandle, SocketSet, SocketType, TcpSocket, TcpState,
UdpSocket, UdpState,
Expand All @@ -50,7 +51,6 @@ pub enum SerialMode {

#[derive(PartialEq, Copy, Clone)]
pub enum DNSState {
NotResolving,
Resolving,
Resolved(IpAddr),
Error(PingError),
Expand Down Expand Up @@ -150,66 +150,42 @@ pub fn new_socket_num<const N: usize, const L: usize>(sockets: &SocketSet<N, L>)
Ok(num)
}

pub(crate) const URC_CAPACITY: usize = 1 + 3;
pub(crate) const URC_CAPACITY: usize = 2;
pub(crate) const URC_SUBSCRIBERS: usize = 1;

pub struct UbloxClient<
'buf,
'sub,
AtCl,
AtUrcCh,
CLK,
RST,
const TIMER_HZ: u32,
const N: usize,
const L: usize,
> where
CLK: fugit_timer::Timer<TIMER_HZ>,
pub struct UbloxClient<'buf, 'sub, AtCl, AtUrcCh, RST, const N: usize, const L: usize>
where
RST: OutputPin,
{
pub(crate) module_started: bool,
pub(crate) initialized: bool,
serial_mode: SerialMode,
pub dns_table: DNSTable,
pub(crate) dns_table: DNSTable,
pub(crate) wifi_connection: Option<WifiConnection>,
pub(crate) wifi_config_active_on_startup: Option<u8>,
pub(crate) client: AtCl,
pub(crate) config: Config<RST>,
pub(crate) sockets: Option<&'static mut SocketSet<N, L>>,
pub(crate) security_credentials: SecurityCredentials,
pub(crate) timer: CLK,
pub(crate) socket_map: SocketMap,
pub(crate) udp_listener: UdpListener<4, N>,
pub(crate) udp_listener: UdpListener<2, N>,
urc_subscription: UrcSubscription<'sub, EdmEvent, URC_CAPACITY, URC_SUBSCRIBERS>,
urc_channel: &'buf AtUrcCh,
}

impl<
'buf,
'sub,
W,
CLK,
RST,
const TIMER_HZ: u32,
const INGRESS_BUF_SIZE: usize,
const N: usize,
const L: usize,
>
impl<'buf, 'sub, W, RST, const INGRESS_BUF_SIZE: usize, const N: usize, const L: usize>
UbloxClient<
'buf,
'sub,
atat::blocking::Client<'buf, W, INGRESS_BUF_SIZE>,
UbloxWifiUrcChannel,
CLK,
RST,
TIMER_HZ,
N,
L,
>
where
'buf: 'sub,
W: embedded_io::Write,
CLK: fugit_timer::Timer<TIMER_HZ>,
RST: OutputPin,
{
/// Create new u-blox device
Expand All @@ -219,29 +195,27 @@ where
pub fn from_buffers(
buffers: &'buf UbloxWifiBuffers<INGRESS_BUF_SIZE>,
tx: W,
timer: CLK,
config: Config<RST>,
) -> (UbloxWifiIngress<INGRESS_BUF_SIZE>, Self) {
let (ingress, client) =
buffers.split_blocking(tx, EdmDigester::default(), atat::Config::default());

(
ingress,
UbloxClient::new(client, &buffers.urc_channel, timer, config),
UbloxClient::new(client, &buffers.urc_channel, config),
)
}
}

impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize>
UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L>
impl<'buf, 'sub, AtCl, AtUrcCh, RST, const N: usize, const L: usize>
UbloxClient<'buf, 'sub, AtCl, AtUrcCh, RST, N, L>
where
'buf: 'sub,
AtCl: AtatClient,
AtUrcCh: AtatUrcChannel<EdmEvent, URC_CAPACITY, URC_SUBSCRIBERS>,
CLK: fugit_timer::Timer<TIMER_HZ>,
RST: OutputPin,
{
pub fn new(client: AtCl, urc_channel: &'buf AtUrcCh, timer: CLK, config: Config<RST>) -> Self {
pub fn new(client: AtCl, urc_channel: &'buf AtUrcCh, config: Config<RST>) -> Self {
let urc_subscription = urc_channel.subscribe().unwrap();
UbloxClient {
module_started: false,
Expand All @@ -254,7 +228,6 @@ where
config,
sockets: None,
security_credentials: SecurityCredentials::default(),
timer,
socket_map: SocketMap::default(),
udp_listener: UdpListener::new(),
urc_subscription,
Expand All @@ -263,12 +236,11 @@ where
}
}

impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize>
UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L>
impl<'buf, 'sub, AtCl, AtUrcCh, RST, const N: usize, const L: usize>
UbloxClient<'buf, 'sub, AtCl, AtUrcCh, RST, N, L>
where
'buf: 'sub,
AtCl: AtatClient,
CLK: fugit_timer::Timer<TIMER_HZ>,
RST: OutputPin,
{
pub fn set_socket_storage(&mut self, socket_set: &'static mut SocketSet<N, L>) {
Expand Down Expand Up @@ -299,8 +271,7 @@ where

while self.serial_mode != SerialMode::ExtendedData {
self.send_internal(&SwitchToEdmCommand, true).ok();
self.timer.start(100.millis()).map_err(|_| Error::Timer)?;
nb::block!(self.timer.wait()).map_err(|_| Error::Timer)?;
BlockingTimer::after(Duration::from_millis(100)).wait();
self.handle_urc()?;
}

Expand Down Expand Up @@ -339,8 +310,7 @@ where

while self.serial_mode != SerialMode::ExtendedData {
self.send_internal(&SwitchToEdmCommand, true).ok();
self.timer.start(100.millis()).map_err(|_| Error::Timer)?;
nb::block!(self.timer.wait()).map_err(|_| Error::Timer)?;
BlockingTimer::after(Duration::from_millis(100)).wait();
self.handle_urc()?;
}

Expand Down Expand Up @@ -426,25 +396,22 @@ where
if let Some(ref mut pin) = self.config.rst_pin {
defmt::warn!("Hard resetting Ublox Short Range");
pin.set_low().ok();
self.timer.start(50.millis()).map_err(|_| Error::Timer)?;
nb::block!(self.timer.wait()).map_err(|_| Error::Timer)?;

BlockingTimer::after(Duration::from_millis(50)).wait();

pin.set_high().ok();

self.timer.start(4.secs()).map_err(|_| Error::Timer)?;
loop {
match self.timer.wait() {
Ok(()) => return Err(Error::_Unknown),
Err(nb::Error::WouldBlock) => {
self.handle_urc().ok();
if self.module_started {
break;
}
}
Err(_) => return Err(Error::Timer),
let expiration = Instant::now() + Duration::from_secs(4);

while Instant::now() < expiration {
self.handle_urc().ok();
if self.module_started {
return Ok(());
}
}
return Err(Error::Timeout);
}

Ok(())
}

Expand All @@ -462,34 +429,21 @@ where
self.send_internal(&EdmAtCmdWrapper(RebootDCE), false)?;
self.clear_buffers()?;

self.timer.start(4.secs()).map_err(|_| Error::Timer)?;
loop {
match self.timer.wait() {
Ok(()) => return Err(Error::_Unknown),
Err(nb::Error::WouldBlock) => {
self.handle_urc().ok();
if self.module_started {
break;
}
}
Err(_) => return Err(Error::Timer),
let expiration = Instant::now() + Duration::from_secs(4);
while Instant::now() < expiration {
self.handle_urc().ok();
if self.module_started {
return Ok(());
}
}

Ok(())
Err(Error::Timeout)
}

pub(crate) fn clear_buffers(&mut self) -> Result<(), Error> {
// self.client.reset(); deprecated

if let Some(ref mut sockets) = self.sockets.as_deref_mut() {
sockets.prune();
}

// Allow ATAT some time to clear the buffers
self.timer.start(300.millis()).map_err(|_| Error::Timer)?;
nb::block!(self.timer.wait()).map_err(|_| Error::Timer)?;

Ok(())
}

Expand All @@ -502,11 +456,6 @@ where

self.connected_to_network()?;

// TODO: Is this smart?
// if let Some(ref mut sockets) = self.sockets.as_deref_mut() {
// sockets.recycle(self.timer.now());
// }

Ok(())
}

Expand Down Expand Up @@ -598,7 +547,7 @@ where
match event.protocol {
IPProtocol::TCP => {
// if let Ok(mut tcp) =
// sockets.get::<TcpSocket<CLK, L>>(event.handle)
// sockets.get::<TcpSocket L>>(event.handle)
// {
// debug!(
// "Binding remote {=[u8]:a} to TCP socket {:?}",
Expand Down
2 changes: 1 addition & 1 deletion ublox-short-range/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum Error {
SocketMemory,
SocketMapMemory,
Supplicant,
Timer,
Timeout,
ShadowStoreBug,
_Unknown,
}
Expand Down
1 change: 1 addition & 0 deletions ublox-short-range/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg_attr(not(test), no_std)]

mod blocking_timer;
mod client;
mod hex;

Expand Down
5 changes: 2 additions & 3 deletions ublox-short-range/src/wifi/ap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ use embedded_hal::digital::OutputPin;

use super::connection::{WiFiState, WifiConnection};

impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize>
UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L>
impl<'buf, 'sub, AtCl, AtUrcCh, RST, const N: usize, const L: usize>
UbloxClient<'buf, 'sub, AtCl, AtUrcCh, RST, N, L>
where
'buf: 'sub,
AtCl: AtatClient,
CLK: fugit_timer::Timer<TIMER_HZ>,
RST: OutputPin,
{
/// Creates wireless hotspot service for host machine.
Expand Down
14 changes: 7 additions & 7 deletions ublox-short-range/src/wifi/dns.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::client::{DNSState, DNSTableEntry};
use atat::blocking::AtatClient;
use embassy_time::{Duration, Instant};
use embedded_hal::digital::OutputPin;
use embedded_nal::{nb, AddrType, Dns, IpAddr};
use fugit::ExtU32;
use heapless::String;

use crate::{command::ping::*, UbloxClient};
use ublox_sockets::Error;

impl<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, const TIMER_HZ: u32, const N: usize, const L: usize> Dns
for UbloxClient<'buf, 'sub, AtCl, AtUrcCh, CLK, RST, TIMER_HZ, N, L>
impl<'buf, 'sub, AtCl, AtUrcCh, RST, const N: usize, const L: usize> Dns
for UbloxClient<'buf, 'sub, AtCl, AtUrcCh, RST, N, L>
where
'buf: 'sub,
AtCl: atat::blocking::AtatClient,
CLK: fugit_timer::Timer<TIMER_HZ>,
AtCl: AtatClient,
RST: OutputPin,
{
type Error = Error;
Expand All @@ -38,12 +38,12 @@ where
String::from(hostname),
));

let expiration = self.timer.now() + 8.secs();
let expiration = Instant::now() + Duration::from_secs(8);

while let Some(DNSState::Resolving) = self.dns_table.get_state(String::from(hostname)) {
self.spin().map_err(|_| nb::Error::Other(Error::Illegal))?;

if self.timer.now() >= expiration {
if Instant::now() >= expiration {
break;
}
}
Expand Down
5 changes: 1 addition & 4 deletions ublox-short-range/src/wifi/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,13 @@ impl HotspotOptions {
}
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, defmt::Format)]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConnectionOptions {
pub ssid: String<64>,
pub password: Option<String<64>>,

#[defmt(Debug2Format)]
pub ip: Option<Ipv4Addr>,
#[defmt(Debug2Format)]
pub subnet: Option<Ipv4Addr>,
#[defmt(Debug2Format)]
pub gateway: Option<Ipv4Addr>,
}

Expand Down
Loading

0 comments on commit 7517cdf

Please sign in to comment.