123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
-
-
- #include <unistd.h>
- #include <iostream>
- #include "nrf8001.h"
- #include "nrf8001_helloworld.h"
- #include <lib_aci.h>
- #include <aci_setup.h>
- #include <signal.h>
- #include "uart_over_ble.h"
-
-
-
- #ifdef SERVICES_PIPE_TYPE_MAPPING_CONTENT
- static services_pipe_type_mapping_t
- services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT;
- #else
- #define NUMBER_OF_PIPES 0
- static services_pipe_type_mapping_t * services_pipe_type_mapping = NULL;
- #endif
-
-
- static hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] = SETUP_MESSAGES_CONTENT;
-
-
- static struct aci_state_t aci_state;
-
-
- static hal_aci_evt_t aci_data;
-
-
- static bool timing_change_done = false;
-
-
- static uart_over_ble_t uart_over_ble;
- static uint8_t uart_buffer[20];
- static uint8_t uart_buffer_len = 0;
- static uint8_t dummychar = 0;
-
- void
- sig_handler(int signo)
- {
- printf("got signal\n");
- if (signo == SIGINT) {
- printf("exiting application\n");
- }
- }
-
- void
- init_aci_setup () {
-
-
- if (NULL != services_pipe_type_mapping) {
- aci_state.aci_setup_info.services_pipe_type_mapping = &services_pipe_type_mapping[0];
- } else {
- aci_state.aci_setup_info.services_pipe_type_mapping = NULL;
- }
-
- aci_state.aci_setup_info.number_of_pipes = NUMBER_OF_PIPES;
- aci_state.aci_setup_info.setup_msgs = setup_msgs;
- aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES;
- }
-
- void
- uart_over_ble_init (void) {
- uart_over_ble.uart_rts_local = true;
- }
-
- bool
- uart_tx (uint8_t *buffer, uint8_t buffer_len) {
- bool status = false;
-
- if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) &&
- (aci_state.data_credit_available >= 1)) {
- status = lib_aci_send_data(PIPE_UART_OVER_BTLE_UART_TX_TX, buffer, buffer_len);
- if (status) {
- aci_state.data_credit_available--;
- }
- }
-
- return status;
- }
-
- bool
- uart_process_control_point_rx(uint8_t *byte, uint8_t length) {
- bool status = false;
- aci_ll_conn_params_t *conn_params;
-
- if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_TX) ) {
- switch (*byte) {
-
-
- case UART_OVER_BLE_DISCONNECT:
-
-
- lib_aci_disconnect(&aci_state, ACI_REASON_TERMINATE);
- status = true;
- break;
-
-
-
- case UART_OVER_BLE_LINK_TIMING_REQ:
-
-
- conn_params = (aci_ll_conn_params_t *)(byte+1);
- lib_aci_change_timing( conn_params->min_conn_interval,
- conn_params->max_conn_interval,
- conn_params->slave_latency,
- conn_params->timeout_mult);
- status = true;
- break;
-
-
-
- case UART_OVER_BLE_TRANSMIT_STOP:
-
-
- uart_over_ble.uart_rts_local = false;
- status = true;
- break;
-
-
-
-
- case UART_OVER_BLE_TRANSMIT_OK:
-
-
- uart_over_ble.uart_rts_local = true;
- status = true;
- break;
- }
- }
-
- return status;
- }
-
- int
- main(int argc, char **argv)
- {
-
-
- init_aci_setup ();
- init_local_interfaces (&aci_state, 10, 8, 4);
-
- while (1) {
- static bool setup_required = false;
-
-
- if (lib_aci_event_get(&aci_state, &aci_data)) {
- aci_evt_t * aci_evt;
- aci_evt = &aci_data.evt;
- switch(aci_evt->evt_opcode) {
-
-
- 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:
-
-
- printf ("Evt Device Started: Setup \n");
- setup_required = true;
- break;
-
- case ACI_DEVICE_STANDBY:
- printf ("Evt Device Started: Standby \n");
-
-
- if (aci_evt->params.device_started.hw_error) {
- usleep (20000);
- } else {
- lib_aci_connect(0, 0x0050 );
- printf ("Advertising started \n");
- }
- break;
- }
- }
- break;
-
- case ACI_EVT_CMD_RSP:
-
- if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) {
-
-
-
- printf ("ACI_EVT_CMD_RSP \n");
- }
- if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode) {
-
- 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:
- printf ("ACI_EVT_CONNECTED");
- uart_over_ble_init ();
- timing_change_done = false;
- aci_state.data_credit_available = aci_state.data_credit_total;
-
-
-
- lib_aci_device_version();
- break;
-
- case ACI_EVT_PIPE_STATUS:
- printf ("ACI_EVT_PIPE_STATUS \n");
- 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();
-
- timing_change_done = true;
-
- char hello[]="Hello World, works";
- uart_tx((uint8_t *)&hello[0], strlen(hello));
- }
- break;
-
- case ACI_EVT_TIMING:
- printf ("Evt link connection interval changed \n");
- 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),
- PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET_MAX_SIZE);
- break;
-
- case ACI_EVT_DISCONNECTED:
- printf ("ACI_EVT_DISCONNECTED \n");
- lib_aci_connect(0, 0x0050 );
- printf ("Advertising started \n");
- break;
-
- case ACI_EVT_DATA_RECEIVED:
- if (PIPE_UART_OVER_BTLE_UART_RX_RX == aci_evt->params.data_received.rx_data.pipe_number) {
- for(int i=0; i<aci_evt->len - 2; i++) {
- uart_buffer[i] = aci_evt->params.data_received.rx_data.aci_data[i];
- }
-
- uart_buffer_len = aci_evt->len - 2;
- if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX)) {
- }
- }
-
- 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);
- }
-
- printf ("Incomming data - %s\n", uart_buffer);
- break;
-
- case ACI_EVT_DATA_CREDIT:
- printf ("ACI_EVT_DATA_CREDIT \n");
- aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit;
- break;
-
- case ACI_EVT_PIPE_ERROR:
- printf ("ACI_EVT_PIPE_ERROR \n");
-
-
-
- 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:
- printf ("ACI_EVT_HW_ERROR \n");
- lib_aci_connect(0, 0x0050 );
- printf ("Advertising started \n");
- break;
-
- }
- }
-
-
-
- if(setup_required) {
- if (SETUP_SUCCESS == do_aci_setup(&aci_state)) {
- setup_required = false;
- }
- }
-
- usleep (100);
- }
-
- close_local_interfaces (&aci_state);
-
-
-
- std::cout << "exiting application" << std::endl;
-
- return 0;
- }
|