Skip to content

Commit

Permalink
Merge branch 'matter_rm/fix_api_naming' into 'master'
Browse files Browse the repository at this point in the history
examples/matter:Moved app_matter api into a component

See merge request app-frameworks/esp-rainmaker!457
  • Loading branch information
dhrishi committed Jul 29, 2024
2 parents 095e028 + ec04f94 commit fd78129
Show file tree
Hide file tree
Showing 32 changed files with 305 additions and 300 deletions.
9 changes: 5 additions & 4 deletions examples/matter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ $ esp-matter-mfg-tool -v 0x131B -p 0x2 -cd $RMAKER_PATH/examples/matter/mfg/cd_1

This not only generates the factory nvs binary required for matter, but also embeds the RainMaker MQTT Host url into it via the master.csv file. Optionally, you can embed the MQTT host into the firmware itself by using `idf.py menuconfig -> ESP RainMaker Config -> ESP_RMAKER_READ_MQTT_HOST_FROM_CONFIG` and then skipping the --csv and --mcsv options to mfg_tool

The factory binary generated above should be flashed onto the fctry partition (default : `0x3f8000` for ESP32-C6 and `0x3e0000` for other chips. Do check your partition table for exact address).
The factory binary generated above should be flashed onto the fctry partition (default : `0x3f9000` for ESP32-C6 and `0x3e0000` for other chips. Do check your partition table for exact address).

```
$ esptool.py write_flash 0x3e0000 out/131b_2/<node-id>/<node-id>_esp_secure_cert.bin 0x3e0000 out/131b_2/<node-id>/<node-id>-partition.bin
$ esptool.py write_flash 0x3e0000 out/131b_2/<node-id>/<node-id>-partition.bin
```

## Building the example
Expand All @@ -71,8 +71,9 @@ $ idf.py flash monitor
The QR Code required for commissioning your device can be found at `${ESP_MATTER_PATH}/tools/mfg_tool/out/<vendor-id>_<product-id>/<node-id>/<node-id>-qrcode.png`


## Manufacturing Considerations [This step is only suggested for Privately deployed Production set up and not required for test set up)]
## Manufacturing Considerations

This step is only suggested for Privately deployed Production and not required for test set up.

### RainMaker MQTT Host

Expand Down Expand Up @@ -106,7 +107,7 @@ $ esp-matter-mfg-tool --dac-in-secure-cert -v 0xFFF2 -p 0x8001 --pai -k $ESP_MAT

Note the path where the files are generated after running the above command since it will be required later.

## Configure your app
### Configure your app
Open the project configuration menu using -

```
Expand Down
7 changes: 7 additions & 0 deletions examples/matter/common/app_matter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
list(APPEND SRCS_LIST "app_matter.cpp")
list(APPEND INCLUDE_DIRS_LIST "include")

idf_component_register(SRCS ${SRCS_LIST}
INCLUDE_DIRS ${INCLUDE_DIRS_LIST}
REQUIRES esp_matter esp_matter_rainmaker
)
62 changes: 62 additions & 0 deletions examples/matter/common/app_matter/app_matter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/

#include <esp_log.h>
#include<esp_matter.h>
#include <esp_matter_rainmaker.h>
#include <esp_matter_console.h>

using namespace esp_matter;

static const char *TAG = "app_matter";

esp_err_t app_matter_init(attribute::callback_t app_attribute_update_cb, identification::callback_t app_identification_cb)
{
/* Create a Matter node */
node::config_t node_config;
node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb);

/* The node and endpoint handles can be used to create/add other endpoints and clusters. */
if (!node) {
ESP_LOGE(TAG, "Matter node creation failed");
return ESP_FAIL;
}

return ESP_OK;
}

esp_err_t app_matter_start(event_callback_t app_event_cb)
{
esp_err_t err = esp_matter::start(app_event_cb);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Matter start failed: %d", err);
}
return err;
}

esp_err_t app_matter_rmaker_init()
{
/* Add custom rainmaker cluster */
return rainmaker::init();
}

esp_err_t app_matter_rmaker_start()
{
/* Other initializations for custom rainmaker cluster */
return rainmaker::start();
}

void app_matter_enable_matter_console()
{
#if CONFIG_ENABLE_CHIP_SHELL
esp_matter::console::diagnostics_register_commands();
esp_matter::console::init();
#else
ESP_LOGI(TAG, "Set CONFIG_ENABLE_CHIP_SHELL to enable Matter Console");
#endif
}
18 changes: 18 additions & 0 deletions examples/matter/common/app_matter/include/app_matter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/

#pragma once

#include <esp_err.h>
#include <esp_matter.h>

esp_err_t app_matter_init(esp_matter::attribute::callback_t app_attribute_update_cb, esp_matter::identification::callback_t app_identification_cb);
esp_err_t app_matter_start(esp_matter::event_callback_t app_event_cb);
esp_err_t app_matter_rmaker_init();
esp_err_t app_matter_rmaker_start();
void app_matter_enable_matter_console();
2 changes: 1 addition & 1 deletion examples/matter/matter_controller/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
set(ldfragments linker.lf)
idf_component_register(SRCS ./app_main.cpp ./app_matter.cpp ./app_driver.cpp
idf_component_register(SRCS ./app_main.cpp ./app_matter_controller.cpp ./app_driver.cpp
PRIV_INCLUDE_DIRS "."
LDFRAGMENTS "${ldfragments}")

Expand Down
2 changes: 1 addition & 1 deletion examples/matter/matter_controller/main/app_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <stdlib.h>
#include <string.h>

#include <app_matter.h>
#include <app_priv.h>
#include <device.h>
#include <esp_matter.h>
Expand All @@ -19,6 +18,7 @@
#include <esp_rmaker_core.h>
#include <esp_rmaker_standard_params.h>
#include <matter_controller_device_mgr.h>
#include <app_matter_controller.h>

using namespace esp_matter;
using namespace chip::app::Clusters;
Expand Down
45 changes: 39 additions & 6 deletions examples/matter/matter_controller/main/app_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@
#include <esp_rmaker_scenes.h>
#include <app_insights.h>
#include <app_reset.h>

#if CONFIG_OPENTHREAD_BORDER_ROUTER
#include <esp_matter_thread_br_cluster.h>
#include <esp_matter_thread_br_console.h>
#include <esp_matter_thread_br_launcher.h>
#include <esp_ot_config.h>
#endif // CONFIG_OPENTHREAD_BORDER_ROUTER
#include <app_priv.h>
#include <app_matter.h>
#include <app_matter_controller.h>
#include <esp_matter_controller_console.h>
#include <matter_controller_cluster.h>

static const char *TAG = "app_main";

using namespace esp_matter;

bool rmaker_init_done = false;

/* Callback to handle commands received from the RainMaker cloud */
Expand Down Expand Up @@ -57,14 +67,30 @@ extern "C" void app_main()
app_driver_handle_t button_handle = app_driver_button_init(NULL);
app_reset_button_register(button_handle);

/* Initialize matter */
/* Initialize Matter */
app_matter_init(app_attribute_update_cb, app_identification_cb);

// The data model for Matter Controller is the same as normal Matter end-devices. We should create root_node
// endpoint for the controller to support network commissioning and other basic features of Matter.
app_matter_init();
/* Add custom matter controller cluster */
node_t *matter_node = node::get();
if (!matter_node) {
ESP_LOGE(TAG, "Matter node creation failed");
abort();
}
endpoint_t *root_endpoint = esp_matter::endpoint::get(matter_node, 0);
esp_matter::cluster::matter_controller::create(root_endpoint, CLUSTER_FLAG_SERVER);

#if CONFIG_OPENTHREAD_BORDER_ROUTER
cluster::thread_br::create(root_endpoint, CLUSTER_FLAG_SERVER);
#endif

app_matter_rmaker_init();

app_matter_endpoint_create();

/* Matter start */
app_matter_start();
app_matter_start(app_event_cb);

/* Initialize the ESP RainMaker Agent.
*/
Expand Down Expand Up @@ -109,12 +135,19 @@ extern "C" void app_main()
app_insights_enable();

/* Pre start */
ESP_ERROR_CHECK(app_matter_pre_rainmaker_start());
ESP_ERROR_CHECK(app_matter_rmaker_start());

/* Start the ESP RainMaker Agent */
esp_rmaker_start();
rmaker_init_done = true;

/* Enable Matter diagnostics console*/
/* Enable Matter console*/
app_matter_enable_matter_console();
#if CONFIG_ESP_MATTER_CONTROLLER_ENABLE
esp_matter::console::controller_register_commands();
#endif
#if CONFIG_OPENTHREAD_BORDER_ROUTER && CONFIG_OPENTHREAD_CLI
esp_matter::console::thread_br_cli_register_command();
#endif // CONFIG_OPENTHREAD_BORDER_ROUTER && CONFIG_OPENTHREAD_CLI

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <platform/ESP32/route_hook/ESP32RouteHook.h>
#include <esp_matter_console.h>
#include <app_matter.h>
#include <app_matter_controller.h>
#include <esp_rmaker_standard_params.h>
#include <esp_rmaker_core.h>
#include <app_priv.h>
Expand Down Expand Up @@ -61,14 +62,14 @@ static esp_rmaker_param_val_t app_matter_get_rmaker_val(esp_matter_attr_val_t *v
return esp_rmaker_int(0);
}

static esp_err_t app_identification_cb(identification::callback_type_t type, uint16_t endpoint_id, uint8_t effect_id,
esp_err_t app_identification_cb(identification::callback_type_t type, uint16_t endpoint_id, uint8_t effect_id,
uint8_t effect_variant, void *priv_data)
{
ESP_LOGI(TAG, "Identification callback: type: %d, effect: %d", type, effect_id);
return ESP_OK;
}

static esp_err_t app_attribute_update_cb(attribute::callback_type_t type, uint16_t endpoint_id, uint32_t cluster_id,
esp_err_t app_attribute_update_cb(attribute::callback_type_t type, uint16_t endpoint_id, uint32_t cluster_id,
uint32_t attribute_id, esp_matter_attr_val_t *val, void *priv_data)
{
esp_err_t err = ESP_OK;
Expand Down Expand Up @@ -102,7 +103,7 @@ static esp_err_t app_attribute_update_cb(attribute::callback_type_t type, uint16
return err;
}

static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg)
void app_event_cb(const ChipDeviceEvent *event, intptr_t arg)
{
switch (event->Type) {
case chip::DeviceLayer::DeviceEventType::PublicEventTypes::kCommissioningComplete:
Expand Down Expand Up @@ -133,29 +134,6 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg)
}
}

esp_err_t app_matter_init()
{
/* Create a Matter node */
node::config_t node_config;
node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb);

/* The node and endpoint handles can be used to create/add other endpoints and clusters. */
if (!node) {
ESP_LOGE(TAG, "Matter node creation failed");
return ESP_FAIL;
}
/* Add custom matter controller cluster */
endpoint_t *root_endpoint = esp_matter::endpoint::get(node, 0);
esp_matter::cluster::matter_controller::create(root_endpoint, CLUSTER_FLAG_SERVER);

#if CONFIG_OPENTHREAD_BORDER_ROUTER
cluster::thread_br::create(root_endpoint, CLUSTER_FLAG_SERVER);
#endif

/* Add custom rainmaker cluster */
return rainmaker::init();
}

esp_err_t app_matter_endpoint_create()
{
node_t *node = node::get();
Expand All @@ -172,37 +150,6 @@ esp_err_t app_matter_endpoint_create()
return ESP_OK;
}

esp_err_t app_matter_pre_rainmaker_start()
{
/* Other initializations for custom rainmaker cluster */
return rainmaker::start();
}

esp_err_t app_matter_start()
{
esp_err_t err = esp_matter::start(app_event_cb);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Matter start failed: %d", err);
}
return err;
}

void app_matter_enable_matter_console()
{
#if CONFIG_ENABLE_CHIP_SHELL
esp_matter::console::diagnostics_register_commands();
esp_matter::console::init();
#if CONFIG_ESP_MATTER_CONTROLLER_ENABLE
esp_matter::console::controller_register_commands();
#endif
#if CONFIG_OPENTHREAD_BORDER_ROUTER && CONFIG_OPENTHREAD_CLI
esp_matter::console::thread_br_cli_register_command();
#endif // CONFIG_OPENTHREAD_BORDER_ROUTER && CONFIG_OPENTHREAD_CLI
#else
ESP_LOGI(TAG, "Set CONFIG_ENABLE_CHIP_SHELL to enable Matter Console");
#endif
}

esp_err_t app_matter_report_power(bool val)
{
esp_matter_attr_val_t value = esp_matter_bool(val);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
#include <esp_err.h>
#include <app_priv.h>

esp_err_t app_matter_init();
esp_err_t app_matter_endpoint_create();
esp_err_t app_matter_start();
esp_err_t app_matter_pre_rainmaker_start();
void app_matter_enable_matter_console();
esp_err_t app_matter_report_power(bool val);
esp_err_t app_matter_endpoint_create();
11 changes: 10 additions & 1 deletion examples/matter/matter_controller/main/app_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef void *app_driver_handle_t;
* This initializes the button driver associated with the selected board.
*
* @param[in] user_data Custom user data that will be used in button toggle callback.
*
*
* @return Handle on success.
* @return NULL in case of failure.
*/
Expand All @@ -47,3 +47,12 @@ esp_err_t app_driver_attribute_update(app_driver_handle_t driver_handle, uint16_
uint32_t attribute_id, esp_matter_attr_val_t *val);

void on_device_list_update(void);


esp_err_t app_attribute_update_cb(esp_matter::attribute::callback_type_t type, uint16_t endpoint_id, uint32_t cluster_id,
uint32_t attribute_id, esp_matter_attr_val_t *val, void *priv_data);

esp_err_t app_identification_cb(esp_matter::identification::callback_type_t type, uint16_t endpoint_id, uint8_t effect_id,
uint8_t effect_variant, void *priv_data);

void app_event_cb(const ChipDeviceEvent *event, intptr_t arg);
2 changes: 1 addition & 1 deletion examples/matter/matter_controller/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CONFIG_LWIP_IPV6_AUTOCONFIG=y

# Use a custom partition table
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_br.csv"
CONFIG_PARTITION_TABLE_OFFSET=0x8000
CONFIG_PARTITION_TABLE_MD5=y

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <string.h>

#include <app/server/Server.h>
#include <app_matter.h>
#include <app_priv.h>
#include <device.h>
#include <esp_check.h>
Expand All @@ -26,6 +25,7 @@

#include "app_matter_ctrl.h"
#include "ui_matter_ctrl.h"
#include <app_matter_controller.h>

using namespace esp_matter;
using namespace chip::app::Clusters;
Expand Down
Loading

0 comments on commit fd78129

Please sign in to comment.