OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 | |
5 #include "device/bluetooth/bluetooth_profile_experimental_chromeos.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/bind.h" | |
11 #include "base/callback.h" | |
12 #include "base/logging.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/string_util.h" | |
15 #include "chromeos/dbus/dbus_thread_manager.h" | |
16 #include "chromeos/dbus/experimental_bluetooth_profile_manager_client.h" | |
17 #include "chromeos/dbus/experimental_bluetooth_profile_service_provider.h" | |
18 #include "dbus/bus.h" | |
19 #include "dbus/file_descriptor.h" | |
20 #include "dbus/object_path.h" | |
21 #include "device/bluetooth/bluetooth_profile.h" | |
22 #include "device/bluetooth/bluetooth_socket.h" | |
23 #include "device/bluetooth/bluetooth_socket_experimental_chromeos.h" | |
24 | |
25 using device::BluetoothProfile; | |
26 using device::BluetoothSocket; | |
27 | |
28 namespace chromeos { | |
29 | |
30 BluetoothProfileExperimentalChromeOS::BluetoothProfileExperimentalChromeOS() | |
31 : weak_ptr_factory_(this) { | |
32 | |
33 } | |
34 | |
35 BluetoothProfileExperimentalChromeOS::~BluetoothProfileExperimentalChromeOS() { | |
36 DCHECK(object_path_.value().empty()); | |
37 DCHECK(profile_.get() == NULL); | |
38 } | |
39 | |
40 void BluetoothProfileExperimentalChromeOS::Init( | |
41 const std::string& uuid, | |
42 const device::BluetoothProfile::Options& options, | |
43 const ProfileCallback& callback) { | |
44 DCHECK(object_path_.value().empty()); | |
45 DCHECK(profile_.get() == NULL); | |
46 | |
47 ExperimentalBluetoothProfileManagerClient::Options bluetooth_options; | |
48 bluetooth_options.name = options.name; | |
49 bluetooth_options.service = uuid; | |
50 bluetooth_options.channel = options.channel; | |
51 bluetooth_options.psm = options.psm; | |
52 bluetooth_options.require_authentication = options.require_authentication; | |
53 bluetooth_options.require_authorization = options.require_authorization; | |
54 bluetooth_options.auto_connect = options.auto_connect; | |
55 bluetooth_options.version = options.version; | |
56 bluetooth_options.features = options.features; | |
57 | |
58 // The object path is relatively meaningless, but has to be unique, so we | |
59 // use the UUID of the profile. | |
60 std::string uuid_path; | |
61 ReplaceChars(uuid, ":", "_", &uuid_path); | |
62 | |
63 object_path_ = dbus::ObjectPath("/org/chromium/bluetooth_profile/" + | |
64 uuid_path); | |
65 | |
66 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); | |
67 profile_.reset(ExperimentalBluetoothProfileServiceProvider::Create( | |
68 system_bus, object_path_, this)); | |
69 DCHECK(profile_.get()); | |
70 | |
71 VLOG(1) << object_path_.value() << ": Register profile"; | |
72 DBusThreadManager::Get()->GetExperimentalBluetoothProfileManagerClient()-> | |
73 RegisterProfile( | |
74 object_path_, | |
75 uuid, | |
76 bluetooth_options, | |
77 base::Bind( | |
78 &BluetoothProfileExperimentalChromeOS::OnRegisterProfile, | |
79 weak_ptr_factory_.GetWeakPtr(), | |
80 callback), | |
81 base::Bind( | |
82 &BluetoothProfileExperimentalChromeOS::OnRegisterProfileError, | |
83 weak_ptr_factory_.GetWeakPtr(), | |
84 callback)); | |
85 } | |
86 | |
87 void BluetoothProfileExperimentalChromeOS::Unregister() { | |
88 DCHECK(!object_path_.value().empty()); | |
89 DCHECK(profile_.get()); | |
90 | |
91 profile_.reset(); | |
92 | |
93 VLOG(1) << object_path_.value() << ": Unregister profile"; | |
94 DBusThreadManager::Get()->GetExperimentalBluetoothProfileManagerClient()-> | |
95 UnregisterProfile( | |
96 object_path_, | |
97 base::Bind( | |
98 &BluetoothProfileExperimentalChromeOS::OnUnregisterProfile, | |
99 weak_ptr_factory_.GetWeakPtr()), | |
100 base::Bind( | |
101 &BluetoothProfileExperimentalChromeOS::OnUnregisterProfileError, | |
102 weak_ptr_factory_.GetWeakPtr())); | |
103 } | |
104 | |
105 void BluetoothProfileExperimentalChromeOS::SetConnectionCallback( | |
106 const SocketCallback& callback) { | |
107 socket_callback_ = callback; | |
108 } | |
109 | |
110 void BluetoothProfileExperimentalChromeOS::Release() { | |
youngki
2013/04/25 14:34:34
Add TODO to implement this method.
keybuk
2013/04/25 17:16:41
This IS the implementation ;) while we have to pro
| |
111 VLOG(1) << object_path_.value() << ": Release"; | |
112 } | |
113 | |
114 void BluetoothProfileExperimentalChromeOS::NewConnection( | |
115 const dbus::ObjectPath& device_path, | |
116 dbus::FileDescriptor* fd, | |
117 const ExperimentalBluetoothProfileServiceProvider::Delegate::Options& | |
118 ptions, | |
119 const ConfirmationCallback& callback) { | |
120 VLOG(1) << object_path_.value() << ": New connection from device: " | |
121 << device_path.value();; | |
122 if (socket_callback_.is_null()) { | |
123 callback.Run(REJECTED); | |
124 return; | |
125 } | |
126 | |
127 callback.Run(SUCCESS); | |
128 | |
129 // TODO(keybuk): look up the device | |
130 | |
131 scoped_refptr<BluetoothSocket> socket(( | |
132 BluetoothSocketExperimentalChromeOS::Create(fd))); | |
133 socket_callback_.Run(socket); | |
134 } | |
135 | |
136 void BluetoothProfileExperimentalChromeOS::RequestDisconnection( | |
youngki
2013/04/25 14:34:34
Add TODO to implement this method.
keybuk
2013/04/25 17:16:41
As above, this IS the implementation - we don't ha
| |
137 const dbus::ObjectPath& device_path, | |
138 const ConfirmationCallback& callback) { | |
139 VLOG(1) << object_path_.value() << ": Request disconnection"; | |
140 callback.Run(SUCCESS); | |
141 } | |
142 | |
143 void BluetoothProfileExperimentalChromeOS::Cancel() { | |
youngki
2013/04/25 14:34:34
Add TODO to implement this method.
keybuk
2013/04/25 17:16:41
Also this IS the implementation, the method is req
| |
144 VLOG(1) << object_path_.value() << ": Cancel"; | |
145 } | |
146 | |
147 void BluetoothProfileExperimentalChromeOS::OnRegisterProfile( | |
148 const ProfileCallback& callback) { | |
149 VLOG(1) << object_path_.value() << ": Profile registered"; | |
150 callback.Run(this); | |
151 } | |
152 | |
153 void BluetoothProfileExperimentalChromeOS::OnRegisterProfileError( | |
154 const ProfileCallback& callback, | |
155 const std::string& error_name, | |
156 const std::string& error_message) { | |
157 LOG(WARNING) << object_path_.value() << ": Failed to register profile: " | |
158 << error_name << ": " << error_message; | |
159 callback.Run(NULL); | |
160 | |
161 Unregister(); | |
162 } | |
163 | |
164 void BluetoothProfileExperimentalChromeOS::OnUnregisterProfile() { | |
165 VLOG(1) << object_path_.value() << ": Profile unregistered"; | |
166 object_path_ = dbus::ObjectPath(""); | |
167 delete this; | |
168 } | |
169 | |
170 void BluetoothProfileExperimentalChromeOS::OnUnregisterProfileError( | |
171 const std::string& error_name, | |
172 const std::string& error_message) { | |
173 LOG(WARNING) << object_path_.value() << ": Failed to unregister profile: " | |
174 << error_name << ": " << error_message; | |
175 object_path_ = dbus::ObjectPath(""); | |
176 delete this; | |
177 } | |
178 | |
179 } // namespace chromeos | |
OLD | NEW |