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>
|
|
|
|
}
|
|
|
|
|
2022-09-13 03:29:26 +00:00
|
|
|
#include "messages.h"
|
|
|
|
|
|
|
|
using namespace fiatlux;
|
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
int version = 23;
|
|
|
|
|
2021-07-19 18:51:03 +00:00
|
|
|
#define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS)
|
|
|
|
|
2022-09-22 19:48:36 +00:00
|
|
|
const char hex_lookup[] = "0123456789ABCDEF\0\0";
|
|
|
|
|
2022-09-13 03:29:26 +00:00
|
|
|
uint16_t voltage_val;
|
|
|
|
|
2021-07-19 18:51:03 +00:00
|
|
|
struct {
|
|
|
|
bool global;
|
|
|
|
bool connection;
|
|
|
|
bool wifi;
|
2022-09-13 03:29:26 +00:00
|
|
|
bool voltage;
|
2023-01-19 17:11:13 +00:00
|
|
|
bool lights;
|
2021-07-19 18:51:03 +00:00
|
|
|
} has_changed;
|
|
|
|
|
|
|
|
void websocket_task(void *pvParameter) {
|
|
|
|
auto *pcb = (struct tcp_pcb *) pvParameter;
|
|
|
|
|
|
|
|
size_t connstarttime = xTaskGetTickCount();
|
2023-01-19 17:11:13 +00:00
|
|
|
has_changed = {true, true, 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");
|
2022-06-27 18:00:48 +00:00
|
|
|
|
|
|
|
taskYIELD();
|
2021-11-18 06:37:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 18:51:03 +00:00
|
|
|
//Global Info
|
2023-01-19 06:07:24 +00:00
|
|
|
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 */
|
2022-09-13 03:29:26 +00:00
|
|
|
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;
|
2023-01-19 17:11:13 +00:00
|
|
|
frame.msg.version = version;
|
|
|
|
frame.msg.peripheral_version = peripheral_version;
|
2022-09-13 03:29:26 +00:00
|
|
|
//frame.msg. = ; // hostname
|
|
|
|
|
2023-01-19 06:07:24 +00:00
|
|
|
LOCK_TCPIP_CORE();
|
|
|
|
websocket_write(pcb, (unsigned char *) &frame, sizeof(frame), WS_BIN_MODE);
|
|
|
|
has_changed.global = false;
|
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
|
2021-07-19 18:51:03 +00:00
|
|
|
free(hostname);
|
2022-09-13 03:29:26 +00:00
|
|
|
/*LOCK_TCPIP_CORE();
|
|
|
|
websocket_write(pcb, (uint8_t * ) &frame, sizeof(frame), WS_BIN_MODE);
|
|
|
|
has_changed.global = false;
|
|
|
|
UNLOCK_TCPIP_CORE();*/
|
2022-06-27 18:00:48 +00:00
|
|
|
|
2023-01-19 17:11:13 +00:00
|
|
|
taskYIELD();
|
2022-06-27 18:00:48 +00:00
|
|
|
} else if(has_changed.lights) {
|
2023-01-19 17:11:13 +00:00
|
|
|
messages::frame<messages::lights> frame;
|
|
|
|
frame.cmd = messages::LIGHTS;
|
2022-06-27 18:00:48 +00:00
|
|
|
frame.msg.top_color = *(uint32_t *) &top_color;
|
|
|
|
frame.msg.bottom_color = *(uint32_t *) &bottom_color;
|
2023-01-19 17:11:13 +00:00
|
|
|
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();
|
|
|
|
|
2022-06-27 18:00:48 +00:00
|
|
|
taskYIELD();
|
2022-09-13 03:29:26 +00:00
|
|
|
} else 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;
|
|
|
|
|
2023-01-19 06:07:24 +00:00
|
|
|
printf("conn %d: %d.%d.%d.%d <-> %d.%d.%d.%d \n", pcb->netif_idx, IP2STR(&pcb->local_ip),
|
2021-08-28 21:44:01 +00:00
|
|
|
IP2STR(&pcb->remote_ip));
|
2021-11-18 06:37:20 +00:00
|
|
|
char response[192];
|
2022-09-13 03:29:26 +00:00
|
|
|
size_t
|
|
|
|
len = snprintf(response, sizeof(response), "{\"connage\" : \"%d\"," "\"clientip\" : \""
|
2023-01-19 06:07:24 +00:00
|
|
|
IPSTR
|
|
|
|
"\"" "}", connuptime, IP2STR(&pcb->remote_ip));
|
2021-07-19 18:51:03 +00:00
|
|
|
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");
|
2022-06-27 18:00:48 +00:00
|
|
|
|
|
|
|
taskYIELD();
|
2022-09-13 03:29:26 +00:00
|
|
|
} else if(has_changed.wifi) {
|
2021-07-19 18:51:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-27 18:00:48 +00:00
|
|
|
taskYIELD();
|
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
|
|
|
}
|
|
|
|
|
2022-09-13 03:29:26 +00:00
|
|
|
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';
|
2021-09-12 21:01:00 +00:00
|
|
|
LOCK_TCPIP_CORE();
|
2023-01-19 06:07:24 +00:00
|
|
|
websocket_write(pcb, (uint8_t *) &res, sizeof(res), WS_BIN_MODE);
|
2021-09-12 21:01:00 +00:00
|
|
|
UNLOCK_TCPIP_CORE();
|
2022-09-13 03:29:26 +00:00
|
|
|
taskYIELD();
|
|
|
|
} else {
|
|
|
|
//taskYIELD();
|
|
|
|
//printf("no change a\n");
|
|
|
|
vTaskDelay(50);
|
|
|
|
//printf("no change b\n");
|
2021-09-12 21:01:00 +00:00
|
|
|
}
|
2022-09-13 03:29:26 +00:00
|
|
|
//printf("9: %d\n",gpio_read(9));
|
|
|
|
//printf("10: %d\n",gpio_read(10));
|
2021-09-12 21:01:00 +00:00
|
|
|
|
2022-06-27 18:00:48 +00:00
|
|
|
|
|
|
|
taskYIELD();
|
2021-09-12 21:01:00 +00:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
vTaskDelayMs(500);
|
2021-07-19 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-22 19:48:36 +00:00
|
|
|
char str[] = "L00000000\n";
|
|
|
|
|
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
|
|
|
|
2022-09-13 03:29:26 +00:00
|
|
|
messages::response res;
|
|
|
|
res.cmd = messages::NONE;
|
|
|
|
res.ret = ERROR;
|
|
|
|
res.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
|
|
|
|
2022-09-13 03:29:26 +00:00
|
|
|
if(data[0] == 'R') {
|
2021-09-12 21:01:00 +00:00
|
|
|
// Restart
|
2022-09-13 03:29:26 +00:00
|
|
|
res.cmd = (messages::id) 'R';
|
|
|
|
res.ret = OK;
|
2021-09-12 21:01:00 +00:00
|
|
|
} else if(data[0] == 'X') {
|
|
|
|
// Clear Config
|
2022-09-13 03:29:26 +00:00
|
|
|
res.cmd = (messages::id) 'X';
|
|
|
|
res.ret = OK;
|
2022-09-22 19:48:36 +00:00
|
|
|
} else if(data[0] == 'B') {
|
|
|
|
// Disable LED
|
2023-01-19 06:07:24 +00:00
|
|
|
uint32_t val = data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24);
|
2022-09-22 19:48:36 +00:00
|
|
|
bottom_color.r = data[7];
|
|
|
|
bottom_color.g = data[6];
|
|
|
|
bottom_color.b = data[5];
|
2023-01-19 17:11:13 +00:00
|
|
|
has_changed.lights = true;
|
2022-09-24 05:48:47 +00:00
|
|
|
syslog("B");
|
|
|
|
syslog_i32(val);
|
|
|
|
syslog("\n");
|
2022-09-22 19:48:36 +00:00
|
|
|
//signal_led(false);
|
|
|
|
res.cmd = (messages::id) 'B';
|
|
|
|
res.ret = OK;
|
|
|
|
res.val = 1;
|
|
|
|
} else if(data[0] == 'T') {
|
|
|
|
// Disable LED
|
2023-01-19 06:07:24 +00:00
|
|
|
uint32_t val = data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24);
|
2022-09-22 19:48:36 +00:00
|
|
|
top_color.r = data[7];
|
|
|
|
top_color.g = data[6];
|
|
|
|
top_color.b = data[5];
|
2023-01-19 17:11:13 +00:00
|
|
|
has_changed.lights = true;
|
2023-01-19 06:07:24 +00:00
|
|
|
syslog("T");
|
2022-09-24 05:48:47 +00:00
|
|
|
syslog_i32(val);
|
|
|
|
syslog("\n");
|
2022-09-22 19:48:36 +00:00
|
|
|
//signal_led(false);
|
|
|
|
res.cmd = (messages::id) 'T';
|
|
|
|
res.ret = OK;
|
|
|
|
res.val = 1;
|
2023-01-19 06:07:24 +00:00
|
|
|
} 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);
|
2023-01-19 17:11:13 +00:00
|
|
|
has_changed.lights = true;
|
2023-01-19 06:07:24 +00:00
|
|
|
syslog("J");
|
|
|
|
syslog_i32(val);
|
|
|
|
syslog("\n");
|
|
|
|
//signal_led(false);
|
|
|
|
res.cmd = (messages::id) 'J';
|
|
|
|
res.ret = OK;
|
|
|
|
res.val = 1;
|
2021-09-12 21:01:00 +00:00
|
|
|
} else if(data[0] == 'D') {
|
|
|
|
// Disable LED
|
|
|
|
syslog("G\n");
|
|
|
|
signal_led(false);
|
2022-09-13 03:29:26 +00:00
|
|
|
res.cmd = (messages::id) 'G';
|
|
|
|
res.ret = OK;
|
|
|
|
res.val = 1;
|
2021-09-12 21:01:00 +00:00
|
|
|
} else if(data[0] == 'E') {
|
|
|
|
// Enable LED
|
|
|
|
syslog("E\n");
|
|
|
|
signal_led(true);
|
2022-09-13 03:29:26 +00:00
|
|
|
res.cmd = (messages::id) 'G';
|
|
|
|
res.ret = OK;
|
|
|
|
res.val = 0;
|
2021-09-12 21:01:00 +00:00
|
|
|
} else if(data[0] == 'F') {
|
|
|
|
togl = !togl;
|
|
|
|
signal_led(togl);
|
2022-09-13 03:29:26 +00:00
|
|
|
auto *f = (messages::fw_frame *) data;
|
2021-09-12 21:01:00 +00:00
|
|
|
if(f->seq == 0) {
|
|
|
|
system_otaflash_init();
|
|
|
|
}
|
|
|
|
uint16_t ack = 0;
|
2022-09-13 03:29:26 +00:00
|
|
|
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';
|
2021-09-12 21:01:00 +00:00
|
|
|
} else if(data[0] == 'C') {
|
|
|
|
signal_led(false);
|
2022-09-13 03:29:26 +00:00
|
|
|
auto *f = (messages::fw_check *) data;
|
|
|
|
res.ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash));
|
|
|
|
res.cmd = (messages::id) 'C';
|
2021-09-12 21:01:00 +00:00
|
|
|
} 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);
|
2022-09-13 03:29:26 +00:00
|
|
|
res.cmd = (messages::id) 'S';
|
2021-09-12 21:01:00 +00:00
|
|
|
} 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);
|
2022-09-13 03:29:26 +00:00
|
|
|
res.cmd = (messages::id) 'A';
|
2021-09-12 21:01:00 +00:00
|
|
|
} else {
|
|
|
|
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data);
|
|
|
|
printf("Unknown command %c\n", data[0]);
|
2022-09-13 03:29:26 +00:00
|
|
|
res.ret = ERROR;
|
2021-07-19 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 21:44:01 +00:00
|
|
|
LOCK_TCPIP_CORE();
|
2023-01-19 06:07:24 +00:00
|
|
|
websocket_write(pcb, (uint8_t *) &res, sizeof(res), WS_BIN_MODE);
|
2021-08-28 21:44:01 +00:00
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
|
2022-09-13 03:29:26 +00:00
|
|
|
if(res.ret == OK) {
|
|
|
|
if(res.cmd == 'R' || res.cmd == 'C') { // Restart
|
2021-09-12 19:55:17 +00:00
|
|
|
printf("rebooting now");
|
2022-06-27 18:00:48 +00:00
|
|
|
taskYIELD();
|
2021-09-12 19:55:17 +00:00
|
|
|
vPortEnterCritical();
|
|
|
|
sdk_system_restart();
|
2022-09-13 03:29:26 +00:00
|
|
|
} else if(res.cmd == 'X') { // Clear Config
|
2022-06-27 18:00:48 +00:00
|
|
|
taskYIELD();
|
2021-09-12 19:55:17 +00:00
|
|
|
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
|
|
|
|
2022-09-13 03:29:26 +00:00
|
|
|
/*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";
|
|
|
|
}*/
|
|
|
|
|
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
|
|
|
|
2022-06-27 18:00:48 +00:00
|
|
|
//while (!uxSemaphoreGetCount(wifi_available_semaphore))
|
2022-09-13 03:29:26 +00:00
|
|
|
// 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]));*/
|
2022-06-27 18:00:48 +00:00
|
|
|
//while (!uxSemaphoreGetCount(wifi_available_semaphore))
|
|
|
|
// taskYIELD();
|
|
|
|
printf("httpd_task: wifi is available\n");
|
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();
|
|
|
|
|
2023-01-19 06:07:24 +00:00
|
|
|
uint16_t last_voltage = 0;
|
2022-09-13 03:29:26 +00:00
|
|
|
while (1) {
|
2023-01-19 06:07:24 +00:00
|
|
|
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);
|
2022-09-13 03:29:26 +00:00
|
|
|
}
|
2021-07-18 23:25:27 +00:00
|
|
|
}
|