From 959cbeff4edaaf33d06f1e2b23ab89a845e82869 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 17 Oct 2017 15:26:43 +0700 Subject: [PATCH] get and response to HAP Chr Signature Read --- .../BLEHomekit/src/HAPCharacteristic.cpp | 180 ++++++++++++++++-- libraries/BLEHomekit/src/HAPCharacteristic.h | 3 + 2 files changed, 166 insertions(+), 17 deletions(-) diff --git a/libraries/BLEHomekit/src/HAPCharacteristic.cpp b/libraries/BLEHomekit/src/HAPCharacteristic.cpp index 2b9fa4ef..bc904e70 100644 --- a/libraries/BLEHomekit/src/HAPCharacteristic.cpp +++ b/libraries/BLEHomekit/src/HAPCharacteristic.cpp @@ -52,6 +52,8 @@ HAPCharacteristic::HAPCharacteristic(BLEUuid bleuuid, uint8_t format, uint16_t u _cid = 0; _hap_props = 0; + _hap_resp = NULL; + setPresentationFormatDescriptor(format, 0, unit, 1, 0); } @@ -77,7 +79,8 @@ void HAPCharacteristic::setHapProperties(uint16_t prop) err_t HAPCharacteristic::begin(void) { // Needed to response to HAP Procedure - _attr_meta.rd_auth = _attr_meta.wr_auth = 1; + _attr_meta.rd_auth = 1; + _attr_meta.wr_auth = 1; // Descriptors are not included in Gatt Table, only returned in // HAP Characteristic Signature Read procedure. @@ -123,20 +126,33 @@ void HAPCharacteristic::_eventHandler(ble_evt_t* event) { case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: { - ble_gatts_evt_rw_authorize_request_t * request = &event->evt.gatts_evt.params.authorize_request; - - ble_gatts_rw_authorize_reply_params_t reply = + /*------------- Handle HAP Request -------------*/ + if (event->evt.gatts_evt.params.authorize_request.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) { - .type = request->type, - // BLE_GATT_STATUS_SUCCESS - }; + ble_gatts_evt_write_t * gatt_req = &event->evt.gatts_evt.params.authorize_request.request.write; - // Handle HAP Request - if (request->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) - { - HAPRequest_t* hap_req = (HAPRequest_t*) request->request.write.data; + LOG_LV2(GATTS, "attr's value, uuid = 0x%04X", gatt_req->uuid.uuid); + PRINT2_BUFFER(gatt_req->data, gatt_req->len); + + HAPRequest_t* hap_req = (HAPRequest_t*) gatt_req->data; VERIFY(hap_req->header.control.type == HAP_PDU_REQUEST, ); +// PRINT_INT(hap_req->header.opcode); + + ble_gatts_rw_authorize_reply_params_t reply = + { + .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE, + .params = { + .write = { + .gatt_status = BLE_GATT_STATUS_SUCCESS, + .update = 1, + .offset = 0, + .len = 0, + .p_data = NULL + } + } + }; + switch(hap_req->header.opcode) { /* Return @@ -146,27 +162,157 @@ void HAPCharacteristic::_eventHandler(ble_evt_t* event) */ case HAP_OPCODE_CHR_SIGNATURE_READ: { + uint16_t svc_id = ((HAPService*)_service)->getSvcId(); + // ble_gatts_char_pf_t is not packed struct + struct ATTR_PACKED + { + uint8_t format; + int8_t exponent; + uint16_t unit; + uint8_t name_space; + uint16_t desc; + }fmt_desc = + { + .format = _format_desc.format, + .exponent = _format_desc.exponent, + .unit = _format_desc.unit, + .name_space = _format_desc.name_space, + .desc = _format_desc.desc + }; + + TLV8_t tlv_para[] = + { + { .type = HAP_PARAM_CHR_TYPE, .len = 16, .value = uuid._uuid128 }, + { .type = HAP_PARAM_SVC_TYPE, .len = 16, .value = _service->uuid._uuid128 }, + { .type = HAP_PARAM_SVC_ID , .len = 2 , .value = &svc_id }, + // Descriptors + { .type = HAP_PARAM_HAP_CHR_PROPERTIES_DESC, .len = 2 , .value = &_hap_props }, + { .type = HAP_PARAM_GATT_FORMAT_DESC , .len = sizeof(fmt_desc) , .value = &fmt_desc }, + }; + + // Determine body len + uint8_t body_len = 2; + for(uint8_t i=0; i < arrcount(tlv_para); i++) + { + body_len += tlv_para[i].len + 2; + } + + // Free if in used (a previous Response Read is skipped) + if ( _hap_resp ) rtos_free(_hap_resp); + + _hap_resp = (HAPResponse_t*) rtos_malloc(sizeof(HAPResponseHeader_t) + body_len); + VERIFY( _hap_resp != NULL, ); + + /*------------- Header -------------*/ + varclr(&_hap_resp->header.control); + _hap_resp->header.control.fragment = 0; + _hap_resp->header.control.type = HAP_PDU_RESPONSE; + + _hap_resp->header.tid = hap_req->header.tid; + _hap_resp->header.status = HAP_STATUS_SUCCESS; + _hap_resp->body_len = body_len; + + /*------------- Serialize Data -------------*/ + uint8_t* pdata = _hap_resp->body_data; + for(uint8_t i=0; i < arrcount(tlv_para); i++) + { + memcpy(pdata, &tlv_para[i], 2); // type + len + pdata += 2; + + memcpy(pdata, tlv_para[i].value, tlv_para[i].len); + pdata += tlv_para[i].len; + } } break; default: - reply.params.write.gatt_status = BLE_GATT_STATUS_UNKNOWN; +// reply.params.write.gatt_status = BLE_GATT_STATUS_UNKNOWN; break; } - sd_ble_gatts_rw_authorize_reply(Bluefruit.connHandle(), &reply); + VERIFY_STATUS( sd_ble_gatts_rw_authorize_reply(Bluefruit.connHandle(), &reply), ); } - // Handle HAP Response - if (request->type == BLE_GATTS_AUTHORIZE_TYPE_READ) + /*------------- Handle HAP Response -------------*/ + if (event->evt.gatts_evt.params.authorize_request.type == BLE_GATTS_AUTHORIZE_TYPE_READ) { - _rd_authorize_cb(*this, &request->request.read); + ble_gatts_evt_read_t * gatt_req = &event->evt.gatts_evt.params.authorize_request.request.read; + + LOG_LV2(GATTS, "attr's value, uuid = 0x%04X", gatt_req->uuid.uuid); + + ble_gatts_rw_authorize_reply_params_t reply = + { + .type = BLE_GATTS_AUTHORIZE_TYPE_READ, + .params = { + .read = { + .gatt_status = BLE_GATT_STATUS_UNKNOWN, + .update = 0, + .offset = 0, + .len = 0, + .p_data = NULL + } + } + }; + + if ( _hap_resp ) + { + reply.params.read.gatt_status = BLE_GATT_STATUS_SUCCESS; + reply.params.read.len = _hap_resp->body_len + sizeof(HAPResponseHeader_t) ; + reply.params.read.p_data = (uint8_t*) _hap_resp; + } + + PRINT2_BUFFER(reply.params.read.p_data, reply.params.read.len); + + VERIFY_STATUS( sd_ble_gatts_rw_authorize_reply(Bluefruit.connHandle(), &reply), ); + +// if ( _hap_resp ) +// { +// rtos_free(_hap_resp); +// _hap_resp = NULL; +// } } } break; + case BLE_GATTS_EVT_WRITE: + { + ble_gatts_evt_write_t* request = &event->evt.gatts_evt.params.write; + + // Value write + if (request->handle == _handles.value_handle) + { + LOG_LV2(GATTS, "attr's value, uuid = 0x%04X", request->uuid.uuid); + PRINT2_BUFFER(request->data, request->len); + + // TODO Ada callback + if (_wr_cb) _wr_cb(*this, request->data, request->len, request->offset); + } + + // CCCD write + if ( request->handle == _handles.cccd_handle ) + { + LOG_LV2(GATTS, "attr's cccd"); + PRINT2_BUFFER(request->data, request->len); + + // Invoke callback if set + if (_cccd_wr_cb) + { + uint16_t value; + memcpy(&value, request->data, 2); + _cccd_wr_cb(*this, value); + } + + // TODO could move later + // Save CCCD if bonded to file whenever changed + extern void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle); + if ( Bluefruit.connPaired() ) + { + ada_callback(NULL, _adafruit_save_bond_cccd_dfr, event->evt.common_evt.conn_handle); + } + } + } + default: break; } } - diff --git a/libraries/BLEHomekit/src/HAPCharacteristic.h b/libraries/BLEHomekit/src/HAPCharacteristic.h index 7bc37786..753f830f 100644 --- a/libraries/BLEHomekit/src/HAPCharacteristic.h +++ b/libraries/BLEHomekit/src/HAPCharacteristic.h @@ -37,6 +37,7 @@ #define HAPCHARACTERISTIC_H_ #include +#include "HAPProcedure.h" enum HAPChrProperties_t { @@ -68,6 +69,8 @@ class HAPCharacteristic : public BLECharacteristic uint16_t _cid; uint16_t _hap_props; + HAPResponse_t* _hap_resp; + err_t _addChrIdDescriptor(void); err_t _addHapDescriptor (void); };