blob: ec0dd748b0a7cfb26e8d4acc7798380156ae03b0 [file] [log] [blame]
Jakub Pawlowski67d5a252016-07-13 11:55:16 -07001//
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07002// Copyright 2015 Google, Inc.
Jakub Pawlowski67d5a252016-07-13 11:55:16 -07003//
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 <atomic>
20#include <functional>
21#include <map>
22#include <mutex>
23
24#include <base/macros.h>
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070025#include <bluetooth/uuid.h>
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070026
27#include "service/bluetooth_instance.h"
28#include "service/common/bluetooth/advertise_data.h"
29#include "service/common/bluetooth/advertise_settings.h"
30#include "service/common/bluetooth/low_energy_constants.h"
31#include "service/common/bluetooth/scan_filter.h"
32#include "service/common/bluetooth/scan_result.h"
33#include "service/common/bluetooth/scan_settings.h"
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070034#include "service/hal/bluetooth_gatt_interface.h"
35
36namespace bluetooth {
37
38class Adapter;
39
40// A LowEnergyAdvertiser represents an application's handle to perform various
41// Bluetooth Low Energy GAP operations. Instances cannot be created directly and
42// should be obtained through the factory.
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -070043class LowEnergyAdvertiser : public BluetoothInstance {
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070044 public:
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070045 // The destructor automatically unregisters this client instance from the
46 // stack.
47 ~LowEnergyAdvertiser() override;
48
49 // Callback type used to return the result of asynchronous operations below.
50 using StatusCallback = std::function<void(BLEStatus)>;
51
52 // Starts advertising based on the given advertising and scan response
53 // data and the provided |settings|. Reports the result of the operation in
54 // |callback|. Return true on success, false otherwise. Please see logs for
55 // details in case of error.
56 bool StartAdvertising(const AdvertiseSettings& settings,
57 const AdvertiseData& advertise_data,
58 const AdvertiseData& scan_response,
59 const StatusCallback& callback);
60
61 // Stops advertising if it was already started. Reports the result of the
62 // operation in |callback|.
63 bool StopAdvertising(const StatusCallback& callback);
64
65 // Returns true if advertising has been started.
66 bool IsAdvertisingStarted() const;
67
68 // Returns the state of pending advertising operations.
69 bool IsStartingAdvertising() const;
70 bool IsStoppingAdvertising() const;
71
72 // Returns the current advertising settings.
73 const AdvertiseSettings& advertise_settings() const {
74 return advertise_settings_;
75 }
76
77 // BluetoothClientInstace overrides:
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070078 const Uuid& GetAppIdentifier() const override;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070079 int GetInstanceId() const override;
80
81 private:
82 friend class LowEnergyAdvertiserFactory;
83
84 // Constructor shouldn't be called directly as instances are meant to be
85 // obtained from the factory.
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070086 LowEnergyAdvertiser(const Uuid& uuid, int advertiser_id);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070087
88 // BluetoothGattInterface::AdvertiserObserver overrides:
Myles Watson911d1ae2016-11-28 16:44:40 -080089 void SetDataCallback(uint8_t advertiser_id, uint8_t status);
90 void SetParamsCallback(uint8_t advertiser_id, uint8_t status);
91 void EnableCallback(bool enable, uint8_t advertiser_id, uint8_t status);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070092
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070093 // Calls and clears the pending callbacks.
94 void InvokeAndClearStartCallback(BLEStatus status);
95 void InvokeAndClearStopCallback(BLEStatus status);
96
97 // See getters above for documentation.
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -070098 Uuid app_identifier_;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070099 int advertiser_id_;
100
101 // Protects advertising-related members below.
102 std::mutex adv_fields_lock_;
103
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700104 // Latest advertising settings.
105 AdvertiseSettings advertise_settings_;
106
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700107 std::atomic_bool adv_started_;
108 std::unique_ptr<StatusCallback> adv_start_callback_;
109 std::unique_ptr<StatusCallback> adv_stop_callback_;
110
111 DISALLOW_COPY_AND_ASSIGN(LowEnergyAdvertiser);
112};
113
114// LowEnergyAdvertiserFactory is used to register and obtain a per-application
Myles Watson911d1ae2016-11-28 16:44:40 -0800115// LowEnergyAdvertiser instance. Users should call RegisterInstance to obtain
116// their
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700117// own unique LowEnergyAdvertiser instance that has been registered with the
118// Bluetooth stack.
Myles Watson911d1ae2016-11-28 16:44:40 -0800119class LowEnergyAdvertiserFactory : public BluetoothInstanceFactory {
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700120 public:
121 // Don't construct/destruct directly except in tests. Instead, obtain a handle
122 // from an Adapter instance.
123 explicit LowEnergyAdvertiserFactory();
124 ~LowEnergyAdvertiserFactory() override;
125
126 // BluetoothInstanceFactory override:
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700127 bool RegisterInstance(const Uuid& app_uuid,
Myles Watson911d1ae2016-11-28 16:44:40 -0800128 const RegisterCallback& callback) override;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700129
130 private:
131 friend class LowEnergyAdvertiser;
132
133 // BluetoothGattInterface::AdvertiserObserver overrides:
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700134 void RegisterAdvertiserCallback(const RegisterCallback& callback,
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700135 const Uuid& app_uuid, uint8_t advertiser_id,
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -0700136 uint8_t status);
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700137
138 // Map of pending calls to register.
139 std::mutex pending_calls_lock_;
Jakub Pawlowski819e2ec2017-07-10 09:56:09 -0700140 std::unordered_set<Uuid> pending_calls_;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700141
142 DISALLOW_COPY_AND_ASSIGN(LowEnergyAdvertiserFactory);
143};
144
145} // namespace bluetooth