Bluetooth Connected TOF Sensor

Dependencies:   BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Thu Feb 02 17:35:34 2017 +0000
Revision:
30:e324e95c68a9
Parent:
29:cf61a5826426
Final Version 2.0 of S16_Blue_ToF

Who changed what in which revision?

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