Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32: Add bridge app example to ESP32 #8368

Merged
merged 1 commit into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/examples-esp32.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ jobs:
mkdir -p example_binaries/$BUILD_TYPE-build
cp examples/lock-app/esp32/build/chip-lock-app.elf \
example_binaries/$BUILD_TYPE-build/chip-lock-app.elf
- name: Build example Bridge App
timeout-minutes: 5
run: scripts/examples/esp_example.sh bridge-app
- name: Copy aside build products
run: |
mkdir -p example_binaries/$BUILD_TYPE-build
cp examples/bridge-app/esp32/build/chip-bridge-app.elf \
example_binaries/$BUILD_TYPE-build/chip-bridge-app.elf
- name: Build example Persistent Storage App
timeout-minutes: 5
run: scripts/examples/esp_example.sh persistent-storage sdkconfig.defaults
Expand Down
27 changes: 27 additions & 0 deletions examples/bridge-app/esp32/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Copyright (c) 2021 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.

cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)

set(EXTRA_COMPONENT_DIRS
"${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/config/esp32/components"
"${CMAKE_CURRENT_LIST_DIR}/../../common/QRCode"
)

project(chip-bridge-app)
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++14;-Os;-DLWIP_IPV6_SCOPES=0;-DCHIP_HAVE_CONFIG_H;-DDYNAMIC_ENDPOINT_COUNT=16" APPEND)
idf_build_set_property(C_COMPILE_OPTIONS "-Os;-DLWIP_IPV6_SCOPES=0" APPEND)
111 changes: 111 additions & 0 deletions examples/bridge-app/esp32/main/CHIPDeviceManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
*
* 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 <stdlib.h>

#include "CHIPDeviceManager.h"
#include <app/util/basic-types.h>
#include <setup_payload/SetupPayload.h>
#include <support/CHIPMem.h>
#include <support/CodeUtils.h>
#include <support/ErrorStr.h>

using namespace ::chip;

namespace chip {

namespace DeviceManager {

using namespace ::chip::DeviceLayer;

void CHIPDeviceManager::CommonDeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg)
{
CHIPDeviceManagerCallbacks * cb = reinterpret_cast<CHIPDeviceManagerCallbacks *>(arg);
if (cb != nullptr)
{
cb->DeviceEventCallback(event, reinterpret_cast<intptr_t>(cb));
}
}

CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb)
{
CHIP_ERROR err;
mCB = cb;
RendezvousInformationFlags flags = RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE);

// Initialize the CHIP stack.
err = PlatformMgr().InitChipStack();
if (err != CHIP_NO_ERROR)
{
return err;
}

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);
}
else
{
// If rendezvous is bypassed, enable SoftAP so that the device can still
// be communicated with via its SoftAP as needed.
ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled);
}

err = Platform::MemoryInit();
if (err != CHIP_NO_ERROR)
{
return err;
}

// 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<intptr_t>(cb));

// Start a task to run the CHIP Device event loop.
err = PlatformMgr().StartEventLoopTask();
if (err != CHIP_NO_ERROR)
{
return err;
}
return CHIP_NO_ERROR;
}
} // namespace DeviceManager
} // namespace chip

void emberAfPostAttributeChangeCallback(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId, uint8_t mask,
uint16_t manufacturerCode, uint8_t type, uint16_t size, uint8_t * value)
{
chip::DeviceManager::CHIPDeviceManagerCallbacks * cb =
chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks();
if (cb != nullptr)
{
cb->PostAttributeChangeCallback(endpointId, clusterId, attributeId, mask, manufacturerCode, type, size, value);
}
}
40 changes: 40 additions & 0 deletions examples/bridge-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright (c) 2021 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.

idf_component_register(PRIV_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/bridge-app/bridge-common"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32"
"${CMAKE_CURRENT_LIST_DIR}/include"
SRC_DIRS
"${CMAKE_CURRENT_LIST_DIR}"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/bridge-app/bridge-common/gen"
"${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"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/basic"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/level-control"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/diagnostic-logs-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/descriptor"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/on-off-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server"
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-commissioning-server"
PRIV_REQUIRES chip QRCode bt)

set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 14)
target_compile_options(${COMPONENT_LIB} PRIVATE "-DLWIP_IPV6_SCOPES=0" "-DCHIP_HAVE_CONFIG_H")

121 changes: 121 additions & 0 deletions examples/bridge-app/esp32/main/Device.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
*
* 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.
*/

#include "Device.h"

#include <cstdio>
#include <platform/CHIPDeviceLayer.h>
#include <support/CHIPMemString.h>

using namespace ::chip::Platform;

Device::Device(const char * szDeviceName, const char * szLocation)
{
CopyString(mName, sizeof(mName), szDeviceName);
CopyString(mLocation, sizeof(mLocation), szLocation);
mState = kState_Off;
mReachable = false;
mEndpointId = 0;
mChanged_CB = nullptr;
}

bool Device::IsOn() const
{
return mState == kState_On;
}

bool Device::IsReachable() const
{
return mReachable;
}

void Device::SetOnOff(bool aOn)
{
bool changed;

if (aOn)
{
changed = (mState != kState_On);
mState = kState_On;
ChipLogProgress(DeviceLayer, "Device[%s]: ON", mName);
}
else
{
changed = (mState != kState_Off);
mState = kState_Off;
ChipLogProgress(DeviceLayer, "Device[%s]: OFF", mName);
}

if (changed && mChanged_CB)
{
mChanged_CB(this, kChanged_State);
}
}

void Device::SetReachable(bool aReachable)
{
bool changed = (mReachable != aReachable);

mReachable = aReachable;

if (aReachable)
{
ChipLogProgress(DeviceLayer, "Device[%s]: ONLINE", mName);
}
else
{
ChipLogProgress(DeviceLayer, "Device[%s]: OFFLINE", mName);
}

if (changed && mChanged_CB)
{
mChanged_CB(this, kChanged_Reachable);
}
}

void Device::SetName(const char * szName)
{
bool changed = (strncmp(mName, szName, sizeof(mName)) != 0);

ChipLogProgress(DeviceLayer, "Device[%s]: New Name=\"%s\"", mName, szName);

CopyString(mName, sizeof(mName), szName);

if (changed && mChanged_CB)
{
mChanged_CB(this, kChanged_Name);
}
}

void Device::SetLocation(const char * szLocation)
{
bool changed = (strncmp(mLocation, szLocation, sizeof(mLocation)) != 0);

CopyString(mLocation, sizeof(mLocation), szLocation);

ChipLogProgress(DeviceLayer, "Device[%s]: Location=\"%s\"", mName, mLocation);

if (changed && mChanged_CB)
{
mChanged_CB(this, kChanged_Location);
}
}

void Device::SetChangeCallback(DeviceCallback_fn aChanged_CB)
{
mChanged_CB = aChanged_CB;
}
Loading