Skip to content

Commit

Permalink
Test mDns
Browse files Browse the repository at this point in the history
Add modifications needed to the httpd freertos example

Fixes raspberrypi#272
  • Loading branch information
peterharperuk committed Oct 3, 2022
1 parent adc509b commit 42d24ce
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pico_w/freertos/httpd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_include_directories(picow_freertos_httpd_nosys PRIVATE
target_link_libraries(picow_freertos_httpd_nosys
pico_cyw43_arch_lwip_threadsafe_background
pico_lwip_http
pico_lwip_mdns
pico_stdlib
FreeRTOS-Kernel-Heap4 # FreeRTOS kernel and dynamic heap
)
Expand All @@ -35,6 +36,7 @@ target_include_directories(picow_freertos_httpd_sys PRIVATE
target_link_libraries(picow_freertos_httpd_sys
pico_cyw43_arch_lwip_sys_freertos
pico_lwip_http
pico_lwip_mdns
pico_stdlib
FreeRTOS-Kernel-Heap4 # FreeRTOS kernel and dynamic heap
)
Expand Down
2 changes: 1 addition & 1 deletion pico_w/freertos/httpd/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#define configUSE_TICK_HOOK 0
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES 32
#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 256
#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 1024
#define configUSE_16_BIT_TICKS 0

#define configIDLE_SHOULD_YIELD 1
Expand Down
8 changes: 7 additions & 1 deletion pico_w/freertos/httpd/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
// This example uses a common include to avoid repetition
#include "lwipopts_examples_common.h"

// The following is needed to test mDns
#define LWIP_MDNS_RESPONDER 1
#define LWIP_IGMP 1
#define LWIP_NUM_NETIF_CLIENT_DATA 1
#define MDNS_RESP_USENETIF_EXTCALLBACK 1

#if !NO_SYS
#define TCPIP_THREAD_STACKSIZE 1024
#define TCPIP_THREAD_STACKSIZE 2048 // mDNS needs more stack
#define DEFAULT_THREAD_STACKSIZE 1024
#define DEFAULT_RAW_RECVMBOX_SIZE 8
#define TCPIP_MBOX_SIZE 8
Expand Down
58 changes: 57 additions & 1 deletion pico_w/freertos/httpd/picow_freertos_httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "pico/stdlib.h"

#include "lwip/ip4_addr.h"
#include "lwip/apps/mdns.h"
#include "lwip/init.h"

#include "FreeRTOS.h"
#include "task.h"
Expand All @@ -20,12 +22,51 @@ void httpd_init(void);

#define TEST_TASK_PRIORITY ( tskIDLE_PRIORITY + 1UL )

#if LWIP_MDNS_RESPONDER
static void srv_txt(struct mdns_service *service, void *txt_userdata)
{
err_t res;
LWIP_UNUSED_ARG(txt_userdata);

res = mdns_resp_add_service_txtitem(service, "path=/", 6);
LWIP_ERROR("mdns add service txt failed\n", (res == ERR_OK), return);
}
#endif

#if LWIP_MDNS_RESPONDER
// Return some characters from the ascii representation of the mac address
// e.g. 112233445566
// chr_off is index of character in mac to start
// chr_len is length of result
// chr_off=8 and chr_len=4 would return "5566"
// Return number of characters put into destination
static size_t get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest_in) {
static const char hexchr[16] = "0123456789ABCDEF";
uint8_t mac[6];
char *dest = dest_in;
assert(chr_off + chr_len <= (2 * sizeof(mac)));
cyw43_hal_get_mac(idx, mac);
for (; chr_len && (chr_off >> 1) < sizeof(mac); ++chr_off, --chr_len) {
*dest++ = hexchr[mac[chr_off >> 1] >> (4 * (1 - (chr_off & 1))) & 0xf];
}
return dest - dest_in;
}
#endif

void main_task(__unused void *params) {
if (cyw43_arch_init()) {
printf("failed to initialise\n");
return;
}

cyw43_arch_enable_sta_mode();

char hostname[sizeof(CYW43_HOST_NAME) + 4];
memcpy(&hostname[0], CYW43_HOST_NAME, sizeof(CYW43_HOST_NAME) - 1);
get_mac_ascii(CYW43_HAL_MAC_WLAN0, 8, 4, &hostname[sizeof(CYW43_HOST_NAME) - 1]);
hostname[sizeof(hostname) - 1] = '\0';
netif_set_hostname(&cyw43_state.netif[CYW43_ITF_STA], hostname);

printf("Connecting to WiFi...\n");
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
printf("failed to connect.\n");
Expand All @@ -34,14 +75,29 @@ void main_task(__unused void *params) {
printf("Connected.\n");
}

#if LWIP_MDNS_RESPONDER
mdns_resp_init();
printf("mdns host name %s.local\n", hostname);
#if LWIP_VERSION_MAJOR >= 2 && LWIP_VERSION_MINOR >= 2
mdns_resp_add_netif(&cyw43_state.netif[CYW43_ITF_STA], hostname);
mdns_resp_add_service(&cyw43_state.netif[CYW43_ITF_STA], "picow_freertos_httpd", "_http", DNSSD_PROTO_TCP, 80, srv_txt, NULL);
#else
mdns_resp_add_netif(&cyw43_state.netif[CYW43_ITF_STA], hostname, 60);
mdns_resp_add_service(&cyw43_state.netif[CYW43_ITF_STA], "picow_freertos_httpd", "_http", DNSSD_PROTO_TCP, 80, 60, srv_txt, NULL);
#endif
#endif

printf("\nReady, running httpd at %s\n", ip4addr_ntoa(netif_ip4_addr(netif_list)));
httpd_init();

while(true) {
// not much to do as LED is in another task, and we're using RAW (callback) lwIP API
vTaskDelay(100);
}

#if LWIP_MDNS_RESPONDER
mdns_resp_remove_netif(&cyw43_state.netif[CYW43_ITF_STA]);
#endif

cyw43_arch_deinit();
}

Expand Down

0 comments on commit 42d24ce

Please sign in to comment.