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