GAP based TOF Demo

Dependencies:   BLE_API X_NUCLEO_6180XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Fri Jan 06 20:49:58 2017 +0000
Revision:
24:0f08f68579bd
Parent:
23:677689000369
Child:
27:32267cee7cb8
Version 1.0 - now satisfying behavior. No known bugs!

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 24:0f08f68579bd 98 // Parameter mode is used to define properties based on a string combination
hux 24:0f08f68579bd 99 // of the following characters:
hux 23:677689000369 100 //
hux 23:677689000369 101 // "" BLE_GATT_CHAR_PROPERTIES_NONE (0x00)
hux 23:677689000369 102 // "b" BLE_GATT_CHAR_PROPERTIES_BROADCAST (0x01)
hux 23:677689000369 103 // "r" BLE_GATT_CHAR_PROPERTIES_READ (0x02)
hux 23:677689000369 104 // "s" BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE (0x04) (send&pray)
hux 23:677689000369 105 // "w" BLE_GATT_CHAR_PROPERTIES_WRITE (0x08)
hux 23:677689000369 106 // "n" BLE_GATT_CHAR_PROPERTIES_NOTIFY (0x10)
hux 23:677689000369 107 // "i" BLE_GATT_CHAR_PROPERTIES_INDICATE (0x20)
hux 23:677689000369 108 // "a" BLE_GATT_CHAR_PROPERTIES_AUTHENTICATED_SIGNED_WRITES (0x40)
hux 23:677689000369 109 // "x" BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES (0x80)
hux 23:677689000369 110 //
hux 23:677689000369 111 // Example:
hux 23:677689000369 112 // ServiceList list;
hux 23:677689000369 113 //
hux 23:677689000369 114 // Characteristic<ObjectId> id(list,0x2AC3,"rw","ID");
hux 23:677689000369 115 // Characteristic<ObjectName> name(list,0x2ABE,"rw","Name");
hux 23:677689000369 116 // Characteristic<Digital> presence(list,0x2A56,"rn","Presence");
hux 23:677689000369 117 //
hux 23:677689000369 118 //
hux 23:677689000369 119 static IniBuffer inidata; // to initialize a characteristic
hux 23:677689000369 120
hux 23:677689000369 121 static void init() // initialize inibuffer
hux 23:677689000369 122 {
hux 23:677689000369 123 static int initialized = 0;
hux 23:677689000369 124
hux 23:677689000369 125 if (!initialized) // clear init data buffer
hux 23:677689000369 126 { initialized = 1;
hux 23:677689000369 127 memset(inidata,0,sizeof(IniBuffer));
hux 23:677689000369 128 }
hux 23:677689000369 129 }
hux 24:0f08f68579bd 130
hux 24:0f08f68579bd 131 class Service;
hux 23:677689000369 132
hux 23:677689000369 133 template <typename Type>
hux 23:677689000369 134 class Characteristic : public GattCharacteristic
hux 23:677689000369 135 {
hux 23:677689000369 136 private:
hux 23:677689000369 137 UserDescriptor descriptor; // characteristic's user descriptor
hux 23:677689000369 138
hux 23:677689000369 139 uint8_t getmask(const char *pmask)
hux 23:677689000369 140 {
hux 23:677689000369 141 uint8_t mask = 0;
hux 23:677689000369 142
hux 23:677689000369 143 for(;*pmask;pmask++)
hux 23:677689000369 144 { switch (*pmask)
hux 23:677689000369 145 {
hux 23:677689000369 146 case 'b': // broad cast
hux 23:677689000369 147 mask = mask | BLE_GATT_CHAR_PROPERTIES_BROADCAST;
hux 23:677689000369 148 break;
hux 23:677689000369 149 case 'r': // read
hux 23:677689000369 150 mask = mask | BLE_GATT_CHAR_PROPERTIES_READ;
hux 23:677689000369 151 break;
hux 23:677689000369 152 case 's': // send (& pray)
hux 23:677689000369 153 mask = mask | BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE;
hux 23:677689000369 154 break;
hux 23:677689000369 155 case 'w': // write
hux 23:677689000369 156 mask = mask | BLE_GATT_CHAR_PROPERTIES_WRITE;
hux 23:677689000369 157 break;
hux 23:677689000369 158 case 'n': // notify
hux 23:677689000369 159 mask = mask | BLE_GATT_CHAR_PROPERTIES_NOTIFY;
hux 23:677689000369 160 break;
hux 23:677689000369 161 case 'i': // indicate
hux 23:677689000369 162 mask = mask | BLE_GATT_CHAR_PROPERTIES_INDICATE;
hux 23:677689000369 163 break;
hux 23:677689000369 164 case 'a': // authenticated signed write
hux 23:677689000369 165 mask = mask | BLE_GATT_CHAR_PROPERTIES_AUTHENTICATED_SIGNED_WRITES;
hux 23:677689000369 166 break;
hux 23:677689000369 167 case 'x': // extended properties
hux 23:677689000369 168 mask = mask | BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES;
hux 23:677689000369 169 break;
hux 23:677689000369 170 }
hux 23:677689000369 171 }
hux 23:677689000369 172 return mask;
hux 23:677689000369 173 }
hux 23:677689000369 174
hux 23:677689000369 175 public:
hux 24:0f08f68579bd 176 Characteristic<Type> (Service &service, UUID uuid, const char *mode,
hux 23:677689000369 177 Type *valuePtr = (Type*)&inidata) :
hux 23:677689000369 178 GattCharacteristic(uuid,
hux 23:677689000369 179 reinterpret_cast<uint8_t*> (valuePtr),
hux 23:677689000369 180 sizeof(Type),
hux 23:677689000369 181 sizeof(Type),
hux 23:677689000369 182 getmask(mode),
hux 23:677689000369 183 NULL, // descriptor list
hux 23:677689000369 184 0, // number of descriptors
hux 24:0f08f68579bd 185 false)
hux 23:677689000369 186 {
hux 23:677689000369 187 init(); // assure initializing
hux 23:677689000369 188
hux 23:677689000369 189 // there is a potential danger that we use implicite initializing
hux 23:677689000369 190 // with sizeof(Type) > sizeof(inibuffer). This must be prevented!
hux 23:677689000369 191
hux 23:677689000369 192 int feasible = (sizeof(Type) <= sizeof(inibuffer));
hux 23:677689000369 193 if (feasible || valuePtr != (Type*)&inidata)
hux 23:677689000369 194 {
hux 24:0f08f68579bd 195 service.add(this); // add to service
hux 23:677689000369 196 } // otherwise just ignore!
hux 23:677689000369 197 }
hux 23:677689000369 198
hux 23:677689000369 199 public:
hux 24:0f08f68579bd 200 Characteristic<Type> (Service &service, UUID uuid, const char *mode,
hux 23:677689000369 201 const char *name, Type *valuePtr = (Type*)&inidata) :
hux 23:677689000369 202 GattCharacteristic(uuid,
hux 23:677689000369 203 reinterpret_cast<uint8_t*> (valuePtr),
hux 23:677689000369 204 sizeof(Type),
hux 23:677689000369 205 sizeof(Type),
hux 23:677689000369 206 getmask(mode),
hux 23:677689000369 207 descriptor.plist, // descriptor list
hux 23:677689000369 208 1, // number of descriptors
hux 23:677689000369 209 false),
hux 23:677689000369 210 descriptor(name) // construct descriptor
hux 23:677689000369 211 {
hux 23:677689000369 212 init(); // assure initializing
hux 23:677689000369 213
hux 23:677689000369 214 // there is a potential danger that we use implicite initializing
hux 23:677689000369 215 // with sizeof(Type) > sizeof(inibuffer). This must be prevented!
hux 23:677689000369 216
hux 23:677689000369 217 int feasible = (sizeof(Type) <= sizeof(IniBuffer));
hux 23:677689000369 218 if (feasible || valuePtr != (Type*)&inidata)
hux 23:677689000369 219 {
hux 24:0f08f68579bd 220 service.add(this); // add to service
hux 23:677689000369 221 } // otherwise just ignore!
hux 23:677689000369 222 }
hux 23:677689000369 223 };
hux 23:677689000369 224
hux 23:677689000369 225
hux 23:677689000369 226 #endif // _CHARACTERISTIC_H_