From d388ad422258ad251ae52463e1a26b65243f532c Mon Sep 17 00:00:00 2001 From: Krzysztof Mazur Date: Sun, 13 Nov 2022 15:25:50 +0100 Subject: [PATCH] Implement Access Point in setup mode Closes #6 --- src/CMakeLists.txt | 2 ++ src/access_point.cpp | 39 ++++++++++++++++++++++++++++++++++ src/access_point.h | 7 ++++++ src/double_reset_detector.h | 6 ++++++ src/{ssi.h => http_server.cpp} | 11 ++++++++-- src/http_server.h | 4 ++++ src/lwipopts.h | 2 ++ src/main.cpp | 34 +++++++++++++---------------- 8 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 src/access_point.cpp create mode 100644 src/access_point.h create mode 100644 src/double_reset_detector.h rename src/{ssi.h => http_server.cpp} (89%) create mode 100644 src/http_server.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4f2d291..3469a77 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,6 +21,8 @@ file(RENAME fsdata.c my_fsdata.c) add_executable(${PROGRAM_NAME} main.cpp double_reset_detector.cpp + access_point.cpp + http_server.cpp ) target_compile_definitions(${PROGRAM_NAME} PRIVATE WIFI_SSID=\"${WIFI_SSID}\" diff --git a/src/access_point.cpp b/src/access_point.cpp new file mode 100644 index 0000000..3484193 --- /dev/null +++ b/src/access_point.cpp @@ -0,0 +1,39 @@ +#include "access_point.h" + +#include +#include +#include + +namespace access_point { + +int stop_access_point(){ + autoip_stop(&cyw43_state.netif[CYW43_ITF_AP]); + return 0; +} + +int setup_access_point() { + if (!cyw43_is_initialized(&cyw43_state)){ + if (cyw43_arch_init()) { + printf("failed to initialise\n"); + return 1; + } + } + + const char *ap_name = "picow_webserver"; + const char *password = NULL; + + cyw43_arch_enable_ap_mode(ap_name, password, CYW43_AUTH_WPA2_AES_PSK); + // sleep mostly to let serial monitor catch the ip log. probably to be removed once mDNS is set up + sleep_ms(2000); + + autoip_start(&cyw43_state.netif[CYW43_ITF_AP]); + auto link_local_ip_addr = netif_autoip_data(&cyw43_state.netif[CYW43_ITF_AP])->llipaddr.addr; + printf("IP Address: %lu.%lu.%lu.%lu\n", + link_local_ip_addr & 0xFF, + (link_local_ip_addr >> 8) & 0xFF, + (link_local_ip_addr >> 16) & 0xFF, + link_local_ip_addr >> 24); + return 0; +} + +} // namespace access_point \ No newline at end of file diff --git a/src/access_point.h b/src/access_point.h new file mode 100644 index 0000000..045f2f7 --- /dev/null +++ b/src/access_point.h @@ -0,0 +1,7 @@ +#pragma once + +namespace access_point { + int stop_access_point(); + + int setup_access_point(); +} // namespace access_point \ No newline at end of file diff --git a/src/double_reset_detector.h b/src/double_reset_detector.h new file mode 100644 index 0000000..f3cfb78 --- /dev/null +++ b/src/double_reset_detector.h @@ -0,0 +1,6 @@ +#pragma once +namespace double_reset_detector { + +extern bool double_reset_detected; + +} // namespace double_reset_detector \ No newline at end of file diff --git a/src/ssi.h b/src/http_server.cpp similarity index 89% rename from src/ssi.h rename to src/http_server.cpp index 4879418..1355994 100644 --- a/src/ssi.h +++ b/src/http_server.cpp @@ -1,11 +1,11 @@ -#pragma once - #include "hardware/adc.h" #include "lwip/apps/httpd.h" #include "pico/cyw43_arch.h" #include "lwipopts.h" +namespace http_server { + // max length of the tags defaults to be 8 chars // LWIP_HTTPD_MAX_TAG_NAME_LEN const char * __not_in_flash("httpd") ssi_example_tags[] = { @@ -54,3 +54,10 @@ void ssi_init() { ssi_example_tags, LWIP_ARRAYSIZE(ssi_example_tags) ); } + +void start_http_server() { + httpd_init(); + ssi_init(); + printf("Http server initialized.\n"); +} +} // namespace http_server \ No newline at end of file diff --git a/src/http_server.h b/src/http_server.h new file mode 100644 index 0000000..8e237ed --- /dev/null +++ b/src/http_server.h @@ -0,0 +1,4 @@ +#pragma once +namespace http_server { + void start_http_server(); +} // namespace http_server \ No newline at end of file diff --git a/src/lwipopts.h b/src/lwipopts.h index e545ae1..4f01973 100644 --- a/src/lwipopts.h +++ b/src/lwipopts.h @@ -55,6 +55,8 @@ #define LWIP_STATS_DISPLAY 1 #endif +#define LWIP_AUTOIP 1 + #define LWIP_HTTPD 1 #define LWIP_HTTPD_SSI 1 // don't include the tag comment - less work for the CPU, but may be harder to debug diff --git a/src/main.cpp b/src/main.cpp index a8b34dc..ebd65dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,16 @@ #include #include -#include "lwip/apps/httpd.h" -#include "lwip/err.h" -#include "pico/stdlib.h" -#include "pico/cyw43_arch.h" +#include +#include +#include + +#include "double_reset_detector.h" +#include "http_server.h" +#include "access_point.h" #include "lwipopts.h" -#include "ssi.h" + namespace double_reset_detector { extern bool double_reset_detected; } @@ -26,28 +29,17 @@ int connect_to_wifi() { } else { printf("Connected.\n"); - extern cyw43_t cyw43_state; auto ip_addr = cyw43_state.netif[CYW43_ITF_STA].ip_addr.addr; printf("IP Address: %lu.%lu.%lu.%lu\n", ip_addr & 0xFF, (ip_addr >> 8) & 0xFF, (ip_addr >> 16) & 0xFF, ip_addr >> 24); } return ERR_OK; } -void run_server() { - if (connect_to_wifi() != ERR_OK){ - return; - } - httpd_init(); - ssi_init(); - printf("Http server initialized.\n"); - // infinite loop for now - for (;;) {} -} - void setup() { if (double_reset_detector::double_reset_detected) { printf("Double reset detected!\n"); - // just blink the LED for now + access_point::setup_access_point(); + http_server::start_http_server(); while (true) { cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); sleep_ms(500); @@ -67,5 +59,9 @@ int main() { // turn on LED to distinguish from BOOTSEL mode cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); setup(); - run_server(); + if (connect_to_wifi() != ERR_OK) { + return 1; + } + http_server::start_http_server(); + while (true) {}; } \ No newline at end of file