70#include "pico/cyw43_arch.h"
71#include "pico/stdlib.h"
73#include "lwip/ip4_addr.h"
74#include "lwip/apps/mdns.h"
75#include "lwip/apps/mdns_priv.h"
77#include "lwip/apps/httpd.h"
80#include "arch/sys_arch.h"
81#include "pico/cyw43_arch_stk.h"
87#define STR_HELPER(x) #x
88#define STR(x) STR_HELPER(x)
106#if LWIP_HTTPD_SUPPORT_POST
107#define LED_STATE_BUFSIZE 4
109static const uint32_t FLAG_LED_UPDATE = (1U << 0);
110static const uint32_t FLAG_CONNECT_WIFI = (1U << 1);
111static const uint32_t FLAG_INIT_NET = (1U << 2);
112static const uint32_t FLAG_SHUTDOWN_NET = (1U << 3);
114static void *s_CurrentConnection;
115static absolute_time_t s_ConnectedTime;
116static bool s_LedOn =
false;
119static void RequestLedUpdate() { s_ControlFlags.
Set(FLAG_LED_UPDATE); }
120static void RequestConnectWiFi() { s_ControlFlags.
Set(FLAG_CONNECT_WIFI); }
121static void RequestInitNetwork() { s_ControlFlags.
Set(FLAG_INIT_NET); }
122static void RequestShutdownNetwork() { s_ControlFlags.
Set(FLAG_SHUTDOWN_NET); }
124#if LWIP_MDNS_RESPONDER
125static void srv_txt(
struct mdns_service *service,
void *txt_userdata)
128 LWIP_UNUSED_ARG(txt_userdata);
130 res = mdns_resp_add_service_txtitem(service,
"path=/", 6);
131 LWIP_ERROR(
"mdns add service txt failed\n", (res == ERR_OK),
return);
139static size_t get_mac_ascii(
int idx,
size_t chr_off,
size_t chr_len,
char *dest_in)
141 static const char hexchr[] =
"0123456789ABCDEF";
143 char *dest = dest_in;
144 assert(chr_off + chr_len <= (2 *
sizeof(mac)));
145 cyw43_hal_get_mac(idx, mac);
146 for (; chr_len && (chr_off >> 1) <
sizeof(mac); ++chr_off, --chr_len)
148 *dest++ = hexchr[mac[chr_off >> 1] >> (4 * (1 - (chr_off & 1))) & 0xf];
150 return dest - dest_in;
153static const char *cgi_handler_test(
int iIndex,
int iNumParams,
char *pcParam[],
char *pcValue[])
160 if (strcmp(pcParam[0],
"test") == 0)
162 return "/test.shtml";
165 return "/index.shtml";
168static const char *cgi_handler_restart(
int iIndex,
int iNumParams,
char *pcParam[],
char *pcValue[])
171 return "/index.shtml";
174static const tCGI CGI_HANDLERS[] =
176 {
"/", cgi_handler_test },
177 {
"/index.shtml", cgi_handler_test },
178 {
"/restart", cgi_handler_restart },
183u16_t ssi_example_ssi_handler(
int iIndex,
char *pcInsert,
int iInsertLen
185 , uint16_t current_tag_part, uint16_t *next_tag_part
194 printed = snprintf(pcInsert, iInsertLen,
"Pass");
199 printed = snprintf(pcInsert, iInsertLen,
"Hello from Pico");
204 uint64_t uptime_s = absolute_time_diff_us(s_ConnectedTime, get_absolute_time()) / 1e6;
205 printed = snprintf(pcInsert, iInsertLen,
"%d", (
int)uptime_s);
210 printed = snprintf(pcInsert, iInsertLen,
"%s", s_LedOn ?
"ON" :
"OFF");
215 printed = snprintf(pcInsert, iInsertLen,
"%s", s_LedOn ?
"OFF" :
"ON");
218#if LWIP_HTTPD_SSI_MULTIPART
221 printed = snprintf(pcInsert, iInsertLen,
"<tr><td>This is table row number %d</td></tr>", current_tag_part + 1);
224 if (current_tag_part < 9)
226 *next_tag_part = current_tag_part + 1;
237 return (u16_t)printed;
240err_t httpd_post_begin(
void *connection,
const char *uri,
const char *http_request,
241 u16_t http_request_len,
int content_len,
char *response_uri,
242 u16_t response_uri_len, u8_t *post_auto_wnd)
248 if (memcmp(uri,
"/led.cgi", 8) == 0 && s_CurrentConnection != connection)
250 s_CurrentConnection = connection;
251 snprintf(response_uri, response_uri_len,
"/ledfail.shtml");
261char *httpd_param_value(
struct pbuf *p,
const char *param_name,
char *value_buf,
size_t value_buf_len)
263 size_t param_len = strlen(param_name);
264 u16_t param_pos = pbuf_memfind(p, param_name, param_len, 0);
265 if (param_pos != 0xFFFF)
267 u16_t param_value_pos = param_pos + param_len;
268 u16_t param_value_len = 0;
269 u16_t tmp = pbuf_memfind(p,
"&", 1, param_value_pos);
272 param_value_len = tmp - param_value_pos;
276 param_value_len = p->tot_len - param_value_pos;
278 if (param_value_len > 0 && param_value_len < value_buf_len)
280 char *result = (
char *)pbuf_get_contiguous(p, value_buf, value_buf_len, param_value_len, param_value_pos);
283 result[param_value_len] = 0;
291err_t httpd_post_receive_data(
void *connection,
struct pbuf *p)
294 LWIP_ASSERT(
"NULL pbuf", p != NULL);
295 if (s_CurrentConnection == connection)
297 char buf[LED_STATE_BUFSIZE];
298 char *val = httpd_param_value(p,
"led_state=", buf,
sizeof(buf));
301 s_LedOn = (strcmp(val,
"ON") == 0) ?
true : false;
310void httpd_post_finished(
void *connection,
char *response_uri, u16_t response_uri_len)
312 snprintf(response_uri, response_uri_len,
"/ledfail.shtml");
313 if (s_CurrentConnection == connection)
315 snprintf(response_uri, response_uri_len,
"/ledpass.shtml");
317 s_CurrentConnection = NULL;
326 if (cyw43_arch_init())
328 printf(
"failed to initialise\n");
331 cyw43_arch_enable_sta_mode();
338#if LWIP_MDNS_RESPONDER
339 mdns_resp_remove_netif(&cyw43_state.netif[CYW43_ITF_STA]);
340 udp_remove(get_mdns_pcb());
344 cyw43_arch_disable_sta_mode();
350 char hostname[
sizeof(CYW43_HOST_NAME) + 4];
351 memcpy(&hostname[0], CYW43_HOST_NAME,
sizeof(CYW43_HOST_NAME) - 1);
352 get_mac_ascii(CYW43_HAL_MAC_WLAN0, 8, 4, &hostname[
sizeof(CYW43_HOST_NAME) - 1]);
353 hostname[
sizeof(hostname) - 1] =
'\0';
354 netif_set_hostname(&cyw43_state.netif[CYW43_ITF_STA], hostname);
356#if LWIP_MDNS_RESPONDER
358 cyw43_arch_lwip_begin();
360 printf(
"mdns host name %s.local\n", hostname);
361#if LWIP_VERSION_MAJOR >= 2 && LWIP_VERSION_MINOR >= 2
362 mdns_resp_add_netif(&cyw43_state.netif[CYW43_ITF_STA], hostname);
363 mdns_resp_add_service(&cyw43_state.netif[CYW43_ITF_STA],
"pico_httpd",
"_http", DNSSD_PROTO_TCP, 80, srv_txt, NULL);
365 mdns_resp_add_netif(&cyw43_state.netif[CYW43_ITF_STA], hostname, 60);
366 mdns_resp_add_service(&cyw43_state.netif[CYW43_ITF_STA],
"pico_httpd",
"_http", DNSSD_PROTO_TCP, 80, 60, srv_txt, NULL);
368 cyw43_arch_lwip_end();
371 cyw43_arch_lwip_begin();
373 http_set_cgi_handlers(CGI_HANDLERS, LWIP_ARRAYSIZE(CGI_HANDLERS));
374 http_set_ssi_handler(ssi_example_ssi_handler,
SSI_TAGS, LWIP_ARRAYSIZE(
SSI_TAGS));
375 cyw43_arch_lwip_end();
380 printf(
"Connecting to WiFi...\n");
383 printf(
"failed to connect.\n");
388 printf(
"Connected.\n");
390 printf(
"\nReady, running httpd at %s\n", ip4addr_ntoa(netif_ip4_addr(netif_list)));
393 s_ConnectedTime = get_absolute_time();
417 if (raised & FLAG_LED_UPDATE)
419 cyw43_arch_lwip_begin();
420 cyw43_gpio_set(&cyw43_state, 0, s_LedOn);
421 cyw43_arch_lwip_end();
423 if (raised & FLAG_CONNECT_WIFI)
427 RequestConnectWiFi();
430 if (raised & FLAG_INIT_NET)
435 RequestConnectWiFi();
439 RequestInitNetwork();
442 if (raised & FLAG_SHUTDOWN_NET)
445 RequestInitNetwork();
482 sys_arch_set_kernel(&kernel);
485 cyw43_arch_stk_preinit(&kernel, &timer_host);
488 RequestInitNetwork();
#define LWIP_HTTPD_SSI_MULTIPART
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Collection of synchronization primitives (stk::sync namespace).
Collection of time-related primitives (stk::time namespace).
Namespace of STK package.
@ ACCESS_USER
Unprivileged access mode (access to some hardware is restricted, see CPU manual for details)....
static constexpr Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
@ KERNEL_TICKLESS
Tickless mode. To use this mode STK_TICKLESS_IDLE must be defined to 1 in stk_config....
@ KERNEL_SYNC
Synchronization support (see Event).
@ KERNEL_DYNAMIC
Tasks can be added or removed and therefore exit when done.
EventFlagsT< uint32_t > EventFlags
Default event flags group with a 32-bit flags word (31 usable flags, bit 31 reserved).
Concrete implementation of IKernel.
void Start() override
Start the scheduler. This call does not return until all tasks have exited (KERNEL_DYNAMIC mode) or i...
EAddResult AddTask(ITask *user_task) override
Register task for a soft real-time (SRT) scheduling.
void Initialize(uint32_t resolution_us=PERIODICITY_DEFAULT) override
Initialize kernel.
Partial implementation of the user task.
T Set(T flags)
Set one or more flags.
static constexpr uint32_t OPT_WAIT_ANY
T Wait(T flags, uint32_t options=OPT_WAIT_ANY, Timeout timeout_ticks=WAIT_INFINITE)
Wait for one or more flags to be set.
static bool IsError(uint32_t result)
Software timer multiplexer that manages multiple Timer instances on top of a small fixed set of kerne...
EAddResult Initialize(IKernel *kernel, EAccessMode mode, const Config &cfg=Config())
Initialize timer host instance.
@ TASK_COUNT
total number of tasks serving this instance
void Run() override
Entry point of the user task.
static const char * SSI_TAGS[]
static bool ConnectWiFi()
static void ShutdownNetwork()
const char WIFI_PASSWORD[]
static bool InitNetwork()
static bool ConnectWiFi()
static void ShutdownNetwork()
static bool InitNetwork()