blob: 999922a381539572082be8acdb1d660ad0ad8564 [file] [log] [blame]
JP Abgrall2e5dd6e2011-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 Albert020292b2015-02-18 18:03:26 -080020#include <sys/types.h>
21
Yabin Cui2d4c1982015-08-28 15:09:44 -070022#include <list>
Elliott Hughesab882422015-04-16 22:54:44 -070023#include <string>
Dan Albertbe8e54b2015-05-18 13:06:53 -070024#include <unordered_set>
Dan Albertb302d122015-02-24 15:51:19 -080025
Elliott Hughesab882422015-04-16 22:54:44 -070026#include "adb.h"
Dan Albertbbbbea62014-11-24 23:34:35 -080027
Dan Albertbe8e54b2015-05-18 13:06:53 -070028typedef std::unordered_set<std::string> FeatureSet;
29
30const FeatureSet& supported_features();
31
David Pursell8da19a42015-08-31 10:42:13 -070032const extern char kFeatureShell2[];
33
Dan Albertbe8e54b2015-05-18 13:06:53 -070034class atransport {
35public:
36 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
37 // historically just a struct, but making the whole thing a more idiomatic
38 // class in one go is a very large change. Given how bad our testing is,
39 // it's better to do this piece by piece.
40
41 atransport() {
42 auth_fde = {};
43 transport_fde = {};
44 protocol_version = A_VERSION;
45 max_payload = MAX_PAYLOAD;
46 }
47
48 virtual ~atransport() {}
49
50 int (*read_from_remote)(apacket* p, atransport* t) = nullptr;
51 int (*write_to_remote)(apacket* p, atransport* t) = nullptr;
52 void (*close)(atransport* t) = nullptr;
53 void (*kick)(atransport* t) = nullptr;
54
55 int fd = -1;
56 int transport_socket = -1;
57 fdevent transport_fde;
Yabin Cui4d64fd82015-08-27 12:03:11 -070058 size_t ref_count = 0;
Dan Albertbe8e54b2015-05-18 13:06:53 -070059 uint32_t sync_token = 0;
60 ConnectionState connection_state = kCsOffline;
61 bool online = false;
62 TransportType type = kTransportAny;
63
64 // USB handle or socket fd as needed.
65 usb_handle* usb = nullptr;
66 int sfd = -1;
67
68 // Used to identify transports for clients.
69 char* serial = nullptr;
70 char* product = nullptr;
71 char* model = nullptr;
72 char* device = nullptr;
73 char* devpath = nullptr;
74 int adb_port = -1; // Use for emulators (local transport)
75 bool kicked = false;
76
Dan Albertbe8e54b2015-05-18 13:06:53 -070077 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
Yabin Cui2d4c1982015-08-28 15:09:44 -070099 void AddDisconnect(adisconnect* disconnect);
100 void RemoveDisconnect(adisconnect* disconnect);
101 void RunDisconnects();
102
Dan Albertbe8e54b2015-05-18 13:06:53 -0700103private:
104 // A set of features transmitted in the banner with the initial connection.
105 // This is stored in the banner as 'features=feature0,feature1,etc'.
106 FeatureSet features_;
107 int protocol_version;
108 size_t max_payload;
109
Yabin Cui2d4c1982015-08-28 15:09:44 -0700110 // A list of adisconnect callbacks called when the transport is kicked.
111 std::list<adisconnect*> disconnects_;
112
Dan Albertbe8e54b2015-05-18 13:06:53 -0700113 DISALLOW_COPY_AND_ASSIGN(atransport);
114};
115
Dan Albertb302d122015-02-24 15:51:19 -0800116/*
117 * Obtain a transport from the available transports.
Dan Albert9a50f4c2015-05-18 16:43:57 -0700118 * If state is != kCsAny, only transports in that state are considered.
Dan Albertb302d122015-02-24 15:51:19 -0800119 * If serial is non-NULL then only the device with that serial will be chosen.
120 * If no suitable transport is found, error is set.
121 */
Dan Albert9a50f4c2015-05-18 16:43:57 -0700122atransport* acquire_one_transport(ConnectionState state, TransportType type,
Elliott Hughesab882422015-04-16 22:54:44 -0700123 const char* serial, std::string* error_out);
Dan Albertb302d122015-02-24 15:51:19 -0800124void kick_transport(atransport* t);
Dan Albertb302d122015-02-24 15:51:19 -0800125void update_transports(void);
126
Dan Albertb302d122015-02-24 15:51:19 -0800127void init_transport_registration(void);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700128std::string list_transports(bool long_listing);
Dan Albertb302d122015-02-24 15:51:19 -0800129atransport* find_transport(const char* serial);
Yabin Cui4d64fd82015-08-27 12:03:11 -0700130void kick_all_tcp_devices();
Dan Albertb302d122015-02-24 15:51:19 -0800131
132void register_usb_transport(usb_handle* h, const char* serial,
133 const char* devpath, unsigned writeable);
134
135/* cause new transports to be init'd and added to the list */
136int register_socket_transport(int s, const char* serial, int port, int local);
137
Dan Albert9a50f4c2015-05-18 16:43:57 -0700138// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albertb302d122015-02-24 15:51:19 -0800139void unregister_usb_transport(usb_handle* usb);
140
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100141int check_header(apacket* p, atransport* t);
Dan Albertb302d122015-02-24 15:51:19 -0800142int check_data(apacket* p);
143
144/* for MacOS X cleanup */
145void close_usb_devices();
146
147void send_packet(apacket* p, atransport* t);
148
149asocket* create_device_tracker(void);
150
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700151#endif /* __TRANSPORT_H */