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