• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cstdint>
18 
19 #include "osi/include/log.h"
20 #include "stack/btm/btm_ble_int.h"
21 #include "stack/btm/btm_dev.h"
22 #include "stack/btm/btm_sec.h"
23 #include "stack/gatt/connection_manager.h"
24 #include "stack/include/acl_api.h"
25 #include "stack/include/bt_types.h"
26 #include "stack/include/l2cap_hci_link_interface.h"
27 #include "types/raw_address.h"
28 
29 extern tBTM_CB btm_cb;
30 
31 void btm_ble_advertiser_notify_terminated_legacy(uint8_t status,
32                                                  uint16_t connection_handle);
33 void btm_ble_increment_link_topology_mask(uint8_t link_role);
34 
35 bool maybe_resolve_address(RawAddress* bda, tBLE_ADDR_TYPE* bda_type);
36 
acl_ble_common_connection(const tBLE_BD_ADDR & address_with_type,uint16_t handle,tHCI_ROLE role,bool is_in_security_db,uint16_t conn_interval,uint16_t conn_latency,uint16_t conn_timeout)37 static bool acl_ble_common_connection(const tBLE_BD_ADDR& address_with_type,
38                                       uint16_t handle, tHCI_ROLE role,
39                                       bool is_in_security_db,
40                                       uint16_t conn_interval,
41                                       uint16_t conn_latency,
42                                       uint16_t conn_timeout) {
43   if (role == HCI_ROLE_CENTRAL) {
44     btm_cb.ble_ctr_cb.set_connection_state_idle();
45     btm_ble_clear_topology_mask(BTM_BLE_STATE_INIT_BIT);
46   }
47 
48   // Inform any applications that a connection has completed.
49   connection_manager::on_connection_complete(address_with_type.bda);
50 
51   // Allocate or update the security device record for this device
52   btm_ble_connected(address_with_type.bda, handle, HCI_ENCRYPT_MODE_DISABLED,
53                     role, address_with_type.type, is_in_security_db);
54 
55   // Update the link topology information for our device
56   btm_ble_increment_link_topology_mask(role);
57 
58   // Inform l2cap of a potential connection.
59   if (!l2cble_conn_comp(handle, role, address_with_type.bda,
60                         address_with_type.type, conn_interval, conn_latency,
61                         conn_timeout)) {
62     btm_sec_disconnect(handle, HCI_ERR_NO_CONNECTION,
63                        "stack::acl::ble_acl fail");
64     LOG_WARN("Unable to complete l2cap connection");
65     return false;
66   }
67 
68   /* Tell BTM Acl management about the link */
69   btm_acl_created(address_with_type.bda, handle, role, BT_TRANSPORT_LE);
70 
71   return true;
72 }
73 
acl_ble_connection_complete(const tBLE_BD_ADDR & address_with_type,uint16_t handle,tHCI_ROLE role,bool match,uint16_t conn_interval,uint16_t conn_latency,uint16_t conn_timeout)74 void acl_ble_connection_complete(const tBLE_BD_ADDR& address_with_type,
75                                  uint16_t handle, tHCI_ROLE role, bool match,
76                                  uint16_t conn_interval, uint16_t conn_latency,
77                                  uint16_t conn_timeout) {
78   if (!acl_ble_common_connection(address_with_type, handle, role, match,
79                                  conn_interval, conn_latency, conn_timeout)) {
80     LOG_WARN("Unable to create non enhanced ble acl connection");
81     return;
82   }
83 
84   btm_ble_update_mode_operation(role, &address_with_type.bda, HCI_SUCCESS);
85 
86   if (role == HCI_ROLE_PERIPHERAL)
87     btm_ble_advertiser_notify_terminated_legacy(HCI_SUCCESS, handle);
88 }
89 
acl_ble_enhanced_connection_complete(const tBLE_BD_ADDR & address_with_type,uint16_t handle,tHCI_ROLE role,bool match,uint16_t conn_interval,uint16_t conn_latency,uint16_t conn_timeout,const RawAddress & local_rpa,const RawAddress & peer_rpa,tBLE_ADDR_TYPE peer_addr_type)90 void acl_ble_enhanced_connection_complete(
91     const tBLE_BD_ADDR& address_with_type, uint16_t handle, tHCI_ROLE role,
92     bool match, uint16_t conn_interval, uint16_t conn_latency,
93     uint16_t conn_timeout, const RawAddress& local_rpa,
94     const RawAddress& peer_rpa, tBLE_ADDR_TYPE peer_addr_type) {
95   if (!acl_ble_common_connection(address_with_type, handle, role, match,
96                                  conn_interval, conn_latency, conn_timeout)) {
97     LOG_WARN("Unable to create enhanced ble acl connection");
98     return;
99   }
100 
101   btm_ble_refresh_local_resolvable_private_addr(address_with_type.bda,
102                                                 local_rpa);
103 
104   if (peer_addr_type & BLE_ADDR_TYPE_ID_BIT)
105     btm_ble_refresh_peer_resolvable_private_addr(
106         address_with_type.bda, peer_rpa, tBTM_SEC_BLE::BTM_BLE_ADDR_RRA);
107   btm_ble_update_mode_operation(role, &address_with_type.bda, HCI_SUCCESS);
108 
109   if (role == HCI_ROLE_PERIPHERAL)
110     btm_ble_advertiser_notify_terminated_legacy(HCI_SUCCESS, handle);
111 }
112 
maybe_resolve_received_address(const tBLE_BD_ADDR & address_with_type,tBLE_BD_ADDR * resolved_address_with_type)113 static bool maybe_resolve_received_address(
114     const tBLE_BD_ADDR& address_with_type,
115     tBLE_BD_ADDR* resolved_address_with_type) {
116   ASSERT(resolved_address_with_type != nullptr);
117 
118   *resolved_address_with_type = address_with_type;
119   return maybe_resolve_address(&resolved_address_with_type->bda,
120                                &resolved_address_with_type->type);
121 }
122 
acl_ble_enhanced_connection_complete_from_shim(const tBLE_BD_ADDR & address_with_type,uint16_t handle,tHCI_ROLE role,uint16_t conn_interval,uint16_t conn_latency,uint16_t conn_timeout,const RawAddress & local_rpa,const RawAddress & peer_rpa,tBLE_ADDR_TYPE peer_addr_type)123 void acl_ble_enhanced_connection_complete_from_shim(
124     const tBLE_BD_ADDR& address_with_type, uint16_t handle, tHCI_ROLE role,
125     uint16_t conn_interval, uint16_t conn_latency, uint16_t conn_timeout,
126     const RawAddress& local_rpa, const RawAddress& peer_rpa,
127     tBLE_ADDR_TYPE peer_addr_type) {
128   connection_manager::on_connection_complete(address_with_type.bda);
129 
130   tBLE_BD_ADDR resolved_address_with_type;
131   const bool is_in_security_db = maybe_resolve_received_address(
132       address_with_type, &resolved_address_with_type);
133 
134   acl_ble_enhanced_connection_complete(resolved_address_with_type, handle, role,
135                                        is_in_security_db, conn_interval,
136                                        conn_latency, conn_timeout, local_rpa,
137                                        peer_rpa, peer_addr_type);
138 
139   // The legacy stack continues the LE connection after the read remote version
140   // complete has been received.
141   // maybe_chain_more_commands_after_read_remote_version_complete
142 }
143 
acl_ble_connection_fail(const tBLE_BD_ADDR & address_with_type,uint16_t handle,bool enhanced,tHCI_STATUS status)144 void acl_ble_connection_fail(const tBLE_BD_ADDR& address_with_type,
145                              uint16_t handle, bool enhanced,
146                              tHCI_STATUS status) {
147   if (status != HCI_ERR_ADVERTISING_TIMEOUT) {
148     btm_cb.ble_ctr_cb.set_connection_state_idle();
149     btm_ble_clear_topology_mask(BTM_BLE_STATE_INIT_BIT);
150     tBLE_BD_ADDR resolved_address_with_type;
151     maybe_resolve_received_address(address_with_type,
152                                    &resolved_address_with_type);
153     connection_manager::on_connection_timed_out_from_shim(
154         resolved_address_with_type.bda);
155     LOG_WARN("LE connection fail peer:%s bd_addr:%s hci_status:%s",
156              PRIVATE_ADDRESS(address_with_type),
157              PRIVATE_ADDRESS(resolved_address_with_type.bda),
158              hci_status_code_text(status).c_str());
159   } else {
160     btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
161   }
162   btm_ble_update_mode_operation(HCI_ROLE_UNKNOWN, &address_with_type.bda,
163                                 status);
164 }
165 
166 void gatt_notify_conn_update(const RawAddress& remote, uint16_t interval,
167                              uint16_t latency, uint16_t timeout,
168                              tHCI_STATUS status);
acl_ble_update_event_received(tHCI_STATUS status,uint16_t handle,uint16_t interval,uint16_t latency,uint16_t timeout)169 void acl_ble_update_event_received(tHCI_STATUS status, uint16_t handle,
170                                    uint16_t interval, uint16_t latency,
171                                    uint16_t timeout) {
172   l2cble_process_conn_update_evt(handle, status, interval, latency, timeout);
173 
174   tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev_by_handle(handle);
175 
176   if (!p_dev_rec) return;
177 
178   gatt_notify_conn_update(p_dev_rec->ble.pseudo_addr, interval, latency,
179                           timeout, status);
180 }
181 
acl_ble_data_length_change_event(uint16_t handle,uint16_t max_tx_octets,uint16_t max_tx_time,uint16_t max_rx_octets,uint16_t max_rx_time)182 void acl_ble_data_length_change_event(uint16_t handle, uint16_t max_tx_octets,
183                                       uint16_t max_tx_time,
184                                       uint16_t max_rx_octets,
185                                       uint16_t max_rx_time) {
186   LOG_DEBUG(
187       "Data length change event received handle:0x%04x max_tx_octets:%hu "
188       "max_tx_time:%hu max_rx_octets:%hu max_rx_time:%hu",
189       handle, max_tx_octets, max_tx_time, max_rx_octets, max_rx_time);
190   l2cble_process_data_length_change_event(handle, max_tx_octets, max_rx_octets);
191 }
192