2021-06-19 14:42:38 +00:00
|
|
|
#include <espressif/esp_common.h>
|
|
|
|
#include <esp/uart.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-07-09 19:35:41 +00:00
|
|
|
#include "system.h"
|
2021-07-02 16:45:29 +00:00
|
|
|
#include "wifi.h"
|
|
|
|
#include "web.h"
|
|
|
|
#include "mqtt.h"
|
|
|
|
#include "lux.h"
|
2021-06-19 14:42:38 +00:00
|
|
|
|
2021-06-21 19:35:22 +00:00
|
|
|
#define LED_PIN 2
|
|
|
|
#define SWITCH_PIN 2
|
2021-06-19 14:42:38 +00:00
|
|
|
|
2021-06-21 19:35:22 +00:00
|
|
|
/* Add extras/sntp component to makefile for this include to work */
|
|
|
|
#include <sntp.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
const int cs0 = 15;
|
|
|
|
const int gpio4 = 4;
|
|
|
|
const int gpio5 = 5;
|
|
|
|
|
|
|
|
const int led_number = 8;
|
|
|
|
|
|
|
|
static ws2812_pixel_t next_colour(int i) {
|
|
|
|
ws2812_pixel_t colour = {{0, 0, 0, 0}};
|
|
|
|
if(i == 8) {
|
|
|
|
colour.white = 32;
|
|
|
|
} else {
|
|
|
|
colour.red = i & 1 ? 32 : 0;
|
|
|
|
colour.green = i & 2 ? 32 : 0;
|
|
|
|
colour.blue = i & 4 ? 32 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return colour;
|
|
|
|
}
|
|
|
|
|
|
|
|
void spi_dac(int id, int val) {
|
|
|
|
int dac_val = (val << 2) & 0x3FFC;
|
|
|
|
|
|
|
|
spi_transfer_8(1, ~(0x00));
|
|
|
|
gpio_write(cs0, 1);
|
|
|
|
gpio_write(cs0, 0);
|
|
|
|
spi_transfer_8(1, ~(0x01 << id));
|
|
|
|
gpio_write(cs0, 1);
|
|
|
|
gpio_write(cs0, 0);
|
|
|
|
|
|
|
|
spi_transfer_16(1, dac_val);
|
|
|
|
|
|
|
|
spi_transfer_8(1, ~(0x00));
|
|
|
|
gpio_write(cs0, 1);
|
|
|
|
gpio_write(cs0, 0);
|
|
|
|
spi_transfer_8(1, ~(0x01 << id));
|
|
|
|
gpio_write(cs0, 1);
|
|
|
|
gpio_write(cs0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This task uses the high level GPIO API (esp_gpio.h) to blink an LED.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void blinkenTask(void *pvParameters) {
|
|
|
|
|
|
|
|
gpio_enable(9, GPIO_INPUT);
|
|
|
|
gpio_enable(10, GPIO_INPUT);
|
|
|
|
|
|
|
|
ws2812_pixel_t pixels[led_number];
|
|
|
|
ws2812_i2s_init(led_number, PIXEL_RGBW);
|
|
|
|
memset(pixels, 0, sizeof(ws2812_pixel_t) * led_number);
|
|
|
|
|
|
|
|
gpio_enable(cs0, GPIO_OUTPUT);
|
|
|
|
gpio_enable(gpio4, GPIO_OUTPUT);
|
|
|
|
gpio_enable(gpio5, GPIO_OUTPUT);
|
|
|
|
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_BIG_ENDIAN, 1);
|
|
|
|
while (1) {
|
|
|
|
gpio_write(gpio4, 1);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
|
|
gpio_write(gpio4, 0);
|
|
|
|
for (int j = 0; j < 64; j++) {
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
spi_dac(i, 64 * j);
|
|
|
|
//printf("> %d\n", 64*j);
|
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
spi_dac(i, 0);
|
|
|
|
|
|
|
|
gpio_write(gpio5, 1);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
|
|
gpio_write(gpio5, 0);
|
|
|
|
|
|
|
|
for (int c = 8; c >= 0; c--) {
|
|
|
|
|
|
|
|
for (int i = 0; i < led_number; i++) {
|
|
|
|
pixels[i] = next_colour(c);
|
|
|
|
}
|
|
|
|
ws2812_i2s_update(pixels, PIXEL_RGBW);
|
|
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \
|
|
|
|
"2.pool.ntp.org", "3.pool.ntp.org"
|
|
|
|
|
|
|
|
#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_PERIOD_MS)
|
|
|
|
#define UNUSED_ARG(x) (void)x
|
|
|
|
|
|
|
|
const gpio_inttype_t int_type = GPIO_INTTYPE_EDGE_NEG;
|
|
|
|
|
|
|
|
|
|
|
|
const char *gpio_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) {
|
|
|
|
for (int i = 0; i < iNumParams; i++) {
|
|
|
|
if(strcmp(pcParam[i], "on") == 0) {
|
|
|
|
uint8_t gpio_num = atoi(pcValue[i]);
|
|
|
|
gpio_enable(gpio_num, GPIO_OUTPUT);
|
|
|
|
gpio_write(gpio_num, true);
|
|
|
|
} else if(strcmp(pcParam[i], "off") == 0) {
|
|
|
|
uint8_t gpio_num = atoi(pcValue[i]);
|
|
|
|
gpio_enable(gpio_num, GPIO_OUTPUT);
|
|
|
|
gpio_write(gpio_num, false);
|
|
|
|
} else if(strcmp(pcParam[i], "toggle") == 0) {
|
|
|
|
uint8_t gpio_num = atoi(pcValue[i]);
|
|
|
|
gpio_enable(gpio_num, GPIO_OUTPUT);
|
|
|
|
gpio_toggle(gpio_num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "/index.html";
|
|
|
|
}
|
|
|
|
|
|
|
|
void websocket_task(void *pvParameter) {
|
|
|
|
struct tcp_pcb *pcb = (struct tcp_pcb *) pvParameter;
|
|
|
|
|
|
|
|
int connstarttime = xTaskGetTickCount();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if(pcb == NULL || pcb->state != ESTABLISHED) {
|
|
|
|
printf("Connection closed, deleting task\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Global Info
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
int uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000;
|
|
|
|
int heap = (int) xPortGetFreeHeapSize();
|
|
|
|
uint32_t chip_id = sdk_system_get_chip_id();
|
|
|
|
uint32_t flash_id = sdk_spi_flash_get_id();
|
|
|
|
uint32_t flash_size = sdk_flashchip.chip_size >> 10;
|
|
|
|
char *hostname = NULL;
|
|
|
|
|
|
|
|
sysparam_get_string("hostname", &hostname);
|
|
|
|
/* Generate response in JSON format */
|
|
|
|
char response[160];
|
|
|
|
int len = snprintf(response, sizeof(response),
|
|
|
|
"{\"walltime\" : \"%d\","
|
|
|
|
"\"uptime\" : \"%d\","
|
|
|
|
" \"heap\" : \"%d\","
|
|
|
|
" \"chipid\" : \"%08x\","
|
|
|
|
" \"flashid\" : \"0x%08x\","
|
|
|
|
" \"flashsize\" : \"%u\","
|
|
|
|
" \"hostname\" : \"%s\""
|
|
|
|
"}", (int) tv.tv_sec, uptime, heap, chip_id, flash_id, flash_size, hostname);
|
|
|
|
free(hostname);
|
|
|
|
if(len < sizeof(response)) {
|
|
|
|
LOCK_TCPIP_CORE();
|
|
|
|
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
|
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
} else
|
|
|
|
printf("buffer too small 1");
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
|
|
|
|
|
|
|
|
|
|
//Connection Info
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
int connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000;
|
|
|
|
|
|
|
|
printf("conn %d: "
|
|
|
|
IPSTR
|
|
|
|
" <-> "
|
|
|
|
IPSTR
|
|
|
|
" \n", pcb->netif_idx, IP2STR(&pcb->local_ip), IP2STR(&pcb->remote_ip));
|
|
|
|
char response[160];
|
|
|
|
int len = snprintf(response, sizeof(response),
|
|
|
|
"{\"connage\" : \"%d\","
|
|
|
|
"\"clientip\" : \""
|
|
|
|
IPSTR
|
|
|
|
"\""
|
|
|
|
"}", connuptime, IP2STR(&pcb->remote_ip));
|
|
|
|
if(len < sizeof(response)) {
|
|
|
|
LOCK_TCPIP_CORE();
|
|
|
|
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
|
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
} else
|
|
|
|
printf("buffer too small 1");
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
|
|
|
|
|
|
{
|
|
|
|
uint8_t opmode = sdk_wifi_get_opmode();
|
|
|
|
const char *opmode_str = "??";
|
|
|
|
switch (opmode) {
|
|
|
|
case NULL_MODE:
|
|
|
|
opmode_str = "Null";
|
|
|
|
break;
|
|
|
|
case STATION_MODE:
|
|
|
|
opmode_str = "Station";
|
|
|
|
break;
|
|
|
|
case SOFTAP_MODE:
|
|
|
|
opmode_str = "SoftAP";
|
|
|
|
break;
|
|
|
|
case STATIONAP_MODE:
|
|
|
|
opmode_str = "StationAP";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*struct sockaddr_storage addr;
|
|
|
|
socklen_t addr_len = sizeof(addr);
|
|
|
|
if (getpeername(s, (struct sockaddr *)&addr, &addr_len) == 0) {
|
|
|
|
printf("peer\n");
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
if(opmode == SOFTAP_MODE || opmode == STATIONAP_MODE) {
|
|
|
|
uint8_t hwaddr[6];
|
|
|
|
sdk_wifi_get_macaddr(SOFTAP_IF, hwaddr);
|
|
|
|
struct ip_info info;
|
|
|
|
sdk_wifi_get_ip_info(SOFTAP_IF, &info);
|
|
|
|
|
|
|
|
char *apssid = NULL;
|
|
|
|
sysparam_get_string("wifi_ap_ssid", &apssid);
|
|
|
|
|
|
|
|
/* Generate response in JSON format */
|
|
|
|
char response[128];
|
|
|
|
int len = snprintf(response, sizeof(response),
|
|
|
|
"{\"opmode\" : \"%s\","
|
|
|
|
" \"apssid\" : \"%s\","
|
|
|
|
" \"apip\" : \""
|
|
|
|
IPSTR
|
|
|
|
"\","
|
|
|
|
" \"apmac\" : \""
|
|
|
|
MACSTR
|
|
|
|
"\""
|
|
|
|
"}", opmode_str, apssid, IP2STR(&info.ip), MAC2STR(hwaddr));
|
|
|
|
free(apssid);
|
|
|
|
if(len < sizeof(response)) {
|
|
|
|
LOCK_TCPIP_CORE();
|
|
|
|
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
|
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
} else
|
|
|
|
printf("buffer too small 2");
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
|
|
|
|
|
|
if(opmode == STATION_MODE || opmode == STATIONAP_MODE) {
|
|
|
|
uint8_t hwaddr[6];
|
|
|
|
sdk_wifi_get_macaddr(STATION_IF, hwaddr);
|
|
|
|
struct ip_info info;
|
|
|
|
sdk_wifi_get_ip_info(STATION_IF, &info);
|
|
|
|
char *stassid = NULL;
|
|
|
|
sysparam_get_string("wifi_sta_ssid", &stassid);
|
|
|
|
|
|
|
|
/* Generate response in JSON format */
|
|
|
|
char response[128];
|
|
|
|
int len = snprintf(response, sizeof(response),
|
|
|
|
"{\"opmode\" : \"%s\","
|
|
|
|
" \"stassid\" : \"%s\","
|
|
|
|
" \"staip\" : \""
|
|
|
|
IPSTR
|
|
|
|
"\","
|
|
|
|
" \"stamac\" : \""
|
|
|
|
MACSTR
|
|
|
|
"\""
|
|
|
|
"}", opmode_str, stassid, IP2STR(&info.ip), MAC2STR(hwaddr));
|
|
|
|
free(stassid);
|
|
|
|
if(len < sizeof(response)) {
|
|
|
|
LOCK_TCPIP_CORE();
|
|
|
|
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
|
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
} else
|
|
|
|
printf("buffer too small 3");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
|
|
//printf("9: %d\n",gpio_read(9));
|
|
|
|
//printf("10: %d\n",gpio_read(10));
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is called when websocket frame is received.
|
|
|
|
*
|
|
|
|
* Note: this function is executed on TCP thread and should return as soon
|
|
|
|
* as possible.
|
|
|
|
*/
|
|
|
|
void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, uint8_t mode) {
|
|
|
|
|
|
|
|
uint8_t response[3];
|
|
|
|
uint16_t val = 0;
|
|
|
|
char cmd = '0';
|
|
|
|
|
|
|
|
uint8_t en = 0;
|
|
|
|
/*uint8_t ap_disable_if_sta = 0;
|
|
|
|
uint8_t ssid_hidden = 0;
|
|
|
|
uint8_t dns_enable = 0;
|
|
|
|
uint8_t mdns_enable = 0;*/
|
|
|
|
|
|
|
|
switch (data[0]) {
|
|
|
|
case 'V': // ADC
|
|
|
|
/* This should be done on a separate thread in 'real' applications */
|
|
|
|
val = sdk_system_adc_read();
|
|
|
|
cmd = 'V';
|
|
|
|
break;
|
|
|
|
case 'R': // Restart
|
|
|
|
cmd = 'R';
|
|
|
|
break;
|
|
|
|
case 'X': // Clear Config
|
|
|
|
cmd = 'X';
|
|
|
|
break;
|
|
|
|
case 'D': // Disable LED
|
|
|
|
gpio_write(LED_PIN, true);
|
|
|
|
val = 0xDEAD;
|
|
|
|
cmd = 'G';
|
|
|
|
break;
|
|
|
|
case 'E': // Enable LED
|
|
|
|
gpio_write(LED_PIN, false);
|
|
|
|
val = 0xBEEF;
|
|
|
|
cmd = 'G';
|
|
|
|
break;
|
|
|
|
case 'S': {
|
|
|
|
if(data[1] == 'E')
|
|
|
|
en = 1;
|
|
|
|
char *ssid = &data[2];
|
|
|
|
size_t ssid_len = strlen(ssid);
|
|
|
|
char *password = &data[3 + ssid_len];
|
|
|
|
size_t password_len = strlen(password);
|
|
|
|
|
|
|
|
sysparam_set_int8("wifi_sta_enable", en);
|
|
|
|
sysparam_set_string("wifi_sta_ssid", ssid);
|
|
|
|
sysparam_set_string("wifi_sta_password", password);
|
|
|
|
}
|
|
|
|
cmd = 'S';
|
|
|
|
break;
|
|
|
|
case 'A': {
|
|
|
|
if(data[1] == 'E')
|
|
|
|
en = 1;
|
|
|
|
char *ssid = &data[2];
|
|
|
|
size_t ssid_len = strlen(ssid);
|
|
|
|
char *password = &data[3 + ssid_len];
|
|
|
|
size_t password_len = strlen(password);
|
|
|
|
|
|
|
|
sysparam_set_int8("wifi_ap_enable", en);
|
|
|
|
sysparam_set_string("wifi_ap_ssid", ssid);
|
|
|
|
sysparam_set_string("wifi_ap_password", password);
|
|
|
|
|
|
|
|
//sysparam_set_int8("wifi_ap_disable_if_sta", ap_disable_if_sta);
|
|
|
|
//sysparam_set_int8("wifi_ap_ssid_hidden", ssid_hidden);
|
|
|
|
//sysparam_set_int8("wifi_ap_dns", dns_enable);
|
|
|
|
//sysparam_set_int8("wifi_ap_mdns", mdns_enable);
|
|
|
|
}
|
|
|
|
cmd = 'A';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data);
|
|
|
|
printf("Unknown command\n");
|
|
|
|
val = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
response[2] = (uint8_t) val;
|
|
|
|
response[1] = val >> 8;
|
|
|
|
response[0] = cmd;
|
|
|
|
|
|
|
|
websocket_write(pcb, response, 3, WS_BIN_MODE);
|
|
|
|
|
|
|
|
if(data[0] == 'R') { // Restart
|
|
|
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
|
|
vPortEnterCritical();
|
|
|
|
sdk_system_restart();
|
|
|
|
} else if(data[0] == 'X') { // Clear Config
|
|
|
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
|
|
vPortEnterCritical();
|
|
|
|
uint32_t num_sectors = 5 + DEFAULT_SYSPARAM_SECTORS;
|
|
|
|
uint32_t start = sdk_flashchip.chip_size - num_sectors * sdk_flashchip.sector_size;
|
|
|
|
for (uint32_t i = 0; i < num_sectors; i++) {
|
|
|
|
spiflash_erase_sector(start + i * sdk_flashchip.sector_size);
|
|
|
|
}
|
|
|
|
sdk_system_restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is called when new websocket is open and
|
|
|
|
* creates a new websocket_task if requested URI equals '/stream'.
|
|
|
|
*/
|
|
|
|
void websocket_open_cb(struct tcp_pcb *pcb, const char *uri) {
|
|
|
|
printf("WS URI: %s\n", uri);
|
|
|
|
if(!strcmp(uri, "/stream")) {
|
|
|
|
xTaskCreate(&websocket_task, "websocket_task", 512, (void *) pcb, 2, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void httpd_task(void *pvParameters) {
|
|
|
|
tCGI pCGIs[] = {
|
|
|
|
{"/gpio", (tCGIHandler) gpio_cgi_handler},
|
|
|
|
};
|
|
|
|
|
|
|
|
/* register handlers and start the server */
|
|
|
|
http_set_cgi_handlers(pCGIs, sizeof(pCGIs) / sizeof(pCGIs[0]));
|
|
|
|
websocket_register_callbacks((tWsOpenHandler) websocket_open_cb,
|
|
|
|
(tWsHandler) websocket_cb);
|
|
|
|
httpd_init();
|
|
|
|
|
|
|
|
for (;;);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void sntp_task(void *pvParameters) {
|
|
|
|
const char *servers[] = {SNTP_SERVERS};
|
|
|
|
UNUSED_ARG(pvParameters);
|
|
|
|
|
|
|
|
/* Wait until we have joined AP and are assigned an IP */
|
|
|
|
while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
|
|
|
|
vTaskDelayMs(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start SNTP */
|
|
|
|
printf("Starting SNTP... ");
|
|
|
|
/* SNTP will request an update each 5 minutes */
|
|
|
|
sntp_set_update_delay(5 * 60000);
|
|
|
|
/* Set GMT+1 zone, daylight savings off */
|
|
|
|
const struct timezone tz = {1 * 60, 1};
|
|
|
|
/* SNTP initialization */
|
|
|
|
sntp_initialize(&tz);
|
|
|
|
/* Servers must be configured right after initialization */
|
|
|
|
sntp_set_servers(servers, sizeof(servers) / sizeof(char *));
|
|
|
|
printf("DONE!\n");
|
|
|
|
|
|
|
|
/* Print date and time each 5 seconds */
|
|
|
|
while (1) {
|
|
|
|
vTaskDelayMs(5000);
|
|
|
|
//time_t ts = time(NULL);
|
|
|
|
//int t = ts;
|
|
|
|
//printf("TIME: %d %d %s", t,(int) day_seconds(), ctime(&ts));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dns_task(void *pvParameters) {
|
|
|
|
char *wifi_ap_ip_addr = NULL;
|
|
|
|
sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr);
|
|
|
|
if(!wifi_ap_ip_addr) {
|
|
|
|
printf("dns: no ip address\n");
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
ip4_addr_t server_addr;
|
|
|
|
server_addr.addr = ipaddr_addr(wifi_ap_ip_addr);
|
|
|
|
|
|
|
|
#if LWIP_IPV6
|
|
|
|
int fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
struct sockaddr_in6 serv_addr;
|
|
|
|
memset(&serv_addr, '0', sizeof(serv_addr));
|
|
|
|
serv_addr.sin6_family = AF_INET6;
|
|
|
|
serv_addr.sin6_port = htons(53);
|
|
|
|
serv_addr.sin6_flowinfo = 0;
|
|
|
|
serv_addr.sin6_addr = in6addr_any;
|
|
|
|
serv_addr.sin6_scope_id = IP6_NO_ZONE;
|
|
|
|
#else
|
|
|
|
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
struct sockaddr_in serv_addr;
|
|
|
|
memset(&serv_addr, '0', sizeof(serv_addr));
|
|
|
|
serv_addr.sin_family = AF_INET;
|
|
|
|
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
serv_addr.sin_port = htons(53);
|
|
|
|
#endif
|
|
|
|
bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
|
|
|
|
|
|
|
|
const struct ifreq ifreq0 = {"en0"};
|
|
|
|
const struct ifreq ifreq1 = {"en1"};
|
|
|
|
setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
|
|
|
|
sdk_wifi_get_opmode() == STATIONAP_MODE ? &ifreq1 : &ifreq0,
|
|
|
|
sizeof(ifreq0));
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char buffer[96];
|
|
|
|
struct sockaddr_storage src_addr;
|
|
|
|
socklen_t src_addr_len = sizeof(src_addr);
|
|
|
|
ssize_t count = recvfrom(fd, buffer, sizeof(buffer), 0, (struct sockaddr *) &src_addr, &src_addr_len);
|
|
|
|
|
|
|
|
/* Drop messages that are too large to send a response in the buffer */
|
|
|
|
if(count > 0 && count <= sizeof(buffer) - 16) {
|
|
|
|
size_t qname_len = strlen(buffer + 12) + 1;
|
|
|
|
uint32_t reply_len = 2 + 10 + qname_len + 16 + 4;
|
|
|
|
|
|
|
|
char *head = buffer + 2;
|
|
|
|
*head++ = 0x80; // Flags
|
|
|
|
*head++ = 0x00;
|
|
|
|
*head++ = 0x00; // Q count
|
|
|
|
*head++ = 0x01;
|
|
|
|
*head++ = 0x00; // A count
|
|
|
|
*head++ = 0x01;
|
|
|
|
*head++ = 0x00; // Auth count
|
|
|
|
*head++ = 0x00;
|
|
|
|
*head++ = 0x00; // Add count
|
|
|
|
*head++ = 0x00;
|
|
|
|
head += qname_len;
|
|
|
|
*head++ = 0x00; // Q type
|
|
|
|
*head++ = 0x01;
|
|
|
|
*head++ = 0x00; // Q class
|
|
|
|
*head++ = 0x01;
|
|
|
|
*head++ = 0xC0; // LBL offs
|
|
|
|
*head++ = 0x0C;
|
|
|
|
*head++ = 0x00; // Type
|
|
|
|
*head++ = 0x01;
|
|
|
|
*head++ = 0x00; // Class
|
|
|
|
*head++ = 0x01;
|
|
|
|
*head++ = 0x00; // TTL
|
|
|
|
*head++ = 0x00;
|
|
|
|
*head++ = 0x00;
|
|
|
|
*head++ = 0x78;
|
|
|
|
*head++ = 0x00; // RD len
|
|
|
|
*head++ = 0x04;
|
|
|
|
*head++ = ip4_addr1(&server_addr);
|
|
|
|
*head++ = ip4_addr2(&server_addr);
|
|
|
|
*head++ = ip4_addr3(&server_addr);
|
|
|
|
*head++ = ip4_addr4(&server_addr);
|
|
|
|
|
|
|
|
sendto(fd, buffer, reply_len, 0, (struct sockaddr *) &src_addr, src_addr_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *wificfg_default_ssid = "fiatlux_%02X%02X%02X";
|
|
|
|
const char *wificfg_default_password = "fiatlux02";
|
|
|
|
const char *wificfg_default_hostname = "fiatlux-%02x%02x%02x";
|
|
|
|
|
|
|
|
void user_init(void) {
|
2021-06-19 14:42:38 +00:00
|
|
|
uart_set_baud(0, 115200);
|
|
|
|
printf("SDK version: %s\n", sdk_system_get_sdk_version());
|
|
|
|
|
|
|
|
sdk_wifi_set_sleep_type(WIFI_SLEEP_MODEM);
|
|
|
|
|
2021-07-09 19:35:41 +00:00
|
|
|
system_init_config();
|
2021-06-21 19:35:22 +00:00
|
|
|
char *wifi_sta_ssid = NULL;
|
|
|
|
char *wifi_sta_password = NULL;
|
|
|
|
char *wifi_ap_ssid = NULL;
|
|
|
|
char *wifi_ap_password = NULL;
|
|
|
|
|
|
|
|
/* Default a hostname. */
|
|
|
|
char *hostname = NULL;
|
|
|
|
sysparam_get_string("hostname", &hostname);
|
|
|
|
if(!hostname && wificfg_default_hostname) {
|
|
|
|
uint8_t macaddr[6];
|
|
|
|
char name[32];
|
|
|
|
sdk_wifi_get_macaddr(1, macaddr);
|
|
|
|
snprintf(name, sizeof(name), wificfg_default_hostname, macaddr[3],
|
|
|
|
macaddr[4], macaddr[5]);
|
|
|
|
sysparam_set_string("hostname", name);
|
|
|
|
}
|
|
|
|
if(hostname) {
|
|
|
|
free(hostname);
|
|
|
|
}
|
|
|
|
|
|
|
|
sysparam_get_string("wifi_ap_ssid", &wifi_ap_ssid);
|
|
|
|
sysparam_get_string("wifi_ap_password", &wifi_ap_password);
|
|
|
|
sysparam_get_string("wifi_sta_ssid", &wifi_sta_ssid);
|
|
|
|
sysparam_get_string("wifi_sta_password", &wifi_sta_password);
|
|
|
|
|
|
|
|
int8_t wifi_sta_enable = 1;
|
|
|
|
int8_t wifi_ap_enable = 1;
|
|
|
|
sysparam_get_int8("wifi_sta_enable", &wifi_sta_enable);
|
|
|
|
sysparam_get_int8("wifi_ap_enable", &wifi_ap_enable);
|
|
|
|
|
|
|
|
if(!wifi_sta_enable)
|
|
|
|
wifi_ap_enable = 1;
|
|
|
|
|
|
|
|
int8_t wifi_sta_disabled_restarts = 0;
|
|
|
|
sysparam_get_int8("wifi_sta_disabled_restarts", &wifi_sta_disabled_restarts);
|
|
|
|
if(wifi_sta_disabled_restarts > 0) {
|
|
|
|
wifi_sta_enable = 0;
|
|
|
|
wifi_sta_disabled_restarts--;
|
|
|
|
sysparam_set_int8("wifi_sta_disabled_restarts", wifi_sta_disabled_restarts);
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t wifi_ap_disabled_restarts = 0;
|
|
|
|
sysparam_get_int8("wifi_ap_disabled_restarts", &wifi_ap_disabled_restarts);
|
|
|
|
if(wifi_ap_disabled_restarts > 0) {
|
|
|
|
wifi_ap_enable = 0;
|
|
|
|
wifi_ap_disabled_restarts--;
|
|
|
|
sysparam_set_int8("wifi_ap_disabled_restarts", wifi_ap_disabled_restarts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate the configuration. */
|
|
|
|
|
|
|
|
if(wifi_sta_enable && (!wifi_sta_ssid || !wifi_sta_password ||
|
|
|
|
strlen(wifi_sta_ssid) < 1 ||
|
|
|
|
strlen(wifi_sta_ssid) > 32 ||
|
|
|
|
!wifi_sta_password ||
|
|
|
|
strlen(wifi_sta_password) < 8 ||
|
|
|
|
strlen(wifi_sta_password) >= 64)) {
|
|
|
|
wifi_sta_enable = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wifi_ap_enable) {
|
|
|
|
/* Default AP ssid and password. */
|
|
|
|
if(!wifi_ap_ssid && wificfg_default_ssid) {
|
|
|
|
uint8_t macaddr[6];
|
|
|
|
char ssid[32];
|
|
|
|
sdk_wifi_get_macaddr(1, macaddr);
|
|
|
|
snprintf(ssid, sizeof(ssid), wificfg_default_ssid, macaddr[3],
|
|
|
|
macaddr[4], macaddr[5]);
|
|
|
|
sysparam_set_string("wifi_ap_ssid", ssid);
|
|
|
|
sysparam_get_string("wifi_ap_ssid", &wifi_ap_ssid);
|
|
|
|
|
|
|
|
if(!wifi_ap_password && wificfg_default_password) {
|
|
|
|
sysparam_set_string("wifi_ap_password", wificfg_default_password);
|
|
|
|
sysparam_get_string("wifi_ap_password", &wifi_ap_password);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("ssid: %s\n", wifi_ap_ssid);
|
|
|
|
|
|
|
|
/* If the ssid and password are not valid then disable the AP interface. */
|
|
|
|
if(!wifi_ap_ssid || strlen(wifi_ap_ssid) < 1 || strlen(wifi_ap_ssid) >= 32 ||
|
|
|
|
!wifi_ap_password || strlen(wifi_ap_password) < 8 || strlen(wifi_ap_password) >= 64) {
|
|
|
|
printf("len err\n");
|
|
|
|
wifi_ap_enable = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t wifi_mode = NULL_MODE;
|
|
|
|
if(wifi_sta_enable && wifi_ap_enable)
|
|
|
|
wifi_mode = STATIONAP_MODE;
|
|
|
|
else if(wifi_sta_enable)
|
|
|
|
wifi_mode = STATION_MODE;
|
|
|
|
else if(wifi_ap_enable)
|
|
|
|
wifi_mode = SOFTAP_MODE;
|
|
|
|
sdk_wifi_set_opmode(wifi_mode);
|
|
|
|
|
|
|
|
if(wifi_sta_enable) {
|
|
|
|
printf("try STA Mode\n");
|
|
|
|
struct sdk_station_config config;
|
|
|
|
strcpy((char *) config.ssid, wifi_sta_ssid);
|
|
|
|
strcpy((char *) config.password, wifi_sta_password);
|
|
|
|
config.bssid_set = 0;
|
|
|
|
|
|
|
|
int8_t wifi_sta_dhcp = 1;
|
|
|
|
sysparam_get_int8("wifi_sta_dhcp", &wifi_sta_dhcp);
|
|
|
|
|
|
|
|
if(!wifi_sta_dhcp) {
|
|
|
|
char *wifi_sta_ip_addr = NULL;
|
|
|
|
char *wifi_sta_netmask = NULL;
|
|
|
|
char *wifi_sta_gateway = NULL;
|
|
|
|
sysparam_get_string("wifi_sta_ip_addr", &wifi_sta_ip_addr);
|
|
|
|
sysparam_get_string("wifi_sta_netmask", &wifi_sta_netmask);
|
|
|
|
sysparam_get_string("wifi_sta_gateway", &wifi_sta_gateway);
|
|
|
|
|
|
|
|
if(wifi_sta_ip_addr && strlen(wifi_sta_ip_addr) > 4 &&
|
|
|
|
wifi_sta_netmask && strlen(wifi_sta_netmask) > 4 &&
|
|
|
|
wifi_sta_gateway && strlen(wifi_sta_gateway) > 4) {
|
|
|
|
sdk_wifi_station_dhcpc_stop();
|
|
|
|
struct ip_info info;
|
|
|
|
memset(&info, 0x0, sizeof(info));
|
|
|
|
info.ip.addr = ipaddr_addr(wifi_sta_ip_addr);
|
|
|
|
info.netmask.addr = ipaddr_addr(wifi_sta_netmask);
|
|
|
|
info.gw.addr = ipaddr_addr(wifi_sta_gateway);
|
|
|
|
sdk_wifi_set_ip_info(STATION_IF, &info);
|
|
|
|
}
|
|
|
|
if(wifi_sta_ip_addr) free(wifi_sta_ip_addr);
|
|
|
|
if(wifi_sta_netmask) free(wifi_sta_netmask);
|
|
|
|
if(wifi_sta_gateway) free(wifi_sta_gateway);
|
|
|
|
}
|
|
|
|
|
|
|
|
sdk_wifi_station_set_config(&config);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wifi_ap_enable) {
|
|
|
|
printf("try AP Mode\n");
|
|
|
|
/* Read and validate paramenters. */
|
|
|
|
int8_t wifi_ap_ssid_hidden = 0;
|
|
|
|
sysparam_get_int8("wifi_ap_ssid_hidden", &wifi_ap_ssid_hidden);
|
|
|
|
if(wifi_ap_ssid_hidden < 0 || wifi_ap_ssid_hidden > 1) {
|
|
|
|
wifi_ap_ssid_hidden = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t wifi_ap_channel = 6;
|
|
|
|
sysparam_get_int8("wifi_ap_channel", &wifi_ap_channel);
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* AU does not allow channels above 13, although 14 works. */
|
|
|
|
if(wifi_ap_channel > 13) {
|
|
|
|
wifi_ap_channel = 13;
|
|
|
|
}
|
|
|
|
/* US does not allow channels above 11, although they work. */
|
|
|
|
if (wifi_ap_channel > 11) {
|
|
|
|
wifi_ap_channel = 11;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if(wifi_ap_channel < 1 || wifi_ap_channel > 14) {
|
|
|
|
wifi_ap_channel = 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t wifi_ap_authmode = AUTH_WPA_WPA2_PSK;
|
|
|
|
sysparam_get_int8("wifi_ap_authmode", &wifi_ap_authmode);
|
|
|
|
if(wifi_ap_authmode != AUTH_OPEN && wifi_ap_authmode != AUTH_WPA_PSK &&
|
|
|
|
wifi_ap_authmode != AUTH_WPA2_PSK && wifi_ap_authmode != AUTH_WPA_WPA2_PSK) {
|
|
|
|
wifi_ap_authmode = AUTH_WPA_WPA2_PSK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t wifi_ap_max_conn = 3;
|
|
|
|
sysparam_get_int8("wifi_ap_max_conn", &wifi_ap_max_conn);
|
|
|
|
if(wifi_ap_max_conn < 1 || wifi_ap_max_conn > 8) {
|
|
|
|
wifi_ap_max_conn = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t wifi_ap_beacon_interval = 100;
|
|
|
|
sysparam_get_int32("wifi_ap_beacon_interval", &wifi_ap_beacon_interval);
|
|
|
|
if(wifi_ap_beacon_interval < 0 || wifi_ap_beacon_interval > 1000) {
|
|
|
|
wifi_ap_beacon_interval = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Default AP IP address and netmask. */
|
|
|
|
char *wifi_ap_ip_addr = NULL;
|
|
|
|
sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr);
|
|
|
|
if(!wifi_ap_ip_addr) {
|
|
|
|
sysparam_set_string("wifi_ap_ip_addr", "172.16.0.1");
|
|
|
|
sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr);
|
|
|
|
}
|
|
|
|
char *wifi_ap_netmask = NULL;
|
|
|
|
sysparam_get_string("wifi_ap_netmask", &wifi_ap_netmask);
|
|
|
|
if(!wifi_ap_netmask) {
|
|
|
|
sysparam_set_string("wifi_ap_netmask", "255.255.0.0");
|
|
|
|
sysparam_get_string("wifi_ap_netmask", &wifi_ap_netmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strlen(wifi_ap_ip_addr) >= 7 && strlen(wifi_ap_netmask) >= 7) {
|
|
|
|
struct ip_info ap_ip;
|
|
|
|
ap_ip.ip.addr = ipaddr_addr(wifi_ap_ip_addr);
|
|
|
|
ap_ip.netmask.addr = ipaddr_addr(wifi_ap_netmask);
|
|
|
|
IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0);
|
|
|
|
sdk_wifi_set_ip_info(1, &ap_ip);
|
|
|
|
|
|
|
|
struct sdk_softap_config ap_config = {
|
|
|
|
.ssid_hidden = wifi_ap_ssid_hidden,
|
|
|
|
.channel = wifi_ap_channel,
|
|
|
|
.authmode = wifi_ap_authmode,
|
|
|
|
.max_connection = wifi_ap_max_conn,
|
|
|
|
.beacon_interval = wifi_ap_beacon_interval,
|
|
|
|
};
|
|
|
|
strcpy((char *) ap_config.ssid, wifi_ap_ssid);
|
|
|
|
ap_config.ssid_len = strlen(wifi_ap_ssid);
|
|
|
|
strcpy((char *) ap_config.password, wifi_ap_password);
|
|
|
|
sdk_wifi_softap_set_config(&ap_config);
|
|
|
|
|
|
|
|
int8_t wifi_ap_dhcp_leases = 4;
|
|
|
|
sysparam_get_int8("wifi_ap_dhcp_leases", &wifi_ap_dhcp_leases);
|
|
|
|
|
|
|
|
if(wifi_ap_dhcp_leases) {
|
|
|
|
ip4_addr_t first_client_ip;
|
|
|
|
first_client_ip.addr = ap_ip.ip.addr + htonl(1);
|
|
|
|
|
|
|
|
int8_t wifi_ap_dns = 1;
|
|
|
|
sysparam_get_int8("wifi_ap_dns", &wifi_ap_dns);
|
|
|
|
if(wifi_ap_dns < 0 || wifi_ap_dns > 1)
|
|
|
|
wifi_ap_dns = 1;
|
|
|
|
|
|
|
|
dhcpserver_start(&first_client_ip, wifi_ap_dhcp_leases);
|
|
|
|
dhcpserver_set_router(&ap_ip.ip);
|
|
|
|
if(wifi_ap_dns) {
|
|
|
|
dhcpserver_set_dns(&ap_ip.ip);
|
|
|
|
xTaskCreate(dns_task, "WiFi Cfg DNS", 384, NULL, 2, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(wifi_ap_ip_addr);
|
|
|
|
free(wifi_ap_netmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wifi_sta_ssid) free(wifi_sta_ssid);
|
|
|
|
if(wifi_sta_password) free(wifi_sta_password);
|
|
|
|
if(wifi_ap_ssid) free(wifi_ap_ssid);
|
|
|
|
if(wifi_ap_password) free(wifi_ap_password);
|
|
|
|
|
|
|
|
if(wifi_mode != NULL_MODE) {
|
|
|
|
|
|
|
|
/* turn off LED */
|
|
|
|
//gpio_enable(LED_PIN, GPIO_OUTPUT);
|
|
|
|
//gpio_write(LED_PIN, true);
|
|
|
|
xTaskCreate(blinkenTask, "blinkenTask", 256, NULL, 2, NULL);
|
|
|
|
|
|
|
|
/* initialize tasks */
|
|
|
|
xTaskCreate(&httpd_task, "HTTP Daemon", 2048, NULL, 2, NULL);
|
|
|
|
|
|
|
|
xTaskCreate(&sntp_task, "SNTP", 512, NULL, 1, NULL);
|
|
|
|
}
|
|
|
|
>>>>>>> 4b8d354 (basic webconf)
|
2021-06-19 14:42:38 +00:00
|
|
|
}
|