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