/**@brief Function for extracting the BSP event valid at startup. * * @details When a button was used to wake up the device, the button press will not generate an * interrupt. This function reads which button was pressed at startup, and returns the * appropriate BSP event. * * @param[out] p_startup_event Where to put the extracted BSP event. * * @retval NRF_SUCCESS Extraction was successful. * @return A propagated error code. */ static uint32_t startup_event_extract(bsp_event_t * p_startup_event) { uint32_t err_code; bool wakeup_button_is_pressed, bond_erase_button_is_pressed; // Read buttons err_code = bsp_button_is_pressed(BTN_ID_WAKEUP, &wakeup_button_is_pressed); RETURN_ON_ERROR(err_code); err_code = bsp_button_is_pressed(BTN_ID_WAKEUP_BOND_DELETE, &bond_erase_button_is_pressed); RETURN_ON_ERROR(err_code); // React to button states if (bond_erase_button_is_pressed) { *p_startup_event = BSP_EVENT_CLEAR_BONDING_DATA; } else if (wakeup_button_is_pressed) { *p_startup_event = BSP_EVENT_WAKEUP; } else { *p_startup_event = BSP_EVENT_NOTHING; } return NRF_SUCCESS; }
/**@brief Function for the Device Manager initialization. */ static void device_manager_init(void) { uint32_t err_code; dm_init_param_t init_data; dm_application_param_t register_param; // Initialize persistent storage module. err_code = pstorage_init(); APP_ERROR_CHECK(err_code); // Clear all bonded centrals if the "delete all bonds" button is pushed. err_code = bsp_button_is_pressed(BOND_DELETE_ALL_WAKEUP_BUTTON_ID,&(init_data.clear_persistent_data)); APP_ERROR_CHECK(err_code); err_code = dm_init(&init_data); APP_ERROR_CHECK(err_code); memset(®ister_param.sec_param, 0, sizeof(ble_gap_sec_params_t)); register_param.sec_param.timeout = SEC_PARAM_TIMEOUT; register_param.sec_param.bond = SEC_PARAM_BOND; register_param.sec_param.mitm = SEC_PARAM_MITM; register_param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES; register_param.sec_param.oob = SEC_PARAM_OOB; register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE; register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE; register_param.evt_handler = device_manager_evt_handler; register_param.service_type = DM_PROTOCOL_CNTXT_GATT_SRVR_ID; err_code = dm_register(&m_app_handle, ®ister_param); APP_ERROR_CHECK(err_code); }
/**@brief Encode current state of buttons * * Configure bitfield encoding the state of the buttons * Bit 0 = 0 if button 0 is pressed * Bit 1 = 0 if button 1 is pressed * Bit 2 = 0 if button 2 is pressed * Bit 3 = 0 if button 3 is pressed * Bit 4-7 = 1 (unused) */ static void encode_button_state(void) { uint32_t err_code; bool button0; bool button1; bool button2; bool button3; err_code = bsp_button_is_pressed(0, &button0); APP_ERROR_CHECK(err_code); err_code = bsp_button_is_pressed(1, &button1); APP_ERROR_CHECK(err_code); err_code = bsp_button_is_pressed(2, &button2); APP_ERROR_CHECK(err_code); err_code = bsp_button_is_pressed(3, &button3); APP_ERROR_CHECK(err_code); m_tx_input_pin_state = 0xFF; if (button0) { m_tx_input_pin_state &= 0xFE; } if (button1) { m_tx_input_pin_state &= 0xFD; } if (button2) { m_tx_input_pin_state &= 0xFB; } if (button3) { m_tx_input_pin_state &= 0xF7; } }
static void service_add(void) { ble_ancs_c_init_t ancs_init_obj; ble_uuid_t service_uuid; uint32_t err_code; bool services_delete; err_code = sd_ble_uuid_vs_add(&ble_ancs_base_uuid128, &m_ancs_uuid_type); APP_ERROR_CHECK(err_code); err_code = sd_ble_uuid_vs_add(&ble_ancs_cp_base_uuid128, &service_uuid.type); APP_ERROR_CHECK(err_code); err_code = sd_ble_uuid_vs_add(&ble_ancs_ns_base_uuid128, &service_uuid.type); APP_ERROR_CHECK(err_code); err_code = sd_ble_uuid_vs_add(&ble_ancs_ds_base_uuid128, &service_uuid.type); APP_ERROR_CHECK(err_code); memset(&ancs_init_obj, 0, sizeof(ancs_init_obj)); memset(m_apple_message_buffer, 0, MESSAGE_BUFFER_SIZE); ancs_init_obj.evt_handler = on_ancs_c_evt; ancs_init_obj.message_buffer_size = MESSAGE_BUFFER_SIZE; ancs_init_obj.p_message_buffer = m_apple_message_buffer; ancs_init_obj.error_handler = apple_notification_error_handler; err_code = ble_ancs_c_init(&m_ancs_c, &ancs_init_obj); APP_ERROR_CHECK(err_code); // Clear all discovered and stored services if the "delete all bonds" button is pushed. err_code = bsp_button_is_pressed(BOND_DELETE_ALL_WAKEUP_BUTTON_ID,&(services_delete)); APP_ERROR_CHECK(err_code); if (services_delete) { err_code = ble_ans_c_service_delete(); APP_ERROR_CHECK(err_code); } err_code = ble_ans_c_service_load(&m_ancs_c); APP_ERROR_CHECK(err_code); }
/**@brief Function for initializing the Device Manager. * * @details Device manager is initialized here. */ static void device_manager_init(void) { dm_application_param_t param; dm_init_param_t init_param; uint32_t err_code; err_code = pstorage_init(); APP_ERROR_CHECK(err_code); // Clear all bonded devices if user requests to. err_code = bsp_button_is_pressed(BOND_DELETE_ALL_BUTTON_ID,&(init_param.clear_persistent_data)); APP_ERROR_CHECK(err_code); err_code = dm_init(&init_param); APP_ERROR_CHECK(err_code); memset(¶m.sec_param, 0, sizeof (ble_gap_sec_params_t)); // Event handler to be registered with the module. param.evt_handler = device_manager_event_handler; // Service or protocol context for device manager to load, store and apply on behalf of application. // Here set to client as application is a GATT client. param.service_type = DM_PROTOCOL_CNTXT_GATT_CLI_ID; // Secuirty parameters to be used for security procedures. param.sec_param.bond = SEC_PARAM_BOND; param.sec_param.mitm = SEC_PARAM_MITM; param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES; param.sec_param.oob = SEC_PARAM_OOB; param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE; param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE; param.sec_param.kdist_periph.enc = 1; param.sec_param.kdist_periph.id = 1; err_code = dm_register(&m_dm_app_id, ¶m); APP_ERROR_CHECK(err_code); }
/**@brief Function for application main entry. */ int main(void) { uint32_t err_code; bool is_notification_mode = false; bool is_non_connectable_mode = false; timers_init(); APP_GPIOTE_INIT(1); err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), NULL); APP_ERROR_CHECK(err_code); #if BUTTONS_NUMBER > 2 // Check button states. // Notification Start button. err_code = bsp_button_is_pressed(NOTIF_BUTTON_ID, &(is_notification_mode)); APP_ERROR_CHECK(err_code); // Non-connectable advertisement start button. if (!is_notification_mode) { err_code = bsp_button_is_pressed(NON_CONN_ADV_BUTTON_ID, &(is_non_connectable_mode)); APP_ERROR_CHECK(err_code); } // Un-configured button. else { } #else is_notification_mode = true; #endif // Initialize SoftDevice. ble_stack_init(); if (!is_notification_mode && !is_non_connectable_mode) { // The startup was not because of button presses. This is the first start. // Go into System-Off mode. Button presses will wake the chip up. err_code = sd_power_system_off(); APP_ERROR_CHECK(err_code); } // If we reach this point, the application was woken up by pressing one of the two configured // buttons. gap_params_init(); if (is_notification_mode) { // Notification button is pressed. Start connectable advertisement. connectable_adv_init(); service_add(); } else { non_connectable_adv_init(); } advertising_data_init(); advertising_start(); // Enter main loop. for (;;) { power_manage(); } }
static void read_keyboard_and_send(void) { uint8_t keyboard_packet[NRFR_KEYBOARD_PACKET_LENGTH]; // "Scan" keyboard. keyboard_get_non_empty_packet(keyboard_packet); // Send address request if required. if(!host_id_received && !system_addr_received) { system_addr_received = gzp_address_req_send(); } /* Send Host ID request if required. This may take a few attempts * as the Host may require some time to generate the Host ID. */ if(!host_id_received && system_addr_received ) { while(send_host_id_req() == GZP_ID_RESP_PENDING); } /* After receiving the Host ID we send one packet in order * to update the dynamic key. */ if(host_id_received && !dyn_key_ok) { bool keyboard_send_ok = true; keyboard_send_ok = gzp_crypt_data_send(keyboard_packet, NRFR_KEYBOARD_PACKET_LENGTH); if(!keyboard_send_ok) { host_id_received = false; } else { dyn_key_ok = true; } } /* If we have the Host ID and dynamic key we can transmit encrypted data. */ if(host_id_received && dyn_key_ok) { bool keyboard_send_ok = true; keyboard_send_ok = gzp_crypt_data_send(keyboard_packet, NRFR_KEYBOARD_PACKET_LENGTH); if (keyboard_send_ok) { printf("Keyboard sent OK\n"); } else { printf("Keyboard sent FAILED\n"); } if(keyboard_send_ok) { // Wait until button is depressed. bool button_pressed; do{ UNUSED_VARIABLE(bsp_button_is_pressed(SEND_KEYBOARD_DATA_BUTTON_ID,&(button_pressed))); }while(button_pressed); // Send empty keyboard packet to release all keys. keyboard_get_empty_packet(keyboard_packet); keyboard_send_ok = gzp_crypt_data_send(keyboard_packet, NRFR_KEYBOARD_PACKET_LENGTH); } if(!keyboard_send_ok) { dyn_key_ok = false; } } }
int main() { bool init_ok = false; uint32_t err_code; //lint -save -e514 Unusual use of a boolean expression (use of &= assignment). const app_uart_comm_params_t comm_params = { RX_PIN_NUMBER, TX_PIN_NUMBER, RTS_PIN_NUMBER, CTS_PIN_NUMBER, APP_UART_FLOW_CONTROL_ENABLED, false, UART_BAUDRATE_BAUDRATE_Baud38400 }; APP_UART_FIFO_INIT(&comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE, uart_error_handle, APP_IRQ_PRIORITY_LOW, err_code); (void)err_code; UNUSED_VARIABLE(bsp_init(BSP_INIT_BUTTONS,0,NULL)); printf("Desktop emulator example\n"); // Initialize and enable "mouse sensor" init_ok = mouse_sensor_init(MOUSE_SENSOR_SAMPLE_PERIOD_8_MS); mouse_sensor_enable(); // Initialize and enable Gazell init_ok &= nrf_gzll_init(NRF_GZLL_MODE_DEVICE); // Ensure Gazell parameters are configured. init_ok &= nrf_gzll_set_max_tx_attempts(150); init_ok &= nrf_gzll_set_device_channel_selection_policy(NRF_GZLLDE_DEVICE_CHANNEL_SELECTION_POLICY); init_ok &= nrf_gzll_set_timeslot_period(NRF_GZLLDE_RXPERIOD_DIV_2); init_ok &= nrf_gzll_set_sync_lifetime(0); // Asynchronous mode, more efficient for pairing. switch(gzp_get_pairing_status()) { case -2: host_id_received = false; system_addr_received = false; break; case -1: host_id_received = false; system_addr_received = true; break; default: host_id_received = true; system_addr_received = true; } gzp_init(); init_ok &= nrf_gzll_enable(); if(init_ok) { while(1) { // If BUTTON_SEND_MOUSE_DATA button is pressed. bool send_mouse_data_button_pressed; err_code = bsp_button_is_pressed(SEND_MOUSE_DATA_BUTTON_ID,&(send_mouse_data_button_pressed)); if( send_mouse_data_button_pressed) { read_mouse_and_send(); } else { //indicate mouse button not pressed } // If BUTTON_SEND_KEYBOARD_DATA button is pressed bool send_keyboard_data_button_pressed; err_code = bsp_button_is_pressed(SEND_KEYBOARD_DATA_BUTTON_ID,&(send_keyboard_data_button_pressed)); if(send_keyboard_data_button_pressed) { read_keyboard_and_send(); } else { //indicate keyboard button not pressed } error_report(); /* CPU sleep. We will wake up from all enabled interrupts, which here are the internal Gazell interrupts and the "mouse sensor" internal timer interrupt. */ //__WFI(); } } else { /* The initialization failed. Use nrf_gzll_get_error_code() to investigate the cause. */ } //lint -restore }