@neurodevs/ndx-native 7.0.1 → 7.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/__tests__/impl/LibndxAdapter.test.d.ts +25 -13
- package/build/__tests__/impl/LibndxAdapter.test.js +151 -54
- package/build/__tests__/impl/LibndxAdapter.test.js.map +1 -1
- package/build/impl/LibndxAdapter.d.ts +25 -10
- package/build/impl/LibndxAdapter.js +43 -10
- package/build/impl/LibndxAdapter.js.map +1 -1
- package/build/testDoubles/Libndx/FakeLibndx.d.ts +11 -7
- package/build/testDoubles/Libndx/FakeLibndx.js +24 -12
- package/build/testDoubles/Libndx/FakeLibndx.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/impl/LibndxAdapter.test.ts +196 -49
- package/src/impl/LibndxAdapter.ts +113 -20
- package/src/testDoubles/Libndx/FakeLibndx.ts +29 -13
|
@@ -13,6 +13,7 @@ export default class LibndxAdapter implements Libndx {
|
|
|
13
13
|
private static onDiscoveredProto?: ReturnType<typeof koffi.proto>
|
|
14
14
|
private static onConnectedProto?: ReturnType<typeof koffi.proto>
|
|
15
15
|
private static onRssiProto?: ReturnType<typeof koffi.proto>
|
|
16
|
+
private static onUsbDataProto?: ReturnType<typeof koffi.proto>
|
|
16
17
|
|
|
17
18
|
private static instance?: Libndx
|
|
18
19
|
|
|
@@ -49,6 +50,7 @@ export default class LibndxAdapter implements Libndx {
|
|
|
49
50
|
delete this.onDiscoveredProto
|
|
50
51
|
delete this.onConnectedProto
|
|
51
52
|
delete this.onRssiProto
|
|
53
|
+
delete this.onUsbDataProto
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
private tryToLoadBindings() {
|
|
@@ -64,11 +66,17 @@ export default class LibndxAdapter implements Libndx {
|
|
|
64
66
|
LibndxAdapter.getOnDiscoveredProto()
|
|
65
67
|
LibndxAdapter.getOnConnectedProto()
|
|
66
68
|
LibndxAdapter.getOnRssiProto()
|
|
69
|
+
LibndxAdapter.getOnUsbDataProto()
|
|
70
|
+
|
|
67
71
|
const lib = LibndxAdapter.koffiLoad(this.libndxPath)
|
|
68
72
|
|
|
69
73
|
const wrap1 = (f: (a: string) => string) => (args: [string]) =>
|
|
70
74
|
f(args[0])
|
|
71
75
|
|
|
76
|
+
const wrap2 =
|
|
77
|
+
(f: (a: string, b: string) => string) => (args: [string, string]) =>
|
|
78
|
+
f(args[0], args[1])
|
|
79
|
+
|
|
72
80
|
const wrap3 =
|
|
73
81
|
(f: (a: string, b: string, c: string) => string) =>
|
|
74
82
|
(args: [string, string, string]) =>
|
|
@@ -79,6 +87,11 @@ export default class LibndxAdapter implements Libndx {
|
|
|
79
87
|
(args: [string, unknown, unknown, number]) =>
|
|
80
88
|
f(args[0], args[1], args[2], args[3])
|
|
81
89
|
|
|
90
|
+
const wrapAddBleCharCallbacks =
|
|
91
|
+
(f: (a: string, b: unknown, c: number) => string) =>
|
|
92
|
+
(args: [string, unknown, number]) =>
|
|
93
|
+
f(args[0], args[1], args[2])
|
|
94
|
+
|
|
82
95
|
const wrapSetBleRssiInterval =
|
|
83
96
|
(f: (a: string, b: number, c: unknown) => string) =>
|
|
84
97
|
(args: [string, number, unknown]) =>
|
|
@@ -89,6 +102,11 @@ export default class LibndxAdapter implements Libndx {
|
|
|
89
102
|
(args: [string, unknown]) =>
|
|
90
103
|
f(args[0], args[1])
|
|
91
104
|
|
|
105
|
+
const wrapStartUsb =
|
|
106
|
+
(f: (a: string, b: unknown) => string) =>
|
|
107
|
+
(args: [string, unknown]) =>
|
|
108
|
+
f(args[0], args[1])
|
|
109
|
+
|
|
92
110
|
this.bindings = {
|
|
93
111
|
discover_ble_uuid: wrapDiscoverBleUuid(
|
|
94
112
|
lib.func(
|
|
@@ -103,6 +121,11 @@ export default class LibndxAdapter implements Libndx {
|
|
|
103
121
|
'str start_ble_backend(str uuid, OnConnectedFn *on_connected, CharCallback *callbacks, int num_callbacks)'
|
|
104
122
|
)
|
|
105
123
|
),
|
|
124
|
+
add_ble_char_callbacks: wrapAddBleCharCallbacks(
|
|
125
|
+
lib.func(
|
|
126
|
+
'str add_ble_char_callbacks(str uuid, CharCallback *callbacks, int num_callbacks)'
|
|
127
|
+
)
|
|
128
|
+
),
|
|
106
129
|
write_ble_characteristic: wrap3(
|
|
107
130
|
lib.func(
|
|
108
131
|
'str write_ble_characteristic(str uuid, str charUuid, str value)'
|
|
@@ -114,14 +137,19 @@ export default class LibndxAdapter implements Libndx {
|
|
|
114
137
|
)
|
|
115
138
|
),
|
|
116
139
|
stop_ble_backend: wrap1(lib.func('str stop_ble_backend(str uuid)')),
|
|
117
|
-
|
|
118
|
-
lib.func('str
|
|
140
|
+
create_usb_backend: wrap1(
|
|
141
|
+
lib.func('str create_usb_backend(str config)')
|
|
142
|
+
),
|
|
143
|
+
start_usb_backend: wrapStartUsb(
|
|
144
|
+
lib.func(
|
|
145
|
+
'str start_usb_backend(str serial, OnUsbDataFn *on_data)'
|
|
146
|
+
)
|
|
119
147
|
),
|
|
120
|
-
|
|
121
|
-
lib.func('str
|
|
148
|
+
write_usb_backend: wrap2(
|
|
149
|
+
lib.func('str write_usb_backend(str serial, str value)')
|
|
122
150
|
),
|
|
123
|
-
|
|
124
|
-
lib.func('str
|
|
151
|
+
stop_usb_backend: wrap1(
|
|
152
|
+
lib.func('str stop_usb_backend(str serial)')
|
|
125
153
|
),
|
|
126
154
|
}
|
|
127
155
|
}
|
|
@@ -204,6 +232,33 @@ export default class LibndxAdapter implements Libndx {
|
|
|
204
232
|
)
|
|
205
233
|
}
|
|
206
234
|
|
|
235
|
+
public addBleCharCallbacks(options: AddBleCharCallbacksOptions) {
|
|
236
|
+
const { deviceUuid, charCallbacks } = options
|
|
237
|
+
|
|
238
|
+
const registered = charCallbacks.map(
|
|
239
|
+
({ charUuid, charName, onData }) => ({
|
|
240
|
+
charUuid,
|
|
241
|
+
charName,
|
|
242
|
+
onData: LibndxAdapter.koffiRegister(
|
|
243
|
+
onData,
|
|
244
|
+
LibndxAdapter.koffiPointer(
|
|
245
|
+
LibndxAdapter.getCharCallbackProto()!
|
|
246
|
+
)
|
|
247
|
+
),
|
|
248
|
+
})
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
this.registeredCallbacks.push(...registered)
|
|
252
|
+
|
|
253
|
+
return JSON.parse(
|
|
254
|
+
this.bindings.add_ble_char_callbacks([
|
|
255
|
+
deviceUuid,
|
|
256
|
+
registered,
|
|
257
|
+
registered.length,
|
|
258
|
+
])
|
|
259
|
+
)
|
|
260
|
+
}
|
|
261
|
+
|
|
207
262
|
public writeBleCharacteristic(options: WriteBleCharacteristicOptions) {
|
|
208
263
|
const { deviceUuid, charUuid: characteristicUuid, value } = options
|
|
209
264
|
|
|
@@ -238,21 +293,37 @@ export default class LibndxAdapter implements Libndx {
|
|
|
238
293
|
return JSON.parse(this.bindings.stop_ble_backend([deviceUuid]))
|
|
239
294
|
}
|
|
240
295
|
|
|
241
|
-
public
|
|
296
|
+
public createUsbBackend(options: UsbBackendOptions) {
|
|
242
297
|
const { serialNumber } = options
|
|
243
298
|
const configJson = JSON.stringify({ serial_number: serialNumber })
|
|
244
299
|
|
|
245
|
-
return JSON.parse(this.bindings.
|
|
300
|
+
return JSON.parse(this.bindings.create_usb_backend([configJson]))
|
|
246
301
|
}
|
|
247
302
|
|
|
248
|
-
public
|
|
249
|
-
const { serialNumber } = options
|
|
250
|
-
|
|
303
|
+
public startUsbBackend(options: UsbBackendOptions) {
|
|
304
|
+
const { serialNumber, onData } = options
|
|
305
|
+
|
|
306
|
+
const registeredOnData = LibndxAdapter.koffiRegister(
|
|
307
|
+
onData ?? (() => {}),
|
|
308
|
+
LibndxAdapter.koffiPointer(LibndxAdapter.getOnUsbDataProto()!)
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
return JSON.parse(
|
|
312
|
+
this.bindings.start_usb_backend([serialNumber, registeredOnData])
|
|
313
|
+
)
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
public writeUsbBackend(options: WriteUsbBackendOptions) {
|
|
317
|
+
const { serialNumber, value } = options
|
|
318
|
+
|
|
319
|
+
return JSON.parse(
|
|
320
|
+
this.bindings.write_usb_backend([serialNumber, value])
|
|
321
|
+
)
|
|
251
322
|
}
|
|
252
323
|
|
|
253
|
-
public
|
|
324
|
+
public stopUsbBackend(options: UsbBackendOptions) {
|
|
254
325
|
const { serialNumber } = options
|
|
255
|
-
return JSON.parse(this.bindings.
|
|
326
|
+
return JSON.parse(this.bindings.stop_usb_backend([serialNumber]))
|
|
256
327
|
}
|
|
257
328
|
|
|
258
329
|
private static getCharCallbackProto() {
|
|
@@ -282,6 +353,15 @@ export default class LibndxAdapter implements Libndx {
|
|
|
282
353
|
return this.onRssiProto
|
|
283
354
|
}
|
|
284
355
|
|
|
356
|
+
private static getOnUsbDataProto() {
|
|
357
|
+
if (!this.onUsbDataProto) {
|
|
358
|
+
this.onUsbDataProto = LibndxAdapter.koffiProto(
|
|
359
|
+
'void OnUsbDataFn(uint8 *data, int length, double timestamp_sec)'
|
|
360
|
+
)
|
|
361
|
+
}
|
|
362
|
+
return this.onUsbDataProto
|
|
363
|
+
}
|
|
364
|
+
|
|
285
365
|
private static getOnDiscoveredProto() {
|
|
286
366
|
if (!this.onDiscoveredProto) {
|
|
287
367
|
this.onDiscoveredProto = LibndxAdapter.koffiProto(
|
|
@@ -311,12 +391,14 @@ export interface Libndx {
|
|
|
311
391
|
discoverBleUuid(options: DiscoverBleUuidOptions): NativeResult
|
|
312
392
|
createBleBackend(options: BleBackendOptions): NativeResult
|
|
313
393
|
startBleBackend(options: StartBleBackendOptions): NativeResult
|
|
394
|
+
addBleCharCallbacks(options: AddBleCharCallbacksOptions): NativeResult
|
|
314
395
|
writeBleCharacteristic(options: WriteBleCharacteristicOptions): NativeResult
|
|
315
396
|
setBleRssiInterval(options: BleRssiOptions): NativeResult
|
|
316
397
|
stopBleBackend(options: BleBackendOptions): NativeResult
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
398
|
+
createUsbBackend(options: UsbBackendOptions): NativeResult
|
|
399
|
+
startUsbBackend(options: UsbBackendOptions): NativeResult
|
|
400
|
+
writeUsbBackend(options: WriteUsbBackendOptions): NativeResult
|
|
401
|
+
stopUsbBackend(options: UsbBackendOptions): NativeResult
|
|
320
402
|
}
|
|
321
403
|
|
|
322
404
|
export type LibndxConstructor = new (options?: LibndxAdapterOptions) => Libndx
|
|
@@ -339,6 +421,10 @@ export interface StartBleBackendOptions extends BleBackendOptions {
|
|
|
339
421
|
charCallbacks: CharacteristicCallback[]
|
|
340
422
|
}
|
|
341
423
|
|
|
424
|
+
export interface AddBleCharCallbacksOptions extends BleBackendOptions {
|
|
425
|
+
charCallbacks: CharacteristicCallback[]
|
|
426
|
+
}
|
|
427
|
+
|
|
342
428
|
export interface BleRssiOptions extends BleBackendOptions {
|
|
343
429
|
intervalMs: number
|
|
344
430
|
onRssi: (rssi: number) => void
|
|
@@ -356,20 +442,27 @@ export interface CharacteristicCallback {
|
|
|
356
442
|
onData: (data: Buffer, length: number, timestampSec: number) => void
|
|
357
443
|
}
|
|
358
444
|
|
|
359
|
-
export interface
|
|
445
|
+
export interface UsbBackendOptions {
|
|
360
446
|
serialNumber: string
|
|
447
|
+
onData?: (data: Buffer, length: number, timestampSec: number) => void
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export interface WriteUsbBackendOptions extends UsbBackendOptions {
|
|
451
|
+
value: string
|
|
361
452
|
}
|
|
362
453
|
|
|
363
454
|
export interface LibndxBindings {
|
|
364
455
|
discover_ble_uuid(args: [string, unknown]): string
|
|
365
456
|
create_ble_backend(args: [string]): string
|
|
366
457
|
start_ble_backend(args: [string, unknown, unknown, number]): string
|
|
458
|
+
add_ble_char_callbacks(args: [string, unknown, number]): string
|
|
367
459
|
write_ble_characteristic(args: [string, string, string]): string
|
|
368
460
|
set_ble_rssi_interval(args: [string, number, unknown]): string
|
|
369
461
|
stop_ble_backend(args: [string]): string
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
462
|
+
create_usb_backend(args: [string]): string
|
|
463
|
+
start_usb_backend(args: [string, unknown]): string
|
|
464
|
+
write_usb_backend(args: [string, string]): string
|
|
465
|
+
stop_usb_backend(args: [string]): string
|
|
373
466
|
}
|
|
374
467
|
|
|
375
468
|
export interface NativePeripheral {
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
+
AddBleCharCallbacksOptions,
|
|
2
3
|
BleBackendOptions,
|
|
3
4
|
WriteBleCharacteristicOptions,
|
|
4
|
-
|
|
5
|
+
UsbBackendOptions,
|
|
6
|
+
WriteUsbBackendOptions,
|
|
5
7
|
Libndx,
|
|
6
8
|
LibndxAdapterOptions,
|
|
7
9
|
StartBleBackendOptions,
|
|
@@ -15,13 +17,15 @@ export default class FakeLibndx implements Libndx {
|
|
|
15
17
|
public static callsToDiscoverBleUuid: DiscoverBleUuidOptions[] = []
|
|
16
18
|
public static callsToCreateBleBackend: BleBackendOptions[] = []
|
|
17
19
|
public static callsToStartBleBackend: StartBleBackendOptions[] = []
|
|
20
|
+
public static callsToAddBleCharCallbacks: AddBleCharCallbacksOptions[] = []
|
|
18
21
|
public static callsToWriteBleCharacteristic: WriteBleCharacteristicOptions[] =
|
|
19
22
|
[]
|
|
20
23
|
public static callsToStopBleBackend: BleBackendOptions[] = []
|
|
21
24
|
public static callsToSetBleRssiInterval: BleRssiOptions[] = []
|
|
22
|
-
public static
|
|
23
|
-
public static
|
|
24
|
-
public static
|
|
25
|
+
public static callsToCreateUsbBackend: UsbBackendOptions[] = []
|
|
26
|
+
public static callsToStartUsbBackend: UsbBackendOptions[] = []
|
|
27
|
+
public static callsToWriteUsbBackend: WriteUsbBackendOptions[] = []
|
|
28
|
+
public static callsToStopUsbBackend: UsbBackendOptions[] = []
|
|
25
29
|
|
|
26
30
|
public static fakeResult: NativeResult = { status: 200 }
|
|
27
31
|
|
|
@@ -44,6 +48,11 @@ export default class FakeLibndx implements Libndx {
|
|
|
44
48
|
return FakeLibndx.fakeResult
|
|
45
49
|
}
|
|
46
50
|
|
|
51
|
+
public addBleCharCallbacks(options: AddBleCharCallbacksOptions) {
|
|
52
|
+
FakeLibndx.callsToAddBleCharCallbacks.push(options)
|
|
53
|
+
return FakeLibndx.fakeResult
|
|
54
|
+
}
|
|
55
|
+
|
|
47
56
|
public writeBleCharacteristic(options: WriteBleCharacteristicOptions) {
|
|
48
57
|
FakeLibndx.callsToWriteBleCharacteristic.push(options)
|
|
49
58
|
return FakeLibndx.fakeResult
|
|
@@ -59,18 +68,23 @@ export default class FakeLibndx implements Libndx {
|
|
|
59
68
|
return FakeLibndx.fakeResult
|
|
60
69
|
}
|
|
61
70
|
|
|
62
|
-
public
|
|
63
|
-
FakeLibndx.
|
|
71
|
+
public createUsbBackend(options: UsbBackendOptions) {
|
|
72
|
+
FakeLibndx.callsToCreateUsbBackend.push(options)
|
|
73
|
+
return FakeLibndx.fakeResult
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public startUsbBackend(options: UsbBackendOptions) {
|
|
77
|
+
FakeLibndx.callsToStartUsbBackend.push(options)
|
|
64
78
|
return FakeLibndx.fakeResult
|
|
65
79
|
}
|
|
66
80
|
|
|
67
|
-
public
|
|
68
|
-
FakeLibndx.
|
|
81
|
+
public writeUsbBackend(options: WriteUsbBackendOptions) {
|
|
82
|
+
FakeLibndx.callsToWriteUsbBackend.push(options)
|
|
69
83
|
return FakeLibndx.fakeResult
|
|
70
84
|
}
|
|
71
85
|
|
|
72
|
-
public
|
|
73
|
-
FakeLibndx.
|
|
86
|
+
public stopUsbBackend(options: UsbBackendOptions) {
|
|
87
|
+
FakeLibndx.callsToStopUsbBackend.push(options)
|
|
74
88
|
return FakeLibndx.fakeResult
|
|
75
89
|
}
|
|
76
90
|
|
|
@@ -79,11 +93,13 @@ export default class FakeLibndx implements Libndx {
|
|
|
79
93
|
FakeLibndx.callsToDiscoverBleUuid = []
|
|
80
94
|
FakeLibndx.callsToCreateBleBackend = []
|
|
81
95
|
FakeLibndx.callsToStartBleBackend = []
|
|
96
|
+
FakeLibndx.callsToAddBleCharCallbacks = []
|
|
82
97
|
FakeLibndx.callsToWriteBleCharacteristic = []
|
|
83
98
|
FakeLibndx.callsToStopBleBackend = []
|
|
84
99
|
FakeLibndx.callsToSetBleRssiInterval = []
|
|
85
|
-
FakeLibndx.
|
|
86
|
-
FakeLibndx.
|
|
87
|
-
FakeLibndx.
|
|
100
|
+
FakeLibndx.callsToCreateUsbBackend = []
|
|
101
|
+
FakeLibndx.callsToStartUsbBackend = []
|
|
102
|
+
FakeLibndx.callsToWriteUsbBackend = []
|
|
103
|
+
FakeLibndx.callsToStopUsbBackend = []
|
|
88
104
|
}
|
|
89
105
|
}
|