OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "tools/android/forwarder2/host_controller.h" | 5 #include "tools/android/forwarder2/host_controller.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
15 #include "tools/android/forwarder2/command.h" | 15 #include "tools/android/forwarder2/command.h" |
16 #include "tools/android/forwarder2/forwarder.h" | 16 #include "tools/android/forwarder2/forwarder.h" |
17 #include "tools/android/forwarder2/socket.h" | 17 #include "tools/android/forwarder2/socket.h" |
18 | 18 |
19 namespace forwarder2 { | 19 namespace forwarder2 { |
20 | 20 |
21 // static | 21 // static |
22 std::unique_ptr<HostController> HostController::Create( | 22 std::unique_ptr<HostController> HostController::Create( |
| 23 const std::string& device_serial, |
23 int device_port, | 24 int device_port, |
24 int host_port, | 25 int host_port, |
25 int adb_port, | 26 int adb_port, |
26 int exit_notifier_fd, | 27 int exit_notifier_fd, |
27 const ErrorCallback& error_callback) { | 28 const ErrorCallback& error_callback) { |
28 std::unique_ptr<HostController> host_controller; | 29 std::unique_ptr<HostController> host_controller; |
29 std::unique_ptr<PipeNotifier> delete_controller_notifier(new PipeNotifier()); | 30 std::unique_ptr<PipeNotifier> delete_controller_notifier(new PipeNotifier()); |
30 std::unique_ptr<Socket> adb_control_socket(new Socket()); | 31 std::unique_ptr<Socket> adb_control_socket(new Socket()); |
31 adb_control_socket->AddEventFd(exit_notifier_fd); | 32 adb_control_socket->AddEventFd(exit_notifier_fd); |
32 adb_control_socket->AddEventFd(delete_controller_notifier->receiver_fd()); | 33 adb_control_socket->AddEventFd(delete_controller_notifier->receiver_fd()); |
33 if (!adb_control_socket->ConnectTcp(std::string(), adb_port)) { | 34 if (!adb_control_socket->ConnectTcp(std::string(), adb_port)) { |
34 LOG(ERROR) << "Could not connect HostController socket on port: " | 35 LOG(ERROR) << device_serial |
| 36 << ": Could not connect HostController socket on port: " |
35 << adb_port; | 37 << adb_port; |
36 return host_controller; | 38 return host_controller; |
37 } | 39 } |
38 // Send the command to the device start listening to the "device_forward_port" | 40 // Send the command to the device start listening to the "device_forward_port" |
39 bool send_command_success = SendCommand( | 41 bool send_command_success = SendCommand( |
40 command::LISTEN, device_port, adb_control_socket.get()); | 42 command::LISTEN, device_port, adb_control_socket.get()); |
41 CHECK(send_command_success); | 43 CHECK(send_command_success); |
42 int device_port_allocated; | 44 int device_port_allocated; |
43 command::Type command; | 45 command::Type command; |
44 if (!ReadCommand( | 46 if (!ReadCommand( |
45 adb_control_socket.get(), &device_port_allocated, &command) || | 47 adb_control_socket.get(), &device_port_allocated, &command) || |
46 command != command::BIND_SUCCESS) { | 48 command != command::BIND_SUCCESS) { |
47 LOG(ERROR) << "Device binding error using port " << device_port; | 49 LOG(ERROR) << device_serial |
| 50 << ": Device binding error using port " |
| 51 << device_port; |
48 return host_controller; | 52 return host_controller; |
49 } | 53 } |
50 host_controller.reset(new HostController( | 54 host_controller.reset(new HostController( |
51 device_port_allocated, host_port, adb_port, error_callback, | 55 device_serial, device_port_allocated, host_port, adb_port, error_callback, |
52 std::move(adb_control_socket), std::move(delete_controller_notifier))); | 56 std::move(adb_control_socket), std::move(delete_controller_notifier))); |
53 return host_controller; | 57 return host_controller; |
54 } | 58 } |
55 | 59 |
56 HostController::~HostController() { | 60 HostController::~HostController() { |
57 DCHECK(deletion_task_runner_->RunsTasksOnCurrentThread()); | 61 DCHECK(deletion_task_runner_->RunsTasksOnCurrentThread()); |
58 delete_controller_notifier_->Notify(); | 62 delete_controller_notifier_->Notify(); |
59 } | 63 } |
60 | 64 |
61 void HostController::Start() { | 65 void HostController::Start() { |
62 thread_.Start(); | 66 thread_.Start(); |
63 ReadNextCommandSoon(); | 67 ReadNextCommandSoon(); |
64 } | 68 } |
65 | 69 |
66 HostController::HostController( | 70 HostController::HostController( |
| 71 const std::string& device_serial, |
67 int device_port, | 72 int device_port, |
68 int host_port, | 73 int host_port, |
69 int adb_port, | 74 int adb_port, |
70 const ErrorCallback& error_callback, | 75 const ErrorCallback& error_callback, |
71 std::unique_ptr<Socket> adb_control_socket, | 76 std::unique_ptr<Socket> adb_control_socket, |
72 std::unique_ptr<PipeNotifier> delete_controller_notifier) | 77 std::unique_ptr<PipeNotifier> delete_controller_notifier) |
73 : self_deleter_helper_(this, error_callback), | 78 : self_deleter_helper_(this, error_callback), |
| 79 device_serial_(device_serial), |
74 device_port_(device_port), | 80 device_port_(device_port), |
75 host_port_(host_port), | 81 host_port_(host_port), |
76 adb_port_(adb_port), | 82 adb_port_(adb_port), |
77 adb_control_socket_(std::move(adb_control_socket)), | 83 adb_control_socket_(std::move(adb_control_socket)), |
78 delete_controller_notifier_(std::move(delete_controller_notifier)), | 84 delete_controller_notifier_(std::move(delete_controller_notifier)), |
79 deletion_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 85 deletion_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
80 thread_("HostControllerThread") {} | 86 thread_("HostControllerThread") {} |
81 | 87 |
82 void HostController::ReadNextCommandSoon() { | 88 void HostController::ReadNextCommandSoon() { |
83 thread_.task_runner()->PostTask( | 89 thread_.task_runner()->PostTask( |
84 FROM_HERE, | 90 FROM_HERE, |
85 base::Bind(&HostController::ReadCommandOnInternalThread, | 91 base::Bind(&HostController::ReadCommandOnInternalThread, |
86 base::Unretained(this))); | 92 base::Unretained(this))); |
87 } | 93 } |
88 | 94 |
89 void HostController::ReadCommandOnInternalThread() { | 95 void HostController::ReadCommandOnInternalThread() { |
90 if (!ReceivedCommand(command::ACCEPT_SUCCESS, adb_control_socket_.get())) { | 96 if (!ReceivedCommand(command::ACCEPT_SUCCESS, adb_control_socket_.get())) { |
91 LOG(ERROR) << "Did not receive ACCEPT_SUCCESS for port: " | 97 LOG(ERROR) << device_serial_ |
| 98 << ": Did not receive ACCEPT_SUCCESS for port: " |
92 << host_port_; | 99 << host_port_; |
93 OnInternalThreadError(); | 100 OnInternalThreadError(); |
94 return; | 101 return; |
95 } | 102 } |
96 // Try to connect to host server. | 103 // Try to connect to host server. |
97 std::unique_ptr<Socket> host_server_data_socket(new Socket()); | 104 std::unique_ptr<Socket> host_server_data_socket(new Socket()); |
98 if (!host_server_data_socket->ConnectTcp(std::string(), host_port_)) { | 105 if (!host_server_data_socket->ConnectTcp(std::string(), host_port_)) { |
99 LOG(ERROR) << "Could not Connect HostServerData socket on port: " | 106 LOG(ERROR) << device_serial_ |
| 107 << ": Could not Connect HostServerData socket on port: " |
100 << host_port_; | 108 << host_port_; |
101 SendCommand( | 109 SendCommand( |
102 command::HOST_SERVER_ERROR, device_port_, adb_control_socket_.get()); | 110 command::HOST_SERVER_ERROR, device_port_, adb_control_socket_.get()); |
103 if (ReceivedCommand(command::ACK, adb_control_socket_.get())) { | 111 if (ReceivedCommand(command::ACK, adb_control_socket_.get())) { |
104 // It can continue if the host forwarder could not connect to the host | 112 // It can continue if the host forwarder could not connect to the host |
105 // server but the device acknowledged that, so that the device could | 113 // server but the device acknowledged that, so that the device could |
106 // re-try later. | 114 // re-try later. |
107 ReadNextCommandSoon(); | 115 ReadNextCommandSoon(); |
108 return; | 116 return; |
109 } | 117 } |
110 OnInternalThreadError(); | 118 OnInternalThreadError(); |
111 return; | 119 return; |
112 } | 120 } |
113 LOG(INFO) << "Will send HOST_SERVER_SUCCESS: " << host_port_; | 121 LOG(INFO) << device_serial_ |
| 122 << ": Will send HOST_SERVER_SUCCESS: " |
| 123 << host_port_; |
114 SendCommand( | 124 SendCommand( |
115 command::HOST_SERVER_SUCCESS, device_port_, adb_control_socket_.get()); | 125 command::HOST_SERVER_SUCCESS, device_port_, adb_control_socket_.get()); |
116 StartForwarder(std::move(host_server_data_socket)); | 126 StartForwarder(std::move(host_server_data_socket)); |
117 ReadNextCommandSoon(); | 127 ReadNextCommandSoon(); |
118 } | 128 } |
119 | 129 |
120 void HostController::StartForwarder( | 130 void HostController::StartForwarder( |
121 std::unique_ptr<Socket> host_server_data_socket) { | 131 std::unique_ptr<Socket> host_server_data_socket) { |
122 std::unique_ptr<Socket> adb_data_socket(new Socket()); | 132 std::unique_ptr<Socket> adb_data_socket(new Socket()); |
123 if (!adb_data_socket->ConnectTcp("", adb_port_)) { | 133 if (!adb_data_socket->ConnectTcp("", adb_port_)) { |
124 LOG(ERROR) << "Could not connect AdbDataSocket on port: " << adb_port_; | 134 LOG(ERROR) << device_serial_ |
| 135 << ": Could not connect AdbDataSocket on port: " |
| 136 << adb_port_; |
125 OnInternalThreadError(); | 137 OnInternalThreadError(); |
126 return; | 138 return; |
127 } | 139 } |
128 // Open the Adb data connection, and send a command with the | 140 // Open the Adb data connection, and send a command with the |
129 // |device_forward_port| as a way for the device to identify the connection. | 141 // |device_forward_port| as a way for the device to identify the connection. |
130 SendCommand(command::DATA_CONNECTION, device_port_, adb_data_socket.get()); | 142 SendCommand(command::DATA_CONNECTION, device_port_, adb_data_socket.get()); |
131 | 143 |
132 // Check that the device received the new Adb Data Connection. Note that this | 144 // Check that the device received the new Adb Data Connection. Note that this |
133 // check is done through the |adb_control_socket_| that is handled in the | 145 // check is done through the |adb_control_socket_| that is handled in the |
134 // DeviceListener thread just after the call to WaitForAdbDataSocket(). | 146 // DeviceListener thread just after the call to WaitForAdbDataSocket(). |
135 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, | 147 if (!ReceivedCommand(command::ADB_DATA_SOCKET_SUCCESS, |
136 adb_control_socket_.get())) { | 148 adb_control_socket_.get())) { |
137 LOG(ERROR) << "Device could not handle the new Adb Data Connection."; | 149 LOG(ERROR) << device_serial_ |
| 150 << ": Device could not handle the new Adb Data Connection."; |
138 OnInternalThreadError(); | 151 OnInternalThreadError(); |
139 return; | 152 return; |
140 } | 153 } |
141 forwarders_manager_.CreateAndStartNewForwarder( | 154 forwarders_manager_.CreateAndStartNewForwarder( |
142 std::move(host_server_data_socket), std::move(adb_data_socket)); | 155 std::move(host_server_data_socket), std::move(adb_data_socket)); |
143 } | 156 } |
144 | 157 |
145 void HostController::OnInternalThreadError() { | 158 void HostController::OnInternalThreadError() { |
146 UnmapPortOnDevice(); | 159 UnmapPortOnDevice(); |
147 self_deleter_helper_.MaybeSelfDeleteSoon(); | 160 self_deleter_helper_.MaybeSelfDeleteSoon(); |
148 } | 161 } |
149 | 162 |
150 void HostController::UnmapPortOnDevice() { | 163 void HostController::UnmapPortOnDevice() { |
151 Socket socket; | 164 Socket socket; |
152 if (!socket.ConnectTcp("", adb_port_)) { | 165 if (!socket.ConnectTcp("", adb_port_)) { |
153 LOG(ERROR) << "Could not connect to device on port " << adb_port_; | 166 LOG(ERROR) << device_serial_ |
| 167 << ": Could not connect to device on port " |
| 168 << adb_port_; |
154 return; | 169 return; |
155 } | 170 } |
156 if (!SendCommand(command::UNLISTEN, device_port_, &socket)) { | 171 if (!SendCommand(command::UNLISTEN, device_port_, &socket)) { |
157 LOG(ERROR) << "Could not send unmap command for port " << device_port_; | 172 LOG(ERROR) << device_serial_ |
| 173 << ": Could not send unmap command for port " |
| 174 << device_port_; |
158 return; | 175 return; |
159 } | 176 } |
160 if (!ReceivedCommand(command::UNLISTEN_SUCCESS, &socket)) { | 177 if (!ReceivedCommand(command::UNLISTEN_SUCCESS, &socket)) { |
161 LOG(ERROR) << "Unamp command failed for port " << device_port_; | 178 LOG(ERROR) << device_serial_ |
| 179 << ": Unmap command failed for port " |
| 180 << device_port_; |
162 return; | 181 return; |
163 } | 182 } |
164 } | 183 } |
165 | 184 |
166 } // namespace forwarder2 | 185 } // namespace forwarder2 |
OLD | NEW |