static void rfcomm_mpaf_rx_data_handler( uint8_t endpoint, bt_packet_t* packet, uint8_t* data_start, uint32_t data_size ) { UNUSED_PARAMETER( data_start ); UNUSED_PARAMETER( data_size ); wiced_bt_rfcomm_socket_t* socket; rfcomm_instance.shared.lock(); socket = rfcomm_instance.shared.connected_socket_list[endpoint - 1]; rfcomm_instance.shared.unlock(); socket->shared.lock( socket ); bt_linked_list_insert_at_rear( &socket->shared.rx_packet_list, &packet->node ); socket->shared.unlock( socket ); if ( wiced_rtos_set_semaphore( &socket->semaphore ) != WICED_BT_SUCCESS ) { WPRINT_LIB_ERROR( ("Error setting RFCOMM socket semaphore\n") ); } ((wiced_bt_rfcomm_event_handler_t)socket->handler)( socket, RFCOMM_EVENT_INCOMING_PACKET_READY, socket->arg ); }
void console_thread_func( uint32_t arg ) { wiced_result_t result; uint8_t received_character; UNUSED_PARAMETER(result); /* turn off buffers, so IO occurs immediately */ setvbuf( stdin, NULL, _IONBF, 0 ); setvbuf( stdout, NULL, _IONBF, 0 ); setvbuf( stderr, NULL, _IONBF, 0 ); for(;;) { result = wiced_uart_receive_bytes( cons.uart, &received_character, 1, 1000 ); if( result == WICED_SUCCESS ) { console_process_char( received_character ); } else { if( cons.quit == WICED_TRUE ) { wiced_rtos_set_semaphore(&cons.console_quit_semaphore); break; } } } }
/* * Callback function to handle scan results */ wiced_result_t sniffer( wiced_scan_handler_result_t* malloced_scan_result ) { malloc_transfer_to_curr_thread( malloced_scan_result ); SnifferInfo* info = (SnifferInfo*)malloced_scan_result->user_data; if ( malloced_scan_result->status == WICED_SCAN_INCOMPLETE ) { wiced_scan_result_t* record = &malloced_scan_result->ap_details; info->count++; if (!info->callback) { if (record->SSID.length==info->ssid_len && !memcmp(record->SSID.value, info->ssid, info->ssid_len)) { info->security = record->security; info->rssi = record->signal_strength; } } else { WiFiAccessPoint data; memcpy(data.ssid, record->SSID.value, record->SSID.length); memcpy(data.bssid, (uint8_t*)&record->BSSID, 6); data.ssidLength = record->SSID.length; data.ssid[data.ssidLength] = 0; data.security = toSecurityType(record->security); data.rssi = record->signal_strength; data.channel = record->channel; data.maxDataRate = record->max_data_rate; info->callback(&data, info->callback_data); } } else { wiced_rtos_set_semaphore(&info->complete); } free( malloced_scan_result ); return WICED_SUCCESS; }
int process() { int result = 1; if (signal_complete_!=NULL) { wiced_rtos_set_semaphore(signal_complete_); result = 0; } return result; }
static void spi_slave_ready_pin_irq_handler( void* arg ) { spi_master_t* master = (spi_master_t*)arg; if ( master->in_transaction == WICED_TRUE ) { wiced_rtos_set_semaphore( &master->data_ready_semaphore ); } else { wiced_rtos_send_asynchronous_event( WICED_NETWORKING_WORKER_THREAD, spi_slave_data_ready_worker_callback, arg ); } }
/* * Call back function to handle channel events. * * For each event: * - The call back will set the expected_event global to the received event. * - The call back will set the event semaphore to run any blocking thread functions waiting on this event * - Some events will also log other global variables required for extra processing. * * A thread function will probably be waiting for the received event. Once the event is received and the * semaphore is set, the thread function will check for the received event and make sure it matches what * it is expecting. * * Note: This mechanism is not thread safe as we are using a non protected global variable for read/write. * However as this snip is a single controlled thread, there is no risc of racing conditions. It is * however not recommended for multi-threaded applications. */ static wiced_result_t wiced_amqp_channel_event_cb( wiced_amqp_event_t event, void *args, uint16_t channel, wiced_amqp_connection_t *conn ) { switch( event ) { case AMQP_EVENT_CHANNEL_RECV_OPEN_OK: case AMQP_EVENT_CHANNEL_RECV_CLOSE_OK: case AMQP_EVENT_QUEUE_RECV_DECLARE_OK: case AMQP_EVENT_QUEUE_RECV_BIND_OK: case AMQP_EVENT_BASIC_RECV_CONSUME_OK: { expected_event = event; wiced_rtos_set_semaphore( &semaphore ); } break; case AMQP_EVENT_BASIC_RECV_DELIVER: { wiced_amqp_basic_deliver_arg_t *deliver_args = (wiced_amqp_basic_deliver_arg_t *)args; delivery_tag = deliver_args->delivery_tag; } break; case AMQP_EVENT_CONTENT_RECV_CONTENT: { wiced_amqp_frame_t *frame = ( wiced_amqp_frame_t *) args; /* Data is sent as short string */ memcpy( str, frame->buffer.data, frame->size ); str[ frame->size ] = '\0'; expected_event = event; wiced_rtos_set_semaphore( &semaphore ); } break; default: break; } return WICED_SUCCESS; }
/* This is the callback event for HTTP events */ static void event_handler( http_client_t* client, http_event_t event, http_response_t* response ) { static uint8_t count = 1; // Keep track of how many responses we have received switch( event ) { case HTTP_CONNECTED: /* This state is never called */ break; /* This is called when we are disconnected by the server */ case HTTP_DISCONNECTED: { connected = WICED_FALSE; http_client_disconnect( client ); /* Need to keep client connection state synchronized with the server */ WPRINT_APP_INFO(( "Disconnected from %s\n", SERVER_HOST )); break; } /* This is called when new data is received (header, or payload) */ case HTTP_DATA_RECEIVED: { WPRINT_APP_INFO( ( "------------------ Received response: %d ------------------\n", count ) ); /* Print Response Header */ if(response->response_hdr != NULL) { WPRINT_APP_INFO( ( "----- Response Header: -----\n " ) ); print_data( (char*) response->response_hdr, response->response_hdr_length ); } /* Print Response Payload */ WPRINT_APP_INFO( ("\n----- Response Payload: -----\n" ) ); print_data( (char*) response->payload, response->payload_data_length ); if(response->remaining_length == 0) { WPRINT_APP_INFO( ("\n------------------ End Response %d ------------------\n", count ) ); http_request_deinit( (http_request_t*) &(response->request) ); wiced_rtos_set_semaphore(&httpWait); // Set semaphore that allows the next request to start count++; } break; } default: break; } }
int platform_semaphore_post(Semaphore* s) { if (!s) { platform_printf("%s: invalid semaphore %p\n", __func__, s); return -1; } wiced_result_t rc = wiced_rtos_set_semaphore(&s->sem); if (rc != WICED_SUCCESS) { platform_printf("%s: FAILED to post semaphore %p: %d\n", __func__, &s->sem, rc); return -1; } return 0; }
/* * Call back function to handle MQTT events */ wiced_result_t mqtt_connection_event_cb( wiced_mqtt_object_t mqtt_object, wiced_mqtt_event_info_t *event ) { switch ( event->type ) { case WICED_MQTT_EVENT_TYPE_CONNECT_REQ_STATUS: case WICED_MQTT_EVENT_TYPE_DISCONNECTED: case WICED_MQTT_EVENT_TYPE_PUBLISHED: case WICED_MQTT_EVENT_TYPE_SUBCRIBED: case WICED_MQTT_EVENT_TYPE_UNSUBSCRIBED: { app_info.expected_event = event->type; wiced_rtos_set_semaphore( &app_info.msg_semaphore ); } break; case WICED_MQTT_EVENT_TYPE_PUBLISH_MSG_RECEIVED: default: break; } return WICED_SUCCESS; }
/* * Call back function to handle connection events. */ static wiced_result_t wiced_amqp_connection_event_cb( wiced_amqp_event_t event, void *args, wiced_amqp_connection_t *conn ) { switch( event ) { case AMQP_EVENT_CONNECTION_RECV_OPEN_OK: case AMQP_EVENT_CONNECTION_RECV_CLOSE_OK: { expected_event = event; wiced_rtos_set_semaphore( &semaphore ); } break; case AMQP_EVENT_CONNECTION_ERROR: { WPRINT_APP_INFO(("[AMQP] ERROR LOST CONNECTION\n\n")); } break; case AMQP_EVENT_PROTOCOL_RECV_HEADER: { WPRINT_APP_INFO(("[AMQP] ERROR RECEIVED PROTOCOL HEADER.\n")); WPRINT_APP_INFO(("[AMQP] Broker should support AMQP0.9-1.\n\n")); } break; case AMQP_EVENT_CONNECTION_RECV_CLOSE: { wiced_amqp_connection_close_arg_t *close_args = ( wiced_amqp_connection_close_arg_t* )args; /* Data is sent as short string */ memcpy( str, close_args->reply_text.str, close_args->reply_text.len ); str[ close_args->reply_text.len ] = '\0'; WPRINT_APP_INFO(("[AMQP] ERROR RECEIVED BROKER CLOSE REQUEST.\n")); WPRINT_APP_INFO(("[AMQP] REPLY TEXT: %s.\n\n",str)); } break; default: break; } return WICED_SUCCESS; }
static void rfcomm_mpaf_event_handler( mpaf_event_opcode_t event, bt_packet_t* packet, mpaf_event_params_t* params ) { switch ( event ) { case MPAF_RFCOMM_SERVICE_CHANNEL_NUMBER: { wiced_bt_rfcomm_socket_t* socket; rfcomm_instance.shared.lock(); socket = rfcomm_instance.shared.server_connecting_socket; rfcomm_instance.shared.unlock(); socket->shared.lock( socket ); socket->shared.channel = params->rfcomm_service_channel_number.channel; socket->shared.unlock( socket ); break; } case MPAF_COMMAND_COMPLETE: { switch ( params->command_complete.command_opcode ) { case MPAF_RFCOMM_CREATE_CONNECTION_CANCEL: /* Notify app thread that waits for the MPAF_COMMAND_COMPLETE event */ if ( bt_management_mpaf_notify_event_received( packet ) != WICED_BT_SUCCESS ) { WPRINT_LIB_ERROR( ("Error pushing event packet to queue\n") ); } return; default: break; } break; } case MPAF_COMMAND_STATUS: { switch ( params->command_status.command_opcode ) { case MPAF_RFCOMM_CREATE_CONNECTION: case MPAF_RFCOMM_REMOVE_CONNECTION: /* Notify app thread that waits for the MPAF_COMMAND_STATUS event */ if ( bt_management_mpaf_notify_event_received( packet ) != WICED_BT_SUCCESS ) { WPRINT_LIB_ERROR( ("Error pushing event packet to queue\n") ); } return; default: break; } break; } case MPAF_RFCOMM_CONNECTION_COMPLETE: if ( params->rfcomm_connection_complete.status == MPAF_SUCCESS ) { wiced_bt_rfcomm_socket_t* socket; wiced_bool_t is_connecting; rfcomm_instance.shared.lock(); socket = rfcomm_instance.shared.server_connecting_socket; rfcomm_instance.shared.connected_socket_list[params->rfcomm_connection_complete.endpoint - 1] = socket; rfcomm_instance.shared.server_connecting_socket = NULL; rfcomm_instance.shared.unlock(); /* Update socket */ socket->shared.lock( socket ); is_connecting = ( ( socket->shared.status & RFCOMM_SOCKET_CONNECTING ) != 0 ) ? WICED_TRUE : WICED_FALSE; if ( is_connecting == WICED_TRUE ) { socket->shared.status &= ~(uint8_t)RFCOMM_SOCKET_CONNECTING; } else { socket->shared.status &= ~(uint8_t)RFCOMM_SOCKET_LISTENING; } socket->shared.status |= RFCOMM_SOCKET_CONNECTED; socket->shared.endpoint = params->rfcomm_connection_complete.endpoint; socket->shared.unlock( socket ); /* Notify app via callback */ if ( is_connecting == WICED_TRUE ) { ((wiced_bt_rfcomm_event_handler_t)socket->handler)( socket, RFCOMM_EVENT_CLIENT_CONNECTED, socket->arg ); } else { ((wiced_bt_rfcomm_event_handler_t)socket->handler)( socket, RFCOMM_EVENT_CONNECTION_ACCEPTED, socket->arg ); } } else { wiced_bt_rfcomm_socket_t* socket; /* Update shared data */ rfcomm_instance.shared.lock(); socket = rfcomm_instance.shared.server_connecting_socket; /* Save socket pointer */ rfcomm_instance.shared.server_connecting_socket = NULL; /* Already connected. Reset server_connecting_socket */ rfcomm_instance.shared.unlock(); /* Update socket */ socket->shared.lock( socket ); socket->shared.status &= ~(uint8_t)( RFCOMM_SOCKET_LISTENING | RFCOMM_SOCKET_CONNECTING ); socket->shared.unlock( socket ); ((wiced_bt_rfcomm_event_handler_t)socket->handler)( socket, RFCOMM_EVENT_CONNECTION_ERROR, socket->arg ); } break; case MPAF_RFCOMM_DISCONNECTION_COMPLETE: if ( params->rfcomm_connection_complete.status == MPAF_SUCCESS ) { wiced_bt_rfcomm_socket_t* socket; wiced_bool_t disconnect_by_host; /* Update socket list */ rfcomm_instance.shared.lock(); socket = rfcomm_instance.shared.connected_socket_list[params->rfcomm_connection_complete.endpoint - 1]; rfcomm_instance.shared.connected_socket_list[params->rfcomm_connection_complete.endpoint - 1] = NULL; rfcomm_instance.shared.unlock(); /* Update socket */ socket->shared.lock( socket ); disconnect_by_host = ( socket->shared.status & RFCOMM_SOCKET_DISCONNECTING ) ? WICED_TRUE : WICED_FALSE; socket->shared.status &= ~(uint8_t)( RFCOMM_SOCKET_CONNECTED | RFCOMM_SOCKET_DISCONNECTING ); socket->shared.status |= RFCOMM_SOCKET_TERMINATED; socket->shared.endpoint = 0; socket->shared.unlock( socket ); /* Notify waiting thread that connection is down */ if ( wiced_rtos_set_semaphore( &socket->semaphore ) != WICED_BT_SUCCESS ) { WPRINT_LIB_ERROR( ("Error setting RFCOMM socket semaphore\n") ); } if ( disconnect_by_host == WICED_TRUE ) { /* Notify app thread that waits for the MPAF_RFCOMM_DISCONNECTION_COMPLETE event */ if ( bt_management_mpaf_notify_event_received( packet ) != WICED_BT_SUCCESS ) { WPRINT_LIB_ERROR( ("Error pushing event packet to queue\n") ); } return; } else { ((wiced_bt_rfcomm_event_handler_t)socket->handler)( socket, RFCOMM_EVENT_CONNECTION_DISCONNECTED, socket->arg ); } } else { WPRINT_LIB_ERROR( ("Error pushing event packet to queue\n") ); } break; default: break; } /* Packet is not needed anymore. Delete! */ bt_packet_pool_free_packet( packet ); }
wiced_result_t wiced_wakeup_system_monitor_thread(void) { return wiced_rtos_set_semaphore(&system_monitor_thread_semaphore); }
/* * Call back function to handle connection events. */ wiced_result_t mqtt_connection_event_cb( wiced_mqtt_object_t mqtt_object, wiced_mqtt_event_info_t *event ) { cJSON *json = NULL; cJSON *original = NULL; switch ( event->type ) { case WICED_MQTT_EVENT_TYPE_CONNECT_REQ_STATUS: case WICED_MQTT_EVENT_TYPE_DISCONNECTED: case WICED_MQTT_EVENT_TYPE_PUBLISHED: case WICED_MQTT_EVENT_TYPE_SUBCRIBED: case WICED_MQTT_EVENT_TYPE_UNSUBSCRIBED: { app_info.expected_event = event->type; wiced_rtos_set_semaphore( &app_info.msg_semaphore ); } break; case WICED_MQTT_EVENT_TYPE_PUBLISH_MSG_RECEIVED: { cJSON *child = NULL; wiced_mqtt_topic_msg_t msg = event->data.pub_recvd; WPRINT_APP_DEBUG(( "[MQTT] Received %.*s for TOPIC : %.*s\n", (int) msg.data_len, msg.data, (int) msg.topic_len, msg.topic )); if ( strncmp( (char*) msg.topic, (const char*) app_info.thing_name, msg.topic_len ) == 0 ) { WPRINT_APP_DEBUG (( "Requested WICED_BULB State[%.*s] Current WICED_BULB State [%s]\n", (int) msg.data_len, msg.data, led_status )); if ( strncasecmp( (char*) msg.data, led_status, msg.data_len ) ) { if ( smart_control == 1 ) { if ( strncmp( (char*) msg.data, "ON", 2 ) == 0 ) led_status = "ON"; else led_status = "OFF"; wiced_rtos_set_semaphore( &app_info.wake_semaphore ); } } else { break; } } else if ( strncmp( (char*) msg.topic, app_info.shadow_delta_topic, msg.topic_len ) == 0 ) { cJSON* temp; original = cJSON_Parse( strchr( (char*) msg.data, '{' ) ); child = original->child; json = NULL; while ( child != NULL ) { WPRINT_APP_DEBUG (( "child->string [%s]\n", child->string )); if ( strcmp( child->string, "state" ) == 0 ) { json = child; break; } child = child->next; } if ( json != NULL ) { temp = cJSON_GetObjectItem( json, "auto" ); if ( temp ) { if ( strncmp( temp->valuestring, "YES", 3 ) == 0 ) { smart_control = button_pressed = 1; WPRINT_APP_INFO (( "smart_control enabled\n" )); } else { smart_control = button_pressed = 0; WPRINT_APP_INFO (( "smart_control not enabled\n" )); } } temp = cJSON_GetObjectItem( json, "status" ); if ( temp ) { WPRINT_APP_INFO(( "Requested updated state from AWS thing LED State [%s] Current LED State [%s]\n", temp->string, led_status )); if ( strncmp( temp->valuestring, "ON", 2 ) == 0 ) { led_status = "ON"; } else { led_status = "OFF"; } wiced_rtos_set_semaphore( &app_info.wake_semaphore ); } } cJSON_Delete( original ); } else { WPRINT_APP_INFO(( "Topic Not found\n" )); } } break; default: break; } return WICED_SUCCESS; }