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 ST

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?

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