Skip to content

Commit

Permalink
ESP32: Add OTARequstor to all-clusters-app (#12360)
Browse files Browse the repository at this point in the history
* Add OTARequstor to all-clusters-app on ESP32 platform

* Change the expected clientList value in Test Suites

* If the uart driver is installed, skip intalling the uart driver
  • Loading branch information
wqx6 authored and pull[bot] committed Dec 8, 2021
1 parent 03e4f73 commit 4007146
Show file tree
Hide file tree
Showing 25 changed files with 989 additions and 67 deletions.
14 changes: 5 additions & 9 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ if(NOT CHIP_ROOT)
get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../.. REALPATH)
endif()

set(CHIP_REQURIE_COMPONENTS freertos lwip bt mdns mbedtls fatfs)

if (CONFIG_ENABLE_CHIP_SHELL)
list(APPEND CHIP_REQURIE_COMPONENTS console)
endif()

if (CONFIG_OPENTHREAD_ENABLED)
list(APPEND CHIP_REQURIE_COMPONENTS openthread)
endif()
set(CHIP_REQURIE_COMPONENTS freertos lwip bt mdns mbedtls fatfs app_update console openthread)

if (NOT CMAKE_BUILD_EARLY_EXPANSION)
if (CONFIG_COMPILER_OPTIMIZATION_DEFAULT OR CONFIG_COMPILER_OPTIMIZATION_NONE)
Expand Down Expand Up @@ -107,6 +99,10 @@ if (CONFIG_OPENTHREAD_ENABLED)
chip_gn_arg_append("chip_enable_openthread" "true")
endif()

if (CONFIG_ENABLE_OTA_REQUESTOR)
chip_gn_arg_append("chip_enable_ota_requestor" "true")
endif()

set(args_gn_input "${CMAKE_CURRENT_BINARY_DIR}/args.gn.in")
file(GENERATE OUTPUT "${args_gn_input}" CONTENT "${chip_gn_args}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1382,30 +1382,30 @@
"mfgCode": null,
"define": "OTA_PROVIDER_CLUSTER",
"side": "client",
"enabled": 0,
"enabled": 1,
"commands": [
{
"name": "QueryImage",
"code": 0,
"mfgCode": null,
"source": "client",
"incoming": 1,
"incoming": 0,
"outgoing": 1
},
{
"name": "ApplyUpdateRequest",
"code": 1,
"mfgCode": null,
"source": "client",
"incoming": 1,
"incoming": 0,
"outgoing": 1
},
{
"name": "NotifyUpdateApplied",
"code": 2,
"mfgCode": null,
"source": "client",
"incoming": 1,
"incoming": 0,
"outgoing": 1
}
],
Expand All @@ -1415,12 +1415,12 @@
"code": 65533,
"mfgCode": null,
"side": "client",
"included": 1,
"included": 0,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
"defaultValue": "0x0001",
"reportable": 1,
"reportable": 0,
"minInterval": 0,
"maxInterval": 65344,
"reportableChange": 0
Expand Down
9 changes: 9 additions & 0 deletions examples/all-clusters-app/esp32/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,12 @@ depends on ENABLE_PW_RPC
about available pin numbers for UART.

endmenu

menu "OTA Options"

config ENABLE_OTA_REQUESTOR
bool "Enable OTA Requestor"
default y
help
Enable this option to enable the OTA Requestor
endmenu
4 changes: 3 additions & 1 deletion examples/all-clusters-app/esp32/partitions.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, , 0x6000,
otadata, data, ota, , 0x2000,
phy_init, data, phy, , 0x1000,
factory, app, factory, , 1900K,
ota_0, app, ota_0, , 1500K,
ota_1, app, ota_1, , 1500K,
ot_storage, data, 0x3a, , 0x2000,
6 changes: 5 additions & 1 deletion examples/all-clusters-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ CONFIG_LWIP_IPV6_AUTOCONFIG=y

# Use a custom partition table
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"

# Vendor and product id
CONFIG_DEVICE_VENDOR_ID=0x235A
Expand All @@ -49,3 +49,7 @@ CONFIG_ENABLE_CHIP_SHELL=y
#enable lwIP route hooks
CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=y
CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y

# Serial Flasher config
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
6 changes: 5 additions & 1 deletion examples/all-clusters-app/esp32/sdkconfig_c3devkit.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ CONFIG_LWIP_IPV6_AUTOCONFIG=y

# Use a custom partition table
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"

#enable lwIP route hooks
CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=y
CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y

# Serial Flasher config
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
7 changes: 6 additions & 1 deletion examples/all-clusters-app/esp32/sdkconfig_m5stack.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ CONFIG_LWIP_IPV6_AUTOCONFIG=y

# Use a custom partition table
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"

# Vendor and product id
CONFIG_DEVICE_VENDOR_ID=0x235A
Expand All @@ -54,3 +54,8 @@ CONFIG_ENABLE_CHIP_SHELL=y
#enable lwIP route hooks
CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=y
CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y

# Serial Flasher config
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ CONFIG_LWIP_IPV6_AUTOCONFIG=y

# Use a custom partition table
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"

# Vendor and product id
CONFIG_DEVICE_VENDOR_ID=0x235A
Expand All @@ -56,4 +56,8 @@ CONFIG_EXAMPLE_UART_TXD=1
CONFIG_ENABLE_PW_RPC=y

# Disable shell
CONFIG_ENABLE_CHIP_SHELL=n
CONFIG_ENABLE_CHIP_SHELL=n

# Serial Flasher config
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
4 changes: 3 additions & 1 deletion src/app/tests/suites/TestDescriptorCluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ tests:
command: "readAttribute"
attribute: "Client List"
response:
value: []
value: [
0x0029, # OTA Software Update Provider
]

- label: "Read attribute Parts list"
command: "readAttribute"
Expand Down
3 changes: 2 additions & 1 deletion src/darwin/Framework/CHIPTests/CHIPClustersTests.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/lib/shell/commands/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ source_set("commands") {
if (chip_device_platform == "nrfconnect") {
sources += [ "DFUManager_nrfconnect.h" ]
}
if (chip_device_platform == "esp32") {
sources += [
"DFUManager_esp32.h",
"OTAUpdater_esp32.h",
]
}
}

if (chip_device_platform != "none") {
Expand Down
147 changes: 147 additions & 0 deletions src/lib/shell/commands/DFUManager_esp32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "OTAUpdater_esp32.h"
#include "platform/ESP32/ESP32Utils.h"
#include <lib/support/TypeTraits.h>
#include <lib/support/logging/CHIPLogging.h>
#include <messaging/ExchangeContext.h>
#include <messaging/ExchangeDelegate.h>
#include <protocols/bdx/BdxMessages.h>
#include <protocols/bdx/BdxTransferSession.h>
#include <protocols/bdx/TransferFacilitator.h>

namespace chip {
namespace Shell {

class DFUManager : public bdx::Initiator
{
public:
void SetInitialExchange(Messaging::ExchangeContext * ec) { mExchangeCtx = ec; }
CHIP_ERROR ApplyUpdate();
CHIP_ERROR DiscardUpdate();

private:
void HandleTransferSessionOutput(bdx::TransferSession::OutputEvent & event) override;

uint8_t mDfuBuffer[1024];
bool mIsTransferComplete = false;
};

inline CHIP_ERROR DFUManager::ApplyUpdate()
{
return DeviceLayer::Internal::ESP32Utils::MapError(OTAUpdater::GetInstance().Apply());
}

inline CHIP_ERROR DFUManager::DiscardUpdate()
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline void DFUManager::HandleTransferSessionOutput(bdx::TransferSession::OutputEvent & event)
{
using OutputEventType = bdx::TransferSession::OutputEventType;
using MessageType = bdx::MessageType;
using SendMessageFlags = Messaging::SendMessageFlags;

CHIP_ERROR err = CHIP_NO_ERROR;

if (event.EventType != OutputEventType::kNone)
{
ChipLogDetail(BDX, "OutputEvent type: %s", event.ToString(event.EventType));
}

switch (event.EventType)
{
case OutputEventType::kNone:
if (mIsTransferComplete)
{
ChipLogProgress(BDX, "Transfer complete!");
OTAUpdater::GetInstance().End();
mTransfer.Reset();
mIsTransferComplete = false;
}
break;
case OutputEventType::kMsgToSend: {
Messaging::SendFlags sendFlags;
VerifyOrReturn(mExchangeCtx != nullptr, ChipLogError(BDX, "mExchangeContext is null, cannot proceed"));
sendFlags.Set(SendMessageFlags::kFromInitiator, event.msgTypeData.MessageType == to_underlying(MessageType::ReceiveInit));
sendFlags.Set(SendMessageFlags::kExpectResponse, event.msgTypeData.MessageType != to_underlying(MessageType::BlockAckEOF));
err = mExchangeCtx->SendMessage(event.msgTypeData.ProtocolId, event.msgTypeData.MessageType, std::move(event.MsgData),
sendFlags);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(BDX, "SendMessage() failed: %" CHIP_ERROR_FORMAT, err.Format()));
break;
}
case OutputEventType::kAcceptReceived: {
VerifyOrReturn(CHIP_NO_ERROR == mTransfer.PrepareBlockQuery(), ChipLogError(BDX, "PrepareBlockQuery failed"));
break;
}
case OutputEventType::kBlockReceived: {
ChipLogDetail(BDX, "Got block length %zu", event.blockdata.Length);
if (OTAUpdater::GetInstance().IsInProgress() == false)
{
OTAUpdater::GetInstance().Begin();
}
// TODO: Process/skip the Matter OTA header
OTAUpdater::GetInstance().Write(reinterpret_cast<const void *>(event.blockdata.Data), event.blockdata.Length);
if (event.blockdata.IsEof)
{
err = mTransfer.PrepareBlockAck();
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(BDX, "PrepareBlockAck failed: %" CHIP_ERROR_FORMAT, err.Format()));
mIsTransferComplete = true;
}
else
{
err = mTransfer.PrepareBlockQuery();
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(BDX, "PrepareBlockQuery failed: %" CHIP_ERROR_FORMAT, err.Format()));
}
break;
}
case OutputEventType::kStatusReceived: {
ChipLogError(BDX, "Got StatusReport %x", static_cast<uint16_t>(event.statusData.statusCode));
OTAUpdater::GetInstance().Abort();
mTransfer.Reset();
mExchangeCtx->Close();
break;
}
case OutputEventType::kInternalError: {
ChipLogError(BDX, "Transfer stopped due to internal error");
OTAUpdater::GetInstance().Abort();
mTransfer.Reset();
mExchangeCtx->Close();
break;
}
case OutputEventType::kTransferTimeout: {
ChipLogError(BDX, "Transfer timed out");
OTAUpdater::GetInstance().Abort();
mTransfer.Reset();
mExchangeCtx->Close();
break;
}
case OutputEventType::kInitReceived:
case OutputEventType::kAckReceived:
case OutputEventType::kQueryReceived:
case OutputEventType::kAckEOFReceived:
default:
ChipLogError(BDX, "Unexpected BDX event type: %" PRIu16, static_cast<uint16_t>(event.EventType));
}
}

} // namespace Shell
} // namespace chip
Loading

0 comments on commit 4007146

Please sign in to comment.