blob: 1ccff921c8cdb2b8075e69e622351c398115e4cf [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albert76649012015-02-24 15:51:19 -080018
Dan Albert33134262015-03-19 15:21:08 -070019#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080020#include "transport.h"
21
Dan Albert055f1aa2015-02-20 17:24:58 -080022#include <ctype.h>
Dan Albert76649012015-02-24 15:51:19 -080023#include <errno.h>
Josh Gaob122b172017-08-16 16:57:01 -070024#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdio.h>
26#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080028#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Spencer Low363af562015-11-07 18:51:54 -080030#include <algorithm>
Josh Gao0bbf69c2018-02-16 13:24:58 -080031#include <deque>
Dan Albertc7915a32015-05-18 16:46:31 -070032#include <list>
Josh Gao0cd3ae12016-09-21 12:37:10 -070033#include <mutex>
Josh Gaoe1dacfc2017-04-12 17:00:49 -070034#include <thread>
Dan Albertc7915a32015-05-18 16:46:31 -070035
Elliott Hughes4f713192015-12-04 22:00:26 -080036#include <android-base/logging.h>
David Pursell3f902aa2016-03-01 08:58:26 -080037#include <android-base/parsenetaddress.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080038#include <android-base/stringprintf.h>
39#include <android-base/strings.h>
Josh Gaob122b172017-08-16 16:57:01 -070040#include <android-base/thread_annotations.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070041
Josh Gao27768452018-01-02 12:01:43 -080042#include <diagnose_usb.h>
43
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#include "adb.h"
Elliott Hughes0aeb5052016-06-29 17:42:01 -070045#include "adb_auth.h"
Josh Gaob800d882018-01-28 20:32:46 -080046#include "adb_io.h"
Josh Gaocfe72e22016-11-29 09:40:29 -080047#include "adb_trace.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070048#include "adb_utils.h"
Yabin Cuib5e11412017-03-10 16:01:01 -080049#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
51static void transport_unref(atransport *t);
52
Josh Gaob122b172017-08-16 16:57:01 -070053// TODO: unordered_map<TransportId, atransport*>
Josh Gaob7b1edf2015-11-11 17:56:12 -080054static auto& transport_list = *new std::list<atransport*>();
55static auto& pending_list = *new std::list<atransport*>();
Benoit Goby1c45ee92013-03-29 18:22:36 -070056
Josh Gao1db71af2017-08-17 13:50:51 -070057static auto& transport_lock = *new std::recursive_mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Todd Kennedy51c05ec2015-11-10 00:03:25 +000059const char* const kFeatureShell2 = "shell_v2";
60const char* const kFeatureCmd = "cmd";
Josh Gao5a1e3fd2016-12-05 17:11:34 -080061const char* const kFeatureStat2 = "stat_v2";
Josh Gao5d1756c2017-02-22 17:07:01 -080062const char* const kFeatureLibusb = "libusb";
Dan Albert5176df82017-05-23 14:30:00 -070063const char* const kFeaturePushSync = "push_sync";
Todd Kennedy51c05ec2015-11-10 00:03:25 +000064
Josh Gaob122b172017-08-16 16:57:01 -070065TransportId NextTransportId() {
66 static std::atomic<TransportId> next(1);
67 return next++;
68}
69
Josh Gao0bbf69c2018-02-16 13:24:58 -080070BlockingConnectionAdapter::BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection)
71 : underlying_(std::move(connection)) {}
72
73BlockingConnectionAdapter::~BlockingConnectionAdapter() {
74 LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): destructing";
75 Stop();
76}
77
78void BlockingConnectionAdapter::Start() {
79 read_thread_ = std::thread([this]() {
80 LOG(INFO) << this->transport_name_ << ": read thread spawning";
81 while (true) {
82 std::unique_ptr<apacket> packet(new apacket());
83 if (!underlying_->Read(packet.get())) {
84 PLOG(INFO) << this->transport_name_ << ": read failed";
85 break;
86 }
87 read_callback_(this, std::move(packet));
88 }
89 std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "read failed"); });
90 });
91
92 write_thread_ = std::thread([this]() {
93 LOG(INFO) << this->transport_name_ << ": write thread spawning";
94 while (true) {
95 std::unique_lock<std::mutex> lock(mutex_);
96 cv_.wait(lock, [this]() { return this->stopped_ || !this->write_queue_.empty(); });
97
98 if (this->stopped_) {
99 return;
100 }
101
102 std::unique_ptr<apacket> packet = std::move(this->write_queue_.front());
103 this->write_queue_.pop_front();
104 lock.unlock();
105
106 if (!this->underlying_->Write(packet.get())) {
107 break;
108 }
109 }
110 std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "write failed"); });
111 });
112}
113
114void BlockingConnectionAdapter::Stop() {
115 std::unique_lock<std::mutex> lock(mutex_);
116 if (stopped_) {
117 LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): already stopped";
118 return;
119 }
120
121 stopped_ = true;
122 lock.unlock();
123
124 LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopping";
125
126 this->underlying_->Close();
127
128 this->cv_.notify_one();
129 read_thread_.join();
130 write_thread_.join();
131
132 LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopped";
133 std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "requested stop"); });
134}
135
136bool BlockingConnectionAdapter::Write(std::unique_ptr<apacket> packet) {
137 {
138 std::unique_lock<std::mutex> lock(this->mutex_);
139 write_queue_.emplace_back(std::move(packet));
140 }
141
142 cv_.notify_one();
143 return true;
144}
145
Josh Gaob800d882018-01-28 20:32:46 -0800146bool FdConnection::Read(apacket* packet) {
147 if (!ReadFdExactly(fd_.get(), &packet->msg, sizeof(amessage))) {
148 D("remote local: read terminated (message)");
149 return false;
150 }
151
Josh Gaof571fcb2018-02-05 18:49:10 -0800152 if (packet->msg.data_length > MAX_PAYLOAD) {
Josh Gao5caaebd2018-02-02 14:38:04 -0800153 D("remote local: read overflow (data length = %" PRIu32 ")", packet->msg.data_length);
154 return false;
155 }
156
Josh Gaof571fcb2018-02-05 18:49:10 -0800157 packet->payload.resize(packet->msg.data_length);
158
159 if (!ReadFdExactly(fd_.get(), &packet->payload[0], packet->payload.size())) {
Josh Gaob800d882018-01-28 20:32:46 -0800160 D("remote local: terminated (data)");
161 return false;
162 }
163
164 return true;
165}
166
167bool FdConnection::Write(apacket* packet) {
Josh Gaof571fcb2018-02-05 18:49:10 -0800168 if (!WriteFdExactly(fd_.get(), &packet->msg, sizeof(packet->msg))) {
Josh Gaob800d882018-01-28 20:32:46 -0800169 D("remote local: write terminated");
170 return false;
171 }
172
Josh Gaof571fcb2018-02-05 18:49:10 -0800173 if (packet->msg.data_length) {
174 if (!WriteFdExactly(fd_.get(), &packet->payload[0], packet->msg.data_length)) {
175 D("remote local: write terminated");
176 return false;
177 }
178 }
179
Josh Gaob800d882018-01-28 20:32:46 -0800180 return true;
181}
182
183void FdConnection::Close() {
184 adb_shutdown(fd_.get());
185 fd_.reset();
186}
187
Yabin Cuiaed3c612015-09-22 15:52:57 -0700188static std::string dump_packet(const char* name, const char* func, apacket* p) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800189 unsigned command = p->msg.command;
190 int len = p->msg.data_length;
191 char cmd[9];
192 char arg0[12], arg1[12];
193 int n;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100194
195 for (n = 0; n < 4; n++) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800196 int b = (command >> (n * 8)) & 255;
197 if (b < 32 || b >= 127) break;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100198 cmd[n] = (char)b;
199 }
200 if (n == 4) {
201 cmd[4] = 0;
202 } else {
203 /* There is some non-ASCII name in the command, so dump
204 * the hexadecimal value instead */
205 snprintf(cmd, sizeof cmd, "%08x", command);
206 }
207
208 if (p->msg.arg0 < 256U)
209 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
210 else
211 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
212
213 if (p->msg.arg1 < 256U)
214 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
215 else
216 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
217
Josh Gao1290fbf2016-11-22 14:32:34 -0800218 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", name,
219 func, cmd, arg0, arg1, len);
Josh Gaof571fcb2018-02-05 18:49:10 -0800220 result += dump_hex(p->payload.data(), p->payload.size());
Yabin Cuiaed3c612015-09-22 15:52:57 -0700221 return result;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100222}
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100223
Josh Gao06d61d42016-10-06 13:31:44 -0700224void send_packet(apacket* p, atransport* t) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 p->msg.magic = p->msg.command ^ 0xffffffff;
Tim Murrayde471942017-12-07 11:40:00 -0800226 // compute a checksum for connection/auth packets for compatibility reasons
227 if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) {
228 p->msg.data_check = 0;
229 } else {
230 p->msg.data_check = calculate_apacket_checksum(p);
231 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232
Josh Gao0bbf69c2018-02-16 13:24:58 -0800233 VLOG(TRANSPORT) << dump_packet(t->serial, "to remote", p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234
235 if (t == NULL) {
Josh Gao06d61d42016-10-06 13:31:44 -0700236 fatal("Transport is null");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 }
238
Josh Gao0bbf69c2018-02-16 13:24:58 -0800239 if (t->Write(p) != 0) {
240 D("%s: failed to enqueue packet, closing transport", t->serial);
241 t->Kick();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
243}
244
Yabin Cuif4b99282015-08-27 12:03:11 -0700245void kick_transport(atransport* t) {
Josh Gao1db71af2017-08-17 13:50:51 -0700246 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui1f4ec192016-04-05 13:50:44 -0700247 // As kick_transport() can be called from threads without guarantee that t is valid,
248 // check if the transport is in transport_list first.
Josh Gaob122b172017-08-16 16:57:01 -0700249 //
250 // TODO(jmgao): WTF? Is this actually true?
Yabin Cui1f4ec192016-04-05 13:50:44 -0700251 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cui7f274902016-04-18 11:22:34 -0700252 t->Kick();
Yabin Cui1f4ec192016-04-05 13:50:44 -0700253 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700254}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255
256static int transport_registration_send = -1;
257static int transport_registration_recv = -1;
258static fdevent transport_registration_fde;
259
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261
262/* this adds support required by the 'track-devices' service.
263 * this is used to send the content of "list_transport" to any
264 * number of client connections that want it through a single
265 * live TCP connection
266 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267struct device_tracker {
Josh Gao1290fbf2016-11-22 14:32:34 -0800268 asocket socket;
Josh Gaoe0361d12018-02-12 17:24:00 -0800269 bool update_needed = false;
270 bool long_output = false;
271 device_tracker* next = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272};
273
274/* linked list of all device trackers */
Josh Gao1290fbf2016-11-22 14:32:34 -0800275static device_tracker* device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276
Josh Gao1290fbf2016-11-22 14:32:34 -0800277static void device_tracker_remove(device_tracker* tracker) {
278 device_tracker** pnode = &device_tracker_list;
279 device_tracker* node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280
Josh Gao1db71af2017-08-17 13:50:51 -0700281 std::lock_guard<std::recursive_mutex> lock(transport_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 while (node) {
283 if (node == tracker) {
284 *pnode = node->next;
285 break;
286 }
287 pnode = &node->next;
Josh Gao1290fbf2016-11-22 14:32:34 -0800288 node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290}
291
Josh Gao1290fbf2016-11-22 14:32:34 -0800292static void device_tracker_close(asocket* socket) {
293 device_tracker* tracker = (device_tracker*)socket;
294 asocket* peer = socket->peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295
Josh Gao1290fbf2016-11-22 14:32:34 -0800296 D("device tracker %p removed", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 if (peer) {
298 peer->peer = NULL;
299 peer->close(peer);
300 }
301 device_tracker_remove(tracker);
Josh Gaoe0361d12018-02-12 17:24:00 -0800302 delete tracker;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303}
304
Josh Gao27cb7dc2018-02-01 13:17:50 -0800305static int device_tracker_enqueue(asocket* socket, std::string) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306 /* you can't read from a device tracker, close immediately */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 device_tracker_close(socket);
308 return -1;
309}
310
Elliott Hughese67f1f82015-04-30 17:32:03 -0700311static int device_tracker_send(device_tracker* tracker, const std::string& string) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700312 asocket* peer = tracker->socket.peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313
Josh Gao27cb7dc2018-02-01 13:17:50 -0800314 std::string data;
315 data.resize(4 + string.size());
316 char buf[5];
317 snprintf(buf, sizeof(buf), "%04x", static_cast<int>(string.size()));
318 memcpy(&data[0], buf, 4);
319 memcpy(&data[4], string.data(), string.size());
320 return peer->enqueue(peer, std::move(data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321}
322
Elliott Hughese67f1f82015-04-30 17:32:03 -0700323static void device_tracker_ready(asocket* socket) {
324 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325
Elliott Hughese67f1f82015-04-30 17:32:03 -0700326 // We want to send the device list when the tracker connects
327 // for the first time, even if no update occurred.
Josh Gaob0c18022017-08-14 18:57:54 -0700328 if (tracker->update_needed) {
329 tracker->update_needed = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330
Josh Gaob0c18022017-08-14 18:57:54 -0700331 std::string transports = list_transports(tracker->long_output);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700332 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 }
334}
335
Josh Gaob0c18022017-08-14 18:57:54 -0700336asocket* create_device_tracker(bool long_output) {
Josh Gaoe0361d12018-02-12 17:24:00 -0800337 device_tracker* tracker = new device_tracker();
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700338 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339
Josh Gao1290fbf2016-11-22 14:32:34 -0800340 D("device tracker %p created", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341
342 tracker->socket.enqueue = device_tracker_enqueue;
Josh Gao1290fbf2016-11-22 14:32:34 -0800343 tracker->socket.ready = device_tracker_ready;
344 tracker->socket.close = device_tracker_close;
Josh Gaob0c18022017-08-14 18:57:54 -0700345 tracker->update_needed = true;
346 tracker->long_output = long_output;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347
Josh Gao1290fbf2016-11-22 14:32:34 -0800348 tracker->next = device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349 device_tracker_list = tracker;
350
351 return &tracker->socket;
352}
353
Josh Gaofd713e52017-05-03 22:37:10 -0700354// Check if all of the USB transports are connected.
355bool iterate_transports(std::function<bool(const atransport*)> fn) {
Josh Gao1db71af2017-08-17 13:50:51 -0700356 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaofd713e52017-05-03 22:37:10 -0700357 for (const auto& t : transport_list) {
358 if (!fn(t)) {
359 return false;
360 }
361 }
362 for (const auto& t : pending_list) {
363 if (!fn(t)) {
364 return false;
365 }
366 }
367 return true;
368}
369
Elliott Hughese67f1f82015-04-30 17:32:03 -0700370// Call this function each time the transport list has changed.
371void update_transports() {
Josh Gaofd713e52017-05-03 22:37:10 -0700372 update_transport_status();
373
374 // Notify `adb track-devices` clients.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700375 std::string transports = list_transports(false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376
Elliott Hughese67f1f82015-04-30 17:32:03 -0700377 device_tracker* tracker = device_tracker_list;
378 while (tracker != nullptr) {
379 device_tracker* next = tracker->next;
380 // This may destroy the tracker if the connection is closed.
381 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382 tracker = next;
383 }
384}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700385
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386#else
Elliott Hughese67f1f82015-04-30 17:32:03 -0700387
388void update_transports() {
389 // Nothing to do on the device side.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700391
Josh Gao1290fbf2016-11-22 14:32:34 -0800392#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393
Josh Gao1290fbf2016-11-22 14:32:34 -0800394struct tmsg {
395 atransport* transport;
396 int action;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397};
398
Josh Gao1290fbf2016-11-22 14:32:34 -0800399static int transport_read_action(int fd, struct tmsg* m) {
400 char* p = (char*)m;
401 int len = sizeof(*m);
402 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403
Josh Gao1290fbf2016-11-22 14:32:34 -0800404 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 r = adb_read(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800406 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800408 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700410 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411 return -1;
412 }
413 }
414 return 0;
415}
416
Josh Gao1290fbf2016-11-22 14:32:34 -0800417static int transport_write_action(int fd, struct tmsg* m) {
418 char* p = (char*)m;
419 int len = sizeof(*m);
420 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421
Josh Gao1290fbf2016-11-22 14:32:34 -0800422 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423 r = adb_write(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800424 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800426 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700428 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 return -1;
430 }
431 }
432 return 0;
433}
434
Josh Gao0bbf69c2018-02-16 13:24:58 -0800435static void remove_transport(atransport*);
436
437static void transport_registration_func(int _fd, unsigned ev, void*) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 tmsg m;
Josh Gao1290fbf2016-11-22 14:32:34 -0800439 atransport* t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440
Josh Gao1290fbf2016-11-22 14:32:34 -0800441 if (!(ev & FDE_READ)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 return;
443 }
444
Josh Gao1290fbf2016-11-22 14:32:34 -0800445 if (transport_read_action(_fd, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 fatal_errno("cannot read transport registration socket");
447 }
448
449 t = m.transport;
450
Dan Albert1792c232015-05-18 13:06:53 -0700451 if (m.action == 0) {
Josh Gao0bbf69c2018-02-16 13:24:58 -0800452 D("transport: %s deleting", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453
Josh Gao0cd3ae12016-09-21 12:37:10 -0700454 {
Josh Gao1db71af2017-08-17 13:50:51 -0700455 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700456 transport_list.remove(t);
457 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458
Josh Gao1290fbf2016-11-22 14:32:34 -0800459 if (t->product) free(t->product);
460 if (t->serial) free(t->serial);
461 if (t->model) free(t->model);
462 if (t->device) free(t->device);
463 if (t->devpath) free(t->devpath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464
Dan Albertc7915a32015-05-18 16:46:31 -0700465 delete t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466
467 update_transports();
468 return;
469 }
470
Mike Lockwood0927bf92009-08-08 12:37:44 -0400471 /* don't create transport threads for inaccessible devices */
Yabin Cuib5e11412017-03-10 16:01:01 -0800472 if (t->GetConnectionState() != kCsNoPerm) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 /* initial references are the two threads */
Josh Gao0bbf69c2018-02-16 13:24:58 -0800474 t->ref_count = 1;
475 t->connection->SetTransportName(t->serial_name());
476 t->connection->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) {
477 if (!check_header(p.get(), t)) {
478 D("%s: remote read: bad header", t->serial);
479 return false;
480 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481
Josh Gao0bbf69c2018-02-16 13:24:58 -0800482 VLOG(TRANSPORT) << dump_packet(t->serial, "from remote", p.get());
483 apacket* packet = p.release();
Mike Lockwood0927bf92009-08-08 12:37:44 -0400484
Josh Gao0bbf69c2018-02-16 13:24:58 -0800485 // TODO: Does this need to run on the main thread?
486 fdevent_run_on_main_thread([packet, t]() { handle_packet(packet, t); });
487 return true;
488 });
489 t->connection->SetErrorCallback([t](Connection*, const std::string& error) {
490 D("%s: connection terminated: %s", t->serial, error.c_str());
491 fdevent_run_on_main_thread([t]() {
492 handle_offline(t);
493 transport_unref(t);
494 });
495 });
Mike Lockwood0927bf92009-08-08 12:37:44 -0400496
Josh Gao0bbf69c2018-02-16 13:24:58 -0800497 t->connection->Start();
498#if ADB_HOST
499 send_connect(t);
500#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 }
502
Josh Gao0cd3ae12016-09-21 12:37:10 -0700503 {
Josh Gao1db71af2017-08-17 13:50:51 -0700504 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700505 pending_list.remove(t);
506 transport_list.push_front(t);
507 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 update_transports();
510}
511
Josh Gao1290fbf2016-11-22 14:32:34 -0800512void init_transport_registration(void) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 int s[2];
514
Josh Gao1290fbf2016-11-22 14:32:34 -0800515 if (adb_socketpair(s)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 fatal_errno("cannot open transport registration socketpair");
517 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700518 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519
520 transport_registration_send = s[0];
521 transport_registration_recv = s[1];
522
Josh Gao1290fbf2016-11-22 14:32:34 -0800523 fdevent_install(&transport_registration_fde, transport_registration_recv,
524 transport_registration_func, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525
526 fdevent_set(&transport_registration_fde, FDE_READ);
Josh Gao01b7bc42017-05-09 13:43:35 -0700527}
528
529void kick_all_transports() {
530 // To avoid only writing part of a packet to a transport after exit, kick all transports.
Josh Gao1db71af2017-08-17 13:50:51 -0700531 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao01b7bc42017-05-09 13:43:35 -0700532 for (auto t : transport_list) {
533 t->Kick();
534 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535}
536
537/* the fdevent select pump is single threaded */
Josh Gao1290fbf2016-11-22 14:32:34 -0800538static void register_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 tmsg m;
540 m.transport = transport;
541 m.action = 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700542 D("transport: %s registered", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800543 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 fatal_errno("cannot write transport registration socket\n");
545 }
546}
547
Josh Gao1290fbf2016-11-22 14:32:34 -0800548static void remove_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549 tmsg m;
550 m.transport = transport;
551 m.action = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700552 D("transport: %s removed", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800553 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 fatal_errno("cannot write transport registration socket\n");
555 }
556}
557
Yabin Cuif4b99282015-08-27 12:03:11 -0700558static void transport_unref(atransport* t) {
559 CHECK(t != nullptr);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700560
Josh Gaoe48ecce2017-09-13 13:40:57 -0700561 std::lock_guard<std::recursive_mutex> lock(transport_lock);
562 CHECK_GT(t->ref_count, 0u);
563 t->ref_count--;
564 if (t->ref_count == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700565 D("transport: %s unref (kicking and closing)", t->serial);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800566 t->connection->Stop();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400567 remove_transport(t);
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100568 } else {
Josh Gaoe48ecce2017-09-13 13:40:57 -0700569 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400570 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800571}
572
Josh Gao1290fbf2016-11-22 14:32:34 -0800573static int qual_match(const char* to_test, const char* prefix, const char* qual,
574 bool sanitize_qual) {
575 if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700576 return !qual || !*qual;
577
Josh Gao1290fbf2016-11-22 14:32:34 -0800578 if (!qual) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700579
580 if (prefix) {
581 while (*prefix) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800582 if (*prefix++ != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700583 }
584 }
585
586 while (*qual) {
587 char ch = *qual++;
Josh Gao1290fbf2016-11-22 14:32:34 -0800588 if (sanitize_qual && !isalnum(ch)) ch = '_';
589 if (ch != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700590 }
591
592 /* Everything matched so far. Return true if *to_test is a NUL. */
593 return !*to_test;
594}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595
Josh Gaob122b172017-08-16 16:57:01 -0700596atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
597 bool* is_ambiguous, std::string* error_out,
598 bool accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700599 atransport* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600
Josh Gaob122b172017-08-16 16:57:01 -0700601 if (transport_id != 0) {
602 *error_out =
603 android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id);
604 } else if (serial) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700605 *error_out = android::base::StringPrintf("device '%s' not found", serial);
606 } else if (type == kTransportLocal) {
607 *error_out = "no emulators found";
608 } else if (type == kTransportAny) {
609 *error_out = "no devices/emulators found";
610 } else {
611 *error_out = "no devices found";
612 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613
Josh Gao1db71af2017-08-17 13:50:51 -0700614 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700615 for (const auto& t : transport_list) {
Yabin Cuib5e11412017-03-10 16:01:01 -0800616 if (t->GetConnectionState() == kCsNoPerm) {
David Purselld2acbd12015-12-02 15:14:31 -0800617 *error_out = UsbNoPermissionsLongHelpText();
Mike Lockwood37d31112009-08-08 13:53:16 -0400618 continue;
619 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400620
Josh Gaob122b172017-08-16 16:57:01 -0700621 if (transport_id) {
622 if (t->id == transport_id) {
623 result = t;
624 break;
625 }
626 } else if (serial) {
David Pursell3f902aa2016-03-01 08:58:26 -0800627 if (t->MatchesTarget(serial)) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700628 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700629 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700630 if (is_ambiguous) *is_ambiguous = true;
631 result = nullptr;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700632 break;
633 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634 result = t;
Scott Andersone109d262012-04-20 11:21:14 -0700635 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 } else {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700637 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700639 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700640 if (is_ambiguous) *is_ambiguous = true;
641 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800642 break;
643 }
644 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700645 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700647 *error_out = "more than one emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700648 if (is_ambiguous) *is_ambiguous = true;
649 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 break;
651 }
652 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700653 } else if (type == kTransportAny) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700655 *error_out = "more than one device/emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700656 if (is_ambiguous) *is_ambiguous = true;
657 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658 break;
659 }
660 result = t;
661 }
662 }
663 }
Josh Gao0cd3ae12016-09-21 12:37:10 -0700664 lock.unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665
Elliott Hughes8d28e192015-10-07 14:55:10 -0700666 // Don't return unauthorized devices; the caller can't do anything with them.
Yabin Cuib5e11412017-03-10 16:01:01 -0800667 if (result && result->GetConnectionState() == kCsUnauthorized && !accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700668 *error_out = "device unauthorized.\n";
669 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
670 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
671 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
672 *error_out += "\n";
673 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
674 *error_out += "Otherwise check for a confirmation dialog on your device.";
675 result = nullptr;
676 }
Benoit Goby77e8e582013-01-15 12:36:47 -0800677
Elliott Hughes8d28e192015-10-07 14:55:10 -0700678 // Don't return offline devices; the caller can't do anything with them.
Yabin Cuib5e11412017-03-10 16:01:01 -0800679 if (result && result->GetConnectionState() == kCsOffline && !accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700680 *error_out = "device offline";
681 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682 }
683
684 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700685 *error_out = "success";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 }
687
688 return result;
689}
690
Yabin Cuib5e11412017-03-10 16:01:01 -0800691int atransport::Write(apacket* p) {
Josh Gao0bbf69c2018-02-16 13:24:58 -0800692 return this->connection->Write(std::unique_ptr<apacket>(p)) ? 0 : -1;
Yabin Cuib5e11412017-03-10 16:01:01 -0800693}
694
Yabin Cui7f274902016-04-18 11:22:34 -0700695void atransport::Kick() {
696 if (!kicked_) {
Josh Gaob800d882018-01-28 20:32:46 -0800697 D("kicking transport %s", this->serial);
Yabin Cui7f274902016-04-18 11:22:34 -0700698 kicked_ = true;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800699 this->connection->Stop();
Yabin Cui7f274902016-04-18 11:22:34 -0700700 }
701}
702
Yabin Cuib5e11412017-03-10 16:01:01 -0800703ConnectionState atransport::GetConnectionState() const {
704 return connection_state_;
705}
706
707void atransport::SetConnectionState(ConnectionState state) {
708 check_main_thread();
709 connection_state_ = state;
710}
711
David Purselld2acbd12015-12-02 15:14:31 -0800712const std::string atransport::connection_state_name() const {
Yabin Cuib5e11412017-03-10 16:01:01 -0800713 ConnectionState state = GetConnectionState();
714 switch (state) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800715 case kCsOffline:
716 return "offline";
717 case kCsBootloader:
718 return "bootloader";
719 case kCsDevice:
720 return "device";
721 case kCsHost:
722 return "host";
723 case kCsRecovery:
724 return "recovery";
725 case kCsNoPerm:
726 return UsbNoPermissionsShortHelpText();
727 case kCsSideload:
728 return "sideload";
729 case kCsUnauthorized:
730 return "unauthorized";
731 default:
732 return "unknown";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800733 }
734}
735
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100736void atransport::update_version(int version, size_t payload) {
737 protocol_version = std::min(version, A_VERSION);
738 max_payload = std::min(payload, MAX_PAYLOAD);
739}
740
741int atransport::get_protocol_version() const {
742 return protocol_version;
743}
744
745size_t atransport::get_max_payload() const {
746 return max_payload;
747}
748
David Pursell4e2fd362015-09-22 10:43:08 -0700749namespace {
David Pursell0955c662015-08-31 10:42:13 -0700750
David Pursell4e2fd362015-09-22 10:43:08 -0700751constexpr char kFeatureStringDelimiter = ',';
752
753} // namespace
Dan Albert1792c232015-05-18 13:06:53 -0700754
755const FeatureSet& supported_features() {
David Pursell4e2fd362015-09-22 10:43:08 -0700756 // Local static allocation to avoid global non-POD variables.
757 static const FeatureSet* features = new FeatureSet{
Josh Gao1290fbf2016-11-22 14:32:34 -0800758 kFeatureShell2, kFeatureCmd, kFeatureStat2,
David Pursellbbe3d212015-09-25 08:37:13 -0700759 // Increment ADB_SERVER_VERSION whenever the feature list changes to
760 // make sure that the adb client and server features stay in sync
761 // (http://b/24370690).
David Pursell4e2fd362015-09-22 10:43:08 -0700762 };
763
764 return *features;
765}
766
767std::string FeatureSetToString(const FeatureSet& features) {
768 return android::base::Join(features, kFeatureStringDelimiter);
769}
770
771FeatureSet StringToFeatureSet(const std::string& features_string) {
David Purselld2b588e2015-09-25 13:04:21 -0700772 if (features_string.empty()) {
773 return FeatureSet();
774 }
775
Josh Gao1290fbf2016-11-22 14:32:34 -0800776 auto names = android::base::Split(features_string, {kFeatureStringDelimiter});
David Pursell4e2fd362015-09-22 10:43:08 -0700777 return FeatureSet(names.begin(), names.end());
Dan Albert1792c232015-05-18 13:06:53 -0700778}
779
David Pursell70ef7b42015-09-30 13:35:42 -0700780bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800781 return feature_set.count(feature) > 0 && supported_features().count(feature) > 0;
David Pursell70ef7b42015-09-30 13:35:42 -0700782}
783
Dan Albert1792c232015-05-18 13:06:53 -0700784bool atransport::has_feature(const std::string& feature) const {
785 return features_.count(feature) > 0;
786}
787
David Pursell4e2fd362015-09-22 10:43:08 -0700788void atransport::SetFeatures(const std::string& features_string) {
789 features_ = StringToFeatureSet(features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700790}
791
Yabin Cuib3298242015-08-28 15:09:44 -0700792void atransport::AddDisconnect(adisconnect* disconnect) {
793 disconnects_.push_back(disconnect);
794}
795
796void atransport::RemoveDisconnect(adisconnect* disconnect) {
797 disconnects_.remove(disconnect);
798}
799
800void atransport::RunDisconnects() {
Elliott Hughes65fe2512015-10-07 15:59:35 -0700801 for (const auto& disconnect : disconnects_) {
Yabin Cuib3298242015-08-28 15:09:44 -0700802 disconnect->func(disconnect->opaque, this);
803 }
804 disconnects_.clear();
805}
806
David Pursell3f902aa2016-03-01 08:58:26 -0800807bool atransport::MatchesTarget(const std::string& target) const {
808 if (serial) {
809 if (target == serial) {
810 return true;
811 } else if (type == kTransportLocal) {
812 // Local transports can match [tcp:|udp:]<hostname>[:port].
813 const char* local_target_ptr = target.c_str();
814
815 // For fastboot compatibility, ignore protocol prefixes.
816 if (android::base::StartsWith(target, "tcp:") ||
Josh Gao1290fbf2016-11-22 14:32:34 -0800817 android::base::StartsWith(target, "udp:")) {
David Pursell3f902aa2016-03-01 08:58:26 -0800818 local_target_ptr += 4;
819 }
820
821 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
822 std::string serial_host, error;
823 int serial_port = -1;
Josh Gao1290fbf2016-11-22 14:32:34 -0800824 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) {
David Pursell3f902aa2016-03-01 08:58:26 -0800825 // |target| may omit the port to default to ours.
826 std::string target_host;
827 int target_port = serial_port;
828 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
829 nullptr, &error) &&
Josh Gao1290fbf2016-11-22 14:32:34 -0800830 serial_host == target_host && serial_port == target_port) {
David Pursell3f902aa2016-03-01 08:58:26 -0800831 return true;
832 }
833 }
834 }
835 }
836
837 return (devpath && target == devpath) ||
838 qual_match(target.c_str(), "product:", product, false) ||
839 qual_match(target.c_str(), "model:", model, true) ||
840 qual_match(target.c_str(), "device:", device, false);
841}
842
Elliott Hughese67f1f82015-04-30 17:32:03 -0700843#if ADB_HOST
844
Josh Gaob122b172017-08-16 16:57:01 -0700845// We use newline as our delimiter, make sure to never output it.
846static std::string sanitize(std::string str, bool alphanumeric) {
847 auto pred = alphanumeric ? [](const char c) { return !isalnum(c); }
848 : [](const char c) { return c == '\n'; };
849 std::replace_if(str.begin(), str.end(), pred, '_');
850 return str;
851}
852
Josh Gao1290fbf2016-11-22 14:32:34 -0800853static void append_transport_info(std::string* result, const char* key, const char* value,
Josh Gaob122b172017-08-16 16:57:01 -0700854 bool alphanumeric) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700855 if (value == nullptr || *value == '\0') {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700856 return;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700857 }
858
Elliott Hughese67f1f82015-04-30 17:32:03 -0700859 *result += ' ';
860 *result += key;
Josh Gaob122b172017-08-16 16:57:01 -0700861 *result += sanitize(value, alphanumeric);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700862}
863
Josh Gao1290fbf2016-11-22 14:32:34 -0800864static void append_transport(const atransport* t, std::string* result, bool long_listing) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700865 const char* serial = t->serial;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700866 if (!serial || !serial[0]) {
Dan Albertd99d9022015-05-06 16:48:52 -0700867 serial = "(no serial number)";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700868 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700869
870 if (!long_listing) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700871 *result += serial;
872 *result += '\t';
873 *result += t->connection_state_name();
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700874 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800875 android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str());
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700876
Elliott Hughese67f1f82015-04-30 17:32:03 -0700877 append_transport_info(result, "", t->devpath, false);
878 append_transport_info(result, "product:", t->product, false);
879 append_transport_info(result, "model:", t->model, true);
880 append_transport_info(result, "device:", t->device, false);
Josh Gaob122b172017-08-16 16:57:01 -0700881
882 // Put id at the end, so that anyone parsing the output here can always find it by scanning
883 // backwards from newlines, even with hypothetical devices named 'transport_id:1'.
884 *result += " transport_id:";
885 *result += std::to_string(t->id);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700886 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700887 *result += '\n';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700888}
889
Elliott Hughese67f1f82015-04-30 17:32:03 -0700890std::string list_transports(bool long_listing) {
Josh Gao1db71af2017-08-17 13:50:51 -0700891 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Artem Iglikov04398a92017-12-17 10:56:07 +0000892
893 auto sorted_transport_list = transport_list;
894 sorted_transport_list.sort([](atransport*& x, atransport*& y) {
895 if (x->type != y->type) {
896 return x->type < y->type;
897 }
898 return strcmp(x->serial, y->serial) < 0;
899 });
900
901 std::string result;
902 for (const auto& t : sorted_transport_list) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700903 append_transport(t, &result, long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800904 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700905 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800906}
907
Josh Gao22d2b3e2016-10-27 14:01:08 -0700908void close_usb_devices(std::function<bool(const atransport*)> predicate) {
Josh Gao1db71af2017-08-17 13:50:51 -0700909 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao22d2b3e2016-10-27 14:01:08 -0700910 for (auto& t : transport_list) {
911 if (predicate(t)) {
912 t->Kick();
913 }
914 }
915}
916
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800917/* hack for osx */
Dan Albertc7915a32015-05-18 16:46:31 -0700918void close_usb_devices() {
Josh Gao22d2b3e2016-10-27 14:01:08 -0700919 close_usb_devices([](const atransport*) { return true; });
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800920}
Josh Gao1290fbf2016-11-22 14:32:34 -0800921#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800922
Josh Gao1290fbf2016-11-22 14:32:34 -0800923int register_socket_transport(int s, const char* serial, int port, int local) {
Dan Albertc7915a32015-05-18 16:46:31 -0700924 atransport* t = new atransport();
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100925
926 if (!serial) {
Dan Albertc7915a32015-05-18 16:46:31 -0700927 char buf[32];
928 snprintf(buf, sizeof(buf), "T-%p", t);
929 serial = buf;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100930 }
Dan Albertc7915a32015-05-18 16:46:31 -0700931
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700932 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700933 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertc7915a32015-05-18 16:46:31 -0700934 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700935 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700937
Josh Gao1db71af2017-08-17 13:50:51 -0700938 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700939 for (const auto& transport : pending_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700940 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700941 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -0800942 << " is already in pending_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -0700943 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700944 return -1;
945 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800946 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700947
Elliott Hughes65fe2512015-10-07 15:59:35 -0700948 for (const auto& transport : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700949 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700950 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -0800951 << " is already in transport_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -0700952 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700953 return -1;
954 }
955 }
956
Dan Albertc7915a32015-05-18 16:46:31 -0700957 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700958 t->serial = strdup(serial);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700959
960 lock.unlock();
Benoit Goby1c45ee92013-03-29 18:22:36 -0700961
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800962 register_transport(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700963 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800964}
965
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400966#if ADB_HOST
Josh Gao1290fbf2016-11-22 14:32:34 -0800967atransport* find_transport(const char* serial) {
Dan Albertc7915a32015-05-18 16:46:31 -0700968 atransport* result = nullptr;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400969
Josh Gao1db71af2017-08-17 13:50:51 -0700970 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700971 for (auto& t : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700972 if (t->serial && strcmp(serial, t->serial) == 0) {
973 result = t;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400974 break;
975 }
Dan Albertc7915a32015-05-18 16:46:31 -0700976 }
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400977
Dan Albertc7915a32015-05-18 16:46:31 -0700978 return result;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400979}
980
Yabin Cuif4b99282015-08-27 12:03:11 -0700981void kick_all_tcp_devices() {
Josh Gao1db71af2017-08-17 13:50:51 -0700982 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700983 for (auto& t : transport_list) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700984 if (t->IsTcpDevice()) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700985 // Kicking breaks the read_transport thread of this transport out of any read, then
986 // the read_transport thread will notify the main thread to make this transport
987 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cuif4b99282015-08-27 12:03:11 -0700988 // Finally, this transport will be closed and freed in the main thread.
Yabin Cui7f274902016-04-18 11:22:34 -0700989 t->Kick();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400990 }
Dan Albertc7915a32015-05-18 16:46:31 -0700991 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400992}
993
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400994#endif
995
Josh Gao1290fbf2016-11-22 14:32:34 -0800996void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
997 unsigned writeable) {
Yabin Cuib5e11412017-03-10 16:01:01 -0800998 atransport* t = new atransport((writeable ? kCsOffline : kCsNoPerm));
Dan Albertc7915a32015-05-18 16:46:31 -0700999
Josh Gao1290fbf2016-11-22 14:32:34 -08001000 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
Yabin Cuib5e11412017-03-10 16:01:01 -08001001 init_usb_transport(t, usb);
Josh Gao1290fbf2016-11-22 14:32:34 -08001002 if (serial) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001003 t->serial = strdup(serial);
1004 }
Dan Albertc7915a32015-05-18 16:46:31 -07001005
1006 if (devpath) {
Scott Andersone109d262012-04-20 11:21:14 -07001007 t->devpath = strdup(devpath);
1008 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001009
Josh Gao0cd3ae12016-09-21 12:37:10 -07001010 {
Josh Gao1db71af2017-08-17 13:50:51 -07001011 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -07001012 pending_list.push_front(t);
1013 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001014
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001015 register_transport(t);
1016}
1017
Dan Albertdcd78a12015-05-18 16:43:57 -07001018// This should only be used for transports with connection_state == kCsNoPerm.
Josh Gao1290fbf2016-11-22 14:32:34 -08001019void unregister_usb_transport(usb_handle* usb) {
Josh Gao1db71af2017-08-17 13:50:51 -07001020 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaob800d882018-01-28 20:32:46 -08001021 transport_list.remove_if([usb](atransport* t) {
1022 if (auto connection = dynamic_cast<UsbConnection*>(t->connection.get())) {
1023 return connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm;
1024 }
1025 return false;
1026 });
Mike Lockwood0927bf92009-08-08 12:37:44 -04001027}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001028
Josh Gao36dadca2017-05-16 15:02:45 -07001029bool check_header(apacket* p, atransport* t) {
Josh Gao1290fbf2016-11-22 14:32:34 -08001030 if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cuib5e11412017-03-10 16:01:01 -08001031 VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command
1032 << ", magic = " << p->msg.magic;
Josh Gao36dadca2017-05-16 15:02:45 -07001033 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001034 }
1035
Josh Gao1290fbf2016-11-22 14:32:34 -08001036 if (p->msg.data_length > t->get_max_payload()) {
1037 VLOG(RWX) << "check_header(): " << p->msg.data_length
1038 << " atransport::max_payload = " << t->get_max_payload();
Josh Gao36dadca2017-05-16 15:02:45 -07001039 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001040 }
1041
Josh Gao36dadca2017-05-16 15:02:45 -07001042 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001043}
1044
Josh Gao3bd28792016-10-05 19:02:29 -07001045#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -07001046std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001047 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1048
Josh Gao2e671202016-08-18 22:00:12 -07001049 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001050 keys_.pop_front();
1051 return result;
1052}
Josh Gao3bd28792016-10-05 19:02:29 -07001053#endif