code wiki / (root) / nx_tuya_smartconfig_gate.nx

nx_tuya_smartconfig_gate.nx source

↩ module page · 29 lines · 1434 B

1// nx_tuya_smartconfig_gate.nx -- KAT for the sovereign Tuya broadcast encoder vs tuya-convert 2// reference vectors (generated from the cited broadcast.py+crc.py). expect_exit: 0 3// CRC-8: crc_8([1])=94, [10]=126, [255]=53, ["abc"]=66, [0]=0 4// body("a","b","c") = [16,37,51,79,194,128,257,353,257,355,218,129,354,256] 5// license_tier: ORIGINAL 6import "nx_tuya_smartconfig.nx" 7 8func main() -> i64 { 9 let a: *i64 = sys_mmap(8 * 8) as *i64 10 a[0] = 0; if nx_tuya_crc8(a, 1) != 0 { return 1 } 11 a[0] = 1; if nx_tuya_crc8(a, 1) != 94 { return 2 } 12 a[0] = 10; if nx_tuya_crc8(a, 1) != 126 { return 3 } 13 a[0] = 255; if nx_tuya_crc8(a, 1) != 53 { return 4 } 14 a[0] = 97; a[1] = 98; a[2] = 99; if nx_tuya_crc8(a, 3) != 66 { return 5 } 15 16 let pw: *u8 = sys_mmap(8); pw[0] = 0x61 as u8 // "a" 17 let ss: *u8 = sys_mmap(8); ss[0] = 0x62 as u8 // "b" 18 let tk: *u8 = sys_mmap(8); tk[0] = 0x63 as u8 // "c" 19 let out: *i64 = sys_mmap(8 * 64) as *i64 20 let n: i64 = nx_tuya_bcast(pw, 1, ss, 1, tk, 1, out, 64) 21 if n != 14 { return 10 } 22 23 let exp: *i64 = sys_mmap(8 * 16) as *i64 24 exp[0] = 16; exp[1] = 37; exp[2] = 51; exp[3] = 79; exp[4] = 194; exp[5] = 128; exp[6] = 257 25 exp[7] = 353; exp[8] = 257; exp[9] = 355; exp[10] = 218; exp[11] = 129; exp[12] = 354; exp[13] = 256 26 var i: i64 = 0 27 while i < 14 { if out[i] != exp[i] { return 20 + i } i = i + 1 } 28 return 0 29}