mirror of
https://github.com/espressif/esp-matter.git
synced 2026-04-27 19:13:13 +00:00
cfc2e10a1c
Fixes https://github.com/espressif/esp-matter/issues/211 Added two fixed-labels using nvs in the generic_switch example. Added the mfg generated binary file 20202020_3841.bin to flash at the specified address after flashing the firmware.
150 lines
4.8 KiB
C++
150 lines
4.8 KiB
C++
/*
|
|
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_err.h>
|
|
#include <esp_log.h>
|
|
#include <nvs_flash.h>
|
|
|
|
#include <esp_matter.h>
|
|
#include <esp_matter_console.h>
|
|
#include <esp_matter_ota.h>
|
|
|
|
#include <app_priv.h>
|
|
#include <app_reset.h>
|
|
|
|
static const char *TAG = "app_main";
|
|
uint16_t switch_endpoint_id = 0;
|
|
|
|
using namespace esp_matter;
|
|
using namespace esp_matter::attribute;
|
|
using namespace esp_matter::endpoint;
|
|
using namespace chip::app::Clusters;
|
|
|
|
static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg)
|
|
{
|
|
switch (event->Type) {
|
|
case chip::DeviceLayer::DeviceEventType::kInterfaceIpAddressChanged:
|
|
ESP_LOGI(TAG, "Interface IP Address changed");
|
|
break;
|
|
|
|
case chip::DeviceLayer::DeviceEventType::kCommissioningComplete:
|
|
ESP_LOGI(TAG, "Commissioning complete");
|
|
break;
|
|
|
|
case chip::DeviceLayer::DeviceEventType::kFailSafeTimerExpired:
|
|
ESP_LOGI(TAG, "Commissioning failed, fail safe timer expired");
|
|
break;
|
|
|
|
case chip::DeviceLayer::DeviceEventType::kCommissioningSessionStarted:
|
|
ESP_LOGI(TAG, "Commissioning session started");
|
|
break;
|
|
|
|
case chip::DeviceLayer::DeviceEventType::kCommissioningSessionStopped:
|
|
ESP_LOGI(TAG, "Commissioning session stopped");
|
|
break;
|
|
|
|
case chip::DeviceLayer::DeviceEventType::kCommissioningWindowOpened:
|
|
ESP_LOGI(TAG, "Commissioning window opened");
|
|
break;
|
|
|
|
case chip::DeviceLayer::DeviceEventType::kCommissioningWindowClosed:
|
|
ESP_LOGI(TAG, "Commissioning window closed");
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static esp_err_t app_identification_cb(identification::callback_type_t type, uint16_t endpoint_id, uint8_t effect_id,
|
|
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,
|
|
uint32_t attribute_id, esp_matter_attr_val_t *val, void *priv_data)
|
|
{
|
|
esp_err_t err = ESP_OK;
|
|
|
|
if (type == PRE_UPDATE) {
|
|
/* Driver update */
|
|
app_driver_handle_t driver_handle = (app_driver_handle_t)priv_data;
|
|
err = app_driver_attribute_update(driver_handle, endpoint_id, cluster_id, attribute_id, val);
|
|
}
|
|
|
|
return err;
|
|
}
|
|
|
|
extern "C" void app_main()
|
|
{
|
|
esp_err_t err = ESP_OK;
|
|
|
|
/* Initialize the ESP NVS layer */
|
|
nvs_flash_init();
|
|
|
|
/* Initialize driver */
|
|
app_driver_handle_t button_handle = app_driver_button_init();
|
|
|
|
/* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */
|
|
node::config_t node_config;
|
|
node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb);
|
|
|
|
generic_switch::config_t switch_config;
|
|
endpoint_t *endpoint = generic_switch::create(node, &switch_config, ENDPOINT_FLAG_NONE, button_handle);
|
|
|
|
/* These node and endpoint handles can be used to create/add other endpoints and clusters. */
|
|
if (!node || !endpoint) {
|
|
ESP_LOGE(TAG, "Matter node creation failed");
|
|
}
|
|
|
|
switch_endpoint_id = endpoint::get_id(endpoint);
|
|
ESP_LOGI(TAG, "Generic Switch created with endpoint_id %d", switch_endpoint_id);
|
|
|
|
cluster::fixed_label::config_t fl_config;
|
|
cluster_t *fl_cluster = cluster::fixed_label::create(endpoint, &fl_config, CLUSTER_FLAG_SERVER);
|
|
|
|
/* Add additional features to the node */
|
|
cluster_t *cluster = cluster::get(endpoint, Switch::Id);
|
|
#if CONFIG_GENERIC_SWITCH_TYPE_LATCHING
|
|
cluster::switch_cluster::feature::latching_switch::add(cluster);
|
|
#endif
|
|
|
|
#if CONFIG_GENERIC_SWITCH_TYPE_MOMENTARY
|
|
cluster::switch_cluster::feature::momentary_switch::add(cluster);
|
|
#endif
|
|
/* Matter start */
|
|
err = esp_matter::start(app_event_cb);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Matter start failed: %d", err);
|
|
}
|
|
|
|
nvs_handle_t handle;
|
|
nvs_open_from_partition(CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION_LABEL, "chip-factory", NVS_READWRITE, &handle);
|
|
|
|
int32_t out_value = 0;
|
|
if (nvs_get_i32(handle, "fl-sz/1", &out_value) == ESP_ERR_NVS_NOT_FOUND)
|
|
{
|
|
nvs_set_i32(handle, "fl-sz/1", 2);
|
|
nvs_set_str(handle, "fl-k/1/0", "myEP1LBL1");
|
|
nvs_set_str(handle, "fl-v/1/0", "valEP1LBL1");
|
|
nvs_set_str(handle, "fl-k/1/1", "myEP1LBL2");
|
|
nvs_set_str(handle, "fl-v/1/1", "valEP1LBL2");
|
|
}
|
|
|
|
nvs_commit(handle);
|
|
nvs_close(handle);
|
|
|
|
#if CONFIG_ENABLE_CHIP_SHELL
|
|
esp_matter::console::diagnostics_register_commands();
|
|
esp_matter::console::wifi_register_commands();
|
|
esp_matter::console::init();
|
|
#endif
|
|
}
|