bluetooth_instance.h 2.6 KB

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