blob: 3b56c55e04e6fc86951b0a1c81a6ef8c225b7a9c [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
Yabin Cuib3298242015-08-28 15:09:44 -070022#include <list>
Elliott Hughes7be29c82015-04-16 22:54:44 -070023#include <string>
Dan Albert1792c232015-05-18 13:06:53 -070024#include <unordered_set>
Dan Albert76649012015-02-24 15:51:19 -080025
Elliott Hughes7be29c82015-04-16 22:54:44 -070026#include "adb.h"
Dan Albert630b9af2014-11-24 23:34:35 -080027
Dan Albert1792c232015-05-18 13:06:53 -070028typedef std::unordered_set<std::string> FeatureSet;
29
30const FeatureSet& supported_features();
31
32class atransport {
33public:
34 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
35 // historically just a struct, but making the whole thing a more idiomatic
36 // class in one go is a very large change. Given how bad our testing is,
37 // it's better to do this piece by piece.
38
39 atransport() {
40 auth_fde = {};
41 transport_fde = {};
42 protocol_version = A_VERSION;
43 max_payload = MAX_PAYLOAD;
44 }
45
46 virtual ~atransport() {}
47
48 int (*read_from_remote)(apacket* p, atransport* t) = nullptr;
49 int (*write_to_remote)(apacket* p, atransport* t) = nullptr;
50 void (*close)(atransport* t) = nullptr;
51 void (*kick)(atransport* t) = nullptr;
52
53 int fd = -1;
54 int transport_socket = -1;
55 fdevent transport_fde;
Yabin Cuif4b99282015-08-27 12:03:11 -070056 size_t ref_count = 0;
Dan Albert1792c232015-05-18 13:06:53 -070057 uint32_t sync_token = 0;
58 ConnectionState connection_state = kCsOffline;
59 bool online = false;
60 TransportType type = kTransportAny;
61
62 // USB handle or socket fd as needed.
63 usb_handle* usb = nullptr;
64 int sfd = -1;
65
66 // Used to identify transports for clients.
67 char* serial = nullptr;
68 char* product = nullptr;
69 char* model = nullptr;
70 char* device = nullptr;
71 char* devpath = nullptr;
72 int adb_port = -1; // Use for emulators (local transport)
73 bool kicked = false;
74
Dan Albert1792c232015-05-18 13:06:53 -070075 void* key = nullptr;
76 unsigned char token[TOKEN_SIZE] = {};
77 fdevent auth_fde;
78 size_t failed_auth_attempts = 0;
79
80 const char* connection_state_name() const;
81
82 void update_version(int version, size_t payload);
83 int get_protocol_version() const;
84 size_t get_max_payload() const;
85
86 inline const FeatureSet features() const {
87 return features_;
88 }
89
90 bool has_feature(const std::string& feature) const;
91 void add_feature(const std::string& feature);
92
93 // Returns true if both we and the other end of the transport support the
94 // feature.
95 bool CanUseFeature(const std::string& feature) const;
96
Yabin Cuib3298242015-08-28 15:09:44 -070097 void AddDisconnect(adisconnect* disconnect);
98 void RemoveDisconnect(adisconnect* disconnect);
99 void RunDisconnects();
100
Dan Albert1792c232015-05-18 13:06:53 -0700101private:
102 // A set of features transmitted in the banner with the initial connection.
103 // This is stored in the banner as 'features=feature0,feature1,etc'.
104 FeatureSet features_;
105 int protocol_version;
106 size_t max_payload;
107
Yabin Cuib3298242015-08-28 15:09:44 -0700108 // A list of adisconnect callbacks called when the transport is kicked.
109 std::list<adisconnect*> disconnects_;
110
Dan Albert1792c232015-05-18 13:06:53 -0700111 DISALLOW_COPY_AND_ASSIGN(atransport);
112};
113
Dan Albert76649012015-02-24 15:51:19 -0800114/*
115 * Obtain a transport from the available transports.
Dan Albertdcd78a12015-05-18 16:43:57 -0700116 * If state is != kCsAny, only transports in that state are considered.
Dan Albert76649012015-02-24 15:51:19 -0800117 * If serial is non-NULL then only the device with that serial will be chosen.
118 * If no suitable transport is found, error is set.
119 */
Dan Albertdcd78a12015-05-18 16:43:57 -0700120atransport* acquire_one_transport(ConnectionState state, TransportType type,
Elliott Hughes7be29c82015-04-16 22:54:44 -0700121 const char* serial, std::string* error_out);
Dan Albert76649012015-02-24 15:51:19 -0800122void kick_transport(atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800123void update_transports(void);
124
Dan Albert76649012015-02-24 15:51:19 -0800125void init_transport_registration(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700126std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800127atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700128void kick_all_tcp_devices();
Dan Albert76649012015-02-24 15:51:19 -0800129
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
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100139int check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800140int check_data(apacket* p);
141
142/* for MacOS X cleanup */
143void close_usb_devices();
144
145void send_packet(apacket* p, atransport* t);
146
147asocket* create_device_tracker(void);
148
JP Abgrall408fa572011-03-16 15:57:42 -0700149#endif /* __TRANSPORT_H */