forked from Moddable-OpenSource/moddable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbles2gatt.js
1434 lines (1376 loc) · 50.9 KB
/
bles2gatt.js
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
/*
* Copyright (c) 2016-2022 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Tools.
*
* The Moddable SDK Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Moddable SDK Tools. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { FILE, TOOL } from "tool";
class GATTFile {
constructor(dictionary) {
this.tool = dictionary.tool;
this.client = dictionary.client;
this.server = dictionary.server;
this.nimble = dictionary.nimble;
this.file = dictionary.file;
this.services = dictionary.services;
}
generate() {}
};
class ESP32GATTFile extends GATTFile {
constructor(dictionary) {
super(dictionary);
this.sdkconfig = dictionary.sdkconfig;
}
};
class BluedroidGATTFile extends ESP32GATTFile {
generate() {
let tool = this.tool;
let file = this.file;
let services = this.services;
file.line('/* WARNING: This file is automatically generated. Do not edit. */');
file.line("");
file.line("typedef struct {");
file.line("\tuint8_t service_index;");
file.line("\tuint8_t att_index;");
file.line("\tconst char *name;");
file.line("\tconst char *type;");
file.line("} char_name_table;");
file.line("");
if (0 == services.length) {
file.line("#define service_count 0");
file.line("#define char_name_count 0");
file.line("#define max_attribute_count 0");
file.line("");
file.line("static const uint8_t attribute_counts[0] = {};");
file.line("static const esp_gatts_attr_db_t gatt_db[0][0] = {};");
file.line("static const char_name_table char_names[0] = {};");
return;
}
var attributeIndex = 0;
var char_names = [];
file.line("#define CHAR_DECLARATION_SIZE (sizeof(uint8_t))");
file.line("");
file.line("static const uint16_t primary_service_uuid = 0x2800;");
file.line("static const uint16_t character_declaration_uuid = 0x2803;");
file.line("static const uint16_t character_client_config_uuid = 0x2902;");
file.line("");
var maxAttributeCount = 0;
var attributeCounts = new Array(services.length);
var characteristicIndex = 0;
var descriptorIndex = 0;
services.forEach((service, index) => {
let attributeCount = 1;
let characteristics = service.characteristics;
attributeCount += (Object.keys(characteristics).length * 2);
for (let key in characteristics) {
let characteristic = characteristics[key];
if ((undefined === characteristic.permissions) && this.client)
characteristic.permissions = "read";
if ((undefined === characteristic.properties) && this.client)
characteristic.properties = "read";
let properties = characteristic.properties;
if (properties.includes("notify") || properties.includes("indicate")) {
++attributeCount;
characteristic._notify = true;
}
if ("descriptors" in characteristic) {
attributeCount += Object.keys(characteristic.descriptors).length;
characteristic._descriptors = true;
}
}
if (attributeCount > maxAttributeCount)
maxAttributeCount = attributeCount;
attributeCounts[index] = attributeCount;
this.writeAttributeUUID(file, service, index, "service");
for (let key in characteristics) {
let characteristic = characteristics[key];
this.writeAttributeUUID(file, characteristic, characteristicIndex, "char");
if ("value" in characteristic) {
this.writeAttributeValue(file, characteristic, characteristicIndex, "char");
}
if (characteristic._notify) {
file.line(`static const uint8_t char_ccc${characteristicIndex}[2] = { 0x00, 0x00 };`);
}
let properties = this.parseProperties(characteristic.properties.split(","));
file.line(`static const uint8_t char_properties${characteristicIndex} = ${toPaddedHex(properties)};`);
if ("descriptors" in characteristic) {
for (let key2 in characteristic.descriptors) {
let descriptor = characteristic.descriptors[key2];
this.writeAttributeUUID(file, descriptor, descriptorIndex, "desc");
if ("value" in descriptor) {
this.writeAttributeValue(file, descriptor, descriptorIndex, "desc");
}
++descriptorIndex;
}
}
++characteristicIndex;
}
file.line("");
});
characteristicIndex = 0;
descriptorIndex = 0;
file.line(`#define service_count ${services.length}`);
file.line(`#define max_attribute_count ${maxAttributeCount}`);
file.write(`static const uint8_t attribute_counts[${services.length}] = { `);
file.write(buffer2hexlist(attributeCounts));
file.write(" };");
file.line("");
file.line(`static const esp_gatts_attr_db_t gatt_db[${services.length}][${maxAttributeCount}] = {`);
services.forEach((service, index) => {
attributeIndex = 0;
file.line("\t{");
// primary service attribute
file.line(`\t\t// Service ${service.uuid}`);
file.line("\t\t[", attributeIndex, "] = {");
file.line("\t\t\t{ESP_GATT_AUTO_RSP},");
file.line(`\t\t\t{ESP_UUID_LEN_16, (uint8_t*)&primary_service_uuid, ESP_GATT_PERM_READ, sizeof(uint16_t), sizeof(service_uuid${index}), (uint8_t*)&service_uuid${index}}`);
file.line("\t\t},");
++attributeIndex;
let characteristics = service.characteristics;
for (let key in characteristics) {
let characteristic = characteristics[key];
if ((undefined === characteristic.permissions) && this.client)
characteristic.permissions = "read";
if ((undefined === characteristic.properties) && this.client)
characteristic.properties = "read";
// characteristic declaration
let permissions = this.parsePermissions(characteristic.permissions.split(","));
file.line("\t\t[", attributeIndex, "] = {");
file.line("\t\t\t{ESP_GATT_AUTO_RSP},");
file.line(`\t\t\t{ESP_UUID_LEN_16, (uint8_t*)&character_declaration_uuid, ${permissions}, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t*)&char_properties${characteristicIndex}}`);
file.line("\t\t},");
++attributeIndex;
// characteristic value
let maxBytes;
if (this.server && !this.client)
maxBytes = characteristic.maxBytes;
else
maxBytes = ("maxBytes" in characteristic ? characteristic.maxBytes : 0);
let esp_uuid_len = (4 == characteristic.uuid.length ? "ESP_UUID_LEN_16" : "ESP_UUID_LEN_128");
file.line("\t\t[", attributeIndex, "] = {");
if ("value" in characteristic)
file.line("\t\t\t{ESP_GATT_AUTO_RSP},");
else {
file.line("\t\t\t{ESP_GATT_RSP_BY_APP},");
}
let char_name = { service_index:index, att_index:attributeIndex, name:key };
char_name.type = characteristic.type ? characteristic.type: "";
char_names.push(char_name);
file.write(`\t\t\t{${esp_uuid_len}, (uint8_t*)&char_uuid${characteristicIndex}, ${permissions}, ${maxBytes}, `);
if ("value" in characteristic)
file.write(`${characteristic._length}, (uint8_t*)&char_value${characteristicIndex}}`);
else
file.write("0, NULL}");
file.line("");
file.line("\t\t},");
++attributeIndex;
// characteristic configuration descriptor
if (characteristic._notify) {
file.line("\t\t[", attributeIndex, "] = {");
file.line("\t\t\t{ESP_GATT_AUTO_RSP},");
file.line(`\t\t\t{ESP_UUID_LEN_16, (uint8_t*)&character_client_config_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, sizeof(uint16_t), sizeof(char_ccc${characteristicIndex}), (uint8_t*)char_ccc${characteristicIndex}}`);
file.line("\t\t},");
++attributeIndex;
}
// other characteristic descriptors
if (characteristic._descriptors) {
for (let key2 in characteristic.descriptors) {
let descriptor = characteristic.descriptors[key2];
if ((undefined === descriptor.permissions) && this.client)
descriptor.permissions = "read";
if ((undefined === descriptor.properties) && this.client)
descriptor.properties = "read";
if ((undefined === descriptor.maxBytes) && this.client)
descriptor.maxBytes = 2;
permissions = this.parsePermissions(descriptor.permissions.split(","));
esp_uuid_len = (4 == descriptor.uuid.length ? "ESP_UUID_LEN_16" : "ESP_UUID_LEN_128");
file.line("\t\t[", attributeIndex, "] = {");
if ("value" in descriptor)
file.line("\t\t\t{ESP_GATT_AUTO_RSP},");
else {
file.line("\t\t\t{ESP_GATT_RSP_BY_APP},");
}
let char_name = { service_index:index, att_index:attributeIndex, name:key2 };
char_name.type = descriptor.type ? descriptor.type: "";
char_names.push(char_name);
file.write(`\t\t\t{${esp_uuid_len}, (uint8_t*)&desc_uuid${descriptorIndex}, ${permissions}, ${descriptor.maxBytes}, `);
if ("value" in descriptor)
file.write(`${descriptor._length}, (uint8_t*)&desc_value${descriptorIndex}}`);
else
file.write("0, NULL}");
file.line("");
file.line("\t\t},");
++descriptorIndex;
++attributeIndex;
}
}
++characteristicIndex;
}
file.line("\t},");
});
file.line("};");
file.line("");
file.line(`#define char_name_count ${char_names.length}`);
file.line(`static const char_name_table char_names[${char_names.length}] = {`);
char_names.forEach(entry => {
file.line(`\t{${entry.service_index}, ${entry.att_index}, "${entry.name}", "${entry.type}"},`);
});
file.line("};");
file.line("");
}
parseProperties(properties) {
const ESP_GATT_CHAR_PROP_BIT_READ = (1 << 1);
const ESP_GATT_CHAR_PROP_BIT_WRITE_NR = (1 << 2);
const ESP_GATT_CHAR_PROP_BIT_WRITE = (1 << 3);
const ESP_GATT_CHAR_PROP_BIT_NOTIFY = (1 << 4);
const ESP_GATT_CHAR_PROP_BIT_INDICATE = (1 << 5);
const ESP_GATT_CHAR_PROP_BIT_AUTH = (1 << 6);
const ESP_GATT_CHAR_PROP_BIT_EXT_PROP = (1 << 7);
let props = 0;
properties.forEach(p => {
switch(p.trim()) {
case "read":
props |= ESP_GATT_CHAR_PROP_BIT_READ;
break;
case "write":
props |= ESP_GATT_CHAR_PROP_BIT_WRITE;
break;
case "writeNoResponse":
props |= ESP_GATT_CHAR_PROP_BIT_WRITE_NR;
break;
case "notify":
props |= ESP_GATT_CHAR_PROP_BIT_NOTIFY;
break;
case "indicate":
props |= ESP_GATT_CHAR_PROP_BIT_INDICATE;
break;
case "extended":
props |= ESP_GATT_CHAR_PROP_BIT_EXT_PROP;
break;
default:
throw new Error("unknown property");
}
});
return props;
}
parsePermissions(permissions) {
let perms = [];
let readPerms = 0;
let writePerms = 0;
permissions.forEach(p => {
switch(p.trim()) {
case "read":
++readPerms;
perms.push("ESP_GATT_PERM_READ");
break;
case "readEncrypted":
++readPerms;
perms.push("ESP_GATT_PERM_READ_ENCRYPTED");
break;
case "write":
++writePerms;
perms.push("ESP_GATT_PERM_WRITE");
break;
case "writeEncrypted":
++writePerms;
perms.push("ESP_GATT_PERM_WRITE_ENCRYPTED");
break;
default:
throw new Error("unsupported permission");
}
});
if (readPerms > 1)
throw new Error("only one of read/readEncrypted can be specified");
if (writePerms > 1)
throw new Error("only one of write/writeEncrypted can be specified");
return perms.join("|");
}
writeAttributeValue(file, attribute, index, prefix) {
let buffer = typedValueToBuffer(attribute.type, attribute.value);
attribute._length = buffer.byteLength;
file.write(`static const uint8_t ${prefix}_value${index}[${buffer.byteLength}] = { `);
file.write(buffer2hexlist(buffer));
file.write(" };");
file.line("");
}
writeAttributeUUID(file, attribute, index, prefix) {
let uuid = attribute.uuid;
if (4 == uuid.length)
file.line(`static const uint16_t ${prefix}_uuid${index} = 0x${uuid};`);
else if (36 == uuid.length) {
file.write(`static const uint8_t ${prefix}_uuid${index}[16] = { `);
file.write(buffer2hexlist(uuid128toBuffer(uuid)));
file.write(" };");
file.line("");
}
else
throw new Error("unsupported UUID length");
}
};
class NimBLEGATTFile extends ESP32GATTFile {
generate() {
let tool = this.tool;
let file = this.file;
let services = this.services;
file.line('/* WARNING: This file is automatically generated. Do not edit. */');
file.line("");
file.line("typedef struct {");
file.line("\tuint8_t service_index;");
file.line("\tuint8_t att_index;");
file.line("\tconst char *name;");
file.line("\tconst char *type;");
file.line("} char_name_table;");
file.line("");
file.line("typedef struct {");
file.line("\tconst uint8_t *value;");
file.line("\tsize_t length;");
file.line("} static_value;");
file.line("");
if (0 == services.length) {
file.line("#define service_count 0");
file.line("#define char_name_count 0");
file.line("#define max_attribute_count 0");
file.line("");
file.line("static const uint8_t attribute_counts[0] = {};");
file.line("static const struct ble_gatt_svc_def gatt_svr_svcs[] = {};");
file.line("static const char_name_table char_names[0] = {};");
return;
}
var attributeIndex = 0;
var char_names = [];
var maxAttributeCount = 0;
var attributeCounts = new Array(services.length);
var characteristicIndex = 0;
var descriptorIndex = 0;
services.forEach((service, index) => {
characteristicIndex = 0;
attributeIndex = 0;
let attributeCount = 0;
let characteristics = service.characteristics;
attributeCount += (Object.keys(characteristics).length);
if (4 == service.uuid.length) {
file.write(`static const ble_uuid16_t service${index}_uuid = BLE_UUID16_INIT(`);
file.write(`0x${service.uuid}`);
file.write(");");
}
else {
file.write(`static const ble_uuid128_t service${index}_uuid = BLE_UUID128_INIT(`);
file.write(buffer2hexlist(uuid128toBuffer(service.uuid)));
file.write(");");
}
file.line("");
for (let key in characteristics) {
let characteristic = characteristics[key];
if ((undefined === characteristic.permissions) && this.client)
characteristic.permissions = "read";
if ((undefined === characteristic.properties) && this.client)
characteristic.properties = "read";
if ("descriptors" in characteristic) {
attributeCount += Object.keys(characteristic.descriptors).length;
characteristic._descriptors = true;
}
if (4 == characteristic.uuid.length) {
file.write(`static const ble_uuid16_t service${index}_chr${characteristicIndex}_uuid = BLE_UUID16_INIT(`);
file.write(`0x${characteristic.uuid}`);
file.write(");");
}
else {
file.write(`static const ble_uuid128_t service${index}_chr${characteristicIndex}_uuid = BLE_UUID128_INIT(`);
file.write(buffer2hexlist(uuid128toBuffer(characteristic.uuid)));
file.write(");");
}
file.line("");
if ("value" in characteristic) {
let buffer = typedValueToBuffer(characteristic.type, characteristic.value);
file.write(`static const uint8_t service${index}_chr${characteristicIndex}_value[${buffer.byteLength}] = { `);
file.write(buffer2hexlist(buffer));
file.write(" };");
file.line("");
file.line(`static static_value service${index}_chr${characteristicIndex}_value_struct = { .value = service${index}_chr${characteristicIndex}_value, .length = sizeof(service${index}_chr${characteristicIndex}_value)};`);
}
if (characteristic._descriptors) {
descriptorIndex = 0;
let descriptors = characteristic.descriptors;
for (let key2 in descriptors) {
let descriptor = descriptors[key2];
if (4 == descriptor.uuid.length) {
file.write(`static const ble_uuid16_t service${index}_chr${characteristicIndex}_dsc${descriptorIndex}_uuid = BLE_UUID16_INIT(`);
file.write(`0x${descriptor.uuid}`);
file.write(");");
}
else {
file.write(`static const ble_uuid128_t service${index}_chr${characteristicIndex}_dsc${descriptorIndex}_uuid = BLE_UUID128_INIT(`);
file.write(buffer2hexlist(uuid128toBuffer(descriptor.uuid)));
file.write(");");
}
file.line("");
if ("value" in descriptor) {
let buffer = typedValueToBuffer(descriptor.type, descriptor.value);
file.write(`static const uint8_t service${index}_chr${characteristicIndex}_dsc${descriptorIndex}_value[${buffer.byteLength}] = { `);
file.write(buffer2hexlist(buffer));
file.write(" };");
file.line("");
file.line(`static static_value service${index}_chr${characteristicIndex}_dsc${descriptorIndex}_value_struct = {.value = service${index}_chr${characteristicIndex}_dsc${descriptorIndex}_value, .length = sizeof(service${index}_chr${characteristicIndex}_dsc${descriptorIndex}_value)};`);
}
++descriptorIndex;
}
}
if (this.server)
file.line(`static uint16_t service${index}_chr${characteristicIndex}_handle;`);
let char_name = { service_index:index, att_index:attributeIndex, name:key };
char_name.type = characteristic.type ? characteristic.type: "";
char_names.push(char_name);
++characteristicIndex;
++attributeIndex;
if (characteristic._descriptors) {
let descriptors = characteristic.descriptors;
for (let key2 in descriptors) {
let descriptor = descriptors[key2];
let char_name = { service_index:index, att_index:attributeIndex, name:key2 };
char_name.type = descriptor.type ? descriptor.type: "";
char_names.push(char_name);
++attributeIndex;
}
}
}
file.line("");
if (attributeCount > maxAttributeCount)
maxAttributeCount = attributeCount;
attributeCounts[index] = attributeCount;
});
file.line(`#define service_count ${services.length}`);
file.line(`#define max_attribute_count ${maxAttributeCount}`);
file.write(`static const uint8_t attribute_counts[${services.length}] = { `);
file.write(buffer2hexlist(attributeCounts));
file.write(" };");
file.line("");
file.line("");
if (this.server) {
file.line("int gatt_svr_chr_dynamic_value_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);");
file.line("");
let hasStaticCharacteristicValues = false;
services.forEach((service, index) => {
let characteristics = service.characteristics;
for (let key in characteristics) {
let characteristic = characteristics[key];
if ("value" in characteristic)
hasStaticCharacteristicValues = true;
}
});
if (hasStaticCharacteristicValues) {
file.line("static int gatt_svr_chr_static_value_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)");
file.line("{");
file.line("\tif (arg == NULL) return BLE_ATT_ERR_UNLIKELY;");
file.line("");
file.line("\tstatic_value value = *(static_value*)arg;");
file.line(`\tint rc = os_mbuf_append(ctxt->om, value.value, value.length);`);
file.line("\treturn rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;");
file.line("}");
file.line("");
}
else {
file.line("static int gatt_svr_chr_static_value_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)");
file.line("{");
file.line("\treturn 0;");
file.line("}");
file.line("");
}
}
file.line(`static const struct ble_gatt_svc_def gatt_svr_svcs[] = {`);
services.forEach((service, index) => {
characteristicIndex = 0;
file.line("\t{");
file.line(`\t\t// Service ${service.uuid}`);
file.line("\t\t.type = BLE_GATT_SVC_TYPE_PRIMARY,");
file.line(`\t\t.uuid = &service${index}_uuid.u,`);
file.line("\t\t.characteristics = (struct ble_gatt_chr_def[])");
file.line("\t\t{");
let characteristics = service.characteristics;
for (let key in characteristics) {
descriptorIndex = 0;
let characteristic = characteristics[key];
file.line("\t\t\t{")
file.line(`\t\t\t\t.uuid = &service${index}_chr${characteristicIndex}_uuid.u,`);
if (this.server) {
if ("value" in characteristic)
file.line("\t\t\t\t.access_cb = gatt_svr_chr_static_value_access_cb,");
else
file.line("\t\t\t\t.access_cb = gatt_svr_chr_dynamic_value_access_cb,");
}
else
file.line("\t\t\t\t.access_cb = NULL,");
let flags = this.parseAccess(characteristic.permissions.split(","), characteristic.properties.split(","));
file.line(`\t\t\t\t.flags = ${flags},`);
if (this.server) {
if ("value" in characteristic) {
file.line(`\t\t\t\t.arg = &service${index}_chr${characteristicIndex}_value_struct,`);
} else {
file.line(`\t\t\t\t.arg = NULL,`);
}
file.line(`\t\t\t\t.val_handle = &service${index}_chr${characteristicIndex}_handle,`);
}
if (characteristic._descriptors) {
file.line("\t\t\t\t.descriptors = (struct ble_gatt_dsc_def[])");
file.line("\t\t\t\t{");
let descriptors = characteristic.descriptors;
for (let key2 in descriptors) {
let descriptor = descriptors[key2];
if ((undefined === descriptor.permissions) && this.client)
descriptor.permissions = "read";
if ((undefined === descriptor.properties) && this.client)
descriptor.properties = "read";
file.line("\t\t\t\t\t{")
file.line(`\t\t\t\t\t\t.uuid = &service${index}_chr${characteristicIndex}_dsc${descriptorIndex}_uuid.u,`);
if (this.server) {
if ("value" in descriptor) {
file.line("\t\t\t\t\t\t.access_cb = gatt_svr_chr_static_value_access_cb,");
file.line(`\t\t\t\t\t\t.arg = &service${index}_chr${characteristicIndex}_dsc${descriptorIndex}_value_struct,`);
} else {
file.line("\t\t\t\t\t\t.access_cb = gatt_svr_chr_dynamic_value_access_cb,");
file.line(`\t\t\t\t\t\t.arg = NULL,`);
}
}
else
file.line("\t\t\t\t\t\t.access_cb = NULL,");
let flags = this.parseAccess(descriptor.permissions.split(","), descriptor.properties.split(","), "descriptor");
file.line(`\t\t\t\t\t\t.att_flags = ${flags},`);
file.line("\t\t\t\t\t},")
++descriptorIndex;
}
file.line("\t\t\t\t\t{")
file.line("\t\t\t\t\t\t0,")
file.line("\t\t\t\t\t},")
file.line("\t\t\t\t}");
}
file.line("\t\t\t},");
++characteristicIndex;
}
file.line("\t\t\t{")
file.line("\t\t\t\t0,")
file.line("\t\t\t},")
file.line("\t\t}");
file.line("\t},");
});
file.line("\t{");
file.line("\t\t0,")
file.line("\t}")
file.line("};");
file.line("");
file.line(`#define char_name_count ${char_names.length}`);
file.line(`static const char_name_table char_names[${char_names.length}] = {`);
char_names.forEach(entry => {
file.line(`\t{${entry.service_index}, ${entry.att_index}, "${entry.name}", "${entry.type}"},`);
});
file.line("};");
file.line("");
}
parseAccess(permissions, properties, type="characteristic") {
const BLE_GATT_CHR_F_BROADCAST = 0x0001;
const BLE_GATT_CHR_F_READ = 0x0002;
const BLE_GATT_CHR_F_WRITE_NO_RSP = 0x0004;
const BLE_GATT_CHR_F_WRITE = 0x0008;
const BLE_GATT_CHR_F_NOTIFY = 0x0010;
const BLE_GATT_CHR_F_INDICATE = 0x0020;
const BLE_GATT_CHR_F_AUTH_SIGN_WRITE = 0x0040;
const BLE_GATT_CHR_F_RELIABLE_WRITE = 0x0080;
const BLE_GATT_CHR_F_AUX_WRITE = 0x0100;
const BLE_GATT_CHR_F_READ_ENC = 0x0200;
const BLE_GATT_CHR_F_READ_AUTHEN = 0x0400;
const BLE_GATT_CHR_F_READ_AUTHOR = 0x0800;
const BLE_GATT_CHR_F_WRITE_ENC = 0x1000;
const BLE_GATT_CHR_F_WRITE_AUTHEN = 0x2000;
const BLE_GATT_CHR_F_WRITE_AUTHOR = 0x4000;
const BLE_GATT_DESC_F_READ = 0x01;
const BLE_GATT_DESC_F_WRITE = 0x02;
const BLE_GATT_DESC_F_READ_ENC = 0x04;
const BLE_GATT_DESC_F_READ_AUTHEN = 0x08;
const BLE_GATT_DESC_F_READ_AUTHOR = 0x10;
const BLE_GATT_DESC_F_WRITE_ENC = 0x20;
const BLE_GATT_DESC_F_WRITE_AUTHEN = 0x40;
const BLE_GATT_DESC_F_WRITE_AUTHOR = 0x80;
const isCharacteristic = (type == "characteristic");
let flags = 0;
properties.forEach(p => {
switch(p.trim()) {
case "read":
flags |= (isCharacteristic ? BLE_GATT_CHR_F_READ : BLE_GATT_DESC_F_READ);
break;
case "write":
flags |= (isCharacteristic ? BLE_GATT_CHR_F_WRITE : BLE_GATT_DESC_F_WRITE);
break;
case "writeNoResponse":
flags |= BLE_GATT_CHR_F_WRITE_NO_RSP;
break;
case "notify":
flags |= BLE_GATT_CHR_F_NOTIFY;
break;
case "indicate":
flags |= BLE_GATT_CHR_F_INDICATE;
break;
case "extended":
flags |= BLE_GATT_CHR_F_AUX_WRITE;
break;
default:
throw new Error("unknown property");
}
});
let readPerms = 0;
let writePerms = 0;
permissions.forEach(p => {
switch(p.trim()) {
case "read":
++readPerms;
flags |= (isCharacteristic ? BLE_GATT_CHR_F_READ : BLE_GATT_DESC_F_READ);
break;
case "readEncrypted":
++readPerms;
flags |= (isCharacteristic ? BLE_GATT_CHR_F_READ_ENC : BLE_GATT_DESC_F_READ_ENC);
break;
case "write":
++writePerms;
flags |= (isCharacteristic ? BLE_GATT_CHR_F_WRITE : BLE_GATT_DESC_F_WRITE);
break;
case "writeEncrypted":
++writePerms;
flags |= (isCharacteristic ? BLE_GATT_CHR_F_WRITE_ENC : BLE_GATT_DESC_F_WRITE_ENC);
break;
default:
throw new Error("unsupported permission");
}
});
if (readPerms > 1)
throw new Error("only one of read/readEncrypted can be specified");
if (writePerms > 1)
throw new Error("only one of write/writeEncrypted can be specified");
if (!isCharacteristic && flags > 0xFF)
throw new Error("characteristic properties cannot be applied to descriptors");
return flags;
}
};
class QCA4020GATTFile extends GATTFile {
generate() {
let tool = this.tool;
let file = this.file;
let services = this.services;
file.line('/* WARNING: This file is automatically generated. Do not edit. */');
file.line("");
file.line("typedef struct {");
file.line("\tuint8_t service_index;");
file.line("\tuint8_t att_index;");
file.line("\tuint8_t encrypted;");
file.line("\tconst char *name;");
file.line("\tconst char *type;");
file.line("} char_name_table;");
file.line("");
if (0 == services.length) {
file.line("#define service_count 0");
file.line("#define char_name_count 0");
file.line("#define max_attribute_count 0");
file.line("");
file.line("static qapi_BLE_GATT_Service_Attribute_Entry_t ServiceTable[0][0] = {};");
file.line("static const uint8_t attribute_counts[0] = {};");
file.line("static const char_name_table char_names[0] = {};");
return;
}
var attributeIndex = 0;
var char_names = [];
var maxAttributeCount = 0;
var attributeCounts = new Array(services.length);
var characteristicIndex = 0;
var did_cccd = 0;
services.forEach((service, index) => {
let attributeCount = 1;
let characteristics = service.characteristics;
attributeCount += (Object.keys(characteristics).length * 2);
for (let key in characteristics) {
let characteristic = characteristics[key];
if ((undefined === characteristic.permissions) && this.client)
characteristic.permissions = "read";
if ((undefined === characteristic.properties) && this.client)
characteristic.properties = "read";
let properties = characteristic.properties;
if (properties.includes("notify") || properties.includes("indicate")) {
++attributeCount;
characteristic._notify = true;
if (!did_cccd) {
did_cccd = true;
file.line("static qapi_BLE_GATT_Characteristic_Descriptor_16_Entry_t cccd = {{0x02, 0x29}, 2, NULL};");
file.line("");
}
}
}
if (attributeCount > maxAttributeCount)
maxAttributeCount = attributeCount;
attributeCounts[index] = attributeCount;
if (4 == service.uuid.length) {
file.write(`static qapi_BLE_GATT_Primary_Service_16_Entry_t service${index} = {{`);
file.write(buffer2hexlist(uuid16toBuffer(parseInt(service.uuid, 16))));
file.write("}};");
}
else {
file.write(`static qapi_BLE_GATT_Primary_Service_128_Entry_t service${index} = {{`);
file.write(buffer2hexlist(uuid128toBuffer(service.uuid)));
file.write("}};");
}
file.line("");
file.line("");
for (let key in characteristics) {
let characteristic = characteristics[key];
let uuid = characteristic.uuid;
let properties = this.parseProperties(characteristic.properties.split(","));
if (4 == uuid.length) {
file.write(`static qapi_BLE_GATT_Characteristic_Declaration_16_Entry_t characteristic${characteristicIndex} = {`);
file.write(`${properties}, {`);
file.write(buffer2hexlist(uuid16toBuffer(parseInt(uuid, 16))));
file.write("}};");
file.line("");
}
else {
file.write(`static qapi_BLE_GATT_Characteristic_Declaration_128_Entry_t characteristic${characteristicIndex} = {`);
file.write(`${properties}, {`);
file.write(buffer2hexlist(uuid128toBuffer(uuid)));
file.write("}};");
file.line("");
}
if ("value" in characteristic) {
let buffer = typedValueToBuffer(characteristic.type, characteristic.value);
characteristic._length = buffer.byteLength;
file.write(`static uint8_t char_value_buffer${characteristicIndex}[${buffer.byteLength}] = { `);
file.write(buffer2hexlist(buffer));
file.write(" };");
file.line("");
if (4 == uuid.length) {
file.write(`static qapi_BLE_GATT_Characteristic_Value_16_Entry_t char_value${characteristicIndex} = {{`);
file.write(buffer2hexlist(uuid16toBuffer(parseInt(uuid, 16))));
file.write(`}, ${characteristic._length}, char_value_buffer${characteristicIndex}`);
file.write(" };");
}
else {
file.write(`static qapi_BLE_GATT_Characteristic_Value_128_Entry_t char_value${characteristicIndex} = {{`);
file.write(buffer2hexlist(uuid128toBuffer(uuid)));
file.write(`}, ${characteristic._length}, char_value_buffer${characteristicIndex}`);
file.write(" };");
}
}
else {
let maxBytes;
if (this.server && !this.client)
maxBytes = characteristic.maxBytes;
else
maxBytes = ("maxBytes" in characteristic ? characteristic.maxBytes : 0);
if (4 == uuid.length) {
file.write(`static qapi_BLE_GATT_Characteristic_Value_16_Entry_t char_value${characteristicIndex} = {{`);
file.write(buffer2hexlist(uuid16toBuffer(parseInt(uuid, 16))));
file.write(`}, ${maxBytes}, NULL`);
file.write(" };");
}
else {
file.write(`static qapi_BLE_GATT_Characteristic_Value_128_Entry_t char_value${characteristicIndex} = {{`);
file.write(buffer2hexlist(uuid128toBuffer(uuid)));
file.write(`}, ${maxBytes}, NULL`);
file.write(" };");
}
}
file.line("");
++characteristicIndex;
}
});
characteristicIndex = 0;
file.line(`#define service_count ${services.length}`);
file.line(`#define max_attribute_count ${maxAttributeCount}`);
file.write(`static const uint8_t attribute_counts[${services.length}] = { `);
file.write(buffer2hexlist(attributeCounts));
file.write(" };");
file.line("");
file.line(`static qapi_BLE_GATT_Service_Attribute_Entry_t ServiceTable[${services.length}][${maxAttributeCount}] = {`);
services.forEach((service, index) => {
attributeIndex = 0;
file.line("\t{");
// primary service attribute
let uuid = service.uuid;
file.line(`\t\t// Service ${uuid}`);
file.line("\t\t[", attributeIndex, "] = {");
file.line("\t\t\tQAPI_BLE_GATT_ATTRIBUTE_FLAGS_READABLE,");
if (4 == uuid.length)
file.line("\t\t\tQAPI_BLE_AET_PRIMARY_SERVICE_16_E,");
else
file.line("\t\t\tQAPI_BLE_AET_PRIMARY_SERVICE_128_E,");
file.line(`\t\t\t(void*)&service${index},`);
file.line("\t\t},");
++attributeIndex;
let characteristics = service.characteristics;
for (let key in characteristics) {
let characteristic = characteristics[key];
let uuid = characteristic.uuid;
let encrypted = this.isEncrypted(characteristic);
// characteristic declaration
file.line("\t\t[", attributeIndex, "] = {");
file.line("\t\t\tQAPI_BLE_GATT_ATTRIBUTE_FLAGS_READABLE,");
if (4 == uuid.length)
file.line("\t\t\tQAPI_BLE_AET_CHARACTERISTIC_DECLARATION_16_E,");
else
file.line("\t\t\tQAPI_BLE_AET_CHARACTERISTIC_DECLARATION_128_E,");
file.line(`\t\t\t(void*)&characteristic${characteristicIndex},`);
file.line("\t\t},");
++attributeIndex;
// characteristic value
let permissions = this.parsePermissions(characteristic.permissions.split(","));
file.line("\t\t[", attributeIndex, "] = {");
file.line(`\t\t\t${permissions},`);
if (4 == uuid.length)
file.line("\t\t\tQAPI_BLE_AET_CHARACTERISTIC_VALUE_16_E,");
else
file.line("\t\t\tQAPI_BLE_AET_CHARACTERISTIC_VALUE_128_E,");
file.line(`\t\t\t(void*)&char_value${characteristicIndex},`);
file.line("\t\t},");
let char_name = { service_index:index, att_index:attributeIndex, encrypted, name:key };
char_name.type = characteristic.type ? characteristic.type: "";
char_names.push(char_name);
++attributeIndex;
// characteristic configuration descriptor
if (characteristic._notify) {
file.line("\t\t[", attributeIndex, "] = {");
file.line("\t\t\tQAPI_BLE_GATT_ATTRIBUTE_FLAGS_WRITABLE,");
file.line("\t\t\tQAPI_BLE_AET_CHARACTERISTIC_DESCRIPTOR_16_E,");
file.line("\t\t\t(void*)&cccd,");
file.line("\t\t},");
++attributeIndex;
}
++characteristicIndex;
}
file.line("\t},");
});
file.line("};");
file.line("");
file.line(`#define char_name_count ${char_names.length}`);
file.line(`static const char_name_table char_names[${char_names.length}] = {`);
char_names.forEach(entry => {
file.line(`\t{${entry.service_index}, ${entry.att_index}, ${entry.encrypted}, "${entry.name}", "${entry.type}"},`);
});
file.line("};");
file.line("");
}
parseProperties(properties) {
let props = [];
properties.forEach(p => {
switch(p.trim()) {
case "read":
props.push("QAPI_BLE_GATT_CHARACTERISTIC_PROPERTIES_READ");
break;
case "write":
props.push("QAPI_BLE_GATT_CHARACTERISTIC_PROPERTIES_WRITE");
break;
case "writeNoResponse":
props.push("QAPI_BLE_GATT_CHARACTERISTIC_PROPERTIES_WRITE_WITHOUT_RESPONSE");
break;
case "notify":
props.push("QAPI_BLE_GATT_CHARACTERISTIC_PROPERTIES_NOTIFY");
break;
case "indicate":
props.push("QAPI_BLE_GATT_CHARACTERISTIC_PROPERTIES_INDICATE");
break;
case "extended":
props.push("QAPI_BLE_GATT_CHARACTERISTIC_PROPERTIES_EXTENDED_PROPERTIES");
break;
default:
throw new Error("unknown property");
}
});
return props.join('|');
}
parsePermissions(permissions) {
let perms = [];
let readPerms = 0;
let writePerms = 0;
permissions.forEach(p => {
switch(p.trim()) {
case "read":
++readPerms;
perms.push("QAPI_BLE_GATT_ATTRIBUTE_FLAGS_READABLE");
break;
case "readEncrypted":
++readPerms;
perms.push("QAPI_BLE_GATT_ATTRIBUTE_FLAGS_READABLE");
break;
case "write":
++writePerms;
perms.push("QAPI_BLE_GATT_ATTRIBUTE_FLAGS_WRITABLE");
break;
case "writeEncrypted":
++writePerms;
perms.push("QAPI_BLE_GATT_ATTRIBUTE_FLAGS_WRITABLE");
break;
default:
throw new Error("unsupported permission");
}
});
if (readPerms > 1)
throw new Error("only one of read/readEncrypted can be specified");
if (writePerms > 1)
throw new Error("only one of write/writeEncrypted can be specified");
return perms.join('|');
}
isEncrypted(characteristic) {
let encrypted = (-1 != characteristic.permissions.indexOf("readEncrypted") || -1 != characteristic.permissions.indexOf("writeEncrypted"));
return encrypted ? 1 : 0;
}
};
class GeckoGATTFile extends GATTFile {
generate() {
let file = this.file;
let services = this.services;
let clientOnly = (this.client && !this.server);
file.line('/* WARNING: This file is automatically generated. Do not edit. */');
file.line('');
if (!clientOnly) {
file.line('#include "bg_gattdb_def.h"');
file.line('');
file.line('#ifdef __GNUC__');
file.line('\t#define GATT_HEADER(F) F __attribute__ ((section (".gatt_header")))');
file.line('\t#define GATT_DATA(F) F __attribute__ ((section (".gatt_data")))');
file.line('#else');
file.line('\t#ifdef __ICCARM__');
file.line('\t\t#define GATT_HEADER(F) _Pragma("location=\\".gatt_header\\"") F');
file.line('\t\t#define GATT_DATA(F) _Pragma("location=\\".gatt_data\\"") F');
file.line('\t#else');
file.line('\t\t#define GATT_HEADER(F) F');
file.line('\t\t#define GATT_DATA(F) F');
file.line('\t#endif');
file.line('#endif');
file.line('');