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 <mutex> 20 #include <unordered_map> 21 22 #include <bluetooth/uuid.h> 23 24 #include "service/bluetooth_instance.h" 25 #include "service/hal/bluetooth_gatt_interface.h" 26 27 namespace bluetooth { 28 29 // A GattClient instance represents an application's handle to perform GATT 30 // client-role operations. Instances cannot be created directly and should be 31 // obtained through the factory. 32 class GattClient : public BluetoothInstance { 33 public: 34 GattClient(const GattClient&) = delete; 35 GattClient& operator=(const GattClient&) = delete; 36 37 ~GattClient() override; 38 39 // BluetoothClientInstace overrides: 40 const Uuid& GetAppIdentifier() const override; 41 int GetInstanceId() const override; 42 43 private: 44 friend class GattClientFactory; 45 46 // Constructor shouldn't be called directly as instances are meant to be 47 // obtained from the factory. 48 GattClient(const Uuid& uuid, int client_id); 49 50 // See getters above for documentation. 51 Uuid app_identifier_; 52 int client_id_; 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. 58 class GattClientFactory : public BluetoothInstanceFactory, 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(const GattClientFactory&) = delete; 65 GattClientFactory& operator=(const GattClientFactory&) = delete; 66 67 ~GattClientFactory() override; 68 69 // BluetoothInstanceFactory override: 70 bool RegisterInstance(const Uuid& uuid, 71 const RegisterCallback& callback) override; 72 73 private: 74 // hal::BluetoothGattInterface::ClientObserver override: 75 void RegisterClientCallback(hal::BluetoothGattInterface* gatt_iface, 76 int status, int client_id, 77 const Uuid& app_uuid) override; 78 79 // Map of pending calls to register. 80 std::mutex pending_calls_lock_; 81 std::unordered_map<Uuid, RegisterCallback> pending_calls_; 82 }; 83 84 } // namespace bluetooth 85