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
use super::ffi;
use huesmith_core::hue::scene::{
    fields_from_scene_state, parse_add_scene_payload, read_u16_le, scene_state_from_fields,
    SceneField, MAX_SCENE_FIELDS,
};
use huesmith_core::light::state::{ColorMode, LightState, SceneState};

const MAX_SCENE_RESPONSE_ASDU: usize = 16;
const ZCL_FRAME_CONTROL_CLUSTER_RESPONSE: u8 = 0x19;

static mut SCENE_RESPONSE_ASDU: [u8; MAX_SCENE_RESPONSE_ASDU] = [0; MAX_SCENE_RESPONSE_ASDU];

/// Register the scene commands that ZBOSS 1.6 cannot safely parse from Hue payloads.
///
/// The stock ZBOSS scene parser asserts/crashes on certain Hue-generated Add Scene
/// payloads (especially named scenes or specific extension fields), so we intercept
/// Add Scene (0x00) and Enhanced Add Scene (0x40) as privilege commands and parse
/// them defensively via [`huesmith_core::hue::scene`] instead.
///
/// Flow:
///   Hue Bridge → Add Scene (0x00) / Enhanced (0x40)
///   → privilege command callback (registered here)
///   → `parse_add_scene_payload` (defensive, handles named + unnamed)
///   → `store_scene_fields` (builds the ZBOSS extension-field linked list)
///   → later Recall Scene → `fields_from_extension_list` → `apply_scene_state`
pub fn register_privilege_commands(endpoint: u8) {
    for command in [
        ffi::CMD_SCENES_ADD_SCENE,
        ffi::CMD_SCENES_ENHANCED_ADD_SCENE,
    ] {
        let ret = unsafe {
            ffi::esp_zb_zcl_add_privilege_command(endpoint, ffi::CLUSTER_SCENES, command as u16)
        };
        log::info!(
            "Scenes privilege command: ep={} cluster=0x{:04X} cmd=0x{:02X} ret={}",
            endpoint,
            ffi::CLUSTER_SCENES,
            command,
            ret,
        );
    }
}

/// # Safety
///
/// `message` must be the `esp_zb_zcl_privilege_command_message_t` pointer the
/// stack passes for `ESP_ZB_CORE_CMD_PRIVILEGE_COMMAND_REQ_CB_ID` (or null);
/// it and its `data` buffer are only valid for the duration of the callback.
pub unsafe fn handle_privilege_command(message: *const core::ffi::c_void) -> i32 {
    if message.is_null() {
        log::warn!("Scenes privilege command: NULL message");
        return 0;
    }

    // SAFETY: non-null checked above; layout per callback-id contract (see doc).
    let msg = &*(message as *const ffi::esp_zb_zcl_privilege_command_message_t);
    if msg.info.cluster != ffi::CLUSTER_SCENES {
        return 0;
    }

    let cmd_id = msg.info.command.id;
    if cmd_id != ffi::CMD_SCENES_ADD_SCENE && cmd_id != ffi::CMD_SCENES_ENHANCED_ADD_SCENE {
        log::debug!("Unhandled privileged scene command: 0x{:02X}", cmd_id);
        return 0;
    }

    let payload = if msg.data.is_null() || msg.size == 0 {
        &[]
    } else {
        core::slice::from_raw_parts(msg.data as *const u8, msg.size as usize)
    };

    // Log the raw payload for Add Scene (useful when debugging Hue scenes).
    if !payload.is_empty() {
        log::debug!(
            "Add Scene raw payload (len={}): {:02X?}",
            payload.len(),
            &payload[..payload.len().min(32)]
        );
    }

    let fallback_group = read_u16_le(payload, 0).unwrap_or(0);
    let fallback_scene = payload.get(2).copied().unwrap_or(0);

    // parse_add_scene_payload normalizes the transition time to tenths of a
    // second based on the command variant (Add Scene carries whole seconds).
    let status = match parse_add_scene_payload(payload, cmd_id) {
        Ok(parsed) => {
            log::info!(
                "Add Scene: group=0x{:04X} scene={} transition={} fields={} cmd=0x{:02X}",
                parsed.group_id,
                parsed.scene_id,
                parsed.transition_time,
                parsed.fields.len(),
                cmd_id,
            );

            let ret = store_scene_fields(
                msg.info.dst_endpoint,
                parsed.group_id,
                parsed.scene_id,
                parsed.transition_time,
                &parsed.fields,
            );
            if ret == 0 {
                ffi::ZCL_STATUS_SUCCESS
            } else {
                log::warn!(
                    "Add Scene store failed: group=0x{:04X} scene={} ret={}",
                    parsed.group_id,
                    parsed.scene_id,
                    ret,
                );
                ffi::ZCL_STATUS_INVALID_FIELD
            }
        }
        Err(err) => {
            log::warn!(
                "Add Scene rejected: cmd=0x{:02X} size={} ({err})",
                cmd_id,
                payload.len(),
            );
            err.zcl_status()
        }
    };

    let response_cmd = if cmd_id == ffi::CMD_SCENES_ENHANCED_ADD_SCENE {
        ffi::CMD_SCENES_ENHANCED_ADD_SCENE
    } else {
        ffi::CMD_SCENES_ADD_SCENE
    };
    send_operate_scene_response(
        &msg.info,
        response_cmd,
        status,
        fallback_group,
        fallback_scene,
    );

    0
}

/// # Safety
///
/// `message` must be the `esp_zb_zcl_store_scene_message_t` pointer the stack
/// passes for `ESP_ZB_CORE_SCENES_STORE_SCENE_CB_ID` (or null), valid for the
/// duration of the callback. Must run on the single ZBOSS task so the
/// `light_state` RefCell borrow cannot alias.
pub unsafe fn handle_store_scene(
    message: *const core::ffi::c_void,
    light_state: Option<&core::cell::RefCell<LightState>>,
    has_color_control: bool,
) -> i32 {
    if message.is_null() {
        log::warn!("Store Scene: NULL message");
        return -1;
    }

    // SAFETY: non-null checked above; layout per callback-id contract (see doc).
    let msg = &*(message as *const ffi::esp_zb_zcl_store_scene_message_t);
    if msg.info.status != ffi::ZCL_STATUS_SUCCESS as u32 {
        log::warn!(
            "Store Scene failed before app handler: status={}",
            msg.info.status
        );
        return -1;
    }

    let Some(light_state) = light_state else {
        log::warn!("Store Scene: light state unavailable");
        return -1;
    };

    let snapshot = light_state.borrow().scene_snapshot();
    let fields = fields_from_scene_state(&snapshot, has_color_control);
    let ret = store_scene_fields(
        msg.info.dst_endpoint,
        msg.group_id,
        msg.scene_id,
        0,
        &fields,
    );

    log::info!(
        "Store Scene: group=0x{:04X} scene={} fields={} ret={}",
        msg.group_id,
        msg.scene_id,
        fields.len(),
        ret,
    );
    ret
}

/// # Safety
///
/// `message` must be the `esp_zb_zcl_recall_scene_message_t` pointer the stack
/// passes for `ESP_ZB_CORE_SCENES_RECALL_SCENE_CB_ID` (or null); it and its
/// `field_set` linked list are only valid for the duration of the callback.
/// Must run on the single ZBOSS task so the RefCell borrow cannot alias.
pub unsafe fn handle_recall_scene(
    message: *const core::ffi::c_void,
    light_state: Option<&core::cell::RefCell<LightState>>,
    has_color_control: bool,
) -> i32 {
    if message.is_null() {
        log::warn!("Recall Scene: NULL message");
        return 0;
    }

    // SAFETY: non-null checked above; layout per callback-id contract (see doc).
    let msg = &*(message as *const ffi::esp_zb_zcl_recall_scene_message_t);
    if msg.info.status != ffi::ZCL_STATUS_SUCCESS as u32 {
        log::warn!(
            "Recall Scene failed before app handler: status={}",
            msg.info.status
        );
        return 0;
    }

    let fields = fields_from_extension_list(msg.field_set);
    let scene = scene_state_from_fields(&fields);

    log::info!(
        "Recall Scene: group=0x{:04X} scene={} transition={} fields={}",
        msg.group_id,
        msg.scene_id,
        msg.transition_time,
        fields.len(),
    );

    if let Some(light_state) = light_state {
        light_state
            .borrow_mut()
            .apply_scene_state_with_transition(&scene, msg.transition_time);
        sync_zcl_attributes(msg.info.dst_endpoint, &scene, has_color_control);
    } else {
        log::warn!("Recall Scene: light state unavailable");
    }

    0
}

/// Owns the byte data and the C extension-field nodes for scene storage.
///
/// SAFETY:
/// - All pointers (`extension_field_attribute_value_list` and `next`) are only
///   created *after* the owning Vecs reach their final capacity (no reallocations).
/// - The linked list is only valid for the duration of the call to
///   `esp_zb_zcl_scenes_table_store`.
/// - This is the recommended pattern when the C API requires a linked list of
///   caller-owned structures.
struct SceneExtensionFieldList {
    // Keep the data alive for the lifetime of the nodes
    _owned_data: Vec<Vec<u8>>,
    nodes: Vec<ffi::esp_zb_zcl_scenes_extension_field_t>,
}

impl SceneExtensionFieldList {
    fn from_fields(fields: &[SceneField]) -> Self {
        // Reserve exact capacity to minimize chance of reallocation after pointers are taken
        let mut owned_data: Vec<Vec<u8>> = Vec::with_capacity(fields.len());
        owned_data.extend(fields.iter().map(|f| f.data.clone()));

        let mut nodes: Vec<ffi::esp_zb_zcl_scenes_extension_field_t> =
            Vec::with_capacity(fields.len());
        nodes.extend(fields.iter().enumerate().map(|(i, field)| {
            ffi::esp_zb_zcl_scenes_extension_field_t {
                cluster_id: field.cluster_id,
                length: owned_data[i].len().min(u8::MAX as usize) as u8,
                extension_field_attribute_value_list: owned_data[i].as_mut_ptr(),
                next: core::ptr::null_mut(),
            }
        }));

        // Only after both vectors are fully built do we set internal pointers.
        // At this point no more pushes/resizes will occur for these allocations.
        for i in 0..nodes.len().saturating_sub(1) {
            nodes[i].next = unsafe { nodes.as_mut_ptr().add(i + 1) };
        }

        Self {
            _owned_data: owned_data,
            nodes,
        }
    }

    fn head(&mut self) -> *mut ffi::esp_zb_zcl_scenes_extension_field_t {
        self.nodes.first_mut().map_or(core::ptr::null_mut(), |n| {
            n as *mut ffi::esp_zb_zcl_scenes_extension_field_t
        })
    }
}

fn store_scene_fields(
    endpoint: u8,
    group_id: u16,
    scene_id: u8,
    transition_time: u16,
    fields: &[SceneField],
) -> i32 {
    let mut field_list = SceneExtensionFieldList::from_fields(fields);
    let head = field_list.head();

    let ret = unsafe {
        ffi::esp_zb_zcl_scenes_table_store(endpoint, group_id, scene_id, transition_time, head)
    };

    if ret == 0 {
        unsafe { ffi::esp_zb_zcl_scenes_table_show(endpoint) };
    }

    ret
}

fn fields_from_extension_list(
    mut field: *mut ffi::esp_zb_zcl_scenes_extension_field_t,
) -> Vec<SceneField> {
    let mut out = Vec::new();
    let mut guard = 0usize;

    while !field.is_null() && guard < MAX_SCENE_FIELDS {
        let item = unsafe { &*field };
        let data = if item.extension_field_attribute_value_list.is_null() || item.length == 0 {
            Vec::new()
        } else {
            unsafe {
                core::slice::from_raw_parts(
                    item.extension_field_attribute_value_list,
                    item.length as usize,
                )
            }
            .to_vec()
        };

        out.push(SceneField {
            cluster_id: item.cluster_id,
            data,
        });
        field = item.next;
        guard += 1;
    }

    out
}

fn sync_zcl_attributes(endpoint: u8, scene: &SceneState, has_color_control: bool) {
    if let Some(on) = scene.on {
        set_attr(
            endpoint,
            ffi::CLUSTER_ON_OFF,
            ffi::ATTR_ON_OFF,
            &on as *const bool as *const core::ffi::c_void,
        );
    }

    if let Some(level) = scene.brightness {
        set_attr(
            endpoint,
            ffi::CLUSTER_LEVEL_CONTROL,
            ffi::ATTR_LEVEL_CURRENT_LEVEL,
            &level as *const u8 as *const core::ffi::c_void,
        );
    }

    if !has_color_control {
        return;
    }

    sync_color_zcl_attributes(endpoint, scene);
}

fn sync_color_zcl_attributes(endpoint: u8, scene: &SceneState) {
    if let Some(x) = scene.color_x {
        set_attr(
            endpoint,
            ffi::CLUSTER_COLOR_CONTROL,
            ffi::ATTR_COLOR_CURRENT_X,
            &x as *const u16 as *const core::ffi::c_void,
        );
    }

    if let Some(y) = scene.color_y {
        set_attr(
            endpoint,
            ffi::CLUSTER_COLOR_CONTROL,
            ffi::ATTR_COLOR_CURRENT_Y,
            &y as *const u16 as *const core::ffi::c_void,
        );
    }

    if let Some(temp) = scene.color_temp_mireds {
        set_attr(
            endpoint,
            ffi::CLUSTER_COLOR_CONTROL,
            ffi::ATTR_COLOR_TEMPERATURE_MIREDS,
            &temp as *const u16 as *const core::ffi::c_void,
        );
    }

    if let Some(enhanced_hue) = scene.enhanced_hue {
        let hue = huesmith_core::color::enhanced_hue_to_hue(enhanced_hue);
        set_attr(
            endpoint,
            ffi::CLUSTER_COLOR_CONTROL,
            ffi::ATTR_COLOR_CURRENT_HUE,
            &hue as *const u8 as *const core::ffi::c_void,
        );
        set_attr(
            endpoint,
            ffi::CLUSTER_COLOR_CONTROL,
            ffi::ATTR_COLOR_ENHANCED_CURRENT_HUE,
            &enhanced_hue as *const u16 as *const core::ffi::c_void,
        );
    } else if let Some(hue) = scene.hue {
        set_attr(
            endpoint,
            ffi::CLUSTER_COLOR_CONTROL,
            ffi::ATTR_COLOR_CURRENT_HUE,
            &hue as *const u8 as *const core::ffi::c_void,
        );
    }

    if let Some(saturation) = scene.saturation {
        set_attr(
            endpoint,
            ffi::CLUSTER_COLOR_CONTROL,
            ffi::ATTR_COLOR_CURRENT_SATURATION,
            &saturation as *const u8 as *const core::ffi::c_void,
        );
    }

    if let Some(mode) = scene.color_mode {
        let color_mode = match mode {
            ColorMode::HueSaturation => ffi::COLOR_MODE_HUE_SAT,
            ColorMode::XY => ffi::COLOR_MODE_XY,
            ColorMode::ColorTemperature => ffi::COLOR_MODE_COLOR_TEMP,
        };
        let enhanced_mode = match mode {
            ColorMode::HueSaturation if scene.enhanced_hue.is_some() => {
                ffi::ENHANCED_COLOR_MODE_ENHANCED_HUE_SAT
            }
            ColorMode::HueSaturation => ffi::ENHANCED_COLOR_MODE_HUE_SAT,
            ColorMode::XY => ffi::ENHANCED_COLOR_MODE_XY,
            ColorMode::ColorTemperature => ffi::ENHANCED_COLOR_MODE_COLOR_TEMP,
        };

        set_attr(
            endpoint,
            ffi::CLUSTER_COLOR_CONTROL,
            ffi::ATTR_COLOR_MODE,
            &color_mode as *const u8 as *const core::ffi::c_void,
        );
        set_attr(
            endpoint,
            ffi::CLUSTER_COLOR_CONTROL,
            ffi::ATTR_COLOR_ENHANCED_COLOR_MODE,
            &enhanced_mode as *const u8 as *const core::ffi::c_void,
        );
    }
}

fn set_attr(endpoint: u8, cluster: u16, attr: u16, value: *const core::ffi::c_void) {
    let ret = unsafe {
        ffi::esp_zb_zcl_set_attribute_val(
            endpoint,
            cluster,
            ffi::ZB_ZCL_CLUSTER_SERVER_ROLE,
            attr,
            value,
            false,
        )
    };

    if ret != 0 {
        log::debug!(
            "Scene attribute sync failed: ep={} cluster=0x{:04X} attr=0x{:04X} ret={}",
            endpoint,
            cluster,
            attr,
            ret,
        );
    }
}

fn send_operate_scene_response(
    info: &ffi::esp_zb_zcl_cmd_info_t,
    command_id: u8,
    status: u8,
    group_id: u16,
    scene_id: u8,
) {
    let mut payload = Vec::with_capacity(4);
    payload.push(status);
    payload.extend_from_slice(&group_id.to_le_bytes());
    payload.push(scene_id);
    send_scene_response(info, command_id, &payload);
}

fn send_scene_response(info: &ffi::esp_zb_zcl_cmd_info_t, command_id: u8, payload: &[u8]) {
    let Some(dst_short) = info.src_address.short_addr() else {
        log::warn!(
            "Scene response skipped: unsupported source address type {}",
            info.src_address.addr_type,
        );
        return;
    };

    let asdu_length = 3usize + payload.len();
    if asdu_length > MAX_SCENE_RESPONSE_ASDU {
        log::warn!("Scene response skipped: ASDU too large ({})", asdu_length);
        return;
    }

    let asdu = core::ptr::addr_of_mut!(SCENE_RESPONSE_ASDU) as *mut u8;
    unsafe {
        asdu.add(0).write(ZCL_FRAME_CONTROL_CLUSTER_RESPONSE);
        asdu.add(1).write(info.header.tsn);
        asdu.add(2).write(command_id);
        core::ptr::copy_nonoverlapping(payload.as_ptr(), asdu.add(3), payload.len());
    }

    let mut req = ffi::esp_zb_apsde_data_req_t {
        dst_addr_mode: ffi::ESP_ZB_APS_ADDR_MODE_16_ENDP_PRESENT,
        dst_addr: ffi::esp_zb_addr_u {
            addr_short: dst_short,
        },
        dst_endpoint: info.src_endpoint,
        profile_id: info.profile,
        cluster_id: ffi::CLUSTER_SCENES,
        src_endpoint: info.dst_endpoint,
        asdu_length: asdu_length as u32,
        asdu,
        tx_options: ffi::ESP_ZB_APSDE_TX_OPT_SECURITY_ENABLED | ffi::ESP_ZB_APSDE_TX_OPT_ACK_TX,
        use_alias: false,
        alias_src_addr: 0,
        alias_seq_num: 0,
        radius: 0,
    };

    let ret = unsafe { ffi::esp_zb_aps_data_request(&mut req) };
    log::debug!(
        "Scene response: dst=0x{:04X} src_ep={} dst_ep={} cmd=0x{:02X} status_ret={}",
        dst_short,
        info.dst_endpoint,
        info.src_endpoint,
        command_id,
        ret,
    );
}