bluetooth_device_win.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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_device_win.h"
  5. #include <string>
  6. #include <unordered_map>
  7. #include "base/bind.h"
  8. #include "base/check_op.h"
  9. #include "base/memory/ptr_util.h"
  10. #include "base/notreached.h"
  11. #include "base/task/sequenced_task_runner.h"
  12. #include "device/bluetooth/bluetooth_adapter_win.h"
  13. #include "device/bluetooth/bluetooth_remote_gatt_service_win.h"
  14. #include "device/bluetooth/bluetooth_service_record_win.h"
  15. #include "device/bluetooth/bluetooth_socket_thread.h"
  16. #include "device/bluetooth/bluetooth_socket_win.h"
  17. #include "device/bluetooth/bluetooth_task_manager_win.h"
  18. #include "device/bluetooth/public/cpp/bluetooth_address.h"
  19. #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
  20. namespace {
  21. const char kApiUnavailable[] = "This API is not implemented on this platform.";
  22. } // namespace
  23. namespace device {
  24. BluetoothDeviceWin::BluetoothDeviceWin(
  25. BluetoothAdapterWin* adapter,
  26. const BluetoothTaskManagerWin::DeviceState& device_state,
  27. scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
  28. scoped_refptr<BluetoothSocketThread> socket_thread)
  29. : BluetoothDevice(adapter),
  30. ui_task_runner_(std::move(ui_task_runner)),
  31. socket_thread_(std::move(socket_thread)) {
  32. Update(device_state);
  33. }
  34. BluetoothDeviceWin::~BluetoothDeviceWin() {
  35. // Explicitly take and erase GATT services one by one to ensure that calling
  36. // GetGattService on removed service in GattServiceRemoved returns null.
  37. std::vector<std::string> service_keys;
  38. for (const auto& gatt_service : gatt_services_) {
  39. service_keys.push_back(gatt_service.first);
  40. }
  41. for (const auto& key : service_keys) {
  42. std::unique_ptr<BluetoothRemoteGattService> service =
  43. std::move(gatt_services_[key]);
  44. gatt_services_.erase(key);
  45. }
  46. }
  47. uint32_t BluetoothDeviceWin::GetBluetoothClass() const {
  48. return bluetooth_class_;
  49. }
  50. std::string BluetoothDeviceWin::GetAddress() const {
  51. return address_;
  52. }
  53. BluetoothDevice::AddressType BluetoothDeviceWin::GetAddressType() const {
  54. NOTIMPLEMENTED();
  55. return ADDR_TYPE_UNKNOWN;
  56. }
  57. BluetoothDevice::VendorIDSource
  58. BluetoothDeviceWin::GetVendorIDSource() const {
  59. return VENDOR_ID_UNKNOWN;
  60. }
  61. uint16_t BluetoothDeviceWin::GetVendorID() const {
  62. return 0;
  63. }
  64. uint16_t BluetoothDeviceWin::GetProductID() const {
  65. return 0;
  66. }
  67. uint16_t BluetoothDeviceWin::GetDeviceID() const {
  68. return 0;
  69. }
  70. uint16_t BluetoothDeviceWin::GetAppearance() const {
  71. // TODO(crbug.com/588083): Implementing GetAppearance()
  72. // on mac, win, and android platforms for chrome
  73. NOTIMPLEMENTED();
  74. return 0;
  75. }
  76. absl::optional<std::string> BluetoothDeviceWin::GetName() const {
  77. return name_;
  78. }
  79. bool BluetoothDeviceWin::IsPaired() const {
  80. return paired_;
  81. }
  82. bool BluetoothDeviceWin::IsConnected() const {
  83. return connected_;
  84. }
  85. bool BluetoothDeviceWin::IsGattConnected() const {
  86. // If a BLE device is not GATT connected, Windows will automatically
  87. // reconnect.
  88. return is_low_energy_;
  89. }
  90. bool BluetoothDeviceWin::IsConnectable() const {
  91. return false;
  92. }
  93. bool BluetoothDeviceWin::IsConnecting() const {
  94. return false;
  95. }
  96. BluetoothDevice::UUIDSet BluetoothDeviceWin::GetUUIDs() const {
  97. return uuids_;
  98. }
  99. absl::optional<int8_t> BluetoothDeviceWin::GetInquiryRSSI() const {
  100. // In windows, we can only get connected devices and connected
  101. // devices don't have an Inquiry RSSI.
  102. return absl::nullopt;
  103. }
  104. absl::optional<int8_t> BluetoothDeviceWin::GetInquiryTxPower() const {
  105. // In windows, we can only get connected devices and connected
  106. // devices don't have an Inquiry Tx Power.
  107. return absl::nullopt;
  108. }
  109. bool BluetoothDeviceWin::ExpectingPinCode() const {
  110. NOTIMPLEMENTED();
  111. return false;
  112. }
  113. bool BluetoothDeviceWin::ExpectingPasskey() const {
  114. NOTIMPLEMENTED();
  115. return false;
  116. }
  117. bool BluetoothDeviceWin::ExpectingConfirmation() const {
  118. NOTIMPLEMENTED();
  119. return false;
  120. }
  121. void BluetoothDeviceWin::GetConnectionInfo(ConnectionInfoCallback callback) {
  122. NOTIMPLEMENTED();
  123. std::move(callback).Run(ConnectionInfo());
  124. }
  125. void BluetoothDeviceWin::SetConnectionLatency(
  126. ConnectionLatency connection_latency,
  127. base::OnceClosure callback,
  128. ErrorCallback error_callback) {
  129. NOTIMPLEMENTED();
  130. }
  131. void BluetoothDeviceWin::Connect(PairingDelegate* pairing_delegate,
  132. ConnectCallback callback) {
  133. NOTIMPLEMENTED();
  134. }
  135. void BluetoothDeviceWin::SetPinCode(const std::string& pincode) {
  136. NOTIMPLEMENTED();
  137. }
  138. void BluetoothDeviceWin::SetPasskey(uint32_t passkey) {
  139. NOTIMPLEMENTED();
  140. }
  141. void BluetoothDeviceWin::ConfirmPairing() {
  142. NOTIMPLEMENTED();
  143. }
  144. void BluetoothDeviceWin::RejectPairing() {
  145. NOTIMPLEMENTED();
  146. }
  147. void BluetoothDeviceWin::CancelPairing() {
  148. NOTIMPLEMENTED();
  149. }
  150. void BluetoothDeviceWin::Disconnect(base::OnceClosure callback,
  151. ErrorCallback error_callback) {
  152. NOTIMPLEMENTED();
  153. }
  154. void BluetoothDeviceWin::Forget(base::OnceClosure callback,
  155. ErrorCallback error_callback) {
  156. NOTIMPLEMENTED();
  157. }
  158. void BluetoothDeviceWin::ConnectToService(
  159. const BluetoothUUID& uuid,
  160. ConnectToServiceCallback callback,
  161. ConnectToServiceErrorCallback error_callback) {
  162. scoped_refptr<BluetoothSocketWin> socket(
  163. BluetoothSocketWin::CreateBluetoothSocket(
  164. ui_task_runner_, socket_thread_));
  165. socket->Connect(this, uuid, base::BindOnce(std::move(callback), socket),
  166. std::move(error_callback));
  167. }
  168. void BluetoothDeviceWin::ConnectToServiceInsecurely(
  169. const BluetoothUUID& uuid,
  170. ConnectToServiceCallback callback,
  171. ConnectToServiceErrorCallback error_callback) {
  172. std::move(error_callback).Run(kApiUnavailable);
  173. }
  174. const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord(
  175. const device::BluetoothUUID& uuid) const {
  176. for (const auto& record : service_record_list_)
  177. if (record->uuid() == uuid)
  178. return record.get();
  179. return nullptr;
  180. }
  181. bool BluetoothDeviceWin::IsEqual(
  182. const BluetoothTaskManagerWin::DeviceState& device_state) {
  183. if (address_ != device_state.address || name_ != device_state.name ||
  184. bluetooth_class_ != device_state.bluetooth_class ||
  185. visible_ != device_state.visible ||
  186. connected_ != device_state.connected ||
  187. is_low_energy_ == device_state.is_bluetooth_classic() ||
  188. paired_ != device_state.authenticated) {
  189. return false;
  190. }
  191. // Checks service collection
  192. UUIDSet new_services;
  193. std::unordered_map<std::string, std::unique_ptr<BluetoothServiceRecordWin>>
  194. new_service_records;
  195. for (auto iter = device_state.service_record_states.begin();
  196. iter != device_state.service_record_states.end(); ++iter) {
  197. auto service_record = std::make_unique<BluetoothServiceRecordWin>(
  198. address_, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid);
  199. new_services.insert(service_record->uuid());
  200. new_service_records[service_record->uuid().canonical_value()] =
  201. std::move(service_record);
  202. }
  203. // Check that no new services have been added or removed.
  204. if (uuids_ != new_services) {
  205. return false;
  206. }
  207. for (const auto& service_record : service_record_list_) {
  208. BluetoothServiceRecordWin* new_service_record =
  209. new_service_records[service_record->uuid().canonical_value()].get();
  210. if (!service_record->IsEqual(*new_service_record))
  211. return false;
  212. }
  213. return true;
  214. }
  215. void BluetoothDeviceWin::Update(
  216. const BluetoothTaskManagerWin::DeviceState& device_state) {
  217. address_ = device_state.address;
  218. // Note: Callers are responsible for providing a canonicalized address.
  219. DCHECK_EQ(address_, CanonicalizeBluetoothAddress(address_));
  220. name_ = device_state.name;
  221. bluetooth_class_ = device_state.bluetooth_class;
  222. visible_ = device_state.visible;
  223. connected_ = device_state.connected;
  224. is_low_energy_ = !device_state.is_bluetooth_classic();
  225. paired_ = device_state.authenticated;
  226. UpdateServices(device_state);
  227. }
  228. void BluetoothDeviceWin::GattServiceDiscoveryComplete(
  229. BluetoothRemoteGattServiceWin* service) {
  230. DCHECK(ui_task_runner_->RunsTasksInCurrentSequence());
  231. DCHECK(BluetoothDeviceWin::IsGattServiceDiscovered(
  232. service->GetUUID(), service->GetAttributeHandle()));
  233. discovery_completed_included_services_.insert(
  234. {service->GetUUID(), service->GetAttributeHandle()});
  235. if (discovery_completed_included_services_.size() != gatt_services_.size())
  236. return;
  237. SetGattServicesDiscoveryComplete(true);
  238. adapter_->NotifyGattServicesDiscovered(this);
  239. }
  240. void BluetoothDeviceWin::CreateGattConnectionImpl(
  241. absl::optional<BluetoothUUID> service_uuid) {
  242. // Windows will create the Gatt connection as needed. See:
  243. // https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-client#connecting-to-the-device
  244. }
  245. void BluetoothDeviceWin::DisconnectGatt() {
  246. // On Windows, the adapter cannot force a disconnection.
  247. }
  248. void BluetoothDeviceWin::SetVisible(bool visible) {
  249. visible_ = visible;
  250. }
  251. void BluetoothDeviceWin::UpdateServices(
  252. const BluetoothTaskManagerWin::DeviceState& device_state) {
  253. uuids_.clear();
  254. service_record_list_.clear();
  255. for (const auto& record_state : device_state.service_record_states) {
  256. auto service_record = std::make_unique<BluetoothServiceRecordWin>(
  257. device_state.address, record_state->name, record_state->sdp_bytes,
  258. record_state->gatt_uuid);
  259. uuids_.insert(service_record->uuid());
  260. service_record_list_.push_back(std::move(service_record));
  261. }
  262. if (!device_state.is_bluetooth_classic())
  263. UpdateGattServices(device_state.service_record_states);
  264. }
  265. bool BluetoothDeviceWin::IsGattServiceDiscovered(const BluetoothUUID& uuid,
  266. uint16_t attribute_handle) {
  267. for (const auto& gatt_service : gatt_services_) {
  268. uint16_t it_att_handle =
  269. static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second.get())
  270. ->GetAttributeHandle();
  271. BluetoothUUID it_uuid = gatt_service.second->GetUUID();
  272. if (attribute_handle == it_att_handle && uuid == it_uuid) {
  273. return true;
  274. }
  275. }
  276. return false;
  277. }
  278. bool BluetoothDeviceWin::DoesGattServiceExist(
  279. const std::vector<std::unique_ptr<
  280. BluetoothTaskManagerWin::ServiceRecordState>>& service_state,
  281. BluetoothRemoteGattService* service) {
  282. uint16_t attribute_handle =
  283. static_cast<BluetoothRemoteGattServiceWin*>(service)
  284. ->GetAttributeHandle();
  285. BluetoothUUID uuid = service->GetUUID();
  286. for (const auto& record_state : service_state) {
  287. if (attribute_handle == record_state->attribute_handle &&
  288. uuid == record_state->gatt_uuid) {
  289. return true;
  290. }
  291. }
  292. return false;
  293. }
  294. void BluetoothDeviceWin::UpdateGattServices(
  295. const std::vector<
  296. std::unique_ptr<BluetoothTaskManagerWin::ServiceRecordState>>&
  297. service_state) {
  298. // First, remove no longer existent GATT service.
  299. {
  300. std::vector<std::string> to_be_removed_services;
  301. for (const auto& gatt_service : gatt_services_) {
  302. if (!DoesGattServiceExist(service_state, gatt_service.second.get())) {
  303. to_be_removed_services.push_back(gatt_service.first);
  304. }
  305. }
  306. for (const auto& service : to_be_removed_services) {
  307. std::unique_ptr<BluetoothRemoteGattService> service_ptr =
  308. std::move(gatt_services_[service]);
  309. gatt_services_.erase(service);
  310. }
  311. // Update previously discovered services.
  312. for (const auto& gatt_service : gatt_services_) {
  313. static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second.get())
  314. ->Update();
  315. }
  316. }
  317. // Return if no new services have been added.
  318. if (gatt_services_.size() == service_state.size())
  319. return;
  320. // Add new services.
  321. for (const auto& record_state : service_state) {
  322. if (!IsGattServiceDiscovered(record_state->gatt_uuid,
  323. record_state->attribute_handle)) {
  324. BluetoothRemoteGattServiceWin* primary_service =
  325. new BluetoothRemoteGattServiceWin(
  326. this, record_state->path, record_state->gatt_uuid,
  327. record_state->attribute_handle, true, nullptr, ui_task_runner_);
  328. gatt_services_[primary_service->GetIdentifier()] =
  329. base::WrapUnique(primary_service);
  330. adapter_->NotifyGattServiceAdded(primary_service);
  331. }
  332. }
  333. }
  334. } // namespace device