This commit is contained in:
pvvx 2017-05-09 18:55:08 +03:00
parent d9a4d8b83c
commit ca69e76c51
6 changed files with 70 additions and 63 deletions

View file

@ -7,7 +7,7 @@
***********************************/ ***********************************/
#define __TCP_SERV_CONN_H__ #define __TCP_SERV_CONN_H__
//#include "user_interface.h" #include "user_config.h"
//#include "os_type.h" //#include "os_type.h"
#include "lwip/err.h" #include "lwip/err.h"

View file

@ -76,12 +76,13 @@ typedef struct
typedef void (* web_func_cb)(TCP_SERV_CONN *ts_conn); typedef void (* web_func_cb)(TCP_SERV_CONN *ts_conn);
typedef uint32 (* web_func_disc_cb)(uint32 flg); // отложенная функция, когда соединение закрыто typedef uint32 (* web_ex_func_cb)(uint32 flg); // внешняя или отложенная функция
typedef struct typedef struct
{ {
web_func_disc_cb fnk; web_ex_func_cb fnc;
void * param; void * param;
uint16 pause_ms;
} WEB_SRV_QFNK; } WEB_SRV_QFNK;
typedef struct typedef struct
@ -95,8 +96,6 @@ typedef struct
uint16 msgbufsize; // размер буфера uint16 msgbufsize; // размер буфера
web_func_cb func_web_cb; // calback функция у httpd для обработки приема/передачи кусками web_func_cb func_web_cb; // calback функция у httpd для обработки приема/передачи кусками
uint32 content_len; // размер файла для передачи (GET/POST) или приема, если принимается внешний файл (POST + SCB_RXDATA) uint32 content_len; // размер файла для передачи (GET/POST) или приема, если принимается внешний файл (POST + SCB_RXDATA)
web_func_disc_cb web_disc_cb; // функция вызываемая по закрытию соединения
uint32 web_disc_par; // параметры функции вызываемой по закрытию соединения
#ifdef WEBSOCKET_ENA #ifdef WEBSOCKET_ENA
WS_FRSTAT ws; // параметры websoc WS_FRSTAT ws; // параметры websoc
#endif #endif
@ -206,4 +205,6 @@ err_t webserver_init(uint16 portn);
err_t webserver_close(uint16 portn); err_t webserver_close(uint16 portn);
err_t webserver_reinit(uint16 portn); err_t webserver_reinit(uint16 portn);
BaseType_t webserver_qfn(web_ex_func_cb fnc, void * param, uint16 pause_ms); // вызов функции из task с low priority
#endif /* _INCLUDE_WEB_SRV_H_ */ #endif /* _INCLUDE_WEB_SRV_H_ */

View file

@ -115,7 +115,7 @@ void ICACHE_FLASH_ATTR web_test_adc(TCP_SERV_CONN *ts_conn)
read_adcs((uint16 *)(web_conn->msgbuf + web_conn->msgbuflen), len, 0x0808); read_adcs((uint16 *)(web_conn->msgbuf + web_conn->msgbuflen), len, 0x0808);
web_conn->msgbuflen += len << 1; web_conn->msgbuflen += len << 1;
} }
SetSCB(SCB_FCLOSE | SCB_DISCONNECT); // connection close if(!CheckSCB(SCB_WEBSOC)) SetSCB(SCB_FCLOSE | SCB_DISCONNECT); // connection close
} }
#endif // TEST_SEND_WAVE #endif // TEST_SEND_WAVE
@ -245,14 +245,14 @@ void ICACHE_FLASH_ATTR web_hexdump(TCP_SERV_CONN *ts_conn)
tcp_puts("\r\n"); tcp_puts("\r\n");
if((uint32)addr >= web_conn->udata_stop) { if((uint32)addr >= web_conn->udata_stop) {
ClrSCB(SCB_RETRYCB); ClrSCB(SCB_RETRYCB);
SetSCB(SCB_FCLOSE | SCB_DISCONNECT); // connection close if(!CheckSCB(SCB_WEBSOC)) SetSCB(SCB_FCLOSE | SCB_DISCONNECT); // connection close
return; return;
} }
} }
else { else {
tcp_puts("%p = Bad address!\r\n", addr); tcp_puts("%p = Bad address!\r\n", addr);
ClrSCB(SCB_RETRYCB); ClrSCB(SCB_RETRYCB);
SetSCB(SCB_FCLOSE | SCB_DISCONNECT); // connection close if(!CheckSCB(SCB_WEBSOC)) SetSCB(SCB_FCLOSE | SCB_DISCONNECT); // connection close
return; return;
}; };
} }
@ -366,6 +366,7 @@ void ICACHE_FLASH_ATTR web_int_callback(TCP_SERV_CONN *ts_conn, uint8 *cstr)
cstr+=4; cstr+=4;
ifcmp("scan") web_wscan_xml(ts_conn); ifcmp("scan") web_wscan_xml(ts_conn);
#if WEB_DEBUG_FUNCTIONS #if WEB_DEBUG_FUNCTIONS
else {
#if USE_WEB_AUTH_LEVEL #if USE_WEB_AUTH_LEVEL
if(web_conn->auth_level < WEB_AUTH_LEVEL_USER) return; if(web_conn->auth_level < WEB_AUTH_LEVEL_USER) return;
#endif #endif
@ -375,6 +376,7 @@ void ICACHE_FLASH_ATTR web_int_callback(TCP_SERV_CONN *ts_conn, uint8 *cstr)
else tcp_put('?'); else tcp_put('?');
web_conn->udata_start += 4; web_conn->udata_start += 4;
} }
}
#endif #endif
} }
else ifcmp("sys_") { else ifcmp("sys_") {
@ -403,11 +405,18 @@ void ICACHE_FLASH_ATTR web_int_callback(TCP_SERV_CONN *ts_conn, uint8 *cstr)
else ifcmp("clkcpu") tcp_puts("%u", HalGetCpuClk()); else ifcmp("clkcpu") tcp_puts("%u", HalGetCpuClk());
else ifcmp("debug") tcp_put('1' - (print_off & 1)); // rtl_print on/off else ifcmp("debug") tcp_put('1' - (print_off & 1)); // rtl_print on/off
#if WEB_DEBUG_FUNCTIONS #if WEB_DEBUG_FUNCTIONS
#if 1 // WEB_INA219_DRV
else ifcmp("ina219") {
if(CheckSCB(SCB_WEBSOC)) {
ina219_ws(val);
}
}
#endif
else ifcmp("restart") { else ifcmp("restart") {
#if USE_WEB_AUTH_LEVEL #if USE_WEB_AUTH_LEVEL
if(web_conn->auth_level < WEB_AUTH_LEVEL_USER) return; if(web_conn->auth_level < WEB_AUTH_LEVEL_USER) return;
#endif #endif
web_conn->web_disc_cb = (web_func_disc_cb)sys_reset; webserver_qfn((web_ex_func_cb)sys_reset, NULL, 200);
} }
else ifcmp("ram") tcp_puts("0x%08x", *((uint32 *)(ahextoul(cstr+3)&(~3)))); else ifcmp("ram") tcp_puts("0x%08x", *((uint32 *)(ahextoul(cstr+3)&(~3))));
else ifcmp("rdec") tcp_puts("%d", *((uint32 *)(ahextoul(cstr+4)&(~3)))); else ifcmp("rdec") tcp_puts("%d", *((uint32 *)(ahextoul(cstr+4)&(~3))));
@ -451,10 +460,7 @@ void ICACHE_FLASH_ATTR web_int_callback(TCP_SERV_CONN *ts_conn, uint8 *cstr)
else ifcmp("wifi_") { else ifcmp("wifi_") {
cstr+=5; cstr+=5;
ifcmp("rdcfg") read_wifi_cfg(-1); ifcmp("rdcfg") read_wifi_cfg(-1);
else ifcmp("newcfg") { else ifcmp("newcfg") webserver_qfn((web_ex_func_cb)wifi_run, (void *)wifi_cfg.mode, 200);
web_conn->web_disc_cb = (web_func_disc_cb)wifi_run;
web_conn->web_disc_par = wifi_cfg.mode;
}
else ifcmp("cmode") tcp_puts("%d", wifi_mode); else ifcmp("cmode") tcp_puts("%d", wifi_mode);
else ifcmp("mode") tcp_puts("%d", wifi_cfg.mode); else ifcmp("mode") tcp_puts("%d", wifi_cfg.mode);
else ifcmp("bgn") tcp_puts("%d", wifi_cfg.bgn); else ifcmp("bgn") tcp_puts("%d", wifi_cfg.bgn);

View file

@ -96,7 +96,7 @@ void ICACHE_FLASH_ATTR web_int_vars(TCP_SERV_CONN *ts_conn, uint8 *pcmd, uint8 *
else ifcmp("sys_") { else ifcmp("sys_") {
cstr+=4; cstr+=4;
ifcmp("restart") { ifcmp("restart") {
if(val == 12345) web_conn->web_disc_cb = (web_func_disc_cb)sys_reset; if(val == 12345) webserver_qfn((web_ex_func_cb)sys_reset, NULL, 200);
} }
else ifcmp("ram") { uint32 *ptr = (uint32 *)(ahextoul(cstr+3)&(~3)); str_array(pvar, ptr, 32); } else ifcmp("ram") { uint32 *ptr = (uint32 *)(ahextoul(cstr+3)&(~3)); str_array(pvar, ptr, 32); }
else ifcmp("debug") print_off = (!val) & 1; // rtl_print on/off else ifcmp("debug") print_off = (!val) & 1; // rtl_print on/off
@ -119,9 +119,8 @@ void ICACHE_FLASH_ATTR web_int_vars(TCP_SERV_CONN *ts_conn, uint8 *pcmd, uint8 *
cstr += 4; cstr += 4;
ifcmp("port") { ifcmp("port") {
if(syscfg.web_port != val) { if(syscfg.web_port != val) {
web_conn->web_disc_par = syscfg.web_port; // ts_conn->pcfg->port webserver_qfn((web_ex_func_cb)webserver_reinit, (void *)syscfg.web_port, 200);
syscfg.web_port = val; syscfg.web_port = val;
web_conn->web_disc_cb = (web_func_disc_cb)webserver_reinit;
} }
} }
else ifcmp("twd") { else ifcmp("twd") {
@ -190,10 +189,7 @@ void ICACHE_FLASH_ATTR web_int_vars(TCP_SERV_CONN *ts_conn, uint8 *pcmd, uint8 *
cstr+=5; cstr+=5;
ifcmp("scan") api_wifi_scan(NULL); ifcmp("scan") api_wifi_scan(NULL);
else ifcmp("rdcfg") web_conn->udata_stop = read_wifi_cfg(val); else ifcmp("rdcfg") web_conn->udata_stop = read_wifi_cfg(val);
else ifcmp("newcfg") { else ifcmp("newcfg") webserver_qfn((web_ex_func_cb)wifi_run, (void *)wifi_cfg.mode, 200);
web_conn->web_disc_cb = (web_func_disc_cb)wifi_run;
web_conn->web_disc_par = wifi_cfg.mode;
}
else ifcmp("mode") wifi_cfg.mode = val; else ifcmp("mode") wifi_cfg.mode = val;
else ifcmp("bgn") wifi_cfg.bgn = val; else ifcmp("bgn") wifi_cfg.bgn = val;
else ifcmp("lflg") wifi_cfg.load_flg = val; else ifcmp("lflg") wifi_cfg.load_flg = val;
@ -247,7 +243,7 @@ void ICACHE_FLASH_ATTR web_int_vars(TCP_SERV_CONN *ts_conn, uint8 *pcmd, uint8 *
netbios_set_name(WLAN_AP_NETIF_NUM, lwip_host_name[1]); netbios_set_name(WLAN_AP_NETIF_NUM, lwip_host_name[1]);
if(wifi_cfg.save_flg & BID_AP_HOSTNAME) { if(wifi_cfg.save_flg & BID_AP_HOSTNAME) {
WEB_SRV_QFNK x; WEB_SRV_QFNK x;
x.fnk = write_wifi_cfg; x.fnc = write_wifi_cfg;
x.param = BID_AP_HOSTNAME; x.param = BID_AP_HOSTNAME;
xQueueSendToBack(xQueueWebSrv, &x, 0); xQueueSendToBack(xQueueWebSrv, &x, 0);
} }
@ -304,8 +300,9 @@ void ICACHE_FLASH_ATTR web_int_vars(TCP_SERV_CONN *ts_conn, uint8 *pcmd, uint8 *
} }
if(wifi_cfg.save_flg & BID_ST_HOSTNAME) { if(wifi_cfg.save_flg & BID_ST_HOSTNAME) {
WEB_SRV_QFNK x; WEB_SRV_QFNK x;
x.fnk = write_wifi_cfg; x.fnc = write_wifi_cfg;
x.param = BID_ST_HOSTNAME; x.param = BID_ST_HOSTNAME;
x.pause_ms = 0;
xQueueSendToBack(xQueueWebSrv, &x, 0); xQueueSendToBack(xQueueWebSrv, &x, 0);
} }
} }

View file

@ -1982,34 +1982,47 @@ LOCAL void ICACHE_FLASH_ATTR webserver_disconnect(TCP_SERV_CONN *ts_conn)
if(CheckSCB(SCB_SYSSAVE)) { if(CheckSCB(SCB_SYSSAVE)) {
ClrSCB(SCB_SYSSAVE); ClrSCB(SCB_SYSSAVE);
sys_write_cfg(); sys_write_cfg();
}
if(web_conn->web_disc_cb != NULL) {
if(xQueueSendToBack(xQueueWebSrv, &web_conn->web_disc_cb, 0) != pdPASS) {
#if DEBUGSOO > 1
os_printf("\nWEB: Queue QFn full!\n");
#endif
}
// web_conn->web_disc_cb(web_conn->web_disc_par);
}; };
} }
/****************************************************************************** /******************************************************************************
******************************************************************************/ ******************************************************************************/
BaseType_t webserver_qfn(web_ex_func_cb fnc, void * param, uint16 pause_ms) {
WEB_SRV_QFNK qfn;
qfn.fnc = fnc;
qfn.param = param;
qfn.pause_ms = pause_ms;
return xQueueSendToBack(xQueueWebSrv, &qfn, 0);
}
/******************************************************************************
* todo: временная затычка, необходимо переделать...
******************************************************************************/
void qfnk_task(void) void qfnk_task(void)
{ {
WEB_SRV_QFNK qfn; WEB_SRV_QFNK qfn;
WEB_SRV_QFNK qfnt;
TickType_t timetick;
while(1) { while(1) {
if(xQueueReceive(xQueueWebSrv, &qfn, portMAX_DELAY ) == pdPASS) { if(xQueueReceive(xQueueWebSrv, &qfn, 5) == pdPASS) { // portMAX_DELAY
if(qfn.fnk) { if(qfn.fnc) {
#if DEBUGSOO > 2 #if DEBUGSOO > 2
os_printf("qfn: %p(%p)\n", qfn.fnk, qfn.param); os_printf("qfn: %p(%p),%d\n", qfn.fnc, qfn.param, qfn.pause_ms);
#endif #endif
vTaskDelay(200); // Timeout + WDT if(qfn.pause_ms) {
qfn.fnk(qfn.param); timetick = xTaskGetTickCount();
qfnt = qfn;
}
else qfn.fnc(qfn.param);
}
}
else if(qfnt.fnc) {
if(xTaskGetTickCount() - timetick > qfnt.pause_ms) {
#if DEBUGSOO > 3
os_printf("qfnt: %p(%p),%d\n", qfnt.fnc, qfnt.param, qfnt.pause_ms);
#endif
qfnt.fnc(qfnt.param);
qfnt.fnc = NULL;
} }
// else {
// vTaskDelete(NULL);
// }
} }
} }
} }
@ -2019,15 +2032,12 @@ void qfnk_task(void)
* Parameters : arg -- port N * Parameters : arg -- port N
* Returns : none * Returns : none
*******************************************************************************/ *******************************************************************************/
//TaskHandle_t xHandleQfn;
err_t ICACHE_FLASH_ATTR webserver_init(uint16 portn) err_t ICACHE_FLASH_ATTR webserver_init(uint16 portn)
{ {
// WEBFSInit(); // файловая система // WEBFSInit(); // файловая система
err_t err = ERR_MEM; err_t err = ERR_MEM;
xQueueWebSrv = xQueueCreate(5, sizeof( WEB_SRV_QFNK )); // Create a queue... xQueueWebSrv = xQueueCreate(5, sizeof( WEB_SRV_QFNK )); // Create a queue...
if(xQueueWebSrv) { if(xQueueWebSrv) {
// if(xTaskCreate(qfnk_task, "web_qfn", 1024, NULL, tskIDLE_PRIORITY + 1 + PRIORITIE_OFFSET, &xHandleQfn) == pdPASS)
if(xTaskCreate(qfnk_task, "web_qfn", 1024, NULL, tskIDLE_PRIORITY + 1 + PRIORITIE_OFFSET, NULL) == pdPASS) if(xTaskCreate(qfnk_task, "web_qfn", 1024, NULL, tskIDLE_PRIORITY + 1 + PRIORITIE_OFFSET, NULL) == pdPASS)
{ {
TCP_SERV_CFG *p = tcpsrv_init(portn); TCP_SERV_CFG *p = tcpsrv_init(portn);
@ -2065,7 +2075,6 @@ err_t ICACHE_FLASH_ATTR webserver_init(uint16 portn)
// else err = ERR_MEM; // else err = ERR_MEM;
return err; return err;
} }
/****************************************************************************** /******************************************************************************
* FunctionName : webserver_close * FunctionName : webserver_close
* Description : закрытие сервера * Description : закрытие сервера
@ -2081,21 +2090,14 @@ err_t ICACHE_FLASH_ATTR webserver_close(uint16 portn)
#endif #endif
if(xQueueWebSrv) { if(xQueueWebSrv) {
WEB_SRV_QFNK qfn; WEB_SRV_QFNK qfn;
qfn.fnk = vTaskDelete; qfn.fnc = vTaskDelete;
qfn.fnk = NULL;
qfn.param = NULL; qfn.param = NULL;
qfn.pause_ms = 0;
if(xQueueSendToBack(xQueueWebSrv, &qfn, 1000) == pdPASS) { if(xQueueSendToBack(xQueueWebSrv, &qfn, 1000) == pdPASS) {
while(uxQueueMessagesWaiting(xQueueWebSrv)) { while(uxQueueMessagesWaiting(xQueueWebSrv)) {
vTaskDelay(10); vTaskDelay(10);
}; };
} }
/*
else if(xHandleQfn) {
vTaskDelete(xHandleQfn);
}
vQueueDelete(xQueueWebSrv);
xHandleQfn = NULL;
*/
xQueueWebSrv = NULL; xQueueWebSrv = NULL;
}; };
return err; return err;

View file

@ -162,6 +162,7 @@ websock_rx_data(TCP_SERV_CONN *ts_conn)
} }
} }
else { else {
if(web_conn->msgbuf) os_free(web_conn->msgbuf);
web_conn->msgbuf = (uint8 *) os_malloc(web_conn->msgbufsize); web_conn->msgbuf = (uint8 *) os_malloc(web_conn->msgbufsize);
if (web_conn->msgbuf == NULL) { if (web_conn->msgbuf == NULL) {
#if DEBUGSOO > 0 #if DEBUGSOO > 0
@ -176,7 +177,7 @@ websock_rx_data(TCP_SERV_CONN *ts_conn)
if(CheckSCB(SCB_RETRYCB)) { // повторный callback? да if(CheckSCB(SCB_RETRYCB)) { // повторный callback? да
if(web_conn->func_web_cb != NULL) web_conn->func_web_cb(ts_conn); if(web_conn->func_web_cb != NULL) web_conn->func_web_cb(ts_conn);
if(!CheckSCB(SCB_RETRYCB)) { if(!CheckSCB(SCB_RETRYCB)) {
ClrSCB(SCB_FCLOSE | SCB_DISCONNECT); // ClrSCB(SCB_FCLOSE | SCB_DISCONNECT);
opcode = WS_OPCODE_CONTINUE | WS_FRAGMENT_FIN; opcode = WS_OPCODE_CONTINUE | WS_FRAGMENT_FIN;
} }
else opcode = WS_OPCODE_CONTINUE; else opcode = WS_OPCODE_CONTINUE;
@ -194,18 +195,18 @@ websock_rx_data(TCP_SERV_CONN *ts_conn)
} }
if(CheckSCB(SCB_RETRYCB)) opcode = WS_OPCODE_TEXT; if(CheckSCB(SCB_RETRYCB)) opcode = WS_OPCODE_TEXT;
else { else {
ClrSCB(SCB_FCLOSE | SCB_DISCONNECT); // ClrSCB(SCB_FCLOSE | SCB_DISCONNECT);
opcode = WS_OPCODE_TEXT | WS_FRAGMENT_FIN; opcode = WS_OPCODE_TEXT | WS_FRAGMENT_FIN;
} }
} }
if(web_conn->msgbuflen != 0) { if(web_conn->msgbuflen != 0) {
if(websock_tx_frame(ts_conn, opcode, web_conn->msgbuf, web_conn->msgbuflen) != ERR_OK) { if(websock_tx_frame(ts_conn, opcode, web_conn->msgbuf, web_conn->msgbuflen) != ERR_OK) {
os_free(web_conn->msgbuf); if(web_conn->msgbuf) os_free(web_conn->msgbuf);
web_conn->msgbuf = NULL; web_conn->msgbuf = NULL;
return false; // не докачивать, ошибка или закрытие return false; // не докачивать, ошибка или закрытие
} }
} }
os_free(web_conn->msgbuf); if(web_conn->msgbuf) os_free(web_conn->msgbuf);
web_conn->msgbuf = NULL; web_conn->msgbuf = NULL;
if(CheckSCB(SCB_RETRYCB)) return false; if(CheckSCB(SCB_RETRYCB)) return false;
} }