1 /******************************************************************************
2 *
3 * Copyright 1999-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This file contains functions for BLE device control utilities, and LE
22 * security functions.
23 *
24 ******************************************************************************/
25
26 #define LOG_TAG "ble"
27
28 #include <bluetooth/log.h>
29
30 #include <cstdint>
31
32 #include "base/functional/bind.h"
33 #include "hci/controller_interface.h"
34 #include "main/shim/entry.h"
35 #include "os/log.h"
36 #include "stack/btm/btm_int_types.h"
37 #include "stack/gatt/gatt_int.h"
38 #include "stack/include/acl_api.h"
39 #include "stack/include/bt_types.h"
40 #include "stack/include/btm_ble_api.h"
41 #include "stack/include/btu_hcif.h"
42 #include "stack/include/gatt_api.h"
43 #include "stack/include/hcimsgs.h"
44
45 using namespace bluetooth;
46
47 extern tBTM_CB btm_cb;
48
49 /*******************************************************************************
50 *
51 * Function BTM_BleReceiverTest
52 *
53 * Description This function is called to start the LE Receiver test
54 *
55 * Parameter rx_freq - Frequency Range
56 * p_cmd_cmpl_cback - Command Complete callback
57 *
58 ******************************************************************************/
BTM_BleReceiverTest(uint8_t rx_freq,tBTM_CMPL_CB * p_cmd_cmpl_cback)59 void BTM_BleReceiverTest(uint8_t rx_freq, tBTM_CMPL_CB* p_cmd_cmpl_cback) {
60 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
61
62 btsnd_hcic_ble_receiver_test(rx_freq);
63 }
64
65 /*******************************************************************************
66 *
67 * Function BTM_BleTransmitterTest
68 *
69 * Description This function is called to start the LE Transmitter test
70 *
71 * Parameter tx_freq - Frequency Range
72 * test_data_len - Length in bytes of payload data in each
73 * packet
74 * packet_payload - Pattern to use in the payload
75 * p_cmd_cmpl_cback - Command Complete callback
76 *
77 ******************************************************************************/
BTM_BleTransmitterTest(uint8_t tx_freq,uint8_t test_data_len,uint8_t packet_payload,tBTM_CMPL_CB * p_cmd_cmpl_cback)78 void BTM_BleTransmitterTest(uint8_t tx_freq, uint8_t test_data_len,
79 uint8_t packet_payload,
80 tBTM_CMPL_CB* p_cmd_cmpl_cback) {
81 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
82 btsnd_hcic_ble_transmitter_test(tx_freq, test_data_len, packet_payload);
83 }
84
85 /*******************************************************************************
86 *
87 * Function BTM_BleTestEnd
88 *
89 * Description This function is called to stop the in-progress TX or RX
90 * test
91 *
92 * Parameter p_cmd_cmpl_cback - Command complete callback
93 *
94 ******************************************************************************/
BTM_BleTestEnd(tBTM_CMPL_CB * p_cmd_cmpl_cback)95 void BTM_BleTestEnd(tBTM_CMPL_CB* p_cmd_cmpl_cback) {
96 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
97
98 btsnd_hcic_ble_test_end();
99 }
100
101 /*******************************************************************************
102 * Internal Functions
103 ******************************************************************************/
btm_ble_test_command_complete(uint8_t * p)104 void btm_ble_test_command_complete(uint8_t* p) {
105 tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_le_test_cmd_cmpl_cb;
106
107 btm_cb.devcb.p_le_test_cmd_cmpl_cb = NULL;
108
109 if (p_cb) {
110 (*p_cb)(p);
111 }
112 }
113
114 /*******************************************************************************
115 *
116 * Function BTM_UseLeLink
117 *
118 * Description This function is to select the underlying physical link to
119 * use.
120 *
121 * Returns true to use LE, false use BR/EDR.
122 *
123 ******************************************************************************/
BTM_UseLeLink(const RawAddress & bd_addr)124 bool BTM_UseLeLink(const RawAddress& bd_addr) {
125 if (BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_BR_EDR)) {
126 return false;
127 } else if (BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_LE)) {
128 return true;
129 }
130
131 tBT_DEVICE_TYPE dev_type;
132 tBLE_ADDR_TYPE addr_type;
133 BTM_ReadDevInfo(bd_addr, &dev_type, &addr_type);
134 return (dev_type == BT_DEVICE_TYPE_BLE);
135 }
136
read_phy_cb(base::Callback<void (uint8_t tx_phy,uint8_t rx_phy,uint8_t status)> cb,uint8_t * data,uint16_t len)137 void read_phy_cb(
138 base::Callback<void(uint8_t tx_phy, uint8_t rx_phy, uint8_t status)> cb,
139 uint8_t* data, uint16_t len) {
140 uint8_t status, tx_phy, rx_phy;
141 uint16_t handle;
142
143 log::assert_that(len == 5, "Received bad response length:{}", len);
144 uint8_t* pp = data;
145 STREAM_TO_UINT8(status, pp);
146 STREAM_TO_UINT16(handle, pp);
147 handle = handle & 0x0FFF;
148 STREAM_TO_UINT8(tx_phy, pp);
149 STREAM_TO_UINT8(rx_phy, pp);
150
151 cb.Run(tx_phy, rx_phy, status);
152 }
153
154 /*******************************************************************************
155 *
156 * Function BTM_BleReadPhy
157 *
158 * Description To read the current PHYs for specified LE connection
159 *
160 *
161 * Returns BTM_SUCCESS if command successfully sent to controller,
162 * BTM_MODE_UNSUPPORTED if local controller doesn't support LE
163 * 2M or LE Coded PHY,
164 * BTM_WRONG_MODE if Device in wrong mode for request.
165 *
166 ******************************************************************************/
BTM_BleReadPhy(const RawAddress & bd_addr,base::Callback<void (uint8_t tx_phy,uint8_t rx_phy,uint8_t status)> cb)167 void BTM_BleReadPhy(
168 const RawAddress& bd_addr,
169 base::Callback<void(uint8_t tx_phy, uint8_t rx_phy, uint8_t status)> cb) {
170 if (!BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_LE)) {
171 log::error("Wrong mode: no LE link exist or LE not supported");
172 cb.Run(0, 0, HCI_ERR_NO_CONNECTION);
173 return;
174 }
175
176 // checking if local controller supports it!
177 if (!bluetooth::shim::GetController()->SupportsBle2mPhy() &&
178 !bluetooth::shim::GetController()->SupportsBleCodedPhy()) {
179 log::error("request not supported in local controller!");
180 cb.Run(0, 0, GATT_REQ_NOT_SUPPORTED);
181 return;
182 }
183
184 uint16_t handle = BTM_GetHCIConnHandle(bd_addr, BT_TRANSPORT_LE);
185
186 const uint8_t len = HCIC_PARAM_SIZE_BLE_READ_PHY;
187 uint8_t data[len];
188 uint8_t* pp = data;
189 UINT16_TO_STREAM(pp, handle);
190 btu_hcif_send_cmd_with_cb(FROM_HERE, HCI_BLE_READ_PHY, data, len,
191 base::Bind(&read_phy_cb, std::move(cb)));
192 }
193
doNothing(uint8_t *,uint16_t)194 void doNothing(uint8_t* /* data */, uint16_t /* len */) {}
195
BTM_BleSetPhy(const RawAddress & bd_addr,uint8_t tx_phys,uint8_t rx_phys,uint16_t phy_options)196 void BTM_BleSetPhy(const RawAddress& bd_addr, uint8_t tx_phys, uint8_t rx_phys,
197 uint16_t phy_options) {
198 if (!BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_LE)) {
199 log::info(
200 "Unable to set phy preferences because no le acl is connected to "
201 "device");
202 return;
203 }
204
205 uint8_t all_phys = 0;
206 if (tx_phys == 0) all_phys &= 0x01;
207 if (rx_phys == 0) all_phys &= 0x02;
208
209 uint16_t handle = BTM_GetHCIConnHandle(bd_addr, BT_TRANSPORT_LE);
210
211 // checking if local controller supports it!
212 if (!bluetooth::shim::GetController()->SupportsBle2mPhy() &&
213 !bluetooth::shim::GetController()->SupportsBleCodedPhy()) {
214 log::info(
215 "Local controller unable to support setting of le phy parameters");
216 gatt_notify_phy_updated(static_cast<tHCI_STATUS>(GATT_REQ_NOT_SUPPORTED),
217 handle, tx_phys, rx_phys);
218 return;
219 }
220
221 if (!acl_peer_supports_ble_2m_phy(handle) &&
222 !acl_peer_supports_ble_coded_phy(handle)) {
223 log::info("Remote device unable to support setting of le phy parameter");
224 gatt_notify_phy_updated(static_cast<tHCI_STATUS>(GATT_REQ_NOT_SUPPORTED),
225 handle, tx_phys, rx_phys);
226 return;
227 }
228
229 const uint8_t len = HCIC_PARAM_SIZE_BLE_SET_PHY;
230 uint8_t data[len];
231 uint8_t* pp = data;
232 UINT16_TO_STREAM(pp, handle);
233 UINT8_TO_STREAM(pp, all_phys);
234 UINT8_TO_STREAM(pp, tx_phys);
235 UINT8_TO_STREAM(pp, rx_phys);
236 UINT16_TO_STREAM(pp, phy_options);
237 btu_hcif_send_cmd_with_cb(FROM_HERE, HCI_BLE_SET_PHY, data, len,
238 base::Bind(doNothing));
239 }
240