blob: e809407c5057e237aa7b622e21c6d0b6b3d7842e [file] [log] [blame]
JP Abgrall408fa572011-03-16 15:57:42 -07001/*
2 * Copyright (C) 2011 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
17#ifndef __TRANSPORT_H
18#define __TRANSPORT_H
19
Dan Alberte9fca142015-02-18 18:03:26 -080020#include <sys/types.h>
21
Elliott Hughes7be29c82015-04-16 22:54:44 -070022#include <string>
Dan Albert1792c232015-05-18 13:06:53 -070023#include <unordered_set>
Dan Albert76649012015-02-24 15:51:19 -080024
Elliott Hughes7be29c82015-04-16 22:54:44 -070025#include "adb.h"
Dan Albert630b9af2014-11-24 23:34:35 -080026
Dan Albert1792c232015-05-18 13:06:53 -070027typedef std::unordered_set<std::string> FeatureSet;
28
29const FeatureSet& supported_features();
30
31class atransport {
32public:
33 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
34 // historically just a struct, but making the whole thing a more idiomatic
35 // class in one go is a very large change. Given how bad our testing is,
36 // it's better to do this piece by piece.
37
38 atransport() {
39 auth_fde = {};
40 transport_fde = {};
41 protocol_version = A_VERSION;
42 max_payload = MAX_PAYLOAD;
43 }
44
45 virtual ~atransport() {}
46
47 int (*read_from_remote)(apacket* p, atransport* t) = nullptr;
48 int (*write_to_remote)(apacket* p, atransport* t) = nullptr;
49 void (*close)(atransport* t) = nullptr;
50 void (*kick)(atransport* t) = nullptr;
51
52 int fd = -1;
53 int transport_socket = -1;
54 fdevent transport_fde;
55 int ref_count = 0;
56 uint32_t sync_token = 0;
57 ConnectionState connection_state = kCsOffline;
58 bool online = false;
59 TransportType type = kTransportAny;
60
61 // USB handle or socket fd as needed.
62 usb_handle* usb = nullptr;
63 int sfd = -1;
64
65 // Used to identify transports for clients.
66 char* serial = nullptr;
67 char* product = nullptr;
68 char* model = nullptr;
69 char* device = nullptr;
70 char* devpath = nullptr;
71 int adb_port = -1; // Use for emulators (local transport)
72 bool kicked = false;
73
74 // A list of adisconnect callbacks called when the transport is kicked.
75 adisconnect disconnects = {};
76
77 void* key = nullptr;
78 unsigned char token[TOKEN_SIZE] = {};
79 fdevent auth_fde;
80 size_t failed_auth_attempts = 0;
81
82 const char* connection_state_name() const;
83
84 void update_version(int version, size_t payload);
85 int get_protocol_version() const;
86 size_t get_max_payload() const;
87
88 inline const FeatureSet features() const {
89 return features_;
90 }
91
92 bool has_feature(const std::string& feature) const;
93 void add_feature(const std::string& feature);
94
95 // Returns true if both we and the other end of the transport support the
96 // feature.
97 bool CanUseFeature(const std::string& feature) const;
98
99private:
100 // A set of features transmitted in the banner with the initial connection.
101 // This is stored in the banner as 'features=feature0,feature1,etc'.
102 FeatureSet features_;
103 int protocol_version;
104 size_t max_payload;
105
106 DISALLOW_COPY_AND_ASSIGN(atransport);
107};
108
Dan Albert76649012015-02-24 15:51:19 -0800109/*
110 * Obtain a transport from the available transports.
Dan Albertdcd78a12015-05-18 16:43:57 -0700111 * If state is != kCsAny, only transports in that state are considered.
Dan Albert76649012015-02-24 15:51:19 -0800112 * If serial is non-NULL then only the device with that serial will be chosen.
113 * If no suitable transport is found, error is set.
114 */
Dan Albertdcd78a12015-05-18 16:43:57 -0700115atransport* acquire_one_transport(ConnectionState state, TransportType type,
Elliott Hughes7be29c82015-04-16 22:54:44 -0700116 const char* serial, std::string* error_out);
Dan Albert76649012015-02-24 15:51:19 -0800117void add_transport_disconnect(atransport* t, adisconnect* dis);
118void remove_transport_disconnect(atransport* t, adisconnect* dis);
119void kick_transport(atransport* t);
120void run_transport_disconnects(atransport* t);
121void update_transports(void);
122
123/* transports are ref-counted
124** get_device_transport does an acquire on your behalf before returning
125*/
126void init_transport_registration(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700127std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800128atransport* find_transport(const char* serial);
129
130void register_usb_transport(usb_handle* h, const char* serial,
131 const char* devpath, unsigned writeable);
132
133/* cause new transports to be init'd and added to the list */
134int register_socket_transport(int s, const char* serial, int port, int local);
135
Dan Albertdcd78a12015-05-18 16:43:57 -0700136// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albert76649012015-02-24 15:51:19 -0800137void unregister_usb_transport(usb_handle* usb);
138
139/* these should only be used for the "adb disconnect" command */
140void unregister_transport(atransport* t);
141void unregister_all_tcp_transports();
142
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100143int check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800144int check_data(apacket* p);
145
146/* for MacOS X cleanup */
147void close_usb_devices();
148
149void send_packet(apacket* p, atransport* t);
150
151asocket* create_device_tracker(void);
152
JP Abgrall408fa572011-03-16 15:57:42 -0700153#endif /* __TRANSPORT_H */