Skip to content

Commit

Permalink
[v1.2-branch][ESP32][cherry-pick]Cherry pick some necessary fixes for…
Browse files Browse the repository at this point in the history
… v1.2-branch (project-chip#33161)

* [1.1] Cherry pick Thread DNS client and memory leak fixes (project-chip#31457)

* [app] Fix DeferredAttributePersister memory leak (project-chip#31075)

* [app] Fix DeferredAttributePerister memory leak

ScopedMemoryBuffer's Release() method was used instead of
Free(). Add CHECK_RETURN_VALUE annotation to the Release()
method to prevent from making such a mistake in the future.

Signed-off-by: Damian Krolik <[email protected]>

* Code review

---------

Signed-off-by: Damian Krolik <[email protected]>
(cherry picked from commit 3e8aeeb)

* [OpenThread] Harden DNS record parsing (project-chip#31227)

OpenThread applications would crash upon receiving an empty
DNS TXT record. The reason was that the code for copying OT
DNS service info object into Matter DnssdService object
would not initialize the TXT entry count in the latter
object in such a case.

In the reported case, the Matter stack was presented an
empty TXT record because OpenThread's DNS client received
a TXT record with TTL 0 and it discarded its contents.
Nevertheless, the issue could be reproduced by publishing
Matter service without TXT entries and kicking off DNS query.

1. Initialize the TXT entry and subtype count properly in all
   scenarios.
2. Do not even process the service info object if an error was
   returned by OpenThread before.
3. Extract some boilerplate to a separate function to improve
   readability.

Signed-off-by: Damian Krolik <[email protected]>
(cherry picked from commit 76b6bb5)

* ESP32: Add EndpointQueueFilter for ESP32 platform (project-chip#31440)

* Add EndpointQueueFilter for ESP32 platform

* Restyled by clang-format

* Restyled by gn

* fix compile error when disabling inet ipv4

* review changes

* Restyled by clang-format

* review changes

* review changes

---------

Co-authored-by: Restyled.io <[email protected]>

* [ESP32] Limit number of returned WiFi scan results to configured limit (project-chip#30780)

Scan results are allocated on the heap and on a resource critical device
where heap is less, this may fail if there are a lot of APs in the
vicinity.

* [ESP32] Fix the threading issue in nimble (project-chip#29180)

* [ESP32] Fix the threading issue in nimble

Send ble connection error than executing in nimble thread context

* comment explaining why we are posting connection error event

* Adding a comment for kCHIPoBLEConnectionError

* IM: Create ReadHandler after Session Establishment for Subscription Resumption (project-chip#30491)

* IM: Create ReadHandler after Session Establishment for Subscription Resumption

* Restyled by clang-format

* Make SubscriptionResumptionHelper inherits from SubscriptionInfo

* review changes

* Rename Helper to SessionEstablisher

* Restyled by clang-format

* RAII changes

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>

* [ESP32] Fix adding NDEBUG flag to CPPFLAGS (project-chip#30763)

In esp-idf, NDEBUG flag is added to CPPFLAGS only if assertions are
disabled. Making this inline to that.

* Add records of session establishment for subscription resumption (project-chip#31755)

* Add records of session establishment for subscription resumption

* Restyled by clang-format

* review changes

* Schedule subscription resumption when failing to establish the session in SubscriptionResumptionSessionEstablisher

* Add option to set subscription timeout resumption retry interval seconds for Linux app
Add cirque test for subscription resumption timeout

* Restyled by clang-format

* Restyled by autopep8

* Restyled by isort

* fix CI building

* Add test to the test list

* add subscription resumption restries number to SubscriptionInfo struct

* review changes

* make resumption retries persistent

* Restyled by clang-format

* ci build fixes

* try to fix cirque test

---------

Co-authored-by: Restyled.io <[email protected]>

* ESP32: check ap info in IsStationConnected (project-chip#31438)

* Add checks for mOTInst in GenericThreadStackManagerImpl_OpenThread (project-chip#32482)

* Add checks for mOTInst in GenericThreadStackManagerImpl_OpenThread

* review changes

* [ESP32] Fix few attributes with fixed quality in DeviceInfoProvider (project-chip#32893)

* [ESP32] Fix few attributes with fixed quality in DeviceInfoProvider

Fixed labels, supported locales, supported calendar types were being
read from the nvs(flash) and during OTA its a hassle if one wants to
upgrade these values. Added few APIs to set the data for these
attributes in ESP32DeviceInfoProvider.

* Restyled by clang-format

* Restyled by prettier-markdown

* fix the lint errors

* Add back the original Device info provider which reads from the nvs

Add StaticESP32DeviceInfoProvider along with APIs to set data

Remove changes from example and add a guide along with usage

---------

Co-authored-by: Restyled.io <[email protected]>

* Implement BLE Manager Shutdown for nimble host (project-chip#33109)

* [ESP32] Implement BLE Manager Shutdown for nimble host

- Replace ble deinit imple in Esp32AppServer with BLEMgr().Shutdown()
- Replace few ESP_LOG with ChipLog in Esp32AppServer
- Move ble deinit kCommissioningComplete switch case
- Make USE_BLE_ONLY_FOR_COMMISSIONING depends on BT_ENABLED

* Restyled by clang-format

* address reviews

* Add checks for timer handler

---------

Co-authored-by: Restyled.io <[email protected]>

* [ESP32]  Made a provision to generate esp_secure_cert partition in factory partition script. (project-chip#29840)

* Made a provision to generate esp_secure_cert partition in factory partition script.

- Added the provision to generate esp_secure_cert_partition based on option --dac-in-secure-cert.
- Refactored some code of the existing script - made it more modular.

* made outdir user governed

* Added the support for onboarding paylaod in factory script (project-chip#31274)

---------

Co-authored-by: Damian Królik <[email protected]>
Co-authored-by: Wang Qixiang <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Shubham Patil <[email protected]>
Co-authored-by: shripad621git <[email protected]>
  • Loading branch information
6 people authored Apr 26, 2024
1 parent d3e48c6 commit c11bca8
Show file tree
Hide file tree
Showing 38 changed files with 1,455 additions and 352 deletions.
5 changes: 1 addition & 4 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ if(NOT "${IDF_TARGET}" STREQUAL "esp32h2")
endif()

if (NOT CMAKE_BUILD_EARLY_EXPANSION)
if (CONFIG_COMPILER_OPTIMIZATION_DEFAULT OR CONFIG_COMPILER_OPTIMIZATION_NONE)
if (CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE)
set(is_debug TRUE)
else()
if (NOT CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE)
message(FATAL_ERROR "CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE shall be set")
endif()
set(is_debug FALSE)
endif()
endif()
Expand Down
20 changes: 14 additions & 6 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ menu "CHIP Core"
help
Enable this option to use LwIP default IPv6 route hook for Route Information Option(RIO) feature.

config ENABLE_ENDPOINT_QUEUE_FILTER
bool "Enable UDP Endpoint queue filter for mDNS Broadcast packets"
depends on USE_MINIMAL_MDNS
default y
help
Enable this option to start a UDP Endpoint queue filter for mDNS Broadcast packets

config ENABLE_LWIP_THREAD_SAFETY
bool "Enable LwIP Thread safety options"
default y
Expand Down Expand Up @@ -625,12 +632,13 @@ menu "CHIP Device Layer"
When set, WoBLE advertisements will stop while a WoBLE connection is active.

config USE_BLE_ONLY_FOR_COMMISSIONING
bool "Use BLE only for commissioning"
default y
help
Disable this flag if BLE is used for any other purpose than commissioning.
When enabled, it deinitialized the BLE on successful commissioning, and on
bootup do not initialize the BLE if device is already provisioned with Wi-Fi/Thread credentials.
depends on BT_ENABLED
bool "Use BLE only for commissioning"
default y
help
Disable this flag if BLE is used for any other purpose than commissioning.
When enabled, it deinitialized the BLE on successful commissioning, and on
bootup do not initialize the BLE if device is already provisioned with Wi-Fi/Thread credentials.

endmenu

Expand Down
1 change: 1 addition & 0 deletions docs/guides/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ example on ESP32 series of SoCs
- [Matter OTA](ota.md)
- [Generating and Using ESP Secure Cert Partition](secure_cert_partition.md)
- [BLE Settings](ble_settings.md)
- [Providers](providers.md)
10 changes: 3 additions & 7 deletions docs/guides/esp32/factory_data.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@ Following data can be added to the manufacturing partition using
- Serial Number
- Unique identifier

- Device information
- Fixed Labels
- Supported locales
- Supported calendar types
- Supported modes
- Note: As per spec at max size of label should be 64 and `\0` will be
added at the end.
- Supported modes
- Note: As per spec at max size of label should be 64 and `\0` will be
added at the end.

### Configuration Options

Expand Down
76 changes: 76 additions & 0 deletions docs/guides/esp32/providers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
## Providers Implemented for ESP32 Platform

The ESP32 platform has implemented several providers that can be used with data
stored in the factory or by setting fixed data.

Below are the providers that have been implemented:

- [Commissionable Data Provider](https://github.com/project-chip/connectedhomeip/blob/master/src/platform/ESP32/ESP32FactoryDataProvider.h#L47)
This provider reads the discriminator and setup pincode related parameters
from the factory partition.
- [Device Attestation Credentials Provider](https://github.com/project-chip/connectedhomeip/blob/master/src/platform/ESP32/ESP32FactoryDataProvider.h#L56)
This provider manages the attestation data.
- [Device Instance Info Provider](https://github.com/project-chip/connectedhomeip/blob/master/src/platform/ESP32/ESP32FactoryDataProvider.h#L86)
This provider reads basic device information from the factory partition.
- [Device Info Provider](https://github.com/project-chip/connectedhomeip/blob/master/src/platform/ESP32/ESP32DeviceInfoProvider.h#L31)
This provider provides fixed labels, supported calendar types, and supported
locales from the factory partition.
- [Supported Modes](https://github.com/project-chip/connectedhomeip/blob/master/examples/platform/esp32/mode-support/static-supported-modes-manager.h#L28)
This provider offers the supported modes for the mode-select cluster.

More information can be found in the [factory data guide](factory_data.md).

### Device Info Provider

Currently, there are two implementations for this provider:

1. [Reads data stored in the factory partition](https://github.com/project-chip/connectedhomeip/blob/master/src/platform/ESP32/ESP32FactoryDataProvider.h#L56)
_(This will be deprecated in the future)_
2. [Provides APIs to set fixed data that gets read later](https://github.com/project-chip/connectedhomeip/blob/master/src/platform/ESP32/StaticESP32DeviceInfoProvider.h)

- New products should use the `StaticESP32DeviceInfoProvider`. Utilize the
`Set...()` APIs to set the fixed data.
- Existing products using the first implementation can continue to use it if
they do not wish to change the data.
- For products using the first implementation and wanting to change the fixed
data via OTA, they should switch to the second implementation in the OTA
image and use the `Set...()` APIs to set the fixed data.

#### Example:

```cpp
#include <platform/ESP32/StaticESP32FactoryDataProvider.h>

DeviceLayer::StaticESP32DeviceInfoProvider deviceInfoProvider;

// Define array for Supported Calendar Types
using namespace chip::app::Clusters::TimeFormatLocalization::CalendarTypeEnum;
CalendarTypeEnum supportedCalendarTypes[] = {
CalendarTypeEnum::kGregorian, CalendarTypeEnum::kCoptic,
CalendarTypeEnum::kEthiopian, CalendarTypeEnum::kChinese,
};

// Define array for Supported Locales
const char* supportedLocales[] = {
"en-US",
"en-EU",
};

// Define array for Fixed labels { EndpointId, Label, Value }
struct StaticESP32DeviceInfoProvider::FixedLabelEntry fixedLabels[] = {
{ 0, "Room", "Bedroom 2" },
{ 0, "Orientation", "North" },
{ 0, "Direction", "Up" },
};

Span<CalendarTypeEnum> sSupportedCalendarTypes(supportedCalendarTypes);
Span<const char*> sSupportedLocales(supportedLocales);
Span<StaticESP32DeviceInfoProvider::FixedLabelEntry> sFixedLabels(fixedLabels);

{
deviceInfoProvider.SetSupportedLocales(sSupportedLocales);
deviceInfoProvider.SetSupportedCalendarTypes(sSupportedCalendarTypes);
deviceInfoProvider.SetFixedLabels(sFixedLabels);
DeviceLayer::SetDeviceInfoProvider(&deviceInfoProvider);
}
```
2 changes: 1 addition & 1 deletion examples/platform/esp32/common/CommonDeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i

case DeviceEventType::kCHIPoBLEConnectionClosed:
ESP_LOGI(TAG, "CHIPoBLE disconnected");
Esp32AppServer::DeInitBLEIfCommissioned();
break;

case DeviceEventType::kDnssdInitialized:
Expand All @@ -67,6 +66,7 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i

case DeviceEventType::kCommissioningComplete: {
ESP_LOGI(TAG, "Commissioning complete");
Esp32AppServer::DeInitBLEIfCommissioned();
}
break;

Expand Down
44 changes: 5 additions & 39 deletions examples/platform/esp32/common/Esp32AppServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,46 +115,12 @@ static size_t hex_string_to_binary(const char * hex_string, uint8_t * buf, size_

void Esp32AppServer::DeInitBLEIfCommissioned(void)
{
#if CONFIG_BT_ENABLED && CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING
#ifdef CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING
if (chip::Server::GetInstance().GetFabricTable().FabricCount() > 0)
{
esp_err_t err = ESP_OK;

#if CONFIG_BT_NIMBLE_ENABLED
if (!ble_hs_is_enabled())
{
ESP_LOGI(TAG, "BLE already deinited");
return;
}
if (nimble_port_stop() != 0)
{
ESP_LOGE(TAG, "nimble_port_stop() failed");
return;
}
vTaskDelay(100);
nimble_port_deinit();

#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
err = esp_nimble_hci_and_controller_deinit();
#endif
#endif /* CONFIG_BT_NIMBLE_ENABLED */

#if CONFIG_IDF_TARGET_ESP32
err |= esp_bt_mem_release(ESP_BT_MODE_BTDM);
#elif CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2
err |= esp_bt_mem_release(ESP_BT_MODE_BLE);
#endif

if (err != ESP_OK)
{
ESP_LOGE(TAG, "BLE deinit failed");
}
else
{
ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed");
}
chip::DeviceLayer::Internal::BLEMgr().Shutdown();
}
#endif /* CONFIG_BT_ENABLED && CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */
#endif /* CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */
}

void Esp32AppServer::Init(AppDelegate * sAppDelegate)
Expand All @@ -165,7 +131,7 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate)
if (hex_string_to_binary(CONFIG_TEST_EVENT_TRIGGER_ENABLE_KEY, sTestEventTriggerEnableKey,
sizeof(sTestEventTriggerEnableKey)) == 0)
{
ESP_LOGE(TAG, "Failed to convert the EnableKey string to octstr type value");
ChipLogError(DeviceLayer, "Failed to convert the EnableKey string to octstr type value");
memset(sTestEventTriggerEnableKey, 0, sizeof(sTestEventTriggerEnableKey));
}
static OTATestEventTriggerDelegate testEventTriggerDelegate{ ByteSpan(sTestEventTriggerEnableKey) };
Expand All @@ -189,7 +155,7 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate)
if (chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned() &&
(chip::Server::GetInstance().GetFabricTable().FabricCount() != 0))
{
ESP_LOGI(TAG, "Thread has been provisioned, publish the dns service now");
ChipLogProgress(DeviceLayer, "Thread has been provisioned, publish the dns service now");
chip::app::DnssdServer::Instance().StartServer();
}
#endif
Expand Down
12 changes: 12 additions & 0 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,18 @@ void ChipLinuxAppMainLoop(AppMainLoopImplementation * impl)
// Init ZCL Data Model and CHIP App Server
Server::GetInstance().Init(initParams);

#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
// Set ReadHandler Capacity for Subscriptions
chip::app::InteractionModelEngine::GetInstance()->SetHandlerCapacityForSubscriptions(
LinuxDeviceOptions::GetInstance().subscriptionCapacity);
chip::app::InteractionModelEngine::GetInstance()->SetForceHandlerQuota(true);
#if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS && CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
// Set subscription time resumption retry interval seconds
chip::app::InteractionModelEngine::GetInstance()->SetSubscriptionTimeoutResumptionRetryIntervalSeconds(
LinuxDeviceOptions::GetInstance().subscriptionResumptionRetryIntervalSec);
#endif // CHIP_CONFIG_PERSIST_SUBSCRIPTIONS && CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST

// Now that the server has started and we are done with our startup logging,
// log our discovery/onboarding information again so it's not lost in the
// noise.
Expand Down
40 changes: 40 additions & 0 deletions examples/platform/linux/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ enum
kCommissionerOption_FabricID = 0x1020,
kTraceTo = 0x1021,
kOptionSimulateNoInternalTime = 0x1022,
#if defined(PW_RPC_ENABLED)
kOptionRpcServerPort = 0x1023,
#endif
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
kDeviceOption_SubscriptionCapacity = 0x1024,
#endif
kDeviceOption_WiFiSupports5g = 0x1025,
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
kDeviceOption_SubscriptionResumptionRetryIntervalSec = 0x1026,
#endif
};

constexpr unsigned kAppUsageLength = 64;
Expand Down Expand Up @@ -138,6 +148,13 @@ OptionDef sDeviceOptionDefs[] = {
{ "trace-to", kArgumentRequired, kTraceTo },
#endif
{ "simulate-no-internal-time", kNoArgument, kOptionSimulateNoInternalTime },
#if defined(PW_RPC_ENABLED)
{ "rpc-server-port", kArgumentRequired, kOptionRpcServerPort },
#endif
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
{ "subscription-capacity", kArgumentRequired, kDeviceOption_SubscriptionCapacity },
{ "subscription-resumption-retry-interval", kArgumentRequired, kDeviceOption_SubscriptionResumptionRetryIntervalSec },
#endif
{}
};

Expand Down Expand Up @@ -254,6 +271,16 @@ const char * sDeviceOptionHelp =
#endif
" --simulate-no-internal-time\n"
" Time cluster does not use internal platform time\n"
#if defined(PW_RPC_ENABLED)
" --rpc-server-port\n"
" Start RPC server on specified port\n"
#endif
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
" --subscription-capacity\n"
" Max number of subscriptions the device will allow\n"
" --subscription-resumption-retry-interval\n"
" subscription timeout resumption retry interval in seconds\n"
#endif
"\n";

bool Base64ArgToVector(const char * arg, size_t maxSize, std::vector<uint8_t> & outVector)
Expand Down Expand Up @@ -507,6 +534,19 @@ bool HandleOption(const char * aProgram, OptionSet * aOptions, int aIdentifier,
case kOptionSimulateNoInternalTime:
LinuxDeviceOptions::GetInstance().mSimulateNoInternalTime = true;
break;
#if defined(PW_RPC_ENABLED)
case kOptionRpcServerPort:
LinuxDeviceOptions::GetInstance().rpcServerPort = static_cast<uint16_t>(atoi(aValue));
break;
#endif
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
case kDeviceOption_SubscriptionCapacity:
LinuxDeviceOptions::GetInstance().subscriptionCapacity = static_cast<int32_t>(atoi(aValue));
break;
case kDeviceOption_SubscriptionResumptionRetryIntervalSec:
LinuxDeviceOptions::GetInstance().subscriptionResumptionRetryIntervalSec = static_cast<int32_t>(atoi(aValue));
break;
#endif
default:
PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", aProgram, aName);
retval = false;
Expand Down
8 changes: 7 additions & 1 deletion examples/platform/linux/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ struct LinuxDeviceOptions
chip::FabricId commissionerFabricId = chip::kUndefinedFabricId;
std::vector<std::string> traceTo;
bool mSimulateNoInternalTime = false;

#if defined(PW_RPC_ENABLED)
uint16_t rpcServerPort = 33000;
#endif
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
int32_t subscriptionCapacity = CHIP_IM_MAX_NUM_SUBSCRIPTIONS;
int32_t subscriptionResumptionRetryIntervalSec = -1;
#endif
static LinuxDeviceOptions & GetInstance();
};

Expand Down
3 changes: 3 additions & 0 deletions scripts/tests/cirque_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ CIRQUE_TESTS=(
"CommissioningFailureOnReportTest"
"PythonCommissioningTest"
"CommissioningWindowTest"
"SubscriptionResumptionTest"
"SubscriptionResumptionCapacityTest"
"SubscriptionResumptionTimeoutTest"
)

BOLD_GREEN_TEXT="\033[1;32m"
Expand Down
Loading

0 comments on commit c11bca8

Please sign in to comment.