-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdbClient.cpp
319 lines (262 loc) · 8.82 KB
/
AdbClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "pch.h"
#include "AdbClient.h"
#include "CommandExecution.h"
#include <fstream>
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <string>
#include <tlhelp32.h>
#pragma comment(lib, "ws2_32.lib")
AdbClient::AdbClient()
{
}
AdbClient::~AdbClient()
{
cleanupWsa();
}
// 终端命令执行的方式(powershell)异步执行adb命令
std::future<CommandExecution::CommandResult> AdbClient::run_async(const CString& command)
{
return CommandExecution::ExecCommandAsync(_T("adb.exe ") + command);
}
// 终端命令执行的方式(powershell)阻塞的执行adb命令
CString AdbClient::run(const CString& command)
{
// ??? CommandExecution ???????????????
auto result = CommandExecution::ExecCommand(_T("adb.exe ") + command);
if (result.result != 0) {
return result.errorMsg;
}
return result.successMsg;
}
// 使用 CommandExecution 类的同步执行命令方法
std::vector<CString> AdbClient::DetectDevices()
{
std::vector<CString> m_devices;
CString output = AdbClient::run(_T("devices"));
int pos = 0;
CString line = output.Tokenize(_T("\r\n"), pos);
while (!line.IsEmpty())
{
int tabPos = line.Find(_T('\t'));
if (tabPos != -1)
{
CString deviceId = line.Left(tabPos);
m_devices.push_back(deviceId);
}
line = output.Tokenize(_T("\r\n"), pos);
}
return m_devices;
}
// 通过终端命令执行异步安装多个apk,不阻塞,不关心是否成功
bool AdbClient::InstallAPK(const CString& deviceId, std::vector<CString> apks, bool force)
{
if (deviceId.IsEmpty()) {
return false;
}
CString command;
command.Format(_T("-s %s install-multi-package"), deviceId.GetString());
if (force) command.Append(_T(" -r -d -t")); // 强制安装,允许覆盖、降级、测试签
bool hasValidApk = false;
for (auto apk : apks)
{
std::ifstream file(apk);
if (!file.good()) continue;
// adb????.apk??.apex
if (apk.Right(4).CompareNoCase(_T(".apk")) != 0 && apk.Right(5).CompareNoCase(_T(".apex")) != 0) continue;
command.AppendFormat(_T(" \"%s\""), apk.GetBuffer());
hasValidApk = true;
}
// 确保至少有一个有效的 APK 文件,可以排除掉选择多个文件包含了apk但不都是apk的情况
if (!hasValidApk) return false;
// 异步执行命令
run_async(command);
return true;
}
bool AdbClient::initializeWsa() {
if (!wsaInitialized) {
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cerr << "WSAStartup failed" << std::endl;
return false;
}
wsaInitialized = true;
}
return true;
}
void AdbClient::cleanupWsa() {
if (wsaInitialized) {
WSACleanup();
wsaInitialized = false;
}
}
SOCKET AdbClient::createAndConnectSocket() {
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
std::cerr << "Socket creation error" << std::endl;
return INVALID_SOCKET;
}
sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(ADB_PORT);
inet_pton(AF_INET, ADB_HOST.c_str(), &serv_addr.sin_addr);
if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == SOCKET_ERROR) {
std::cerr << "Connection Failed" << std::endl;
closesocket(sock);
return INVALID_SOCKET;
}
return sock;
}
AdbClient::AdbResponse AdbClient::sendCommand(const std::string& command) {
if (!initializeWsa()) {
return { "ERRO", "WSA initialization failed" };
}
SOCKET sock = createAndConnectSocket();
if (sock == INVALID_SOCKET) {
cleanupWsa();
return { "ERRO", "Socket creation or connection failed" };
}
char lengthStr[5];
std::snprintf(lengthStr, sizeof(lengthStr), "%04x", static_cast<int>(command.size()));
std::string msg = std::string(lengthStr) + command;
send(sock, msg.c_str(), msg.length(), 0);
char response[5] = { 0 };
if (recv(sock, response, 4, 0) <= 0) {
closesocket(sock);
cleanupWsa();
return { "ERRO", "Failed to receive response" };
}
AdbResponse result;
if (strncmp(response, "OKAY", 4) == 0) {
result = receiveData(sock);
}
else if (strncmp(response, "FAIL", 4) == 0) {
result = receiveErrorMessage(sock);
}
else {
result = { "ERRO", "Unexpected response: " + std::string(response, 4) };
}
closesocket(sock);
cleanupWsa();
return result;
}
AdbClient::AdbResponse AdbClient::receiveData(SOCKET sock) {
char lengthBuffer[5] = { 0 };
if (recv(sock, lengthBuffer, 4, 0) <= 0) {
return { "ERRO", "Failed to receive data length" };
}
int dataLength = std::stoi(std::string(lengthBuffer), nullptr, 16);
std::string data(dataLength, '\0');
int received = recv(sock, &data[0], dataLength, 0);
if (received > 0) {
return { "OKAY", data };
}
else {
return { "ERRO", "Failed to receive complete data" };
}
}
AdbClient::AdbResponse AdbClient::receiveErrorMessage(SOCKET sock) {
char lengthBuffer[5] = { 0 };
if (recv(sock, lengthBuffer, 4, 0) <= 0) {
return { "ERRO", "Failed to receive error length" };
}
int errorLength = std::stoi(std::string(lengthBuffer), nullptr, 16);
std::string errorMessage(errorLength, '\0');
int received = recv(sock, &errorMessage[0], errorLength, 0);
if (received > 0) {
return { "FAIL", errorMessage };
}
else {
return { "ERRO", "Failed to receive complete error message" };
}
}
bool AdbClient::isProcessRunning(const TCHAR* processName) {
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return false;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return false;
}
do {
if (_tcsicmp(pe32.szExeFile, processName) == 0) { // Use _tcsicmp for case-insensitive comparison
CloseHandle(hSnapshot);
return true;
}
} while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return false;
}
bool AdbClient::isAdbServerRunning() {
return isProcessRunning(_T("adb.exe"));
}
bool AdbClient::startAdbServer() {
if (isAdbServerRunning()) {
return true;
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (CommandExecution::ExecCommand(_T("adb.exe start-server")).result != 0) {
std::cerr << "Failed to start ADB server" << std::endl;
return false;
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return isAdbServerRunning();
}
bool AdbClient::stopAdbServer() {
AdbResponse response = sendCommand("host:kill");
return response.status == "OKAY";
}
std::vector<std::string> AdbClient::getConnectedDevices() {
AdbResponse response = sendCommand("host:devices");
if (response.status != "OKAY") {
return {};
}
std::vector<std::string> devices;
// 处理整个响应字符串
size_t start = 0;
size_t end = response.data.find("\n");
// 遍历每一行
while (end != std::string::npos) {
std::string device = response.data.substr(start, end - start);
size_t tab_pos = device.find("\t");
// 检查是否找到设备,并且设备状态是否为 "device"
if (tab_pos != std::string::npos) {
std::string device_serial = device.substr(0, tab_pos);
std::string device_status = device.substr(tab_pos + 1);
// 检查设备状态是否为有效状态(如 "device")
if (device_status.find("device") != std::string::npos) {
devices.push_back(device_serial);
}
}
// ??????????
start = end + 1;
end = response.data.find("\n", start);
}
// 移动到下一行
std::string last_device = response.data.substr(start);
size_t tab_pos = last_device.find("\t");
if (tab_pos != std::string::npos) {
std::string device_serial = last_device.substr(0, tab_pos);
std::string device_status = last_device.substr(tab_pos + 1);
if (device_status.find("device") != std::string::npos) {
devices.push_back(device_serial);
}
}
return devices;
}
// http://aospxref.com/android-13.0.0_r3/xref/packages/modules/adb/OVERVIEW.TXT
// http://aospxref.com/android-13.0.0_r3/xref/packages/modules/adb/SERVICES.TXT
AdbClient::AdbResponse AdbClient::executeCommand(const std::string& command) {
return sendCommand(command);
}
const std::string AdbClient::ADB_HOST = "127.0.0.1";