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