1 | //===-- AdbClient.h ---------------------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_ADBCLIENT_H |
10 | #define LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_ADBCLIENT_H |
11 | |
12 | #include "lldb/Utility/Status.h" |
13 | #include <chrono> |
14 | #include <functional> |
15 | #include <list> |
16 | #include <memory> |
17 | #include <string> |
18 | #include <vector> |
19 | |
20 | namespace lldb_private { |
21 | |
22 | class FileSpec; |
23 | |
24 | namespace platform_android { |
25 | |
26 | class AdbClient { |
27 | public: |
28 | enum UnixSocketNamespace { |
29 | UnixSocketNamespaceAbstract, |
30 | UnixSocketNamespaceFileSystem, |
31 | }; |
32 | |
33 | using DeviceIDList = std::list<std::string>; |
34 | |
35 | class SyncService { |
36 | friend class AdbClient; |
37 | |
38 | public: |
39 | virtual ~SyncService(); |
40 | |
41 | virtual Status PullFile(const FileSpec &remote_file, |
42 | const FileSpec &local_file); |
43 | |
44 | Status PushFile(const FileSpec &local_file, const FileSpec &remote_file); |
45 | |
46 | virtual Status Stat(const FileSpec &remote_file, uint32_t &mode, |
47 | uint32_t &size, uint32_t &mtime); |
48 | |
49 | bool IsConnected() const; |
50 | |
51 | protected: |
52 | explicit SyncService(std::unique_ptr<Connection> &&conn); |
53 | |
54 | private: |
55 | Status SendSyncRequest(const char *request_id, const uint32_t data_len, |
56 | const void *data); |
57 | |
58 | Status ReadSyncHeader(std::string &response_id, uint32_t &data_len); |
59 | |
60 | Status PullFileChunk(std::vector<char> &buffer, bool &eof); |
61 | |
62 | Status ReadAllBytes(void *buffer, size_t size); |
63 | |
64 | Status internalPullFile(const FileSpec &remote_file, |
65 | const FileSpec &local_file); |
66 | |
67 | Status internalPushFile(const FileSpec &local_file, |
68 | const FileSpec &remote_file); |
69 | |
70 | Status internalStat(const FileSpec &remote_file, uint32_t &mode, |
71 | uint32_t &size, uint32_t &mtime); |
72 | |
73 | Status executeCommand(const std::function<Status()> &cmd); |
74 | |
75 | std::unique_ptr<Connection> m_conn; |
76 | }; |
77 | |
78 | static Status CreateByDeviceID(const std::string &device_id, AdbClient &adb); |
79 | |
80 | AdbClient(); |
81 | explicit AdbClient(const std::string &device_id); |
82 | |
83 | virtual ~AdbClient(); |
84 | |
85 | const std::string &GetDeviceID() const; |
86 | |
87 | Status GetDevices(DeviceIDList &device_list); |
88 | |
89 | Status SetPortForwarding(const uint16_t local_port, |
90 | const uint16_t remote_port); |
91 | |
92 | Status SetPortForwarding(const uint16_t local_port, |
93 | llvm::StringRef remote_socket_name, |
94 | const UnixSocketNamespace socket_namespace); |
95 | |
96 | Status DeletePortForwarding(const uint16_t local_port); |
97 | |
98 | Status Shell(const char *command, std::chrono::milliseconds timeout, |
99 | std::string *output); |
100 | |
101 | virtual Status ShellToFile(const char *command, |
102 | std::chrono::milliseconds timeout, |
103 | const FileSpec &output_file_spec); |
104 | |
105 | virtual std::unique_ptr<SyncService> GetSyncService(Status &error); |
106 | |
107 | Status SwitchDeviceTransport(); |
108 | |
109 | private: |
110 | Status Connect(); |
111 | |
112 | void SetDeviceID(const std::string &device_id); |
113 | |
114 | Status SendMessage(const std::string &packet, const bool reconnect = true); |
115 | |
116 | Status SendDeviceMessage(const std::string &packet); |
117 | |
118 | Status ReadMessage(std::vector<char> &message); |
119 | |
120 | Status ReadMessageStream(std::vector<char> &message, |
121 | std::chrono::milliseconds timeout); |
122 | |
123 | Status GetResponseError(const char *response_id); |
124 | |
125 | Status ReadResponseStatus(); |
126 | |
127 | Status Sync(); |
128 | |
129 | Status StartSync(); |
130 | |
131 | Status internalShell(const char *command, std::chrono::milliseconds timeout, |
132 | std::vector<char> &output_buf); |
133 | |
134 | Status ReadAllBytes(void *buffer, size_t size); |
135 | |
136 | std::string m_device_id; |
137 | std::unique_ptr<Connection> m_conn; |
138 | }; |
139 | |
140 | } // namespace platform_android |
141 | } // namespace lldb_private |
142 | |
143 | #endif // LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_ADBCLIENT_H |
144 | |