From 290ed99995c92eabf2744b977757ae01c8cb964f Mon Sep 17 00:00:00 2001 From: Rohit Jadhav <69809379+jadhavrohit924@users.noreply.github.com> Date: Wed, 18 May 2022 18:20:38 +0530 Subject: [PATCH] [ESP32] Refactor all esp32 apps (#18426) * [ESP32] Refactor all esp32 apps * Fixed pipeline crash * Rebased and addressed review comments --- config/esp32/components/chip/Kconfig | 5 + .../esp32/main/CMakeLists.txt | 1 + .../esp32/main/DeviceCallbacks.cpp | 166 +++-------------- .../esp32/main/Kconfig.projbuild | 5 - .../esp32/main/include/DeviceCallbacks.h | 18 +- examples/all-clusters-app/esp32/main/main.cpp | 26 +-- .../esp32/main/CHIPDeviceManager.cpp | 91 ---------- examples/bridge-app/esp32/main/CMakeLists.txt | 1 + .../bridge-app/esp32/main/DeviceCallbacks.cpp | 72 +------- .../esp32/main/include/CHIPDeviceManager.h | 119 ------------ .../esp32/main/include/DeviceCallbacks.h | 10 +- examples/bridge-app/esp32/main/main.cpp | 24 +-- .../esp32/main/CHIPDeviceManager.cpp | 93 ---------- .../esp32/main/CMakeLists.txt | 3 +- .../esp32/main/DeviceCallbacks.cpp | 144 +-------------- .../esp32/main/Kconfig.projbuild | 7 +- .../esp32/main/include/CHIPDeviceManager.h | 123 ------------- .../esp32/main/include/DeviceCallbacks.h | 10 +- examples/light-switch-app/esp32/main/main.cpp | 36 +--- .../esp32/main/CHIPDeviceManager.cpp | 93 ---------- .../lighting-app/esp32/main/CMakeLists.txt | 5 +- .../esp32/main/DeviceCallbacks.cpp | 148 +-------------- .../lighting-app/esp32/main/Kconfig.projbuild | 5 - .../esp32/main/include/CHIPDeviceManager.h | 121 ------------- .../esp32/main/include/DeviceCallbacks.h | 8 +- examples/lighting-app/esp32/main/main.cpp | 49 +---- .../lock-app/esp32/main/CHIPDeviceManager.cpp | 93 ---------- examples/lock-app/esp32/main/CMakeLists.txt | 4 +- .../lock-app/esp32/main/DeviceCallbacks.cpp | 75 +------- .../esp32/main/include/CHIPDeviceManager.h | 124 ------------- .../esp32/main/include/DeviceCallbacks.h | 10 +- examples/lock-app/esp32/main/main.cpp | 27 +-- .../esp32/main/CHIPDeviceManager.cpp | 91 ---------- .../esp32/main/CMakeLists.txt | 2 + .../esp32/main/DeviceCallbacks.cpp | 68 +------ .../esp32/main/include/CHIPDeviceManager.h | 126 ------------- .../esp32/main/include/DeviceCallbacks.h | 10 +- examples/ota-provider-app/esp32/main/main.cpp | 23 +-- .../esp32/main/CHIPDeviceManager.cpp | 91 ---------- .../esp32/main/CMakeLists.txt | 2 + .../esp32/main/DeviceCallbacks.cpp | 90 +--------- .../esp32/main/include/CHIPDeviceManager.h | 126 ------------- .../esp32/main/include/DeviceCallbacks.h | 10 +- .../ota-requestor-app/esp32/main/main.cpp | 25 +-- .../esp32/common}/CHIPDeviceManager.cpp | 0 .../esp32/common}/CHIPDeviceManager.h | 0 .../esp32/common/CommonDeviceCallbacks.cpp | 169 ++++++++++++++++++ .../esp32/common/CommonDeviceCallbacks.h | 47 +++++ .../platform/esp32/common/Esp32AppServer.cpp | 74 ++++++++ .../platform/esp32/common/Esp32AppServer.h | 26 +++ examples/platform/esp32/ota/OTAHelper.cpp | 16 +- examples/platform/esp32/ota/OTAHelper.h | 3 +- .../esp32/main/CHIPDeviceManager.cpp | 94 ---------- .../esp32/main/CMakeLists.txt | 1 + .../esp32/main/DeviceCallbacks.cpp | 76 +------- .../esp32/main/include/CHIPDeviceManager.h | 126 ------------- .../esp32/main/include/DeviceCallbacks.h | 10 +- .../esp32/main/main.cpp | 28 +-- 58 files changed, 457 insertions(+), 2593 deletions(-) delete mode 100644 examples/bridge-app/esp32/main/CHIPDeviceManager.cpp delete mode 100644 examples/bridge-app/esp32/main/include/CHIPDeviceManager.h delete mode 100644 examples/light-switch-app/esp32/main/CHIPDeviceManager.cpp delete mode 100644 examples/light-switch-app/esp32/main/include/CHIPDeviceManager.h delete mode 100644 examples/lighting-app/esp32/main/CHIPDeviceManager.cpp delete mode 100644 examples/lighting-app/esp32/main/include/CHIPDeviceManager.h delete mode 100644 examples/lock-app/esp32/main/CHIPDeviceManager.cpp delete mode 100644 examples/lock-app/esp32/main/include/CHIPDeviceManager.h delete mode 100644 examples/ota-provider-app/esp32/main/CHIPDeviceManager.cpp delete mode 100644 examples/ota-provider-app/esp32/main/include/CHIPDeviceManager.h delete mode 100644 examples/ota-requestor-app/esp32/main/CHIPDeviceManager.cpp delete mode 100644 examples/ota-requestor-app/esp32/main/include/CHIPDeviceManager.h rename examples/{all-clusters-app/esp32/main => platform/esp32/common}/CHIPDeviceManager.cpp (100%) rename examples/{all-clusters-app/esp32/main/include => platform/esp32/common}/CHIPDeviceManager.h (100%) create mode 100644 examples/platform/esp32/common/CommonDeviceCallbacks.cpp create mode 100644 examples/platform/esp32/common/CommonDeviceCallbacks.h create mode 100644 examples/platform/esp32/common/Esp32AppServer.cpp create mode 100644 examples/platform/esp32/common/Esp32AppServer.h delete mode 100644 examples/temperature-measurement-app/esp32/main/CHIPDeviceManager.cpp delete mode 100644 examples/temperature-measurement-app/esp32/main/include/CHIPDeviceManager.h diff --git a/config/esp32/components/chip/Kconfig b/config/esp32/components/chip/Kconfig index 33a95e63384983..35e926e781340c 100644 --- a/config/esp32/components/chip/Kconfig +++ b/config/esp32/components/chip/Kconfig @@ -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" diff --git a/examples/all-clusters-app/esp32/main/CMakeLists.txt b/examples/all-clusters-app/esp32/main/CMakeLists.txt index 8e83837ab522e4..df3431cd088b9d 100644 --- a/examples/all-clusters-app/esp32/main/CMakeLists.txt +++ b/examples/all-clusters-app/esp32/main/CMakeLists.txt @@ -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" diff --git a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp index 0b05da8e240985..52d632f7fa5afa 100644 --- a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp @@ -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 #include -#include #include #include -#include #include #include +#include #include -#include -#include + +#if CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM +#include +#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) { @@ -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(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: " @@ -202,58 +122,7 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster ESP_LOGI(TAG, "Current free heap: %u\n", static_cast(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)); @@ -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; @@ -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, @@ -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) { @@ -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); +} diff --git a/examples/all-clusters-app/esp32/main/Kconfig.projbuild b/examples/all-clusters-app/esp32/main/Kconfig.projbuild index b1a935811f318b..8791a80a443e71 100644 --- a/examples/all-clusters-app/esp32/main/Kconfig.projbuild +++ b/examples/all-clusters-app/esp32/main/Kconfig.projbuild @@ -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" diff --git a/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h b/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h index 4bfcb3921d8ad7..db97219ed86c9e 100644 --- a/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h @@ -25,21 +25,16 @@ #pragma once -#include "CHIPDeviceManager.h" -#include -#include -#include +#include +#include -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 @@ -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; +}; diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 0638b838d77e9d..80e0b9ecf15edb 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -18,7 +18,6 @@ #include "AppTask.h" #include "BluetoothWidget.h" -#include "CHIPDeviceManager.h" #include "DeviceCallbacks.h" #include "Globals.h" #include "LEDWidget.h" @@ -35,14 +34,12 @@ #include "nvs_flash.h" #include "platform/PlatformManager.h" #include "shell_extension/launch.h" +#include -#include #include #include #include -#include -#include -#include +#include #if CONFIG_HAVE_DISPLAY #include "DeviceWithDisplay.h" @@ -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: @@ -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(); @@ -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)); diff --git a/examples/bridge-app/esp32/main/CHIPDeviceManager.cpp b/examples/bridge-app/esp32/main/CHIPDeviceManager.cpp deleted file mode 100644 index 1dec61ef5c8d57..00000000000000 --- a/examples/bridge-app/esp32/main/CHIPDeviceManager.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * - * 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. - */ - -/** - * @file - * This file implements the CHIP Device Interface that is used by - * applications to interact with the CHIP stack - * - */ - -#include - -#include "CHIPDeviceManager.h" -#include -#include -#include -#include -#include -#include - -using namespace ::chip; - -namespace chip { - -namespace DeviceManager { - -using namespace ::chip::DeviceLayer; - -void CHIPDeviceManager::CommonDeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg) -{ - CHIPDeviceManagerCallbacks * cb = reinterpret_cast(arg); - if (cb != nullptr) - { - cb->DeviceEventCallback(event, reinterpret_cast(cb)); - } -} - -CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb) -{ - mCB = cb; - RendezvousInformationFlags flags = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE); - - ReturnErrorOnFailure(Platform::MemoryInit()); - - // Initialize the CHIP stack. - ReturnErrorOnFailure(PlatformMgr().InitChipStack()); - - if (flags.Has(RendezvousInformationFlag::kBLE)) - { - ConnectivityMgr().SetBLEAdvertisingEnabled(true); - } - else if (flags.Has(RendezvousInformationFlag::kSoftAP)) - { - // TODO(cecille): Fix for the case where BLE and SoftAP are both enabled.` - ConnectivityMgr().SetBLEAdvertisingEnabled(false); - ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled); - } - - // Register a function to receive events from the CHIP device layer. Note that calls to - // this function will happen on the CHIP event loop thread, not the app_main thread. - PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast(cb)); - - // Start a task to run the CHIP Device event loop. - return PlatformMgr().StartEventLoopTask(); -} -} // namespace DeviceManager -} // namespace chip - -void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & path, uint8_t type, uint16_t size, uint8_t * value) -{ - chip::DeviceManager::CHIPDeviceManagerCallbacks * cb = - chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks(); - if (cb != nullptr) - { - cb->PostAttributeChangeCallback(path.mEndpointId, path.mClusterId, path.mAttributeId, type, size, value); - } -} diff --git a/examples/bridge-app/esp32/main/CMakeLists.txt b/examples/bridge-app/esp32/main/CMakeLists.txt index f253e8de664868..ebf11fca948731 100644 --- a/examples/bridge-app/esp32/main/CMakeLists.txt +++ b/examples/bridge-app/esp32/main/CMakeLists.txt @@ -48,6 +48,7 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-commissioning-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" PRIV_REQUIRES chip QRCode bt) set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 14) diff --git a/examples/bridge-app/esp32/main/DeviceCallbacks.cpp b/examples/bridge-app/esp32/main/DeviceCallbacks.cpp index 58a59b2603d6de..9ca8038c32a716 100644 --- a/examples/bridge-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/bridge-app/esp32/main/DeviceCallbacks.cpp @@ -17,85 +17,17 @@ */ #include "DeviceCallbacks.h" -#include "esp_check.h" -#include "esp_err.h" -#include "esp_heap_caps.h" -#include "esp_log.h" -#include "route_hook/esp_route_hook.h" -#include -#include -#include -#include static const char * TAG = "bridge-devicecallbacks"; using namespace ::chip; using namespace ::chip::Inet; using namespace ::chip::System; -using namespace ::chip::DeviceLayer; -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::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(); - } - break; - } - - ESP_LOGI(TAG, "Current free heap: %d\n", 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: '0x%02x', Attribute ID: '0x%04x'", clusterId, endpointId, attributeId); ESP_LOGI(TAG, "Current free heap: %d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT)); } - -void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) -{ - if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) - { - ESP_LOGI(TAG, "IPv4 Server ready..."); - chip::app::DnssdServer::Instance().StartServer(); - } - else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) - { - ESP_LOGE(TAG, "Lost IPv4 connectivity..."); - } - if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established) - { - ESP_LOGI(TAG, "IPv6 Server ready..."); - chip::app::DnssdServer::Instance().StartServer(); - ESP_ERROR_CHECK(esp_route_hook_init(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"))); - } - 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!"); - } -} diff --git a/examples/bridge-app/esp32/main/include/CHIPDeviceManager.h b/examples/bridge-app/esp32/main/include/CHIPDeviceManager.h deleted file mode 100644 index 3d755dcdbcfce2..00000000000000 --- a/examples/bridge-app/esp32/main/include/CHIPDeviceManager.h +++ /dev/null @@ -1,119 +0,0 @@ -/* - * - * 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. - */ - -/** - * @file - * This file contains definitions for the CHIP DeviceManager Interface - * - * This object will co-ordinate multiple activities such as - * initialisation, rendezvous, session mgmt and other such - * activities within the CHIP stack. This is a singleton object. - */ - -#include -#include -#include - -#include - -#include -#include - -#include - -namespace chip { -namespace DeviceManager { - -/** - * @brief - * This class provides a skeleton for all the callback functions. The functions will be - * called by other objects within the CHIP stack for specific events. - * Applications interested in receiving specific callbacks can specialize this class and handle - * these events in their implementation of this class. - */ -class DLL_EXPORT CHIPDeviceManagerCallbacks -{ -public: - /** - * @brief - * Called when CHIP Device events (PublicEventTypes) are triggered. - * - * @param event ChipDeviceEvent that occurred - * @param arg arguments specific to the event, if any - */ - virtual void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - /** - * @brief - * Called after an attribute has been changed - * - * @param endpoint endpoint id - * @param clusterID cluster id - * @param attributeId attribute id that was changed - * @param manufacturerCode manufacturer code - * @param type attribute type - * @param size size of the attribute - * @param value pointer to the new value - */ - virtual void PostAttributeChangeCallback(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId, - uint8_t type, uint16_t size, uint8_t * value) - {} - virtual ~CHIPDeviceManagerCallbacks() {} -}; - -/** - * @brief - * A common class that drives other components of the CHIP stack - */ -class DLL_EXPORT CHIPDeviceManager -{ -public: - CHIPDeviceManager(const CHIPDeviceManager &) = delete; - CHIPDeviceManager(const CHIPDeviceManager &&) = delete; - CHIPDeviceManager & operator=(const CHIPDeviceManager &) = delete; - - static CHIPDeviceManager & GetInstance() - { - static CHIPDeviceManager instance; - return instance; - } - - /** - * @brief - * Initialise CHIPDeviceManager - * - * @param cb Application's instance of the CHIPDeviceManagerCallbacks for consuming events - */ - CHIP_ERROR Init(CHIPDeviceManagerCallbacks * cb); - /** - * @brief - * Fetch a pointer to the registered CHIPDeviceManagerCallbacks object. - * - */ - CHIPDeviceManagerCallbacks * GetCHIPDeviceManagerCallbacks() { return mCB; } - - /** - * Use internally for registration of the ChipDeviceEvents - */ - static void CommonDeviceEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - -private: - CHIPDeviceManagerCallbacks * mCB = nullptr; - CHIPDeviceManager() {} -}; - -} // namespace DeviceManager -} // namespace chip diff --git a/examples/bridge-app/esp32/main/include/DeviceCallbacks.h b/examples/bridge-app/esp32/main/include/DeviceCallbacks.h index 03ce245674e373..60a9ccf6cb4eb5 100644 --- a/examples/bridge-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/bridge-app/esp32/main/include/DeviceCallbacks.h @@ -23,19 +23,15 @@ * **/ -#include "CHIPDeviceManager.h" -#include -#include +#include +#include -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 OnIdentifyPostAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value); }; diff --git a/examples/bridge-app/esp32/main/main.cpp b/examples/bridge-app/esp32/main/main.cpp index 7cc518c476384a..ae5424de9f345a 100644 --- a/examples/bridge-app/esp32/main/main.cpp +++ b/examples/bridge-app/esp32/main/main.cpp @@ -22,33 +22,22 @@ #include #include #include -#include #include -#include #include -#include -#include +#include #include #include #include -#include #include const char * TAG = "bridge-app"; using namespace ::chip; -using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; -using namespace ::chip::DeviceLayer; using namespace ::chip::Platform; -static DeviceCallbacks AppCallback; - -namespace { -app::Clusters::NetworkCommissioning::Instance - sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); -} // namespace +static AppDeviceCallbacks AppCallback; static const int kNodeLabelSize = 32; // Current ZCL implementation of Struct uses a max-size array of 254 bytes @@ -387,14 +376,7 @@ const EmberAfDeviceType gBridgedOnOffDeviceTypes[] = { { DEVICE_TYPE_LO_ON_OFF_L static void InitServer(intptr_t context) { - static chip::CommonCaseDeviceServerInitParams initParams; - (void) initParams.InitializeStaticResourcesBeforeServerInit(); - chip::Server::GetInstance().Init(initParams); - - // Initialize device attestation config - SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); - - sWiFiNetworkCommissioningInstance.Init(); + Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config // Set starting endpoint id where dynamic endpoints will be assigned, which // will be the next consecutive endpoint id after the last fixed endpoint. diff --git a/examples/light-switch-app/esp32/main/CHIPDeviceManager.cpp b/examples/light-switch-app/esp32/main/CHIPDeviceManager.cpp deleted file mode 100644 index 30d77efadcd230..00000000000000 --- a/examples/light-switch-app/esp32/main/CHIPDeviceManager.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/* - * - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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. - */ - -/** - * @file - * This file implements the CHIP Device Interface that is used by - * applications to interact with the CHIP stack - * - */ - -#include "CHIPDeviceManager.h" -#include -#include -#include -#include -#include -#include -#include - -using namespace ::chip; - -namespace chip { - -namespace DeviceManager { - -using namespace ::chip::DeviceLayer; - -void CHIPDeviceManager::CommonDeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg) -{ - CHIPDeviceManagerCallbacks * cb = reinterpret_cast(arg); - if (cb != nullptr) - { - cb->DeviceEventCallback(event, reinterpret_cast(cb)); - } -} - -CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb) -{ - mCB = cb; - RendezvousInformationFlags flags = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE); - - ReturnErrorOnFailure(Platform::MemoryInit()); - - // Initialize the CHIP stack. - ReturnErrorOnFailure(PlatformMgr().InitChipStack()); - - if (flags.Has(RendezvousInformationFlag::kBLE)) - { - ConnectivityMgr().SetBLEAdvertisingEnabled(true); - } - else if (flags.Has(RendezvousInformationFlag::kSoftAP)) - { - // TODO(cecille): Fix for the case where BLE and SoftAP are both enabled.` - ConnectivityMgr().SetBLEAdvertisingEnabled(false); - ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled); - } - - // Register a function to receive events from the CHIP device layer. Note that calls to - // this function will happen on the CHIP event loop thread, not the app_main thread. - PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast(cb)); - - // Start a task to run the CHIP Device event loop. - return PlatformMgr().StartEventLoopTask(); -} -} // namespace DeviceManager -} // namespace chip - -void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t mask, uint8_t type, - uint16_t size, uint8_t * value) -{ - chip::DeviceManager::CHIPDeviceManagerCallbacks * cb = - chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks(); - if (cb != nullptr) - { - cb->PostAttributeChangeCallback(attributePath.mEndpointId, attributePath.mClusterId, attributePath.mAttributeId, mask, type, - size, value); - } -} diff --git a/examples/light-switch-app/esp32/main/CMakeLists.txt b/examples/light-switch-app/esp32/main/CMakeLists.txt index 1e3ec645cca4ea..959a9bdb37f123 100644 --- a/examples/light-switch-app/esp32/main/CMakeLists.txt +++ b/examples/light-switch-app/esp32/main/CMakeLists.txt @@ -27,6 +27,7 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/light-switch-app/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" @@ -63,4 +64,4 @@ set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) target_compile_options(${COMPONENT_LIB} PRIVATE "-DLWIP_IPV6_SCOPES=0" "-DCHIP_HAVE_CONFIG_H") target_compile_options(${COMPONENT_LIB} PUBLIC "-DCHIP_ADDRESS_RESOLVE_IMPL_INCLUDE_HEADER=" -) \ No newline at end of file +) diff --git a/examples/light-switch-app/esp32/main/DeviceCallbacks.cpp b/examples/light-switch-app/esp32/main/DeviceCallbacks.cpp index 6858180c76d162..4e4c62e2fd45c9 100644 --- a/examples/light-switch-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/light-switch-app/esp32/main/DeviceCallbacks.cpp @@ -24,101 +24,17 @@ **/ #include "DeviceCallbacks.h" -#include "esp_bt.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 -#include -#include -#include -#include #include -#include -static const char * TAG = "light-switch-app-callbacks"; -constexpr uint32_t kInitOTARequestorDelaySec = 3; +static const char * TAG = "light-switch-app-callbacks"; using namespace chip; using namespace chip::Inet; using namespace chip::System; -using namespace chip::DeviceLayer; using namespace chip::app::Clusters; -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(heap_caps_get_free_size(MALLOC_CAP_8BIT))); -} - -void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId, uint8_t mask, - uint8_t type, uint16_t size, uint8_t * value) +void AppDeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId, + uint8_t mask, uint8_t type, uint16_t size, uint8_t * value) { ESP_LOGI(TAG, "PostAttributeChangeCallback - Cluster ID: '0x%04x', EndPoint ID: '0x%02x', Attribute ID: '0x%04x'", clusterId, endpointId, attributeId); @@ -136,58 +52,8 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster ESP_LOGI(TAG, "Current free heap: %u\n", static_cast(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, "Server ready at: %s:%d", event->InternetConnectivityChange.address, CHIP_PORT); - 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..."); - } - 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::OnOffSwitchConfigurationAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t type, - uint8_t * value, uint16_t size) +void AppDeviceCallbacks::OnOffSwitchConfigurationAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, + uint8_t type, uint8_t * value, uint16_t size) { ChipLogProgress(Zcl, "OnOff Switch Configuration attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u", ChipLogValueMEI(attributeId), type, *value, size); diff --git a/examples/light-switch-app/esp32/main/Kconfig.projbuild b/examples/light-switch-app/esp32/main/Kconfig.projbuild index 5984500891eb8b..9fbfa68ab1a7aa 100644 --- a/examples/light-switch-app/esp32/main/Kconfig.projbuild +++ b/examples/light-switch-app/esp32/main/Kconfig.projbuild @@ -72,9 +72,4 @@ menu "Demo" default 4 if RENDEZVOUS_MODE_THREAD default 8 if RENDEZVOUS_MODE_ETHERNET - 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 \ No newline at end of file +endmenu diff --git a/examples/light-switch-app/esp32/main/include/CHIPDeviceManager.h b/examples/light-switch-app/esp32/main/include/CHIPDeviceManager.h deleted file mode 100644 index edd8eda6ba27b1..00000000000000 --- a/examples/light-switch-app/esp32/main/include/CHIPDeviceManager.h +++ /dev/null @@ -1,123 +0,0 @@ -/* - * - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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. - */ - -/** - * @file - * This file contains definitions for the CHIP DeviceManager Interface - * - * This object will co-ordinate multiple activities such as - * initialisation, rendezvous, session mgmt and other such - * activities within the CHIP stack. This is a singleton object. - */ - -#pragma once - -#include -#include -#include - -#include - -#include -#include - -#include - -namespace chip { -namespace DeviceManager { - -/** - * @brief - * This class provides a skeleton for all the callback functions. The functions will be - * called by other objects within the CHIP stack for specific events. - * Applications interested in receiving specific callbacks can specialize this class and handle - * these events in their implementation of this class. - */ -class DLL_EXPORT CHIPDeviceManagerCallbacks -{ -public: - /** - * @brief - * Called when CHIP Device events (PublicEventTypes) are triggered. - * - * @param event ChipDeviceEvent that occurred - * @param arg arguments specific to the event, if any - */ - virtual void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - /** - * @brief - * Called after an attribute has been changed - * - * @param endpoint endpoint id - * @param clusterID cluster id - * @param attributeId attribute id that was changed - * @param mask mask of the attribute - * @param manufacturerCode manufacturer code - * @param type attribute type - * @param size size of the attribute - * @param value pointer to the new value - */ - virtual void PostAttributeChangeCallback(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId, - uint8_t mask, uint8_t type, uint16_t size, uint8_t * value) - {} - virtual ~CHIPDeviceManagerCallbacks() {} -}; - -/** - * @brief - * A common class that drives other components of the CHIP stack - */ -class DLL_EXPORT CHIPDeviceManager -{ -public: - CHIPDeviceManager(const CHIPDeviceManager &) = delete; - CHIPDeviceManager(const CHIPDeviceManager &&) = delete; - CHIPDeviceManager & operator=(const CHIPDeviceManager &) = delete; - - static CHIPDeviceManager & GetInstance() - { - static CHIPDeviceManager instance; - return instance; - } - - /** - * @brief - * Initialise CHIPDeviceManager - * - * @param cb Application's instance of the CHIPDeviceManagerCallbacks for consuming events - */ - CHIP_ERROR Init(CHIPDeviceManagerCallbacks * cb); - /** - * @brief - * Fetch a pointer to the registered CHIPDeviceManagerCallbacks object. - * - */ - CHIPDeviceManagerCallbacks * GetCHIPDeviceManagerCallbacks() { return mCB; } - - /** - * Use internally for registration of the ChipDeviceEvents - */ - static void CommonDeviceEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - -private: - CHIPDeviceManagerCallbacks * mCB = nullptr; - CHIPDeviceManager() {} -}; - -} // namespace DeviceManager -} // namespace chip diff --git a/examples/light-switch-app/esp32/main/include/DeviceCallbacks.h b/examples/light-switch-app/esp32/main/include/DeviceCallbacks.h index 660064b0906dfc..0e226f574feeb5 100644 --- a/examples/light-switch-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/light-switch-app/esp32/main/include/DeviceCallbacks.h @@ -25,20 +25,16 @@ #pragma once -#include "CHIPDeviceManager.h" -#include -#include +#include +#include -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 mask, 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 OnIdentifyPostAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value); void OnOffSwitchConfigurationAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t type, uint8_t * value, uint16_t size); diff --git a/examples/light-switch-app/esp32/main/main.cpp b/examples/light-switch-app/esp32/main/main.cpp index 44553d462a247b..0d76460e52c854 100644 --- a/examples/light-switch-app/esp32/main/main.cpp +++ b/examples/light-switch-app/esp32/main/main.cpp @@ -16,8 +16,9 @@ * limitations under the License. */ -#include "CHIPDeviceManager.h" #include "DeviceCallbacks.h" +#include +#include #include "AppTask.h" #include "BindingHandler.h" @@ -28,52 +29,23 @@ #include "freertos/task.h" #include "nvs_flash.h" #include "shell_extension/launch.h" -#include #include -#include -#include -#include -#include using namespace ::chip; -using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; -using namespace ::chip::DeviceLayer; static const char * TAG = "light-switch-app"; -static DeviceCallbacks EchoCallbacks; -namespace { -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI -app::Clusters::NetworkCommissioning::Instance - sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); -#endif -} // namespace +static AppDeviceCallbacks EchoCallbacks; static void InitServer(intptr_t context) { // Print QR Code URL PrintOnboardingCodes(chip::RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE)); - static chip::CommonCaseDeviceServerInitParams initParams; - (void) initParams.InitializeStaticResourcesBeforeServerInit(); - chip::Server::GetInstance().Init(initParams); - - // Initialize device attestation config - SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); + Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - sWiFiNetworkCommissioningInstance.Init(); -#endif -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD - if (chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned() && - (chip::Server::GetInstance().GetFabricTable().FabricCount() != 0)) - { - ESP_LOGI(TAG, "Thread has been provisioned, publish the dns service now"); - chip::app::DnssdServer::Instance().StartServer(); - } -#endif InitBindingHandler(); } diff --git a/examples/lighting-app/esp32/main/CHIPDeviceManager.cpp b/examples/lighting-app/esp32/main/CHIPDeviceManager.cpp deleted file mode 100644 index 6a801fd5a1fda7..00000000000000 --- a/examples/lighting-app/esp32/main/CHIPDeviceManager.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/* - * - * 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. - */ - -/** - * @file - * This file implements the CHIP Device Interface that is used by - * applications to interact with the CHIP stack - * - */ - -#include - -#include "CHIPDeviceManager.h" -#include -#include -#include -#include -#include -#include - -using namespace ::chip; - -namespace chip { - -namespace DeviceManager { - -using namespace ::chip::DeviceLayer; - -void CHIPDeviceManager::CommonDeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg) -{ - CHIPDeviceManagerCallbacks * cb = reinterpret_cast(arg); - if (cb != nullptr) - { - cb->DeviceEventCallback(event, reinterpret_cast(cb)); - } -} - -CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb) -{ - mCB = cb; - RendezvousInformationFlags flags = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE); - - ReturnErrorOnFailure(Platform::MemoryInit()); - - // Initialize the CHIP stack. - ReturnErrorOnFailure(PlatformMgr().InitChipStack()); - - if (flags.Has(RendezvousInformationFlag::kBLE)) - { - ConnectivityMgr().SetBLEAdvertisingEnabled(true); - } - else if (flags.Has(RendezvousInformationFlag::kSoftAP)) - { - // TODO(cecille): Fix for the case where BLE and SoftAP are both enabled.` - ConnectivityMgr().SetBLEAdvertisingEnabled(false); - ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled); - } - - // Register a function to receive events from the CHIP device layer. Note that calls to - // this function will happen on the CHIP event loop thread, not the app_main thread. - PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast(cb)); - - // Start a task to run the CHIP Device event loop. - return PlatformMgr().StartEventLoopTask(); -} -} // namespace DeviceManager -} // namespace chip - -void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, - uint8_t * value) -{ - chip::DeviceManager::CHIPDeviceManagerCallbacks * cb = - chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks(); - if (cb != nullptr) - { - cb->PostAttributeChangeCallback(attributePath.mEndpointId, attributePath.mClusterId, attributePath.mAttributeId, type, size, - value); - } -} diff --git a/examples/lighting-app/esp32/main/CMakeLists.txt b/examples/lighting-app/esp32/main/CMakeLists.txt index 6b26bfd4d08bc3..93ff9dc67217bd 100644 --- a/examples/lighting-app/esp32/main/CMakeLists.txt +++ b/examples/lighting-app/esp32/main/CMakeLists.txt @@ -28,8 +28,9 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/lighting-app/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lighting-app/lighting-common/src" "${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/shell_extension" + "${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" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/reporting" diff --git a/examples/lighting-app/esp32/main/DeviceCallbacks.cpp b/examples/lighting-app/esp32/main/DeviceCallbacks.cpp index eaffb0b8efee36..8183e4eb163fa5 100644 --- a/examples/lighting-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/lighting-app/esp32/main/DeviceCallbacks.cpp @@ -26,103 +26,17 @@ #include "DeviceCallbacks.h" #include "LEDWidget.h" -#include "esp_bt.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 -#include -#include -#include -#include -#include - -static const char * TAG = "light-app-callbacks"; -constexpr uint32_t kInitOTARequestorDelaySec = 3; +static const char * TAG = "light-app-callbacks"; extern LEDWidget AppLED; using namespace chip; using namespace chip::Inet; using namespace chip::System; -using namespace chip::DeviceLayer; using namespace chip::app::Clusters; -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(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: '0x%02x', Attribute ID: '0x%04x'", clusterId, endpointId, attributeId); @@ -151,57 +65,7 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster ESP_LOGI(TAG, "Current free heap: %u\n", static_cast(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..."); - 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..."); - } - 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 == OnOff::Attributes::OnOff::Id, ESP_LOGI(TAG, "Unhandled Attribute ID: '0x%04x", attributeId)); VerifyOrExit(endpointId == 1, ESP_LOGE(TAG, "Unexpected EndPoint ID: `0x%02x'", endpointId)); @@ -212,7 +76,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) { VerifyOrExit(attributeId == LevelControl::Attributes::CurrentLevel::Id, ESP_LOGI(TAG, "Unhandled Attribute ID: '0x%04x", attributeId)); @@ -226,7 +90,7 @@ void DeviceCallbacks::OnLevelControlAttributeChangeCallback(EndpointId endpointI // Currently ColorControl cluster is supported for ESP32C3_DEVKITM and ESP32S3_DEVKITM which have an on-board RGB-LED #if CONFIG_LED_TYPE_RMT -void DeviceCallbacks::OnColorControlAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value) +void AppDeviceCallbacks::OnColorControlAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value) { uint8_t hue, saturation; diff --git a/examples/lighting-app/esp32/main/Kconfig.projbuild b/examples/lighting-app/esp32/main/Kconfig.projbuild index d099a445f2123a..a13c76c28249b0 100644 --- a/examples/lighting-app/esp32/main/Kconfig.projbuild +++ b/examples/lighting-app/esp32/main/Kconfig.projbuild @@ -106,9 +106,4 @@ menu "Demo" default 4 if RENDEZVOUS_MODE_THREAD default 8 if RENDEZVOUS_MODE_ETHERNET - 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 diff --git a/examples/lighting-app/esp32/main/include/CHIPDeviceManager.h b/examples/lighting-app/esp32/main/include/CHIPDeviceManager.h deleted file mode 100644 index a1b85d28f741dd..00000000000000 --- a/examples/lighting-app/esp32/main/include/CHIPDeviceManager.h +++ /dev/null @@ -1,121 +0,0 @@ -/* - * - * 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. - */ - -/** - * @file - * This file contains definitions for the CHIP DeviceManager Interface - * - * This object will co-ordinate multiple activities such as - * initialisation, rendezvous, session mgmt and other such - * activities within the CHIP stack. This is a singleton object. - */ - -#pragma once - -#include -#include -#include - -#include - -#include -#include - -#include - -namespace chip { -namespace DeviceManager { - -/** - * @brief - * This class provides a skeleton for all the callback functions. The functions will be - * called by other objects within the CHIP stack for specific events. - * Applications interested in receiving specific callbacks can specialize this class and handle - * these events in their implementation of this class. - */ -class DLL_EXPORT CHIPDeviceManagerCallbacks -{ -public: - /** - * @brief - * Called when CHIP Device events (PublicEventTypes) are triggered. - * - * @param event ChipDeviceEvent that occurred - * @param arg arguments specific to the event, if any - */ - virtual void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - /** - * @brief - * Called after an attribute has been changed - * - * @param endpoint endpoint id - * @param clusterID cluster id - * @param attributeId attribute id that was changed - * @param manufacturerCode manufacturer code - * @param type attribute type - * @param size size of the attribute - * @param value pointer to the new value - */ - virtual void PostAttributeChangeCallback(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId, - uint8_t type, uint16_t size, uint8_t * value) - {} - virtual ~CHIPDeviceManagerCallbacks() {} -}; - -/** - * @brief - * A common class that drives other components of the CHIP stack - */ -class DLL_EXPORT CHIPDeviceManager -{ -public: - CHIPDeviceManager(const CHIPDeviceManager &) = delete; - CHIPDeviceManager(const CHIPDeviceManager &&) = delete; - CHIPDeviceManager & operator=(const CHIPDeviceManager &) = delete; - - static CHIPDeviceManager & GetInstance() - { - static CHIPDeviceManager instance; - return instance; - } - - /** - * @brief - * Initialise CHIPDeviceManager - * - * @param cb Application's instance of the CHIPDeviceManagerCallbacks for consuming events - */ - CHIP_ERROR Init(CHIPDeviceManagerCallbacks * cb); - /** - * @brief - * Fetch a pointer to the registered CHIPDeviceManagerCallbacks object. - * - */ - CHIPDeviceManagerCallbacks * GetCHIPDeviceManagerCallbacks() { return mCB; } - - /** - * Use internally for registration of the ChipDeviceEvents - */ - static void CommonDeviceEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - -private: - CHIPDeviceManagerCallbacks * mCB = nullptr; - CHIPDeviceManager() {} -}; - -} // namespace DeviceManager -} // namespace chip diff --git a/examples/lighting-app/esp32/main/include/DeviceCallbacks.h b/examples/lighting-app/esp32/main/include/DeviceCallbacks.h index c6fb5ef6f0e6c6..05933202b8c22a 100644 --- a/examples/lighting-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/lighting-app/esp32/main/include/DeviceCallbacks.h @@ -25,20 +25,18 @@ #pragma once -#include "CHIPDeviceManager.h" #include +#include +#include #include -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 OnIdentifyPostAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value); void OnOnOffPostAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value); void OnLevelControlAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value); diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index 838b77a1234d09..748ad7ea67588c 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -15,9 +15,10 @@ * limitations under the License. */ -#include "CHIPDeviceManager.h" #include "DeviceCallbacks.h" #include "LEDWidget.h" +#include +#include #include "esp_log.h" #include "esp_spi_flash.h" @@ -26,66 +27,24 @@ #include "freertos/task.h" #include "nvs_flash.h" #include "shell_extension/launch.h" -#include #include #include -#include -#include -#include -#include - -#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER -#include -#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER using namespace ::chip; -using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; -using namespace ::chip::DeviceLayer; LEDWidget AppLED; static const char * TAG = "light-app"; -static DeviceCallbacks EchoCallbacks; -namespace { -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI -app::Clusters::NetworkCommissioning::Instance - sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); -#endif - -#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER -ESP32FactoryDataProvider sFactoryDataProvider; -#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER -} // namespace +static AppDeviceCallbacks EchoCallbacks; static void InitServer(intptr_t context) { // Print QR Code URL PrintOnboardingCodes(chip::RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE)); - static chip::CommonCaseDeviceServerInitParams initParams; - (void) initParams.InitializeStaticResourcesBeforeServerInit(); - chip::Server::GetInstance().Init(initParams); - - // Initialize device attestation config -#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER - SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); -#else - SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); -#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER - -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI - sWiFiNetworkCommissioningInstance.Init(); -#endif -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD - if (chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned() && - (chip::Server::GetInstance().GetFabricTable().FabricCount() != 0)) - { - ESP_LOGI(TAG, "Thread has been provisioned, publish the dns service now"); - chip::app::DnssdServer::Instance().StartServer(); - } -#endif + Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config } extern "C" void app_main() diff --git a/examples/lock-app/esp32/main/CHIPDeviceManager.cpp b/examples/lock-app/esp32/main/CHIPDeviceManager.cpp deleted file mode 100644 index cda76136242f37..00000000000000 --- a/examples/lock-app/esp32/main/CHIPDeviceManager.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/* - * - * Copyright (c) 2020 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. - */ - -/** - * @file - * This file implements the CHIP Device Interface that is used by - * applications to interact with the CHIP stack - * - */ - -#include - -#include "CHIPDeviceManager.h" -#include -#include -#include -#include -#include -#include - -using namespace ::chip; - -namespace chip { - -namespace DeviceManager { - -using namespace ::chip::DeviceLayer; - -void CHIPDeviceManager::CommonDeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg) -{ - CHIPDeviceManagerCallbacks * cb = reinterpret_cast(arg); - if (cb != nullptr) - { - cb->DeviceEventCallback(event, reinterpret_cast(cb)); - } -} - -CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb) -{ - mCB = cb; - RendezvousInformationFlags flags = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE); - - ReturnErrorOnFailure(Platform::MemoryInit()); - - // Initialize the CHIP stack. - ReturnErrorOnFailure(PlatformMgr().InitChipStack()); - - if (flags.Has(RendezvousInformationFlag::kBLE)) - { - ConnectivityMgr().SetBLEAdvertisingEnabled(true); - } - else if (flags.Has(RendezvousInformationFlag::kSoftAP)) - { - // TODO(cecille): Fix for the case where BLE and SoftAP are both enabled.` - ConnectivityMgr().SetBLEAdvertisingEnabled(false); - ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled); - } - - // Register a function to receive events from the CHIP device layer. Note that calls to - // this function will happen on the CHIP event loop thread, not the app_main thread. - PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast(cb)); - - // Start a task to run the CHIP Device event loop. - return PlatformMgr().StartEventLoopTask(); -} -} // namespace DeviceManager -} // namespace chip - -void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size, - uint8_t * value) -{ - chip::DeviceManager::CHIPDeviceManagerCallbacks * cb = - chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks(); - if (cb != nullptr) - { - cb->PostAttributeChangeCallback(attributePath.mEndpointId, attributePath.mClusterId, attributePath.mAttributeId, type, size, - value); - } -} diff --git a/examples/lock-app/esp32/main/CMakeLists.txt b/examples/lock-app/esp32/main/CMakeLists.txt index f194e021bbfdf5..2b1b8a46ce7aef 100644 --- a/examples/lock-app/esp32/main/CMakeLists.txt +++ b/examples/lock-app/esp32/main/CMakeLists.txt @@ -31,7 +31,8 @@ idf_component_register(INCLUDE_DIRS SRC_DIRS "${CMAKE_CURRENT_LIST_DIR}" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/common/pigweed/esp32" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated" @@ -148,6 +149,7 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/lock-app/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/util" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/reporting" diff --git a/examples/lock-app/esp32/main/DeviceCallbacks.cpp b/examples/lock-app/esp32/main/DeviceCallbacks.cpp index 10a2c37d223889..a7e12754e7cb4d 100644 --- a/examples/lock-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/lock-app/esp32/main/DeviceCallbacks.cpp @@ -27,15 +27,8 @@ #include "AppConfig.h" #include "BoltLockManager.h" -#include "esp_check.h" -#include "esp_err.h" -#include "esp_heap_caps.h" -#include "esp_log.h" -#include "route_hook/esp_route_hook.h" #include #include -#include -#include static const char * TAG = "lock-devicecallbacks"; @@ -44,40 +37,8 @@ using namespace ::chip::Inet; using namespace ::chip::System; using namespace ::chip::DeviceLayer; -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::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: %d\n", 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: '0x%02x', Attribute ID: '0x%04x'", clusterId, endpointId, attributeId); @@ -96,37 +57,7 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster ESP_LOGI(TAG, "Current free heap: %d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT)); } -void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) -{ - if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) - { - ESP_LOGI(TAG, "IPv4 Server ready..."); - chip::app::DnssdServer::Instance().StartServer(); - } - else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) - { - ESP_LOGE(TAG, "Lost IPv4 connectivity..."); - } - if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established) - { - ESP_LOGI(TAG, "IPv6 Server ready..."); - chip::app::DnssdServer::Instance().StartServer(); - } - 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)); diff --git a/examples/lock-app/esp32/main/include/CHIPDeviceManager.h b/examples/lock-app/esp32/main/include/CHIPDeviceManager.h deleted file mode 100644 index e1ab643bc58ae0..00000000000000 --- a/examples/lock-app/esp32/main/include/CHIPDeviceManager.h +++ /dev/null @@ -1,124 +0,0 @@ -/* - * - * Copyright (c) 2020 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. - */ - -/** - * @file - * This file contains definitions for the CHIP DeviceManager Interface - * - * This object will co-ordinate multiple activities such as - * initialisation, rendezvous, session mgmt and other such - * activities within the CHIP stack. This is a singleton object. - */ - -#ifndef CHIP_LOCK_DEVICEMANAGER_H_ -#define CHIP_LOCK_DEVICEMANAGER_H_ - -#include -#include -#include - -#include - -#include -#include - -#include - -namespace chip { -namespace DeviceManager { - -/** - * @brief - * This class provides a skeleton for all the callback functions. The functions will be - * called by other objects within the CHIP stack for specific events. - * Applications interested in receiving specific callbacks can specialize this class and handle - * these events in their implementation of this class. - */ -class DLL_EXPORT CHIPDeviceManagerCallbacks -{ -public: - /** - * @brief - * Called when CHIP Device events (PublicEventTypes) are triggered. - * - * @param event ChipDeviceEvent that occurred - * @param arg arguments specific to the event, if any - */ - virtual void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - /** - * @brief - * Called after an attribute has been changed - * - * @param endpoint endpoint id - * @param clusterID cluster id - * @param attributeId attribute id that was changed - * @param manufacturerCode manufacturer code - * @param type attribute type - * @param size size of the attribute - * @param value pointer to the new value - */ - virtual void PostAttributeChangeCallback(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId, - uint8_t type, uint16_t size, uint8_t * value) - {} - virtual ~CHIPDeviceManagerCallbacks() {} -}; - -/** - * @brief - * A common class that drives other components of the CHIP stack - */ -class DLL_EXPORT CHIPDeviceManager -{ -public: - CHIPDeviceManager(const CHIPDeviceManager &) = delete; - CHIPDeviceManager(const CHIPDeviceManager &&) = delete; - CHIPDeviceManager & operator=(const CHIPDeviceManager &) = delete; - - static CHIPDeviceManager & GetInstance() - { - static CHIPDeviceManager instance; - return instance; - } - - /** - * @brief - * Initialise CHIPDeviceManager - * - * @param cb Application's instance of the CHIPDeviceManagerCallbacks for consuming events - */ - CHIP_ERROR Init(CHIPDeviceManagerCallbacks * cb); - /** - * @brief - * Fetch a pointer to the registered CHIPDeviceManagerCallbacks object. - * - */ - CHIPDeviceManagerCallbacks * GetCHIPDeviceManagerCallbacks() { return mCB; } - - /** - * Use internally for registration of the ChipDeviceEvents - */ - static void CommonDeviceEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - -private: - CHIPDeviceManagerCallbacks * mCB = nullptr; - CHIPDeviceManager() {} -}; - -} // namespace DeviceManager -} // namespace chip - -#endif /* CHIP_LOCK_DEVICEMANAGER_H_ */ diff --git a/examples/lock-app/esp32/main/include/DeviceCallbacks.h b/examples/lock-app/esp32/main/include/DeviceCallbacks.h index f8066f6ad7ff50..142a7c19739233 100644 --- a/examples/lock-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/lock-app/esp32/main/include/DeviceCallbacks.h @@ -26,20 +26,16 @@ #ifndef DEVICE_LOCK_CALLBACKS_H #define DEVICE_LOCK_CALLBACKS_H -#include "CHIPDeviceManager.h" -#include -#include +#include +#include -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 OnIdentifyPostAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value); }; diff --git a/examples/lock-app/esp32/main/main.cpp b/examples/lock-app/esp32/main/main.cpp index 20bb45062349e0..9ef90a02e1deab 100644 --- a/examples/lock-app/esp32/main/main.cpp +++ b/examples/lock-app/esp32/main/main.cpp @@ -16,7 +16,6 @@ */ #include "AppTask.h" -#include "CHIPDeviceManager.h" #include "DeviceCallbacks.h" #include "esp_heap_caps_init.h" #include "esp_log.h" @@ -28,12 +27,8 @@ #include "freertos/task.h" #include "nvs_flash.h" #include "shell_extension/launch.h" -#include -#include -#include - -#include -#include +#include +#include #include #include @@ -48,29 +43,15 @@ #endif using namespace ::chip; -using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; -using namespace ::chip::DeviceLayer; static const char * TAG = "lock-app"; -static DeviceCallbacks EchoCallbacks; - -namespace { -app::Clusters::NetworkCommissioning::Instance - sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); -} // namespace +static AppDeviceCallbacks EchoCallbacks; static void InitServer(intptr_t context) { - static chip::CommonCaseDeviceServerInitParams initParams; - (void) initParams.InitializeStaticResourcesBeforeServerInit(); - chip::Server::GetInstance().Init(initParams); - - // Initialize device attestation config - SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); - - sWiFiNetworkCommissioningInstance.Init(); + Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config ESP_LOGI(TAG, "------------------------Starting App Task---------------------------"); CHIP_ERROR error = GetAppTask().StartAppTask(); diff --git a/examples/ota-provider-app/esp32/main/CHIPDeviceManager.cpp b/examples/ota-provider-app/esp32/main/CHIPDeviceManager.cpp deleted file mode 100644 index 1dec61ef5c8d57..00000000000000 --- a/examples/ota-provider-app/esp32/main/CHIPDeviceManager.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * - * 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. - */ - -/** - * @file - * This file implements the CHIP Device Interface that is used by - * applications to interact with the CHIP stack - * - */ - -#include - -#include "CHIPDeviceManager.h" -#include -#include -#include -#include -#include -#include - -using namespace ::chip; - -namespace chip { - -namespace DeviceManager { - -using namespace ::chip::DeviceLayer; - -void CHIPDeviceManager::CommonDeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg) -{ - CHIPDeviceManagerCallbacks * cb = reinterpret_cast(arg); - if (cb != nullptr) - { - cb->DeviceEventCallback(event, reinterpret_cast(cb)); - } -} - -CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb) -{ - mCB = cb; - RendezvousInformationFlags flags = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE); - - ReturnErrorOnFailure(Platform::MemoryInit()); - - // Initialize the CHIP stack. - ReturnErrorOnFailure(PlatformMgr().InitChipStack()); - - if (flags.Has(RendezvousInformationFlag::kBLE)) - { - ConnectivityMgr().SetBLEAdvertisingEnabled(true); - } - else if (flags.Has(RendezvousInformationFlag::kSoftAP)) - { - // TODO(cecille): Fix for the case where BLE and SoftAP are both enabled.` - ConnectivityMgr().SetBLEAdvertisingEnabled(false); - ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled); - } - - // Register a function to receive events from the CHIP device layer. Note that calls to - // this function will happen on the CHIP event loop thread, not the app_main thread. - PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast(cb)); - - // Start a task to run the CHIP Device event loop. - return PlatformMgr().StartEventLoopTask(); -} -} // namespace DeviceManager -} // namespace chip - -void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & path, uint8_t type, uint16_t size, uint8_t * value) -{ - chip::DeviceManager::CHIPDeviceManagerCallbacks * cb = - chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks(); - if (cb != nullptr) - { - cb->PostAttributeChangeCallback(path.mEndpointId, path.mClusterId, path.mAttributeId, type, size, value); - } -} diff --git a/examples/ota-provider-app/esp32/main/CMakeLists.txt b/examples/ota-provider-app/esp32/main/CMakeLists.txt index d2d847bbcd29e9..15ec1edff9bb30 100644 --- a/examples/ota-provider-app/esp32/main/CMakeLists.txt +++ b/examples/ota-provider-app/esp32/main/CMakeLists.txt @@ -51,6 +51,8 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-provider" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/ota-provider-app/ota-provider-common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" EXCLUDE_SRCS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/ota-provider-app/ota-provider-common/BdxOtaSender.cpp" PRIV_REQUIRES chip QRCode bt console spiffs) diff --git a/examples/ota-provider-app/esp32/main/DeviceCallbacks.cpp b/examples/ota-provider-app/esp32/main/DeviceCallbacks.cpp index b4ed0159ebf89c..94edc5595f37ae 100644 --- a/examples/ota-provider-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/ota-provider-app/esp32/main/DeviceCallbacks.cpp @@ -24,48 +24,14 @@ **/ #include "DeviceCallbacks.h" -#include "esp_heap_caps.h" -#include "esp_log.h" -#include -#include -#include -#include - static const char * TAG = "echo-devicecallbacks"; using namespace ::chip; using namespace ::chip::Inet; using namespace ::chip::System; -using namespace ::chip::DeviceLayer; - -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::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(); - } - break; - } - ESP_LOGI(TAG, "Current free heap: %d\n", 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: '" ChipLogFormatMEI @@ -77,33 +43,3 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster ESP_LOGI(TAG, "Current free heap: %d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT)); } - -void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) -{ - if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) - { - ESP_LOGI(TAG, "IPv4 Server ready..."); - chip::app::DnssdServer::Instance().StartServer(); - } - else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) - { - ESP_LOGE(TAG, "Lost IPv4 connectivity..."); - } - if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established) - { - ESP_LOGI(TAG, "IPv6 Server ready..."); - chip::app::DnssdServer::Instance().StartServer(); - } - 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!"); - } -} diff --git a/examples/ota-provider-app/esp32/main/include/CHIPDeviceManager.h b/examples/ota-provider-app/esp32/main/include/CHIPDeviceManager.h deleted file mode 100644 index 6d8524733c3bfd..00000000000000 --- a/examples/ota-provider-app/esp32/main/include/CHIPDeviceManager.h +++ /dev/null @@ -1,126 +0,0 @@ -/* - * - * 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. - */ - -/** - * @file - * This file contains definitions for the CHIP DeviceManager Interface - * - * This object will co-ordinate multiple activities such as - * initialisation, rendezvous, session mgmt and other such - * activities within the CHIP stack. This is a singleton object. - */ - -#ifndef CHIP_DEVICEMANAGER_H_ -#define CHIP_DEVICEMANAGER_H_ - -#include -#include -#include - -#include - -#include -#include - -#include - -namespace chip { -namespace DeviceManager { - -/** - * @brief - * This class provides a skeleton for all the callback functions. The functions will be - * called by other objects within the CHIP stack for specific events. - * Applications interested in receiving specific callbacks can specialize this class and handle - * these events in their implementation of this class. - */ -class DLL_EXPORT CHIPDeviceManagerCallbacks -{ -public: - /** - * @brief - * Called when CHIP Device events (PublicEventTypes) are triggered. - * - * @param event ChipDeviceEvent that occurred - * @param arg arguments specific to the event, if any - */ - virtual void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - - /** - * @brief - * Called after an attribute has been changed - * - * @param endpoint endpoint id - * @param clusterID cluster id - * @param attributeId attribute id that was changed - * @param manufacturerCode manufacturer code - * @param type attribute type - * @param size size of the attribute - * @param value pointer to the new value - */ - virtual void PostAttributeChangeCallback(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId, - uint8_t type, uint16_t size, uint8_t * value) - {} - virtual ~CHIPDeviceManagerCallbacks() {} -}; - -/** - * @brief - * A common class that drives other components of the CHIP stack - */ -class DLL_EXPORT CHIPDeviceManager -{ -public: - CHIPDeviceManager(const CHIPDeviceManager &) = delete; - CHIPDeviceManager(const CHIPDeviceManager &&) = delete; - CHIPDeviceManager & operator=(const CHIPDeviceManager &) = delete; - - static CHIPDeviceManager & GetInstance() - { - static CHIPDeviceManager instance; - return instance; - } - - /** - * @brief - * Initialise CHIPDeviceManager - * - * @param cb Application's instance of the CHIPDeviceManagerCallbacks for consuming events - */ - CHIP_ERROR Init(CHIPDeviceManagerCallbacks * cb); - - /** - * @brief - * Fetch a pointer to the registered CHIPDeviceManagerCallbacks object. - * - */ - CHIPDeviceManagerCallbacks * GetCHIPDeviceManagerCallbacks() { return mCB; } - - /** - * Use internally for registration of the ChipDeviceEvents - */ - static void CommonDeviceEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - -private: - CHIPDeviceManagerCallbacks * mCB = nullptr; - CHIPDeviceManager() {} -}; - -} // namespace DeviceManager -} // namespace chip - -#endif /* CHIP_DEVICEMANAGER_H_ */ diff --git a/examples/ota-provider-app/esp32/main/include/DeviceCallbacks.h b/examples/ota-provider-app/esp32/main/include/DeviceCallbacks.h index 9993310d25e8fc..2a6445bfc81b3f 100644 --- a/examples/ota-provider-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/ota-provider-app/esp32/main/include/DeviceCallbacks.h @@ -26,20 +26,16 @@ #ifndef DEVICE_CALLBACKS_H #define DEVICE_CALLBACKS_H -#include "CHIPDeviceManager.h" -#include -#include +#include +#include -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 OnIdentifyPostAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value); }; diff --git a/examples/ota-provider-app/esp32/main/main.cpp b/examples/ota-provider-app/esp32/main/main.cpp index e4396b226f16f9..2ac9f0630a3c52 100644 --- a/examples/ota-provider-app/esp32/main/main.cpp +++ b/examples/ota-provider-app/esp32/main/main.cpp @@ -15,19 +15,16 @@ * limitations under the License. */ -#include "CHIPDeviceManager.h" #include "DeviceCallbacks.h" #include "esp_log.h" #include "esp_spi_flash.h" #include "esp_spiffs.h" #include "nvs_flash.h" -#include #include -#include -#include +#include +#include #include #include -#include #include #include @@ -39,9 +36,7 @@ using chip::Callback::Callback; using namespace chip; using namespace chip::Shell; using namespace chip::System; -using namespace chip::Credentials; using namespace chip::DeviceManager; -using namespace chip::DeviceLayer; using namespace chip::app::Clusters::OtaSoftwareUpdateProvider; CHIP_ERROR OnBlockQuery(void * context, chip::System::PacketBufferHandle & blockBuf, size_t & size, bool & isEof, uint32_t offset); @@ -51,7 +46,7 @@ void OnTransferFailed(void * context, BdxSenderErrorTypes status); namespace { const char * TAG = "ota-provider-app"; const uint8_t kMaxImagePathlen = 35; -static DeviceCallbacks EchoCallbacks; +static AppDeviceCallbacks EchoCallbacks; // TODO: this should probably be done dynamically constexpr chip::EndpointId kOtaProviderEndpoint = 0; @@ -65,19 +60,9 @@ chip::Callback::Callback onBlockQueryCallback(OnBlockQuery, nul chip::Callback::Callback onTransferCompleteCallback(OnTransferComplete, nullptr); chip::Callback::Callback onTransferFailedCallback(OnTransferFailed, nullptr); -app::Clusters::NetworkCommissioning::Instance - sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); - static void InitServer(intptr_t context) { - static chip::CommonCaseDeviceServerInitParams initParams; - (void) initParams.InitializeStaticResourcesBeforeServerInit(); - chip::Server::GetInstance().Init(initParams); - - // Initialize device attestation config - SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); - - sWiFiNetworkCommissioningInstance.Init(); + Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config BdxOtaSender * bdxOtaSender = otaProvider.GetBdxOtaSender(); VerifyOrReturn(bdxOtaSender != nullptr, ESP_LOGE(TAG, "bdxOtaSender is nullptr")); diff --git a/examples/ota-requestor-app/esp32/main/CHIPDeviceManager.cpp b/examples/ota-requestor-app/esp32/main/CHIPDeviceManager.cpp deleted file mode 100644 index 1dec61ef5c8d57..00000000000000 --- a/examples/ota-requestor-app/esp32/main/CHIPDeviceManager.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * - * 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. - */ - -/** - * @file - * This file implements the CHIP Device Interface that is used by - * applications to interact with the CHIP stack - * - */ - -#include - -#include "CHIPDeviceManager.h" -#include -#include -#include -#include -#include -#include - -using namespace ::chip; - -namespace chip { - -namespace DeviceManager { - -using namespace ::chip::DeviceLayer; - -void CHIPDeviceManager::CommonDeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg) -{ - CHIPDeviceManagerCallbacks * cb = reinterpret_cast(arg); - if (cb != nullptr) - { - cb->DeviceEventCallback(event, reinterpret_cast(cb)); - } -} - -CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb) -{ - mCB = cb; - RendezvousInformationFlags flags = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE); - - ReturnErrorOnFailure(Platform::MemoryInit()); - - // Initialize the CHIP stack. - ReturnErrorOnFailure(PlatformMgr().InitChipStack()); - - if (flags.Has(RendezvousInformationFlag::kBLE)) - { - ConnectivityMgr().SetBLEAdvertisingEnabled(true); - } - else if (flags.Has(RendezvousInformationFlag::kSoftAP)) - { - // TODO(cecille): Fix for the case where BLE and SoftAP are both enabled.` - ConnectivityMgr().SetBLEAdvertisingEnabled(false); - ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled); - } - - // Register a function to receive events from the CHIP device layer. Note that calls to - // this function will happen on the CHIP event loop thread, not the app_main thread. - PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast(cb)); - - // Start a task to run the CHIP Device event loop. - return PlatformMgr().StartEventLoopTask(); -} -} // namespace DeviceManager -} // namespace chip - -void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & path, uint8_t type, uint16_t size, uint8_t * value) -{ - chip::DeviceManager::CHIPDeviceManagerCallbacks * cb = - chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks(); - if (cb != nullptr) - { - cb->PostAttributeChangeCallback(path.mEndpointId, path.mClusterId, path.mAttributeId, type, size, value); - } -} diff --git a/examples/ota-requestor-app/esp32/main/CMakeLists.txt b/examples/ota-requestor-app/esp32/main/CMakeLists.txt index f89b111d538656..a21d42949bef0d 100644 --- a/examples/ota-requestor-app/esp32/main/CMakeLists.txt +++ b/examples/ota-requestor-app/esp32/main/CMakeLists.txt @@ -51,6 +51,8 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-requestor" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" PRIV_REQUIRES chip QRCode bt console app_update) set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 14) diff --git a/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp b/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp index c58596631b8f70..94edc5595f37ae 100644 --- a/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/ota-requestor-app/esp32/main/DeviceCallbacks.cpp @@ -24,50 +24,14 @@ **/ #include "DeviceCallbacks.h" -#include "esp_heap_caps.h" -#include "esp_log.h" -#include -#include -#include -#include -#include - -static const char * TAG = "echo-devicecallbacks"; -constexpr uint32_t kInitOTARequestorDelaySec = 3; +static const char * TAG = "echo-devicecallbacks"; using namespace ::chip; using namespace ::chip::Inet; using namespace ::chip::System; -using namespace ::chip::DeviceLayer; - -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::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(); - } - break; - } - ESP_LOGI(TAG, "Current free heap: %d\n", 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: '" ChipLogFormatMEI @@ -79,51 +43,3 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster ESP_LOGI(TAG, "Current free heap: %d\n", 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..."); - 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..."); - } - 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!"); - } -} diff --git a/examples/ota-requestor-app/esp32/main/include/CHIPDeviceManager.h b/examples/ota-requestor-app/esp32/main/include/CHIPDeviceManager.h deleted file mode 100644 index 6d8524733c3bfd..00000000000000 --- a/examples/ota-requestor-app/esp32/main/include/CHIPDeviceManager.h +++ /dev/null @@ -1,126 +0,0 @@ -/* - * - * 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. - */ - -/** - * @file - * This file contains definitions for the CHIP DeviceManager Interface - * - * This object will co-ordinate multiple activities such as - * initialisation, rendezvous, session mgmt and other such - * activities within the CHIP stack. This is a singleton object. - */ - -#ifndef CHIP_DEVICEMANAGER_H_ -#define CHIP_DEVICEMANAGER_H_ - -#include -#include -#include - -#include - -#include -#include - -#include - -namespace chip { -namespace DeviceManager { - -/** - * @brief - * This class provides a skeleton for all the callback functions. The functions will be - * called by other objects within the CHIP stack for specific events. - * Applications interested in receiving specific callbacks can specialize this class and handle - * these events in their implementation of this class. - */ -class DLL_EXPORT CHIPDeviceManagerCallbacks -{ -public: - /** - * @brief - * Called when CHIP Device events (PublicEventTypes) are triggered. - * - * @param event ChipDeviceEvent that occurred - * @param arg arguments specific to the event, if any - */ - virtual void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - - /** - * @brief - * Called after an attribute has been changed - * - * @param endpoint endpoint id - * @param clusterID cluster id - * @param attributeId attribute id that was changed - * @param manufacturerCode manufacturer code - * @param type attribute type - * @param size size of the attribute - * @param value pointer to the new value - */ - virtual void PostAttributeChangeCallback(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId, - uint8_t type, uint16_t size, uint8_t * value) - {} - virtual ~CHIPDeviceManagerCallbacks() {} -}; - -/** - * @brief - * A common class that drives other components of the CHIP stack - */ -class DLL_EXPORT CHIPDeviceManager -{ -public: - CHIPDeviceManager(const CHIPDeviceManager &) = delete; - CHIPDeviceManager(const CHIPDeviceManager &&) = delete; - CHIPDeviceManager & operator=(const CHIPDeviceManager &) = delete; - - static CHIPDeviceManager & GetInstance() - { - static CHIPDeviceManager instance; - return instance; - } - - /** - * @brief - * Initialise CHIPDeviceManager - * - * @param cb Application's instance of the CHIPDeviceManagerCallbacks for consuming events - */ - CHIP_ERROR Init(CHIPDeviceManagerCallbacks * cb); - - /** - * @brief - * Fetch a pointer to the registered CHIPDeviceManagerCallbacks object. - * - */ - CHIPDeviceManagerCallbacks * GetCHIPDeviceManagerCallbacks() { return mCB; } - - /** - * Use internally for registration of the ChipDeviceEvents - */ - static void CommonDeviceEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - -private: - CHIPDeviceManagerCallbacks * mCB = nullptr; - CHIPDeviceManager() {} -}; - -} // namespace DeviceManager -} // namespace chip - -#endif /* CHIP_DEVICEMANAGER_H_ */ diff --git a/examples/ota-requestor-app/esp32/main/include/DeviceCallbacks.h b/examples/ota-requestor-app/esp32/main/include/DeviceCallbacks.h index 9993310d25e8fc..2a6445bfc81b3f 100644 --- a/examples/ota-requestor-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/ota-requestor-app/esp32/main/include/DeviceCallbacks.h @@ -26,20 +26,16 @@ #ifndef DEVICE_CALLBACKS_H #define DEVICE_CALLBACKS_H -#include "CHIPDeviceManager.h" -#include -#include +#include +#include -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 OnIdentifyPostAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value); }; diff --git a/examples/ota-requestor-app/esp32/main/main.cpp b/examples/ota-requestor-app/esp32/main/main.cpp index 02adf0680655ae..4b04f6f499db23 100644 --- a/examples/ota-requestor-app/esp32/main/main.cpp +++ b/examples/ota-requestor-app/esp32/main/main.cpp @@ -15,7 +15,6 @@ * limitations under the License. */ -#include "CHIPDeviceManager.h" #include "DeviceCallbacks.h" #include "app/util/af-enums.h" #include "app/util/af.h" @@ -28,38 +27,22 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "nvs_flash.h" -#include -#include -#include - -#include -#include +#include +#include #include using namespace ::chip; using namespace ::chip::System; -using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; -using namespace ::chip::DeviceLayer; namespace { const char * TAG = "ota-requester-app"; -static DeviceCallbacks EchoCallbacks; - -app::Clusters::NetworkCommissioning::Instance - sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); +static AppDeviceCallbacks EchoCallbacks; static void InitServer(intptr_t context) { - static chip::CommonCaseDeviceServerInitParams initParams; - (void) initParams.InitializeStaticResourcesBeforeServerInit(); - chip::Server::GetInstance().Init(initParams); - - // Initialize device attestation config - SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); - - sWiFiNetworkCommissioningInstance.Init(); + Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config } } // namespace diff --git a/examples/all-clusters-app/esp32/main/CHIPDeviceManager.cpp b/examples/platform/esp32/common/CHIPDeviceManager.cpp similarity index 100% rename from examples/all-clusters-app/esp32/main/CHIPDeviceManager.cpp rename to examples/platform/esp32/common/CHIPDeviceManager.cpp diff --git a/examples/all-clusters-app/esp32/main/include/CHIPDeviceManager.h b/examples/platform/esp32/common/CHIPDeviceManager.h similarity index 100% rename from examples/all-clusters-app/esp32/main/include/CHIPDeviceManager.h rename to examples/platform/esp32/common/CHIPDeviceManager.h diff --git a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp new file mode 100644 index 00000000000000..d641fe7a9a6889 --- /dev/null +++ b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp @@ -0,0 +1,169 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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. + */ +#include "CommonDeviceCallbacks.h" + +#include "esp_bt.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 +#include +#include +#include +#include +#if CONFIG_ENABLE_OTA_REQUESTOR +#include +#endif + +static const char * TAG = "app-devicecallbacks"; + +using namespace chip; +using namespace chip::DeviceLayer; +using namespace chip::System; + +DeviceCallbacksDelegate * appDelegate = nullptr; + +void CommonDeviceCallbacks::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(heap_caps_get_free_size(MALLOC_CAP_8BIT))); +} + +void CommonDeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) +{ +#if CONFIG_ENABLE_OTA_REQUESTOR + static bool isOTAInitialized = false; +#endif + appDelegate = DeviceCallbacksDelegate::Instance().GetAppDelegate(); + if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) + { + ESP_LOGI(TAG, "IPv4 Server ready..."); + if (appDelegate != nullptr) + { + appDelegate->OnIPv4ConnectivityEstablished(); + } + chip::app::DnssdServer::Instance().StartServer(); +#if CONFIG_ENABLE_OTA_REQUESTOR + if (!isOTAInitialized) + { + OTAHelpers::Instance().InitOTARequestor(); + isOTAInitialized = true; + } +#endif + } + else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) + { + ESP_LOGE(TAG, "Lost IPv4 connectivity..."); + if (appDelegate != nullptr) + { + appDelegate->OnIPv4ConnectivityLost(); + } + } + if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established) + { + ESP_LOGI(TAG, "IPv6 Server ready..."); + chip::app::DnssdServer::Instance().StartServer(); + +#if CONFIG_ENABLE_OTA_REQUESTOR + if (!isOTAInitialized) + { + OTAHelpers::Instance().InitOTARequestor(); + isOTAInitialized = true; + } +#endif + } + else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost) + { + ESP_LOGE(TAG, "Lost IPv6 connectivity..."); + } +} + +void CommonDeviceCallbacks::OnSessionEstablished(const ChipDeviceEvent * event) +{ + if (event->SessionEstablished.IsCommissioner) + { + ESP_LOGI(TAG, "Commissioner detected!"); + } +} diff --git a/examples/platform/esp32/common/CommonDeviceCallbacks.h b/examples/platform/esp32/common/CommonDeviceCallbacks.h new file mode 100644 index 00000000000000..841f86bd764db4 --- /dev/null +++ b/examples/platform/esp32/common/CommonDeviceCallbacks.h @@ -0,0 +1,47 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 +#include +#include + +class CommonDeviceCallbacks : public chip::DeviceManager::CHIPDeviceManagerCallbacks +{ +public: + virtual void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); + +private: + void OnInternetConnectivityChange(const chip::DeviceLayer::ChipDeviceEvent * event); + void OnSessionEstablished(const chip::DeviceLayer::ChipDeviceEvent * event); +}; + +class DeviceCallbacksDelegate +{ +public: + static DeviceCallbacksDelegate & Instance() + { + static DeviceCallbacksDelegate instance; + return instance; + } + virtual void OnIPv4ConnectivityEstablished() {} + virtual void OnIPv4ConnectivityLost() {} + DeviceCallbacksDelegate * mDelegate = nullptr; + void SetAppDelegate(DeviceCallbacksDelegate * delegate) { mDelegate = delegate; } + DeviceCallbacksDelegate * GetAppDelegate() { return mDelegate; } +}; diff --git a/examples/platform/esp32/common/Esp32AppServer.cpp b/examples/platform/esp32/common/Esp32AppServer.cpp new file mode 100644 index 00000000000000..2d8e42dd603793 --- /dev/null +++ b/examples/platform/esp32/common/Esp32AppServer.cpp @@ -0,0 +1,74 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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. + */ + +#include "Esp32AppServer.h" +#include "CHIPDeviceManager.h" +#include +#include +#include +#include +#include + +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +#include +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + +using namespace chip; +using namespace chip::Credentials; +using namespace chip::DeviceLayer; +namespace { +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI +app::Clusters::NetworkCommissioning::Instance + sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); +#endif + +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +ESP32FactoryDataProvider sFactoryDataProvider; +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +} // namespace + +void Esp32AppServer::Init(AppDelegate * sAppDelegate) +{ + // Init ZCL Data Model and CHIP App Server + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + if (sAppDelegate != nullptr) + { + initParams.appDelegate = sAppDelegate; + } + chip::Server::GetInstance().Init(initParams); + + // Initialize device attestation config +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); +#else + SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + sWiFiNetworkCommissioningInstance.Init(); +#endif +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + if (chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned() && + (chip::Server::GetInstance().GetFabricTable().FabricCount() != 0)) + { + ESP_LOGI(TAG, "Thread has been provisioned, publish the dns service now"); + chip::app::DnssdServer::Instance().StartServer(); + } +#endif +} diff --git a/examples/platform/esp32/common/Esp32AppServer.h b/examples/platform/esp32/common/Esp32AppServer.h new file mode 100644 index 00000000000000..aa6fcc3974d1e1 --- /dev/null +++ b/examples/platform/esp32/common/Esp32AppServer.h @@ -0,0 +1,26 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 +#include + +namespace Esp32AppServer { +void Init(AppDelegate * context = nullptr); +} diff --git a/examples/platform/esp32/ota/OTAHelper.cpp b/examples/platform/esp32/ota/OTAHelper.cpp index d561a09ad815e6..bf7fdcfd850bab 100644 --- a/examples/platform/esp32/ota/OTAHelper.cpp +++ b/examples/platform/esp32/ota/OTAHelper.cpp @@ -22,29 +22,31 @@ #include #include #include +#include using namespace chip::DeviceLayer; using namespace chip; namespace { - -#if CONFIG_ENABLE_OTA_REQUESTOR DefaultOTARequestor gRequestorCore; DefaultOTARequestorStorage gRequestorStorage; DefaultOTARequestorDriver gRequestorUser; BDXDownloader gDownloader; OTAImageProcessorImpl gImageProcessor; -#endif } // namespace -void OTAHelpers::InitOTARequestor() -{ -#if CONFIG_ENABLE_OTA_REQUESTOR +static void InitOTARequestorHandler(System::Layer * systemLayer, void * appState) +{ SetRequestorInstance(&gRequestorCore); gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage()); gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, gRequestorUser, gDownloader); gImageProcessor.SetOTADownloader(&gDownloader); gDownloader.SetImageProcessorDelegate(&gImageProcessor); gRequestorUser.Init(&gRequestorCore, &gImageProcessor); -#endif +} + +void OTAHelpers::InitOTARequestor() +{ + chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec), InitOTARequestorHandler, + nullptr); } diff --git a/examples/platform/esp32/ota/OTAHelper.h b/examples/platform/esp32/ota/OTAHelper.h index 521c5b39532b6d..e80858b531e97a 100644 --- a/examples/platform/esp32/ota/OTAHelper.h +++ b/examples/platform/esp32/ota/OTAHelper.h @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include class OTAHelpers { public: @@ -23,5 +23,6 @@ class OTAHelpers static OTAHelpers sInitOTA; return sInitOTA; } + static constexpr uint32_t kInitOTARequestorDelaySec = 3; void InitOTARequestor(void); }; diff --git a/examples/temperature-measurement-app/esp32/main/CHIPDeviceManager.cpp b/examples/temperature-measurement-app/esp32/main/CHIPDeviceManager.cpp deleted file mode 100644 index 526b3cd3bea937..00000000000000 --- a/examples/temperature-measurement-app/esp32/main/CHIPDeviceManager.cpp +++ /dev/null @@ -1,94 +0,0 @@ -/* - * - * Copyright (c) 2020 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. - */ - -/** - * @file - * This file implements the CHIP Device Interface that is used by - * applications to interact with the CHIP stack - * - */ - -#include - -#include "CHIPDeviceManager.h" -#include -#include -#include -#include -#include -#include - -using namespace ::chip; - -namespace chip { - -namespace DeviceManager { - -using namespace ::chip::DeviceLayer; - -void CHIPDeviceManager::CommonDeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg) -{ - CHIPDeviceManagerCallbacks * cb = reinterpret_cast(arg); - if (cb != nullptr) - { - cb->DeviceEventCallback(event, reinterpret_cast(cb)); - } -} - -/** - * - */ -CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb) -{ - mCB = cb; - RendezvousInformationFlags flags = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE); - - ReturnErrorOnFailure(Platform::MemoryInit()); - - // Initialize the CHIP stack. - ReturnErrorOnFailure(PlatformMgr().InitChipStack()); - - if (flags.Has(RendezvousInformationFlag::kBLE)) - { - ConnectivityMgr().SetBLEAdvertisingEnabled(true); - } - else if (flags.Has(RendezvousInformationFlag::kSoftAP)) - { - // TODO(cecille): Fix for the case where BLE and SoftAP are both enabled.` - ConnectivityMgr().SetBLEAdvertisingEnabled(false); - ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled); - } - - // Register a function to receive events from the CHIP device layer. Note that calls to - // this function will happen on the CHIP event loop thread, not the app_main thread. - PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast(cb)); - - // Start a task to run the CHIP Device event loop. - return PlatformMgr().StartEventLoopTask(); -} -} // namespace DeviceManager -} // namespace chip - -void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & path, uint8_t type, uint16_t size, uint8_t * value) -{ - chip::DeviceManager::CHIPDeviceManagerCallbacks * cb = - chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks(); - if (cb != nullptr) - { - cb->PostAttributeChangeCallback(path.mEndpointId, path.mClusterId, path.mAttributeId, type, size, value); - } -} diff --git a/examples/temperature-measurement-app/esp32/main/CMakeLists.txt b/examples/temperature-measurement-app/esp32/main/CMakeLists.txt index 0a113c9508727d..1eca5dec454e18 100644 --- a/examples/temperature-measurement-app/esp32/main/CMakeLists.txt +++ b/examples/temperature-measurement-app/esp32/main/CMakeLists.txt @@ -50,6 +50,7 @@ set(SRC_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" ) set(PRIV_REQUIRES_LIST chip QRCode bt) diff --git a/examples/temperature-measurement-app/esp32/main/DeviceCallbacks.cpp b/examples/temperature-measurement-app/esp32/main/DeviceCallbacks.cpp index ea388f20a06b89..f5c05da05cb2a7 100644 --- a/examples/temperature-measurement-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/temperature-measurement-app/esp32/main/DeviceCallbacks.cpp @@ -24,56 +24,14 @@ **/ #include "DeviceCallbacks.h" -#include "esp_check.h" -#include "esp_err.h" -#include "esp_heap_caps.h" -#include "esp_log.h" -#include "route_hook/esp_route_hook.h" -#include -#include -#include -#include - static const char * TAG = "echo-devicecallbacks"; using namespace ::chip; using namespace ::chip::Inet; using namespace ::chip::System; -using namespace ::chip::DeviceLayer; - -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::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: %d\n", 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: '0x%02x', Attribute ID: '0x%04x'", clusterId, endpointId, attributeId); @@ -83,33 +41,3 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster ESP_LOGI(TAG, "Current free heap: %d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT)); } - -void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event) -{ - if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) - { - ESP_LOGI(TAG, "IPv4 Server ready..."); - chip::app::DnssdServer::Instance().StartServer(); - } - else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) - { - ESP_LOGE(TAG, "Lost IPv4 connectivity..."); - } - if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established) - { - ESP_LOGI(TAG, "IPv6 Server ready..."); - chip::app::DnssdServer::Instance().StartServer(); - } - 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!"); - } -} diff --git a/examples/temperature-measurement-app/esp32/main/include/CHIPDeviceManager.h b/examples/temperature-measurement-app/esp32/main/include/CHIPDeviceManager.h deleted file mode 100644 index 4d583571762391..00000000000000 --- a/examples/temperature-measurement-app/esp32/main/include/CHIPDeviceManager.h +++ /dev/null @@ -1,126 +0,0 @@ -/* - * - * Copyright (c) 2020 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. - */ - -/** - * @file - * This file contains definitions for the CHIP DeviceManager Interface - * - * This object will co-ordinate multiple activities such as - * initialisation, rendezvous, session mgmt and other such - * activities within the CHIP stack. This is a singleton object. - */ - -#ifndef CHIP_DEVICEMANAGER_H_ -#define CHIP_DEVICEMANAGER_H_ - -#include -#include -#include - -#include - -#include -#include - -#include - -namespace chip { -namespace DeviceManager { - -/** - * @brief - * This class provides a skeleton for all the callback functions. The functions will be - * called by other objects within the CHIP stack for specific events. - * Applications interested in receiving specific callbacks can specialize this class and handle - * these events in their implementation of this class. - */ -class DLL_EXPORT CHIPDeviceManagerCallbacks -{ -public: - /** - * @brief - * Called when CHIP Device events (PublicEventTypes) are triggered. - * - * @param event ChipDeviceEvent that occurred - * @param arg arguments specific to the event, if any - */ - virtual void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - - /** - * @brief - * Called after an attribute has been changed - * - * @param endpoint endpoint id - * @param clusterID cluster id - * @param attributeId attribute id that was changed - * @param manufacturerCode manufacturer code - * @param type attribute type - * @param size size of the attribute - * @param value pointer to the new value - */ - virtual void PostAttributeChangeCallback(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId, - uint8_t type, uint16_t size, uint8_t * value) - {} - virtual ~CHIPDeviceManagerCallbacks() {} -}; - -/** - * @brief - * A common class that drives other components of the CHIP stack - */ -class DLL_EXPORT CHIPDeviceManager -{ -public: - CHIPDeviceManager(const CHIPDeviceManager &) = delete; - CHIPDeviceManager(const CHIPDeviceManager &&) = delete; - CHIPDeviceManager & operator=(const CHIPDeviceManager &) = delete; - - static CHIPDeviceManager & GetInstance() - { - static CHIPDeviceManager instance; - return instance; - } - - /** - * @brief - * Initialise CHIPDeviceManager - * - * @param cb Application's instance of the CHIPDeviceManagerCallbacks for consuming events - */ - CHIP_ERROR Init(CHIPDeviceManagerCallbacks * cb); - - /** - * @brief - * Fetch a pointer to the registered CHIPDeviceManagerCallbacks object. - * - */ - CHIPDeviceManagerCallbacks * GetCHIPDeviceManagerCallbacks() { return mCB; } - - /** - * Use internally for registration of the ChipDeviceEvents - */ - static void CommonDeviceEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); - -private: - CHIPDeviceManagerCallbacks * mCB = nullptr; - CHIPDeviceManager() {} -}; - -} // namespace DeviceManager -} // namespace chip - -#endif /* CHIP_DEVICEMANAGER_H_ */ diff --git a/examples/temperature-measurement-app/esp32/main/include/DeviceCallbacks.h b/examples/temperature-measurement-app/esp32/main/include/DeviceCallbacks.h index 9993310d25e8fc..2a6445bfc81b3f 100644 --- a/examples/temperature-measurement-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/temperature-measurement-app/esp32/main/include/DeviceCallbacks.h @@ -26,20 +26,16 @@ #ifndef DEVICE_CALLBACKS_H #define DEVICE_CALLBACKS_H -#include "CHIPDeviceManager.h" -#include -#include +#include +#include -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 OnIdentifyPostAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value); }; diff --git a/examples/temperature-measurement-app/esp32/main/main.cpp b/examples/temperature-measurement-app/esp32/main/main.cpp index f48bd06fdfa28e..1d8ec2687ac23b 100644 --- a/examples/temperature-measurement-app/esp32/main/main.cpp +++ b/examples/temperature-measurement-app/esp32/main/main.cpp @@ -15,7 +15,6 @@ * limitations under the License. */ -#include "CHIPDeviceManager.h" #include "DeviceCallbacks.h" #include "esp_heap_caps_init.h" #include "esp_log.h" @@ -27,17 +26,14 @@ #include "freertos/task.h" #include "nvs_flash.h" #include +#include +#include #include #include #include #include -#include -#include -#include -#include - #include #if CONFIG_ENABLE_PW_RPC @@ -45,30 +41,17 @@ #endif using namespace ::chip; -using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; -using namespace ::chip::DeviceLayer; const char * TAG = "temperature-measurement-app"; -static DeviceCallbacks EchoCallbacks; - -namespace { - -app::Clusters::NetworkCommissioning::Instance - sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); +static AppDeviceCallbacks EchoCallbacks; static void InitServer(intptr_t context) { - static chip::CommonCaseDeviceServerInitParams initParams; - (void) initParams.InitializeStaticResourcesBeforeServerInit(); - chip::Server::GetInstance().Init(initParams); - - sWiFiNetworkCommissioningInstance.Init(); + Esp32AppServer::Init(); // Init ZCL Data Model and CHIP App Server AND Initialize device attestation config } -} // namespace - extern "C" void app_main() { #if CONFIG_ENABLE_PW_RPC @@ -107,7 +90,4 @@ extern "C" void app_main() } chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast(nullptr)); - - // Initialize device attestation config - SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); }