-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbleKeyLegacy.be
More file actions
469 lines (410 loc) · 14.9 KB
/
bleKeyLegacy.be
File metadata and controls
469 lines (410 loc) · 14.9 KB
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
#######################################################################
# BLE legacy bind key generator for ESP32 - ESP32C3 - ESP32S3
#
# use : `import keyGenLeg`
#
# Provides BLE bindings and a Web UI
#######################################################################
var keyGenLeg = module('keyGenLeg')
# List of PRODUCT_ID:
# 339: For 'YLYK01YL'
# 950: For 'YLKG07YL/YLKG08YL'
# 959: For 'YLYB01YL-BHFRC'
# 1254: For 'YLYK01YL-VENFAN'
# 1678: For 'YLYK01YL-FANCL'
#
#################################################################################
# Globals
#################################################################################
import math
import BLE
UUID_SERVICE = "fe95"
UUID_AUTH = '0001'
UUID_FIRMWARE_VERSION = '0004'
UUID_AUTH_INIT = '0010'
UUID_BEACON_KEY = '0014'
MI_KEY1 = bytes('90CA85DE')
MI_KEY2 = bytes('92AB54FA')
PID = 950
buf = bytes(-64)
#################################################################################
# BLE_keyGenLeg_UI
#
# WebUI for the legacy bind key generator
#################################################################################
class BLE_keyGenLeg_UI
var token, rev_MAC, MAC, fw_version
var current_func, next_func
var msg, key, shallSendKey
def reverseMac(mac)
var reMAC = bytes(-6)
for i:0..5
reMAC[i] = mac[5-i]
end
return reMAC
end
def mixBase(mac)
var buf = bytes(-8)
buf[0] = mac[0]
buf[1] = mac[2]
buf[2] = mac[5]
return buf
end
def mixA(mac,PID)
var buf = self.mixBase(mac)
buf[3] = PID & 0xff
buf[4] = PID & 0xff
buf[5] = mac[4]
buf[6] = mac[5]
buf[7] = mac[1]
return buf
end
def mixB(mac,PID)
var buf = self.mixBase(mac)
buf[3] = (PID >> 8) & 0xff
buf[4] = mac[4]
buf[5] = mac[0]
buf[6] = mac[5]
buf[7] = PID & 0xff
return buf
end
def cipherInit(key)
var perm = bytes(-256)
for i:0..255
perm[i] = i & 0xff
end
var keyLen = size(key)
var j = 0
for i:0..255
j += perm[i] + key[i % keyLen]
j = j & 0xff
var temp = perm[i]
perm[i] = perm[j]
perm[j] = temp
end
return perm
end
def cipherCrypt(inp, perm)
var index1 = 0
var index2 = 0
var _size = size(inp)
var output = bytes(-_size)
for i:0.._size-1
index1 += 1
index1 = index1 & 0xff
index2 += perm[index1]
index2 = index2 & 0xff
var temp = perm[index1]
perm[index1] = perm[index2]
perm[index2] = temp
var idx = perm[index1] + perm[index2]
idx = idx & 0xff
var outputByte = inp[i] ^ perm[idx]
output[i] = outputByte & 0xff
end
return output
end
def cipher(key, inp)
var _perm = self.cipherInit(key)
return self.cipherCrypt(inp, _perm)
end
def generateRandomToken()
var token = bytes(-12)
for i:0..11
token[i] = math.rand()%255
end
return token
end
def init()
var cbp = cb.gen_cb(/e,o,u,h->self.cb(e,o,u,h))
BLE.conn_cb(cbp,buf)
self.current_func = self.wait
self.msg = ""
self.key = ""
self.shallSendKey=false
end
def log(msg)
print(msg)
if self.msg==''
self.msg=msg
else
self.msg+="\\n"
self.msg+=msg
end
end
def every_second()
self.current_func()
end
def wait()
# do nothing
end
def then(func)
# save function pointers for callback
self.next_func = func
self.current_func = self.wait
end
def pair(MAC)
self.MAC = MAC
self.token = self.generateRandomToken()
self.log(format("Generated random token: %s",str(self.token)))
self.rev_MAC = self.reverseMac(bytes(MAC))
self.log(format("Try to connect to: %s",MAC))
self.log("Long press pair button, LED should blink shortly!")
BLE.set_MAC(bytes(MAC),0)
BLE.set_svc(UUID_SERVICE,true)
BLE.run(7,true)
self.then(/->self.authInit())
end
def cb(error,op,uuid,handle)
print("BLE Op:",op,"UUID:",uuid)
if error == 0
if op == 103
print("Got notification...")
self.current_func = /->self.func4()
return
end
self.current_func = self.next_func # fulfil our promise ;)
return
else
print(error,op,uuid,handle)
end
if op == 5
self.log("Did successfully disconnect.")
elif error < 3
self.log("BLE error, did disconnect!")
elif error > 2
self.log("BLE error, will disconnect!")
BLE.run(5) # disconnect
end
end
def authInit()
import BLE
BLE.set_chr(UUID_AUTH_INIT)
buf.setbytes(1,MI_KEY1)
buf[0] = size(MI_KEY1)
BLE.run(2,true)
self.then(/->self.subscribeAuth())
end
def subscribeAuth()
self.log("Did connect, subscribe to UUID_AUTH")
BLE.set_chr(UUID_AUTH)
BLE.run(3,true)
self.then(/->self.writeMixToAuth())
end
def writeMixToAuth()
self.log("Write to UUID_AUTH")
# self.current_func = self.wait
BLE.set_chr(UUID_AUTH)
var _mixA = self.mixA(self.rev_MAC,PID)
var _buf = self.cipher(_mixA,self.token)
buf.setbytes(1,_buf)
buf[0] = size(_buf)
BLE.run(2,true)
self.next_func = self.wait
end
# virtual func3 is just waiting for the notification in the cb
def func4()
BLE.set_chr(UUID_AUTH)
var _buf = self.cipher(self.token, MI_KEY2)
print(_buf)
buf.setbytes(1,_buf)
buf[0] = size(_buf)
print("size:", buf[0])
BLE.run(2,true)
self.then(/->self.readFW())
end
def readFW()
self.log("Will read FW version")
BLE.set_chr(UUID_FIRMWARE_VERSION)
BLE.run(1)
self.then(/->self.readKey())
end
def readKey()
self.log("Got FW version, will read key")
self.fw_version = self.cipher(self.token, buf[1..buf[0]]).asstring()
self.log(self.fw_version)
BLE.set_chr(UUID_BEACON_KEY)
BLE.run(1)
self.then(/->self.receiveKey())
end
def receiveKey()
import string
print("Got key with length:", buf[0])
var key = self.cipher(self.token, buf[1..buf[0]])
self.key = string.split(str(key),"'")[1]
self.log(format("Bind Key: %s", self.key))
if int(self.fw_version) == 1
self.log("Old firmware type ... will convert key to:")
var _s = string.split(self.key,12)
self.key = _s[0] + "8D3D3C97" + _s[1]
else
self.log("New firmware typee ... will convert key to:")
self.key += "FFFFFFFF"
end
self.log(self.key)
self.log("Success!! Will disconnect sensor.")
self.shallSendKey = true;
BLE.run(5)
self.then(/->self.addKeyToSensor())
end
def addKeyToSensor()
var keyMAC = self.key + self.MAC
tasmota.cmd("mi32key "+keyMAC)
self.then(/->self.wait())
end
def saveCfg()
tasmota.cmd("mi32cfg")
end
# create a method for adding a button to the console menu
# the button 'BLE scan' redirects to '/ble_key_l?'
def web_add_button()
import webserver
webserver.content_send(
"<form id=but_ble_key_l style='display: block;' action='ble_key_l' method='get'><button>BLE Key Gen legacy</button></form>")
end
#######################################################################
# Display the compatible devices
#######################################################################
def show_devices()
import webserver
import string
import MI32
webserver.content_send("<p>Compatible devices:<p><select name='sensors' id='sens'>")
var num = MI32.devices()-1
for i:0..num
if MI32.get_name(i) == "YLKG08"
var mac = MI32.get_MAC(i)
var mac_str = string.split(str(mac),"'")[1]
webserver.content_send(format("<option value=%s>YLKG08 - MAC: %s</option>",mac_str,mac_str))
self.log(format("Found YLKG08 with MAC: %s",mac_str))
end
end
webserver.content_send("</select><br><br>")
if num>0
webserver.content_send("<button onclick='pair()'>Generate Key</button><br>")
end
end
#######################################################################
# Upload javascript
#######################################################################
def upl_js()
import webserver
var script_start = "<script>function update(cmnd){if(!cmnd){cmnd='loop=1'}var xr=new XMLHttpRequest();xr.onreadystatechange=()=>{if(xr.readyState==4&&xr.status==200){"
"var cr=xr.response.replace(/bytes\\(\\'([^']+)\\'\\)/g,'$1'); const r=JSON.parse(cr);if('LOG' in r){let l=eb('log');l.value+=r.LOG+'\\n';l.scrollTop=l.scrollHeight;}else if('KEY' in r){eb('key').innerHTML='Key: '+r.KEY}"
"};};xr.open('GET','/ble_key_l?'+cmnd,true);xr.send();};setInterval(update,250);"
var script_1 = "function save(){update('save=1');}"
"function pair(){update('pair='+eb('sens').value);eb('log').value+='Start pairing with: '+eb('sens').value+'\\n';}"
var script_2 = "</script>"
# str.replace(/bytes\(\'([^']+)\'\)/g,"$1")
webserver.content_send(script_start)
webserver.content_send(script_1)
webserver.content_send(script_2)
end
#######################################################################
# Display the complete page
#######################################################################
def page_ble_key_l()
import webserver
import string
import json
if !webserver.check_privileged_access() return nil end
# AJAX response
if webserver.has_arg("loop")
if self.shallSendKey==true
self.shallSendKey=false
webserver.content_response(format("{\"KEY\":\"%s\"}",self.key))
return
end
if self.msg==''
webserver.content_response("{\"OK\":[]}")
else
webserver.content_response(format("{\"LOG\":\"%s\"}",self.msg))
self.msg=''
end
return
elif webserver.has_arg("pair")
webserver.content_response("{\"OK\":[]}")
var mac = webserver.arg("pair")
self.log("Connect requested from WebGUI.")
self.pair(mac)
return
elif webserver.has_arg("save")
webserver.content_response("{\"OK\":[]}")
self.saveCfg()
self.shallSendKey=false
self.log("Saving mi32cfg.")
return
end
# regular web page
webserver.content_start("BLE legacy key generator") #- title of the web page -#
webserver.content_send_style() #- send standard Tasmota styles -#
webserver.content_send("<style>.parent{display:flex;flex-flow:row wrap;}.parent > *{flex: 1 100%;}tr:nth-child(even){background-color: #f2f2f230;}.box {margin: 5px;padding: 5px;border-radius: 0.8rem;background-color: rgba(221, 221, 221, 0.2);}@media all and (min-width: 600px){.side{flex:1 auto;}}@media all and (min-width:800px){.main{flex:3 0px;}.side-1{order:1;}.main{order:2;}.side-2{order:3;}.footer{order: 4;}}</style>")
self.upl_js() #- send own JS -#
webserver.content_send("<div class='parent'><header class='box'><h2> BLE legacy key generator / MI32</h2><p style='text-align:right;'>... powered by BERRY</p></header>")
webserver.content_send("<div class='box side side-1'>Using only sensors visible to the MI32 driver.<br>Check your 'mi32cfg' or<br>enable scanning for new sensors<br>with 'mi32option3 0'.")
self.show_devices()
webserver.content_send("</div>")
webserver.content_send(
"<div class='box side side-2'><p id='key'></p><br><button onclick='save()'>Save to mi32cfg</button></div>") #- close .box-#
webserver.content_send("<div class='large box main'><h3>Log:</h3><textarea id='log' style='min-width:600px'>Waiting for driver ...\n</textarea>")
webserver.content_send("</div><br>")
webserver.content_send("<p></p></div><br>") #- close .parent div-#
webserver.content_button(webserver.BUTTON_MANAGEMENT) #- button back to management page -#
webserver.content_stop() #- end of web page -#
end
#######################################################################
# Web Controller, called by POST to `/ble_key_l`
#######################################################################
def page_ble_key_l_ctl()
import webserver
import string
if !webserver.check_privileged_access() return nil end
try
#---------------------------------------------------------------------#
# To do.
#---------------------------------------------------------------------#
if webserver.has_arg("ble_key_l")
print("key generator")
self.page_ble_key_l()
else
raise "value_error", "Unknown command"
end
except .. as e, m
print(format("BRY: Exception> '%s' - %s", e, m))
#- display error page -#
webserver.content_start("Parameter error") #- title of the web page -#
webserver.content_send_style() #- send standard Tasmota styles -#
webserver.content_send(format("<p style='width:340px;'><b>Exception:</b><br>'%s'<br>%s</p>", e, m))
webserver.content_button(webserver.BUTTON_MANAGEMENT) #- button back to management page -#
webserver.content_send("<p></p>")
webserver.content_stop() #- end of web page -#
end
end
#- ---------------------------------------------------------------------- -#
# respond to web_add_handler() event to register web listeners
#- ---------------------------------------------------------------------- -#
#- this is called at Tasmota start-up, as soon as Wifi/Eth is up and web server running -#
def web_add_handler()
import webserver
#- we need to register a closure, not just a function, that captures the current instance -#
webserver.on("/ble_key_l", / -> self.page_ble_key_l(), webserver.HTTP_GET)
webserver.on("/ble_key_l", / -> self.page_ble_key_l_ctl(), webserver.HTTP_POST)
end
end
keyGenLeg.BLE_keyGenLeg_UI = BLE_keyGenLeg_UI
#- create and register driver in Tasmota -#
if tasmota
var BLE_keyGenLeg_UI = keyGenLeg.BLE_keyGenLeg_UI()
tasmota.add_driver(BLE_keyGenLeg_UI)
## can be removed if put in 'autoexec.bat'
BLE_keyGenLeg_UI.web_add_handler()
def pair(cmd, idx, payload, payload_json)
BLE_keyGenLeg_UI.pair(payload)
return true
end
tasmota.add_cmd('pair', pair) # MAC of the dimmer
end
return keyGenLeg
#- Example
import keyGenLeg
-#