huesmith 0.1.0

Hue-compatible Zigbee light library for ESP32-C6/H2 (ESP-IDF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
// Raw FFI bindings to esp-zigbee-sdk via esp-idf-sys.
// All functions here are unsafe extern "C" wrappers.

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

// ── Profile IDs ──────────────────────────────────────────────────────

pub const PROFILE_HA: u16 = 0x0104;

// ── Cluster IDs ──────────────────────────────────────────────────────

pub const CLUSTER_BASIC: u16 = 0x0000;
pub const CLUSTER_IDENTIFY: u16 = 0x0003;
pub const CLUSTER_SCENES: u16 = 0x0005;
// Single source of truth lives in huesmith-core (its scene parser keys off these);
// re-exported here so all FFI call sites keep the `ffi::` path.
pub use huesmith_core::hue::scene::{CLUSTER_COLOR_CONTROL, CLUSTER_LEVEL_CONTROL, CLUSTER_ON_OFF};
// ── Manufacturer Codes ───────────────────────────────────────────────

/// Signify Netherlands B.V. (Philips Hue). Used as the OTA cluster's
/// manufacturer field so the Bridge lists the device under the Hue brand.
pub const MANUF_CODE_SIGNIFY: u16 = 0x100B;

// ── Basic Cluster Attribute IDs ──────────────────────────────────────

pub const ATTR_BASIC_ZCL_VERSION: u16 = 0x0000;
pub const ATTR_BASIC_HW_VERSION: u16 = 0x0003;
pub const ATTR_BASIC_MANUFACTURER_NAME: u16 = 0x0004;
pub const ATTR_BASIC_MODEL_IDENTIFIER: u16 = 0x0005;
pub const ATTR_BASIC_DATE_CODE: u16 = 0x0006;
pub const ATTR_BASIC_SW_BUILD_ID: u16 = 0x4000;

// ── On/Off Cluster Attribute IDs ─────────────────────────────────────

pub const ATTR_ON_OFF: u16 = 0x0000;
pub const ATTR_ON_OFF_GLOBAL_SCENE_CONTROL: u16 = 0x4000;
pub const ATTR_ON_OFF_ON_TIME: u16 = 0x4001;
pub const ATTR_ON_OFF_OFF_WAIT_TIME: u16 = 0x4002;
pub const ATTR_ON_OFF_START_UP_ON_OFF: u16 = 0x4003;

// ── Level Control Attribute IDs ──────────────────────────────────────

pub const ATTR_LEVEL_CURRENT_LEVEL: u16 = 0x0000;
pub const ATTR_LEVEL_OPTIONS: u16 = 0x000F;
pub const ATTR_LEVEL_START_UP_CURRENT_LEVEL: u16 = 0x4000;

// ── Color Control Attribute IDs ──────────────────────────────────────

pub const ATTR_COLOR_CURRENT_HUE: u16 = 0x0000;
pub const ATTR_COLOR_CURRENT_SATURATION: u16 = 0x0001;
pub const ATTR_COLOR_CURRENT_X: u16 = 0x0003;
pub const ATTR_COLOR_CURRENT_Y: u16 = 0x0004;
pub const ATTR_COLOR_TEMPERATURE_MIREDS: u16 = 0x0007;
pub const ATTR_COLOR_MODE: u16 = 0x0008;
pub const ATTR_COLOR_ENHANCED_CURRENT_HUE: u16 = 0x4000;
pub const ATTR_COLOR_ENHANCED_COLOR_MODE: u16 = 0x4001;
pub const ATTR_COLOR_LOOP_ACTIVE: u16 = 0x4002;
pub const ATTR_COLOR_LOOP_DIRECTION: u16 = 0x4003;
pub const ATTR_COLOR_LOOP_TIME: u16 = 0x4004;
pub const ATTR_COLOR_LOOP_START_ENHANCED_HUE: u16 = 0x4005;
pub const ATTR_COLOR_LOOP_STORED_ENHANCED_HUE: u16 = 0x4006;
pub const ATTR_COLOR_CAPABILITIES: u16 = 0x400A;
pub const ATTR_COLOR_TEMP_PHYSICAL_MIN_MIREDS: u16 = 0x400B;
pub const ATTR_COLOR_TEMP_PHYSICAL_MAX_MIREDS: u16 = 0x400C;
pub const ATTR_COLOR_COUPLE_COLOR_TEMP_TO_LEVEL_MIN_MIREDS: u16 = 0x400D;
pub const ATTR_COLOR_START_UP_COLOR_TEMPERATURE_MIREDS: u16 = 0x4010;

// ── Color Modes ──────────────────────────────────────────────────────

pub const COLOR_MODE_HUE_SAT: u8 = 0x00;
pub const COLOR_MODE_XY: u8 = 0x01;
pub const COLOR_MODE_COLOR_TEMP: u8 = 0x02;

// ── Enhanced Color Modes ─────────────────────────────────────────────

pub const ENHANCED_COLOR_MODE_HUE_SAT: u8 = 0x00;
pub const ENHANCED_COLOR_MODE_XY: u8 = 0x01;
pub const ENHANCED_COLOR_MODE_COLOR_TEMP: u8 = 0x02;
pub const ENHANCED_COLOR_MODE_ENHANCED_HUE_SAT: u8 = 0x03;

// ── Power Source ─────────────────────────────────────────────────────

pub const POWER_SOURCE_MAINS: u8 = 0x01;

// ── Scenes Command IDs ───────────────────────────────────────────────

// Defined in huesmith-core: the parser keys the transition-time unit off them.
pub use huesmith_core::hue::scene::{CMD_SCENES_ADD_SCENE, CMD_SCENES_ENHANCED_ADD_SCENE};

// ── ZCL Attribute Types ──────────────────────────────────────────────

pub const ZCL_ATTR_TYPE_BOOL: u8 = 0x10;
pub const ZCL_ATTR_TYPE_U8: u8 = 0x20;
pub const ZCL_ATTR_TYPE_U16: u8 = 0x21;
pub const ZCL_ATTR_TYPE_ENUM8: u8 = 0x30;

// ── ZCL Status Codes ─────────────────────────────────────────────────

pub const ZCL_STATUS_SUCCESS: u8 = 0x00;
pub const ZCL_STATUS_INVALID_FIELD: u8 = 0x85;

// ── BDB Commissioning Modes ─────────────────────────────────────────

pub const BDB_MODE_INITIALIZATION: u8 = 0;
pub const BDB_MODE_NETWORK_STEERING: u8 = 2;

// ── Zigbee Channel Masks ─────────────────────────────────────────────

pub const ALL_CHANNELS_MASK: u32 = 0x07FFF800; // channels 11-26

// ── ZLL Master Key (publicly known) ─────────────────────────────────

/// ZLL Commissioning Trust Centre Link Key (used by Hue Bridge for distributed security)
pub const ZLL_TC_LINK_KEY: [u8; 16] = [
    0x81, 0x42, 0x86, 0x86, 0x5D, 0xC1, 0xC8, 0xB2, 0xC8, 0xCB, 0xC5, 0x2E, 0x5D, 0x65, 0xD1, 0xB8,
];

// ── FFI Function Wrappers ────────────────────────────────────────────
// These wrap the esp_zigbee_* C functions from the ZBOSS SDK.
// All are unsafe because they call into C code with raw pointers.

/// uart_config_t from ESP-IDF driver/uart.h
#[repr(C)]
#[derive(Clone, Copy)]
pub struct uart_config_t {
    pub baud_rate: i32,
    pub data_bits: i32,
    pub parity: i32,
    pub stop_bits: i32,
    pub flow_ctrl: i32,
    pub rx_flow_ctrl_thresh: u8,
    _pad: [u8; 3],
    pub source_clk: i32,
}

/// esp_zb_uart_config_t from zb_config_platform.h
#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_uart_config_t {
    pub port: i32,
    pub rx_pin: i32,
    pub tx_pin: i32,
    pub uart_config: uart_config_t,
}

/// esp_zb_radio_config_t
#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_radio_config_t {
    pub radio_mode: u32,
    pub radio_uart_config: esp_zb_uart_config_t,
}

/// esp_zb_host_config_t
#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_host_config_t {
    pub host_connection_mode: u32,
    pub host_uart_config: esp_zb_uart_config_t,
}

/// esp_zb_platform_config_t
#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_platform_config_t {
    pub radio_config: esp_zb_radio_config_t,
    pub host_config: esp_zb_host_config_t,
}

/// Radio mode: native 802.15.4
pub const RADIO_MODE_NATIVE: u32 = 0;
/// Host connection: none (SoC mode)
pub const HOST_CONNECTION_MODE_NONE: u32 = 0;

extern "C" {
    // ── Stack Lifecycle ──────────────────────────────────────────────

    pub fn esp_zb_platform_config(config: *const esp_zb_platform_config_t) -> i32;
    pub fn esp_zb_init(cfg: *const esp_zb_cfg_t);
    pub fn esp_zb_set_primary_network_channel_set(channel_mask: u32);
    pub fn esp_zb_start(autostart: bool) -> i32;
    pub fn esp_zb_stack_main_loop();
    pub fn esp_zb_bdb_start_top_level_commissioning(mode: u8) -> i32;
    pub fn esp_zb_bdb_is_factory_new() -> bool;
    pub fn esp_zb_secur_TC_standard_distributed_key_set(key: *mut u8);
    pub fn esp_zb_enable_joining_to_distributed(enable: bool);
    pub fn esp_zb_get_short_address() -> u16;
    pub fn esp_zb_get_pan_id() -> u16;
    pub fn esp_zb_get_current_channel() -> u8;
    pub fn esp_zb_get_long_address(addr: *mut u8);
    pub fn esp_zb_set_long_address(addr: *const u8);
    pub fn esp_zb_nvram_erase_at_start(erase: bool);
    pub fn esp_zb_io_buffer_size_set(size: u16) -> i32;
    pub fn esp_zb_scheduler_queue_size_set(size: u16) -> i32;

    pub fn esp_zb_scheduler_alarm(cb: unsafe extern "C" fn(u8), param: u8, timeout_ms: u32);

    // ── Cluster List Management ──────────────────────────────────────

    pub fn esp_zb_zcl_cluster_list_create() -> *mut esp_zb_cluster_list_t;

    pub fn esp_zb_basic_cluster_create(
        cfg: *const esp_zb_basic_cluster_cfg_t,
    ) -> *mut esp_zb_attribute_list_t;
    pub fn esp_zb_identify_cluster_create(
        cfg: *const esp_zb_identify_cluster_cfg_t,
    ) -> *mut esp_zb_attribute_list_t;
    pub fn esp_zb_groups_cluster_create(
        cfg: *const esp_zb_groups_cluster_cfg_t,
    ) -> *mut esp_zb_attribute_list_t;
    pub fn esp_zb_scenes_cluster_create(
        cfg: *const esp_zb_scenes_cluster_cfg_t,
    ) -> *mut esp_zb_attribute_list_t;
    pub fn esp_zb_on_off_cluster_create(
        cfg: *const esp_zb_on_off_cluster_cfg_t,
    ) -> *mut esp_zb_attribute_list_t;
    pub fn esp_zb_level_cluster_create(
        cfg: *const esp_zb_level_cluster_cfg_t,
    ) -> *mut esp_zb_attribute_list_t;
    pub fn esp_zb_color_control_cluster_create(
        cfg: *const esp_zb_color_control_cluster_cfg_t,
    ) -> *mut esp_zb_attribute_list_t;
    pub fn esp_zb_ota_cluster_create(
        cfg: *const esp_zb_ota_cluster_cfg_t,
    ) -> *mut esp_zb_attribute_list_t;
    /// Touchlink Commissioning cluster (0x1000). The cfg pointer is ignored
    /// (empty struct); pass null.
    pub fn esp_zb_touchlink_commissioning_cluster_create(
        cfg: *const core::ffi::c_void,
    ) -> *mut esp_zb_attribute_list_t;

    pub fn esp_zb_cluster_add_attr(
        cluster: *mut esp_zb_attribute_list_t,
        cluster_id: u16,
        attr_id: u16,
        attr_type: u8,
        access: u8,
        value: *mut core::ffi::c_void,
    ) -> i32;

    pub fn esp_zb_cluster_update_attr(
        attr_list: *mut esp_zb_attribute_list_t,
        attr_id: u16,
        value: *mut core::ffi::c_void,
    ) -> i32;

    pub fn esp_zb_basic_cluster_add_attr(
        attr_list: *mut esp_zb_attribute_list_t,
        attr_id: u16,
        value: *mut core::ffi::c_void,
    ) -> i32;

    pub fn esp_zb_cluster_list_add_basic_cluster(
        list: *mut esp_zb_cluster_list_t,
        cluster: *mut esp_zb_attribute_list_t,
        role: u8,
    ) -> i32;
    pub fn esp_zb_cluster_list_add_identify_cluster(
        list: *mut esp_zb_cluster_list_t,
        cluster: *mut esp_zb_attribute_list_t,
        role: u8,
    ) -> i32;
    pub fn esp_zb_cluster_list_add_groups_cluster(
        list: *mut esp_zb_cluster_list_t,
        cluster: *mut esp_zb_attribute_list_t,
        role: u8,
    ) -> i32;
    pub fn esp_zb_cluster_list_add_scenes_cluster(
        list: *mut esp_zb_cluster_list_t,
        cluster: *mut esp_zb_attribute_list_t,
        role: u8,
    ) -> i32;
    pub fn esp_zb_cluster_list_add_on_off_cluster(
        list: *mut esp_zb_cluster_list_t,
        cluster: *mut esp_zb_attribute_list_t,
        role: u8,
    ) -> i32;
    pub fn esp_zb_cluster_list_add_level_cluster(
        list: *mut esp_zb_cluster_list_t,
        cluster: *mut esp_zb_attribute_list_t,
        role: u8,
    ) -> i32;
    pub fn esp_zb_cluster_list_add_color_control_cluster(
        list: *mut esp_zb_cluster_list_t,
        cluster: *mut esp_zb_attribute_list_t,
        role: u8,
    ) -> i32;
    pub fn esp_zb_cluster_list_add_ota_cluster(
        list: *mut esp_zb_cluster_list_t,
        cluster: *mut esp_zb_attribute_list_t,
        role: u8,
    ) -> i32;
    pub fn esp_zb_cluster_list_add_touchlink_commissioning_cluster(
        list: *mut esp_zb_cluster_list_t,
        cluster: *mut esp_zb_attribute_list_t,
        role: u8,
    ) -> i32;

    // ── Endpoint Management ──────────────────────────────────────────

    pub fn esp_zb_ep_list_create() -> *mut esp_zb_ep_list_t;
    pub fn esp_zb_ep_list_add_ep(
        ep_list: *mut esp_zb_ep_list_t,
        cluster_list: *mut esp_zb_cluster_list_t,
        endpoint_cfg: esp_zb_endpoint_config_t,
    ) -> i32;
    pub fn esp_zb_device_register(ep_list: *mut esp_zb_ep_list_t) -> i32;

    // ── Attribute Operations ─────────────────────────────────────────

    pub fn esp_zb_zcl_set_attribute_val(
        endpoint: u8,
        cluster_id: u16,
        cluster_role: u8,
        attr_id: u16,
        value: *const core::ffi::c_void,
        check: bool,
    ) -> i32;

    pub fn esp_zb_zcl_get_attribute(
        endpoint: u8,
        cluster_id: u16,
        cluster_role: u8,
        attr_id: u16,
    ) -> *mut esp_zb_zcl_attr_t;

    // ── Reporting ────────────────────────────────────────────────────

    pub fn esp_zb_zcl_start_attr_reporting(attr_info: esp_zb_zcl_attr_location_info_t) -> i32;
    pub fn esp_zb_zcl_update_reporting_info(info: *mut esp_zb_zcl_reporting_info_t) -> i32;

    // ── Scenes / Privilege Commands ─────────────────────────────────

    pub fn esp_zb_zcl_add_privilege_command(endpoint: u8, cluster: u16, command: u16) -> i32;

    pub fn esp_zb_zcl_scenes_table_store(
        endpoint: u8,
        group_id: u16,
        scene_id: u8,
        transition_time: u16,
        field: *mut esp_zb_zcl_scenes_extension_field_t,
    ) -> i32;

    pub fn esp_zb_zcl_scenes_table_show(endpoint: u8);

    // ── APS Raw Send ────────────────────────────────────────────────

    pub fn esp_zb_aps_data_request(req: *mut esp_zb_apsde_data_req_t) -> i32;
}

// ── Cluster Config Structs ───────────────────────────────────────────
// These mirror the C structs from esp-zigbee-sdk

#[repr(C)]
pub struct esp_zb_cfg_t {
    pub esp_zb_role: u32,
    pub install_code_policy: bool,
    pub nwk_cfg: esp_zb_nwk_cfg_t,
}

/// Union of zczr_cfg (1 byte) and zed_cfg (8 bytes).
/// Alignment 4 required to match C union containing uint32_t.
#[repr(C, align(4))]
pub struct esp_zb_nwk_cfg_t {
    _data: [u8; 8],
}

impl esp_zb_nwk_cfg_t {
    pub fn zczr(max_children: u8) -> Self {
        let mut data = [0u8; 8];
        data[0] = max_children;
        Self { _data: data }
    }
}

// Device role: the library always registers as a router (mains-powered light).
pub const ZB_ROUTER: u32 = 1;

#[repr(C)]
pub struct esp_zb_basic_cluster_cfg_t {
    pub zcl_version: u8,
    pub power_source: u8,
}

#[repr(C)]
pub struct esp_zb_identify_cluster_cfg_t {
    pub identify_time: u16,
}

#[repr(C)]
pub struct esp_zb_groups_cluster_cfg_t {
    pub groups_name_support_id: u8,
}

#[repr(C)]
pub struct esp_zb_scenes_cluster_cfg_t {
    pub scenes_count: u8,
    pub current_scene: u8,
    pub current_group: u16,
    pub scene_valid: bool,
    pub name_support: u8,
}

#[repr(C)]
pub struct esp_zb_on_off_cluster_cfg_t {
    pub on_off: bool,
}

#[repr(C)]
pub struct esp_zb_level_cluster_cfg_t {
    pub current_level: u8,
}

/// C name: esp_zb_color_cluster_cfg_t (NOT color_control)
#[repr(C)]
pub struct esp_zb_color_control_cluster_cfg_t {
    pub current_x: u16,
    pub current_y: u16,
    pub color_mode: u8,
    pub options: u8,
    pub enhanced_color_mode: u8,
    pub color_capabilities: u16,
}

// Endpoint config — MUST be packed to match C: ESP_ZB_PACKED_STRUCT with uint32_t bitfield
#[repr(C, packed)]
#[derive(Clone, Copy)]
pub struct esp_zb_endpoint_config_t {
    pub endpoint: u8,
    pub app_profile_id: u16,
    pub app_device_id: u16,
    /// C type is `uint32_t app_device_version: 4` — 4-bit bitfield, stored in 1 byte when packed
    pub app_device_version: u8,
}

// Cluster role
pub const ZB_ZCL_CLUSTER_SERVER_ROLE: u8 = 0x01;

// ZCL report direction
pub const ZB_ZCL_REPORT_DIRECTION_SEND: u8 = 0x00;

// Manufacturer code marker for standard, non-manufacturer-specific attributes.
pub const ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC: u16 = 0xFFFF;

// Attribute access
pub const ZB_ZCL_ATTR_ACCESS_READ_ONLY: u8 = 0x01;
pub const ZB_ZCL_ATTR_ACCESS_READ_WRITE: u8 = 0x03;
pub const ZB_ZCL_ATTR_ACCESS_REPORTING: u8 = 0x04;

// Opaque types from C (we only use pointers)
#[repr(C)]
pub struct esp_zb_cluster_list_t {
    _opaque: [u8; 0],
}

#[repr(C)]
pub struct esp_zb_attribute_list_t {
    _opaque: [u8; 0],
}

#[repr(C)]
pub struct esp_zb_ep_list_t {
    _opaque: [u8; 0],
}

// PACKED in C (ESP_ZB_PACKED_STRUCT)
#[repr(C, packed)]
pub struct esp_zb_zcl_attr_t {
    pub id: u16,
    pub type_: u8,
    pub access: u8,
    pub manuf_code: u16,
    pub data_p: *mut core::ffi::c_void,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_zcl_attr_location_info_t {
    pub endpoint_id: u8,
    pub cluster_id: u16,
    pub cluster_role: u8,
    pub manuf_code: u16,
    pub attr_id: u16,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub union esp_zb_zcl_attr_var_u {
    pub u8_: u8,
    pub s8_: i8,
    pub u16_: u16,
    pub s16_: i16,
    pub u32_: u32,
    pub s32_: i32,
    pub u48_: [u8; 6],
    pub data_buf: [u8; 4],
}

impl esp_zb_zcl_attr_var_u {
    pub const fn zero() -> Self {
        Self { data_buf: [0; 4] }
    }

    pub const fn from_u8(value: u8) -> Self {
        Self { u8_: value }
    }

    pub const fn from_u16(value: u16) -> Self {
        Self { u16_: value }
    }
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_zcl_reporting_send_info_t {
    pub min_interval: u16,
    pub max_interval: u16,
    pub delta: esp_zb_zcl_attr_var_u,
    pub reported_value: esp_zb_zcl_attr_var_u,
    pub def_min_interval: u16,
    pub def_max_interval: u16,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_zcl_reporting_recv_info_t {
    pub timeout: u16,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub union esp_zb_zcl_reporting_info_u {
    pub send_info: esp_zb_zcl_reporting_send_info_t,
    pub recv_info: esp_zb_zcl_reporting_recv_info_t,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_zcl_reporting_dst_t {
    pub short_addr: u16,
    pub endpoint: u8,
    pub profile_id: u16,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_zcl_reporting_info_t {
    pub direction: u8,
    pub ep: u8,
    pub cluster_id: u16,
    pub cluster_role: u8,
    pub attr_id: u16,
    pub flags: u8,
    pub run_time: u64,
    pub u: esp_zb_zcl_reporting_info_u,
    pub dst: esp_zb_zcl_reporting_dst_t,
    pub manuf_code: u16,
}

// ── Signal Types (from esp_zigbee_zdo_common.h) ───────────────────────
pub const ESP_ZB_ZDO_SIGNAL_SKIP_STARTUP: u32 = 0x01;
pub const ESP_ZB_ZDO_SIGNAL_LEAVE: u32 = 0x03;
pub const ESP_ZB_BDB_SIGNAL_DEVICE_FIRST_START: u32 = 0x05;
pub const ESP_ZB_BDB_SIGNAL_DEVICE_REBOOT: u32 = 0x06;
pub const ESP_ZB_BDB_SIGNAL_TOUCHLINK_NWK_STARTED: u32 = 0x07;
pub const ESP_ZB_BDB_SIGNAL_TOUCHLINK_NWK_JOINED_ROUTER: u32 = 0x08;
pub const ESP_ZB_BDB_SIGNAL_STEERING: u32 = 0x0a;
pub const ESP_ZB_COMMON_SIGNAL_CAN_SLEEP: u32 = 0x16;
// Values verified against components/esp-zigbee-lib/include/zdo/esp_zigbee_zdo_common.h.
pub const ESP_ZB_ZDO_SIGNAL_DEVICE_ANNCE: u32 = 0x02; // A new device announced itself
pub const ESP_ZB_MACSPLIT_DEVICE_BOOT: u32 = 0x2b; // MAC co-processor boot
pub const ESP_ZB_NLME_STATUS_INDICATION: u32 = 0x32; // NWK status (route/delivery problem)
pub const ESP_ZB_NWK_SIGNAL_PERMIT_JOIN_STATUS: u32 = 0x36; // Permit-join window changed
pub const ESP_ZB_ZDO_DEVICE_UNAVAILABLE: u32 = 0x3c; // A neighbour became unreachable

/// Application signal struct passed to esp_zb_app_signal_handler
#[repr(C)]
pub struct esp_zb_app_signal_t {
    pub p_app_signal: *mut u32,
    pub esp_err_status: i32,
}

// ── Cluster Role ─────────────────────────────────────────────────────

pub const ZB_ZCL_CLUSTER_CLIENT_ROLE: u8 = 0x02; // from zboss_api_af.h ZB_ZCL_CLUSTER_CLIENT_ROLE

// ── Action Callback Types ────────────────────────────────────────────

// `esp_zb_core_action_callback_id_t` is a C enum, i.e. int-sized: the callback
// parameter and these constants must be u32, even though all values fit in 16 bits.
pub const ESP_ZB_CORE_SET_ATTR_VALUE_CB_ID: u32 = 0x0000;
pub const ESP_ZB_CORE_SCENES_STORE_SCENE_CB_ID: u32 = 0x0001;
pub const ESP_ZB_CORE_SCENES_RECALL_SCENE_CB_ID: u32 = 0x0002;
pub const ESP_ZB_CORE_IDENTIFY_EFFECT_CB_ID: u32 = 0x0012; // TriggerEffect command on Identify cluster
pub const ESP_ZB_CORE_CMD_PRIVILEGE_COMMAND_REQ_CB_ID: u32 = 0x1050;

// ── Callback message types (matching C headers exactly) ──────────────

/// esp_zb_device_cb_common_info_t — NOT packed in C
/// CRITICAL: `status` is esp_zb_zcl_status_t which is a C enum = int = 4 bytes on RISC-V!
#[repr(C)]
pub struct esp_zb_device_cb_common_info_t {
    pub status: u32, // esp_zb_zcl_status_t (C enum = 4 bytes!)
    pub dst_endpoint: u8,
    // 1 byte padding (u16 alignment)
    pub cluster: u16,
}

/// esp_zb_zcl_attribute_data_t — PACKED in C (ESP_ZB_PACKED_STRUCT)
/// CRITICAL: `type_` is esp_zb_zcl_attr_type_t which is a C enum = 4 bytes on RISC-V!
#[repr(C, packed)]
pub struct esp_zb_zcl_attribute_data_t {
    pub type_: u32, // esp_zb_zcl_attr_type_t (C enum = 4 bytes!)
    pub size: u16,
    pub value: *mut core::ffi::c_void,
}

/// esp_zb_zcl_attribute_t — NOT packed in C
#[repr(C)]
pub struct esp_zb_zcl_attribute_t {
    pub id: u16,
    pub data: esp_zb_zcl_attribute_data_t,
}

/// esp_zb_zcl_set_attr_value_message_t — NOT packed in C
#[repr(C)]
pub struct esp_zb_zcl_set_attr_value_message_t {
    pub info: esp_zb_device_cb_common_info_t,
    pub attribute: esp_zb_zcl_attribute_t,
}

/// esp_zb_zcl_identify_effect_message_t — NOT packed in C
/// Delivered for ESP_ZB_CORE_IDENTIFY_EFFECT_CB_ID (0x0012).
#[repr(C)]
pub struct esp_zb_zcl_identify_effect_message_t {
    pub info: esp_zb_device_cb_common_info_t,
    pub effect_id: u8,
    pub effect_variant: u8,
}

/// Mandatory attribute config for the OTA Upgrade client cluster (0x0019).
/// Layout verified against esp_zigbee_type.h esp_zb_ota_cluster_cfg_s.
#[repr(C)]
pub struct esp_zb_ota_cluster_cfg_t {
    pub ota_upgrade_file_version: u32,
    pub ota_upgrade_manufacturer: u16,
    pub ota_upgrade_image_type: u16,
    pub ota_min_block_reque: u16,
    // 2 bytes implicit padding (repr(C) aligns next u32 to offset 12)
    pub ota_upgrade_file_offset: u32,
    pub ota_upgrade_downloaded_file_ver: u32,
    pub ota_upgrade_server_id: [u8; 8],
    pub ota_image_upgrade_status: u8,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_zcl_command_t {
    pub id: u8,
    pub direction: u8,
    pub is_common: u8,
}

#[repr(C)]
pub struct esp_zb_zcl_store_scene_message_t {
    pub info: esp_zb_device_cb_common_info_t,
    pub command: esp_zb_zcl_command_t,
    pub group_id: u16,
    pub scene_id: u8,
}

#[repr(C)]
pub struct esp_zb_zcl_recall_scene_message_t {
    pub info: esp_zb_device_cb_common_info_t,
    pub command: esp_zb_zcl_command_t,
    pub group_id: u16,
    pub scene_id: u8,
    pub transition_time: u16,
    pub field_set: *mut esp_zb_zcl_scenes_extension_field_t,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_zcl_scenes_extension_field_t {
    pub cluster_id: u16,
    pub length: u8,
    pub extension_field_attribute_value_list: *mut u8,
    pub next: *mut esp_zb_zcl_scenes_extension_field_t,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub union esp_zb_addr_u {
    pub addr_short: u16,
    pub addr_long: [u8; 8],
}

#[repr(C, packed)]
#[derive(Clone, Copy)]
pub struct esp_zb_zcl_addr_t {
    pub addr_type: u8,
    pub u: [u8; 8],
}

impl esp_zb_zcl_addr_t {
    pub fn short_addr(&self) -> Option<u16> {
        if self.addr_type == 0 {
            let base = core::ptr::addr_of!(self.u) as *const u8;
            let lo = unsafe { base.read_unaligned() };
            let hi = unsafe { base.add(1).read_unaligned() };
            Some(u16::from_le_bytes([lo, hi]))
        } else {
            None
        }
    }
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_zcl_frame_header_t {
    pub fc: u8,
    pub manuf_code: u16,
    pub tsn: u8,
    pub rssi: i8,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_zcl_cmd_info_t {
    pub status: u32,
    pub header: esp_zb_zcl_frame_header_t,
    pub src_address: esp_zb_zcl_addr_t,
    pub dst_address: u16,
    pub src_endpoint: u8,
    pub dst_endpoint: u8,
    pub cluster: u16,
    pub profile: u16,
    pub command: esp_zb_zcl_command_t,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct esp_zb_zcl_privilege_command_message_t {
    pub info: esp_zb_zcl_cmd_info_t,
    pub size: u16,
    pub data: *mut core::ffi::c_void,
}

pub const ESP_ZB_APS_ADDR_MODE_16_ENDP_PRESENT: u8 = 0x02;
pub const ESP_ZB_APSDE_TX_OPT_SECURITY_ENABLED: u8 = 0x01;
pub const ESP_ZB_APSDE_TX_OPT_ACK_TX: u8 = 0x04;

#[repr(C)]
pub struct esp_zb_apsde_data_req_t {
    pub dst_addr_mode: u8,
    pub dst_addr: esp_zb_addr_u,
    pub dst_endpoint: u8,
    pub profile_id: u16,
    pub cluster_id: u16,
    pub src_endpoint: u8,
    pub asdu_length: u32,
    pub asdu: *mut u8,
    pub tx_options: u8,
    pub use_alias: bool,
    pub alias_src_addr: u16,
    pub alias_seq_num: i32,
    pub radius: u8,
}

extern "C" {
    /// Create an empty attribute list for a given cluster ID.
    /// Used to register a cluster with no attributes (e.g., ZLL Commissioning 0x1000).
    pub fn esp_zb_zcl_attr_list_create(cluster_id: u16) -> *mut esp_zb_attribute_list_t;

    /// Add a custom (non-standard or empty) cluster to a cluster list.
    pub fn esp_zb_cluster_list_add_custom_cluster(
        cluster_list: *mut esp_zb_cluster_list_t,
        attr_list: *mut esp_zb_attribute_list_t,
        role: u8,
    ) -> i32;
}

extern "C" {
    /// C: `esp_err_t (*esp_zb_core_action_callback_t)(esp_zb_core_action_callback_id_t, const void *)`
    /// (esp_zigbee_core.h). The callback-id parameter is a C enum — int-sized,
    /// so it must be `u32` here; declaring it `u16` is a function-type mismatch (UB).
    pub fn esp_zb_core_action_handler_register(
        callback: Option<
            unsafe extern "C" fn(callback_id: u32, message: *const core::ffi::c_void) -> i32,
        >,
    );
}

// ── Layout guards ────────────────────────────────────────────────────
// Compile-time checks that the hand-written layout of the remote-reachable
// SET_ATTR message path matches what the C stack writes into it. A future edit
// that perturbs field order or width — e.g. dropping the u32 C-enum width of
// `status`/`type_` back to u8 (see docs/FFI-SAFETY.md rule on RISC-V enum size)
// — fails the build here instead of silently misparsing radio input at runtime.
// Only offsets that precede any pointer field are asserted, so they are
// independent of the target's pointer width.
const _: () = {
    // esp_zb_device_cb_common_info_t: `status` is a C enum = 4 bytes on RISC-V.
    assert!(core::mem::offset_of!(esp_zb_device_cb_common_info_t, status) == 0);
    assert!(core::mem::offset_of!(esp_zb_device_cb_common_info_t, dst_endpoint) == 4);
    assert!(core::mem::offset_of!(esp_zb_device_cb_common_info_t, cluster) == 6);
    assert!(core::mem::size_of::<esp_zb_device_cb_common_info_t>() == 8);

    // esp_zb_zcl_attribute_data_t (packed): `type_` is a C enum = 4 bytes.
    assert!(core::mem::offset_of!(esp_zb_zcl_attribute_data_t, type_) == 0);
    assert!(core::mem::offset_of!(esp_zb_zcl_attribute_data_t, size) == 4);
    assert!(core::mem::offset_of!(esp_zb_zcl_attribute_data_t, value) == 6);

    // esp_zb_zcl_attribute_t: `id` then the packed data struct.
    assert!(core::mem::offset_of!(esp_zb_zcl_attribute_t, id) == 0);
    assert!(core::mem::offset_of!(esp_zb_zcl_attribute_t, data) == 2);

    // esp_zb_zcl_set_attr_value_message_t: common info then the attribute — the
    // exact struct command.rs reads its raw attribute pointer out of.
    assert!(core::mem::offset_of!(esp_zb_zcl_set_attr_value_message_t, info) == 0);
    assert!(core::mem::offset_of!(esp_zb_zcl_set_attr_value_message_t, attribute) == 8);

    // esp_zb_zcl_cmd_info_t — the other remote-reachable header, read field-wise
    // by the scenes/privilege path in scene.rs (status, cluster, command.id,
    // src_address, src/dst_endpoint, profile). `header` at offset 4 is the guard
    // that proves `status` kept its 4-byte C-enum width: narrow it to u8 and
    // `header` slides to 2, `command` to 26, and size shrinks — all trip here.
    // Layout (RV32): status u32@0, header(size6,align2)@4, src_address(packed
    // size9,align1)@10, dst_address u16@20 (pad@19), src_ep@22, dst_ep@23,
    // cluster u16@24, profile u16@26, command(size3,align1)@28 → size 32.
    assert!(core::mem::size_of::<esp_zb_zcl_frame_header_t>() == 6);
    assert!(core::mem::offset_of!(esp_zb_zcl_cmd_info_t, status) == 0);
    assert!(core::mem::offset_of!(esp_zb_zcl_cmd_info_t, header) == 4);
    assert!(core::mem::offset_of!(esp_zb_zcl_cmd_info_t, cluster) == 24);
    assert!(core::mem::offset_of!(esp_zb_zcl_cmd_info_t, command) == 28);
    assert!(core::mem::size_of::<esp_zb_zcl_cmd_info_t>() == 32);
};