gatt_client.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <mutex>
  18. #include <unordered_map>
  19. #include <base/macros.h>
  20. #include <bluetooth/uuid.h>
  21. #include "service/bluetooth_instance.h"
  22. #include "service/hal/bluetooth_gatt_interface.h"
  23. namespace bluetooth {
  24. // A GattClient instance represents an application's handle to perform GATT
  25. // client-role operations. Instances cannot be created directly and should be
  26. // obtained through the factory.
  27. class GattClient : public BluetoothInstance {
  28. public:
  29. ~GattClient() override;
  30. // BluetoothClientInstace overrides:
  31. const Uuid& GetAppIdentifier() const override;
  32. int GetInstanceId() const override;
  33. private:
  34. friend class GattClientFactory;
  35. // Constructor shouldn't be called directly as instances are meant to be
  36. // obtained from the factory.
  37. GattClient(const Uuid& uuid, int client_id);
  38. // See getters above for documentation.
  39. Uuid app_identifier_;
  40. int client_id_;
  41. DISALLOW_COPY_AND_ASSIGN(GattClient);
  42. };
  43. // GattClientFactory is used to register and obtain a per-application GattClient
  44. // instance. Users should call RegisterClient to obtain their own unique
  45. // GattClient instance that has been registered with the Bluetooth stack.
  46. class GattClientFactory : public BluetoothInstanceFactory,
  47. private hal::BluetoothGattInterface::ClientObserver {
  48. public:
  49. // Don't construct/destruct directly except in tests. Instead, obtain a handle
  50. // from an Adapter instance.
  51. GattClientFactory();
  52. ~GattClientFactory() override;
  53. // BluetoothInstanceFactory override:
  54. bool RegisterInstance(const Uuid& uuid,
  55. const RegisterCallback& callback) override;
  56. private:
  57. // hal::BluetoothGattInterface::ClientObserver override:
  58. void RegisterClientCallback(hal::BluetoothGattInterface* gatt_iface,
  59. int status, int client_id,
  60. const Uuid& app_uuid) override;
  61. // Map of pending calls to register.
  62. std::mutex pending_calls_lock_;
  63. std::unordered_map<Uuid, RegisterCallback> pending_calls_;
  64. DISALLOW_COPY_AND_ASSIGN(GattClientFactory);
  65. };
  66. } // namespace bluetooth