unknownconstant / ArduinoBLE

ArduinoBLE library for Arduino
GNU Lesser General Public License v2.1
6 stars 4 forks source link

Notify and Indicate not being set (solution provided) #19

Open MichaelRoop opened 3 years ago

MichaelRoop commented 3 years ago

This is likely in the main branch but I will enter it here to see if it can be incorporated here. Let me know if I have to work with whoever manages main Arduino BLE

Problem. Noticed that the Notify and Indicate were always seen as Disabled in my Win App. I dug into the ArduinoBLE code and found the following bug

BLELocalCharacteristic constructor (line 32 in BLELocalCharacteristic.cpp)

Existing constructor body

BLELocalCharacteristic::BLELocalCharacteristic(const char* uuid, uint16_t permissions, int valueSize, bool fixedLength) :
  BLELocalAttribute(uuid),
  _properties((uint8_t)(permissions&0x000FF)),
  _valueSize(min(valueSize, 512)),
  _valueLength(0),
  _fixedLength(fixedLength),
  _handle(0x0000),
  _broadcast(false),
  _written(false),
  _cccdValue(0x0000),
  _permissions((uint8_t)((permissions&0xFF00)>>8))
{
  memset(_eventHandlers, 0x00, sizeof(_eventHandlers));
  if (permissions & (BLENotify | BLEIndicate)) {
    BLELocalDescriptor* cccd = new BLELocalDescriptor("2902", (uint8_t*)&_cccdValue, sizeof(_cccdValue));
    _descriptors.add(cccd);
  }
  _value = (uint8_t*)malloc(valueSize);
}

My change to set the proper bits for the Notify and Indicate

BLELocalCharacteristic::BLELocalCharacteristic(const char* uuid, uint16_t permissions, int valueSize, bool fixedLength) :
  BLELocalAttribute(uuid),
  _properties((uint8_t)(permissions&0x000FF)),
  _valueSize(min(valueSize, 512)),
  _valueLength(0),
  _fixedLength(fixedLength),
  _handle(0x0000),
  _broadcast(false),
  _written(false),
  _cccdValue(0x0000),
  _permissions((uint8_t)((permissions&0xFF00)>>8))
{
  memset(_eventHandlers, 0x00, sizeof(_eventHandlers));

  if (permissions & (BLENotify | BLEIndicate)) {
     // *** Start of change
     // Set the bits for the Client Characteristic Config descriptor so Notify and Indicate are visible
     if (permissions & BLENotify) {
        _cccdValue = _cccdValue ^ 0x0001;
     }
     if (permissions & BLEIndicate) {
          _cccdValue = _cccdValue ^ 0x0002;
     }
     // *** End of change

    BLELocalDescriptor* cccd = new BLELocalDescriptor("2902", (uint8_t*)&_cccdValue, sizeof(_cccdValue));

    _descriptors.add(cccd);
  }

  _value = (uint8_t*)malloc(valueSize);
}
MichaelRoop commented 3 years ago

These changes are in the same branch as my aggregate format support changes are. (#issue 20).