ble_sync_cb.rst 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. Respond to *sync* and *reset* events
  2. ------------------------------------
  3. sync
  4. ~~~~
  5. The NimBLE stack is inoperable while the host and controller are out of
  6. sync. In a combined host-controller app, the sync happens immediately at
  7. startup. When the host and controller are separate, sync typically
  8. occurs in under a second after the application starts. An application
  9. learns when sync is achieved by configuring the host's *sync callback*:
  10. ``ble_hs_cfg.sync_cb``. The host calls the sync callback whenever sync
  11. is acquired. The sync callback has the following form:
  12. .. code-block:: cpp
  13. typedef void ble_hs_sync_fn(void);
  14. Because the NimBLE stack begins in the unsynced state, the application
  15. should delay all BLE operations until the sync callback has been called.
  16. reset
  17. ~~~~~
  18. Another event indicated by the host is a *controller reset*. The NimBLE
  19. stack resets itself when a catastrophic error occurs, such as loss of
  20. communication between the host and controller. Upon resetting, the host
  21. drops all BLE connections and loses sync with the controller. After a
  22. reset, the application should refrain from using the host until sync is
  23. again signaled via the sync callback.
  24. An application learns of a host reset by configuring the host's *reset
  25. callback*: ``ble_hs_cfg.reset_cb``. This callback has the following
  26. form:
  27. .. code-block:: cpp
  28. typedef void ble_hs_reset_fn(int reason);
  29. The ``reason`` parameter is a :doc:`NimBLE host return
  30. code <../ble_hs/ble_hs_return_codes>`.
  31. Example
  32. ~~~~~~~
  33. The following example demonstrates the configuration of the sync and
  34. reset callbacks.
  35. .. code-block:: cpp
  36. #include "sysinit/sysinit.h"
  37. #include "console/console.h"
  38. #include "host/ble_hs.h"
  39. static void
  40. on_sync(void)
  41. {
  42. /* Begin advertising, scanning for peripherals, etc. */
  43. }
  44. static void
  45. on_reset(int reason)
  46. {
  47. console_printf("Resetting state; reason=%d\n", reason);
  48. }
  49. int
  50. main(void)
  51. {
  52. /* Initialize all packages. */
  53. sysinit();
  54. ble_hs_cfg.sync_cb = on_sync;
  55. ble_hs_cfg.reset_cb = on_reset;
  56. /* As the last thing, process events from default event queue. */
  57. while (1) {
  58. os_eventq_run(os_eventq_dflt_get());
  59. }
  60. }