Skip to content

Commit 8e50cdb

Browse files
committed
Updater.cpp:
- use new AutoInterruptLock - add delay to give the RTOS some time to handle TCP WiFiClient.cpp - add stopAllexcepted to cancel all TCP excepted one ClientContext.h - add getLocalPort() ESP8266HTTPUpdate.cpp - close all not needed TCP and UDP osapi.h - missing commit from SDK
1 parent 4ad8946 commit 8e50cdb

File tree

9 files changed

+78
-24
lines changed

9 files changed

+78
-24
lines changed

cores/esp8266/Updater.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Updater.h"
22
#include "Arduino.h"
33
#include "eboot_command.h"
4+
#include "interrupts.h"
45

56
//#define DEBUG_UPDATER Serial
67

@@ -111,28 +112,36 @@ bool UpdaterClass::end(bool evenIfRemaining){
111112
return true;
112113
}
113114

114-
bool UpdaterClass::_writeBuffer(){
115-
noInterrupts();
116-
int rc = SPIEraseSector(_currentAddress/FLASH_SECTOR_SIZE);
117-
interrupts();
118-
yield();
119-
if(!rc){
120-
noInterrupts();
121-
rc = SPIWrite(_currentAddress, _buffer, _bufferLen);
122-
interrupts();
123-
}
124-
interrupts();
125-
if (rc) {
126-
_error = UPDATE_ERROR_WRITE;
127-
_currentAddress = (_startAddress + _size);
115+
bool UpdaterClass::_writeBuffer() {
116+
int rc = 0;
117+
delay(2); // give the rtos some time to handle TCP
118+
{
119+
AutoInterruptLock(15);
120+
rc = SPIEraseSector(_currentAddress / FLASH_SECTOR_SIZE);
121+
}
122+
123+
delay(2); // give the rtos some time to handle TCP
124+
125+
if(!rc) {
126+
{
127+
AutoInterruptLock(15);
128+
rc = SPIWrite(_currentAddress, _buffer, _bufferLen);
129+
}
130+
}
131+
132+
delay(2); // give the rtos some time to handle TCP
133+
134+
if(rc) {
135+
_error = UPDATE_ERROR_WRITE;
136+
_currentAddress = (_startAddress + _size);
128137
#ifdef DEBUG_UPDATER
129-
printError(DEBUG_UPDATER);
138+
printError(DEBUG_UPDATER);
130139
#endif
131-
return false;
132-
}
133-
_currentAddress += _bufferLen;
134-
_bufferLen = 0;
135-
return true;
140+
return false;
141+
}
142+
_currentAddress += _bufferLen;
143+
_bufferLen = 0;
144+
return true;
136145
}
137146

138147
size_t UpdaterClass::write(uint8_t *data, size_t len) {

cores/esp8266/Updater.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#define UPDATE_ERROR_SIZE 4
1212
#define UPDATE_ERROR_STREAM 5
1313

14+
#define DEBUG_UPDATER Serial1
15+
1416
class UpdaterClass {
1517
public:
1618
UpdaterClass();

cores/esp8266/debug.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
#define ARD_DEBUG_H
33

44
#include <stddef.h>
5-
// #define DEBUGV(...) ets_printf(__VA_ARGS__)
5+
//#define DEBUGV(...) ets_printf(__VA_ARGS__)
6+
7+
#ifndef DEBUGV
68
#define DEBUGV(...)
9+
#endif
710

811
#ifdef __cplusplus
912
void hexdump(uint8_t *mem, uint32_t len, uint8_t cols = 16);

libraries/ESP8266WiFi/src/WiFiClient.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,27 @@ void WiFiClient::stopAll()
284284
}
285285
}
286286
}
287+
288+
289+
void WiFiClient::stopAllexcepted(WiFiClient * exC) {
290+
for (WiFiClient* it = _s_first; it; it = it->_next) {
291+
ClientContext* c = it->_client;
292+
293+
if(c && exC->_client) {
294+
if(exC->_client->getRemoteAddress() == c->getRemoteAddress()) {
295+
if(exC->_client->getRemotePort() == c->getRemotePort()) {
296+
if(exC->_client->getLocalPort() == c->getLocalPort()) {
297+
// ignore this
298+
c = NULL;
299+
}
300+
}
301+
}
302+
}
303+
304+
if (c) {
305+
c->abort();
306+
c->unref();
307+
it->_client = 0;
308+
}
309+
}
310+
}

libraries/ESP8266WiFi/src/WiFiClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class WiFiClient : public Client, public SList<WiFiClient> {
9191
using Print::write;
9292

9393
static void stopAll();
94+
static void stopAllexcepted(WiFiClient * c);
9495

9596
private:
9697

libraries/ESP8266WiFi/src/include/ClientContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ class ClientContext {
125125
return _pcb->remote_port;
126126
}
127127

128+
uint16_t getLocalPort() {
129+
if(!_pcb) return 0;
130+
131+
return _pcb->local_port;
132+
}
133+
128134
size_t getSize() const {
129135
if(!_rx_buf) return 0;
130136

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,17 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(const char * host, uint16_t port,
128128
ret = HTTP_UPDATE_FAILD;
129129
DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
130130
} else {
131-
if(ESP.updateSketch(tcp, len)) {
132-
// may never reached!
131+
132+
WiFiUDP::stopAll();
133+
WiFiClient::stopAllexcepted(&tcp);
134+
135+
delay(100);
136+
137+
if(ESP.updateSketch(tcp, len, false, false)) {
133138
ret = HTTP_UPDATE_OK;
134139
DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n");
140+
tcp.stop();
141+
ESP.restart();
135142
} else {
136143
ret = HTTP_UPDATE_FAILD;
137144
DEBUG_HTTP_UPDATE("[httpUpdate] Update failed\n");

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include <Arduino.h>
3030
#include <ESP8266WiFi.h>
31+
#include <WiFiUdp.h>
32+
#include <WiFiClient.h>
3133

3234
//#define DEBUG_HTTP_UPDATE(...) Serial1.printf( __VA_ARGS__ )
3335

tools/sdk/include/osapi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
#ifdef USE_OPTIMIZE_PRINTF
4848
#define os_printf(fmt, ...) do { \
49-
static const char flash_str[] ICACHE_RODATA_ATTR = fmt; \
49+
static const char flash_str[] ICACHE_RODATA_ATTR __attribute__((aligned(4))) = fmt; \
5050
os_printf_plus(flash_str, ##__VA_ARGS__); \
5151
} while(0)
5252
#else

0 commit comments

Comments
 (0)