Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 1 | // |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google, Inc. |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 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 | #pragma once |
| 18 | |
| 19 | #include <mutex> |
| 20 | #include <unordered_map> |
| 21 | |
| 22 | #include <base/macros.h> |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 23 | #include <bluetooth/uuid.h> |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 24 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 25 | #include "service/bluetooth_instance.h" |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 26 | #include "service/hal/bluetooth_gatt_interface.h" |
| 27 | |
| 28 | namespace bluetooth { |
| 29 | |
| 30 | // A GattClient instance represents an application's handle to perform GATT |
| 31 | // client-role operations. Instances cannot be created directly and should be |
| 32 | // obtained through the factory. |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 33 | class GattClient : public BluetoothInstance { |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 34 | public: |
| 35 | ~GattClient() override; |
| 36 | |
| 37 | // BluetoothClientInstace overrides: |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 38 | const Uuid& GetAppIdentifier() const override; |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 39 | int GetInstanceId() const override; |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 40 | |
| 41 | private: |
| 42 | friend class GattClientFactory; |
| 43 | |
| 44 | // Constructor shouldn't be called directly as instances are meant to be |
| 45 | // obtained from the factory. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 46 | GattClient(const Uuid& uuid, int client_id); |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 47 | |
| 48 | // See getters above for documentation. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 49 | Uuid app_identifier_; |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 50 | int client_id_; |
| 51 | |
| 52 | DISALLOW_COPY_AND_ASSIGN(GattClient); |
| 53 | }; |
| 54 | |
| 55 | // GattClientFactory is used to register and obtain a per-application GattClient |
| 56 | // instance. Users should call RegisterClient to obtain their own unique |
| 57 | // GattClient instance that has been registered with the Bluetooth stack. |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 58 | class GattClientFactory : public BluetoothInstanceFactory, |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 59 | private hal::BluetoothGattInterface::ClientObserver { |
| 60 | public: |
| 61 | // Don't construct/destruct directly except in tests. Instead, obtain a handle |
| 62 | // from an Adapter instance. |
| 63 | GattClientFactory(); |
| 64 | ~GattClientFactory() override; |
| 65 | |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 66 | // BluetoothInstanceFactory override: |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 67 | bool RegisterInstance(const Uuid& uuid, |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 68 | const RegisterCallback& callback) override; |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | // hal::BluetoothGattInterface::ClientObserver override: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 72 | void RegisterClientCallback(hal::BluetoothGattInterface* gatt_iface, |
| 73 | int status, int client_id, |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 74 | const Uuid& app_uuid) override; |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 75 | |
| 76 | // Map of pending calls to register. |
| 77 | std::mutex pending_calls_lock_; |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 78 | std::unordered_map<Uuid, RegisterCallback> pending_calls_; |
Arman Uguray | 50a3154 | 2015-11-10 18:03:36 -0800 | [diff] [blame] | 79 | |
| 80 | DISALLOW_COPY_AND_ASSIGN(GattClientFactory); |
| 81 | }; |
| 82 | |
| 83 | } // namespace bluetooth |