refactoring code

This commit is contained in:
j3d1 2021-06-25 15:41:33 +02:00 committed by Eleon
parent f89f3e1816
commit d75262dbea
8 changed files with 372 additions and 320 deletions

View file

@ -25,4 +25,4 @@ unittest:
systest: systest:
true true
.NOTPARALLEL: html all .NOTPARALLEL: html all

View file

@ -15,6 +15,10 @@
#include <sntp.h> #include <sntp.h>
#include <time.h> #include <time.h>
#include "wifi.h"
#include "web.h"
#include "mqtt.h"
const int cs0 = 15; const int cs0 = 15;
const int gpio4 = 4; const int gpio4 = 4;
const int gpio5 = 5; const int gpio5 = 5;
@ -108,325 +112,7 @@ void blinkenTask(void *pvParameters) {
const gpio_inttype_t int_type = GPIO_INTTYPE_EDGE_NEG; 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) { void sntp_task(void *pvParameters) {

View file

@ -5,6 +5,7 @@
#ifndef FIRMWARE_MQTT_H #ifndef FIRMWARE_MQTT_H
#define FIRMWARE_MQTT_H #define FIRMWARE_MQTT_H
<<<<<<< HEAD
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -13,4 +14,6 @@ extern "C" {
} }
#endif #endif
=======
>>>>>>> 7d1fb53 (refactoring code)
#endif //FIRMWARE_MQTT_H #endif //FIRMWARE_MQTT_H

View file

@ -20,6 +20,7 @@ void system_clear_config(){
spiflash_erase_sector(start + i * sdk_flashchip.sector_size); spiflash_erase_sector(start + i * sdk_flashchip.sector_size);
} }
sdk_system_restart(); sdk_system_restart();
<<<<<<< HEAD
} }
void system_init_config(){ void system_init_config(){
@ -34,4 +35,6 @@ void system_init_config(){
} }
sdk_system_restart(); sdk_system_restart();
} }
=======
>>>>>>> 7d1fb53 (refactoring code)
} }

View file

@ -10,10 +10,18 @@ extern "C" {
#endif #endif
void system_clear_config(); void system_clear_config();
<<<<<<< HEAD
void system_init_config(); void system_init_config();
=======
>>>>>>> 7d1fb53 (refactoring code)
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
<<<<<<< HEAD
=======
>>>>>>> 7d1fb53 (refactoring code)
#endif //FIRMWARE_SYSTEM_H #endif //FIRMWARE_SYSTEM_H

View file

@ -1,3 +1,347 @@
// //
// Created by jedi on 25.06.21. // Created by jedi on 25.06.21.
// <<<<<<< HEAD
//
=======
//
#include "web.h"
#include "system.h"
#include <string.h>
#include <FreeRTOS.h>
#include <task.h>
extern "C" {
#include <sysparam.h>
}
#include <espressif/esp_common.h>
#include <lwip/tcp.h>
extern "C" {
#include <httpd/httpd.h>
}
#define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS)
struct {
bool global;
bool connection;
bool wifi;
} has_changed;
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
if(has_changed.global){
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];
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);
UNLOCK_TCPIP_CORE();
} else
printf("buffer too small 1");
vTaskDelayMs(2000);
}
//Connection Info
if(has_changed.connection){
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];
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);
UNLOCK_TCPIP_CORE();
} else
printf("buffer too small 1");
vTaskDelayMs(2000);
}
if(has_changed.wifi){
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];
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
printf("buffer too small 2");
}
vTaskDelayMs(2000);
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];
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
printf("buffer too small 3");
}
}
vTaskDelayMs(2000);
//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);
(void) password_len;
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);
(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_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);
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, 2, NULL);
}
}
/*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) {
/*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 (;;);
}
>>>>>>> 7d1fb53 (refactoring code)

View file

@ -9,6 +9,11 @@
extern "C" { extern "C" {
#endif #endif
<<<<<<< HEAD
=======
void httpd_task(void *pvParameters);
>>>>>>> 7d1fb53 (refactoring code)
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -5,6 +5,7 @@
#ifndef FIRMWARE_WIFI_H #ifndef FIRMWARE_WIFI_H
#define FIRMWARE_WIFI_H #define FIRMWARE_WIFI_H
<<<<<<< HEAD
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -13,4 +14,6 @@ extern "C" {
} }
#endif #endif
=======
>>>>>>> 7d1fb53 (refactoring code)
#endif //FIRMWARE_WIFI_H #endif //FIRMWARE_WIFI_H