blob: 98809025f6a2729444032a4712d675417dd5b15e [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>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <stdio.h>
25#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080027#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Spencer Low363af562015-11-07 18:51:54 -080029#include <algorithm>
Dan Albertc7915a32015-05-18 16:46:31 -070030#include <list>
Josh Gao0cd3ae12016-09-21 12:37:10 -070031#include <mutex>
Dan Albertc7915a32015-05-18 16:46:31 -070032
Elliott Hughes4f713192015-12-04 22:00:26 -080033#include <android-base/logging.h>
David Pursell3f902aa2016-03-01 08:58:26 -080034#include <android-base/parsenetaddress.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/stringprintf.h>
36#include <android-base/strings.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070037
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038#include "adb.h"
Elliott Hughes0aeb5052016-06-29 17:42:01 -070039#include "adb_auth.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070040#include "adb_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080041#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
43static void transport_unref(atransport *t);
44
Josh Gaob7b1edf2015-11-11 17:56:12 -080045static auto& transport_list = *new std::list<atransport*>();
46static auto& pending_list = *new std::list<atransport*>();
Benoit Goby1c45ee92013-03-29 18:22:36 -070047
Josh Gao0cd3ae12016-09-21 12:37:10 -070048static std::mutex& transport_lock = *new std::mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
Todd Kennedy51c05ec2015-11-10 00:03:25 +000050const char* const kFeatureShell2 = "shell_v2";
51const char* const kFeatureCmd = "cmd";
52
Yabin Cuiaed3c612015-09-22 15:52:57 -070053static std::string dump_packet(const char* name, const char* func, apacket* p) {
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010054 unsigned command = p->msg.command;
55 int len = p->msg.data_length;
56 char cmd[9];
57 char arg0[12], arg1[12];
58 int n;
59
60 for (n = 0; n < 4; n++) {
61 int b = (command >> (n*8)) & 255;
62 if (b < 32 || b >= 127)
63 break;
64 cmd[n] = (char)b;
65 }
66 if (n == 4) {
67 cmd[4] = 0;
68 } else {
69 /* There is some non-ASCII name in the command, so dump
70 * the hexadecimal value instead */
71 snprintf(cmd, sizeof cmd, "%08x", command);
72 }
73
74 if (p->msg.arg0 < 256U)
75 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
76 else
77 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
78
79 if (p->msg.arg1 < 256U)
80 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
81 else
82 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
83
Yabin Cuiaed3c612015-09-22 15:52:57 -070084 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ",
85 name, func, cmd, arg0, arg1, len);
86 result += dump_hex(p->data, len);
87 return result;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010088}
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010089
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090static int
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010091read_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092{
Yabin Cui62641292015-07-30 19:58:10 -070093 char buff[8];
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010094 if (!name) {
95 snprintf(buff, sizeof buff, "fd=%d", fd);
96 name = buff;
97 }
Yabin Cui62641292015-07-30 19:58:10 -070098 char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */
99 int len = sizeof(apacket*);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 while(len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700101 int r = adb_read(fd, p, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 if(r > 0) {
103 len -= r;
Yabin Cui62641292015-07-30 19:58:10 -0700104 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700106 D("%s: read_packet (fd=%d), error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107 return -1;
108 }
109 }
110
Yabin Cuiaed3c612015-09-22 15:52:57 -0700111 VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 return 0;
113}
114
115static int
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100116write_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117{
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100118 char buff[8];
119 if (!name) {
120 snprintf(buff, sizeof buff, "fd=%d", fd);
121 name = buff;
122 }
Yabin Cuiaed3c612015-09-22 15:52:57 -0700123 VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket);
Yabin Cui62641292015-07-30 19:58:10 -0700124 char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */
125 int len = sizeof(apacket*);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 while(len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700127 int r = adb_write(fd, p, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 if(r > 0) {
129 len -= r;
130 p += r;
131 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700132 D("%s: write_packet (fd=%d) error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 return -1;
134 }
135 }
136 return 0;
137}
138
139static void transport_socket_events(int fd, unsigned events, void *_t)
140{
Dan Albertbac34742015-02-25 17:51:28 -0800141 atransport *t = reinterpret_cast<atransport*>(_t);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700142 D("transport_socket_events(fd=%d, events=%04x,...)", fd, events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 if(events & FDE_READ){
144 apacket *p = 0;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100145 if(read_packet(fd, t->serial, &p)){
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700146 D("%s: failed to read packet from transport socket on fd %d", t->serial, fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 } else {
148 handle_packet(p, (atransport *) _t);
149 }
150 }
151}
152
153void send_packet(apacket *p, atransport *t)
154{
155 unsigned char *x;
156 unsigned sum;
157 unsigned count;
158
159 p->msg.magic = p->msg.command ^ 0xffffffff;
160
161 count = p->msg.data_length;
162 x = (unsigned char *) p->data;
163 sum = 0;
164 while(count-- > 0){
165 sum += *x++;
166 }
167 p->msg.data_check = sum;
168
169 print_packet("send", p);
170
171 if (t == NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700172 D("Transport is null");
JP Abgrall408fa572011-03-16 15:57:42 -0700173 // Zap errno because print_packet() and other stuff have errno effect.
174 errno = 0;
175 fatal_errno("Transport is null");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 }
177
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100178 if(write_packet(t->transport_socket, t->serial, &p)){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 fatal_errno("cannot enqueue packet on transport socket");
180 }
181}
182
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700183// The transport is opened by transport_register_func before
184// the read_transport and write_transport threads are started.
185//
186// The read_transport thread issues a SYNC(1, token) message to let
187// the write_transport thread know to start things up. In the event
188// of transport IO failure, the read_transport thread will post a
189// SYNC(0,0) message to ensure shutdown.
190//
191// The transport will not actually be closed until both threads exit, but the threads
192// will kick the transport on their way out to disconnect the underlying device.
193//
194// read_transport thread reads data from a transport (representing a usb/tcp connection),
195// and makes the main thread call handle_packet().
Josh Gaob5fea142016-02-12 14:31:15 -0800196static void read_transport_thread(void* _t) {
Dan Albertbac34742015-02-25 17:51:28 -0800197 atransport *t = reinterpret_cast<atransport*>(_t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 apacket *p;
199
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700200 adb_thread_setname(android::base::StringPrintf("<-%s",
201 (t->serial != nullptr ? t->serial : "transport")));
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700202 D("%s: starting read_transport thread on fd %d, SYNC online (%d)",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100203 t->serial, t->fd, t->sync_token + 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 p = get_apacket();
205 p->msg.command = A_SYNC;
206 p->msg.arg0 = 1;
207 p->msg.arg1 = ++(t->sync_token);
208 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100209 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700211 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 goto oops;
213 }
214
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700215 D("%s: data pump started", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 for(;;) {
217 p = get_apacket();
218
219 if(t->read_from_remote(p, t) == 0){
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700220 D("%s: received remote packet, sending to transport",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100221 t->serial);
222 if(write_packet(t->fd, t->serial, &p)){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700224 D("%s: failed to write apacket to transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 goto oops;
226 }
227 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700228 D("%s: remote read failed for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 put_apacket(p);
230 break;
231 }
232 }
233
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700234 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 p = get_apacket();
236 p->msg.command = A_SYNC;
237 p->msg.arg0 = 0;
238 p->msg.arg1 = 0;
239 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100240 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700242 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 }
244
245oops:
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700246 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 kick_transport(t);
248 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249}
250
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700251// write_transport thread gets packets sent by the main thread (through send_packet()),
252// and writes to a transport (representing a usb/tcp connection).
Josh Gaob5fea142016-02-12 14:31:15 -0800253static void write_transport_thread(void* _t) {
Dan Albertbac34742015-02-25 17:51:28 -0800254 atransport *t = reinterpret_cast<atransport*>(_t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 apacket *p;
256 int active = 0;
257
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700258 adb_thread_setname(android::base::StringPrintf("->%s",
259 (t->serial != nullptr ? t->serial : "transport")));
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700260 D("%s: starting write_transport thread, reading from fd %d",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100261 t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262
263 for(;;){
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100264 if(read_packet(t->fd, t->serial, &p)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700265 D("%s: failed to read apacket from transport on fd %d",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100266 t->serial, t->fd );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 break;
268 }
269 if(p->msg.command == A_SYNC){
270 if(p->msg.arg0 == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700271 D("%s: transport SYNC offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 put_apacket(p);
273 break;
274 } else {
275 if(p->msg.arg1 == t->sync_token) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700276 D("%s: transport SYNC online", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 active = 1;
278 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700279 D("%s: transport ignoring SYNC %d != %d",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100280 t->serial, p->msg.arg1, t->sync_token);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 }
282 }
283 } else {
284 if(active) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700285 D("%s: transport got packet, sending to remote", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 t->write_to_remote(p, t);
287 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700288 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 }
290 }
291
292 put_apacket(p);
293 }
294
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700295 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 kick_transport(t);
297 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298}
299
Yabin Cuif4b99282015-08-27 12:03:11 -0700300void kick_transport(atransport* t) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700301 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cui1f4ec192016-04-05 13:50:44 -0700302 // As kick_transport() can be called from threads without guarantee that t is valid,
303 // check if the transport is in transport_list first.
304 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cui7f274902016-04-18 11:22:34 -0700305 t->Kick();
Yabin Cui1f4ec192016-04-05 13:50:44 -0700306 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700307}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308
309static int transport_registration_send = -1;
310static int transport_registration_recv = -1;
311static fdevent transport_registration_fde;
312
313
314#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315
316/* this adds support required by the 'track-devices' service.
317 * this is used to send the content of "list_transport" to any
318 * number of client connections that want it through a single
319 * live TCP connection
320 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321struct device_tracker {
322 asocket socket;
323 int update_needed;
324 device_tracker* next;
325};
326
327/* linked list of all device trackers */
328static device_tracker* device_tracker_list;
329
330static void
331device_tracker_remove( device_tracker* tracker )
332{
333 device_tracker** pnode = &device_tracker_list;
334 device_tracker* node = *pnode;
335
Josh Gao0cd3ae12016-09-21 12:37:10 -0700336 std::lock_guard<std::mutex> lock(transport_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 while (node) {
338 if (node == tracker) {
339 *pnode = node->next;
340 break;
341 }
342 pnode = &node->next;
343 node = *pnode;
344 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345}
346
347static void
348device_tracker_close( asocket* socket )
349{
350 device_tracker* tracker = (device_tracker*) socket;
351 asocket* peer = socket->peer;
352
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700353 D( "device tracker %p removed", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 if (peer) {
355 peer->peer = NULL;
356 peer->close(peer);
357 }
358 device_tracker_remove(tracker);
359 free(tracker);
360}
361
362static int
363device_tracker_enqueue( asocket* socket, apacket* p )
364{
365 /* you can't read from a device tracker, close immediately */
366 put_apacket(p);
367 device_tracker_close(socket);
368 return -1;
369}
370
Elliott Hughese67f1f82015-04-30 17:32:03 -0700371static int device_tracker_send(device_tracker* tracker, const std::string& string) {
372 apacket* p = get_apacket();
373 asocket* peer = tracker->socket.peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374
Elliott Hughese67f1f82015-04-30 17:32:03 -0700375 snprintf(reinterpret_cast<char*>(p->data), 5, "%04x", static_cast<int>(string.size()));
376 memcpy(&p->data[4], string.data(), string.size());
377 p->len = 4 + string.size();
378 return peer->enqueue(peer, p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379}
380
Elliott Hughese67f1f82015-04-30 17:32:03 -0700381static void device_tracker_ready(asocket* socket) {
382 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383
Elliott Hughese67f1f82015-04-30 17:32:03 -0700384 // We want to send the device list when the tracker connects
385 // for the first time, even if no update occurred.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386 if (tracker->update_needed > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 tracker->update_needed = 0;
388
Elliott Hughese67f1f82015-04-30 17:32:03 -0700389 std::string transports = list_transports(false);
390 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391 }
392}
393
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394asocket*
395create_device_tracker(void)
396{
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700397 device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker)));
398 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700400 D( "device tracker %p created", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401
402 tracker->socket.enqueue = device_tracker_enqueue;
403 tracker->socket.ready = device_tracker_ready;
404 tracker->socket.close = device_tracker_close;
405 tracker->update_needed = 1;
406
407 tracker->next = device_tracker_list;
408 device_tracker_list = tracker;
409
410 return &tracker->socket;
411}
412
413
Elliott Hughese67f1f82015-04-30 17:32:03 -0700414// Call this function each time the transport list has changed.
415void update_transports() {
416 std::string transports = list_transports(false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417
Elliott Hughese67f1f82015-04-30 17:32:03 -0700418 device_tracker* tracker = device_tracker_list;
419 while (tracker != nullptr) {
420 device_tracker* next = tracker->next;
421 // This may destroy the tracker if the connection is closed.
422 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423 tracker = next;
424 }
425}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700426
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427#else
Elliott Hughese67f1f82015-04-30 17:32:03 -0700428
429void update_transports() {
430 // Nothing to do on the device side.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700432
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433#endif // ADB_HOST
434
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435struct tmsg
436{
437 atransport *transport;
438 int action;
439};
440
441static int
442transport_read_action(int fd, struct tmsg* m)
443{
444 char *p = (char*)m;
445 int len = sizeof(*m);
446 int r;
447
448 while(len > 0) {
449 r = adb_read(fd, p, len);
450 if(r > 0) {
451 len -= r;
452 p += r;
453 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700454 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 return -1;
456 }
457 }
458 return 0;
459}
460
461static int
462transport_write_action(int fd, struct tmsg* m)
463{
464 char *p = (char*)m;
465 int len = sizeof(*m);
466 int r;
467
468 while(len > 0) {
469 r = adb_write(fd, p, len);
470 if(r > 0) {
471 len -= r;
472 p += r;
473 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700474 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 return -1;
476 }
477 }
478 return 0;
479}
480
481static void transport_registration_func(int _fd, unsigned ev, void *data)
482{
483 tmsg m;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484 int s[2];
485 atransport *t;
486
487 if(!(ev & FDE_READ)) {
488 return;
489 }
490
491 if(transport_read_action(_fd, &m)) {
492 fatal_errno("cannot read transport registration socket");
493 }
494
495 t = m.transport;
496
Dan Albert1792c232015-05-18 13:06:53 -0700497 if (m.action == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700498 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499
500 /* IMPORTANT: the remove closes one half of the
501 ** socket pair. The close closes the other half.
502 */
503 fdevent_remove(&(t->transport_fde));
504 adb_close(t->fd);
505
Josh Gao0cd3ae12016-09-21 12:37:10 -0700506 {
507 std::lock_guard<std::mutex> lock(transport_lock);
508 transport_list.remove(t);
509 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 if (t->product)
512 free(t->product);
513 if (t->serial)
514 free(t->serial);
Scott Andersone82c2db2012-05-25 14:10:02 -0700515 if (t->model)
516 free(t->model);
517 if (t->device)
518 free(t->device);
Scott Andersone109d262012-04-20 11:21:14 -0700519 if (t->devpath)
520 free(t->devpath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521
Dan Albertc7915a32015-05-18 16:46:31 -0700522 delete t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523
524 update_transports();
525 return;
526 }
527
Mike Lockwood0927bf92009-08-08 12:37:44 -0400528 /* don't create transport threads for inaccessible devices */
Dan Albertdcd78a12015-05-18 16:43:57 -0700529 if (t->connection_state != kCsNoPerm) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 /* initial references are the two threads */
Mike Lockwood0927bf92009-08-08 12:37:44 -0400531 t->ref_count = 2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532
Dan Albertc7915a32015-05-18 16:46:31 -0700533 if (adb_socketpair(s)) {
Mike Lockwood0927bf92009-08-08 12:37:44 -0400534 fatal_errno("cannot open transport socketpair");
535 }
536
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700537 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400538
539 t->transport_socket = s[0];
540 t->fd = s[1];
541
Mike Lockwood0927bf92009-08-08 12:37:44 -0400542 fdevent_install(&(t->transport_fde),
543 t->transport_socket,
544 transport_socket_events,
545 t);
546
547 fdevent_set(&(t->transport_fde), FDE_READ);
548
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700549 if (!adb_thread_create(write_transport_thread, t)) {
550 fatal_errno("cannot create write_transport thread");
Mike Lockwood0927bf92009-08-08 12:37:44 -0400551 }
552
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700553 if (!adb_thread_create(read_transport_thread, t)) {
554 fatal_errno("cannot create read_transport thread");
Mike Lockwood0927bf92009-08-08 12:37:44 -0400555 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 }
557
Josh Gao0cd3ae12016-09-21 12:37:10 -0700558 {
559 std::lock_guard<std::mutex> lock(transport_lock);
560 pending_list.remove(t);
561 transport_list.push_front(t);
562 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564 update_transports();
565}
566
567void init_transport_registration(void)
568{
569 int s[2];
570
571 if(adb_socketpair(s)){
572 fatal_errno("cannot open transport registration socketpair");
573 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700574 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575
576 transport_registration_send = s[0];
577 transport_registration_recv = s[1];
578
579 fdevent_install(&transport_registration_fde,
580 transport_registration_recv,
581 transport_registration_func,
582 0);
583
584 fdevent_set(&transport_registration_fde, FDE_READ);
585}
586
587/* the fdevent select pump is single threaded */
588static void register_transport(atransport *transport)
589{
590 tmsg m;
591 m.transport = transport;
592 m.action = 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700593 D("transport: %s registered", transport->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800594 if(transport_write_action(transport_registration_send, &m)) {
595 fatal_errno("cannot write transport registration socket\n");
596 }
597}
598
599static void remove_transport(atransport *transport)
600{
601 tmsg m;
602 m.transport = transport;
603 m.action = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700604 D("transport: %s removed", transport->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605 if(transport_write_action(transport_registration_send, &m)) {
606 fatal_errno("cannot write transport registration socket\n");
607 }
608}
609
610
Yabin Cuif4b99282015-08-27 12:03:11 -0700611static void transport_unref(atransport* t) {
612 CHECK(t != nullptr);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700613
614 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700615 CHECK_GT(t->ref_count, 0u);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400616 t->ref_count--;
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400617 if (t->ref_count == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700618 D("transport: %s unref (kicking and closing)", t->serial);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400619 t->close(t);
620 remove_transport(t);
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100621 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700622 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400623 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800624}
625
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700626static int qual_match(const char *to_test,
Elliott Hughes09a45a12015-04-03 16:12:15 -0700627 const char *prefix, const char *qual, bool sanitize_qual)
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700628{
629 if (!to_test || !*to_test)
630 /* Return true if both the qual and to_test are null strings. */
631 return !qual || !*qual;
632
633 if (!qual)
634 return 0;
635
636 if (prefix) {
637 while (*prefix) {
638 if (*prefix++ != *to_test++)
639 return 0;
640 }
641 }
642
643 while (*qual) {
644 char ch = *qual++;
Elliott Hughes09a45a12015-04-03 16:12:15 -0700645 if (sanitize_qual && !isalnum(ch))
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700646 ch = '_';
647 if (ch != *to_test++)
648 return 0;
649 }
650
651 /* Everything matched so far. Return true if *to_test is a NUL. */
652 return !*to_test;
653}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654
Elliott Hughes8d28e192015-10-07 14:55:10 -0700655atransport* acquire_one_transport(TransportType type, const char* serial,
656 bool* is_ambiguous, std::string* error_out) {
657 atransport* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658
Elliott Hughes8d28e192015-10-07 14:55:10 -0700659 if (serial) {
660 *error_out = android::base::StringPrintf("device '%s' not found", serial);
661 } else if (type == kTransportLocal) {
662 *error_out = "no emulators found";
663 } else if (type == kTransportAny) {
664 *error_out = "no devices/emulators found";
665 } else {
666 *error_out = "no devices found";
667 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668
Josh Gao0cd3ae12016-09-21 12:37:10 -0700669 std::unique_lock<std::mutex> lock(transport_lock);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700670 for (const auto& t : transport_list) {
Dan Albertdcd78a12015-05-18 16:43:57 -0700671 if (t->connection_state == kCsNoPerm) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800672#if ADB_HOST
David Purselld2acbd12015-12-02 15:14:31 -0800673 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes1b708d32015-12-11 19:07:01 -0800674#endif
Mike Lockwood37d31112009-08-08 13:53:16 -0400675 continue;
676 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400677
Elliott Hughes8d28e192015-10-07 14:55:10 -0700678 // Check for matching serial number.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679 if (serial) {
David Pursell3f902aa2016-03-01 08:58:26 -0800680 if (t->MatchesTarget(serial)) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700681 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700682 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700683 if (is_ambiguous) *is_ambiguous = true;
684 result = nullptr;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700685 break;
686 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800687 result = t;
Scott Andersone109d262012-04-20 11:21:14 -0700688 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 } else {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700690 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700692 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700693 if (is_ambiguous) *is_ambiguous = true;
694 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 break;
696 }
697 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700698 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700700 *error_out = "more than one emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700701 if (is_ambiguous) *is_ambiguous = true;
702 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703 break;
704 }
705 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700706 } else if (type == kTransportAny) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700708 *error_out = "more than one device/emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700709 if (is_ambiguous) *is_ambiguous = true;
710 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 break;
712 }
713 result = t;
714 }
715 }
716 }
Josh Gao0cd3ae12016-09-21 12:37:10 -0700717 lock.unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718
Elliott Hughes8d28e192015-10-07 14:55:10 -0700719 // Don't return unauthorized devices; the caller can't do anything with them.
720 if (result && result->connection_state == kCsUnauthorized) {
721 *error_out = "device unauthorized.\n";
722 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
723 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
724 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
725 *error_out += "\n";
726 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
727 *error_out += "Otherwise check for a confirmation dialog on your device.";
728 result = nullptr;
729 }
Benoit Goby77e8e582013-01-15 12:36:47 -0800730
Elliott Hughes8d28e192015-10-07 14:55:10 -0700731 // Don't return offline devices; the caller can't do anything with them.
732 if (result && result->connection_state == kCsOffline) {
733 *error_out = "device offline";
734 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800735 }
736
737 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700738 *error_out = "success";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800739 }
740
741 return result;
742}
743
Yabin Cui7f274902016-04-18 11:22:34 -0700744void atransport::Kick() {
745 if (!kicked_) {
746 kicked_ = true;
747 CHECK(kick_func_ != nullptr);
748 kick_func_(this);
749 }
750}
751
David Purselld2acbd12015-12-02 15:14:31 -0800752const std::string atransport::connection_state_name() const {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700753 switch (connection_state) {
David Purselld2acbd12015-12-02 15:14:31 -0800754 case kCsOffline: return "offline";
755 case kCsBootloader: return "bootloader";
756 case kCsDevice: return "device";
757 case kCsHost: return "host";
758 case kCsRecovery: return "recovery";
Elliott Hughes1b708d32015-12-11 19:07:01 -0800759 case kCsNoPerm: return UsbNoPermissionsShortHelpText();
David Purselld2acbd12015-12-02 15:14:31 -0800760 case kCsSideload: return "sideload";
761 case kCsUnauthorized: return "unauthorized";
762 default: return "unknown";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800763 }
764}
765
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100766void atransport::update_version(int version, size_t payload) {
767 protocol_version = std::min(version, A_VERSION);
768 max_payload = std::min(payload, MAX_PAYLOAD);
769}
770
771int atransport::get_protocol_version() const {
772 return protocol_version;
773}
774
775size_t atransport::get_max_payload() const {
776 return max_payload;
777}
778
David Pursell4e2fd362015-09-22 10:43:08 -0700779namespace {
David Pursell0955c662015-08-31 10:42:13 -0700780
David Pursell4e2fd362015-09-22 10:43:08 -0700781constexpr char kFeatureStringDelimiter = ',';
782
783} // namespace
Dan Albert1792c232015-05-18 13:06:53 -0700784
785const FeatureSet& supported_features() {
David Pursell4e2fd362015-09-22 10:43:08 -0700786 // Local static allocation to avoid global non-POD variables.
787 static const FeatureSet* features = new FeatureSet{
Todd Kennedy51c05ec2015-11-10 00:03:25 +0000788 kFeatureShell2,
789 kFeatureCmd
David Pursellbbe3d212015-09-25 08:37:13 -0700790 // Increment ADB_SERVER_VERSION whenever the feature list changes to
791 // make sure that the adb client and server features stay in sync
792 // (http://b/24370690).
David Pursell4e2fd362015-09-22 10:43:08 -0700793 };
794
795 return *features;
796}
797
798std::string FeatureSetToString(const FeatureSet& features) {
799 return android::base::Join(features, kFeatureStringDelimiter);
800}
801
802FeatureSet StringToFeatureSet(const std::string& features_string) {
David Purselld2b588e2015-09-25 13:04:21 -0700803 if (features_string.empty()) {
804 return FeatureSet();
805 }
806
David Pursell4e2fd362015-09-22 10:43:08 -0700807 auto names = android::base::Split(features_string,
808 {kFeatureStringDelimiter});
809 return FeatureSet(names.begin(), names.end());
Dan Albert1792c232015-05-18 13:06:53 -0700810}
811
David Pursell70ef7b42015-09-30 13:35:42 -0700812bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
813 return feature_set.count(feature) > 0 &&
814 supported_features().count(feature) > 0;
815}
816
Dan Albert1792c232015-05-18 13:06:53 -0700817bool atransport::has_feature(const std::string& feature) const {
818 return features_.count(feature) > 0;
819}
820
David Pursell4e2fd362015-09-22 10:43:08 -0700821void atransport::SetFeatures(const std::string& features_string) {
822 features_ = StringToFeatureSet(features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700823}
824
Yabin Cuib3298242015-08-28 15:09:44 -0700825void atransport::AddDisconnect(adisconnect* disconnect) {
826 disconnects_.push_back(disconnect);
827}
828
829void atransport::RemoveDisconnect(adisconnect* disconnect) {
830 disconnects_.remove(disconnect);
831}
832
833void atransport::RunDisconnects() {
Elliott Hughes65fe2512015-10-07 15:59:35 -0700834 for (const auto& disconnect : disconnects_) {
Yabin Cuib3298242015-08-28 15:09:44 -0700835 disconnect->func(disconnect->opaque, this);
836 }
837 disconnects_.clear();
838}
839
David Pursell3f902aa2016-03-01 08:58:26 -0800840bool atransport::MatchesTarget(const std::string& target) const {
841 if (serial) {
842 if (target == serial) {
843 return true;
844 } else if (type == kTransportLocal) {
845 // Local transports can match [tcp:|udp:]<hostname>[:port].
846 const char* local_target_ptr = target.c_str();
847
848 // For fastboot compatibility, ignore protocol prefixes.
849 if (android::base::StartsWith(target, "tcp:") ||
850 android::base::StartsWith(target, "udp:")) {
851 local_target_ptr += 4;
852 }
853
854 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
855 std::string serial_host, error;
856 int serial_port = -1;
857 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr,
858 &error)) {
859 // |target| may omit the port to default to ours.
860 std::string target_host;
861 int target_port = serial_port;
862 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
863 nullptr, &error) &&
864 serial_host == target_host && serial_port == target_port) {
865 return true;
866 }
867 }
868 }
869 }
870
871 return (devpath && target == devpath) ||
872 qual_match(target.c_str(), "product:", product, false) ||
873 qual_match(target.c_str(), "model:", model, true) ||
874 qual_match(target.c_str(), "device:", device, false);
875}
876
Elliott Hughese67f1f82015-04-30 17:32:03 -0700877#if ADB_HOST
878
Dan Albertd99d9022015-05-06 16:48:52 -0700879static void append_transport_info(std::string* result, const char* key,
880 const char* value, bool sanitize) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700881 if (value == nullptr || *value == '\0') {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700882 return;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700883 }
884
Elliott Hughese67f1f82015-04-30 17:32:03 -0700885 *result += ' ';
886 *result += key;
887
888 for (const char* p = value; *p; ++p) {
889 result->push_back((!sanitize || isalnum(*p)) ? *p : '_');
890 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700891}
892
Dan Albertc7915a32015-05-18 16:46:31 -0700893static void append_transport(const atransport* t, std::string* result,
894 bool long_listing) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700895 const char* serial = t->serial;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700896 if (!serial || !serial[0]) {
Dan Albertd99d9022015-05-06 16:48:52 -0700897 serial = "(no serial number)";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700898 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700899
900 if (!long_listing) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700901 *result += serial;
902 *result += '\t';
903 *result += t->connection_state_name();
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700904 } else {
David Purselld2acbd12015-12-02 15:14:31 -0800905 android::base::StringAppendF(result, "%-22s %s", serial,
906 t->connection_state_name().c_str());
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700907
Elliott Hughese67f1f82015-04-30 17:32:03 -0700908 append_transport_info(result, "", t->devpath, false);
909 append_transport_info(result, "product:", t->product, false);
910 append_transport_info(result, "model:", t->model, true);
911 append_transport_info(result, "device:", t->device, false);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700912 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700913 *result += '\n';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700914}
915
Elliott Hughese67f1f82015-04-30 17:32:03 -0700916std::string list_transports(bool long_listing) {
917 std::string result;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700918
919 std::lock_guard<std::mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700920 for (const auto& t : transport_list) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700921 append_transport(t, &result, long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800922 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700923 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800924}
925
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800926/* hack for osx */
Dan Albertc7915a32015-05-18 16:46:31 -0700927void close_usb_devices() {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700928 std::lock_guard<std::mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700929 for (const auto& t : transport_list) {
Yabin Cui7f274902016-04-18 11:22:34 -0700930 t->Kick();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800931 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800932}
933#endif // ADB_HOST
934
Dan Albertc7915a32015-05-18 16:46:31 -0700935int register_socket_transport(int s, const char *serial, int port, int local) {
936 atransport* t = new atransport();
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100937
938 if (!serial) {
Dan Albertc7915a32015-05-18 16:46:31 -0700939 char buf[32];
940 snprintf(buf, sizeof(buf), "T-%p", t);
941 serial = buf;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100942 }
Dan Albertc7915a32015-05-18 16:46:31 -0700943
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700944 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700945 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertc7915a32015-05-18 16:46:31 -0700946 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700947 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800948 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700949
Josh Gao0cd3ae12016-09-21 12:37:10 -0700950 std::unique_lock<std::mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700951 for (const auto& transport : pending_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700952 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700953 VLOG(TRANSPORT) << "socket transport " << transport->serial
954 << " is already in pending_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -0700955 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700956 return -1;
957 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800958 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700959
Elliott Hughes65fe2512015-10-07 15:59:35 -0700960 for (const auto& transport : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700961 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700962 VLOG(TRANSPORT) << "socket transport " << transport->serial
963 << " is already in transport_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -0700964 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700965 return -1;
966 }
967 }
968
Dan Albertc7915a32015-05-18 16:46:31 -0700969 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700970 t->serial = strdup(serial);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700971
972 lock.unlock();
Benoit Goby1c45ee92013-03-29 18:22:36 -0700973
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800974 register_transport(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700975 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800976}
977
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400978#if ADB_HOST
Dan Albertc7915a32015-05-18 16:46:31 -0700979atransport *find_transport(const char *serial) {
980 atransport* result = nullptr;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400981
Josh Gao0cd3ae12016-09-21 12:37:10 -0700982 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700983 for (auto& t : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700984 if (t->serial && strcmp(serial, t->serial) == 0) {
985 result = t;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400986 break;
987 }
Dan Albertc7915a32015-05-18 16:46:31 -0700988 }
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400989
Dan Albertc7915a32015-05-18 16:46:31 -0700990 return result;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400991}
992
Yabin Cuif4b99282015-08-27 12:03:11 -0700993void kick_all_tcp_devices() {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700994 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700995 for (auto& t : transport_list) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700996 if (t->IsTcpDevice()) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700997 // Kicking breaks the read_transport thread of this transport out of any read, then
998 // the read_transport thread will notify the main thread to make this transport
999 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cuif4b99282015-08-27 12:03:11 -07001000 // Finally, this transport will be closed and freed in the main thread.
Yabin Cui7f274902016-04-18 11:22:34 -07001001 t->Kick();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001002 }
Dan Albertc7915a32015-05-18 16:46:31 -07001003 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001004}
1005
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001006#endif
1007
Dan Albertc7915a32015-05-18 16:46:31 -07001008void register_usb_transport(usb_handle* usb, const char* serial,
1009 const char* devpath, unsigned writeable) {
1010 atransport* t = new atransport();
1011
Yabin Cui7a3f8d62015-09-02 17:44:28 -07001012 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001013 serial ? serial : "");
Dan Albertdcd78a12015-05-18 16:43:57 -07001014 init_usb_transport(t, usb, (writeable ? kCsOffline : kCsNoPerm));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001015 if(serial) {
1016 t->serial = strdup(serial);
1017 }
Dan Albertc7915a32015-05-18 16:46:31 -07001018
1019 if (devpath) {
Scott Andersone109d262012-04-20 11:21:14 -07001020 t->devpath = strdup(devpath);
1021 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001022
Josh Gao0cd3ae12016-09-21 12:37:10 -07001023 {
1024 std::lock_guard<std::mutex> lock(transport_lock);
1025 pending_list.push_front(t);
1026 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001027
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001028 register_transport(t);
1029}
1030
Dan Albertdcd78a12015-05-18 16:43:57 -07001031// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albertc7915a32015-05-18 16:46:31 -07001032void unregister_usb_transport(usb_handle *usb) {
Josh Gao0cd3ae12016-09-21 12:37:10 -07001033 std::lock_guard<std::mutex> lock(transport_lock);
Dan Albertc7915a32015-05-18 16:46:31 -07001034 transport_list.remove_if([usb](atransport* t) {
1035 return t->usb == usb && t->connection_state == kCsNoPerm;
1036 });
Mike Lockwood0927bf92009-08-08 12:37:44 -04001037}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001038
Tamas Berghammer3d2904c2015-07-13 19:12:28 +01001039int check_header(apacket *p, atransport *t)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001040{
1041 if(p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cuiaed3c612015-09-22 15:52:57 -07001042 VLOG(RWX) << "check_header(): invalid magic";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001043 return -1;
1044 }
1045
Tamas Berghammer3d2904c2015-07-13 19:12:28 +01001046 if(p->msg.data_length > t->get_max_payload()) {
Yabin Cuiaed3c612015-09-22 15:52:57 -07001047 VLOG(RWX) << "check_header(): " << p->msg.data_length << " atransport::max_payload = "
1048 << t->get_max_payload();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001049 return -1;
1050 }
1051
1052 return 0;
1053}
1054
1055int check_data(apacket *p)
1056{
1057 unsigned count, sum;
1058 unsigned char *x;
1059
1060 count = p->msg.data_length;
1061 x = p->data;
1062 sum = 0;
1063 while(count-- > 0) {
1064 sum += *x++;
1065 }
1066
1067 if(sum != p->msg.data_check) {
1068 return -1;
1069 } else {
1070 return 0;
1071 }
1072}
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001073
Josh Gao3bd28792016-10-05 19:02:29 -07001074#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -07001075std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001076 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1077
Josh Gao2e671202016-08-18 22:00:12 -07001078 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001079 keys_.pop_front();
1080 return result;
1081}
Josh Gao3bd28792016-10-05 19:02:29 -07001082#endif