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 100
/
u_ble_gap_extmod.c
672 lines (627 loc) · 26.5 KB
/
u_ble_gap_extmod.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
/*
* Copyright 2019-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 Implementation of the GAP API for BLE.
*/
#ifndef U_CFG_BLE_MODULE_INTERNAL
#ifdef U_CFG_OVERRIDE
#include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stdbool.h"
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdio.h" // snprintf()
#include "stdlib.h" // strol(), atoi(), strol(), strtof()
#include "string.h" // memset(), strncpy(), strtok_r(), strtol()
#include "u_error_common.h"
#include "u_timeout.h"
#include "u_at_client.h"
#include "u_ble.h"
#include "u_ble_cfg.h"
#include "u_ble_extmod_private.h"
#include "u_ble_gap.h"
#include "u_ble_private.h"
#include "u_cfg_sw.h"
#include "u_port.h"
#include "u_port_os.h"
#include "u_port_heap.h"
#include "u_short_range.h"
#include "u_short_range_module_type.h"
#include "u_short_range_pbuf.h"
#include "u_short_range_private.h"
#include "u_network.h"
#include "u_network_shared.h"
#include "u_network_config_ble.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
typedef enum { BLE_ROLE_ANY,
BLE_ROLE_CENTRAL,
BLE_ROLE_PERIPHERAL
} bleRoleCheck_t;
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
static uint32_t msToTick(uint32_t ms)
{
return ms * 1000 / 625;
}
static int32_t setUrcHandler(uAtClientHandle_t atHandle, const char *pPrefix,
void (*pHandler)(uAtClientHandle_t, void *), void *pHandlerParam)
{
// Remove possible existing URC and add new one if specified
int32_t errorCode = 0;
uAtClientRemoveUrcHandler(atHandle, pPrefix);
if (pHandler != NULL) {
errorCode = uAtClientSetUrcHandler(atHandle, pPrefix, pHandler, pHandlerParam);
}
return errorCode;
}
static bool validateBle(uShortRangePrivateInstance_t *pInstance, bleRoleCheck_t roleCheck)
{
// Validate that the device has BLE enabled, has the correct role and that
// sps server is not enabled
bool ok = false;
if (pInstance) {
uDeviceNetworkData_t *pNetworkData =
pUNetworkGetNetworkData(pInstance->devHandle, U_NETWORK_TYPE_BLE);
if (pNetworkData) {
uNetworkCfgBle_t *pCfgBle = (uNetworkCfgBle_t *)pNetworkData->pCfg;
ok = pCfgBle != NULL &&
!pCfgBle->spsServer &&
pInstance->atHandle != NULL;
if (ok && roleCheck != BLE_ROLE_ANY) {
int32_t currRole = uBlePrivateGetRole(pInstance->devHandle);
if (roleCheck == BLE_ROLE_CENTRAL) {
ok = currRole == U_BLE_CFG_ROLE_CENTRAL ||
currRole == U_BLE_CFG_ROLE_CENTRAL_AND_PERIPHERAL;
} else {
ok = currRole == U_BLE_CFG_ROLE_PERIPHERAL ||
currRole == U_BLE_CFG_ROLE_CENTRAL_AND_PERIPHERAL;
}
}
}
}
return ok;
}
static void connectUrc(uAtClientHandle_t atHandle, void *pParameter)
{
uBleGapConnectCallback_t cb = (uBleGapConnectCallback_t)pParameter;
char address[U_SHORT_RANGE_BT_ADDRESS_SIZE];
int32_t connHandle = uAtClientReadInt(atHandle);
(void)uAtClientReadInt(atHandle);
uAtClientReadString(atHandle, address, sizeof(address), false);
cb(connHandle, address, true);
}
static void disconnectUrc(uAtClientHandle_t atHandle, void *pParameter)
{
uBleGapConnectCallback_t cb = (uBleGapConnectCallback_t)pParameter;
int32_t connHandle = uAtClientReadInt(atHandle);
cb(connHandle, NULL, false);
}
// Use common connect urc callback using the provided application callback as parameter
static int32_t setConnectUrc(uAtClientHandle_t atHandle, uBleGapConnectCallback_t cb)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_SUCCESS;
if (cb) {
errorCode = setUrcHandler(atHandle, "+UUBTACLC:", connectUrc, cb);
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
errorCode = setUrcHandler(atHandle, "+UUBTACLD:", disconnectUrc, cb);
}
}
return errorCode;
}
static void phyUpdateUrc(uAtClientHandle_t atHandle, void *pParameter)
{
uBleGapPhyUpdateCallback_t cb = (uBleGapPhyUpdateCallback_t)pParameter;
int32_t connHandle = uAtClientReadInt(atHandle);
int32_t status = uAtClientReadInt(atHandle);
int32_t txPhy = uAtClientReadInt(atHandle);
int32_t rxPhy = uAtClientReadInt(atHandle);
cb(connHandle, status, txPhy, rxPhy);
}
static void bleBondCompleteUrc(uAtClientHandle_t atHandle, void *pParameter)
{
uBleGapBondCompleteCallback_t cb = (uBleGapBondCompleteCallback_t)pParameter;
char address[U_SHORT_RANGE_BT_ADDRESS_SIZE];
uAtClientReadString(atHandle, address, sizeof(address), false);
int32_t status = uAtClientReadInt(atHandle);
cb(address, status);
}
static void bleBondConfirmCUrc(uAtClientHandle_t atHandle, void *pParameter)
{
uBleGapBondConfirmCallback_t cb = (uBleGapBondConfirmCallback_t)pParameter;
char address[U_SHORT_RANGE_BT_ADDRESS_SIZE];
uAtClientReadString(atHandle, address, sizeof(address), false);
int32_t numVal = uAtClientReadInt(atHandle);
cb(address, numVal);
}
static void bleBondPassKeyRequestUrc(uAtClientHandle_t atHandle, void *pParameter)
{
uBleGapBondPasskeyRequestCallback_t cb = (uBleGapBondPasskeyRequestCallback_t)pParameter;
char address[U_SHORT_RANGE_BT_ADDRESS_SIZE];
uAtClientReadString(atHandle, address, sizeof(address), false);
cb(address);
}
static void bleBondPassKeyEnterUrc(uAtClientHandle_t atHandle, void *pParameter)
{
uBleGapBondPasskeyEntryCallback_t cb = (uBleGapBondPasskeyEntryCallback_t)pParameter;
char address[U_SHORT_RANGE_BT_ADDRESS_SIZE];
uAtClientReadString(atHandle, address, sizeof(address), false);
int32_t passKey = uAtClientReadInt(atHandle);
cb(address, passKey);
}
static bool setBleConfig(const uAtClientHandle_t atHandle, int32_t parameter, uint64_t value)
{
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTLECFG=");
uAtClientWriteInt(atHandle, parameter);
uAtClientWriteUint64(atHandle, value);
uAtClientCommandStopReadResponse(atHandle);
return uAtClientUnlock(atHandle) == 0;
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
int32_t uBleGapGetMac(uDeviceHandle_t devHandle, char *pMac)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
errorCode = (int32_t)U_BLE_ERROR_TEMPORARY_FAILURE;
if (pInstance != NULL &&
pInstance->atHandle != NULL) {
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UMLA=1");
uAtClientCommandStop(atHandle);
if (uAtClientResponseStart(atHandle, "+UMLA:") == 0) {
uAtClientReadString(atHandle,
pMac,
U_SHORT_RANGE_BT_ADDRESS_SIZE,
false);
uAtClientResponseStop(atHandle);
}
errorCode = uAtClientUnlock(atHandle);
}
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapSetPairable(uDeviceHandle_t devHandle, bool isPairable)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTPM=");
uAtClientWriteInt(atHandle, isPairable ? 2 : 1);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientUnlock(atHandle);
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleSetBondParameters(uDeviceHandle_t devHandle, int32_t ioCapabilities,
int32_t bondSecurity,
uBleGapBondConfirmCallback_t confirmCb,
uBleGapBondPasskeyRequestCallback_t passKeyRequestCb,
uBleGapBondPasskeyEntryCallback_t passKeyEntryCb)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
// Try to map the never UCX concept parameters to the old ones.
int32_t secMode = 2;
if (bondSecurity == U_BT_LE_BOND_NO_SEC) {
secMode = 1;
} else if (bondSecurity == U_BT_LE_BOND_UNAUTH) {
secMode = 2;
} else if (bondSecurity >= U_BT_LE_BOND_AUTH) {
if (ioCapabilities == U_BT_LE_IO_DISP_ONLY) {
secMode = 3;
} else if (ioCapabilities == U_BT_LE_IO_DISP_YES_NO) {
secMode = 4;
} else if (ioCapabilities == U_BT_LE_IO_KEYB_ONLY) {
secMode = 5;
}
}
uAtClientCommandStart(atHandle, "AT+UBTSM=");
uAtClientWriteInt(atHandle, secMode);
uAtClientWriteInt(atHandle, 0);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientUnlock(atHandle);
if (errorCode == 0) {
errorCode = setUrcHandler(atHandle, "+UUBTUC:", bleBondConfirmCUrc, confirmCb);
if (errorCode == 0) {
errorCode = setUrcHandler(atHandle, "+UUBTUPE:", bleBondPassKeyRequestUrc,
passKeyRequestCb);
}
if (errorCode == 0) {
errorCode = setUrcHandler(atHandle, "+UUBTUPD:", bleBondPassKeyEnterUrc,
passKeyEntryCb);
}
}
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapBond(uDeviceHandle_t devHandle, const char *pAddress,
uBleGapBondCompleteCallback_t cb)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == 0) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
errorCode = (int32_t)U_BLE_ERROR_INVALID_MODE;
if (validateBle(pInstance, BLE_ROLE_CENTRAL)) {
uAtClientHandle_t atHandle = pInstance->atHandle;
errorCode = setUrcHandler(atHandle, "+UUBTB:", bleBondCompleteUrc, cb);
if (errorCode == 0) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTB=");
uAtClientWriteString(atHandle, pAddress, false);
uAtClientWriteInt(atHandle, 1);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientUnlock(atHandle);
}
}
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapRemoveBond(uDeviceHandle_t devHandle, const char *pAddress)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == 0) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
if (pAddress == NULL) {
pAddress = "FFFFFFFFFFFF";
}
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientRemoveUrcHandler(atHandle, "+UUBTB:");
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTUB=");
uAtClientWriteString(atHandle, pAddress, false);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientUnlock(atHandle);
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapBondConfirm(uDeviceHandle_t devHandle, bool confirm, const char *pAddress)
{
// This will be called from URC callback so don't call uShortRangeLock here.
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
pInstance = pUShortRangePrivateGetInstance(devHandle);
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientCommandStart(atHandle, "AT+UBTUC=");
uAtClientWriteString(atHandle, pAddress, false);
uAtClientWriteInt(atHandle, confirm ? 1 : 0);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientErrorGet(atHandle);
return errorCode;
}
int32_t uBleGapBondEnterPasskey(uDeviceHandle_t devHandle, bool confirm, const char *pAddress,
int32_t passkey)
{
// Respond to passkey request.
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == 0) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTUPE=");
uAtClientWriteString(atHandle, pAddress, false);
uAtClientWriteInt(atHandle, confirm ? 1 : 0);
uAtClientWriteInt(atHandle, passkey);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientUnlock(atHandle);
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapSetConnectCallback(uDeviceHandle_t devHandle, uBleGapConnectCallback_t cb)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
if (validateBle(pInstance, BLE_ROLE_ANY)) {
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
errorCode = setConnectUrc(atHandle, cb);
uAtClientUnlock(atHandle);
} else {
errorCode = (int32_t)U_BLE_ERROR_INVALID_MODE;
}
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapScan(uDeviceHandle_t devHandle,
uBleGapDiscoveryType_t discType,
bool activeScan,
uint32_t timeousMs,
uBleGapScanCallback_t cb)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
errorCode = (int32_t)U_BLE_ERROR_INVALID_MODE;
if (validateBle(pInstance, BLE_ROLE_CENTRAL)) {
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
// Set the timeout for the AT response and thereby the scan timeout
uAtClientTimeoutSet(atHandle, timeousMs + 500);
// Start the scan
uAtClientCommandStart(atHandle, "AT+UBTD=");
uAtClientWriteInt(atHandle, discType);
uAtClientWriteInt(atHandle, activeScan ? 1 : 2);
uAtClientWriteInt(atHandle, timeousMs);
uAtClientCommandStop(atHandle);
// Get the responses synchronously
uBleScanResult_t result;
bool ok = true;
bool keepGoing = true;
while (keepGoing && uAtClientResponseStart(atHandle, "+UBTD:") == 0) {
errorCode = (int32_t)U_ERROR_COMMON_SUCCESS;
ok = uAtClientReadString(atHandle,
result.address,
sizeof(result.address),
false) == (sizeof(result.address) - 1);
result.rssi = uAtClientReadInt(atHandle);
ok = ok && uAtClientErrorGet(atHandle) == 0;
ok = ok && uAtClientReadString(atHandle,
result.name,
sizeof(result.name),
false) >= 0;
int32_t x = uAtClientReadInt(atHandle);
ok = ok && (x >= 0);
result.dataType = (uint8_t) x;
ok = ok && (result.dataType == 1 || result.dataType == 2);
if (ok) {
ok = false;
x = uAtClientReadHexData(atHandle, result.data, sizeof(result.data));
if (x >= 0) {
result.dataLength = (uint8_t) x;
ok = true;
}
}
if (ok && cb) {
keepGoing = cb(&result);
}
}
uAtClientResponseStop(atHandle);
uAtClientUnlock(atHandle);
}
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapSetConnectParams(uDeviceHandle_t devHandle, uBleGapConnectConfig_t *pConfig)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
errorCode = (int32_t)U_BLE_ERROR_INVALID_MODE;
if (validateBle(pInstance, U_BLE_CFG_ROLE_CENTRAL)) {
uAtClientHandle_t atHandle = pInstance->atHandle;
bool ok = setBleConfig(atHandle, 4, msToTick(pConfig->connIntervalMinMs));
ok = ok && setBleConfig(atHandle, 5, msToTick(pConfig->connIntervalMaxMs));
ok = ok && setBleConfig(atHandle, 6, pConfig->connLatency);
ok = ok && setBleConfig(atHandle, 7, msToTick(pConfig->linkLossTimeoutMs));
ok = ok && setBleConfig(atHandle, 8, msToTick(pConfig->connCreateTimeoutMs));
ok = ok && setBleConfig(atHandle, 9, msToTick(pConfig->scanIntervalMs));
ok = ok && setBleConfig(atHandle, 10, msToTick(pConfig->scanWindowMs));
ok = ok && setBleConfig(atHandle, 27, pConfig->preferredTxPhy);
ok = ok && setBleConfig(atHandle, 28, pConfig->preferredRxPhy);
errorCode = ok ? 0 : uAtClientErrorGet(atHandle);
}
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapConnect(uDeviceHandle_t devHandle, const char *pAddress)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
errorCode = (int32_t)U_BLE_ERROR_INVALID_MODE;
if (validateBle(pInstance, BLE_ROLE_CENTRAL)) {
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTACLC=");
uAtClientWriteString(atHandle, pAddress, false);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientUnlock(atHandle);
}
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapRequestPhyChange(uDeviceHandle_t devHandle, int32_t connHandle,
int32_t txPhy, int32_t rxPhy, uBleGapPhyUpdateCallback_t cb)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
errorCode = (int32_t)U_BLE_ERROR_INVALID_MODE;
if (validateBle(pInstance, U_BLE_CFG_ROLE_CENTRAL)) {
uAtClientHandle_t atHandle = pInstance->atHandle;
setUrcHandler(atHandle, "+UUBTLEPHYU:", phyUpdateUrc, cb);
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTLEPHYR=");
uAtClientWriteInt(atHandle, connHandle);
uAtClientWriteInt(atHandle, txPhy);
uAtClientWriteInt(atHandle, rxPhy);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientUnlock(atHandle);
}
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapDisconnect(uDeviceHandle_t devHandle, int32_t connHandle)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
errorCode = (int32_t)U_BLE_ERROR_INVALID_MODE;
if (validateBle(pInstance, BLE_ROLE_ANY)) {
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTACLD=");
uAtClientWriteInt(atHandle, connHandle);
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientErrorGet(atHandle);
uAtClientUnlock(atHandle);
}
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapSetAdvData(const char *pName,
const uint8_t *pManufData, uint8_t manufDataSize,
uint8_t *pAdvData, uint8_t advDataSize)
{
if (!pName && !pManufData) {
return (int32_t)U_ERROR_COMMON_INVALID_PARAMETER;
}
uint8_t nameSize = pName ? (uint8_t)(strlen(pName) + 2) : 0; /* string length, tag and the string */
manufDataSize = pManufData ? manufDataSize + 2 : 0;
int32_t totSize = nameSize + manufDataSize;
if (totSize > advDataSize) {
return (int32_t)U_ERROR_COMMON_INVALID_PARAMETER;
}
if (pName) {
nameSize--;
*(pAdvData++) = nameSize--;
*(pAdvData++) = U_BT_DATA_NAME_COMPLETE;
memcpy(pAdvData, pName, nameSize);
pAdvData += nameSize;
}
if (pManufData) {
manufDataSize--;
*(pAdvData++) = manufDataSize--;
*(pAdvData++) = U_BT_DATA_MANUFACTURER_DATA;
memcpy(pAdvData, pManufData, manufDataSize);
}
return totSize;
}
int32_t uBleGapAdvertiseStart(uDeviceHandle_t devHandle, const uBleGapAdvConfig_t *pConfig)
{
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
errorCode = (int32_t)U_BLE_ERROR_INVALID_MODE;
if (validateBle(pInstance, BLE_ROLE_PERIPHERAL)) {
uAtClientHandle_t atHandle = pInstance->atHandle;
bool ok;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTCM=");
uAtClientWriteInt(atHandle, pConfig->connectable ? 2 : 1);
uAtClientCommandStopReadResponse(atHandle);
ok = uAtClientUnlock(atHandle) == 0;
ok = ok && setBleConfig(atHandle, 1, msToTick(pConfig->minIntervalMs));
ok = ok && setBleConfig(atHandle, 2, msToTick(pConfig->maxIntervalMs));
if (ok) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTCFG=");
uAtClientWriteInt(atHandle, 2);
uAtClientWriteUint64(atHandle, msToTick(pConfig->maxClients));
uAtClientCommandStopReadResponse(atHandle);
ok = uAtClientUnlock(atHandle) == 0;
}
if (ok && pConfig->pAdvData) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTAD=");
uAtClientWriteHexData(atHandle, pConfig->pAdvData, pConfig->advDataLength);
uAtClientCommandStopReadResponse(atHandle);
ok = uAtClientUnlock(atHandle) == 0;
}
if (ok && pConfig->pRespData) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UBTSD=");
uAtClientWriteHexData(atHandle, pConfig->pRespData, pConfig->respDataLength);
uAtClientCommandStopReadResponse(atHandle);
ok = uAtClientUnlock(atHandle) == 0;
}
errorCode = ok ? 0 : uAtClientErrorGet(atHandle);
}
uShortRangeUnlock();
}
return errorCode;
}
int32_t uBleGapAdvertiseStop(uDeviceHandle_t devHandle)
{
// Not implemented for now due to currently not available in u-connect
(void)devHandle;
return U_ERROR_COMMON_NOT_IMPLEMENTED;
}
int32_t uBleGapReset(uDeviceHandle_t devHandle)
{
// Restart the module to restore device to default settings
int32_t errorCode = (int32_t)U_ERROR_COMMON_NOT_INITIALISED;
uShortRangePrivateInstance_t *pInstance;
if (uShortRangeLock() == (int32_t)U_ERROR_COMMON_SUCCESS) {
pInstance = pUShortRangePrivateGetInstance(devHandle);
errorCode = (int32_t)U_BLE_ERROR_INVALID_MODE;
if (validateBle(pInstance, BLE_ROLE_ANY)) {
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+UFACTORY");
uAtClientCommandStopReadResponse(atHandle);
uAtClientCommandStart(atHandle, "AT+CPWROFF");
uAtClientCommandStopReadResponse(atHandle);
errorCode = uAtClientUnlock(atHandle);
// Wait for restart to complete
if (errorCode == (int32_t)U_ERROR_COMMON_SUCCESS) {
uPortTaskBlock(5000);
uAtClientFlush(atHandle);
}
}
uShortRangeUnlock();
}
return errorCode;
}
#endif