96 lines
3.2 KiB
C
96 lines
3.2 KiB
C
#include <stdint.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
#include <espressif/esp_common.h>
|
|
#include <FreeRTOS.h>
|
|
#include <task.h>
|
|
#include <dhcpserver.h>
|
|
#include <lwip/api.h>
|
|
|
|
#include "common.h"
|
|
|
|
/* This task ensures the network connection is up.
|
|
* If the network has not been configured yet,
|
|
* setup an AP with captive portal to allow
|
|
* network configuration.
|
|
*/
|
|
void NetworkConnectionTask(void *p) {
|
|
struct sdk_softap_config apconf;
|
|
struct sdk_station_config staconf;
|
|
uint8_t mac[6];
|
|
|
|
if (1 || !sdk_wifi_station_get_config(&staconf)) {
|
|
/* Station mode not configured: fallback to SoftAP configuration */
|
|
printf("sdk_wifi_station_get_config() failed\n");
|
|
if (!sdk_wifi_softap_get_config(&apconf)) {
|
|
/* SoftAP mode not configured: initialize default configuration */
|
|
printf("sdk_wifi_softap_get_config() failed\n");
|
|
sdk_wifi_get_macaddr(SOFTAP_IF, mac);
|
|
sprintf((char *)apconf.ssid, "UT61e-%02x%02x%02x", mac[3], mac[4], mac[5]);
|
|
apconf.ssid_len = strlen((char *)apconf.ssid);
|
|
apconf.channel = 1; /* TODO: auto channel selection? */
|
|
apconf.authmode = AUTH_OPEN;
|
|
apconf.ssid_hidden = 0;
|
|
apconf.max_connection = 4;
|
|
apconf.beacon_interval = 100;
|
|
sdk_wifi_softap_set_config(&apconf);
|
|
} else {
|
|
struct ip_info ap_ip;
|
|
ip_addr_t first_client_ip;
|
|
|
|
IP4_ADDR(&ap_ip.ip, 192, 168, 13, 1);
|
|
IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0);
|
|
IP4_ADDR(&ap_ip.netmask, 255, 255, 255, 0);
|
|
sdk_wifi_set_ip_info(1, &ap_ip);
|
|
|
|
IP4_ADDR(&first_client_ip, 192, 168, 13, 2);
|
|
dhcpserver_start(&first_client_ip, 4);
|
|
|
|
sdk_wifi_set_opmode(SOFTAP_MODE);
|
|
|
|
printf("SoftAP config:\n");
|
|
printf("SSID: %s\n", apconf.ssid);
|
|
printf("Password: %s\n", apconf.password);
|
|
printf("Channel: %d\n", apconf.channel);
|
|
printf("Authmode: %d\n", apconf.authmode);
|
|
for (;;)
|
|
vTaskDelay(TIME_MS(5000));
|
|
}
|
|
} else {
|
|
sdk_wifi_set_opmode(STATION_MODE);
|
|
sdk_wifi_station_connect();
|
|
printf("Station config:\n");
|
|
printf("SSID: %s\n", staconf.ssid);
|
|
printf("Password: %s\n", staconf.password);
|
|
printf("BSSID: %s\n", staconf.bssid);
|
|
printf("BSSID set: %d\n", staconf.bssid_set);
|
|
uint8_t st;
|
|
for (;;) {
|
|
switch (st = sdk_wifi_station_get_connect_status()) {
|
|
case STATION_IDLE:
|
|
printf("IDLE\n");
|
|
break;
|
|
case STATION_CONNECTING:
|
|
printf("CONNECTING\n");
|
|
break;
|
|
case STATION_WRONG_PASSWORD:
|
|
printf("WRONG_PASSWORD\n");
|
|
break;
|
|
case STATION_NO_AP_FOUND:
|
|
printf("NO_AP_FOUND\n");
|
|
break;
|
|
case STATION_CONNECT_FAIL:
|
|
printf("CONNECT_FAIL\n");
|
|
break;
|
|
case STATION_GOT_IP:
|
|
printf("GOT_IP\n");
|
|
break;
|
|
default:
|
|
printf("UNEXPECTED status: %d\n", st);
|
|
}
|
|
vTaskDelay(TIME_MS(750));
|
|
}
|
|
}
|
|
}
|