bluetooth_adapter_win.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "device/bluetooth/bluetooth_adapter_win.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "base/check.h"
  10. #include "base/feature_list.h"
  11. #include "base/location.h"
  12. #include "base/memory/ptr_util.h"
  13. #include "base/notreached.h"
  14. #include "base/stl_util.h"
  15. #include "base/task/sequenced_task_runner.h"
  16. #include "base/task/single_thread_task_runner.h"
  17. #include "base/threading/thread_task_runner_handle.h"
  18. #include "base/win/windows_version.h"
  19. #include "device/base/features.h"
  20. #include "device/bluetooth/bluetooth_adapter_winrt.h"
  21. #include "device/bluetooth/bluetooth_classic_win.h"
  22. #include "device/bluetooth/bluetooth_device_win.h"
  23. #include "device/bluetooth/bluetooth_discovery_session_outcome.h"
  24. #include "device/bluetooth/bluetooth_socket_thread.h"
  25. #include "device/bluetooth/bluetooth_socket_win.h"
  26. #include "device/bluetooth/bluetooth_task_manager_win.h"
  27. #include "device/bluetooth/public/cpp/bluetooth_address.h"
  28. #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
  29. namespace device {
  30. // static
  31. scoped_refptr<BluetoothAdapter> BluetoothAdapter::CreateAdapter() {
  32. return BluetoothAdapterWin::CreateAdapter();
  33. }
  34. // static
  35. scoped_refptr<BluetoothAdapter> BluetoothAdapterWin::CreateAdapter() {
  36. if (UseNewBLEWinImplementation())
  37. return base::WrapRefCounted(new BluetoothAdapterWinrt());
  38. return BluetoothAdapterWin::CreateClassicAdapter();
  39. }
  40. // static
  41. scoped_refptr<BluetoothAdapter> BluetoothAdapterWin::CreateClassicAdapter() {
  42. return base::WrapRefCounted(new BluetoothAdapterWin());
  43. }
  44. // static
  45. bool BluetoothAdapterWin::UseNewBLEWinImplementation() {
  46. return base::FeatureList::IsEnabled(kNewBLEWinImplementation) &&
  47. base::win::GetVersion() >= base::win::Version::WIN10;
  48. }
  49. BluetoothAdapterWin::BluetoothAdapterWin()
  50. : BluetoothAdapter(),
  51. initialized_(false),
  52. powered_(false),
  53. discovery_status_(NOT_DISCOVERING),
  54. force_update_device_for_test_(false) {}
  55. BluetoothAdapterWin::~BluetoothAdapterWin() {
  56. if (task_manager_.get())
  57. task_manager_->RemoveObserver(this);
  58. }
  59. std::string BluetoothAdapterWin::GetAddress() const {
  60. return address_;
  61. }
  62. std::string BluetoothAdapterWin::GetName() const {
  63. return name_;
  64. }
  65. void BluetoothAdapterWin::SetName(const std::string& name,
  66. base::OnceClosure callback,
  67. ErrorCallback error_callback) {
  68. NOTIMPLEMENTED();
  69. }
  70. // TODO(youngki): Return true when |task_manager_| initializes the adapter
  71. // state.
  72. bool BluetoothAdapterWin::IsInitialized() const {
  73. return initialized_;
  74. }
  75. bool BluetoothAdapterWin::IsPresent() const {
  76. return !address_.empty();
  77. }
  78. bool BluetoothAdapterWin::IsPowered() const {
  79. return powered_;
  80. }
  81. void BluetoothAdapterWin::SetPowered(bool powered,
  82. base::OnceClosure callback,
  83. ErrorCallback error_callback) {
  84. task_manager_->PostSetPoweredBluetoothTask(powered, std::move(callback),
  85. std::move(error_callback));
  86. }
  87. bool BluetoothAdapterWin::IsDiscoverable() const {
  88. NOTIMPLEMENTED();
  89. return false;
  90. }
  91. void BluetoothAdapterWin::SetDiscoverable(bool discoverable,
  92. base::OnceClosure callback,
  93. ErrorCallback error_callback) {
  94. NOTIMPLEMENTED();
  95. }
  96. bool BluetoothAdapterWin::IsDiscovering() const {
  97. return discovery_status_ == DISCOVERING ||
  98. discovery_status_ == DISCOVERY_STOPPING;
  99. }
  100. void BluetoothAdapterWin::DiscoveryStarted(bool success) {
  101. discovery_status_ = success ? DISCOVERING : NOT_DISCOVERING;
  102. std::move(discovery_changed_callback_)
  103. .Run(/*is_error=*/!success, UMABluetoothDiscoverySessionOutcome::UNKNOWN);
  104. if (success) {
  105. for (auto& observer : observers_)
  106. observer.AdapterDiscoveringChanged(this, true);
  107. }
  108. }
  109. void BluetoothAdapterWin::DiscoveryStopped() {
  110. discovered_devices_.clear();
  111. bool was_discovering = IsDiscovering();
  112. discovery_status_ = NOT_DISCOVERING;
  113. std::move(discovery_changed_callback_)
  114. .Run(/*is_error=*/false, UMABluetoothDiscoverySessionOutcome::SUCCESS);
  115. if (was_discovering)
  116. for (auto& observer : observers_)
  117. observer.AdapterDiscoveringChanged(this, false);
  118. }
  119. BluetoothAdapter::UUIDList BluetoothAdapterWin::GetUUIDs() const {
  120. NOTIMPLEMENTED();
  121. return UUIDList();
  122. }
  123. void BluetoothAdapterWin::CreateRfcommService(
  124. const BluetoothUUID& uuid,
  125. const ServiceOptions& options,
  126. CreateServiceCallback callback,
  127. CreateServiceErrorCallback error_callback) {
  128. scoped_refptr<BluetoothSocketWin> socket =
  129. BluetoothSocketWin::CreateBluetoothSocket(
  130. ui_task_runner_, socket_thread_);
  131. socket->Listen(this, uuid, options,
  132. base::BindOnce(std::move(callback), socket),
  133. std::move(error_callback));
  134. }
  135. void BluetoothAdapterWin::CreateL2capService(
  136. const BluetoothUUID& uuid,
  137. const ServiceOptions& options,
  138. CreateServiceCallback callback,
  139. CreateServiceErrorCallback error_callback) {
  140. // TODO(keybuk): implement.
  141. NOTIMPLEMENTED();
  142. }
  143. void BluetoothAdapterWin::RegisterAdvertisement(
  144. std::unique_ptr<BluetoothAdvertisement::Data> advertisement_data,
  145. CreateAdvertisementCallback callback,
  146. AdvertisementErrorCallback error_callback) {
  147. NOTIMPLEMENTED();
  148. std::move(error_callback)
  149. .Run(BluetoothAdvertisement::ERROR_UNSUPPORTED_PLATFORM);
  150. }
  151. BluetoothLocalGattService* BluetoothAdapterWin::GetGattService(
  152. const std::string& identifier) const {
  153. return nullptr;
  154. }
  155. void BluetoothAdapterWin::RemovePairingDelegateInternal(
  156. BluetoothDevice::PairingDelegate* pairing_delegate) {
  157. }
  158. void BluetoothAdapterWin::AdapterStateChanged(
  159. const BluetoothTaskManagerWin::AdapterState& state) {
  160. DCHECK(thread_checker_.CalledOnValidThread());
  161. name_ = state.name;
  162. bool was_present = IsPresent();
  163. bool is_present = !state.address.empty();
  164. address_ = CanonicalizeBluetoothAddress(state.address);
  165. if (was_present != is_present) {
  166. for (auto& observer : observers_)
  167. observer.AdapterPresentChanged(this, is_present);
  168. }
  169. if (powered_ != state.powered) {
  170. powered_ = state.powered;
  171. for (auto& observer : observers_)
  172. observer.AdapterPoweredChanged(this, powered_);
  173. }
  174. if (!initialized_) {
  175. initialized_ = true;
  176. std::move(init_callback_).Run();
  177. }
  178. }
  179. void BluetoothAdapterWin::DevicesPolled(
  180. const std::vector<std::unique_ptr<BluetoothTaskManagerWin::DeviceState>>&
  181. devices) {
  182. DCHECK(thread_checker_.CalledOnValidThread());
  183. // We are receiving a new list of all devices known to the system. Merge this
  184. // new list with the list we know of (|devices_|) and raise corresponding
  185. // DeviceAdded, DeviceRemoved and DeviceChanged events.
  186. using DeviceAddressSet = std::set<std::string>;
  187. DeviceAddressSet known_devices;
  188. for (const auto& device : devices_)
  189. known_devices.insert(device.first);
  190. DeviceAddressSet new_devices;
  191. for (const auto& device_state : devices)
  192. new_devices.insert(device_state->address);
  193. // Process device removal first.
  194. DeviceAddressSet removed_devices =
  195. base::STLSetDifference<DeviceAddressSet>(known_devices, new_devices);
  196. for (const auto& device : removed_devices) {
  197. auto it = devices_.find(device);
  198. std::unique_ptr<BluetoothDevice> device_win = std::move(it->second);
  199. devices_.erase(it);
  200. for (auto& observer : observers_)
  201. observer.DeviceRemoved(this, device_win.get());
  202. }
  203. // Process added and (maybe) changed devices in one pass.
  204. DeviceAddressSet added_devices =
  205. base::STLSetDifference<DeviceAddressSet>(new_devices, known_devices);
  206. DeviceAddressSet changed_devices =
  207. base::STLSetIntersection<DeviceAddressSet>(known_devices, new_devices);
  208. for (const auto& device_state : devices) {
  209. if (added_devices.find(device_state->address) != added_devices.end()) {
  210. auto device_win = std::make_unique<BluetoothDeviceWin>(
  211. this, *device_state, ui_task_runner_, socket_thread_);
  212. BluetoothDeviceWin* device_win_raw = device_win.get();
  213. devices_[device_state->address] = std::move(device_win);
  214. for (auto& observer : observers_)
  215. observer.DeviceAdded(this, device_win_raw);
  216. } else if (changed_devices.find(device_state->address) !=
  217. changed_devices.end()) {
  218. auto iter = devices_.find(device_state->address);
  219. DCHECK(iter != devices_.end());
  220. BluetoothDeviceWin* device_win =
  221. static_cast<BluetoothDeviceWin*>(iter->second.get());
  222. if (!device_win->IsEqual(*device_state)) {
  223. device_win->Update(*device_state);
  224. for (auto& observer : observers_)
  225. observer.DeviceChanged(this, device_win);
  226. }
  227. // Above IsEqual returns true if device name, address, status and services
  228. // (primary services of BLE device) are the same. However, in BLE tests,
  229. // we may simulate characteristic, descriptor and secondary GATT service
  230. // after device has been initialized.
  231. if (force_update_device_for_test_) {
  232. device_win->Update(*device_state);
  233. }
  234. }
  235. }
  236. }
  237. base::WeakPtr<BluetoothAdapter> BluetoothAdapterWin::GetWeakPtr() {
  238. return weak_ptr_factory_.GetWeakPtr();
  239. }
  240. // BluetoothAdapterWin should override SetPowered() instead.
  241. bool BluetoothAdapterWin::SetPoweredImpl(bool powered) {
  242. NOTREACHED();
  243. return false;
  244. }
  245. void BluetoothAdapterWin::UpdateFilter(
  246. std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
  247. DiscoverySessionResultCallback callback) {
  248. DCHECK(discovery_status_ == DISCOVERING ||
  249. discovery_status_ == DISCOVERY_STARTING);
  250. if (discovery_status_ == DISCOVERING) {
  251. std::move(callback).Run(false,
  252. UMABluetoothDiscoverySessionOutcome::SUCCESS);
  253. return;
  254. }
  255. }
  256. void BluetoothAdapterWin::StartScanWithFilter(
  257. std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
  258. DiscoverySessionResultCallback callback) {
  259. discovery_changed_callback_ = std::move(callback);
  260. MaybePostStartDiscoveryTask();
  261. }
  262. void BluetoothAdapterWin::StopScan(DiscoverySessionResultCallback callback) {
  263. if (discovery_status_ == NOT_DISCOVERING) {
  264. std::move(callback).Run(/*is_error=*/true,
  265. UMABluetoothDiscoverySessionOutcome::NOT_ACTIVE);
  266. return;
  267. }
  268. discovery_changed_callback_ = std::move(callback);
  269. MaybePostStopDiscoveryTask();
  270. }
  271. void BluetoothAdapterWin::Initialize(base::OnceClosure init_callback) {
  272. init_callback_ = std::move(init_callback);
  273. ui_task_runner_ = base::ThreadTaskRunnerHandle::Get();
  274. socket_thread_ = BluetoothSocketThread::Get();
  275. task_manager_ =
  276. base::MakeRefCounted<BluetoothTaskManagerWin>(ui_task_runner_);
  277. task_manager_->AddObserver(this);
  278. task_manager_->Initialize();
  279. }
  280. void BluetoothAdapterWin::InitForTest(
  281. base::OnceClosure init_callback,
  282. std::unique_ptr<win::BluetoothClassicWrapper> classic_wrapper,
  283. std::unique_ptr<win::BluetoothLowEnergyWrapper> le_wrapper,
  284. scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
  285. scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner) {
  286. init_callback_ = std::move(init_callback);
  287. ui_task_runner_ = ui_task_runner;
  288. if (!ui_task_runner_)
  289. ui_task_runner_ = base::ThreadTaskRunnerHandle::Get();
  290. task_manager_ = BluetoothTaskManagerWin::CreateForTesting(
  291. std::move(classic_wrapper), std::move(le_wrapper), ui_task_runner_);
  292. task_manager_->AddObserver(this);
  293. task_manager_->InitializeWithBluetoothTaskRunner(bluetooth_task_runner);
  294. }
  295. void BluetoothAdapterWin::MaybePostStartDiscoveryTask() {
  296. if (discovery_status_ == NOT_DISCOVERING) {
  297. discovery_status_ = DISCOVERY_STARTING;
  298. task_manager_->PostStartDiscoveryTask();
  299. }
  300. }
  301. void BluetoothAdapterWin::MaybePostStopDiscoveryTask() {
  302. if (discovery_status_ != DISCOVERING)
  303. return;
  304. discovery_status_ = DISCOVERY_STOPPING;
  305. task_manager_->PostStopDiscoveryTask();
  306. }
  307. } // namespace device