nx_iot_control_page_gate.nx source
↩ module page · 63 lines · 3300 B
1// nx_iot_control_page_gate.nx -- proves the IoT control page is CROSS-BROWSER + CONTROLS.
2//
3// Cross-browser/reproducible = ZERO <script> (no JS engine needed -> renders the same in the
4// Nishi browser AND Chrome/Firefox/Edge) + real <form> POSTs (server-rendered control, the
5// universal no-JS pattern). Capability-gated = a switch shows On/Off only; a colour bulb adds
6// brightness/colour/scene; an UNKNOWN device shows NO controls (no silent always-on actuation).
7// expect_exit: 0 license_tier: ORIGINAL
8
9import "nx_syscalls.nx"
10import "nx_iot_control_page.nx"
11
12func main() -> i64 {
13 let cap: i64 = 262144
14
15 // ===== Buffer 1: a SWITCH (On/Off only) =====
16 let h: *u8 = sys_mmap(cap)
17 var w: i64 = 0
18 w = cp_head(h, w)
19 w = cp_card(h, w, "d8:0d:17:a1:c2:09" as *u8, "Kitchen Switch" as *u8, "Kitchen" as *u8, "Kasa HS210" as *u8, NX_IOT_CLASS_SWITCH, 1)
20 w = cp_foot(h, w)
21 // CROSS-BROWSER: not a single real <script> tag (the footer's is <script>-escaped)
22 if cp_find(h, w, "<script" as *u8, 7) != (0 - 1) { return 10 }
23 // valid HTML shell (doctype at byte 0, closes cleanly)
24 if cp_find(h, w, "<!doctype html" as *u8, 14) != 0 { return 11 }
25 if cp_find(h, w, "</html>" as *u8, 7) < 0 { return 12 }
26 // REAL control forms that POST to the hub (actuating, not disabled)
27 if cp_find(h, w, "method=\"post\"" as *u8, 13) < 0 { return 13 }
28 if cp_find(h, w, "action=\"/home/cmd\"" as *u8, 18) < 0 { return 14 }
29 // the device id rides in the form
30 if cp_find(h, w, "d8:0d:17:a1:c2:09" as *u8, 17) < 0 { return 15 }
31 // On + Off controls present
32 if cp_find(h, w, "value=\"on\"" as *u8, 10) < 0 { return 16 }
33 if cp_find(h, w, "value=\"off\"" as *u8, 11) < 0 { return 17 }
34 // CAP-GATED: a switch has NO brightness slider
35 if cp_find(h, w, "name=\"level\"" as *u8, 12) != (0 - 1) { return 18 }
36
37 // ===== Buffer 2: a COLOUR BULB (On/Off + brightness + colour + warmth + scene) =====
38 let h2: *u8 = sys_mmap(cap)
39 var w2: i64 = 0
40 w2 = cp_head(h2, w2)
41 w2 = cp_card(h2, w2, "a8:bb:50:11:22:33" as *u8, "Living Room Lamp" as *u8, "Living Room" as *u8, "WiZ colour" as *u8, NX_IOT_CLASS_BULB_COLOR, 0)
42 w2 = cp_foot(h2, w2)
43 if cp_find(h2, w2, "<script" as *u8, 7) != (0 - 1) { return 20 }
44 if cp_find(h2, w2, "value=\"on\"" as *u8, 10) < 0 { return 21 }
45 if cp_find(h2, w2, "name=\"level\"" as *u8, 12) < 0 { return 22 }
46 if cp_find(h2, w2, "value=\"bri\"" as *u8, 11) < 0 { return 23 }
47 if cp_find(h2, w2, "value=\"scene\"" as *u8, 13) < 0 { return 24 }
48 if cp_find(h2, w2, "c:4cc2ff" as *u8, 8) < 0 { return 25 }
49
50 // ===== Buffer 3 (NEG): an UNKNOWN device -> shown, but NO controls =====
51 let h3: *u8 = sys_mmap(cap)
52 var w3: i64 = 0
53 w3 = cp_head(h3, w3)
54 w3 = cp_card(h3, w3, "unknown-0001" as *u8, "Mystery" as *u8, "?" as *u8, "unclassified" as *u8, NX_IOT_CLASS_UNKNOWN, 0)
55 w3 = cp_foot(h3, w3)
56 if cp_find(h3, w3, "<script" as *u8, 7) != (0 - 1) { return 30 }
57 // no capabilities -> NO control form emitted (no silent always-on actuation)
58 if cp_find(h3, w3, "action=\"/home/cmd\"" as *u8, 18) != (0 - 1) { return 31 }
59 // ...but the device is still shown on the page
60 if cp_find(h3, w3, "Mystery" as *u8, 7) < 0 { return 32 }
61
62 return 0
63}