bluetooth_discovery_manager_mac.mm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2014 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_discovery_manager_mac.h"
  5. #include "base/memory/raw_ptr.h"
  6. #import <IOBluetooth/objc/IOBluetoothDevice.h>
  7. #import <IOBluetooth/objc/IOBluetoothDeviceInquiry.h>
  8. #include "base/check_op.h"
  9. #include "base/logging.h"
  10. #include "base/mac/scoped_nsobject.h"
  11. namespace device {
  12. class BluetoothDiscoveryManagerMacClassic;
  13. } // namespace device
  14. // IOBluetoothDeviceInquiryDelegate implementation.
  15. @interface BluetoothDeviceInquiryDelegate
  16. : NSObject<IOBluetoothDeviceInquiryDelegate> {
  17. @private
  18. raw_ptr<device::BluetoothDiscoveryManagerMacClassic> _manager; // weak
  19. }
  20. - (instancetype)initWithManager:
  21. (device::BluetoothDiscoveryManagerMacClassic*)manager;
  22. @end
  23. namespace device {
  24. // Implementation of BluetoothDiscoveryManagerMac for Bluetooth classic device
  25. // discovery, using the IOBluetooth framework.
  26. class BluetoothDiscoveryManagerMacClassic
  27. : public BluetoothDiscoveryManagerMac {
  28. public:
  29. explicit BluetoothDiscoveryManagerMacClassic(Observer* observer)
  30. : BluetoothDiscoveryManagerMac(observer),
  31. should_do_discovery_(false),
  32. inquiry_running_(false),
  33. inquiry_delegate_(
  34. [[BluetoothDeviceInquiryDelegate alloc] initWithManager:this]),
  35. inquiry_([[IOBluetoothDeviceInquiry alloc]
  36. initWithDelegate:inquiry_delegate_]) {}
  37. BluetoothDiscoveryManagerMacClassic(
  38. const BluetoothDiscoveryManagerMacClassic&) = delete;
  39. BluetoothDiscoveryManagerMacClassic& operator=(
  40. const BluetoothDiscoveryManagerMacClassic&) = delete;
  41. ~BluetoothDiscoveryManagerMacClassic() override {}
  42. // BluetoothDiscoveryManagerMac override.
  43. bool IsDiscovering() const override { return should_do_discovery_; }
  44. // BluetoothDiscoveryManagerMac override.
  45. bool StartDiscovery() override {
  46. DVLOG(1) << "Bluetooth Classic: StartDiscovery";
  47. DCHECK(!should_do_discovery_);
  48. DVLOG(1) << "Discovery requested";
  49. should_do_discovery_ = true;
  50. // Clean the cache so that new discovery sessions find previously
  51. // discovered devices as well.
  52. [inquiry_ clearFoundDevices];
  53. if (inquiry_running_) {
  54. DVLOG(1) << "Device inquiry already running";
  55. return true;
  56. }
  57. DVLOG(1) << "Requesting to start device inquiry";
  58. if ([inquiry_ start] != kIOReturnSuccess) {
  59. DVLOG(1) << "Failed to start device inquiry";
  60. // Set |should_do_discovery_| to false here. Since we're reporting an
  61. // error, we're indicating that the adapter call StartDiscovery again
  62. // if needed.
  63. should_do_discovery_ = false;
  64. return false;
  65. }
  66. DVLOG(1) << "Device inquiry start was successful";
  67. return true;
  68. }
  69. // BluetoothDiscoveryManagerMac override.
  70. bool StopDiscovery() override {
  71. DVLOG(1) << "Bluetooth Classic: StopDiscovery";
  72. DCHECK(should_do_discovery_);
  73. should_do_discovery_ = false;
  74. if (!inquiry_running_) {
  75. DVLOG(1) << "No device inquiry running; discovery stopped";
  76. return true;
  77. }
  78. DVLOG(1) << "Requesting to stop device inquiry";
  79. IOReturn status = [inquiry_ stop];
  80. if (status == kIOReturnSuccess) {
  81. DVLOG(1) << "Device inquiry stop was successful";
  82. return true;
  83. }
  84. if (status == kIOReturnNotPermitted) {
  85. DVLOG(1) << "Device inquiry was already stopped";
  86. return true;
  87. }
  88. LOG(WARNING) << "Failed to stop device inquiry";
  89. return false;
  90. }
  91. // Called by BluetoothDeviceInquiryDelegate.
  92. void DeviceInquiryStarted(IOBluetoothDeviceInquiry* inquiry) {
  93. DCHECK(!inquiry_running_);
  94. DVLOG(1) << "Device inquiry started!";
  95. // If discovery was requested to stop in the mean time, stop the inquiry.
  96. if (!should_do_discovery_) {
  97. DVLOG(1) << "Discovery stop was requested earlier. Stopping inquiry";
  98. [inquiry stop];
  99. return;
  100. }
  101. inquiry_running_ = true;
  102. }
  103. void DeviceFound(IOBluetoothDeviceInquiry* inquiry,
  104. IOBluetoothDevice* device) {
  105. DCHECK(observer_);
  106. observer_->ClassicDeviceFound(device);
  107. }
  108. void DeviceInquiryComplete(IOBluetoothDeviceInquiry* inquiry,
  109. IOReturn error,
  110. bool aborted) {
  111. DCHECK_EQ(inquiry_, inquiry);
  112. DCHECK(observer_);
  113. DVLOG(1) << "Device inquiry complete";
  114. inquiry_running_ = false;
  115. // If discovery is no longer desired, notify observers that discovery
  116. // has stopped and return.
  117. if (!should_do_discovery_) {
  118. observer_->ClassicDiscoveryStopped(false /* unexpected */);
  119. return;
  120. }
  121. // If discovery has stopped due to an unexpected reason, notify the
  122. // observers and return.
  123. if (error != kIOReturnSuccess) {
  124. DVLOG(1) << "Inquiry has stopped with an error: " << error;
  125. should_do_discovery_ = false;
  126. observer_->ClassicDiscoveryStopped(true /* unexpected */);
  127. return;
  128. }
  129. DVLOG(1) << "Restarting device inquiry";
  130. if ([inquiry_ start] == kIOReturnSuccess) {
  131. DVLOG(1) << "Device inquiry restart was successful";
  132. return;
  133. }
  134. DVLOG(1) << "Failed to restart discovery";
  135. should_do_discovery_ = false;
  136. DCHECK(observer_);
  137. observer_->ClassicDiscoveryStopped(true /* unexpected */);
  138. }
  139. private:
  140. // The requested discovery state.
  141. bool should_do_discovery_;
  142. // The current inquiry state.
  143. bool inquiry_running_;
  144. // Objective-C objects for running and tracking device inquiry.
  145. base::scoped_nsobject<BluetoothDeviceInquiryDelegate> inquiry_delegate_;
  146. base::scoped_nsobject<IOBluetoothDeviceInquiry> inquiry_;
  147. };
  148. BluetoothDiscoveryManagerMac::BluetoothDiscoveryManagerMac(
  149. Observer* observer) : observer_(observer) {
  150. DCHECK(observer);
  151. }
  152. BluetoothDiscoveryManagerMac::~BluetoothDiscoveryManagerMac() {
  153. }
  154. // static
  155. BluetoothDiscoveryManagerMac* BluetoothDiscoveryManagerMac::CreateClassic(
  156. Observer* observer) {
  157. return new BluetoothDiscoveryManagerMacClassic(observer);
  158. }
  159. } // namespace device
  160. @implementation BluetoothDeviceInquiryDelegate
  161. - (instancetype)initWithManager:
  162. (device::BluetoothDiscoveryManagerMacClassic*)manager {
  163. if ((self = [super init]))
  164. _manager = manager;
  165. return self;
  166. }
  167. - (void)deviceInquiryStarted:(IOBluetoothDeviceInquiry*)sender {
  168. _manager->DeviceInquiryStarted(sender);
  169. }
  170. - (void)deviceInquiryDeviceFound:(IOBluetoothDeviceInquiry*)sender
  171. device:(IOBluetoothDevice*)device {
  172. _manager->DeviceFound(sender, device);
  173. }
  174. - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender
  175. error:(IOReturn)error
  176. aborted:(BOOL)aborted {
  177. _manager->DeviceInquiryComplete(sender, error, aborted);
  178. }
  179. @end