Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 1 | //===-- AdbClient.cpp -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // Other libraries and framework includes |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 11 | #include "AdbClient.h" |
| 12 | |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
| 15 | #include "llvm/ADT/STLExtras.h" |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 16 | #include "llvm/Support/FileUtilities.h" |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 17 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 18 | #include "lldb/Core/DataBuffer.h" |
| 19 | #include "lldb/Core/DataBufferHeap.h" |
| 20 | #include "lldb/Core/DataEncoder.h" |
| 21 | #include "lldb/Core/DataExtractor.h" |
| 22 | #include "lldb/Core/StreamString.h" |
| 23 | #include "lldb/Host/ConnectionFileDescriptor.h" |
| 24 | #include "lldb/Host/FileSpec.h" |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 25 | |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 26 | #include <limits.h> |
| 27 | |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 29 | #include <fstream> |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 30 | #include <sstream> |
| 31 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 34 | using namespace lldb_private::platform_android; |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | |
Pavel Labath | 204ef66 | 2016-05-16 11:41:36 +0000 | [diff] [blame] | 38 | const std::chrono::seconds kReadTimeout(8); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 39 | const char * kOKAY = "OKAY"; |
| 40 | const char * kFAIL = "FAIL"; |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 41 | const char * kDATA = "DATA"; |
| 42 | const char * kDONE = "DONE"; |
| 43 | |
| 44 | const char * kSEND = "SEND"; |
| 45 | const char * kRECV = "RECV"; |
| 46 | const char * kSTAT = "STAT"; |
| 47 | |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 48 | const size_t kSyncPacketLen = 8; |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 49 | // Maximum size of a filesync DATA packet. |
| 50 | const size_t kMaxPushData = 2*1024; |
| 51 | // Default mode for pushed files. |
| 52 | const uint32_t kDefaultMode = 0100770; // S_IFREG | S_IRWXU | S_IRWXG |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 53 | |
Oleksiy Vyalov | e7df5f5 | 2015-11-03 01:37:01 +0000 | [diff] [blame] | 54 | const char * kSocketNamespaceAbstract = "localabstract"; |
| 55 | const char * kSocketNamespaceFileSystem = "localfilesystem"; |
| 56 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 57 | Error |
| 58 | ReadAllBytes (Connection &conn, void *buffer, size_t size) |
| 59 | { |
| 60 | using namespace std::chrono; |
| 61 | |
| 62 | Error error; |
| 63 | ConnectionStatus status; |
| 64 | char *read_buffer = static_cast<char*>(buffer); |
| 65 | |
| 66 | auto now = steady_clock::now(); |
| 67 | const auto deadline = now + kReadTimeout; |
| 68 | size_t total_read_bytes = 0; |
| 69 | while (total_read_bytes < size && now < deadline) |
| 70 | { |
| 71 | uint32_t timeout_usec = duration_cast<microseconds>(deadline - now).count(); |
| 72 | auto read_bytes = |
| 73 | conn.Read(read_buffer + total_read_bytes, size - total_read_bytes, timeout_usec, status, &error); |
| 74 | if (error.Fail ()) |
| 75 | return error; |
| 76 | total_read_bytes += read_bytes; |
| 77 | if (status != eConnectionStatusSuccess) |
| 78 | break; |
| 79 | now = steady_clock::now(); |
| 80 | } |
| 81 | if (total_read_bytes < size) |
| 82 | error = Error("Unable to read requested number of bytes. Connection status: %d.", status); |
| 83 | return error; |
| 84 | } |
| 85 | |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 86 | } // namespace |
| 87 | |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 88 | Error |
Chaoren Lin | 3ea689b | 2015-05-01 16:49:28 +0000 | [diff] [blame] | 89 | AdbClient::CreateByDeviceID(const std::string &device_id, AdbClient &adb) |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 90 | { |
| 91 | DeviceIDList connect_devices; |
Chaoren Lin | 3ea689b | 2015-05-01 16:49:28 +0000 | [diff] [blame] | 92 | auto error = adb.GetDevices(connect_devices); |
| 93 | if (error.Fail()) |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 94 | return error; |
| 95 | |
Chaoren Lin | 3ea689b | 2015-05-01 16:49:28 +0000 | [diff] [blame] | 96 | if (device_id.empty()) |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 97 | { |
Chaoren Lin | 3ea689b | 2015-05-01 16:49:28 +0000 | [diff] [blame] | 98 | if (connect_devices.size() != 1) |
| 99 | return Error("Expected a single connected device, got instead %" PRIu64, |
| 100 | static_cast<uint64_t>(connect_devices.size())); |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 101 | |
Chaoren Lin | 3ea689b | 2015-05-01 16:49:28 +0000 | [diff] [blame] | 102 | adb.SetDeviceID(connect_devices.front()); |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 103 | } |
| 104 | else |
| 105 | { |
Chaoren Lin | 3ea689b | 2015-05-01 16:49:28 +0000 | [diff] [blame] | 106 | auto find_it = std::find(connect_devices.begin(), connect_devices.end(), device_id); |
| 107 | if (find_it == connect_devices.end()) |
| 108 | return Error("Device \"%s\" not found", device_id.c_str()); |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 109 | |
Chaoren Lin | 3ea689b | 2015-05-01 16:49:28 +0000 | [diff] [blame] | 110 | adb.SetDeviceID(*find_it); |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 111 | } |
| 112 | return error; |
| 113 | } |
| 114 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 115 | AdbClient::AdbClient () {} |
| 116 | |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 117 | AdbClient::AdbClient (const std::string &device_id) |
| 118 | : m_device_id (device_id) |
| 119 | { |
| 120 | } |
| 121 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 122 | AdbClient::~AdbClient() {} |
| 123 | |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 124 | void |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 125 | AdbClient::SetDeviceID (const std::string &device_id) |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 126 | { |
| 127 | m_device_id = device_id; |
| 128 | } |
| 129 | |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 130 | const std::string& |
| 131 | AdbClient::GetDeviceID() const |
| 132 | { |
| 133 | return m_device_id; |
| 134 | } |
| 135 | |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 136 | Error |
| 137 | AdbClient::Connect () |
| 138 | { |
| 139 | Error error; |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 140 | m_conn.reset (new ConnectionFileDescriptor); |
| 141 | m_conn->Connect ("connect://localhost:5037", &error); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 142 | |
| 143 | return error; |
| 144 | } |
| 145 | |
| 146 | Error |
| 147 | AdbClient::GetDevices (DeviceIDList &device_list) |
| 148 | { |
| 149 | device_list.clear (); |
| 150 | |
| 151 | auto error = SendMessage ("host:devices"); |
| 152 | if (error.Fail ()) |
| 153 | return error; |
| 154 | |
| 155 | error = ReadResponseStatus (); |
| 156 | if (error.Fail ()) |
| 157 | return error; |
| 158 | |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 159 | std::vector<char> in_buffer; |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 160 | error = ReadMessage (in_buffer); |
| 161 | |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 162 | llvm::StringRef response (&in_buffer[0], in_buffer.size ()); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 163 | llvm::SmallVector<llvm::StringRef, 4> devices; |
| 164 | response.split (devices, "\n", -1, false); |
| 165 | |
| 166 | for (const auto device: devices) |
| 167 | device_list.push_back (device.split ('\t').first); |
| 168 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 169 | // Force disconnect since ADB closes connection after host:devices |
| 170 | // response is sent. |
| 171 | m_conn.reset (); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 172 | return error; |
| 173 | } |
| 174 | |
| 175 | Error |
Oleksiy Vyalov | e7eabbb | 2015-09-01 19:02:14 +0000 | [diff] [blame] | 176 | AdbClient::SetPortForwarding (const uint16_t local_port, const uint16_t remote_port) |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 177 | { |
| 178 | char message[48]; |
Oleksiy Vyalov | e7eabbb | 2015-09-01 19:02:14 +0000 | [diff] [blame] | 179 | snprintf (message, sizeof (message), "forward:tcp:%d;tcp:%d", local_port, remote_port); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 180 | |
| 181 | const auto error = SendDeviceMessage (message); |
| 182 | if (error.Fail ()) |
| 183 | return error; |
| 184 | |
| 185 | return ReadResponseStatus (); |
| 186 | } |
| 187 | |
| 188 | Error |
Oleksiy Vyalov | e7df5f5 | 2015-11-03 01:37:01 +0000 | [diff] [blame] | 189 | AdbClient::SetPortForwarding (const uint16_t local_port, |
| 190 | const char* remote_socket_name, |
| 191 | const UnixSocketNamespace socket_namespace) |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 192 | { |
| 193 | char message[PATH_MAX]; |
Oleksiy Vyalov | e7df5f5 | 2015-11-03 01:37:01 +0000 | [diff] [blame] | 194 | const char * sock_namespace_str = (socket_namespace == UnixSocketNamespaceAbstract) ? |
| 195 | kSocketNamespaceAbstract : kSocketNamespaceFileSystem; |
| 196 | snprintf (message, sizeof (message), "forward:tcp:%d;%s:%s", |
| 197 | local_port, |
| 198 | sock_namespace_str, |
| 199 | remote_socket_name); |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 200 | |
| 201 | const auto error = SendDeviceMessage (message); |
| 202 | if (error.Fail ()) |
| 203 | return error; |
| 204 | |
| 205 | return ReadResponseStatus (); |
| 206 | } |
| 207 | |
| 208 | Error |
Oleksiy Vyalov | e7eabbb | 2015-09-01 19:02:14 +0000 | [diff] [blame] | 209 | AdbClient::DeletePortForwarding (const uint16_t local_port) |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 210 | { |
| 211 | char message[32]; |
Oleksiy Vyalov | e7eabbb | 2015-09-01 19:02:14 +0000 | [diff] [blame] | 212 | snprintf (message, sizeof (message), "killforward:tcp:%d", local_port); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 213 | |
| 214 | const auto error = SendDeviceMessage (message); |
| 215 | if (error.Fail ()) |
| 216 | return error; |
| 217 | |
| 218 | return ReadResponseStatus (); |
| 219 | } |
| 220 | |
| 221 | Error |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 222 | AdbClient::SendMessage (const std::string &packet) |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 223 | { |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 224 | Error error; |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 225 | if (!m_conn || !m_conn->IsConnected()) |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 226 | { |
| 227 | error = Connect (); |
| 228 | if (error.Fail ()) |
| 229 | return error; |
| 230 | } |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 231 | |
| 232 | char length_buffer[5]; |
Oleksiy Vyalov | e17800c | 2015-04-10 03:59:52 +0000 | [diff] [blame] | 233 | snprintf (length_buffer, sizeof (length_buffer), "%04x", static_cast<int>(packet.size ())); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 234 | |
| 235 | ConnectionStatus status; |
| 236 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 237 | m_conn->Write (length_buffer, 4, status, &error); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 238 | if (error.Fail ()) |
| 239 | return error; |
| 240 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 241 | m_conn->Write (packet.c_str (), packet.size (), status, &error); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 242 | return error; |
| 243 | } |
| 244 | |
| 245 | Error |
| 246 | AdbClient::SendDeviceMessage (const std::string &packet) |
| 247 | { |
| 248 | std::ostringstream msg; |
| 249 | msg << "host-serial:" << m_device_id << ":" << packet; |
| 250 | return SendMessage (msg.str ()); |
| 251 | } |
| 252 | |
| 253 | Error |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 254 | AdbClient::ReadMessage (std::vector<char> &message) |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 255 | { |
| 256 | message.clear (); |
| 257 | |
| 258 | char buffer[5]; |
| 259 | buffer[4] = 0; |
| 260 | |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 261 | auto error = ReadAllBytes (buffer, 4); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 262 | if (error.Fail ()) |
| 263 | return error; |
| 264 | |
Vince Harron | d7e6a4f | 2015-05-13 00:25:54 +0000 | [diff] [blame] | 265 | unsigned int packet_len = 0; |
Oleksiy Vyalov | e17800c | 2015-04-10 03:59:52 +0000 | [diff] [blame] | 266 | sscanf (buffer, "%x", &packet_len); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 267 | |
| 268 | message.resize (packet_len, 0); |
| 269 | error = ReadAllBytes (&message[0], packet_len); |
| 270 | if (error.Fail ()) |
| 271 | message.clear (); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 272 | |
| 273 | return error; |
| 274 | } |
| 275 | |
| 276 | Error |
Tamas Berghammer | 9d8dde8 | 2015-09-29 11:04:18 +0000 | [diff] [blame] | 277 | AdbClient::ReadMessageStream (std::vector<char>& message, uint32_t timeout_ms) |
| 278 | { |
| 279 | auto start = std::chrono::steady_clock::now(); |
| 280 | message.clear(); |
| 281 | |
| 282 | Error error; |
| 283 | lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess; |
| 284 | char buffer[1024]; |
| 285 | while (error.Success() && status == lldb::eConnectionStatusSuccess) |
| 286 | { |
| 287 | auto end = std::chrono::steady_clock::now(); |
| 288 | uint32_t elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 289 | if (elapsed_time >= timeout_ms) |
| 290 | return Error("Timed out"); |
| 291 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 292 | size_t n = m_conn->Read(buffer, sizeof(buffer), 1000 * (timeout_ms - elapsed_time), status, &error); |
Tamas Berghammer | 9d8dde8 | 2015-09-29 11:04:18 +0000 | [diff] [blame] | 293 | if (n > 0) |
| 294 | message.insert(message.end(), &buffer[0], &buffer[n]); |
| 295 | } |
| 296 | return error; |
| 297 | } |
| 298 | |
| 299 | Error |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 300 | AdbClient::ReadResponseStatus() |
| 301 | { |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 302 | char response_id[5]; |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 303 | |
| 304 | static const size_t packet_len = 4; |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 305 | response_id[packet_len] = 0; |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 306 | |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 307 | auto error = ReadAllBytes (response_id, packet_len); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 308 | if (error.Fail ()) |
Oleksiy Vyalov | e17800c | 2015-04-10 03:59:52 +0000 | [diff] [blame] | 309 | return error; |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 310 | |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 311 | if (strncmp (response_id, kOKAY, packet_len) != 0) |
| 312 | return GetResponseError (response_id); |
| 313 | |
| 314 | return error; |
| 315 | } |
| 316 | |
| 317 | Error |
| 318 | AdbClient::GetResponseError (const char *response_id) |
| 319 | { |
| 320 | if (strcmp (response_id, kFAIL) != 0) |
| 321 | return Error ("Got unexpected response id from adb: \"%s\"", response_id); |
| 322 | |
| 323 | std::vector<char> error_message; |
| 324 | auto error = ReadMessage (error_message); |
| 325 | if (error.Success ()) |
| 326 | error.SetErrorString (std::string (&error_message[0], error_message.size ()).c_str ()); |
| 327 | |
| 328 | return error; |
| 329 | } |
| 330 | |
| 331 | Error |
| 332 | AdbClient::SwitchDeviceTransport () |
| 333 | { |
| 334 | std::ostringstream msg; |
| 335 | msg << "host:transport:" << m_device_id; |
| 336 | |
| 337 | auto error = SendMessage (msg.str ()); |
| 338 | if (error.Fail ()) |
| 339 | return error; |
| 340 | |
| 341 | return ReadResponseStatus (); |
| 342 | } |
| 343 | |
| 344 | Error |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 345 | AdbClient::StartSync () |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 346 | { |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 347 | auto error = SwitchDeviceTransport (); |
| 348 | if (error.Fail ()) |
| 349 | return Error ("Failed to switch to device transport: %s", error.AsCString ()); |
| 350 | |
| 351 | error = Sync (); |
| 352 | if (error.Fail ()) |
| 353 | return Error ("Sync failed: %s", error.AsCString ()); |
| 354 | |
| 355 | return error; |
| 356 | } |
| 357 | |
| 358 | Error |
| 359 | AdbClient::Sync () |
| 360 | { |
| 361 | auto error = SendMessage ("sync:"); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 362 | if (error.Fail ()) |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 363 | return error; |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 364 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 365 | return ReadResponseStatus (); |
| 366 | } |
| 367 | |
| 368 | Error |
| 369 | AdbClient::ReadAllBytes (void *buffer, size_t size) |
| 370 | { |
| 371 | return ::ReadAllBytes (*m_conn, buffer, size); |
| 372 | } |
| 373 | |
| 374 | Error |
| 375 | AdbClient::Shell (const char* command, uint32_t timeout_ms, std::string* output) |
| 376 | { |
| 377 | StreamString adb_command; |
| 378 | adb_command.Printf("shell:%s", command); |
| 379 | auto error = SendMessage (adb_command.GetData()); |
| 380 | if (error.Fail ()) |
| 381 | return error; |
| 382 | |
| 383 | error = ReadResponseStatus (); |
| 384 | if (error.Fail ()) |
| 385 | return error; |
| 386 | |
| 387 | std::vector<char> in_buffer; |
| 388 | error = ReadMessageStream (in_buffer, timeout_ms); |
| 389 | if (error.Fail()) |
| 390 | return error; |
| 391 | |
| 392 | if (output) |
| 393 | output->assign(in_buffer.begin(), in_buffer.end()); |
| 394 | return error; |
| 395 | } |
| 396 | |
| 397 | std::unique_ptr<AdbClient::SyncService> |
| 398 | AdbClient::GetSyncService (Error &error) |
| 399 | { |
| 400 | std::unique_ptr<SyncService> sync_service; |
| 401 | error = StartSync (); |
| 402 | if (error.Success ()) |
| 403 | sync_service.reset (new SyncService(std::move(m_conn))); |
| 404 | |
| 405 | return sync_service; |
| 406 | } |
| 407 | |
| 408 | Error |
| 409 | AdbClient::SyncService::PullFile (const FileSpec &remote_file, const FileSpec &local_file) |
| 410 | { |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 411 | const auto local_file_path = local_file.GetPath (); |
| 412 | llvm::FileRemover local_file_remover (local_file_path.c_str ()); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 413 | |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 414 | std::ofstream dst (local_file_path, std::ios::out | std::ios::binary); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 415 | if (!dst.is_open ()) |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 416 | return Error ("Unable to open local file %s", local_file_path.c_str()); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 417 | |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 418 | const auto remote_file_path = remote_file.GetPath (false); |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 419 | auto error = SendSyncRequest (kRECV, remote_file_path.length (), remote_file_path.c_str ()); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 420 | if (error.Fail ()) |
| 421 | return error; |
| 422 | |
| 423 | std::vector<char> chunk; |
| 424 | bool eof = false; |
| 425 | while (!eof) |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 426 | { |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 427 | error = PullFileChunk (chunk, eof); |
| 428 | if (error.Fail ()) |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 429 | return error; |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 430 | if (!eof) |
| 431 | dst.write (&chunk[0], chunk.size ()); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 434 | local_file_remover.releaseFile (); |
| 435 | return error; |
| 436 | } |
| 437 | |
| 438 | Error |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 439 | AdbClient::SyncService::PushFile (const FileSpec &local_file, const FileSpec &remote_file) |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 440 | { |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 441 | const auto local_file_path (local_file.GetPath ()); |
| 442 | std::ifstream src (local_file_path.c_str(), std::ios::in | std::ios::binary); |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 443 | if (!src.is_open ()) |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 444 | return Error ("Unable to open local file %s", local_file_path.c_str()); |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 445 | |
| 446 | std::stringstream file_description; |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 447 | file_description << remote_file.GetPath(false).c_str() << "," << kDefaultMode; |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 448 | std::string file_description_str = file_description.str(); |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 449 | auto error = SendSyncRequest (kSEND, file_description_str.length(), file_description_str.c_str()); |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 450 | if (error.Fail ()) |
| 451 | return error; |
| 452 | |
| 453 | char chunk[kMaxPushData]; |
| 454 | while (!src.eof() && !src.read(chunk, kMaxPushData).bad()) |
| 455 | { |
| 456 | size_t chunk_size = src.gcount(); |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 457 | error = SendSyncRequest(kDATA, chunk_size, chunk); |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 458 | if (error.Fail ()) |
| 459 | return Error ("Failed to send file chunk: %s", error.AsCString ()); |
| 460 | } |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 461 | error = SendSyncRequest(kDONE, local_file.GetModificationTime().seconds(), nullptr); |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 462 | if (error.Fail ()) |
| 463 | return error; |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 464 | |
| 465 | std::string response_id; |
Jason Molenda | d10f6aa | 2015-10-28 23:31:03 +0000 | [diff] [blame] | 466 | uint32_t data_len; |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 467 | error = ReadSyncHeader (response_id, data_len); |
| 468 | if (error.Fail ()) |
| 469 | return Error ("Failed to read DONE response: %s", error.AsCString ()); |
| 470 | if (response_id == kFAIL) |
| 471 | { |
| 472 | std::string error_message (data_len, 0); |
| 473 | error = ReadAllBytes (&error_message[0], data_len); |
| 474 | if (error.Fail ()) |
| 475 | return Error ("Failed to read DONE error message: %s", error.AsCString ()); |
| 476 | return Error ("Failed to push file: %s", error_message.c_str ()); |
| 477 | } |
| 478 | else if (response_id != kOKAY) |
| 479 | return Error ("Got unexpected DONE response: %s", response_id.c_str ()); |
| 480 | |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 481 | // If there was an error reading the source file, finish the adb file |
| 482 | // transfer first so that adb isn't expecting any more data. |
| 483 | if (src.bad()) |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 484 | return Error ("Failed read on %s", local_file_path.c_str()); |
| 485 | return error; |
| 486 | } |
| 487 | |
| 488 | Error |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 489 | AdbClient::SyncService::Stat (const FileSpec &remote_file, uint32_t &mode, uint32_t &size, uint32_t &mtime) |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 490 | { |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 491 | const std::string remote_file_path (remote_file.GetPath (false)); |
| 492 | auto error = SendSyncRequest (kSTAT, remote_file_path.length (), remote_file_path.c_str ()); |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 493 | if (error.Fail ()) |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 494 | return Error ("Failed to send request: %s", error.AsCString ()); |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 495 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 496 | static const size_t stat_len = strlen (kSTAT); |
| 497 | static const size_t response_len = stat_len + (sizeof (uint32_t) * 3); |
| 498 | |
| 499 | std::vector<char> buffer (response_len); |
| 500 | error = ReadAllBytes (&buffer[0], buffer.size ()); |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 501 | if (error.Fail ()) |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 502 | return Error ("Failed to read response: %s", error.AsCString ()); |
| 503 | |
| 504 | DataExtractor extractor (&buffer[0], buffer.size (), eByteOrderLittle, sizeof (void*)); |
| 505 | offset_t offset = 0; |
| 506 | |
| 507 | const void* command = extractor.GetData (&offset, stat_len); |
| 508 | if (!command) |
| 509 | return Error ("Failed to get response command"); |
| 510 | const char* command_str = static_cast<const char*> (command); |
| 511 | if (strncmp (command_str, kSTAT, stat_len)) |
| 512 | return Error ("Got invalid stat command: %s", command_str); |
| 513 | |
| 514 | mode = extractor.GetU32 (&offset); |
| 515 | size = extractor.GetU32 (&offset); |
| 516 | mtime = extractor.GetU32 (&offset); |
| 517 | return Error (); |
| 518 | } |
| 519 | |
| 520 | AdbClient::SyncService::SyncService(std::unique_ptr<Connection> &&conn): |
| 521 | m_conn(std::move(conn)) |
| 522 | { |
| 523 | } |
| 524 | |
| 525 | AdbClient::SyncService::~SyncService () {} |
| 526 | |
| 527 | Error |
| 528 | AdbClient::SyncService::SendSyncRequest (const char *request_id, const uint32_t data_len, const void *data) |
| 529 | { |
| 530 | const DataBufferSP data_sp (new DataBufferHeap (kSyncPacketLen, 0)); |
| 531 | DataEncoder encoder (data_sp, eByteOrderLittle, sizeof (void*)); |
| 532 | auto offset = encoder.PutData (0, request_id, strlen(request_id)); |
| 533 | encoder.PutU32 (offset, data_len); |
| 534 | |
| 535 | Error error; |
| 536 | ConnectionStatus status; |
| 537 | m_conn->Write (data_sp->GetBytes (), kSyncPacketLen, status, &error); |
| 538 | if (error.Fail ()) |
| 539 | return error; |
| 540 | |
| 541 | if (data) |
| 542 | m_conn->Write (data, data_len, status, &error); |
| 543 | return error; |
| 544 | } |
| 545 | |
| 546 | Error |
| 547 | AdbClient::SyncService::ReadSyncHeader (std::string &response_id, uint32_t &data_len) |
| 548 | { |
| 549 | char buffer[kSyncPacketLen]; |
| 550 | |
| 551 | auto error = ReadAllBytes (buffer, kSyncPacketLen); |
| 552 | if (error.Success ()) |
| 553 | { |
| 554 | response_id.assign (&buffer[0], 4); |
| 555 | DataExtractor extractor (&buffer[4], 4, eByteOrderLittle, sizeof (void*)); |
| 556 | offset_t offset = 0; |
| 557 | data_len = extractor.GetU32 (&offset); |
| 558 | } |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 559 | |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 560 | return error; |
| 561 | } |
| 562 | |
| 563 | Error |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 564 | AdbClient::SyncService::PullFileChunk (std::vector<char> &buffer, bool &eof) |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 565 | { |
| 566 | buffer.clear (); |
| 567 | |
| 568 | std::string response_id; |
| 569 | uint32_t data_len; |
| 570 | auto error = ReadSyncHeader (response_id, data_len); |
| 571 | if (error.Fail ()) |
| 572 | return error; |
| 573 | |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 574 | if (response_id == kDATA) |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 575 | { |
| 576 | buffer.resize (data_len, 0); |
| 577 | error = ReadAllBytes (&buffer[0], data_len); |
| 578 | if (error.Fail ()) |
| 579 | buffer.clear (); |
| 580 | } |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 581 | else if (response_id == kDONE) |
| 582 | { |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 583 | eof = true; |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 584 | } |
| 585 | else if (response_id == kFAIL) |
| 586 | { |
| 587 | std::string error_message (data_len, 0); |
| 588 | error = ReadAllBytes (&error_message[0], data_len); |
| 589 | if (error.Fail ()) |
| 590 | return Error ("Failed to read pull error message: %s", error.AsCString ()); |
| 591 | return Error ("Failed to pull file: %s", error_message.c_str ()); |
| 592 | } |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 593 | else |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 594 | return Error ("Pull failed with unknown response: %s", response_id.c_str ()); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 595 | |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 596 | return Error (); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | Error |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 600 | AdbClient::SyncService::ReadAllBytes (void *buffer, size_t size) |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 601 | { |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame^] | 602 | return ::ReadAllBytes (*m_conn, buffer, size); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 603 | } |
| 604 | |