From 06a399b84aaeb99a80588582c621e946aba58b11 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Mon, 16 Sep 2019 19:54:21 +0300 Subject: [PATCH] Extend logging of ArduinoOTA (#3217) --- libraries/ArduinoOTA/src/ArduinoOTA.cpp | 49 +++++++++---------------- libraries/Update/src/Update.h | 2 + libraries/Update/src/Updater.cpp | 4 ++ 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/libraries/ArduinoOTA/src/ArduinoOTA.cpp b/libraries/ArduinoOTA/src/ArduinoOTA.cpp index 5df367d5..884c9d34 100644 --- a/libraries/ArduinoOTA/src/ArduinoOTA.cpp +++ b/libraries/ArduinoOTA/src/ArduinoOTA.cpp @@ -127,9 +127,7 @@ void ArduinoOTAClass::begin() { } _initialized = true; _state = OTA_IDLE; -#ifdef OTA_DEBUG - OTA_DEBUG.printf("OTA server at: %s.local:%u\n", _hostname.c_str(), _port); -#endif + log_i("OTA server at: %s.local:%u", _hostname.c_str(), _port); } int ArduinoOTAClass::parseInt(){ @@ -173,6 +171,7 @@ void ArduinoOTAClass::_onRx(){ _md5 = readStringUntil('\n'); _md5.trim(); if(_md5.length() != 32){ + log_e("bad md5 length"); return; } @@ -198,6 +197,7 @@ void ArduinoOTAClass::_onRx(){ } else if (_state == OTA_WAITAUTH) { int cmd = parseInt(); if (cmd != U_AUTH) { + log_e("%d was expected. got %d instead", U_AUTH, cmd); _state = OTA_IDLE; return; } @@ -205,6 +205,7 @@ void ArduinoOTAClass::_onRx(){ String cnonce = readStringUntil(' '); String response = readStringUntil('\n'); if (cnonce.length() != 32 || response.length() != 32) { + log_e("auth param fail"); _state = OTA_IDLE; return; } @@ -225,6 +226,7 @@ void ArduinoOTAClass::_onRx(){ } else { _udp_ota.beginPacket(_udp_ota.remoteIP(), _udp_ota.remotePort()); _udp_ota.print("Authentication Failed"); + log_w("Authentication Failed"); _udp_ota.endPacket(); if (_error_callback) _error_callback(OTA_AUTH_ERROR); _state = OTA_IDLE; @@ -234,9 +236,9 @@ void ArduinoOTAClass::_onRx(){ void ArduinoOTAClass::_runUpdate() { if (!Update.begin(_size, _cmd)) { -#ifdef OTA_DEBUG - Update.printError(OTA_DEBUG); -#endif + + log_e("Begin ERROR: %s", Update.errorString()); + if (_error_callback) { _error_callback(OTA_BEGIN_ERROR); } @@ -272,21 +274,15 @@ void ArduinoOTAClass::_runUpdate() { } if (!waited){ if(written && tried++ < 3){ -#ifdef OTA_DEBUG - OTA_DEBUG.printf("Try[%u]: %u\n", tried, written); -#endif + log_i("Try[%u]: %u", tried, written); if(!client.printf("%u", written)){ -#ifdef OTA_DEBUG - OTA_DEBUG.printf("failed to respond\n"); -#endif + log_e("failed to respond"); _state = OTA_IDLE; break; } continue; } -#ifdef OTA_DEBUG - OTA_DEBUG.printf("Receive Failed\n"); -#endif + log_e("Receive Failed"); if (_error_callback) { _error_callback(OTA_RECEIVE_ERROR); } @@ -295,9 +291,7 @@ void ArduinoOTAClass::_runUpdate() { return; } if(!available){ -#ifdef OTA_DEBUG - OTA_DEBUG.printf("No Data: %u\n", waited); -#endif + log_e("No Data: %u", waited); _state = OTA_IDLE; break; } @@ -317,18 +311,14 @@ void ArduinoOTAClass::_runUpdate() { log_w("didn't write enough! %u != %u", written, r); } if(!client.printf("%u", written)){ -#ifdef OTA_DEBUG - OTA_DEBUG.printf("failed to respond\n"); -#endif + log_w("failed to respond"); } total += written; if(_progress_callback) { _progress_callback(total, _size); } } else { -#ifdef OTA_DEBUG - Update.printError(OTA_DEBUG); -#endif + log_e("Write ERROR: %s", Update.errorString()); } } @@ -351,10 +341,7 @@ void ArduinoOTAClass::_runUpdate() { Update.printError(client); client.stop(); delay(10); -#ifdef OTA_DEBUG - OTA_DEBUG.print("Update ERROR: "); - Update.printError(OTA_DEBUG); -#endif + log_e("Update ERROR: %s", Update.errorString()); _state = OTA_IDLE; } } @@ -366,9 +353,7 @@ void ArduinoOTAClass::end() { MDNS.end(); } _state = OTA_IDLE; -#ifdef OTA_DEBUG - OTA_DEBUG.println("OTA server stopped."); -#endif + log_i("OTA server stopped."); } void ArduinoOTAClass::handle() { @@ -395,4 +380,4 @@ void ArduinoOTAClass::setTimeout(int timeoutInMillis) { #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_ARDUINOOTA) ArduinoOTAClass ArduinoOTA; -#endif \ No newline at end of file +#endif diff --git a/libraries/Update/src/Update.h b/libraries/Update/src/Update.h index 43b3a887..9a46a784 100644 --- a/libraries/Update/src/Update.h +++ b/libraries/Update/src/Update.h @@ -80,6 +80,8 @@ class UpdateClass { */ void printError(Stream &out); + const char * errorString(); + /* sets the expected MD5 for the firmware (hexString) */ diff --git a/libraries/Update/src/Updater.cpp b/libraries/Update/src/Updater.cpp index 5efa0920..cfa28827 100644 --- a/libraries/Update/src/Updater.cpp +++ b/libraries/Update/src/Updater.cpp @@ -363,4 +363,8 @@ void UpdateClass::printError(Stream &out){ out.println(_err2str(_error)); } +const char * UpdateClass::errorString(){ + return _err2str(_error); +} + UpdateClass Update;