Skip to content

Jira 859. Support BLE descriptor processing. #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,30 @@ void exploreCharacteristic(BLECharacteristic characteristic) {
printData(characteristic.value(), characteristic.valueLength());
}
}

Serial.println();

// loop the descriptors of the characteristic and explore each
for (int i = 0; i < characteristic.descriptorCount(); i++) {
BLEDescriptor descriptor = characteristic.descriptor(i);

exploreDescriptor(descriptor);
}
}

void exploreDescriptor(BLEDescriptor descriptor) {
// print the UUID of the descriptor
Serial.print("\t\tDescriptor ");
Serial.print(descriptor.uuid());

// read the descriptor value
descriptor.read();
delay(1000);

// print out the value of the descriptor
Serial.print(", value 0x");
printData(descriptor.value(), descriptor.valueLength());

Serial.println();
}

void printData(const unsigned char data[], int length) {
Expand Down
11 changes: 8 additions & 3 deletions libraries/CurieBLE/src/BLECharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,20 @@ int BLECharacteristic::addDescriptor(BLEDescriptor& descriptor)
else if (BLEUtils::isLocalBLE(_bledev) == true)
{
// Only support the GATT server that create the service in local device.
_chrc_local_imp = new BLECharacteristicImp(*this, _bledev);
// Consider to add multi-descriptor
if (NULL == _chrc_local_imp)
{
retVar = BLE_STATUS_NO_MEMORY;
_chrc_local_imp = new BLECharacteristicImp(*this, _bledev);
}
else

if (NULL != _chrc_local_imp)
{
retVar = _chrc_local_imp->addDescriptor(descriptor);
}
else
{
retVar = BLE_STATUS_NO_MEMORY;
}
}
return retVar;
}
Expand Down
117 changes: 72 additions & 45 deletions libraries/CurieBLE/src/BLEDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,19 @@ BLEDescriptor::BLEDescriptor(BLEDescriptorImp* descriptorImp,
const BLEDevice *bleDev):
_bledev(bleDev),
_value_size(0),
_value(NULL)
_value(NULL),
_internal(descriptorImp)
{
_properties = descriptorImp->properties();
memset(_uuid_cstr, 0, sizeof (_uuid_cstr));
BLEUtils::uuidBT2String(descriptorImp->bt_uuid(), _uuid_cstr);

_value_size = descriptorImp->valueSize();
_value = (unsigned char*)malloc(_value_size);
if (NULL == _value)
{
memcpy(_value, descriptorImp->value(), _value_size);
}
else
{
errno = ENOMEM;
}
}

BLEDescriptor::BLEDescriptor(const char* uuid,
const unsigned char value[],
unsigned short valueLength):
_bledev()
_bledev(),
_internal(NULL)
{
bt_uuid_128_t uuid_tmp;
memset(_uuid_cstr, 0, sizeof (_uuid_cstr));
Expand All @@ -65,15 +56,16 @@ BLEDescriptor::BLEDescriptor(const char* uuid,

_bledev.setAddress(*BLEUtils::bleGetLoalAddress());

_value_size = valueLength > BLE_MAX_ATTR_LONGDATA_LEN ? BLE_MAX_ATTR_LONGDATA_LEN : valueLength;
_value_size = (valueLength > BLE_MAX_ATTR_LONGDATA_LEN) ? BLE_MAX_ATTR_LONGDATA_LEN : valueLength;
_value = (unsigned char*)malloc(_value_size);
if (NULL == _value)
if (NULL != _value)
{
memcpy(_value, value, _value_size);
}
else
{
errno = ENOMEM;
_value_size = 0;
}
}

Expand All @@ -82,22 +74,27 @@ BLEDescriptor::BLEDescriptor(const char* uuid,
BLEDescriptor(uuid, (const unsigned char*)value, strlen(value))
{}

BLEDescriptor::BLEDescriptor(const BLEDescriptor& rhs)
BLEDescriptor::BLEDescriptor(const BLEDescriptor& rhs):
_bledev(&rhs._bledev),
_properties(rhs._properties),
_value_size(0),
_value(NULL),
_internal(rhs._internal)
{
_value = (unsigned char*)malloc(rhs._value_size); // Sid. KW: allocate memory for _value, not local
if (_value)
{
memcpy(_value, rhs._value, rhs._value_size);
_value_size = rhs._value_size;
}
else
memcpy(_uuid_cstr, rhs._uuid_cstr, sizeof(_uuid_cstr));
if (NULL == _internal && rhs._value_size > 0)
{
_value_size = 0;
errno = ENOMEM;
_value = (unsigned char*)malloc(rhs._value_size); // Sid. KW: allocate memory for _value, not local
if (_value)
{
memcpy(_value, rhs._value, rhs._value_size);
_value_size = rhs._value_size;
}
else
{
errno = ENOMEM;
}
}
memcpy(_uuid_cstr, rhs._uuid_cstr, sizeof(_uuid_cstr));
_properties = rhs._properties;
_bledev = BLEDevice(&rhs._bledev);
}

BLEDescriptor& BLEDescriptor::operator= (const BLEDescriptor& rhs)
Expand All @@ -107,23 +104,27 @@ BLEDescriptor& BLEDescriptor::operator= (const BLEDescriptor& rhs)
memcpy(_uuid_cstr, rhs._uuid_cstr, sizeof(_uuid_cstr));
_properties = rhs._properties;
_bledev = BLEDevice(&rhs._bledev);
if (_value_size < rhs._value_size)
_internal = rhs._internal;
if (NULL == _internal && rhs._value_size > 0)
{
_value_size = rhs._value_size;
if (_value_size < rhs._value_size)
{
_value_size = rhs._value_size;

if (NULL != _value)
free(_value);
_value = (unsigned char*)malloc(_value_size);
}

if (NULL != _value)
free(_value);
_value = (unsigned char*)malloc(_value_size);
}

if (NULL != _value)
{
memcpy(_value, rhs._value, rhs._value_size);
}
else
{
_value_size = 0;
errno = ENOMEM;
{
memcpy(_value, rhs._value, rhs._value_size);
}
else
{
_value_size = 0;
errno = ENOMEM;
}
}
}
return *this;
Expand All @@ -145,12 +146,22 @@ const char* BLEDescriptor::uuid() const

const byte* BLEDescriptor::value() const
{
return _value;
const byte* ret = _value;
if (NULL != _internal)
{
ret = _internal->value();
}
return ret;
}

int BLEDescriptor::valueLength() const
{
return _value_size;
int ret = _value_size;
if (NULL != _internal)
{
ret = _internal->valueLength();
}
return ret;
}

BLEDescriptor::operator bool() const
Expand All @@ -166,6 +177,22 @@ unsigned char BLEDescriptor::properties() const

int BLEDescriptor::valueSize() const
{
return _value_size;
int ret = _value_size;
if (NULL != _internal)
{
ret = _internal->valueSize();
}
return ret;
}

bool BLEDescriptor::read()
{
bool retVar = false;

if (NULL != _internal)
{
retVar = _internal->read();
}
return retVar;
}

99 changes: 59 additions & 40 deletions libraries/CurieBLE/src/BLEDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,73 +37,92 @@ class BLEDescriptor

virtual ~BLEDescriptor();

/**
* @brief Get the descriptor's UUID string
*
* @param none
*
* @return const char* The UUID string
*
* @note none
*/
const char* uuid() const;

virtual const byte* value() const; // returns the value buffer
virtual int valueLength() const; // returns the current length of the value

virtual operator bool() const; // is the descriptor valid (discovered from peripheral)

unsigned char properties() const;
int valueSize() const;
private:
char _uuid_cstr[37]; // The characteristic UUID
BLEDevice _bledev;

unsigned char _properties; // The characteristic property

unsigned short _value_size; // The value size
unsigned char* _value; // The value. Will delete after create the _internal


// The API reserved for feature release
// move here for temp

/**
* @brief Write the value of the descriptor
* @brief Get the value of descriptor
*
* @param value The value buffer that want to write to descriptor
* @param none
*
* @param length The value buffer's length
* @return const byte* The value buffer
*
* @return bool true - Success, false - Failed
* @note none
*/
virtual const byte* value() const;

/**
* @brief Get the current length of the value
*
* @param none
*
* @return int The current length of the value string
*
* @note none
*/
//virtual bool writeValue(const byte value[], int length);
virtual int valueLength() const;

/**
* @brief Write the value of the descriptor
* @brief Is the descriptor valid
*
* @param none
*
* @param value The value buffer that want to write to descriptor
* @return bool true/false
*
* @param length The value buffer's length
* @note none
*/
virtual operator bool() const;

/**
* @brief Read the descriptor value
*
* @param offset The offset in the descriptor's data
* @param none
*
* @return bool true - Success, false - Failed
*
* @note Only for GATT client. Schedule read request to the GATT server
*/
bool read();

/**
* @brief Get the property mask of the descriptor
*
* @param none
*
* @return unsigned char The property mask of the descriptor
*
* @note none
*/
//bool writeValue(const byte value[], int length, int offset);
unsigned char properties() const;

/**
* @brief Write the value of the descriptor
* @brief Get the maximum size of the value
*
* @param value The value string that want to write to descriptor
* @param none
*
* @return bool true - Success, false - Failed
* @return int The maximum size of the value
*
* @note none
*/
//bool writeValue(const char* value);
//virtual byte operator[] (int offset) const; // returns a byte of the value at the specified offset

// GATT client Write the value of the descriptor
//virtual bool write(const byte value[], int length);
//bool write(const byte value[], int length, int offset);
//bool write(const char* value);
//bool read();
int valueSize() const;
private:
char _uuid_cstr[37]; // The characteristic UUID
BLEDevice _bledev;

unsigned char _properties; // The characteristic property

unsigned short _value_size; // The value size
unsigned char* _value; // The value. Will delete after create the _internal
BLEDescriptorImp *_internal; // The real implementation of Descriptor
};

#endif
6 changes: 6 additions & 0 deletions libraries/CurieBLE/src/BLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ class BLEDevice
void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler); // set an event handler (callback)

protected:
friend class BLEDescriptorImp;
friend class BLECharacteristicImp;
friend class BLEServiceImp;
friend class BLEDeviceManager;
Expand All @@ -659,6 +660,11 @@ class BLEDevice
bt_gatt_read_params_t *params,
const void *data,
uint16_t length);
friend uint8_t profile_descriptor_read_rsp_process(bt_conn_t *conn,
int err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length);
const bt_addr_le_t* bt_le_address() const;
const bt_le_conn_param* bt_conn_param() const;
void setAddress(const bt_addr_le_t& addr);
Expand Down
Loading