1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Replace some manually managed buffers with Strings, fix code style

This commit is contained in:
Ivan Grokhotkov 2015-11-11 01:07:02 +03:00
parent 450718b4a4
commit 92069e657b
5 changed files with 188 additions and 167 deletions

View File

@ -32,7 +32,6 @@ void UpdaterClass::_reset() {
_currentAddress = 0; _currentAddress = 0;
_size = 0; _size = 0;
_command = U_FLASH; _command = U_FLASH;
_target_md5 = 0;
} }
bool UpdaterClass::begin(size_t size, int command) { bool UpdaterClass::begin(size_t size, int command) {
@ -98,14 +97,13 @@ bool UpdaterClass::begin(size_t size, int command) {
_buffer = new uint8_t[FLASH_SECTOR_SIZE]; _buffer = new uint8_t[FLASH_SECTOR_SIZE];
_command = command; _command = command;
_target_md5 = new char[64];
_md5.begin(); _md5.begin();
return true; return true;
} }
void UpdaterClass::setMD5(const char * expected_md5){ void UpdaterClass::setMD5(const char * expected_md5){
if(strlen(expected_md5) != 32) return; if(strlen(expected_md5) != 32) return;
strcpy(_target_md5, expected_md5); _target_md5 = expected_md5;
} }
bool UpdaterClass::end(bool evenIfRemaining){ bool UpdaterClass::end(bool evenIfRemaining){
@ -133,16 +131,16 @@ bool UpdaterClass::end(bool evenIfRemaining){
} }
_md5.calculate(); _md5.calculate();
if(_target_md5 && strlen(_target_md5) == 32){ if(_target_md5.length()) {
if(strcmp(_target_md5, _md5.toString().c_str()) != 0){ if(_target_md5 != _md5.toString()){
_error = UPDATE_ERROR_MD5; _error = UPDATE_ERROR_MD5;
#ifdef DEBUG_UPDATER #ifdef DEBUG_UPDATER
DEBUG_UPDATER.printf("MD5 Failed: expected:%s, calculated:%s\n", _target_md5, _md5.toString().c_str()); DEBUG_UPDATER.printf("MD5 Failed: expected:%s, calculated:%s\n", _target_md5.c_str(), _md5.toString().c_str());
#endif #endif
return false; return false;
} }
#ifdef DEBUG_UPDATER #ifdef DEBUG_UPDATER
else DEBUG_UPDATER.printf("MD5 Success: %s\n", _md5.toString().c_str()); else DEBUG_UPDATER.printf("MD5 Success: %s\n", _target_md5.c_str());
#endif #endif
} }

View File

@ -15,6 +15,7 @@
#define U_FLASH 0 #define U_FLASH 0
#define U_SPIFFS 100 #define U_SPIFFS 100
#define U_AUTH 200
//#define DEBUG_UPDATER Serial //#define DEBUG_UPDATER Serial
@ -138,7 +139,7 @@ class UpdaterClass {
uint32_t _currentAddress; uint32_t _currentAddress;
uint32_t _command; uint32_t _command;
char *_target_md5; String _target_md5;
MD5Builder _md5; MD5Builder _md5;
}; };

View File

@ -6,154 +6,160 @@
//#define OTA_DEBUG 1 //#define OTA_DEBUG 1
#define U_AUTH 200
ArduinoOTAClass::ArduinoOTAClass() ArduinoOTAClass::ArduinoOTAClass()
: _port(0)
, _state(OTA_IDLE)
, _size(0)
, _cmd(0)
, _ota_port(0)
, _start_callback(NULL)
, _end_callback(NULL)
, _progress_callback(NULL)
, _error_callback(NULL)
{ {
_udp_ota = new WiFiUDP();
_password = 0;
_hostname = 0;
_port = 0;
_nonce = 0;
_state = OTA_IDLE;
_size = 0;
_cmd = 0;
_ota_port = 0;
_ota_ip = (uint32_t)0;
_md5 = new char[33];
_start_callback = NULL;
_end_callback = NULL;
_progress_callback = NULL;
_error_callback = NULL;
} }
void ArduinoOTAClass::onStart(OTA_CALLBACK(fn)){ void ArduinoOTAClass::onStart(OTA_CALLBACK(fn)) {
_start_callback = fn; _start_callback = fn;
} }
void ArduinoOTAClass::onEnd(OTA_CALLBACK(fn)){ void ArduinoOTAClass::onEnd(OTA_CALLBACK(fn)) {
_end_callback = fn; _end_callback = fn;
} }
void ArduinoOTAClass::onProgress(OTA_CALLBACK_PROGRESS(fn)){ void ArduinoOTAClass::onProgress(OTA_CALLBACK_PROGRESS(fn)) {
_progress_callback = fn; _progress_callback = fn;
} }
void ArduinoOTAClass::onError(OTA_CALLBACK_ERROR(fn)){ void ArduinoOTAClass::onError(OTA_CALLBACK_ERROR(fn)) {
_error_callback = fn; _error_callback = fn;
} }
ArduinoOTAClass::~ArduinoOTAClass(){ ArduinoOTAClass::~ArduinoOTAClass() {
delete _udp_ota;
} }
void ArduinoOTAClass::setPort(uint16_t port){ void ArduinoOTAClass::setPort(uint16_t port) {
if(!_initialized && !_port && port){ if (!_initialized && !_port && port) {
_port = port; _port = port;
} }
} }
void ArduinoOTAClass::setHostname(const char * hostname){ void ArduinoOTAClass::setHostname(const char * hostname) {
if(!_initialized && !_hostname && hostname){ if (!_initialized && !_hostname.length() && hostname) {
_hostname = new char[strlen(hostname)]; _hostname = hostname;
sprintf(_hostname, "%s", hostname);
} }
} }
void ArduinoOTAClass::setPassword(const char * password){ void ArduinoOTAClass::setPassword(const char * password) {
if(!_initialized && !_password && password){ if (!_initialized && !_password.length() && password) {
_password = new char[strlen(password)]; _password = password;
sprintf(_password, "%s", password);
} }
} }
void ArduinoOTAClass::begin() { void ArduinoOTAClass::begin() {
if(_initialized) if (_initialized)
return; return;
_initialized = true; _initialized = true;
if(!_hostname){
_hostname = new char[15];
sprintf(_hostname, "esp8266-%02x", ESP.getChipId());
}
if(!_port)
_port = 8266;
_udp_ota->begin(_port); if (!_hostname.length()) {
MDNS.begin(_hostname); char tmp[15];
if(_password){ sprintf(tmp, "esp8266-%02x", ESP.getChipId());
_nonce = new char[33]; _hostname = tmp;
}
if (!_port) {
_port = 8266;
}
_udp_ota.begin(_port);
MDNS.begin(_hostname.c_str());
if (_password.length()) {
MDNS.enableArduino(_port, true); MDNS.enableArduino(_port, true);
} else } else {
MDNS.enableArduino(_port); MDNS.enableArduino(_port);
}
_state = OTA_IDLE; _state = OTA_IDLE;
#if OTA_DEBUG #if OTA_DEBUG
Serial.printf("OTA server at: %s.local:%u\n", _hostname, _port); Serial.printf("OTA server at: %s.local:%u\n", _hostname.c_str(), _port);
#endif #endif
} }
void ArduinoOTAClass::_runUpdate(){ void ArduinoOTAClass::_runUpdate() {
if(!Update.begin(_size, _cmd)){ if (!Update.begin(_size, _cmd)) {
#if OTA_DEBUG #if OTA_DEBUG
Serial.println("Update Begin Error"); Serial.println("Update Begin Error");
#endif #endif
if (_error_callback) _error_callback(OTA_BEGIN_ERROR); if (_error_callback) {
_udp_ota->begin(_port); _error_callback(OTA_BEGIN_ERROR);
}
_udp_ota.begin(_port);
_state = OTA_IDLE; _state = OTA_IDLE;
return; return;
} }
Update.setMD5(_md5); Update.setMD5(_md5.c_str());
WiFiUDP::stopAll(); WiFiUDP::stopAll();
WiFiClient::stopAll(); WiFiClient::stopAll();
if (_start_callback) {
if (_start_callback) _start_callback(); _start_callback();
if (_progress_callback) _progress_callback(0, _size); }
if (_progress_callback) {
_progress_callback(0, _size);
}
WiFiClient client; WiFiClient client;
if (!client.connect(_ota_ip, _ota_port)) { if (!client.connect(_ota_ip, _ota_port)) {
#if OTA_DEBUG #if OTA_DEBUG
Serial.printf("Connect Failed\n"); Serial.printf("Connect Failed\n");
#endif #endif
_udp_ota->begin(_port); _udp_ota.begin(_port);
if (_error_callback) _error_callback(OTA_CONNECT_ERROR); if (_error_callback) {
_error_callback(OTA_CONNECT_ERROR);
}
_state = OTA_IDLE; _state = OTA_IDLE;
} }
uint32_t written, total = 0; uint32_t written, total = 0;
while(!Update.isFinished() && client.connected()){ while (!Update.isFinished() && client.connected()) {
int waited = 1000; int waited = 1000;
while(!client.available() && waited--) while (!client.available() && waited--)
delay(1); delay(1);
if(!waited){ if (!waited){
#if OTA_DEBUG #if OTA_DEBUG
Serial.printf("Recieve Failed\n"); Serial.printf("Recieve Failed\n");
#endif #endif
_udp_ota->begin(_port); _udp_ota.begin(_port);
if (_error_callback) _error_callback(OTA_RECIEVE_ERROR); if (_error_callback) {
_error_callback(OTA_RECIEVE_ERROR);
}
_state = OTA_IDLE; _state = OTA_IDLE;
} }
written = Update.write(client); written = Update.write(client);
if(written > 0){ if (written > 0) {
client.print(written, DEC); client.print(written, DEC);
total += written; total += written;
if(_progress_callback) _progress_callback(total, _size); if(_progress_callback) {
_progress_callback(total, _size);
}
} }
} }
if(Update.end()){ if (Update.end()) {
client.print("OK"); client.print("OK");
client.stop(); client.stop();
delay(10); delay(10);
#if OTA_DEBUG #if OTA_DEBUG
Serial.printf("Update Success\nRebooting...\n"); Serial.printf("Update Success\nRebooting...\n");
#endif #endif
if(_end_callback) _end_callback(); if (_end_callback) {
_end_callback();
}
ESP.restart(); ESP.restart();
} else { } else {
_udp_ota->begin(_port); _udp_ota.begin(_port);
if (_error_callback) _error_callback(OTA_END_ERROR); if (_error_callback) {
_error_callback(OTA_END_ERROR);
}
Update.printError(client); Update.printError(client);
#if OTA_DEBUG #if OTA_DEBUG
Update.printError(Serial); Update.printError(Serial);
@ -163,60 +169,61 @@ void ArduinoOTAClass::_runUpdate(){
} }
void ArduinoOTAClass::handle() { void ArduinoOTAClass::handle() {
if (!*_udp_ota) { if (!_udp_ota) {
_udp_ota->begin(_port); _udp_ota.begin(_port);
#if OTA_DEBUG #if OTA_DEBUG
Serial.println("OTA restarted"); Serial.println("OTA restarted");
#endif #endif
} }
if (!_udp_ota->parsePacket()) return; if (!_udp_ota.parsePacket()) return;
if(_state == OTA_IDLE){ if (_state == OTA_IDLE) {
int cmd = _udp_ota->parseInt(); int cmd = _udp_ota.parseInt();
if(cmd != U_FLASH && cmd != U_SPIFFS) if (cmd != U_FLASH && cmd != U_SPIFFS)
return; return;
_ota_ip = _udp_ota->remoteIP(); _ota_ip = _udp_ota.remoteIP();
_cmd = cmd; _cmd = cmd;
_ota_port = _udp_ota->parseInt(); _ota_port = _udp_ota.parseInt();
_size = _udp_ota->parseInt(); _size = _udp_ota.parseInt();
_udp_ota->read(); _udp_ota.read();
sprintf(_md5, "%s", _udp_ota->readStringUntil('\n').c_str()); _md5 = _udp_ota.readStringUntil('\n');
if(strlen(_md5) != 32) _md5.trim();
if(_md5.length() != 32)
return; return;
#if OTA_DEBUG #if OTA_DEBUG
Serial.print("Update Start: ip:"); Serial.print("Update Start: ip:");
Serial.print(_ota_ip); Serial.print(_ota_ip);
Serial.printf(", port:%d, size:%d, md5:%s\n", _ota_port, _size, _md5); Serial.printf(", port:%d, size:%d, md5:%s\n", _ota_port, _size, _md5.c_str());
#endif #endif
_udp_ota->beginPacket(_ota_ip, _udp_ota->remotePort()); _udp_ota.beginPacket(_ota_ip, _udp_ota.remotePort());
if(_password){ if (_password){
MD5Builder nonce_md5; MD5Builder nonce_md5;
nonce_md5.begin(); nonce_md5.begin();
nonce_md5.add(String(micros())); nonce_md5.add(String(micros()));
nonce_md5.calculate(); nonce_md5.calculate();
nonce_md5.getChars(_nonce); _nonce = nonce_md5.toString();
_udp_ota->printf("AUTH %s", _nonce); _udp_ota.printf("AUTH %s", _nonce.c_str());
_udp_ota->endPacket(); _udp_ota.endPacket();
_state = OTA_WAITAUTH; _state = OTA_WAITAUTH;
return; return;
} else { } else {
_udp_ota->print("OK"); _udp_ota.print("OK");
_udp_ota->endPacket(); _udp_ota.endPacket();
_state = OTA_RUNUPDATE; _state = OTA_RUNUPDATE;
} }
} else if(_state == OTA_WAITAUTH){ } else if (_state == OTA_WAITAUTH) {
int cmd = _udp_ota->parseInt(); int cmd = _udp_ota.parseInt();
if(cmd != U_AUTH){ if (cmd != U_AUTH) {
_state = OTA_IDLE; _state = OTA_IDLE;
return; return;
} }
_udp_ota->read(); _udp_ota.read();
String cnonce = _udp_ota->readStringUntil(' '); String cnonce = _udp_ota.readStringUntil(' ');
String response = _udp_ota->readStringUntil('\n'); String response = _udp_ota.readStringUntil('\n');
if(cnonce.length() != 32 || response.length() != 32){ if (cnonce.length() != 32 || response.length() != 32) {
_state = OTA_IDLE; _state = OTA_IDLE;
return; return;
} }
@ -235,21 +242,22 @@ void ArduinoOTAClass::handle() {
String result = _challengemd5.toString(); String result = _challengemd5.toString();
if(result.equals(response)){ if(result.equals(response)){
_udp_ota->beginPacket(_ota_ip, _udp_ota->remotePort()); _udp_ota.beginPacket(_ota_ip, _udp_ota.remotePort());
_udp_ota->print("OK"); _udp_ota.print("OK");
_udp_ota->endPacket(); _udp_ota.endPacket();
_state = OTA_RUNUPDATE; _state = OTA_RUNUPDATE;
} else { } else {
_udp_ota->beginPacket(_ota_ip, _udp_ota->remotePort()); _udp_ota.beginPacket(_ota_ip, _udp_ota.remotePort());
_udp_ota->print("Authentication Failed"); _udp_ota.print("Authentication Failed");
_udp_ota->endPacket(); _udp_ota.endPacket();
if (_error_callback) _error_callback(OTA_AUTH_ERROR); if (_error_callback) _error_callback(OTA_AUTH_ERROR);
_state = OTA_IDLE; _state = OTA_IDLE;
} }
} }
if(_state == OTA_RUNUPDATE) if (_state == OTA_RUNUPDATE) {
_runUpdate(); _runUpdate();
}
} }
ArduinoOTAClass ArduinoOTA; ArduinoOTAClass ArduinoOTA;

View File

@ -23,26 +23,6 @@ typedef enum {
class ArduinoOTAClass class ArduinoOTAClass
{ {
private:
int _port;
char *_password;
char * _hostname;
char * _nonce;
WiFiUDP* _udp_ota;
bool _initialized;
ota_state_t _state;
int _size, _cmd, _ota_port;
IPAddress _ota_ip;
char * _md5;
OTA_CALLBACK(_start_callback);
OTA_CALLBACK(_end_callback);
OTA_CALLBACK_ERROR(_error_callback);
OTA_CALLBACK_PROGRESS(_progress_callback);
void _runUpdate(void);
public: public:
ArduinoOTAClass(); ArduinoOTAClass();
~ArduinoOTAClass(); ~ArduinoOTAClass();
@ -55,6 +35,27 @@ class ArduinoOTAClass
void onError(OTA_CALLBACK_ERROR (fn)); void onError(OTA_CALLBACK_ERROR (fn));
void begin(); void begin();
void handle(); void handle();
private:
int _port;
String _password;
String _hostname;
String _nonce;
WiFiUDP _udp_ota;
bool _initialized;
ota_state_t _state;
int _size;
int _cmd;
int _ota_port;
IPAddress _ota_ip;
String _md5;
OTA_CALLBACK(_start_callback);
OTA_CALLBACK(_end_callback);
OTA_CALLBACK_ERROR(_error_callback);
OTA_CALLBACK_PROGRESS(_progress_callback);
void _runUpdate(void);
}; };
extern ArduinoOTAClass ArduinoOTA; extern ArduinoOTAClass ArduinoOTA;

View File

@ -3,37 +3,50 @@
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
const char* ssid = "..."; const char* ssid = "..........";
const char* password = "..."; const char* password = "..........";
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.println("Booting"); Serial.println("Booting");
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED){ while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting..."); Serial.println("Connection Failed! Rebooting...");
delay(5000); delay(5000);
ESP.reset(); ESP.restart();
} }
//ArduinoOTA.setPort(8266);//Defaults to 8266
//ArduinoOTA.setHostname((const char *)"myesp8266");//Defaults to esp8266-[ChipID] // Port defaults to 8266
//ArduinoOTA.setPassword((const char *)"123");//defaults to no authentication // ArduinoOTA.setPort(8266);
ArduinoOTA.onStart([]() { Serial.println("Start"); });
ArduinoOTA.onEnd([]() { Serial.println("End"); }); // Hostname defaults to esp8266-[ChipID]
// ArduinoOTA.setHostname("myesp8266");
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("End");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\n", (progress/(total/100))); Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
}); });
ArduinoOTA.onError([](ota_error_t error) { ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error); Serial.printf("Error[%u]: ", error);
if(error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if(error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if(error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if(error == OTA_RECIEVE_ERROR) Serial.println("Recieve Failed"); else if (error == OTA_RECIEVE_ERROR) Serial.println("Receive Failed");
else if(error == OTA_END_ERROR) Serial.println("End Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed");
}); });
ArduinoOTA.begin(); ArduinoOTA.begin();
Serial.println("Ready"); Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
} }
void loop() { void loop() {