Skip to content

Commit

Permalink
Working PPP + CMUX implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasKoch committed Mar 15, 2024
1 parent 89dfd1d commit 8206f13
Show file tree
Hide file tree
Showing 26 changed files with 1,082 additions and 423 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"rust-analyzer.cargo.features": [
"lara-r6",
"embassy-embedded-hal"
],
"rust-analyzer.cargo.target": "thumbv7em-none-eabihf",
"rust-analyzer.diagnostics.disabled": [
Expand Down
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,10 @@ embassy-net = { version = "0.4", features = [
"proto-ipv4",
"medium-ip",
], optional = true }
embassy-embedded-hal = { version = "0.1", optional = true }

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

[dev-dependencies]
atat = { version = "*", features = ["heapless"] }


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

Expand Down
18 changes: 18 additions & 0 deletions examples/embassy-rp2040-example/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// override the default setting (`cargo check --all-targets`) which produces the following error
// "can't find crate for `test`" when the default compilation target is a no_std target
// with these changes RA will call `cargo check --bins` on save
"rust-analyzer.checkOnSave.allTargets": false,
"rust-analyzer.procMacro.enable": true,
"rust-analyzer.cargo.target": "thumbv6m-none-eabi",
// "rust-analyzer.cargo.target": "thumbv7em-none-eabihf",
"rust-analyzer.cargo.features": [
"ppp",
],
"rust-analyzer.checkOnSave.command": "clippy",
"rust-analyzer.cargoRunner": null,
"rust-analyzer.experimental.procAttrMacros": false,
"rust-analyzer.diagnostics.disabled": [
"macro-error"
],
}
60 changes: 48 additions & 12 deletions examples/embassy-rp2040-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,44 @@ version = "0.1.0"
edition = "2021"

[dependencies]
embassy-rp = { version = "0.1.0", features = ["defmt", "time-driver", "unstable-pac", "rom-v2-intrinsics", "critical-section-impl"] }
embassy-executor = { version = "0.5.0", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers", "nightly"] }
embassy-time = { version = "0.3", features = ["defmt", "defmt-timestamp-uptime"] }
embassy-rp = { version = "0.1.0", features = [
"defmt",
"time-driver",
"unstable-pac",
"rom-v2-intrinsics",
"critical-section-impl",
] }
embassy-executor = { version = "0.5.0", features = [
"arch-cortex-m",
"executor-thread",
"defmt",
"integrated-timers",
"nightly",
] }
embassy-time = { version = "0.3", features = [
"defmt",
"defmt-timestamp-uptime",
] }
embassy-futures = { version = "0.1", features = ["defmt"] }
embassy-sync = { version = "0.5", features = ["defmt"] }

embassy-net = { version = "0.4", features = ["defmt", "proto-ipv4", "medium-ip", "tcp", "udp"] }
embassy-net = { version = "0.4", features = [
"defmt",
"packet-trace",
"proto-ipv4",
"medium-ip",
"tcp",
"udp",
"dns",
] }
embassy-net-ppp = { version = "0.1", features = ["defmt"] }
embassy-at-cmux = { path = "../../../embassy/embassy-at-cmux", features = ["defmt"] }
embedded-tls = { path = "../../../embedded-tls", default-features = false, features = ["defmt"] }
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 = [
"defmt",
] }

embedded-io-async = { version = "0.6" }
heapless = "0.8"
Expand All @@ -26,10 +54,21 @@ defmt = "0.3.5"
defmt-rtt = "0.4.0"
panic-probe = { version = "0.3.1", features = ["print-defmt"] }

static_cell = { version = "2.0", features = []}
static_cell = { version = "2.0", features = [] }

atat = { version = "0.21.0", features = ["derive", "bytes", "defmt"] }
ublox-cellular-rs = {version = "0.4.0", path = "../..", features = ["lara-r6", "defmt"]}
ublox-cellular-rs = { version = "0.4.0", path = "../..", features = [
"lara-r6",
"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 }


[features]
ppp = ["ublox-cellular-rs/ppp"]
Expand Down Expand Up @@ -58,11 +97,8 @@ embassy-executor = { path = "../../../embassy/embassy-executor" }

atat = { path = "../../../atat/atat" }

#ublox-sockets = { path = "../../../ublox-sockets" }
#atat = { path = "../../../atat/atat" }

[profile.dev]
opt-level = "s"

[profile.release]
opt-level = "s"
opt-level = "s"
8 changes: 1 addition & 7 deletions examples/embassy-rp2040-example/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
# Before upgrading check that everything is available on all tier1 targets here:
# https://rust-lang.github.io/rustup-components-history
[toolchain]
channel = "1.75"
channel = "nightly-2024-02-01"
components = [ "rust-src", "rustfmt", "llvm-tools" ]
targets = [
"thumbv7em-none-eabi",
"thumbv7m-none-eabi",
"thumbv6m-none-eabi",
"thumbv7em-none-eabihf",
"thumbv8m.main-none-eabihf",
"riscv32imac-unknown-none-elf",
"wasm32-unknown-unknown",
]
166 changes: 116 additions & 50 deletions examples/embassy-rp2040-example/src/bin/embassy-smoltcp-ppp.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#![cfg(feature = "ppp")]

#![no_std]
#![no_main]
#![allow(stable_features)]
#![feature(type_alias_impl_trait)]

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::Ipv4Address;
use embassy_net::Stack;
use embassy_net::StackResources;
use embassy_rp::gpio;
Expand All @@ -17,11 +18,29 @@ use embassy_rp::gpio::Input;
use embassy_rp::gpio::OutputOpenDrain;
use embassy_rp::uart::BufferedUart;
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;
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_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 Down Expand Up @@ -82,8 +101,8 @@ async fn main(spawner: Spawner) {
embassy_rp::init(config)
};

static TX_BUF: StaticCell<[u8; 16]> = StaticCell::new();
static RX_BUF: StaticCell<[u8; 16]> = StaticCell::new();
static TX_BUF: StaticCell<[u8; 256]> = StaticCell::new();
static RX_BUF: StaticCell<[u8; 256]> = StaticCell::new();

let cell_uart = BufferedUart::new_with_rtscts(
p.UART0,
Expand All @@ -92,8 +111,8 @@ async fn main(spawner: Spawner) {
p.PIN_1,
p.PIN_3,
p.PIN_2,
TX_BUF.init([0; 16]),
RX_BUF.init([0; 16]),
TX_BUF.init([0; 256]),
RX_BUF.init([0; 256]),
embassy_rp::uart::Config::default(),
);

Expand All @@ -103,15 +122,12 @@ async fn main(spawner: Spawner) {

static RESOURCES: StaticCell<Resources<CMD_BUF_SIZE, INGRESS_BUF_SIZE, URC_CAPACITY>> =
StaticCell::new();

let (net_device, mut control, runner) = ublox_cellular::asynch::new_ppp(
RESOURCES.init(Resources::new()),
MyCelullarConfig {
let (net_device, mut cell_control, mut runner) =
ublox_cellular::asynch::new_ppp(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)
});

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

spawner.spawn(net_task(stack)).unwrap();
spawner.spawn(ppp_task(runner, cell_uart, &stack)).unwrap();

control.set_desired_state(OperationState::Connected).await;

stack.wait_config_up().await;

info!("We have network!");

// 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), 80);
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!");
}

#[embassy_executor::task]
async fn net_task(stack: &'static Stack<embassy_net_ppp::Device<'static>>) -> ! {
stack.run().await
}
let http_fut = async {
stack.wait_config_up().await;

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

// 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 ppp_task(
mut runner: PPPRunner<'static, MyCelullarConfig, INGRESS_BUF_SIZE, URC_CAPACITY>,
interface: BufferedUart<'static, UART0>,
stack: &'static embassy_net::Stack<embassy_net_ppp::Device<'static>>,
) -> ! {
let (rx, tx) = interface.split();
runner.run(rx, tx, stack).await
let (rx, tx) = cell_uart.split();
embassy_futures::join::join3(
stack.run(),
runner.run(rx, tx, |ipv4| {
let Some(addr) = ipv4.address else {
warn!("PPP did not provide an IP address.");
return;
};
let mut dns_servers = heapless::Vec::new();
for s in ipv4.dns_servers.iter().flatten() {
let _ = dns_servers.push(embassy_net::Ipv4Address::from_bytes(&s.0));
}
let config = embassy_net::ConfigV4::Static(embassy_net::StaticConfigV4 {
address: embassy_net::Ipv4Cidr::new(
embassy_net::Ipv4Address::from_bytes(&addr.0),
0,
),
gateway: None,
dns_servers,
});

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

core::unreachable!();
}
5 changes: 5 additions & 0 deletions examples/tokio-std-example/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[build]
target = "x86_64-unknown-linux-gnu"

[env]
RUST_LOG = "trace,embassy_hal_internal=error"
9 changes: 9 additions & 0 deletions examples/tokio-std-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
**/*.rs.bk
.#*
.gdb_history
*.fifo
target/
*.o
**/*secrets*

.DS_Store

0 comments on commit 8206f13

Please sign in to comment.