Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 1 | // |
| 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 Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 16 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 17 | #include "service/gatt_server.h" |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 18 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 19 | using std::lock_guard; |
| 20 | using std::mutex; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 21 | |
| 22 | namespace bluetooth { |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 23 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 24 | // GattServer implementation |
| 25 | // ======================================================== |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 26 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 27 | GattServer::GattServer(const UUID& uuid, int server_if) |
| 28 | : app_identifier_(uuid), |
| 29 | server_if_(server_if) { |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 32 | GattServer::~GattServer() { |
| 33 | hal::BluetoothGattInterface::Get()-> |
| 34 | GetServerHALInterface()->unregister_server(server_if_); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 37 | const UUID& GattServer::GetAppIdentifier() const { |
| 38 | return app_identifier_; |
| 39 | } |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 40 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 41 | int GattServer::GetClientId() const { |
| 42 | return server_if_; |
| 43 | } |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 44 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 45 | // GattServerFactory implementation |
| 46 | // ======================================================== |
| 47 | |
| 48 | GattServerFactory::GattServerFactory() { |
| 49 | hal::BluetoothGattInterface::Get()->AddServerObserver(this); |
| 50 | } |
| 51 | |
| 52 | GattServerFactory::~GattServerFactory() { |
| 53 | hal::BluetoothGattInterface::Get()->RemoveServerObserver(this); |
| 54 | } |
| 55 | |
| 56 | bool 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 Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 67 | const btgatt_server_interface_t* hal_iface = |
| 68 | hal::BluetoothGattInterface::Get()->GetServerHALInterface(); |
| 69 | bt_uuid_t app_uuid = uuid.GetBlueDroid(); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 70 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 71 | if (hal_iface->register_server(&app_uuid) != BT_STATUS_SUCCESS) |
| 72 | return false; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 73 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 74 | pending_calls_[uuid] = callback; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 75 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 76 | return true; |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 79 | void 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 Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 84 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 85 | VLOG(1) << __func__ << " - UUID: " << uuid.ToString(); |
| 86 | lock_guard<mutex> lock(pending_calls_lock_); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 87 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 88 | auto iter = pending_calls_.find(uuid); |
| 89 | if (iter == pending_calls_.end()) { |
| 90 | VLOG(1) << "Ignoring callback for unknown app_id: " << uuid.ToString(); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 94 | // 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 Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 102 | // Notify the result via the result callback. |
| 103 | iter->second(result, uuid, std::move(server)); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 104 | |
Arman Uguray | 0f2d489 | 2015-09-22 14:20:42 -0700 | [diff] [blame^] | 105 | pending_calls_.erase(iter); |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Ian Coolidge | 611fcf9 | 2015-06-03 17:20:30 -0700 | [diff] [blame] | 108 | } // namespace bluetooth |