8000 PoC for handling Erase WiFi Setting after OTA by mhightower83 · Pull Request #6965 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

PoC for handling Erase WiFi Setting after OTA #6965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 64 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
6fde10f
Proof of concept for post OTA 1st boot erase config.
mhightower83 Dec 28, 2019
93fb3d3
Added a 2nd method
mhightower83 Dec 30, 2019
c66ae2e
Merge branch 'master' into pr-erase-config
mhightower83 Dec 30, 2019
6d882e4
Added two_shots init to noextra4k
mhightower83 Dec 30, 2019
500b7cc
Removed cleaned up debug crud and added test example.
mhightower83 Dec 31, 2019
afd603b
Merge branch 'master' into pr-erase-config
mhightower83 Dec 31, 2019
bfb21b1
A proof of concept for handling Erase WiFi Setting after OTA
mhightower83 Dec 31, 2019
e51ab08
Addressed Tavis CI style issues.
mhightower83 Dec 31, 2019
a284b0a
Take two
mhightower83 Dec 31, 2019
3258d63
Merge branch 'master' into pr-erase-config
mhightower83 Jan 6, 2020
9d6e059
Merge branch 'master' into pr-erase-config
mhightower83 Jan 8, 2020
95eefed
Update example. Removed some debug crud. Added a Flash Info hotkey op…
mhightower83 Jan 8, 2020
815f1dc
Merge branch 'master' into pr-erase-config
mhightower83 Jan 9, 2020
660d19c
Merge branch 'master' into pr-erase-config
mhightower83 Jan 9, 2020
4554a7c
style fix
mhightower83 Jan 9, 2020
1ce4c60
Merge branch 'master' into pr-erase-config
mhightower83 Jan 16, 2020
7130877
Merge branch 'master' into pr-erase-config
mhightower83 Jan 23, 2020
73f16dc
Merge branch 'master' into pr-erase-config
mhightower83 Jan 28, 2020
9c4e44b
Added print messages to show progress.
mhightower83 Feb 6, 2020
546f0cb
Merge branch 'master' into pr-erase-config
mhightower83 Feb 6, 2020
560ddc5
Set default Method to 2, to side step travis issue of using preinit().
mhightower83 Feb 7, 2020
049d91b
Added a Third method.
mhightower83 Feb 18, 2020
4e2493a
Resolve merge conflict.
mhightower83 Feb 19, 2020
39160c8
one more :(
mhightower83 Feb 19, 2020
c0394fb
Merge branch 'master' into pr-erase-config
mhightower83 Feb 19, 2020
f1acb40
Access bin image header through ICACHE reduces IRAM usage by 16.
mhightower83 Feb 19, 2020
604b40a
Merge branch 'master' into pr-erase-config
mhightower83 Feb 21, 2020
4553009
Made function name longer to avoid possible name conflicts in the fut…
mhightower83 Feb 22, 2020
372998c
Merge branch 'master' into pr-erase-config
mhightower83 Mar 11, 2020
39b9508
CI issues
mhightower83 Mar 11, 2020
4954dcd
Merge branch 'master' into pr-erase-config
mhightower83 Mar 17, 2020
828a47d
Merge branch 'master' into pr-erase-config
mhightower83 Mar 27, 2020
b2d35ea
Merge branch 'master' into pr-erase-config
mh 8000 ightower83 Apr 9, 2020
74129c5
Merge branch 'master' into pr-erase-config
mhightower83 Apr 16, 2020
14fede0
Merge branch 'master' into pr-erase-config
mhightower83 Apr 28, 2020
be5fe6d
Merge branch 'master' into pr-erase-config
mhightower83 May 5, 2020
cfd54df
Merge branch 'master' into pr-erase-config
mhightower83 May 16, 2020
6f91be0
Merge branch 'master' into pr-erase-config
mhightower83 May 31, 2020
6b1cdb8
Merge branch 'master' into pr-erase-config
mhightower83 Jun 4, 2020
fcb8b21
Merge branch 'master' into pr-erase-config
mhightower83 Jun 19, 2020
c40a873
Merge branch 'master' into pr-erase-config
mhightower83 Jul 7, 2020
2b2def2
Corrected new errors after upgrade to GCC 10.1 toolchain.
mhightower83 Jul 10, 2020
48ed5e9
Merge branch 'master' into pr-erase-config
devyte Jul 22, 2020
b211727
Merge branch 'master' into pr-erase-config
mhightower83 Sep 2, 2020
ab9b6b2
Corrected manual merge error, with init order of class.
mhightower83 Sep 2, 2020
e5539fe
Merge branch 'master' into pr-erase-config
mhightower83 Sep 17, 2020
502d8bc
Merge branch 'master' into pr-erase-config
mhightower83 Oct 2, 2020
f50b3a6
Merge branch 'master' into pr-erase-config
mhightower83 Nov 11, 2020
94365e1
Fixed merge errors and code rot.
mhightower83 Nov 11, 2020
5234f3f
Updated wifi_health_t and WiFiDisconnectLog_t to take
mhightower83 Nov 11, 2020
025bb2f
Merge branch 'master' into pr-erase-config
mhightower83 Dec 8, 2020
edade1c
Merge branch 'master' into pr-erase-config
mhightower83 Dec 29, 2020
24a117c
Merge branch 'master' into pr-erase-config
d-a-v Feb 19, 2021
4e31c91
Merge branch 'master' into pr-erase-config
mhightower83 Mar 14, 2021
0b01518
Replaced ROM_uart_div_modify and associated logic, with the current
mhightower83 Mar 14, 2021
f9a3f95
Merge branch 'master' into pr-erase-config
devyte Apr 3, 2021
217b2d2
Cleanup merge conflict. Now use IRAM_ATTR and _NOINLINE_STATIC w/o ch…
mhightower83 Apr 3, 2021
0b7fced
Merge branch 'master' into pr-erase-config
mhightower83 Jul 28, 2021
9f0e220
Merge branch 'master' into pr-erase-config
mhightower83 Aug 8, 2021
e44e752
Merge branch 'master' into pr-erase-config
mhightower83 Aug 21, 2021
c44a3f9
Changed to comply with strict-aliasing rules. Changes based on discus…
mhightower83 Aug 22, 2021
63a2048
Merge branch 'pr-erase-config' of github.com:mhightower83/Arduino int…
mhightower83 Aug 22, 2021
21b0154
Merge branch 'master' into pr-erase-config
mhightower83 Dec 21, 2021
fb05f15
Merge branch 'master' into pr-erase-config
mhightower83 May 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Access bin image header through ICACHE reduces IRAM usage by 16.
Latest merge broke my TZ logic. Updated to the new configTime method.

Updated WifiHealth to handle multiple/duplicate disconnects w/o connects.
When a AP goes off line, a disconnect callback is done for each failed
connect attempt. Before the total connect time was getting inflated.

Now uses LED_BUILTIN for WiFi Up/Down state.

Updated WifiHealth.ino to report total time connected and disconnected.
"Connection Uptime" is the time since last connect.
"Total Uptime" is the sum of all past uptimes.
"Total Downtime" is the sum of all past downtimes.
"min/max/avg" reportings are of past uptimes or past downtimes.

Adjust comments for style?
  • Loading branch information
mhightower83 committed Feb 21, 2020
commit f1acb40ea46c544675952284c0f8eaf2bf7d32e9
41 changes: 4 additions & 37 deletions cores/esp8266/erase_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,37 +403,6 @@ void print_flashchip() {
#define PRINT_FLASHCHIP() do{}while(false)
#endif

#if 0
void set_flashchip_and_check_erase_config(uint8_t byte) {
/*
We patch and restore chip_size here. It should be noted that the ROM APIs
use both the chip_size and sector_size to validate calling parameters.
From what I see at least with esptool.py and core, there is a general
assumption that sector_size is always 4K.

I don't see a need to set and restore sector_size at this time.
*/
PRINT_FLASHCHIP();
uint32_t old_flash_size = flashchip->chip_size;
flashchip->chip_size = esp_c_magic_flash_chip_size((byte >> 4) & 0x0F);
PRINT_FLASHCHIP();
if (flashchip->chip_size) {
check_and_erase_config();
}
flashchip->chip_size = old_flash_size;
}

void ICACHE_RAM_ATTR erase_config_method3(void) {
/* Read configuration from flash before enabling Flash code execution. */
uint8_t bytes[4] __attribute__((aligned(4)));
if (SPI_FLASH_RESULT_OK == SPIRead(0x0000, &bytes[0], sizeof(bytes))) {
Cache_Read_Enable(0, 0, 0);
set_flashchip_and_check_erase_config(bytes[3]);
Cache_Read_Disable();
}
}

#else
void set_flashchip_and_check_erase_config(void) {
/*
We patch and restore chip_size here. It should be noted that the ROM APIs
Expand All @@ -446,11 +415,10 @@ void set_flashchip_and_check_erase_config(void) {
PRINT_FLASHCHIP();
/* Since Flash code has been mapped for execution, we can just address the
flash image header located at the beginning of flash as iCACHE like memory. */
uint32_t word32 = *((uint32_t *)0x40200000);
uint8_t *bytes = (uint8_t *)&word32;
ETS_PRINTF("\n*((uint32_t *)0x40200000) == 0x%08X\n", word32);
const uint32_t imghdr_4bytes = *((uint32_t *)0x40200000);
const image_header_t *partial_imghdr = (const image_header_t *)&imghdr_4bytes;
uint32_t old_flash_size = flashchip->chip_size;
flashchip->chip_size = esp_c_magic_flash_chip_size((bytes[3] >> 4) & 0x0F);
flashchip->chip_size = esp_c_magic_flash_chip_size((partial_imghdr->flash_size_freq >> 4) & 0x0F);
PRINT_FLASHCHIP();
if (flashchip->chip_size) {
check_and_erase_config();
Expand All @@ -459,11 +427,10 @@ void set_flashchip_and_check_erase_config(void) {
}

void ICACHE_RAM_ATTR erase_config_method3(void) {
Cache_Read_Enable(0, 0, 0);
Cache_Read_Enable(0, 0, 0); // 16K ICACHE
set_flashchip_and_check_erase_config();
Cache_Read_Disable();
}
#endif

#include "cont.h"

Expand Down
33 changes: 17 additions & 16 deletions libraries/ArduinoOTA/examples/OTAConfigErase/OTAConfigErase.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@
This config_erase example used BasicOTA as a start point.

Build customization for this PoC:

* To select one of three erase config methods:
Update `#define ERASE_CONFIG_METHOD` in `cores/esp8266/erase_config.cpp`
A description of the different method is also there.

* To turn on debug printing:
Uncomment `#define DEBUG_ERASE_CONFIG` in `cores/esp8266/erase_config.h`
This requires Serial speed 74880 bps.

* To build w/o erase config option:
Comment out `#include "erase_config.h"` in both `cores/esp8266/Update.cpp`
and `Arduino.h`
*/
*/
#include <time.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Ticker.h>
#include <TZ.h>

#include "WifiHealth.h"

#include <umm_malloc/umm_malloc_cfg.h>
#include "AddOns.h"

Expand All @@ -30,6 +33,10 @@
#define STAPSK "your-password"
#endif

#ifndef LOCALTZ
#define LOCALTZ TZ_America_Los_Angeles
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

Expand All @@ -53,17 +60,12 @@ ERASE_CONFIG_MASK_t eraseConfigOption = ERASE_CONFIG_RF_CAL; //ERASE_CONFIG_BLAN
WiFiEventHandler handler1;
WiFiEventHandler handler2;

extern char **environ;
void setTimeTZ(const char *tz) {
char **old_environ = environ;
char * tzEnv[2] = { (char *)tz, NULL };
environ = &tzEnv[0];
tzset();
environ = old_environ;
#define LED_BUILTIN_ON (0)
void wifiLedOn(void) {
digitalWrite(LED_BUILTIN, (LED_BUILTIN_ON) ? 1 : 0);
}

void setTimeTZ(String& tz) {
setTimeTZ(tz.c_str());
void wifiLedOff(void) {
digitalWrite(LED_BUILTIN, (LED_BUILTIN_ON) ? 0 : 1);
}

void setup() {
Expand All @@ -76,13 +78,12 @@ void setup() {
delay(20);
Serial.println();
Serial.println("setup ...");
pinMode(LED_BUILTIN, OUTPUT);
wifiLedOff();

WiFi.persistent(false); // w/o this a flash write occurs at every boot
WiFi.mode(WIFI_OFF);

String tz = F("TZ=PST+8PDT+7,M3.2.0M11.1.0");
setTimeTZ(tz);
configTime(0, 0, "pool.ntp.org");
configTime(LOCALTZ, "pool.ntp.org");

// Register wifi Event to control connection LED
handler1 = WiFi.onStationModeConnected([](WiFiEventStationModeConnected data) {
Expand Down
7 changes: 4 additions & 3 deletions libraries/ArduinoOTA/examples/OTAConfigErase/WifiHealth.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ bool printLocalTime(Print& oPrint, time_t timeIn = 0, const char *fmt = "%I:%M:%
bool printLocalTime(Print& oPrint, time_t timeIn, const char *fmt);
extern bool WifiUp;

//static unsigned long last_garp = 0;

typedef struct _WIFI_HEALTH {
int32_t connected_count;
int32_t rssi_sum;
uint32_t rssi_count;
time_t gtime_adjust;
time_t connected_time;
time_t disconnected_time;
time_t uptime_sum;
time_t uptime_max;
time_t uptime_min;
time_t disconnected_time;
time_t downtime_sum;
time_t downtime_max;
time_t downtime_min;
uint8 bssid[6];
uint8_t channel;
int8_t rssi_max;
Expand Down
94 changes: 66 additions & 28 deletions libraries/ArduinoOTA/examples/OTAConfigErase/WifiHealth.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

wifi_health_t wifiHealth = {0, 0, 0, 0, 0, 0, 0, 0, ~(time_t)0, "", 0, INT8_MIN, INT8_MAX, 0};
wifi_health_t wifiHealth = {
0, 0, 0, 0,
0, 0, 0, (time_t)LONG_MAX,
0, 0, 0, (time_t)LONG_MAX,
"", 0, INT8_MIN, INT8_MAX, 0};
WiFiDisconnectLog_t wifi_disconnect_log[MAX_CONNECTION_LOST_TIME_LOG] = {0, 0, 0, 0};
Ticker scheduleCheck;
bool WifiUp = false;
Expand Down Expand Up @@ -188,44 +192,46 @@ void updateWiFiStats(void) {
}

void onWiFiConnected(WiFiEventStationModeConnected data) {
(void) data;
wifiHealth.channel = data.channel;
memcpy(wifiHealth.bssid, data.bssid, sizeof(wifiHealth.bssid));
wifiHealth.connected_count++;
wifiHealth.connected_time = (time_t)(micros64() / 1000000);
time_t stime = (time_t)(micros64() / 1000000);
wifiHealth.connected_time = stime;
wifiHealth.rssi = WiFi.RSSI();
WifiUp = true;

// wifiLed.strobe(0);
// wifiLed.on();
// last_garp = millis(); // init for doGarp(NULL);
wifiLedOn();

if (wifiHealth.disconnected_time) {
time_t downtime = stime - wifiHealth.disconnected_time;
wifiHealth.downtime_sum += downtime;
wifiHealth.downtime_max = max(wifiHealth.downtime_max, downtime);
wifiHealth.downtime_min = min(wifiHealth.downtime_min, downtime);
wifiHealth.disconnected_time = (time_t)0;
}
}

void onWiFiDisconnected(WiFiEventStationModeDisconnected data) {
(void) data;
/* After a disconnect, an attempt is made about every 3 secs to reconnect.
On each failed attempt, this function is called. */
WifiUp = false;
// wifiLed.strobe(0);
// wifiLed.off();
time_t gtime;
time(&gtime);
time_t stime = (time_t)(micros64() / 1000000);
wifiHealth.disconnected_time = stime;
wifiHealth.gtime_adjust = gtime - stime;

if (wifiHealth.connected_time) {
wifiLedOff();
wifiHealth.disconnected_time = stime;
size_t last = wifiHealth.connected_count % MAX_CONNECTION_LOST_TIME_LOG;
wifi_disconnect_log[last].time = stime; // Note, 1st entry is at [1].
wifi_disconnect_log[last].reason = data.reason;
wifi_disconnect_log[last].channel = wifiHealth.channel;
wifi_disconnect_log[last].rssi = wifiHealth.rssi;
wifi_disconnect_log[last].rssi = wifiHealth.rssi;
time_t uptime = stime - wifiHealth.connected_time;
wifiHealth.uptime_sum += uptime;
wifiHealth.uptime_max = max(wifiHealth.uptime_max, uptime);
if (~(time_t)0 != wifiHealth.uptime_min) {
wifiHealth.uptime_min = min(wifiHealth.uptime_min, uptime);
} else {
wifiHealth.uptime_min = uptime;
}
wifiHealth.uptime_min = min(wifiHealth.uptime_min, uptime);
wifiHealth.connected_time = (time_t)0;
}
}

Expand Down Expand Up @@ -256,21 +262,53 @@ bool printWiFiStats(Print& oStream) {
printUpTime(oStream, uptime);
oStream.println();

if (wifiHealth.uptime_max) {
oStream.print(String_F(" MAX: "));
printUpTime(oStream, wifiHealth.uptime_max);
uptime = wifiHealth.uptime_sum;
if (uptime) {
oStream.print(String_F(" Total Uptime: "));
printUpTime(oStream, uptime);
oStream.println();

if (2 < wifiHealth.connected_count) {
if (wifiHealth.uptime_max) {
oStream.print(String_F(" MAX: "));
printUpTime(oStream, wifiHealth.uptime_max);
oStream.println();
}
if ((time_t)LONG_MAX != wifiHealth.uptime_min) {
oStream.print(String_F(" MIN: "));
printUpTime(oStream, wifiHealth.uptime_min);
oStream.println();
}
oStream.print(String_F(" AVG: "));
uptime /= (decltype(uptime))(wifiHealth.connected_count - 1);
printUpTime(oStream, uptime);
oStream.println();
}
}
if (~(time_t)0 != wifiHealth.uptime_min) {
oStream.print(String_F(" MIN: "));
printUpTime(oStream, wifiHealth.uptime_min);

time_t downtime = wifiHealth.downtime_sum;
if (downtime) {
oStream.print(String_F(" Total Downtime: "));
printUpTime(oStream, downtime);
oStream.println();

if (2 < wifiHealth.connected_count) {
if (wifiHealth.downtime_max) {
oStream.print(String_F(" MAX: "));
printUpTime(oStream, wifiHealth.downtime_max);
oStream.println();
}
if ((time_t)LONG_MAX != wifiHealth.downtime_min) {
oStream.print(String_F(" MIN: "));
printUpTime(oStream, wifiHealth.downtime_min);
oStream.println();
}
oStream.print(String_F(" AVG: "));
downtime /= (decltype(uptime))(wifiHealth.connected_count - 1);
printUpTime(oStream, downtime);
oStream.println();
}
}
oStream.print(String_F(" AVG: "));
uptime += wifiHealth.uptime_sum;
uptime /= (decltype(uptime))wifiHealth.connected_count;
printUpTime(oStream, uptime);
oStream.println();
oStream.println(String_F(" Reconnects: ") + (wifiHealth.connected_count - 1));

if (wifiHealth.connected_count > 1) {
Expand Down
0