-
Notifications
You must be signed in to change notification settings - Fork 41
/
serverClient.test.js
425 lines (361 loc) · 12.9 KB
/
serverClient.test.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
/* Copyright (c) 2010 - 2017, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Use in source and binary forms, redistribution in binary form only, with
* or without modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 2. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 3. This software, with or without modification, must only be used with a Nordic
* Semiconductor ASA integrated circuit.
*
* 4. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
'use strict';
const { grabAdapter, releaseAdapter, setupAdapter, outcome } = require('./setup');
const api = require('../index');
const debug = require('debug')('ble-driver:test:mtu');
const serviceFactory = new api.ServiceFactory();
const PERIPHERAL_DEVICE_ADDRESS = 'FF:11:22:33:AA:CE';
const PERIPHERAL_DEVICE_ADDRESS_TYPE = 'BLE_GAP_ADDR_TYPE_RANDOM_STATIC';
const CENTRAL_DEVICE_ADDRESS = 'FF:11:22:33:AA:CF';
const CENTRAL_DEVICE_ADDRESS_TYPE = 'BLE_GAP_ADDR_TYPE_RANDOM_STATIC';
const SERVICE_0_UUID = 'F000F000AAAAAAAAAAAAAAAAAAAAAAAA';
const SERVICE_1_UUID = 'F001F000AAAAAAAAAAAAAAAAAAAAAAAA';
const CHAR_1_1_UUID = 'F001F001AAAAAAAAAAAAAAAAAAAAAAAA';
const CHAR_1_2_UUID = 'F001F002AAAAAAAAAAAAAAAAAAAAAAAA';
const SERVICE_2_UUID = 'F002F000AAAAAAAAAAAAAAAAAAAAAAAA';
const CHAR_2_1_UUID = 'F002F001AAAAAAAAAAAAAAAAAAAAAAAA';
const CHAR_2_2_UUID = 'F002F002AAAAAAAAAAAAAAAAAAAAAAAA';
const CCCD_UUID = '2902';
let service0uuid;
let service1uuid;
let char11uuid;
let char12uuid;
let service2uuid;
let char21uuid;
let char22uuid;
const serialNumberA = process.env.DEVICE_A_SERIAL_NUMBER;
if (!serialNumberA) {
console.log('Missing env DEVICE_A_SERIAL_NUMBER=<SN e.g. from nrf-device-lister>');
process.exit(1);
}
const serialNumberB = process.env.DEVICE_B_SERIAL_NUMBER;
if (!serialNumberA) {
console.log('Missing env DEVICE_B_SERIAL_NUMBER=<SN e.g. from nrf-device-lister>');
process.exit(1);
}
function connect(adapter, connectToAddress) {
return new Promise((resolve, reject) => {
const options = {
scanParams: {
active: false,
interval: 100,
window: 50,
timeout: 20,
},
connParams: {
min_conn_interval: 7.5,
max_conn_interval: 7.5,
slave_latency: 0,
conn_sup_timeout: 4000,
},
};
adapter.connect(
connectToAddress,
options,
connectErr => {
if (connectErr) {
return reject(connectErr);
}
return resolve();
});
});
}
function disconnect(adapter, device) {
return new Promise((resolve, reject) => {
adapter.disconnect(device.instanceId, disconnectErr => {
if (disconnectErr) {
reject(disconnectErr);
return;
}
debug(`Initiated disconnect from ${device.address}/${device.addressType}.`);
resolve();
});
});
}
function characteristicsInit(service0, service1, service2) {
const charValue = [0x0b, 0x0a];
const char_1_1 = serviceFactory.createCharacteristic(
service1,
CHAR_1_1_UUID,
charValue,
{
broadcast: false,
read: false,
write: false,
writeWoResp: false,
reliableWrite: false,
notify: true,
indicate: false,
},
{
maxLength: charValue.length,
readPerm: ['open'],
writePerm: ['open'],
});
serviceFactory.createDescriptor(
char_1_1,
CCCD_UUID,
[0, 0],
{
maxLength: 2,
readPerm: ['open'],
writePerm: ['open'],
variableLength: false,
});
serviceFactory.createCharacteristic(
service1,
CHAR_1_2_UUID,
charValue,
{
broadcast: false,
read: true,
write: false,
writeWoResp: false,
reliableWrite: false,
notify: false,
indicate: false,
},
{
maxLength: charValue.length,
readPerm: ['open'],
writePerm: ['open'],
});
const char_2_1 = serviceFactory.createCharacteristic(
service2,
CHAR_2_1_UUID,
charValue,
{
broadcast: false,
read: false,
write: false,
writeWoResp: false,
reliableWrite: false,
notify: true,
indicate: false,
},
{
maxLength: charValue.length,
readPerm: ['open'],
writePerm: ['open'],
});
serviceFactory.createDescriptor(
char_2_1,
CCCD_UUID,
[0, 0],
{
maxLength: 2,
readPerm: ['open'],
writePerm: ['open'],
variableLength: false,
});
serviceFactory.createCharacteristic(
service2,
CHAR_2_2_UUID,
charValue,
{
broadcast: false,
read: true,
write: false,
writeWoResp: false,
reliableWrite: false,
notify: false,
indicate: false,
},
{
maxLength: charValue.length,
readPerm: ['open'],
writePerm: ['open'],
});
}
function servicesInit(adapter) {
return new Promise((resolve, reject) => {
debug('Adding services');
const service0 = serviceFactory.createService(SERVICE_0_UUID);
const service1 = serviceFactory.createService(SERVICE_1_UUID);
const service2 = serviceFactory.createService(SERVICE_2_UUID);
characteristicsInit(service0, service1, service2);
adapter.setServices([service0, service1, service2], err => {
if (err) {
return reject(new Error(`Error initializing services: ${JSON.stringify(err, null, 1)}'.`));
}
return resolve();
});
});
}
async function startAdvertising(adapter) {
await new Promise((resolve, reject) =>
adapter.setAdvertisingData(
{
txPowerLevel: 20,
},
{}, // scan response data
advDataErr => {
if (advDataErr) {
reject(advDataErr);
return;
}
resolve();
}));
await new Promise((resolve, reject) => {
adapter.startAdvertising(
{
interval: 100,
timeout: 100,
},
startAdvErr => {
if (startAdvErr) {
reject(startAdvErr);
return;
}
resolve();
});
});
}
async function onConnected(adapter, peerDevice) {
const services = await new Promise((resolve, reject) => {
adapter.getServices(peerDevice.instanceId, (getServicesErr, readServices) => {
if (getServicesErr) {
reject(getServicesErr);
return;
}
resolve(readServices);
});
});
if (!services || services.length < 4) {
throw new Error('Invalid services input');
}
service0uuid = services[2].uuid;
service1uuid = services[3].uuid;
service2uuid = services[4].uuid;
const service1Id = services[3].instanceId;
const service2Id = services[4].instanceId;
const service1chars = await new Promise((resolve, reject) => {
adapter.getCharacteristics(service1Id, (getCharacteristicsErr, chrs) => {
if (getCharacteristicsErr) {
reject(getCharacteristicsErr);
return;
}
resolve(chrs);
});
});
if (!service1chars || service1chars.length < 2) {
throw new Error('Invalid characteristics input');
}
const service2chars = await new Promise((resolve, reject) => {
adapter.getCharacteristics(service2Id, (getCharacteristicsErr, chrs) => {
if (getCharacteristicsErr) {
reject(getCharacteristicsErr);
return;
}
resolve(chrs);
});
});
if (!service2chars || service2chars.length < 2) {
throw new Error('Invalid characteristics input');
}
char11uuid = service1chars[0].uuid;
char12uuid = service1chars[1].uuid;
char21uuid = service2chars[0].uuid;
char22uuid = service2chars[1].uuid;
/*
const charId = characteristics[0].instanceId;
const characteristic = await new Promise((resolve, reject) => {
adapter.readCharacteristicValue(charId, readCharacteristicValueErr => {
if (readCharacteristicValueErr) {
reject(readCharacteristicValueErr);
return;
}
resolve(characteristics[0]);
});
});
if (!characteristic || !characteristic.instanceId) {
throw new Error('Invalid characteristic input');
} */
}
describe('the API', () => {
let centralAdapter;
let peripheralAdapter;
beforeAll(async () => {
centralAdapter = await grabAdapter(serialNumberA);
peripheralAdapter = await grabAdapter(serialNumberB);
await Promise.all([
setupAdapter(centralAdapter, '#CENTRAL', 'central', CENTRAL_DEVICE_ADDRESS, CENTRAL_DEVICE_ADDRESS_TYPE),
setupAdapter(peripheralAdapter, '#PERIPH', 'peripheral', PERIPHERAL_DEVICE_ADDRESS, PERIPHERAL_DEVICE_ADDRESS_TYPE)]);
});
afterAll(async () => {
await Promise.all([
releaseAdapter(centralAdapter.state.serialNumber),
releaseAdapter(peripheralAdapter.state.serialNumber)]);
});
it('shall support discovering all services and characteristics correctly', async () => {
expect(centralAdapter).toBeDefined();
expect(peripheralAdapter).toBeDefined();
if (centralAdapter.driver.NRF_SD_BLE_API_VERSION === 2 || peripheralAdapter.driver.NRF_SD_BLE_API_VERSION === 2) {
debug('SoftDevice V2 does not support changing L2CAP packet length or MTU');
return;
}
await servicesInit(peripheralAdapter);
await startAdvertising(peripheralAdapter);
const deviceConnectedCentral = new Promise((resolve, reject) => {
centralAdapter.once('deviceConnected', peripheralDevice => {
onConnected(centralAdapter, peripheralDevice).then(() => {
resolve({
address: peripheralDevice.address,
type: peripheralDevice.addressType,
instanceId: peripheralDevice.instanceId,
});
}).catch(reject);
});
});
await connect(centralAdapter, { address: PERIPHERAL_DEVICE_ADDRESS, type: PERIPHERAL_DEVICE_ADDRESS_TYPE });
const [
deviceConnectedCentralResult,
] = await outcome([
deviceConnectedCentral,
], 10000);
expect(deviceConnectedCentralResult.address).toBe(PERIPHERAL_DEVICE_ADDRESS);
expect(deviceConnectedCentralResult.type).toBe(PERIPHERAL_DEVICE_ADDRESS_TYPE);
expect(service0uuid).toBe(SERVICE_0_UUID);
expect(service1uuid).toBe(SERVICE_1_UUID);
expect(service2uuid).toBe(SERVICE_2_UUID);
expect(char11uuid).toBe(CHAR_1_1_UUID);
expect(char12uuid).toBe(CHAR_1_2_UUID);
expect(char21uuid).toBe(CHAR_2_1_UUID);
expect(char22uuid).toBe(CHAR_2_2_UUID);
await disconnect(centralAdapter, deviceConnectedCentralResult);
});
});