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/blob.h@22:d467526abc4a, 2016-12-18 (annotated)
- Committer:
- hux
- Date:
- Sun Dec 18 05:33:58 2016 +0000
- Revision:
- 22:d467526abc4a
some new stuff added - works now
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 22:d467526abc4a | 1 | // blob.cpp - 'BLOBs' are BLuetooth OBjects |
hux | 22:d467526abc4a | 2 | #ifndef _BLOB_H_ |
hux | 22:d467526abc4a | 3 | #define _BLOB_H_ |
hux | 22:d467526abc4a | 4 | |
hux | 22:d467526abc4a | 5 | #include "ble/BLE.h" |
hux | 22:d467526abc4a | 6 | #include "ble/Gap.h" |
hux | 22:d467526abc4a | 7 | |
hux | 22:d467526abc4a | 8 | #define ICCC BLE::InitializationCompleteCallbackContext // pure short hand |
hux | 22:d467526abc4a | 9 | #define GDCP Gap::DisconnectionCallbackParams_t // pure short hand |
hux | 22:d467526abc4a | 10 | #define GCCP Gap::ConnectionCallbackParams_t // pure short hand |
hux | 22:d467526abc4a | 11 | #define GWCP GattWriteCallbackParams // pure short hand |
hux | 22:d467526abc4a | 12 | |
hux | 22:d467526abc4a | 13 | typedef uint8_t byte; |
hux | 22:d467526abc4a | 14 | typedef const char *cptr; |
hux | 22:d467526abc4a | 15 | |
hux | 22:d467526abc4a | 16 | class Service |
hux | 22:d467526abc4a | 17 | { |
hux | 22:d467526abc4a | 18 | public: |
hux | 22:d467526abc4a | 19 | GattService *pService; |
hux | 22:d467526abc4a | 20 | |
hux | 22:d467526abc4a | 21 | private: |
hux | 22:d467526abc4a | 22 | GattCharacteristic **pCharacteristics; |
hux | 22:d467526abc4a | 23 | int count; |
hux | 22:d467526abc4a | 24 | uint16_t uuid; |
hux | 22:d467526abc4a | 25 | |
hux | 22:d467526abc4a | 26 | public: |
hux | 22:d467526abc4a | 27 | Service(uint16_t uid); |
hux | 22:d467526abc4a | 28 | void add(GattCharacteristic *p); |
hux | 22:d467526abc4a | 29 | GattService *create(); // actual creation of GATT service |
hux | 22:d467526abc4a | 30 | }; |
hux | 22:d467526abc4a | 31 | |
hux | 22:d467526abc4a | 32 | class Characteristic |
hux | 22:d467526abc4a | 33 | { |
hux | 22:d467526abc4a | 34 | public: |
hux | 22:d467526abc4a | 35 | GattCharacteristic *pc; |
hux | 22:d467526abc4a | 36 | Characteristic(Service &svc, uint16_t uuid, cptr mode, cptr name); |
hux | 22:d467526abc4a | 37 | Characteristic(Service &svc, uint16_t uuid, cptr mode); |
hux | 22:d467526abc4a | 38 | }; |
hux | 22:d467526abc4a | 39 | |
hux | 22:d467526abc4a | 40 | |
hux | 22:d467526abc4a | 41 | class Blob |
hux | 22:d467526abc4a | 42 | { |
hux | 22:d467526abc4a | 43 | public: |
hux | 22:d467526abc4a | 44 | BLE *pble; // to access THE ble object |
hux | 22:d467526abc4a | 45 | const ICCC *pComplete; // params to ICCC context |
hux | 22:d467526abc4a | 46 | const GCCP *pConnect; // params to GCCP context |
hux | 22:d467526abc4a | 47 | const GDCP *pDisconnect; // params to GDPC context |
hux | 22:d467526abc4a | 48 | const GWCP *pWritten; // params to GWCP context |
hux | 22:d467526abc4a | 49 | |
hux | 22:d467526abc4a | 50 | public: |
hux | 22:d467526abc4a | 51 | Blob(); // standard constructor |
hux | 22:d467526abc4a | 52 | void init(void (*)(Blob&),void (*)(Blob&)); // initialize BLE system |
hux | 22:d467526abc4a | 53 | void init(void (*)(Blob&)); // initialize BLE system |
hux | 22:d467526abc4a | 54 | void sleep(void); // low power wait |
hux | 22:d467526abc4a | 55 | |
hux | 22:d467526abc4a | 56 | public: // some short hands (inline) |
hux | 22:d467526abc4a | 57 | GattServer& gatt() { return pble->gattServer(); } |
hux | 22:d467526abc4a | 58 | |
hux | 22:d467526abc4a | 59 | public: // setup GAP advertising |
hux | 22:d467526abc4a | 60 | void device(const char *name); // set device name characteristic |
hux | 22:d467526abc4a | 61 | |
hux | 22:d467526abc4a | 62 | // adding custom service |
hux | 22:d467526abc4a | 63 | |
hux | 22:d467526abc4a | 64 | void service(Service &svc); // add custom service |
hux | 22:d467526abc4a | 65 | void service(GattService &svc); // add custom service |
hux | 22:d467526abc4a | 66 | |
hux | 22:d467526abc4a | 67 | // advertising flags |
hux | 22:d467526abc4a | 68 | |
hux | 22:d467526abc4a | 69 | void mode(const char *p); // Set advertising type and flags |
hux | 22:d467526abc4a | 70 | void data(const uint8_t*, size_t); // advertising data |
hux | 22:d467526abc4a | 71 | void data(const char *str); // advertising data |
hux | 22:d467526abc4a | 72 | void name(const char *str); // add name to device |
hux | 22:d467526abc4a | 73 | void start(void); // start advertising |
hux | 22:d467526abc4a | 74 | void start(int msec); // start advertising (msec: periode) |
hux | 22:d467526abc4a | 75 | void advertise(cptr pmod,int ms=100);// advertise |
hux | 22:d467526abc4a | 76 | |
hux | 22:d467526abc4a | 77 | void onConnect(void (*fptr)(Blob&)); // setup disconnect callback |
hux | 22:d467526abc4a | 78 | void onDisconnect(void (*fptr)(Blob&));// setup disconnect callback |
hux | 22:d467526abc4a | 79 | void onWritten(void (*fptr)(Blob&)); // setup data written callback |
hux | 22:d467526abc4a | 80 | }; |
hux | 22:d467526abc4a | 81 | |
hux | 22:d467526abc4a | 82 | |
hux | 22:d467526abc4a | 83 | |
hux | 22:d467526abc4a | 84 | #endif // _BLOB_H_ |