define a new handler for dataWritten event;
this exposes the data to the callback handler.
This commit is contained in:
parent
04c2ea794f
commit
a601082563
1 changed files with 34 additions and 22 deletions
|
@ -77,7 +77,7 @@ ble_error_t nRF51GattServer::addService(GattService &service)
|
|||
p_characteristics[characteristicCount++] = p_char;
|
||||
|
||||
p_char->getValueAttribute().setHandle(charHandle);
|
||||
|
||||
|
||||
/* Add optional descriptors if any */
|
||||
/* ToDo: Make sure we don't overflow the array */
|
||||
for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
|
||||
|
@ -95,7 +95,7 @@ ble_error_t nRF51GattServer::addService(GattService &service)
|
|||
BLE_ERROR_PARAM_OUT_OF_RANGE );
|
||||
|
||||
uint16_t descHandle = descriptorCount;
|
||||
p_descriptors[descriptorCount++] = p_desc;
|
||||
p_descriptors[descriptorCount++] = p_desc;
|
||||
p_desc->setHandle(descHandle);
|
||||
}
|
||||
|
||||
|
@ -259,52 +259,51 @@ ble_error_t nRF51GattServer::getAppearance(uint16_t *appearanceP)
|
|||
/**************************************************************************/
|
||||
void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
|
||||
{
|
||||
uint16_t handle_value;
|
||||
GattServerEvents::gattEvent_t event;
|
||||
uint16_t handle_value;
|
||||
GattServerEvents::gattEvent_t eventType;
|
||||
const ble_gatts_evt_t *gattsEventP = &p_ble_evt->evt.gatts_evt;
|
||||
|
||||
switch (p_ble_evt->header.evt_id) {
|
||||
case BLE_GATTS_EVT_WRITE:
|
||||
/* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */
|
||||
|
||||
/* 1.) Handle CCCD changes */
|
||||
handle_value = p_ble_evt->evt.gatts_evt.params.write.handle;
|
||||
handle_value = gattsEventP->params.write.handle;
|
||||
for (uint8_t i = 0; i<characteristicCount; i++) {
|
||||
if ((p_characteristics[i]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
|
||||
(nrfCharacteristicHandles[i].cccd_handle == handle_value)) {
|
||||
uint16_t cccd_value =
|
||||
(p_ble_evt->evt.gatts_evt.params.write.data[1] << 8) |
|
||||
p_ble_evt->evt.gatts_evt.params.write.data[0]; /* Little Endian but M0 may be mis-aligned */
|
||||
(gattsEventP->params.write.data[1] << 8) |
|
||||
gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */
|
||||
|
||||
if (((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) ||
|
||||
((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) {
|
||||
event = GattServerEvents::GATT_EVENT_UPDATES_ENABLED;
|
||||
eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED;
|
||||
} else {
|
||||
event = GattServerEvents::GATT_EVENT_UPDATES_DISABLED;
|
||||
eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED;
|
||||
}
|
||||
|
||||
handleEvent(event, i);
|
||||
handleEvent(eventType, i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* 2.) Changes to the characteristic value will be handled with other events below */
|
||||
event = GattServerEvents::GATT_EVENT_DATA_WRITTEN;
|
||||
eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN;
|
||||
break;
|
||||
|
||||
case BLE_EVT_TX_COMPLETE:
|
||||
for (uint8_t i = 0; i<p_ble_evt->evt.common_evt.params.tx_complete.count; i++){
|
||||
handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT);
|
||||
}
|
||||
return;
|
||||
|
||||
case BLE_GATTS_EVT_HVC:
|
||||
/* Indication confirmation received */
|
||||
event = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED;
|
||||
handle_value = p_ble_evt->evt.gatts_evt.params.hvc.handle;
|
||||
eventType = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED;
|
||||
handle_value = gattsEventP->params.hvc.handle;
|
||||
break;
|
||||
|
||||
case BLE_EVT_TX_COMPLETE:
|
||||
handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT);
|
||||
return;
|
||||
|
||||
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
|
||||
sd_ble_gatts_sys_attr_set(p_ble_evt->evt.gatts_evt.conn_handle, NULL, 0);
|
||||
sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0);
|
||||
return;
|
||||
|
||||
default:
|
||||
|
@ -314,8 +313,21 @@ void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
|
|||
/* Find index (charHandle) in the pool */
|
||||
for (uint8_t i = 0; i<characteristicCount; i++) {
|
||||
if (nrfCharacteristicHandles[i].value_handle == handle_value) {
|
||||
handleEvent(event, i);
|
||||
break;
|
||||
switch (eventType) {
|
||||
case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
|
||||
GattCharacteristicWriteEvent eventData = {
|
||||
.op = static_cast<GattCharacteristicWriteEvent::Type>(gattsEventP->params.write.op),
|
||||
.offset = gattsEventP->params.write.offset,
|
||||
.len = gattsEventP->params.write.len,
|
||||
.data = gattsEventP->params.write.data
|
||||
};
|
||||
handleDataWrittenEvent(i, &eventData);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
handleEvent(eventType, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue