@@ -76,6 +76,8 @@ static struct bt_conn_auth_info_cb auth_info_cb;
7676 */
7777#define HCI_CMD_MAX_PARAM 65
7878
79+ #define DEFAULT_SCAN_TIMEOUT_SEC 10
80+
7981#if defined(CONFIG_BT_BROADCASTER )
8082enum {
8183 SHELL_ADV_OPT_CONNECTABLE ,
@@ -218,9 +220,31 @@ static struct bt_auto_connect {
218220 bool addr_set ;
219221 bool connect_name ;
220222} auto_connect ;
221- #endif
223+ #endif /* CONFIG_BT_CENTRAL */
222224
223225#if defined(CONFIG_BT_OBSERVER )
226+ static void active_scan_timeout (struct k_work * work )
227+ {
228+ int err ;
229+
230+ shell_print (ctx_shell , "Scan timeout" );
231+
232+ err = bt_le_scan_stop ();
233+ if (err ) {
234+ shell_error (ctx_shell , "Failed to stop scan (err %d)" , err );
235+ }
236+
237+ #if defined(CONFIG_BT_CENTRAL )
238+ if (auto_connect .connect_name ) {
239+ auto_connect .connect_name = false;
240+ /* "name" is what would be in argv[0] normally */
241+ cmd_scan_filter_clear_name (ctx_shell , 1 , (char * []){ "name" });
242+ }
243+ #endif /* CONFIG_BT_CENTRAL */
244+ }
245+
246+ static K_WORK_DELAYABLE_DEFINE (active_scan_timeout_work , active_scan_timeout ) ;
247+
224248static struct bt_scan_filter {
225249 char name [NAME_LEN ];
226250 bool name_set ;
@@ -591,14 +615,6 @@ static void scan_recv(const struct bt_le_scan_recv_info *info, struct net_buf_si
591615static void scan_timeout (void )
592616{
593617 shell_print (ctx_shell , "Scan timeout" );
594-
595- #if defined(CONFIG_BT_CENTRAL )
596- if (auto_connect .connect_name ) {
597- auto_connect .connect_name = false;
598- /* "name" is what would be in argv[0] normally */
599- cmd_scan_filter_clear_name (ctx_shell , 1 , (char * []){ "name" });
600- }
601- #endif /* CONFIG_BT_CENTRAL */
602618}
603619#endif /* CONFIG_BT_OBSERVER */
604620
@@ -1542,7 +1558,7 @@ static int cmd_active_scan_on(const struct shell *sh, uint32_t options,
15421558 .options = BT_LE_SCAN_OPT_NONE ,
15431559 .interval = BT_GAP_SCAN_FAST_INTERVAL ,
15441560 .window = BT_GAP_SCAN_FAST_WINDOW ,
1545- .timeout = timeout , };
1561+ .timeout = 0 , };
15461562
15471563 param .options |= options ;
15481564
@@ -1555,6 +1571,11 @@ static int cmd_active_scan_on(const struct shell *sh, uint32_t options,
15551571 shell_print (sh , "Bluetooth active scan enabled" );
15561572 }
15571573
1574+ if (timeout != 0 ) {
1575+ /* Schedule the k_work to act as a timeout */
1576+ (void )k_work_reschedule (& active_scan_timeout_work , K_SECONDS (timeout ));
1577+ }
1578+
15581579 return 0 ;
15591580}
15601581
@@ -1587,6 +1608,9 @@ static int cmd_scan_off(const struct shell *sh)
15871608{
15881609 int err ;
15891610
1611+ /* Cancel the potentially pending scan timeout work */
1612+ (void )k_work_cancel_delayable (& active_scan_timeout_work );
1613+
15901614 err = bt_le_scan_stop ();
15911615 if (err ) {
15921616 shell_error (sh , "Stopping scanning failed (err %d)" , err );
@@ -3267,13 +3291,12 @@ static int cmd_auto_conn(const struct shell *sh, size_t argc, char *argv[])
32673291
32683292static int cmd_connect_le_name (const struct shell * sh , size_t argc , char * argv [])
32693293{
3270- const uint16_t timeout_seconds = 10 ;
32713294 const struct bt_le_scan_param param = {
32723295 .type = BT_LE_SCAN_TYPE_ACTIVE ,
32733296 .options = BT_LE_SCAN_OPT_NONE ,
32743297 .interval = BT_GAP_SCAN_FAST_INTERVAL ,
32753298 .window = BT_GAP_SCAN_FAST_WINDOW ,
3276- .timeout = timeout_seconds * 100 , /* 10ms units */
3299+ .timeout = 0 ,
32773300 };
32783301 int err ;
32793302
@@ -3299,6 +3322,9 @@ static int cmd_connect_le_name(const struct shell *sh, size_t argc, char *argv[]
32993322 /* Set boolean to tell the scan callback to connect to this name */
33003323 auto_connect .connect_name = true;
33013324
3325+ /* Schedule the k_work to act as a timeout */
3326+ (void )k_work_reschedule (& active_scan_timeout_work , K_SECONDS (DEFAULT_SCAN_TIMEOUT_SEC ));
3327+
33023328 return 0 ;
33033329}
33043330#endif /* CONFIG_BT_CENTRAL */
0 commit comments