예제 #1
0
void application_start( void )
{
    user_dct_data_t* dct;
    uint32_t sample_interval;
    uint16_t max_sockets = 10;

    wiced_init( );

    /* Configure device */
    wiced_configure_device( app_config );

    /* Initialise Xively data. Xively data is shared among multiple threads; therefore a mutex is required */
    memset( &xively_data, 0, sizeof( xively_data ) );
    wiced_rtos_init_mutex( &xively_data.mutex );

    /* Initialise temperature set point. Set point is shared among multiple threads; therefore a mutex is required */
    wiced_rtos_init_mutex( &setpoint.mutex );
    setpoint.temperature = DEFAULT_SETPOINT;
    adjust_setpoint_led_brightness( );

    /* Initialise Thermistor */
    wiced_adc_init( THERMISTOR_ADC, 5 );

    /* Initialise Set Point Control keypad and assigns it's callback function to run on hardware_io_worker_thread's context */
    gpio_keypad_enable( &setpoint_control_keypad, WICED_HARDWARE_IO_WORKER_THREAD, setpoint_control_keypad_handler, 250, 2, setpoint_control_key_list );

    /* Disable roaming to other access points */
    wiced_wifi_set_roam_trigger( -99 ); /* -99dBm ie. extremely low signal level */

    /* Bringup the network interface */
    wiced_network_up( WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL );

    /* Timestamp is needed for sending data to Xively.
     * Start automatic time synchronisation and synchronise once every day.
     */
    sntp_start_auto_time_sync( 1 * DAYS );

    wiced_dct_read_lock( (void**) &dct, WICED_FALSE, DCT_APP_SECTION, 0, sizeof(user_dct_data_t) );
    sample_interval = dct->sample_interval;
    wiced_dct_read_unlock( dct, WICED_FALSE );

    /* Setup timed events that will take a measurement & activate Xively thread to send measurements to Xively */
    wiced_rtos_register_timed_event( &xively_timed_event,      WICED_NETWORKING_WORKER_THREAD,  send_data_to_xively,     10 * SECONDS, 0 );
    wiced_rtos_register_timed_event( &temperature_timed_event, WICED_HARDWARE_IO_WORKER_THREAD, take_temperature_reading, sample_interval, 0 );

    /* Start web server to display current temperature & setpoint */
    wiced_http_server_start( &http_server, 80, max_sockets, web_pages, WICED_STA_INTERFACE, DEFAULT_URL_PROCESSOR_STACK_SIZE );

    /* Advertise webpage services using Gedday */
    gedday_init( WICED_STA_INTERFACE, "wiced-temp-controller" );
    gedday_add_service( "temp_control web server", "_http._tcp.local", 80, 300, "" );
}
예제 #2
0
wiced_result_t wiced_bt_rfcomm_init( void )
{
    wiced_result_t result;

    if ( rfcomm_instance.initialised == WICED_TRUE )
    {
        return WICED_BT_SUCCESS;
    }

    /* Memset shared data */
    memset( &rfcomm_instance, 0, sizeof( rfcomm_instance ) );

    rfcomm_instance.shared.lock   = rfcomm_lock_shared_data;
    rfcomm_instance.shared.unlock = rfcomm_unlock_shared_data;

    /* Initialise data mutex */
    result = wiced_rtos_init_mutex( &rfcomm_instance.shared.mutex );
    if ( result != WICED_BT_SUCCESS )
    {
        WPRINT_LIB_ERROR( ("Error initialising RFCOMM data mutex\n") );
        return result;
    }

    /* Initialise MPAF client */
    result = bt_mpaf_register_rfcomm_callback( rfcomm_mpaf_event_handler, rfcomm_mpaf_rx_data_handler );
    if ( result != WICED_BT_SUCCESS )
    {
        WPRINT_LIB_ERROR( ("Error registering MPAF RFCOMM callbacks\n") );
        return result;
    }

    rfcomm_instance.initialised      = WICED_TRUE;
    rfcomm_instance.sdp_record_added = WICED_FALSE;
    return WICED_BT_SUCCESS;
}
예제 #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;
}
예제 #4
0
void platform_mutex_init(Mutex* m)
{
    if (!m)
    {
        platform_printf("%s: invalid mutex %p\n", __func__, m);
        return;
    }

    wiced_rtos_init_mutex(&m->mutex);
}
예제 #5
0
wiced_result_t bt_packet_pool_init( bt_packet_pool_t* pool, uint32_t packet_count, uint32_t header_size, uint32_t data_size )
{
    uint32_t       packet_size = header_size + data_size + sizeof(bt_packet_t) - 1;
    uint32_t       buffer_size = packet_count * packet_size;
    uint8_t*       packet_ptr  = NULL;
    void*          buffer      = NULL;
    wiced_result_t result;
    uint32_t       a;

    memset( pool, 0, sizeof( *pool ) );

    buffer = (void*)malloc_named( "bt_packet_pool", buffer_size );
    if ( buffer == NULL )
    {
        return WICED_BT_OUT_OF_HEAP_SPACE;
    }

    result = linked_list_init( &pool->pool_list );
    if ( result != WICED_BT_SUCCESS )
    {
        return result;
    }

    pool->pool_id          = PACKET_POOL_ID;
    pool->max_packet_count = packet_count;
    pool->header_size      = header_size;
    pool->data_size        = data_size;
    pool->pool_buffer      = buffer;
    packet_ptr             = buffer;

    for ( a = 0; a < packet_count; a++ )
    {
        bt_packet_t* packet = (bt_packet_t*)packet_ptr;

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

        if ( result == WICED_BT_SUCCESS )
        {
            packet->node.data = (void*)packet;
            packet->pool      = pool;
            packet->packet_id = PACKET_ID;

        }
        else
        {
            return result;
        }

        packet_ptr += packet_size;
    }

    return wiced_rtos_init_mutex( &pool->mutex );
}
예제 #6
0
wiced_result_t wiced_bt_rfcomm_init_socket( wiced_bt_rfcomm_socket_t* socket, wiced_bt_rfcomm_event_handler_t handler, void* arg )
{
    wiced_result_t result;

    if ( socket->id == RFCOMM_SOCKET_ID )
    {
        return WICED_BT_SOCKET_IN_USE;
    }

    socket->id              = RFCOMM_SOCKET_ID;
    socket->handler         = (void*)handler;
    socket->arg             = arg;
    socket->shared.status   = RFCOMM_SOCKET_INITIALISED;
    socket->shared.channel  = 0;
    socket->shared.endpoint = 0;
    socket->shared.lock     = rfcomm_lock_socket;
    socket->shared.unlock   = rfcomm_unlock_socket;

    result = wiced_rtos_init_semaphore( &socket->semaphore );
    if ( result != WICED_BT_SUCCESS )
    {
        WPRINT_LIB_ERROR( ("Error initting RFCOMM socket semaphore\n") );
        return result;
    }

    result = wiced_rtos_init_mutex( &socket->shared.mutex );
    if ( result != WICED_BT_SUCCESS )
    {
        WPRINT_LIB_ERROR( ("Error initting RFCOMM socket mutex\n") );
        return result;
    }

    result = bt_linked_list_init( &socket->shared.rx_packet_list );
    if ( result != WICED_BT_SUCCESS )
    {
        WPRINT_LIB_ERROR( ("Error initting RX packet list\n") );
        return result;
    }

    return WICED_BT_SUCCESS;
}
wiced_result_t bt_smartbridge_att_cache_enable( uint32_t cache_count )
{
    uint32_t a;
    wiced_result_t result;
    bt_smartbridge_att_cache_manager_t* manager;

    if ( att_cache_manager != NULL )
    {
        return WICED_BT_SUCCESS;
    }

    manager = (bt_smartbridge_att_cache_manager_t*)malloc_named( "att_cache", CALCULATE_ATT_CACHE_MANAGER_SIZE( cache_count ) );
    if ( manager == NULL )
    {
        return WICED_BT_OUT_OF_HEAP_SPACE;
    }

    memset( manager, 0, CALCULATE_ATT_CACHE_MANAGER_SIZE( cache_count ) );

    att_cache_manager = manager;
    manager->count    = cache_count;

    result = bt_linked_list_init( &manager->free_list );
    if ( result != WICED_BT_SUCCESS )
    {
        WPRINT_LIB_INFO( ( "Error creating linked list\n" ) );
        goto error;
    }

    result = bt_linked_list_init( &manager->used_list );
    if ( result != WICED_BT_SUCCESS )
    {
        WPRINT_LIB_INFO( ( "Error creating linked list\n" ) );
        goto error;
    }

    result = wiced_rtos_init_mutex( &manager->mutex );
    if ( result != WICED_SUCCESS )
    {
        WPRINT_LIB_INFO( ( "Error creating mutex\n" ) );
        goto error;
    }

    /* Initialise mutexes for protecting access to cached attributes */
    for ( a = 0; a < manager->count; a++ )
    {
        result = wiced_rtos_init_mutex( &manager->pool[a].mutex );
        if ( result != WICED_BT_SUCCESS )
        {
            goto error;
        }

        /* Point node data to cached attribute instance */
        manager->pool[a].node.data = (void*)&manager->pool[a];

        /* Insert cached attribute instance into free list */
        result = bt_linked_list_insert_at_rear( &manager->free_list, &manager->pool[a].node );
        if ( result != WICED_BT_SUCCESS )
        {
            goto error;
        }
    }

    return WICED_BT_SUCCESS;

    error:
    bt_smartbridge_att_cache_disable();
    return result;
}
예제 #8
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;

}