Skip to content

Commit 9387bfe

Browse files
committed
example: refactor bridge examples with virtual class
1 parent 9f9bddc commit 9387bfe

26 files changed

Lines changed: 908 additions & 539 deletions

examples/bridge_apps/blemesh_bridge/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ set(EXTRA_COMPONENT_DIRS
3737

3838
project(blemesh_bridge)
3939

40-
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++17;-Os;-DCHIP_HAVE_CONFIG_H;-fpermissive;-Wno-overloaded-virtual" APPEND)
40+
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++17;-Os;-DCHIP_HAVE_CONFIG_H" APPEND)
4141
idf_build_set_property(C_COMPILE_OPTIONS "-Os" APPEND)
4242
# For RISCV chips, project_include.cmake sets -Wno-format, but does not clear various
4343
# flags that depend on -Wformat
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
This example code is in the Public Domain (or CC0 licensed, at your option.)
3+
4+
Unless required by applicable law or agreed to in writing, this
5+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6+
CONDITIONS OF ANY KIND, either express or implied.
7+
*/
8+
9+
#include <esp_check.h>
10+
#include <esp_err.h>
11+
#include <esp_log.h>
12+
#include <esp_matter_mem.h>
13+
#include <nvs.h>
14+
15+
#include <lib/support/CodeUtils.h>
16+
#include <platform/ESP32/ScopedNvsHandle.h>
17+
18+
#include <app_blemesh_bridged_device.h>
19+
20+
#define TAG "BLE-Mesh Bridge"
21+
22+
esp_err_t app_blemesh_bridged_device_t::set_dev_addr(const void *addr_ctx)
23+
{
24+
if (!addr_ctx) {
25+
return ESP_ERR_INVALID_ARG;
26+
}
27+
if (m_dev_addr_ctx) {
28+
esp_matter_mem_free(m_dev_addr_ctx);
29+
}
30+
m_dev_addr_ctx = esp_matter_mem_calloc(1, sizeof(blemesh_device_addr_t));
31+
if (!m_dev_addr_ctx) {
32+
return ESP_ERR_NO_MEM;
33+
}
34+
memcpy(m_dev_addr_ctx, addr_ctx, sizeof(blemesh_device_addr_t));
35+
return ESP_OK;
36+
}
37+
38+
bool app_blemesh_bridged_device_t::check_dev_addr(const void *addr_ctx)
39+
{
40+
if (addr_ctx && m_dev_addr_ctx) {
41+
return memcmp(m_dev_addr_ctx, addr_ctx, sizeof(blemesh_device_addr_t)) == 0;
42+
}
43+
return false;
44+
}
45+
46+
esp_err_t app_blemesh_bridged_device_t::delete_dev_addr()
47+
{
48+
if (m_dev_addr_ctx) {
49+
esp_matter_mem_free(m_dev_addr_ctx);
50+
m_dev_addr_ctx = nullptr;
51+
}
52+
return ESP_OK;
53+
}
54+
55+
esp_err_t app_blemesh_bridged_device_t::store_dev_addr()
56+
{
57+
VerifyOrReturnValue(m_dev, ESP_ERR_INVALID_STATE);
58+
chip::DeviceLayer::Internal::ScopedNvsHandle scopedNvsHandle;
59+
CHIP_ERROR err = scopedNvsHandle.Open(ESP_MATTER_BRIDGE_NAMESPACE, NVS_READWRITE, CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME);
60+
if (err != CHIP_NO_ERROR) {
61+
ESP_LOGE(TAG, "Error opening partition %s namespace %s. Err: %s", CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME,
62+
ESP_MATTER_BRIDGE_NAMESPACE, err.AsString());
63+
return map_matter_error(err);
64+
}
65+
uint16_t endpoint_id = esp_matter::endpoint::get_id(m_dev->endpoint);
66+
ESP_RETURN_ON_ERROR(nvs_set_blob(scopedNvsHandle, esp_matter_bridge::nvs_key_allocator::endpoint_dev_addr(endpoint_id).KeyName(), m_dev_addr_ctx,
67+
sizeof(blemesh_device_addr_t)), TAG, "Error storing the device address");;
68+
ESP_RETURN_ON_ERROR(nvs_commit(scopedNvsHandle), TAG, "Error commit NVS");
69+
return ESP_OK;
70+
}
71+
72+
esp_err_t app_blemesh_bridged_device_t::restore_dev_addr()
73+
{
74+
VerifyOrReturnValue(m_dev, ESP_ERR_INVALID_STATE);
75+
chip::DeviceLayer::Internal::ScopedNvsHandle scopedNvsHandle;
76+
CHIP_ERROR err = scopedNvsHandle.Open(ESP_MATTER_BRIDGE_NAMESPACE, NVS_READONLY, CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME);
77+
if (err != CHIP_NO_ERROR) {
78+
ESP_LOGE(TAG, "Error opening partition %s namespace %s. Err: %s", CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME,
79+
ESP_MATTER_BRIDGE_NAMESPACE, err.AsString());
80+
return map_matter_error(err);
81+
}
82+
if (m_dev_addr_ctx) {
83+
esp_matter_mem_free(m_dev_addr_ctx);
84+
}
85+
m_dev_addr_ctx = esp_matter_mem_calloc(1, sizeof(blemesh_device_addr_t));
86+
if (m_dev_addr_ctx) {
87+
uint16_t endpoint_id = esp_matter::endpoint::get_id(m_dev->endpoint);
88+
size_t read_size = sizeof(blemesh_device_addr_t);
89+
ESP_RETURN_ON_ERROR(nvs_get_blob(scopedNvsHandle, esp_matter_bridge::nvs_key_allocator::endpoint_dev_addr(endpoint_id).KeyName(), m_dev_addr_ctx,
90+
&read_size), TAG, "Error reading the device address");
91+
return ESP_OK;
92+
}
93+
return ESP_ERR_NO_MEM;
94+
}
95+
96+
esp_err_t app_blemesh_bridged_device_t::erase_dev_addr()
97+
{
98+
VerifyOrReturnValue(m_dev, ESP_ERR_INVALID_STATE);
99+
chip::DeviceLayer::Internal::ScopedNvsHandle scopedNvsHandle;
100+
CHIP_ERROR err = scopedNvsHandle.Open(ESP_MATTER_BRIDGE_NAMESPACE, NVS_READWRITE, CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME);
101+
if (err != CHIP_NO_ERROR) {
102+
ESP_LOGE(TAG, "Error opening partition %s namespace %s. Err: %s", CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME,
103+
ESP_MATTER_BRIDGE_NAMESPACE, err.AsString());
104+
return map_matter_error(err);
105+
}
106+
uint16_t endpoint_id = esp_matter::endpoint::get_id(m_dev->endpoint);
107+
ESP_RETURN_ON_ERROR(nvs_erase_key(scopedNvsHandle, esp_matter_bridge::nvs_key_allocator::endpoint_dev_addr(endpoint_id).KeyName()),
108+
TAG, "Error erasing the device address");
109+
ESP_RETURN_ON_ERROR(nvs_commit(scopedNvsHandle), TAG, "Error commit NVS");
110+
return ESP_OK;
111+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
This example code is in the Public Domain (or CC0 licensed, at your option.)
3+
4+
Unless required by applicable law or agreed to in writing, this
5+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6+
CONDITIONS OF ANY KIND, either express or implied.
7+
*/
8+
9+
#pragma once
10+
11+
#include <app_bridged_device.h>
12+
13+
typedef struct blemesh_device_addr {
14+
uint16_t blemesh_addr;
15+
} blemesh_device_addr_t;
16+
17+
class app_blemesh_bridged_device_t : public app_bridged_device_t {
18+
public:
19+
esp_err_t set_dev_addr(const void *addr_ctx) override;
20+
21+
bool check_dev_addr(const void *addr_ctx) override;
22+
23+
esp_err_t delete_dev_addr() override;
24+
25+
esp_err_t store_dev_addr() override;
26+
27+
esp_err_t restore_dev_addr() override;
28+
29+
esp_err_t erase_dev_addr() override;
30+
};

examples/bridge_apps/blemesh_bridge/main/app_main.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "blemesh_bridge.h"
2121
#include "app_blemesh.h"
22+
#include "app_blemesh_bridged_device.h"
2223

2324
static const char *TAG = "app_main";
2425

@@ -117,6 +118,16 @@ esp_err_t create_bridge_devices(esp_matter::endpoint_t *ep, uint32_t device_type
117118
return err;
118119
}
119120

121+
static app_bridged_device_t *create_blemesh_bridged_device(node_t *node, uint16_t endpoint)
122+
{
123+
return chip::Platform::New<app_blemesh_bridged_device_t>();
124+
}
125+
126+
static void free_blemesh_bridged_device(app_bridged_device_t *device)
127+
{
128+
chip::Platform::Delete((app_blemesh_bridged_device_t *)device);
129+
}
130+
120131
extern "C" void app_main()
121132
{
122133
esp_err_t err = ESP_OK;
@@ -140,7 +151,7 @@ extern "C" void app_main()
140151
err = esp_matter::start(app_event_cb);
141152
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err));
142153

143-
err = app_bridge_initialize(node, create_bridge_devices);
154+
err = app_bridge_initialize(node, create_bridge_devices, create_blemesh_bridged_device, free_blemesh_bridged_device);
144155
ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to resume the bridged endpoints: %d", err));
145156

146157
#if CONFIG_ENABLE_CHIP_SHELL

examples/bridge_apps/blemesh_bridge/main/blemesh_bridge.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <app_bridged_device.h>
1818
#include <blemesh_bridge.h>
1919
#include <app_blemesh.h>
20+
#include "app_blemesh_bridged_device.h"
2021

2122
static const char *TAG = "blemesh_bridge";
2223

@@ -50,17 +51,17 @@ esp_err_t blemesh_bridge_match_bridged_onoff_light(uint8_t *composition_data, ui
5051
ESP_LOGI(TAG, "This is an expected device ...");
5152
node_t *node = node::get();
5253
ESP_RETURN_ON_FALSE(node, ESP_ERR_INVALID_STATE, TAG, "Could not find esp_matter node");
53-
if (app_bridge_get_device_by_blemesh_addr(blemesh_addr)) {
54-
ESP_LOGI(TAG, "Bridged node for 0x%04x bridged device on endpoint %d has been created", blemesh_addr,
55-
app_bridge_get_matter_endpointid_by_blemesh_addr(blemesh_addr));
54+
blemesh_device_addr_t dev_addr = {blemesh_addr};
55+
if (app_bridge_get_device(&dev_addr)) {
56+
ESP_LOGI(TAG, "Bridged node for 0x%04x bridged device on endpoint %u has been created", blemesh_addr,
57+
app_bridge_get_endpoint(&dev_addr));
5658
} else {
57-
app_bridged_device_t *bridged_device =
58-
app_bridge_create_bridged_device(node, aggregator_endpoint_id, ESP_MATTER_ON_OFF_LIGHT_DEVICE_TYPE_ID,
59-
ESP_MATTER_BRIDGED_DEVICE_TYPE_BLEMESH,
60-
app_bridge_blemesh_address(blemesh_addr), NULL);
61-
ESP_RETURN_ON_FALSE(bridged_device, ESP_FAIL, TAG, "Failed to create bridged device (on_off light)");
62-
ESP_LOGI(TAG, "Create/Update bridged node for 0x%04x bridged device on endpoint %d", blemesh_addr,
63-
app_bridge_get_matter_endpointid_by_blemesh_addr(blemesh_addr));
59+
esp_err_t err = app_bridge_create_new_device(
60+
node, aggregator_endpoint_id, ESP_MATTER_ON_OFF_LIGHT_DEVICE_TYPE_ID,
61+
&dev_addr, NULL);
62+
ESP_RETURN_ON_ERROR(err, TAG, "Failed to create bridged device (on_off light)");
63+
ESP_LOGI(TAG, "Create/Update bridged node for 0x%04x bridged device on endpoint %u", blemesh_addr,
64+
app_bridge_get_endpoint(&dev_addr));
6465
}
6566
} else {
6667
ESP_LOGW(TAG, "This isn't an unexpected device ...");
@@ -71,12 +72,14 @@ esp_err_t blemesh_bridge_match_bridged_onoff_light(uint8_t *composition_data, ui
7172
esp_err_t blemesh_bridge_attribute_update(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id,
7273
esp_matter_attr_val_t *val, app_bridged_device_t *bridged_device)
7374
{
74-
if (bridged_device && bridged_device->dev && bridged_device->dev->endpoint) {
75+
esp_matter_bridge::device_t *matter_device = bridged_device ? bridged_device->get_matter_device() : nullptr;
76+
if (matter_device && matter_device->endpoint) {
7577
if (cluster_id == OnOff::Id) {
7678
if (attribute_id == OnOff::Attributes::OnOff::Id) {
7779
ESP_LOGD(TAG, "Update Bridged Device, ep: 0x%x, cluster: 0x%lx, att: 0x%lx", endpoint_id, cluster_id,
7880
attribute_id);
79-
app_ble_mesh_onoff_set(bridged_device->dev_addr.blemesh_addr, val->val.b);
81+
blemesh_device_addr_t *dev_addr = (blemesh_device_addr_t *)bridged_device->get_dev_addr();
82+
app_ble_mesh_onoff_set(dev_addr->blemesh_addr, val->val.b);
8083
}
8184
}
8285
} else {

examples/bridge_apps/bridge_cli/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ set(EXTRA_COMPONENT_DIRS
3636

3737
project(bridge_cli)
3838

39-
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++17;-Os;-DCHIP_HAVE_CONFIG_H;-fpermissive;-Wno-overloaded-virtual" APPEND)
39+
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++17;-Os;-DCHIP_HAVE_CONFIG_H;-Wno-overloaded-virtual" APPEND)
4040
idf_build_set_property(C_COMPILE_OPTIONS "-Os" APPEND)
4141
# For RISCV chips, project_include.cmake sets -Wno-format, but does not clear various
4242
# flags that depend on -Wformat

examples/bridge_apps/esp-now_bridge_light/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ set(EXTRA_COMPONENT_DIRS
3737

3838
project(espnow_light)
3939

40-
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++17;-Os;-DCHIP_HAVE_CONFIG_H;-Wno-overloaded-virtual" APPEND)
40+
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++17;-Os;-DCHIP_HAVE_CONFIG_H" APPEND)
4141
idf_build_set_property(C_COMPILE_OPTIONS "-Os" APPEND)
4242
# For RISCV chips, project_include.cmake sets -Wno-format, but does not clear various
4343
# flags that depend on -Wformat

examples/bridge_apps/esp-now_bridge_light/main/app_espnow.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ static void espnow_ctrl_onoff(espnow_addr_t src_addr, bool status)
4343
} else {
4444
req_handle.command_path.mCommandId = OnOff::Commands::Off::Id;
4545
}
46-
uint16_t bridged_switch_endpoint_id = app_bridge_get_matter_endpointid_by_espnow_macaddr(src_addr);
47-
ESP_LOGI(TAG, "Using bridge endpoint: %d", bridged_switch_endpoint_id);
46+
47+
uint16_t bridged_switch_endpoint_id = app_bridge_get_endpoint(&src_addr);
48+
ESP_LOGI(TAG, "Using bridge endpoint: %u", bridged_switch_endpoint_id);
4849

4950
if (bridged_switch_endpoint_id != chip::kInvalidEndpointId) {
5051
lock::ScopedChipStackLock lock(portMAX_DELAY);
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
This example code is in the Public Domain (or CC0 licensed, at your option.)
3+
4+
Unless required by applicable law or agreed to in writing, this
5+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6+
CONDITIONS OF ANY KIND, either express or implied.
7+
*/
8+
9+
#include <esp_check.h>
10+
#include <esp_err.h>
11+
#include <esp_log.h>
12+
#include <esp_matter_mem.h>
13+
#include <nvs.h>
14+
15+
#include <lib/support/CodeUtils.h>
16+
#include <platform/ESP32/ScopedNvsHandle.h>
17+
18+
#include <app_espnow_bridged_device.h>
19+
20+
#define TAG "ESP-NOW Bridge"
21+
22+
esp_err_t app_espnow_bridged_device_t::set_dev_addr(const void *addr_ctx)
23+
{
24+
if (!addr_ctx) {
25+
return ESP_ERR_INVALID_ARG;
26+
}
27+
if (m_dev_addr_ctx) {
28+
esp_matter_mem_free(m_dev_addr_ctx);
29+
}
30+
m_dev_addr_ctx = esp_matter_mem_calloc(1, sizeof(espnow_device_addr_t));
31+
if (!m_dev_addr_ctx) {
32+
return ESP_ERR_NO_MEM;
33+
}
34+
memcpy(m_dev_addr_ctx, addr_ctx, sizeof(espnow_device_addr_t));
35+
return ESP_OK;
36+
}
37+
38+
bool app_espnow_bridged_device_t::check_dev_addr(const void *addr_ctx)
39+
{
40+
if (addr_ctx && m_dev_addr_ctx) {
41+
return memcmp(m_dev_addr_ctx, addr_ctx, sizeof(espnow_device_addr_t)) == 0;
42+
}
43+
return false;
44+
}
45+
46+
esp_err_t app_espnow_bridged_device_t::delete_dev_addr()
47+
{
48+
if (m_dev_addr_ctx) {
49+
esp_matter_mem_free(m_dev_addr_ctx);
50+
m_dev_addr_ctx = nullptr;
51+
}
52+
return ESP_OK;
53+
}
54+
55+
esp_err_t app_espnow_bridged_device_t::store_dev_addr()
56+
{
57+
VerifyOrReturnValue(m_dev, ESP_ERR_INVALID_STATE);
58+
chip::DeviceLayer::Internal::ScopedNvsHandle scopedNvsHandle;
59+
CHIP_ERROR err = scopedNvsHandle.Open(ESP_MATTER_BRIDGE_NAMESPACE, NVS_READWRITE, CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME);
60+
if (err != CHIP_NO_ERROR) {
61+
ESP_LOGE(TAG, "Error opening partition %s namespace %s. Err: %s", CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME,
62+
ESP_MATTER_BRIDGE_NAMESPACE, err.AsString());
63+
return map_matter_error(err);
64+
}
65+
uint16_t endpoint_id = esp_matter::endpoint::get_id(m_dev->endpoint);
66+
ESP_RETURN_ON_ERROR(nvs_set_blob(scopedNvsHandle, esp_matter_bridge::nvs_key_allocator::endpoint_dev_addr(endpoint_id).KeyName(), m_dev_addr_ctx,
67+
sizeof(espnow_device_addr_t)), TAG, "Error storing the device address");
68+
ESP_RETURN_ON_ERROR(nvs_commit(scopedNvsHandle), TAG, "Error committing NVS");
69+
return ESP_OK;
70+
}
71+
72+
esp_err_t app_espnow_bridged_device_t::restore_dev_addr()
73+
{
74+
VerifyOrReturnValue(m_dev, ESP_ERR_INVALID_STATE);
75+
chip::DeviceLayer::Internal::ScopedNvsHandle scopedNvsHandle;
76+
CHIP_ERROR err = scopedNvsHandle.Open(ESP_MATTER_BRIDGE_NAMESPACE, NVS_READONLY, CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME);
77+
if (err != CHIP_NO_ERROR) {
78+
ESP_LOGE(TAG, "Error opening partition %s namespace %s. Err: %s", CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME,
79+
ESP_MATTER_BRIDGE_NAMESPACE, err.AsString());
80+
return map_matter_error(err);
81+
}
82+
if (m_dev_addr_ctx) {
83+
esp_matter_mem_free(m_dev_addr_ctx);
84+
}
85+
m_dev_addr_ctx = esp_matter_mem_calloc(1, sizeof(espnow_device_addr_t));
86+
if (m_dev_addr_ctx) {
87+
uint16_t endpoint_id = esp_matter::endpoint::get_id(m_dev->endpoint);
88+
size_t read_size = sizeof(espnow_device_addr_t);
89+
ESP_RETURN_ON_ERROR(nvs_get_blob(scopedNvsHandle, esp_matter_bridge::nvs_key_allocator::endpoint_dev_addr(endpoint_id).KeyName(), m_dev_addr_ctx,
90+
&read_size), TAG, "Error reading the device address");
91+
return ESP_OK;
92+
}
93+
return ESP_ERR_NO_MEM;
94+
}
95+
96+
esp_err_t app_espnow_bridged_device_t::erase_dev_addr()
97+
{
98+
VerifyOrReturnValue(m_dev, ESP_ERR_INVALID_STATE);
99+
chip::DeviceLayer::Internal::ScopedNvsHandle scopedNvsHandle;
100+
CHIP_ERROR err = scopedNvsHandle.Open(ESP_MATTER_BRIDGE_NAMESPACE, NVS_READWRITE, CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME);
101+
if (err != CHIP_NO_ERROR) {
102+
ESP_LOGE(TAG, "Error opening partition %s namespace %s. Err: %s", CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME,
103+
ESP_MATTER_BRIDGE_NAMESPACE, err.AsString());
104+
return map_matter_error(err);
105+
}
106+
uint16_t endpoint_id = esp_matter::endpoint::get_id(m_dev->endpoint);
107+
ESP_RETURN_ON_ERROR(nvs_erase_key(scopedNvsHandle, esp_matter_bridge::nvs_key_allocator::endpoint_dev_addr(endpoint_id).KeyName()),
108+
TAG, "Error erasing the device address");
109+
ESP_RETURN_ON_ERROR(nvs_commit(scopedNvsHandle), TAG, "Error committing NVS");
110+
return ESP_OK;
111+
}

0 commit comments

Comments
 (0)