This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

16bits to 128bits to 16bits convert problem

When discovering services using nrf51-ble-driver_win_0.4.1 driver, some ID are received with type BLE_UUID_TYPE_UNKNOWN. So I assume they are 128bits. I then retrieve the 128bits value (using sd_ble_gattc_read) and then register it using sd_ble_uuid_vs_add. This works fine and retrieve no error.

Later, I try to retrieve the 128bits uuid from 16bits using sd_ble_uuid_encode. This returns no error but did nothing.

Here is the code:

uint32_t Convert128To16BitsUUID( ble_uuid128_t uuid128,
                                 ble_uuid_t& result )
{
    uint8_t p_type;

    //add the uuid to the base    
    uint32_t err_code = sd_ble_uuid_vs_add( &uuid128, &p_type );
    if (err_code == NRF_SUCCESS)
    {
        uint16_t smalluuid;
        smalluuid = *((uint16_t*) (uuid128.uuid128 + 12));

        BLE_UUID_BLE_ASSIGN(result, smalluuid); 

        result.type = p_type;
     }
     return err_code;
}

void Fix128BitsUUID( const NRF51::PrivateDataRemoteDevice& device,
                                ble_uuid_t& uuid,
                                uint16_t handle )
{
    if ( uuid.type == BLE_UUID_TYPE_UNKNOWN && uuid.uuid == 0 )
    {
        uint8_t* value = NULL;
        uint16_t len = 0;
        if ( doReadValue( device, handle, value, len ).empty() )
        {
            if ( len == sizeof(ble_uuid128_t) ) 
            {
                // Mask 16-bit UUID part to zeros.
                //value[12] = 0x00; 
                //value[13] = 0x00;

                ble_uuid128_t uuid128;
                // Copy gathered 128bit UUID as future base.
                memcpy(uuid128.uuid128, value, sizeof(ble_uuid128_t));

                if ( Convert128To16BitsUUID( uuid128, uuid ) != NRF_SUCCESS )
                {
                    BTLE_LOG_STREAM( "Failed to register 128bits UUID", Error );
                }
                else
                {
                    BTLE_LOG_STREAM( "Correctly mapped 16bits UUID " + toHex( uuid.uuid ) + " to " + NRF51::PrivateDataService::UUID2String( uuid ), Info );

                    // Sanity check: Retrieve 128bits uuid from 16bits
                    uint8_t len = 0;
                    ble_uuid128_t restored128;
                    if ( sd_ble_uuid_encode(&uuid, &len, restored128.uuid128) == NRF_SUCCESS )
                    {
                        assert( len == 16 ); // len is actually 0....
                        assert( memcmp( restored128.uuid128, uuid128.uuid128, len*sizeof(uint8_t) ) == 0 );
                    }
                    else
                    {
                        assert( false );
                    }

                }
            }
        }
        else
        {
            BTLE_LOG_STREAM( "Failed to resolve 128bits UUID", Error );
        }
    }
}

Call to sd_ble_uuid_encode returns NRF_SUCCESS but len is 0 and restored128.uuid128 is unchanged, isn't len supposed to be 16 and restored128.uuid128 supposed to be equal to uuid128.uuid128?

Related