TOF based Presence Detector
Dependencies: BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
bricks/characteristic.h@23:677689000369, 2017-01-06 (annotated)
- Committer:
- hux
- Date:
- Fri Jan 06 15:28:18 2017 +0000
- Revision:
- 23:677689000369
- Child:
- 24:0f08f68579bd
Already nice & easy level. Still bugs!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 23:677689000369 | 1 | // characteristic.h - an easy to use overloaded characteristic class |
hux | 23:677689000369 | 2 | // |
hux | 23:677689000369 | 3 | // Synopsis: |
hux | 23:677689000369 | 4 | // |
hux | 23:677689000369 | 5 | // Characteristic<type> myCharacteristic(col,uuid,mode); |
hux | 23:677689000369 | 6 | // Characteristic<type> myCharacteristic(col,uuid,mode,name); |
hux | 23:677689000369 | 7 | // Characteristic<type> myCharacteristic(col,uuid,mode,name,inidata); |
hux | 23:677689000369 | 8 | // Characteristic<type> myCharacteristic(col,uuid,mode,inidata); |
hux | 23:677689000369 | 9 | // |
hux | 23:677689000369 | 10 | // Declaration of a characteristic, specifying value type, UUID and access mode, |
hux | 23:677689000369 | 11 | // optionally providing a name (user descriptor) and initial data. |
hux | 23:677689000369 | 12 | // |
hux | 23:677689000369 | 13 | // col: Collection, to which the constructed characteristic is automati- |
hux | 23:677689000369 | 14 | // cally added. After construction and adding all characteristics to |
hux | 23:677689000369 | 15 | // a collection this collection is passed to a service constructor, |
hux | 23:677689000369 | 16 | // which gets thus knowledge about all characteristics which have to |
hux | 23:677689000369 | 17 | // be involved in the service |
hux | 23:677689000369 | 18 | // |
hux | 23:677689000369 | 19 | // uuid: A unique universal ID which identifies the characteristic |
hux | 23:677689000369 | 20 | // |
hux | 23:677689000369 | 21 | // mode: A string comprising zero or more access mode characters which |
hux | 23:677689000369 | 22 | // define the possible access modes of the characteristic. |
hux | 23:677689000369 | 23 | // |
hux | 23:677689000369 | 24 | // "" no access |
hux | 23:677689000369 | 25 | // "b" broadcast |
hux | 23:677689000369 | 26 | // "r" read access |
hux | 23:677689000369 | 27 | // "s" send & pray (write without response) |
hux | 23:677689000369 | 28 | // "w" write access (with feedback) |
hux | 23:677689000369 | 29 | // "n" notification (no client confirmation) |
hux | 23:677689000369 | 30 | // "i" indication (with client confirmation) |
hux | 23:677689000369 | 31 | // "a" authentication signed write |
hux | 23:677689000369 | 32 | // "x" extended access |
hux | 23:677689000369 | 33 | // |
hux | 23:677689000369 | 34 | // name: An optional name provided in terms of a user descriptor. If this |
hux | 23:677689000369 | 35 | // argument is omitted the user descriptor is not created. |
hux | 23:677689000369 | 36 | // |
hux | 23:677689000369 | 37 | // inidata: A pointer to a variable containing initializing data for the |
hux | 23:677689000369 | 38 | // characteristic. If the inidata argument is not provided, an |
hux | 23:677689000369 | 39 | // implicit initialization is performed which clears the characte- |
hux | 23:677689000369 | 40 | // ristic (all bytes set to 0). As a concluding remark it needs to |
hux | 23:677689000369 | 41 | // be noted that the implicite initialozation works only for |
hux | 23:677689000369 | 42 | // sizeof(Type) <= SIZE_INIBUFFER (defined in "bricks/types.h") |
hux | 23:677689000369 | 43 | // |
hux | 23:677689000369 | 44 | // |
hux | 23:677689000369 | 45 | // Example 1: A protocol might be declared as a class as follows |
hux | 23:677689000369 | 46 | // |
hux | 23:677689000369 | 47 | // Collection col; // collection used during setup |
hux | 23:677689000369 | 48 | // |
hux | 23:677689000369 | 49 | // Characteristic<ObjectId> id(col,0x2AC3,"rw","ID"); |
hux | 23:677689000369 | 50 | // Characteristic<ObjectName> name(col,0x2ABE,"rw","Name"); |
hux | 23:677689000369 | 51 | // Characteristic<Digital> presence(col,0x2A56,"r","Presence"); |
hux | 23:677689000369 | 52 | // |
hux | 23:677689000369 | 53 | // Service presenceDetection(col,0xA001); // instantiate service |
hux | 23:677689000369 | 54 | // |
hux | 23:677689000369 | 55 | // onSetup(Blob &blue) |
hux | 23:677689000369 | 56 | // { |
hux | 23:677689000369 | 57 | // blue.service(presenceDetection); // add service |
hux | 23:677689000369 | 58 | // } |
hux | 23:677689000369 | 59 | // |
hux | 23:677689000369 | 60 | // Example 2: service definition by means of a service definition class |
hux | 23:677689000369 | 61 | // |
hux | 23:677689000369 | 62 | // class PresenceDetector |
hux | 23:677689000369 | 63 | // { |
hux | 23:677689000369 | 64 | // public: |
hux | 23:677689000369 | 65 | // Collection col; // collection used during setup |
hux | 23:677689000369 | 66 | // |
hux | 23:677689000369 | 67 | // Characteristic<ObjectId> id; // ID of presence detector |
hux | 23:677689000369 | 68 | // Characteristic<ObjectName> name; // name of presence detector |
hux | 23:677689000369 | 69 | // Characteristic<Digital> presence; // digital presence value |
hux | 23:677689000369 | 70 | // Characteristic<DateTime> timestamp; // last detection change's time |
hux | 23:677689000369 | 71 | // Characteristic<ObjectName> layout; // name of model railway layout |
hux | 23:677689000369 | 72 | // |
hux | 23:677689000369 | 73 | // Service presenceDetection; // the service |
hux | 23:677689000369 | 74 | // |
hux | 23:677689000369 | 75 | // public: |
hux | 23:677689000369 | 76 | // PresenceDetector(Blob &blue, cost UUID uuid) : |
hux | 23:677689000369 | 77 | // list; // init service list |
hux | 23:677689000369 | 78 | // id(list,0x2AC3,"rw","ID"), // instantiate characteristic |
hux | 23:677689000369 | 79 | // name(list,0x2ABE,"rw","Name"), // instantiate characteristic |
hux | 23:677689000369 | 80 | // presence(list,0x2A56,"r","Presence"),// instantiate characteristic |
hux | 23:677689000369 | 81 | // timestamp(list,0x2A08,"r","Timestamp"),// instantiate characteristic |
hux | 23:677689000369 | 82 | // layout(list,0x2ABE,"rw","Layout"), // instantiate characteristic |
hux | 23:677689000369 | 83 | // presenceDetection(list,uuid) // instantiate service |
hux | 23:677689000369 | 84 | // { |
hux | 23:677689000369 | 85 | // blue.service(presenceDetection); // add service |
hux | 23:677689000369 | 86 | // } |
hux | 23:677689000369 | 87 | // }; |
hux | 23:677689000369 | 88 | // |
hux | 23:677689000369 | 89 | #ifndef _CHARACTERISTIC_H_ |
hux | 23:677689000369 | 90 | #define _CHARACTERISTIC_H_ |
hux | 23:677689000369 | 91 | |
hux | 23:677689000369 | 92 | #include "ble/BLE.h" |
hux | 23:677689000369 | 93 | #include "ble/Gap.h" |
hux | 23:677689000369 | 94 | #include "bricks/types.h" |
hux | 23:677689000369 | 95 | #include "bricks/collection.h" |
hux | 23:677689000369 | 96 | |
hux | 23:677689000369 | 97 | //============================================================================== |
hux | 23:677689000369 | 98 | // UserDescriptor Class - Create a User Descriptor |
hux | 23:677689000369 | 99 | //============================================================================== |
hux | 23:677689000369 | 100 | |
hux | 23:677689000369 | 101 | class UserDescriptor : public GattAttribute |
hux | 23:677689000369 | 102 | { |
hux | 23:677689000369 | 103 | private: |
hux | 23:677689000369 | 104 | typedef GattAttribute *GattAttributePtr; |
hux | 23:677689000369 | 105 | |
hux | 23:677689000369 | 106 | public: |
hux | 23:677689000369 | 107 | GattAttributePtr plist[1]; |
hux | 23:677689000369 | 108 | |
hux | 23:677689000369 | 109 | UserDescriptor(const char *name) : |
hux | 23:677689000369 | 110 | GattAttribute(0x2901,(uint8_t*)name,strlen(name),strlen(name)) |
hux | 23:677689000369 | 111 | { |
hux | 23:677689000369 | 112 | plist[0] = this; |
hux | 23:677689000369 | 113 | } |
hux | 23:677689000369 | 114 | }; |
hux | 23:677689000369 | 115 | |
hux | 23:677689000369 | 116 | //============================================================================== |
hux | 23:677689000369 | 117 | // Characteristic Class - derived GattCharacteristic, auto-adds to service list |
hux | 23:677689000369 | 118 | //============================================================================== |
hux | 23:677689000369 | 119 | // |
hux | 23:677689000369 | 120 | // Synopsis: |
hux | 23:677689000369 | 121 | // Characteristic(list,uuid,mode) |
hux | 23:677689000369 | 122 | // Characteristic(list,uuid,mode,description) |
hux | 23:677689000369 | 123 | // |
hux | 23:677689000369 | 124 | // Parameter mode is a string combination of the following characters: |
hux | 23:677689000369 | 125 | // "" BLE_GATT_CHAR_PROPERTIES_NONE (0x00) |
hux | 23:677689000369 | 126 | // "b" BLE_GATT_CHAR_PROPERTIES_BROADCAST (0x01) |
hux | 23:677689000369 | 127 | // "r" BLE_GATT_CHAR_PROPERTIES_READ (0x02) |
hux | 23:677689000369 | 128 | // "s" BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE (0x04) (send&pray) |
hux | 23:677689000369 | 129 | // "w" BLE_GATT_CHAR_PROPERTIES_WRITE (0x08) |
hux | 23:677689000369 | 130 | // "n" BLE_GATT_CHAR_PROPERTIES_NOTIFY (0x10) |
hux | 23:677689000369 | 131 | // "i" BLE_GATT_CHAR_PROPERTIES_INDICATE (0x20) |
hux | 23:677689000369 | 132 | // "a" BLE_GATT_CHAR_PROPERTIES_AUTHENTICATED_SIGNED_WRITES (0x40) |
hux | 23:677689000369 | 133 | // "x" BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES (0x80) |
hux | 23:677689000369 | 134 | // |
hux | 23:677689000369 | 135 | // Example: |
hux | 23:677689000369 | 136 | // ServiceList list; |
hux | 23:677689000369 | 137 | // |
hux | 23:677689000369 | 138 | // Characteristic<ObjectId> id(list,0x2AC3,"rw","ID"); |
hux | 23:677689000369 | 139 | // Characteristic<ObjectName> name(list,0x2ABE,"rw","Name"); |
hux | 23:677689000369 | 140 | // Characteristic<Digital> presence(list,0x2A56,"rn","Presence"); |
hux | 23:677689000369 | 141 | // |
hux | 23:677689000369 | 142 | // |
hux | 23:677689000369 | 143 | static IniBuffer inidata; // to initialize a characteristic |
hux | 23:677689000369 | 144 | |
hux | 23:677689000369 | 145 | static void init() // initialize inibuffer |
hux | 23:677689000369 | 146 | { |
hux | 23:677689000369 | 147 | static int initialized = 0; |
hux | 23:677689000369 | 148 | |
hux | 23:677689000369 | 149 | if (!initialized) // clear init data buffer |
hux | 23:677689000369 | 150 | { initialized = 1; |
hux | 23:677689000369 | 151 | memset(inidata,0,sizeof(IniBuffer)); |
hux | 23:677689000369 | 152 | } |
hux | 23:677689000369 | 153 | } |
hux | 23:677689000369 | 154 | |
hux | 23:677689000369 | 155 | template <typename Type> |
hux | 23:677689000369 | 156 | class Characteristic : public GattCharacteristic |
hux | 23:677689000369 | 157 | { |
hux | 23:677689000369 | 158 | private: |
hux | 23:677689000369 | 159 | UserDescriptor descriptor; // characteristic's user descriptor |
hux | 23:677689000369 | 160 | |
hux | 23:677689000369 | 161 | uint8_t getmask(const char *pmask) |
hux | 23:677689000369 | 162 | { |
hux | 23:677689000369 | 163 | uint8_t mask = 0; |
hux | 23:677689000369 | 164 | |
hux | 23:677689000369 | 165 | for(;*pmask;pmask++) |
hux | 23:677689000369 | 166 | { switch (*pmask) |
hux | 23:677689000369 | 167 | { |
hux | 23:677689000369 | 168 | case 'b': // broad cast |
hux | 23:677689000369 | 169 | mask = mask | BLE_GATT_CHAR_PROPERTIES_BROADCAST; |
hux | 23:677689000369 | 170 | break; |
hux | 23:677689000369 | 171 | case 'r': // read |
hux | 23:677689000369 | 172 | mask = mask | BLE_GATT_CHAR_PROPERTIES_READ; |
hux | 23:677689000369 | 173 | break; |
hux | 23:677689000369 | 174 | case 's': // send (& pray) |
hux | 23:677689000369 | 175 | mask = mask | BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE; |
hux | 23:677689000369 | 176 | break; |
hux | 23:677689000369 | 177 | case 'w': // write |
hux | 23:677689000369 | 178 | mask = mask | BLE_GATT_CHAR_PROPERTIES_WRITE; |
hux | 23:677689000369 | 179 | break; |
hux | 23:677689000369 | 180 | case 'n': // notify |
hux | 23:677689000369 | 181 | mask = mask | BLE_GATT_CHAR_PROPERTIES_NOTIFY; |
hux | 23:677689000369 | 182 | break; |
hux | 23:677689000369 | 183 | case 'i': // indicate |
hux | 23:677689000369 | 184 | mask = mask | BLE_GATT_CHAR_PROPERTIES_INDICATE; |
hux | 23:677689000369 | 185 | break; |
hux | 23:677689000369 | 186 | case 'a': // authenticated signed write |
hux | 23:677689000369 | 187 | mask = mask | BLE_GATT_CHAR_PROPERTIES_AUTHENTICATED_SIGNED_WRITES; |
hux | 23:677689000369 | 188 | break; |
hux | 23:677689000369 | 189 | case 'x': // extended properties |
hux | 23:677689000369 | 190 | mask = mask | BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES; |
hux | 23:677689000369 | 191 | break; |
hux | 23:677689000369 | 192 | } |
hux | 23:677689000369 | 193 | } |
hux | 23:677689000369 | 194 | return mask; |
hux | 23:677689000369 | 195 | } |
hux | 23:677689000369 | 196 | |
hux | 23:677689000369 | 197 | public: |
hux | 23:677689000369 | 198 | Characteristic<Type> (Collection &list, UUID uuid, const char *mode, |
hux | 23:677689000369 | 199 | Type *valuePtr = (Type*)&inidata) : |
hux | 23:677689000369 | 200 | GattCharacteristic(uuid, |
hux | 23:677689000369 | 201 | reinterpret_cast<uint8_t*> (valuePtr), |
hux | 23:677689000369 | 202 | sizeof(Type), |
hux | 23:677689000369 | 203 | sizeof(Type), |
hux | 23:677689000369 | 204 | getmask(mode), |
hux | 23:677689000369 | 205 | NULL, // descriptor list |
hux | 23:677689000369 | 206 | 0, // number of descriptors |
hux | 23:677689000369 | 207 | false), |
hux | 23:677689000369 | 208 | { |
hux | 23:677689000369 | 209 | init(); // assure initializing |
hux | 23:677689000369 | 210 | |
hux | 23:677689000369 | 211 | // there is a potential danger that we use implicite initializing |
hux | 23:677689000369 | 212 | // with sizeof(Type) > sizeof(inibuffer). This must be prevented! |
hux | 23:677689000369 | 213 | |
hux | 23:677689000369 | 214 | int feasible = (sizeof(Type) <= sizeof(inibuffer)); |
hux | 23:677689000369 | 215 | if (feasible || valuePtr != (Type*)&inidata) |
hux | 23:677689000369 | 216 | { |
hux | 23:677689000369 | 217 | list.add(this); // add to setup list |
hux | 23:677689000369 | 218 | } // otherwise just ignore! |
hux | 23:677689000369 | 219 | } |
hux | 23:677689000369 | 220 | |
hux | 23:677689000369 | 221 | public: |
hux | 23:677689000369 | 222 | Characteristic<Type> (Collection &list, UUID uuid, const char *mode, |
hux | 23:677689000369 | 223 | const char *name, Type *valuePtr = (Type*)&inidata) : |
hux | 23:677689000369 | 224 | GattCharacteristic(uuid, |
hux | 23:677689000369 | 225 | reinterpret_cast<uint8_t*> (valuePtr), |
hux | 23:677689000369 | 226 | sizeof(Type), |
hux | 23:677689000369 | 227 | sizeof(Type), |
hux | 23:677689000369 | 228 | getmask(mode), |
hux | 23:677689000369 | 229 | descriptor.plist, // descriptor list |
hux | 23:677689000369 | 230 | 1, // number of descriptors |
hux | 23:677689000369 | 231 | false), |
hux | 23:677689000369 | 232 | descriptor(name) // construct descriptor |
hux | 23:677689000369 | 233 | { |
hux | 23:677689000369 | 234 | init(); // assure initializing |
hux | 23:677689000369 | 235 | |
hux | 23:677689000369 | 236 | // there is a potential danger that we use implicite initializing |
hux | 23:677689000369 | 237 | // with sizeof(Type) > sizeof(inibuffer). This must be prevented! |
hux | 23:677689000369 | 238 | |
hux | 23:677689000369 | 239 | int feasible = (sizeof(Type) <= sizeof(IniBuffer)); |
hux | 23:677689000369 | 240 | if (feasible || valuePtr != (Type*)&inidata) |
hux | 23:677689000369 | 241 | { |
hux | 23:677689000369 | 242 | list.add(this); // add to setup list |
hux | 23:677689000369 | 243 | } // otherwise just ignore! |
hux | 23:677689000369 | 244 | } |
hux | 23:677689000369 | 245 | }; |
hux | 23:677689000369 | 246 | |
hux | 23:677689000369 | 247 | |
hux | 23:677689000369 | 248 | #endif // _CHARACTERISTIC_H_ |