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 <atomic>
20 #include <functional>
21 #include <map>
22 #include <mutex>
23 
24 #include <base/macros.h>
25 #include <bluetooth/uuid.h>
26 
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"
34 #include "service/hal/bluetooth_gatt_interface.h"
35 
36 namespace bluetooth {
37 
38 class 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.
43 class LowEnergyAdvertiser : public BluetoothInstance {
44  public:
45   // 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.
advertise_settings()73   const AdvertiseSettings& advertise_settings() const {
74     return advertise_settings_;
75   }
76 
77   // BluetoothClientInstace overrides:
78   const Uuid& GetAppIdentifier() const override;
79   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.
86   LowEnergyAdvertiser(const Uuid& uuid, int advertiser_id);
87 
88   // BluetoothGattInterface::AdvertiserObserver overrides:
89   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);
92 
93   // Calls and clears the pending callbacks.
94   void InvokeAndClearStartCallback(BLEStatus status);
95   void InvokeAndClearStopCallback(BLEStatus status);
96 
97   // See getters above for documentation.
98   Uuid app_identifier_;
99   int advertiser_id_;
100 
101   // Protects advertising-related members below.
102   std::mutex adv_fields_lock_;
103 
104   // Latest advertising settings.
105   AdvertiseSettings advertise_settings_;
106 
107   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
115 // LowEnergyAdvertiser instance. Users should call RegisterInstance to obtain
116 // their
117 // own unique LowEnergyAdvertiser instance that has been registered with the
118 // Bluetooth stack.
119 class LowEnergyAdvertiserFactory : public BluetoothInstanceFactory {
120  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:
127   bool RegisterInstance(const Uuid& app_uuid,
128                         const RegisterCallback& callback) override;
129 
130  private:
131   friend class LowEnergyAdvertiser;
132 
133   // BluetoothGattInterface::AdvertiserObserver overrides:
134   void RegisterAdvertiserCallback(const RegisterCallback& callback,
135                                   const Uuid& app_uuid, uint8_t advertiser_id,
136                                   uint8_t status);
137 
138   // Map of pending calls to register.
139   std::mutex pending_calls_lock_;
140   std::unordered_set<Uuid> pending_calls_;
141 
142   DISALLOW_COPY_AND_ASSIGN(LowEnergyAdvertiserFactory);
143 };
144 
145 }  // namespace bluetooth
146