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