Beispiel #1
0
wiced_result_t wiced_bt_rfcomm_receive_packet( wiced_bt_rfcomm_socket_t* socket, wiced_bt_packet_t** packet, uint8_t** data, uint32_t* data_size, uint32_t timeout_ms )
{
    wiced_result_t result;
    uint8_t        status;

    if ( packet == NULL || data == NULL || data_size == NULL )
    {
        return WICED_BT_BADARG;
    }

    socket->shared.lock( socket );
    status = socket->shared.status;
    socket->shared.unlock( socket );

    if ( ( status & RFCOMM_SOCKET_CONNECTED ) == 0 )
    {
        return WICED_BT_SOCKET_NOT_CONNECTED;
    }

    socket->shared.lock( socket );
    socket->shared.status |= RFCOMM_SOCKET_WAITING_FOR_PACKET;
    socket->shared.unlock( socket );

    /* Wait in a semaphore. If successful, data is available */
    result = wiced_rtos_get_semaphore( &socket->semaphore, timeout_ms );

    socket->shared.lock( socket );
    status = socket->shared.status;
    socket->shared.status &= ~(uint8_t)RFCOMM_SOCKET_WAITING_FOR_PACKET;

    if ( ( status & RFCOMM_SOCKET_CONNECTED ) == 0 )
    {
        result = WICED_BT_SOCKET_NOT_CONNECTED;
    }
    else if ( result == WICED_BT_SUCCESS )
    {
        /* Data is available. Retreive data */
        bt_list_node_t* removed_node;

        result = bt_linked_list_remove_from_front( &socket->shared.rx_packet_list, &removed_node );

        if ( result == WICED_BT_SUCCESS )
        {
            *packet          = (bt_packet_t*)removed_node->data;
            (*packet)->owner = BT_PACKET_OWNER_APP;
            *data            = (*packet)->data_start;
            *data_size       = (uint32_t)( (*packet)->data_end - (*packet)->data_start );
        }
    }

    socket->shared.unlock( socket );

    return result;
}
wiced_result_t sniff_security(SnifferInfo* info) {

    wiced_result_t result = wiced_rtos_init_semaphore(&info->complete);
    if (result!=WICED_SUCCESS) return result;
    result = wiced_wifi_scan_networks(sniffer, info);
    if (result==WICED_SUCCESS) {
        wiced_rtos_get_semaphore(&info->complete, 30000);
    }
    wiced_rtos_deinit_semaphore(&info->complete);
    if (!info->rssi)
        result = WICED_NOT_FOUND;
    return result;
}
static wiced_result_t mqtt_wait_for( wiced_mqtt_event_type_t event, uint32_t timeout )
{
    if ( wiced_rtos_get_semaphore( &aws_app_info->msg_semaphore, timeout ) != WICED_SUCCESS )
    {
        return WICED_ERROR;
    }
    else
    {
        if ( event != aws_app_info->expected_event )
        {
            return WICED_ERROR;
        }
    }
    return WICED_SUCCESS;
}
Beispiel #4
0
/*
 * A blocking call to an expected event.
 */
static wiced_result_t amqp_wait_for( wiced_amqp_event_t event, uint32_t timeout )
{
        if ( wiced_rtos_get_semaphore( &semaphore, timeout ) != WICED_SUCCESS )
    {
        return WICED_ERROR;
    }
    else
    {
        if ( event != expected_event )
        {
            return WICED_ERROR;
        }
    }
    return WICED_SUCCESS;
}
void system_monitor_thread_main( wiced_thread_arg_t arg )
{
    UNUSED_PARAMETER(arg);

    memset(system_monitors, 0, sizeof(system_monitors));

    if (wiced_rtos_init_semaphore(&system_monitor_thread_semaphore) != WICED_SUCCESS)
    {
        wiced_assert("semaphore init failed", 0);
    }

    /* - Can watch threads
     * Each thread can set a counter that is decremented every event.
     * Once any timer == 0, the watchdog is no longer kicked
     *
     * - Can watch packet buffer status
     * If no RX packets are available, take timestamp (T). If (current time - T) > X seconds, stop kicking watchdog.
     * X can be 10 second default. Time will be set to zero once RX buffer is freed
     *
     * - Can watch bus data credits
     * If no credits are available, take timestamp (B). If (current time - B) > X seconds, stop kicking watchdog.
     * This will be dependent on if WLAN is up. Timer will be set to 0 if credits become available.
     */
    while (1)
    {
        int a;
        uint32_t current_time = host_rtos_get_time();

        for (a = 0; a < MAXIMUM_NUMBER_OF_SYSTEM_MONITORS; ++a)
        {
            if (system_monitors[a] != NULL)
            {
                if ((current_time - system_monitors[a]->last_update) > system_monitors[a]->longest_permitted_delay)
                {
                    /* A system monitor update period has been missed and hence explicitly resetting the MCU rather than waiting for the watchdog to bite */
                    wiced_framework_reboot();
                }
            }
        }
        wiced_watchdog_kick();
        wiced_rtos_get_semaphore(&system_monitor_thread_semaphore, DEFAULT_SYSTEM_MONITOR_PERIOD);
    }
    /* Should never get here */
    wiced_rtos_deinit_semaphore(&system_monitor_thread_semaphore);

    WICED_END_OF_CURRENT_THREAD( );
}
Beispiel #6
0
int platform_semaphore_wait(Semaphore* s, int timeout_ms)
{
    if (!s)
    {
        platform_printf("%s: invalid semaphore %p\n", __func__, s);
        return -1;
    }

    wiced_result_t rc = wiced_rtos_get_semaphore(&s->sem, timeout_ms);
    if (rc != WICED_SUCCESS)
    {
        //platform_printf("%s: FAILED to wait semaphore %p: %d\n", __func__, &s->sem, rc);
        return -1;
    }

    return 0;
}
wiced_result_t command_console_deinit(void)
{
    wiced_result_t result;
    cons.quit = WICED_TRUE;

    /* Wait on a semaphore till the console thread is ready to quit */
    result = wiced_rtos_get_semaphore(&cons.console_quit_semaphore, 1000);
    if( result != WICED_SUCCESS )
    {
        return result;
    }
    wiced_rtos_thread_join(&cons.console_thread);
    result = wiced_rtos_delete_thread( &cons.console_thread);
    wiced_rtos_deinit_semaphore(&cons.console_quit_semaphore);
    if( cons.uart != STDIO_UART )
    {
        wiced_uart_deinit(cons.uart);
    }
    return WICED_SUCCESS;
}
void application_start( void )
{
    wiced_ip_address_t  ip_address;
    wiced_result_t      result;

    /* We need three headers - host, content type, and content length */
    http_header_field_t header[3];
    /* Header 0 is the Host header */
    header[0].field        = HTTP_HEADER_HOST;
    header[0].field_length = strlen( HTTP_HEADER_HOST );
    header[0].value        = SERVER_HOST;
    header[0].value_length = strlen( SERVER_HOST );
    /* Header 1 is the content type (JSON) */
    header[1].field        =  HTTP_HEADER_CONTENT_TYPE;
    header[1].field_length = strlen( HTTP_HEADER_CONTENT_TYPE );
    header[1].value        = "application/json";
    header[1].value_length = strlen( "application/json" );
    /* Header 2 is the content length. In this case, it is the length of the JSON message */
    sprintf(json_len,"%d", strlen(JSON_MSG)); /* Calculate the length of the JSON message and store the value as a string */
    header[2].field        = HTTP_HEADER_CONTENT_LENGTH;
    header[2].field_length = strlen( HTTP_HEADER_CONTENT_LENGTH );
    header[2].value        = json_len; // This holds the length of the JSON message as a sting containing the decimal value
    header[2].value_length = strlen( json_len ); // This is the length of the string that holds the JSON message size. For example, if the JSON is 12 characters, this would be "2" because the string "12" is 2 characters long.

    wiced_init( );

    /* This semaphore will be used to wait for one request to finish before re-initializing and starting the next one */
    wiced_rtos_init_semaphore(&httpWait);

    wiced_network_up(WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL);

    WPRINT_APP_INFO( ( "Resolving IP address of %s\n", SERVER_HOST ) );
    wiced_hostname_lookup( SERVER_HOST, &ip_address, DNS_TIMEOUT_MS, WICED_STA_INTERFACE );
    WPRINT_APP_INFO( ( "%s is at %u.%u.%u.%u\n", SERVER_HOST,
                                                 (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 24),
                                                 (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 16),
                                                 (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 8),
                                                 (uint8_t)(GET_IPV4_ADDRESS(ip_address) >> 0) ) );

    /* Initialize client */
    http_client_init( &client, WICED_STA_INTERFACE, event_handler, NULL );
    client.peer_cn = NULL; /* If you set hostname, library will make sure subject name in the server certificate is matching with host name you are trying to connect. Pass NULL if you don't want to enable this check */

    /* Connect to the server */
    if ( ( result = http_client_connect( &client, (const wiced_ip_address_t*)&ip_address, SERVER_PORT, HTTP_NO_SECURITY, CONNECT_TIMEOUT_MS ) ) == WICED_SUCCESS )
    {
        connected = WICED_TRUE;
        WPRINT_APP_INFO( ( "Connected to %s\n", SERVER_HOST ) );
    }
    else
    {
        WPRINT_APP_INFO( ( "Error: failed to connect to server: %u\n", result) );
        return; /* Connection failed - exit program */
    }

    /* Send a POST to resource /anything */
    http_request_init( &request, &client, HTTP_POST, "/post", HTTP_1_1 );
    http_request_write_header( &request, &header[0], 3 ); // We need 3 headers
    http_request_write_end_header( &request );
    http_request_write( &request, (uint8_t* ) JSON_MSG, strlen(JSON_MSG)); /* Write the content */
    http_request_flush( &request );

    wiced_rtos_get_semaphore(&httpWait, WICED_WAIT_FOREVER); /* Wait for this request to complete before going on */
    /* Disconnect from the server and deinit since we are done now */
    http_client_disconnect( &client );
    http_client_deinit( &client );
}
Beispiel #9
0
wiced_result_t spi_master_write_register( spi_master_t* master, uint16_t address, uint16_t size, uint8_t* data_out )
{
    wiced_result_t                    result;
    wiced_spi_slave_command_t         command;
    wiced_spi_message_segment_t       message;
    wiced_spi_slave_transfer_status_t status;

    memset( &command, 0x00, sizeof( command ) );

    /* Indicate that master is currently in transaction */
    master->in_transaction = WICED_TRUE;

    /* Send command */
    command.direction   = SPI_SLAVE_TRANSFER_WRITE;
    command.address     = address;
    command.data_length = size;
    message.length      = sizeof(command);
    message.tx_buffer   = &command;
    message.rx_buffer   = NULL;

    /* Wait until data ready pin is set to low logic level( falling edge is detected ) */
    /* Slave is ready to receive a command only when it sets its ready pin to low logic level */
    if( wiced_gpio_input_get( master->data_ready_pin ) == WICED_TRUE )
    {
        while(1)
        {
            result = wiced_rtos_get_semaphore( &master->data_ready_semaphore, DATA_READY_TIMEOUT_MS );
            if( result != WICED_SUCCESS )
            {
                master->in_transaction = WICED_FALSE;
                return result;
            }

            /* Check whether the ready ping is set to high logic level, if not wait on a semaphore again */
            if( wiced_gpio_input_get( master->data_ready_pin ) == WICED_FALSE )
            {
                break;
            }
            else
            {
                //wiced_assert("Unexpected condition", 0!=0);
            }
        }
    }

    /* Send the command */
    result = wiced_spi_transfer( master->device, &message, 1 );
    if( result != WICED_SUCCESS )
    {
        master->in_transaction = WICED_FALSE;
        return result;
    }

    /* Wait until data ready pin is set to high logic level( rising edge is detected ) */
    if( wiced_gpio_input_get( master->data_ready_pin ) == WICED_FALSE )
    {
        while(1)
        {
            result = wiced_rtos_get_semaphore( &master->data_ready_semaphore, DATA_READY_TIMEOUT_MS );
            if( result != WICED_SUCCESS )
            {
                master->in_transaction = WICED_FALSE;
                return result;
            }
            /* Check whether the ready ping is set to high logic level, if not wait on a semaphore again */
            if( wiced_gpio_input_get( master->data_ready_pin ) == WICED_TRUE )
            {
                break;
            }
            else
            {
                //wiced_assert("Unexpected condition", 0!=0);
            }
        }
    }

    /* Read status byte */
    message.length    = 1;
    message.tx_buffer = NULL;
    message.rx_buffer = &status;
    result = wiced_spi_transfer( master->device, &message, 1 );
    if ( result != WICED_SUCCESS )
    {
        master->in_transaction = WICED_FALSE;
        return result;
    }
    if ( status != SPI_SLAVE_TRANSFER_SUCCESS )
    {
        master->in_transaction = WICED_FALSE;
        return spi_slave_convert_status_to_result( status );
    }

    /* Write data value */
    message.length      = size;
    message.tx_buffer   = data_out;
    message.rx_buffer   = NULL;
    result = wiced_spi_transfer( master->device, &message, 1 );
    if ( result != WICED_SUCCESS )
    {
        master->in_transaction = WICED_FALSE;
        return result;
    }

    return WICED_SUCCESS;
}
/******************************************************
 *               Function Definitions
 ******************************************************/
void application_start( void )
{
    wiced_result_t ret = WICED_SUCCESS;
    uint32_t       connection_retries = 0;
    uint32_t       retries = 0;

    ret = aws_app_init( &app_info );

    wiced_gpio_input_irq_enable( WICED_BUTTON1, IRQ_TRIGGER_RISING_EDGE, publish_callback, NULL );
    do
    {
        ret = aws_mqtt_conn_open( app_info.mqtt_object, mqtt_connection_event_cb );
        connection_retries++ ;
    } while ( ( ret != WICED_SUCCESS ) && ( connection_retries < WICED_MQTT_CONNECTION_NUMBER_OF_RETRIES ) );

    aws_mqtt_app_publish( app_info.mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, (uint8_t*) app_info.shadow_state_topic, (uint8_t*) SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED, sizeof( SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED ) );

    wiced_rtos_delay_milliseconds( MQTT_DELAY_IN_MILLISECONDS * 2 );

    aws_mqtt_app_subscribe( app_info.mqtt_object, app_info.shadow_delta_topic, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE );

    do
    {
        ret = aws_mqtt_app_subscribe( app_info.mqtt_object, app_info.thing_name, WICED_MQTT_QOS_DELIVER_AT_MOST_ONCE );
        retries++ ;
    } while ( ( ret != WICED_SUCCESS ) && ( retries < WICED_MQTT_SUBSCRIBE_RETRY_COUNT ) );
    if ( ret != WICED_SUCCESS )
    {
        return;
    }

    while ( 1 )
    {
        /* Wait forever on wake semaphore until the LED status is changed */
        wiced_rtos_get_semaphore( &app_info.wake_semaphore, WICED_NEVER_TIMEOUT );

        /* Toggle the LED */
        if ( ( strncasecmp( led_status, "OFF", 3 ) == 0 ) && smart_control == 1 )
        {
            wiced_gpio_output_low( WICED_LED1 );
            led_status = "OFF";
            WPRINT_APP_INFO(("[MQTT] Publishing to Thing state topic\n"));
            aws_mqtt_app_publish( app_info.mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, (uint8_t*) app_info.shadow_state_topic, (uint8_t*) SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED_STATUS_ON, sizeof( SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED_STATUS_ON ) );
        }
        else if ( ( strncasecmp( led_status, "ON", 2 ) == 0 ) && smart_control == 0 )
        {
            wiced_gpio_output_high( WICED_LED1 );
            led_status = "ON";
            WPRINT_APP_INFO(("[MQTT] Publishing to Thing state topic\n"));
            aws_mqtt_app_publish( app_info.mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, (uint8_t*) app_info.shadow_state_topic, (uint8_t*) SHADOW_PUBLISH_MESSAGE_STR_ON_DESIRED_AND_REPORTED_STATUS_OFF, sizeof( SHADOW_PUBLISH_MESSAGE_STR_ON_DESIRED_AND_REPORTED_STATUS_OFF ) );
        }
        else if ( ( strncasecmp( led_status, "ON", 2 ) == 0 ) && smart_control == 1 )
        {
            wiced_gpio_output_high( WICED_LED1 );
            led_status = "ON";
            WPRINT_APP_INFO(("[MQTT] Publishing to Thing state topic\n"));
            aws_mqtt_app_publish( app_info.mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, (uint8_t*) app_info.shadow_state_topic, (uint8_t*) SHADOW_PUBLISH_MESSAGE_STR_ON_DESIRED_AND_REPORTED, sizeof( SHADOW_PUBLISH_MESSAGE_STR_ON_DESIRED_AND_REPORTED ) );
        }
        else
        {
            wiced_gpio_output_low( WICED_LED1 );
            led_status = "OFF";
            WPRINT_APP_INFO(("[MQTT] Publishing to Thing state topic\n"));
            aws_mqtt_app_publish( app_info.mqtt_object, WICED_MQTT_QOS_DELIVER_AT_LEAST_ONCE, (uint8_t*) app_info.shadow_state_topic, (uint8_t*) SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED, sizeof( SHADOW_PUBLISH_MESSAGE_STR_OFF_DESIRED_AND_REPORTED ) );
        }
    }

    aws_mqtt_conn_close( app_info.mqtt_object );

    wiced_rtos_deinit_semaphore( &app_info.msg_semaphore );
    ret = wiced_mqtt_deinit( app_info.mqtt_object );
    wiced_rtos_deinit_semaphore( &app_info.wake_semaphore );
    free( app_info.mqtt_object );
    app_info.mqtt_object = NULL;

    return;
}