blob: 629b82af9bad33fe787b93c90eec47a785755861 [file] [log] [blame]
Arman Uguray50a31542015-11-10 18:03:36 -08001//
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07002// Copyright 2015 Google, Inc.
Arman Uguray50a31542015-11-10 18:03:36 -08003//
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
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070029GattClient::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
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070041const 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
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070056bool GattClientFactory::RegisterInstance(const Uuid& uuid,
Myles Watson911d1ae2016-11-28 16:44:40 -080057 const RegisterCallback& callback) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070058 VLOG(1) << __func__ << " - Uuid: " << uuid.ToString();
Arman Uguray50a31542015-11-10 18:03:36 -080059 lock_guard<mutex> lock(pending_calls_lock_);
60
61 if (pending_calls_.find(uuid) != pending_calls_.end()) {
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070062 LOG(ERROR) << "GATT client with given Uuid already registered - "
63 << "Uuid: " << uuid.ToString();
Arman Uguray50a31542015-11-10 18:03:36 -080064 return false;
65 }
66
67 const btgatt_client_interface_t* hal_iface =
68 hal::BluetoothGattInterface::Get()->GetClientHALInterface();
Arman Uguray50a31542015-11-10 18:03:36 -080069
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070070 if (hal_iface->register_client(uuid) != BT_STATUS_SUCCESS) return false;
Arman Uguray50a31542015-11-10 18:03:36 -080071
72 pending_calls_[uuid] = callback;
73
74 return true;
75}
76
77void GattClientFactory::RegisterClientCallback(
Myles Watson911d1ae2016-11-28 16:44:40 -080078 hal::BluetoothGattInterface* /* gatt_iface */, int status, int client_id,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070079 const Uuid& app_uuid) {
80 Uuid uuid(app_uuid);
Arman Uguray50a31542015-11-10 18:03:36 -080081
82 auto iter = pending_calls_.find(uuid);
83 if (iter == pending_calls_.end()) {
84 VLOG(1) << "Ignoring callback for unknown app_id: " << uuid.ToString();
85 return;
86 }
87
88 bool success = (status == BT_STATUS_SUCCESS);
89 BLEStatus result = success ? BLE_STATUS_SUCCESS : BLE_STATUS_FAILURE;
90
91 // No need to construct a client if the call wasn't successful.
92 std::unique_ptr<GattClient> client;
Myles Watson911d1ae2016-11-28 16:44:40 -080093 if (success) client.reset(new GattClient(uuid, client_id));
Arman Uguray50a31542015-11-10 18:03:36 -080094
95 // Notify the result via the result callback.
96 iter->second(result, uuid, std::move(client));
97
98 pending_calls_.erase(iter);
99}
100
101} // namespace bluetooth