@@ -367,9 +367,23 @@ static void le_conn_timeout(struct work_struct *work)
367367{
368368 struct hci_conn * conn = container_of (work , struct hci_conn ,
369369 le_conn_timeout .work );
370+ struct hci_dev * hdev = conn -> hdev ;
370371
371372 BT_DBG ("" );
372373
374+ /* We could end up here due to having done directed advertising,
375+ * so clean up the state if necessary. This should however only
376+ * happen with broken hardware or if low duty cycle was used
377+ * (which doesn't have a timeout of its own).
378+ */
379+ if (test_bit (HCI_ADVERTISING , & hdev -> dev_flags )) {
380+ u8 enable = 0x00 ;
381+ hci_send_cmd (hdev , HCI_OP_LE_SET_ADV_ENABLE , sizeof (enable ),
382+ & enable );
383+ hci_le_conn_failed (conn , HCI_ERROR_ADVERTISING_TIMEOUT );
384+ return ;
385+ }
386+
373387 hci_le_create_connection_cancel (conn );
374388}
375389
@@ -549,6 +563,11 @@ void hci_le_conn_failed(struct hci_conn *conn, u8 status)
549563 * favor of connection establishment, we should restart it.
550564 */
551565 hci_update_background_scan (hdev );
566+
567+ /* Re-enable advertising in case this was a failed connection
568+ * attempt as a peripheral.
569+ */
570+ mgmt_reenable_advertising (hdev );
552571}
553572
554573static void create_le_conn_complete (struct hci_dev * hdev , u8 status )
@@ -609,6 +628,45 @@ static void hci_req_add_le_create_conn(struct hci_request *req,
609628 conn -> state = BT_CONNECT ;
610629}
611630
631+ static void hci_req_directed_advertising (struct hci_request * req ,
632+ struct hci_conn * conn )
633+ {
634+ struct hci_dev * hdev = req -> hdev ;
635+ struct hci_cp_le_set_adv_param cp ;
636+ u8 own_addr_type ;
637+ u8 enable ;
638+
639+ enable = 0x00 ;
640+ hci_req_add (req , HCI_OP_LE_SET_ADV_ENABLE , sizeof (enable ), & enable );
641+
642+ /* Clear the HCI_ADVERTISING bit temporarily so that the
643+ * hci_update_random_address knows that it's safe to go ahead
644+ * and write a new random address. The flag will be set back on
645+ * as soon as the SET_ADV_ENABLE HCI command completes.
646+ */
647+ clear_bit (HCI_ADVERTISING , & hdev -> dev_flags );
648+
649+ /* Set require_privacy to false so that the remote device has a
650+ * chance of identifying us.
651+ */
652+ if (hci_update_random_address (req , false, & own_addr_type ) < 0 )
653+ return ;
654+
655+ memset (& cp , 0 , sizeof (cp ));
656+ cp .type = LE_ADV_DIRECT_IND ;
657+ cp .own_address_type = own_addr_type ;
658+ cp .direct_addr_type = conn -> dst_type ;
659+ bacpy (& cp .direct_addr , & conn -> dst );
660+ cp .channel_map = hdev -> le_adv_channel_map ;
661+
662+ hci_req_add (req , HCI_OP_LE_SET_ADV_PARAM , sizeof (cp ), & cp );
663+
664+ enable = 0x01 ;
665+ hci_req_add (req , HCI_OP_LE_SET_ADV_ENABLE , sizeof (enable ), & enable );
666+
667+ conn -> state = BT_CONNECT ;
668+ }
669+
612670struct hci_conn * hci_connect_le (struct hci_dev * hdev , bdaddr_t * dst ,
613671 u8 dst_type , u8 sec_level , u8 auth_type )
614672{
@@ -618,9 +676,6 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
618676 struct hci_request req ;
619677 int err ;
620678
621- if (test_bit (HCI_ADVERTISING , & hdev -> flags ))
622- return ERR_PTR (- ENOTSUPP );
623-
624679 /* Some devices send ATT messages as soon as the physical link is
625680 * established. To be able to handle these ATT messages, the user-
626681 * space first establishes the connection and then starts the pairing
@@ -668,13 +723,20 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
668723 return ERR_PTR (- ENOMEM );
669724
670725 conn -> dst_type = dst_type ;
671-
672- conn -> out = true;
673- conn -> link_mode |= HCI_LM_MASTER ;
674726 conn -> sec_level = BT_SECURITY_LOW ;
675727 conn -> pending_sec_level = sec_level ;
676728 conn -> auth_type = auth_type ;
677729
730+ hci_req_init (& req , hdev );
731+
732+ if (test_bit (HCI_ADVERTISING , & hdev -> dev_flags )) {
733+ hci_req_directed_advertising (& req , conn );
734+ goto create_conn ;
735+ }
736+
737+ conn -> out = true;
738+ conn -> link_mode |= HCI_LM_MASTER ;
739+
678740 params = hci_conn_params_lookup (hdev , & conn -> dst , conn -> dst_type );
679741 if (params ) {
680742 conn -> le_conn_min_interval = params -> conn_min_interval ;
@@ -684,8 +746,6 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
684746 conn -> le_conn_max_interval = hdev -> le_conn_max_interval ;
685747 }
686748
687- hci_req_init (& req , hdev );
688-
689749 /* If controller is scanning, we stop it since some controllers are
690750 * not able to scan and connect at the same time. Also set the
691751 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
@@ -699,6 +759,7 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
699759
700760 hci_req_add_le_create_conn (& req , conn );
701761
762+ create_conn :
702763 err = hci_req_run (& req , create_le_conn_complete );
703764 if (err ) {
704765 hci_conn_del (conn );
0 commit comments