wifidmm/src/wifidmm.c

115 lines
3.6 KiB
C

#include <string.h>
#include <espressif/esp_common.h>
#include <esp/uart.h>
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <dhcpserver.h>
#include <lwip/api.h>
#define TIME_MS(ms) ((ms) / portTICK_PERIOD_MS)
void panic() {
printf("\n\n*** PANIC! ***\n");
for (;;);
}
/* 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 {
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);
}
} 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));
}
}
/* This task continuosly listen for incoming data
* on the UART line, parses the packets and feeds
* the meter readings to the network shipping task
* discarding invalid packets
*/
void SerialDataCollectionTask(void *p) {
for (;;)
vTaskDelay(TIME_MS(5000));
}
/* This task sends parsed measurements to the network */
void MeasurementShippingTask(void *p) {
for (;;)
vTaskDelay(TIME_MS(5000));
}
void user_init(void) {
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
xTaskCreate(NetworkConnectionTask, "NetworkConnection", 512, NULL, 1, NULL);
xTaskCreate(SerialDataCollectionTask, "SerialDataCollection", 512, NULL, 2, NULL);
xTaskCreate(MeasurementShippingTask, "MeasurementShipping", 512, NULL, 1, NULL);
}
/* vim: set et ai ts=4 sw=4: */