blob: fb473e1a4b06ac87c0816af189972cb690de0a9b [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
21using std::lock_guard;
22using std::mutex;
23
24namespace bluetooth {
25
26LowEnergyClient::LowEnergyClient(const UUID& uuid, int client_if)
27 : app_identifier_(uuid),
28 client_if_(client_if) {
29}
30
31LowEnergyClient::~LowEnergyClient() {
32 // Automatically unregister the client.
33 VLOG(1) << "LowEnergyClient unregistering client: " << client_if_;
34 hal::BluetoothGattInterface::Get()->
35 GetClientHALInterface()->unregister_client(client_if_);
36}
37
38LowEnergyClientFactory::LowEnergyClientFactory() {
39 hal::BluetoothGattInterface::Get()->AddClientObserver(this);
40}
41
42LowEnergyClientFactory::~LowEnergyClientFactory() {
43 hal::BluetoothGattInterface::Get()->RemoveClientObserver(this);
44}
45
46bool LowEnergyClientFactory::RegisterClient(const UUID& uuid,
47 const ClientCallback& callback) {
48 VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
49 lock_guard<mutex> lock(pending_calls_lock_);
50
51 if (pending_calls_.find(uuid) != pending_calls_.end()) {
52 LOG(ERROR) << "Low-Energy client with given UUID already registered - "
53 << "UUID: " << uuid.ToString();
54 return false;
55 }
56
57 const btgatt_client_interface_t* hal_iface =
58 hal::BluetoothGattInterface::Get()->GetClientHALInterface();
59 bt_uuid_t app_uuid = uuid.GetBlueDroid();
60
61 if (hal_iface->register_client(&app_uuid) != BT_STATUS_SUCCESS)
62 return false;
63
64 pending_calls_[uuid] = callback;
65
66 return true;
67}
68
69void LowEnergyClientFactory::RegisterClientCallback(
70 int status, int client_if,
71 const bt_uuid_t& app_uuid) {
72 UUID uuid(app_uuid);
73
74 VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
75 lock_guard<mutex> lock(pending_calls_lock_);
76
77 auto iter = pending_calls_.find(uuid);
78 if (iter == pending_calls_.end()) {
79 VLOG(1) << "Ignoring callback for unknown app_id: " << uuid.ToString();
80 return;
81 }
82
83 // No need to construct a client if the call wasn't successful.
84 std::unique_ptr<LowEnergyClient> client;
85 BLEStatus result = BLE_STATUS_FAILURE;
86 if (status == BT_STATUS_SUCCESS) {
87 client.reset(new LowEnergyClient(uuid, client_if));
88 result = BLE_STATUS_SUCCESS;
89 }
90
91 // Notify the result via the success callback.
92 iter->second(result, uuid, std::move(client));
93
94 pending_calls_.erase(iter);
95}
96
97} // namespace bluetooth