Ejemplo n.º 1
0
ble_error_t nRF51GattServer::addService(GattService &service)
{
    /* ToDo: Make sure we don't overflow the array, etc. */
    /* ToDo: Make sure this service UUID doesn't already exist (?) */
    /* ToDo: Basic validation */

    /* Add the service to the nRF51 */
    ble_uuid_t nordicUUID;
    nordicUUID = custom_convert_to_nordic_uuid(service.getUUID());

    uint16_t serviceHandle;
    // ASSERT( ERROR_NONE ==      //_modify
            // sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                     // &nordicUUID,
                                     // &serviceHandle),
            // BLE_ERROR_PARAM_OUT_OF_RANGE );
    nrf_err_check(sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                     &nordicUUID,
                                     &serviceHandle) );        
            
    service.setHandle(serviceHandle);

    /* Add characteristics to the service */
    for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) {
        GattCharacteristic *p_char = service.getCharacteristic(i);

        nordicUUID = custom_convert_to_nordic_uuid(p_char->getUUID());

        // ASSERT ( ERROR_NONE ==    //_modify
                 // custom_add_in_characteristic(service.getHandle(),
                                              // &nordicUUID,
                                              // p_char->getProperties(),
                                              // NULL,
                                              // p_char->getInitialLength(),
                                              // p_char->getMaxLength(),
                                              // &nrfCharacteristicHandles[characteristicCount]),
                 // BLE_ERROR_PARAM_OUT_OF_RANGE );
        nrf_err_check(custom_add_in_characteristic(service.getHandle(),
                                              &nordicUUID,
                                              p_char->getProperties(),
                                              NULL,
                                              p_char->getInitialLength(),
                                              p_char->getMaxLength(),
                                              &nrfCharacteristicHandles[characteristicCount]));
                 
        /* Update the characteristic handle */
        uint16_t charHandle = characteristicCount;
        p_characteristics[characteristicCount++] = p_char;

        p_char->setHandle(charHandle);
        if ((p_char->getValuePtr() != NULL) && (p_char->getInitialLength() > 0)) {
            updateValue(charHandle, p_char->getValuePtr(), p_char->getInitialLength(), false /* localOnly */);
        }
    }

    serviceCount++;

    return BLE_ERROR_NONE;
}
ble_error_t BlueNRGGattServer::addService(GattService &service)
{
    /* ToDo: Make sure we don't overflow the array, etc. */
    /* ToDo: Make sure this service UUID doesn't already exist (?) */
    /* ToDo: Basic validation */

    tBleStatus ret;
    uint8_t type;
    uint16_t short_uuid;
    uint8_t primary_short_uuid[2];
    uint8_t primary_base_uuid[16];
    uint8_t char_base_uuid[16];
    const uint8_t *base_uuid;
    const uint8_t *base_char_uuid;

    uint8_t charsCount = service.getCharacteristicCount();
    const uint8_t available_characteristics = BLE_TOTAL_CHARACTERISTICS - characteristicCount;

    // check that there is enough characteristics left in the
    // characteristic array.
    if (charsCount > available_characteristics) {
        PRINTF("charCount = %u and characteristicCount = %u\r\n", charsCount, available_characteristics);
        return BLE_ERROR_NO_MEM;
    }

    const uint16_t maxAttrRecords = computeAttributesRecord(service);

    type = (service.getUUID()).shortOrLong();
    PRINTF("AddService(): Type:%d\n\r", type);

    /* Add the service to the BlueNRG */
    short_uuid = (service.getUUID()).getShortUUID();
    STORE_LE_16(primary_short_uuid, short_uuid);

    if(type==UUID::UUID_TYPE_LONG) {
        base_uuid = (service.getUUID()).getBaseUUID();

        COPY_UUID_128(primary_base_uuid, base_uuid[15],base_uuid[14],primary_short_uuid[1],primary_short_uuid[0],base_uuid[11],base_uuid[10],base_uuid[9],base_uuid[8],base_uuid[7],base_uuid[6],base_uuid[5],base_uuid[4],base_uuid[3],base_uuid[2],base_uuid[1],base_uuid[0]);
    }

    ret = BLE_STATUS_SUCCESS;

    if(type==UUID::UUID_TYPE_SHORT) {
        ret = aci_gatt_add_serv(UUID_TYPE_16,
                                primary_short_uuid,
                                PRIMARY_SERVICE,
                                maxAttrRecords/*7*/,
                                &servHandle);
        PRINTF("aci_gatt_add_serv UUID_TYPE_SHORT ret=%d\n\r", ret);

    } else if(type==UUID::UUID_TYPE_LONG) {
        ret = aci_gatt_add_serv(UUID_TYPE_128,
                                primary_base_uuid,
                                PRIMARY_SERVICE,
                                maxAttrRecords/*7*/,
                                &servHandle);
        PRINTF("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret);
    }

    switch (ret) {
        case BLE_STATUS_SUCCESS:
            break;

        case BLE_STATUS_INVALID_PARAMETER:
            return BLE_ERROR_INVALID_PARAM;

        case BLE_STATUS_OUT_OF_HANDLE:
        case BLE_STATUS_INSUFFICIENT_RESOURCES:
        case ERR_UNSPECIFIED_ERROR:
            return BLE_ERROR_NO_MEM;

        case BLE_STATUS_ERROR:
        default:
            return BLE_ERROR_INTERNAL_STACK_FAILURE;
    }



    service.setHandle(servHandle);
    //serviceHandleVector.push_back(servHandle);
    PRINTF("added servHandle handle =%u\n\r", servHandle);
    uint16_t bleCharacteristic;

    //iterate to include all characteristics
    for (uint8_t i = 0; i < charsCount; i++) {
        GattCharacteristic *p_char = service.getCharacteristic(i);
        uint16_t char_uuid = (p_char->getValueAttribute().getUUID()).getShortUUID();

        uint8_t int_8_uuid[2];
        STORE_LE_16(int_8_uuid, char_uuid);

        type = (p_char->getValueAttribute().getUUID()).shortOrLong();

        if(type==UUID::UUID_TYPE_LONG) {
            base_char_uuid = (p_char->getValueAttribute().getUUID()).getBaseUUID();
#ifdef DEBUG
            for(uint8_t j=0; j<16; j++) {
                PRINTF("base_char_uuid[%d] 0x%02x ", j, base_char_uuid[j]);
            }
            PRINTF("\n\r");
#endif
            COPY_UUID_128(char_base_uuid,base_char_uuid[15],base_char_uuid[14],int_8_uuid[1],int_8_uuid[0],base_char_uuid[11],base_char_uuid[10],base_char_uuid[9],base_char_uuid[8],base_char_uuid[7],base_char_uuid[6],base_char_uuid[5],base_char_uuid[4],base_char_uuid[3],base_char_uuid[2],base_char_uuid[1],base_char_uuid[0]);
        }

        PRINTF("Char Properties 0x%x\n\r", p_char->getProperties());
        /*
        * Gatt_Evt_Mask -> HardCoded (0)
        * Encryption_Key_Size -> Hardcoded (16)
        * isVariable (variable length value field) -> Hardcoded (1)
        */
        uint8_t Gatt_Evt_Mask = 0x0;

        if((p_char->getProperties() &
                    (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) {
            PRINTF("Setting up Gatt GATT_NOTIFY_ATTRIBUTE_WRITE Mask\n\r");
            Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_ATTRIBUTE_WRITE  | GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP;
        }
        if((p_char->getProperties() &
                    (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ|
                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) {
            PRINTF("Setting up Gatt GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP Mask\n\r");
            Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP;
        }    //This will support also GATT_SERVER_ATTR_READ_WRITE since it will be covered by previous if() check.

        if(type==UUID::UUID_TYPE_SHORT) {
            ret =  aci_gatt_add_char(service.getHandle(),
                                     UUID_TYPE_16,
                                     int_8_uuid,
                                     p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
                                     p_char->getProperties(),
                                     ATTR_PERMISSION_NONE,
                                     Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
                                     16 /*Encryption_Key_Size*/,
                                     1 /*isVariable*/,
                                     &bleCharacteristic);

            PRINTF("aci_gatt_add_char UUID_TYPE_16 props=%d MaxLength=%d ret=%d\n\r",
                    p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret);

        } else if(type==UUID::UUID_TYPE_LONG) {
            ret =  aci_gatt_add_char(service.getHandle(),
                                     UUID_TYPE_128,
                                     char_base_uuid,
                                     p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
                                     p_char->getProperties(),
                                     ATTR_PERMISSION_NONE,
                                     Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
                                     16 /*Encryption_Key_Size*/,
                                     1 /*isVariable*/,
                                     &bleCharacteristic);

            PRINTF("aci_gatt_add_char UUID_TYPE_128 props=%d MaxLength=%d ret=%d\n\r",
                    p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret);
        }

        switch (ret) {
            case BLE_STATUS_SUCCESS:
                break;

            case ERR_UNSPECIFIED_ERROR:
            case BLE_STATUS_INSUFFICIENT_RESOURCES:
            case BLE_STATUS_OUT_OF_HANDLE:
                // TODO remove characteristics and the service previously added.
                // remove service in the stack by using: Aci_Gatt_Del_Service
                // remove characteristics in the stack by using: Aci_Gatt_Del_Char
                // update service counter
                // destroy registered characteristic and updat echaracteristic counter
                return BLE_ERROR_NO_MEM;

            case BLE_STATUS_INVALID_HANDLE:
            case BLE_STATUS_INVALID_PARAMETER:
            case BLE_STATUS_CHARAC_ALREADY_EXISTS:
            // TODO remove characteristics and the service previously added.
            // remove service in the stack by using: Aci_Gatt_Del_Service
            // remove characteristics in the stack by using: Aci_Gatt_Del_Char
            // update service counter
            // destroy registered characteristic and updat echaracteristic counter
                return BLE_ERROR_INVALID_PARAM;

            case BLE_STATUS_ERROR:
            default:
            // TODO remove characteristics and the service previously added.
            // remove service in the stack by using: Aci_Gatt_Del_Service
            // remove characteristics in the stack by using: Aci_Gatt_Del_Char
            // update service counter
            // destroy registered characteristic and updat echaracteristic counter
                return BLE_ERROR_INTERNAL_STACK_FAILURE;
        }

        bleCharHandleMap.insert(std::pair<uint16_t, uint16_t>(bleCharacteristic, servHandle));

        p_characteristics[characteristicCount++] = p_char;
        /* Set the characteristic value handle */
        p_char->getValueAttribute().setHandle(bleCharacteristic+BlueNRGGattServer::CHAR_VALUE_HANDLE);
        PRINTF("added bleCharacteristic (value handle =%u)\n\r", p_char->getValueAttribute().getHandle());

        if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getLength() > 0)) {
            ble_error_t err = write(p_char->getValueAttribute().getHandle(),
                  p_char->getValueAttribute().getValuePtr(),
                  p_char->getValueAttribute().getLength(), false /* localOnly */);
            if (err) {
                PRINTF("ERROR HERE !!!!\r\n");
                return err;
            }
        }

        // add descriptors now
        uint16_t descHandle = 0;
        PRINTF("p_char->getDescriptorCount()=%d\n\r", p_char->getDescriptorCount());

        for(uint8_t descIndex=0; descIndex<p_char->getDescriptorCount(); descIndex++) {
            GattAttribute *descriptor = p_char->getDescriptor(descIndex);
            uint8_t desc_uuid[16] = { 0 };


            uint8_t desc_uuid_type = CHAR_DESC_TYPE_16_BIT;
            STORE_LE_16(desc_uuid, descriptor->getUUID().getShortUUID());

            if((descriptor->getUUID()).shortOrLong() == UUID::UUID_TYPE_LONG) {
                desc_uuid_type = CHAR_DESC_TYPE_128_BIT;
                const uint8_t* base_desc_uuid  = descriptor->getUUID().getBaseUUID();

                COPY_UUID_128(desc_uuid, base_desc_uuid[15], base_desc_uuid[14],base_desc_uuid[13],base_desc_uuid[12], base_desc_uuid[11], base_desc_uuid[10], base_desc_uuid[9], base_desc_uuid[8], base_desc_uuid[7], base_desc_uuid[6], base_desc_uuid[5], base_desc_uuid[4], base_desc_uuid[3], base_desc_uuid[2], base_desc_uuid[1], base_desc_uuid[0]);
            }

            ret = aci_gatt_add_char_desc(service.getHandle(),
                                         bleCharacteristic,
                                         desc_uuid_type,
                                         desc_uuid,
                                         descriptor->getMaxLength(),
                                         descriptor->getLength(),
                                         descriptor->getValuePtr(),
                                         CHAR_DESC_SECURITY_PERMISSION,
                                         CHAR_DESC_ACCESS_PERMISSION,
                                         GATT_NOTIFY_ATTRIBUTE_WRITE,
                                         MIN_ENCRY_KEY_SIZE,
                                         CHAR_ATTRIBUTE_LEN_IS_FIXED,
                                         &descHandle);
            PRINTF("Adding Descriptor descriptor handle=%d ret=%d\n\r", descHandle, ret);

            switch (ret) {
                case BLE_STATUS_SUCCESS:
                    PRINTF("Descriptor added successfully, descriptor handle=%d\n\r", descHandle);
                    descriptor->setHandle(descHandle);
                    break;

                case ERR_UNSPECIFIED_ERROR:
                case BLE_STATUS_INSUFFICIENT_RESOURCES:
                case BLE_STATUS_OUT_OF_HANDLE:
                    // TODO remove characteristics and the service previously added.
                    // remove service in the stack by using: Aci_Gatt_Del_Service
                    // remove characteristics in the stack by using: Aci_Gatt_Del_Char
                    // update service counter
                    // destroy registered characteristic and updat echaracteristic counter
                    return BLE_ERROR_NO_MEM;

                case BLE_STATUS_INVALID_HANDLE:
                case BLE_STATUS_INVALID_PARAMETER:
                // TODO remove characteristics and the service previously added.
                // remove service in the stack by using: Aci_Gatt_Del_Service
                // remove characteristics in the stack by using: Aci_Gatt_Del_Char
                // update service counter
                // destroy registered characteristic and updat echaracteristic counter
                    return BLE_ERROR_INVALID_PARAM;

                case BLE_STATUS_INVALID_OPERATION:
                    return BLE_ERROR_OPERATION_NOT_PERMITTED;

                case BLE_STATUS_ERROR:
                default:
                // TODO remove characteristics and the service previously added.
                // remove service in the stack by using: Aci_Gatt_Del_Service
                // remove characteristics in the stack by using: Aci_Gatt_Del_Char
                // update service counter
                // destroy registered characteristic and updat echaracteristic counter
                    return BLE_ERROR_INTERNAL_STACK_FAILURE;
            }
        }
    }

    serviceCount++;

    //FIXME: There is no GattService pointer array in GattServer.
    //        There should be one? (Only the user is aware of GattServices!) Report to forum.

    return BLE_ERROR_NONE;
}
Ejemplo n.º 3
0
ble_error_t nRF5xGattServer::addService(GattService &service)
{
    /* ToDo: Make sure this service UUID doesn't already exist (?) */
    /* ToDo: Basic validation */

    /* Add the service to the nRF51 */
    ble_uuid_t nordicUUID;
    nordicUUID = custom_convert_to_nordic_uuid(service.getUUID());

    uint16_t serviceHandle;
    ASSERT_TRUE( ERROR_NONE ==
            sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                     &nordicUUID,
                                     &serviceHandle),
            BLE_ERROR_PARAM_OUT_OF_RANGE );
    service.setHandle(serviceHandle);

    /* Add characteristics to the service */
    for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) {
        if (characteristicCount >= BLE_TOTAL_CHARACTERISTICS) {
            return BLE_ERROR_NO_MEM;
        }
        GattCharacteristic *p_char = service.getCharacteristic(i);
        GattAttribute *p_description_descriptor = NULL;
        GattAttribute *p_presentation_format_descriptor = NULL;

        /* Skip any incompletely defined, read-only characteristics. */
        if ((p_char->getValueAttribute().getValuePtr() == NULL) &&
            (p_char->getValueAttribute().getLength() == 0) &&
            (p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) {
            continue;
        }

        nordicUUID = custom_convert_to_nordic_uuid(p_char->getValueAttribute().getUUID());

        /* The user-description and presentation-format descriptors are special cases
         * that need to be handled at the time of adding each characteristic. The
         * following block is meant to discover their presence. */
        const uint8_t *userDescriptionDescriptorValuePtr = NULL;
        uint16_t userDescriptionDescriptorValueLen = 0;
        const uint8_t *presentationFormatDescriptorValuePtr = NULL;
        uint16_t presentationFormatDescriptorValueLen = 0;
        for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
            GattAttribute *p_desc = p_char->getDescriptor(j);
            if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) {
                p_description_descriptor = p_desc;
                userDescriptionDescriptorValuePtr = p_desc->getValuePtr();
                userDescriptionDescriptorValueLen = p_desc->getLength();
            }
            if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_PRESENTATION_FORMAT) {
                p_presentation_format_descriptor = p_desc;
                presentationFormatDescriptorValuePtr = p_desc->getValuePtr();
                presentationFormatDescriptorValueLen = p_desc->getLength();
            }
        }

        ASSERT_TRUE ( ERROR_NONE ==
                 custom_add_in_characteristic(
                     BLE_GATT_HANDLE_INVALID,
                                              &nordicUUID,
                                              p_char->getProperties(),
                     p_char->getReadSecurityRequirement(),
                     p_char->getWriteSecurityRequirement(),
                     p_char->getUpdateSecurityRequirement(),
                                              p_char->getValueAttribute().getValuePtr(),
                                              p_char->getValueAttribute().getLength(),
                                              p_char->getValueAttribute().getMaxLength(),
                                              p_char->getValueAttribute().hasVariableLength(),
                                              userDescriptionDescriptorValuePtr,
                                              userDescriptionDescriptorValueLen,
                                              presentationFormatDescriptorValuePtr,
                                              presentationFormatDescriptorValueLen,
                                              p_char->isReadAuthorizationEnabled(),
                                              p_char->isWriteAuthorizationEnabled(),
                     &nrfCharacteristicHandles[characteristicCount]
                 ),
                 BLE_ERROR_PARAM_OUT_OF_RANGE );

        /* Update the characteristic handle */
        p_characteristics[characteristicCount] = p_char;
        p_char->getValueAttribute().setHandle(nrfCharacteristicHandles[characteristicCount].value_handle);
        if (p_description_descriptor) {
            p_description_descriptor->setHandle(
                nrfCharacteristicHandles[characteristicCount].user_desc_handle
            );
        }
        if (p_presentation_format_descriptor) {
            // The handle is not available from the SoftDevice
            p_presentation_format_descriptor->setHandle(GattAttribute::INVALID_HANDLE);
        }
        characteristicCount++;

        /* Add optional descriptors if any */
        for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
            if (descriptorCount >= BLE_TOTAL_DESCRIPTORS) {
                return BLE_ERROR_NO_MEM;
            }

            GattAttribute *p_desc = p_char->getDescriptor(j);
            /* skip the user-description or presentation-format descriptor here;
             * they have already been handled when adding the characteristic (above). */
            if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC
                || p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_PRESENTATION_FORMAT) {
                continue;
            }

            nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID());

            ASSERT_TRUE(ERROR_NONE ==
                   custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID,
                                            &nordicUUID,
                                            p_desc->getValuePtr(),
                                            p_desc->getLength(),
                                            p_desc->getMaxLength(),
                                            p_desc->hasVariableLength(),
                                            &nrfDescriptorHandles[descriptorCount],
                                            p_desc->getReadSecurityRequirement(),
                                            p_desc->getWriteSecurityRequirement()),
                BLE_ERROR_PARAM_OUT_OF_RANGE);

            p_descriptors[descriptorCount] = p_desc;
            p_desc->setHandle(nrfDescriptorHandles[descriptorCount]);
            descriptorCount++;
        }
    }

    serviceCount++;

    return BLE_ERROR_NONE;
}
Ejemplo n.º 4
0
ble_error_t nRF5xGattServer::addService(GattService &service)
{
    /* ToDo: Make sure we don't overflow the array, etc. */
    /* ToDo: Make sure this service UUID doesn't already exist (?) */
    /* ToDo: Basic validation */

    /* Add the service to the nRF51 */
    ble_uuid_t nordicUUID;
    nordicUUID = custom_convert_to_nordic_uuid(service.getUUID());

    uint16_t serviceHandle;
    ASSERT( ERROR_NONE ==
            sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                     &nordicUUID,
                                     &serviceHandle),
            BLE_ERROR_PARAM_OUT_OF_RANGE );
    service.setHandle(serviceHandle);

    /* Add characteristics to the service */
    for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) {
        GattCharacteristic *p_char = service.getCharacteristic(i);

        /* Skip any incompletely defined, read-only characteristics. */
        if ((p_char->getValueAttribute().getValuePtr() == NULL) &&
            (p_char->getValueAttribute().getInitialLength() == 0) &&
            (p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) {
            continue;
        }

        nordicUUID = custom_convert_to_nordic_uuid(p_char->getValueAttribute().getUUID());

        /* The user-description descriptor is a special case which needs to be
         * handled at the time of adding the characteristic. The following block
         * is meant to discover its presence. */
        const uint8_t *userDescriptionDescriptorValuePtr = NULL;
        uint16_t userDescriptionDescriptorValueLen = 0;
        for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
            GattAttribute *p_desc = p_char->getDescriptor(j);
            if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) {
                userDescriptionDescriptorValuePtr = p_desc->getValuePtr();
                userDescriptionDescriptorValueLen = p_desc->getLength();
            }
        }

        ASSERT ( ERROR_NONE ==
                 custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID,
                                              &nordicUUID,
                                              p_char->getProperties(),
                                              p_char->getRequiredSecurity(),
                                              p_char->getValueAttribute().getValuePtr(),
                                              p_char->getValueAttribute().getInitialLength(),
                                              p_char->getValueAttribute().getMaxLength(),
                                              userDescriptionDescriptorValuePtr,
                                              userDescriptionDescriptorValueLen,
                                              p_char->isReadAuthorizationEnabled(),
                                              p_char->isWriteAuthorizationEnabled(),
                                              &nrfCharacteristicHandles[characteristicCount]),
                 BLE_ERROR_PARAM_OUT_OF_RANGE );

        /* Update the characteristic handle */
        p_characteristics[characteristicCount] = p_char;
        p_char->getValueAttribute().setHandle(nrfCharacteristicHandles[characteristicCount].value_handle);
        characteristicCount++;

        /* Add optional descriptors if any */
        /* ToDo: Make sure we don't overflow the array */
        for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
            GattAttribute *p_desc = p_char->getDescriptor(j);
            /* skip the user-description-descriptor here; this has already been handled when adding the characteristic (above). */
            if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) {
                continue;
            }

            nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID());

            ASSERT(ERROR_NONE ==
                   custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID,
                                            &nordicUUID,
                                            p_desc->getValuePtr(),
                                            p_desc->getInitialLength(),
                                            p_desc->getMaxLength(),
                                            &nrfDescriptorHandles[descriptorCount]),
                BLE_ERROR_PARAM_OUT_OF_RANGE);

            p_descriptors[descriptorCount++] = p_desc;
            p_desc->setHandle(nrfDescriptorHandles[descriptorCount]);
        }
    }

    serviceCount++;

    return BLE_ERROR_NONE;
}
Ejemplo n.º 5
0
ble_error_t MaximGattServer::addService(GattService &service)
{
    currentHandle = (currentHandle + 0xF) & ~0xF;
    uint16_t startHandle = currentHandle;

    mxmService_t *mxmSvc = new mxmService_t;

    // Create WiCentric attribute group
    mxmSvc->attGroup = new attsGroup_t;

    // Determine the attribute list length
    unsigned int attListLen = 1;
    for (int i = 0; i < service.getCharacteristicCount(); i++) {
        attListLen += 2;
        GattCharacteristic *p_char = service.getCharacteristic(i);

        if (p_char->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE)) {
            // add a CCCD
            attListLen++;
        }
    }

    // Create WiCentric attribute list
    mxmSvc->attGroup->pAttr = (attsAttr_t*)malloc(attListLen * sizeof(attsAttr_t));;
    if (mxmSvc->attGroup->pAttr == NULL) {
        return BLE_ERROR_BUFFER_OVERFLOW;
    }

    // Create characteristics
    mxmSvc->chars = new mxmChar_t [service.getCharacteristicCount()];

    attsAttr_t *currAtt = mxmSvc->attGroup->pAttr;

    /* Service */
    currAtt->pUuid = attPrimSvcUuid;
    if (service.getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) {
        mxmSvc->uuidLen = UUID::LENGTH_OF_LONG_UUID;
    } else {
        mxmSvc->uuidLen = sizeof(UUID::ShortUUIDBytes_t);
    }
    currAtt->pValue = (uint8_t*)malloc(mxmSvc->uuidLen);
    memcpy(currAtt->pValue, service.getUUID().getBaseUUID(), mxmSvc->uuidLen);
    currAtt->maxLen = mxmSvc->uuidLen;
    currAtt->pLen = &mxmSvc->uuidLen;
    currAtt->settings = 0;
    currAtt->permissions = ATTS_PERMIT_READ;

    currAtt++;

    /* Add characteristics to the service */
    for (int i = 0; i < service.getCharacteristicCount(); i++) {
        GattCharacteristic *p_char = service.getCharacteristic(i);

        /* Skip any incompletely defined, read-only characteristics. */
        if ((p_char->getValueAttribute().getValuePtr() == NULL) &&
            (p_char->getValueAttribute().getLength() == 0) &&
            (p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) {
            continue;
        }

        // Create Characteristic Attribute
        currentHandle += 2;
        currAtt->pUuid = attChUuid;

        p_char->getValueAttribute().setHandle(currentHandle);
        mxmSvc->chars[i].descLen = 1 + sizeof(currentHandle) + p_char->getValueAttribute().getUUID().getLen();
        currAtt->pValue = (uint8_t*)malloc(mxmSvc->chars[i].descLen);
        uint8_t *pValue = currAtt->pValue;
        *pValue++ = p_char->getProperties();
        memcpy(pValue, &currentHandle, sizeof(currentHandle));
        pValue += sizeof(currentHandle);
        memcpy(pValue, p_char->getValueAttribute().getUUID().getBaseUUID(), p_char->getValueAttribute().getUUID().getLen());

        currAtt->pLen = &mxmSvc->chars[i].descLen;
        currAtt->maxLen = mxmSvc->chars[i].descLen;
        currAtt->settings = 0;
        currAtt->permissions = ATTS_PERMIT_READ;
        currAtt++;

        // Create Value Attribute
        currAtt->pUuid = p_char->getValueAttribute().getUUID().getBaseUUID();
        currAtt->pValue = p_char->getValueAttribute().getValuePtr();
        currAtt->pLen = p_char->getValueAttribute().getLengthPtr();
        currAtt->maxLen = p_char->getValueAttribute().getMaxLength();
        currAtt->settings = ATTS_SET_WRITE_CBACK | ATTS_SET_READ_CBACK;
        if (p_char->getValueAttribute().getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) {
            currAtt->settings |= ATTS_SET_UUID_128;
        }
        currAtt->permissions = 0;
        if (p_char->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)  { currAtt->permissions |= ATTS_PERMIT_READ; }
        if (p_char->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) { currAtt->permissions |= ATTS_PERMIT_WRITE; }
        currAtt++;

        bool cccCreated = false;

        for (int i = 0; i < p_char->getDescriptorCount(); i++) {
            GattAttribute *p_att = p_char->getDescriptor(i);

            currentHandle++;

            currAtt->pUuid = p_att->getUUID().getBaseUUID();
            currAtt->pValue = p_att->getValuePtr();
            currAtt->pLen = p_att->getLengthPtr();
            currAtt->maxLen = p_att->getMaxLength();
            currAtt->settings = 0;
            currAtt->permissions = 0;
            if (p_att->getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) {
                currAtt->settings |= ATTS_SET_UUID_128;
            }
            if (p_att->getUUID() == UUID(BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG)) {
                cccCreated = true;
                currAtt->settings |= ATTS_SET_CCC;
                currAtt->permissions |= ATTS_PERMIT_READ;
                currAtt->permissions |= ATTS_PERMIT_WRITE;

                if (cccCnt < MAX_CCC_CNT) {
                    cccSet[cccCnt].handle = currentHandle;
                    cccSet[cccCnt].valueRange = 0;
                    if (p_char->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
                        cccSet[cccCnt].valueRange |= ATT_CLIENT_CFG_NOTIFY;
                    }
                    if (p_char->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) {
                        cccSet[cccCnt].valueRange |= ATT_CLIENT_CFG_INDICATE;
                    }
                    cccSet[cccCnt].secLevel = DM_SEC_LEVEL_NONE;
                    cccHandles[cccCnt] = p_char->getValueAttribute().getHandle();
                    cccCnt++;
                } else {
                    return BLE_ERROR_PARAM_OUT_OF_RANGE;
                }
            }
            currAtt++;
        }

        if (!cccCreated && (p_char->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) {
            /* There was not a CCCD included in the descriptors, but this
             * characteristic is notifiable and/or indicatable. A CCCD is
             * required so create one now.
             */
            if (cccCnt >= MAX_CCC_CNT) {
                return BLE_ERROR_PARAM_OUT_OF_RANGE;
            }

            currentHandle++;

            currAtt->pUuid = cccUUID.getBaseUUID();
            currAtt->pValue = (uint8_t*)&cccValues[cccCnt];
            currAtt->pLen = (uint16_t*)&cccSize;
            currAtt->maxLen = sizeof(uint16_t);
            currAtt->settings = ATTS_SET_CCC;
            currAtt->permissions = (ATTS_PERMIT_READ | ATTS_PERMIT_WRITE);

            cccSet[cccCnt].handle = currentHandle;
            cccSet[cccCnt].valueRange = 0;
            if (p_char->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
                cccSet[cccCnt].valueRange |= ATT_CLIENT_CFG_NOTIFY;
            }
            if (p_char->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) {
                cccSet[cccCnt].valueRange |= ATT_CLIENT_CFG_INDICATE;
            }
            cccSet[cccCnt].secLevel = DM_SEC_LEVEL_NONE;
            cccHandles[cccCnt] = p_char->getValueAttribute().getHandle();

            cccCnt++;
            currAtt++;
        }
    }

    mxmSvc->attGroup->pNext = NULL;
    mxmSvc->attGroup->readCback = attsReadCback;
    mxmSvc->attGroup->writeCback = attsWriteCback;
    mxmSvc->attGroup->startHandle = startHandle;
    mxmSvc->attGroup->endHandle = currentHandle;
    AttsAddGroup(mxmSvc->attGroup);

    AttRegister(attCback);
    AttsCccRegister(cccCnt, (attsCccSet_t*)cccSet, cccCback);

    return BLE_ERROR_NONE;
}
Ejemplo n.º 6
0
ble_error_t BlueNRGGattServer::addService(GattService &service)
{
    /* ToDo: Make sure we don't overflow the array, etc. */
    /* ToDo: Make sure this service UUID doesn't already exist (?) */
    /* ToDo: Basic validation */
    
    tBleStatus ret;
    uint8_t type;
    uint16_t short_uuid;
    uint8_t primary_short_uuid[2];
    uint8_t primary_base_uuid[16];
    uint8_t char_base_uuid[16];
    const uint8_t *base_uuid;
    const uint8_t *base_char_uuid;
    
    uint8_t charsCount = 0;
    uint8_t maxAttrRecords = 0;

    type = (service.getUUID()).shortOrLong();
    PRINTF("AddService(): Type:%d\n\r", type);
    
    /* Add the service to the BlueNRG */
    short_uuid = (service.getUUID()).getShortUUID();
    STORE_LE_16(primary_short_uuid, short_uuid);
    
    if(type==UUID::UUID_TYPE_LONG) {
        base_uuid = (service.getUUID()).getBaseUUID();   
        
        COPY_UUID_128(primary_base_uuid, base_uuid[15],base_uuid[14],primary_short_uuid[1],primary_short_uuid[0],base_uuid[11],base_uuid[10],base_uuid[9],base_uuid[8],base_uuid[7],base_uuid[6],base_uuid[5],base_uuid[4],base_uuid[3],base_uuid[2],base_uuid[1],base_uuid[0]);
    }

    charsCount = service.getCharacteristicCount();
    //1(service record)+2records*char+1record*char_desc
    maxAttrRecords = 1+3*charsCount;

    if(type==UUID::UUID_TYPE_SHORT) {
        ret = aci_gatt_add_serv(UUID_TYPE_16,
                                primary_short_uuid,
                                PRIMARY_SERVICE,
                                maxAttrRecords/*7*/,
                                &servHandle);
        PRINTF("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret);
    }
    else if(type==UUID::UUID_TYPE_LONG) {
        ret = aci_gatt_add_serv(UUID_TYPE_128,
                                primary_base_uuid,
                                PRIMARY_SERVICE,
                                maxAttrRecords/*7*/,
                                &servHandle);
        PRINTF("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret);
    }
    
    service.setHandle(servHandle);
    //serviceHandleVector.push_back(servHandle);
    PRINTF("added servHandle handle =%u\n\r", servHandle);
    uint16_t bleCharacteristic;
    
    //iterate to include all characteristics
    for (uint8_t i = 0; i < charsCount; i++) {
        GattCharacteristic *p_char = service.getCharacteristic(i);
        uint16_t char_uuid = (p_char->getValueAttribute().getUUID()).getShortUUID();   
        
        uint8_t int_8_uuid[2];
        STORE_LE_16(int_8_uuid, char_uuid);
        
        if(type==UUID::UUID_TYPE_LONG) {
            base_char_uuid = (p_char->getValueAttribute().getUUID()).getBaseUUID();
            
            COPY_UUID_128(char_base_uuid,base_char_uuid[15],base_char_uuid[14],int_8_uuid[1],int_8_uuid[0],base_char_uuid[11],base_char_uuid[10],base_char_uuid[9],base_char_uuid[8],base_char_uuid[7],base_char_uuid[6],base_char_uuid[5],base_char_uuid[4],base_char_uuid[3],base_char_uuid[2],base_char_uuid[1],base_char_uuid[0]);
        }
        
        PRINTF("Char Properties 0x%x\n\r", p_char->getProperties());
        /*
        * Gatt_Evt_Mask -> HardCoded (0)
        * Encryption_Key_Size -> Hardcoded (16)
        * isVariable (variable length value field) -> Hardcoded (1)
        */
        uint8_t Gatt_Evt_Mask = 0x0;

        if((p_char->getProperties() &
                    (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) {
            PRINTF("Setting up Gatt GATT_NOTIFY_ATTRIBUTE_WRITE Mask\n\r");
            Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_ATTRIBUTE_WRITE;
        }
        if((p_char->getProperties() &
                    (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ|
                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) {
            PRINTF("Setting up Gatt GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP Mask\n\r");
            Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP; 
        }    //This will support also GATT_SERVER_ATTR_READ_WRITE since it will be covered by previous if() check.

        if(type==UUID::UUID_TYPE_SHORT) {
            ret =  aci_gatt_add_char(service.getHandle(),
                                     UUID_TYPE_16,
                                     int_8_uuid,
                                     p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
                                     p_char->getProperties(),
                                     ATTR_PERMISSION_NONE,
                                     Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
                                     16 /*Encryption_Key_Size*/,
                                     1 /*isVariable*/,
                                     &bleCharacteristic);
            
            PRINTF("aci_gatt_add_char UUID_TYPE_16 props=%d MaxLength=%d ret=%d\n\r", p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret);

        } else if(type==UUID::UUID_TYPE_LONG) {
            ret =  aci_gatt_add_char(service.getHandle(),
                                     UUID_TYPE_128,
                                     char_base_uuid,
                                     p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
                                     p_char->getProperties(),
                                     ATTR_PERMISSION_NONE,
                                     Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
                                     16 /*Encryption_Key_Size*/,
                                     1 /*isVariable*/,
                                     &bleCharacteristic);
            
            PRINTF("aci_gatt_add_char UUID_TYPE_128 props=%d MaxLength=%d ret=%d\n\r", p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret);
        }

        /* Update the characteristic handle */
        //uint16_t charHandle = characteristicCount;   
        
        bleCharHanldeMap.insert(std::pair<uint16_t, uint16_t>(bleCharacteristic, servHandle)); 
        
        p_characteristics[characteristicCount++] = p_char;
        p_char->getValueAttribute().setHandle(bleCharacteristic);    //Set the characteristic count as the corresponding char handle
        PRINTF("added bleCharacteristic handle =%u\n\r", bleCharacteristic);

        if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getLength() > 0)) {
            write(p_char->getValueAttribute().getHandle(), p_char->getValueAttribute().getValuePtr(), p_char->getValueAttribute().getLength(), false /* localOnly */);
        }

        // add descriptors now
        uint16_t descHandle = 0;
        PRINTF("p_char->getDescriptorCount()=%d\n\r", p_char->getDescriptorCount());
        
        for(uint8_t descIndex=0; descIndex<p_char->getDescriptorCount(); descIndex++) {
            GattAttribute *descriptor = p_char->getDescriptor(descIndex);
            uint16_t shortUUID = descriptor->getUUID().getShortUUID();
            const uint8_t uuidArray[] = {(uint8_t)((shortUUID>>8)&0xFF), (uint8_t)((shortUUID&0xFF))};
            ret = aci_gatt_add_char_desc(service.getHandle(), p_char->getValueAttribute().getHandle(), 
            CHAR_DESC_TYPE_16_BIT, uuidArray, descriptor->getMaxLength(), descriptor->getLength(),
            descriptor->getValuePtr(), CHAR_DESC_SECURITY_PERMISSION, CHAR_DESC_ACCESS_PERMISSION, GATT_NOTIFY_ATTRIBUTE_WRITE,
            MIN_ENCRY_KEY_SIZE, CHAR_ATTRIBUTE_LEN_IS_FIXED, &descHandle);
            PRINTF("Adding Descriptor descriptor handle=%d ret=%d\n\r", descHandle, ret);
            if(ret==(tBleStatus)0) {
                PRINTF("Descriptor added successfully, descriptor handle=%d\n\r", descHandle);
                descriptor->setHandle(descHandle);
            }
        }

    }    
    
    serviceCount++;
    
    //FIXME: There is no GattService pointer array in GattServer. 
    //        There should be one? (Only the user is aware of GattServices!) Report to forum.
    
    return BLE_ERROR_NONE;
}
Ejemplo n.º 7
0
ble_error_t nRF51GattServer::addService(GattService &service)
{
    /* ToDo: Make sure we don't overflow the array, etc. */
    /* ToDo: Make sure this service UUID doesn't already exist (?) */
    /* ToDo: Basic validation */

    /* Add the service to the nRF51 */
    ble_uuid_t nordicUUID;
    nordicUUID = custom_convert_to_nordic_uuid(service.getUUID());

    uint16_t serviceHandle;
    ASSERT( ERROR_NONE ==
            sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                     &nordicUUID,
                                     &serviceHandle),
            BLE_ERROR_PARAM_OUT_OF_RANGE );
    service.setHandle(serviceHandle);

    /* Add characteristics to the service */
    for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) {
        GattCharacteristic *p_char = service.getCharacteristic(i);

        /* Skip any incompletely defined, read-only characteristics. */
        if ((p_char->getValueAttribute().getValuePtr() == NULL) &&
            (p_char->getValueAttribute().getInitialLength() == 0) &&
            (p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) {
            continue;
        }

        nordicUUID = custom_convert_to_nordic_uuid(p_char->getValueAttribute().getUUID());

        ASSERT ( ERROR_NONE ==
                 custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID,
                                              &nordicUUID,
                                              p_char->getProperties(),
                                              p_char->getValueAttribute().getValuePtr(),
                                              p_char->getValueAttribute().getInitialLength(),
                                              p_char->getValueAttribute().getMaxLength(),
                                              &nrfCharacteristicHandles[characteristicCount]),
                 BLE_ERROR_PARAM_OUT_OF_RANGE );

        /* Update the characteristic handle */
        uint16_t charHandle = characteristicCount;
        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++) {
             GattAttribute *p_desc = p_char->getDescriptor(j);

             nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID());

             ASSERT ( ERROR_NONE ==
                      custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID,
                                               &nordicUUID,
                                               p_desc->getValuePtr(),
                                               p_desc->getInitialLength(),
                                               p_desc->getMaxLength(),
                                               &nrfDescriptorHandles[descriptorCount]),
                 BLE_ERROR_PARAM_OUT_OF_RANGE );

            uint16_t descHandle = descriptorCount;
            p_descriptors[descriptorCount++] = p_desc;
            p_desc->setHandle(descHandle);
        }
    }

    serviceCount++;

    return BLE_ERROR_NONE;
}