void nRF5xGattServer::hwCallback(const ble_evt_t *p_ble_evt)
{
    GattAttribute::Handle_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 = gattsEventP->params.write.handle;
                int characteristicIndex = resolveCCCDHandleToCharIndex(handle_value);
                if ((characteristicIndex != -1) &&
                    (p_characteristics[characteristicIndex]->getProperties() &
                        (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) {

                    uint16_t cccd_value = (gattsEventP->params.write.data[1] << 8) | gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */

                    if (((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) ||
                        ((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) {
                        eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED;
                    } else {
                        eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED;
                    }

                    handleEvent(eventType, p_characteristics[characteristicIndex]->getValueHandle());
                    return;
                }

                /* 2.) Changes to the characteristic value will be handled with other events below */
                eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN;
            }
            break;

        case BLE_GATTS_EVT_HVC:
            /* Indication confirmation received */
            eventType    = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED;
            handle_value = gattsEventP->params.hvc.handle;
            break;
        
#if  NRF_SD_BLE_API_VERSION >= 4 // This event has been renamed in API V4+
        case BLE_GATTS_EVT_HVN_TX_COMPLETE: {
            handleDataSentEvent(p_ble_evt->evt.gatts_evt.params.hvn_tx_complete.count);
            return;
        }
#else
        case BLE_EVT_TX_COMPLETE: {
            handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count);
            return;
        }
#endif

        case BLE_GATTS_EVT_SYS_ATTR_MISSING:
            sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0, 0);
            return;

        case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
            switch (gattsEventP->params.authorize_request.type) {
                case BLE_GATTS_AUTHORIZE_TYPE_READ:
                    eventType    = GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ;
                    handle_value = gattsEventP->params.authorize_request.request.read.handle;
                    break;
                case BLE_GATTS_AUTHORIZE_TYPE_WRITE:
                    eventType    = GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ;
                    handle_value = gattsEventP->params.authorize_request.request.write.handle;
                    break;
                default:
                    return;
            }
            break;

        case BLE_EVT_USER_MEM_REQUEST: {
            uint16_t conn_handle = p_ble_evt->evt.common_evt.conn_handle;

            // allocate a new long request for this connection
            // NOTE: we don't care about the result at this stage,
            // it is not possible to cancel the operation anyway.
            // If the request was not allocated then it will gracefully failled
            // at subsequent stages.
            allocateLongWriteRequest(conn_handle);
            sd_ble_user_mem_reply(conn_handle, NULL);
            return;
        }

        default:
            return;
    }

    int characteristicIndex = resolveValueHandleToCharIndex(handle_value);
    if (characteristicIndex == -1) {
        // filter out the case were the request is a long one,
        // and there is no attribute handle provided
        uint8_t write_op = gattsEventP->params.authorize_request.request.write.op;
        if (eventType != GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ ||
            (write_op != BLE_GATTS_OP_EXEC_WRITE_REQ_NOW &&
             write_op != BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)) {
            return;
        }
    }

    /* Find index (charHandle) in the pool */
    switch (eventType) {
        case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
            GattWriteCallbackParams cbParams = {
                /* .connHandle = */ gattsEventP->conn_handle,
                /* .handle     = */ handle_value,
                /* .writeOp    = */ static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.write.op),
                /* .offset     = */ gattsEventP->params.write.offset,
                /* .len        = */ gattsEventP->params.write.len,
                /* .data       = */ gattsEventP->params.write.data
            };
            handleDataWrittenEvent(&cbParams);
            break;
        }
        case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: {
            uint16_t conn_handle = gattsEventP->conn_handle;
            const ble_gatts_evt_write_t& input_req = gattsEventP->params.authorize_request.request.write;
            const uint16_t max_size = getBiggestCharacteristicSize();

            // this is a long write request, handle it here.
            switch (input_req.op) {
                case BLE_GATTS_OP_PREP_WRITE_REQ: {
                    // verify that the request is not outside of the possible range
                    if ((input_req.offset + input_req.len) > max_size) {
                        sd_ble_gatts_rw_authorize_reply(conn_handle, &write_auth_invalid_offset_reply);
                        releaseLongWriteRequest(conn_handle);
                        return;
                    }

                    // find the write request
                    long_write_request_t* req = findLongWriteRequest(conn_handle);
                    if (!req)  {
                        sd_ble_gatts_rw_authorize_reply(conn_handle, &write_auth_invalid_reply);
                        return;
                    }

                    // initialize the first request by setting the offset
                    if (req->length == 0) {
                        req->attr_handle = input_req.handle;
                        req->offset = input_req.offset;
                    } else {
                        // it should be the subsequent write
                        if ((req->offset + req->length) != input_req.offset) {
                            sd_ble_gatts_rw_authorize_reply(conn_handle, &write_auth_invalid_offset_reply);
                            releaseLongWriteRequest(conn_handle);
                            return;
                        }

                        // it is not allowed to write multiple characteristic with the same request
                        if (input_req.handle != req->attr_handle) {
                            sd_ble_gatts_rw_authorize_reply(conn_handle, &write_auth_invalid_reply);
                            releaseLongWriteRequest(conn_handle);
                            return;
                        }
                    }

                    // start the copy of what is in input
                    memcpy(req->data + req->length, input_req.data, input_req.len);

                    // update the lenght of the data written
                    req->length = req->length + input_req.len;

                    // success, signal it to the softdevice
                    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 = */ input_req.offset,
                                /* .len = */ input_req.len,
                                /* .p_data = */ input_req.data
                            }
                        }
                    };

                    sd_ble_gatts_rw_authorize_reply(conn_handle, &reply);
                }   return;

                case BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL: {
                    releaseLongWriteRequest(conn_handle);
                    sd_ble_gatts_rw_authorize_reply(conn_handle, &write_auth_succes_reply);
                }   return;

                case BLE_GATTS_OP_EXEC_WRITE_REQ_NOW: {
                    long_write_request_t* req = findLongWriteRequest(conn_handle);
                    if (!req) {
                        sd_ble_gatts_rw_authorize_reply(conn_handle, &write_auth_invalid_reply);
                        return;
                    }

                    GattWriteAuthCallbackParams cbParams = {
                        /* .connHandle = */ conn_handle,
                        /* .handle     = */ req->attr_handle,
                        /* .offset     = */ req->offset,
                        /* .len        = */ req->length,
                        /* .data       = */ req->data,
                        /* .authorizationReply = */ AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member
                                                                           * set to AUTH_CALLBACK_REPLY_SUCCESS if the client
                                                                           * request is to proceed. */
                    };
                    uint16_t write_authorization = p_characteristics[characteristicIndex]->authorizeWrite(&cbParams);

                    // the user code didn't provide the write authorization,
                    // just leave here.
                    if (write_authorization != AUTH_CALLBACK_REPLY_SUCCESS) {
                        // report the status of the operation in any cases
                        sd_ble_gatts_rw_authorize_reply(conn_handle, &write_auth_invalid_reply);
                        releaseLongWriteRequest(conn_handle);
                        return;
                    }

                    // FIXME can't use ::write here, this function doesn't take the offset into account ...
                    ble_gatts_value_t value = {
                        /* .len     = */ req->length,
                        /* .offset  = */ req->offset,
                        /* .p_value = */ req->data
                    };
                    uint32_t update_err = sd_ble_gatts_value_set(conn_handle, req->attr_handle, &value);
                    if (update_err) {
                        sd_ble_gatts_rw_authorize_reply(conn_handle, &write_auth_invalid_reply);
                        releaseLongWriteRequest(conn_handle);
                        return;
                    }

                    sd_ble_gatts_rw_authorize_reply(conn_handle, &write_auth_succes_reply);

                    GattWriteCallbackParams writeParams = {
                        /* .connHandle = */ conn_handle,
                        /* .handle     = */ req->attr_handle,
                        /* .writeOp    = */ static_cast<GattWriteCallbackParams::WriteOp_t>(input_req.op),
                        /* .offset     = */ req->offset,
                        /* .len        = */ req->length,
                        /* .data       = */ req->data,
                    };
                    handleDataWrittenEvent(&writeParams);
                    releaseLongWriteRequest(conn_handle);
                }   return;
            }

            GattWriteAuthCallbackParams cbParams = {
                /* .connHandle = */ gattsEventP->conn_handle,
                /* .handle     = */ handle_value,
                /* .offset     = */ gattsEventP->params.authorize_request.request.write.offset,
                /* .len        = */ gattsEventP->params.authorize_request.request.write.len,
                /* .data       = */ gattsEventP->params.authorize_request.request.write.data,
                /* .authorizationReply = */ AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member
                                                                   * set to AUTH_CALLBACK_REPLY_SUCCESS if the client
                                                                   * request is to proceed. */
            };

            ble_gatts_rw_authorize_reply_params_t reply = {
                /* .type = */ BLE_GATTS_AUTHORIZE_TYPE_WRITE,
                /* .params = */ {
                    /* .write = */ {
                        /* .gatt_status = */ p_characteristics[characteristicIndex]->authorizeWrite(&cbParams),
                        /* .update = */ 1,
                        /* .offset = */ cbParams.offset,
                        /* .len = */ cbParams.len,
                        /* .p_data = */ cbParams.data
                    }
                }
            };

            if (reply.params.write.gatt_status != BLE_GATT_STATUS_SUCCESS)
            {
                reply.params.write.update = 0;
            }

            sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);

            /*
             * If write-authorization is enabled for a characteristic,
             * AUTHORIZATION_REQ event (if replied with true) is *not*
             * followed by another DATA_WRITTEN event; so we still need
             * to invoke handleDataWritten(), much the same as we would
             * have done if write-authorization had not been enabled.
             */
            if (reply.params.write.gatt_status == BLE_GATT_STATUS_SUCCESS) {
                GattWriteCallbackParams cbParams = {
                    /* .connHandle = */ gattsEventP->conn_handle,
                    /* .handle     = */ handle_value,
                    /* .writeOp    = */ static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.authorize_request.request.write.op),
                    /* .offset     = */ gattsEventP->params.authorize_request.request.write.offset,
                    /* .len        = */ gattsEventP->params.authorize_request.request.write.len,
                    /* .data       = */ gattsEventP->params.authorize_request.request.write.data,
                };
                handleDataWrittenEvent(&cbParams);
            }
            break;
        }
        case GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ: {
            GattReadAuthCallbackParams cbParams = {
                /* .connHandle         = */ gattsEventP->conn_handle,
                /* .handle             = */ handle_value,
                /* .offset             = */ gattsEventP->params.authorize_request.request.read.offset,
                /* .len                = */ 0,
                /* .data               = */ NULL,
                /* .authorizationReply = */ AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member
                                                                   * set to AUTH_CALLBACK_REPLY_SUCCESS if the client
                                                                   * request is to proceed. */
            };

            ble_gatts_rw_authorize_reply_params_t reply = {
                /* .type = */ BLE_GATTS_AUTHORIZE_TYPE_READ,
                /* .params = */ {
                    /* .read = */ {
                        /* .gatt_status = */ p_characteristics[characteristicIndex]->authorizeRead(&cbParams)
                    }
                }
            };

            if (cbParams.authorizationReply == BLE_GATT_STATUS_SUCCESS) {
                if (cbParams.data != NULL) {
                    reply.params.read.update = 1;
                    reply.params.read.offset = cbParams.offset;
                    reply.params.read.len    = cbParams.len;
                    reply.params.read.p_data = cbParams.data;
                }
            }

            sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
            break;
        }

        default:
            handleEvent(eventType, handle_value);
            break;
    }
}
Example #2
0
void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
{
    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 = 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 =
                        (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))) {
                        eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED;
                    } else {
                        eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED;
                    }

                    handleEvent(eventType, i);
                    return;
                }
            }

            /* 2.) Changes to the characteristic value will be handled with other events below */
            eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN;
            break;

        case BLE_GATTS_EVT_HVC:
            /* Indication confirmation received */
            eventType    = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED;
            handle_value = gattsEventP->params.hvc.handle;
            break;

        case BLE_EVT_TX_COMPLETE: {
            handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count);
            return;
        }

        case BLE_GATTS_EVT_SYS_ATTR_MISSING:
            sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0);
            return;

        default:
            return;
    }

    /* Find index (charHandle) in the pool */
    for (uint8_t i = 0; i<characteristicCount; i++) {
        if (nrfCharacteristicHandles[i].value_handle == handle_value) {
            switch (eventType) {
                case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
                    GattCharacteristicWriteCBParams cbParams = {
                        .charHandle = i,
                        .op     = static_cast<GattCharacteristicWriteCBParams::Type>(gattsEventP->params.write.op),
                        .offset = gattsEventP->params.write.offset,
                        .len    = gattsEventP->params.write.len,
                        .data   = gattsEventP->params.write.data
                    };
                    handleDataWrittenEvent(&cbParams);
                    break;
                }
                default:
                    handleEvent(eventType, i);
                    break;
            }
        }
    }
}

ble_error_t
nRF51GattServer::initializeGATTDatabase(void)
{
    /* Empty. Services are populated in the GattDatabase through addService(). */
    return BLE_ERROR_NONE;
}