GAP based TOF Demo

Dependencies:   BLE_API X_NUCLEO_6180XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Mon Aug 20 16:42:06 2018 +0000
Revision:
29:0f068d70c1a5
made in Shanghai

Who changed what in which revision?

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