diff --git a/code/espurna/analog.ino b/code/espurna/analog.ino index 91cef6d0..77abe603 100644 --- a/code/espurna/analog.ino +++ b/code/espurna/analog.ino @@ -55,7 +55,7 @@ void analogLoop() { // Update websocket clients #if WEB_SUPPORT char buffer[100]; - snprintf_P(buffer, strlen(buffer), PSTR("{\"analogVisible\": 1, \"analogValue\": %d}"), analog); + snprintf_P(buffer, sizeof(buffer), PSTR("{\"analogVisible\": 1, \"analogValue\": %d}"), analog); wsSend(buffer); #endif diff --git a/code/espurna/button.ino b/code/espurna/button.ino index 4167c1b1..fc1142b4 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -25,7 +25,7 @@ std::vector _buttons; void buttonMQTT(unsigned char id, uint8_t event) { if (id >= _buttons.size()) return; char payload[2]; - snprintf_P(payload, strlen(payload), PSTR("%d"), event); + snprintf_P(payload, sizeof(payload), PSTR("%d"), event); mqttSend(MQTT_TOPIC_BUTTON, id, payload); } #endif diff --git a/code/espurna/dht.ino b/code/espurna/dht.ino index d1198f91..5dd5b199 100644 --- a/code/espurna/dht.ino +++ b/code/espurna/dht.ino @@ -93,7 +93,7 @@ void dhtLoop() { status = HUMIDITY_DRY; } char buffer[2]; - snprintf_P(buffer, strlen(buffer), PSTR("%d"), status); + snprintf_P(buffer, sizeof(buffer), PSTR("%d"), status); domoticzSend("dczHumIdx", humidity, buffer); } #endif @@ -106,7 +106,7 @@ void dhtLoop() { // Update websocket clients #if WEB_SUPPORT char buffer[100]; - snprintf_P(buffer, strlen(buffer), PSTR("{\"dhtVisible\": 1, \"dhtTmp\": %s, \"dhtHum\": %s, \"tmpUnits\": %d}"), temperature, humidity, tmpUnits); + snprintf_P(buffer, sizeof(buffer), PSTR("{\"dhtVisible\": 1, \"dhtTmp\": %s, \"dhtHum\": %s, \"tmpUnits\": %d}"), temperature, humidity, tmpUnits); wsSend(buffer); #endif diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index 72d8dc12..38e90c11 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -72,7 +72,7 @@ template void domoticzSend(const char * key, T nvalue, const char * unsigned int idx = getSetting(key).toInt(); if (idx > 0) { char payload[128]; - snprintf(payload, strlen(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue); + snprintf(payload, sizeof(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue); mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload); } } @@ -84,13 +84,13 @@ template void domoticzSend(const char * key, T nvalue) { void domoticzSendRelay(unsigned int relayID) { if (!_dczEnabled) return; char buffer[15]; - snprintf_P(buffer, strlen(buffer), PSTR("dczRelayIdx%d"), relayID); + snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%d"), relayID); domoticzSend(buffer, relayStatus(relayID) ? "1" : "0"); } int domoticzIdx(unsigned int relayID) { char buffer[15]; - snprintf_P(buffer, strlen(buffer), PSTR("dczRelayIdx%d"), relayID); + snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%d"), relayID); return getSetting(buffer).toInt(); } diff --git a/code/espurna/ds18b20.ino b/code/espurna/ds18b20.ino index 9bbb8d76..a7a7ff74 100644 --- a/code/espurna/ds18b20.ino +++ b/code/espurna/ds18b20.ino @@ -110,7 +110,7 @@ void dsLoop() { // Update websocket clients #if WEB_SUPPORT char buffer[100]; - snprintf_P(buffer, strlen(buffer), PSTR("{\"dsVisible\": 1, \"dsTmp\": %s, \"tmpUnits\": %d}"), getDSTemperatureStr(), tmpUnits); + snprintf_P(buffer, sizeof(buffer), PSTR("{\"dsVisible\": 1, \"dsTmp\": %s, \"tmpUnits\": %d}"), getDSTemperatureStr(), tmpUnits); wsSend(buffer); #endif diff --git a/code/espurna/emon.ino b/code/espurna/emon.ino index 065cab50..e2c90a28 100644 --- a/code/espurna/emon.ino +++ b/code/espurna/emon.ino @@ -169,7 +169,7 @@ void powerMonitorLoop() { // Update websocket clients #if WEB_SUPPORT char buffer[100]; - snprintf_P(buffer, strlen(buffer), PSTR("{\"emonVisible\": 1, \"emonApparentPower\": %d, \"emonCurrent\": %s}"), int(current * voltage), String(current, 3).c_str()); + snprintf_P(buffer, sizeof(buffer), PSTR("{\"emonVisible\": 1, \"emonApparentPower\": %d, \"emonCurrent\": %s}"), int(current * voltage), String(current, 3).c_str()); wsSend(buffer); #endif @@ -195,11 +195,11 @@ void powerMonitorLoop() { #if DOMOTICZ_SUPPORT { char buffer[20]; - snprintf_P(buffer, strlen(buffer), PSTR("%d;%s"), _emonPower, String(energy_delta, 3).c_str()); + snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), _emonPower, String(energy_delta, 3).c_str()); domoticzSend("dczPowIdx", 0, buffer); - snprintf_P(buffer, strlen(buffer), PSTR("%s"), String(energy_delta, 3).c_str()); + snprintf_P(buffer, sizeof(buffer), PSTR("%s"), String(energy_delta, 3).c_str()); domoticzSend("dczEnergyIdx", 0, buffer); - snprintf_P(buffer, strlen(buffer), PSTR("%s"), String(_emonCurrent, 3).c_str()); + snprintf_P(buffer, sizeof(buffer), PSTR("%s"), String(_emonCurrent, 3).c_str()); domoticzSend("dczCurrentIdx", 0, buffer); } #endif diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 51b13f07..333a6f66 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -28,7 +28,7 @@ along with this program. If not, see . String getIdentifier() { char buffer[20]; - snprintf_P(buffer, strlen(buffer), PSTR("%s_%06X"), DEVICE, ESP.getChipId()); + snprintf_P(buffer, sizeof(buffer), PSTR("%s_%06X"), DEVICE, ESP.getChipId()); return String(buffer); } diff --git a/code/espurna/hlw8012.ino b/code/espurna/hlw8012.ino index 0f3b3ea0..348f626d 100644 --- a/code/espurna/hlw8012.ino +++ b/code/espurna/hlw8012.ino @@ -183,7 +183,7 @@ void hlw8012Setup() { }); #endif // WEB_SUPPORT - + } void hlw8012Loop() { @@ -299,13 +299,13 @@ void hlw8012Loop() { #if DOMOTICZ_SUPPORT { char buffer[20]; - snprintf_P(buffer, strlen(buffer), PSTR("%d;%s"), _hlwPower, String(energy_delta, 3).c_str()); + snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), _hlwPower, String(energy_delta, 3).c_str()); domoticzSend("dczPowIdx", 0, buffer); - snprintf_P(buffer, strlen(buffer), PSTR("%s"), String(energy_delta, 3).c_str()); + snprintf_P(buffer, sizeof(buffer), PSTR("%s"), String(energy_delta, 3).c_str()); domoticzSend("dczEnergyIdx", 0, buffer); - snprintf_P(buffer, strlen(buffer), PSTR("%d"), _hlwVoltage); + snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _hlwVoltage); domoticzSend("dczVoltIdx", 0, buffer); - snprintf_P(buffer, strlen(buffer), PSTR("%s"), String(_hlwCurrent).c_str()); + snprintf_P(buffer, sizeof(buffer), PSTR("%s"), String(_hlwCurrent).c_str()); domoticzSend("dczCurrentIdx", 0, buffer); } #endif diff --git a/code/espurna/influxdb.ino b/code/espurna/influxdb.ino index 087652a0..caf79c2e 100644 --- a/code/espurna/influxdb.ino +++ b/code/espurna/influxdb.ino @@ -28,11 +28,11 @@ template bool influxDBSend(const char * topic, T payload) { } char data[128]; - snprintf(data, strlen(data), "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str()); + snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str()); DEBUG_MSG("[INFLUXDB] Data: %s\n", data); char request[256]; - snprintf(request, strlen(request), "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\nHost: %s:%d\r\nContent-Length: %d\r\n\r\n%s", + snprintf(request, sizeof(request), "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\nHost: %s:%d\r\nContent-Length: %d\r\n\r\n%s", getSetting("idbDatabase").c_str(), getSetting("idbUsername").c_str(), getSetting("idbPassword").c_str(), getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt(), strlen(data), data); diff --git a/code/espurna/led.ino b/code/espurna/led.ino index 580454a9..7366c268 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -64,7 +64,7 @@ void ledMQTTCallback(unsigned int type, const char * topic, const char * payload if (type == MQTT_CONNECT_EVENT) { char buffer[strlen(MQTT_TOPIC_LED) + 3]; - snprintf_P(buffer, strlen(buffer), PSTR("%s/+"), MQTT_TOPIC_LED); + snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_LED); mqttSubscribe(buffer); } diff --git a/code/espurna/light.ino b/code/espurna/light.ino index f93d0399..3d8db861 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -293,7 +293,7 @@ void _lightMQTTCallback(unsigned int type, const char * topic, const char * payl } char buffer[strlen(MQTT_TOPIC_CHANNEL) + 3]; - snprintf_P(buffer, strlen(buffer), PSTR("%s/+"), MQTT_TOPIC_CHANNEL); + snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_CHANNEL); mqttSubscribe(buffer); } @@ -370,14 +370,14 @@ void lightMQTT() { mqttSend(MQTT_TOPIC_COLOR, buffer); // Brightness - snprintf_P(buffer, strlen(buffer), PSTR("%d"), _brightness); + snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _brightness); mqttSend(MQTT_TOPIC_BRIGHTNESS, buffer); } // Channels for (unsigned int i=0; i < _channels.size(); i++) { - snprintf_P(buffer, strlen(buffer), PSTR("%d"), _channels[i].value); + snprintf_P(buffer, sizeof(buffer), PSTR("%d"), _channels[i].value); mqttSend(MQTT_TOPIC_CHANNEL, i, buffer); } @@ -509,10 +509,10 @@ void _lightAPISetup() { for (unsigned int id=0; id= _relays.size()) return; char buffer[10]; - snprintf_P(buffer, strlen(buffer), PSTR("%s,id=%d"), MQTT_TOPIC_RELAY, id); + snprintf_P(buffer, sizeof(buffer), PSTR("%s,id=%d"), MQTT_TOPIC_RELAY, id); influxDBSend(buffer, relayStatus(id) ? "1" : "0"); } #endif diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index ee2aa9af..f4f59f3c 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -58,7 +58,7 @@ void _rfbLearn() { #if WEB_SUPPORT char buffer[100]; - snprintf_P(buffer, strlen(buffer), PSTR("{\"action\": \"rfbLearn\", \"data\":{\"id\": %d, \"status\": %d}}"), _learnId, _learnStatus ? 1 : 0); + snprintf_P(buffer, sizeof(buffer), PSTR("{\"action\": \"rfbLearn\", \"data\":{\"id\": %d, \"status\": %d}}"), _learnId, _learnStatus ? 1 : 0); wsSend(buffer); #endif @@ -120,7 +120,7 @@ void _rfbDecode() { // Websocket update #if WEB_SUPPORT char wsb[100]; - snprintf_P(wsb, strlen(wsb), PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"%s\"}]}"), _learnId, _learnStatus ? 1 : 0, buffer); + snprintf_P(wsb, sizeof(wsb), PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"%s\"}]}"), _learnId, _learnStatus ? 1 : 0, buffer); wsSend(wsb); #endif @@ -216,7 +216,7 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa if (type == MQTT_CONNECT_EVENT) { char buffer[strlen(MQTT_TOPIC_RFLEARN) + 3]; - snprintf_P(buffer, strlen(buffer), PSTR("%s/+"), MQTT_TOPIC_RFLEARN); + snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_RFLEARN); mqttSubscribe(buffer); mqttSubscribe(MQTT_TOPIC_RFOUT); } @@ -257,13 +257,13 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa void rfbStore(unsigned char id, bool status, const char * code) { DEBUG_MSG_P(PSTR("[RFBRIDGE] Storing %d-%s => '%s'\n"), id, status ? "ON" : "OFF", code); char key[8] = {0}; - snprintf_P(key, strlen(key), PSTR("rfb%d%s"), id, status ? "on" : "off"); + snprintf_P(key, sizeof(key), PSTR("rfb%d%s"), id, status ? "on" : "off"); setSetting(key, code); } String rfbRetrieve(unsigned char id, bool status) { char key[8] = {0}; - snprintf_P(key, strlen(key), PSTR("rfb%d%s"), id, status ? "on" : "off"); + snprintf_P(key, sizeof(key), PSTR("rfb%d%s"), id, status ? "on" : "off"); return getSetting(key); } @@ -288,13 +288,13 @@ void rfbLearn(unsigned char id, bool status) { void rfbForget(unsigned char id, bool status) { char key[8] = {0}; - snprintf_P(key, strlen(key), PSTR("rfb%d%s"), id, status ? "on" : "off"); + snprintf_P(key, sizeof(key), PSTR("rfb%d%s"), id, status ? "on" : "off"); delSetting(key); // Websocket update #if WEB_SUPPORT char wsb[100]; - snprintf_P(wsb, strlen(wsb), PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"\"}]}"), id, status ? 1 : 0); + snprintf_P(wsb, sizeof(wsb), PSTR("{\"rfb\":[{\"id\": %d, \"status\": %d, \"data\": \"\"}]}"), id, status ? 1 : 0); wsSend(wsb); #endif diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 5e032b0f..48539c29 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -383,7 +383,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { void _wsStart(uint32_t client_id) { char chipid[6]; - snprintf_P(chipid, strlen(chipid), PSTR("%06X"), ESP.getChipId()); + snprintf_P(chipid, sizeof(chipid), PSTR("%06X"), ESP.getChipId()); DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); @@ -691,6 +691,7 @@ void wsSetup() { _ws.onEvent(_wsEvent); mqttRegister(_wsMQTTCallback); _server->addHandler(&_ws); + _server->on("/auth", HTTP_GET, _onAuth); } // ----------------------------------------------------------------------------- @@ -764,7 +765,7 @@ ArRequestHandlerFunction _bindAPI(unsigned int apiID) { // Format response according to the Accept header if (_asJson(request)) { char buffer[64]; - snprintf_P(buffer, strlen(buffer), PSTR("{ \"%s\": %s }"), api.key, p); + snprintf_P(buffer, sizeof(buffer), PSTR("{ \"%s\": %s }"), api.key, p); request->send(200, "application/json", buffer); } else { request->send(200, "text/plain", p); @@ -837,7 +838,7 @@ void apiRegister(const char * url, const char * key, apiGetCallbackFunction getF // Store it web_api_t api; char buffer[40]; - snprintf_P(buffer, strlen(buffer), PSTR("/api/%s"), url); + snprintf_P(buffer, sizeof(buffer), PSTR("/api/%s"), url); api.url = strdup(buffer); api.key = strdup(key); api.getFn = getFn; @@ -913,7 +914,7 @@ void _onGetConfig(AsyncWebServerRequest *request) { } char buffer[100]; - snprintf_P(buffer, strlen(buffer), PSTR("attachment; filename=\"%s-backup.json\""), (char *) getSetting("hostname").c_str()); + snprintf_P(buffer, sizeof(buffer), PSTR("attachment; filename=\"%s-backup.json\""), (char *) getSetting("hostname").c_str()); response->addHeader("Content-Disposition", buffer); response->setLength(); request->send(response); @@ -988,7 +989,7 @@ void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t inde void webSetup() { // Cache the Last-Modifier header value - snprintf_P(_last_modified, strlen(_last_modified), PSTR("%s %s GMT"), __DATE__, __TIME__); + snprintf_P(_last_modified, sizeof(_last_modified), PSTR("%s %s GMT"), __DATE__, __TIME__); // Create server _server = new AsyncWebServer(getSetting("webPort", WEB_PORT).toInt()); @@ -1007,7 +1008,6 @@ void webSetup() { _server->on("/index.html", HTTP_GET, _onHome); #endif _server->on("/config", HTTP_GET, _onGetConfig); - _server->on("/auth", HTTP_GET, _onAuth); _server->on("/upgrade", HTTP_POST, _onUpgrade, _onUpgradeData); // Serve static files