@@ -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
@@ -1602,7 +1618,7 @@ static int cmd_active_scan_on(const struct shell *sh, uint32_t options,
16021618 .options = BT_LE_SCAN_OPT_NONE ,
16031619 .interval = BT_GAP_SCAN_FAST_INTERVAL ,
16041620 .window = BT_GAP_SCAN_FAST_WINDOW ,
1605- .timeout = timeout , };
1621+ .timeout = 0 , };
16061622
16071623 param .options |= options ;
16081624
@@ -1615,6 +1631,11 @@ static int cmd_active_scan_on(const struct shell *sh, uint32_t options,
16151631 shell_print (sh , "Bluetooth active scan enabled" );
16161632 }
16171633
1634+ if (timeout != 0 ) {
1635+ /* Schedule the k_work to act as a timeout */
1636+ (void )k_work_reschedule (& active_scan_timeout_work , K_SECONDS (timeout ));
1637+ }
1638+
16181639 return 0 ;
16191640}
16201641
@@ -1647,6 +1668,9 @@ static int cmd_scan_off(const struct shell *sh)
16471668{
16481669 int err ;
16491670
1671+ /* Cancel the potentially pending scan timeout work */
1672+ (void )k_work_cancel_delayable (& active_scan_timeout_work );
1673+
16501674 err = bt_le_scan_stop ();
16511675 if (err ) {
16521676 shell_error (sh , "Stopping scanning failed (err %d)" , err );
@@ -3327,13 +3351,12 @@ static int cmd_auto_conn(const struct shell *sh, size_t argc, char *argv[])
33273351
33283352static int cmd_connect_le_name (const struct shell * sh , size_t argc , char * argv [])
33293353{
3330- const uint16_t timeout_seconds = 10 ;
33313354 const struct bt_le_scan_param param = {
33323355 .type = BT_LE_SCAN_TYPE_ACTIVE ,
33333356 .options = BT_LE_SCAN_OPT_NONE ,
33343357 .interval = BT_GAP_SCAN_FAST_INTERVAL ,
33353358 .window = BT_GAP_SCAN_FAST_WINDOW ,
3336- .timeout = timeout_seconds * 100 , /* 10ms units */
3359+ .timeout = 0 ,
33373360 };
33383361 int err ;
33393362
@@ -3359,6 +3382,9 @@ static int cmd_connect_le_name(const struct shell *sh, size_t argc, char *argv[]
33593382 /* Set boolean to tell the scan callback to connect to this name */
33603383 auto_connect .connect_name = true;
33613384
3385+ /* Schedule the k_work to act as a timeout */
3386+ (void )k_work_reschedule (& active_scan_timeout_work , K_SECONDS (DEFAULT_SCAN_TIMEOUT_SEC ));
3387+
33623388 return 0 ;
33633389}
33643390#endif /* CONFIG_BT_CENTRAL */
0 commit comments