• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <bluetooth/uuid.h>
25 
26 #include "service/bluetooth_instance.h"
27 #include "service/common/bluetooth/advertise_data.h"
28 #include "service/common/bluetooth/advertise_settings.h"
29 #include "service/common/bluetooth/low_energy_constants.h"
30 #include "service/common/bluetooth/scan_filter.h"
31 #include "service/common/bluetooth/scan_result.h"
32 #include "service/common/bluetooth/scan_settings.h"
33 #include "service/hal/bluetooth_gatt_interface.h"
34 
35 namespace bluetooth {
36 
37 class Adapter;
38 
39 // A LowEnergyAdvertiser represents an application's handle to perform various
40 // Bluetooth Low Energy GAP operations. Instances cannot be created directly and
41 // should be obtained through the factory.
42 class LowEnergyAdvertiser : public BluetoothInstance {
43  public:
44   LowEnergyAdvertiser(const LowEnergyAdvertiser&) = delete;
45   LowEnergyAdvertiser& operator=(const LowEnergyAdvertiser&) = delete;
46 
47   // The destructor automatically unregisters this client instance from the
48   // stack.
49   ~LowEnergyAdvertiser() override;
50 
51   // Callback type used to return the result of asynchronous operations below.
52   using StatusCallback = std::function<void(BLEStatus)>;
53 
54   // Starts advertising based on the given advertising and scan response
55   // data and the provided |settings|. Reports the result of the operation in
56   // |callback|. Return true on success, false otherwise. Please see logs for
57   // details in case of error.
58   bool StartAdvertising(const AdvertiseSettings& settings,
59                         const AdvertiseData& advertise_data,
60                         const AdvertiseData& scan_response,
61                         const StatusCallback& callback);
62 
63   // Stops advertising if it was already started. Reports the result of the
64   // operation in |callback|.
65   bool StopAdvertising(const StatusCallback& callback);
66 
67   // Returns true if advertising has been started.
68   bool IsAdvertisingStarted() const;
69 
70   // Returns the state of pending advertising operations.
71   bool IsStartingAdvertising() const;
72   bool IsStoppingAdvertising() const;
73 
74   // Returns the current advertising settings.
advertise_settings()75   const AdvertiseSettings& advertise_settings() const {
76     return advertise_settings_;
77   }
78 
79   // BluetoothClientInstace overrides:
80   const Uuid& GetAppIdentifier() const override;
81   int GetInstanceId() const override;
82 
83  private:
84   friend class LowEnergyAdvertiserFactory;
85 
86   // Constructor shouldn't be called directly as instances are meant to be
87   // obtained from the factory.
88   LowEnergyAdvertiser(const Uuid& uuid, int advertiser_id);
89 
90   // BluetoothGattInterface::AdvertiserObserver overrides:
91   void SetDataCallback(uint8_t advertiser_id, uint8_t status);
92   void SetParamsCallback(uint8_t advertiser_id, uint8_t status);
93   void EnableCallback(bool enable, uint8_t advertiser_id, uint8_t status);
94 
95   // Calls and clears the pending callbacks.
96   void InvokeAndClearStartCallback(BLEStatus status);
97   void InvokeAndClearStopCallback(BLEStatus status);
98 
99   // See getters above for documentation.
100   Uuid app_identifier_;
101   int advertiser_id_;
102 
103   // Protects advertising-related members below.
104   std::mutex adv_fields_lock_;
105 
106   // Latest advertising settings.
107   AdvertiseSettings advertise_settings_;
108 
109   std::atomic_bool adv_started_;
110   std::unique_ptr<StatusCallback> adv_start_callback_;
111   std::unique_ptr<StatusCallback> adv_stop_callback_;
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(const LowEnergyAdvertiserFactory&) = delete;
125   LowEnergyAdvertiserFactory& operator=(const LowEnergyAdvertiserFactory&) =
126       delete;
127 
128   ~LowEnergyAdvertiserFactory() override;
129 
130   // BluetoothInstanceFactory override:
131   bool RegisterInstance(const Uuid& app_uuid,
132                         const RegisterCallback& callback) override;
133 
134  private:
135   friend class LowEnergyAdvertiser;
136 
137   // BluetoothGattInterface::AdvertiserObserver overrides:
138   void RegisterAdvertiserCallback(const RegisterCallback& callback,
139                                   const Uuid& app_uuid, uint8_t advertiser_id,
140                                   uint8_t status);
141 
142   // Map of pending calls to register.
143   std::mutex pending_calls_lock_;
144   std::unordered_set<Uuid> pending_calls_;
145 };
146 
147 }  // namespace bluetooth
148