diff --git a/libraries/BLEHomekit/BLEHomekit.h b/libraries/BLEHomekit/BLEHomekit.h index 8a305bf8..d9303900 100644 --- a/libraries/BLEHomekit/BLEHomekit.h +++ b/libraries/BLEHomekit/BLEHomekit.h @@ -37,6 +37,9 @@ #define BLEHOMEKIT_H_ #include "HAPUuid.h" +#include "HAPCharacteristic.h" +#include "HAPService.h" + #include "HapAccessoryInfo.h" enum diff --git a/libraries/BLEHomekit/HAPCharacteristic.cpp b/libraries/BLEHomekit/HAPCharacteristic.cpp index 71708567..2e1200b4 100644 --- a/libraries/BLEHomekit/HAPCharacteristic.cpp +++ b/libraries/BLEHomekit/HAPCharacteristic.cpp @@ -34,5 +34,53 @@ */ /**************************************************************************/ +#include +#include +#include "BLEHomekit.h" +BLEUuid HAPCharacteristic::_g_uuid_cid(HAP_UUID_CHR_CHARACTERISTIC_ID); + +err_t HAPCharacteristic::begin(void) +{ + VERIFY_STATUS( BLECharacteristic::begin() ); + return _addChrID(); +} + +/** + * Add Characteristic Instance ID descriptor + * @return status code + */ +err_t HAPCharacteristic::_addChrID(void) +{ + // Add Descriptor UUID if not yet added + if (_g_uuid_cid._uuid.type == BLE_UUID_TYPE_UNKNOWN) + { + _g_uuid_cid.begin(); + } + + _cid = BLEHomekit::_gInstanceID++; + + ble_gatts_attr_md_t cid_md = + { + .read_perm = BLE_SECMODE_OPEN, + .write_perm = BLE_SECMODE_NO_ACCESS, + .vlen = 0, + .vloc = BLE_GATTS_VLOC_STACK + }; + + ble_gatts_attr_t cid_desc = + { + .p_uuid = &_g_uuid_cid._uuid, + .p_attr_md = &cid_md, + .init_len = 2, + .init_offs = 0, + .max_len = 2, + .p_value = (uint8_t*) &_cid + }; + + uint16_t ref_hdl; + VERIFY_STATUS ( sd_ble_gatts_descriptor_add(BLE_GATT_HANDLE_INVALID, &cid_desc, &ref_hdl) ); + + return ERROR_NONE; +} diff --git a/libraries/BLEHomekit/HAPCharacteristic.h b/libraries/BLEHomekit/HAPCharacteristic.h index dc53f4e5..04d6f1c0 100644 --- a/libraries/BLEHomekit/HAPCharacteristic.h +++ b/libraries/BLEHomekit/HAPCharacteristic.h @@ -38,6 +38,20 @@ #include -class HAPCharacteristic +class HAPCharacteristic : public BLECharacteristic +{ + public: + static BLEUuid _g_uuid_cid; + + HAPCharacteristic(BLEUuid bleuuid) : BLECharacteristic(bleuuid) { _cid = 0; } + + virtual err_t begin(void); + + private: + uint16_t _cid; + + err_t _addChrID(void); + +}; #endif /* HAPCHARACTERISTIC_H_ */ diff --git a/libraries/BLEHomekit/HAPService.cpp b/libraries/BLEHomekit/HAPService.cpp index 90991935..a97b7868 100644 --- a/libraries/BLEHomekit/HAPService.cpp +++ b/libraries/BLEHomekit/HAPService.cpp @@ -37,7 +37,6 @@ #include #include #include "BLEHomekit.h" -#include "HAPService.h" err_t HAPService::begin(void) { @@ -46,6 +45,10 @@ err_t HAPService::begin(void) return _addSvcID(); } +/** + * Add HAP Service Instance Characteristic + * @return status code + */ err_t HAPService::_addSvcID(void) { BLECharacteristic chr(HAP_UUID_CHR_SERVICE_ID); diff --git a/libraries/BLEHomekit/HAPUuid.h b/libraries/BLEHomekit/HAPUuid.h index 59337e7a..e6255c5d 100644 --- a/libraries/BLEHomekit/HAPUuid.h +++ b/libraries/BLEHomekit/HAPUuid.h @@ -36,6 +36,10 @@ #ifndef HAPUUID_H_ #define HAPUUID_H_ +#define HAP_UUID_CHR_SERVICE_ID ((const uint8_t[]) { 0xD1, 0xA0, 0x83, 0x50, 0x00, 0xAA, 0xD3, 0x87, 0x17, 0x48, 0x59, 0xA7, 0x5D, 0xE9, 0x04, 0xE6 }) +#define HAP_UUID_CHR_CHARACTERISTIC_ID ((const uint8_t[]) { 0x9A, 0x93, 0x96, 0xD7, 0xBD, 0x6A, 0xD9, 0xB5, 0x16, 0x46, 0xD2, 0x81, 0xFE, 0xF0, 0x46, 0xDC }) +#define HAP_UUID_CHR_SERVICE_SIGNATURE ((const uint8_t[]) { 0x91, 0x52, 0x76, 0xBB, 0x26, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xA5, 0x00, 0x00, 0x00 }) + /*------------------------------------------------------------------*/ /* PAIRING *------------------------------------------------------------------*/ @@ -195,13 +199,4 @@ #define HAP_UUID_CHR_SERVICE_LABLE_NAMESPACE ((const uint8_t[]) { 0x91, 0x52, 0x76, 0xBB, 0x26, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xCD, 0x00, 0x00, 0x00 }) #define HAP_UUID_CHR_COLOR_TEMP ((const uint8_t[]) { 0x91, 0x52, 0x76, 0xBB, 0x26, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xCE, 0x00, 0x00, 0x00 }) - -#define HAP_UUID_CHR_SERVICE_ID ((const uint8_t[]) { 0xD1, 0xA0, 0x83, 0x50, 0x00, 0xAA, 0xD3, 0x87, 0x17, 0x48, 0x59, 0xA7, 0x5D, 0xE9, 0x04, 0xE6 }) -#define HAP_UUID_CHR_CHARACTERISTIC_ID ((const uint8_t[]) { 0x9A, 0x93, 0x96, 0xD7, 0xBD, 0x6A, 0xD9, 0xB5, 0x16, 0x46, 0xD2, 0x81, 0xFE, 0xF0, 0x46, 0xDC }) -#define HAP_UUID_CHR_SERVICE_SIGNATURE ((const uint8_t[]) { 0x91, 0x52, 0x76, 0xBB, 0x26, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xA5, 0x00, 0x00, 0x00 }) - -/*------------------------------------------------------------------*/ -/* Characteristics - *------------------------------------------------------------------*/ - #endif /* HAPUUID_H_ */ diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.h b/libraries/Bluefruit52Lib/src/BLECharacteristic.h index 914520f1..08e8caa7 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.h @@ -137,7 +137,7 @@ class BLECharacteristic void setReadAuthorizeCallback(read_authorize_cb_t fp); void setWriteAuthorizeCallbak(write_authorize_cb_t fp); - err_t begin(void); + virtual err_t begin(void); ble_gatts_char_handles_t handles(void);