GAP based TOF Demo

Dependencies:   BLE_API X_NUCLEO_6180XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Sat Jan 14 08:43:14 2017 +0000
Revision:
27:32267cee7cb8
Parent:
24:0f08f68579bd
Setup a GATT Detector Service. There is some bug in the GATT setup, resulting in different behavior between Nordic nRF51822-DK and NUCLEO-L476RG, but with the workaround it works also for the NUCLEO board. (using Bricks V1A)

Who changed what in which revision?

UserRevisionLine numberNew 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 24:0f08f68579bd 21 // mode: A string comprising zero or more mode characters which
hux 24:0f08f68579bd 22 // define possible properties of the characteristic.
hux 23:677689000369 23 //
hux 24:0f08f68579bd 24 // "" no properties
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 24:0f08f68579bd 96 #include "bricks/descriptor.h"
hux 23:677689000369 97
hux 27:32267cee7cb8 98 // the following #define is helpful if characteristics are defined on base
hux 27:32267cee7cb8 99 // of the BLE API (the tedious way)
hux 27:32267cee7cb8 100
hux 27:32267cee7cb8 101 #define UserDescriptor(name,attribute,descriptor,text) \
hux 27:32267cee7cb8 102 static char *name = text; \
hux 27:32267cee7cb8 103 static GattAttribute attribute(0x2901,(uint8_t*)name,strlen(name),strlen(name)); \
hux 27:32267cee7cb8 104 static GattAttribute *descriptor[] = {&attribute};
hux 27:32267cee7cb8 105
hux 27:32267cee7cb8 106 // the declaration of Characteristics needs the availability of an
hux 27:32267cee7cb8 107 // initializing buffer in the default case
hux 27:32267cee7cb8 108
hux 27:32267cee7cb8 109 extern IniBuffer characteristicInidata; // to initialize a characteristic
hux 23:677689000369 110
hux 27:32267cee7cb8 111 // class definition of a characteristic initializer
hux 23:677689000369 112
hux 27:32267cee7cb8 113 class CharacteristicInitializer // provides pointers to initialized data
hux 27:32267cee7cb8 114 {
hux 27:32267cee7cb8 115 private:
hux 27:32267cee7cb8 116 IniBuffer inibuf; // initialized data
hux 27:32267cee7cb8 117
hux 27:32267cee7cb8 118 public:
hux 27:32267cee7cb8 119 ObjectId *pObjectId() { return (ObjectId*) &inibuf; }
hux 27:32267cee7cb8 120 ObjectName *pObjectName() { return (ObjectName*) &inibuf; }
hux 27:32267cee7cb8 121 Bool *pBool() { return (Bool*) &inibuf; }
hux 27:32267cee7cb8 122 Digital *pDigital() { return (Digital*) &inibuf; }
hux 27:32267cee7cb8 123 DateTime *pDateTime() { return (DateTime*) &inibuf; }
hux 27:32267cee7cb8 124 Buffer *pBuffer() { return (Buffer*) &inibuf; }
hux 27:32267cee7cb8 125
hux 27:32267cee7cb8 126 public:
hux 27:32267cee7cb8 127 CharacteristicInitializer() // constructor
hux 27:32267cee7cb8 128 {
hux 27:32267cee7cb8 129 memset(inibuf,0,sizeof(IniBuffer));
hux 27:32267cee7cb8 130
hux 27:32267cee7cb8 131 // besides of the local inibuf also initialize the global
hux 27:32267cee7cb8 132 // init buffer characteristicInidata;
hux 27:32267cee7cb8 133
hux 27:32267cee7cb8 134 memset(characteristicInidata,0,sizeof(IniBuffer));
hux 27:32267cee7cb8 135 }
hux 27:32267cee7cb8 136 };
hux 27:32267cee7cb8 137
hux 27:32267cee7cb8 138 // definition of class Characteristic
hux 27:32267cee7cb8 139
hux 24:0f08f68579bd 140 class Service;
hux 23:677689000369 141
hux 23:677689000369 142 template <typename Type>
hux 23:677689000369 143 class Characteristic : public GattCharacteristic
hux 23:677689000369 144 {
hux 23:677689000369 145 private:
hux 27:32267cee7cb8 146 void init() // initialize inibuffer
hux 27:32267cee7cb8 147 {
hux 27:32267cee7cb8 148 static int initialized = 0;
hux 27:32267cee7cb8 149
hux 27:32267cee7cb8 150 if (!initialized) // clear init data buffer
hux 27:32267cee7cb8 151 { initialized = 1;
hux 27:32267cee7cb8 152 memset(characteristicInidata,0,sizeof(IniBuffer));
hux 27:32267cee7cb8 153 }
hux 27:32267cee7cb8 154 }
hux 27:32267cee7cb8 155
hux 27:32267cee7cb8 156 private:
hux 23:677689000369 157 UserDescriptor descriptor; // characteristic's user descriptor
hux 23:677689000369 158
hux 23:677689000369 159 uint8_t getmask(const char *pmask)
hux 23:677689000369 160 {
hux 23:677689000369 161 uint8_t mask = 0;
hux 23:677689000369 162
hux 23:677689000369 163 for(;*pmask;pmask++)
hux 23:677689000369 164 { switch (*pmask)
hux 23:677689000369 165 {
hux 23:677689000369 166 case 'b': // broad cast
hux 23:677689000369 167 mask = mask | BLE_GATT_CHAR_PROPERTIES_BROADCAST;
hux 23:677689000369 168 break;
hux 23:677689000369 169 case 'r': // read
hux 23:677689000369 170 mask = mask | BLE_GATT_CHAR_PROPERTIES_READ;
hux 23:677689000369 171 break;
hux 23:677689000369 172 case 's': // send (& pray)
hux 23:677689000369 173 mask = mask | BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE;
hux 23:677689000369 174 break;
hux 23:677689000369 175 case 'w': // write
hux 23:677689000369 176 mask = mask | BLE_GATT_CHAR_PROPERTIES_WRITE;
hux 23:677689000369 177 break;
hux 23:677689000369 178 case 'n': // notify
hux 23:677689000369 179 mask = mask | BLE_GATT_CHAR_PROPERTIES_NOTIFY;
hux 23:677689000369 180 break;
hux 23:677689000369 181 case 'i': // indicate
hux 23:677689000369 182 mask = mask | BLE_GATT_CHAR_PROPERTIES_INDICATE;
hux 23:677689000369 183 break;
hux 23:677689000369 184 case 'a': // authenticated signed write
hux 23:677689000369 185 mask = mask | BLE_GATT_CHAR_PROPERTIES_AUTHENTICATED_SIGNED_WRITES;
hux 23:677689000369 186 break;
hux 23:677689000369 187 case 'x': // extended properties
hux 23:677689000369 188 mask = mask | BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES;
hux 23:677689000369 189 break;
hux 23:677689000369 190 }
hux 23:677689000369 191 }
hux 23:677689000369 192 return mask;
hux 23:677689000369 193 }
hux 27:32267cee7cb8 194 /*
hux 23:677689000369 195 public:
hux 24:0f08f68579bd 196 Characteristic<Type> (Service &service, UUID uuid, const char *mode,
hux 27:32267cee7cb8 197 Type *valuePtr = (Type*)&characteristicInidata) :
hux 23:677689000369 198 GattCharacteristic(uuid,
hux 23:677689000369 199 reinterpret_cast<uint8_t*> (valuePtr),
hux 23:677689000369 200 sizeof(Type),
hux 23:677689000369 201 sizeof(Type),
hux 23:677689000369 202 getmask(mode),
hux 23:677689000369 203 NULL, // descriptor list
hux 23:677689000369 204 0, // number of descriptors
hux 27:32267cee7cb8 205 false),
hux 27:32267cee7cb8 206 descriptor("") // construct descriptor
hux 23:677689000369 207 {
hux 23:677689000369 208 init(); // assure initializing
hux 23:677689000369 209
hux 23:677689000369 210 // there is a potential danger that we use implicite initializing
hux 23:677689000369 211 // with sizeof(Type) > sizeof(inibuffer). This must be prevented!
hux 23:677689000369 212
hux 27:32267cee7cb8 213 int feasible = (sizeof(Type) <= sizeof(IniBuffer));
hux 27:32267cee7cb8 214 if (feasible || valuePtr != (Type*)&characteristicInidata)
hux 23:677689000369 215 {
hux 24:0f08f68579bd 216 service.add(this); // add to service
hux 23:677689000369 217 } // otherwise just ignore!
hux 23:677689000369 218 }
hux 27:32267cee7cb8 219 */
hux 23:677689000369 220 public:
hux 24:0f08f68579bd 221 Characteristic<Type> (Service &service, UUID uuid, const char *mode,
hux 27:32267cee7cb8 222 const char *name, Type *valuePtr = (Type*)&characteristicInidata) :
hux 23:677689000369 223 GattCharacteristic(uuid,
hux 23:677689000369 224 reinterpret_cast<uint8_t*> (valuePtr),
hux 23:677689000369 225 sizeof(Type),
hux 23:677689000369 226 sizeof(Type),
hux 23:677689000369 227 getmask(mode),
hux 23:677689000369 228 descriptor.plist, // descriptor list
hux 23:677689000369 229 1, // number of descriptors
hux 23:677689000369 230 false),
hux 23:677689000369 231 descriptor(name) // construct descriptor
hux 23:677689000369 232 {
hux 23:677689000369 233 init(); // assure initializing
hux 23:677689000369 234
hux 23:677689000369 235 // there is a potential danger that we use implicite initializing
hux 23:677689000369 236 // with sizeof(Type) > sizeof(inibuffer). This must be prevented!
hux 23:677689000369 237
hux 23:677689000369 238 int feasible = (sizeof(Type) <= sizeof(IniBuffer));
hux 27:32267cee7cb8 239 if (feasible || valuePtr != (Type*)&characteristicInidata)
hux 23:677689000369 240 {
hux 24:0f08f68579bd 241 service.add(this); // add to service
hux 23:677689000369 242 } // otherwise just ignore!
hux 23:677689000369 243 }
hux 23:677689000369 244 };
hux 23:677689000369 245
hux 23:677689000369 246
hux 23:677689000369 247 #endif // _CHARACTERISTIC_H_