nx_iot_capability_test.nx source
↩ module page · 67 lines · 3491 B
1// nx_iot_capability_test.nx -- gate for the per-device capability model.
2//
3// Proves: class -> capability set, capability queries, range clamping
4// (incl. "not supported" -> -1), and the JSON descriptor the /iot UI
5// consumes (exact lengths: a switch advertises only on_off; a colour
6// bulb advertises on_off+brightness+color+color_temp+scene).
7//
8// expect_exit: 0
9//
10// license_tier: ORIGINAL
11
12import "nx_syscalls_x86_64.nx"
13import "nx_iot_capability.nx"
14
15func main() -> i64 {
16 // ---- class validity --------------------------------------------
17 if nx_iot_class_is_valid(NX_IOT_CLASS_SWITCH) != 1 { return 1 }
18 if nx_iot_class_is_valid(NX_IOT_CLASS_BULB_COLOR) != 1 { return 2 }
19 if nx_iot_class_is_valid(NX_IOT_CLASS_N) != 0 { return 3 }
20 if nx_iot_class_is_valid(-1) != 0 { return 4 }
21
22 // ---- class -> capability set -----------------------------------
23 if nx_iot_cap_for_class(0, NX_IOT_CLASS_SWITCH) != NX_IOT_CAP_ONOFF { return 10 }
24 if nx_iot_cap_for_class(0, NX_IOT_CLASS_PLUG_ENERGY) != (NX_IOT_CAP_ONOFF | NX_IOT_CAP_ENERGY) { return 11 }
25 if nx_iot_cap_for_class(0, NX_IOT_CLASS_DIMMER) != (NX_IOT_CAP_ONOFF | NX_IOT_CAP_BRIGHTNESS) { return 12 }
26 if nx_iot_cap_for_class(0, NX_IOT_CLASS_BULB_TUNABLE) != (NX_IOT_CAP_ONOFF | NX_IOT_CAP_BRIGHTNESS | NX_IOT_CAP_COLOR_TEMP) { return 13 }
27 let cc: i64 = nx_iot_cap_for_class(0, NX_IOT_CLASS_BULB_COLOR)
28 if cc != (NX_IOT_CAP_ONOFF | NX_IOT_CAP_BRIGHTNESS | NX_IOT_CAP_COLOR | NX_IOT_CAP_COLOR_TEMP | NX_IOT_CAP_SCENE) { return 14 }
29
30 // ---- capability queries ----------------------------------------
31 if nx_iot_cap_has(cc, NX_IOT_CAP_COLOR) != 1 { return 20 }
32 if nx_iot_cap_has(cc, NX_IOT_CAP_ENERGY) != 0 { return 21 }
33 let cs: i64 = nx_iot_cap_for_class(0, NX_IOT_CLASS_SWITCH)
34 if nx_iot_cap_has(cs, NX_IOT_CAP_COLOR) != 0 { return 22 }
35 if nx_iot_cap_has(cs, NX_IOT_CAP_ONOFF) != 1 { return 23 }
36
37 // ---- range clamping --------------------------------------------
38 if nx_iot_cap_clamp_brightness(cc, 50) != 50 { return 30 }
39 if nx_iot_cap_clamp_brightness(cc, 150) != 100 { return 31 }
40 if nx_iot_cap_clamp_brightness(cc, -5) != 0 { return 32 }
41 if nx_iot_cap_clamp_brightness(cs, 50) != -1 { return 33 } // switch has no brightness
42 if nx_iot_cap_clamp_color_temp(cc, 3000) != 3000 { return 34 }
43 if nx_iot_cap_clamp_color_temp(cc, 1000) != 2700 { return 35 }
44 if nx_iot_cap_clamp_color_temp(cc, 9000) != 6500 { return 36 }
45 if nx_iot_cap_clamp_color_temp(cs, 4000) != -1 { return 37 }
46
47 // ---- UI descriptor JSON ----------------------------------------
48 let out: *u8 = sys_mmap(128)
49 // switch -> ["on_off"] (length 10)
50 let ls: i64 = nx_iot_cap_emit_json(cs, out, 128)
51 if ls != 10 { return 40 }
52 if (out[0] as i64) != 0x5B { return 41 } // [
53 if (out[1] as i64) != 0x22 { return 42 } // "
54 if (out[2] as i64) != 0x6F { return 43 } // o
55 if (out[ls - 1] as i64) != 0x5D { return 44 } // ]
56 // plug+energy -> ["on_off","energy"] (length 19)
57 let ce: i64 = nx_iot_cap_for_class(0, NX_IOT_CLASS_PLUG_ENERGY)
58 let le: i64 = nx_iot_cap_emit_json(ce, out, 128)
59 if le != 19 { return 45 }
60 // colour bulb -> ["on_off","brightness","color","color_temp","scene"] (length 52)
61 let lc: i64 = nx_iot_cap_emit_json(cc, out, 128)
62 if lc != 52 { return 46 }
63 if (out[0] as i64) != 0x5B { return 47 }
64 if (out[lc - 1] as i64) != 0x5D { return 48 }
65
66 return 0
67}