23
23
#include < app/server/Server.h>
24
24
#include < lib/core/Optional.h>
25
25
26
+ #ifdef CONFIG_SUBSCRIBE_TO_ON_OFF_SERVER_AFTER_BINDING
27
+ #include < app/AttributePathParams.h>
28
+ #include < app/ConcreteAttributePath.h>
29
+ #include < lib/core/TLVReader.h>
30
+ #endif
31
+
26
32
using chip::kInvalidClusterId ;
27
33
static constexpr chip::CommandId kInvalidCommandId = 0xFFFF'FFFF ;
28
34
@@ -33,6 +39,53 @@ using namespace esp_matter::cluster;
33
39
static const char *TAG = " app_driver" ;
34
40
extern uint16_t switch_endpoint_id;
35
41
42
+ #ifdef CONFIG_SUBSCRIBE_TO_ON_OFF_SERVER_AFTER_BINDING
43
+ class MyReadClientCallback : public chip ::app::ReadClient::Callback {
44
+ public:
45
+ void OnAttributeData (const chip::app::ConcreteDataAttributePath &aPath,
46
+ chip::TLV::TLVReader *aReader,
47
+ const chip::app::StatusIB &aStatus) override {
48
+ // Handle the attribute data
49
+ if (aPath.mClusterId == chip::app::Clusters::OnOff::Id) {
50
+ if (aPath.mAttributeId == chip::app::Clusters::OnOff::Attributes::OnOff::Id) {
51
+ ESP_LOGI (TAG, " Received OnOff attribute" );
52
+ }
53
+ }
54
+ }
55
+
56
+ void OnEventData (const chip::app::EventHeader &aEventHeader, chip::TLV::TLVReader * apData,
57
+ const chip::app::StatusIB *aStatus) override {
58
+ // Handle event data
59
+ }
60
+
61
+ void OnError (CHIP_ERROR aError) override {
62
+ // Handle the error
63
+ ESP_LOGI (TAG, " ReadClient Error: %s" , ErrorStr (aError));
64
+ }
65
+
66
+ void OnDone (chip::app::ReadClient * apReadClient) override {
67
+ // Cleanup after done
68
+ ESP_LOGI (TAG, " ReadClient Done" );
69
+ }
70
+ };
71
+ MyReadClientCallback readClientCb;
72
+
73
+ void app_client_subscribe_command_callback (client::peer_device_t *peer_device, client::request_handle_t *req_handle,
74
+ void *priv_data)
75
+ {
76
+ uint16_t min_interval = 5 ;
77
+ uint16_t max_interval = 10 ;
78
+ bool keep_subscription = true ;
79
+ bool auto_resubscribe = true ;
80
+ chip::Platform::ScopedMemoryBufferWithSize<chip::app::AttributePathParams> attrb_path;
81
+ attrb_path.Alloc (1 );
82
+ client::interaction::subscribe::send_request (peer_device, &req_handle->attribute_path , attrb_path.AllocatedSize (),
83
+ &req_handle->event_path , 0 , min_interval, max_interval, keep_subscription,
84
+ auto_resubscribe, readClientCb);
85
+ }
86
+
87
+ #endif
88
+
36
89
#if CONFIG_ENABLE_CHIP_SHELL
37
90
static char console_buffer[101 ] = {0 };
38
91
static esp_err_t app_driver_bound_console_handler (int argc, char **argv)
@@ -180,34 +233,46 @@ static void send_command_failure_callback(void *context, CHIP_ERROR error)
180
233
void app_driver_client_invoke_command_callback (client::peer_device_t *peer_device, client::request_handle_t *req_handle,
181
234
void *priv_data)
182
235
{
183
- if (req_handle->type != esp_matter::client::INVOKE_CMD) {
184
- return ;
185
- }
186
- char command_data_str[32 ];
187
- // on_off light switch should support on_off cluster and identify cluster commands sending.
188
- if (req_handle->command_path .mClusterId == OnOff::Id) {
189
- strcpy (command_data_str, " {}" );
190
- } else if (req_handle->command_path .mClusterId == Identify::Id) {
191
- if (req_handle->command_path .mCommandId == Identify::Commands::Identify::Id) {
192
- if (((char *)req_handle->request_data )[0 ] != 1 ) {
193
- ESP_LOGE (TAG, " Number of parameters error" );
236
+ if (req_handle->type == esp_matter::client::INVOKE_CMD) {
237
+ char command_data_str[32 ];
238
+ // on_off light switch should support on_off cluster and identify cluster commands sending.
239
+ if (req_handle->command_path .mClusterId == OnOff::Id) {
240
+ strcpy (command_data_str, " {}" );
241
+ } else if (req_handle->command_path .mClusterId == Identify::Id) {
242
+ if (req_handle->command_path .mCommandId == Identify::Commands::Identify::Id) {
243
+ if (((char *)req_handle->request_data )[0 ] != 1 ) {
244
+ ESP_LOGE (TAG, " Number of parameters error" );
245
+ return ;
246
+ }
247
+ snprintf (command_data_str, sizeof (command_data_str), " {\" 0:U16\" : %ld}" ,
248
+ strtoul ((const char *)(req_handle->request_data ) + 1 , NULL , 16 ));
249
+ } else {
250
+ ESP_LOGE (TAG, " Unsupported command" );
194
251
return ;
195
252
}
196
- sprintf (command_data_str, " {\" 0:U16\" : %ld}" ,
197
- strtoul ((const char *)(req_handle->request_data ) + 1 , NULL , 16 ));
198
253
} else {
199
- ESP_LOGE (TAG, " Unsupported command " );
254
+ ESP_LOGE (TAG, " Unsupported cluster " );
200
255
return ;
201
256
}
202
- } else {
203
- ESP_LOGE (TAG, " Unsupported cluster " );
204
- return ;
257
+ client::interaction::invoke::send_request ( NULL , peer_device, req_handle-> command_path , command_data_str,
258
+ send_command_success_callback, send_command_failure_callback,
259
+ chip::NullOptional) ;
205
260
}
206
- client::interaction::invoke::send_request (NULL , peer_device, req_handle->command_path , command_data_str,
207
- send_command_success_callback, send_command_failure_callback,
208
- chip::NullOptional);
261
+ return ;
209
262
}
210
263
264
+ void app_driver_client_callback (client::peer_device_t *peer_device, client::request_handle_t *req_handle,
265
+ void *priv_data)
266
+ {
267
+ if (req_handle->type == esp_matter::client::INVOKE_CMD) {
268
+ app_driver_client_invoke_command_callback (peer_device, req_handle, priv_data);
269
+ #ifdef CONFIG_SUBSCRIBE_TO_ON_OFF_SERVER_AFTER_BINDING
270
+ } else if (req_handle->type == esp_matter::client::SUBSCRIBE_ATTR) {
271
+ app_client_subscribe_command_callback (peer_device, req_handle, priv_data);
272
+ #endif
273
+ }
274
+ return ;
275
+ }
211
276
void app_driver_client_group_invoke_command_callback (uint8_t fabric_index, client::request_handle_t *req_handle,
212
277
void *priv_data)
213
278
{
@@ -224,7 +289,7 @@ void app_driver_client_group_invoke_command_callback(uint8_t fabric_index, clien
224
289
ESP_LOGE (TAG, " Number of parameters error" );
225
290
return ;
226
291
}
227
- sprintf (command_data_str, " {\" 0:U16\" : %ld}" ,
292
+ snprintf (command_data_str, sizeof (command_data_str) , " {\" 0:U16\" : %ld}" ,
228
293
strtoul ((const char *)(req_handle->request_data ) + 1 , NULL , 16 ));
229
294
} else {
230
295
ESP_LOGE (TAG, " Unsupported command" );
@@ -262,7 +327,7 @@ app_driver_handle_t app_driver_switch_init()
262
327
#if CONFIG_ENABLE_CHIP_SHELL
263
328
app_driver_register_commands ();
264
329
#endif // CONFIG_ENABLE_CHIP_SHELL
265
- client::set_request_callback (app_driver_client_invoke_command_callback ,
330
+ client::set_request_callback (app_driver_client_callback ,
266
331
app_driver_client_group_invoke_command_callback, NULL );
267
332
268
333
return (app_driver_handle_t )btns[0 ];
0 commit comments