forked from hyxbiao/iosutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.cpp
276 lines (249 loc) · 5.65 KB
/
manager.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
#include <stdio.h>
#include "MobileDevice.h"
#include "common.h"
#include "manager.h"
Manager* Manager::_inst = 0;
Manager::Manager()
:_devices(NULL)
, _device_id(NULL), _cmd(CMD_UNKNOWN), _state(S_UNKNOWN)
{
}
Manager * Manager::getInstance()
{
if (_inst == NULL) {
_inst = new Manager();
}
return _inst;
}
int Manager::parse(int argc, char *argv[])
{
int i = 1;
_option = NULL;
while (i < argc) {
if (strcmp(argv[i], "-s") == 0) {
_device_id = argv[i+1];
i++;
} else {
if (strcmp(argv[i], "devices") == 0) {
_cmd = CMD_DEVICES;
} else if (strcmp(argv[i], "install") == 0) {
_cmd = CMD_INSTALL;
_arg1 = argv[++i];
} else if (strcmp(argv[i], "uninstall") == 0) {
_cmd = CMD_UNINSTALL;
_arg1 = argv[++i];
} else if (strcmp(argv[i], "listapp") == 0) {
_cmd = CMD_LISTAPP;
} else if (strcmp(argv[i], "logcat") == 0) {
_cmd = CMD_LOGCAT;
} else if (strcmp(argv[i], "info") == 0) {
_cmd = CMD_INFO;
} else if (strcmp(argv[i], "ls") == 0) {
_cmd = CMD_LISTDIR;
if (strcmp(argv[i+1], "-b") == 0) {
i += 2;
_option = argv[i];
}
_arg1 = argv[++i];
} else if (strcmp(argv[i], "push") == 0) {
_cmd = CMD_PUSH;
if (strcmp(argv[i+1], "-b") == 0) {
i += 2;
_option = argv[i];
}
_arg1 = argv[++i];
_arg2 = argv[++i];
} else if (strcmp(argv[i], "pull") == 0) {
_cmd = CMD_PULL;
if (strcmp(argv[i+1], "-b") == 0) {
i += 2;
_option = argv[i];
}
_arg1 = argv[++i];
_arg2 = argv[++i];
} else if (strcmp(argv[i], "rm") == 0) {
_cmd = CMD_REMOVE;
if (strcmp(argv[i+1], "-b") == 0) {
i += 2;
_option = argv[i];
}
_arg1 = argv[++i];
} else {
return -1;
}
break;
}
i++;
}
//printf("i:%d, argc:%d\n", i, argc);
if (argc <= 1 || i >= argc) {
return -1;
}
if (_cmd == CMD_UNKNOWN) {
return -1;
}
/*
if (_device_id == NULL && _cmd != CMD_DEVICES) {
return -1;
}
*/
return 0;
}
void Manager::run()
{
AMDSetLogLevel(5); // otherwise syslog gets flooded with crap
_devices = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
int timeout = 1;
CFRunLoopTimerContext context;
context.version = 0;
context.info = this;
context.retain = NULL;
context.release = NULL;
context.copyDescription = NULL;
CFRunLoopTimerRef timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() + timeout, 0, 0, 0, timeoutCallback, &context);
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);
//printf("[....] Waiting up to %d seconds for iOS device to be connected\n", timeout);
struct am_device_notification *notify;
AMDeviceNotificationSubscribe(&deviceCallback, 0, 0, this, ¬ify);
CFRunLoopRun();
}
void Manager::connectDevice(struct am_device *amdevice)
{
CFStringRef device_id = AMDeviceCopyDeviceIdentifier(amdevice);
const char *str_device_id = CFStringGetCStringPtr(device_id, CFStringGetSystemEncoding());
if (_device_id != NULL) {
if (strcmp(_device_id, str_device_id) == 0) {
_state = S_RUN_ONCE;
} else {
CFRelease(device_id);
return;
}
}
_state = S_RUN_ONCE;
Device *device = getDevice(amdevice, true);
int ret = 0;
//dispatch
switch (_cmd) {
case CMD_DEVICES: {
if (_device_id == NULL) {
_state = S_RUN_LOOP;
}
printf("[device] %s\n", str_device_id);
break;
}
case CMD_INFO: {
device->showInfo();
break;
}
case CMD_INSTALL: {
const char *app_path = _arg1;
ret = device->install(app_path);
if (ret == 0) {
printf("Install success!\n");
} else {
printf("Install fail!\n");
}
exit(ret);
break;
}
case CMD_UNINSTALL: {
const char *bundle_id = _arg1;
ret = device->uninstall(bundle_id);
if (ret == 0) {
printf("Uninstall success!\n");
} else {
printf("Uninstall fail!\n");
}
exit(ret);
break;
}
case CMD_LISTAPP: {
if (device->listApps() != 0) {
printf("List applications fail!\n");
}
break;
}
case CMD_LOGCAT: {
_state = S_RUN_LOOP;
device->startLogcat();
break;
}
case CMD_LISTDIR:
case CMD_PUSH:
case CMD_PULL:
case CMD_REMOVE:
{
ret = device->operateFile(_cmd, _option, _arg1, _arg2);
if (ret != 0) {
printf("Access file fail!\n");
}
exit(ret);
break;
}
default: {}
break;
}
CFRelease(device_id);
}
void Manager::disConnectDevice(struct am_device *amdevice)
{
Device *device = getDevice(amdevice);
if (device != NULL) {
device->setAlive(false);
if (_cmd == CMD_LOGCAT) {
device->stopLogcat();
}
}
}
Device * Manager::getDevice(struct am_device *amdevice, bool bcreate)
{
Device *device = (Device *)CFDictionaryGetValue(_devices, amdevice);
if (bcreate && device == NULL) {
device = new Device(amdevice);
device->setAlive(true);
CFDictionarySetValue(_devices, amdevice, device);
}
return device;
}
bool Manager::isActive()
{
return (_cmd != CMD_DEVICES && _state != S_UNKNOWN);
}
bool Manager::isRunLoop()
{
//return (_state == S_RUN_LOOP);
return (_state != S_RUN_ONCE);
}
void Manager::release()
{
if (_devices) {
CFRelease(_devices);
}
}
void Manager::timeoutCallback(CFRunLoopTimerRef timer, void *arg)
{
Manager *manager = (Manager *)arg;
if (!manager->isActive()) {
//fprintf(stderr, "Timed out waiting for device.\n");
exit(-1);
}
}
void Manager::deviceCallback(struct am_device_notification_callback_info *info, void *arg)
{
Manager *manager = (Manager *)arg;
switch (info->msg) {
case ADNCI_MSG_CONNECTED: {
manager->connectDevice(info->dev);
break;
}
case ADNCI_MSG_DISCONNECTED: {
manager->disConnectDevice(info->dev);
break;
}
default:
break;
}
if (!manager->isRunLoop()) {
CFRunLoopStop(CFRunLoopGetCurrent());
}
}