Hugo Pristauz / Mbed 2 deprecated M18_LED_Switch

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Button by Bluetooth Low Energy

Committer:
hux
Date:
Sat Jan 07 23:48:53 2017 +0000
Revision:
11:80f2c19ecbce
New Blob class derived from BLE class

Who changed what in which revision?

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