2021-07-02 16:45:29 +00:00
|
|
|
//
|
|
|
|
// Created by jedi on 25.06.21.
|
2021-07-18 23:25:27 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "web.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "lux.h"
|
|
|
|
#include "wifi.h"
|
2021-11-18 06:37:20 +00:00
|
|
|
#include "log.h"
|
2021-07-18 23:25:27 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <task.h>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <sysparam.h>
|
|
|
|
#include <lwipopts.h>
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <espressif/esp_common.h>
|
|
|
|
#include <lwip/tcp.h>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <httpd/httpd.h>
|
|
|
|
}
|
|
|
|
|
2021-07-19 18:51:03 +00:00
|
|
|
#define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS)
|
|
|
|
|
|
|
|
struct {
|
|
|
|
bool global;
|
|
|
|
bool connection;
|
|
|
|
bool wifi;
|
|
|
|
} has_changed;
|
|
|
|
|
|
|
|
void websocket_task(void *pvParameter) {
|
|
|
|
auto *pcb = (struct tcp_pcb *) pvParameter;
|
|
|
|
|
|
|
|
size_t connstarttime = xTaskGetTickCount();
|
|
|
|
has_changed = {true, true, true};
|
2021-11-18 06:37:20 +00:00
|
|
|
syslog_attach();
|
|
|
|
unsigned local_log_tail = syslog_current_tail();
|
2021-07-19 18:51:03 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2021-08-28 21:44:01 +00:00
|
|
|
if(pcb == nullptr || pcb->state != ESTABLISHED) {
|
2021-11-18 06:37:20 +00:00
|
|
|
syslog("Connection closed, deleting task\n");
|
2021-07-19 18:51:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-18 06:37:20 +00:00
|
|
|
//Syslog
|
|
|
|
if(syslog_data_after(local_log_tail) != 0) {
|
|
|
|
char response[128];
|
|
|
|
response[0] = 'L';
|
|
|
|
size_t len = syslog_copy_out(&response[4], 124, local_log_tail);
|
|
|
|
response[1] = len;
|
|
|
|
((uint16_t &) response[2]) = local_log_tail & 0xFFFF;
|
|
|
|
if(len < sizeof(response)) {
|
|
|
|
LOCK_TCPIP_CORE();
|
|
|
|
websocket_write(pcb, (unsigned char *) response, len + 4, WS_BIN_MODE);
|
|
|
|
local_log_tail += len;
|
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
} else
|
|
|
|
syslog("buffer too small -1\n");
|
|
|
|
vTaskDelayMs(1000);
|
|
|
|
}
|
|
|
|
|
2021-07-19 18:51:03 +00:00
|
|
|
//Global Info
|
|
|
|
if(has_changed.global) {
|
2021-08-28 21:44:01 +00:00
|
|
|
timeval tv{};
|
|
|
|
gettimeofday(&tv, nullptr);
|
|
|
|
size_t uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000;
|
2021-07-19 18:51:03 +00:00
|
|
|
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;
|
2021-08-28 21:44:01 +00:00
|
|
|
char *hostname = nullptr;
|
2021-07-19 18:51:03 +00:00
|
|
|
|
|
|
|
sysparam_get_string("hostname", &hostname);
|
|
|
|
/* Generate response in JSON format */
|
2021-11-18 06:37:20 +00:00
|
|
|
char response[192];
|
2021-07-19 18:51:03 +00:00
|
|
|
size_t 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);
|
2021-11-18 06:37:20 +00:00
|
|
|
has_changed.global = false;
|
2021-07-19 18:51:03 +00:00
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
} else
|
2021-11-18 06:37:20 +00:00
|
|
|
syslog("buffer too small 0\n");
|
|
|
|
vTaskDelayMs(1000);
|
2021-07-19 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Connection Info
|
|
|
|
if(has_changed.connection) {
|
2021-08-28 21:44:01 +00:00
|
|
|
timeval tv{};
|
|
|
|
gettimeofday(&tv, nullptr);
|
|
|
|
size_t connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000;
|
|
|
|
|
|
|
|
printf("conn %d: " IPSTR " <-> " IPSTR " \n", pcb->netif_idx, IP2STR(&pcb->local_ip),
|
|
|
|
IP2STR(&pcb->remote_ip));
|
2021-11-18 06:37:20 +00:00
|
|
|
char response[192];
|
2021-07-19 18:51:03 +00:00
|
|
|
size_t len = snprintf(response, sizeof(response),
|
|
|
|
"{\"connage\" : \"%d\","
|
2021-08-28 21:44:01 +00:00
|
|
|
"\"clientip\" : \"" IPSTR "\""
|
2021-07-19 18:51:03 +00:00
|
|
|
"}", connuptime, IP2STR(&pcb->remote_ip));
|
|
|
|
if(len < sizeof(response)) {
|
|
|
|
LOCK_TCPIP_CORE();
|
|
|
|
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
|
2021-11-18 06:37:20 +00:00
|
|
|
has_changed.connection = false;
|
2021-07-19 18:51:03 +00:00
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
} else
|
2021-11-18 06:37:20 +00:00
|
|
|
syslog("buffer too small 1\n");
|
|
|
|
vTaskDelayMs(1000);
|
2021-07-19 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(has_changed.wifi) {
|
|
|
|
has_changed.wifi = false;
|
|
|
|
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);
|
2021-08-28 21:44:01 +00:00
|
|
|
ip_info info{};
|
2021-07-19 18:51:03 +00:00
|
|
|
sdk_wifi_get_ip_info(SOFTAP_IF, &info);
|
|
|
|
|
2021-08-28 21:44:01 +00:00
|
|
|
char *apssid = nullptr;
|
2021-07-19 18:51:03 +00:00
|
|
|
sysparam_get_string("wifi_ap_ssid", &apssid);
|
|
|
|
|
|
|
|
/* Generate response in JSON format */
|
|
|
|
char response[128];
|
|
|
|
size_t len = snprintf(response, sizeof(response),
|
|
|
|
"{\"opmode\" : \"%s\","
|
|
|
|
" \"apssid\" : \"%s\","
|
2021-08-28 21:44:01 +00:00
|
|
|
" \"apip\" : \"" IPSTR "\","
|
|
|
|
" \"apmac\" : \"" MACSTR "\""
|
2021-07-19 18:51:03 +00:00
|
|
|
"}", 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
|
2021-11-18 06:37:20 +00:00
|
|
|
syslog("buffer too small 2\n");
|
2021-07-19 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 06:37:20 +00:00
|
|
|
vTaskDelayMs(1000);
|
2021-07-19 18:51:03 +00:00
|
|
|
|
|
|
|
if(opmode == STATION_MODE || opmode == STATIONAP_MODE) {
|
|
|
|
uint8_t hwaddr[6];
|
|
|
|
sdk_wifi_get_macaddr(STATION_IF, hwaddr);
|
2021-08-28 21:44:01 +00:00
|
|
|
ip_info info{};
|
2021-07-19 18:51:03 +00:00
|
|
|
sdk_wifi_get_ip_info(STATION_IF, &info);
|
|
|
|
char *stassid = nullptr;
|
|
|
|
sysparam_get_string("wifi_sta_ssid", &stassid);
|
|
|
|
|
|
|
|
/* Generate response in JSON format */
|
|
|
|
char response[128];
|
|
|
|
size_t len = snprintf(response, sizeof(response),
|
|
|
|
"{\"opmode\" : \"%s\","
|
|
|
|
" \"stassid\" : \"%s\","
|
2021-08-28 21:44:01 +00:00
|
|
|
" \"staip\" : \"" IPSTR "\","
|
|
|
|
" \"stamac\" : \"" MACSTR "\""
|
2021-07-19 18:51:03 +00:00
|
|
|
"}", 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
|
2021-11-18 06:37:20 +00:00
|
|
|
syslog("buffer too small 3\n");
|
2021-07-19 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-12 21:01:00 +00:00
|
|
|
vTaskDelayMs(250);
|
|
|
|
{
|
|
|
|
uint8_t response[3];
|
|
|
|
uint16_t val;
|
|
|
|
val = sdk_system_adc_read();
|
|
|
|
response[2] = (uint8_t) val;
|
|
|
|
response[1] = val >> 8;
|
|
|
|
response[0] = 'V';
|
|
|
|
LOCK_TCPIP_CORE();
|
|
|
|
websocket_write(pcb, response, 3, WS_BIN_MODE);
|
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
}
|
2021-07-19 18:51:03 +00:00
|
|
|
vTaskDelayMs(500);
|
|
|
|
}
|
|
|
|
|
2021-11-18 06:37:20 +00:00
|
|
|
syslog_detach();
|
|
|
|
|
2021-08-28 21:44:01 +00:00
|
|
|
vTaskDelete(nullptr);
|
2021-07-19 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 21:44:01 +00:00
|
|
|
struct fw_frame {
|
|
|
|
char t;
|
|
|
|
uint8_t reserved[3];
|
|
|
|
uint16_t seq;
|
|
|
|
uint16_t len;
|
|
|
|
uint32_t hash;
|
|
|
|
uint8_t data[];
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
struct fw_check {
|
|
|
|
char t;
|
|
|
|
uint8_t reserved[3];
|
|
|
|
uint32_t len;
|
|
|
|
uint32_t hash;
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
2021-07-19 18:51:03 +00:00
|
|
|
/**
|
|
|
|
* This function is called when websocket frame is received.
|
|
|
|
*
|
|
|
|
* Note: this function is executed on TCP thread and should return as soon
|
|
|
|
* as possible.
|
|
|
|
*/
|
2021-08-28 21:44:01 +00:00
|
|
|
void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len,
|
|
|
|
uint8_t /*mode*/) { //mode should be WS_BIN_MODE or WS_TEXT_MODE
|
2021-07-19 18:51:03 +00:00
|
|
|
|
2021-09-12 19:55:17 +00:00
|
|
|
uint8_t response[4];
|
|
|
|
auto &cmd = (char &) response[0];
|
|
|
|
auto &ret = response[1];
|
|
|
|
auto &val = (uint16_t &) response[2];
|
|
|
|
cmd = '0';
|
|
|
|
ret = ERROR;
|
|
|
|
val = 0;
|
2021-07-19 18:51:03 +00:00
|
|
|
|
2021-09-12 19:55:17 +00:00
|
|
|
bool togl = false;
|
2021-08-28 21:44:01 +00:00
|
|
|
|
2021-09-12 21:01:00 +00:00
|
|
|
if(data[0] == 'V') {
|
|
|
|
/* This should be done on a separate thread in 'real' applications */
|
|
|
|
val = sdk_system_adc_read();
|
|
|
|
cmd = 'V';
|
|
|
|
} else if(data[0] == 'R') {
|
|
|
|
// Restart
|
|
|
|
cmd = 'R';
|
|
|
|
ret = OK;
|
|
|
|
} else if(data[0] == 'X') {
|
|
|
|
// Clear Config
|
|
|
|
cmd = 'X';
|
|
|
|
ret = OK;
|
|
|
|
} else if(data[0] == 'D') {
|
|
|
|
// Disable LED
|
|
|
|
syslog("G\n");
|
|
|
|
signal_led(false);
|
|
|
|
cmd = 'G';
|
|
|
|
ret = OK;
|
|
|
|
val = 1;
|
|
|
|
} else if(data[0] == 'E') {
|
|
|
|
// Enable LED
|
|
|
|
syslog("E\n");
|
|
|
|
signal_led(true);
|
|
|
|
cmd = 'G';
|
|
|
|
ret = OK;
|
|
|
|
val = 0;
|
|
|
|
} else if(data[0] == 'F') {
|
|
|
|
togl = !togl;
|
|
|
|
signal_led(togl);
|
|
|
|
auto *f = (fw_frame *) data;
|
|
|
|
if(f->seq == 0) {
|
|
|
|
system_otaflash_init();
|
|
|
|
}
|
|
|
|
uint16_t ack = 0;
|
|
|
|
ret = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash), &ack);
|
|
|
|
val = htons(ack);
|
|
|
|
cmd = 'F';
|
|
|
|
} else if(data[0] == 'C') {
|
|
|
|
signal_led(false);
|
|
|
|
auto *f = (fw_check *) data;
|
|
|
|
ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash));
|
|
|
|
cmd = 'C';
|
|
|
|
} else if(data[0] == 'S') {
|
|
|
|
int8_t en = 0;
|
|
|
|
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);
|
|
|
|
(void) password_len;
|
|
|
|
|
|
|
|
sysparam_set_int8("wifi_sta_enable", en);
|
|
|
|
sysparam_set_string("wifi_sta_ssid", ssid);
|
|
|
|
sysparam_set_string("wifi_sta_password", password);
|
|
|
|
|
|
|
|
//sysparam_get_int8("wifi_sta_dhcp", wifi_sta_dhcp);
|
|
|
|
//sysparam_get_int32("wifi_sta_ip_addr", wifi_sta_ip_addr);
|
|
|
|
//sysparam_get_int32("wifi_sta_netmask", wifi_sta_netmask);
|
|
|
|
//sysparam_get_int32("wifi_sta_gateway", wifi_sta_gateway);
|
|
|
|
cmd = 'S';
|
|
|
|
} else if(data[0] == 'A') {
|
|
|
|
int8_t en = 0;
|
|
|
|
uint8_t dns_enable = 0;
|
|
|
|
uint8_t mdns_enable = 0;
|
|
|
|
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);
|
|
|
|
(void) password_len;
|
|
|
|
|
|
|
|
sysparam_set_int8("wifi_ap_enable", en);
|
|
|
|
sysparam_set_string("wifi_ap_ssid", ssid);
|
|
|
|
sysparam_set_string("wifi_ap_password", password);
|
|
|
|
|
|
|
|
//sysparam_get_int32("wifi_ap_ip_addr", "172.16.0.1");
|
|
|
|
//sysparam_get_int32("wifi_ap_netmask", "255.255.0.0");
|
|
|
|
//sysparam_set_int8("wifi_ap_dns", dns_enable);
|
|
|
|
//sysparam_set_int8("wifi_ap_mdns", mdns_enable);
|
|
|
|
cmd = 'A';
|
|
|
|
} else {
|
|
|
|
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data);
|
|
|
|
printf("Unknown command %c\n", data[0]);
|
|
|
|
ret = ERROR;
|
2021-07-19 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 21:44:01 +00:00
|
|
|
LOCK_TCPIP_CORE();
|
2021-09-12 19:55:17 +00:00
|
|
|
websocket_write(pcb, response, 4, WS_BIN_MODE);
|
2021-08-28 21:44:01 +00:00
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
|
2021-09-12 19:55:17 +00:00
|
|
|
if(ret == OK) {
|
|
|
|
if(cmd == 'R' || cmd == 'C') { // Restart
|
|
|
|
printf("rebooting now");
|
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
|
|
vPortEnterCritical();
|
|
|
|
sdk_system_restart();
|
|
|
|
} else if(cmd == 'X') { // Clear Config
|
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
|
|
system_clear_config();
|
|
|
|
}
|
2021-08-28 21:44:01 +00:00
|
|
|
}
|
2021-07-19 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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, 3, nullptr);
|
|
|
|
}
|
|
|
|
}
|
2021-07-18 23:25:27 +00:00
|
|
|
|
2021-07-19 20:31:30 +00:00
|
|
|
extern "C" void httpd_task(void *pvParameters) {
|
2021-08-28 21:44:01 +00:00
|
|
|
(void) pvParameters;
|
2021-07-18 23:25:27 +00:00
|
|
|
|
2021-07-19 20:31:30 +00:00
|
|
|
while (!uxSemaphoreGetCount(wifi_available_semaphore))
|
2021-07-18 23:25:27 +00:00
|
|
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
2021-07-19 18:51:03 +00:00
|
|
|
websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb);
|
2021-07-18 23:25:27 +00:00
|
|
|
httpd_init();
|
|
|
|
|
2021-07-19 20:31:30 +00:00
|
|
|
vTaskDelete(nullptr);
|
2021-07-18 23:25:27 +00:00
|
|
|
}
|