Mistake on this page?
Report an issue in GitHub or email us
Gap.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2020 ARM Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef BLE_GAP_GAP_H
20 #define BLE_GAP_GAP_H
21 
22 #include "ble/common/CallChainOfFunctionPointersWithContext.h"
23 
24 #include "ble/common/BLERoles.h"
25 #include "ble/common/BLETypes.h"
26 #include "ble/gap/AdvertisingDataBuilder.h"
27 #include "ble/gap/AdvertisingDataParser.h"
28 #include "ble/gap/AdvertisingDataSimpleBuilder.h"
29 #include "ble/gap/AdvertisingDataTypes.h"
30 #include "ble/gap/AdvertisingParameters.h"
31 #include "ble/gap/ConnectionParameters.h"
32 #include "ble/gap/ScanParameters.h"
33 #include "ble/gap/Events.h"
34 #include "ble/gap/Types.h"
35 
36 namespace ble {
37 
38 #if !defined(DOXYGEN_ONLY)
39 namespace impl {
40 class Gap;
41 }
42 #endif // !defined(DOXYGEN_ONLY)
43 
44 /**
45  * @addtogroup ble
46  * @{
47  * @addtogroup gap
48  * @{
49  */
50 
51 /**
52  * Define device discovery, connection and link management procedures.
53  *
54  * - Device discovery: A device can advertise to nearby peers its existence,
55  * identity and capabilities. Similarly, a device can scan its environment to
56  * find advertising peers. The information acquired during the scan helps to
57  * identify peers and understand their use. A scanner may acquire more information
58  * about an advertising peer by sending a scan request. If the peer accepts scan
59  * requests, it may reply with additional information about its state.
60  *
61  * - Connection: A bluetooth device can establish a connection to a connectable
62  * advertising peer. Once the connection is established, both devices can
63  * communicate using the GATT protocol. The GATT protocol allows connected
64  * devices to expose a set of states that the other peer can discover, read and write.
65  *
66  * - Link Management: Connected devices may drop the connection and may adjust
67  * connection parameters according to the power envelop needed for their
68  * application.
69  *
70  * @par Accessing gap
71  *
72  * Instance of a Gap class for a given BLE device should be accessed using
73  * BLE::gap(). The reference returned remains valid until the BLE instance
74  * shut down (see BLE::shutdown()).
75  *
76  * @code
77  * // assuming ble_device has been initialized
78  * BLE& ble_device;
79  *
80  * ble::Gap& gap = ble_device.gap();
81  * @endcode
82  *
83  * @par Advertising
84  *
85  * Advertising consists of broadcasting at a regular interval a small amount of
86  * data containing valuable information about the device. These packets may be
87  * scanned by peer devices listening on BLE advertising channels.
88  *
89  * Scanners may also request additional information from a device advertising by
90  * sending a scan request. If the broadcaster accepts scan requests, it can reply
91  * with a scan response packet containing additional information.
92  *
93  * Advertising parameters are updated using setAdvertisingParams(). The main
94  * advertising payload is updated using setAdvertisingPayload(), and the scan response
95  * is updated using setAdvertisingScanResponse(). If the advertising is already
96  * updated, the data will take effect from the next advertising event.
97  *
98  * To create a valid advertising payload and scan response, you may use
99  * AdvertisingDataBuilder. You must first allocate memory and create an mbed::Span and
100  * pass that into the AdvertisingDataBuilder, which will only be able to add as much
101  * data as fits in the provided buffer. The builder accepts any size of the buffer,
102  * but for the created data to be usable, it must be smaller than the maximum data
103  * length returned from getMaxAdvertisingDataLength().
104  *
105  * Another option is to use AdvertisingDataSimpleBuilder, which allocates memory
106  * on the stack and offers a fluent interface at the expense of a reduced set of
107  * APIs and error management options.
108  *
109  * @note Prior to Bluetooth 5, advertising and scanning payload size were limited
110  * to LEGACY_ADVERTISING_MAX_SIZE. It changed with Bluetooth 5, and now the maximum
111  * size of data that can be advertised depends on the controller. If you wish
112  * to be compatible with older devices, you may wish to advertise with the
113  * LEGACY_ADVERTISING_HANDLE. This uses payloads no larger than LEGACY_ADVERTISING_MAX_SIZE
114  * with that advertising set.
115  *
116  * @par Extended advertising
117  *
118  * Extended advertising allows for a wider choice of options than legacy advertising.
119  * You can send bigger payloads and use different PHYs. This allows for bigger throughput
120  * or longer range.
121  *
122  * Extended advertising may be split across many packets and takes place on both the
123  * regular advertising channels and the rest of the 37 channels normally used by
124  * connected devices.
125  *
126  * The 3 channels used in legacy advertising are called primary advertisement channels.
127  * The remaining 37 channels are used for secondary advertising. Unlike sending data
128  * during a connection, this allows the device to broadcast data to multiple devices.
129  *
130  * The advertising starts on the primary channels (which you may select) and continues
131  * on the secondary channels as indicated in the packet sent on the primary channel.
132  * This way, the advertising can send large payloads without saturating the advertising
133  * channels. Primary channels are limited to 1M and coded PHYs, but secondary channels
134  * may use the increased throughput 2M PHY.
135  *
136  * @par Periodic advertising
137  *
138  * Similarly, you can use periodic advertising to transfer regular data to multiple
139  * devices.
140  *
141  * The advertiser uses primary channels to advertise the information needed to
142  * listen to the periodic advertisements on secondary channels. This sync information
143  * will be used by the scanner who can now optimize for power consumption and only
144  * listen for the periodic advertisements at specified times.
145  *
146  * Like extended advertising, periodic advertising offers extra PHY options of 2M
147  * and coded. The payload may be updated at any time and will be updated on the next
148  * advertisement event when the periodic advertising is active.
149  *
150  * @par Advertising sets
151  *
152  * Advertisers may advertise multiple payloads at the same time. The configuration
153  * and identification of these is done through advertising sets. Use a handle
154  * obtained from createAvertisingSet() for advertising operations. After ending
155  * all advertising operations, remove the handle from the system using
156  * destroyAdvertisingHandle().
157  *
158  * Extended advertising and periodic advertising is an optional feature, and not all
159  * devices support it. Some will only be able to see the now-called legacy advertising.
160  *
161  * Legacy advertising is available through a special handle, LEGACY_ADVERTISING_HANDLE.
162  * This handle is always available, doesn't need to be created and can't be
163  * destroyed.
164  *
165  * There is a limited number of advertising sets available because they require support
166  * from the controller. Their availability is dynamic and may be queried at any time
167  * using getMaxAdvertisingSetNumber(). Advertising sets take up resources even if
168  * they are not actively advertising right now, so it's important to destroy the set
169  * when you're done with it (or reuse it in the next advertisement).
170  *
171  * Periodic advertising and extended advertising share the same set but not the same
172  * data. Extended advertising carries out periodic advertising synchronization
173  * information. Therefore, to let other devices be aware that your device
174  * exposes periodic advertising, you should start extended advertising of the set.
175  * Subsequently, you may disable extended advertising, and the periodic advertising
176  * will continue. If you start periodic advertising while extended advertising is
177  * inactive, periodic advertising won't start until you start extended advertising
178  * at a later time.
179  *
180  * @par Privacy
181  *
182  * Privacy is a feature that allows a device to avoid being tracked by other
183  * (untrusted) devices. The device achieves it by periodically generating a
184  * new random address. The random address may be a resolvable random address,
185  * enabling trusted devices to recognize it as belonging to the same
186  * device. These trusted devices receive an Identity Resolution Key (IRK)
187  * during pairing. This is handled by the SecurityManager and relies on the
188  * other device accepting and storing the IRK.
189  *
190  * You need to enable privacy by calling enablePrivacy() after having
191  * initialized the SecurityManager because privacy requires SecurityManager
192  * to handle IRKs. The behavior of privacy enabled devices is set by
193  * using setCentralPrivacyConfiguration(), which specifies what the device
194  * should be with devices using random addresses. Random addresses
195  * generated by privacy enabled devices can be of two types: resolvable
196  * (by devices who have the IRK) and unresolvable. Unresolvable addresses
197  * can't be used for connecting and connectable advertising. Therefore, a
198  * resolvable one will be used for these regardless of the privacy
199  * configuration.
200  *
201  * @par Scanning
202  *
203  * Scanning consists of listening for peer advertising packets. From a scan, a
204  * device can identify devices available in its environment.
205  *
206  * If the device scans actively, then it will send scan request to scannable
207  * advertisers and collect their scan responses.
208  *
209  * Scanning is done by creating ScanParameters and applying them with
210  * setScanParameters(). One configured, you may call startScan().
211  *
212  * When a scanning device receives an advertising packet, it will call
213  * onAdvertisingReport() in the registered event handler. A whitelist may be used
214  * to limit the advertising reports by setting the correct policy in the scan
215  * parameters.
216  *
217  * @par Connection event handling
218  *
219  * A peer may connect device advertising connectable packets. The advertising
220  * procedure ends as soon as the device is connected. If an advertising timeout
221  * has been set in the advertising parameters then onAdvertisingEnd will be called
222  * in the registered eventHandler when it runs out.
223  *
224  * A device accepting a connection request from a peer is named a peripheral,
225  * and the device initiating the connection is named a central.
226  *
227  * Connection is initiated by central devices. A call to connect() will result in
228  * the device scanning on any PHYs set in ConectionParamters passed in.
229  *
230  * Peripheral and central receive a connection event when the connection is
231  * effective. If successful will result in a call to onConnectionComplete in the
232  * EventHandler registered with the Gap.
233  *
234  * It the connection attempt fails it will result in onConnectionComplete called
235  * on the central device with the event carrying the error flag.
236  *
237  * @par Changing the PHYsical transport of a connection
238  *
239  * Once a connection has been established, it is possible to change the physical
240  * transport used between the local and the distant device. Changing the transport
241  * can either increase the bandwidth or increase the communication range.
242  * An increased bandwidth equals a better power consumption but also a loss in
243  * sensibility and therefore a degraded range.
244  *
245  * Symmetrically an increased range means a lowered bandwidth and a degraded power
246  * consumption.
247  *
248  * Applications can change the PHY used by calling the function setPhy. Once the
249  * update has been made the result is forwarded to the application by calling the
250  * function onPhyUpdateComplete of the event handler registered.
251  *
252  * @par disconnection
253  *
254  * The application code initiates a disconnection when it calls the
255  * disconnect(Handle_t, DisconnectionReason_t) function.
256  *
257  * Disconnection may also be initiated by the remote peer or the local
258  * controller/stack. To catch all disconnection events, application code may
259  * set up an handler taking care of disconnection events by calling
260  * onDisconnection().
261  *
262  * @par Modulation Schemes
263  *
264  * When supported by the host and controller you can select different modulation
265  * schemes (@see BLUETOOTH SPECIFICATION Version 5.0 | Vol 1, Part A - 1.2):
266  * - LE 1M PHY
267  * - LE 2M PHY
268  * - LE coded PHY
269  *
270  * You may set preferred PHYs (separately for RX and TX) using setPreferredPhys().
271  * You may also set the currently used PHYs on a selected connection using setPhy().
272  * Both of these settings are only advisory and the controller is allowed to make
273  * its own decision on the best PHY to use based on your request, the peer's
274  * supported features and the connection's physical conditions.
275  *
276  * You may query the currently used PHY using readPhy() which will return the
277  * result through a call to the registered event handler. You may register the
278  * handler with setEventHandler(). The events inform about the currently used
279  * PHY and of any changes to PHYs which may be triggered autonomously by the
280  * controller or by the peer.
281  */
282 class Gap {
283 public:
284  /**
285  * Gap shutdown event handler.
286  *
287  * @see Gap::onShutdown().
288  */
290 
291  /**
292  * Callchain of gap shutdown event handler.
293  *
294  * @see Gap::onShutdown().
295  */
298 
299 public:
300  /**
301  * Definition of the general handler of Gap related events.
302  */
303  struct EventHandler {
304  /**
305  * Called when an advertising device receive a scan response.
306  *
307  * @param event Scan request event.
308  *
309  * @version: 5+.
310  *
311  * @see AdvertisingParameters::setScanRequestNotification().
312  */
313  virtual void onScanRequestReceived(const ScanRequestEvent &event)
314  {
315  }
316 
317  /**
318  * Called when advertising starts.
319  *
320  * @param event Advertising start event.
321  *
322  * @see startAdvertising()
323  */
324  virtual void onAdvertisingStart(const AdvertisingStartEvent &event)
325  {
326  }
327 
328  /**
329  * Called when advertising ends.
330  *
331  * Advertising ends when the process timeout or if it is stopped by the
332  * application or if the local device accepts a connection request.
333  *
334  * @param event Advertising end event.
335  *
336  * @see startAdvertising()
337  * @see stopAdvertising()
338  * @see onConnectionComplete()
339  */
340  virtual void onAdvertisingEnd(const AdvertisingEndEvent &event)
341  {
342  }
343 
344  /**
345  * Called when a scanner receives an advertising or a scan response packet.
346  *
347  * @param event Advertising report.
348  *
349  * @see startScan()
350  */
351  virtual void onAdvertisingReport(const AdvertisingReportEvent &event)
352  {
353  }
354 
355  /**
356  * Called when scan times out.
357  *
358  * @param event Associated event.
359  *
360  * @see startScan()
361  */
362  virtual void onScanTimeout(const ScanTimeoutEvent &event)
363  {
364  }
365 
366  /**
367  * Called when first advertising packet in periodic advertising is received.
368  *
369  * @param event Periodic advertising sync event.
370  *
371  * @version: 5+.
372  *
373  * @see createSync()
374  */
377  )
378  {
379  }
380 
381  /**
382  * Called when a periodic advertising packet is received.
383  *
384  * @param event Periodic advertisement event.
385  *
386  * @version: 5+.
387  *
388  * @see createSync()
389  */
391  const PeriodicAdvertisingReportEvent &event
392  )
393  {
394  }
395 
396  /**
397  * Called when a periodic advertising sync has been lost.
398  *
399  * @param event Details of the event.
400  *
401  * @version: 5+.
402  *
403  * @see createSync()
404  */
406  const PeriodicAdvertisingSyncLoss &event
407  )
408  {
409  }
410 
411  /**
412  * Called when connection attempt ends or an advertising device has been
413  * connected.
414  *
415  * @see startAdvertising()
416  * @see connect()
417  *
418  * @param event Connection event.
419  */
421  {
422  }
423 
424  /**
425  * Called when the peer request connection parameters updates.
426  *
427  * Application must accept the update with acceptConnectionParametersUpdate()
428  * or reject it with rejectConnectionParametersUpdate().
429  *
430  * @param event The connection parameters requested by the peer.
431  *
432  * @version 4.1+.
433  *
434  * @note This event is not generated if connection parameters update
435  * is managed by the middleware.
436  *
437  * @see manageConnectionParametersUpdateRequest()
438  * @see acceptConnectionParametersUpdate()
439  * @see rejectConnectionParametersUpdate()
440  */
443  )
444  {
445  }
446 
447  /**
448  * Called when connection parameters have been updated.
449  *
450  * @param event The new connection parameters.
451  *
452  * @see updateConnectionParameters()
453  * @see acceptConnectionParametersUpdate()
454  */
457  )
458  {
459  }
460 
461  /**
462  * Called when a connection has been disconnected.
463  *
464  * @param event Details of the event.
465  *
466  * @see disconnect()
467  */
469  {
470  }
471 
472  /**
473  * Function invoked when the current transmitter and receiver PHY have
474  * been read for a given connection.
475  *
476  * @param status Status of the operation: BLE_ERROR_NONE in case of
477  * success or an appropriate error code.
478  *
479  * @param connectionHandle: The handle of the connection for which the
480  * PHYs have been read.
481  *
482  * @param txPhy PHY used by the transmitter.
483  *
484  * @param rxPhy PHY used by the receiver.
485  *
486  * @see readPhy().
487  *
488  * @version: 5+.
489  */
490  virtual void onReadPhy(
491  ble_error_t status,
492  connection_handle_t connectionHandle,
493  phy_t txPhy,
494  phy_t rxPhy
495  )
496  {
497  }
498 
499  /**
500  * Function invoked when the update process of the PHY has been completed.
501  *
502  * The process can be initiated by a call to the function setPhy, the
503  * local bluetooth subsystem or the peer.
504  *
505  * @param status Status of the operation: BLE_ERROR_NONE in case of
506  * success or an appropriate error code.
507  *
508  * @param connectionHandle: The handle of the connection on which the
509  * operation was made.
510  *
511  * @param txPhy PHY used by the transmitter.
512  *
513  * @param rxPhy PHY used by the receiver.
514  *
515  * @note Success doesn't mean the PHY has been updated it means both
516  * ends have negotiated the best PHY according to their configuration and
517  * capabilities. The PHY currently used are present in the txPhy and
518  * rxPhy parameters.
519  *
520  * @see setPhy()
521  *
522  * @version: 5+.
523  */
524  virtual void onPhyUpdateComplete(
525  ble_error_t status,
526  connection_handle_t connectionHandle,
527  phy_t txPhy,
528  phy_t rxPhy
529  )
530  {
531  }
532 
533  /**
534  * Function invoked when the connections changes the maximum number of octets
535  * that can be sent or received by the controller in a single packet. A single
536  * L2CAP packet can be fragmented across many such packets.
537  *
538  * @note This only triggers if controller supports data length extension and
539  * negotiated data length is longer than the default 23.
540  *
541  * @param connectionHandle The handle of the connection that changed the size.
542  * @param txSize Number of octets we can send on this connection in a single packet.
543  * @param rxSize Number of octets we can receive on this connection in a single packet.
544  */
545  virtual void onDataLengthChange(
546  connection_handle_t connectionHandle,
547  uint16_t txSize,
548  uint16_t rxSize
549  )
550  {
551  }
552 
553  /**
554  * Function invoked when the privacy subsystem has been enabled and is
555  * ready to be used.
556  */
557  virtual void onPrivacyEnabled()
558  {
559  }
560  protected:
561  /**
562  * Prevent polymorphic deletion and avoid unnecessary virtual destructor
563  * as the Gap class will never delete the instance it contains.
564  */
565  ~EventHandler() = default;
566  };
567 
568  /**
569  * Preferred connection parameter display in Generic Access Service.
570  */
571  typedef struct {
572  /**
573  * Minimum interval between two connection events allowed for a
574  * connection.
575  *
576  * It shall be less than or equal to maxConnectionInterval. This value,
577  * in units of 1.25ms, is included in the range [0x0006 : 0x0C80].
578  */
580 
581  /**
582  * Maximum interval between two connection events allowed for a
583  * connection.
584  *
585  * It shall be greater than or equal to minConnectionInterval. This
586  * value is in unit of 1.25ms and is in the range [0x0006 : 0x0C80].
587  */
589 
590  /**
591  * Number of connection events the slave can drop if it has nothing to
592  * communicate to the master.
593  *
594  * This value shall be in the range [0x0000 : 0x01F3].
595  */
596  uint16_t slaveLatency;
597 
598  /**
599  * Link supervision timeout for the connection.
600  *
601  * Time after which the connection is considered lost if the device
602  * didn't receive a packet from its peer.
603  *
604  * It is larger than:
605  * (1 + slaveLatency) * maxConnectionInterval * 2
606  *
607  * This value is in the range [0x000A : 0x0C80] and is in unit of
608  * 10 ms.
609  *
610  * @note maxConnectionInterval is in ms in the formulae above.
611  */
614 
615  /**
616  * Assign the event handler implementation that will be used by the gap
617  * module to signal events back to the application.
618  *
619  * @param handler Application implementation of an EventHandler.
620  *
621  * @note Multiple discrete EventHandler instances may be used by adding them
622  * to a ChainableGapEventHandler and then setting the chain as the primary
623  * Gap EventHandler using this function.
624  *
625  * @see ChainableGapEventHandler
626  */
627  void setEventHandler(EventHandler *handler);
628 
629  /** Check controller support for a specific feature.
630  *
631  * @param feature Feature to check.
632  * @return True if feature is supported.
633  */
634  bool isFeatureSupported(controller_supported_features_t feature);
635 
636  /* advertising */
637 #if BLE_ROLE_BROADCASTER
638  /** Return currently available number of supported advertising sets.
639  * This may change at runtime.
640  *
641  * @note Devices that do not support Bluetooth 5 still offers one advertising
642  * set that has the handle LEGACY_ADVERTISING_HANDLE.
643  *
644  * @return Number of advertising sets that may be created at the same time.
645  */
646  uint8_t getMaxAdvertisingSetNumber();
647 
648  /** Return maximum advertising data length supported.
649  *
650  * @return Maximum advertising data length supported.
651  */
652  uint16_t getMaxAdvertisingDataLength();
653 
654  /** Return maximum advertising data length supported for connectable advertising.
655  *
656  * @return Maximum advertising data length supported for connectable advertising.
657  */
658  uint16_t getMaxConnectableAdvertisingDataLength();
659 
660  /** Return maximum advertising data length you may set if advertising set is active.
661  *
662  * @return Maximum advertising data length you may set if advertising set is active.
663  */
664  uint16_t getMaxActiveSetAdvertisingDataLength();
665 
666 #if BLE_FEATURE_EXTENDED_ADVERTISING
667  /** Create an advertising set and apply the passed in parameters. The handle returned
668  * by this function must be used for all other calls that accept an advertising handle.
669  * When done with advertising, remove from the system using destroyAdvertisingSet().
670  *
671  * @note The exception is the LEGACY_ADVERTISING_HANDLE which may be used at any time.
672  *
673  * @param[out] handle Advertising handle returned, valid only if function returned success.
674  * @param parameters Advertising parameters for the newly created set.
675  * @return BLE_ERROR_NONE on success.
676  *
677  * @version 5+
678  */
679  ble_error_t createAdvertisingSet(
680  advertising_handle_t *handle,
681  const AdvertisingParameters &parameters
682  );
683 
684  /** Remove the advertising set (resets its set parameters). The advertising set must not
685  * be active.
686  *
687  * @note LEGACY_ADVERTISING_HANDLE may not be destroyed.
688  *
689  * @param handle Advertising set handle.
690  * @return BLE_ERROR_NONE on success.
691  *
692  * @version 5+
693  */
694  ble_error_t destroyAdvertisingSet(advertising_handle_t handle);
695 #endif // BLE_FEATURE_EXTENDED_ADVERTISING
696 
697  /** Set advertising parameters of an existing set.
698  *
699  * @param handle Advertising set handle.
700  * @param params New advertising parameters.
701  * @return BLE_ERROR_NONE on success.
702  */
703  ble_error_t setAdvertisingParameters(
704  advertising_handle_t handle,
705  const AdvertisingParameters &params
706  );
707 
708  /** Set new advertising payload for a given advertising set.
709  *
710  * @param handle Advertising set handle.
711  * @param payload Advertising payload.
712  *
713  * @note If advertising set is active you may only set payload of length equal or less
714  * than getMaxActiveSetAdvertisingDataLength(). If you require a longer payload you must
715  * stop the advertising set, set the payload and restart the set.
716  *
717  * @return BLE_ERROR_NONE on success.
718  *
719  * @see ble::AdvertisingDataBuilder to build a payload.
720  */
721  ble_error_t setAdvertisingPayload(
722  advertising_handle_t handle,
724  );
725 
726  /** Set new advertising scan response for a given advertising set. This will be sent to
727  * device who perform active scanning.
728  *
729  * @param handle Advertising set handle.
730  * @param response Advertising scan response.
731  *
732  * @note If advertising set is active you may only set payload of length equal or less
733  * than getMaxActiveSetAdvertisingDataLength(). If you require a longer payload you must
734  * stop the advertising set, set the payload and restart the set.
735  *
736  * @return BLE_ERROR_NONE on success.
737  *
738  * @see ble::AdvertisingDataBuilder to build a payload.
739  */
740  ble_error_t setAdvertisingScanResponse(
741  advertising_handle_t handle,
743  );
744 
745  /** Start advertising using the given advertising set.
746  *
747  * @param handle Advertising set handle.
748  * @param maxDuration Max duration for advertising (in units of 10ms) - 0 means no limit.
749  * @param maxEvents Max number of events produced during advertising - 0 means no limit.
750  * @return BLE_ERROR_NONE on success. This does not guarantee the set has started if
751  * extended advertising is enabled. Register an event handler and wait for onAdvertisingStart
752  * event. An (unlikely) failed start the status of the event will contain an error.
753  *
754  * @see EventHandler::onAdvertisingStart when the advertising starts.
755  * @see EventHandler::onScanRequestReceived when a scan request is received.
756  * @see EventHandler::onAdvertisingEnd when the advertising ends.
757  * @see EventHandler::onConnectionComplete when the device gets connected
758  * by a peer.
759  */
760  ble_error_t startAdvertising(
761  advertising_handle_t handle,
762  adv_duration_t maxDuration = adv_duration_t::forever(),
763  uint8_t maxEvents = 0
764  );
765 
766  /** Stop advertising given advertising set. This is separate from periodic advertising
767  * which will not be affected.
768  *
769  * @param handle Advertising set handle.
770  * @return BLE_ERROR_NONE on success. For extented advertising this does not guarantee
771  * the set is stopped if. Register an event handler and wait for onAdvertisingEnd event.
772  * An (unlikely) failed stop the event status will contain the error code.
773  */
774  ble_error_t stopAdvertising(advertising_handle_t handle);
775 
776  /** Check if advertising is active for a given advertising set.
777  *
778  * @param handle Advertising set handle.
779  * @return True if advertising is active on this set.
780  */
781  bool isAdvertisingActive(advertising_handle_t handle);
782 #endif // BLE_ROLE_BROADCASTER
783 
784 #if BLE_ROLE_BROADCASTER
785 #if BLE_FEATURE_PERIODIC_ADVERTISING
786  /** Set periodic advertising parameters for a given advertising set.
787  *
788  * @param handle Advertising set handle.
789  * @param periodicAdvertisingIntervalMin Minimum interval for periodic advertising.
790  * @param periodicAdvertisingIntervalMax Maximum interval for periodic advertising.
791  * @param advertiseTxPower Include transmission power in the advertisements.
792  * @return BLE_ERROR_NONE on success.
793  *
794  * @version 5+
795  */
796  ble_error_t setPeriodicAdvertisingParameters(
797  advertising_handle_t handle,
798  periodic_interval_t periodicAdvertisingIntervalMin,
799  periodic_interval_t periodicAdvertisingIntervalMax,
800  bool advertiseTxPower = true
801  );
802 
803  /** Set new periodic advertising payload for a given advertising set.
804  *
805  * @param handle Advertising set handle.
806  * @param payload Advertising payload.
807  * @return BLE_ERROR_NONE on success.
808  *
809  * @note If advertising set is active you may only set payload of length equal or less
810  * than getMaxActiveSetAdvertisingDataLength(). If you require a longer payload you must
811  * stop the advertising set, set the payload and restart the set. Stopping the set will
812  * cause peers to lose sync on the periodic set.
813  *
814  * @see ble::AdvertisingDataBuilder to build a payload.
815  *
816  * @version 5+
817  */
818  ble_error_t setPeriodicAdvertisingPayload(
819  advertising_handle_t handle,
821  );
822 
823  /** Start periodic advertising for a given set. Periodic advertising will not start until
824  * normal advertising is running but will continue to run after normal advertising has stopped.
825  *
826  * @param handle Advertising set handle.
827  * @return BLE_ERROR_NONE on success.
828  *
829  * @version 5+
830  */
831  ble_error_t startPeriodicAdvertising(advertising_handle_t handle);
832 
833  /** Stop periodic advertising for a given set.
834  *
835  * @param handle Advertising set handle.
836  * @return BLE_ERROR_NONE on success.
837  *
838  * @version 5+
839  */
840  ble_error_t stopPeriodicAdvertising(advertising_handle_t handle);
841 
842  /** Check if periodic advertising is active for a given advertising set.
843  *
844  * @param handle Advertising set handle.
845  * @return True if periodic advertising is active on this set.
846  *
847  * @version 5+
848  */
849  bool isPeriodicAdvertisingActive(advertising_handle_t handle);
850 #endif // BLE_ROLE_BROADCASTER
851 #endif // BLE_FEATURE_PERIODIC_ADVERTISING
852 
853  /* scanning */
854 #if BLE_ROLE_OBSERVER
855  /** Set new scan parameters.
856  *
857  * @param params Scan parameters, @see GapScanParameters for details.
858  * @return BLE_ERROR_NONE on success.
859  */
860  ble_error_t setScanParameters(const ScanParameters &params);
861 
862  /** Start scanning.
863  *
864  * @param duration How long to scan for. Special value 0 means scan forever.
865  * @param filtering Filtering policy.
866  * @param period How long to scan for in single period. If the period is 0 and duration
867  * is nonzero the scan will last for single duration.
868  *
869  * @note When the duration and period parameters are non-zero scanning will last for
870  * the duration within the period. After the scan period has expired a new scan period
871  * will begin and scanning. This will repeat until stopScan() is called.
872  *
873  * @return BLE_ERROR_NONE on success.
874  *
875  * @see EventHandler::onAdvertisingReport to collect advertising reports.
876  * @see EventHandler::onScanTimeout when scanning timeout.
877  */
878  ble_error_t startScan(
879  scan_duration_t duration = scan_duration_t::forever(),
880  duplicates_filter_t filtering = duplicates_filter_t::DISABLE,
881  scan_period_t period = scan_period_t(0)
882  );
883 
884  /**
885  * Stop the ongoing scanning procedure.
886  *
887  * The current scanning parameters remain in effect.
888  *
889  * @retval BLE_ERROR_NONE if successfully stopped scanning procedure.
890  */
891  ble_error_t stopScan();
892 #endif // BLE_ROLE_OBSERVER
893 
894 #if BLE_ROLE_OBSERVER
895 #if BLE_FEATURE_PERIODIC_ADVERTISING
896  /** Synchronize with periodic advertising from an advertiser and begin receiving periodic
897  * advertising packets.
898  *
899  * @param peerAddressType Peer address type.
900  * @param peerAddress Peer address.
901  * @param sid Advertiser set identifier.
902  * @param maxPacketSkip Number of consecutive periodic advertising packets that the receiver
903  * may skip after successfully receiving a periodic advertising packet.
904  * @param timeout Maximum permitted time between successful receptions. If this time is
905  * exceeded, synchronisation is lost.
906  * @return BLE_ERROR_NONE on success.
907  *
908  * @see EventHandler::onPeriodicAdvertisingSyncEstablished when the sync is
909  * effective.
910  * @see EventHandler::onPeriodicAdvertisingReport when data are issued by the
911  * peer.
912  * @see EventHandler::onPeriodicAdvertisingSyncLoss when the sync has been
913  * loss.
914  *
915  * @version 5+
916  */
917  ble_error_t createSync(
918  peer_address_type_t peerAddressType,
919  const address_t &peerAddress,
920  uint8_t sid,
921  slave_latency_t maxPacketSkip,
922  sync_timeout_t timeout
923  );
924 
925  /** Synchronize with periodic advertising from an advertiser and begin receiving periodic
926  * advertising packets. Use periodic advertising sync list to determine who to sync with.
927  *
928  * @param maxPacketSkip Number of consecutive periodic advertising packets that the receiver
929  * may skip after successfully receiving a periodic advertising packet.
930  * @param timeout Maximum permitted time between successful receives.
931  * If this time is exceeded, synchronisation is lost.
932  * @return BLE_ERROR_NONE on success.
933  *
934  * @see EventHandler::onPeriodicAdvertisingSyncEstablished when the sync is
935  * effective.
936  * @see EventHandler::onPeriodicAdvertisingReport when data are issued by the
937  * peer.
938  * @see EventHandler::onPeriodicAdvertisingSyncLoss when the sync has been
939  * loss.
940  *
941  * @version 5+
942  */
943  ble_error_t createSync(
944  slave_latency_t maxPacketSkip,
945  sync_timeout_t timeout
946  );
947 
948  /** Cancel sync attempt.
949  *
950  * @return BLE_ERROR_NONE on success.
951  */
952  ble_error_t cancelCreateSync();
953 
954  /** Stop reception of the periodic advertising identified by the handle.
955  *
956  * @param handle Periodic advertising synchronisation handle.
957  * @return BLE_ERROR_NONE on success.
958  */
959  ble_error_t terminateSync(periodic_sync_handle_t handle);
960 
961  /** Add device to the periodic advertiser list. Cannot be called when sync is ongoing.
962  *
963  * @param peerAddressType Peer address type.
964  * @param peerAddress Peer address.
965  * @param sid Advertiser set identifier.
966  * @return BLE_ERROR_NONE on success.
967  */
968  ble_error_t addDeviceToPeriodicAdvertiserList(
969  peer_address_type_t peerAddressType,
970  const address_t &peerAddress,
971  advertising_sid_t sid
972  );
973 
974  /** Remove device from the periodic advertiser list. Cannot be called when sync is ongoing.
975  *
976  * @param peerAddressType Peer address type.
977  * @param peerAddress Peer address.
978  * @param sid Advertiser set identifier.
979  * @return BLE_ERROR_NONE on success.
980  */
981  ble_error_t removeDeviceFromPeriodicAdvertiserList(
982  peer_address_type_t peerAddressType,
983  const address_t &peerAddress,
984  advertising_sid_t sid
985  );
986 
987  /** Remove all devices from periodic advertiser list.
988  *
989  * @return BLE_ERROR_NONE on success.
990  */
991  ble_error_t clearPeriodicAdvertiserList();
992 
993  /** Get number of devices that can be added to the periodic advertiser list.
994  * @return Number of devices that can be added to the periodic advertiser list.
995  */
996  uint8_t getMaxPeriodicAdvertiserListSize();
997 #endif // BLE_ROLE_OBSERVER
998 #endif // BLE_FEATURE_PERIODIC_ADVERTISING
999 
1000 #if BLE_ROLE_CENTRAL
1001  /**
1002  * Initiate a connection to a peer.
1003  *
1004  * Once the connection is established an onConnectionComplete in the event handler
1005  * will be called.
1006  *
1007  * @param peerAddressType
1008  * @param peerAddress
1009  * @param connectionParams
1010  *
1011  * @return BLE_ERROR_NONE if connection establishment procedure is started
1012  * successfully. The connectionCallChain (if set) is invoked upon
1013  * a connection event.
1014  *
1015  * @see EventHandler::onConnectionComplete will be called whether the
1016  * connection process succeed or fail.
1017  * @see EventHandler::onDisconnectionComplete is called when the connection
1018  * ends.
1019  */
1020  ble_error_t connect(
1021  peer_address_type_t peerAddressType,
1022  const address_t &peerAddress,
1023  const ConnectionParameters &connectionParams
1024  );
1025 
1026  /** Cancel the connection attempt. This is not guaranteed to succeed. As a result
1027  * onConnectionComplete in the event handler will be called. Check the success parameter
1028  * to see if the connection was created.
1029  *
1030  * @return BLE_ERROR_NONE if the connection attempt has been requested to be cancelled.
1031  * Returns BLE_ERROR_OPERATION_NOT_PERMITTED if no ongoing connection for last used address found.
1032  */
1033  ble_error_t cancelConnect();
1034 #endif // BLE_ROLE_CENTRAL
1035 
1036 #if BLE_FEATURE_CONNECTABLE
1037  /**
1038  * Update connection parameters of an existing connection.
1039  *
1040  * In the central role, this initiates a Link Layer connection parameter
1041  * update procedure. In the peripheral role, this sends the corresponding
1042  * L2CAP request and waits for the central to accept or reject the requested
1043  * connection parameters.
1044  *
1045  * @param connectionHandle The handle of the connection to update.
1046  * @param minConnectionInterval The minimum connection interval requested.
1047  * @param maxConnectionInterval The maximum connection interval requested.
1048  * @param slaveLatency The slave latency requested.
1049  * @param supervision_timeout The supervision timeout requested.
1050  * @param minConnectionEventLength The minimum connection event length requested.
1051  * @param maxConnectionEventLength The maximum connection event length requested.
1052  *
1053  * @return BLE_ERROR_NONE if the request has been sent and false otherwise.
1054  *
1055  * @see EventHandler::onUpdateConnectionParametersRequest when a central
1056  * receives a request to update the connection parameters.
1057  * @see EventHandler::onConnectionParametersUpdateComplete when connection
1058  * parameters have been updated.
1059  *
1060  * @version 4.0+ for central
1061  * @version 4.1+ for peripheral
1062  */
1063  ble_error_t updateConnectionParameters(
1064  connection_handle_t connectionHandle,
1065  conn_interval_t minConnectionInterval,
1066  conn_interval_t maxConnectionInterval,
1067  slave_latency_t slaveLatency,
1068  supervision_timeout_t supervision_timeout,
1069  conn_event_length_t minConnectionEventLength = conn_event_length_t(0),
1070  conn_event_length_t maxConnectionEventLength = conn_event_length_t(0)
1071  );
1072 
1073  /**
1074  * Allows the application to accept or reject a connection parameters update
1075  * request.
1076  *
1077  * If this process is managed by the middleware; new connection parameters
1078  * from a slave are always accepted.
1079  *
1080  * @param userManageConnectionUpdateRequest true to let the application
1081  * manage the process and false to let the middleware manage it.
1082  *
1083  * @return BLE_ERROR_NONE in case of success or an appropriate error code.
1084  *
1085  * @version 4.1+
1086  *
1087  * @see EventHandler::onUpdateConnectionParametersRequest when a central
1088  * receives a request to update the connection parameters.
1089  *
1090  * @see acceptConnectionParametersUpdate to accept the request.
1091  * @see rejectConnectionParametersUpdate to reject the request.
1092  */
1093  ble_error_t manageConnectionParametersUpdateRequest(
1094  bool userManageConnectionUpdateRequest
1095  );
1096 
1097  /**
1098  * Accept update of the connection parameters.
1099  *
1100  * The central can adjust the new connection parameters.
1101  *
1102  * @param connectionHandle The handle of the connection that has initiated
1103  * the request.
1104  * @param minConnectionInterval The minimum connection interval to be applied.
1105  * @param maxConnectionInterval The maximum connection interval to be applied.
1106  * @param slaveLatency The slave latency to be applied.
1107  * @param supervision_timeout The supervision timeout to be applied.
1108  * @param minConnectionEventLength The minimum connection event length to be
1109  * applied.
1110  * @param maxConnectionEventLength The maximum connection event length to be
1111  * applied.
1112  *
1113  * @return BLE_ERROR_NONE in case of success or an appropriate error code.
1114  *
1115  * @version 4.1+
1116  *
1117  * @see manageConnectionParametersUpdateRequest To let the application
1118  * manage the process.
1119  *
1120  * @see EventHandler::onUpdateConnectionParametersRequest Called when a
1121  * request to update the connection parameters is received.
1122  *
1123  * @see EventHandler::onConnectionParametersUpdateComplete Called when the
1124  * new connection parameters are effective.
1125  */
1126  ble_error_t acceptConnectionParametersUpdate(
1127  connection_handle_t connectionHandle,
1128  conn_interval_t minConnectionInterval,
1129  conn_interval_t maxConnectionInterval,
1130  slave_latency_t slaveLatency,
1131  supervision_timeout_t supervision_timeout,
1132  conn_event_length_t minConnectionEventLength = conn_event_length_t(0),
1133  conn_event_length_t maxConnectionEventLength = conn_event_length_t(0)
1134  );
1135 
1136  /**
1137  * Reject a request to change the connection parameters.
1138  *
1139  * @param connectionHandle The handle of the connection that has initiated
1140  * the request.
1141  *
1142  * @return BLE_ERROR_NONE in case of success or an appropriate error code.
1143  *
1144  * @version 4.1+
1145  *
1146  * @see manageConnectionParametersUpdateRequest To let the application
1147  * manage the process.
1148  *
1149  * @see EventHandler::onUpdateConnectionParametersRequest Called when a
1150  * request to update the connection parameters is received.
1151  */
1152  ble_error_t rejectConnectionParametersUpdate(
1153  connection_handle_t connectionHandle
1154  );
1155 
1156  /**
1157  * Initiate a disconnection procedure.
1158  *
1159  * Once the disconnection procedure has completed a
1160  * DisconnectionCallbackParams_t, the event is emitted to handlers that
1161  * have been registered with onDisconnection().
1162  *
1163  * @param[in] reason Reason of the disconnection transmitted to the peer.
1164  * @param[in] connectionHandle Handle of the connection to end.
1165  *
1166  * @return BLE_ERROR_NONE if the disconnection procedure successfully
1167  * started.
1168  *
1169  * @see EventHandler::onDisconnectionComplete when the disconnection is
1170  * effective.
1171  */
1172  ble_error_t disconnect(
1173  connection_handle_t connectionHandle,
1174  local_disconnection_reason_t reason
1175  );
1176 #endif // BLE_FEATURE_CONNECTABLE
1177 #if BLE_FEATURE_PHY_MANAGEMENT
1178  /**
1179  * Read the PHY used by the transmitter and the receiver on a connection.
1180  *
1181  * Once the PHY has been read, it is reported back via the function onPhyRead
1182  * of the event handler registered by the application.
1183  *
1184  * @param connection Handle of the connection for which the PHY being used is
1185  * queried.
1186  *
1187  * @return BLE_ERROR_NONE if the read PHY procedure has been started or an
1188  * appropriate error code.
1189  *
1190  * @version 5+
1191  *
1192  * @see EventHandler::onReadPhy is called when the phy has been read.
1193  */
1194  ble_error_t readPhy(connection_handle_t connection);
1195 
1196  /**
1197  * Set the preferred PHYs to use in a connection.
1198  *
1199  * @param txPhys: Set of PHYs preferred for tx operations. If NULL then no
1200  * preferred PHYs are set and the default value of the subsystem is used.
1201  *
1202  * @param rxPhys: Set of PHYs preferred for rx operations. If NULL then no
1203  * preferred PHYs are set and the default value of the subsystem is used.
1204  *
1205  * @return BLE_ERROR_NONE if the preferences have been set or an appropriate
1206  * error code.
1207  *
1208  * @version 5+
1209  */
1210  ble_error_t setPreferredPhys(
1211  const phy_set_t *txPhys,
1212  const phy_set_t *rxPhys
1213  );
1214 
1215  /**
1216  * Update the PHY used by a connection.
1217  *
1218  * Once the update process has been completed, it is reported back to the
1219  * application via the function onPhyUpdateComplete of the event handler
1220  * registered by the application.
1221  *
1222  * @param connection Handle of the connection to update.
1223  *
1224  * @param txPhys Set of PHYs preferred for tx operations. If NULL then the
1225  * choice is up to the Bluetooth subsystem.
1226  *
1227  * @param rxPhys Set of PHYs preferred for rx operations. If NULL then the
1228  * choice is up to the Bluetooth subsystem.
1229  *
1230  * @param codedSymbol Number of symbols used to code a bit when le coded is
1231  * used. If the value is UNDEFINED then the choice is up to the Bluetooth
1232  * subsystem.
1233  *
1234  * @return BLE_ERROR_NONE if the update PHY procedure has been successfully
1235  * started or an error code.
1236  *
1237  * @see EventHandler::onPhyUpdateComplete is called when the phy used by the
1238  * connection has been updated.
1239  */
1240  ble_error_t setPhy(
1241  connection_handle_t connection,
1242  const phy_set_t *txPhys,
1243  const phy_set_t *rxPhys,
1244  coded_symbol_per_bit_t codedSymbol
1245  );
1246 #endif // BLE_FEATURE_PHY_MANAGEMENT
1247 
1248 #if BLE_FEATURE_PRIVACY
1249  /**
1250  * Enable or disable privacy mode of the local device.
1251  *
1252  * When privacy is enabled, the system use private addresses while it scans,
1253  * advertises or initiate a connection. The device private address is
1254  * renewed every 15 minutes.
1255  *
1256  * @par Configuration
1257  *
1258  * The privacy feature can be configured with the help of the functions
1259  * setPeripheralPrivacyConfiguration and setCentralPrivacyConfiguration
1260  * which respectively set the privacy configuration of the peripheral and
1261  * central role.
1262  *
1263  * @par Default configuration of peripheral role
1264  *
1265  * By default private resolvable addresses are used for all procedures;
1266  * including advertisement of nonconnectable packets. Connection request
1267  * from an unknown initiator with a private resolvable address triggers the
1268  * pairing procedure.
1269  *
1270  * @par Default configuration of central role
1271  *
1272  * By default private resolvable addresses are used for all procedures;
1273  * including active scanning. Addresses present in advertisement packet are
1274  * resolved and advertisement packets are forwarded to the application
1275  * even if the advertiser private address is unknown.
1276  *
1277  * @par Initialization of the privacy subsystem
1278  *
1279  * When privacy is enabled, the system generates new resolvable and non
1280  * resolvable private addresses. Scan, Advertising and Connecting to a peer
1281  * won't be available until the generation process completes. When addresses
1282  * have been generated, the application is notified that privacy
1283  * initialisation as completed with a call to EventHandler::onPrivacyEnabled .
1284  *
1285  * @param[in] enable Should be set to true to enable the privacy mode and
1286  * false to disable it.
1287  *
1288  * @return BLE_ERROR_NONE in case of success or an appropriate error code.
1289  */
1290  ble_error_t enablePrivacy(bool enable);
1291 
1292 #if BLE_ROLE_BROADCASTER
1293  /**
1294  * Set the privacy configuration used by the peripheral role.
1295  *
1296  * @param[in] configuration The configuration to set.
1297  *
1298  * @return BLE_ERROR_NONE in case of success or an appropriate error code.
1299  */
1300  ble_error_t setPeripheralPrivacyConfiguration(
1301  const peripheral_privacy_configuration_t *configuration
1302  );
1303 
1304  /**
1305  * Get the privacy configuration used by the peripheral role.
1306  *
1307  * @param[out] configuration The variable filled with the current
1308  * configuration.
1309  *
1310  * @return BLE_ERROR_NONE in case of success or an appropriate error code.
1311  */
1312  ble_error_t getPeripheralPrivacyConfiguration(
1313  peripheral_privacy_configuration_t *configuration
1314  );
1315 #endif // BLE_ROLE_BROADCASTER
1316 
1317 #if BLE_ROLE_OBSERVER
1318  /**
1319  * Set the privacy configuration used by the central role.
1320  *
1321  * @param[in] configuration The configuration to set.
1322  *
1323  * @return BLE_ERROR_NONE in case of success or an appropriate error code.
1324  */
1325  ble_error_t setCentralPrivacyConfiguration(
1326  const central_privacy_configuration_t *configuration
1327  );
1328 
1329  /**
1330  * Get the privacy configuration used by the central role.
1331  *
1332  * @param[out] configuration The variable filled with the current
1333  * configuration.
1334  *
1335  * @return BLE_ERROR_NONE in case of success or an appropriate error code.
1336  */
1337  ble_error_t getCentralPrivacyConfiguration(
1338  central_privacy_configuration_t *configuration
1339  );
1340 #endif // BLE_ROLE_OBSERVER
1341 #endif // BLE_FEATURE_PRIVACY
1342 
1343 #if BLE_FEATURE_WHITELIST
1344  /**
1345  * Get the maximum size of the whitelist.
1346  *
1347  * @return Maximum size of the whitelist.
1348  */
1349  uint8_t getMaxWhitelistSize() const;
1350 
1351  /**
1352  * Get the Link Layer to use the internal whitelist when scanning,
1353  * advertising or initiating a connection depending on the filter policies.
1354  *
1355  * @param[in,out] whitelist Define the whitelist instance which is used
1356  * to store the whitelist requested. In input, the caller provisions memory.
1357  *
1358  * @return BLE_ERROR_NONE if the implementation's whitelist was successfully
1359  * copied into the supplied reference.
1360  */
1361  ble_error_t getWhitelist(whitelist_t &whitelist) const;
1362 
1363  /**
1364  * Set the value of the whitelist to be used during GAP procedures.
1365  *
1366  * @param[in] whitelist A reference to a whitelist containing the addresses
1367  * to be copied to the internal whitelist.
1368  *
1369  * @return BLE_ERROR_NONE if the implementation's whitelist was successfully
1370  * populated with the addresses in the given whitelist.
1371  *
1372  * @note The whitelist must not contain non-resolvable addresses. This
1373  * results in a @ref BLE_ERROR_INVALID_PARAM because the remote peer might
1374  * change its private address at any time, and it is not possible to resolve
1375  * it.
1376  *
1377  * @note If the input whitelist is larger than @ref getMaxWhitelistSize(),
1378  * then @ref BLE_ERROR_PARAM_OUT_OF_RANGE is returned.
1379  */
1380  ble_error_t setWhitelist(const whitelist_t &whitelist);
1381 
1382 #endif // BLE_FEATURE_WHITELIST
1383 
1384  /**
1385  * Fetch the current address and its type.
1386  *
1387  * @param[out] typeP Type of the current address set.
1388  * @param[out] address Value of the current address.
1389  *
1390  * @note If privacy is enabled the device address may be unavailable to
1391  * application code.
1392  *
1393  * @return BLE_ERROR_NONE on success.
1394  */
1395  ble_error_t getAddress(
1396  own_address_type_t &typeP,
1397  address_t &address
1398  );
1399 
1400  /**
1401  * Return the type of a random address.
1402  *
1403  * @param[in] address The random address to retrieve the type from. The
1404  * address must be ordered in little endian.
1405  *
1406  * @param[out] addressType Type of the address to fill.
1407  *
1408  * @return BLE_ERROR_NONE in case of success or BLE_ERROR_INVALID_PARAM if
1409  * the address in input was not identifiable as a random address.
1410  */
1411  static ble_error_t getRandomAddressType(
1412  ble::address_t address,
1413  ble::random_address_type_t *addressType
1414  );
1415 
1416  /**
1417  * Reset the Gap instance.
1418  *
1419  * Reset process starts by notifying all registered shutdown event handlers
1420  * that the Gap instance is about to be shut down. Then, it clears all Gap state
1421  * of the associated object and then cleans the state present in the vendor
1422  * implementation.
1423  *
1424  * This function is meant to be overridden in the platform-specific
1425  * subclass. Nevertheless, the subclass only resets its
1426  * state and not the data held in Gap members. This is achieved by a
1427  * call to Gap::reset() from the subclass' reset() implementation.
1428  *
1429  * @return BLE_ERROR_NONE on success.
1430  *
1431  * @note Currently, a call to reset() does not reset the advertising and
1432  * scan parameters to default values.
1433  */
1434  ble_error_t reset();
1435 
1436  /**
1437  * Register a Gap shutdown event handler.
1438  *
1439  * The handler is called when the Gap instance is about to shut down.
1440  * It is usually issued after a call to BLE::shutdown().
1441  *
1442  * @param[in] callback Shutdown event handler to register.
1443  *
1444  * @note To unregister a shutdown event handler, use
1445  * onShutdown().detach(callback).
1446  */
1447  void onShutdown(const GapShutdownCallback_t &callback);
1448 
1449  /**
1450  * Register a Gap shutdown event handler.
1451  *
1452  * @param[in] objPtr Instance used to invoke @p memberPtr.
1453  * @param[in] memberPtr Shutdown event handler to register.
1454  */
1455  template<typename T>
1456  void onShutdown(T *objPtr, void (T::*memberPtr)(const Gap *)) {
1457  onShutdown(GapShutdownCallback_t(objPtr, memberPtr));
1458  }
1459 
1460  /**
1461  * Access the callchain of shutdown event handler.
1462  *
1463  * @note To register callbacks, use onShutdown().add(callback).
1464  *
1465  * @note To unregister callbacks, use onShutdown().detach(callback).
1466  *
1467  * @return A reference to the shutdown event callback chain.
1468  */
1469  GapShutdownCallbackChain_t &onShutdown();
1470 
1471 #if !defined(DOXYGEN_ONLY)
1472  /*
1473  * Constructor from the private implementation.
1474  */
1475  Gap(impl::Gap* impl) : impl(impl) {}
1476 
1477  /*
1478  * Restrict copy and move.
1479  */
1480  Gap(const Gap&) = delete;
1481  Gap& operator=(const Gap&) = delete;
1482 
1483  /*
1484  * API reserved for the controller driver to set the random static address.
1485  * Setting a new random static address while the controller is operating is
1486  * forbidden by the Bluetooth specification.
1487  */
1488  ble_error_t setRandomStaticAddress(const ble::address_t& address);
1489 
1490  ble::address_t getRandomStaticAddress();
1491 #endif // !defined(DOXYGEN_ONLY)
1492 
1493 private:
1494  impl::Gap* impl;
1495 };
1496 
1497 /**
1498  * @}
1499  * @}
1500  */
1501 
1502 } // namespace ble
1503 
1504 /** @deprecated Use the namespaced ble::Gap instead of the global Gap. */
1505 using ble::Gap;
1506 
1507 #endif // BLE_GAP_GAP_H
Event generated when a connection initiation ends (successfully or not).
Definition: Events.h:211
Function like object adapter over freestanding and member functions.
Event produced when advertising ends.
Definition: Events.h:624
virtual void onPrivacyEnabled()
Function invoked when the privacy subsystem has been enabled and is ready to be used.
Definition: Gap.h:557
virtual void onScanRequestReceived(const ScanRequestEvent &event)
Called when an advertising device receive a scan response.
Definition: Gap.h:313
Define device discovery, connection and link management procedures.
Definition: Gap.h:282
Event received when connection parameters have been updated.
Definition: Events.h:888
Event generated when periodic advertising sync is lost.
Definition: Events.h:545
uintptr_t connection_handle_t
Opaque reference to a connection.
virtual void onReadPhy(ble_error_t status, connection_handle_t connectionHandle, phy_t txPhy, phy_t rxPhy)
Function invoked when the current transmitter and receiver PHY have been read for a given connection...
Definition: Gap.h:490
Event produced when advertising start.
Definition: Events.h:583
Event generated when an advertising packet is seen during passive scanning or a scan response is rece...
Definition: Events.h:40
virtual void onUpdateConnectionParametersRequest(const UpdateConnectionParametersRequestEvent &event)
Called when the peer request connection parameters updates.
Definition: Gap.h:441
Type that describes a random device address type.
Type describing the number of symbols per bit in le coded PHY.
virtual void onConnectionComplete(const ConnectionCompleteEvent &event)
Called when connection attempt ends or an advertising device has been connected.
Definition: Gap.h:420
virtual void onDataLengthChange(connection_handle_t connectionHandle, uint16_t txSize, uint16_t rxSize)
Function invoked when the connections changes the maximum number of octets that can be sent or receiv...
Definition: Gap.h:545
Representation of a whitelist of addresses.
virtual void onAdvertisingStart(const AdvertisingStartEvent &event)
Called when advertising starts.
Definition: Gap.h:324
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=nullptr) noexcept
Create a callback class with type inferred from the arguments.
Definition: Callback.h:678
Parameters defining the scan process.
MAC address data type.
Type that describes a bluetooth PHY(sical) transport.
uint16_t maxConnectionInterval
Maximum interval between two connection events allowed for a connection.
Definition: Gap.h:588
Parameters defining the connection initiation process.
virtual void onAdvertisingEnd(const AdvertisingEndEvent &event)
Called when advertising ends.
Definition: Gap.h:340
CallChainOfFunctionPointersWithContext< const Gap * > GapShutdownCallbackChain_t
Callchain of gap shutdown event handler.
Definition: Gap.h:297
virtual void onScanTimeout(const ScanTimeoutEvent &event)
Called when scan times out.
Definition: Gap.h:362
virtual void onConnectionParametersUpdateComplete(const ConnectionParametersUpdateCompleteEvent &event)
Called when connection parameters have been updated.
Definition: Gap.h:455
uint16_t slaveLatency
Number of connection events the slave can drop if it has nothing to communicate to the master...
Definition: Gap.h:596
Event generated when you first receive a periodic advertisement.
Definition: Events.h:373
Preferred connection parameter display in Generic Access Service.
Definition: Gap.h:571
FunctionPointerWithContext< const Gap * > GapShutdownCallback_t
Gap shutdown event handler.
Definition: Gap.h:289
uint16_t connectionSupervisionTimeout
Link supervision timeout for the connection.
Definition: Gap.h:612
Type that describes a peer device address type.
Event generated when scan times out.
Definition: Events.h:576
Event produced when a disconnection is complete.
Definition: Events.h:776
uint16_t minConnectionInterval
Minimum interval between two connection events allowed for a connection.
Definition: Gap.h:579
Type that describe a set of PHY(sical) transports.
Parameters defining the advertising process.
Definition of the general handler of Gap related events.
Definition: Gap.h:303
virtual void onDisconnectionComplete(const DisconnectionCompleteEvent &event)
Called when a connection has been disconnected.
Definition: Gap.h:468
Function like object hosting a list of FunctionPointerWithContext.
Event generated when periodic advertising packet is received.
Definition: Events.h:474
void onShutdown(T *objPtr, void(T::*memberPtr)(const Gap *))
Register a Gap shutdown event handler.
Definition: Gap.h:1456
Event received when a peer wants to change the connection parameters.
Definition: Events.h:815
virtual void onPeriodicAdvertisingSyncLoss(const PeriodicAdvertisingSyncLoss &event)
Called when a periodic advertising sync has been lost.
Definition: Gap.h:405
virtual void onPeriodicAdvertisingReport(const PeriodicAdvertisingReportEvent &event)
Called when a periodic advertising packet is received.
Definition: Gap.h:390
virtual void onAdvertisingReport(const AdvertisingReportEvent &event)
Called when a scanner receives an advertising or a scan response packet.
Definition: Gap.h:351
virtual void onPhyUpdateComplete(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.
Definition: Gap.h:524
Entry namespace for all BLE API definitions.
Features supported by the controller.
Event produced when a peer requests a scan response from the advertiser.
Definition: Events.h:725
virtual void onPeriodicAdvertisingSyncEstablished(const PeriodicAdvertisingSyncEstablishedEvent &event)
Called when first advertising packet in periodic advertising is received.
Definition: Gap.h:375
ble_error_t
Error codes for the BLE API.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.