Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 1 | // |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google, Inc. |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -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 <functional> |
| 20 | #include <memory> |
| 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 | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 24 | |
| 25 | #include "service/common/bluetooth/low_energy_constants.h" |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 26 | |
| 27 | namespace bluetooth { |
| 28 | |
| 29 | // A BluetoothInstance represents an application's handle to an instance |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 30 | // that is registered with the underlying Bluetooth stack using a Uuid and has a |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 31 | // stack-assigned integer "instance_id" ID associated with it. |
| 32 | class BluetoothInstance { |
| 33 | public: |
| 34 | virtual ~BluetoothInstance() = default; |
| 35 | |
| 36 | // Returns the app-specific unique ID used while registering this instance. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 37 | virtual const Uuid& GetAppIdentifier() const = 0; |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 38 | |
| 39 | // Returns the HAL "interface ID" assigned to this instance by the stack. |
| 40 | virtual int GetInstanceId() const = 0; |
| 41 | |
| 42 | protected: |
| 43 | // Constructor shouldn't be called directly as instances are meant to be |
| 44 | // obtained from the factory. |
| 45 | BluetoothInstance() = default; |
| 46 | |
| 47 | private: |
| 48 | DISALLOW_COPY_AND_ASSIGN(BluetoothInstance); |
| 49 | }; |
| 50 | |
| 51 | // A BluetoothInstanceFactory provides a common interface for factory |
| 52 | // classes that handle asynchronously registering a per-application instance of |
| 53 | // a BluetoothInstance with the underlying stack. |
| 54 | class BluetoothInstanceFactory { |
| 55 | public: |
| 56 | BluetoothInstanceFactory() = default; |
| 57 | virtual ~BluetoothInstanceFactory() = default; |
| 58 | |
| 59 | // Callback invoked as a result of a call to RegisterInstance. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 60 | using RegisterCallback = |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 61 | std::function<void(BLEStatus status, const Uuid& app_uuid, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 62 | std::unique_ptr<BluetoothInstance> instance)>; |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 63 | |
| 64 | // Registers an instance for the given unique identifier |app_uuid|. |
| 65 | // On success, this asynchronously invokes |callback| with a unique pointer |
| 66 | // to a BluetoothInstance whose ownership can be taken by the caller. In |
| 67 | // the case of an error, the pointer will contain nullptr. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 68 | virtual bool RegisterInstance(const Uuid& app_uuid, |
Arman Uguray | bb18c41 | 2015-11-12 13:44:31 -0800 | [diff] [blame] | 69 | const RegisterCallback& callback) = 0; |
| 70 | |
| 71 | private: |
| 72 | DISALLOW_COPY_AND_ASSIGN(BluetoothInstanceFactory); |
| 73 | }; |
| 74 | |
| 75 | } // namespace bluetooth |