/
Gap.h
1920 lines (1763 loc) · 71.5 KB
/
Gap.h
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* 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.
*/
#ifndef __GAP_H__
#define __GAP_H__
#include "BLETypes.h"
#include "ble/BLEProtocol.h"
#include "GapAdvertisingData.h"
#include "GapAdvertisingParams.h"
#include "GapScanningParams.h"
#include "GapEvents.h"
#include "CallChainOfFunctionPointersWithContext.h"
#include "FunctionPointerWithContext.h"
#include "deprecate.h"
/* Forward declarations for classes that will only be used for pointers or references in the following. */
class GapAdvertisingParams;
class GapScanningParams;
class GapAdvertisingData;
class Gap {
/*
* DEPRECATION ALERT: all of the APIs in this `public` block are deprecated.
* They have been relocated to the class BLEProtocol.
*/
public:
/**
* Address-type for BLEProtocol addresses.
*
* @deprecated Use BLEProtocol::AddressType_t instead.
*/
typedef BLEProtocol::AddressType_t AddressType_t;
/**
* Address-type for BLEProtocol addresses.
*
* @deprecated Use BLEProtocol::AddressType_t instead.
*/
typedef BLEProtocol::AddressType_t addr_type_t;
/**
* Address-type for BLEProtocol addresses.
*
* @deprecated Use BLEProtocol::AddressType_t instead. The following
* constants have been left in their deprecated state to
* transparently support existing applications which may have
* used Gap::ADDR_TYPE_*.
*/
enum DeprecatedAddressType_t {
ADDR_TYPE_PUBLIC = BLEProtocol::AddressType::PUBLIC,
ADDR_TYPE_RANDOM_STATIC = BLEProtocol::AddressType::RANDOM_STATIC,
ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE = BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE,
ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE = BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE
};
/**
* Length (in octets) of the BLE MAC address.
*/
static const unsigned ADDR_LEN = BLEProtocol::ADDR_LEN;
/**
* 48-bit address, LSB format.
*
* @deprecated Use BLEProtocol::AddressBytes_t instead.
*/
typedef BLEProtocol::AddressBytes_t Address_t;
/**
* 48-bit address, LSB format.
*
* @deprecated Use BLEProtocol::AddressBytes_t instead.
*/
typedef BLEProtocol::AddressBytes_t address_t;
public:
/**
* Enumeration for timeout sources.
*/
enum TimeoutSource_t {
TIMEOUT_SRC_ADVERTISING = 0x00, /**< Advertising timeout. */
TIMEOUT_SRC_SECURITY_REQUEST = 0x01, /**< Security request timeout. */
TIMEOUT_SRC_SCAN = 0x02, /**< Scanning timeout. */
TIMEOUT_SRC_CONN = 0x03, /**< Connection timeout. */
};
/**
* Enumeration for disconnection reasons. The values for these reasons are
* derived from Nordic's implementation, but the reasons are meant to be
* independent of the transport. If you are returned a reason that is not
* covered by this enumeration, please refer to the underlying
* transport library.
*/
enum DisconnectionReason_t {
CONNECTION_TIMEOUT = 0x08,
REMOTE_USER_TERMINATED_CONNECTION = 0x13,
REMOTE_DEV_TERMINATION_DUE_TO_LOW_RESOURCES = 0x14, /**< Remote device terminated connection due to low resources.*/
REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF = 0x15, /**< Remote device terminated connection due to power off. */
LOCAL_HOST_TERMINATED_CONNECTION = 0x16,
CONN_INTERVAL_UNACCEPTABLE = 0x3B,
};
/**
* Enumeration for whitelist advertising policy filter modes. The possible
* filter modes were obtained from the Bluetooth Core Specification
* 4.2 (Vol. 6), Part B, Section 4.3.2.
*
* @experimental
*/
enum AdvertisingPolicyMode_t {
ADV_POLICY_IGNORE_WHITELIST = 0,
ADV_POLICY_FILTER_SCAN_REQS = 1,
ADV_POLICY_FILTER_CONN_REQS = 2,
ADV_POLICY_FILTER_ALL_REQS = 3,
};
/**
* Enumeration for whitelist scanning policy filter modes. The possible
* filter modes were obtained from the Bluetooth Core Specification
* 4.2 (Vol. 6), Part B, Section 4.3.3.
*
* @experimental
*/
enum ScanningPolicyMode_t {
SCAN_POLICY_IGNORE_WHITELIST = 0,
SCAN_POLICY_FILTER_ALL_ADV = 1,
};
/**
* Enumeration for the whitelist initiator policy fiter modes. The possible
* filter modes were obtained from the Bluetooth Core Specification
* 4.2 (vol. 6), Part B, Section 4.4.4.
*
* @experimental
*/
enum InitiatorPolicyMode_t {
INIT_POLICY_IGNORE_WHITELIST = 0,
INIT_POLICY_FILTER_ALL_ADV = 1,
};
/**
* Representation of a Bluetooth Low Enery Whitelist containing addresses.
*
* @experimental
*/
struct Whitelist_t {
BLEProtocol::Address_t *addresses; /**< List of BLE addresses in the whitelist. */
uint8_t size; /**< Total number of BLE addresses in this whitelist */
uint8_t capacity; /**< Maximum number of BLE addresses that can be added to this whitelist. */
};
/**
* Describes the current state of the device (more than one bit can be set).
*/
struct GapState_t {
unsigned advertising : 1; /**< Peripheral is currently advertising. */
unsigned connected : 1; /**< Peripheral is connected to a central. */
};
/**
* Type for connection handle.
*/
typedef ble::connection_handle_t Handle_t;
/**
* Structure containing GAP connection parameters. When in peripheral role
* the connection parameters are suggestions. The choice of the connection
* parameters is eventually up to the central.
*/
typedef struct {
uint16_t minConnectionInterval; /**< Minimum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/
uint16_t maxConnectionInterval; /**< Maximum Connection Interval in 1.25 ms units, see BLE_GAP_CP_LIMITS.*/
uint16_t slaveLatency; /**< Slave Latency in number of connection events, see BLE_GAP_CP_LIMITS.*/
uint16_t connectionSupervisionTimeout; /**< Connection Supervision Timeout in 10 ms units, see BLE_GAP_CP_LIMITS.*/
} ConnectionParams_t;
/**
* Enumeration for the possible GAP roles of a BLE device.
*/
enum Role_t {
PERIPHERAL = 0x1, /**< Peripheral Role. */
CENTRAL = 0x2, /**< Central Role. */
};
/**
* Structure containing data and metadata of a scanned advertising packet.
*/
struct AdvertisementCallbackParams_t {
BLEProtocol::AddressBytes_t peerAddr; /**< The peer's BLE address. */
int8_t rssi; /**< The advertisement packet RSSI value. */
bool isScanResponse; /**< Whether this packet is the response to a scan request. */
GapAdvertisingParams::AdvertisingType_t type; /**< The type of advertisement. */
uint8_t advertisingDataLen; /**< Length of the advertisement data. */
const uint8_t *advertisingData; /**< Pointer to the advertisement packet's data. */
};
/**
* Type for the handlers of advertisement callback events. Refer to
* Gap::startScan().
*/
typedef FunctionPointerWithContext<const AdvertisementCallbackParams_t *> AdvertisementReportCallback_t;
/**
* Encapsulates the parameters of a connection. This information is passed
* to the registered handler of connection events. Refer to Gap::onConnection().
*/
struct ConnectionCallbackParams_t {
Handle_t handle; /**< The ID for this connection */
Role_t role; /**< This device's role in the connection */
BLEProtocol::AddressType_t peerAddrType; /**< The peer's BLE address type */
BLEProtocol::AddressBytes_t peerAddr; /**< The peer's BLE address */
BLEProtocol::AddressType_t ownAddrType; /**< This device's BLE address type */
BLEProtocol::AddressBytes_t ownAddr; /**< This devices's BLE address */
const ConnectionParams_t *connectionParams; /**< The currently configured connection parameters */
/**
* Constructor for ConnectionCallbackParams_t.
*
* @param[in] handleIn
* Value for ConnectionCallbackParams_t::handle
* @param[in] roleIn
* Value for ConnectionCallbackParams_t::role
* @param[in] peerAddrTypeIn
* Value for ConnectionCallbackParams_t::peerAddrType
* @param[in] peerAddrIn
* Value for ConnectionCallbackParams_t::peerAddr
* @param[in] ownAddrTypeIn
* Value for ConnectionCallbackParams_t::ownAddrType
* @param[in] ownAddrIn
* Value for ConnectionCallbackParams_t::ownAddr
* @param[in] connectionParamsIn
* Value for ConnectionCallbackParams_t::connectionParams
*/
ConnectionCallbackParams_t(Handle_t handleIn,
Role_t roleIn,
BLEProtocol::AddressType_t peerAddrTypeIn,
const uint8_t *peerAddrIn,
BLEProtocol::AddressType_t ownAddrTypeIn,
const uint8_t *ownAddrIn,
const ConnectionParams_t *connectionParamsIn) :
handle(handleIn),
role(roleIn),
peerAddrType(peerAddrTypeIn),
peerAddr(),
ownAddrType(ownAddrTypeIn),
ownAddr(),
connectionParams(connectionParamsIn) {
memcpy(peerAddr, peerAddrIn, ADDR_LEN);
memcpy(ownAddr, ownAddrIn, ADDR_LEN);
}
};
/**
* Structure that encapsulates information about a disconnection event.
* Refer to Gap::onDisconnection().
*/
struct DisconnectionCallbackParams_t {
Handle_t handle; /**< The ID of the connection that caused the disconnection event */
DisconnectionReason_t reason; /**< The reason of the disconnection event */
/**
* Constructor for DisconnectionCallbackParams_t.
*
* @param[in] handleIn
* Value for DisconnectionCallbackParams_t::handle.
* @param[in] reasonIn
* Value for DisconnectionCallbackParams_t::reason.
*/
DisconnectionCallbackParams_t(Handle_t handleIn,
DisconnectionReason_t reasonIn) :
handle(handleIn),
reason(reasonIn)
{}
};
static const uint16_t UNIT_1_25_MS = 1250; /**< Number of microseconds in 1.25 milliseconds. */
/**
* Helper function to convert from units of milliseconds to GAP duration
* units.
*
* @param[in] durationInMillis
* The duration in milliseconds.
*
* @return The duration in GAP duration units.
*/
static uint16_t MSEC_TO_GAP_DURATION_UNITS(uint32_t durationInMillis) {
return (durationInMillis * 1000) / UNIT_1_25_MS;
}
/**
* Type for the registered callbacks added to the timeout event callchain.
* Refer to Gap::onTimeout().
*/
typedef FunctionPointerWithContext<TimeoutSource_t> TimeoutEventCallback_t;
/**
* Type for the timeout event callchain. Refer to Gap::onTimeout().
*/
typedef CallChainOfFunctionPointersWithContext<TimeoutSource_t> TimeoutEventCallbackChain_t;
/**
* Type for the registered callbacks added to the connection event
* callchain. Refer to Gap::onConnection().
*/
typedef FunctionPointerWithContext<const ConnectionCallbackParams_t *> ConnectionEventCallback_t;
/**
* Type for the connection event callchain. Refer to Gap::onConnection().
*/
typedef CallChainOfFunctionPointersWithContext<const ConnectionCallbackParams_t *> ConnectionEventCallbackChain_t;
/**
* Type for the registered callbacks added to the disconnection event
* callchain. Refer to Gap::onDisconnection().
*/
typedef FunctionPointerWithContext<const DisconnectionCallbackParams_t*> DisconnectionEventCallback_t;
/**
* Type for the disconnection event callchain. Refer to Gap::onDisconnection().
*/
typedef CallChainOfFunctionPointersWithContext<const DisconnectionCallbackParams_t*> DisconnectionEventCallbackChain_t;
/**
* Type for the handlers of radio notification callback events. Refer to
* Gap::onRadioNotification().
*/
typedef FunctionPointerWithContext<bool> RadioNotificationEventCallback_t;
/**
* Type for the handlers of shutdown callback events. Refer to
* Gap::onShutdown().
*/
typedef FunctionPointerWithContext<const Gap *> GapShutdownCallback_t;
/**
* Type for the shutdown event callchain. Refer to Gap::onShutdown().
*/
typedef CallChainOfFunctionPointersWithContext<const Gap *> GapShutdownCallbackChain_t;
/*
* The following functions are meant to be overridden in the platform-specific sub-class.
*/
public:
/**
* Set the BTLE MAC address and type. Please note that the address format is
* least significant byte first (LSB). Please refer to BLEProtocol::AddressBytes_t.
*
* @param[in] type
* The type of the BLE address to set.
* @param[in] address
* The BLE address to set.
*
* @return BLE_ERROR_NONE on success.
*/
virtual ble_error_t setAddress(BLEProtocol::AddressType_t type, const BLEProtocol::AddressBytes_t address) {
/* avoid compiler warnings about unused variables */
(void)type;
(void)address;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Fetch the BTLE MAC address and type.
*
* @param[out] typeP
* The current BLE address type.
* @param[out] address
* The current BLE address.
*
* @return BLE_ERROR_NONE on success.
*/
virtual ble_error_t getAddress(BLEProtocol::AddressType_t *typeP, BLEProtocol::AddressBytes_t address) {
/* Avoid compiler warnings about unused variables. */
(void)typeP;
(void)address;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Get the minimum advertising interval in milliseconds for connectable
* undirected and connectable directed event types supported by the
* underlying BLE stack.
*
* @return Minimum Advertising interval in milliseconds for connectable
* undirected and connectable directed event types.
*/
virtual uint16_t getMinAdvertisingInterval(void) const {
return 0; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Get the minimum advertising interval in milliseconds for scannable
* undirected and non-connectable undirected even types supported by the
* underlying BLE stack.
*
* @return Minimum Advertising interval in milliseconds for scannable
* undirected and non-connectable undirected event types.
*/
virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {
return 0; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Get the maximum advertising interval in milliseconds for all event types
* supported by the underlying BLE stack.
*
* @return Maximum Advertising interval in milliseconds.
*/
virtual uint16_t getMaxAdvertisingInterval(void) const {
return 0xFFFF; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Stop advertising. The current advertising parameters remain in effect.
*
* @retval BLE_ERROR_NONE if successfully stopped advertising procedure.
*/
virtual ble_error_t stopAdvertising(void) {
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Stop scanning. The current scanning parameters remain in effect.
*
* @retval BLE_ERROR_NONE if successfully stopped scanning procedure.
*/
virtual ble_error_t stopScan() {
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Create a connection (GAP Link Establishment).
*
* @param[in] peerAddr
* 48-bit address, LSB format.
* @param[in] peerAddrType
* Address type of the peer.
* @param[in] connectionParams
* Connection parameters.
* @param[in] scanParams
* Parameters to be used while scanning for the peer.
*
* @return BLE_ERROR_NONE if connection establishment procedure is started
* successfully. The connectionCallChain (if set) will be invoked upon
* a connection event.
*/
virtual ble_error_t connect(const BLEProtocol::AddressBytes_t peerAddr,
BLEProtocol::AddressType_t peerAddrType,
const ConnectionParams_t *connectionParams,
const GapScanningParams *scanParams) {
/* Avoid compiler warnings about unused variables. */
(void)peerAddr;
(void)peerAddrType;
(void)connectionParams;
(void)scanParams;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Create a connection (GAP Link Establishment).
*
* @deprecated This funtion overloads Gap::connect(const BLEProtocol::Address_t peerAddr,
* BLEProtocol::AddressType_t peerAddrType,
* const ConnectionParams_t *connectionParams,
* const GapScanningParams *scanParams)
* to maintain backward compatibility for change from Gap::AddressType_t to
* BLEProtocol::AddressType_t.
*/
ble_error_t connect(const BLEProtocol::AddressBytes_t peerAddr,
DeprecatedAddressType_t peerAddrType,
const ConnectionParams_t *connectionParams,
const GapScanningParams *scanParams)
__deprecated_message("Gap::DeprecatedAddressType_t is deprecated, use BLEProtocol::AddressType_t instead") {
return connect(peerAddr, (BLEProtocol::AddressType_t) peerAddrType, connectionParams, scanParams);
}
/**
* This call initiates the disconnection procedure, and its completion will
* be communicated to the application with an invocation of the
* disconnectionCallback.
*
* @param[in] reason
* The reason for disconnection; to be sent back to the peer.
* @param[in] connectionHandle
* The handle of the connection to disconnect from.
*
* @return BLE_ERROR_NONE if disconnection was successful.
*/
virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason) {
/* avoid compiler warnings about unused variables */
(void)connectionHandle;
(void)reason;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* This call initiates the disconnection procedure, and its completion will
* be communicated to the application with an invocation of the
* disconnectionCallback.
*
* @param[in] reason
* The reason for disconnection; to be sent back to the peer.
*
* @return BLE_ERROR_NONE if disconnection was successful.
*
* @deprecated This version of disconnect() doesn't take a connection handle. It
* works reliably only for stacks that are limited to a single
* connection. Use Gap::disconnect(Handle_t connectionHandle,
* DisconnectionReason_t reason) instead.
*/
virtual ble_error_t disconnect(DisconnectionReason_t reason) {
/* Avoid compiler warnings about unused variables. */
(void)reason;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Get the GAP peripheral preferred connection parameters. These are the
* defaults that the peripheral would like to have in a connection. The
* choice of the connection parameters is eventually up to the central.
*
* @param[out] params
* The structure where the parameters will be stored. Memory
* for this is owned by the caller.
*
* @return BLE_ERROR_NONE if the parameters were successfully filled into
* the given structure pointed to by params.
*/
virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) {
/* Avoid compiler warnings about unused variables. */
(void)params;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Set the GAP peripheral preferred connection parameters. These are the
* defaults that the peripheral would like to have in a connection. The
* choice of the connection parameters is eventually up to the central.
*
* @param[in] params
* The structure containing the desired parameters.
*
* @return BLE_ERROR_NONE if the preferred connection params were set
* correctly.
*/
virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) {
/* Avoid compiler warnings about unused variables. */
(void)params;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Update connection parameters. In the central role this will initiate a
* Link Layer connection parameter update procedure. In the peripheral role,
* this will send the corresponding L2CAP request and wait for the central
* to perform the procedure.
*
* @param[in] handle
* Connection Handle.
* @param[in] params
* Pointer to desired connection parameters. If NULL is provided on a peripheral role,
* the parameters in the PPCP characteristic of the GAP service will be used instead.
*
* @return BLE_ERROR_NONE if the connection parameters were updated correctly.
*/
virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) {
/* avoid compiler warnings about unused variables */
(void)handle;
(void)params;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Set the device name characteristic in the GAP service.
*
* @param[in] deviceName
* The new value for the device-name. This is a UTF-8 encoded, <b>NULL-terminated</b> string.
*
* @return BLE_ERROR_NONE if the device name was set correctly.
*/
virtual ble_error_t setDeviceName(const uint8_t *deviceName) {
/* Avoid compiler warnings about unused variables. */
(void)deviceName;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Get the value of the device name characteristic in the GAP service.
*
* @param[out] deviceName
* Pointer to an empty buffer where the UTF-8 *non NULL-
* terminated* string will be placed. Set this
* value to NULL in order to obtain the deviceName-length
* from the 'length' parameter.
*
* @param[in,out] lengthP
* (on input) Length of the buffer pointed to by deviceName;
* (on output) the complete device name length (without the
* null terminator).
*
* @return BLE_ERROR_NONE if the device name was fetched correctly from the
* underlying BLE stack.
*
* @note If the device name is longer than the size of the supplied buffer,
* length will return the complete device name length, and not the
* number of bytes actually returned in deviceName. The application may
* use this information to retry with a suitable buffer size.
*/
virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP) {
/* avoid compiler warnings about unused variables */
(void)deviceName;
(void)lengthP;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Set the appearance characteristic in the GAP service.
*
* @param[in] appearance
* The new value for the device-appearance.
*
* @return BLE_ERROR_NONE if the new appearance was set correctly.
*/
virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance) {
/* Avoid compiler warnings about unused variables. */
(void)appearance;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Get the appearance characteristic in the GAP service.
*
* @param[out] appearance
* The current device-appearance value.
*
* @return BLE_ERROR_NONE if the device-appearance was fetched correctly
* from the underlying BLE stack.
*/
virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP) {
/* Avoid compiler warnings about unused variables. */
(void)appearanceP;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Set the radio's transmit power.
*
* @param[in] txPower
* Radio's transmit power in dBm.
*
* @return BLE_ERROR_NONE if the new radio's transmit power was set
* correctly.
*/
virtual ble_error_t setTxPower(int8_t txPower) {
/* Avoid compiler warnings about unused variables. */
(void)txPower;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Query the underlying stack for permitted arguments for setTxPower().
*
* @param[out] valueArrayPP
* Out parameter to receive the immutable array of Tx values.
* @param[out] countP
* Out parameter to receive the array's size.
*/
virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP) {
/* Avoid compiler warnings about unused variables. */
(void)valueArrayPP;
(void)countP;
*countP = 0; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/**
* Get the maximum size of the whitelist.
*
* @return Maximum size of the whitelist.
*
* @note If using mbed OS the size of the whitelist can be configured by
* setting the YOTTA_CFG_WHITELIST_MAX_SIZE macro in your yotta
* config file.
*
* @experimental
*/
virtual uint8_t getMaxWhitelistSize(void) const
{
return 0;
}
/**
* Get the internal whitelist to be used by the Link Layer when scanning,
* advertising or initiating a connection depending on the filter policies.
*
* @param[in,out] whitelist
* (on input) whitelist.capacity contains the maximum number
* of addresses to be returned.
* (on output) The populated whitelist with copies of the
* addresses in the implementation's whitelist.
*
* @return BLE_ERROR_NONE if the implementation's whitelist was successfully
* copied into the supplied reference.
*
* @experimental
*/
virtual ble_error_t getWhitelist(Whitelist_t &whitelist) const
{
(void) whitelist;
return BLE_ERROR_NOT_IMPLEMENTED;
}
/**
* Set the internal whitelist to be used by the Link Layer when scanning,
* advertising or initiating a connection depending on the filter policies.
*
* @param[in] whitelist
* A reference to a whitelist containing the addresses to
* be added to the internal whitelist.
*
* @return BLE_ERROR_NONE if the implementation's whitelist was successfully
* populated with the addresses in the given whitelist.
*
* @note The whitelist must not contain addresses of type @ref
* BLEProtocol::AddressType_t::RANDOM_PRIVATE_NON_RESOLVABLE, this
* this will result in a @ref BLE_ERROR_INVALID_PARAM since the
* remote peer might change its private address at any time and it
* is not possible to resolve it.
* @note If the input whitelist is larger than @ref getMaxWhitelistSize()
* the @ref BLE_ERROR_PARAM_OUT_OF_RANGE is returned.
*
* @experimental
*/
virtual ble_error_t setWhitelist(const Whitelist_t &whitelist)
{
(void) whitelist;
return BLE_ERROR_NOT_IMPLEMENTED;
}
/**
* Set the advertising policy filter mode to be used in the next call
* to startAdvertising().
*
* @param[in] mode
* The new advertising policy filter mode.
*
* @return BLE_ERROR_NONE if the specified policy filter mode was set
* successfully.
*
* @experimental
*/
virtual ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode)
{
(void) mode;
return BLE_ERROR_NOT_IMPLEMENTED;
}
/**
* Set the scan policy filter mode to be used in the next call
* to startScan().
*
* @param[in] mode
* The new scan policy filter mode.
*
* @return BLE_ERROR_NONE if the specified policy filter mode was set
* successfully.
*
* @experimental
*/
virtual ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode)
{
(void) mode;
return BLE_ERROR_NOT_IMPLEMENTED;
}
/**
* Set the initiator policy filter mode to be used.
*
* @param[in] mode
* The new initiator policy filter mode.
*
* @return BLE_ERROR_NONE if the specified policy filter mode was set
* successfully.
*
* @experimental
*/
virtual ble_error_t setInitiatorPolicyMode(InitiatorPolicyMode_t mode)
{
(void) mode;
return BLE_ERROR_NOT_IMPLEMENTED;
}
/**
* Get the advertising policy filter mode that will be used in the next
* call to startAdvertising().
*
* @return The set advertising policy filter mode.
*
* @experimental
*/
virtual AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const
{
return ADV_POLICY_IGNORE_WHITELIST;
}
/**
* Get the scan policy filter mode that will be used in the next
* call to startScan().
*
* @return The set scan policy filter mode.
*
* @experimental
*/
virtual ScanningPolicyMode_t getScanningPolicyMode(void) const
{
return SCAN_POLICY_IGNORE_WHITELIST;
}
/**
* Get the initiator policy filter mode that will be used.
*
* @return The set scan policy filter mode.
*
* @experimental
*/
virtual InitiatorPolicyMode_t getInitiatorPolicyMode(void) const
{
return INIT_POLICY_IGNORE_WHITELIST;
}
protected:
/* Override the following in the underlying adaptation layer to provide the functionality of scanning. */
/**
* Start scanning procedure in the underlying BLE stack.
*
* @param[in] scanningParams
* The GAP scanning parameters.
*
* @return BLE_ERROR_NONE if the scan procedure started successfully.
*/
virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) {
(void)scanningParams;
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}
/*
* APIs with non-virtual implementations.
*/
public:
/**
* Get the current GAP state of the device using a bitmask that
* describes whether the device is advertising or connected.
*
* @return The current GAP state of the device.
*/
GapState_t getState(void) const {
return state;
}
/**
* Set the GAP advertising mode to use for this device.
*
* @param[in] advType
* The new type of the advertising packets.
*/
void setAdvertisingType(GapAdvertisingParams::AdvertisingType_t advType) {
_advParams.setAdvertisingType(advType);
}
/**
* Set the advertising interval.
*
* @param[in] interval
* Advertising interval in units of milliseconds. Advertising
* is disabled if interval is 0. If interval is smaller than
* the minimum supported value, then the minimum supported
* value is used instead. This minimum value can be discovered
* using getMinAdvertisingInterval().
*
* This field must be set to 0 if connectionMode is equal
* to ADV_CONNECTABLE_DIRECTED.
*
* @note Decreasing this value will allow central devices to detect a
* peripheral faster, at the expense of more power being used by the radio
* due to the higher data transmit rate.
*
* @note [WARNING] This API previously used 0.625ms as the unit for its
* @p interval argument. That required an explicit conversion from
* milliseconds using Gap::MSEC_TO_GAP_DURATION_UNITS(). This conversion is
* no longer required as the new units are milliseconds. Any application
* code depending on the old semantics needs to be updated accordingly.
*/
void setAdvertisingInterval(uint16_t interval) {
if (interval == 0) {
stopAdvertising();
} else if (interval < getMinAdvertisingInterval()) {
interval = getMinAdvertisingInterval();
}
_advParams.setInterval(interval);
}
/**
* Set the advertising timeout. The length of time to advertise for before
* a timeout event is generated.
*
* @param[in] timeout
* Advertising timeout (in seconds) between 0x1 and 0x3FFF (1
* and 16383). Use 0 to disable the advertising timeout.
*/
void setAdvertisingTimeout(uint16_t timeout) {
_advParams.setTimeout(timeout);
}
/**
* Start advertising.
*
* @return BLE_ERROR_NONE if the device started advertising successfully.
*/
ble_error_t startAdvertising(void) {
ble_error_t rc;
if ((rc = startAdvertising(_advParams)) == BLE_ERROR_NONE) {
state.advertising = 1;
}
return rc;
}
/**
* Reset any advertising payload prepared from prior calls to
* accumulateAdvertisingPayload(). This automatically propagates the re-
* initialized advertising payload to the underlying stack.
*/
void clearAdvertisingPayload(void) {
_advPayload.clear();
setAdvertisingData(_advPayload, _scanResponse);
}
/**
* Accumulate an AD structure in the advertising payload. Please note that
* the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
* as an additional 31 bytes if the advertising payload is too
* small.
*
* @param[in] flags
* The flags to be added. Please refer to
* GapAdvertisingData::Flags for valid flags. Multiple
* flags may be specified in combination.
*
* @return BLE_ERROR_NONE if the data was successfully added to the
* advertising payload.
*/
ble_error_t accumulateAdvertisingPayload(uint8_t flags) {
GapAdvertisingData advPayloadCopy = _advPayload;
ble_error_t rc;
if ((rc = advPayloadCopy.addFlags(flags)) != BLE_ERROR_NONE) {
return rc;
}
rc = setAdvertisingData(advPayloadCopy, _scanResponse);
if (rc == BLE_ERROR_NONE) {
_advPayload = advPayloadCopy;
}
return rc;
}
/**
* Accumulate an AD structure in the advertising payload. Please note that
* the payload is limited to 31 bytes. The SCAN_RESPONSE message may be used
* as an additional 31 bytes if the advertising payload is too
* small.
*
* @param[in] app
* The appearance of the peripheral.
*
* @return BLE_ERROR_NONE if the data was successfully added to the
* advertising payload.
*/
ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::Appearance app) {