Exemplo n.º 1
0
/*
 * Takes a temperature reading
 */
static wiced_result_t take_temperature_reading( void* arg )
{
    UNUSED_PARAMETER(arg);

    wiced_rtos_lock_mutex( &xively_data.mutex );

    /* Take thermistor reading */
    if ( thermistor_take_sample( THERMISTOR_ADC, &xively_data.last_sample ) != WICED_SUCCESS )
    {
        wiced_rtos_unlock_mutex( &xively_data.mutex );
        return WICED_ERROR;
    }

    /* Get the current ISO8601 time */
    wiced_time_get_iso8601_time( &xively_data.temperature_readings[xively_data.temperature_reading_index].timestamp );

    /* Create sample string */
    xively_u16toa( xively_data.last_sample / 10, xively_data.temperature_readings[xively_data.temperature_reading_index].sample, 2 );
    xively_data.temperature_readings[xively_data.temperature_reading_index].sample[2] = '.';
    xively_u16toa( xively_data.last_sample % 10, &xively_data.temperature_readings[xively_data.temperature_reading_index].sample[3], 1 );

    if ( ( ++xively_data.temperature_reading_index ) == MAX_TEMPERATURE_READINGS )
    {
        xively_data.temperature_reading_index = 0;
    }

    wiced_rtos_unlock_mutex( &xively_data.mutex );

    return WICED_SUCCESS;
}
wiced_result_t bt_smartbridge_att_cache_generate( const wiced_bt_smart_device_t* remote_device, uint16_t connection_handle, bt_smartbridge_att_cache_t** cache )
{
    bt_smartbridge_att_cache_t* new_cache = NULL;
    wiced_result_t              result;

    if ( cache == NULL )
    {
        return WICED_BT_BADARG;
    }

    if ( att_cache_manager == NULL )
    {
        return WICED_BT_ATT_CACHE_UNINITIALISED;
    }

    /* Cached attributes not found. Get a free instance and discover services */
    result = smartbridge_att_cache_get_free_cache( &new_cache );
    if ( result != WICED_BT_SUCCESS )
    {
        return result;
    }

    /* Copy remote device to cache */
    wiced_rtos_lock_mutex( &new_cache->mutex );
    memcpy( &new_cache->remote_device, remote_device, sizeof( new_cache->remote_device ) );
    new_cache->connection_handle = connection_handle;
    new_cache->is_discovering    = WICED_TRUE;
    wiced_rtos_unlock_mutex( &new_cache->mutex );

    /* Rediscover services */
    result = smartbridge_att_cache_discover_all( new_cache, new_cache->connection_handle );

    wiced_rtos_lock_mutex( &new_cache->mutex );
    new_cache->is_discovering = WICED_FALSE;
    wiced_rtos_unlock_mutex( &new_cache->mutex );

    if ( result == WICED_BT_SUCCESS )
    {
        result = smartbridge_att_cache_insert_to_used_list( new_cache );

        if ( result == WICED_SUCCESS )
        {
            *cache = new_cache;
        }
    }
    else
    {
        smartbridge_att_cache_return_to_free_list( new_cache );
    }

    return result;
}
Exemplo n.º 3
0
int platform_printf(const char* fmt, ...)
{
    if (!stdout_mtx_inited)
    {
        wiced_rtos_init_mutex(&stdout_mtx);
        stdout_mtx_inited++;
    }

    wiced_rtos_lock_mutex(&stdout_mtx);

    va_list vl;
    va_start(vl, fmt);

    char msg[512];
    unsigned n = vsnprintf(msg, sizeof msg, fmt, vl);
    if (n >= sizeof msg)
        msg[sizeof msg - 1] = '\0';

    int rc = printf("%s", msg);

    va_end(vl);

    wiced_rtos_unlock_mutex(&stdout_mtx);

    return rc;
}
Exemplo n.º 4
0
/*
 * Decreases temperature set point
 */
static void decrease_setpoint( void )
{
    wiced_rtos_lock_mutex( &setpoint.mutex );
    setpoint.temperature -= ( setpoint.temperature > MIN_SETPOINT ) ? SETPOINT_INCREMENT : 0.0f;
    adjust_setpoint_led_brightness( );
    wiced_rtos_unlock_mutex( &setpoint.mutex );
}
Exemplo n.º 5
0
wiced_result_t bt_packet_pool_allocate_packet( bt_packet_pool_t* pool, bt_packet_t** packet )
{
    linked_list_node_t* node   = NULL;
    wiced_result_t      result = WICED_BT_SUCCESS;

    if ( pool == NULL || packet == NULL || pool == DYNAMIC_PACKET_POOL )
    {
        return WICED_BT_BADARG;
    }

    wiced_rtos_lock_mutex( &pool->mutex );

    result = linked_list_remove_node_from_rear( &pool->pool_list, &node );

    if ( result == WICED_BT_SUCCESS )
    {
        (*packet)              = (bt_packet_t*)node->data;
        (*packet)->node.data   = (void*)(*packet);
        (*packet)->packet_id   = PACKET_ID;
        (*packet)->pool        = pool;
        (*packet)->owner       = BT_PACKET_OWNER_STACK;
        (*packet)->packet_end  = (*packet)->packet_start + pool->header_size + pool->data_size;
        (*packet)->data_start  = (*packet)->packet_start + pool->header_size;
        (*packet)->data_end    = (*packet)->data_start;
        pool->packet_created++;
    }
    else if ( result == WICED_NOT_FOUND )
    {
        result = WICED_BT_PACKET_POOL_EXHAUSTED;
    }

    wiced_rtos_unlock_mutex( &pool->mutex );

    return result;
}
wiced_result_t bt_smartbridge_att_cache_find( const wiced_bt_smart_device_t* remote_device, bt_smartbridge_att_cache_t** cache )
{
    wiced_result_t  result;
    bt_list_node_t* node_found;

    if ( remote_device == NULL || cache == NULL )
    {
        return WICED_BT_BADARG;
    }

    if ( att_cache_manager == NULL )
    {
        return WICED_BT_ATT_CACHE_UNINITIALISED;
    }

    /* Lock protection */
    result = wiced_rtos_lock_mutex( &att_cache_manager->mutex );
    if ( result != WICED_SUCCESS )
    {
        return result;
    }

    result = bt_linked_list_find( &att_cache_manager->used_list, smartbridge_att_cache_find_by_device_callback, (void*)remote_device, &node_found );
    if ( result == WICED_BT_SUCCESS )
    {
        *cache = (bt_smartbridge_att_cache_t*)node_found->data;
    }

    /* Unlock protection */
    wiced_rtos_unlock_mutex( &att_cache_manager->mutex );
    return result;
}
Exemplo n.º 7
0
/*
 * Gets current temperature set point
 */
static float get_setpoint( void )
{
    float current_setpoint;

    wiced_rtos_lock_mutex( &setpoint.mutex );
    current_setpoint = setpoint.temperature;
    wiced_rtos_unlock_mutex( &setpoint.mutex );

    return current_setpoint;
}
Exemplo n.º 8
0
int platform_mutex_unlock(Mutex* m)
{
    if (!m)
    {
        platform_printf("%s: invalid mutex %p\n", __func__, m);
        return -1;
    }

    wiced_result_t rc = wiced_rtos_unlock_mutex(&m->mutex);
    if (rc != WICED_SUCCESS)
        platform_printf("%s: FAILED to unlock %p: %d\n", __func__, &m->mutex, rc);
    return 0;
}
wiced_result_t bt_smartbridge_att_cache_unlock( bt_smartbridge_att_cache_t* cache )
{
    if ( cache == NULL )
    {
        return WICED_BADARG;
    }

    if ( att_cache_manager == NULL )
    {
        return WICED_BT_ATT_CACHE_UNINITIALISED;
    }

    return wiced_rtos_unlock_mutex( &cache->mutex );
}
static wiced_result_t smartbridge_att_cache_get_free_cache( bt_smartbridge_att_cache_t** free_cache )
{
    wiced_result_t  result;
    bt_list_node_t* node;

    if ( att_cache_manager == NULL )
    {
        return WICED_BT_ATT_CACHE_UNINITIALISED;
    }

    /* Lock protection */
    result = wiced_rtos_lock_mutex( &att_cache_manager->mutex );
    if ( result != WICED_SUCCESS )
    {
        return result;
    }

    /* Remove from front of free list */
    result = bt_linked_list_remove_from_front( &att_cache_manager->free_list, &node );

    /* Free list is empty. Remove the oldest one from used list */
    if ( result != WICED_BT_SUCCESS )
    {
        result = bt_linked_list_find( &att_cache_manager->used_list, smartbridge_att_cache_get_free_callback, NULL, &node );
        if ( result == WICED_BT_SUCCESS )
        {
            result = bt_linked_list_remove( &att_cache_manager->used_list, node );
            if ( result == WICED_BT_SUCCESS )
            {
                wiced_bt_smart_attribute_list_t* list = (wiced_bt_smart_attribute_list_t*)node->data;

                /* Delete list and set data to NULL */
                wiced_bt_smart_attribute_delete_list( list );
            }
        }
    }

    if ( result == WICED_BT_SUCCESS )
    {
        *free_cache = (bt_smartbridge_att_cache_t*)node->data;
    }

    /* Unlock protection */
    wiced_rtos_unlock_mutex( &att_cache_manager->mutex );

    return result;
}
Exemplo n.º 11
0
wiced_result_t bt_packet_pool_free_packet( bt_packet_t* packet )
{
    if ( packet == NULL )
    {
        return WICED_BT_BADARG;
    }

    if ( packet->pool == DYNAMIC_PACKET_POOL )
    {
        malloc_transfer_to_curr_thread( packet );
        packet->owner     = BT_PACKET_OWNER_POOL;
        packet->node.prev = NULL;
        packet->node.next = NULL;
        packet->node.data = NULL;
        free( packet );
        bt_dynamic_packet_deleted++;
        return WICED_BT_SUCCESS;
    }
    else if ( packet->packet_id == PACKET_ID )
    {
        wiced_result_t result;

        wiced_rtos_lock_mutex( &packet->pool->mutex );

        result = linked_list_insert_node_at_front( &packet->pool->pool_list, &packet->node );

        if ( result == WICED_SUCCESS )
        {
            packet->owner = BT_PACKET_OWNER_POOL;
            packet->pool->packet_deleted++;
        }

        wiced_rtos_unlock_mutex( &packet->pool->mutex );

        return result;
    }

    return WICED_BT_UNKNOWN_PACKET;
}
static wiced_result_t smartbridge_att_cache_return_to_free_list( bt_smartbridge_att_cache_t* cache )
{
    wiced_result_t result;

    if ( att_cache_manager == NULL )
    {
        return WICED_BT_ATT_CACHE_UNINITIALISED;
    }

    /* Lock protection */
    result = wiced_rtos_lock_mutex( &att_cache_manager->mutex );
    if ( result != WICED_BT_SUCCESS )
    {
        return result;
    }

    result = bt_linked_list_insert_at_rear( &att_cache_manager->free_list, &cache->node );

    /* Unlock protection */
    wiced_rtos_unlock_mutex( &att_cache_manager->mutex );

    return result;
}
Exemplo n.º 13
0
/*
 * Updates temperature information in the web page
 */
static int32_t process_temperature_update( const char* url_parameters, wiced_http_response_stream_t* stream, void* arg, wiced_http_message_body_t* http_data )
{
    wiced_iso8601_time_t* curr_time;
    float temp_celcius;
    float temp_fahrenheit;
    float setpoint_celcius = get_setpoint();
    float setpoint_fahrenheit;
    char  temp_char_buffer[6];
    int   temp_char_buffer_length;

    UNUSED_PARAMETER(http_data);

    wiced_rtos_lock_mutex( &xively_data.mutex );

    if ( xively_data.temperature_reading_index == 0 )
    {
        curr_time = &xively_data.temperature_readings[MAX_TEMPERATURE_READINGS - 1].timestamp;
    }
    else
    {
        curr_time = &xively_data.temperature_readings[xively_data.temperature_reading_index - 1].timestamp;
    }

    /* Update temperature report with the most recent temperature reading */
    temp_celcius        = (float) xively_data.last_sample / 10.0f;
    temp_fahrenheit     = temp_celcius / 5.0f * 9.0f + 32.0f;
    setpoint_fahrenheit = setpoint_celcius / 5.0f * 9.0f + 32.0f;

    wiced_rtos_unlock_mutex( &xively_data.mutex );

    /* Write the time */
    wiced_http_response_stream_write_resource( stream, &resources_apps_DIR_temp_control_DIR_data_html_time_start );
    wiced_http_response_stream_write( stream, curr_time->hour, sizeof(curr_time->hour)   +
                                                     sizeof(curr_time->colon1) +
                                                     sizeof(curr_time->minute) +
                                                     sizeof(curr_time->colon2) +
                                                     sizeof(curr_time->second) );
    wiced_http_response_stream_write_resource( stream, &resources_apps_DIR_temp_control_DIR_data_html_time_end );

    /* Write the date */
    wiced_http_response_stream_write_resource( stream, &resources_apps_DIR_temp_control_DIR_data_html_date_start );
    wiced_http_response_stream_write(stream, curr_time->year, sizeof(curr_time->year)  +
                                                    sizeof(curr_time->dash1) +
                                                    sizeof(curr_time->month) +
                                                    sizeof(curr_time->dash2) +
                                                    sizeof(curr_time->day) );

    wiced_http_response_stream_write_resource( stream, &resources_apps_DIR_temp_control_DIR_data_html_date_end );

    wiced_http_response_stream_write_resource( stream, &resources_apps_DIR_temp_control_DIR_data_html_temp_start );
    temp_char_buffer_length = sprintf(temp_char_buffer, "%.1f", temp_celcius);
    wiced_http_response_stream_write(stream, temp_char_buffer, temp_char_buffer_length );
    wiced_http_response_stream_write_resource( stream, &resources_apps_DIR_temp_control_DIR_data_html_temp_mid );
    temp_char_buffer_length = sprintf(temp_char_buffer, "%.1f", temp_fahrenheit);
    wiced_http_response_stream_write(stream, temp_char_buffer, temp_char_buffer_length );
    wiced_http_response_stream_write_resource( stream, &resources_apps_DIR_temp_control_DIR_data_html_temp_end );

    wiced_http_response_stream_write_resource( stream, &resources_apps_DIR_temp_control_DIR_data_html_set_start );
    temp_char_buffer_length = sprintf(temp_char_buffer, "%.1f", setpoint_celcius);
    wiced_http_response_stream_write(stream, temp_char_buffer, temp_char_buffer_length );
    wiced_http_response_stream_write_resource( stream, &resources_apps_DIR_temp_control_DIR_data_html_set_mid );
    temp_char_buffer_length = sprintf(temp_char_buffer, "%.1f", setpoint_fahrenheit);
    wiced_http_response_stream_write(stream, temp_char_buffer, temp_char_buffer_length );
    wiced_http_response_stream_write_resource( stream, &resources_apps_DIR_temp_control_DIR_data_html_set_end );

    wiced_http_response_stream_write_resource( stream, &resources_apps_DIR_temp_control_DIR_data_html_page_end );

    return 0;
}
Exemplo n.º 14
0
/*
 * Sends temperature readings to Xively
 */
static wiced_result_t send_data_to_xively( void* arg )
{
    xively_feed_t       sensor_feed;
    xively_datastream_t stream;
    uint16_t            data_points;
    wiced_result_t      result;

    /* Setup sensor feed info */
    memset( &sensor_feed, 0, sizeof( sensor_feed ) );
    user_dct_data_t* dct;

    wiced_dct_read_lock( (void**) &dct, WICED_FALSE, DCT_APP_SECTION, 0, sizeof(user_dct_data_t) );

    if ( ( dct->xively_feed_id[0]    == '\x00' ) ||
         ( dct->xively_channel_id[0] == '\x00' ) ||
         ( dct->xively_api_key[0]    == '\x00' ) )
    {
        WPRINT_APP_INFO(("Xively feed details not in DCT\n"));
        wiced_dct_read_unlock( dct, WICED_FALSE );
        return WICED_ERROR;
    }

    sensor_feed.id       = dct->xively_feed_id;
    sensor_feed.api_key  = dct->xively_api_key;

    /* Open Xively feed */
    result = xively_open_feed( &sensor_feed );
    if ( result != WICED_SUCCESS )
    {
        WPRINT_APP_INFO(("Failed to open Xively feed\n"));
        wiced_dct_read_unlock( dct, WICED_FALSE );
        return result;
    }

    /* Get the amount of temperature readings to send (required for creating Xively datastream). Mutex protection required. */
    wiced_rtos_lock_mutex( &xively_data.mutex );
    data_points = ( xively_data.temperature_reading_index - xively_data.last_sent_temperature_index ) & ( MAX_TEMPERATURE_READINGS - 1 );
    wiced_rtos_unlock_mutex( &xively_data.mutex );

    /* Create Xively datastream */
    result = xively_create_datastream( &sensor_feed, &stream, dct->xively_channel_id, 4, data_points );
    wiced_dct_read_unlock( dct, WICED_FALSE );
    if ( result != WICED_SUCCESS )
    {
        xively_close_feed( &sensor_feed );
        WPRINT_APP_INFO( ("Failed to init Xively datastream\n") );
        return result;
    }


    /* Write data to TCP stream (compensating for looping around the end of the buffer). Mutex protection required */
    wiced_rtos_lock_mutex( &xively_data.mutex );

    while ( data_points > 0 )
    {
        xively_write_datapoint( &stream, (const uint8_t*) &xively_data.temperature_readings[ xively_data.last_sent_temperature_index ].sample, &xively_data.temperature_readings[ xively_data.last_sent_temperature_index ].timestamp );
        xively_data.last_sent_temperature_index = ( xively_data.last_sent_temperature_index + 1 ) & ( MAX_TEMPERATURE_READINGS - 1 );
        data_points--;
    }

    wiced_rtos_unlock_mutex( &xively_data.mutex );

    /* Flush (send) temperature readings in the TCP stream to Xively */
    if ( xively_flush_datastream( &stream ) == WICED_SUCCESS )
    {
        WPRINT_APP_INFO( ("data sent!\n") );
    }
    else
    {
        WPRINT_APP_INFO( ("Failed to send data!\n") );
    }

    /* Close Xively */
    return xively_close_feed( &sensor_feed );
}
static wiced_result_t smartbridge_att_cache_discover_all( bt_smartbridge_att_cache_t* cache, uint16_t connection_handle )
{
    /* This function performs the following:
     * 1. Primary Services discovery
     * 2. Relationship (Included Services) Discovery for every Primary Service
     * 3. Characteristic Discovery for every Primary Service
     * 4. Characteristic Value Read for every Charactertistic
     * 5. Characteristic Descriptor Discovery for every Characteristic
     */

    wiced_bt_smart_attribute_t**     primary_service_array    = NULL;
    wiced_bt_smart_attribute_t**     characteristic_array     = NULL;
    wiced_bt_smart_attribute_t*      characteristic_value     = NULL;
    wiced_bt_smart_attribute_t*      descriptor_with_no_value = NULL;
    wiced_bt_smart_attribute_t*      descriptor_with_value    = NULL;
    wiced_bt_smart_attribute_t*      iterator                 = NULL;
    wiced_result_t                   result                   = WICED_SUCCESS;
    wiced_result_t                   error_code_var           = WICED_ERROR;
    uint32_t                         i                        = 0;
    uint32_t                         j                        = 0;
    uint32_t                         primary_service_count    = 0;
    uint32_t                         characteristic_count     = 0;
    wiced_bt_smart_attribute_list_t  primary_service_list;
    wiced_bt_smart_attribute_list_t  included_service_list;
    wiced_bt_smart_attribute_list_t  characteristic_list;
    wiced_bt_smart_attribute_list_t  descriptor_list;

    if ( att_cache_manager == NULL )
    {
        return WICED_BT_ATT_CACHE_UNINITIALISED;
    }

    /* Initialise local variables */
    memset( &primary_service_list,  0, sizeof( primary_service_list  ) );
    memset( &included_service_list, 0, sizeof( included_service_list ) );
    memset( &characteristic_list,   0, sizeof( characteristic_list   ) );
    memset( &descriptor_list,       0, sizeof( descriptor_list       ) );

    wiced_rtos_lock_mutex( &cache->mutex );

    result = wiced_bt_smart_attribute_create_list( &primary_service_list );

    wiced_rtos_unlock_mutex( &cache->mutex );

    CHECK_FOR_ERROR( result != WICED_BT_SUCCESS, result );

    /**************************************************************************
     * Primary Services Discovery
     **************************************************************************/
    result = bt_smart_gatt_discover_all_primary_services( connection_handle, &primary_service_list );

    CHECK_FOR_ERROR( result != WICED_BT_SUCCESS, result );

    wiced_bt_smart_attribute_get_list_count( &primary_service_list, &primary_service_count );

    primary_service_array = (wiced_bt_smart_attribute_t**)malloc_named( "svc_array", primary_service_count * sizeof(wiced_bt_smart_attribute_t));

    CHECK_FOR_ERROR( primary_service_array == NULL, WICED_BT_OUT_OF_HEAP_SPACE );

    /* Keep the original pointers to the primary service list before the list gets merged */
    wiced_bt_smart_attribute_get_list_head( &primary_service_list, &iterator );

    for ( i = 0; i < primary_service_count; i++ )
    {
        primary_service_array[i] = iterator;
        iterator                 = iterator->next;
    }

    /* Check if characteristic is readable. If not readable, create a control-point attribute */
    for ( i = 0; i < primary_service_count; i++ )
    {
        /* Initialise variable for this iteration */
        memset( &characteristic_list,   0, sizeof( characteristic_list   ) );
        memset( &included_service_list, 0, sizeof( included_service_list ) );
        characteristic_array = NULL;

        /**********************************************************************
         * Relationship Discovery
         **********************************************************************/
        result = bt_smart_gatt_find_included_services( connection_handle, primary_service_array[i]->value.service.start_handle, primary_service_array[i]->value.service.start_handle, &included_service_list );

        CHECK_FOR_ERROR( result == WICED_BT_GATT_TIMEOUT, result );

        wiced_rtos_lock_mutex( &cache->mutex );

        result = wiced_bt_smart_attribute_merge_lists( &primary_service_list, &included_service_list );

        wiced_rtos_unlock_mutex( &cache->mutex );

        CHECK_FOR_ERROR( result != WICED_BT_SUCCESS, result );

        /**********************************************************************
         * Characteristic Discovery
         **********************************************************************/
        result = bt_smart_gatt_discover_all_characteristics_in_a_service( connection_handle, primary_service_array[i]->value.service.start_handle, primary_service_array[i]->value.service.end_handle, &characteristic_list );

        CHECK_FOR_ERROR( result == WICED_BT_GATT_TIMEOUT, result );

        wiced_bt_smart_attribute_get_list_count( &characteristic_list, &characteristic_count );

        characteristic_array = (wiced_bt_smart_attribute_t**)malloc_named( "char_array", characteristic_count * sizeof(characteristic_list));

        CHECK_FOR_ERROR( characteristic_array == NULL, WICED_BT_OUT_OF_HEAP_SPACE );

        /* Keep the original pointers to the characteristic list before the list gets merged. */
        wiced_bt_smart_attribute_get_list_head( &characteristic_list, &iterator );

        for ( j = 0; j < characteristic_count; j++ )
        {
            characteristic_array[j] = iterator;
            iterator                = iterator->next;
        }

        /* Traverse through all characteristics to perform Characteristic Value Read and Descriptors Discovery */
        for ( j = 0; j < characteristic_count; j++ )
        {
            /* Initialise local variables for this iteration */
            memset( &descriptor_list, 0, sizeof( descriptor_list ) );
            characteristic_value = NULL;

            /******************************************************************
             * Characteristic Value Read
             ******************************************************************/
            if ( ( characteristic_array[j]->value.characteristic.properties & 0x02 ) != 0 )
            {
                /* If characteristic is readable. If not readable, create a control-point attribute */
                result = bt_smart_gatt_read_characteristic_value( connection_handle, characteristic_array[j]->value.characteristic.value_handle, &characteristic_array[j]->value.characteristic.uuid, &characteristic_value );

                CHECK_FOR_ERROR( result == WICED_BT_GATT_TIMEOUT, result );
            }
            else
            {
                /* Failed to read. Let's enter a control-point attribute (dummy) here so when notification come, UUID is known. */
                result = wiced_bt_smart_attribute_create( &characteristic_value, WICED_ATTRIBUTE_TYPE_NO_VALUE, 0 );

                if ( result == WICED_BT_SUCCESS )
                {
                    characteristic_value->handle = characteristic_array[j]->value.characteristic.value_handle;
                    characteristic_value->type   = characteristic_array[j]->value.characteristic.uuid;
                }

                CHECK_FOR_ERROR( result != WICED_BT_SUCCESS, result );
            }

            if ( characteristic_value != NULL )
            {
                /* Add Characteristic Value to main list */
                wiced_rtos_lock_mutex( &cache->mutex );

                result = wiced_bt_smart_attribute_add_to_list( &primary_service_list, characteristic_value );

                wiced_rtos_unlock_mutex( &cache->mutex );

                CHECK_FOR_ERROR( result != WICED_BT_SUCCESS, result );
            }

            /******************************************************************
             * Characteristic Descriptor Discovery
             ******************************************************************/
            if ( characteristic_array[j]->value.characteristic.descriptor_start_handle <= characteristic_array[j]->value.characteristic.descriptor_end_handle )
            {
                result = bt_smart_gatt_discover_all_characteristic_descriptors( connection_handle, characteristic_array[j]->value.characteristic.descriptor_start_handle, characteristic_array[j]->value.characteristic.descriptor_end_handle, &descriptor_list );

                CHECK_FOR_ERROR( result == WICED_BT_GATT_TIMEOUT, result );

                wiced_bt_smart_attribute_get_list_head( &descriptor_list, &descriptor_with_no_value );

                /* Traverse through all descriptors */
                while ( descriptor_with_no_value != NULL )
                {
                    /* Initialise variable for this iteration */
                    descriptor_with_value = NULL;

                    result = bt_smart_gatt_read_characteristic_descriptor( connection_handle, descriptor_with_no_value->handle, &descriptor_with_no_value->type, &descriptor_with_value );

                    CHECK_FOR_ERROR( result == WICED_BT_GATT_TIMEOUT, result );

                    /* Add Descriptor with Value to main list */
                    result = wiced_bt_smart_attribute_add_to_list( &primary_service_list, descriptor_with_value );

                    CHECK_FOR_ERROR( result != WICED_BT_SUCCESS, result );

                    descriptor_with_no_value = descriptor_with_no_value->next;
                }

                /* Delete the empty descriptor list */
                result = wiced_bt_smart_attribute_delete_list( &descriptor_list );

                CHECK_FOR_ERROR( result != WICED_BT_SUCCESS, result );
            }
        }

        /* Merge Characteristics to main list */
        wiced_rtos_lock_mutex( &cache->mutex );

        result = wiced_bt_smart_attribute_merge_lists( &primary_service_list, &characteristic_list );

        wiced_rtos_unlock_mutex( &cache->mutex );

        CHECK_FOR_ERROR( result != WICED_BT_SUCCESS, result );

        /* Free primary service array */
        free( characteristic_array );
    }

    /* Free primary service array */
    free( primary_service_array );

    /* Successful. Now copy the primary service list to the cached attributes list */
    memcpy( &cache->attribute_list, &primary_service_list, sizeof( cache->attribute_list ) );

    return WICED_BT_SUCCESS;

    error:


    /* Delete all local attributes */
    if ( iterator != NULL )
    {
        wiced_bt_smart_attribute_delete( iterator );
    }

    if ( descriptor_with_no_value != NULL )
    {
        wiced_bt_smart_attribute_delete( descriptor_with_no_value );
    }

    if ( descriptor_with_value != NULL )
    {
        wiced_bt_smart_attribute_delete( descriptor_with_value );
    }

    if ( characteristic_value != NULL )
    {
        wiced_bt_smart_attribute_delete( characteristic_value );
    }

    if ( characteristic_array != NULL )
    {
        free( characteristic_array );
    }

    if ( primary_service_array != NULL )
    {
        free( primary_service_array );
    }

    wiced_bt_smart_attribute_delete_list( &descriptor_list );
    wiced_bt_smart_attribute_delete_list( &characteristic_list );
    wiced_bt_smart_attribute_delete_list( &included_service_list );
    wiced_bt_smart_attribute_delete_list( &primary_service_list );

    /* Return the error code to the caller */
    return error_code_var;
}
Exemplo n.º 16
0
static void rfcomm_unlock_shared_data( void )
{
    wiced_result_t result = wiced_rtos_unlock_mutex( &rfcomm_instance.shared.mutex );
    UNUSED_PARAMETER( result );
    wiced_assert( "Error unlocking RFCOMM socket mutex\n", result == WICED_BT_SUCCESS );
}
Exemplo n.º 17
0
static void rfcomm_unlock_socket( wiced_bt_rfcomm_socket_t* socket )
{
    wiced_result_t result = wiced_rtos_unlock_mutex( &socket->shared.mutex );
    UNUSED_PARAMETER( result );
    wiced_assert( "Error unlocking RFCOMM socket mutex\n", result == WICED_BT_SUCCESS );
}
Exemplo n.º 18
0
static wiced_result_t wiced_websocket_common_connect(wiced_websocket_t* websocket, wiced_websocket_handshake_fields_t* websocket_header_fields, wiced_bool_t use_secure_connection)
{
    wiced_result_t      result = WICED_ERROR;
    wiced_ip_address_t  address;


    if( websocket != NULL )
    {

        if ( !mutex_initialised )
        {
            /*create handhsake mutex. There can only be one connection in a connecting state*/
            wiced_rtos_init_mutex( &handshake_lock );
            mutex_initialised = WICED_TRUE;
        }

        /*as per rfc 6455 specification, there can not be more then one socket in a connecting state*/
        wiced_rtos_lock_mutex( &handshake_lock );

        /*perform DNS lookup*/
        result = wiced_hostname_lookup( websocket_header_fields->host, &address, 10000 );
        IF_ERROR_RECORD_AND_EXIT( result, WEBSOCKET_DNS_RESOLVE_ERROR );

        /*register websocket*/
        result = wiced_register_websocket( websocket );
        IF_ERROR_RECORD_AND_EXIT( result, WEBSOCKET_NO_AVAILABLE_SOCKET );

        wiced_update_socket_state( websocket,WEBSOCKET_CONNECTING );

        /*establish secure/non secure tcp connection to server*/
        if (use_secure_connection)
        {
            result = wiced_websocket_tls_connect( websocket, &address );
        }
        else
        {
            result = wiced_websocket_connect( websocket, &address );
        }
        IF_ERROR_RECORD_AND_EXIT( result, WEBSOCKET_CLIENT_CONNECT_ERROR );

        result = wiced_establish_websocket_handshake(websocket, websocket_header_fields);
        IF_ERROR_RECORD_AND_EXIT( result, WEBSOCKET_SERVER_HANDSHAKE_RESPONSE_INVALID );

        /* If a sub protocol was requested, extract it from the handshake */
        if( websocket_header_fields->sec_websocket_protocol != NULL )
        {
            result = wiced_get_websocket_subprotocol( websocket->subprotocol );
            IF_ERROR_RECORD_AND_EXIT( result, WEBSOCKET_SUBPROTOCOL_NOT_SUPPORTED );
        }

        wiced_tcp_register_callbacks( &websocket->socket, NULL, wiced_on_message_callback, wiced_on_close_callback );

        wiced_update_socket_state( websocket,WEBSOCKET_OPEN );
        websocket->on_open( websocket );

        /*release handhsake mutex. There can only be one connection in a connecting state*/
        wiced_rtos_unlock_mutex(&handshake_lock);

    }

    return result;

}