Esempio n. 1
0
void ble_begin()
{
	hal_aci_tl_io_config();  

	is_connected = 0;

	app_state = APP_INIT;
	lib_aci_init();

	ENABLE_INTERRUPTS();

	lib_aci_radio_reset();

	app_state = APP_SLEEP;
	on_process_app();
}
Esempio n. 2
0
void lib_aci_board_init(aci_state_t *aci_stat)
{
	hal_aci_evt_t *aci_data = NULL;
	aci_data = (hal_aci_evt_t *)&msg_to_send;
					
	if (REDBEARLAB_SHIELD_V1_1 == aci_stat->aci_pins.board_name)
	{
	  /*
	  The Bluetooth low energy Arduino shield v1.1 requires about 100ms to reset.
	  This is not required for the nRF2740, nRF2741 modules
	  */
	  delay(100);
  
	  /*
	  Send the soft reset command to the nRF8001 to get the nRF8001 to a known state.
	  */
	  lib_aci_radio_reset();
  
	  while (1)
	  {
		/*Wait for the command response of the radio reset command.
		as the nRF8001 will be in either SETUP or STANDBY after the ACI Reset Radio is processed
		*/

			
		if (true == lib_aci_event_get(aci_stat, aci_data))
		{
		  aci_evt_t * aci_evt;      
		  aci_evt = &(aci_data->evt);
	  
		  if (ACI_EVT_CMD_RSP == aci_evt->evt_opcode)
		  {
				if (ACI_STATUS_ERROR_DEVICE_STATE_INVALID == aci_evt->params.cmd_rsp.cmd_status) //in SETUP
				{
					//Inject a Device Started Event Setup to the ACI Event Queue
					msg_to_send.buffer[0] = 4;    //Length
					msg_to_send.buffer[1] = 0x81; //Device Started Event
					msg_to_send.buffer[2] = 0x02; //Setup
					msg_to_send.buffer[3] = 0;    //Hardware Error -> None
					msg_to_send.buffer[4] = 2;    //Data Credit Available
					m_aci_q_enqueue(&aci_rx_q, &msg_to_send);            
				}
				else if (ACI_STATUS_SUCCESS == aci_evt->params.cmd_rsp.cmd_status) //We are now in STANDBY
				{
					//Inject a Device Started Event Standby to the ACI Event Queue
					msg_to_send.buffer[0] = 4;    //Length
					msg_to_send.buffer[1] = 0x81; //Device Started Event
					msg_to_send.buffer[2] = 0x03; //Standby
					msg_to_send.buffer[3] = 0;    //Hardware Error -> None
					msg_to_send.buffer[4] = 2;    //Data Credit Available
					m_aci_q_enqueue(&aci_rx_q, &msg_to_send);            
				}
				else if (ACI_STATUS_ERROR_CMD_UNKNOWN == aci_evt->params.cmd_rsp.cmd_status) //We are now in TEST
				{
					//Inject a Device Started Event Standby to the ACI Event Queue
					msg_to_send.buffer[0] = 4;    //Length
					msg_to_send.buffer[1] = 0x81; //Device Started Event
					msg_to_send.buffer[2] = 0x01; //Test
					msg_to_send.buffer[3] = 0;    //Hardware Error -> None
					msg_to_send.buffer[4] = 0;    //Data Credit Available
					m_aci_q_enqueue(&aci_rx_q, &msg_to_send);
				}
				
				//Break out of the while loop
				break;
		  }
		  else
		  {			
			//Serial.println(F("Discard any other ACI Events"));
		  }
	  
		}
	  }		
	}
}
/**
 * This method is used to generic system wide actions
 *
 * @param command						- The issued command
 */
void ConnectionProtocolHandler::server(uint8_t command, void* object){

	//! Cast the object
	ConnectionProtocolHandler* access = (ConnectionProtocolHandler*) object;

	//! We switch on the command
	switch(command){

		//! We reset the device
		case CONNECT_CALLBACK:
			
			// Set the new connection state and write the ACK
			access->_utils->connection_state = CONNECTED_STATE;
			access->_rf->write((uint8_t*)OK_CALLBACK, 0x01);
			
			#ifdef DEBUG
				Serial.println("Connected from Remote node.");
			#endif
			break;

                //! We start the device
		case REBOOT_CALLBACK:
		
			// Set the new connection state and write the ACK
			access->_utils->connection_state = CLOSED_STATE;
			access->_rf->write((uint8_t*)OK_CALLBACK, 0x01);
			
			#ifdef DEBUG
				Serial.println("Rebooting...");
			#endif
			
			// Stop the main thread
            access->_utils->start_engine = false;
			
			// Disconnect and reset
			while(!lib_aci_disconnect(access->_rf->_aci_state, 
									  ACI_REASON_TERMINATE));
			while(!lib_aci_radio_reset());

			// Delay and then reboot the system
			delay(1000);
            access->_utils->reboot();
            break;

		//! We stop the device
		case DISCONNECT_CALLBACK:
		
			// Set the new connection state and write the ACK
			access->_utils->connection_state = DISCONNECTED_STATE;
			access->_rf->write((uint8_t*)OK_CALLBACK, 0x01);
			
			// Disconnect
			while(!lib_aci_disconnect(access->_rf->_aci_state,
										ACI_REASON_TERMINATE));

			#ifdef DEBUG
				Serial.println("Disconnected from Remote node.");
			#endif
			
			// We are now connected
			access->_utils->start_engine = true;
            break;
       
		//! Nothing happens
		default:
			break;
	}
}