Skip to content

Commit

Permalink
Refactor async completely for a more intuitive API. URCs over PPP UDP…
Browse files Browse the repository at this point in the history
… socket is still not working properly
  • Loading branch information
MathiasKoch committed May 31, 2024
1 parent e96769a commit 27ed97e
Show file tree
Hide file tree
Showing 21 changed files with 955 additions and 1,058 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"rust-analyzer.cargo.target": "thumbv6m-none-eabi",
"rust-analyzer.check.allTargets": false,
"rust-analyzer.linkedProjects": [],
"rust-analyzer.cargo.features": [
"odin_w2xx",
"ppp"
],
"rust-analyzer.server.extraEnv": {
"WIFI_NETWORK": "foo",
"WIFI_PASSWORD": "foo",
Expand Down
11 changes: 3 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ name = "ublox_short_range"
doctest = false

[dependencies]
# atat = { version = "0.21", features = ["derive", "bytes"] }
atat = { path = "../atat/atat", features = ["derive", "bytes"] }
atat = { version = "0.22", features = ["derive", "bytes"] }

# atat = { git = "https://github.com/BlackbirdHQ/atat", rev = "70283be", features = ["derive", "defmt", "bytes"] }
heapless = { version = "^0.8", features = ["serde"] }
no-std-net = { version = "0.6", features = ["serde"] }
serde = { version = "^1", default-features = false, features = ["derive"] }
# ublox-sockets = { version = "0.5", features = ["edm"], optional = true }
ublox-sockets = { git = "https://github.com/BlackbirdHQ/ublox-sockets", rev = "9f7fe54", optional = true }
postcard = "1.0.4"
portable-atomic = "1.6"

log = { version = "^0.4", default-features = false, optional = true }
Expand All @@ -38,7 +35,6 @@ embassy-futures = "0.1"
embedded-nal-async = { version = "0.7" }
futures-util = { version = "0.3.29", default-features = false }

embedded-io = "0.6"
embedded-io-async = "0.6"

embassy-net-ppp = { version = "0.1", optional = true }
Expand All @@ -49,7 +45,7 @@ embassy-net = { version = "0.4", features = [


[features]
default = ["socket-tcp", "socket-udp", "odin_w2xx"]
default = ["socket-tcp", "socket-udp"]

internal-network-stack = ["dep:ublox-sockets", "edm"]
edm = ["ublox-sockets?/edm"]
Expand All @@ -62,7 +58,6 @@ socket-udp = ["ublox-sockets?/socket-udp", "embassy-net?/udp"]

defmt = [
"dep:defmt",
"postcard/use-defmt",
"heapless/defmt-03",
"atat/defmt",
"ublox-sockets?/defmt",
Expand All @@ -86,4 +81,4 @@ exclude = ["examples"]

[patch.crates-io]
no-std-net = { git = "https://github.com/rushmorem/no-std-net", branch = "issue-15" }
atat = { path = "../atat/atat" }
atat = { path = "../atat/atat" }
26 changes: 13 additions & 13 deletions examples/rpi-pico/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ embassy-net = { version = "0.4", optional = true, features = [
"medium-ip",
"tcp",
"udp",
"dns"
] }
embassy-net-ppp = { version = "0.1", optional = true, features = ["defmt"] }
embedded-tls = { path = "../../../embedded-tls", default-features = false, features = [
"defmt",
] }
reqwless = { git = "https://github.com/drogue-iot/reqwless", features = ["defmt"] }
smoltcp = { version = "*", default-features = false, features = ["dns-max-server-count-4"]}
rand_chacha = { version = "0.3", default-features = false }
embedded-tls = { path = "../../../embedded-tls", default-features = false, features = ["defmt"] }


[features]
internal-network-stack = ["ublox-short-range-rs/internal-network-stack"]
Expand All @@ -68,16 +71,13 @@ ppp = ["dep:embassy-net", "dep:embassy-net-ppp", "ublox-short-range-rs/ppp"]
# embassy-net-driver-channel = { git = "https://github.com/embassy-rs/embassy", rev = "03d6363d5af5dcaf21b52734994a466ca593d2b6" }


# embassy-executor = { path = "../../../embassy/embassy-executor" }
# embassy-hal-internal = { path = "../../../embassy/embassy-hal-internal" }
# embassy-time = { path = "../../../embassy/embassy-time" }
# embassy-futures = { path = "../../../embassy/embassy-futures" }
# embassy-sync = { path = "../../../embassy/embassy-sync" }
# embassy-rp = { path = "../../../embassy/embassy-rp" }
# embassy-net-driver = { path = "../../../embassy/embassy-net-driver" }
# embassy-net = { path = "../../../embassy/embassy-net" }
# embassy-net-ppp = { path = "../../../embassy/embassy-net-ppp" }
atat = { path = "../../../atat/atat" }
embassy-rp = { path = "../../../embassy/embassy-rp" }
embassy-time = { path = "../../../embassy/embassy-time" }
embassy-sync = { path = "../../../embassy/embassy-sync" }
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" }
ublox-sockets = { path = "../../../ublox-sockets" }
no-std-net = { path = "../../../no-std-net" }

Expand Down
84 changes: 65 additions & 19 deletions examples/rpi-pico/src/bin/embassy-smoltcp-ppp.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
#![cfg(feature = "ppp")]
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

#[cfg(not(feature = "ppp"))]
compile_error!("You must enable the `ppp` feature flag to build this example");

use defmt::*;
use embassy_executor::Spawner;
use embassy_net::{Stack, StackResources};
use embassy_net::tcp::TcpSocket;
use embassy_net::{Ipv4Address, Stack, StackResources};
use embassy_rp::gpio::{AnyPin, Level, Output, Pin};
use embassy_rp::peripherals::UART1;
use embassy_rp::uart::{BufferedInterruptHandler, BufferedUart};
use embassy_rp::{bind_interrupts, uart};
use embassy_time::{Duration, Timer};
use embedded_tls::Aes128GcmSha256;
use embedded_tls::TlsConfig;
use embedded_tls::TlsConnection;
use embedded_tls::TlsContext;
use embedded_tls::UnsecureProvider;
use rand_chacha::rand_core::SeedableRng;
use rand_chacha::ChaCha8Rng;
use reqwless::headers::ContentType;
use reqwless::request::Request;
use reqwless::request::RequestBuilder as _;
use reqwless::response::Response;
use static_cell::StaticCell;
use ublox_short_range::asynch::{PPPRunner, Resources};
use {defmt_rtt as _, panic_probe as _};
Expand All @@ -26,7 +40,7 @@ async fn net_task(stack: &'static Stack<embassy_net_ppp::Device<'static>>) -> !

#[embassy_executor::task]
async fn ppp_task(
mut runner: PPPRunner<'static, Output<'static, AnyPin>, INGRESS_BUF_SIZE, URC_CAPACITY>,
mut runner: PPPRunner<'static, Output<'static>, INGRESS_BUF_SIZE, URC_CAPACITY>,
interface: BufferedUart<'static, UART1>,
stack: &'static embassy_net::Stack<embassy_net_ppp::Device<'static>>,
) -> ! {
Expand Down Expand Up @@ -68,7 +82,7 @@ async fn main(spawner: Spawner) {

// Init network stack
static STACK: StaticCell<Stack<embassy_net_ppp::Device<'static>>> = StaticCell::new();
static STACK_RESOURCES: StaticCell<StackResources<2>> = StaticCell::new();
static STACK_RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();

let stack = &*STACK.init(Stack::new(
net_device,
Expand All @@ -91,19 +105,51 @@ async fn main(spawner: Spawner) {

control.join_open("Test").await;

// // Then we can use it!
// 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 remote_endpoint = (Ipv4Address::new(93, 184, 216, 34), 12345);
// 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!");
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(10)));

let hostname = "ecdsa-test.germancoding.com";

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);

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();
info!("{=[u8]:a}", rx_buf);
}
60 changes: 60 additions & 0 deletions src/asynch/at_udp_socket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use embassy_net::{udp::UdpSocket, Ipv4Address};
use embedded_io_async::{Read, Write};

pub struct AtUdpSocket<'a>(pub(crate) UdpSocket<'a>);

impl<'a> AtUdpSocket<'a> {
pub(crate) const PPP_AT_PORT: u16 = 23;
}

impl<'a> embedded_io_async::ErrorType for &AtUdpSocket<'a> {
type Error = core::convert::Infallible;
}

impl<'a> Read for &AtUdpSocket<'a> {
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let (len, _) = self.0.recv_from(buf).await.unwrap();
debug!("READ {} bytes: {=[u8]:a}", len, &buf[..len]);
Ok(len)
}
}

impl<'a> Write for &AtUdpSocket<'a> {
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
self.0
.send_to(
buf,
(Ipv4Address::new(172, 30, 0, 251), AtUdpSocket::PPP_AT_PORT),
)
.await
.unwrap();

Ok(buf.len())
}
}

impl<'a> embedded_io_async::ErrorType for AtUdpSocket<'a> {
type Error = core::convert::Infallible;
}

impl<'a> Read for AtUdpSocket<'a> {
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let (len, _) = self.0.recv_from(buf).await.unwrap();
debug!("READ {} bytes: {=[u8]:a}", len, &buf[..len]);
Ok(len)
}
}

impl<'a> Write for AtUdpSocket<'a> {
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
self.0
.send_to(
buf,
(Ipv4Address::new(172, 30, 0, 251), AtUdpSocket::PPP_AT_PORT),
)
.await
.unwrap();

Ok(buf.len())
}
}
Loading

0 comments on commit 27ed97e

Please sign in to comment.