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