Skip to content

Commit

Permalink
[ESP32] Refactor all esp32 apps (#18426)
Browse files Browse the repository at this point in the history
* [ESP32] Refactor all esp32 apps

* Fixed pipeline crash

* Rebased and addressed review comments
  • Loading branch information
jadhavrohit924 authored and pull[bot] committed Nov 15, 2023
1 parent 1979caf commit 290ed99
Show file tree
Hide file tree
Showing 58 changed files with 457 additions and 2,593 deletions.
5 changes: 5 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,11 @@ menu "CHIP Device Layer"
Automatically disable CHIP-over-BLE (WoBLE) advertising when the device transitions
to a fully provisioned state.

config DEINIT_BLE_ON_COMMISSIONING_COMPLETE
bool "Disable and DeInit BLE on commissioning complete"
default y
help
Disable and deinit BLE and reclaim all its memory, once the commissioning is successful and Commissioning complete event is received in the application.
endmenu

menu "CHIP Thread Options"
Expand Down
1 change: 1 addition & 0 deletions examples/all-clusters-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(SRC_DIRS_LIST
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/util"
Expand Down
166 changes: 22 additions & 144 deletions examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,30 @@
**/
#include "DeviceCallbacks.h"

#include "CHIPDeviceManager.h"
#include "Globals.h"
#include "LEDWidget.h"
#include "WiFiWidget.h"
#include "esp_bt.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_nimble_hci.h"
#include "nimble/nimble_port.h"
#include "route_hook/esp_route_hook.h"
#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/cluster-id.h>
#include <app/CommandHandler.h>
#include <app/clusters/identify-server/identify-server.h>
#include <app/server/Dnssd.h>
#include <app/util/basic-types.h>
#include <app/util/util.h>
#include <common/CHIPDeviceManager.h>
#include <lib/dnssd/Advertiser.h>
#include <lib/support/CodeUtils.h>
#include <ota/OTAHelper.h>

#if CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM
#include <app-common/zap-generated/cluster-id.h>
#endif

static const char * TAG = "app-devicecallbacks";

using namespace ::chip;
using namespace ::chip::Inet;
using namespace ::chip::System;
using namespace ::chip::DeviceLayer;
using namespace chip::app;

constexpr uint32_t kIdentifyTimerDelayMS = 250;
constexpr uint32_t kInitOTARequestorDelaySec = 3;
constexpr uint32_t kIdentifyTimerDelayMS = 250;

void OnIdentifyTriggerEffect(Identify * identify)
{
Expand Down Expand Up @@ -99,78 +89,8 @@ Identify gIdentify1 = {
OnIdentifyTriggerEffect,
};

void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg)
{
switch (event->Type)
{
case DeviceEventType::kInternetConnectivityChange:
OnInternetConnectivityChange(event);
break;

case DeviceEventType::kSessionEstablished:
OnSessionEstablished(event);
break;

case DeviceEventType::kCHIPoBLEConnectionEstablished:
ESP_LOGI(TAG, "CHIPoBLE connection established");
break;

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

case DeviceEventType::kCommissioningComplete: {
ESP_LOGI(TAG, "Commissioning complete");
#if CONFIG_BT_NIMBLE_ENABLED && CONFIG_DEINIT_BLE_ON_COMMISSIONING_COMPLETE

if (ble_hs_is_enabled())
{
int ret = nimble_port_stop();
if (ret == 0)
{
nimble_port_deinit();
esp_err_t err = esp_nimble_hci_and_controller_deinit();
err += esp_bt_mem_release(ESP_BT_MODE_BLE);
if (err == ESP_OK)
{
ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed");
}
}
else
{
ESP_LOGW(TAG, "nimble_port_stop() failed");
}
}
else
{
ESP_LOGI(TAG, "BLE already deinited");
}
#endif
}
break;

case DeviceEventType::kInterfaceIpAddressChanged:
if ((event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV4_Assigned) ||
(event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned))
{
// MDNS server restart on any ip assignment: if link local ipv6 is
// configured, that will not trigger a 'internet connectivity change' as
// there is no internet connectivity. MDNS still wants to refresh its
// listening interfaces to include the newly selected address.
chip::app::DnssdServer::Instance().StartServer();
}
if (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned)
{
ESP_ERROR_CHECK(esp_route_hook_init(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF")));
}
break;
}

ESP_LOGI(TAG, "Current free heap: %u\n", static_cast<unsigned int>(heap_caps_get_free_size(MALLOC_CAP_8BIT)));
}

void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId, uint8_t type,
uint16_t size, uint8_t * value)
void AppDeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId,
uint8_t type, uint16_t size, uint8_t * value)
{
ESP_LOGI(TAG,
"PostAttributeChangeCallback - Cluster ID: '0x%04x', EndPoint ID: "
Expand Down Expand Up @@ -202,58 +122,7 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster
ESP_LOGI(TAG, "Current free heap: %u\n", static_cast<unsigned int>(heap_caps_get_free_size(MALLOC_CAP_8BIT)));
}

void InitOTARequestorHandler(System::Layer * systemLayer, void * appState)
{
OTAHelpers::Instance().InitOTARequestor();
}

void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event)
{
static bool isOTAInitialized = false;
if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established)
{
ESP_LOGI(TAG, "IPv4 Server ready...");
wifiLED.Set(true);
chip::app::DnssdServer::Instance().StartServer();

if (!isOTAInitialized)
{
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec),
InitOTARequestorHandler, nullptr);
isOTAInitialized = true;
}
}
else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost)
{
ESP_LOGE(TAG, "Lost IPv4 connectivity...");
wifiLED.Set(false);
}
if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established)
{
ESP_LOGI(TAG, "IPv6 Server ready...");
chip::app::DnssdServer::Instance().StartServer();
if (!isOTAInitialized)
{
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec),
InitOTARequestorHandler, nullptr);
isOTAInitialized = true;
}
}
else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost)
{
ESP_LOGE(TAG, "Lost IPv6 connectivity...");
}
}

void DeviceCallbacks::OnSessionEstablished(const ChipDeviceEvent * event)
{
if (event->SessionEstablished.IsCommissioner)
{
ESP_LOGI(TAG, "Commissioner detected!");
}
}

void DeviceCallbacks::OnOnOffPostAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value)
void AppDeviceCallbacks::OnOnOffPostAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value)
{
VerifyOrExit(attributeId == ZCL_ON_OFF_ATTRIBUTE_ID, ESP_LOGI(TAG, "Unhandled Attribute ID: '0x%04x", attributeId));
VerifyOrExit(endpointId == 1 || endpointId == 2, ESP_LOGE(TAG, "Unexpected EndPoint ID: `0x%02x'", endpointId));
Expand All @@ -266,7 +135,7 @@ void DeviceCallbacks::OnOnOffPostAttributeChangeCallback(EndpointId endpointId,
return;
}

void DeviceCallbacks::OnLevelControlAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value)
void AppDeviceCallbacks::OnLevelControlAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value)
{
bool onOffState = mEndpointOnOffState[endpointId - 1];
uint8_t brightness = onOffState ? *value : 0;
Expand All @@ -284,7 +153,7 @@ void DeviceCallbacks::OnLevelControlAttributeChangeCallback(EndpointId endpointI
// Currently we only support ColorControl cluster for ESP32C3_DEVKITM which has
// an on-board RGB-LED
#if CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM
void DeviceCallbacks::OnColorControlAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value)
void AppDeviceCallbacks::OnColorControlAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value)
{
VerifyOrExit(attributeId == ZCL_COLOR_CONTROL_CURRENT_HUE_ATTRIBUTE_ID ||
attributeId == ZCL_COLOR_CONTROL_CURRENT_SATURATION_ATTRIBUTE_ID,
Expand Down Expand Up @@ -312,8 +181,8 @@ void DeviceCallbacks::OnColorControlAttributeChangeCallback(EndpointId endpointI
}
#endif

void DeviceCallbacks::OnIdentifyPostAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint16_t size,
uint8_t * value)
void AppDeviceCallbacks::OnIdentifyPostAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint16_t size,
uint8_t * value)
{
if (attributeId == Clusters::Identify::Attributes::IdentifyTime::Id && size == 2)
{
Expand All @@ -339,3 +208,12 @@ bool emberAfBasicClusterMfgSpecificPingCallback(chip::app::CommandHandler * comm
emberAfSendDefaultResponse(emberAfCurrentCommand(), EMBER_ZCL_STATUS_SUCCESS);
return true;
}

void AppDeviceCallbacksDelegate::OnIPv4ConnectivityEstablished()
{
wifiLED.Set(true);
}
void AppDeviceCallbacksDelegate::OnIPv4ConnectivityLost()
{
wifiLED.Set(false);
}
5 changes: 0 additions & 5 deletions examples/all-clusters-app/esp32/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ menu "Demo"
help
Each board has a status led, define its pin number.

config DEINIT_BLE_ON_COMMISSIONING_COMPLETE
bool "Disable and DeInit BLE on commissioning complete"
default y
help
Disable and deinit BLE and reclaim all its memory, once the commissioning is successful and Commissioning complete event is received in the application.
endmenu

menu "PW RPC Debug channel"
Expand Down
18 changes: 10 additions & 8 deletions examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,16 @@

#pragma once

#include "CHIPDeviceManager.h"
#include <app/util/af-types.h>
#include <app/util/basic-types.h>
#include <platform/CHIPDeviceLayer.h>
#include <common/CHIPDeviceManager.h>
#include <common/CommonDeviceCallbacks.h>

class DeviceCallbacks : public chip::DeviceManager::CHIPDeviceManagerCallbacks
class AppDeviceCallbacks : public CommonDeviceCallbacks
{
public:
virtual void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
virtual void PostAttributeChangeCallback(chip::EndpointId endpointId, chip::ClusterId clusterId, chip::AttributeId attributeId,
uint8_t type, uint16_t size, uint8_t * value);

private:
void OnInternetConnectivityChange(const chip::DeviceLayer::ChipDeviceEvent * event);
void OnSessionEstablished(const chip::DeviceLayer::ChipDeviceEvent * event);
void OnOnOffPostAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value);
void OnLevelControlAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value);
#if CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM
Expand All @@ -49,3 +44,10 @@ class DeviceCallbacks : public chip::DeviceManager::CHIPDeviceManagerCallbacks
uint8_t * value);
bool mEndpointOnOffState[2];
};

class AppDeviceCallbacksDelegate : public DeviceCallbacksDelegate
{
public:
void OnIPv4ConnectivityEstablished(void) override;
void OnIPv4ConnectivityLost(void) override;
};
26 changes: 6 additions & 20 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "AppTask.h"
#include "BluetoothWidget.h"
#include "CHIPDeviceManager.h"
#include "DeviceCallbacks.h"
#include "Globals.h"
#include "LEDWidget.h"
Expand All @@ -35,14 +34,12 @@
#include "nvs_flash.h"
#include "platform/PlatformManager.h"
#include "shell_extension/launch.h"
#include <common/CHIPDeviceManager.h>

#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/server/OnboardingCodesUtil.h>
#include <app/util/af.h>
#include <binding-handler.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <platform/ESP32/NetworkCommissioningDriver.h>
#include <common/Esp32AppServer.h>

#if CONFIG_HAVE_DISPLAY
#include "DeviceWithDisplay.h"
Expand All @@ -58,22 +55,17 @@

using namespace ::chip;
using namespace ::chip::Shell;
using namespace ::chip::Credentials;
using namespace ::chip::DeviceManager;
using namespace ::chip::DeviceLayer;

// Used to indicate that an IP address has been added to the QRCode
#define EXAMPLE_VENDOR_TAG_IP 1

const char * TAG = "all-clusters-app";

static DeviceCallbacks EchoCallbacks;

static AppDeviceCallbacks EchoCallbacks;
static AppDeviceCallbacksDelegate sAppDeviceCallbacksDelegate;
namespace {

app::Clusters::NetworkCommissioning::Instance
sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance()));

class AppCallbacks : public AppDelegate
{
public:
Expand All @@ -95,18 +87,11 @@ constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE;

static void InitServer(intptr_t context)
{
// Init ZCL Data Model and CHIP App Server
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();
initParams.appDelegate = &sCallbacks;
chip::Server::GetInstance().Init(initParams);
Esp32AppServer::Init(&sCallbacks); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config

// We only have network commissioning on endpoint 0.
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false);

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());
sWiFiNetworkCommissioningInstance.Init();
InitBindingHandlers();
#if CONFIG_DEVICE_TYPE_M5STACK
SetupPretendDevices();
Expand Down Expand Up @@ -143,6 +128,7 @@ extern "C" void app_main()

CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance();
CHIP_ERROR error = deviceMgr.Init(&EchoCallbacks);
DeviceCallbacksDelegate::Instance().SetAppDelegate(&sAppDeviceCallbacksDelegate);
if (error != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "device.Init() failed: %s", ErrorStr(error));
Expand Down
Loading

0 comments on commit 290ed99

Please sign in to comment.