![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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/callback.cpp@26:dce30a5341bb, 2018-05-19 (annotated)
- Committer:
- hux
- Date:
- Sat May 19 14:10:17 2018 +0000
- Revision:
- 26:dce30a5341bb
Published
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 26:dce30a5341bb | 1 | // callback.cpp - deal with callbacks |
hux | 26:dce30a5341bb | 2 | |
hux | 26:dce30a5341bb | 3 | #include "bricks/blob.h" |
hux | 26:dce30a5341bb | 4 | #include "bricks/callback.h" |
hux | 26:dce30a5341bb | 5 | #include "bricks/trace.h" |
hux | 26:dce30a5341bb | 6 | |
hux | 26:dce30a5341bb | 7 | //============================================================================== |
hux | 26:dce30a5341bb | 8 | // Setup Data Written Callback |
hux | 26:dce30a5341bb | 9 | //============================================================================== |
hux | 26:dce30a5341bb | 10 | |
hux | 26:dce30a5341bb | 11 | static void (*cbWritten)(O&o) = 0; // where we have to follow up |
hux | 26:dce30a5341bb | 12 | |
hux | 26:dce30a5341bb | 13 | // default onDataWritten callback |
hux | 26:dce30a5341bb | 14 | |
hux | 26:dce30a5341bb | 15 | static void dfWritten(const GattWriteCallbackParams *params) |
hux | 26:dce30a5341bb | 16 | { |
hux | 26:dce30a5341bb | 17 | O o; // setup a blob |
hux | 26:dce30a5341bb | 18 | o.pWritten = params; // store to provide access |
hux | 26:dce30a5341bb | 19 | |
hux | 26:dce30a5341bb | 20 | trace(o,2,"<data written>\n"); |
hux | 26:dce30a5341bb | 21 | |
hux | 26:dce30a5341bb | 22 | if (cbWritten) |
hux | 26:dce30a5341bb | 23 | (*cbWritten)(o); // user callback follow-up |
hux | 26:dce30a5341bb | 24 | } |
hux | 26:dce30a5341bb | 25 | |
hux | 26:dce30a5341bb | 26 | |
hux | 26:dce30a5341bb | 27 | void onWritten(O&o, void (*fptr)(O&o)) // setup data written callback |
hux | 26:dce30a5341bb | 28 | { |
hux | 26:dce30a5341bb | 29 | cbWritten = fptr; // remember function to follow-up |
hux | 26:dce30a5341bb | 30 | o.gattServer().onDataWritten(dfWritten);// actual calback setup in GATT |
hux | 26:dce30a5341bb | 31 | } |
hux | 26:dce30a5341bb | 32 | |
hux | 26:dce30a5341bb | 33 | //============================================================================== |
hux | 26:dce30a5341bb | 34 | // Setup Connection Callback |
hux | 26:dce30a5341bb | 35 | //============================================================================== |
hux | 26:dce30a5341bb | 36 | |
hux | 26:dce30a5341bb | 37 | static void (*cbConnect)(Blob&) = 0;// where we have to follow up |
hux | 26:dce30a5341bb | 38 | |
hux | 26:dce30a5341bb | 39 | // default connection callback |
hux | 26:dce30a5341bb | 40 | |
hux | 26:dce30a5341bb | 41 | static void dfConnect(const Gap::ConnectionCallbackParams_t *params) |
hux | 26:dce30a5341bb | 42 | { |
hux | 26:dce30a5341bb | 43 | O o; // setup a blob object |
hux | 26:dce30a5341bb | 44 | o.pConnect = params; // store to provide access |
hux | 26:dce30a5341bb | 45 | |
hux | 26:dce30a5341bb | 46 | trace(o,2,"<connect>\n"); |
hux | 26:dce30a5341bb | 47 | |
hux | 26:dce30a5341bb | 48 | if (cbConnect) |
hux | 26:dce30a5341bb | 49 | (*cbConnect)(o); // user callback follow-up |
hux | 26:dce30a5341bb | 50 | } |
hux | 26:dce30a5341bb | 51 | |
hux | 26:dce30a5341bb | 52 | void onConnect(O&o,void(*fptr)(O&)) // setup connection callback |
hux | 26:dce30a5341bb | 53 | { |
hux | 26:dce30a5341bb | 54 | cbConnect = fptr; // remember function to follow-up |
hux | 26:dce30a5341bb | 55 | o.gap().onConnection(dfConnect); // actual callback setup in GAP |
hux | 26:dce30a5341bb | 56 | } |
hux | 26:dce30a5341bb | 57 | |
hux | 26:dce30a5341bb | 58 | //============================================================================== |
hux | 26:dce30a5341bb | 59 | // Setup Disconnection Callback |
hux | 26:dce30a5341bb | 60 | //============================================================================== |
hux | 26:dce30a5341bb | 61 | |
hux | 26:dce30a5341bb | 62 | static void (*cbDisconnect)(O&o) = 0; // where we have to follow up |
hux | 26:dce30a5341bb | 63 | |
hux | 26:dce30a5341bb | 64 | // default disconnection callback |
hux | 26:dce30a5341bb | 65 | |
hux | 26:dce30a5341bb | 66 | static void dfDisconnect(const Gap::DisconnectionCallbackParams_t *params) |
hux | 26:dce30a5341bb | 67 | { |
hux | 26:dce30a5341bb | 68 | Blob o; // setup a blob |
hux | 26:dce30a5341bb | 69 | o.pDisconnect = params; // store to provide access |
hux | 26:dce30a5341bb | 70 | |
hux | 26:dce30a5341bb | 71 | trace(o,2,"<disconnected>\n"); |
hux | 26:dce30a5341bb | 72 | |
hux | 26:dce30a5341bb | 73 | if (cbDisconnect) |
hux | 26:dce30a5341bb | 74 | (*cbDisconnect)(o); // user callback follow-up |
hux | 26:dce30a5341bb | 75 | } |
hux | 26:dce30a5341bb | 76 | |
hux | 26:dce30a5341bb | 77 | void onDisconnect(O&o,void(*fptr)(O&o)) // setup disconnection callback |
hux | 26:dce30a5341bb | 78 | { |
hux | 26:dce30a5341bb | 79 | cbDisconnect = fptr; // remember function to follow-up |
hux | 26:dce30a5341bb | 80 | |
hux | 26:dce30a5341bb | 81 | o.gap().onDisconnection(dfDisconnect);// actualcallback setup in GAP |
hux | 26:dce30a5341bb | 82 | } |
hux | 26:dce30a5341bb | 83 |