From 4b09a8611b2db6d3675410f873c522f22456b2e6 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Sun, 13 Apr 2025 19:14:43 +0200 Subject: [PATCH] wifi scan --- CmakeLists.txt | 13 ++++++ lwipopts.h | 10 +++++ lwipopts_examples_common.h | 92 ++++++++++++++++++++++++++++++++++++++ wifi_scan.c | 59 ++++++++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 lwipopts.h create mode 100644 lwipopts_examples_common.h create mode 100644 wifi_scan.c diff --git a/CmakeLists.txt b/CmakeLists.txt index 412eef0..bf4391b 100644 --- a/CmakeLists.txt +++ b/CmakeLists.txt @@ -28,3 +28,16 @@ target_link_libraries( node1 pico_stdlib hardware_i2c) # create map/bin/hex file etc. pico_add_extra_outputs( node1 ) + +add_executable(wifi_scan wifi_scan.c) +target_include_directories(wifi_scan PRIVATE + ${CMAKE_CURRENT_LIST_DIR} + ${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts + ) + +target_link_libraries(wifi_scan + pico_cyw43_arch_lwip_threadsafe_background + pico_stdlib + ) + +pico_add_extra_outputs(wifi_scan) diff --git a/lwipopts.h b/lwipopts.h new file mode 100644 index 0000000..8c1301e --- /dev/null +++ b/lwipopts.h @@ -0,0 +1,10 @@ +#ifndef _LWIPOPTS_H +#define _LWIPOPTS_H + +// Generally you would define your own explicit list of lwIP options +// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html) +// +// This example uses a common include to avoid repetition +#include "lwipopts_examples_common.h" + +#endif \ No newline at end of file diff --git a/lwipopts_examples_common.h b/lwipopts_examples_common.h new file mode 100644 index 0000000..129d42b --- /dev/null +++ b/lwipopts_examples_common.h @@ -0,0 +1,92 @@ +#ifndef _LWIPOPTS_EXAMPLE_COMMONH_H +#define _LWIPOPTS_EXAMPLE_COMMONH_H + + +// Common settings used in most of the pico_w examples +// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details) + +// allow override in some examples +#ifndef NO_SYS +#define NO_SYS 1 +#endif +// allow override in some examples +#ifndef LWIP_SOCKET +#define LWIP_SOCKET 0 +#endif +#if PICO_CYW43_ARCH_POLL +#define MEM_LIBC_MALLOC 1 +#else +// MEM_LIBC_MALLOC is incompatible with non polling versions +#define MEM_LIBC_MALLOC 0 +#endif +#define MEM_ALIGNMENT 4 +#ifndef MEM_SIZE +#define MEM_SIZE 4000 +#endif +#define MEMP_NUM_TCP_SEG 32 +#define MEMP_NUM_ARP_QUEUE 10 +#define PBUF_POOL_SIZE 24 +#define LWIP_ARP 1 +#define LWIP_ETHERNET 1 +#define LWIP_ICMP 1 +#define LWIP_RAW 1 +#define TCP_WND (8 * TCP_MSS) +#define TCP_MSS 1460 +#define TCP_SND_BUF (8 * TCP_MSS) +#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS)) +#define LWIP_NETIF_STATUS_CALLBACK 1 +#define LWIP_NETIF_LINK_CALLBACK 1 +#define LWIP_NETIF_HOSTNAME 1 +#define LWIP_NETCONN 0 +#define MEM_STATS 0 +#define SYS_STATS 0 +#define MEMP_STATS 0 +#define LINK_STATS 0 +// #define ETH_PAD_SIZE 2 +#define LWIP_CHKSUM_ALGORITHM 3 +#define LWIP_DHCP 1 +#define LWIP_IPV4 1 +#define LWIP_TCP 1 +#define LWIP_UDP 1 +#define LWIP_DNS 1 +#define LWIP_TCP_KEEPALIVE 1 +#define LWIP_NETIF_TX_SINGLE_PBUF 1 +#define DHCP_DOES_ARP_CHECK 0 +#define LWIP_DHCP_DOES_ACD_CHECK 0 + +#ifndef NDEBUG +#define LWIP_DEBUG 1 +#define LWIP_STATS 1 +#define LWIP_STATS_DISPLAY 1 +#endif + +#define ETHARP_DEBUG LWIP_DBG_OFF +#define NETIF_DEBUG LWIP_DBG_OFF +#define PBUF_DEBUG LWIP_DBG_OFF +#define API_LIB_DEBUG LWIP_DBG_OFF +#define API_MSG_DEBUG LWIP_DBG_OFF +#define SOCKETS_DEBUG LWIP_DBG_OFF +#define ICMP_DEBUG LWIP_DBG_OFF +#define INET_DEBUG LWIP_DBG_OFF +#define IP_DEBUG LWIP_DBG_OFF +#define IP_REASS_DEBUG LWIP_DBG_OFF +#define RAW_DEBUG LWIP_DBG_OFF +#define MEM_DEBUG LWIP_DBG_OFF +#define MEMP_DEBUG LWIP_DBG_OFF +#define SYS_DEBUG LWIP_DBG_OFF +#define TCP_DEBUG LWIP_DBG_OFF +#define TCP_INPUT_DEBUG LWIP_DBG_OFF +#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF +#define TCP_RTO_DEBUG LWIP_DBG_OFF +#define TCP_CWND_DEBUG LWIP_DBG_OFF +#define TCP_WND_DEBUG LWIP_DBG_OFF +#define TCP_FR_DEBUG LWIP_DBG_OFF +#define TCP_QLEN_DEBUG LWIP_DBG_OFF +#define TCP_RST_DEBUG LWIP_DBG_OFF +#define UDP_DEBUG LWIP_DBG_OFF +#define TCPIP_DEBUG LWIP_DBG_OFF +#define PPP_DEBUG LWIP_DBG_OFF +#define SLIP_DEBUG LWIP_DBG_OFF +#define DHCP_DEBUG LWIP_DBG_OFF + +#endif /* __LWIPOPTS_H__ */ \ No newline at end of file diff --git a/wifi_scan.c b/wifi_scan.c new file mode 100644 index 0000000..e829173 --- /dev/null +++ b/wifi_scan.c @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include + +#include "pico/stdlib.h" +#include "pico/cyw43_arch.h" + +static int scan_result(void *env, const cyw43_ev_scan_result_t *result) { + if (result) { + printf("ssid: %-32s rssi: %4d chan: %3d mac: %02x:%02x:%02x:%02x:%02x:%02x sec: %u\n", + result->ssid, result->rssi, result->channel, + result->bssid[0], result->bssid[1], result->bssid[2], result->bssid[3], result->bssid[4], result->bssid[5], + result->auth_mode); + } + return 0; +} + +#include "hardware/vreg.h" +#include "hardware/clocks.h" + +int main() { + stdio_init_all(); + + if (cyw43_arch_init()) { + printf("failed to initialise\n"); + return 1; + } + + cyw43_arch_enable_sta_mode(); + + absolute_time_t scan_time = nil_time; + bool scan_in_progress = false; + while(true) { + if (absolute_time_diff_us(get_absolute_time(), scan_time) < 0) { + if (!scan_in_progress) { + cyw43_wifi_scan_options_t scan_options = {0}; + int err = cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, scan_result); + if (err == 0) { + printf("\nPerforming wifi scan\n"); + scan_in_progress = true; + } else { + printf("Failed to start scan: %d\n", err); + scan_time = make_timeout_time_ms(10000); // wait 10s and scan again + } + } else if (!cyw43_wifi_scan_active(&cyw43_state)) { + scan_time = make_timeout_time_ms(10000); // wait 10s and scan again + scan_in_progress = false; + } + } + sleep_ms(1000); + } + + cyw43_arch_deinit(); + return 0; +}