blob: 1e2d7f33da7b95855840b8313a24442fa07923dc [file] [log] [blame]
Arman Ugurayc2fc0f22015-09-03 15:09:41 -07001//
2// Copyright (C) 2015 Google, Inc.
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 "service/low_energy_client.h"
18
19#include <base/logging.h>
20
Jakub Pawlowski60b0e8f2016-01-12 13:51:35 -080021#include "service/adapter.h"
Jakub Pawlowskia6372e92016-01-19 16:42:37 -080022#include "service/common/bluetooth/util/address_helper.h"
Arman Uguray82ea72f2015-12-02 17:29:27 -080023#include "service/logging_helpers.h"
Arman Uguray12338402015-09-16 18:00:05 -070024#include "stack/include/bt_types.h"
25#include "stack/include/hcidefs.h"
26
Arman Ugurayc2fc0f22015-09-03 15:09:41 -070027using std::lock_guard;
28using std::mutex;
29
30namespace bluetooth {
31
Arman Uguray12338402015-09-16 18:00:05 -070032// LowEnergyClient implementation
33// ========================================================
34
Myles Watson911d1ae2016-11-28 16:44:40 -080035LowEnergyClient::LowEnergyClient(Adapter& adapter, const UUID& uuid,
36 int client_id)
Jakub Pawlowski60b0e8f2016-01-12 13:51:35 -080037 : adapter_(adapter),
38 app_identifier_(uuid),
Arman Uguraybb18c412015-11-12 13:44:31 -080039 client_id_(client_id),
Myles Watson911d1ae2016-11-28 16:44:40 -080040 delegate_(nullptr) {}
Arman Ugurayc2fc0f22015-09-03 15:09:41 -070041
42LowEnergyClient::~LowEnergyClient() {
43 // Automatically unregister the client.
Arman Uguraybb18c412015-11-12 13:44:31 -080044 VLOG(1) << "LowEnergyClient unregistering client: " << client_id_;
Arman Uguray12338402015-09-16 18:00:05 -070045
46 // Unregister as observer so we no longer receive any callbacks.
47 hal::BluetoothGattInterface::Get()->RemoveClientObserver(this);
48
Myles Watson911d1ae2016-11-28 16:44:40 -080049 hal::BluetoothGattInterface::Get()
50 ->GetClientHALInterface()
51 ->unregister_client(client_id_);
Arman Uguray480174f2015-11-30 15:36:17 -080052}
53
Chih-Hung Hsieh5dc0d152016-08-17 14:12:51 -070054bool LowEnergyClient::Connect(const std::string& address, bool is_direct) {
Jakub Pawlowskia6372e92016-01-19 16:42:37 -080055 VLOG(2) << __func__ << "Address: " << address << " is_direct: " << is_direct;
56
57 bt_bdaddr_t bda;
58 util::BdAddrFromString(address, &bda);
59
Myles Watson911d1ae2016-11-28 16:44:40 -080060 bt_status_t status =
61 hal::BluetoothGattInterface::Get()->GetClientHALInterface()->connect(
Jakub Pawlowski96fb2732017-03-24 17:52:02 -070062 client_id_, &bda, is_direct, BT_TRANSPORT_LE, PHY_LE_1M_MASK);
Jakub Pawlowskia6372e92016-01-19 16:42:37 -080063 if (status != BT_STATUS_SUCCESS) {
64 LOG(ERROR) << "HAL call to connect failed";
65 return false;
66 }
67
68 return true;
69}
70
Chih-Hung Hsieh5dc0d152016-08-17 14:12:51 -070071bool LowEnergyClient::Disconnect(const std::string& address) {
Jakub Pawlowskia6372e92016-01-19 16:42:37 -080072 VLOG(2) << __func__ << "Address: " << address;
73
74 bt_bdaddr_t bda;
75 util::BdAddrFromString(address, &bda);
76
77 std::map<const bt_bdaddr_t, int>::iterator conn_id;
78 {
79 lock_guard<mutex> lock(connection_fields_lock_);
80 conn_id = connection_ids_.find(bda);
81 if (conn_id == connection_ids_.end()) {
82 LOG(WARNING) << "Can't disconnect, no existing connection to " << address;
83 return false;
84 }
85 }
86
Myles Watson911d1ae2016-11-28 16:44:40 -080087 bt_status_t status =
88 hal::BluetoothGattInterface::Get()->GetClientHALInterface()->disconnect(
89 client_id_, &bda, conn_id->second);
Jakub Pawlowskia6372e92016-01-19 16:42:37 -080090 if (status != BT_STATUS_SUCCESS) {
91 LOG(ERROR) << "HAL call to disconnect failed";
92 return false;
93 }
94
95 return true;
96}
97
Chih-Hung Hsieh5dc0d152016-08-17 14:12:51 -070098bool LowEnergyClient::SetMtu(const std::string& address, int mtu) {
Myles Watson911d1ae2016-11-28 16:44:40 -080099 VLOG(2) << __func__ << "Address: " << address << " MTU: " << mtu;
Jakub Pawlowskia6551072016-01-26 12:58:47 -0800100
101 bt_bdaddr_t bda;
102 util::BdAddrFromString(address, &bda);
103
104 std::map<const bt_bdaddr_t, int>::iterator conn_id;
105 {
106 lock_guard<mutex> lock(connection_fields_lock_);
107 conn_id = connection_ids_.find(bda);
108 if (conn_id == connection_ids_.end()) {
109 LOG(WARNING) << "Can't set MTU, no existing connection to " << address;
110 return false;
111 }
112 }
113
Myles Watson911d1ae2016-11-28 16:44:40 -0800114 bt_status_t status = hal::BluetoothGattInterface::Get()
115 ->GetClientHALInterface()
116 ->configure_mtu(conn_id->second, mtu);
Jakub Pawlowskia6551072016-01-26 12:58:47 -0800117 if (status != BT_STATUS_SUCCESS) {
118 LOG(ERROR) << "HAL call to set MTU failed";
119 return false;
120 }
121
122 return true;
123}
124
Arman Uguray82ea72f2015-12-02 17:29:27 -0800125void LowEnergyClient::SetDelegate(Delegate* delegate) {
126 lock_guard<mutex> lock(delegate_mutex_);
127 delegate_ = delegate;
128}
129
Arman Uguray08f80eb2015-09-21 11:17:07 -0700130const UUID& LowEnergyClient::GetAppIdentifier() const {
131 return app_identifier_;
132}
133
Myles Watson911d1ae2016-11-28 16:44:40 -0800134int LowEnergyClient::GetInstanceId() const { return client_id_; }
Arman Uguray08f80eb2015-09-21 11:17:07 -0700135
Myles Watson911d1ae2016-11-28 16:44:40 -0800136void LowEnergyClient::ConnectCallback(hal::BluetoothGattInterface* gatt_iface,
137 int conn_id, int status, int client_id,
138 const bt_bdaddr_t& bda) {
139 if (client_id != client_id_) return;
Jakub Pawlowskia6372e92016-01-19 16:42:37 -0800140
141 VLOG(1) << __func__ << "client_id: " << client_id << " status: " << status;
142
143 {
144 lock_guard<mutex> lock(connection_fields_lock_);
145 auto success = connection_ids_.emplace(bda, conn_id);
146 if (!success.second) {
147 LOG(ERROR) << __func__ << " Insertion into connection_ids_ failed!";
148 }
149 }
150
151 if (delegate_)
152 delegate_->OnConnectionState(this, status, BtAddrString(&bda).c_str(),
153 true);
154}
155
156void LowEnergyClient::DisconnectCallback(
Myles Watson911d1ae2016-11-28 16:44:40 -0800157 hal::BluetoothGattInterface* gatt_iface, int conn_id, int status,
158 int client_id, const bt_bdaddr_t& bda) {
159 if (client_id != client_id_) return;
Jakub Pawlowskia6372e92016-01-19 16:42:37 -0800160
161 VLOG(1) << __func__ << " client_id: " << client_id << " status: " << status;
162 {
163 lock_guard<mutex> lock(connection_fields_lock_);
164 if (!connection_ids_.erase(bda)) {
165 LOG(ERROR) << __func__ << " Erasing from connection_ids_ failed!";
166 }
167 }
168
169 if (delegate_)
170 delegate_->OnConnectionState(this, status, BtAddrString(&bda).c_str(),
171 false);
172}
173
Jakub Pawlowskia6551072016-01-26 12:58:47 -0800174void LowEnergyClient::MtuChangedCallback(
Myles Watson911d1ae2016-11-28 16:44:40 -0800175 hal::BluetoothGattInterface* gatt_iface, int conn_id, int status, int mtu) {
Jakub Pawlowskia6551072016-01-26 12:58:47 -0800176 VLOG(1) << __func__ << " conn_id: " << conn_id << " status: " << status
177 << " mtu: " << mtu;
178
Myles Watson911d1ae2016-11-28 16:44:40 -0800179 const bt_bdaddr_t* bda = nullptr;
Jakub Pawlowskia6551072016-01-26 12:58:47 -0800180 {
181 lock_guard<mutex> lock(connection_fields_lock_);
Myles Watson911d1ae2016-11-28 16:44:40 -0800182 for (auto& connection : connection_ids_) {
Jakub Pawlowskia6551072016-01-26 12:58:47 -0800183 if (connection.second == conn_id) {
184 bda = &connection.first;
185 break;
186 }
187 }
188 }
189
Myles Watson911d1ae2016-11-28 16:44:40 -0800190 if (!bda) return;
Jakub Pawlowskia6551072016-01-26 12:58:47 -0800191
Myles Watson911d1ae2016-11-28 16:44:40 -0800192 const char* addr = BtAddrString(bda).c_str();
193 if (delegate_) delegate_->OnMtuChanged(this, status, addr, mtu);
Jakub Pawlowskia6551072016-01-26 12:58:47 -0800194}
195
Arman Uguray12338402015-09-16 18:00:05 -0700196// LowEnergyClientFactory implementation
197// ========================================================
198
Jakub Pawlowski60b0e8f2016-01-12 13:51:35 -0800199LowEnergyClientFactory::LowEnergyClientFactory(Adapter& adapter)
200 : adapter_(adapter) {
Arman Ugurayc2fc0f22015-09-03 15:09:41 -0700201 hal::BluetoothGattInterface::Get()->AddClientObserver(this);
202}
203
204LowEnergyClientFactory::~LowEnergyClientFactory() {
205 hal::BluetoothGattInterface::Get()->RemoveClientObserver(this);
206}
207
Arman Uguraybb18c412015-11-12 13:44:31 -0800208bool LowEnergyClientFactory::RegisterInstance(
Myles Watson911d1ae2016-11-28 16:44:40 -0800209 const UUID& uuid, const RegisterCallback& callback) {
Arman Ugurayc2fc0f22015-09-03 15:09:41 -0700210 VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
211 lock_guard<mutex> lock(pending_calls_lock_);
212
213 if (pending_calls_.find(uuid) != pending_calls_.end()) {
214 LOG(ERROR) << "Low-Energy client with given UUID already registered - "
215 << "UUID: " << uuid.ToString();
216 return false;
217 }
218
219 const btgatt_client_interface_t* hal_iface =
220 hal::BluetoothGattInterface::Get()->GetClientHALInterface();
221 bt_uuid_t app_uuid = uuid.GetBlueDroid();
222
Myles Watson911d1ae2016-11-28 16:44:40 -0800223 if (hal_iface->register_client(&app_uuid) != BT_STATUS_SUCCESS) return false;
Arman Ugurayc2fc0f22015-09-03 15:09:41 -0700224
225 pending_calls_[uuid] = callback;
226
227 return true;
228}
229
230void LowEnergyClientFactory::RegisterClientCallback(
Myles Watson911d1ae2016-11-28 16:44:40 -0800231 hal::BluetoothGattInterface* gatt_iface, int status, int client_id,
Arman Ugurayc2fc0f22015-09-03 15:09:41 -0700232 const bt_uuid_t& app_uuid) {
233 UUID uuid(app_uuid);
234
235 VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
236 lock_guard<mutex> lock(pending_calls_lock_);
237
238 auto iter = pending_calls_.find(uuid);
239 if (iter == pending_calls_.end()) {
240 VLOG(1) << "Ignoring callback for unknown app_id: " << uuid.ToString();
241 return;
242 }
243
244 // No need to construct a client if the call wasn't successful.
245 std::unique_ptr<LowEnergyClient> client;
246 BLEStatus result = BLE_STATUS_FAILURE;
247 if (status == BT_STATUS_SUCCESS) {
Jakub Pawlowski60b0e8f2016-01-12 13:51:35 -0800248 client.reset(new LowEnergyClient(adapter_, uuid, client_id));
Arman Uguray12338402015-09-16 18:00:05 -0700249
Jakub Pawlowski25689c12016-01-20 16:24:03 -0800250 gatt_iface->AddClientObserver(client.get());
Arman Uguray12338402015-09-16 18:00:05 -0700251
Arman Ugurayc2fc0f22015-09-03 15:09:41 -0700252 result = BLE_STATUS_SUCCESS;
253 }
254
Arman Uguray08f80eb2015-09-21 11:17:07 -0700255 // Notify the result via the result callback.
Arman Ugurayc2fc0f22015-09-03 15:09:41 -0700256 iter->second(result, uuid, std::move(client));
257
258 pending_calls_.erase(iter);
259}
260
261} // namespace bluetooth