Skip to content

Commit 1dda263

Browse files
donghengdongheng
authored andcommitted
feat(ota): add option into example to load original AP information when update from old SDK
1 parent 7351110 commit 1dda263

File tree

4 files changed

+94
-12
lines changed

4 files changed

+94
-12
lines changed

examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/main/Kconfig.projbuild

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
menu "Example Configuration"
22

3+
config CONNECT_ORIGINAL_AP
4+
bool "Connect to the original AP"
5+
default n
6+
help
7+
Connect to the original AP of old SDK
8+
39
config WIFI_SSID
410
string "WiFi SSID"
511
default "myssid"
12+
depends on !CONNECT_ORIGINAL_AP
613
help
714
SSID (network name) for the example to connect to.
815

916
config WIFI_PASSWORD
1017
string "WiFi Password"
1118
default "myssid"
19+
depends on !CONNECT_ORIGINAL_AP
1220
help
1321
WiFi password (WPA or WPA2) for the example to use.
1422

examples/system/ota/native_ota/1MB_flash/new_to_new_with_old/main/ota_example_main.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "freertos/task.h"
1616
#include "freertos/event_groups.h"
1717

18+
#ifdef CONFIG_CONNECT_ORIGINAL_AP
19+
#include "internal/esp_system_internal.h"
20+
#endif
1821
#include "esp_system.h"
1922
#include "esp_wifi.h"
2023
#include "esp_event_loop.h"
@@ -105,18 +108,48 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
105108

106109
static void initialise_wifi(void)
107110
{
108-
tcpip_adapter_init();
109-
wifi_event_group = xEventGroupCreate();
110-
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
111-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
112-
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
113-
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
111+
#ifdef CONFIG_CONNECT_ORIGINAL_AP
112+
int ret;
113+
uint32_t addr;
114+
struct old_sysconf *sysconf;
115+
wifi_config_t wifi_config = { 0 };
116+
117+
if (!(addr = esp_get_old_sysconf_addr())) {
118+
ESP_LOGE(TAG, "Current firmware does not update from old SDK firmware");
119+
return ;
120+
}
121+
122+
if (!(sysconf = malloc(sizeof(struct old_sysconf)))) {
123+
ESP_LOGE(TAG, "No enough memory for old system config");
124+
return ;
125+
}
126+
127+
if ((ret = spi_flash_read(addr, sysconf, sizeof(struct old_sysconf))) != ESP_OK) {
128+
free(sysconf);
129+
ESP_LOGE(TAG, "read old system config from flash error %d", ret);
130+
return ;
131+
}
132+
133+
memcpy(&wifi_config.sta.ssid, sysconf->ap_ssid[sysconf->ap_index].ssid, sysconf->ap_ssid[sysconf->ap_index].len);
134+
memcpy(&wifi_config.sta.password, sysconf->ap_ssid[sysconf->ap_index].passwd, 32);
135+
136+
free(sysconf);
137+
#else
114138
wifi_config_t wifi_config = {
115139
.sta = {
116140
.ssid = EXAMPLE_WIFI_SSID,
117141
.password = EXAMPLE_WIFI_PASS,
118142
},
119143
};
144+
#endif
145+
146+
tcpip_adapter_init();
147+
wifi_event_group = xEventGroupCreate();
148+
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
149+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
150+
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
151+
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
152+
120153
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
121154
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
122155
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );

examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/main/Kconfig.projbuild

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
menu "Example Configuration"
22

3+
config CONNECT_ORIGINAL_AP
4+
bool "Connect to the original AP"
5+
default n
6+
help
7+
Connect to the original AP of old SDK
8+
39
config WIFI_SSID
410
string "WiFi SSID"
511
default "myssid"
12+
depends on !CONNECT_ORIGINAL_AP
613
help
714
SSID (network name) for the example to connect to.
815

916
config WIFI_PASSWORD
1017
string "WiFi Password"
1118
default "myssid"
19+
depends on !CONNECT_ORIGINAL_AP
1220
help
1321
WiFi password (WPA or WPA2) for the example to use.
1422

examples/system/ota/native_ota/2+MB_flash/new_to_new_with_old/main/ota_example_main.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "freertos/task.h"
1616
#include "freertos/event_groups.h"
1717

18+
#ifdef CONFIG_CONNECT_ORIGINAL_AP
19+
#include "internal/esp_system_internal.h"
20+
#endif
1821
#include "esp_system.h"
1922
#include "esp_wifi.h"
2023
#include "esp_event_loop.h"
@@ -105,18 +108,48 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
105108

106109
static void initialise_wifi(void)
107110
{
108-
tcpip_adapter_init();
109-
wifi_event_group = xEventGroupCreate();
110-
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
111-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
112-
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
113-
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
111+
#ifdef CONFIG_CONNECT_ORIGINAL_AP
112+
int ret;
113+
uint32_t addr;
114+
struct old_sysconf *sysconf;
115+
wifi_config_t wifi_config = { 0 };
116+
117+
if (!(addr = esp_get_old_sysconf_addr())) {
118+
ESP_LOGE(TAG, "Current firmware does not update from old SDK firmware");
119+
return ;
120+
}
121+
122+
if (!(sysconf = malloc(sizeof(struct old_sysconf)))) {
123+
ESP_LOGE(TAG, "No enough memory for old system config");
124+
return ;
125+
}
126+
127+
if ((ret = spi_flash_read(addr, sysconf, sizeof(struct old_sysconf))) != ESP_OK) {
128+
free(sysconf);
129+
ESP_LOGE(TAG, "read old system config from flash error %d", ret);
130+
return ;
131+
}
132+
133+
memcpy(&wifi_config.sta.ssid, sysconf->ap_ssid[sysconf->ap_index].ssid, sysconf->ap_ssid[sysconf->ap_index].len);
134+
memcpy(&wifi_config.sta.password, sysconf->ap_ssid[sysconf->ap_index].passwd, 32);
135+
136+
free(sysconf);
137+
#else
114138
wifi_config_t wifi_config = {
115139
.sta = {
116140
.ssid = EXAMPLE_WIFI_SSID,
117141
.password = EXAMPLE_WIFI_PASS,
118142
},
119143
};
144+
#endif
145+
146+
tcpip_adapter_init();
147+
wifi_event_group = xEventGroupCreate();
148+
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
149+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
150+
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
151+
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
152+
120153
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
121154
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
122155
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );

0 commit comments

Comments
 (0)