wifidmm/src/network.c

142 lines
4.6 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"
#include "network.h"
SemaphoreHandle_t semNetworks;
struct network_info networks[MAX_NETWORKS];
static void scan_done_cb(void *arg, sdk_scan_status_t status) {
int i;
if (xSemaphoreTake(semNetworks, TIME_MS(50)) == pdTRUE) {
if (status != SCAN_OK) {
networks[0].channel = networks[0].rssi = 0;
} else {
struct sdk_bss_info *bss = (struct sdk_bss_info *)arg;
// first one is invalid
bss = bss->next.stqe_next;
for (i=0; NULL != bss && i<MAX_NETWORKS; i++) {
memcpy(networks[i].bssid, bss->bssid, sizeof(networks[i].bssid));
memcpy(networks[i].ssid, bss->ssid, sizeof(networks[i].ssid));
networks[i].channel = bss->channel;
networks[i].rssi= bss->rssi;
networks[i].authmode = bss->authmode;
networks[i].is_hidden = bss->is_hidden;
bss = bss->next.stqe_next;
}
if (i < MAX_NETWORKS) {
networks[i].channel = networks[i].rssi = 0;
}
}
xSemaphoreGive(semNetworks);
}
}
void NetworkScanTask(void *p) {
networks[0].channel = networks[0].rssi = 0;
semNetworks = xSemaphoreCreateMutex();
for (;;) {
sdk_wifi_station_scan(NULL, scan_done_cb);
vTaskDelay(TIME_MS(5000));
}
}
/* 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));
}
}
}