/**@brief Nordic UART Service (NUS) Client Event Handler. */ static void uart_c_evt_handler(ble_uart_c_t * p_uart_c, ble_uart_c_evt_t * p_uart_c_evt) { uint32_t err_code; switch (p_uart_c_evt->evt_type) { case BLE_UART_C_EVT_DISCOVERY_COMPLETE: // Initiate bonding. err_code = dm_security_setup_req(&m_dm_device_handle); APP_ERROR_CHECK(err_code); // Nordic UART service discovered. Enable notification of RX data channel. err_code = ble_uart_c_rx_notif_enable(p_uart_c); APP_ERROR_CHECK(err_code); break; case BLE_UART_C_EVT_RX_DATA_NOTIFICATION: { for (uint32_t i = 0; i < p_uart_c_evt->params.uart.len; i++) { while(app_uart_put(p_uart_c_evt->params.uart.rx_data[i]) != NRF_SUCCESS); } break; } default: break; } }
/**@brief Heart Rate Collector Handler. */ static void hrs_c_evt_handler(ble_hrs_c_t * p_hrs_c, ble_hrs_c_evt_t * p_hrs_c_evt) { uint32_t err_code; switch (p_hrs_c_evt->evt_type) { case BLE_HRS_C_EVT_DISCOVERY_COMPLETE: // Initiate bonding. err_code = dm_security_setup_req(&m_dm_device_handle); APP_ERROR_CHECK(err_code); // Heart rate service discovered. Enable notification of Heart Rate Measurement. err_code = ble_hrs_c_hrm_notif_enable(p_hrs_c); APP_ERROR_CHECK(err_code); printf("Heart rate service discovered \r\n"); break; case BLE_HRS_C_EVT_HRM_NOTIFICATION: { APPL_LOG("[APPL]: HR Measurement received %d \r\n", p_hrs_c_evt->params.hrm.hr_value); printf("Heart Rate = %d\r\n", p_hrs_c_evt->params.hrm.hr_value); break; } default: break; } }
/**@brief Running Speed and Cadence Collector Handler. */ static void rscs_c_evt_handler(ble_rscs_c_t * p_rsc_c, ble_rscs_c_evt_t * p_rsc_c_evt) { uint32_t err_code; switch (p_rsc_c_evt->evt_type) { case BLE_RSCS_C_EVT_DISCOVERY_COMPLETE: // Initiate bonding. err_code = dm_security_setup_req(&m_dm_device_handle); APP_ERROR_CHECK(err_code); // Heart rate service discovered. Enable notification of Heart Rate Measurement. err_code = ble_rscs_c_rsc_notif_enable(p_rsc_c); APP_ERROR_CHECK(err_code); printf("Running Speed and Cadence service discovered \r\n"); if (m_peer_count < MAX_PEER_COUNT) { scan_start(); } break; case BLE_RSCS_C_EVT_RSC_NOTIFICATION: { printf("\r\n"); APPL_LOG("[APPL]: RSC Measurement received %d \r\n", p_rsc_c_evt->params.rsc.inst_speed); printf("Speed = %d\r\n", p_rsc_c_evt->params.rsc.inst_speed); ble_rscs_meas_t rscs_measurment; rscs_measurment.is_inst_stride_len_present = p_rsc_c_evt->params.rsc.is_inst_stride_len_present; rscs_measurment.is_total_distance_present = p_rsc_c_evt->params.rsc.is_total_distance_present; rscs_measurment.is_running = p_rsc_c_evt->params.rsc.is_running; rscs_measurment.inst_stride_length = p_rsc_c_evt->params.rsc.inst_stride_length; rscs_measurment.inst_cadence = p_rsc_c_evt->params.rsc.inst_cadence; rscs_measurment.inst_speed = p_rsc_c_evt->params.rsc.inst_speed; rscs_measurment.total_distance = p_rsc_c_evt->params.rsc.total_distance; err_code = ble_rscs_measurement_send(&m_rscs, &rscs_measurment); if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_NO_TX_BUFFERS) && (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING) ) { APP_ERROR_HANDLER(err_code); } break; } default: break; } }
/**@brief Running Speed and Cadence Collector Handler. */ static void rscs_c_evt_handler(ble_rscs_c_t * p_rsc_c, ble_rscs_c_evt_t * p_rsc_c_evt) { uint32_t err_code; switch (p_rsc_c_evt->evt_type) { case BLE_RSCS_C_EVT_DISCOVERY_COMPLETE: // Initiate bonding. err_code = dm_security_setup_req(&m_dm_device_handle); APP_ERROR_CHECK(err_code); // Running Speed and Cadence service discovered. Enable Running Speed and Cadence notifications. err_code = ble_rscs_c_rsc_notif_enable(p_rsc_c); APP_ERROR_CHECK(err_code); printf("Running Speed and Cadence service discovered \r\n"); break; case BLE_RSCS_C_EVT_RSC_NOTIFICATION: { printf("\r\n"); APPL_LOG("[APPL]: RSC Measurement received %d \r\n", p_rsc_c_evt->params.rsc.inst_speed); printf("Instantanious Speed = %d\r\n", p_rsc_c_evt->params.rsc.inst_speed); if (p_rsc_c_evt->params.rsc.is_inst_stride_len_present) { printf("Stride Length = %d\r\n", p_rsc_c_evt->params.rsc.inst_stride_length); } if (p_rsc_c_evt->params.rsc.is_total_distance_present) { printf("Total Distance = %u\r\n", (unsigned int)p_rsc_c_evt->params.rsc.total_distance); } printf("Instantanious Cadence = %d\r\n", p_rsc_c_evt->params.rsc.inst_cadence); printf("Flags\r\n"); printf(" Stride Length Present = %d\r\n", p_rsc_c_evt->params.rsc.is_inst_stride_len_present); printf(" Total Distance Present= %d\r\n", p_rsc_c_evt->params.rsc.is_total_distance_present); printf(" Is Running = %d\r\n", p_rsc_c_evt->params.rsc.is_running); break; } default: break; } }
static void on_ams_c_evt(ble_ams_c_evt_t * p_evt) { uint32_t err_code = NRF_SUCCESS; switch (p_evt->evt_type) { case BLE_AMS_C_EVT_DISCOVER_COMPLETE: err_code = dm_security_setup_req(&m_peer_handle); APP_ERROR_CHECK(err_code); break; default: //No implementation needed break; } }
/**@brief Function for handling the security request timer time-out. * * @details This function is called each time the security request timer expires. * * @param[in] p_context Pointer used for passing context information from the * app_start_timer() call to the time-out handler. */ static void sec_req_timeout_handler(void * p_context) { uint32_t err_code; dm_security_status_t status; if (m_conn_handle != BLE_CONN_HANDLE_INVALID) { err_code = dm_security_status_req(&m_peer_handle, &status); APP_ERROR_CHECK(err_code); // If the link is still not secured by the peer, initiate security procedure. if (status == NOT_ENCRYPTED) { err_code = dm_security_setup_req(&m_peer_handle); APP_ERROR_CHECK(err_code); } } }
/**@brief Function for handling the Security Request timer timeout. * * @details This function will be called each time the Security Request timer expires. * * @param[in] p_context Pointer used for passing some arbitrary information (context) from the * app_start_timer() call to the timeout handler. */ static void sec_req_timeout_handler(void * p_context) { uint32_t err_code; dm_security_status_t status; if (m_conn_handle != BLE_CONN_HANDLE_INVALID) { err_code = dm_security_status_req(&m_dm_handle, &status); APP_ERROR_CHECK(err_code); // In case the link is secured by the peer during timeout, the request is not sent. if (status == NOT_ENCRYPTED) { err_code = dm_security_setup_req(&m_dm_handle); APP_ERROR_CHECK(err_code); } } }
/**@brief Heart Rate Collector Handler. */ static void hrs_c_evt_handler(ble_hrs_c_t * p_hrs_c, ble_hrs_c_evt_t * p_hrs_c_evt) { uint32_t err_code; switch (p_hrs_c_evt->evt_type) { case BLE_HRS_C_EVT_DISCOVERY_COMPLETE: // Initiate bonding. err_code = dm_security_setup_req(&m_dm_device_handle); APP_ERROR_CHECK(err_code); // Heart rate service discovered. Enable notification of Heart Rate Measurement. err_code = ble_hrs_c_hrm_notif_enable(p_hrs_c); APP_ERROR_CHECK(err_code); printf("Heart rate service discovered \r\n"); if (m_peer_count < MAX_PEER_COUNT) { scan_start(); } break; case BLE_HRS_C_EVT_HRM_NOTIFICATION: { APPL_LOG("[APPL]: HR Measurement received %d \r\n", p_hrs_c_evt->params.hrm.hr_value); printf("Heart Rate = %d\r\n", p_hrs_c_evt->params.hrm.hr_value); err_code = ble_hrs_heart_rate_measurement_send(&m_hrs, p_hrs_c_evt->params.hrm.hr_value); if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_NO_TX_BUFFERS) && (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING) ) { APP_ERROR_HANDLER(err_code); } break; } default: break; } }
void client_handling_ble_evt_handler(ble_evt_t * p_ble_evt) { client_t * p_client = NULL; uint32_t index = client_find(p_ble_evt->evt.gattc_evt.conn_handle); if (index != MAX_CLIENTS) { p_client = &m_client[index]; } switch (p_ble_evt->header.evt_id) { case BLE_GATTC_EVT_WRITE_RSP: if ((p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_AUTHENTICATION) || (p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION)) { uint32_t err_code = dm_security_setup_req(&p_client->handle); APP_ERROR_CHECK(err_code); } on_evt_write_rsp(p_ble_evt, p_client); break; case BLE_GATTC_EVT_HVX: on_evt_hvx(p_ble_evt, p_client, index); break; case BLE_GATTC_EVT_TIMEOUT: on_evt_timeout(p_ble_evt, p_client); break; default: break; } if (p_client != NULL) { ble_db_discovery_on_ble_evt(&(p_client->srv_db), p_ble_evt); } }
/**@brief Function for handling the Apple Notification Service Client. * * @details This function will be called for all events in the Apple Notification Client which * are passed to the application. * * @param[in] p_evt Event received from the Apple Notification Service Client. */ static void on_ancs_c_evt(ble_ancs_c_evt_t * p_evt) { uint32_t err_code = NRF_SUCCESS; switch (p_evt->evt_type) { case BLE_ANCS_C_EVT_DISCOVER_COMPLETE: err_code = dm_security_setup_req(&m_peer_handle); APP_ERROR_CHECK(err_code); break; case BLE_ANCS_C_EVT_IOS_NOTIFICATION: evt_ios_notification(&p_evt->data.notification); break; case BLE_ANCS_C_EVT_NOTIF_ATTRIBUTE: evt_notif_attribute(&p_evt->data.attribute); break; default: //No implementation needed break; } }
/**@brief Callback handling device manager events. * * @details This function is called to notify the application of device manager events. * * @param[in] p_handle Device Manager Handle. For link related events, this parameter * identifies the peer. * @param[in] p_event Pointer to the device manager event. * @param[in] event_status Status of the event. */ static ret_code_t device_manager_event_handler(const dm_handle_t * p_handle, const dm_event_t * p_event, const ret_code_t event_result) { uint32_t err_code; switch (p_event->event_id) { case DM_EVT_CONNECTION: { APPL_LOG("[APPL]: >> DM_EVT_CONNECTION\r\n"); #ifdef ENABLE_DEBUG_LOG_SUPPORT ble_gap_addr_t * peer_addr; peer_addr = &p_event->event_param.p_gap_param->params.connected.peer_addr; #endif // ENABLE_DEBUG_LOG_SUPPORT APPL_LOG("[APPL]:[%02X %02X %02X %02X %02X %02X]: Connection Established\r\n", peer_addr->addr[0], peer_addr->addr[1], peer_addr->addr[2], peer_addr->addr[3], peer_addr->addr[4], peer_addr->addr[5]); err_code = bsp_indication_set(BSP_INDICATE_CONNECTED); APP_ERROR_CHECK(err_code); m_conn_handle = p_event->event_param.p_gap_param->conn_handle; m_dm_device_handle = (*p_handle); // Discover peer's services. err_code = ble_db_discovery_start(&m_ble_db_discovery, p_event->event_param.p_gap_param->conn_handle); APP_ERROR_CHECK(err_code); m_peer_count++; if (m_peer_count < MAX_PEER_COUNT) { scan_start(); } APPL_LOG("[APPL]: << DM_EVT_CONNECTION\r\n"); break; } case DM_EVT_DISCONNECTION: { APPL_LOG("[APPL]: >> DM_EVT_DISCONNECTION\r\n"); memset(&m_ble_db_discovery, 0 , sizeof (m_ble_db_discovery)); err_code = bsp_indication_set(BSP_INDICATE_IDLE); APP_ERROR_CHECK(err_code); if (m_peer_count == MAX_PEER_COUNT) { scan_start(); } m_peer_count--; APPL_LOG("[APPL]: << DM_EVT_DISCONNECTION\r\n"); break; } case DM_EVT_SECURITY_SETUP: { APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_SECURITY_SETUP\r\n", p_handle->connection_id); // Slave securtiy request received from peer, if from a non bonded device, // initiate security setup, else, wait for encryption to complete. err_code = dm_security_setup_req(&m_dm_device_handle); APP_ERROR_CHECK(err_code); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_SECURITY_SETUP\r\n", p_handle->connection_id); break; } case DM_EVT_SECURITY_SETUP_COMPLETE: { APPL_LOG("[APPL]: >> DM_EVT_SECURITY_SETUP_COMPLETE\r\n"); // Heart rate service discovered. Enable notification of Heart Rate Measurement. err_code = ble_hrs_c_hrm_notif_enable(&m_ble_hrs_c); APP_ERROR_CHECK(err_code); APPL_LOG("[APPL]: << DM_EVT_SECURITY_SETUP_COMPLETE\r\n"); break; } case DM_EVT_LINK_SECURED: APPL_LOG("[APPL]: >> DM_LINK_SECURED_IND\r\n"); APPL_LOG("[APPL]: << DM_LINK_SECURED_IND\r\n"); break; case DM_EVT_DEVICE_CONTEXT_LOADED: APPL_LOG("[APPL]: >> DM_EVT_LINK_SECURED\r\n"); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]: << DM_EVT_DEVICE_CONTEXT_LOADED\r\n"); break; case DM_EVT_DEVICE_CONTEXT_STORED: APPL_LOG("[APPL]: >> DM_EVT_DEVICE_CONTEXT_STORED\r\n"); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]: << DM_EVT_DEVICE_CONTEXT_STORED\r\n"); break; case DM_EVT_DEVICE_CONTEXT_DELETED: APPL_LOG("[APPL]: >> DM_EVT_DEVICE_CONTEXT_DELETED\r\n"); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]: << DM_EVT_DEVICE_CONTEXT_DELETED\r\n"); break; default: break; } return NRF_SUCCESS; }
/**@brief Callback handling device manager events. * * @details This function is called to notify the application of device manager events. * * @param[in] p_handle Device Manager Handle. For link related events, this parameter * identifies the peer. * @param[in] p_event Pointer to the device manager event. * @param[in] event_status Status of the event. */ static api_result_t device_manager_event_handler(const dm_handle_t * p_handle, const dm_event_t * p_event, const api_result_t event_result) { uint32_t err_code; switch(p_event->event_id) { case DM_EVT_CONNECTION: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_CONNECTION\r\n", p_handle->connection_id); #ifdef ENABLE_DEBUG_LOG_SUPPORT ble_gap_addr_t * p_peer_addr; p_peer_addr = &p_event->event_param.p_gap_param->params.connected.peer_addr; #endif // ENABLE_DEBUG_LOG_SUPPORT APPL_LOG("[APPL]:[%02X %02X %02X %02X %02X %02X]: Connection Established\r\n", p_peer_addr->addr[0], p_peer_addr->addr[1], p_peer_addr->addr[2], p_peer_addr->addr[3], p_peer_addr->addr[4], p_peer_addr->addr[5]); APPL_LOG("\r\n"); APPL_LOG("[APPL]:[CI 0x%02X]: Requesting GATT client create\r\n", p_handle->connection_id); err_code = client_handling_create(p_handle, p_event->event_param.p_gap_param->conn_handle); APP_ERROR_CHECK(err_code); m_peer_count++; if (m_peer_count < MAX_PEER_COUNT) { scan_start(); } APPL_LOG("[APPL]:[0x%02X] << DM_EVT_CONNECTION\r\n", p_handle->connection_id); break; case DM_EVT_DISCONNECTION: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_DISCONNECTION\r\n", p_handle->connection_id); err_code = client_handling_destroy(p_handle); APP_ERROR_CHECK(err_code); if (m_peer_count == MAX_PEER_COUNT) { scan_start(); } m_peer_count--; APPL_LOG("[APPL]:[0x%02X] << DM_EVT_DISCONNECTION\r\n", p_handle->connection_id); break; case DM_EVT_SECURITY_SETUP: { dm_handle_t handle = (*p_handle); APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_SECURITY_SETUP\r\n", p_handle->connection_id); // Slave securtiy request received from peer, if from a non bonded device, // initiate security setup, else, wait for encryption to complete. err_code = dm_security_setup_req(&handle); APP_ERROR_CHECK(err_code); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_SECURITY_SETUP\r\n", p_handle->connection_id); break; } case DM_EVT_SECURITY_SETUP_COMPLETE: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_SECURITY_SETUP_COMPLETE, result 0x%08X\r\n", p_handle->connection_id, event_result); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_SECURITY_SETUP_COMPLETE\r\n", p_handle->connection_id); break; case DM_EVT_LINK_SECURED: APPL_LOG("[APPL]:[0x%02X] >> DM_LINK_SECURED_IND, result 0x%08X\r\n", p_handle->connection_id, event_result); APPL_LOG("[APPL]:[0x%02X] << DM_LINK_SECURED_IND\r\n", p_handle->connection_id); break; case DM_EVT_DEVICE_CONTEXT_LOADED: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_LINK_SECURED\r\n", p_handle->connection_id); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_DEVICE_CONTEXT_LOADED\r\n", p_handle->connection_id); break; case DM_EVT_DEVICE_CONTEXT_STORED: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_DEVICE_CONTEXT_STORED\r\n", p_handle->connection_id); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_DEVICE_CONTEXT_STORED\r\n", p_handle->connection_id); break; case DM_EVT_DEVICE_CONTEXT_DELETED: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_DEVICE_CONTEXT_DELETED\r\n", p_handle->connection_id); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_DEVICE_CONTEXT_DELETED\r\n", p_handle->connection_id); break; default: break; } // Relay the event to client handling module. err_code = client_handling_dm_event_handler(p_handle, p_event, event_result); APP_ERROR_CHECK(err_code); return NRF_SUCCESS; }
/**@brief Callback handling device manager events. * * @details This function is called to notify the application of device manager events. * * @param[in] p_handle Device Manager Handle. For link related events, this parameter * identifies the peer. * @param[in] p_event Pointer to the device manager event. * @param[in] event_status Status of the event. */ static ret_code_t device_manager_event_handler(const dm_handle_t * p_handle, const dm_event_t * p_event, const ret_code_t event_result) { uint32_t err_code; switch(p_event->event_id) { case DM_EVT_CONNECTION: { nrf_gpio_pin_set(CONNECTED_LED_PIN_NO); printf("Connected \r\n"); m_dm_device_handle = (*p_handle); // Discover peer's services. err_code = ble_db_discovery_start(&m_ble_db_discovery, p_event->event_param.p_gap_param->conn_handle); APP_ERROR_CHECK(err_code); m_peer_count++; if (m_peer_count < MAX_PEER_COUNT) { scan_start(); } break; } case DM_EVT_DISCONNECTION: { memset(&m_ble_db_discovery, 0 , sizeof (m_ble_db_discovery)); nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO); if (m_peer_count == MAX_PEER_COUNT) { scan_start(); } m_peer_count--; break; } case DM_EVT_SECURITY_SETUP: { // Slave securtiy request received from peer, if from a non bonded device, // initiate security setup, else, wait for encryption to complete. err_code = dm_security_setup_req(&m_dm_device_handle); APP_ERROR_CHECK(err_code); break; } case DM_EVT_SECURITY_SETUP_COMPLETE: { // Nordic UART service discovered. Enable notification of RX channel. err_code = ble_uart_c_rx_notif_enable(&m_ble_uart_c); APP_ERROR_CHECK(err_code); break; } case DM_EVT_LINK_SECURED: break; case DM_EVT_DEVICE_CONTEXT_LOADED: APP_ERROR_CHECK(event_result); break; case DM_EVT_DEVICE_CONTEXT_STORED: APP_ERROR_CHECK(event_result); break; case DM_EVT_DEVICE_CONTEXT_DELETED: APP_ERROR_CHECK(event_result); break; default: break; } return NRF_SUCCESS; }