xref: /nrf52832-nimble/packages/NimBLE-latest/docs/ble_setup/ble_sync_cb.rst (revision 042d53a763ad75cb1465103098bb88c245d95138)
1*042d53a7SEvalZeroRespond to *sync* and *reset* events
2*042d53a7SEvalZero------------------------------------
3*042d53a7SEvalZero
4*042d53a7SEvalZerosync
5*042d53a7SEvalZero~~~~
6*042d53a7SEvalZero
7*042d53a7SEvalZeroThe NimBLE stack is inoperable while the host and controller are out of
8*042d53a7SEvalZerosync. In a combined host-controller app, the sync happens immediately at
9*042d53a7SEvalZerostartup. When the host and controller are separate, sync typically
10*042d53a7SEvalZerooccurs in under a second after the application starts. An application
11*042d53a7SEvalZerolearns when sync is achieved by configuring the host's *sync callback*:
12*042d53a7SEvalZero``ble_hs_cfg.sync_cb``. The host calls the sync callback whenever sync
13*042d53a7SEvalZerois acquired. The sync callback has the following form:
14*042d53a7SEvalZero
15*042d53a7SEvalZero.. code-block:: cpp
16*042d53a7SEvalZero
17*042d53a7SEvalZero    typedef void ble_hs_sync_fn(void);
18*042d53a7SEvalZero
19*042d53a7SEvalZeroBecause the NimBLE stack begins in the unsynced state, the application
20*042d53a7SEvalZeroshould delay all BLE operations until the sync callback has been called.
21*042d53a7SEvalZero
22*042d53a7SEvalZeroreset
23*042d53a7SEvalZero~~~~~
24*042d53a7SEvalZero
25*042d53a7SEvalZeroAnother event indicated by the host is a *controller reset*. The NimBLE
26*042d53a7SEvalZerostack resets itself when a catastrophic error occurs, such as loss of
27*042d53a7SEvalZerocommunication between the host and controller. Upon resetting, the host
28*042d53a7SEvalZerodrops all BLE connections and loses sync with the controller. After a
29*042d53a7SEvalZeroreset, the application should refrain from using the host until sync is
30*042d53a7SEvalZeroagain signaled via the sync callback.
31*042d53a7SEvalZero
32*042d53a7SEvalZeroAn application learns of a host reset by configuring the host's *reset
33*042d53a7SEvalZerocallback*: ``ble_hs_cfg.reset_cb``. This callback has the following
34*042d53a7SEvalZeroform:
35*042d53a7SEvalZero
36*042d53a7SEvalZero.. code-block:: cpp
37*042d53a7SEvalZero
38*042d53a7SEvalZero    typedef void ble_hs_reset_fn(int reason);
39*042d53a7SEvalZero
40*042d53a7SEvalZeroThe ``reason`` parameter is a :doc:`NimBLE host return
41*042d53a7SEvalZerocode <../ble_hs/ble_hs_return_codes>`.
42*042d53a7SEvalZero
43*042d53a7SEvalZeroExample
44*042d53a7SEvalZero~~~~~~~
45*042d53a7SEvalZero
46*042d53a7SEvalZeroThe following example demonstrates the configuration of the sync and
47*042d53a7SEvalZeroreset callbacks.
48*042d53a7SEvalZero
49*042d53a7SEvalZero.. code-block:: cpp
50*042d53a7SEvalZero
51*042d53a7SEvalZero    #include "sysinit/sysinit.h"
52*042d53a7SEvalZero    #include "console/console.h"
53*042d53a7SEvalZero    #include "host/ble_hs.h"
54*042d53a7SEvalZero
55*042d53a7SEvalZero    static void
56*042d53a7SEvalZero    on_sync(void)
57*042d53a7SEvalZero    {
58*042d53a7SEvalZero        /* Begin advertising, scanning for peripherals, etc. */
59*042d53a7SEvalZero    }
60*042d53a7SEvalZero
61*042d53a7SEvalZero    static void
62*042d53a7SEvalZero    on_reset(int reason)
63*042d53a7SEvalZero    {
64*042d53a7SEvalZero        console_printf("Resetting state; reason=%d\n", reason);
65*042d53a7SEvalZero    }
66*042d53a7SEvalZero
67*042d53a7SEvalZero    int
68*042d53a7SEvalZero    main(void)
69*042d53a7SEvalZero    {
70*042d53a7SEvalZero        /* Initialize all packages. */
71*042d53a7SEvalZero        sysinit();
72*042d53a7SEvalZero
73*042d53a7SEvalZero        ble_hs_cfg.sync_cb = on_sync;
74*042d53a7SEvalZero        ble_hs_cfg.reset_cb = on_reset;
75*042d53a7SEvalZero
76*042d53a7SEvalZero        /* As the last thing, process events from default event queue. */
77*042d53a7SEvalZero        while (1) {
78*042d53a7SEvalZero            os_eventq_run(os_eventq_dflt_get());
79*042d53a7SEvalZero        }
80*042d53a7SEvalZero    }
81