Skip to content

Commit 5e8ad7d

Browse files
ESP:Use spiffs in ESP OTA Provider example (project-chip#12400)
* Use spiffs in ESP OTA Provider Example * Temporarily fix the compilation errors for ESP OTA Requestor app * Restyled by prettier-markdown * Modify the README.md files Co-authored-by: Restyled.io <commits@restyled.io>
1 parent f2f4b14 commit 5e8ad7d

File tree

9 files changed

+80
-69
lines changed

9 files changed

+80
-69
lines changed

examples/ota-provider-app/esp32/README.md

+14-23
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,31 @@ A prototype application that demonstrates OTA provider capabilities.
55
## Supported Devices
66

77
- This example supports ESP32 and ESP32C3. For details please check
8-
[here](https://github.com/shubhamdp/connectedhomeip/tree/shubhamdp-patch-1/examples/all-clusters-app/esp32#supported-devices).
8+
[here](https://github.com/project-chip/connectedhomeip/tree/master/examples/all-clusters-app/esp32#supported-devices).
99

10-
## Building the Example Application
11-
12-
- If you are building for the first time please check
13-
[Building the Example Application](https://github.com/shubhamdp/connectedhomeip/tree/shubhamdp-patch-1/examples/all-clusters-app/esp32#building-the-example-application)
14-
guide.
15-
- Otherwise, `idf.py build` works!
10+
## Copy the OTA image
1611

17-
## Flashing the Example Application
12+
- Copy the binary file which you are going to send to the OTA Requestor to
13+
`./spiffs_image` path and make sure the `OTA_IMAGE_NAME` is the same as the
14+
name of the image file you placed in `./spiffs_image`.
1815

1916
```
20-
idf.py -p <OTAProviderSerialPort> flash
17+
idf.py menuconfig
2118
```
2219

23-
## Flashing the hello-world.bin OTA image
24-
25-
Flash hello-world OTA image on OTA Provider's "ota_data" flash partition. Please
26-
find hello-world.bin
27-
[here](http://shubhamdp.github.io/esp_ota/esp32/hello-world-flash-in-ota-provider-partition.bin).
28-
This OTA image is built for ESP32, it will not work on other devices. This is
29-
the OTA upgrade image and will be sent to OTA requestor.
20+
- Edit the `OTA_IMAGE_NAME` through `Demo`->`OTA image file name`.
3021

31-
```
32-
esptool.py -p <OTAProviderSerialPort> write_flash 0x206400 hello-world-flash-in-ota-provider-partition.bin
33-
```
22+
## Building the Example Application
3423

35-
NOTE: This is a modified binary which contains the size of OTA image at first 4
36-
bytes.
24+
- If you are building for the first time please check
25+
[Building the Example Application](https://github.com/project-chip/connectedhomeip/tree/master/examples/all-clusters-app/esp32#building-the-example-application)
26+
guide.
27+
- Otherwise, `idf.py build` works!
3728

38-
Run the idf monitor
29+
## Flashing the Example Application
3930

4031
```
41-
idf.py -p <OTAProviderSerialPort> monitor
32+
idf.py -p <OTAProviderSerialPort> flash
4233
```
4334

4435
## Commissioning over BLE using chip-tool

examples/ota-provider-app/esp32/main/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ idf_component_register(PRIV_INCLUDE_DIRS
4545
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/ota-provider-app/ota-provider-common"
4646
EXCLUDE_SRCS
4747
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/ota-provider-app/ota-provider-common/BdxOtaSender.cpp"
48-
PRIV_REQUIRES chip QRCode bt console)
48+
PRIV_REQUIRES chip QRCode bt console spiffs)
4949

50+
spiffs_create_partition_image(img_storage ../spiffs_image FLASH_IN_PROJECT)
5051
set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 14)
5152
target_compile_options(${COMPONENT_LIB} PRIVATE "-DLWIP_IPV6_SCOPES=0" "-DCHIP_HAVE_CONFIG_H")

examples/ota-provider-app/esp32/main/Kconfig.projbuild

+6
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,10 @@ menu "Demo"
4747
default 4 if RENDEZVOUS_MODE_THREAD
4848
default 8 if RENDEZVOUS_MODE_ETHERNET
4949

50+
config OTA_IMAGE_NAME
51+
string "OTA image file name"
52+
default "hello_world.bin"
53+
help
54+
Name of OTA image file which will be added to '/spiffs_image/' and sent to the OTA Requestor.
55+
5056
endmenu

examples/ota-provider-app/esp32/main/main.cpp

+36-34
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "esp_log.h"
2222
#include "esp_netif.h"
2323
#include "esp_spi_flash.h"
24+
#include "esp_spiffs.h"
2425
#include "esp_system.h"
2526
#include "esp_wifi.h"
2627
#include "freertos/FreeRTOS.h"
@@ -60,7 +61,8 @@ void OnTransferComplete(void * context);
6061
void OnTransferFailed(void * context, BdxSenderErrorTypes status);
6162

6263
namespace {
63-
const char * TAG = "ota-provider-app";
64+
const char * TAG = "ota-provider-app";
65+
const uint8_t kMaxImagePathlen = 35;
6466
static DeviceCallbacks EchoCallbacks;
6567
BdxOtaSender bdxServer;
6668

@@ -69,10 +71,11 @@ constexpr chip::EndpointId kOtaProviderEndpoint = 0;
6971
constexpr uint32_t kMaxBdxBlockSize = 1024;
7072
constexpr chip::System::Clock::Timeout kBdxTimeout = chip::System::Clock::Seconds16(5 * 60); // Specification mandates >= 5 minutes
7173
constexpr chip::System::Clock::Timeout kBdxPollFreq = chip::System::Clock::Milliseconds32(500);
72-
const char * otaFilename = "hello-world.bin";
73-
const esp_partition_t * otaPartition = nullptr;
74+
const char * otaFilename = CONFIG_OTA_IMAGE_NAME;
75+
FILE * otaImageFile = NULL;
7476
uint32_t otaImageLen = 0;
7577
uint32_t otaTransferInProgress = false;
78+
static OTAProviderExample otaProvider;
7679

7780
chip::Callback::Callback<OnBdxBlockQuery> onBlockQueryCallback(OnBlockQuery, nullptr);
7881
chip::Callback::Callback<OnBdxTransferComplete> onTransferCompleteCallback(OnTransferComplete, nullptr);
@@ -83,22 +86,17 @@ CHIP_ERROR OnBlockQuery(void * context, chip::System::PacketBufferHandle & block
8386
{
8487
if (otaTransferInProgress == false)
8588
{
86-
if (otaPartition == nullptr || otaImageLen == 0)
89+
if (otaImageFile == NULL || otaImageLen == 0)
8790
{
88-
ESP_LOGE(TAG, "OTA partition not found");
91+
ESP_LOGE(TAG, "Failed to open the OTA image file");
8992
return CHIP_ERROR_OPEN_FAILED;
9093
}
9194
otaTransferInProgress = true;
9295
}
93-
9496
uint16_t blockBufAvailableLength = blockBuf->AvailableDataLength();
9597
uint16_t transferBlockSize = bdxServer.GetTransferBlockSize();
9698

9799
size = (blockBufAvailableLength < transferBlockSize) ? blockBufAvailableLength : transferBlockSize;
98-
99-
// There are two types of messages requestor can use to query a block: `BlockQuery` and `BlockQueryWithSkip` so,
100-
// to handle both case in a single callback offset is used which is managed by the `BdxOtaSender`.
101-
// So, offset + size will not overflow and even if it overflows the esp_partition_read API will return an error.
102100
if (offset + size >= otaImageLen)
103101
{
104102
size = otaImageLen - offset;
@@ -108,17 +106,15 @@ CHIP_ERROR OnBlockQuery(void * context, chip::System::PacketBufferHandle & block
108106
{
109107
isEof = false;
110108
}
111-
112-
esp_err_t err = esp_partition_read(otaPartition, offset + sizeof(otaImageLen), blockBuf->Start(), size);
113-
if (err != ESP_OK)
109+
size_t size_read = fread(blockBuf->Start(), 1, size, otaImageFile);
110+
if (size_read != size)
114111
{
115-
ESP_LOGI(TAG, "Failed to read %d bytes from offset %d", size, offset + sizeof(otaImageLen));
112+
ESP_LOGE(TAG, "Failed to read %d bytes from %s", size, otaFilename);
116113
size = 0;
117114
isEof = false;
118115
return CHIP_ERROR_READ_FAILED;
119116
}
120-
121-
ESP_LOGI(TAG, "Read %d bytes from offset %d", size, offset + sizeof(otaImageLen));
117+
ESP_LOGI(TAG, "Read %d bytes from %s", size, otaFilename);
122118
return CHIP_NO_ERROR;
123119
}
124120

@@ -157,8 +153,6 @@ extern "C" void app_main()
157153
return;
158154
}
159155

160-
OTAProviderExample otaProvider;
161-
162156
CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance();
163157

164158
CHIP_ERROR error = deviceMgr.Init(&EchoCallbacks);
@@ -188,26 +182,34 @@ extern "C" void app_main()
188182
callbacks.onTransferFailed = &onTransferFailedCallback;
189183
bdxServer.SetCallbacks(callbacks);
190184

191-
// If OTA image is available in flash storage then set to update available
192-
otaPartition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, "ota_data");
193-
if (otaPartition != nullptr)
194-
{
195-
ESP_LOGI(TAG, "Partition found %s address:0x%x size:0x%x", otaPartition->label, otaPartition->address, otaPartition->size);
185+
esp_vfs_spiffs_conf_t spiffs_conf = {
186+
.base_path = "/spiffs",
187+
.partition_label = NULL,
188+
.max_files = 3,
189+
.format_if_mount_failed = false,
190+
};
196191

197-
// TODO: Use the OTA image header specified in the specification
198-
// Right now we are using just image length instead of full header
199-
esp_partition_read(otaPartition, 0, &otaImageLen, sizeof(otaImageLen));
200-
if (otaImageLen > otaPartition->size)
201-
{
202-
otaImageLen = 0;
203-
}
204-
ESP_LOGI(TAG, "OTA image length %d bytes", otaImageLen);
192+
err = esp_vfs_spiffs_register(&spiffs_conf);
193+
if (err != ESP_OK)
194+
{
195+
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(err));
196+
return;
205197
}
206-
else
198+
size_t total = 0, used = 0;
199+
err = esp_spiffs_info(NULL, &total, &used);
200+
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
201+
char otaImagePath[kMaxImagePathlen];
202+
sprintf(otaImagePath, "/spiffs/%s", otaFilename);
203+
otaImageFile = fopen(otaImagePath, "r");
204+
if (otaImageFile == NULL)
207205
{
208-
ESP_LOGE(TAG, "OTA partition not found");
206+
ESP_LOGE(TAG, "Failed to open %s", otaFilename);
207+
return;
209208
}
210-
209+
fseek(otaImageFile, 0, SEEK_END);
210+
otaImageLen = ftell(otaImageFile);
211+
rewind(otaImageFile);
212+
ESP_LOGI(TAG, "The OTA image size: %d", otaImageLen);
211213
if (otaImageLen > 0)
212214
{
213215
otaProvider.SetQueryImageBehavior(OTAProviderExample::kRespondWithUpdateAvailable);
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Name, Type, SubType, Offset, Size, Flags
22
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
33
nvs, data, nvs, , 0x6000,
4-
otadata, data, ota, , 0x2000,
54
phy_init, data, phy, , 0x1000,
6-
# Factory partition size about 1.9MB
7-
factory, app, factory, , 1945K,
8-
# OTA partition of size 1M
9-
ota_data, data, nvs, , 1M,
10-
# ota_1, app, ota_1, , 512K,
5+
# Factory partition size about 1.5MB
6+
factory, app, factory, , 1500K,
7+
# spiffs partition to storage the image file size about 1.5MB
8+
img_storage, data, spiffs, , 1500K,
Binary file not shown.

examples/ota-requestor-app/esp32/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ Before moving ahead, make sure you have
1010
## Supported Devices
1111

1212
- This example supports ESP32 and ESP32C3. For details please check
13-
[here](https://github.com/shubhamdp/connectedhomeip/tree/shubhamdp-patch-1/examples/all-clusters-app/esp32#supported-devices).
13+
[here](https://github.com/project-chip/connectedhomeip/tree/master/examples/all-clusters-app/esp32#supported-devices).
1414

1515
## Building the Example Application
1616

1717
- If you are building for the first time please check
18-
[Building the Example Application](https://github.com/shubhamdp/connectedhomeip/tree/shubhamdp-patch-1/examples/all-clusters-app/esp32#building-the-example-application)
18+
[Building the Example Application](https://github.com/project-chip/connectedhomeip/tree/master/examples/all-clusters-app/esp32#building-the-example-application)
1919
guide.
2020
- Otherwise, `idf.py build` works!
2121

examples/ota-requestor-app/esp32/main/OTARequesterImpl.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232

3333
using chip::ByteSpan;
3434
using chip::CharSpan;
35-
using chip::DeviceProxy;
3635
using chip::EndpointId;
3736
using chip::FabricIndex;
3837
using chip::NodeId;
3938
using chip::OnDeviceConnected;
4039
using chip::OnDeviceConnectionFailure;
40+
using chip::OperationalDeviceProxy;
4141
using chip::Optional;
4242
using chip::PeerId;
4343
using chip::Server;
@@ -55,7 +55,7 @@ void OnQueryImageFailure(void * context, EmberAfStatus status);
5555
void OnApplyUpdateResponse(void * context, const ApplyUpdateResponse::DecodableType & response);
5656
void OnApplyUpdateRequestFailure(void * context, EmberAfStatus status);
5757

58-
void OnConnected(void * context, chip::DeviceProxy * deviceProxy);
58+
void OnConnected(void * context, chip::OperationalDeviceProxy * deviceProxy);
5959
void OnConnectionFailure(void * context, NodeId deviceId, CHIP_ERROR error);
6060

6161
void OnBlockReceived(void * context, const chip::bdx::TransferSession::BlockData & blockdata);
@@ -174,7 +174,7 @@ void OnApplyUpdateRequestFailure(void * context, EmberAfStatus status)
174174
ChipLogDetail(SoftwareUpdate, "ApplyUpdateRequest failure response %" PRIu8, status);
175175
}
176176

177-
void OnConnected(void * context, chip::DeviceProxy * deviceProxy)
177+
void OnConnected(void * context, chip::OperationalDeviceProxy * deviceProxy)
178178
{
179179
ChipLogDetail(SoftwareUpdate, "Callback OnConnected");
180180
uint8_t * command = reinterpret_cast<uint8_t *>(context);

examples/ota-requestor-app/esp32/main/main.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include "CHIPDeviceManager.h"
1919
#include "DeviceCallbacks.h"
20+
#include "app/util/af-enums.h"
21+
#include "app/util/af.h"
2022
#include "esp_heap_caps_init.h"
2123
#include "esp_log.h"
2224
#include "esp_netif.h"
@@ -180,3 +182,14 @@ extern "C" void app_main()
180182

181183
ESPInitConsole();
182184
}
185+
186+
// TODO: We should use the function definition in /src/app/clusters/ota-requestor/ClusterInterface.cpp
187+
// Temporarily add this function.
188+
189+
bool emberAfOtaSoftwareUpdateRequestorClusterAnnounceOtaProviderCallback(
190+
chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
191+
const chip::app::Clusters::OtaSoftwareUpdateRequestor::Commands::AnnounceOtaProvider::DecodableType & commandData)
192+
{
193+
emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS);
194+
return true;
195+
}

0 commit comments

Comments
 (0)