Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(963)

Side by Side Diff: device/bluetooth/bluetooth_profile_win.cc

Issue 236203018: win: Implement Bluetooth server. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « device/bluetooth/bluetooth_profile_win.h ('k') | device/bluetooth/bluetooth_socket_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/bluetooth/bluetooth_profile_win.h" 5 #include "device/bluetooth/bluetooth_profile_win.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h"
8 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
9 #include "base/sequenced_task_runner.h" 10 #include "base/sequenced_task_runner.h"
11 #include "base/strings/stringprintf.h"
10 #include "device/bluetooth/bluetooth_adapter_factory.h" 12 #include "device/bluetooth/bluetooth_adapter_factory.h"
13 #include "device/bluetooth/bluetooth_adapter_win.h"
11 #include "device/bluetooth/bluetooth_device_win.h" 14 #include "device/bluetooth/bluetooth_device_win.h"
12 #include "device/bluetooth/bluetooth_service_record.h" 15 #include "device/bluetooth/bluetooth_service_record.h"
13 #include "device/bluetooth/bluetooth_socket_thread_win.h" 16 #include "device/bluetooth/bluetooth_socket_thread_win.h"
14 #include "device/bluetooth/bluetooth_socket_win.h" 17 #include "device/bluetooth/bluetooth_socket_win.h"
15 18
16 namespace { 19 namespace {
17 20
18 using device::BluetoothAdapter; 21 using device::BluetoothAdapter;
19 using device::BluetoothDevice; 22 using device::BluetoothDevice;
20 using device::BluetoothProfileWin; 23 using device::BluetoothProfileWin;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 socket)); 58 socket));
56 } 59 }
57 60
58 void OnConnectErrorUI(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, 61 void OnConnectErrorUI(scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
59 const BluetoothProfileWin::ErrorCallback& error_callback, 62 const BluetoothProfileWin::ErrorCallback& error_callback,
60 const std::string& error) { 63 const std::string& error) {
61 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); 64 DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
62 error_callback.Run(error); 65 error_callback.Run(error);
63 } 66 }
64 67
68 std::string IPEndPointToBluetoothAddress(const net::IPEndPoint& end_point) {
69 if (end_point.address().size() != net::kBluetoothAddressSize)
70 return std::string();
71 // The address is copied from BTH_ADDR field of SOCKADDR_BTH, which is a
72 // 64-bit ULONGLONG that stores Bluetooth address in little-endian. Print in
73 // reverse order to preserve the correct ordering.
74 return base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X",
75 end_point.address()[5],
76 end_point.address()[4],
77 end_point.address()[3],
78 end_point.address()[2],
79 end_point.address()[1],
80 end_point.address()[0]);
81 }
82
65 } // namespace 83 } // namespace
66 84
67 namespace device { 85 namespace device {
68 86
69 BluetoothProfileWin::BluetoothProfileWin(const BluetoothUUID& uuid, 87 BluetoothProfileWin::BluetoothProfileWin()
70 const std::string& name) 88 : BluetoothProfile(), rfcomm_channel_(0), weak_ptr_factory_(this) {
71 : BluetoothProfile(), uuid_(uuid), name_(name) {
72 } 89 }
73 90
74 BluetoothProfileWin::~BluetoothProfileWin() { 91 BluetoothProfileWin::~BluetoothProfileWin() {
75 } 92 }
76 93
77 void BluetoothProfileWin::Unregister() { 94 void BluetoothProfileWin::Unregister() {
95 if (profile_socket_)
96 profile_socket_->Close();
97
78 delete this; 98 delete this;
79 } 99 }
80 100
81 void BluetoothProfileWin::SetConnectionCallback( 101 void BluetoothProfileWin::SetConnectionCallback(
82 const ConnectionCallback& callback) { 102 const ConnectionCallback& callback) {
83 connection_callback_ = callback; 103 connection_callback_ = callback;
84 } 104 }
85 105
106 void BluetoothProfileWin::Init(const BluetoothUUID& uuid,
107 const BluetoothProfile::Options& options,
108 const ProfileCallback& callback) {
109 uuid_ = uuid;
110 name_ = options.name;
111 rfcomm_channel_ = options.channel;
112
113 BluetoothAdapterFactory::GetAdapter(
114 base::Bind(&BluetoothProfileWin::OnGetAdapter,
115 weak_ptr_factory_.GetWeakPtr(),
116 callback));
117 }
118
86 void BluetoothProfileWin::Connect( 119 void BluetoothProfileWin::Connect(
87 const BluetoothDeviceWin* device, 120 const BluetoothDeviceWin* device,
88 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, 121 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
89 scoped_refptr<BluetoothSocketThreadWin> socket_thread, 122 scoped_refptr<BluetoothSocketThreadWin> socket_thread,
90 net::NetLog* net_log, 123 net::NetLog* net_log,
91 const net::NetLog::Source& source, 124 const net::NetLog::Source& source,
92 const base::Closure& success_callback, 125 const base::Closure& success_callback,
93 const ErrorCallback& error_callback) { 126 const ErrorCallback& error_callback) {
94 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); 127 DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
95 if (connection_callback_.is_null()) { 128 if (connection_callback_.is_null()) {
96 error_callback.Run(kNoConnectionCallback); 129 error_callback.Run(kNoConnectionCallback);
97 return; 130 return;
98 } 131 }
99 132
100 const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_); 133 const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_);
101 if (!record) { 134 if (!record) {
102 error_callback.Run(kProfileNotFound); 135 error_callback.Run(kProfileNotFound);
103 return; 136 return;
104 } 137 }
105 138
106 scoped_refptr<BluetoothSocketWin> socket( 139 scoped_refptr<BluetoothSocketWin> socket(
107 BluetoothSocketWin::CreateBluetoothSocket( 140 BluetoothSocketWin::CreateBluetoothSocket(
108 *record, ui_task_runner, socket_thread, net_log, source)); 141 ui_task_runner, socket_thread, net_log, source));
109 142
110 socket->Connect(base::Bind(&OnConnectSuccessUI, 143 socket->Connect(*record,
144 base::Bind(&OnConnectSuccessUI,
111 ui_task_runner, 145 ui_task_runner,
112 success_callback, 146 success_callback,
113 connection_callback_, 147 connection_callback_,
114 device->GetAddress(), 148 device->GetAddress(),
115 socket), 149 socket),
116 error_callback); 150 error_callback);
117 } 151 }
118 152
153 void BluetoothProfileWin::OnGetAdapter(
154 const ProfileCallback& callback,
155 scoped_refptr<BluetoothAdapter> in_adapter) {
156 DCHECK(!adapter_);
157 DCHECK(!profile_socket_);
158
159 adapter_ = in_adapter;
160 profile_socket_ = BluetoothSocketWin::CreateBluetoothSocket(
161 adapter()->ui_task_runner(),
162 adapter()->socket_thread(),
163 NULL,
164 net::NetLog::Source());
165 profile_socket_->StartService(
166 uuid_,
167 name_,
168 rfcomm_channel_,
169 base::Bind(&BluetoothProfileWin::OnRegisterProfileSuccess,
170 weak_ptr_factory_.GetWeakPtr(),
171 callback),
172 base::Bind(&BluetoothProfileWin::OnRegisterProfileError,
173 weak_ptr_factory_.GetWeakPtr(),
174 callback),
175 base::Bind(&BluetoothProfileWin::OnNewConnection,
176 weak_ptr_factory_.GetWeakPtr()));
177 }
178
179 void BluetoothProfileWin::OnRegisterProfileSuccess(
180 const ProfileCallback& callback) {
181 callback.Run(this);
182 }
183
184 void BluetoothProfileWin::OnRegisterProfileError(
185 const ProfileCallback& callback,
186 const std::string& error_message) {
187 callback.Run(NULL);
188 delete this;
189 }
190
191 void BluetoothProfileWin::OnNewConnection(
192 scoped_refptr<BluetoothSocketWin> connected,
193 const net::IPEndPoint& peer_address) {
194 DCHECK(adapter()->ui_task_runner()->RunsTasksOnCurrentThread());
195 if (connection_callback_.is_null())
196 return;
197
198 std::string device_address = IPEndPointToBluetoothAddress(peer_address);
199 if (device_address.empty()) {
200 LOG(WARNING) << "Failed to accept connection for profile "
201 << "uuid=" << uuid_.value()
202 << ", unexpected peer device address.";
203 return;
204 }
205
206 BluetoothDevice* device = adapter_->GetDevice(device_address);
207 if (!device) {
208 LOG(WARNING) << "Failed to accept connection for profile"
209 << ",uuid=" << uuid_.value()
210 << ", unknown device=" << device_address;
211 return;
212 }
213
214 connection_callback_.Run(device, connected);
215 }
216
217 BluetoothAdapterWin* BluetoothProfileWin::adapter() const {
218 DCHECK(adapter_);
219 return static_cast<BluetoothAdapterWin*>(adapter_.get());
220 }
221
119 } // namespace device 222 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_profile_win.h ('k') | device/bluetooth/bluetooth_socket_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698