Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 1 | // |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google, Inc. |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 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> |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 25 | #include <bluetooth/uuid.h> |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 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" |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 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. |
Jakub Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 43 | class LowEnergyAdvertiser : public BluetoothInstance { |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 44 | public: |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 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. |
| 73 | const AdvertiseSettings& advertise_settings() const { |
| 74 | return advertise_settings_; |
| 75 | } |
| 76 | |
| 77 | // BluetoothClientInstace overrides: |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 78 | const Uuid& GetAppIdentifier() const override; |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 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. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 86 | LowEnergyAdvertiser(const Uuid& uuid, int advertiser_id); |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 87 | |
| 88 | // BluetoothGattInterface::AdvertiserObserver overrides: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 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); |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 92 | |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 93 | // Calls and clears the pending callbacks. |
| 94 | void InvokeAndClearStartCallback(BLEStatus status); |
| 95 | void InvokeAndClearStopCallback(BLEStatus status); |
| 96 | |
| 97 | // See getters above for documentation. |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 98 | Uuid app_identifier_; |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 99 | int advertiser_id_; |
| 100 | |
| 101 | // Protects advertising-related members below. |
| 102 | std::mutex adv_fields_lock_; |
| 103 | |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 104 | // Latest advertising settings. |
| 105 | AdvertiseSettings advertise_settings_; |
| 106 | |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 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 |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 115 | // LowEnergyAdvertiser instance. Users should call RegisterInstance to obtain |
| 116 | // their |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 117 | // own unique LowEnergyAdvertiser instance that has been registered with the |
| 118 | // Bluetooth stack. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 119 | class LowEnergyAdvertiserFactory : public BluetoothInstanceFactory { |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 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: |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 127 | bool RegisterInstance(const Uuid& app_uuid, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 128 | const RegisterCallback& callback) override; |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | friend class LowEnergyAdvertiser; |
| 132 | |
| 133 | // BluetoothGattInterface::AdvertiserObserver overrides: |
Jakub Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 134 | void RegisterAdvertiserCallback(const RegisterCallback& callback, |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 135 | const Uuid& app_uuid, uint8_t advertiser_id, |
Jakub Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 136 | uint8_t status); |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 137 | |
| 138 | // Map of pending calls to register. |
| 139 | std::mutex pending_calls_lock_; |
Jakub Pawlowski | 819e2ec | 2017-07-10 09:56:09 -0700 | [diff] [blame] | 140 | std::unordered_set<Uuid> pending_calls_; |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 141 | |
| 142 | DISALLOW_COPY_AND_ASSIGN(LowEnergyAdvertiserFactory); |
| 143 | }; |
| 144 | |
| 145 | } // namespace bluetooth |