![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
GAP based TOF Demo
Dependencies: BLE_API X_NUCLEO_6180XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
Diff: bricks/characteristic.h
- Revision:
- 27:32267cee7cb8
- Parent:
- 24:0f08f68579bd
--- a/bricks/characteristic.h Sat Jan 07 15:38:15 2017 +0000 +++ b/bricks/characteristic.h Sat Jan 14 08:43:14 2017 +0000 @@ -95,45 +95,65 @@ #include "bricks/collection.h" #include "bricks/descriptor.h" -// Parameter mode is used to define properties based on a string combination -// of the following characters: -// -// "" BLE_GATT_CHAR_PROPERTIES_NONE (0x00) -// "b" BLE_GATT_CHAR_PROPERTIES_BROADCAST (0x01) -// "r" BLE_GATT_CHAR_PROPERTIES_READ (0x02) -// "s" BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE (0x04) (send&pray) -// "w" BLE_GATT_CHAR_PROPERTIES_WRITE (0x08) -// "n" BLE_GATT_CHAR_PROPERTIES_NOTIFY (0x10) -// "i" BLE_GATT_CHAR_PROPERTIES_INDICATE (0x20) -// "a" BLE_GATT_CHAR_PROPERTIES_AUTHENTICATED_SIGNED_WRITES (0x40) -// "x" BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES (0x80) -// -// Example: -// ServiceList list; -// -// Characteristic<ObjectId> id(list,0x2AC3,"rw","ID"); -// Characteristic<ObjectName> name(list,0x2ABE,"rw","Name"); -// Characteristic<Digital> presence(list,0x2A56,"rn","Presence"); -// -// - static IniBuffer inidata; // to initialize a characteristic + // the following #define is helpful if characteristics are defined on base + // of the BLE API (the tedious way) + +#define UserDescriptor(name,attribute,descriptor,text) \ + static char *name = text; \ + static GattAttribute attribute(0x2901,(uint8_t*)name,strlen(name),strlen(name)); \ + static GattAttribute *descriptor[] = {&attribute}; + + // the declaration of Characteristics needs the availability of an + // initializing buffer in the default case + + extern IniBuffer characteristicInidata; // to initialize a characteristic - static void init() // initialize inibuffer - { - static int initialized = 0; + // class definition of a characteristic initializer - if (!initialized) // clear init data buffer - { initialized = 1; - memset(inidata,0,sizeof(IniBuffer)); - } - } - + class CharacteristicInitializer // provides pointers to initialized data + { + private: + IniBuffer inibuf; // initialized data + + public: + ObjectId *pObjectId() { return (ObjectId*) &inibuf; } + ObjectName *pObjectName() { return (ObjectName*) &inibuf; } + Bool *pBool() { return (Bool*) &inibuf; } + Digital *pDigital() { return (Digital*) &inibuf; } + DateTime *pDateTime() { return (DateTime*) &inibuf; } + Buffer *pBuffer() { return (Buffer*) &inibuf; } + + public: + CharacteristicInitializer() // constructor + { + memset(inibuf,0,sizeof(IniBuffer)); + + // besides of the local inibuf also initialize the global + // init buffer characteristicInidata; + + memset(characteristicInidata,0,sizeof(IniBuffer)); + } + }; + + // definition of class Characteristic + class Service; template <typename Type> class Characteristic : public GattCharacteristic { private: + void init() // initialize inibuffer + { + static int initialized = 0; + + if (!initialized) // clear init data buffer + { initialized = 1; + memset(characteristicInidata,0,sizeof(IniBuffer)); + } + } + + private: UserDescriptor descriptor; // characteristic's user descriptor uint8_t getmask(const char *pmask) @@ -171,10 +191,10 @@ } return mask; } - +/* public: Characteristic<Type> (Service &service, UUID uuid, const char *mode, - Type *valuePtr = (Type*)&inidata) : + Type *valuePtr = (Type*)&characteristicInidata) : GattCharacteristic(uuid, reinterpret_cast<uint8_t*> (valuePtr), sizeof(Type), @@ -182,23 +202,24 @@ getmask(mode), NULL, // descriptor list 0, // number of descriptors - false) + false), + descriptor("") // construct descriptor { init(); // assure initializing // there is a potential danger that we use implicite initializing // with sizeof(Type) > sizeof(inibuffer). This must be prevented! - int feasible = (sizeof(Type) <= sizeof(inibuffer)); - if (feasible || valuePtr != (Type*)&inidata) + int feasible = (sizeof(Type) <= sizeof(IniBuffer)); + if (feasible || valuePtr != (Type*)&characteristicInidata) { service.add(this); // add to service } // otherwise just ignore! } - +*/ public: Characteristic<Type> (Service &service, UUID uuid, const char *mode, - const char *name, Type *valuePtr = (Type*)&inidata) : + const char *name, Type *valuePtr = (Type*)&characteristicInidata) : GattCharacteristic(uuid, reinterpret_cast<uint8_t*> (valuePtr), sizeof(Type), @@ -215,7 +236,7 @@ // with sizeof(Type) > sizeof(inibuffer). This must be prevented! int feasible = (sizeof(Type) <= sizeof(IniBuffer)); - if (feasible || valuePtr != (Type*)&inidata) + if (feasible || valuePtr != (Type*)&characteristicInidata) { service.add(this); // add to service } // otherwise just ignore!