-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathTelinkMiFlasher.html
3332 lines (3258 loc) · 152 KB
/
TelinkMiFlasher.html
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="telFlasherClass">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="styles.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Telink Flasher v9.3</title>
<script type="text/javascript" src="./core.js"></script>
</head>
<body>
<script>
//BLE values
var bluetoothDevice, gattServer, Theservice, writeCharacteristic;
var ServiceMain, writeCharacteristicSpeed = null, nitifiyCharTemp, time_char, enc_main, enc_10, enc_19;
//Firmware values
var fwmaxsize = 0x20000;
var otafiles = { loaded: false };
var otazfiles = { loaded: false };
var fzigbeeota = { used : false };
var firmwareArray = "",
startTime = 0,
blockCount = 0,
fsignota = "";
var addSignArray = "";
var flg_memo_act = false;
var devinfo = { hrstr: null, srstr: null, frstr: null };
var devSens = {};
var fwname = '';
//Connection values
var state = 0,
connectTrys = 0;
//Custom firmware
var customEnabled, devTest = false;
var settingsCharacteristics = null;
// Custom config
var MAX_RF_TX_Power = false;
var cfg = {enable: false, ver: 0, hver: 0, ext_hw_id: 0, flg: 0, flg2: 0, temp_offset: 0, humi_offset: 0, advertising_interval: 32, measure_interval: 5, rf_tx_power: 191, connect_latency: 49, lcd_tint: 55, av_meas_mem: 60, step_time: 16000000};
var trg = {enable: false, tmp_thr: 2100, hm_thr: 5000, tmp_hst: -1, hm_hst: 0, flg: 0, rds_type: 0, rds_rpint: 0};
var cmf = {enable: false, tmp_lo: 2100, tmp_hi: 2600, hm_lo: 3000, hm_hi: 6000};
var dnm = {enable: false, name: ""};
var devtime = {};
var pincode = {enable: false, value: 0};
//Mi values
var miEnabled = false,
otaEnabled = false,
EnabledQinping = false,
devInfEnabled = false,
miConnected = false,
devIdEnabled = false,
bigOtaEnabled = false,
mode_activation;
//Login values
var mi_random_key, mi_random_key_recv, mi_device_info_recv, mi_device_info_send, device_known_id, expected_device_infos, is_logged_in = false;
//Activation values
var keypair, is_activated, own_public_key, device_public_key, shared_key, derived_key, mi_write_did;
var device_new_id = "00626c742e332e31323976" + makeRandomID(6) + "673030"; // 0+"blt.6.129v"+rand[6]+"g00"
var $ = function(id) { return document.getElementById(id);}
function resetVariables() {
fwmaxsize = 0x20000;
busy = false;
gattServer = null;
Theservice = null;
mikeys.restore = false;
mikeys.mac = null;
mikeys.id = null;
mikeys.token = null;
mikeys.bindkey = null
mikeys.cbindkey = null
mikeys.cfg = null;
mikeys.delkeys = null;
ext.enable = false;
pincode.enable = false;
cfg.enable = false;
trg.enable = false;
trg.rds2_enable = false;
cmf.enable = false;
dnm.enable = false;
cnt_buf11 = 0;
writeCharacteristic = null;
settingsCharacteristics = null;
miConnected = false;
bigOtaEnabled = false;
is_logged_in = false;
devinfo.hrstr = null;
devinfo.srstr = null;
devinfo.frstr = null;
//fzigbeeota.used = false;
//fwname = '';
devtime = {};
writeCharacteristicSpeed = null;
$("ldfrmw").innerHTML ='';
$("custcfg").innerHTML = '';
$("tempHumiData").innerHTML = "Temp/Humidity: waiting notify for data after connecting";
$("percent").innerHTML = "Status: Disconnected";
}
function handleError(error) {
if (connectTrys == 1000) {
disconnect();
} else {
addLog(error);
resetVariables();
if (connectTrys < 5) {
connectTrys++;
addLog("Reconnect " + connectTrys + " from " + 5);
doConnect();
} else {
addLog("Something went wrong, too many reconnects");
connectTrys = 0;
}
}
}
function disconnect() {
if (bluetoothDevice != null) bluetoothDevice.gatt.disconnect();
$("MAC").innerHTML = '';
$("ldfile").hidden = true;
cfg.hver = null;
hwver_id = null;
resetVariables();
}
function onDisconnected() {
addLog('Disconnected.');
$("MAC").innerHTML = '';
$("ldfile").hidden = true;
cfg.hver = null;
hwver_id = null;
resetVariables();
}
function connect() {
var deviceOptions = {
optionalServices: ['00001f10-0000-1000-8000-00805f9b34fb','00010203-0405-0607-0809-0a0b0c0d1912', 'ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6', '22210000-554a-4546-5542-46534450464d', 0x180a, 0x181c, 0x181e, 0xfe95, 0x1f10, 0x1f11, 0x181a, 0xfff9, 0xfdcd, 0xffe0, 0xfcd2],
services: [0x180a, 0x181c, 0x181e, 0xfe95, 0x1f10, 0x1f11, 0x181a, 0xfff9, 0xfcd2, 0xfdcd, 0xffe0],
acceptAllDevices: true };
const namePrefix = $('namePrefix').value;
if (namePrefix) {
deviceOptions.acceptAllDevices = false;
deviceOptions.filters = namePrefix.split(",")
.map((x) => ({ namePrefix: x }));
}/* else {
deviceOptions.acceptAllDevices = false;
deviceOptions.filters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz_#@!*0123456789';,.<>{}[]"
.split("")
.map((x) => ({ namePrefix: x }));
} */
addClog(deviceOptions)
if (bluetoothDevice != null) bluetoothDevice.gatt.disconnect();
resetVariables();
addAlog("Searching for devices");
connectTrys = 0;
navigator.bluetooth.requestDevice(deviceOptions).then(device => {
bluetoothDevice = device;
if($("advmac").checked)
catchAdvertisement(device);
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
addLog("Connecting to: " + bluetoothDevice.name);
setStatus("Connecting to: " + bluetoothDevice.name + " ...");
if(!$("advmac").checked)
doConnect();
}).catch(handleError);
}
const tabid2name = [
{ id: 0x01AA, name: "LYWSDCGQ"},
{ id: 0x045B, name: "LYWSD02"},
{ id: 0x055B, name: "LYWSD03MMC"},
{ id: 0x1203, name: "XMWSDJ04MMC"},
{ id: 0x04E1, name: "XMMF01JQD"},
{ id: 0x0098, name: "HHCCJCY01"},
{ id: 0x03BC, name: "GCLS002"},
{ id: 0x015D, name: "HHCCPOT002"},
{ id: 0x040A, name: "WX08ZM"},
{ id: 0x098B, name: "MCCGQ02HL"},
{ id: 0x0083, name: "YM-K1501"},
{ id: 0x0113, name: "YM-K1501EU"},
{ id: 0x045C, name: "V-SK152"},
{ id: 0x0863, name: "SJWS01LM"},
{ id: 0x07F6, name: "MJYD02YL"},
{ id: 0x03DD, name: "MUE4094RT"},
{ id: 0x0A8D, name: "RTCGQ02LM"},
{ id: 0x00DB, name: "MMC-T201-1"},
{ id: 0x0489, name: "M1S-T500"},
{ id: 0x0C3C, name: "CGC1"},
{ id: 0x0576, name: "CGD1"},
{ id: 0x066F, name: "CGDK2"},
{ id: 0x0347, name: "CGG1"},
{ id: 0x0B48, name: "CGG1-ENCRYPTED"},
{ id: 0x03D6, name: "CGH1"},
{ id: 0x0A83, name: "CGPR1"},
{ id: 0x06d3, name: "MHO-C303"},
{ id: 0x0387, name: "MHO-C401"},
{ id: 0x02DF, name: "JQJCY01YM"},
{ id: 0x0997, name: "JTYJGD03MI"},
{ id: 0x1568, name: "K9B-1BTN"},
{ id: 0x1569, name: "K9B-2BTN"},
{ id: 0x0DFD, name: "K9B-3BTN"},
{ id: 0x07BF, name: "YLAI003"},
{ id: 0x0153, name: "YLYK01YL"},
{ id: 0x068E, name: "YLYK01YL-FANCL"},
{ id: 0x04E6, name: "YLYK01YL-VENFAN"},
{ id: 0x03BF, name: "YLYB01YL-BHFRC"},
{ id: 0x03B6, name: "YLKG07YL/YLKG08YL"},
{ id: 0x069E, name: "ZNMS16LM"},
{ id: 0x069F, name: "ZNMS17LM"},
{ id: 0x04E9, name: "MJZNMSQ01YD"},
{ id: 0x098C, name: "XMZNMST02YD"},
{ id: 0x2832, name: "MJWSD05MMC"}
];
function id2name(dev_id) {
let did = tabid2name.map(el => el.id);
let i = did.indexOf(dev_id);
if(i != -1) return tabid2name[i].name;
return "?";
}
const tabDoor2event = [
{ id: 0, name: "opened"},
{ id: 1, name: "closed"},
{ id: 2, name: "closing timeout"},
{ id: 3, name: "device reset"}
];
function Door2event(id) {
let did = tabDoor2event.map(el => el.id);
let i = did.indexOf(id);
if(i != -1) return tabDoor2event[i].name;
return "?";
}
const tabHaBle2name = [
{ id: 0, name: "PacketId", unit: "", factor: 1},
{ id: 1, name: "battery", unit: "%", factor: 1},
{ id: 2, name: "temperature", unit: "°C", factor: 0.01},
{ id: 3, name: "humidity", unit: "%", factor: 0.01},
{ id: 4, name: "pressure", unit: "hPa", factor: 0.01},
{ id: 5, name: "illuminance", unit: "lux", factor: 0.01},
{ id: 6, name: "weight", unit: "kg", factor: 0.01},
{ id: 7, name: "weight", unit: "kg", factor: 1},
{ id: 8, name: "dewpoint", unit: "°C", factor: 0.01},
{ id: 9, name: "count", unit: "", factor: 1},
{ id: 10, name: "energy", unit: "kWh", factor: 0.001},
{ id: 11, name: "power", unit: "W", factor: 0.01},
{ id: 12, name: "voltage", unit: "V", factor: 0.001},
{ id: 13, name: "pm2x5", unit: "kg/m3", factor: 1},
{ id: 14, name: "pm10", unit: "kg/m3", factor: 1},
{ id: 15, name: "boolean", unit: "", factor: 1},
{ id: 16, name: "switch", unit: "", factor: 1},
{ id: 17, name: "opened", unit: "", factor: 1}
];
function catchAdvertisement(device) {
addAlog("Get Advertising MAC (Web Experimental Features!)");
const abortController = new AbortController();
device.addEventListener('advertisementreceived', (event) => {
addClog('Received advertisement from "' + device.name + '"...');
event.serviceData.forEach((b, key) => {
const buf = new Uint8Array(b.buffer);
addClog(buf, device, event);
let s = 'MAC: ?';
if(key == '0000fe95-0000-1000-8000-00805f9b34fb') { // mi
if(b.byteLength >= 2) { // custom mi
let ctrl = b.getUint16(0, true);
s = 'MiVer'+ (ctrl>>12);
let i = 2;
if((ctrl&0xf000) >= 0x2000 && b.byteLength >= i + 3) {
let dev_id = b.getUint16(i, true);
let frcntr = b.getUint8(i+2);
i += 3;
s += ', DevID: 0x'+ hex(dev_id, 4);
s += "-" + id2name(dev_id);
s += ', FnCnt: '+ frcntr;
s += ', CtrID: 0x'+ hex(ctrl, 4);
if(ctrl & 0x100)
s +=' Registered and bound';
else
s +=' Not bound';
if(ctrl & 0x200)
s +=', Request APP to register and bind';
switch((ctrl>>10)&3) {
case 0:
s +=', Old version certification';
break;
case 1:
s +=', Safety certification';
break;
case 2:
s +=', Standard certification';
break;
//case 3:
// s +=' reserved';
// break;
}
if(ctrl & 0x200)
s +=', Request APP to register and bind';
if((ctrl & 0x10) && b.byteLength >= i + 6) {
s += ', MAC: '+hex(buf[i+5],2)+hex(buf[i+4],2)+hex(buf[i+3],2)+hex(buf[i+2],2)+hex(buf[i+1],2)+hex(buf[i],2);
i += 6;
}
if((ctrl & 0x20) && b.byteLength >= i + 1) {
let cap = b.getUint8(i);
i += 1;
s +=', Capability: 0x'+ hex(cap,2);
switch((cap>>3) & 3) {
case 0:
s +=' - no binding';
break;
case 1:
s +=' - front binding';
break;
case 2:
s +=' - back binding';
break;
case 3:
s +=' - Combo';
break;
}
if(cap & 0x20 && b.byteLength >= i + 2) {
s +=', IO: 0x'+ hex(b.getUint16(i, true), 4);
i += 2;
}
}
if((ctrl & 0x40)) {
if(b.byteLength >= i + 4) {
if(ctrl & 8) {
s += ', Data encrypted';
} else {
let data_id = b.getUint16(i, true);
let data_len = b.getUint8(i+2);
i += 3;
if(b.byteLength >= i + data_len) switch(data_id) {
case 0x1006:
if(data_len == 2) {
let humi = b.getInt16(i, true) / 10.0;
s += ', Humi: '+humi+'%';
}
break;
case 0x1004:
if(data_len == 2) {
let temp = b.getInt16(i, true) / 10.0;
s += ', Temp: '+temp+'°C';
}
break;
case 0x100a:
if(data_len == 1) {
s += ', Bat: '+ b.getUint8(i) + '%';
if(b.byteLength >= i + 3 && b.getUint8(i+1) == 2) {
let Vbat = b.getUint16(i+2, true);
s += ', Vbat: '+ Vbat + ' mV';
i += 3;
}
}
break;
case 0x100d:
if(data_len == 4 && b.byteLength >= i + 3) {
let temp = b.getInt16(i, true) / 10.0;
let humi = b.getInt16(i+2, true) / 10.0;
s += ', Temp: '+temp+'°C Humi: '+humi+'%';
}
break;
case 0x1019:
if(data_len == 1) {
s += ', Door: '+ Door2event(b.getUint8(i));
}
break;
default:
s += ', DataID: 0x'+hex(data_id,4);
let data_value = 0;
if(data_len == 1)
s += ' Value: 0x'+hex(b.getUint8(i),2);
else if(data_len == 2)
s += ' Value: 0x'+hex(b.getUint16(i, true),4);
else if(data_len == 4)
s += ' Value: 0x'+hex(b.getUint32(i, true), 8);
else
s += ' DataLen: ' + data_len;
break;
}
i += data_len;
}
}
}
if(ctrl & 0x80)
s +=', Mesh Data';
}
}
} else if(key == '0000181a-0000-1000-8000-00805f9b34fb') {
if(b.byteLength >= 15) { // pvvx
// 70 58 56 38 C1 A4 69 0A 60 09 25 0B 48 70 04
let temp = b.getInt16(6, true) / 100.0;
let humi = b.getInt16(8, true) / 100.0;
let vbat = b.getUint16(10, true);
let bat = b.getUint8(12);
let cnt = b.getUint8(13);
let flg = b.getUint8(14);
s = 'MAC: '+hex(buf[5],2)+hex(buf[4],2)+hex(buf[3],2)+hex(buf[2],2)+hex(buf[1],2)+hex(buf[0],2);
s += ', Bat: '+bat+'%, Vbat: '+vbat+' mV , Temp: '+temp+'°C, Humi: '+humi+'%, Count: '+cnt+', Flg: '+flg;
} else if(b.byteLength == 13) { // atc1441
// A4 C1 38 56 58 70 01 0A 18 48 0B 25 70
let temp = b.getInt16(6, false) / 10.0;
let humi = b.getUint8(8);
let bat = b.getUint8(9);
let vbat = b.getUint16(10, false);
let cnt = b.getUint8(12);
s = 'MAC: '+hex(buf[0],2)+hex(buf[1],2)+hex(buf[2],2)+hex(buf[3],2)+hex(buf[4],2)+hex(buf[5],2);
s += ', Bat: '+bat+'%, Vbat: '+vbat+' mV, Temp: '+temp+'°C, Humi: '+humi+'%, Count: '+cnt;
}
} else if(key == '0000fff9-0000-1000-8000-00805f9b34fb') { // CGG1 "Goose"
// 0801 AE1610342D58 0104 1401 4601 0201 58
if(b.byteLength >= 16 && b.getUint16(0, true) == 0x0108 && b.getUint16(8, true) == 0x0401 && b.getUint16(14, true) == 0x0102) {
let temp = b.getInt16(10, true) / 10.0;
let humi = b.getInt16(12, true) / 10.0;
let bat = b.getUint8(16);
let s = 'MAC: '+hex(buf[2],2)+hex(buf[3],2)+hex(buf[4],2)+hex(buf[5],2)+hex(buf[6],2)+hex(buf[7],2);
s += ', Bat: '+bat+'%, Temp: '+temp+'°C, Humi: '+humi+'%';
log(s);
}
} else if(key == '0000fdcd-0000-1000-8000-00805f9b34fb') { // Qingping / ClearGrass
// CGPR1 0812 005E60342D58 0201 64 0F01 72 0904 08000000
// 0812 005E60342D58 0201 64 0F01 6E 0904 91140000
// 4812 005E60342D58 0804 01 911400 0F01 6F
// 4812 005E60342D58 1101 01 0F01 85
// 4812 005E60342D58 1101 00 0F01 8C
let msg_length = b.byteLength;
if(msg_length >= 11 && (b.getUint8(0)&0x3f) == 0x08) {
let device_id = b.getUint8(1);
s = 'MAC: '+hex(buf[2],2)+hex(buf[3],2)+hex(buf[4],2)+hex(buf[5],2)+hex(buf[6],2)+hex(buf[7],2);
if(device_id == 0x01) s+=' CGG1 "Goose"';
else if (device_id == 0x07) s+=' CGG1';
else if (device_id == 0x09) s+=' CGP1W';
else if (device_id == 0x0C) s+=' CGD1';
else if (device_id == 0x12) s+=' CGPR1';
//if((b.getUint8(0)&0x40)!=0) s+=', Event';
let xdata_point = 10;
while (xdata_point < msg_length) {
let xdata_id = b.getUint8(xdata_point-2);
let xdata_size = b.getUint8(xdata_point-1);
if(xdata_point + xdata_size <= msg_length) {
if((xdata_id == 0x01)&&(xdata_size == 4)) {
let temp = b.getInt16(xdata_point, true) / 10.0;
let humi = b.getInt16(xdata_point+2, true) / 10.0;
s += ', Temp: '+temp+'°C, Humi: '+humi+'%';
} else if((xdata_id == 0x02)&&(xdata_size == 1)) {
s += ', Bat: '+b.getUint8(xdata_point)+'%';
} else if((xdata_id == 0x07)&&(xdata_size == 2)) {
s += ', Pressure: '+(b.getInt16(xdata_point, true) / 100.0);
} else if((xdata_id == 0x08)&&(xdata_size == 4)) {
s += ', Motion: '+ b.getUint8(xdata_point)+' & Light: '+(b.getUint16(xdata_point+1, true)+b.getUint8(xdata_point+3)*256);
} else if((xdata_id == 0x09)&&(xdata_size == 4)) {
s += ', Light: '+(b.getUint32(xdata_point, true));
} else if((xdata_id == 0x0F)&&(xdata_size == 1)) {
s += ', Count: '+b.getUint8(xdata_point);
} else if((xdata_id == 0x11)&&(xdata_size == 1)) {
if(b.getUint8(xdata_point) == 0) s += ', Light Off';
else s += ', Light On';
} else if (xdata_size == 1) {
s += ', id: 0x'+hex(xdata_id,2)+' - data: 0x'+hex(buf[xdata_point],2);
} else if (xdata_size == 2) {
s += ', id: 0x'+hex(xdata_id,2)+' - data: 0x'+hex(buf[xdata_point],2)+hex(buf[xdata_point+1],2);
} else if (xdata_size == 4) {
s += ', id: 0x'+hex(xdata_id,2)+' - data: 0x'+hex(buf[xdata_point],2)+hex(buf[xdata_point+1],2)+hex(buf[xdata_point+2],2)+hex(buf[xdata_point+3],2);
}
} else break;
xdata_point = xdata_point + xdata_size + 2;
}
}
} else if(key == '00001f11-0000-1000-8000-00805f9b34fb') { // AdScanerTrg
if(b.byteLength > 10) {
let temp = b.getInt16(0, true) / 100.0; // temp
let humi = b.getInt16(2, true) / 100.0; // humi
let lumi = b.getInt16(4, true); // light
let mtimer = b.getUint16(6, true)
let motion = b.getUint8(8);
let jlight = b.getUint8(9);
let flg = b.getUint8(10);
s = ("AdScanerTrg# Temp: "+temp.toFixed(2)+
", Humi: "+humi.toFixed(2)+
", Lumi: " +lumi.toFixed(0)+
", MTimer: " +mtimer+
", HiMotion: " +motion+
", JLight: "+jlight+
", Flg: 0x"+hex(flg,2)+
" # ReedSwitchOut: "+(flg&1)+
", TempHumiOut: "+((flg>>1)&1)+
", LightMotionOut: "+((flg>>2)&1)+
", TempEvent: "+((flg>>3)&1)+
", HumiEvent: "+((flg>>4)&1)+
", LightEvent: "+((flg>>5)&1));
}
} else if(key == '0000181c-0000-1000-8000-00805f9b34fb') { // BTHome
if(b.byteLength > 1) {
s += " BTHomeV1";
let len = b.byteLength;
let i = 0;
while(i < len) {
let sl = b.getUint8(i)&31; // size
if(i+sl+1 > len) break;
let st = b.getUint8(i)>>5;
if(sl > 0) {
let dd = b.getUint8(i+1);
let value = 0;
let did = tabHaBle2name.map(el => el.id);
let e = did.indexOf(dd);
let f = 1;
if(e != -1) f = tabHaBle2name[e].factor;
if(st == 0) {
if(sl == 2)
value = b.getUint8(i+2);
else if(sl == 3)
value = b.getUint16(i+2, true);
else if(sl == 4)
value = b.getUint16(i+2, true) + (b.getUint8(i+4)<<16);
else if(sl == 5)
value = b.getUint32(i+2, true);
if(f != 1) value = (value*f).toFixed(3);
} else if(st == 1) {
if(sl == 2)
value = b.getInt8(i+2);
else if(sl == 3)
value = b.getInt16(i+2, true);
else if(sl == 4) {
value = b.getUint16(i+2, true) | (b.getInt8(i+4)<<16);
} else if(sl == 5)
value = b.getInt32(i+2, true);
if(f != 1) value = (value*f).toFixed(3);
} else
value = "?";
if(e != -1) {
s += ', '+tabHaBle2name[e].name+': '+value+tabHaBle2name[e].unit;
} else
s += ', '+st+'?: '+value;
}
i += sl+1;
}
}
} else if(key == '0000181e-0000-1000-8000-00805f9b34fb') { // BTHome
s += " BTHomeV1, Data encrypted";
} else if(key == '0000fcd2-0000-1000-8000-00805f9b34fb') { // BTHome v2
if(b.byteLength > 1) {
let ver = b.getUint8(8);
if(ver & 0xe0 == 0x40) {
s += " BTHomeV2";
if(ver & 1 != 0)
s += ", Data encrypted";
}
}
}
addAlog(s);
abortController.abort();
doConnect();
})
}, { once: true });
addClog('Catching advertisement from "' + device.name + '"...');
device.watchAdvertisements({ signal: abortController.signal })
.catch(error => {
addClog('Error: ' + error);
});
}
var cusService, cus017Chr, cus018Chr, cus100Chr;
function sendCmdClear() {
cus017Chr.writeValue(new Uint8Array([0x01,0x0e]))
.then(() => { disconnect(); });
}
function getMTimer() {
cus017Chr.writeValue(new Uint8Array([0x01,0x13]));
}
function setMTimer() {
let t = parseInt($("mtimer").value);
if(t > 100) {
cus017Chr.writeValue(new Uint8Array([0x05,0x10,t&0xff,(t>>8)&0xff,(t>>16)&0xff,(t>>24)&0xff]))
.then(() => { cus017Chr.writeValue(new Uint8Array([0x01,0x13])); });
}
}
function CustomQingpingParse(value) {
//let value = event.target.value;
let cid = value.getUint16(0, true);
if(cid == 0x1706) {
let temp = value.getInt16(2, true)/10;
let humi = value.getInt16(4, true)/10;
let s = "Temp: "+temp+"C, Humi: "+humi+"%";
$("tempHumiData").innerHTML = '<hr>'+s;
addClog(s);
} else if(cid == 0x0202) {
addLog("Battery: "+ value.getUint8(2) + "%");
} else if(cid == 0x0305) {
let mt = value.getUint8(2);
let lx = value.getUint16(3, true) + (value.getUint8(5) << 16);
let tempString = "MotionT: " + mt + ", Illumination: "+ lx + " lx";
$("tempHumiData").innerHTML = '<hr>'+tempString;
addClog(tempString);
} else
addClog("CustNotify100: "+bytesToHex(event.target.value.buffer));
}
function CGPR1_Action() {
addLog("Detected Qingping CGPR1 device");
$("custcfg").innerHTML = '<hr>When doing an activation here the device is needed to be activated in the Mi app again when wanted to use there.<br>Press and hold the button on the device the device for 2 seconds until the LED starts flashing, and then press the "Connect" and "Activate" buttons on the TeLinkMiFlasher.<br><button type="button" onclick="sendRegister();">Do Activation</button> <button type="button" onclick="skipActivation();">Skip Activation</button> <button type="button" onclick="keyMiLogin();">Login</button><br>Device known id:<br><input size="40" type="text" id="known_id" value=""><br>Mi Token:<br><input size="40" type="text" id="mi_token" value=""><br>Mi Bind Key:<br><input size="40" type="text" id="mi_bind_key" value=""><br><br><button type="button" onclick="sendCmdClear();">Clear all settings and reset device (into Qingping mode)</button><br><br>MTimer: <input type="text" id="mtimer" value="?"> ms <button type="button" onclick="getMTimer();">Get</button> <button type="button" onclick="setMTimer();">Set</button><br>';
gattServer.getPrimaryService("22210000-554a-4546-5542-46534450464d")
.then(service => {
addClog("Found Custom service");
cusService = service;
return cusService.getCharacteristic(0x0017);
}).then(characteristic => {
cus017Chr = characteristic;
addClog('Found 0x0017 characteristic');
return cusService.getCharacteristic(0x0018);
}).then(characteristic => {
cus018Chr = characteristic;
addClog('Found 0x0018 characteristic');
return cus018Chr.addEventListener('characteristicvaluechanged', event => {
let value = event.target.value;
addClog("CustNotify018: " + bytesToHex(event.target.value.buffer));
let cid = value.getUint16(0, true);
if(cid == 0x0202) {
addLog("Battery: "+ value.getUint8(2) + "%");
} else if(cid == 0x0405) {
let adc = value.getUint32(2, true);
addLog("Level: " + adc);
} else if(cid == 0x0305) {
let mt = value.getUint8(2);
let lx = value.getUint16(3, true) + (value.getUint8(5) << 16);
addLog("Event: " + mt + ", Illumination: "+ lx + " lx");
} else if(cid == 0x1305) {
let tm = value.getUint32(2, true);
addLog("Timer: "+ tm + " ms (" + (tm/1000) + " sec)");
$("mtimer").value = tm.toString(10);
} else if(cid == 0xff04) {
let cmd = value.getUint8(2);
let err = value.getUint8(3);
if(err == 0) {
if(cmd == 0x10) addLog("Set timer - ok");
else if(cmd == 0x0e) addLog("Clear - ok (Mode Qingping)");
else addLog("Command " + cmd.toString(16) + " - ok");
} else addLog("Command " + cmd.toString(16) + " - Error " + err.toString(16));
} else
addLog("CustNotify018: " + bytesToHex(event.target.value.buffer));
});
}).then(_ => {
return cus018Chr.startNotifications();
}).then(_ => {
return cusService.getCharacteristic(0x0100);
}).then(characteristic => {
cus100Chr = characteristic;
addClog('Found 0x0100 characteristic');
return cus100Chr.startNotifications().then(() => {
cus100Chr.addEventListener('characteristicvaluechanged', event => CustomQingpingParse(event.target.value));
cus017Chr.writeValue(new Uint8Array([0x01,0x02]))
.then(() => { cus017Chr.writeValue(new Uint8Array([0x01,0x13])); });
miAuthorization();
});
}).catch(handleError);
}
function CGDK2_Action() {
cus017Chr = null;
addLog("Detected Qingping CGDK2 device");
//if(devinfo.frstr && devinfo.frstr.substr(0,5) == "2.2.3") addAlog("Warning! The device with this Firmware Revision is not supported!");
$("custcfg").innerHTML = '<hr>When doing an activation here the device is needed to be activated in the Mi app again when wanted to use there.<br><button type="button" onclick="sendRegister();">Do Activation</button> <button type="button" onclick="keyMiLogin();">Login</button><br>Device known id:<br><input size="40" type="text" id="known_id" value=""><br>Mi Token:<br><input size="40" type="text" id="mi_token" value=""><br>Mi Bind Key:<br><input size="40" type="text" id="mi_bind_key" value="">';
gattServer.getPrimaryService("22210000-554a-4546-5542-46534450464d")
.then(service => {
addClog("Found Custom service");
cusService = service;
return cusService.getCharacteristic(0x0100);
}).then(characteristic => {
cus100Chr = characteristic;
addClog('Found 0x0100 characteristic');
return cus100Chr.startNotifications().then(() => {
cus100Chr.addEventListener('characteristicvaluechanged',
event => CustomQingpingParse(event.target.value));
miAuthorization();
});
}).catch(handleError);
}
function CGG1_Action(model) {
cus017Chr = null;
addLog("Detected Qingping CGG1 device " + model);
$("custcfg").innerHTML = '<hr>When doing an activation here the device is needed to be activated in the Mi app again when wanted to use there.<br>Press and hold the button on the back of CGG1-M for 2 seconds until the Bluetooth icon starts blinking on the e-ink display, and then click the "Connect" and "Do Activation" buttons.<br><button type="button" onclick="sendRegister();">Do Activation</button> <button type="button" onclick="keyMiLogin();">Login</button><br>Device known id:<br><input size="40" type="text" id="known_id" value=""><br>Mi Token:<br><input size="40" type="text" id="mi_token" value=""><br>Mi Bind Key:<br><input size="40" type="text" id="mi_bind_key" value=""><br><br>';
gattServer.getPrimaryService("22210000-554a-4546-5542-46534450464d")
.then(service => {
addClog("Found Custom service");
cusService = service;
return cusService.getCharacteristic(0x0100);
}).then(characteristic => {
cus100Chr = characteristic;
addClog('Found 0x0100 characteristic');
return cus100Chr.startNotifications().then(() => {
cus100Chr.addEventListener('characteristicvaluechanged',
event => CustomQingpingParse(event.target.value));
miAuthorization();
});
}).catch(handleError);
}
//var micharacteristics;
function miAction() {
writeCharacteristicSpeed = null;
gattServer.getPrimaryService('ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6')
.then(service => {
addClog("Found MiMain service");
ServiceMain = service;
addClog("Check micharacteristics:");
return ServiceMain.getCharacteristics();
}).then(characteristics => {
//micharacteristics = characteristics;
for (let i = 0; i < characteristics.length; i++) {
addClog(characteristics[i].uuid);
if (characteristics[i].uuid == "ebe0ccd8-7a0a-4b0c-8a1a-6ff2997da3a6") {
ServiceMain.getCharacteristic(characteristics[i].uuid).then (characteristic =>
{ addClog("Found MiSet characteristic Speed");
writeCharacteristicSpeed = characteristic; });
}
else if (characteristics[i].uuid == "ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6") {
ServiceMain.getCharacteristic(characteristics[i].uuid).then ( characteristic =>
{ addClog('Found MiTemp characteristic');
nitifiyCharTemp = characteristic;
return nitifiyCharTemp.startNotifications().then(() => {
nitifiyCharTemp.addEventListener('characteristicvaluechanged', event => {
let value = event.target.value;
let temp = value.getInt16(0, true) / 100;
let hum = value.getUint8(2);
let vbat = value.getUint16(3,true);
let tempTempString = "Temp: " + temp + "°C, Humi: " + hum + "%, "+vbat+" mV";
$("tempHumiData").innerHTML = '<hr>'+tempTempString;
addClog(tempTempString); });
});
});
}
}
miAuthorization()
}).catch(handleError);
}
var buf11;
var cnt_buf11 = 0;
var cnt_delkeys = 0;
var id_buf11 = 0x1000;
var mikeys = {mac: null, id: null, token: null, bindkey: null, cfg: null, delkeys: null, restore: false, cbindkey: null};
var ext = {big_number: 0, small_number: 0, vtime: 60, flg: 0xc7, enable: false};
function concatUint8ArrayArrays(a, b) { // a, b TypedArray of same type
let c = new Uint8Array(a.byteLength + b.byteLength);
c.set(a, 0);
c.set(b, a.byteLength);
return c;
}
function dump(ar, len) {
let s = '';
for(let i=0; i < len; i++) {
s += hex(ar[i],2);
}
return s;
}
var atc_mac, atc_rmac;
function CustomBlkParse(value) {
let len = value.byteLength;
let s = '';
if(len == 0) return;
len--; // size from cmd
let blkid = value.getUint8(0);
if(devTest) {
s = 'Custom (0xffe1) Notifications id: 0x'+hex(blkid,2)+' ['+bytesToHex(value.buffer.slice(1))+']';
addAlog(s);
return;
}
if(blkid == 0x55 && len >= 9) {
cfg.ver = value.getUint8(1);
cfg.flg = value.getUint8(2);
cfg.flg2 = value.getUint8(3);
cfg.temp_offset = value.getInt8(4);
cfg.humi_offset = value.getInt8(5);
cfg.advertising_interval = value.getUint8(6);
cfg.measure_interval = value.getUint8(7);
cfg.rf_tx_power = value.getUint8(8);
if((cfg.rf_tx_power &0x80)==0) MAX_RF_TX_Power=true;
cfg.connect_latency = value.getUint8(9);
if(len >= 10) cfg.lcd_tint = value.getUint8(10);
else cfg.lcd_tint = 55;
if(len >= 11) {
cfg.hver = value.getUint8(11);
if(cfg.ver < 0x36) cfg.hver &= 0x87;
} else if(cfg.hver == null) cfg.hver = 0x80;
if(len >= 12) cfg.av_meas_mem = value.getUint8(12); else cfg.av_meas_mem = 0;
let s = 'Hardware Version: ';
if(cfg.ver < 0x48)
hwver_id = cfg.hver & 0x0F;
else
hwver_id = cfg.hver;
if((hwver_id == 0)||(hwver_id == 3)||(hwver_id == 4)||(hwver_id == 5)||(hwver_id == 10)) {
s +='LYWSD03MMC';
} else if(hwver_id == 1) s +='MHO-C401';
else if(hwver_id == 2) s +='CGG1';
else if(hwver_id == 6) s +='CGDK2';
else if(hwver_id == 7) s +='CGG1-M';
else if(hwver_id == 8) s +='MHO-C401N';
else if(hwver_id == 9) { s +='MJWSD05MMC(ch)'; bigOtaEnabled = true; }
else if(hwver_id == 11) { s +='MHO-C122'; bigOtaEnabled = true; }
else if(hwver_id == 12) { s +='MJWSD05MMC(en)'; bigOtaEnabled = true; }
else {
if(cfg.ver < 0x48) {
if((hwver_id == 15)&&(len >= 11)) {
cfg.ext_hw_id = cfg.lcd_tint & 0x7f;
hwver_id = 16 + cfg.ext_hw_id;
if((cfg.lcd_tint & 0x80) != 0) bigOtaEnabled = true;
}
}
if(hwver_id == 17) s +='TS0201';
else if(hwver_id == 18) s +='TNKS1';
else if(hwver_id == 22) s +='TH03Z';
else if(hwver_id == 27) s +='ZTH01';
else if(hwver_id == 28) s +='ZTH02';
else if(hwver_id == 30) s +='TH03';
else if(hwver_id == 31) s +='LKTMZL02';
else if(hwver_id == 33) s +='ZTH05';
else if(hwver_id == 35) s +='CB3S';
else if(hwver_id == 36) s +='HS09';
else if(hwver_id == 37) s +='ZYZTH02';
else if(hwver_id == 38) s +='ZYZTH02P';
else s += 'Unknown or DIY ('+hwver_id+')';
}
if(cfg.ver > 0x45) bigOtaEnabled = true;
if(devinfo.hrstr) s += ' ' + devinfo.hrstr;
s += ', Software Version: '+(cfg.ver>>4)+'.'+(cfg.ver&0x0f);
setStatus(s);
addAlog(s);
addAlog('Custom config HEX string: 55' + bytesToHex(value.buffer.slice(2)));
if(cfg.ver >= 5) CustomConfig();
else addLog('Old version - out of service!');
} else if(blkid == 0x33 && len >= 8) {
let vbat = value.getUint16(1, true);
let temp = value.getInt16(3, true) / 100.0;
let humi = value.getUint16(5, true) / 100.0;
let count = value.getUint16(7, true);
let flg = 0;
let rds_count = 0;
if(len > 8) {
flg = value.getUint8(9);
if(len > 12)
rds_count = value.getUint32(10, true);
}
let sw = 'Open';
if((flg & 1) == 0) sw = 'Close';
let s = '';
if(cfg.ver >= 0x37)
s = 'Vbat: '+vbat+' mV , Temp: '+temp.toFixed(2)+'°C, Humi: '+humi.toFixed(2)+'%, ID: '+count+', Switch: '+ sw +', Counter: '+ rds_count+', TRG Out: ' + ((flg & 2) >> 1) + ', flg: 0x'+hex(flg,2);
else
s = 'Vbat: '+vbat+' mV , Temp: '+temp.toFixed(2)+'°C, Humi: '+humi.toFixed(2)+'%, ID: '+count+', Switch ' + sw +', TRG Out: ' + ((flg & 2) >> 1) + ', flg: 0x'+hex(flg,2);
$("tempHumiData").innerHTML = s;
addClog(s);
} else if(blkid == 0x22 && len >= 7) {
if((hwver_id == 9) || (hwver_id == 12)) {
ext.big_number = value.getInt32(1, true); // -9950..199950, x0.01
ext.small_number = 0;
} else {
ext.big_number = value.getInt16(1, true); // -995..19995, x0.1
ext.small_number = value.getInt16(3, true); // -9..99, x1
}
ext.vtime = value.getUint16(5, true); // in sec
ext.flg = value.getUint8(7);
s = 'ExtShow: big_number: '+ext.big_number+', small_number: '+ext.small_number+', ext.vtime: '+ext.vtime+' s, flg: '+hex(ext.flg,2);
addAlog(s); UpdExt();
} else if((blkid >= 0x25 && blkid <= 0x26) && (len > 16)) {
// CMD_ID_CFS Get/Set sensor config
devSens.temp_k = value.getUint32(1, true);
devSens.humi_k = value.getUint32(5, true);
devSens.temp_z = value.getInt16(9, true);
devSens.humi_z = value.getInt16(11, true);
$('inpTempK').value = (devSens.temp_k/100.0).toFixed(2);
$('inpHumK').value = (devSens.humi_k/100.0).toFixed(2);
$('inpTempZ').value = (devSens.temp_z/100.0).toFixed(2);
$('inpHumZ').value = (devSens.humi_z/100.0).toFixed(2);
devSens.i2c_addr = value.getUint8(17);
if(len > 17) { // ver 4.9
devSens.id = value.getUint32(13, true);
let sentype = value.getUint8(18);
if(sentype == 1)
st = "SHTV3(C3)";
else if(sentype == 2)
st = "SHT4x";
else if(sentype == 3)
st = "SHT30";
else if(sentype == 4)
st = "CHT8305";
else if(sentype == 5)
st = "ANT20/30";
else if(sentype == 6)
st = "CHT8215";
else if(sentype == 7)
st = "INA226";
else if(sentype == 8)
st = "MY18B20";
else if(sentype == 9)
st = "MY18B20x2";
else if(sentype == 10)
st = "HX71X";
else if(sentype == 0)
st = "None"
} else {
devSens.id = value.getUint32(13, false);
if((devSens.id & 0xFFFF) == 0x2000)
st = "AHT20/30";
else if((devSens.id & 0xFFFF) == 0x3000)
st = "SHT30";
else if(devSens.id == 0x83055959)
st = "CHT8305";
else if(devSens.i2c_addr == 0xE0)
st = "SHTv3";
else if(devSens.i2c_addr == 0x88 || devSens.i2c_addr == 0x8A)
st = "SHT4x";
}
let s = "<b>Sensor Settings:</b> (" + st;
if(devSens.i2c_addr != 0)
s += ", I2C address: 0x"+hex(devSens.i2c_addr,2)
$('lblSensor').innerHTML = s + ", ID: " + hex(devSens.id, 8) + ")";
addAlog("Sensor "+st+" id: "+hex(devSens.id, 8)
+ ", I2C address: 0x" + hex(devSens.i2c_addr,2)
+ ", Kt: " + devSens.temp_k
+ ", Kh: " + devSens.humi_k
+ ", Zt: " + devSens.temp_z
+ ", Zh: " + devSens.humi_z);
} else if((blkid >= 0x27 && blkid <= 0x28) && (len > 6)) {
// CMD_ID_CFB20 Get/Set sensor MY18B20 config
devSens.temp2_k = value.getUint32(1, true);
devSens.temp2_z = value.getInt16(5, true);
$('inpTemp2K').value = (devSens.temp2_k/100.0).toFixed(2);
$('inpTemp2Z').value = (devSens.temp2_z/100.0).toFixed(2);
addAlog("Sensor2: Kt: " + devSens.temp2_k + ", Zt: " + devSens.temp2_z);
} else if(blkid == 0x44 && len >= 7) {
trg.tmp_thr = value.getInt16(1, true) / 100.0; // temp threshold
trg.hm_thr = value.getInt16(3, true) / 100.0; // humi threshold
trg.rds_type = 0;
trg.rds_rpint = 0;
if(len >= 9) {
trg.tmp_hst = value.getInt16(5, true) / 100.0; // temp hysteresis
trg.hm_hst = value.getInt16(7, true) / 100.0; // humi hysteresis
if((len >= 12) && (cfg.ver >= 0x37)) {
trg.rds_rpint = value.getUint16(9, true);
trg.rds_type = value.getUint8(11);
trg.flg = value.getUint8(12);
} else
trg.flg = value.getUint8(9);
} else {
trg.tmp_hst = value.getInt8(5) / 10.0; // temp hysteresis
trg.hm_hst = value.getInt8(6) / 10.0; // humi hysteresis
trg.flg = value.getInt8(7);
}
let s = '';
if(cfg.ver >= 0x37)
s = 'Threshold Temp/Humi: '+trg.tmp_thr.toFixed(2)+'°C/'+trg.hm_thr.toFixed(2)+'%, Hysteresis T/H: '+trg.tmp_hst.toFixed(2)+'°/'+trg.hm_hst.toFixed(2)+'%, Reed switch mode: '+trg.rds_type+', Rs rep.interval: '+trg.rds_rpint+' sec, flg: 0x'+hex(trg.flg,2);
else
s = 'Threshold Temp/Humi: '+trg.tmp_thr.toFixed(2)+'°C/'+trg.hm_thr.toFixed(2)+'%, Hysteresis T/H: '+trg.tmp_hst.toFixed(2)+'°/'+trg.hm_hst.toFixed(2)+'%, flg: 0x'+hex(trg.flg,2);
if((cfg.ver >= 0x39)&&((trg.rds_type&0x10)!=0)) s += ', RS input inversion'
addAlog(s); UpdTrg();
} else if(blkid >= 0x10 && blkid <= 0x14 && len >= 1) {
let lb = value.getUint8(1);
// addClog('id: '+blkid+', lb: '+lb+ ', len: ' + len);
if(lb != 0) {
len -= 1;
if(cnt_buf11 == 0){
//addClog('New miKey length: '+lb);
buf11 = new Uint8Array(lb);
}
for(let i = 0; i < len && i < lb; i++ )
buf11[cnt_buf11++] = value.getUint8(i+2);
if(len == lb) {
if(cnt_buf11 == buf11.length) {
s = '';
if(blkid == 0x14) {
if(cnt_delkeys == 0) {
mikeys.delkeys = [];
}
s = 'Marked as delete Key'+cnt_delkeys+': ';
cnt_delkeys += 1;
mikeys.delkeys.push(buf11);
}
if(cnt_buf11 == 28) {
s += 'miToken: '+dump(buf11.slice(0, 12), 12)+', miBindKey: '+dump(buf11.slice(12), 16);
if(blkid == 0x12) {
cnt_delkeys = 0;
mikeys.token = buf11.slice(0, 12);
mikeys.bindkey = buf11.slice(12);
CustomConfig();
} else if(blkid == 0x14) {
if(mikeys.token) mikeys.restore = true;
}
addLog(s);
} else if(cnt_buf11 == 20) {
let str = new TextDecoder("utf-8").decode(buf11.slice(1));
s += 'miDevId: "'+str+'" '+dump(buf11.slice(1), 19);
if(blkid == 0x11) {
cnt_delkeys = 0;
mikeys.id = buf11;
}
addLog(s);
} else if(cnt_buf11 == 8) {
addClog('Custom (0x1f1f) Notifications id: 0x'+hex(blkid,2)+' ['+bytesToHex(value.buffer.slice(1))+']');
// * public_mac: [0][1][2][3][4][5], const: [3]=38;[4]=C1;[5]=A4
// * random_static_mac: [0][1][2][6][7]C0
m = hex(buf11[5],2)+hex(buf11[4],2)+hex(buf11[3],2)+hex(buf11[2],2)+hex(buf11[1],2)+hex(buf11[0],2);
s += 'MAC: '+m+', RandMAC: '+'C0'+hex(buf11[7],2)+hex(buf11[6],2)+hex(buf11[2],2)+hex(buf11[1],2)+hex(buf11[0],2);
if(blkid == 0x10) {
cnt_delkeys = 0;
mikeys.mac = buf11;
addLog(s);
$("mi_mac").value = m + hex(buf11[7],2)+hex(buf11[6],2);