Skip to content

Commit

Permalink
Add registration management, meticulously derived from ubxlib
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasKoch committed Apr 22, 2024
1 parent 0f459d2 commit 049e462
Show file tree
Hide file tree
Showing 40 changed files with 2,865 additions and 1,792 deletions.
40 changes: 22 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ use-upsd-context-activation = []
# mqtt-will = []
# mqtt-keep-alive = []
# mqtt-security = []
# ucged5 = []
# context-mapping-required = []
ucged5 = []
context-mapping-required = []
# security-tls-cipher-list = []
# auto-bauding = []
# at-profiles = []
Expand All @@ -88,11 +88,10 @@ use-upsd-context-activation = []
# fota = []
# uart-power-saving = []
cmux = ["dep:embassy-at-cmux"]
# cmux-channel-close = []
# snr-reported = []
# authentication-mode-automatic = []
authentication-mode-automatic = []
# lwm2m = []
# ucged = []
ucged = []
# http = []
ppp = ["cmux", "dep:embassy-net-ppp", "dep:embassy-net"]

Expand All @@ -117,20 +116,23 @@ defmt = [
]
log = ["dep:log", "ublox-sockets?/log", "atat/log"]

# Modules
lara-r2 = []
lara-r6 = []
leon-g1 = []
lisa-u2 = []
mpci-l2 = []
sara-g3 = []
sara-g4 = []
sara-r5 = ["use-upsd-context-activation"]
sara-u1 = []
sara-u2 = ["use-upsd-context-activation"]
toby-l2 = []
# The supported list of cellular modules.
#
# Note: if you add a new module type here, you also need to add it in
# `modules.rs`
lara-r6 = ["ucged"]
lena-r8 = []
sara-r410m = ["ucged", "ucged5"]
sara-r412m = ["ucged", "ucged5"]
sara-r422 = ["context-mapping-required", "ucged"]
sara-r5 = ["context-mapping-required", "ucged", "authentication-mode-automatic"]
sara-u201 = [
"use-upsd-context-activation",
"context-mapping-required",
"ucged",
"authentication-mode-automatic",
]
toby-r2 = []
toby-l4 = []

[workspace]
members = []
Expand All @@ -150,3 +152,5 @@ embassy-net = { git = "https://github.com/embassy-rs/embassy", branch = "main" }
#embassy-time = { path = "../embassy/embassy-time" }
#embassy-sync = { path = "../embassy/embassy-sync" }
#embassy-futures = { path = "../embassy/embassy-futures" }

atat = { path = "../atat/atat" }
5 changes: 3 additions & 2 deletions examples/embassy-rp2040-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ embassy-at-cmux = { path = "../../../embassy/embassy-at-cmux", features = [
"defmt",
] }

embedded-tls = { path = "https://github.com/drogue-iot/embedded-tls", rev = "f788e02", default-features = false, features = [
embedded-tls = { git = "https://github.com/drogue-iot/embedded-tls", rev = "f788e02", default-features = false, features = [
"defmt",
] }

Expand All @@ -65,7 +65,7 @@ reqwless = { git = "https://github.com/drogue-iot/reqwless", features = [
"defmt",
] }
smoltcp = { version = "*", default-features = false, features = [
"dns-max-server-count-4",
"dns-max-server-count-4"
] }
rand_chacha = { version = "0.3", default-features = false }

Expand Down Expand Up @@ -94,6 +94,7 @@ embassy-net = { path = "../../../embassy/embassy-net" }
embassy-net-ppp = { path = "../../../embassy/embassy-net-ppp" }
embassy-futures = { path = "../../../embassy/embassy-futures" }
embassy-executor = { path = "../../../embassy/embassy-executor" }
atat = { path = "../../../atat/atat" }


[profile.dev]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async fn main(spawner: Spawner) {
.set_desired_state(OperationState::DataEstablished)
.await;
info!("set_desired_state(PowerState::Alive)");
while control.power_state() != OperationState::DataEstablished {
while control.operation_state() != OperationState::DataEstablished {
Timer::after(Duration::from_millis(1000)).await;
}
Timer::after(Duration::from_millis(10000)).await;
Expand Down Expand Up @@ -160,7 +160,7 @@ async fn main(spawner: Spawner) {
Timer::after(Duration::from_millis(10000)).await;
control.set_desired_state(OperationState::PowerDown).await;
info!("set_desired_state(PowerState::PowerDown)");
while control.power_state() != OperationState::PowerDown {
while control.operation_state() != OperationState::PowerDown {
Timer::after(Duration::from_millis(1000)).await;
}

Expand Down
185 changes: 104 additions & 81 deletions examples/embassy-rp2040-example/src/bin/embassy-smoltcp-ppp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
#![no_main]
#![allow(stable_features)]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

use defmt::*;
use embassy_executor::Spawner;
use embassy_net::dns::DnsSocket;
use embassy_net::tcp::client::TcpClient;
use embassy_net::tcp::client::TcpClientState;
use embassy_net::tcp::TcpSocket;
use embassy_net::Stack;
use embassy_net::StackResources;
Expand All @@ -17,15 +15,11 @@ use embassy_rp::gpio::Input;

use embassy_rp::gpio::OutputOpenDrain;
use embassy_rp::uart::BufferedUart;
use embassy_rp::uart::BufferedUartRx;
use embassy_rp::uart::BufferedUartTx;
use embassy_rp::{bind_interrupts, peripherals::UART0, uart::BufferedInterruptHandler};
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_time::Duration;
use embassy_time::Timer;
use embedded_mqtt::transport::embedded_tls::TlsNalTransport;
use embedded_mqtt::transport::embedded_tls::TlsState;
use embedded_mqtt::DomainBroker;
use embedded_tls::Aes128GcmSha256;
use embedded_tls::Certificate;
use embedded_tls::TlsConfig;
use embedded_tls::TlsConnection;
use embedded_tls::TlsContext;
Expand All @@ -37,10 +31,7 @@ use reqwless::request::Request;
use reqwless::request::RequestBuilder as _;
use reqwless::response::Response;
use static_cell::StaticCell;
use ublox_cellular::asynch::state::OperationState;
use ublox_cellular::asynch::PPPRunner;
use ublox_cellular::asynch::Resources;
use ublox_cellular::config::NoPin;
use {defmt_rtt as _, panic_probe as _};

use ublox_cellular::config::{Apn, CellularConfig};
Expand All @@ -51,7 +42,7 @@ bind_interrupts!(struct Irqs {

const CMD_BUF_SIZE: usize = 128;
const INGRESS_BUF_SIZE: usize = 512;
const URC_CAPACITY: usize = 2;
const URC_CAPACITY: usize = 16;

struct MyCelullarConfig {
reset_pin: Option<OutputOpenDrain<'static>>,
Expand All @@ -67,7 +58,7 @@ impl<'a> CellularConfig<'a> for MyCelullarConfig {
const FLOW_CONTROL: bool = true;

const APN: Apn<'a> = Apn::Given {
name: "em",
name: "onomondo",
username: None,
password: None,
};
Expand Down Expand Up @@ -122,12 +113,18 @@ async fn main(spawner: Spawner) {

static RESOURCES: StaticCell<Resources<CMD_BUF_SIZE, INGRESS_BUF_SIZE, URC_CAPACITY>> =
StaticCell::new();
let (net_device, mut cell_control, mut runner) =
ublox_cellular::asynch::new_ppp(RESOURCES.init(Resources::new()), MyCelullarConfig {
let mut runner = ublox_cellular::asynch::Runner::new(
cell_uart.split(),
RESOURCES.init(Resources::new()),
MyCelullarConfig {
reset_pin: Some(cell_nrst),
power_pin: Some(cell_pwr),
vint_pin: Some(cell_vint)
});
vint_pin: Some(cell_vint),
},
);

static PPP_STATE: StaticCell<embassy_net_ppp::State<2, 2>> = StaticCell::new();
let net_device = runner.ppp_stack(PPP_STATE.init(embassy_net_ppp::State::new()));

// Generate random seed
let seed = 0x0123_4567_89ab_cdef; // chosen by fair dice roll. guarenteed to be random.
Expand All @@ -143,73 +140,103 @@ async fn main(spawner: Spawner) {
seed,
));

spawner.spawn(net_task(stack)).unwrap();
spawner.spawn(cell_task(runner, stack)).unwrap();

stack.wait_config_up().await;

// embassy_time::Timer::after(Duration::from_secs(2)).await;

let http_fut = async {
stack.wait_config_up().await;
info!("We have network!");

info!("We have network!");
let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(Duration::from_secs(20)));

let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(Duration::from_secs(10)));

let hostname = "ecdsa-test.germancoding.com";
// let hostname = "eohkv57m7xxdr4m.m.pipedream.net";
info!("looking up {:?}...", hostname);

let mut remote = stack
.dns_query(hostname, smoltcp::wire::DnsQueryType::A)
.await
.unwrap();
let remote_endpoint = (remote.pop().unwrap(), 443);
info!("connecting to {:?}...", remote_endpoint);
let r = socket.connect(remote_endpoint).await;
if let Err(e) = r {
warn!("connect error: {:?}", e);
return;
}
info!("TCP connected!");

let mut read_record_buffer = [0; 16640];
let mut write_record_buffer = [0; 16640];
let config = TlsConfig::new().with_server_name(hostname);
let mut tls = TlsConnection::new(socket, &mut read_record_buffer, &mut write_record_buffer);

tls.open(TlsContext::new(
&config,
UnsecureProvider::new::<Aes128GcmSha256>(ChaCha8Rng::seed_from_u64(seed)),
))
.await
.expect("error establishing TLS connection");

info!("TLS Established!");

let request = Request::get("/")
.host(hostname)
.content_type(ContentType::TextPlain)
.build();
request.write(&mut tls).await.unwrap();

let mut rx_buf = [0; 4096];
let response = Response::read(&mut tls, reqwless::request::Method::GET, &mut rx_buf)
.await
.unwrap();

let hostname = "ecdsa-test.germancoding.com";
{
info!("Got resp! {=[u8]:a}", &rx_buf[..512]);

let mut remote = stack
.dns_query(hostname, smoltcp::wire::DnsQueryType::A)
.await
.unwrap();
let remote_endpoint = (remote.pop().unwrap(), 443);
info!("connecting to {:?}...", remote_endpoint);
let r = socket.connect(remote_endpoint).await;
if let Err(e) = r {
warn!("connect error: {:?}", e);
return;
}
info!("TCP connected!");

let mut read_record_buffer = [0; 16384];
let mut write_record_buffer = [0; 16384];
let config = TlsConfig::new().with_server_name(hostname);
let mut tls = TlsConnection::new(socket, &mut read_record_buffer, &mut write_record_buffer);
// let mut buf = [0; 16384];
// let len = response
// .body()
// .reader()
// .read_to_end(&mut buf)
// .await
// .unwrap();
// info!("{=[u8]:a}", &buf[..len]);

loop {
embassy_time::Timer::after(Duration::from_secs(1)).await
}
}

tls.open(TlsContext::new(
&config,
UnsecureProvider::new::<Aes128GcmSha256>(ChaCha8Rng::seed_from_u64(seed)),
))
.await
.expect("error establishing TLS connection");

info!("TLS Established!");

let request = Request::get("/")
.host(hostname)
.content_type(ContentType::TextPlain)
.build();
request.write(&mut tls).await.unwrap();

let mut rx_buf = [0; 4096];
let response = Response::read(&mut tls, reqwless::request::Method::GET, &mut rx_buf)
.await
.unwrap();

// let mut buf = vec![0; 16384];
// let len = response
// .body()
// .reader()
// .read_to_end(&mut buf)
// .await
// .unwrap();
// info!("{:?}", core::str::from_utf8(&buf[..len]));
};
#[embassy_executor::task]
async fn net_task(stack: &'static Stack<embassy_net_ppp::Device<'static>>) -> ! {
stack.run().await
}

let (rx, tx) = cell_uart.split();
embassy_futures::join::join3(
stack.run(),
runner.run(rx, tx, |ipv4| {
#[embassy_executor::task]
async fn cell_task(
runner: ublox_cellular::asynch::Runner<
'static,
BufferedUartRx<'static, UART0>,
BufferedUartTx<'static, UART0>,
MyCelullarConfig,
INGRESS_BUF_SIZE,
URC_CAPACITY,
>,
stack: &'static embassy_net::Stack<embassy_net_ppp::Device<'static>>,
) -> ! {
runner
.run(|ipv4| {
let Some(addr) = ipv4.address else {
warn!("PPP did not provide an IP address.");
defmt::warn!("PPP did not provide an IP address.");
return;
};
let mut dns_servers = heapless::Vec::new();
Expand All @@ -226,10 +253,6 @@ async fn main(spawner: Spawner) {
});

stack.set_config_v4(config);
}),
http_fut,
)
.await;

core::unreachable!();
})
.await
}
4 changes: 2 additions & 2 deletions examples/embassy-stm32-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ async fn main_task(spawner: Spawner) {
.set_desired_state(OperationState::DataEstablished)
.await;
info!("set_desired_state(PowerState::Alive)");
while control.power_state() != OperationState::DataEstablished {
while control.operation_state() != OperationState::DataEstablished {
Timer::after(Duration::from_millis(1000)).await;
}
Timer::after(Duration::from_millis(10000)).await;
Expand Down Expand Up @@ -228,7 +228,7 @@ async fn main_task(spawner: Spawner) {
Timer::after(Duration::from_millis(10000)).await;
control.set_desired_state(OperationState::PowerDown).await;
info!("set_desired_state(PowerState::PowerDown)");
while control.power_state() != OperationState::PowerDown {
while control.operation_state() != OperationState::PowerDown {
Timer::after(Duration::from_millis(1000)).await;
}

Expand Down
Loading

0 comments on commit 049e462

Please sign in to comment.