blob: 0567dd7f6f497a2e8a5d7263d6b8a09b3d6a9122 [file] [log] [blame]
Arman Uguray50a31542015-11-10 18:03:36 -08001//
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/gatt_client.h"
18
19#include <base/logging.h>
20
21using std::lock_guard;
22using std::mutex;
23
24namespace bluetooth {
25
26// GattClient implementation
27// ========================================================
28
29GattClient::GattClient(const UUID& uuid, int client_id)
Myles Watson911d1ae2016-11-28 16:44:40 -080030 : app_identifier_(uuid), client_id_(client_id) {}
Arman Uguray50a31542015-11-10 18:03:36 -080031
32GattClient::~GattClient() {
33 // Automatically unregister the client.
34 VLOG(1) << "GattClient unregistering client: " << client_id_;
35
Myles Watson911d1ae2016-11-28 16:44:40 -080036 hal::BluetoothGattInterface::Get()
37 ->GetClientHALInterface()
38 ->unregister_client(client_id_);
Arman Uguray50a31542015-11-10 18:03:36 -080039}
40
Myles Watson911d1ae2016-11-28 16:44:40 -080041const UUID& GattClient::GetAppIdentifier() const { return app_identifier_; }
Arman Uguray50a31542015-11-10 18:03:36 -080042
Myles Watson911d1ae2016-11-28 16:44:40 -080043int GattClient::GetInstanceId() const { return client_id_; }
Arman Uguray50a31542015-11-10 18:03:36 -080044
45// GattClientFactory implementation
46// ========================================================
47
48GattClientFactory::GattClientFactory() {
49 hal::BluetoothGattInterface::Get()->AddClientObserver(this);
50}
51
52GattClientFactory::~GattClientFactory() {
53 hal::BluetoothGattInterface::Get()->RemoveClientObserver(this);
54}
55
Myles Watson911d1ae2016-11-28 16:44:40 -080056bool GattClientFactory::RegisterInstance(const UUID& uuid,
57 const RegisterCallback& callback) {
Arman Uguray50a31542015-11-10 18:03:36 -080058 VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
59 lock_guard<mutex> lock(pending_calls_lock_);
60
61 if (pending_calls_.find(uuid) != pending_calls_.end()) {
62 LOG(ERROR) << "GATT client with given UUID already registered - "
63 << "UUID: " << uuid.ToString();
64 return false;
65 }
66
67 const btgatt_client_interface_t* hal_iface =
68 hal::BluetoothGattInterface::Get()->GetClientHALInterface();
69 bt_uuid_t app_uuid = uuid.GetBlueDroid();
70
Myles Watson911d1ae2016-11-28 16:44:40 -080071 if (hal_iface->register_client(&app_uuid) != BT_STATUS_SUCCESS) return false;
Arman Uguray50a31542015-11-10 18:03:36 -080072
73 pending_calls_[uuid] = callback;
74
75 return true;
76}
77
78void GattClientFactory::RegisterClientCallback(
Myles Watson911d1ae2016-11-28 16:44:40 -080079 hal::BluetoothGattInterface* /* gatt_iface */, int status, int client_id,
Arman Uguray50a31542015-11-10 18:03:36 -080080 const bt_uuid_t& app_uuid) {
81 UUID uuid(app_uuid);
82
83 auto iter = pending_calls_.find(uuid);
84 if (iter == pending_calls_.end()) {
85 VLOG(1) << "Ignoring callback for unknown app_id: " << uuid.ToString();
86 return;
87 }
88
89 bool success = (status == BT_STATUS_SUCCESS);
90 BLEStatus result = success ? BLE_STATUS_SUCCESS : BLE_STATUS_FAILURE;
91
92 // No need to construct a client if the call wasn't successful.
93 std::unique_ptr<GattClient> client;
Myles Watson911d1ae2016-11-28 16:44:40 -080094 if (success) client.reset(new GattClient(uuid, client_id));
Arman Uguray50a31542015-11-10 18:03:36 -080095
96 // Notify the result via the result callback.
97 iter->second(result, uuid, std::move(client));
98
99 pending_calls_.erase(iter);
100}
101
102} // namespace bluetooth