Skip to content

Commit b7dfc6a

Browse files
committed
Merge branch 'controller_keep_suscription_flag' into 'main'
components/esp_matter_controller: support keep subscription flag See merge request app-frameworks/esp-matter!1020
2 parents 2a7a61a + 7fd4135 commit b7dfc6a

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

components/esp_matter_controller/commands/esp_matter_controller_subscribe_command.cpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ void subscribe_command::on_device_connected_fcn(void *context, ExchangeManager &
4242
chip::OperationalDeviceProxy device_proxy(&exchangeMgr, sessionHandle);
4343
esp_err_t err = interaction::subscribe::send_request(
4444
&device_proxy, cmd->m_attr_paths.Get(), cmd->m_attr_paths.AllocatedSize(), cmd->m_event_paths.Get(),
45-
cmd->m_event_paths.AllocatedSize(), cmd->m_min_interval, cmd->m_max_interval, true, cmd->m_auto_resubscribe,
46-
cmd->m_buffered_read_cb);
45+
cmd->m_event_paths.AllocatedSize(), cmd->m_min_interval, cmd->m_max_interval, cmd->m_keep_subscription,
46+
cmd->m_auto_resubscribe, cmd->m_buffered_read_cb);
4747
if (err != ESP_OK) {
4848
chip::Platform::Delete(cmd);
4949
}
@@ -184,7 +184,7 @@ void subscribe_command::OnDone(ReadClient *apReadClient)
184184
esp_err_t send_subscribe_attr_command(uint64_t node_id, ScopedMemoryBufferWithSize<uint16_t> &endpoint_ids,
185185
ScopedMemoryBufferWithSize<uint32_t> &cluster_ids,
186186
ScopedMemoryBufferWithSize<uint32_t> &attribute_ids, uint16_t min_interval,
187-
uint16_t max_interval, bool auto_resubscribe)
187+
uint16_t max_interval, bool auto_resubscribe, bool keep_subscription)
188188
{
189189
if (endpoint_ids.AllocatedSize() != cluster_ids.AllocatedSize() ||
190190
endpoint_ids.AllocatedSize() != attribute_ids.AllocatedSize()) {
@@ -205,7 +205,8 @@ esp_err_t send_subscribe_attr_command(uint64_t node_id, ScopedMemoryBufferWithSi
205205
}
206206

207207
subscribe_command *cmd = chip::Platform::New<subscribe_command>(
208-
node_id, std::move(attr_paths), std::move(event_paths), min_interval, max_interval, auto_resubscribe);
208+
node_id, std::move(attr_paths), std::move(event_paths), min_interval, max_interval, auto_resubscribe, nullptr,
209+
nullptr, nullptr, nullptr, keep_subscription);
209210
if (!cmd) {
210211
ESP_LOGE(TAG, "Failed to alloc memory for subscribe_command");
211212
return ESP_ERR_NO_MEM;
@@ -216,7 +217,7 @@ esp_err_t send_subscribe_attr_command(uint64_t node_id, ScopedMemoryBufferWithSi
216217
esp_err_t send_subscribe_event_command(uint64_t node_id, ScopedMemoryBufferWithSize<uint16_t> &endpoint_ids,
217218
ScopedMemoryBufferWithSize<uint32_t> &cluster_ids,
218219
ScopedMemoryBufferWithSize<uint32_t> &event_ids, uint16_t min_interval,
219-
uint16_t max_interval, bool auto_resubscribe)
220+
uint16_t max_interval, bool auto_resubscribe, bool keep_subscription)
220221
{
221222
if (endpoint_ids.AllocatedSize() != cluster_ids.AllocatedSize() ||
222223
endpoint_ids.AllocatedSize() != event_ids.AllocatedSize()) {
@@ -237,7 +238,8 @@ esp_err_t send_subscribe_event_command(uint64_t node_id, ScopedMemoryBufferWithS
237238
}
238239

239240
subscribe_command *cmd = chip::Platform::New<subscribe_command>(
240-
node_id, std::move(attr_paths), std::move(event_paths), min_interval, max_interval, auto_resubscribe);
241+
node_id, std::move(attr_paths), std::move(event_paths), min_interval, max_interval, auto_resubscribe, nullptr,
242+
nullptr, nullptr, nullptr, keep_subscription);
241243
if (!cmd) {
242244
ESP_LOGE(TAG, "Failed to alloc memory for subscribe_command");
243245
return ESP_ERR_NO_MEM;
@@ -247,7 +249,7 @@ esp_err_t send_subscribe_event_command(uint64_t node_id, ScopedMemoryBufferWithS
247249

248250
esp_err_t send_subscribe_attr_command(uint64_t node_id, uint16_t endpoint_id, uint32_t cluster_id,
249251
uint32_t attribute_id, uint16_t min_interval, uint16_t max_interval,
250-
bool auto_resubscribe)
252+
bool auto_resubscribe, bool keep_subscription)
251253
{
252254
ScopedMemoryBufferWithSize<uint16_t> endpoint_ids;
253255
ScopedMemoryBufferWithSize<uint32_t> cluster_ids;
@@ -259,11 +261,12 @@ esp_err_t send_subscribe_attr_command(uint64_t node_id, uint16_t endpoint_id, ui
259261
return ESP_ERR_NO_MEM;
260262
}
261263
return send_subscribe_attr_command(node_id, endpoint_ids, cluster_ids, attribute_ids, min_interval, max_interval,
262-
auto_resubscribe);
264+
auto_resubscribe, keep_subscription);
263265
}
264266

265267
esp_err_t send_subscribe_event_command(uint64_t node_id, uint16_t endpoint_id, uint32_t cluster_id, uint32_t event_id,
266-
uint16_t min_interval, uint16_t max_interval, bool auto_resubscribe)
268+
uint16_t min_interval, uint16_t max_interval, bool auto_resubscribe,
269+
bool keep_subscription)
267270
{
268271
ScopedMemoryBufferWithSize<uint16_t> endpoint_ids;
269272
ScopedMemoryBufferWithSize<uint32_t> cluster_ids;
@@ -275,7 +278,7 @@ esp_err_t send_subscribe_event_command(uint64_t node_id, uint16_t endpoint_id, u
275278
return ESP_ERR_NO_MEM;
276279
}
277280
return send_subscribe_event_command(node_id, endpoint_ids, cluster_ids, event_ids, min_interval, max_interval,
278-
auto_resubscribe);
281+
auto_resubscribe, keep_subscription);
279282
}
280283

281284
esp_err_t send_shutdown_subscription(uint64_t node_id, uint32_t subscription_id)

components/esp_matter_controller/commands/esp_matter_controller_subscribe_command.h

+16-6
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ class subscribe_command : public ReadClient::Callback {
4646
ScopedMemoryBufferWithSize<EventPathParams> &&event_paths, uint16_t min_interval,
4747
uint16_t max_interval, bool auto_resubscribe = true, attribute_report_cb_t attribute_cb = nullptr,
4848
event_report_cb_t event_cb = nullptr, subscribe_done_cb_t done_cb = nullptr,
49-
subscribe_failure_cb_t connect_failure_cb = nullptr)
49+
subscribe_failure_cb_t connect_failure_cb = nullptr, bool keep_subscription = true)
5050
: m_node_id(node_id)
5151
, m_min_interval(min_interval)
5252
, m_max_interval(max_interval)
5353
, m_auto_resubscribe(auto_resubscribe)
54+
, m_keep_subscription(keep_subscription)
5455
, m_buffered_read_cb(*this)
5556
, m_attr_paths(std::move(attr_paths))
5657
, m_event_paths(std::move(event_paths))
@@ -71,11 +72,12 @@ class subscribe_command : public ReadClient::Callback {
7172
subscribe_command_type_t command_type, uint16_t min_interval, uint16_t max_interval,
7273
bool auto_resubscribe = true, attribute_report_cb_t attribute_cb = nullptr,
7374
event_report_cb_t event_cb = nullptr, subscribe_done_cb_t done_cb = nullptr,
74-
subscribe_failure_cb_t connect_failure_cb = nullptr)
75+
subscribe_failure_cb_t connect_failure_cb = nullptr, bool keep_subscription = true)
7576
: m_node_id(node_id)
7677
, m_min_interval(min_interval)
7778
, m_max_interval(max_interval)
7879
, m_auto_resubscribe(auto_resubscribe)
80+
, m_keep_subscription(keep_subscription)
7981
, m_buffered_read_cb(*this)
8082
, on_device_connected_cb(on_device_connected_fcn, this)
8183
, on_device_connection_failure_cb(on_device_connection_failure_fcn, this)
@@ -125,6 +127,7 @@ class subscribe_command : public ReadClient::Callback {
125127
uint16_t m_min_interval;
126128
uint16_t m_max_interval;
127129
bool m_auto_resubscribe;
130+
bool m_keep_subscription;
128131
BufferedReadCallback m_buffered_read_cb;
129132
uint32_t m_subscription_id = 0;
130133
uint8_t m_resubscribe_retries = 0;
@@ -155,14 +158,16 @@ class subscribe_command : public ReadClient::Callback {
155158
* @param[in] min_interval Minimum interval of the subscription
156159
* @param[in] max_interval Maximum interval of the subscription
157160
* @param[in] auto_resubscribe Auto re-subscribe flag
161+
* @param[in] keep_subscription Keep subscription flag, terminate existing subscriptions if false
158162
*
159163
* @return ESP_OK on success.
160164
* @return error in case of failure.
161165
*/
162166
esp_err_t send_subscribe_attr_command(uint64_t node_id, ScopedMemoryBufferWithSize<uint16_t> &endpoint_ids,
163167
ScopedMemoryBufferWithSize<uint32_t> &cluster_ids,
164168
ScopedMemoryBufferWithSize<uint32_t> &attribute_ids, uint16_t min_interval,
165-
uint16_t max_interval, bool auto_resubscribe = true);
169+
uint16_t max_interval, bool auto_resubscribe = true,
170+
bool keep_subscription = true);
166171

167172
/** Send subscribe command with multiple event paths
168173
*
@@ -176,14 +181,16 @@ esp_err_t send_subscribe_attr_command(uint64_t node_id, ScopedMemoryBufferWithSi
176181
* @param[in] min_interval Minimum interval of the subscription
177182
* @param[in] max_interval Maximum interval of the subscription
178183
* @param[in] auto_resubscribe Auto re-subscribe flag
184+
* @param[in] keep_subscription Keep subscription flag, terminate existing subscriptions if false
179185
*
180186
* @return ESP_OK on success.
181187
* @return error in case of failure.
182188
*/
183189
esp_err_t send_subscribe_event_command(uint64_t node_id, ScopedMemoryBufferWithSize<uint16_t> &endpoint_ids,
184190
ScopedMemoryBufferWithSize<uint32_t> &cluster_ids,
185191
ScopedMemoryBufferWithSize<uint32_t> &event_ids, uint16_t min_interval,
186-
uint16_t max_interval, bool auto_resubscribe = true);
192+
uint16_t max_interval, bool auto_resubscribe = true,
193+
bool keep_subscription = true);
187194

188195
/** Send subscribe command with single attribute path
189196
*
@@ -197,13 +204,14 @@ esp_err_t send_subscribe_event_command(uint64_t node_id, ScopedMemoryBufferWithS
197204
* @param[in] min_interval Minimum interval of the subscription
198205
* @param[in] max_interval Maximum interval of the subscription
199206
* @param[in] auto_resubscribe Auto re-subscribe flag
207+
* @param[in] keep_subscription Keep subscription flag, terminate existing subscriptions if false
200208
*
201209
* @return ESP_OK on success.
202210
* @return error in case of failure.
203211
*/
204212
esp_err_t send_subscribe_attr_command(uint64_t node_id, uint16_t endpoint_id, uint32_t cluster_id,
205213
uint32_t attribute_id, uint16_t min_interval, uint16_t max_interval,
206-
bool auto_resubscribe = true);
214+
bool auto_resubscribe = true, bool keep_subscription = true);
207215

208216
/** Send subscribe command with single event path
209217
*
@@ -217,12 +225,14 @@ esp_err_t send_subscribe_attr_command(uint64_t node_id, uint16_t endpoint_id, ui
217225
* @param[in] min_interval Minimum interval of the subscription
218226
* @param[in] max_interval Maximum interval of the subscription
219227
* @param[in] auto_resubscribe Auto re-subscribe flag
228+
* @param[in] keep_subscription Keep subscription flag, terminate existing subscriptions if false
220229
*
221230
* @return ESP_OK on success.
222231
* @return error in case of failure.
223232
*/
224233
esp_err_t send_subscribe_event_command(uint64_t node_id, uint16_t endpoint_id, uint32_t cluster_id, uint32_t event_id,
225-
uint16_t min_interval, uint16_t max_interval, bool auto_resubscribe = true);
234+
uint16_t min_interval, uint16_t max_interval, bool auto_resubscribe = true,
235+
bool keep_subscription = true);
226236

227237
/** Shut down a subscription for given node id and subscription id
228238
*

components/esp_matter_controller/core/esp_matter_controller_console.cpp

+29-6
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ static esp_err_t controller_read_event_handler(int argc, char **argv)
503503

504504
static esp_err_t controller_subscribe_attr_handler(int argc, char **argv)
505505
{
506-
if (argc != 6) {
506+
if (argc < 6) {
507507
return ESP_ERR_INVALID_ARG;
508508
}
509509

@@ -517,13 +517,23 @@ static esp_err_t controller_subscribe_attr_handler(int argc, char **argv)
517517
uint16_t min_interval = string_to_uint16(argv[4]);
518518
uint16_t max_interval = string_to_uint16(argv[5]);
519519

520+
bool keep_subscription = true;
521+
if (argc >= 7) {
522+
keep_subscription = string_to_bool(argv[6]);
523+
}
524+
525+
bool auto_resubscribe = true;
526+
if (argc >= 8) {
527+
auto_resubscribe = string_to_bool(argv[7]);
528+
}
529+
520530
return controller::send_subscribe_attr_command(node_id, endpoint_ids, cluster_ids, attribute_ids, min_interval,
521-
max_interval);
531+
max_interval, keep_subscription, auto_resubscribe);
522532
}
523533

524534
static esp_err_t controller_subscribe_event_handler(int argc, char **argv)
525535
{
526-
if (argc != 6) {
536+
if (argc < 6) {
527537
return ESP_ERR_INVALID_ARG;
528538
}
529539

@@ -536,8 +546,18 @@ static esp_err_t controller_subscribe_event_handler(int argc, char **argv)
536546
ESP_RETURN_ON_ERROR(string_to_uint32_array(argv[3], event_ids), TAG, "Failed to parse event IDs");
537547
uint16_t min_interval = string_to_uint16(argv[4]);
538548
uint16_t max_interval = string_to_uint16(argv[5]);
549+
550+
bool keep_subscription = true;
551+
if (argc >= 7) {
552+
keep_subscription = string_to_bool(argv[6]);
553+
}
554+
555+
bool auto_resubscribe = true;
556+
if (argc >= 8) {
557+
auto_resubscribe = string_to_bool(argv[7]);
558+
}
539559
return controller::send_subscribe_event_command(node_id, endpoint_ids, cluster_ids, event_ids, min_interval,
540-
max_interval);
560+
max_interval, keep_subscription, auto_resubscribe);
541561
}
542562

543563
static esp_err_t controller_shutdown_subscription_handler(int argc, char **argv)
@@ -674,14 +694,17 @@ esp_err_t controller_register_commands()
674694
.name = "subs-attr",
675695
.description = "Subscribe attributes of the nodes.\n"
676696
"\tUsage: controller subs-attr <node-id> <endpoint-ids> <cluster-ids> <attr-ids> "
677-
"<min-interval> <max-interval>",
697+
"<min-interval> <max-interval> [keep-subscription] [auto-resubscribe]\n"
698+
"\tNotes: If 'keep-subscription' is 'false', existing subscriptions will be terminated for the node. "
699+
"If 'auto-resubscribe' is 'true', controller will auto resubscribe if subscriptions timeout",
678700
.handler = controller_subscribe_attr_handler,
679701
},
680702
{
681703
.name = "subs-event",
682704
.description = "Subscribe events of the nodes.\n"
683705
"\tUsage: controller subs-event <node-id> <endpoint-ids> <cluster-ids> <event-ids> "
684-
"<min-interval> <max-interval>",
706+
"<min-interval> <max-interval> [keep-subscription] [auto-resubscribe]\n"
707+
"\tNotes: 'keep-subscription' and 'auto-resubscribe' are the same as 'subs-attr' command",
685708
.handler = controller_subscribe_event_handler,
686709
},
687710
{

0 commit comments

Comments
 (0)