Fix improv possible string errors

This commit is contained in:
Theo Arends 2022-04-02 14:17:33 +02:00
parent c3132594d3
commit 379c84a863
1 changed files with 19 additions and 30 deletions

View File

@ -114,6 +114,21 @@ void ImprovSendResponse(uint8_t* response, uint32_t size) {
data[7] = IMPROV_TYPE_RPC_RESPONSE; // 0x04
data[8] = size -1;
memcpy(data +9, response, size);
data[10] = size -3; // Total length of strings following
if (data[10]) {
// Replace '\n' (= lf) with string length
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...
// I M P R O V ve ty le co lf T a s m o t a lf 1 1 . 0 . 0 . 5 lf ...
// I M P R O V ve ty le co l1 T a s m o t a l2 1 1 . 0 . 0 . 5 lf ...
uint32_t str_pos = 11;
for (uint32_t i = 12; i < sizeof(data); i++) {
if ('\n' == data[i]) {
data[str_pos] = i - str_pos -1; // Replace lf with string length
str_pos = i;
}
}
}
ImprovWriteData(data, sizeof(data));
}
@ -121,17 +136,10 @@ void ImprovSendSetting(uint32_t command) {
char data[100];
uint32_t len = 0;
#ifdef USE_WEBSERVER
len = ext_snprintf_P(data, sizeof(data), PSTR("01|http://%_I:%d|"), (uint32_t)WiFi.localIP(), WEB_PORT);
uint32_t str_pos = 2;
for (uint32_t i = 3; i < len; i++) {
if ('|' == data[i]) {
data[str_pos] = i - str_pos -1;
}
}
len = ext_snprintf_P(data, sizeof(data), PSTR("01\nhttp://%_I:%d\n"), (uint32_t)WiFi.localIP(), WEB_PORT);
len -= 3;
#endif // USE_WEBSERVER
data[0] = command;
data[1] = len;
ImprovSendResponse((uint8_t*)data, len +3);
}
@ -207,18 +215,9 @@ bool ImprovParseSerialByte(void) {
}
case IMPROV_GET_DEVICE_INFO: { // 0x03
char data[200];
uint32_t len = snprintf_P(data, sizeof(data), PSTR("01|Tasmota|%s|%s|%s|"),
uint32_t len = snprintf_P(data, sizeof(data), PSTR("01\nTasmota\n%s\n%s\n%s\n"),
TasmotaGlobal.version, GetDeviceHardware().c_str(), SettingsText(SET_DEVICENAME));
data[0] = IMPROV_GET_DEVICE_INFO;
data[1] = len -3;
uint32_t str_pos = 2;
for (uint32_t i = 3; i < len; i++) {
if ('|' == data[i]) {
data[str_pos] = i - str_pos -1;
str_pos = i;
}
}
ImprovSendResponse((uint8_t*)data, len);
break;
}
@ -259,25 +258,15 @@ bool ImprovParseSerialByte(void) {
if (!ssid_copy.length()) { ssid_copy = F("no_name"); }
// Send each ssid separately to avoid overflowing the buffer
uint32_t len = snprintf_P(data, sizeof(data), PSTR("01|%s|%d|%s|"), ssid_copy.c_str(), rssi, (ENC_TYPE_NONE == WiFi.encryptionType(indices[i]))?"NO":"YES");
uint32_t len = snprintf_P(data, sizeof(data), PSTR("01\n%s\n%d\n%s\n"), ssid_copy.c_str(), rssi, (ENC_TYPE_NONE == WiFi.encryptionType(indices[i]))?"NO":"YES");
data[0] = IMPROV_GET_WIFI_NETWORKS;
data[1] = len -3;
uint32_t str_pos = 2;
for (uint32_t i = 3; i < len; i++) {
if ('|' == data[i]) {
data[str_pos] = i - str_pos -1;
str_pos = i;
}
}
ImprovSendResponse((uint8_t*)data, len);
}
}
// Send empty response to signify the end of the list.
data[0] = IMPROV_GET_WIFI_NETWORKS;
data[1] = 0; // Empty string
ImprovSendResponse((uint8_t*)data, 3);
ImprovSendResponse((uint8_t*)data, 3); // Empty string
break;
}
/*