-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Gap.h
1497 lines (1406 loc) · 54.8 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-2020 ARM Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* 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 BLE_GAP_GAP_H
#define BLE_GAP_GAP_H
#include "ble/common/CallChainOfFunctionPointersWithContext.h"
#include "ble/common/BLERoles.h"
#include "ble/common/BLETypes.h"
#include "ble/gap/AdvertisingDataBuilder.h"
#include "ble/gap/AdvertisingDataParser.h"
#include "ble/gap/AdvertisingDataSimpleBuilder.h"
#include "ble/gap/AdvertisingDataTypes.h"
#include "ble/gap/AdvertisingParameters.h"
#include "ble/gap/ConnectionParameters.h"
#include "ble/gap/ScanParameters.h"
#include "ble/gap/Events.h"
#include "ble/gap/Types.h"
namespace ble {
#if !defined(DOXYGEN_ONLY)
namespace impl {
class Gap;
}
#endif // !defined(DOXYGEN_ONLY)
/**
* @addtogroup ble
* @{
* @addtogroup gap
* @{
*/
/**
* Define device discovery, connection and link management procedures.
*
* - Device discovery: A device can advertise to nearby peers its existence,
* identity and capabilities. Similarly, a device can scan its environment to
* find advertising peers. The information acquired during the scan helps to
* identify peers and understand their use. A scanner may acquire more information
* about an advertising peer by sending a scan request. If the peer accepts scan
* requests, it may reply with additional information about its state.
*
* - Connection: A bluetooth device can establish a connection to a connectable
* advertising peer. Once the connection is established, both devices can
* communicate using the GATT protocol. The GATT protocol allows connected
* devices to expose a set of states that the other peer can discover, read and write.
*
* - Link Management: Connected devices may drop the connection and may adjust
* connection parameters according to the power envelop needed for their
* application.
*
* @par Accessing gap
*
* Instance of a Gap class for a given BLE device should be accessed using
* BLE::gap(). The reference returned remains valid until the BLE instance
* shut down (see BLE::shutdown()).
*
* @code
* // assuming ble_device has been initialized
* BLE& ble_device;
*
* ble::Gap& gap = ble_device.gap();
* @endcode
*
* @par Advertising
*
* Advertising consists of broadcasting at a regular interval a small amount of
* data containing valuable information about the device. These packets may be
* scanned by peer devices listening on BLE advertising channels.
*
* Scanners may also request additional information from a device advertising by
* sending a scan request. If the broadcaster accepts scan requests, it can reply
* with a scan response packet containing additional information.
*
* Advertising parameters are updated using setAdvertisingParams(). The main
* advertising payload is updated using setAdvertisingPayload(), and the scan response
* is updated using setAdvertisingScanResponse(). If the advertising is already
* updated, the data will take effect from the next advertising event.
*
* To create a valid advertising payload and scan response, you may use
* AdvertisingDataBuilder. You must first allocate memory and create an mbed::Span and
* pass that into the AdvertisingDataBuilder, which will only be able to add as much
* data as fits in the provided buffer. The builder accepts any size of the buffer,
* but for the created data to be usable, it must be smaller than the maximum data
* length returned from getMaxAdvertisingDataLength().
*
* Another option is to use AdvertisingDataSimpleBuilder, which allocates memory
* on the stack and offers a fluent interface at the expense of a reduced set of
* APIs and error management options.
*
* @note Prior to Bluetooth 5, advertising and scanning payload size were limited
* to LEGACY_ADVERTISING_MAX_SIZE. It changed with Bluetooth 5, and now the maximum
* size of data that can be advertised depends on the controller. If you wish
* to be compatible with older devices, you may wish to advertise with the
* LEGACY_ADVERTISING_HANDLE. This uses payloads no larger than LEGACY_ADVERTISING_MAX_SIZE
* with that advertising set.
*
* @par Extended advertising
*
* Extended advertising allows for a wider choice of options than legacy advertising.
* You can send bigger payloads and use different PHYs. This allows for bigger throughput
* or longer range.
*
* Extended advertising may be split across many packets and takes place on both the
* regular advertising channels and the rest of the 37 channels normally used by
* connected devices.
*
* The 3 channels used in legacy advertising are called primary advertisement channels.
* The remaining 37 channels are used for secondary advertising. Unlike sending data
* during a connection, this allows the device to broadcast data to multiple devices.
*
* The advertising starts on the primary channels (which you may select) and continues
* on the secondary channels as indicated in the packet sent on the primary channel.
* This way, the advertising can send large payloads without saturating the advertising
* channels. Primary channels are limited to 1M and coded PHYs, but secondary channels
* may use the increased throughput 2M PHY.
*
* @par Periodic advertising
*
* Similarly, you can use periodic advertising to transfer regular data to multiple
* devices.
*
* The advertiser uses primary channels to advertise the information needed to
* listen to the periodic advertisements on secondary channels. This sync information
* will be used by the scanner who can now optimize for power consumption and only
* listen for the periodic advertisements at specified times.
*
* Like extended advertising, periodic advertising offers extra PHY options of 2M
* and coded. The payload may be updated at any time and will be updated on the next
* advertisement event when the periodic advertising is active.
*
* @par Advertising sets
*
* Advertisers may advertise multiple payloads at the same time. The configuration
* and identification of these is done through advertising sets. Use a handle
* obtained from createAvertisingSet() for advertising operations. After ending
* all advertising operations, remove the handle from the system using
* destroyAdvertisingHandle().
*
* Extended advertising and periodic advertising is an optional feature, and not all
* devices support it. Some will only be able to see the now-called legacy advertising.
*
* Legacy advertising is available through a special handle, LEGACY_ADVERTISING_HANDLE.
* This handle is always available, doesn't need to be created and can't be
* destroyed.
*
* There is a limited number of advertising sets available because they require support
* from the controller. Their availability is dynamic and may be queried at any time
* using getMaxAdvertisingSetNumber(). Advertising sets take up resources even if
* they are not actively advertising right now, so it's important to destroy the set
* when you're done with it (or reuse it in the next advertisement).
*
* Periodic advertising and extended advertising share the same set but not the same
* data. Extended advertising carries out periodic advertising synchronization
* information. Therefore, to let other devices be aware that your device
* exposes periodic advertising, you should start extended advertising of the set.
* Subsequently, you may disable extended advertising, and the periodic advertising
* will continue. If you start periodic advertising while extended advertising is
* inactive, periodic advertising won't start until you start extended advertising
* at a later time.
*
* @par Privacy
*
* Privacy is a feature that allows a device to avoid being tracked by other
* (untrusted) devices. The device achieves it by periodically generating a
* new random address. The random address may be a resolvable random address,
* enabling trusted devices to recognize it as belonging to the same
* device. These trusted devices receive an Identity Resolution Key (IRK)
* during pairing. This is handled by the SecurityManager and relies on the
* other device accepting and storing the IRK.
*
* You need to enable privacy by calling enablePrivacy() after having
* initialized the SecurityManager because privacy requires SecurityManager
* to handle IRKs. The behavior of privacy enabled devices is set by
* using setCentralPrivacyConfiguration(), which specifies what the device
* should be with devices using random addresses. Random addresses
* generated by privacy enabled devices can be of two types: resolvable
* (by devices who have the IRK) and unresolvable. Unresolvable addresses
* can't be used for connecting and connectable advertising. Therefore, a
* resolvable one will be used for these regardless of the privacy
* configuration.
*
* @par Scanning
*
* Scanning consists of listening for peer advertising packets. From a scan, a
* device can identify devices available in its environment.
*
* If the device scans actively, then it will send scan request to scannable
* advertisers and collect their scan responses.
*
* Scanning is done by creating ScanParameters and applying them with
* setScanParameters(). One configured, you may call startScan().
*
* When a scanning device receives an advertising packet, it will call
* onAdvertisingReport() in the registered event handler. A whitelist may be used
* to limit the advertising reports by setting the correct policy in the scan
* parameters.
*
* @par Connection event handling
*
* A peer may connect device advertising connectable packets. The advertising
* procedure ends as soon as the device is connected. If an advertising timeout
* has been set in the advertising parameters then onAdvertisingEnd will be called
* in the registered eventHandler when it runs out.
*
* A device accepting a connection request from a peer is named a peripheral,
* and the device initiating the connection is named a central.
*
* Connection is initiated by central devices. A call to connect() will result in
* the device scanning on any PHYs set in ConectionParamters passed in.
*
* Peripheral and central receive a connection event when the connection is
* effective. If successful will result in a call to onConnectionComplete in the
* EventHandler registered with the Gap.
*
* It the connection attempt fails it will result in onConnectionComplete called
* on the central device with the event carrying the error flag.
*
* @par Changing the PHYsical transport of a connection
*
* Once a connection has been established, it is possible to change the physical
* transport used between the local and the distant device. Changing the transport
* can either increase the bandwidth or increase the communication range.
* An increased bandwidth equals a better power consumption but also a loss in
* sensibility and therefore a degraded range.
*
* Symmetrically an increased range means a lowered bandwidth and a degraded power
* consumption.
*
* Applications can change the PHY used by calling the function setPhy. Once the
* update has been made the result is forwarded to the application by calling the
* function onPhyUpdateComplete of the event handler registered.
*
* @par disconnection
*
* The application code initiates a disconnection when it calls the
* disconnect(Handle_t, DisconnectionReason_t) function.
*
* Disconnection may also be initiated by the remote peer or the local
* controller/stack. To catch all disconnection events, application code may
* set up an handler taking care of disconnection events by calling
* onDisconnection().
*
* @par Modulation Schemes
*
* When supported by the host and controller you can select different modulation
* schemes (@see BLUETOOTH SPECIFICATION Version 5.0 | Vol 1, Part A - 1.2):
* - LE 1M PHY
* - LE 2M PHY
* - LE coded PHY
*
* You may set preferred PHYs (separately for RX and TX) using setPreferredPhys().
* You may also set the currently used PHYs on a selected connection using setPhy().
* Both of these settings are only advisory and the controller is allowed to make
* its own decision on the best PHY to use based on your request, the peer's
* supported features and the connection's physical conditions.
*
* You may query the currently used PHY using readPhy() which will return the
* result through a call to the registered event handler. You may register the
* handler with setEventHandler(). The events inform about the currently used
* PHY and of any changes to PHYs which may be triggered autonomously by the
* controller or by the peer.
*/
class Gap {
public:
/**
* Gap shutdown event handler.
*
* @see Gap::onShutdown().
*/
typedef FunctionPointerWithContext<const Gap *> GapShutdownCallback_t;
/**
* Callchain of gap shutdown event handler.
*
* @see Gap::onShutdown().
*/
typedef CallChainOfFunctionPointersWithContext<const Gap *>
GapShutdownCallbackChain_t;
public:
/**
* Definition of the general handler of Gap related events.
*/
struct EventHandler {
/**
* Called when an advertising device receive a scan response.
*
* @param event Scan request event.
*
* @version: 5+.
*
* @see AdvertisingParameters::setScanRequestNotification().
*/
virtual void onScanRequestReceived(const ScanRequestEvent &event)
{
}
/**
* Called when advertising starts.
*
* @param event Advertising start event.
*
* @see startAdvertising()
*/
virtual void onAdvertisingStart(const AdvertisingStartEvent &event)
{
}
/**
* Called when advertising ends.
*
* Advertising ends when the process timeout or if it is stopped by the
* application or if the local device accepts a connection request.
*
* @param event Advertising end event.
*
* @see startAdvertising()
* @see stopAdvertising()
* @see onConnectionComplete()
*/
virtual void onAdvertisingEnd(const AdvertisingEndEvent &event)
{
}
/**
* Called when a scanner receives an advertising or a scan response packet.
*
* @param event Advertising report.
*
* @see startScan()
*/
virtual void onAdvertisingReport(const AdvertisingReportEvent &event)
{
}
/**
* Called when scan times out.
*
* @param event Associated event.
*
* @see startScan()
*/
virtual void onScanTimeout(const ScanTimeoutEvent &event)
{
}
/**
* Called when first advertising packet in periodic advertising is received.
*
* @param event Periodic advertising sync event.
*
* @version: 5+.
*
* @see createSync()
*/
virtual void onPeriodicAdvertisingSyncEstablished(
const PeriodicAdvertisingSyncEstablishedEvent &event
)
{
}
/**
* Called when a periodic advertising packet is received.
*
* @param event Periodic advertisement event.
*
* @version: 5+.
*
* @see createSync()
*/
virtual void onPeriodicAdvertisingReport(
const PeriodicAdvertisingReportEvent &event
)
{
}
/**
* Called when a periodic advertising sync has been lost.
*
* @param event Details of the event.
*
* @version: 5+.
*
* @see createSync()
*/
virtual void onPeriodicAdvertisingSyncLoss(
const PeriodicAdvertisingSyncLoss &event
)
{
}
/**
* Called when connection attempt ends or an advertising device has been
* connected.
*
* @see startAdvertising()
* @see connect()
*
* @param event Connection event.
*/
virtual void onConnectionComplete(const ConnectionCompleteEvent &event)
{
}
/**
* Called when the peer request connection parameters updates.
*
* Application must accept the update with acceptConnectionParametersUpdate()
* or reject it with rejectConnectionParametersUpdate().
*
* @param event The connection parameters requested by the peer.
*
* @version 4.1+.
*
* @note This event is not generated if connection parameters update
* is managed by the middleware.
*
* @see manageConnectionParametersUpdateRequest()
* @see acceptConnectionParametersUpdate()
* @see rejectConnectionParametersUpdate()
*/
virtual void onUpdateConnectionParametersRequest(
const UpdateConnectionParametersRequestEvent &event
)
{
}
/**
* Called when connection parameters have been updated.
*
* @param event The new connection parameters.
*
* @see updateConnectionParameters()
* @see acceptConnectionParametersUpdate()
*/
virtual void onConnectionParametersUpdateComplete(
const ConnectionParametersUpdateCompleteEvent &event
)
{
}
/**
* Called when a connection has been disconnected.
*
* @param event Details of the event.
*
* @see disconnect()
*/
virtual void onDisconnectionComplete(const DisconnectionCompleteEvent &event)
{
}
/**
* Function invoked when the current transmitter and receiver PHY have
* been read for a given connection.
*
* @param status Status of the operation: BLE_ERROR_NONE in case of
* success or an appropriate error code.
*
* @param connectionHandle: The handle of the connection for which the
* PHYs have been read.
*
* @param txPhy PHY used by the transmitter.
*
* @param rxPhy PHY used by the receiver.
*
* @see readPhy().
*
* @version: 5+.
*/
virtual void onReadPhy(
ble_error_t status,
connection_handle_t connectionHandle,
phy_t txPhy,
phy_t rxPhy
)
{
}
/**
* Function invoked when the update process of the PHY has been completed.
*
* The process can be initiated by a call to the function setPhy, the
* local bluetooth subsystem or the peer.
*
* @param status Status of the operation: BLE_ERROR_NONE in case of
* success or an appropriate error code.
*
* @param connectionHandle: The handle of the connection on which the
* operation was made.
*
* @param txPhy PHY used by the transmitter.
*
* @param rxPhy PHY used by the receiver.
*
* @note Success doesn't mean the PHY has been updated it means both
* ends have negotiated the best PHY according to their configuration and
* capabilities. The PHY currently used are present in the txPhy and
* rxPhy parameters.
*
* @see setPhy()
*
* @version: 5+.
*/
virtual void onPhyUpdateComplete(
ble_error_t status,
connection_handle_t connectionHandle,
phy_t txPhy,
phy_t rxPhy
)
{
}
/**
* Function invoked when the connections changes the maximum number of octets
* that can be sent or received by the controller in a single packet. A single
* L2CAP packet can be fragmented across many such packets.
*
* @note This only triggers if controller supports data length extension and
* negotiated data length is longer than the default 23.
*
* @param connectionHandle The handle of the connection that changed the size.
* @param txSize Number of octets we can send on this connection in a single packet.
* @param rxSize Number of octets we can receive on this connection in a single packet.
*/
virtual void onDataLengthChange(
connection_handle_t connectionHandle,
uint16_t txSize,
uint16_t rxSize
)
{
}
/**
* Function invoked when the privacy subsystem has been enabled and is
* ready to be used.
*/
virtual void onPrivacyEnabled()
{
}
protected:
/**
* Prevent polymorphic deletion and avoid unnecessary virtual destructor
* as the Gap class will never delete the instance it contains.
*/
~EventHandler() = default;
};
/**
* Preferred connection parameter display in Generic Access Service.
*/
typedef struct {
/**
* Minimum interval between two connection events allowed for a
* connection.
*
* It shall be less than or equal to maxConnectionInterval. This value,
* in units of 1.25ms, is included in the range [0x0006 : 0x0C80].
*/
uint16_t minConnectionInterval;
/**
* Maximum interval between two connection events allowed for a
* connection.
*
* It shall be greater than or equal to minConnectionInterval. This
* value is in unit of 1.25ms and is in the range [0x0006 : 0x0C80].
*/
uint16_t maxConnectionInterval;
/**
* Number of connection events the slave can drop if it has nothing to
* communicate to the master.
*
* This value shall be in the range [0x0000 : 0x01F3].
*/
uint16_t slaveLatency;
/**
* Link supervision timeout for the connection.
*
* Time after which the connection is considered lost if the device
* didn't receive a packet from its peer.
*
* It is larger than:
* (1 + slaveLatency) * maxConnectionInterval * 2
*
* This value is in the range [0x000A : 0x0C80] and is in unit of
* 10 ms.
*
* @note maxConnectionInterval is in ms in the formulae above.
*/
uint16_t connectionSupervisionTimeout;
} PreferredConnectionParams_t;
/**
* Assign the event handler implementation that will be used by the gap
* module to signal events back to the application.
*
* @param handler Application implementation of an EventHandler.
*/
void setEventHandler(EventHandler *handler);
/** Check controller support for a specific feature.
*
* @param feature Feature to check.
* @return True if feature is supported.
*/
bool isFeatureSupported(controller_supported_features_t feature);
/* advertising */
#if BLE_ROLE_BROADCASTER
/** Return currently available number of supported advertising sets.
* This may change at runtime.
*
* @note Devices that do not support Bluetooth 5 still offers one advertising
* set that has the handle LEGACY_ADVERTISING_HANDLE.
*
* @return Number of advertising sets that may be created at the same time.
*/
uint8_t getMaxAdvertisingSetNumber();
/** Return maximum advertising data length supported.
*
* @return Maximum advertising data length supported.
*/
uint16_t getMaxAdvertisingDataLength();
/** Return maximum advertising data length supported for connectable advertising.
*
* @return Maximum advertising data length supported for connectable advertising.
*/
uint16_t getMaxConnectableAdvertisingDataLength();
/** Return maximum advertising data length you may set if advertising set is active.
*
* @return Maximum advertising data length you may set if advertising set is active.
*/
uint16_t getMaxActiveSetAdvertisingDataLength();
#if BLE_FEATURE_EXTENDED_ADVERTISING
/** Create an advertising set and apply the passed in parameters. The handle returned
* by this function must be used for all other calls that accept an advertising handle.
* When done with advertising, remove from the system using destroyAdvertisingSet().
*
* @note The exception is the LEGACY_ADVERTISING_HANDLE which may be used at any time.
*
* @param[out] handle Advertising handle returned, valid only if function returned success.
* @param parameters Advertising parameters for the newly created set.
* @return BLE_ERROR_NONE on success.
*
* @version 5+
*/
ble_error_t createAdvertisingSet(
advertising_handle_t *handle,
const AdvertisingParameters ¶meters
);
/** Remove the advertising set (resets its set parameters). The advertising set must not
* be active.
*
* @note LEGACY_ADVERTISING_HANDLE may not be destroyed.
*
* @param handle Advertising set handle.
* @return BLE_ERROR_NONE on success.
*
* @version 5+
*/
ble_error_t destroyAdvertisingSet(advertising_handle_t handle);
#endif // BLE_FEATURE_EXTENDED_ADVERTISING
/** Set advertising parameters of an existing set.
*
* @param handle Advertising set handle.
* @param params New advertising parameters.
* @return BLE_ERROR_NONE on success.
*/
ble_error_t setAdvertisingParameters(
advertising_handle_t handle,
const AdvertisingParameters ¶ms
);
/** Set new advertising payload for a given advertising set.
*
* @param handle Advertising set handle.
* @param payload Advertising payload.
*
* @note If advertising set is active you may only set payload of length equal or less
* than getMaxActiveSetAdvertisingDataLength(). If you require a longer payload you must
* stop the advertising set, set the payload and restart the set.
*
* @return BLE_ERROR_NONE on success.
*
* @see ble::AdvertisingDataBuilder to build a payload.
*/
ble_error_t setAdvertisingPayload(
advertising_handle_t handle,
mbed::Span<const uint8_t> payload
);
/** Set new advertising scan response for a given advertising set. This will be sent to
* device who perform active scanning.
*
* @param handle Advertising set handle.
* @param response Advertising scan response.
*
* @note If advertising set is active you may only set payload of length equal or less
* than getMaxActiveSetAdvertisingDataLength(). If you require a longer payload you must
* stop the advertising set, set the payload and restart the set.
*
* @return BLE_ERROR_NONE on success.
*
* @see ble::AdvertisingDataBuilder to build a payload.
*/
ble_error_t setAdvertisingScanResponse(
advertising_handle_t handle,
mbed::Span<const uint8_t> response
);
/** Start advertising using the given advertising set.
*
* @param handle Advertising set handle.
* @param maxDuration Max duration for advertising (in units of 10ms) - 0 means no limit.
* @param maxEvents Max number of events produced during advertising - 0 means no limit.
* @return BLE_ERROR_NONE on success.
*
* @see EventHandler::onAdvertisingStart when the advertising starts.
* @see EventHandler::onScanRequestReceived when a scan request is received.
* @see EventHandler::onAdvertisingEnd when the advertising ends.
* @see EventHandler::onConnectionComplete when the device gets connected
* by a peer.
*/
ble_error_t startAdvertising(
advertising_handle_t handle,
adv_duration_t maxDuration = adv_duration_t::forever(),
uint8_t maxEvents = 0
);
/** Stop advertising given advertising set. This is separate from periodic advertising
* which will not be affected.
*
* @param handle Advertising set handle.
* @return BLE_ERROR_NONE on success.
*/
ble_error_t stopAdvertising(advertising_handle_t handle);
/** Check if advertising is active for a given advertising set.
*
* @param handle Advertising set handle.
* @return True if advertising is active on this set.
*/
bool isAdvertisingActive(advertising_handle_t handle);
#endif // BLE_ROLE_BROADCASTER
#if BLE_ROLE_BROADCASTER
#if BLE_FEATURE_PERIODIC_ADVERTISING
/** Set periodic advertising parameters for a given advertising set.
*
* @param handle Advertising set handle.
* @param periodicAdvertisingIntervalMin Minimum interval for periodic advertising.
* @param periodicAdvertisingIntervalMax Maximum interval for periodic advertising.
* @param advertiseTxPower Include transmission power in the advertisements.
* @return BLE_ERROR_NONE on success.
*
* @version 5+
*/
ble_error_t setPeriodicAdvertisingParameters(
advertising_handle_t handle,
periodic_interval_t periodicAdvertisingIntervalMin,
periodic_interval_t periodicAdvertisingIntervalMax,
bool advertiseTxPower = true
);
/** Set new periodic advertising payload for a given advertising set.
*
* @param handle Advertising set handle.
* @param payload Advertising payload.
* @return BLE_ERROR_NONE on success.
*
* @note If advertising set is active you may only set payload of length equal or less
* than getMaxActiveSetAdvertisingDataLength(). If you require a longer payload you must
* stop the advertising set, set the payload and restart the set. Stopping the set will
* cause peers to lose sync on the periodic set.
*
* @see ble::AdvertisingDataBuilder to build a payload.
*
* @version 5+
*/
ble_error_t setPeriodicAdvertisingPayload(
advertising_handle_t handle,
mbed::Span<const uint8_t> payload
);
/** Start periodic advertising for a given set. Periodic advertising will not start until
* normal advertising is running but will continue to run after normal advertising has stopped.
*
* @param handle Advertising set handle.
* @return BLE_ERROR_NONE on success.
*
* @version 5+
*/
ble_error_t startPeriodicAdvertising(advertising_handle_t handle);
/** Stop periodic advertising for a given set.
*
* @param handle Advertising set handle.
* @return BLE_ERROR_NONE on success.
*
* @version 5+
*/
ble_error_t stopPeriodicAdvertising(advertising_handle_t handle);
/** Check if periodic advertising is active for a given advertising set.
*
* @param handle Advertising set handle.
* @return True if periodic advertising is active on this set.
*
* @version 5+
*/
bool isPeriodicAdvertisingActive(advertising_handle_t handle);
#endif // BLE_ROLE_BROADCASTER
#endif // BLE_FEATURE_PERIODIC_ADVERTISING
/* scanning */
#if BLE_ROLE_OBSERVER
/** Set new scan parameters.
*
* @param params Scan parameters, @see GapScanParameters for details.
* @return BLE_ERROR_NONE on success.
*/
ble_error_t setScanParameters(const ScanParameters ¶ms);
/** Start scanning.
*
* @param duration How long to scan for. Special value 0 means scan forever.
* @param filtering Filtering policy.
* @param period How long to scan for in single period. If the period is 0 and duration
* is nonzero the scan will last for single duration.
*
* @note When the duration and period parameters are non-zero scanning will last for
* the duration within the period. After the scan period has expired a new scan period
* will begin and scanning. This will repeat until stopScan() is called.
*
* @return BLE_ERROR_NONE on success.
*
* @see EventHandler::onAdvertisingReport to collect advertising reports.
* @see EventHandler::onScanTimeout when scanning timeout.
*/
ble_error_t startScan(
scan_duration_t duration = scan_duration_t::forever(),
duplicates_filter_t filtering = duplicates_filter_t::DISABLE,
scan_period_t period = scan_period_t(0)
);
/**
* Stop the ongoing scanning procedure.
*
* The current scanning parameters remain in effect.
*
* @retval BLE_ERROR_NONE if successfully stopped scanning procedure.
*/
ble_error_t stopScan();
#endif // BLE_ROLE_OBSERVER
#if BLE_ROLE_OBSERVER
#if BLE_FEATURE_PERIODIC_ADVERTISING
/** Synchronize with periodic advertising from an advertiser and begin receiving periodic
* advertising packets.
*
* @param peerAddressType Peer address type.
* @param peerAddress Peer address.
* @param sid Advertiser set identifier.
* @param maxPacketSkip Number of consecutive periodic advertising packets that the receiver
* may skip after successfully receiving a periodic advertising packet.
* @param timeout Maximum permitted time between successful receptions. If this time is
* exceeded, synchronisation is lost.
* @return BLE_ERROR_NONE on success.
*
* @see EventHandler::onPeriodicAdvertisingSyncEstablished when the sync is
* effective.
* @see EventHandler::onPeriodicAdvertisingReport when data are issued by the
* peer.
* @see EventHandler::onPeriodicAdvertisingSyncLoss when the sync has been
* loss.
*
* @version 5+
*/
ble_error_t createSync(
peer_address_type_t peerAddressType,
const address_t &peerAddress,
uint8_t sid,
slave_latency_t maxPacketSkip,
sync_timeout_t timeout
);
/** Synchronize with periodic advertising from an advertiser and begin receiving periodic
* advertising packets. Use periodic advertising sync list to determine who to sync with.
*
* @param maxPacketSkip Number of consecutive periodic advertising packets that the receiver
* may skip after successfully receiving a periodic advertising packet.
* @param timeout Maximum permitted time between successful receives.
* If this time is exceeded, synchronisation is lost.
* @return BLE_ERROR_NONE on success.
*
* @see EventHandler::onPeriodicAdvertisingSyncEstablished when the sync is
* effective.
* @see EventHandler::onPeriodicAdvertisingReport when data are issued by the
* peer.
* @see EventHandler::onPeriodicAdvertisingSyncLoss when the sync has been
* loss.
*
* @version 5+
*/
ble_error_t createSync(
slave_latency_t maxPacketSkip,
sync_timeout_t timeout
);
/** Cancel sync attempt.
*
* @return BLE_ERROR_NONE on success.
*/
ble_error_t cancelCreateSync();
/** Stop reception of the periodic advertising identified by the handle.
*
* @param handle Periodic advertising synchronisation handle.
* @return BLE_ERROR_NONE on success.
*/
ble_error_t terminateSync(periodic_sync_handle_t handle);
/** Add device to the periodic advertiser list. Cannot be called when sync is ongoing.
*
* @param peerAddressType Peer address type.
* @param peerAddress Peer address.
* @param sid Advertiser set identifier.
* @return BLE_ERROR_NONE on success.
*/
ble_error_t addDeviceToPeriodicAdvertiserList(
peer_address_type_t peerAddressType,
const address_t &peerAddress,
advertising_sid_t sid
);
/** Remove device from the periodic advertiser list. Cannot be called when sync is ongoing.
*
* @param peerAddressType Peer address type.
* @param peerAddress Peer address.
* @param sid Advertiser set identifier.
* @return BLE_ERROR_NONE on success.
*/
ble_error_t removeDeviceFromPeriodicAdvertiserList(
peer_address_type_t peerAddressType,
const address_t &peerAddress,
advertising_sid_t sid
);
/** Remove all devices from periodic advertiser list.
*
* @return BLE_ERROR_NONE on success.
*/
ble_error_t clearPeriodicAdvertiserList();
/** Get number of devices that can be added to the periodic advertiser list.
* @return Number of devices that can be added to the periodic advertiser list.
*/
uint8_t getMaxPeriodicAdvertiserListSize();
#endif // BLE_ROLE_OBSERVER
#endif // BLE_FEATURE_PERIODIC_ADVERTISING
#if BLE_ROLE_CENTRAL
/**
* Initiate a connection to a peer.
*
* Once the connection is established an onConnectionComplete in the event handler
* will be called.
*
* @param peerAddressType
* @param peerAddress
* @param connectionParams
*