482 lines
No EOL
16 KiB
C++
482 lines
No EOL
16 KiB
C++
//
|
|
// Created by jedi on 25.06.21.
|
|
//
|
|
|
|
#include "web.h"
|
|
#include "system.h"
|
|
#include "lux.h"
|
|
#include "wifi.h"
|
|
#include "log.h"
|
|
|
|
#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>
|
|
}
|
|
|
|
#include "messages.h"
|
|
|
|
using namespace fiatlux;
|
|
|
|
int version = 23;
|
|
|
|
#define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS)
|
|
|
|
const char hex_lookup[] = "0123456789ABCDEF\0\0";
|
|
|
|
uint16_t voltage_val;
|
|
|
|
struct {
|
|
bool global;
|
|
bool connection;
|
|
bool wifi;
|
|
bool voltage;
|
|
bool lights;
|
|
} has_changed;
|
|
|
|
void websocket_task(void *pvParameter) {
|
|
auto *pcb = (struct tcp_pcb *) pvParameter;
|
|
|
|
size_t connstarttime = xTaskGetTickCount();
|
|
has_changed = {true, true, true, true, true};
|
|
syslog_attach();
|
|
unsigned local_log_tail = syslog_current_tail();
|
|
|
|
for (;;) {
|
|
if(pcb == nullptr || pcb->state != ESTABLISHED) {
|
|
syslog("Connection closed, deleting task\n");
|
|
break;
|
|
}
|
|
|
|
//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");
|
|
|
|
taskYIELD();
|
|
}
|
|
|
|
//Global Info
|
|
if(false && has_changed.global) {
|
|
timeval tv{};
|
|
gettimeofday(&tv, nullptr);
|
|
size_t 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 = nullptr;
|
|
|
|
sysparam_get_string("hostname", &hostname);
|
|
/* Generate response in JSON format */
|
|
messages::frame<messages::system_info> frame;
|
|
frame.cmd = messages::SYSTEM_INFO;
|
|
frame.msg.walltime = tv.tv_sec;
|
|
frame.msg.uptime = uptime;
|
|
frame.msg.heap = heap;
|
|
frame.msg.chipid = chip_id;
|
|
frame.msg.flashid = flash_id;
|
|
frame.msg.flashsize = flash_size;
|
|
frame.msg.version = version;
|
|
frame.msg.peripheral_version = peripheral_version;
|
|
//frame.msg. = ; // hostname
|
|
|
|
LOCK_TCPIP_CORE();
|
|
websocket_write(pcb, (unsigned char *) &frame, sizeof(frame), WS_BIN_MODE);
|
|
has_changed.global = false;
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
free(hostname);
|
|
/*LOCK_TCPIP_CORE();
|
|
websocket_write(pcb, (uint8_t * ) &frame, sizeof(frame), WS_BIN_MODE);
|
|
has_changed.global = false;
|
|
UNLOCK_TCPIP_CORE();*/
|
|
|
|
taskYIELD();
|
|
} else if(has_changed.lights) {
|
|
messages::frame<messages::lights> frame;
|
|
frame.cmd = messages::LIGHTS;
|
|
frame.msg.top_color = *(uint32_t *) &top_color;
|
|
frame.msg.bottom_color = *(uint32_t *) &bottom_color;
|
|
frame.msg.white_brightness = light_value;
|
|
LOCK_TCPIP_CORE();
|
|
websocket_write(pcb, (unsigned char *) &frame, sizeof(frame), WS_BIN_MODE);
|
|
has_changed.lights = false;
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
taskYIELD();
|
|
} else if(has_changed.connection) {
|
|
timeval tv{};
|
|
gettimeofday(&tv, nullptr);
|
|
size_t connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000;
|
|
|
|
printf("conn %d: %d.%d.%d.%d <-> %d.%d.%d.%d \n", pcb->netif_idx, IP2STR(&pcb->local_ip),
|
|
IP2STR(&pcb->remote_ip));
|
|
char response[192];
|
|
size_t
|
|
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);
|
|
has_changed.connection = false;
|
|
UNLOCK_TCPIP_CORE();
|
|
} else
|
|
syslog("buffer too small 1\n");
|
|
|
|
taskYIELD();
|
|
} else 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);
|
|
ip_info info{};
|
|
sdk_wifi_get_ip_info(SOFTAP_IF, &info);
|
|
|
|
char *apssid = nullptr;
|
|
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\","
|
|
" \"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
|
|
syslog("buffer too small 2\n");
|
|
}
|
|
|
|
taskYIELD();
|
|
|
|
if(opmode == STATION_MODE || opmode == STATIONAP_MODE) {
|
|
uint8_t hwaddr[6];
|
|
sdk_wifi_get_macaddr(STATION_IF, hwaddr);
|
|
ip_info info{};
|
|
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\","
|
|
" \"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
|
|
syslog("buffer too small 3\n");
|
|
}
|
|
|
|
taskYIELD();
|
|
} else if(has_changed.voltage) {
|
|
has_changed.voltage = false;
|
|
messages::response res;
|
|
res.val = voltage_val;
|
|
res.ret = OK;
|
|
res.cmd = (messages::id) 'V';
|
|
LOCK_TCPIP_CORE();
|
|
websocket_write(pcb, (uint8_t *) &res, sizeof(res), WS_BIN_MODE);
|
|
UNLOCK_TCPIP_CORE();
|
|
taskYIELD();
|
|
} else {
|
|
//taskYIELD();
|
|
//printf("no change a\n");
|
|
vTaskDelay(50);
|
|
//printf("no change b\n");
|
|
}
|
|
//printf("9: %d\n",gpio_read(9));
|
|
//printf("10: %d\n",gpio_read(10));
|
|
|
|
|
|
taskYIELD();
|
|
}
|
|
|
|
syslog_detach();
|
|
|
|
vTaskDelete(nullptr);
|
|
}
|
|
|
|
char str[] = "L00000000\n";
|
|
|
|
/**
|
|
* 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*/) { //mode should be WS_BIN_MODE or WS_TEXT_MODE
|
|
|
|
messages::response res;
|
|
res.cmd = messages::NONE;
|
|
res.ret = ERROR;
|
|
res.val = 0;
|
|
|
|
bool togl = false;
|
|
|
|
if(data[0] == 'R') {
|
|
// Restart
|
|
res.cmd = (messages::id) 'R';
|
|
res.ret = OK;
|
|
} else if(data[0] == 'X') {
|
|
// Clear Config
|
|
res.cmd = (messages::id) 'X';
|
|
res.ret = OK;
|
|
} else if(data[0] == 'B') {
|
|
// Disable LED
|
|
uint32_t val = data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24);
|
|
bottom_color.r = data[7];
|
|
bottom_color.g = data[6];
|
|
bottom_color.b = data[5];
|
|
has_changed.lights = true;
|
|
syslog("B");
|
|
syslog_i32(val);
|
|
syslog("\n");
|
|
//signal_led(false);
|
|
res.cmd = (messages::id) 'B';
|
|
res.ret = OK;
|
|
res.val = 1;
|
|
} else if(data[0] == 'T') {
|
|
// Disable LED
|
|
uint32_t val = data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24);
|
|
top_color.r = data[7];
|
|
top_color.g = data[6];
|
|
top_color.b = data[5];
|
|
has_changed.lights = true;
|
|
syslog("T");
|
|
syslog_i32(val);
|
|
syslog("\n");
|
|
//signal_led(false);
|
|
res.cmd = (messages::id) 'T';
|
|
res.ret = OK;
|
|
res.val = 1;
|
|
} else if(data[0] == 'J') {
|
|
// Disable LED
|
|
uint32_t val = data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24);
|
|
light_value = data[7] | (data[6] << 8);
|
|
has_changed.lights = true;
|
|
syslog("J");
|
|
syslog_i32(val);
|
|
syslog("\n");
|
|
//signal_led(false);
|
|
res.cmd = (messages::id) 'J';
|
|
res.ret = OK;
|
|
res.val = 1;
|
|
} else if(data[0] == 'D') {
|
|
// Disable LED
|
|
syslog("G\n");
|
|
signal_led(false);
|
|
res.cmd = (messages::id) 'G';
|
|
res.ret = OK;
|
|
res.val = 1;
|
|
} else if(data[0] == 'E') {
|
|
// Enable LED
|
|
syslog("E\n");
|
|
signal_led(true);
|
|
res.cmd = (messages::id) 'G';
|
|
res.ret = OK;
|
|
res.val = 0;
|
|
} else if(data[0] == 'F') {
|
|
togl = !togl;
|
|
signal_led(togl);
|
|
auto *f = (messages::fw_frame *) data;
|
|
if(f->seq == 0) {
|
|
system_otaflash_init();
|
|
}
|
|
uint16_t ack = 0;
|
|
res.ret = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash), &ack);
|
|
res.val = htons(ack);
|
|
res.cmd = (messages::id) 'F';
|
|
} else if(data[0] == 'C') {
|
|
signal_led(false);
|
|
auto *f = (messages::fw_check *) data;
|
|
res.ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash));
|
|
res.cmd = (messages::id) '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);
|
|
res.cmd = (messages::id) '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);
|
|
res.cmd = (messages::id) 'A';
|
|
} else {
|
|
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data);
|
|
printf("Unknown command %c\n", data[0]);
|
|
res.ret = ERROR;
|
|
}
|
|
|
|
LOCK_TCPIP_CORE();
|
|
websocket_write(pcb, (uint8_t *) &res, sizeof(res), WS_BIN_MODE);
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
if(res.ret == OK) {
|
|
if(res.cmd == 'R' || res.cmd == 'C') { // Restart
|
|
printf("rebooting now");
|
|
taskYIELD();
|
|
vPortEnterCritical();
|
|
sdk_system_restart();
|
|
} else if(res.cmd == 'X') { // Clear Config
|
|
taskYIELD();
|
|
system_clear_config();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|
|
|
|
/*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";
|
|
}*/
|
|
|
|
extern "C" void httpd_task(void *pvParameters) {
|
|
(void) pvParameters;
|
|
|
|
//while (!uxSemaphoreGetCount(wifi_available_semaphore))
|
|
// vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
/*tCGI pCGIs[] = {
|
|
{"/gpio", (tCGIHandler) gpio_cgi_handler},
|
|
};
|
|
|
|
// register handlers and start the server
|
|
http_set_cgi_handlers(pCGIs, sizeof(pCGIs) / sizeof(pCGIs[0]));*/
|
|
//while (!uxSemaphoreGetCount(wifi_available_semaphore))
|
|
// vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
/*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();
|
|
|
|
uint16_t last_voltage = 0;
|
|
while (1) {
|
|
uint16_t voltage = sdk_system_adc_read();
|
|
if(false && voltage != last_voltage) {
|
|
last_voltage = voltage;
|
|
voltage_val = voltage;
|
|
has_changed.voltage = true;
|
|
}
|
|
//has_changed.global = true;
|
|
vTaskDelayMs(5000);
|
|
}
|
|
} |