Skip to content

Commit aa380ea

Browse files
authored
fix tcp server (#21660)
1 parent d189258 commit aa380ea

File tree

1 file changed

+60
-22
lines changed

1 file changed

+60
-22
lines changed

tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,12 +2600,19 @@ char *isargs(char *lp, uint32_t isind) {
26002600
if (glob_script_mem.si_num[isind] > MAX_SARRAY_NUM) {
26012601
glob_script_mem.si_num[isind] = MAX_SARRAY_NUM;
26022602
}
2603+
//glob_script_mem.last_index_string[isind] = (char*)calloc(glob_script_mem.max_ssize * glob_script_mem.si_num[isind], 1);
2604+
uint32_t sasize = glob_script_mem.max_ssize * glob_script_mem.si_num[isind];
2605+
glob_script_mem.last_index_string[isind] = (char*)special_malloc(sasize);
2606+
if (glob_script_mem.last_index_string[isind]) {
2607+
memset(glob_script_mem.last_index_string[isind], 0, sasize);
2608+
for (uint32_t cnt = 0; cnt < glob_script_mem.siro_num[isind]; cnt++) {
2609+
char str[SCRIPT_MAX_SBSIZE];
2610+
GetTextIndexed(str, sizeof(str), cnt, sstart);
2611+
strlcpy(glob_script_mem.last_index_string[isind] + (cnt * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize);
2612+
}
2613+
} else {
2614+
// memory error
26032615

2604-
glob_script_mem.last_index_string[isind] = (char*)calloc(glob_script_mem.max_ssize * glob_script_mem.si_num[isind], 1);
2605-
for (uint32_t cnt = 0; cnt < glob_script_mem.siro_num[isind]; cnt++) {
2606-
char str[SCRIPT_MAX_SBSIZE];
2607-
GetTextIndexed(str, sizeof(str), cnt, sstart);
2608-
strlcpy(glob_script_mem.last_index_string[isind] + (cnt * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize);
26092616
}
26102617
} else {
26112618
glob_script_mem.last_index_string[isind] = sstart;
@@ -3439,7 +3446,7 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value);
34393446
FS *cfp = script_file_path(str);
34403447
while (*lp == ' ') lp++;
34413448
uint8_t mode = 0;
3442-
if ((*lp == 'r') || (*lp == 'w') || (*lp == 'a')) {
3449+
if ((*lp == 'r') || (*lp == 'w') || (*lp == 'a') || (*lp == 'u')) {
34433450
switch (*lp) {
34443451
case 'r':
34453452
mode = 0;
@@ -3450,6 +3457,12 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value);
34503457
case 'a':
34513458
mode = 2;
34523459
break;
3460+
case 'u':
3461+
mode = 3;
3462+
break;
3463+
case 'U':
3464+
mode = 4;
3465+
break;
34533466
}
34543467
lp++;
34553468
} else {
@@ -3477,10 +3490,20 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value);
34773490
#ifdef DEBUG_FS
34783491
AddLog(LOG_LEVEL_INFO, PSTR("SCR: open file for write %d"), cnt);
34793492
#endif
3480-
} else {
3493+
} else if (mode == 2) {
34813494
glob_script_mem.files[cnt] = cfp->open(str,FS_FILE_APPEND);
34823495
#ifdef DEBUG_FS
34833496
AddLog(LOG_LEVEL_INFO, PSTR("SCR: open file for append %d"), cnt);
3497+
#endif
3498+
} else if (mode == 3) {
3499+
glob_script_mem.files[cnt] = cfp->open(str, "w+");
3500+
#ifdef DEBUG_FS
3501+
AddLog(LOG_LEVEL_INFO, PSTR("SCR: open file for write update %d"), cnt);
3502+
#endif
3503+
} else {
3504+
glob_script_mem.files[cnt] = cfp->open(str, "r+");
3505+
#ifdef DEBUG_FS
3506+
AddLog(LOG_LEVEL_INFO, PSTR("SCR: open file for read update %d"), cnt);
34843507
#endif
34853508
}
34863509
}
@@ -4744,22 +4767,30 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value);
47444767
len = 0;
47454768
goto exit;
47464769
}
4747-
#ifdef USE_MORITZ
4770+
#ifdef USE_BINPLUGINS
4771+
char *Plugin_Query(uint8_t, uint8_t);
47484772
if (!strncmp_XP(lp, XPSTR("mo("), 3)) {
47494773
TS_FLOAT fvar1;
47504774
lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar1, gv);
47514775
SCRIPT_SKIP_SPACES
47524776
TS_FLOAT fvar2;
47534777
lp = GetNumericArgument(lp, OPER_EQU, &fvar2, gv);
47544778
SCRIPT_SKIP_SPACES
4755-
char rbuff[64];
4756-
fvar = mo_getvars(fvar1, fvar2, rbuff);
4779+
char *rbuff = Plugin_Query(fvar1, fvar2);
4780+
if (rbuff) {
4781+
if (sp) strlcpy(sp, rbuff, glob_script_mem.max_ssize);
4782+
free (rbuff);
4783+
} else {
4784+
if (sp) {
4785+
strcpy_P(sp, PSTR("not found"));
4786+
}
4787+
}
47574788
lp++;
4758-
if (sp) strlcpy(sp, rbuff, glob_script_mem.max_ssize);
47594789
len = 0;
47604790
goto strexit;
47614791
}
4762-
#endif //USE_MORITZ
4792+
#endif //USE_BINPLUGINS
4793+
47634794
#ifdef ESP32_FAST_MUX
47644795
if (!strncmp_XP(lp, XPSTR("mux("), 4)) {
47654796
lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, gv);
@@ -6219,16 +6250,24 @@ void tmod_directModeOutput(uint32_t pin);
62196250
if (!strncmp_XP(lp, XPSTR("wso("), 4)) {
62206251
TS_FLOAT port;
62216252
lp = GetNumericArgument(lp + 4, OPER_EQU, &port, gv);
6222-
glob_script_mem.tcp_server = new WiFiServer(port);
6223-
fvar = 0;
6224-
if (!glob_script_mem.tcp_server) {
6225-
fvar = -1;
6226-
} else {
6227-
AddLog(LOG_LEVEL_INFO, PSTR("tcp server started"));
6253+
if (TasmotaGlobal.global_state.wifi_down) {
6254+
fvar = - 2;
6255+
} else {
6256+
if (glob_script_mem.tcp_server) {
6257+
glob_script_mem.tcp_client.stop();
6258+
glob_script_mem.tcp_server->stop();
6259+
delete glob_script_mem.tcp_server;
6260+
}
6261+
glob_script_mem.tcp_server = new WiFiServer(port);
6262+
fvar = 0;
6263+
if (!glob_script_mem.tcp_server) {
6264+
fvar = -1;
6265+
} else {
6266+
glob_script_mem.tcp_server->begin();
6267+
glob_script_mem.tcp_server->setNoDelay(true);
6268+
AddLog(LOG_LEVEL_INFO, PSTR("tcp server started"));
6269+
}
62286270
}
6229-
glob_script_mem.tcp_server->begin();
6230-
glob_script_mem.tcp_server->setNoDelay(true);
6231-
62326271
goto nfuncexit;
62336272
}
62346273
if (!strncmp_XP(lp, XPSTR("wsc("), 4)) {
@@ -12055,7 +12094,6 @@ exgc:
1205512094
}
1205612095
}
1205712096
snprintf_P(options, SCRIPT_GC_OPTIONS_SIZE, SCRIPT_MSG_GOPT4);
12058-
if (options) free(options);
1205912097
}
1206012098
if (tonly) {
1206112099
WSContentSend_P("]);");

0 commit comments

Comments
 (0)