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