diff --git a/applications/nrf_desktop/src/modules/Kconfig.ble_scan b/applications/nrf_desktop/src/modules/Kconfig.ble_scan index cdf31a9b28cb..e164d664851b 100644 --- a/applications/nrf_desktop/src/modules/Kconfig.ble_scan +++ b/applications/nrf_desktop/src/modules/Kconfig.ble_scan @@ -45,6 +45,16 @@ config DESKTOP_BLE_SCAN_DURATION_S help After this time scan is disabled if any peripheral is connected. +config DESKTOP_BLE_FORCED_SCAN_DURATION_S + int "Forced scan duration [s]" + default 4 + help + Duration of the forced scan operation in seconds. The module enters a + forced scan state right after boot or wakeup, on successful peripheral + discovery, on peripheral disconnection and on connection failure. + + Setting forced scan duration to 0 disables the feature. + config DESKTOP_BLE_SCAN_MOUSE_LIMIT int "Maximum number of bonded mice" default 1 diff --git a/applications/nrf_desktop/src/modules/ble_scan.c b/applications/nrf_desktop/src/modules/ble_scan.c index a19e30555e16..d914dc7338d4 100644 --- a/applications/nrf_desktop/src/modules/ble_scan.c +++ b/applications/nrf_desktop/src/modules/ble_scan.c @@ -27,11 +27,13 @@ LOG_MODULE_REGISTER(MODULE, CONFIG_DESKTOP_BLE_SCAN_LOG_LEVEL); #define SCAN_START_CHECK_MS (1 * MSEC_PER_SEC) #define SCAN_START_TIMEOUT_MS (CONFIG_DESKTOP_BLE_SCAN_START_TIMEOUT_S * MSEC_PER_SEC) #define SCAN_DURATION_MS (CONFIG_DESKTOP_BLE_SCAN_DURATION_S * MSEC_PER_SEC) +#define FORCED_SCAN_DURATION_MS (CONFIG_DESKTOP_BLE_FORCED_SCAN_DURATION_S * MSEC_PER_SEC) #define SCAN_START_DELAY_MS 15 BUILD_ASSERT(SCAN_START_CHECK_MS > 0); BUILD_ASSERT(SCAN_START_TIMEOUT_MS > 0); BUILD_ASSERT(SCAN_DURATION_MS > 0, ""); +BUILD_ASSERT(FORCED_SCAN_DURATION_MS >= 0, ""); BUILD_ASSERT(SCAN_START_DELAY_MS > 0, ""); #define SUBSCRIBED_PEERS_STORAGE_NAME "subscribers" @@ -42,6 +44,7 @@ enum state { STATE_INITIALIZED, STATE_INITIALIZED_OFF, STATE_ACTIVE, + STATE_FORCED_ACTIVE, STATE_DELAYED_ACTIVE, STATE_INACTIVE, STATE_OFF, @@ -534,6 +537,9 @@ static const char *state2str(enum state s) case STATE_ACTIVE: return "ACTIVE"; + case STATE_FORCED_ACTIVE: + return "FORCED_ACTIVE"; + case STATE_DELAYED_ACTIVE: return "DELAYED_ACTIVE"; @@ -557,7 +563,12 @@ static enum state update_state_transition(enum state prev_state, enum state new_ size_t conn_count = count_conn(); size_t bond_count = count_bond(); - if (new_state == STATE_ACTIVE) { + /* Skip forced active state if disabled in configuration. */ + if ((new_state == STATE_FORCED_ACTIVE) && (FORCED_SCAN_DURATION_MS == 0)) { + new_state = STATE_ACTIVE; + } + + if ((new_state == STATE_ACTIVE) || (new_state == STATE_FORCED_ACTIVE)) { if (IS_ENABLED(CONFIG_DESKTOP_BLE_NEW_PEER_SCAN_REQUEST) && (conn_count == bond_count) && peers_only) { if (verbose) { @@ -626,6 +637,7 @@ static int update_scan_control(enum state prev_state, enum state new_state) switch (new_state) { case STATE_ACTIVE: + case STATE_FORCED_ACTIVE: err = scan_start(); break; @@ -656,6 +668,10 @@ static void update_work(enum state prev_state, enum state new_state) } break; + case STATE_FORCED_ACTIVE: + (void)k_work_reschedule(&update_state_work, K_MSEC(FORCED_SCAN_DURATION_MS)); + break; + case STATE_DELAYED_ACTIVE: (void)k_work_reschedule(&update_state_work, K_MSEC(SCAN_START_DELAY_MS)); break; @@ -723,10 +739,14 @@ static void update_state_work_fn(struct k_work *w) update_state(STATE_INACTIVE); break; - case STATE_DELAYED_ACTIVE: + case STATE_FORCED_ACTIVE: update_state(STATE_ACTIVE); break; + case STATE_DELAYED_ACTIVE: + update_state(STATE_FORCED_ACTIVE); + break; + case STATE_INACTIVE: scan_start_counter += SCAN_START_CHECK_MS; if (scan_start_counter >= SCAN_START_TIMEOUT_MS) { @@ -757,7 +777,7 @@ static bool handle_hid_report_event(const struct hid_report_event *event) * avoid negatively affecting HID report rate. */ scan_start_counter = 0; - } else if ((state == STATE_ACTIVE) || (state == STATE_DELAYED_ACTIVE)) { + } else if (state == STATE_ACTIVE) { update_state(STATE_INACTIVE); } @@ -790,7 +810,7 @@ static bool handle_module_state_event(const struct module_state_event *event) */ switch (state) { case STATE_INITIALIZED: - update_state(STATE_ACTIVE); + update_state(STATE_FORCED_ACTIVE); break; case STATE_INITIALIZED_OFF: @@ -857,7 +877,7 @@ static bool handle_ble_peer_operation_event(const struct ble_peer_operation_even } if (state != STATE_OFF) { - update_state(STATE_ACTIVE); + update_state(STATE_FORCED_ACTIVE); } break; @@ -909,6 +929,7 @@ static bool handle_power_down_event(const struct power_down_event *event) case STATE_ACTIVE: case STATE_DELAYED_ACTIVE: case STATE_INACTIVE: + case STATE_FORCED_ACTIVE: update_state(STATE_OFF); break; @@ -932,7 +953,7 @@ static bool handle_wake_up_event(const struct wake_up_event *event) break; case STATE_OFF: - update_state(STATE_ACTIVE); + update_state(STATE_FORCED_ACTIVE); break; default: