@neurodevs/ndx-native 1.1.0 → 2.0.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.
@@ -1,9 +1,8 @@
1
- import { DataType, define, open } from 'ffi-rs'
1
+ import koffi from 'koffi'
2
2
 
3
3
  export default class LibndxAdapter implements Libndx {
4
4
  public static Class?: LibndxConstructor
5
- public static ffiRsOpen = open
6
- public static ffiRsDefine = define
5
+ public static koffiLoad: typeof koffi.load = koffi.load.bind(koffi)
7
6
 
8
7
  private static instance?: Libndx
9
8
 
@@ -35,75 +34,60 @@ export default class LibndxAdapter implements Libndx {
35
34
 
36
35
  private tryToLoadBindings() {
37
36
  try {
38
- this.openLibndx()
39
37
  this.defineBindings()
40
38
  } catch (err: unknown) {
41
39
  this.throwFailedToLoadLibndx(err as Error)
42
40
  }
43
41
  }
44
42
 
45
- private openLibndx() {
46
- this.ffiRsOpen({
47
- library: 'ndx',
48
- path: this.libndxPath,
49
- })
50
- }
51
-
52
43
  private defineBindings() {
53
- this.bindings = this.ffiRsDefine({
54
- create_ble_backend: {
55
- library: 'ndx',
56
- retType: DataType.String,
57
- paramsType: [DataType.String],
58
- },
59
- start_ble_backend: {
60
- library: 'ndx',
61
- retType: DataType.String,
62
- paramsType: [DataType.String],
63
- },
64
- stop_ble_backend: {
65
- library: 'ndx',
66
- retType: DataType.String,
67
- paramsType: [DataType.String],
68
- },
69
- get_rssi_ble_backend: {
70
- library: 'ndx',
71
- retType: DataType.String,
72
- paramsType: [DataType.String],
73
- },
74
- destroy_ble_backend: {
75
- library: 'ndx',
76
- retType: DataType.String,
77
- paramsType: [DataType.String],
78
- },
79
- create_ftdi_backend: {
80
- library: 'ndx',
81
- retType: DataType.String,
82
- paramsType: [DataType.String],
83
- },
84
- start_ftdi_backend: {
85
- library: 'ndx',
86
- retType: DataType.String,
87
- paramsType: [DataType.String],
88
- },
89
- stop_ftdi_backend: {
90
- library: 'ndx',
91
- retType: DataType.String,
92
- paramsType: [DataType.String],
93
- },
94
- destroy_ftdi_backend: {
95
- library: 'ndx',
96
- retType: DataType.String,
97
- paramsType: [DataType.String],
98
- },
99
- }) as LibndxBindings
44
+ const lib = LibndxAdapter.koffiLoad(this.libndxPath)
45
+
46
+ const wrap1 = (f: (a: string) => string) => (args: [string]) =>
47
+ f(args[0])
48
+
49
+ const wrap3 =
50
+ (f: (a: string, b: string, c: string) => string) =>
51
+ (args: [string, string, string]) =>
52
+ f(args[0], args[1], args[2])
53
+
54
+ this.bindings = {
55
+ create_ble_backend: wrap1(
56
+ lib.func('str create_ble_backend(str config)')
57
+ ),
58
+ start_ble_backend: wrap1(
59
+ lib.func('str start_ble_backend(str uuid)')
60
+ ),
61
+ write_ble_characteristic: wrap3(
62
+ lib.func(
63
+ 'str write_ble_characteristic(str uuid, str charUuid, str value)'
64
+ )
65
+ ),
66
+ read_ble_rssi: wrap1(lib.func('str read_ble_rssi(str uuid)')),
67
+ stop_ble_backend: wrap1(lib.func('str stop_ble_backend(str uuid)')),
68
+ destroy_ble_backend: wrap1(
69
+ lib.func('str destroy_ble_backend(str uuid)')
70
+ ),
71
+ create_ftdi_backend: wrap1(
72
+ lib.func('str create_ftdi_backend(str config)')
73
+ ),
74
+ start_ftdi_backend: wrap1(
75
+ lib.func('str start_ftdi_backend(str serial)')
76
+ ),
77
+ stop_ftdi_backend: wrap1(
78
+ lib.func('str stop_ftdi_backend(str serial)')
79
+ ),
80
+ destroy_ftdi_backend: wrap1(
81
+ lib.func('str destroy_ftdi_backend(str serial)')
82
+ ),
83
+ }
100
84
  }
101
85
 
102
86
  private throwFailedToLoadLibndx(err: Error) {
103
87
  throw new Error(
104
88
  `
105
89
  \n -----------------------------------
106
- \n Failed to load libndx! Tried to load from:
90
+ \n Failed to load libndx! Tried to load from:
107
91
  \n ${this.libndxPath}
108
92
  \n Instructions to save your day (on MacOS):
109
93
  \n 1. git clone https://github.com/neurodevs/libndx.git
@@ -111,7 +95,7 @@ export default class LibndxAdapter implements Libndx {
111
95
  \n 3. sudo cp build/libndx.dylib /opt/local/lib/
112
96
  \n 4. Try whatever you were doing again!
113
97
  \n Modify step 3 for your OS if you are not on MacOS.
114
- \n If you're still unsure, ask an LLM with this error and your OS.
98
+ \n If you're still unsure, ask an LLM with this error and your OS.
115
99
  \n You could also post an issue on the repo:
116
100
  \n https://github.com/neurodevs/ndx-native/issues
117
101
  \n Good luck!
@@ -134,6 +118,20 @@ export default class LibndxAdapter implements Libndx {
134
118
  return this.bindings.start_ble_backend([deviceUuid])
135
119
  }
136
120
 
121
+ public writeBleCharacteristic(options: BleWriteOptions) {
122
+ const { deviceUuid, characteristicUuid, value } = options
123
+ return this.bindings.write_ble_characteristic([
124
+ deviceUuid,
125
+ characteristicUuid,
126
+ value,
127
+ ])
128
+ }
129
+
130
+ public readBleRssi(options: BleBackendOptions) {
131
+ const { deviceUuid } = options
132
+ return this.bindings.read_ble_rssi([deviceUuid])
133
+ }
134
+
137
135
  public stopBleBackend(options: BleBackendOptions) {
138
136
  const { deviceUuid } = options
139
137
  return this.bindings.stop_ble_backend([deviceUuid])
@@ -144,11 +142,6 @@ export default class LibndxAdapter implements Libndx {
144
142
  return this.bindings.destroy_ble_backend([deviceUuid])
145
143
  }
146
144
 
147
- public getRssiBleBackend(options: BleBackendOptions) {
148
- const { deviceUuid } = options
149
- return this.bindings.get_rssi_ble_backend([deviceUuid])
150
- }
151
-
152
145
  public createFtdiBackend(options: FtdiBackendOptions) {
153
146
  const { serialNumber } = options
154
147
  const configJson = JSON.stringify({ serialNumber })
@@ -169,22 +162,15 @@ export default class LibndxAdapter implements Libndx {
169
162
  const { serialNumber } = options
170
163
  return this.bindings.destroy_ftdi_backend([serialNumber])
171
164
  }
172
-
173
- private get ffiRsOpen() {
174
- return LibndxAdapter.ffiRsOpen
175
- }
176
-
177
- private get ffiRsDefine() {
178
- return LibndxAdapter.ffiRsDefine
179
- }
180
165
  }
181
166
 
182
167
  export interface Libndx {
183
168
  createBleBackend(options: BleBackendOptions): string
184
169
  startBleBackend(options: BleBackendOptions): string
170
+ writeBleCharacteristic(options: BleWriteOptions): string
171
+ readBleRssi(options: BleBackendOptions): string
185
172
  stopBleBackend(options: BleBackendOptions): string
186
173
  destroyBleBackend(options: BleBackendOptions): string
187
- getRssiBleBackend(options: BleBackendOptions): string
188
174
  createFtdiBackend(options: FtdiBackendOptions): string
189
175
  startFtdiBackend(options: FtdiBackendOptions): string
190
176
  stopFtdiBackend(options: FtdiBackendOptions): string
@@ -201,6 +187,12 @@ export interface BleBackendOptions {
201
187
  deviceUuid: string
202
188
  }
203
189
 
190
+ export interface BleWriteOptions {
191
+ deviceUuid: string
192
+ characteristicUuid: string
193
+ value: string
194
+ }
195
+
204
196
  export interface FtdiBackendOptions {
205
197
  serialNumber: string
206
198
  }
@@ -208,9 +200,10 @@ export interface FtdiBackendOptions {
208
200
  export interface LibndxBindings {
209
201
  create_ble_backend(args: [string]): string
210
202
  start_ble_backend(args: [string]): string
203
+ write_ble_characteristic(args: [string, string, string]): string
204
+ read_ble_rssi(args: [string]): string
211
205
  stop_ble_backend(args: [string]): string
212
206
  destroy_ble_backend(args: [string]): string
213
- get_rssi_ble_backend(args: [string]): string
214
207
  create_ftdi_backend(args: [string]): string
215
208
  start_ftdi_backend(args: [string]): string
216
209
  stop_ftdi_backend(args: [string]): string
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  BleBackendOptions,
3
+ BleWriteOptions,
3
4
  FtdiBackendOptions,
4
5
  Libndx,
5
6
  LibndxAdapterOptions,
@@ -9,6 +10,7 @@ export default class FakeLibndx implements Libndx {
9
10
  public static callsToConstructor: (LibndxAdapterOptions | undefined)[] = []
10
11
  public static callsToCreateBleBackend: BleBackendOptions[] = []
11
12
  public static callsToStartBleBackend: BleBackendOptions[] = []
13
+ public static callsToWriteBle: BleWriteOptions[] = []
12
14
  public static callsToStopBleBacked: BleBackendOptions[] = []
13
15
  public static callsToDestroyBleBackend: BleBackendOptions[] = []
14
16
  public static callsToGetRssiBleBackend: BleBackendOptions[] = []
@@ -17,59 +19,67 @@ export default class FakeLibndx implements Libndx {
17
19
  public static callsToStopFtdiBackend: FtdiBackendOptions[] = []
18
20
  public static callsToDestroyFtdiBackend: FtdiBackendOptions[] = []
19
21
 
22
+ public static fakeResult = ''
23
+
20
24
  public constructor(options?: LibndxAdapterOptions) {
21
25
  FakeLibndx.callsToConstructor.push(options)
22
26
  }
23
27
 
24
28
  public createBleBackend(options: BleBackendOptions) {
25
29
  FakeLibndx.callsToCreateBleBackend.push(options)
26
- return ''
30
+ return FakeLibndx.fakeResult
27
31
  }
28
32
 
29
33
  public startBleBackend(options: BleBackendOptions) {
30
34
  FakeLibndx.callsToStartBleBackend.push(options)
31
- return ''
35
+ return FakeLibndx.fakeResult
36
+ }
37
+
38
+ public writeBleCharacteristic(options: BleWriteOptions) {
39
+ FakeLibndx.callsToWriteBle.push(options)
40
+ return FakeLibndx.fakeResult
41
+ }
42
+
43
+ public readBleRssi(options: BleBackendOptions) {
44
+ FakeLibndx.callsToGetRssiBleBackend.push(options)
45
+ return FakeLibndx.fakeResult
32
46
  }
33
47
 
34
48
  public stopBleBackend(options: BleBackendOptions) {
35
49
  FakeLibndx.callsToStopBleBacked.push(options)
36
- return ''
50
+ return FakeLibndx.fakeResult
37
51
  }
38
52
 
39
53
  public destroyBleBackend(options: BleBackendOptions) {
40
54
  FakeLibndx.callsToDestroyBleBackend.push(options)
41
- return ''
42
- }
43
-
44
- public getRssiBleBackend(options: BleBackendOptions) {
45
- FakeLibndx.callsToGetRssiBleBackend.push(options)
46
- return ''
55
+ return FakeLibndx.fakeResult
47
56
  }
48
57
 
49
58
  public createFtdiBackend(options: FtdiBackendOptions) {
50
59
  FakeLibndx.callsToCreateFtdiBackend.push(options)
51
- return ''
60
+ return FakeLibndx.fakeResult
52
61
  }
53
62
 
54
63
  public startFtdiBackend(options: FtdiBackendOptions) {
55
64
  FakeLibndx.callsToStartFtdiBackend.push(options)
56
- return ''
65
+ return FakeLibndx.fakeResult
57
66
  }
58
67
 
59
68
  public stopFtdiBackend(options: FtdiBackendOptions) {
60
69
  FakeLibndx.callsToStopFtdiBackend.push(options)
61
- return ''
70
+ return FakeLibndx.fakeResult
62
71
  }
63
72
 
64
73
  public destroyFtdiBackend(options: FtdiBackendOptions) {
65
74
  FakeLibndx.callsToDestroyFtdiBackend.push(options)
66
- return ''
75
+ return FakeLibndx.fakeResult
67
76
  }
68
77
 
69
78
  public static resetTestDouble() {
70
79
  FakeLibndx.callsToConstructor = []
71
80
  FakeLibndx.callsToCreateBleBackend = []
72
81
  FakeLibndx.callsToStartBleBackend = []
82
+ FakeLibndx.callsToWriteBle = []
73
83
  FakeLibndx.callsToStopBleBacked = []
74
84
  FakeLibndx.callsToGetRssiBleBackend = []
75
85
  FakeLibndx.callsToDestroyBleBackend = []
package/tsconfig.json CHANGED
@@ -20,5 +20,5 @@
20
20
  "resolveJsonModule": true,
21
21
  "experimentalDecorators": true
22
22
  },
23
- "include": ["src/*.ts", "src/**/*.ts", "./src/*.ts", "./src/**/*.ts"]
23
+ "include": ["./src/*.ts", "./src/**/*.ts", "src/*.ts", "src/**/*.ts"]
24
24
  }
@@ -1,7 +0,0 @@
1
- {
2
- "version": "0.9.4",
3
- "projectName": "ndx-native",
4
- "remote": {
5
- "url": "https://git.regressionproof.ai/liquidg3/ndx-native.git"
6
- }
7
- }
@@ -1,11 +0,0 @@
1
- {
2
- "scriptUpdater": {
3
- "skipped": []
4
- },
5
- "skipped": ["skill"],
6
- "installed": ["test"],
7
- "regressionproof": {
8
- "enabled": true,
9
- "projectName": "ndx-native"
10
- }
11
- }
@@ -1,11 +0,0 @@
1
- {
2
- "scriptUpdater": {
3
- "skipped": []
4
- },
5
- "skipped": ["skill"],
6
- "installed": ["test"],
7
- "regressionproof": {
8
- "enabled": true,
9
- "projectName": "ndx-native"
10
- }
11
- }