forked from intel/zephyr.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzjs_ble.c
1359 lines (1136 loc) · 43.4 KB
/
zjs_ble.c
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-2018, Intel Corporation.
#ifdef BUILD_MODULE_BLE
#ifndef QEMU_BUILD
// C includes
#include <stdlib.h>
#include <string.h>
// Zephyr includes
#include <bluetooth/bluetooth.h>
#include <bluetooth/conn.h>
#include <bluetooth/gatt.h>
#include <bluetooth/hci.h>
#include <bluetooth/uuid.h>
#include <zephyr.h>
// ZJS includes
#include "zjs_ble.h"
#include "zjs_buffer.h"
#include "zjs_callbacks.h"
#include "zjs_event.h"
#include "zjs_util.h"
#define ZJS_BLE_UUID_LEN 36
#define ZJS_BLE_RESULT_SUCCESS 0x00
#define ZJS_BLE_RESULT_INVALID_OFFSET BT_ATT_ERR_INVALID_OFFSET
#define ZJS_BLE_RESULT_ATTR_NOT_LONG BT_ATT_ERR_ATTRIBUTE_NOT_LONG
#define ZJS_BLE_RESULT_INVALID_ATTRIBUTE_LEN BT_ATT_ERR_INVALID_ATTRIBUTE_LEN
#define ZJS_BLE_RESULT_UNLIKELY_ERROR BT_ATT_ERR_UNLIKELY
#define ZJS_BLE_TIMEOUT_TICKS 5000
typedef void (*ccc_cfg_changed_func)(const struct bt_gatt_attr *attr,
u16_t value);
typedef struct ble_buffer_handle {
zjs_callback_id id;
jerry_value_t js_callback;
const void *buffer;
u16_t buffer_size;
u16_t offset;
u32_t error_code;
} ble_buffer_handle_t;
typedef struct ble_notify_handle {
zjs_callback_id id;
jerry_value_t js_callback;
} ble_notify_handle_t;
typedef struct zjs_ble_characteristic {
int flags;
jerry_value_t chrc_obj;
struct bt_uuid *uuid;
struct bt_gatt_attr *chrc_attr;
struct bt_gatt_attr *ccc_attr;
jerry_value_t cud_value;
ble_buffer_handle_t read_cb;
ble_buffer_handle_t write_cb;
ble_notify_handle_t subscribe_cb;
ble_notify_handle_t unsubscribe_cb;
ble_notify_handle_t notify_cb;
ccc_cfg_changed_func ccc_cfg_changed;
struct zjs_ble_characteristic *next;
} ble_characteristic_t;
typedef struct zjs_ble_service {
jerry_value_t service_obj;
struct bt_uuid *uuid;
ble_characteristic_t *characteristics;
struct zjs_ble_service *next;
struct bt_gatt_service svc;
} ble_service_t;
typedef struct zjs_ble_connection {
struct bt_conn *bt_conn;
struct zjs_ble_connection *next;
} ble_connection_t;
typedef struct zjs_ble_handle {
struct bt_gatt_ccc_cfg blvl_ccc_cfg[CONFIG_BT_MAX_PAIRED];
jerry_value_t ble_obj;
ble_service_t *services;
ble_connection_t *connections;
} ble_handle_t;
static struct bt_uuid *gatt_primary_service_uuid = BT_UUID_GATT_PRIMARY;
static struct bt_uuid *gatt_characteristic_uuid = BT_UUID_GATT_CHRC;
static struct bt_uuid *gatt_cud_uuid = BT_UUID_GATT_CUD;
static struct bt_uuid *gatt_ccc_uuid = BT_UUID_GATT_CCC;
static struct k_sem ble_sem;
static ble_handle_t *ble_handle = NULL;
static const jerry_object_native_info_t ble_type_info = {
.free_cb = free_handle_nop
};
struct bt_uuid *zjs_ble_new_uuid_16(u16_t value)
{
struct bt_uuid_16 *uuid = zjs_malloc(sizeof(struct bt_uuid_16));
if (!uuid) {
ERR_PRINT("out of memory allocating struct bt_uuid_16\n");
return NULL;
}
memset(uuid, 0, sizeof(struct bt_uuid_16));
uuid->uuid.type = BT_UUID_TYPE_16;
uuid->val = value;
return (struct bt_uuid *)uuid;
}
static ble_connection_t *zjs_ble_new_connection(ble_connection_t **conns,
struct bt_conn *conn)
{
ble_connection_t *new_conn = zjs_malloc(sizeof(ble_connection_t));
if (!new_conn) {
ERR_PRINT("cannot allocate ble_connection_t\n");
return NULL;
}
memset(new_conn, 0, sizeof(ble_connection_t));
new_conn->bt_conn = conn;
new_conn->next = *conns;
*conns = new_conn;
return new_conn;
}
static void zjs_ble_release_connection(ble_connection_t **conns,
ble_connection_t *conn)
{
if (!conns || !conn)
return;
if (*conns == conn) {
// if conn is the head
ble_connection_t *tmp = *conns;
*conns = (*conns)->next;
bt_conn_unref(tmp->bt_conn);
zjs_free(tmp);
return;
}
ble_connection_t *current = (*conns)->next;
ble_connection_t *previous = *conns;
while (current && previous) {
if (current == conn) {
ble_connection_t *tmp = current;
previous->next = current->next;
bt_conn_unref(tmp->bt_conn);
zjs_free(tmp);
return;
}
previous = current;
current = current->next;
}
}
static void zjs_ble_free_characteristics(ble_characteristic_t *chrc)
{
ble_characteristic_t *tmp;
while (chrc) {
tmp = chrc;
chrc = chrc->next;
jerry_release_value(tmp->chrc_obj);
if (tmp->cud_value) {
jerry_release_value(tmp->cud_value);
}
if (tmp->uuid)
zjs_free(tmp->uuid);
if (tmp->read_cb.id != -1) {
zjs_remove_callback(tmp->read_cb.id);
jerry_release_value(tmp->read_cb.js_callback);
}
if (tmp->write_cb.id != -1) {
zjs_remove_callback(tmp->write_cb.id);
jerry_release_value(tmp->write_cb.js_callback);
}
if (tmp->subscribe_cb.id != -1) {
zjs_remove_callback(tmp->subscribe_cb.id);
jerry_release_value(tmp->subscribe_cb.js_callback);
}
if (tmp->unsubscribe_cb.id != -1) {
zjs_remove_callback(tmp->unsubscribe_cb.id);
jerry_release_value(tmp->unsubscribe_cb.js_callback);
}
if (tmp->notify_cb.id != -1) {
zjs_remove_callback(tmp->notify_cb.id);
jerry_release_value(tmp->notify_cb.js_callback);
}
zjs_free(tmp);
}
}
static void zjs_ble_free_services(ble_service_t *service)
{
ble_service_t *tmp;
while (service) {
tmp = service;
service = service->next;
jerry_release_value(tmp->service_obj);
if (tmp->uuid)
zjs_free(tmp->uuid);
if (tmp->characteristics)
zjs_ble_free_characteristics(tmp->characteristics);
if (tmp->svc.attrs)
zjs_free(tmp->svc.attrs);
zjs_free(tmp);
}
}
static ZJS_DECL_FUNC(zjs_ble_read_callback_function)
{
// TODO: couldn't use ZJS_VALIDATE_ARGS here because it needs to give the
// semaphore on error case
if (argc != 2 || !jerry_value_is_number(argv[0]) ||
!zjs_value_is_buffer(argv[1])) {
k_sem_give(&ble_sem);
return zjs_error("invalid arguments");
}
ZJS_GET_HANDLE(function_obj, struct zjs_ble_characteristic, chrc,
ble_type_info);
chrc->read_cb.error_code = (u32_t)jerry_get_number_value(argv[0]);
zjs_buffer_t *buf = zjs_buffer_find(argv[1]);
if (buf) {
chrc->read_cb.buffer = buf->buffer;
chrc->read_cb.buffer_size = buf->bufsize;
} else {
ERR_PRINT("buffer not found\n");
}
// unblock fiber
k_sem_give(&ble_sem);
return ZJS_UNDEFINED;
}
static void zjs_ble_read_c_callback(void *handle, const void *argv)
{
ble_characteristic_t *chrc = (ble_characteristic_t *)handle;
ble_buffer_handle_t *cb = &chrc->read_cb;
ZVAL offset = jerry_create_number(cb->offset);
ZVAL callback =
jerry_create_external_function(zjs_ble_read_callback_function);
jerry_set_object_native_pointer(callback, handle, &ble_type_info);
jerry_value_t args[2] = { offset, callback };
ZVAL rval = jerry_call_function(cb->js_callback, chrc->chrc_obj, args, 2);
if (jerry_value_is_error(rval)) {
DBG_PRINT("failed to call onReadRequest function\n");
}
}
// INTERRUPT SAFE FUNCTION: No JerryScript VM, allocs, or release prints!
static ssize_t zjs_ble_read_attr_callback(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
void *buf, u16_t len, u16_t offset)
{
if (offset > len) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
ble_characteristic_t *chrc = attr->user_data;
if (!chrc) {
ERR_PRINT("characteristic not found\n");
return BT_GATT_ERR(BT_ATT_ERR_INVALID_HANDLE);
}
if (chrc->read_cb.id != -1) {
// This is from the FIBER context, so we queue up the callback
// to invoke js from task context
chrc->read_cb.offset = offset;
chrc->read_cb.buffer = NULL;
chrc->read_cb.buffer_size = 0;
chrc->read_cb.error_code = BT_ATT_ERR_NOT_SUPPORTED;
zjs_signal_callback(chrc->read_cb.id, NULL, 0);
// block until result is ready
if (k_sem_take(&ble_sem, ZJS_BLE_TIMEOUT_TICKS)) {
ERR_PRINT("JS callback timed out\n");
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
}
if (chrc->read_cb.error_code == ZJS_BLE_RESULT_SUCCESS) {
if (chrc->read_cb.buffer && chrc->read_cb.buffer_size > 0) {
// buffer should point to the Buffer object that JS created
// copy the bytes into the return buffer ptr
memcpy(buf, chrc->read_cb.buffer, chrc->read_cb.buffer_size);
return chrc->read_cb.buffer_size;
}
ERR_PRINT("buffer is empty\n");
return BT_GATT_ERR(BT_ATT_ERR_NOT_SUPPORTED);
} else {
ERR_PRINT("on read attr error %u\n",
(unsigned int)chrc->read_cb.error_code);
return BT_GATT_ERR(chrc->read_cb.error_code);
}
}
DBG_PRINT("js callback not available\n");
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
}
static ZJS_DECL_FUNC(zjs_ble_write_callback_function)
{
// TODO: couldn't use ZJS_VALIDATE_ARGS here because it needs to give the
// semaphore on error case
if (argc != 1 || !jerry_value_is_number(argv[0])) {
k_sem_give(&ble_sem);
return zjs_error("invalid arguments");
}
ZJS_GET_HANDLE(function_obj, struct zjs_ble_characteristic, chrc,
ble_type_info);
// store the return value in the write_cb struct
chrc->write_cb.error_code = (u32_t)jerry_get_number_value(argv[0]);
// unblock fiber
k_sem_give(&ble_sem);
return ZJS_UNDEFINED;
}
static void zjs_ble_write_c_callback(void *handle, const void *argv)
{
ble_characteristic_t *chrc = (ble_characteristic_t *)handle;
ble_buffer_handle_t *cb = &chrc->write_cb;
ZVAL_MUTABLE buf_obj;
if (cb->buffer && cb->buffer_size > 0) {
zjs_buffer_t *buf;
buf_obj = zjs_buffer_create(cb->buffer_size, &buf);
if (buf) {
memcpy(buf->buffer, cb->buffer, cb->buffer_size);
} else {
// can't pass object with error flag as a JS arg
jerry_value_clear_error_flag(&buf_obj);
}
} else {
buf_obj = jerry_create_null();
}
ZVAL offset = jerry_create_number(cb->offset);
// TODO: support withoutResponse flag
ZVAL without_response = jerry_create_boolean(false);
ZVAL callback =
jerry_create_external_function(zjs_ble_write_callback_function);
jerry_set_object_native_pointer(callback, handle, &ble_type_info);
jerry_value_t args[4] = { buf_obj, offset, without_response, callback };
ZVAL rval = jerry_call_function(cb->js_callback, chrc->chrc_obj, args, 4);
if (jerry_value_is_error(rval)) {
DBG_PRINT("failed to call onWriteRequest function\n");
}
}
// INTERRUPT SAFE FUNCTION: No JerryScript VM, allocs, or release prints!
static ssize_t zjs_ble_write_attr_callback(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
const void *buf, u16_t len,
u16_t offset, u8_t flags)
{
ble_characteristic_t *chrc = attr->user_data;
if (!chrc) {
ERR_PRINT("characteristic not found\n");
return BT_GATT_ERR(BT_ATT_ERR_INVALID_HANDLE);
}
if (chrc->write_cb.id != -1) {
// This is from the FIBER context, so we queue up the callback
// to invoke js from task context
chrc->write_cb.offset = offset;
chrc->write_cb.buffer = (len > 0) ? buf : NULL;
chrc->write_cb.buffer_size = len;
chrc->write_cb.error_code = BT_ATT_ERR_NOT_SUPPORTED;
zjs_signal_callback(chrc->write_cb.id, NULL, 0);
// block until result is ready
if (k_sem_take(&ble_sem, ZJS_BLE_TIMEOUT_TICKS)) {
ERR_PRINT("JS callback timed out\n");
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
}
if (chrc->write_cb.error_code == ZJS_BLE_RESULT_SUCCESS) {
return len;
} else {
return BT_GATT_ERR(chrc->write_cb.error_code);
}
}
DBG_PRINT("js callback not available\n");
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
}
static ZJS_DECL_FUNC(zjs_ble_update_value_callback_function)
{
// args: buffer
ZJS_VALIDATE_ARGS(Z_BUFFER);
ZJS_GET_HANDLE(this, struct zjs_ble_characteristic, chrc, ble_type_info);
if (chrc->chrc_attr) {
zjs_buffer_t *buf = zjs_buffer_find(argv[0]);
if (buf) {
// loop through all the connections
ble_connection_t *conn = ble_handle->connections;
while (conn) {
bt_gatt_notify(conn->bt_conn, chrc->chrc_attr, buf->buffer,
buf->bufsize);
conn = conn->next;
}
}
}
return ZJS_UNDEFINED;
}
static void zjs_ble_subscribe_c_callback(void *handle, const void *argv)
{
ble_characteristic_t *chrc = (ble_characteristic_t *)handle;
ble_notify_handle_t *cb = &chrc->subscribe_cb;
ZVAL max_size = jerry_create_number(20);
ZVAL callback =
jerry_create_external_function(zjs_ble_update_value_callback_function);
jerry_value_t args[2] = { max_size, callback };
ZVAL rval = jerry_call_function(cb->js_callback, chrc->chrc_obj, args, 2);
if (jerry_value_is_error(rval)) {
DBG_PRINT("failed to call onSubscribe function\n");
}
}
static void zjs_ble_unsubscribe_c_callback(void *handle, const void *argv)
{
ble_characteristic_t *chrc = (ble_characteristic_t *)handle;
ble_notify_handle_t *cb = &chrc->unsubscribe_cb;
ZVAL rval = jerry_call_function(cb->js_callback, chrc->chrc_obj, NULL, 0);
if (jerry_value_is_error(rval)) {
DBG_PRINT("failed to call onUnsubscribe function\n");
}
}
static void zjs_ble_notify_c_callback(void *handle, const void *argv)
{
ble_characteristic_t *chrc = (ble_characteristic_t *)handle;
ble_notify_handle_t *cb = &chrc->notify_cb;
ZVAL rval = jerry_call_function(cb->js_callback, chrc->chrc_obj, NULL, 0);
if (jerry_value_is_error(rval)) {
DBG_PRINT("failed to call onNotify function\n");
}
}
static ble_characteristic_t *get_base_chrc(const struct bt_gatt_attr *attr)
{
ble_service_t *service;
ble_characteristic_t *chrc;
for (service = ble_handle->services; service; service = service->next) {
for (chrc = service->characteristics; chrc; chrc = chrc->next) {
if (chrc->ccc_attr == attr) {
return chrc;
}
}
}
return NULL;
}
// INTERRUPT SAFE FUNCTION: No JerryScript VM, allocs, or release prints!
static void zjs_ble_blvl_ccc_cfg_changed(const struct bt_gatt_attr *attr,
u16_t value)
{
ble_characteristic_t *base_chrc = get_base_chrc(attr);
if (base_chrc) {
if (base_chrc->subscribe_cb.id != -1 && value == 1) {
DBG_PRINT("client subscribed for notification\n");
zjs_signal_callback(base_chrc->subscribe_cb.id, NULL, 0);
} else if (base_chrc->unsubscribe_cb.id != -1 && value == 0) {
DBG_PRINT("client unsubscribed for notification\n");
zjs_signal_callback(base_chrc->unsubscribe_cb.id, NULL, 0);
}
} else {
ERR_PRINT("base characteristic not found\n");
}
}
// a zjs_pre_emit callback
static bool string_arg(void *unused, jerry_value_t argv[], u32_t *argc,
const char *buffer, u32_t bytes)
{
// requires: buffer contains string with bytes chars including null term
argv[0] = jerry_create_string((jerry_char_t *)buffer);
*argc = 1;
return true;
}
// INTERRUPT SAFE FUNCTION: No JerryScript VM, allocs, or release prints!
static void zjs_ble_connected(struct bt_conn *conn, u8_t err)
{
if (err) {
ERR_PRINT("Connection failed (err %u)\n", err);
} else {
DBG_PRINT("new connection\n");
// FIXME: this should probably be a static pool to avoid malloc here,
// based on configured max connections
ble_connection_t *new_conn =
zjs_ble_new_connection(&ble_handle->connections, conn);
if (!new_conn) {
ERR_PRINT("failed to create new client connection\n");
return;
}
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
new_conn->bt_conn = bt_conn_ref(conn);
zjs_defer_emit_event(ble_handle->ble_obj, "accept", addr,
BT_ADDR_LE_STR_LEN, string_arg, zjs_release_args);
DBG_PRINT("client connected: %s\n", addr);
}
}
// INTERRUPT SAFE FUNCTION: No JerryScript VM, allocs, or release prints!
static void zjs_ble_disconnected(struct bt_conn *conn, u8_t reason)
{
ble_connection_t *ble_conn = ble_handle->connections;
while (ble_conn) {
if (ble_conn->bt_conn == conn) {
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
zjs_defer_emit_event(ble_handle->ble_obj, "disconnect", addr,
BT_ADDR_LE_STR_LEN, string_arg,
zjs_release_args);
zjs_ble_release_connection(&ble_handle->connections, ble_conn);
DBG_PRINT("client disconnected (reason %u): %s\n", reason, addr);
return;
}
ble_conn = ble_conn->next;
}
DBG_PRINT("connection not found\n");
}
static struct bt_conn_cb zjs_ble_conn_callbacks = {
.connected = zjs_ble_connected,
.disconnected = zjs_ble_disconnected,
};
static void zjs_ble_auth_cancel(struct bt_conn *conn)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
DBG_PRINT("pairing cancelled: %s\n", addr);
}
static struct bt_conn_auth_cb zjs_ble_auth_cb_display = {
.cancel = zjs_ble_auth_cancel,
};
void zjs_ble_emit_powered_event()
{
const char state[] = "poweredOn";
ZJS_ASSERT(ble_handle, "ble_handle not set");
zjs_defer_emit_event(ble_handle->ble_obj, "stateChange", state,
sizeof(state), string_arg, zjs_release_args);
}
static ZJS_DECL_FUNC(zjs_ble_disconnect)
{
ZJS_VALIDATE_ARGS(Z_STRING);
jerry_size_t size = BT_ADDR_LE_STR_LEN;
char addr[size];
zjs_copy_jstring(argv[0], addr, &size);
ble_connection_t *conn = ble_handle->connections;
while (conn) {
char bt_addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn->bt_conn), bt_addr,
sizeof(bt_addr));
if (!strncmp(addr, bt_addr, BT_ADDR_LE_STR_LEN)) {
int error = bt_conn_disconnect(conn->bt_conn,
BT_HCI_ERR_REMOTE_USER_TERM_CONN);
if (error) {
return zjs_error("ble disconnect failed");
}
return ZJS_UNDEFINED;
}
conn = conn->next;
}
return zjs_error("ble connection not found");
}
const int ZJS_SUCCESS = 0;
const int ZJS_URL_TOO_LONG = 1;
const int ZJS_ALLOC_FAILED = 2;
const int ZJS_URL_SCHEME_ERROR = 3;
static int zjs_encode_url_frame(jerry_value_t url, u8_t **frame, int *size)
{
// requires: url is a URL string, frame points to a u8_t *, url contains
// only UTF-8 characters and hence no nil values
// effects: allocates a new buffer that will fit an Eddystone URL frame
// with a compressed version of the given url; returns it in
// *frame and returns the size of the frame in bytes in *size,
// and frame is then owned by the caller, to be freed later with
// zjs_free
// returns: 0 for success, 1 for URL too long, 2 for out of memory, 3 for
// invalid url scheme/syntax (only http:// or https:// allowed)
// FIXME: this needs unit tests; there could easily be a bug especially
// regarding whether a final null terminator on the URL fits in the 17
// bytes or not; spec seems unclear on whether it's required
// max len is https://www. and .info/ encoded in 2 bytes + 15 raw = 33
const int MAX_URL_LENGTH = 33;
jerry_size_t len = MAX_URL_LENGTH;
char buf[len];
zjs_copy_jstring(url, buf, &len);
if (!len)
return ZJS_URL_TOO_LONG;
// make sure it starts with http
int offset = 0;
if (strncmp(buf, "http", 4))
return ZJS_URL_SCHEME_ERROR;
offset += 4;
int scheme = 0;
if (buf[offset] == 's') {
scheme++;
offset++;
}
// make sure scheme http/https is followed by ://
if (strncmp(buf + offset, "://", 3))
return ZJS_URL_SCHEME_ERROR;
offset += 3;
if (strncmp(buf + offset, "www.", 4)) {
scheme += 2;
} else {
offset += 4;
}
// FIXME: skipping the compression of .com, .com/, .org, etc for now
len -= offset;
if (len > 17) // max URL length specified by Eddystone spec
return ZJS_URL_TOO_LONG;
u8_t *ptr = zjs_malloc(len + 5);
if (!ptr)
return ZJS_ALLOC_FAILED;
ptr[0] = 0xaa; // Eddystone UUID
ptr[1] = 0xfe; // Eddystone UUID
ptr[2] = 0x10; // Eddystone-URL frame type
ptr[3] = 0x00; // calibrated Tx power at 0m
ptr[4] = scheme; // encoded URL scheme prefix
strncpy(ptr + 5, buf + offset, len);
*size = len + 5;
*frame = ptr;
return ZJS_SUCCESS;
}
static ZJS_DECL_FUNC(zjs_ble_start_advertising)
{
// arg 0 should be the device name to advertise, e.g. "Arduino101"
// arg 1 should be an array of UUIDs (short, 4 hex chars)
// arg 2 should be a short URL (typically registered with Google, I think)
ZJS_VALIDATE_ARGS(Z_STRING, Z_ARRAY, Z_OPTIONAL Z_STRING);
jerry_value_t array = argv[1];
if (!jerry_value_is_array(array)) {
return zjs_error("expected array");
}
const int MAX_NAME_LENGTH = 80;
jerry_size_t size = MAX_NAME_LENGTH;
char name[size];
zjs_copy_jstring(argv[0], name, &size);
if (!size)
return zjs_error("name too long");
struct bt_data sd[] = {
BT_DATA(BT_DATA_NAME_COMPLETE, name, size),
};
/*
* Set Advertisement data. Based on the Eddystone specification:
* https://github.com/google/eddystone/blob/master/protocol-specification.md
* https://github.com/google/eddystone/tree/master/eddystone-url
*/
u8_t *url_frame = NULL;
int frame_size;
if (argc >= 3) {
if (zjs_encode_url_frame(argv[2], &url_frame, &frame_size)) {
DBG_PRINT("error encoding url frame, won't be advertised\n");
// TODO: Make use of error values and turn them into exceptions
}
}
u32_t arraylen = jerry_get_array_length(array);
int records = arraylen;
if (url_frame)
records += 2;
if (records == 0) {
return zjs_error("nothing to advertise");
}
const u8_t url_adv[] = { 0xaa, 0xfe };
struct bt_data ad[records];
int index = 0;
if (url_frame) {
ad[0].type = BT_DATA_UUID16_ALL;
ad[0].data_len = 2;
ad[0].data = url_adv;
ad[1].type = BT_DATA_SVC_DATA16;
ad[1].data_len = frame_size;
ad[1].data = url_frame;
index = 2;
}
for (int i = 0; i < arraylen; i++) {
ZVAL uuid = jerry_get_property_by_index(array, i);
if (!jerry_value_is_string(uuid)) {
return zjs_error("invalid uuid argument type");
}
const int MAX_UUID_LENGTH = 4;
jerry_size_t size = MAX_UUID_LENGTH + 1;
char ubuf[size];
zjs_copy_jstring(uuid, ubuf, &size);
if (size != MAX_UUID_LENGTH) {
ERR_PRINT("SIZE: %u\n", (unsigned int)size);
return zjs_error("unexpected uuid length");
}
u8_t bytes[2];
if (!zjs_hex_to_byte(ubuf + 2, &bytes[0]) ||
!zjs_hex_to_byte(ubuf, &bytes[1])) {
return zjs_error("invalid char in uuid");
}
ad[index].type = BT_DATA_UUID16_ALL;
ad[index].data_len = 2;
ad[index].data = bytes;
index++;
}
// FIXME: If we're really running this synchronously, why are we sending
// back the results asynchronously through hoops?
int err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad),
sd, ARRAY_SIZE(sd));
jerry_value_t error;
if (err) {
error = zjs_error("advertising failed");
jerry_value_clear_error_flag(&error);
} else {
error = jerry_create_null();
}
zjs_defer_emit_event(ble_handle->ble_obj, "advertisingStart", &error,
sizeof(jerry_value_t), zjs_copy_arg, zjs_release_args);
DBG_PRINT("BLE event: advertisingStart\n");
zjs_free(url_frame);
return ZJS_UNDEFINED;
}
static ZJS_DECL_FUNC(zjs_ble_stop_advertising)
{
DBG_PRINT("stopAdvertising has been called\n");
return ZJS_UNDEFINED;
}
static bool zjs_ble_parse_characteristic(ble_characteristic_t *chrc)
{
char uuid[ZJS_BLE_UUID_LEN];
if (!chrc || !chrc->chrc_obj)
return false;
jerry_value_t chrc_obj = chrc->chrc_obj;
if (!zjs_obj_get_string(chrc_obj, "uuid", uuid, ZJS_BLE_UUID_LEN)) {
ERR_PRINT("characteristic uuid doesn't exist\n");
return false;
}
chrc->uuid = zjs_ble_new_uuid_16(strtoul(uuid, NULL, 16));
ZVAL v_props = zjs_get_property(chrc_obj, "properties");
if (!jerry_value_is_array(v_props)) {
ERR_PRINT("properties is empty or not array\n");
return false;
}
for (int i = 0; i < jerry_get_array_length(v_props); i++) {
ZVAL v_property = jerry_get_property_by_index(v_props, i);
if (!jerry_value_is_string(v_property)) {
ERR_PRINT("property is not string\n");
return false;
}
const int MAX_NAME_LENGTH = 20;
jerry_size_t size = MAX_NAME_LENGTH;
char name[MAX_NAME_LENGTH];
zjs_copy_jstring(v_property, name, &size);
if (strequal(name, "read")) {
chrc->flags |= BT_GATT_CHRC_READ;
} else if (strequal(name, "write")) {
chrc->flags |= BT_GATT_CHRC_WRITE;
} else if (strequal(name, "notify")) {
chrc->flags |= BT_GATT_CHRC_NOTIFY;
}
}
ZVAL v_descs = zjs_get_property(chrc_obj, "descriptors");
if (!jerry_value_is_undefined(v_descs) &&
!jerry_value_is_null(v_descs) &&
!jerry_value_is_array(v_descs)) {
ERR_PRINT("descriptors is not array\n");
return false;
}
for (int i = 0; i < jerry_get_array_length(v_descs); i++) {
ZVAL v_desc = jerry_get_property_by_index(v_descs, i);
if (!jerry_value_is_object(v_desc)) {
ERR_PRINT("not valid descriptor object\n");
return false;
}
char desc_uuid[ZJS_BLE_UUID_LEN];
if (!zjs_obj_get_string(v_desc, "uuid", desc_uuid, ZJS_BLE_UUID_LEN)) {
ERR_PRINT("descriptor uuid doesn't exist\n");
return false;
}
unsigned long cud_uuid = (unsigned long)BT_UUID_16(BT_UUID_GATT_CUD)->val;
if (strtoul(desc_uuid, NULL, 16) == cud_uuid) {
// Support CUD only, ignore all other type of descriptors
ZVAL v_value = zjs_get_property(v_desc, "value");
if (jerry_value_is_string(v_value)) {
// transfer ownership, free cud_value later
chrc->cud_value = jerry_acquire_value(v_value);
}
}
}
ZVAL v_read = zjs_get_property(chrc_obj, "onReadRequest");
if (jerry_value_is_function(v_read)) {
chrc->read_cb.js_callback = jerry_acquire_value(v_read);
chrc->read_cb.id = zjs_add_c_callback(chrc, zjs_ble_read_c_callback);
} else {
chrc->read_cb.id = -1;
}
ZVAL v_write = zjs_get_property(chrc_obj, "onWriteRequest");
if (jerry_value_is_function(v_write)) {
chrc->write_cb.js_callback = jerry_acquire_value(v_write);
chrc->write_cb.id = zjs_add_c_callback(chrc, zjs_ble_write_c_callback);
} else {
chrc->write_cb.id = -1;
}
ZVAL v_sub = zjs_get_property(chrc_obj, "onSubscribe");
if (jerry_value_is_function(v_sub)) {
chrc->subscribe_cb.js_callback = jerry_acquire_value(v_sub);
chrc->subscribe_cb.id =
zjs_add_c_callback(chrc, zjs_ble_subscribe_c_callback);
} else {
chrc->subscribe_cb.id = -1;
}
ZVAL v_unsub = zjs_get_property(chrc_obj, "onUnsubscribe");
if (jerry_value_is_function(v_unsub)) {
chrc->unsubscribe_cb.js_callback = jerry_acquire_value(v_unsub);
chrc->unsubscribe_cb.id =
zjs_add_c_callback(chrc, zjs_ble_unsubscribe_c_callback);
} else {
chrc->unsubscribe_cb.id = -1;
}
ZVAL v_notify = zjs_get_property(chrc_obj, "onNotify");
if (jerry_value_is_function(v_notify)) {
chrc->notify_cb.js_callback = jerry_acquire_value(v_notify);
chrc->notify_cb.id =
zjs_add_c_callback(chrc, zjs_ble_notify_c_callback);
} else {
chrc->notify_cb.id = -1;
}
return true;
}
static bool zjs_ble_parse_service(ble_service_t *service)
{
char uuid[ZJS_BLE_UUID_LEN];
if (!service || !service->service_obj)
return false;
jerry_value_t service_obj = service->service_obj;
if (!zjs_obj_get_string(service_obj, "uuid", uuid, ZJS_BLE_UUID_LEN)) {
ERR_PRINT("service uuid doesn't exist\n");
return false;
}
service->uuid = zjs_ble_new_uuid_16(strtoul(uuid, NULL, 16));
ZVAL v_chrcs = zjs_get_property(service_obj, "characteristics");
if (!jerry_value_is_array(v_chrcs)) {
ERR_PRINT("characteristics is empty or not array\n");
return false;
}
char *errmsg = NULL;
ble_characteristic_t *previous = NULL;
for (int i = 0; i < jerry_get_array_length(v_chrcs); i++) {
// FIXME: it would make sense to move these next few lines into
// parse_characteristic
ZVAL v_chrc = jerry_get_property_by_index(v_chrcs, i);
if (!jerry_value_is_object(v_chrc)) {
errmsg = "characteristic is not object";
break;
}
ble_characteristic_t *chrc = zjs_malloc(sizeof(ble_characteristic_t));
if (!chrc) {
errmsg = "out of memory allocating ble_characteristic_t";
break;
}
memset(chrc, 0, sizeof(ble_characteristic_t));
chrc->read_cb.id = chrc->write_cb.id
= chrc->subscribe_cb.id
= chrc->unsubscribe_cb.id
= chrc->notify_cb.id
= -1;
// transfer ownership of v_chrc to chrc struct
chrc->chrc_obj = jerry_acquire_value(v_chrc);
jerry_set_object_native_pointer(chrc->chrc_obj, chrc, &ble_type_info);
// FIXME: All the stuff above this in the loop should move into
// parse_characteristic and it should take chrc_obj instead; it would
// reduce a lot of this error handling.
if (!zjs_ble_parse_characteristic(chrc)) {
jerry_release_value(chrc->chrc_obj);
zjs_free(chrc);
errmsg = "failed to parse temp characteristic";
break;
}
// append to the list
if (!service->characteristics) {
service->characteristics = chrc;
previous = chrc;
} else {
previous->next = chrc;
}
}
if (errmsg) {
ERR_PRINT("%s\n", errmsg);
return false;
}
return true;
}