/************************************************************************** * * Handles low level ACI events, and passes them up to an application * level callback when appropriate * **************************************************************************/ void GoosciBleGatt::pollACI() { static bool setup_required = false; // We enter the if statement only when there is a ACI event // available to be processed if (lib_aci_event_get(&aci_state, &aci_data)) { aci_evt_t *aci_evt; aci_evt = &aci_data.evt; // DEBUG_PRINTLN(F("pollACI")); // DEBUG_PRINT("evt opcode: "); // DEBUG_PRINTLN2(aci_evt->evt_opcode, HEX); // DEBUG_PRINT(F("State Total credit: ")); // DEBUG_PRINTLN(aci_state.data_credit_total); // DEBUG_PRINT(F("State Available credit: ")); // DEBUG_PRINTLN(aci_state.data_credit_available); // DEBUG_PRINT("Event Available credit: "); // DEBUG_PRINTLN(aci_evt->params.device_started.credit_available); switch (aci_evt->evt_opcode) { case ACI_EVT_DEVICE_STARTED: { // DEBUG_PRINTLN("STARTED"); aci_state.data_credit_total = aci_evt->params.device_started.credit_available; switch (aci_evt->params.device_started.device_mode) { case ACI_DEVICE_SETUP: { // DEBUG_PRINTLN("SETUP"); aci_state.device_state = ACI_DEVICE_SETUP; setup_required = true; break; } case ACI_DEVICE_STANDBY: { // DEBUG_PRINTLN("STANDBY"); aci_state.device_state = ACI_DEVICE_STANDBY; // sleep_to_wakeup_timeout = 30; if (aci_evt->params.device_started.hw_error) { // Magic number used to make sure the HW error // event is handled correctly. delay(20); } else { get_address(); // DEBUG_PRINTLN("lib_aci_connect"); lib_aci_connect(adTimeout, adInterval); } break; } case ACI_DEVICE_INVALID: { DEBUG_PRINTLN(F("Evt Device Invalid")); break; } case ACI_DEVICE_TEST: { break; } case ACI_DEVICE_SLEEP: { break; } } break; // case ACI_EVT_DEVICE_STARTED: } case ACI_EVT_CMD_RSP: { DEBUG_PRINTLN("ACI_EVT_CMD_RSP"); // If an ACI command response event comes with an error -> stop if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) { // ACI ReadDynamicData and ACI WriteDynamicData // will have status codes of // TRANSACTION_CONTINUE and TRANSACTION_COMPLETE // all other ACI commands will have status code of // ACI_STATUS_SCUCCESS for a successful command DEBUG_PRINT(F("ACI Command 0x")); DEBUG_PRINTLN2(aci_evt->params.cmd_rsp.cmd_opcode, HEX); DEBUG_PRINT(F("Evt Cmd response: Error. ")); DEBUG_PRINTLN2(aci_evt->params.cmd_rsp.cmd_status, HEX); } if (ACI_CMD_GET_DEVICE_ADDRESS == aci_evt->params.cmd_rsp.cmd_opcode) { // If this is a response to get device address, save the address addrReceived = true; // DEBUG_PRINT(F("Get device address response: ")); for (int i = BTLE_DEVICE_ADDRESS_SIZE - 1; i >= 0; --i) { deviceAddress[i] = aci_evt->params.cmd_rsp.params.get_device_address .bd_addr_own[i]; // DEBUG_PRINT2(deviceAddress[i], HEX); // DEBUG_PRINT(F(":")); } // DEBUG_PRINTLN(F("")); } else if (ACI_CMD_OPEN_ADV_PIPE == aci_evt->params.cmd_rsp.cmd_opcode) { DEBUG_PRINTLN( F("Open advertising pipe response, setting service data.")); lib_aci_set_local_data( &aci_state, PIPE_DEVICE_INFORMATION_MODEL_NUMBER_STRING_BROADCAST, (unsigned char *)longName, 8); DEBUG_PRINT(F("Advertising starting for ")); DEBUG_PRINTLN(deviceName); lib_aci_connect(adTimeout, adInterval); } else if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode) { DEBUG_PRINTLN("ACI_CMD_GET_DEVICE_VERSION"); } else if (ACI_CMD_SET_LOCAL_DATA == aci_evt->params.cmd_rsp.cmd_opcode) { DEBUG_PRINTLN("ACI_CMD_SET_LOCAL_DATA"); } else if (ACI_CMD_CONNECT == aci_evt->params.cmd_rsp.cmd_opcode) { DEBUG_PRINTLN("ACI_CMD_CONNECT"); } else if (ACI_CMD_DISCONNECT == aci_evt->params.cmd_rsp.cmd_opcode) { DEBUG_PRINTLN("ACI_CMD_DISCONNECT"); } else if (ACI_CMD_CHANGE_TIMING == aci_evt->params.cmd_rsp.cmd_opcode) { DEBUG_PRINTLN("ACI_CMD_CHANGE_TIMING"); } else { // print command DEBUG_PRINT(F("Unexpected ACI Command 0x")); DEBUG_PRINTLN2(aci_evt->params.cmd_rsp.cmd_opcode, HEX); } break; } case ACI_EVT_CONNECTED: { // The nRF8001 is now connected to the peer device. DEBUG_PRINTLN(F("Evt Connected")); aci_state.data_credit_available = aci_state.data_credit_total; timing_change_done = false; break; } case ACI_EVT_PIPE_STATUS: { DEBUG_PRINTLN(F("Evt Pipe Status: ")); // DEBUG_PRINT2((int) aci_evt->params.pipe_status.pipes_open_bitmap, HEX); // DEBUG_PRINT(" "); // DEBUG_PRINTLN2((int) aci_evt->params.pipe_status.pipes_closed_bitmap, HEX); if (lib_aci_is_pipe_available(&aci_state, PIPE_GOOSCI_SENSOR_VALUE_TX) && !timing_change_done) { lib_aci_change_timing(6, 6, 0, 600); // Low-latency parameters timing_change_done = true; } break; } case ACI_EVT_TIMING: { // DEBUG_PRINT("ACI_EVT_TIMING: "); // DEBUG_PRINT(aci_evt->params.timing.conn_rf_interval); // DEBUG_PRINT(" "); // DEBUG_PRINT(aci_evt->params.timing.conn_slave_rf_latency); // DEBUG_PRINT(" "); // DEBUG_PRINT(aci_evt->params.timing.conn_rf_timeout); // DEBUG_PRINT(" "); // DEBUG_PRINTLN(aci_evt->params.timing.conn_rf_interval); break; } case ACI_EVT_DISCONNECTED: { // Advertise again if the advertising timed out. DEBUG_PRINTLN(F("Evt Disconnected.")); // TODO(dek): figure out why the transition to using credits // broke disconnection (packets are still transmitted). // Setting the credits to 0 was an experiment but it didn't work. // aci_state.data_credit_available = 0; lib_aci_connect(adTimeout, adInterval); timing_change_done = false; break; } case ACI_EVT_DATA_RECEIVED: { DEBUG_PRINTLN("ACI_EVT_DATA_RECEIVED"); if (aci_evt->params.data_received.rx_data.pipe_number == PIPE_GOOSCI_SENSOR_CONFIGURATION_RX_ACK_AUTO) { int8_t packet_length = aci_evt->len; handle(aci_evt->params.data_received.rx_data.aci_data, aci_evt->len); } else { DEBUG_PRINT(F(" Data(Hex) : ")); for (int i = 0; i < aci_evt->len - 2; i++) { DEBUG_PRINT2(aci_evt->params.data_received.rx_data.aci_data[i], HEX); DEBUG_PRINT(F(" ")); } DEBUG_PRINTLN(F("")); } break; } case ACI_EVT_DATA_CREDIT: { // DEBUG_PRINTLN(F("Evt Credit: Peer has received our send")); aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit; break; } case ACI_EVT_PIPE_ERROR: { // See the appendix in the nRF8001 // Product Specication for details on the error codes DEBUG_PRINT(F("ACI Evt Pipe Error: Pipe #:")); DEBUG_PRINT2(aci_evt->params.pipe_error.pipe_number, DEC); DEBUG_PRINT(F(" Pipe Error Code: 0x")); DEBUG_PRINTLN2(aci_evt->params.pipe_error.error_code, HEX); // Increment the credit available as the data packet was not sent. // The pipe error also represents the Attribute protocol // Error Response sent from the peer and that should not be counted // for the credit. if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code) { aci_state.data_credit_available++; } break; } case ACI_EVT_DATA_ACK: { // DEBUG_PRINTLN(F("ACK")); break; } case ACI_EVT_HW_ERROR: { DEBUG_PRINTLN(F("HW error: ")); DEBUG_PRINTLN2(aci_evt->params.hw_error.line_num, DEC); for (int counter = 0; counter <= (aci_evt->len - 3); counter++) { DEBUG_PRINT(aci_evt->params.hw_error.file_name[counter]); } DEBUG_PRINTLN(); initLocalData(); lib_aci_connect(adTimeout, adInterval); break; } default: { DEBUG_PRINT(F("Evt Opcode 0x")); DEBUG_PRINT2(aci_evt->evt_opcode, HEX); DEBUG_PRINTLN(F(" unhandled")); break; } } } // setup_required is set to true when the device starts // up and enters setup mode. // It indicates that do_aci_setup() should be called. // The flag should be cleared if do_aci_setup() returns // ACI_STATUS_TRANSACTION_COMPLETE. if (setup_required) { int result = do_aci_setup(&aci_state); if (result != SETUP_SUCCESS ) { switch(result) { case SETUP_FAIL_COMMAND_QUEUE_NOT_EMPTY: DEBUG_PRINTLN("SETUP_FAIL_COMMAND_QUEUE_NOT_EMPTY"); break; case SETUP_FAIL_EVENT_QUEUE_NOT_EMPTY: DEBUG_PRINTLN("SETUP_EVENT_COMMAND_QUEUE_NOT_EMPTY"); break; case SETUP_FAIL_NOT_SETUP_EVENT: DEBUG_PRINTLN("SETUP_FAIL_NOT_SETUP_EVENT"); break; case SETUP_FAIL_NOT_COMMAND_RESPONSE: DEBUG_PRINTLN("SETUP_FAIL_NOT_COMMAND_RESPONSE"); break; } } else { setup_required = false; } } }
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")); } } } } }
static void process_events() { // We enter the if statement only when there is a ACI event available to be processed if (lib_aci_event_get(&aci_state, &aci_data)) { aci_evt_t *aci_evt; aci_evt = &aci_data.evt; switch(aci_evt->evt_opcode) { /* As soon as you reset the nRF8001 you will get an ACI Device Started Event */ case ACI_EVT_DEVICE_STARTED: aci_state.data_credit_total = aci_evt->params.device_started.credit_available; switch(aci_evt->params.device_started.device_mode) { case ACI_DEVICE_SETUP: /* When the device is in the setup mode*/ Serial.println(F("Evt Device Started: Setup")); if (ACI_STATUS_TRANSACTION_COMPLETE != do_aci_setup(&aci_state)) { Serial.println(F("Error in ACI Setup")); } break; case ACI_DEVICE_STANDBY: Serial.println(F("Evt Device Started: Standby")); //Looking for an iPhone by sending radio advertisements //When an iPhone connects to us we will get an ACI_EVT_CONNECTED event from the nRF8001 lib_aci_connect(_advt_timeout_inseconds, 0x0050 /* advertising interval 50ms*/); Serial.println(F("Advertising started")); break; } break; //ACI Device Started Event case ACI_EVT_CMD_RSP: //If an ACI command response event comes with an error -> stop if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) { //ACI ReadDynamicData and ACI WriteDynamicData will have status codes of //TRANSACTION_CONTINUE and TRANSACTION_COMPLETE //all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command Serial.print(F("ACI Command ")); Serial.println(aci_evt->params.cmd_rsp.cmd_opcode, HEX); Serial.println(F("Evt Cmd respone: Error. Arduino is in an while(1); loop")); while (1); } if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode) { //Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET, (uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version), sizeof(aci_evt_cmd_rsp_params_get_device_version_t)); } break; case ACI_EVT_CONNECTED: is_connected = 1; Serial.println(F("Evt Connected")); aci_state.data_credit_available = aci_state.data_credit_total; /*Get the device version of the nRF8001 and store it in the Hardware Revision String*/ lib_aci_device_version(); break; case ACI_EVT_PIPE_STATUS: Serial.println(F("Evt Pipe Status")); if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == timing_change_done)) { lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP. // Used to increase or decrease bandwidth timing_change_done = true; } break; case ACI_EVT_TIMING: Serial.println(F("Evt link connection interval changed")); break; case ACI_EVT_DISCONNECTED: is_connected = 0; ack = 1; Serial.println(F("Evt Disconnected/Advertising timed out")); lib_aci_connect(_advt_timeout_inseconds, 0x0100 /* advertising interval 100ms*/); Serial.println(F("Advertising started")); break; case ACI_EVT_DATA_RECEIVED: for(int i=0; i<aci_evt->len - 2; i++) { if(rx_buffer_len == MAX_RX_BUFF) { break; } else { if(p_back == &rx_buff[MAX_RX_BUFF]) { p_back = &rx_buff[0]; } *p_back = aci_evt->params.data_received.rx_data.aci_data[i]; rx_buffer_len++; p_back++; } } break; case ACI_EVT_DATA_CREDIT: aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit; Serial.print("ACI_EVT_DATA_CREDIT "); Serial.print("Data Credit available: "); Serial.println(aci_state.data_credit_available,DEC); ack=1; break; case ACI_EVT_PIPE_ERROR: //See the appendix in the nRF8001 Product Specication for details on the error codes Serial.print(F("ACI Evt Pipe Error: Pipe #:")); Serial.print(aci_evt->params.pipe_error.pipe_number, DEC); Serial.print(F(" Pipe Error Code: 0x")); Serial.println(aci_evt->params.pipe_error.error_code, HEX); //Increment the credit available as the data packet was not sent aci_state.data_credit_available++; Serial.print("Data Credit available: "); Serial.println(aci_state.data_credit_available,DEC); break; } } else { //Serial.println(F("No ACI Events available")); // No event in the ACI Event queue and if there is no event in the ACI command queue the arduino can go to sleep // Arduino can go to sleep now // Wakeup from sleep from the RDYN line } }
static void process_events() { static bool setup_required = false; // We enter the if statement only when there is a ACI event available to be processed if (lib_aci_event_get(&aci_state, &aci_data)) { aci_evt_t *aci_evt; aci_evt = &aci_data.evt; switch(aci_evt->evt_opcode) { /* As soon as you reset the nRF8001 you will get an ACI Device Started Event */ case ACI_EVT_DEVICE_STARTED: aci_state.data_credit_total = aci_evt->params.device_started.credit_available; switch(aci_evt->params.device_started.device_mode) { case ACI_DEVICE_SETUP: /* When the device is in the setup mode*/ Serial.println(F("Evt Device Started: Setup")); setup_required = true; break; case ACI_DEVICE_STANDBY: Serial.println(F("Evt Device Started: Standby")); //Looking for an iPhone by sending radio advertisements //When an iPhone connects to us we will get an ACI_EVT_CONNECTED event from the nRF8001 if (aci_evt->params.device_started.hw_error) { delay(20); //Magic number used to make sure the HW error event is handled correctly. } else { lib_aci_set_local_data(&aci_state, PIPE_GAP_DEVICE_NAME_SET , (uint8_t *)&device_name , strlen(device_name)); // lib_aci_connect(ADVERTISING_TIMEOUT/* in seconds, 0 means forever */, ADVERTISING_INTERVAL /* advertising interval 50ms*/); // Serial.println(F("Advertising started")); } break; } break; //ACI Device Started Event case ACI_EVT_CMD_RSP: //If an ACI command response event comes with an error -> stop if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) { //ACI ReadDynamicData and ACI WriteDynamicData will have status codes of //TRANSACTION_CONTINUE and TRANSACTION_COMPLETE //all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command #ifdef ACI_DEBUG Serial.print(F("ACI Command ")); Serial.println(aci_evt->params.cmd_rsp.cmd_opcode, HEX); Serial.print(F("Evt Cmd respone: Status ")); Serial.println(aci_evt->params.cmd_rsp.cmd_status, HEX); #endif } if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode) { //Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET, (uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version), sizeof(aci_evt_cmd_rsp_params_get_device_version_t)); } break; case ACI_EVT_CONNECTED: Serial.println(F("Evt Connected")); aci_state.data_credit_available = aci_state.data_credit_total; /*Get the device version of the nRF8001 and store it in the Hardware Revision String*/ // lib_aci_device_version(); break; case ACI_EVT_PIPE_STATUS: // if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == timing_change_done)) // { // lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP. // // Used to increase or decrease bandwidth // timing_change_done = true; // } break; case ACI_EVT_TIMING: #ifdef ACI_DEBUG Serial.println(F("Evt link connection interval changed")); #endif break; case ACI_EVT_DISCONNECTED: #ifdef ACI_DEBUG Serial.println(F("Evt Disconnected/Advertising timed out")); #endif break; case ACI_EVT_DATA_RECEIVED: #ifdef ACI_DEBUG Serial.print(F("Pipe Number: ")); Serial.println(aci_evt->params.data_received.rx_data.pipe_number, DEC); #endif // for(int i=0; i<aci_evt->len - 2; i++) // { // if(rx_buffer_len == MAX_RX_BUFF) // { // break; // } // else // { // if(p_back == &rx_buff[MAX_RX_BUFF]) // { // p_back = &rx_buff[0]; // } // *p_back = aci_evt->params.data_received.rx_data.aci_data[i]; // rx_buffer_len++; // p_back++; // } // } break; case ACI_EVT_DATA_CREDIT: aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit; break; case ACI_EVT_PIPE_ERROR: //See the appendix in the nRF8001 Product Specication for details on the error codes Serial.print(F("ACI Evt Pipe Error: Pipe #:")); Serial.print(aci_evt->params.pipe_error.pipe_number, DEC); Serial.print(F(" Pipe Error Code: 0x")); Serial.println(aci_evt->params.pipe_error.error_code, HEX); //Increment the credit available as the data packet was not sent. //The pipe error also represents the Attribute protocol Error Response sent from the peer and that should not be counted //for the credit. if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code) { aci_state.data_credit_available++; } Serial.print("Data Credit available: "); Serial.println(aci_state.data_credit_available,DEC); break; case ACI_EVT_HW_ERROR: Serial.print(F("HW error: ")); Serial.println(aci_evt->params.hw_error.line_num, DEC); for(uint8_t counter = 0; counter <= (aci_evt->len - 3); counter++) { Serial.write(aci_evt->params.hw_error.file_name[counter]); //uint8_t file_name[20]; } Serial.println(); // lib_aci_connect(180/* in seconds, 0 means forever */, 0x0050 /* advertising interval 50ms*/); // lib_aci_connect(ADVERTISING_TIMEOUT/* in seconds, 0 means forever */, ADVERTISING_INTERVAL /* advertising interval 50ms*/); // Serial.println(F("Advertising started")); break; } // Now run our post-event handler postEventHandlerFn(&aci_state, aci_evt); } else { //Serial.println(F("No ACI Events available")); // No event in the ACI Event queue and if there is no event in the ACI command queue the arduino can go to sleep // Arduino can go to sleep now // Wakeup from sleep from the RDYN line } /* setup_required is set to true when the device starts up and enters setup mode. * It indicates that do_aci_setup() should be called. The flag should be cleared if * do_aci_setup() returns ACI_STATUS_TRANSACTION_COMPLETE. */ if(setup_required) { if (SETUP_SUCCESS == do_aci_setup(&aci_state)) { setup_required = false; } } }
/** * This is the default run method. */ void NRF8001Driver::run(){ // We enter the if statement only when there is a ACI event available to be processed if (lib_aci_event_get(this->_aci_state, &this->_aci_data)){ // Get the event pointer aci_evt_t* aci_evt; aci_evt = &this->_aci_data.evt; // We switch on the event opcode switch(aci_evt->evt_opcode){ /** * As soon as you reset the nRF8001 you will get an ACI Device Started Event */ case ACI_EVT_DEVICE_STARTED: // Get the number of credits this->_aci_state->data_credit_total = aci_evt->params.device_started.credit_available; // Switch on the device mode switch(aci_evt->params.device_started.device_mode){ // In setup mode case ACI_DEVICE_SETUP: #ifdef DEBUG Serial.println(F("Evt Device Started: Setup")); #endif // A setup must to issued before use this->_setup_required = true; break; // In standby mode case ACI_DEVICE_STANDBY: #ifdef DEBUG Serial.println(F("Evt Device Started: Standby")); #endif // Looking for a device by sending radio advertisements // When the device connects to us we will get an ACI_EVT_CONNECTED event from the nRF8001 if (aci_evt->params.device_started.hw_error){ delay(20); //Magic number used to make sure the HW error event is handled correctly. }else{ // Connect lib_aci_connect(180/* in seconds */, 0x0050 /* advertising interval 50ms*/); #ifdef DEBUG Serial.println(F("Advertising process started")); #endif } break; } break; /** * If we get a command response */ case ACI_EVT_CMD_RSP: // If an ACI command response event comes with an error -> stop if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status){ // ACI ReadDynamicData and ACI WriteDynamicData will have status codes of // TRANSACTION_CONTINUE and TRANSACTION_COMPLETE // all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command #ifdef DEBUG Serial.print(F("ACI Command ")); Serial.println(aci_evt->params.cmd_rsp.cmd_opcode, HEX); Serial.print(F("Evt Cmd respone: Status ")); Serial.println(aci_evt->params.cmd_rsp.cmd_status, HEX); #endif } // If we get a Device Version request if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode){ // Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic lib_aci_set_local_data(this->_aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET, (uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version), sizeof(aci_evt_cmd_rsp_params_get_device_version_t)); } break; /** * If we get a connected event */ case ACI_EVT_CONNECTED: // UART setup is complete #ifdef DEBUG Serial.println(F("Evt Connected")); #endif this->_uart_over_ble_init(); this->_timing_change_done = false; this->_aci_state->data_credit_available = this->_aci_state->data_credit_total; /** * Get the device version of the nRF8001 and store it in the Hardware Revision String */ lib_aci_device_version(); break; /** * If we get a pipe status request */ case ACI_EVT_PIPE_STATUS: #ifdef DEBUG Serial.println(F("Evt Pipe Status")); #endif // Check the pipes if (lib_aci_is_pipe_available(this->_aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == this->_timing_change_done)){ // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP. // Used to increase or decrease bandwidth lib_aci_change_timing_GAP_PPCP(); this->_timing_change_done = true; } break; /** * If we get a timing change request */ case ACI_EVT_TIMING: #ifdef DEBUG Serial.println(F("Evt link connection interval changed")); #endif // Set the new timing lib_aci_set_local_data(this->_aci_state, PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET, (uint8_t *)&(aci_evt->params.timing.conn_rf_interval), /* Byte aligned */ PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET_MAX_SIZE); break; /** * If we get disconnected status */ case ACI_EVT_DISCONNECTED: #ifdef DEBUG Serial.println(F("Evt Disconnected/Advertising timed out")); #endif // Start advertising lib_aci_connect(180/* in seconds */, 0x0100 /* advertising interval 100ms*/); break; /** * If we get data from the remote node */ case ACI_EVT_DATA_RECEIVED: #ifdef DEBUG Serial.print(F("Pipe Number: ")); Serial.println(aci_evt->params.data_received.rx_data.pipe_number, DEC); #endif // If we have received data from the rx pipe if (PIPE_UART_OVER_BTLE_UART_RX_RX == aci_evt->params.data_received.rx_data.pipe_number){ // Reset the buffer memory memset(this->_uart_buffer.data, 0x00, sizeof(this->_uart_buffer)); // Reset the length this->_uart_buffer.length = 0; // Store the data received into a buffer type for(register uint8_t i = 0; i < aci_evt->len - 2; i++){ this->_uart_buffer.data[i] = aci_evt->params.data_received.rx_data.aci_data[i]; this->_uart_buffer.length ++; } #ifdef DEBUG Serial.print("Read data: "); Serial.write((uint8_t*)this->_uart_buffer.data, this->_uart_buffer.length); Serial.println(); #endif // Put the data within the ring buffer for(register uint8_t i = 0; i < this->_uart_buffer.length; i ++){ this->_utils->ring_buffer.put(this->_uart_buffer.data[i]); } } // If we got a control point byte if (PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_RX == aci_evt->params.data_received.rx_data.pipe_number){ // We process the token this->_uart_process_control_point(&aci_evt->params.data_received.rx_data.aci_data[0], aci_evt->len - 2); //Subtract for Opcode and Pipe number } break; /** * If we get a data credit token */ case ACI_EVT_DATA_CREDIT: // We add a credit token to the queue this->_aci_state->data_credit_available += aci_evt->params.data_credit.credit; break; /** * If we get a pipe error */ case ACI_EVT_PIPE_ERROR: // See the appendix in the nRF8001 Product Specification for details on the error codes // Increment the credit available as the data packet was not sent. // The pipe error also represents the Attribute protocol Error Response sent from the peer and that should not be counted // for the credit. if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code){ this->_aci_state->data_credit_available++; } break; /** * If we get a hardware error */ case ACI_EVT_HW_ERROR: #ifdef DEBUG Serial.print(F("HW error: ")); Serial.println(aci_evt->params.hw_error.line_num, DEC); #endif // Restart the adversiting mode... lib_aci_connect(180/* in seconds */, 0x0050 /* advertising interval 50ms*/); break; } // End switch } // End If /** * If setup_required is set to true when the device starts up and enters setup mode. * It indicates that do_aci_setup() should be called. The flag should be cleared if * do_aci_setup() returns ACI_STATUS_TRANSACTION_COMPLETE. */ if(this->_setup_required){ // If we setup properly if (SETUP_SUCCESS == do_aci_setup(this->_aci_state)) { this->_setup_required = false; } } }
void aci_loop(int *flag) { // We enter the if statement only when there is a ACI event available to be processed if (lib_aci_event_get(&aci_state, &aci_data)) { aci_evt_t * aci_evt; DBPRINTLN("There is an ACI Events available"); aci_evt = &aci_data.evt; switch(aci_evt->evt_opcode) { /* As soon as you reset the nRF8001 you will get an ACI Device Started Event */ case ACI_EVT_DEVICE_STARTED: { aci_state.data_credit_total = aci_evt->params.device_started.credit_available; switch(aci_evt->params.device_started.device_mode) { /** When the device is in the setup mode */ case ACI_DEVICE_SETUP: DBPRINTLN("Evt Device Started: Setup"); if (ACI_STATUS_TRANSACTION_COMPLETE != do_aci_setup(&aci_state)) DBPRINTLN("Error in ACI Setup"); break; case ACI_DEVICE_STANDBY: DBPRINTLN("Evt Device Started: Standby"); //Looking for an iPhone by sending radio advertisements //When an iPhone connects to us we will get an ACI_EVT_CONNECTED event from the nRF8001 lib_aci_connect(180/* in seconds */, 0x0050 /* advertising interval 50ms*/); DBPRINTLN("Advertising started"); break; } } break; //ACI Device Started Event case ACI_EVT_CMD_RSP: //If an ACI command response event comes with an error -> stop if (ACI_STATUS_TRANSACTION_CONTINUE == aci_evt->params.cmd_rsp.cmd_status) { DBPRINTLN("Reading/Writing dynamic data..."); } else if (ACI_STATUS_TRANSACTION_COMPLETE == aci_evt->params.cmd_rsp.cmd_status) { DBPRINTLN("Reading/Writing dynamic data finished."); } else if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) { //ACI ReadDynamicData and ACI WriteDynamicData will have status codes of //TRANSACTION_CONTINUE and TRANSACTION_COMPLETE //all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command DBPRINT("Error ACI Command "); Serial.print(aci_evt->params.cmd_rsp.cmd_opcode, HEX); DBPRINT(" Evt Cmd respone error code: "); Serial.println(aci_evt->params.cmd_rsp.cmd_status, HEX); //while (1); } // react to different command responses switch (aci_evt->params.cmd_rsp.cmd_opcode) { case ACI_CMD_GET_DEVICE_VERSION: // Debug print DBPRINTLN("Debug: printting configuration id, aci version, setup format, id, and status from the cmd rsp opcode:"); DBPRINTLN(aci_evt->params.cmd_rsp.params.get_device_version.configuration_id); DBPRINTLN(aci_evt->params.cmd_rsp.params.get_device_version.aci_version); DBPRINTLN(aci_evt->params.cmd_rsp.params.get_device_version.setup_format); DBPRINTLN(aci_evt->params.cmd_rsp.params.get_device_version.setup_id); DBPRINTLN(aci_evt->params.cmd_rsp.params.get_device_version.setup_status); //Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET, (uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version), sizeof(aci_evt_cmd_rsp_params_get_device_version_t)); break; case ACI_CMD_GET_DEVICE_ADDRESS: // Debug print DBPRINTLN("Debug: printting device address, and address type:"); DBPRINTLN(aci_evt->params.cmd_rsp.params.get_device_address.bd_addr_own[0]); DBPRINTLN(aci_evt->params.cmd_rsp.params.get_device_address.bd_addr_type); break; case ACI_CMD_GET_TEMPERATURE: // Debug print DBPRINTLN("Debug: printting device temperature:"); DBPRINTLN(aci_evt->params.cmd_rsp.params.get_temperature.temperature_value/4); break; case ACI_CMD_READ_DYNAMIC_DATA: // Debug print DBPRINT("Debug: dynamic data:"); DBPRINTLN(aci_evt->params.cmd_rsp.params.get_temperature.temperature_value/4); break; } break; case ACI_EVT_CONNECTED: DBPRINTLN("Evt Connected"); aci_state.data_credit_available = aci_state.data_credit_total; // Get the device version of the nRF8001 and store it in the Hardware Revision String lib_aci_device_version(); break; case ACI_EVT_PIPE_STATUS: DBPRINTLN("Evt Pipe Status"); if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == timing_change_done)) { lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP. // Used to increase or decrease bandwidth timing_change_done = true; } if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX)) { DBPRINTLN("UART pipe over BLE is available"); *flag = 1; } break; case ACI_EVT_TIMING: DBPRINTLN("Evt link connection interval changed"); break; case ACI_EVT_DISCONNECTED: DBPRINTLN("Evt Disconnected/Advertising timed out"); lib_aci_connect(180/* in seconds */, 0x0100 /* advertising interval 100ms*/); DBPRINTLN("Advertising started"); *flag = 0; break; case ACI_EVT_DATA_RECEIVED: DBPRINT("UART RX: 0x"); Serial.print(aci_evt->params.data_received.rx_data.pipe_number, HEX); { DBPRINT(" Data(Hex) : "); for(int i=0; i<aci_evt->len - 2; i++) { Serial.print(aci_evt->params.data_received.rx_data.aci_data[i], HEX); uart_buffer[i] = aci_evt->params.data_received.rx_data.aci_data[i]; DBPRINT(" "); } uart_buffer_len = aci_evt->len - 2; } DBPRINT("I got the request"); break; case ACI_EVT_DATA_CREDIT: aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit; break; case ACI_EVT_PIPE_ERROR: //See the appendix in the nRF8001 Product Specication for details on the error codes DBPRINT("ACI Evt Pipe Error: Pipe #:"); DBPRINT(aci_evt->params.pipe_error.pipe_number); DBPRINT(" Pipe Error Code: 0x"); DBPRINTLN(aci_evt->params.pipe_error.error_code); //Increment the credit available as the data packet was not sent aci_state.data_credit_available++; break; default: DBPRINTLN("Unrecognized Event"); break; } } else { //DBPRINTLN(F("No ACI Events available")); // No event in the ACI Event queue and if there is no event in the ACI command queue the arduino can go to sleep // Arduino can go to sleep now // Wakeup from sleep from the RDYN line } }
void Adafruit_BLE_UART::pollACI() { static bool setup_required = false; // We enter the if statement only when there is a ACI event available to be processed if (lib_aci_event_get(&aci_state, &aci_data)) { aci_evt_t * aci_evt; aci_evt = &aci_data.evt; switch(aci_evt->evt_opcode) { /** As soon as you reset the nRF8001 you will get an ACI Device Started Event */ case ACI_EVT_DEVICE_STARTED: { aci_state.data_credit_total = aci_evt->params.device_started.credit_available; switch(aci_evt->params.device_started.device_mode) { case ACI_DEVICE_SETUP: /** When the device is in the setup mode */ Serial.println(F("Evt Device Started: Setup")); setup_required = true; break; case ACI_DEVICE_STANDBY: Serial.println(F("Evt Device Started: Standby")); //Looking for an iPhone by sending radio advertisements //When an iPhone connects to us we will get an ACI_EVT_CONNECTED event from the nRF8001 if (aci_evt->params.device_started.hw_error) { delay(20); //Handle the HW error event correctly. if (debugMode) { Serial.println(F("Error in ACI Setup")); } } else { lib_aci_connect(0/* in seconds : 0 means forever */, 0x0050 /* advertising interval 50ms*/); defaultACICallback(ACI_EVT_DEVICE_STARTED); Serial.println(F("Advertising started : Tap Connect on the nRF UART app or client application")); } break; } } break; //ACI Device Started Event case ACI_EVT_CMD_RSP: //If an ACI command response event comes with an error -> stop if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) { //ACI ReadDynamicData and ACI WriteDynamicData will have status codes of //TRANSACTION_CONTINUE and TRANSACTION_COMPLETE //all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command Serial.print(F("ACI Command ")); Serial.println(aci_evt->params.cmd_rsp.cmd_opcode, HEX); Serial.print(F("Evt Cmd respone: Status ")); Serial.println(aci_evt->params.cmd_rsp.cmd_status, HEX); } if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode) { //Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET, (uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version), sizeof(aci_evt_cmd_rsp_params_get_device_version_t)); } break; case ACI_EVT_CONNECTED: Serial.println(F("Evt Connected")); uart_over_ble_init(); timing_change_done = false; aci_state.data_credit_available = aci_state.data_credit_total; /* Get the device version of the nRF8001 and store it in the Hardware Revision String */ lib_aci_device_version(); defaultACICallback(ACI_EVT_CONNECTED); break; case ACI_EVT_PIPE_STATUS: Serial.println(F("Evt Pipe Status")); if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == timing_change_done)) { lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP. // Used to increase or decrease bandwidth timing_change_done = true; } break; case ACI_EVT_TIMING: Serial.println(F("Evt link connection interval changed")); lib_aci_set_local_data(&aci_state, PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET, (uint8_t *)&(aci_evt->params.timing.conn_rf_interval), /* Byte aligned */ PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET_MAX_SIZE); break; case ACI_EVT_DISCONNECTED: defaultACICallback(ACI_EVT_DISCONNECTED); Serial.println(F("Evt Disconnected/Advertising timed out")); lib_aci_connect(0/* in seconds : 0 means forever */, 0x0050 /* advertising interval 50ms*/); defaultACICallback(ACI_EVT_DEVICE_STARTED); Serial.println(F("Advertising started. Tap Connect on the nRF UART app")); break; case ACI_EVT_DATA_RECEIVED: Serial.print(F("Pipe Number: ")); Serial.println(aci_evt->params.data_received.rx_data.pipe_number, DEC); defaultRX(aci_evt->params.data_received.rx_data.aci_data, aci_evt->len - 2); if (rx_event) { rx_event(aci_evt->params.data_received.rx_data.aci_data, aci_evt->len - 2); } if (PIPE_UART_OVER_BTLE_UART_RX_RX == aci_evt->params.data_received.rx_data.pipe_number) { Serial.print(F(" Data(Hex) : ")); for(int i=0; i<aci_evt->len - 2; i++) { Serial.print((char)aci_evt->params.data_received.rx_data.aci_data[i]); uart_buffer[i] = aci_evt->params.data_received.rx_data.aci_data[i]; Serial.print(F(" ")); } uart_buffer_len = aci_evt->len - 2; Serial.println(F("")); if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX)) { /*Do this to test the loopback otherwise comment it out*/ /* if (!uart_tx(&uart_buffer[0], aci_evt->len - 2)) { Serial.println(F("UART loopback failed")); } else { Serial.println(F("UART loopback OK")); } */ } } if (PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_RX == aci_evt->params.data_received.rx_data.pipe_number) { uart_process_control_point_rx(&aci_evt->params.data_received.rx_data.aci_data[0], aci_evt->len - 2); //Subtract for Opcode and Pipe number } break; case ACI_EVT_DATA_CREDIT: aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit; break; case ACI_EVT_PIPE_ERROR: //See the appendix in the nRF8001 Product Specication for details on the error codes if ( debugMode ) { Serial.print(F("ACI Evt Pipe Error: Pipe #:")); Serial.print(aci_evt->params.pipe_error.pipe_number, DEC); Serial.print(F(" Pipe Error Code: 0x")); Serial.println(aci_evt->params.pipe_error.error_code, HEX); } //Increment the credit available as the data packet was not sent. //The pipe error also represents the Attribute protocol Error Response sent from the peer and that should not be counted //for the credit. if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code) { aci_state.data_credit_available++; } break; case ACI_EVT_HW_ERROR: Serial.print(F("HW error: ")); Serial.println(aci_evt->params.hw_error.line_num, DEC); for(uint8_t counter = 0; counter <= (aci_evt->len - 3); counter++) { Serial.write(aci_evt->params.hw_error.file_name[counter]); //uint8_t file_name[20]; } Serial.println(); lib_aci_connect(0/* in seconds, 0 means forever */, 0x0050 /* advertising interval 50ms*/); Serial.println(F("Advertising started. Tap Connect on the nRF UART app")); break; } } else { if ( debugMode ) { //Serial.println(F("No ACI Events available")); } // No event in the ACI Event queue and if there is no event in the ACI command queue the arduino can go to sleep // Arduino can go to sleep now // Wakeup from sleep from the RDYN line } /* setup_required is set to true when the device starts up and enters setup mode. * It indicates that do_aci_setup() should be called. The flag should be cleared if * do_aci_setup() returns ACI_STATUS_TRANSACTION_COMPLETE. */ if(setup_required) { if (SETUP_SUCCESS == do_aci_setup(&aci_state)) { setup_required = false; } } }
uint8_t do_aci_setup(aci_state_t *aci_stat) { uint8_t setup_offset = 0; uint32_t i = 0x0000; aci_evt_t * aci_evt = NULL; aci_status_code_t cmd_status = ACI_STATUS_ERROR_CRC_MISMATCH; /* We are using the same buffer since we are copying the contents of the buffer when queuing and immediately processing the buffer when receiving */ hal_aci_evt_t *aci_data = (hal_aci_evt_t *)&msg_to_send; /* Messages in the outgoing queue must be handled before the Setup routine can run. * If it is non-empty we return. The user should then process the messages before calling * do_aci_setup() again. */ if (!lib_aci_command_queue_empty()) { return SETUP_FAIL_COMMAND_QUEUE_NOT_EMPTY; } /* If there are events pending from the device that are not relevant to setup, we return false * so that the user can handle them. At this point we don't care what the event is, * as any event is an error. */ if (lib_aci_event_peek(aci_data)) { return SETUP_FAIL_EVENT_QUEUE_NOT_EMPTY; } /* Fill the ACI command queue with as many Setup messages as it will hold. */ aci_setup_fill(aci_stat, &setup_offset); while (cmd_status != ACI_STATUS_TRANSACTION_COMPLETE) { /* This counter is used to ensure that this function does not loop forever. When the device * returns a valid response, we reset the counter. */ if (i++ > 0xFFFFE) { return SETUP_FAIL_TIMEOUT; } if (lib_aci_event_peek(aci_data)) { aci_evt = &(aci_data->evt); if (ACI_EVT_CMD_RSP != aci_evt->evt_opcode) { //Receiving something other than a Command Response Event is an error. return SETUP_FAIL_NOT_COMMAND_RESPONSE; } cmd_status = (aci_status_code_t) aci_evt->params.cmd_rsp.cmd_status; switch (cmd_status) { case ACI_STATUS_TRANSACTION_CONTINUE: //As the device is responding, reset guard counter i = 0; /* As the device has processed the Setup messages we put in the command queue earlier, * we can proceed to fill the queue with new messages */ aci_setup_fill(aci_stat, &setup_offset); break; case ACI_STATUS_TRANSACTION_COMPLETE: //Break out of the while loop when this status code appears break; default: //An event with any other status code should be handled by the application return SETUP_FAIL_NOT_SETUP_EVENT; } /* If we haven't returned at this point, the event was either ACI_STATUS_TRANSACTION_CONTINUE * or ACI_STATUS_TRANSACTION_COMPLETE. We don't need the event itself, so we simply * remove it from the queue. */ lib_aci_event_get (aci_stat, aci_data); } } return SETUP_SUCCESS; }
void Adafruit_BLE_UART::pollACI() { // We enter the if statement only when there is a ACI event available to be processed if (lib_aci_event_get(&aci_state, &aci_data)) { aci_evt_t * aci_evt; aci_evt = &aci_data.evt; switch(aci_evt->evt_opcode) { /* As soon as you reset the nRF8001 you will get an ACI Device Started Event */ case ACI_EVT_DEVICE_STARTED: { aci_state.data_credit_total = aci_evt->params.device_started.credit_available; switch(aci_evt->params.device_started.device_mode) { case ACI_DEVICE_SETUP: /* Device is in setup mode! */ if (ACI_STATUS_TRANSACTION_COMPLETE != do_aci_setup(&aci_state)) { if (debugMode) { Serial.println(F("Error in ACI Setup")); } } break; case ACI_DEVICE_STANDBY: /* Start advertising ... first value is advertising time in seconds, the */ /* second value is the advertising interval in 0.625ms units */ if (device_name[0] != 0x00) { /* Update the device name */ lib_aci_set_local_data(&aci_state, PIPE_GAP_DEVICE_NAME_SET , (uint8_t *)&device_name, strlen(device_name)); } lib_aci_connect(adv_timeout, adv_interval); defaultACICallback(ACI_EVT_DEVICE_STARTED); if (aci_event) aci_event(ACI_EVT_DEVICE_STARTED); } } break; case ACI_EVT_CMD_RSP: /* If an ACI command response event comes with an error -> stop */ if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) { // ACI ReadDynamicData and ACI WriteDynamicData will have status codes of // TRANSACTION_CONTINUE and TRANSACTION_COMPLETE // all other ACI commands will have status code of ACI_STATUS_SUCCESS for a successful command if (debugMode) { Serial.print(F("ACI Command ")); Serial.println(aci_evt->params.cmd_rsp.cmd_opcode, HEX); Serial.println(F("Evt Cmd respone: Error. Arduino is in an while(1); loop")); } while (1); } if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode) { // Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET, (uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version), sizeof(aci_evt_cmd_rsp_params_get_device_version_t)); } break; case ACI_EVT_CONNECTED: aci_state.data_credit_available = aci_state.data_credit_total; /* Get the device version of the nRF8001 and store it in the Hardware Revision String */ lib_aci_device_version(); defaultACICallback(ACI_EVT_CONNECTED); if (aci_event) aci_event(ACI_EVT_CONNECTED); case ACI_EVT_PIPE_STATUS: if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == timing_change_done)) { lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP. // Used to increase or decrease bandwidth timing_change_done = true; } break; case ACI_EVT_TIMING: /* Link connection interval changed */ break; case ACI_EVT_DISCONNECTED: /* Restart advertising ... first value is advertising time in seconds, the */ /* second value is the advertising interval in 0.625ms units */ defaultACICallback(ACI_EVT_DISCONNECTED); if (aci_event) aci_event(ACI_EVT_DISCONNECTED); lib_aci_connect(adv_timeout, adv_interval); defaultACICallback(ACI_EVT_DEVICE_STARTED); if (aci_event) aci_event(ACI_EVT_DEVICE_STARTED); break; case ACI_EVT_DATA_RECEIVED: defaultRX(aci_evt->params.data_received.rx_data.aci_data, aci_evt->len - 2); if (rx_event) rx_event(aci_evt->params.data_received.rx_data.aci_data, aci_evt->len - 2); break; case ACI_EVT_DATA_CREDIT: aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit; break; case ACI_EVT_PIPE_ERROR: /* See the appendix in the nRF8001 Product Specication for details on the error codes */ if (debugMode) { Serial.print(F("ACI Evt Pipe Error: Pipe #:")); Serial.print(aci_evt->params.pipe_error.pipe_number, DEC); Serial.print(F(" Pipe Error Code: 0x")); Serial.println(aci_evt->params.pipe_error.error_code, HEX); } /* Increment the credit available as the data packet was not sent */ aci_state.data_credit_available++; break; } } else { // Serial.println(F("No ACI Events available")); // No event in the ACI Event queue and if there is no event in the ACI command queue the arduino can go to sleep // Arduino can go to sleep now // Wakeup from sleep from the RDYN line } }