A cute tiny piece of code implementing an IoT NAND device, demonstrating how to setup and advertise a cute GATT (NAND) service. The code has been tested on a Nordic nRF51822-DK.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate_IDB0XA1 by
bricks/init.cpp@23:2e73c391bb12, 2017-01-08 (annotated)
- Committer:
- hux
- Date:
- Sun Jan 08 23:15:53 2017 +0000
- Revision:
- 23:2e73c391bb12
A cute tiny piece of code implementing an Iot NAND device, demonstrating how to setup and advertise a cute GATT (NAND) service.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 23:2e73c391bb12 | 1 | // init.cpp - initialize BLE system |
hux | 23:2e73c391bb12 | 2 | |
hux | 23:2e73c391bb12 | 3 | #include "bricks/init.h" |
hux | 23:2e73c391bb12 | 4 | #include "bricks/blink.h" |
hux | 23:2e73c391bb12 | 5 | #include "bricks/trace.h" |
hux | 23:2e73c391bb12 | 6 | |
hux | 23:2e73c391bb12 | 7 | static void (*cbSetup)(O&o) = 0; // setup callback |
hux | 23:2e73c391bb12 | 8 | static void (*cbError)(O&o) = 0; // error callback |
hux | 23:2e73c391bb12 | 9 | |
hux | 23:2e73c391bb12 | 10 | static void cbDefaultError(O&o) // default error handler |
hux | 23:2e73c391bb12 | 11 | { |
hux | 23:2e73c391bb12 | 12 | blinkError(o); // 'error' blink sequence |
hux | 23:2e73c391bb12 | 13 | } |
hux | 23:2e73c391bb12 | 14 | |
hux | 23:2e73c391bb12 | 15 | static void initComplete(BLE::InitializationCompleteCallbackContext *params) |
hux | 23:2e73c391bb12 | 16 | { |
hux | 23:2e73c391bb12 | 17 | Blob o; // setup a blob |
hux | 23:2e73c391bb12 | 18 | o.pComplete = params; // store to provide access |
hux | 23:2e73c391bb12 | 19 | |
hux | 23:2e73c391bb12 | 20 | // params->error = (ble_error_t)((int)BLE_ERROR_NONE+1); // uncomment for debug |
hux | 23:2e73c391bb12 | 21 | |
hux | 23:2e73c391bb12 | 22 | trace(o,2,"<initialized>\n"); |
hux | 23:2e73c391bb12 | 23 | |
hux | 23:2e73c391bb12 | 24 | if (params->error != BLE_ERROR_NONE) |
hux | 23:2e73c391bb12 | 25 | { |
hux | 23:2e73c391bb12 | 26 | trace(o,0,"error\n"); |
hux | 23:2e73c391bb12 | 27 | if (cbError) |
hux | 23:2e73c391bb12 | 28 | (*cbError)(o); // handle error in follow-up |
hux | 23:2e73c391bb12 | 29 | |
hux | 23:2e73c391bb12 | 30 | return; |
hux | 23:2e73c391bb12 | 31 | } |
hux | 23:2e73c391bb12 | 32 | |
hux | 23:2e73c391bb12 | 33 | // Ensure that it is the default instance of BLE |
hux | 23:2e73c391bb12 | 34 | |
hux | 23:2e73c391bb12 | 35 | if(o.getInstanceID() != BLE::DEFAULT_INSTANCE) |
hux | 23:2e73c391bb12 | 36 | { |
hux | 23:2e73c391bb12 | 37 | return; |
hux | 23:2e73c391bb12 | 38 | } |
hux | 23:2e73c391bb12 | 39 | |
hux | 23:2e73c391bb12 | 40 | (*cbSetup)(o); // followup with setup |
hux | 23:2e73c391bb12 | 41 | } |
hux | 23:2e73c391bb12 | 42 | |
hux | 23:2e73c391bb12 | 43 | |
hux | 23:2e73c391bb12 | 44 | void init(O&o, void (*scb)(O&o),void (*ecb)(O&o)) // init BLE system |
hux | 23:2e73c391bb12 | 45 | { |
hux | 23:2e73c391bb12 | 46 | cbSetup = scb; // store setup callback |
hux | 23:2e73c391bb12 | 47 | cbError = ecb; // store error callback |
hux | 23:2e73c391bb12 | 48 | o.init(initComplete); |
hux | 23:2e73c391bb12 | 49 | } |
hux | 23:2e73c391bb12 | 50 | |
hux | 23:2e73c391bb12 | 51 | |
hux | 23:2e73c391bb12 | 52 | void init(O&o, void (*scb)(O&o)) // initialize BLE system |
hux | 23:2e73c391bb12 | 53 | { |
hux | 23:2e73c391bb12 | 54 | init(o,scb,cbDefaultError); // continue with default error callback |
hux | 23:2e73c391bb12 | 55 | } |