Skip to content

Commit 92069e6

Browse files
committed
Replace some manually managed buffers with Strings, fix code style
1 parent 450718b commit 92069e6

File tree

5 files changed

+188
-167
lines changed

5 files changed

+188
-167
lines changed

cores/esp8266/Updater.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ void UpdaterClass::_reset() {
3232
_currentAddress = 0;
3333
_size = 0;
3434
_command = U_FLASH;
35-
_target_md5 = 0;
3635
}
3736

3837
bool UpdaterClass::begin(size_t size, int command) {
@@ -97,15 +96,14 @@ bool UpdaterClass::begin(size_t size, int command) {
9796
_size = size;
9897
_buffer = new uint8_t[FLASH_SECTOR_SIZE];
9998
_command = command;
100-
101-
_target_md5 = new char[64];
99+
102100
_md5.begin();
103101
return true;
104102
}
105103

106104
void UpdaterClass::setMD5(const char * expected_md5){
107105
if(strlen(expected_md5) != 32) return;
108-
strcpy(_target_md5, expected_md5);
106+
_target_md5 = expected_md5;
109107
}
110108

111109
bool UpdaterClass::end(bool evenIfRemaining){
@@ -131,21 +129,21 @@ bool UpdaterClass::end(bool evenIfRemaining){
131129
}
132130
_size = progress();
133131
}
134-
132+
135133
_md5.calculate();
136-
if(_target_md5 && strlen(_target_md5) == 32){
137-
if(strcmp(_target_md5, _md5.toString().c_str()) != 0){
134+
if(_target_md5.length()) {
135+
if(_target_md5 != _md5.toString()){
138136
_error = UPDATE_ERROR_MD5;
139137
#ifdef DEBUG_UPDATER
140-
DEBUG_UPDATER.printf("MD5 Failed: expected:%s, calculated:%s\n", _target_md5, _md5.toString().c_str());
138+
DEBUG_UPDATER.printf("MD5 Failed: expected:%s, calculated:%s\n", _target_md5.c_str(), _md5.toString().c_str());
141139
#endif
142140
return false;
143141
}
144142
#ifdef DEBUG_UPDATER
145-
else DEBUG_UPDATER.printf("MD5 Success: %s\n", _md5.toString().c_str());
143+
else DEBUG_UPDATER.printf("MD5 Success: %s\n", _target_md5.c_str());
146144
#endif
147145
}
148-
146+
149147
if (_command == U_FLASH) {
150148
eboot_command ebcmd;
151149
ebcmd.action = ACTION_COPY_RAW;

cores/esp8266/Updater.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#define U_FLASH 0
1717
#define U_SPIFFS 100
18+
#define U_AUTH 200
1819

1920
//#define DEBUG_UPDATER Serial
2021

@@ -26,13 +27,13 @@ class UpdaterClass {
2627
Will return false if there is not enough space
2728
*/
2829
bool begin(size_t size, int = U_FLASH);
29-
30+
3031
/*
3132
Writes a buffer to the flash and increments the address
3233
Returns the amount written
3334
*/
3435
size_t write(uint8_t *data, size_t len);
35-
36+
3637
/*
3738
Writes the remaining bytes from the Stream to the flash
3839
Uses readBytes() and sets UPDATE_ERROR_STREAM on timeout
@@ -41,7 +42,7 @@ class UpdaterClass {
4142
Usable for slow streams like Serial
4243
*/
4344
size_t writeStream(Stream &data);
44-
45+
4546
/*
4647
If all bytes are written
4748
this call will write the config to eboot
@@ -53,27 +54,27 @@ class UpdaterClass {
5354
evenIfRemaining is helpfull when you update without knowing the final size first
5455
*/
5556
bool end(bool evenIfRemaining = false);
56-
57+
5758
/*
5859
Prints the last error to an output stream
5960
*/
6061
void printError(Stream &out);
61-
62+
6263
/*
6364
sets the expected MD5 for the firmware (hexString)
6465
*/
6566
void setMD5(const char * expected_md5);
66-
67+
6768
/*
6869
returns the MD5 String of the sucessfully ended firmware
6970
*/
7071
String md5String(void){ return _md5.toString(); }
71-
72+
7273
/*
7374
populated the result with the md5 bytes of the sucessfully ended firmware
7475
*/
7576
void md5(uint8_t * result){ return _md5.getBytes(result); }
76-
77+
7778
//Helpers
7879
uint8_t getError(){ return _error; }
7980
void clearError(){ _error = UPDATE_ERROR_OK; }
@@ -83,7 +84,7 @@ class UpdaterClass {
8384
size_t size(){ return _size; }
8485
size_t progress(){ return _currentAddress - _startAddress; }
8586
size_t remaining(){ return _size - (_currentAddress - _startAddress); }
86-
87+
8788
/*
8889
Template to write from objects that expose
8990
available() and read(uint8_t*, size_t) methods
@@ -125,7 +126,7 @@ class UpdaterClass {
125126
}
126127
return written;
127128
}
128-
129+
129130
private:
130131
void _reset();
131132
bool _writeBuffer();
@@ -137,8 +138,8 @@ class UpdaterClass {
137138
uint32_t _startAddress;
138139
uint32_t _currentAddress;
139140
uint32_t _command;
140-
141-
char *_target_md5;
141+
142+
String _target_md5;
142143
MD5Builder _md5;
143144
};
144145

0 commit comments

Comments
 (0)