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/service.h@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 | // service.h - declaring services and definitions |
hux | 26:dce30a5341bb | 2 | // |
hux | 26:dce30a5341bb | 3 | // Synopsis |
hux | 26:dce30a5341bb | 4 | // |
hux | 26:dce30a5341bb | 5 | // Example 1: A protocol might be declared as a class as follows |
hux | 26:dce30a5341bb | 6 | // |
hux | 26:dce30a5341bb | 7 | // Collection col; // collection used during setup |
hux | 26:dce30a5341bb | 8 | // |
hux | 26:dce30a5341bb | 9 | // Characteristic<ObjectId> id(col,0x2AC3,"rw","ID"); |
hux | 26:dce30a5341bb | 10 | // Characteristic<ObjectName> name(col,0x2ABE,"rw","Name"); |
hux | 26:dce30a5341bb | 11 | // Characteristic<Digital> presence(col,0x2A56,"r","Presence"); |
hux | 26:dce30a5341bb | 12 | // |
hux | 26:dce30a5341bb | 13 | // Service presenceDetection(col,0xA001); // instantiate service |
hux | 26:dce30a5341bb | 14 | // |
hux | 26:dce30a5341bb | 15 | // onSetup(Blob &blue) |
hux | 26:dce30a5341bb | 16 | // { |
hux | 26:dce30a5341bb | 17 | // blue.service(presenceDetection); // use service |
hux | 26:dce30a5341bb | 18 | // } |
hux | 26:dce30a5341bb | 19 | // |
hux | 26:dce30a5341bb | 20 | // Example 2: service definition by means of a service definition class |
hux | 26:dce30a5341bb | 21 | // |
hux | 26:dce30a5341bb | 22 | // class PresenceDetector |
hux | 26:dce30a5341bb | 23 | // { |
hux | 26:dce30a5341bb | 24 | // public: |
hux | 26:dce30a5341bb | 25 | // Collection col; // collection used during setup |
hux | 26:dce30a5341bb | 26 | // |
hux | 26:dce30a5341bb | 27 | // Characteristic<ObjectId> id; // ID of presence detector |
hux | 26:dce30a5341bb | 28 | // Characteristic<ObjectName> name; // name of presence detector |
hux | 26:dce30a5341bb | 29 | // Characteristic<Digital> presence; // digital presence value |
hux | 26:dce30a5341bb | 30 | // Characteristic<DateTime> timestamp; // last detection change's time |
hux | 26:dce30a5341bb | 31 | // Characteristic<ObjectName> layout; // name of model railway layout |
hux | 26:dce30a5341bb | 32 | // |
hux | 26:dce30a5341bb | 33 | // Service presenceDetection; // the service |
hux | 26:dce30a5341bb | 34 | // |
hux | 26:dce30a5341bb | 35 | // public: |
hux | 26:dce30a5341bb | 36 | // PresenceDetector(Blob &blue, cost UUID uuid) : |
hux | 26:dce30a5341bb | 37 | // list; // init service list |
hux | 26:dce30a5341bb | 38 | // id(list,0x2AC3,"rw","ID"), // instantiate characteristic |
hux | 26:dce30a5341bb | 39 | // name(list,0x2ABE,"rw","Name"), // instantiate characteristic |
hux | 26:dce30a5341bb | 40 | // presence(list,0x2A56,"r","Presence"),// instantiate characteristic |
hux | 26:dce30a5341bb | 41 | // timestamp(list,0x2A08,"r","Timestamp"),// instantiate characteristic |
hux | 26:dce30a5341bb | 42 | // layout(list,0x2ABE,"rw","Layout"), // instantiate characteristic |
hux | 26:dce30a5341bb | 43 | // presenceDetection(list,uuid) // instantiate service |
hux | 26:dce30a5341bb | 44 | // { |
hux | 26:dce30a5341bb | 45 | // Blob blue; |
hux | 26:dce30a5341bb | 46 | // blue.service(presenceDetection); // use service |
hux | 26:dce30a5341bb | 47 | // } |
hux | 26:dce30a5341bb | 48 | // }; |
hux | 26:dce30a5341bb | 49 | // |
hux | 26:dce30a5341bb | 50 | #ifndef _SERVICE_H_ |
hux | 26:dce30a5341bb | 51 | #define _SERVICE_H_ |
hux | 26:dce30a5341bb | 52 | |
hux | 26:dce30a5341bb | 53 | #include "ble/BLE.h" |
hux | 26:dce30a5341bb | 54 | #include "ble/Gap.h" |
hux | 26:dce30a5341bb | 55 | #include "bricks/blob.h" |
hux | 26:dce30a5341bb | 56 | #include "bricks/collection.h" |
hux | 26:dce30a5341bb | 57 | |
hux | 26:dce30a5341bb | 58 | // the following #define is helpful if a service is defined on base of the |
hux | 26:dce30a5341bb | 59 | // BLE API (the tedios way) |
hux | 26:dce30a5341bb | 60 | |
hux | 26:dce30a5341bb | 61 | #define GattListLength(list) (sizeof(list)/sizeof(GattCharacteristic*)) |
hux | 26:dce30a5341bb | 62 | |
hux | 26:dce30a5341bb | 63 | // Service classs definition |
hux | 26:dce30a5341bb | 64 | |
hux | 26:dce30a5341bb | 65 | class Service |
hux | 26:dce30a5341bb | 66 | { |
hux | 26:dce30a5341bb | 67 | public: // public properties |
hux | 26:dce30a5341bb | 68 | Collection collection; // collection of characteristics |
hux | 26:dce30a5341bb | 69 | uint16_t uuid; // UUID of service |
hux | 26:dce30a5341bb | 70 | const char *name; // name of service |
hux | 26:dce30a5341bb | 71 | |
hux | 26:dce30a5341bb | 72 | public: // public methods |
hux | 26:dce30a5341bb | 73 | Service(uint16_t _uuid, const char* _name = NULL) : collection() |
hux | 26:dce30a5341bb | 74 | { |
hux | 26:dce30a5341bb | 75 | uuid = _uuid; |
hux | 26:dce30a5341bb | 76 | name = _name; |
hux | 26:dce30a5341bb | 77 | } |
hux | 26:dce30a5341bb | 78 | |
hux | 26:dce30a5341bb | 79 | void add(GattCharacteristic *pChr) // add a characteristic to collection |
hux | 26:dce30a5341bb | 80 | { |
hux | 26:dce30a5341bb | 81 | collection.add(pChr); |
hux | 26:dce30a5341bb | 82 | } |
hux | 26:dce30a5341bb | 83 | }; |
hux | 26:dce30a5341bb | 84 | |
hux | 26:dce30a5341bb | 85 | #endif // _SERVICE_H_ |