blob: 8c6c1f7b4c62e9120df8df994a263c13cc2352f6 [file] [log] [blame]
Ian Coolidge611fcf92015-06-03 17:20:30 -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//
Marie Janssen49a86702015-07-08 11:48:57 -070016
Arman Uguray0f2d4892015-09-22 14:20:42 -070017#include "service/gatt_server.h"
Marie Janssen49a86702015-07-08 11:48:57 -070018
Arman Uguray0f2d4892015-09-22 14:20:42 -070019using std::lock_guard;
20using std::mutex;
Ian Coolidge611fcf92015-06-03 17:20:30 -070021
22namespace bluetooth {
Ian Coolidge611fcf92015-06-03 17:20:30 -070023
Arman Uguray0f2d4892015-09-22 14:20:42 -070024// GattServer implementation
25// ========================================================
Ian Coolidge611fcf92015-06-03 17:20:30 -070026
Arman Uguray0f2d4892015-09-22 14:20:42 -070027GattServer::GattServer(const UUID& uuid, int server_if)
28 : app_identifier_(uuid),
29 server_if_(server_if) {
Ian Coolidge611fcf92015-06-03 17:20:30 -070030}
31
Arman Uguray0f2d4892015-09-22 14:20:42 -070032GattServer::~GattServer() {
33 hal::BluetoothGattInterface::Get()->
34 GetServerHALInterface()->unregister_server(server_if_);
Ian Coolidge611fcf92015-06-03 17:20:30 -070035}
36
Arman Uguray0f2d4892015-09-22 14:20:42 -070037const UUID& GattServer::GetAppIdentifier() const {
38 return app_identifier_;
39}
Ian Coolidge611fcf92015-06-03 17:20:30 -070040
Arman Uguray0f2d4892015-09-22 14:20:42 -070041int GattServer::GetClientId() const {
42 return server_if_;
43}
Ian Coolidge611fcf92015-06-03 17:20:30 -070044
Arman Uguray0f2d4892015-09-22 14:20:42 -070045// GattServerFactory implementation
46// ========================================================
47
48GattServerFactory::GattServerFactory() {
49 hal::BluetoothGattInterface::Get()->AddServerObserver(this);
50}
51
52GattServerFactory::~GattServerFactory() {
53 hal::BluetoothGattInterface::Get()->RemoveServerObserver(this);
54}
55
56bool GattServerFactory::RegisterClient(const UUID& uuid,
57 const RegisterCallback& callback) {
58 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-server client with given UUID already being registered "
63 << " - UUID: " << uuid.ToString();
64 return false;
Ian Coolidge611fcf92015-06-03 17:20:30 -070065 }
66
Arman Uguray0f2d4892015-09-22 14:20:42 -070067 const btgatt_server_interface_t* hal_iface =
68 hal::BluetoothGattInterface::Get()->GetServerHALInterface();
69 bt_uuid_t app_uuid = uuid.GetBlueDroid();
Ian Coolidge611fcf92015-06-03 17:20:30 -070070
Arman Uguray0f2d4892015-09-22 14:20:42 -070071 if (hal_iface->register_server(&app_uuid) != BT_STATUS_SUCCESS)
72 return false;
Ian Coolidge611fcf92015-06-03 17:20:30 -070073
Arman Uguray0f2d4892015-09-22 14:20:42 -070074 pending_calls_[uuid] = callback;
Ian Coolidge611fcf92015-06-03 17:20:30 -070075
Arman Uguray0f2d4892015-09-22 14:20:42 -070076 return true;
Ian Coolidge611fcf92015-06-03 17:20:30 -070077}
78
Arman Uguray0f2d4892015-09-22 14:20:42 -070079void GattServerFactory::RegisterServerCallback(
80 hal::BluetoothGattInterface* /* gatt_iface */,
81 int status, int server_if,
82 const bt_uuid_t& app_uuid) {
83 UUID uuid(app_uuid);
Ian Coolidge611fcf92015-06-03 17:20:30 -070084
Arman Uguray0f2d4892015-09-22 14:20:42 -070085 VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
86 lock_guard<mutex> lock(pending_calls_lock_);
Ian Coolidge611fcf92015-06-03 17:20:30 -070087
Arman Uguray0f2d4892015-09-22 14:20:42 -070088 auto iter = pending_calls_.find(uuid);
89 if (iter == pending_calls_.end()) {
90 VLOG(1) << "Ignoring callback for unknown app_id: " << uuid.ToString();
Ian Coolidge611fcf92015-06-03 17:20:30 -070091 return;
92 }
93
Arman Uguray0f2d4892015-09-22 14:20:42 -070094 // No need to construct a server if the call wasn't successful.
95 std::unique_ptr<GattServer> server;
96 BLEStatus result = BLE_STATUS_FAILURE;
97 if (status == BT_STATUS_SUCCESS) {
98 server.reset(new GattServer(uuid, server_if));
99 result = BLE_STATUS_SUCCESS;
Ian Coolidge611fcf92015-06-03 17:20:30 -0700100 }
101
Arman Uguray0f2d4892015-09-22 14:20:42 -0700102 // Notify the result via the result callback.
103 iter->second(result, uuid, std::move(server));
Ian Coolidge611fcf92015-06-03 17:20:30 -0700104
Arman Uguray0f2d4892015-09-22 14:20:42 -0700105 pending_calls_.erase(iter);
Ian Coolidge611fcf92015-06-03 17:20:30 -0700106}
107
Ian Coolidge611fcf92015-06-03 17:20:30 -0700108} // namespace bluetooth