Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
Network.cpp
Go to the documentation of this file.
1
17#include "framework_config.h" // Must be included before DebugTrace.h to ensure framework_config.h is processed first
18#include "DebugTrace.h"
20
21#include "network/Network.h"
22#include <pico/cyw43_arch.h>
23#include <FreeRTOS.h>
24#include <task.h>
25
33#include "pico/version.h"
34
36bool Network::wifiConnected = false;
37
39bool Network::wifiInitialized = false;
40
42{
43 if (cyw43_arch_init())
44 {
45 printf("[Network] Failed to initialise Wi-Fi\n");
46 return false;
47 }
48
49 cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); // LED off during startup
50 cyw43_arch_enable_sta_mode();
51 wifiInitialized = true;
52 printf("[Network] Wi-Fi initialized successfully.\n");
53 cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); // LED on after startup
54 return true;
55}
56
58// @note This function will block until the Wi-Fi connection is established.
59// @note Make sure to include the necessary headers and define WIFI_SSID and WIFI_PASSWORD
60// in your build environment. For security, it is best defined in your evironment and
61// not hardcoded in the source code.
62
64{
65 if (!wifiInitialized)
66 {
67 if (!initialize())
68 {
69 return false;
70 }
71 wifiInitialized = true;
72 }
73
74 bool success = tryConnect(WIFI_MAX_RETRIES);
75
76 if (!success)
77 {
78 cyw43_arch_deinit(); // Only on total failure
79 }
80
81 return success;
82}
83
85{
86 if (isConnected() && getLinkStatus(CYW43_LINK_UP) == CYW43_LINK_UP)
87 {
88 return true; // Already connected
89 }
90
91 printf("[Network] Connection lost. Attempting reconnect...\n");
92
93 cyw43_arch_enable_sta_mode(); // do not re-init or deinit
94 return tryConnect(1); // One-shot reconnect attempt
95}
96
97bool Network::tryConnect(int attempts)
98{
99 cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); // LED during all connect attempts
100
101 uint32_t pm;
102 cyw43_wifi_get_pm(&cyw43_state, &pm);
103 cyw43_wifi_pm(&cyw43_state, CYW43_DEFAULT_PM & ~0xf); // disable power mgmt during connect
104
105 for (int i = 0; i < attempts; ++i)
106 {
107 printf("\n\n[Network] Connecting to WiFi SSID: %s (attempt %d)\n", WIFI_SSID, i + 1);
108
109 if (cyw43_arch_wifi_connect_async(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_MIXED_PSK) != 0)
110 {
111 printf("[Network] Failed to initiate connection.\n");
112 vTaskDelay(pdMS_TO_TICKS(WIFI_RETRY_TIMEOUT_MS));
113 continue;
114 }
115
116 int status = 0;
117 int waitMs = 0;
118 do
119 {
120 status = Network::getLinkStatus(status);
121 vTaskDelay(pdMS_TO_TICKS(1000));
122 waitMs += 1000;
123 } while (status != CYW43_LINK_UP && waitMs < WIFI_RETRY_TIMEOUT_MS);
124
125 if (status == CYW43_LINK_UP)
126 {
127 wifiConnected = true;
128 printf("[Network] Connected to Wi-Fi network at %s\n", ip4addr_ntoa(netif_ip4_addr(netif_list)));
129 cyw43_wifi_pm(&cyw43_state, pm);
130 return true;
131 }
132
133 printf("[Network] Attempt %d failed.\n", i + 1);
134 }
135
136 wifiConnected = false;
137 cyw43_wifi_pm(&cyw43_state, pm);
138 return false;
139}
140
142// @note This function checks the connection status of the Wi-Fi interface.
143// It returns true if the device is connected to a Wi-Fi network, and false otherwise.
145{
146 return wifiConnected;
147}
148
150{
151 printf("[Network] Forcing Wi-Fi restart...\n");
152
153 cyw43_arch_deinit();
154 wifiConnected = false;
155
157}
158
161{
162 cyw43_arch_deinit();
163}
164
166// @note This function retrieves the current link status of the Wi-Fi interface.
167// It returns an integer representing the link status, which can be one of the following:
168// - CYW43_LINK_UP: The link is up and connected to a network.
169// - CYW43_LINK_NOIP: The link is up but no IP address has been acquired.
170// - CYW43_LINK_JOIN: The device is in the process of joining a network.
171// - CYW43_LINK_DOWN: The link is down and not connected to any network.
172// @note The function prints the current link status to the console for debugging purposes.
173int Network::getLinkStatus(int lastStatus)
174{
175
176 int status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
177 switch (status)
178 {
179 case CYW43_LINK_UP:
180 printf("\n[Network] Link is up\n");
181 break;
182
183 case CYW43_LINK_NOIP:
184 if (lastStatus == CYW43_LINK_NOIP)
185 {
186 printf(".");
187 }
188 else
189 {
190 printf("\n[Network] Acquiring IP address ");
191 }
192 break;
193
194 case CYW43_LINK_JOIN:
195 if (lastStatus == CYW43_LINK_JOIN)
196 {
197 printf(".");
198 }
199 else
200 {
201 printf("\n[Network] Joining network ");
202 }
203 break;
204
205 case CYW43_LINK_DOWN:
206 printf("\n[Network] Link is down\n");
207 break;
208 }
209 return status;
210}
Macro-based debug trace system with optional SD file logging.
#define TRACE_INIT(MODULE_NAME)
Declare trace usage in a source file for a given module.
Definition DebugTrace.h:170
Static class providing Wi-Fi and network control on the Pico W.
Definition Network.h:27
static bool initialize()
Start Wi-Fi with resilience, retrying connection if it fails.
Definition Network.cpp:41
static bool wifiConnected
Make the Wi-Fi connection in station mode Requires WIFI_SSID and WIFI_PASSWORD to be set in the envir...
Definition Network.h:104
static bool checkAndReconnect()
Attempt to connect to Wi-Fi with retries.
Definition Network.cpp:84
static bool wifiInitialized
Definition Network.h:105
static bool startWifiWithResilience()
Start Wi-Fi with resilience, retrying connection if it fails.
Definition Network.cpp:63
static bool tryConnect(int attempts)
Try to connect to Wi-Fi network.
Definition Network.cpp:97
static bool isConnected()
Check whether the device is connected to Wi-Fi.
Definition Network.cpp:144
static void wifi_deinit()
Deinitialize the CYW43 Wi-Fi stack.
Definition Network.cpp:160
static bool restart_wifi()
Restart the Wi-Fi interface.
Definition Network.cpp:149
static int getLinkStatus(int lastStatus)
Get the current link status from the Wi-Fi interface.
Definition Network.cpp:173
Delegates to user or system configuration.
#define WIFI_RETRY_TIMEOUT_MS
This setting defines the retry timeout for WiFi connection The default is 15000 ms (15 seconds)
#define WIFI_MAX_RETRIES
This setting defines the maximum number of retries for WiFi connection.