blob: 5a0322d2dac7340c144b510d1c7591432e15a18e [file] [log] [blame]
Arman Uguray50a31542015-11-10 18:03:36 -08001//
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07002// Copyright 2015 Google, Inc.
Arman Uguray50a31542015-11-10 18:03:36 -08003//
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>
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070023#include <bluetooth/uuid.h>
Arman Uguray50a31542015-11-10 18:03:36 -080024
Arman Uguraybb18c412015-11-12 13:44:31 -080025#include "service/bluetooth_instance.h"
Arman Uguray50a31542015-11-10 18:03:36 -080026#include "service/hal/bluetooth_gatt_interface.h"
27
28namespace 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.
Arman Uguraybb18c412015-11-12 13:44:31 -080033class GattClient : public BluetoothInstance {
Arman Uguray50a31542015-11-10 18:03:36 -080034 public:
35 ~GattClient() override;
36
37 // BluetoothClientInstace overrides:
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070038 const Uuid& GetAppIdentifier() const override;
Arman Uguraybb18c412015-11-12 13:44:31 -080039 int GetInstanceId() const override;
Arman Uguray50a31542015-11-10 18:03:36 -080040
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.
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070046 GattClient(const Uuid& uuid, int client_id);
Arman Uguray50a31542015-11-10 18:03:36 -080047
48 // See getters above for documentation.
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070049 Uuid app_identifier_;
Arman Uguray50a31542015-11-10 18:03:36 -080050 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.
Arman Uguraybb18c412015-11-12 13:44:31 -080058class GattClientFactory : public BluetoothInstanceFactory,
Arman Uguray50a31542015-11-10 18:03:36 -080059 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
Arman Uguraybb18c412015-11-12 13:44:31 -080066 // BluetoothInstanceFactory override:
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070067 bool RegisterInstance(const Uuid& uuid,
Arman Uguraybb18c412015-11-12 13:44:31 -080068 const RegisterCallback& callback) override;
Arman Uguray50a31542015-11-10 18:03:36 -080069
70 private:
71 // hal::BluetoothGattInterface::ClientObserver override:
Myles Watson911d1ae2016-11-28 16:44:40 -080072 void RegisterClientCallback(hal::BluetoothGattInterface* gatt_iface,
73 int status, int client_id,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070074 const Uuid& app_uuid) override;
Arman Uguray50a31542015-11-10 18:03:36 -080075
76 // Map of pending calls to register.
77 std::mutex pending_calls_lock_;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070078 std::unordered_map<Uuid, RegisterCallback> pending_calls_;
Arman Uguray50a31542015-11-10 18:03:36 -080079
80 DISALLOW_COPY_AND_ASSIGN(GattClientFactory);
81};
82
83} // namespace bluetooth