Skip to content

Commit

Permalink
Merge pull request #141 from card-io-ecg/apsta
Browse files Browse the repository at this point in the history
AP-STA mode
  • Loading branch information
bugadani committed Nov 6, 2023
2 parents 01f144b + 50cab30 commit 305e42a
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 56 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ esp-println = { version = "0.7.0", default-features = false, features = [
"critical-section",
] }
esp-alloc = "0.3.0"
esp-wifi = { git = "https://github.com/esp-rs/esp-wifi.git", rev = "bd5f611", default-features = false, features = [
esp-wifi = { git = "https://github.com/esp-rs/esp-wifi.git", rev = "5166089", default-features = false, features = [
"phy-enable-usb",
"ps-max-modem",
"wifi",
Expand Down Expand Up @@ -155,6 +155,7 @@ enumset = "1.1.3"

[patch.'https://github.com/esp-rs/esp-wifi.git']
# esp-wifi = { path = "../esp-wifi/esp-wifi" }
# esp-wifi = { git = "https://github.com/bugadani/esp-wifi.git" }

[features]
default = ["esp32s3", "defmt"]
Expand Down
25 changes: 3 additions & 22 deletions bad-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,34 +85,15 @@ where
}
}

impl<H, EH, RB: RequestBuffer, const MAX_HEADERS: usize> BadServer<Chain<H>, EH, RB, MAX_HEADERS>
impl<H, EH, RB: RequestBuffer, const MAX_HEADERS: usize> BadServer<H, EH, RB, MAX_HEADERS>
where
H: Handler,
H: Handler + ChainElement,
EH: ErrorHandler,
{
pub fn with_handler<H2: Handler<Connection = H::Connection>>(
self,
handler: H2,
) -> BadServer<Link<H2, Chain<H>>, EH, RB, MAX_HEADERS> {
BadServer {
handler: self.handler.append(handler),
error_handler: self.error_handler,
buffer: self.buffer,
}
}
}

impl<H, EH, P, RB: RequestBuffer, const MAX_HEADERS: usize>
BadServer<Link<H, P>, EH, RB, MAX_HEADERS>
where
H: Handler,
P: ChainElement + Handler<Connection = H::Connection>,
EH: ErrorHandler<Connection = H::Connection>,
{
pub fn with_handler<H2: Handler<Connection = H::Connection>>(
self,
handler: H2,
) -> BadServer<Link<H2, Link<H, P>>, EH, RB, MAX_HEADERS> {
) -> BadServer<Link<H2, H>, EH, RB, MAX_HEADERS> {
BadServer {
handler: self.handler.append(handler),
error_handler: self.error_handler,
Expand Down
2 changes: 2 additions & 0 deletions config-site/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ logger = { workspace = true }
log = { workspace = true, optional = true }
norfs = { workspace = true, optional = true }
smol = { version = "1.3", optional = true }
object-chain.workspace = true

[build-dependencies]
libflate = "1.4.0"
Expand All @@ -27,6 +28,7 @@ required-features = ["bad-server/std", "bad-server/log"]
[dev-dependencies]
simple_logger = "4.1"
smol = "1"
smoltcp = { workspace = true, features = ["proto-ipv4"] }

[features]
default = []
Expand Down
25 changes: 24 additions & 1 deletion config-site/examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#![feature(async_fn_in_trait)]

use bad_server::connector::std_compat::StdTcpSocket;
use bad_server::{
connector::{std_compat::StdTcpSocket, Connection},
handler::RequestHandler,
request::Request,
response::ResponseStatus,
HandleError,
};
use config_site::data::{network::WifiNetwork, SharedWebContext, WebContext};
use log::LevelFilter;

Expand Down Expand Up @@ -38,8 +44,25 @@ pub async fn run() {
});

config_site::create(&context, "Example")
.with_handler(RequestHandler::get("/vn", VisibleNetworks))
.with_request_buffer_size::<2048>()
.with_header_count::<48>()
.listen(&mut socket, 8080)
.await;
}

struct VisibleNetworks;
impl<C: Connection> RequestHandler<C> for VisibleNetworks {
async fn handle(&self, request: Request<'_, '_, C>) -> Result<(), HandleError<C>> {
let response = request.start_response(ResponseStatus::Ok).await?;
let mut response = response.start_chunked_body().await?;

response.write("Demo network 1").await?;
response.write("\n").await?;

response.write("Demo network 3").await?;
response.write("\n").await?;

response.end_chunked_response().await
}
}
2 changes: 1 addition & 1 deletion config-site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn create<'a, CON>(
context: &'a SharedWebContext,
fw_version: &'a str,
) -> BadServer<
impl Handler<Connection = CON> + 'a,
impl Handler<Connection = CON> + 'a + object_chain::ChainElement,
impl ErrorHandler<Connection = CON>,
[u8; 1024],
32,
Expand Down
14 changes: 12 additions & 2 deletions config-site/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,12 @@

<fieldset>
<legend>Known networks</legend>
<div class="kn"></div>
<ul class="kn"></ul>
<button onclick="$fe.nn();">Add network</button>
<hr />
<legend>Visible networks</legend>
<ul class="vn"></ul>
<hr />
<div>Backend URL: <span class="bu"></span></div>
<button onclick="$fe.buc();">Change URL</button>
</fieldset>
Expand Down Expand Up @@ -184,7 +187,12 @@

<!-- list item templates -->
<div id="network" class="tpl">
SSID #<span class="index"></span>: <span class="data"></span><button onclick="$fe.dn(this)">Delete</button>
<li>
SSID #<span class="index"></span>: <span class="data"></span><button onclick="$fe.dn(this)">Delete</button>
</li>
</div>
<div id="visible" class="tpl">
<li><span class="data"></span></li>
</div>
</body>

Expand Down Expand Up @@ -347,10 +355,12 @@
let system_info = await $load('/si');
let backend_url = await $load('/bu');
let known_networks = await $load('/kn');
let visible_networks = await $load('/vn');

tpl.set("fw", await system_info.text());
tpl.set("bu", await backend_url.text());
tpl.set_list("kn", "network", await known_networks.text());
tpl.set_list("vn", "visible", await visible_networks.text());
}),

nn: () => $page('nn'),
Expand Down
22 changes: 22 additions & 0 deletions src/board/initialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,28 @@ impl InnerContext {
Some(ap)
}

pub async fn enable_wifi_ap_sta(&mut self) -> Option<(Ap, Sta)> {
if !self.can_enable_wifi() {
self.wifi.stop_if().await;
return None;
}

let apsta = self
.wifi
.configure_ap_sta(
NetConfig::ipv4_static(StaticConfigV4 {
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 2, 1), 24),
gateway: Some(Ipv4Address::from_bytes(&[192, 168, 2, 1])),
dns_servers: Default::default(),
}),
NetConfig::dhcpv4(Default::default()),
&self.clocks,
)
.await;

Some(apsta)
}

/// Note: make sure Sta/Ap is released before calling this.
pub async fn disable_wifi(&mut self) {
self.wifi.stop_if().await
Expand Down
12 changes: 6 additions & 6 deletions src/board/wifi/ap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use gui::widgets::wifi_access_point::WifiAccessPointState;
use crate::{
board::{
hal::{peripherals::WIFI, Rng},
wifi::{net_task, StackWrapper},
wifi::{ap_net_task, StackWrapper},
},
task_control::{TaskControlToken, TaskController},
};
Expand All @@ -15,7 +15,7 @@ use embassy_futures::join::join;
use embassy_net::{Config, Stack};
use embedded_svc::wifi::{AccessPointConfiguration, Configuration, Wifi as _};
use esp_wifi::{
wifi::{WifiController, WifiDevice, WifiEvent, WifiMode},
wifi::{WifiApDevice, WifiController, WifiDevice, WifiEvent},
EspWifiInitialization,
};
use macros as cardio;
Expand All @@ -34,7 +34,7 @@ impl ApConnectionState {

#[derive(Clone)]
pub struct Ap {
pub(super) ap_stack: Rc<StackWrapper>,
pub(super) ap_stack: Rc<StackWrapper<WifiApDevice>>,
pub(super) state: Rc<ApConnectionState>,
}

Expand All @@ -43,7 +43,7 @@ impl Ap {
self.ap_stack.is_link_up()
}

pub fn stack(&self) -> &Stack<WifiDevice<'static>> {
pub fn stack(&self) -> &Stack<WifiDevice<'static, WifiApDevice>> {
&self.ap_stack
}

Expand Down Expand Up @@ -78,7 +78,7 @@ impl ApState {
info!("Configuring AP");

let (ap_device, controller) =
unwrap!(esp_wifi::wifi::new_with_mode(&init, wifi, WifiMode::Ap));
unwrap!(esp_wifi::wifi::new_with_mode(&init, wifi, WifiApDevice));

info!("Starting AP");

Expand All @@ -96,7 +96,7 @@ impl ApState {
));

info!("Starting NET task");
spawner.must_spawn(net_task(ap_stack.clone(), net_task_control.token()));
spawner.must_spawn(ap_net_task(ap_stack.clone(), net_task_control.token()));

Self {
init,
Expand Down
Loading

0 comments on commit 305e42a

Please sign in to comment.