Skip to content

Commit 3a1aa8c

Browse files
committed
Merge branch 'controller/unpair_command' into 'main'
controller: Add unpairing command with callback Closes CON-1533 See merge request app-frameworks/esp-matter!1033
2 parents 3044b5b + 1ee1dda commit 3a1aa8c

4 files changed

+76
-3
lines changed

components/esp_matter_controller/commands/esp_matter_controller_pairing_command.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,20 @@ esp_err_t pairing_code_wifi_thread(NodeId nodeId, const char *ssid, const char *
191191
return ESP_OK;
192192
}
193193

194+
static void remove_fabric_handler(NodeId remote_node, CHIP_ERROR status)
195+
{
196+
if (status == CHIP_NO_ERROR) {
197+
ESP_LOGI(TAG, "Succeeded to remove fabric for remote node 0x%" PRIx64, remote_node);
198+
} else {
199+
ESP_LOGE(TAG, "Failed to remove fabric for remote node 0x%" PRIx64, remote_node);
200+
}
201+
}
202+
203+
esp_err_t unpair_device(NodeId node_id)
204+
{
205+
auto &controller_instance = esp_matter::controller::matter_controller_client::get_instance();
206+
return controller_instance.unpair(node_id, remove_fabric_handler);
207+
}
208+
194209
} // namespace controller
195210
} // namespace esp_matter

components/esp_matter_controller/commands/esp_matter_controller_pairing_command.h

+10
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,15 @@ esp_err_t pairing_code_wifi(NodeId node_id, const char *ssid, const char *passwo
169169
esp_err_t pairing_code_wifi_thread(NodeId node_id, const char *ssid, const char *password, const char *payload,
170170
uint8_t *dataset_buf, uint8_t dataset_len);
171171

172+
/**
173+
* Unpair a Matter end-device which will remove the fabric from the remote device
174+
*
175+
* @param[in] node_id NodeId of the Matter end-device to be unpaired.
176+
*
177+
* @return ESP_OK on success
178+
* @return error in case of failure
179+
*/
180+
esp_err_t unpair_device(NodeId node_id);
181+
172182
} // namespace controller
173183
} // namespace esp_matter

components/esp_matter_controller/core/esp_matter_controller_client.h

+43
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <controller/AutoCommissioner.h>
2222
#include <controller/CHIPDeviceController.h>
2323
#include <controller/CommissionerDiscoveryController.h>
24+
#include <controller/CurrentFabricRemover.h>
2425
#include <controller/ExampleOperationalCredentialsIssuer.h>
2526
#include <controller/OperationalCredentialsDelegate.h>
2627
#include <credentials/DeviceAttestationCredsProvider.h>
@@ -46,6 +47,44 @@ namespace esp_matter {
4647
namespace controller {
4748

4849
#ifndef CONFIG_ESP_MATTER_ENABLE_MATTER_SERVER
50+
typedef void (*remove_fabric_callback)(chip::NodeId remoteNodeId, CHIP_ERROR status);
51+
52+
class auto_fabric_remover : private chip::Controller::CurrentFabricRemover
53+
{
54+
public:
55+
static esp_err_t remove_fabric(chip::Controller::DeviceController *controller, chip::NodeId remote_node,
56+
remove_fabric_callback callback)
57+
{
58+
auto * remover = chip::Platform::New<auto_fabric_remover>(controller, callback);
59+
if (remover == nullptr) {
60+
return ESP_ERR_NO_MEM;
61+
}
62+
CHIP_ERROR err = remover->CurrentFabricRemover::RemoveCurrentFabric(remote_node, &remover->m_matter_callback);
63+
if (err != CHIP_NO_ERROR)
64+
{
65+
// If failing to call RemoveCurrentFabric(), delete the remover here. Otherwise the remover will be deleted
66+
// in on_remove_current_fabric().
67+
chip::Platform::Delete(remover);
68+
}
69+
return err == CHIP_NO_ERROR ? ESP_OK : ESP_FAIL;
70+
}
71+
72+
auto_fabric_remover(chip::Controller::DeviceController *controller, remove_fabric_callback callback) :
73+
chip::Controller::CurrentFabricRemover(controller), m_matter_callback(on_remove_current_fabric, this),
74+
m_remove_fabric_callback(callback) {}
75+
private:
76+
static void on_remove_current_fabric(void * context, chip::NodeId remote_node, CHIP_ERROR status)
77+
{
78+
auto *self = static_cast<auto_fabric_remover *>(context);
79+
if (self && self->m_remove_fabric_callback) {
80+
self->m_remove_fabric_callback(remote_node, status);
81+
}
82+
chip::Platform::Delete(self);
83+
}
84+
chip::Callback::Callback<chip::Controller::OnCurrentFabricRemove> m_matter_callback;
85+
remove_fabric_callback m_remove_fabric_callback;
86+
};
87+
4988
class matter_controller_client {
5089
public:
5190
class controller_storage_delegate : public chip::PersistentStorageDelegate {
@@ -94,6 +133,10 @@ class matter_controller_client {
94133
#ifdef CONFIG_ESP_MATTER_COMMISSIONER_ENABLE
95134
esp_err_t setup_commissioner();
96135
MatterDeviceCommissioner *get_commissioner() { return &m_device_commissioner; }
136+
esp_err_t unpair(NodeId remote_node, remove_fabric_callback callback = nullptr)
137+
{
138+
return auto_fabric_remover::remove_fabric(&m_device_commissioner, remote_node, callback);
139+
}
97140
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
98141
CommissionerDiscoveryController *get_discovery_controller() { return &m_commissioner_discovery_controller; }
99142
#endif

components/esp_matter_controller/core/esp_matter_controller_console.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static bool convert_hex_str_to_bytes(const char *hex_str, uint8_t *bytes, uint8_
174174
#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE
175175
static esp_err_t controller_pairing_handler(int argc, char **argv)
176176
{
177-
VerifyOrReturnError(argc >= 3 && argc <= 6, ESP_ERR_INVALID_ARG);
177+
VerifyOrReturnError(argc >= 2 && argc <= 6, ESP_ERR_INVALID_ARG);
178178
esp_err_t result = ESP_ERR_INVALID_ARG;
179179

180180
if (strncmp(argv[0], "onnetwork", sizeof("onnetwork")) == 0) {
@@ -260,6 +260,10 @@ static esp_err_t controller_pairing_handler(int argc, char **argv)
260260

261261
result = controller::pairing_code_wifi_thread(nodeId, ssid, password, payload, dataset_tlvs_buf,
262262
dataset_tlvs_len);
263+
} else if (strncmp(argv[0], "unpair", sizeof("unpair")) == 0) {
264+
VerifyOrReturnError(argc == 2, ESP_ERR_INVALID_ARG);
265+
uint64_t node_id = string_to_uint64(argv[1]);
266+
result = controller::unpair_device(node_id);
263267
}
264268

265269
if (result != ESP_OK) {
@@ -585,15 +589,16 @@ esp_err_t controller_register_commands()
585589
#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE
586590
{
587591
.name = "pairing",
588-
.description = "Pairing a node.\n"
592+
.description = "Commands for commissioning/unpair nodes.\n"
589593
"\tUsage: controller pairing onnetwork <nodeid> <pincode> OR\n"
590594
"\tcontroller pairing ble-wifi <nodeid> <ssid> <password> <pincode> <discriminator> OR\n"
591595
"\tcontroller pairing ble-thread <nodeid> <dataset> <pincode> <discriminator> OR\n"
592596
"\tcontroller pairing ble-thread <nodeid> <dataset> <pincode> <discriminator> OR\n"
593597
"\tcontroller pairing code <nodeid> <payload> OR\n"
594598
"\tcontroller pairing code-wifi <nodeid> <ssid> <password> <payload> OR\n"
595599
"\tcontroller pairing code-thread <nodeid> <dataset> <payload> OR\n"
596-
"\tcontroller pairing code-wifi-thread <nodeid> <ssid> <password> <dataset> <payload>",
600+
"\tcontroller pairing code-wifi-thread <nodeid> <ssid> <password> <dataset> <payload> OR\n"
601+
"\tcontroller pairing unpair <nodeid>",
597602
.handler = controller_pairing_handler,
598603
},
599604
{

0 commit comments

Comments
 (0)