This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathu_ble_bond_test.c
579 lines (510 loc) · 20.9 KB
/
u_ble_bond_test.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
/*
* Copyright 2024 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Only #includes of u_* and the C standard library are allowed here,
* no platform stuff and no OS stuff. Anything required from
* the platform/OS must be brought in through u_port* to maintain
* portability.
*/
/** @file
* @brief Tests for the BLE bonding API: these should pass on all
* platforms where two UARTs are available.
* IMPORTANT: Please note that two modules are required for this test.
* This means that it can only run by default on a host which have the feature
* short_range_gen2 enabled. However it is also possible to run this test with
* modules which have the older version of u-connectXpress but it requires some
* trickery, se more below and in this case the test can only be executed on
* Windows or Linux.
* IMPORTANT: see notes in u_cfg_test_platform_specific.h for the
* naming rules that must be followed when using the U_PORT_TEST_FUNCTION()
* macro.
*/
#ifdef U_CFG_OVERRIDE
#include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stdint.h" // int32_t etc.
#include "stddef.h"
#include "stdbool.h"
#include "string.h"
#include "stdlib.h"
#ifdef __linux__
# include "unistd.h"
#endif
// Must always be included before u_short_range_test_selector.h
// lint -efile(766, u_ble_module_type.h)
#include "u_ble_module_type.h"
#include "u_short_range_test_selector.h"
#include "u_cfg_sw.h"
#include "u_cfg_app_platform_specific.h"
#include "u_cfg_test_platform_specific.h"
#include "u_error_common.h"
#include "u_port.h"
#include "u_port_debug.h"
#include "u_port_os.h"
#include "u_port_uart.h"
#include "u_test_util_resource_check.h"
#include "u_timeout.h"
#include "u_at_client.h"
#include "u_short_range_pbuf.h"
#include "u_short_range.h"
#include "u_short_range_cfg.h"
#include "u_short_range_private.h"
#include "u_network.h"
#include "u_network_config_ble.h"
#include "u_ble_cfg.h"
#include "u_ble.h"
#include "u_ble_gap.h"
#include "u_port_named_pipe.h"
#if U_SHORT_RANGE_TEST_BLE() && \
defined(U_CFG_TEST_SHORT_RANGE_MODULE_TYPE) && \
defined(U_CFG_APP_SHORT_RANGE_UART2)
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
/** The string to put at the start of all prints from this test.
*/
#define U_TEST_PREFIX "U_BLE_BOND_TEST"
/** Print a whole line, with terminator, prefixed for this test file.
*/
#define U_TEST_PRINT_LINE(format, ...) uPortLog("%s" format "\n", gTestPrefix, ##__VA_ARGS__)
/* Second module uart definitions */
#ifndef U_CFG_APP_PIN_SHORT_RANGE_TXD2
# define U_CFG_APP_PIN_SHORT_RANGE_TXD2 -1
#endif
#ifndef U_CFG_APP_PIN_SHORT_RANGE_RXD2
# define U_CFG_APP_PIN_SHORT_RANGE_RXD2 -1
#endif
#ifndef U_CFG_APP_PIN_SHORT_RANGE_CTS2
# define U_CFG_APP_PIN_SHORT_RANGE_CTS2 -1
#endif
#ifndef U_CFG_APP_PIN_SHORT_RANGE_RTS2
# define U_CFG_APP_PIN_SHORT_RANGE_RTS2 -1
#endif
#ifndef U_CFG_TEST_SHORT_RANGE_MODULE_TYPE2
# define U_CFG_TEST_SHORT_RANGE_MODULE_TYPE2 U_CFG_TEST_SHORT_RANGE_MODULE_TYPE
#endif
#define PIPE_NAME "ubx_ble_bond_test"
// Command handling when running in separate program instances
#define CMD_SETPARAM_FORM "%d,%d,%d,%d"
#define CMD_SETPARAM 0
#define CMD_RESP_MAC 1
#define CMD_INIT_MAC 2
#define CMD_ENTER_PASS 3
#define CMD_REM_BOND 4
#define CMD_EXIT 5
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
static uDeviceHandle_t gInitiatorDeviceHandle = NULL;
static uDeviceCfg_t gInitiatorDeviceCfg = {
.deviceType = U_DEVICE_TYPE_SHORT_RANGE,
.deviceCfg = {.cfgSho = {.moduleType = U_CFG_TEST_SHORT_RANGE_MODULE_TYPE}},
.transportType = U_DEVICE_TRANSPORT_TYPE_UART,
.transportCfg = {
.cfgUart = {
.uart = U_CFG_APP_SHORT_RANGE_UART,
.baudRate = U_SHORT_RANGE_UART_BAUD_RATE,
.pinTxd = U_CFG_APP_PIN_SHORT_RANGE_TXD,
.pinRxd = U_CFG_APP_PIN_SHORT_RANGE_RXD,
.pinCts = U_CFG_APP_PIN_SHORT_RANGE_CTS,
.pinRts = U_CFG_APP_PIN_SHORT_RANGE_RTS,
#ifdef U_CFG_APP_UART_PREFIX
.pPrefix = U_PORT_STRINGIFY_QUOTED(U_CFG_APP_UART_PREFIX) // Relevant for Linux only
#else
.pPrefix = NULL
#endif
}
}
};
static uDeviceHandle_t gResponderDeviceHandle = NULL;
static uDeviceCfg_t gResponderDeviceCfg = {
.deviceType = U_DEVICE_TYPE_SHORT_RANGE,
.deviceCfg = {.cfgSho = {.moduleType = U_CFG_TEST_SHORT_RANGE_MODULE_TYPE2}},
.transportType = U_DEVICE_TRANSPORT_TYPE_UART,
.transportCfg = {
.cfgUart = {
.uart = U_CFG_APP_SHORT_RANGE_UART2,
.baudRate = U_SHORT_RANGE_UART_BAUD_RATE,
.pinTxd = U_CFG_APP_PIN_SHORT_RANGE_TXD2,
.pinRxd = U_CFG_APP_PIN_SHORT_RANGE_RXD2,
.pinCts = U_CFG_APP_PIN_SHORT_RANGE_CTS2,
.pinRts = U_CFG_APP_PIN_SHORT_RANGE_RTS2,
#ifdef U_CFG_APP_UART_PREFIX
.pPrefix = U_PORT_STRINGIFY_QUOTED(U_CFG_APP_UART_PREFIX) // Relevant for Linux only
#else
.pPrefix = NULL
#endif
}
}
};
static uNetworkCfgBle_t gInitiatorNetworkCfg = {
.type = U_NETWORK_TYPE_BLE,
.role = U_BLE_CFG_ROLE_CENTRAL,
.spsServer = false,
};
static uNetworkCfgBle_t gResponderNetworkCfg = {
.type = U_NETWORK_TYPE_BLE,
.role = U_BLE_CFG_ROLE_PERIPHERAL,
.spsServer = false,
};
static uBleGapAdvConfig_t gAdvCfg = {.minIntervalMs = 200,
.maxIntervalMs = 200,
.connectable = true,
.maxClients = 1,
.pAdvData = NULL,
.advDataLength = 0,
.pRespData = NULL,
.respDataLength = 0
};
char gInitiatorMacAddr[U_SHORT_RANGE_BT_ADDRESS_SIZE];
char gResponderMacAddr[U_SHORT_RANGE_BT_ADDRESS_SIZE];
static uPortSemaphoreHandle_t gBondCompleteSemaphore = NULL;
static uPortSemaphoreHandle_t gSyncSemaphore = NULL;
static volatile int32_t gBondStatus;
static volatile int32_t gPasskey;
/** This test can be run in different ways.
* It requires two separate modules.
* The default option is to run both bonding initiator and responder
* in the same program. This can only be used when both the two modules
* have u-connectXpress second generation, i.e. short_range_gen2 is defined
* in UBXLIB_FEATURES.
* For testing earlier version of u-connectXpress this test has to be
* run in two different application instances, one acting as the initiator
* and one as the responder. This is handled here by spawning a new instance
* but with proper environment variables to ensure that only the responder
* variant of this test is run. The initiator will terminate the responder
* when the test has completed. The output from the responder is merged with
* that from the initiator.
*/
static int32_t gTestOption = 0;
static uPortNamePipeHandle_t gPipe = NULL;
static int32_t gResourceCountStart;
static char gTestPrefix[50];
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
// Write integer to a temporary string.
static const char *pIntToStr(uint32_t i)
{
static char result[10];
snprintf(result, sizeof(result), "%d", (int) i);
return result;
}
static void preamble()
{
uPortDeinit();
gResourceCountStart = uTestUtilGetDynamicResourceCount();
if (gBondCompleteSemaphore == NULL) {
U_PORT_TEST_ASSERT(uPortSemaphoreCreate(&gBondCompleteSemaphore, 0, 1) == 0);
}
if (gSyncSemaphore == NULL) {
U_PORT_TEST_ASSERT(uPortSemaphoreCreate(&gSyncSemaphore, 0, 1) == 0);
}
U_PORT_TEST_ASSERT(uPortInit() == 0);
U_PORT_TEST_ASSERT(uDeviceInit() == 0);
if (gTestOption != 0) {
U_PORT_TEST_ASSERT(uPortNamedPipeCreate(&gPipe, PIPE_NAME, gTestOption == 1) == 0);
}
if (gTestOption < 2) {
U_TEST_PRINT_LINE("initiating the bonding initiator module");
U_PORT_TEST_ASSERT(uDeviceOpen(&gInitiatorDeviceCfg, &gInitiatorDeviceHandle) == 0);
U_TEST_PRINT_LINE("initiating bonding initiator BLE");
U_PORT_TEST_ASSERT(uNetworkInterfaceUp(gInitiatorDeviceHandle, U_NETWORK_TYPE_BLE,
&gInitiatorNetworkCfg) == 0);
U_PORT_TEST_ASSERT(uBleGapRemoveBond(gInitiatorDeviceHandle, NULL) == 0);
uBleGapGetMac(gInitiatorDeviceHandle, gInitiatorMacAddr);
U_PORT_TEST_ASSERT(uBleGapSetPairable(gInitiatorDeviceHandle, true) == 0);
}
if (gTestOption != 1) {
U_TEST_PRINT_LINE("initiating the bonding responder module");
U_PORT_TEST_ASSERT(uDeviceOpen(&gResponderDeviceCfg, &gResponderDeviceHandle) == 0);
U_TEST_PRINT_LINE("initiating bonding responder BLE");
U_PORT_TEST_ASSERT(uNetworkInterfaceUp(gResponderDeviceHandle, U_NETWORK_TYPE_BLE,
&gResponderNetworkCfg) == 0);
// U_PORT_TEST_ASSERT(uBleGapReset(gResponderDeviceHandle) == 0);
U_PORT_TEST_ASSERT(uBleGapRemoveBond(gResponderDeviceHandle, NULL) == 0);
uint8_t advData[32];
gAdvCfg.pAdvData = advData;
int32_t res = uBleGapSetAdvData("BondResp", NULL, 0, advData, sizeof(advData));
U_PORT_TEST_ASSERT(res > 0);
gAdvCfg.advDataLength = (uint8_t)res;
U_PORT_TEST_ASSERT(uBleGapAdvertiseStart(gResponderDeviceHandle, &gAdvCfg) == 0);
U_PORT_TEST_ASSERT(uBleGapSetPairable(gResponderDeviceHandle, true) == 0);
uBleGapGetMac(gResponderDeviceHandle, gResponderMacAddr);
} else {
// Launch a new instance. By using popen the stdout of the initiator is inherited
// to the responder and hence all output is merged.
U_TEST_PRINT_LINE("launching responder application instance...");
putenv("U_CFG_APP_FILTER=bleBond");
putenv("U_CFG_TEST_BLE_BOND_OP=2");
putenv("U_CFG_TEST_SPAWNED=1");
FILE *procPipe = NULL;
#if defined(_WIN32)
char *exePath;
U_PORT_TEST_ASSERT(_get_pgmptr(&exePath) == 0);
procPipe = _popen(exePath, "w");
U_PORT_TEST_ASSERT(procPipe != NULL);
#elif defined(__linux__)
char exePath[100];
U_PORT_TEST_ASSERT(readlink("/proc/self/exe", exePath, sizeof(exePath)) > 0);
procPipe = popen(exePath, "w");
#endif
U_PORT_TEST_ASSERT(procPipe != NULL);
putenv("U_CFG_TEST_SPAWNED=");
// Wait for responder to start up.
char buff[20];
U_PORT_TEST_ASSERT(uPortNamedPipeReadStr(gPipe, buff, sizeof(buff)) > 0);
// Exchange mac addresses.
U_PORT_TEST_ASSERT(uPortNamedPipeWriteStr(gPipe, pIntToStr(CMD_RESP_MAC)) == 0);
U_PORT_TEST_ASSERT(uPortNamedPipeReadStr(gPipe, gResponderMacAddr, sizeof(gResponderMacAddr)) > 0);
snprintf(buff, sizeof(buff), "%d,%s", CMD_INIT_MAC, gInitiatorMacAddr);
U_PORT_TEST_ASSERT(uPortNamedPipeWriteStr(gPipe, buff) == 0);
}
}
static void postamble()
{
U_TEST_PRINT_LINE("closing down the modules");
if (gInitiatorDeviceHandle != NULL) {
U_PORT_TEST_ASSERT(uNetworkInterfaceDown(gInitiatorDeviceHandle, U_NETWORK_TYPE_BLE) == 0);
U_PORT_TEST_ASSERT(uDeviceClose(gInitiatorDeviceHandle, false) == 0);
}
if (gResponderDeviceHandle != NULL) {
U_PORT_TEST_ASSERT(uNetworkInterfaceDown(gResponderDeviceHandle, U_NETWORK_TYPE_BLE) == 0);
U_PORT_TEST_ASSERT(uDeviceClose(gResponderDeviceHandle, false) == 0);
}
if (gBondCompleteSemaphore != NULL) {
uPortSemaphoreDelete(gBondCompleteSemaphore);
gBondCompleteSemaphore = NULL;
}
if (gSyncSemaphore != NULL) {
uPortSemaphoreDelete(gSyncSemaphore);
gSyncSemaphore = NULL;
}
if (gTestOption == 1) {
// Close down the spawned responder.
char buff[10];
U_PORT_TEST_ASSERT(uPortNamedPipeWriteStr(gPipe, pIntToStr(CMD_EXIT)) == 0);
// Wait for confirmation on close.
U_PORT_TEST_ASSERT(uPortNamedPipeReadStr(gPipe, buff, sizeof(buff)) > 0);
// Wait a while, initiator final printout should come last and we can't
// sync as the named pipe must be removed before leakage check.
U_TEST_PRINT_LINE("waiting for responder to close.");
uPortTaskBlock(5000);
} else if (gTestOption == 2) {
// Send confirmation on close.
U_PORT_TEST_ASSERT(uPortNamedPipeWriteStr(gPipe, "Done") == 0);
}
if (gTestOption != 0) {
U_PORT_TEST_ASSERT(uPortNamedPipeDelete(gPipe) == 0);
}
uDeviceDeinit();
uPortDeinit();
uTestUtilResourceCheck(gTestPrefix, NULL, true);
int32_t resourceCount = gResourceCountStart - uTestUtilGetDynamicResourceCount();
U_TEST_PRINT_LINE("we have leaked %d resource(s).", resourceCount);
U_PORT_TEST_ASSERT(resourceCount <= 0);
}
/** Different handling for signaling bonding complete has to be applied for old and
* new u-connectXpress. In the old version the module is disconnected after bonding
* and we must wait for that before signaling in order to be able to do subsequent
* bonding operations. In the newer version the connection remains so we signal
* complete directly on the status callback.
*/
static void bondResultCallback(const char *pAddress, int32_t status)
{
gBondStatus = status;
#ifdef U_UCONNECT_GEN2
uPortSemaphoreGive(gBondCompleteSemaphore);
#endif
}
static void connectCallback(int32_t connHandle, char *pAddress, bool connected)
{
(void)connHandle;
(void)pAddress;
#ifndef U_UCONNECT_GEN2
if (!connected) {
uPortSemaphoreGive(gBondCompleteSemaphore);
}
#endif
}
static void doBond(bool expectSuccess)
{
uPortTaskBlock(1000);
// Remove old bonds
U_PORT_TEST_ASSERT(uBleGapRemoveBond(gInitiatorDeviceHandle, NULL) == 0);
if (gTestOption == 0) {
U_PORT_TEST_ASSERT(uBleGapRemoveBond(gResponderDeviceHandle, NULL) == 0);
} else {
U_PORT_TEST_ASSERT(uPortNamedPipeWriteStr(gPipe, pIntToStr(CMD_REM_BOND)) == 0);
}
uPortTaskBlock(1000);
// Start and wait for result callback
U_PORT_TEST_ASSERT(
(uBleGapBond(gInitiatorDeviceHandle, gResponderMacAddr, bondResultCallback) == 0) ||
!expectSuccess
);
U_PORT_TEST_ASSERT(uPortSemaphoreTryTake(gBondCompleteSemaphore, 10000) == 0);
if (expectSuccess) {
U_PORT_TEST_ASSERT(gBondStatus == U_BT_LE_BOND_ERR_SUCCESS);
} else {
U_PORT_TEST_ASSERT(gBondStatus != U_BT_LE_BOND_ERR_SUCCESS);
}
}
static uDeviceHandle_t oppositeHandle(const char *pAddress)
{
return (strstr(pAddress, gInitiatorMacAddr) != NULL) ?
gResponderDeviceHandle :
gInitiatorDeviceHandle;
}
static void confirmNumber(const char *pAddress, int32_t numericValue)
{
// Confirm the specified number
uBleGapBondConfirm(oppositeHandle(pAddress), true, pAddress);
}
static void passKeyEntryCallback(const char *pAddress, int32_t numericValue)
{
// Incoming passkey
gPasskey = numericValue;
if (gTestOption == 0) {
uPortSemaphoreGive(gSyncSemaphore);
} else {
// Send to remote
char buff[20];
snprintf(buff, sizeof(buff), "%d,%d", (int) CMD_ENTER_PASS, (int) gPasskey);
U_PORT_TEST_ASSERT(uPortNamedPipeWriteStr(gPipe, buff) == 0);
}
}
static void passkeyRequestCallback(const char *pAddress)
{
// Wait for and then send the passkey received from the counterpart
if (gTestOption != 2) {
U_PORT_TEST_ASSERT(uPortSemaphoreTryTake(gSyncSemaphore, 10000) == 0);
uBleGapBondEnterPasskey(oppositeHandle(pAddress), true, pAddress, gPasskey);
}
}
static void setParams(uDeviceHandle_t handle, int32_t cap, int32_t sec, bool pairable)
{
if (handle != NULL) {
U_PORT_TEST_ASSERT(uBleSetBondParameters(
handle,
cap,
sec,
confirmNumber,
passkeyRequestCallback,
passKeyEntryCallback
) == 0);
U_PORT_TEST_ASSERT(uBleGapSetPairable(handle, pairable) == 0);
} else if (gInitiatorDeviceHandle != NULL) {
char buff[20];
memset(buff, 0, sizeof(buff));
snprintf(buff, sizeof(buff) - 1, CMD_SETPARAM_FORM, CMD_SETPARAM, (int) cap,
(int) sec, (int) pairable);
U_PORT_TEST_ASSERT(uPortNamedPipeWriteStr(gPipe, buff) == 0);
}
}
static void runAsInitiator()
{
uBleGapSetConnectCallback(gInitiatorDeviceHandle, connectCallback);
// Test some of the combinations from the bonding matrix at:
// https://www.bluetooth.com/blog/bluetooth-pairing-part-2-key-generation-methods/
// -- No security --
// Pairing
U_TEST_PRINT_LINE("Pairing enabled only on one side, should fail");
setParams(gInitiatorDeviceHandle, U_BT_LE_IO_NONE, U_BT_LE_BOND_NO_SEC, true);
setParams(gResponderDeviceHandle, U_BT_LE_IO_NONE, U_BT_LE_BOND_NO_SEC, false);
doBond(false);
U_TEST_PRINT_LINE("Pairing enabled on both sides");
setParams(gResponderDeviceHandle, U_BT_LE_IO_NONE, U_BT_LE_BOND_NO_SEC, true);
doBond(true);
U_TEST_PRINT_LINE("Just works");
setParams(gInitiatorDeviceHandle, U_BT_LE_IO_NONE, U_BT_LE_BOND_UNAUTH, true);
setParams(gResponderDeviceHandle, U_BT_LE_IO_NONE, U_BT_LE_BOND_UNAUTH, true);
doBond(true);
// -- Security --
U_TEST_PRINT_LINE("Security, initiator DISP_ONLY, responder KEYB_ONLY");
setParams(gInitiatorDeviceHandle, U_BT_LE_IO_DISP_ONLY, U_BT_LE_BOND_AUTH, true);
setParams(gResponderDeviceHandle, U_BT_LE_IO_KEYB_ONLY, U_BT_LE_BOND_AUTH, true);
doBond(true);
U_TEST_PRINT_LINE("Security MITM protection, initiator YES_NO, responder YES_NO");
setParams(gInitiatorDeviceHandle, U_BT_LE_IO_DISP_YES_NO, U_BT_LE_BOND_AUTH_ENCR, true);
setParams(gResponderDeviceHandle, U_BT_LE_IO_DISP_YES_NO, U_BT_LE_BOND_AUTH_ENCR, true);
doBond(true);
}
static void runAsResponder()
{
char cmd[50];
U_PORT_TEST_ASSERT(uPortNamedPipeWriteStr(gPipe, "Ready") == 0);
while (uPortNamedPipeReadStr(gPipe, cmd, sizeof(cmd)) >= 0) {
if (strlen(cmd) > 0) {
// Incoming command
int32_t op;
U_PORT_TEST_ASSERT(sscanf(cmd, "%d", (int *) &op) == 1);
if (op == CMD_SETPARAM) {
int32_t cap, sec, pairable;
int cnt = sscanf(cmd, CMD_SETPARAM_FORM, (int *) &op, (int *) &cap,
(int *) &sec, (int *) &pairable);
U_PORT_TEST_ASSERT(cnt == 4);
setParams(gResponderDeviceHandle, cap, sec, pairable == 1);
} else if (op == CMD_RESP_MAC) {
U_PORT_TEST_ASSERT(uPortNamedPipeWriteStr(gPipe, gResponderMacAddr) == 0);
} else if (op == CMD_INIT_MAC) {
U_PORT_TEST_ASSERT(sscanf(cmd, "%d,%s", (int *) &op, gInitiatorMacAddr) == 2);
} else if (op == CMD_ENTER_PASS) {
sscanf(cmd + 2, "%d", (int *) &gPasskey);
uBleGapBondEnterPasskey(gResponderDeviceHandle, true, gInitiatorMacAddr, gPasskey);
} else if (op == CMD_REM_BOND) {
U_PORT_TEST_ASSERT(uBleGapRemoveBond(gResponderDeviceHandle, NULL) == 0);
} else {
// Exit or unknown command
break;
}
}
}
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
/** BLE bonding test.
*/
U_PORT_TEST_FUNCTION("[bleBond]", "bleBond")
{
const char *envOpt = getenv("U_CFG_TEST_BLE_BOND_OP");
if (envOpt != NULL) {
U_PORT_TEST_ASSERT(sscanf(envOpt, "%d", (int *) &gTestOption) == 1);
} else {
#ifndef U_UCONNECT_GEN2
// Older versions of u-connectXpress can only run in separate instances
gTestOption = 1;
#endif
}
if (gTestOption == 0) {
snprintf(gTestPrefix, sizeof(gTestPrefix) - 1, "%s: ", U_TEST_PREFIX);
} else {
snprintf(gTestPrefix, sizeof(gTestPrefix) - 1, "%s(%d): ", U_TEST_PREFIX,
(int) gTestOption);
}
preamble();
if (gInitiatorDeviceHandle != NULL) {
runAsInitiator();
} else {
runAsResponder();
}
postamble();
}
#endif
// End of file