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
|
/*
* idevicenotificationproxy.c
* Simple client for the notification_proxy service
*
* Copyright (c) 2009-2015 Martin Szulecki All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#ifdef WIN32
#include <windows.h>
#define sleep(x) Sleep(x*1000)
#else
#include <unistd.h>
#endif
#include <libimobiledevice/libimobiledevice.h>
#include <libimobiledevice/lockdown.h>
#include <libimobiledevice/notification_proxy.h>
enum cmd_mode {
CMD_NONE = 0,
CMD_OBSERVE,
CMD_POST
};
static int quit_flag = 0;
/**
* signal handler function for cleaning up properly
*/
static void clean_exit(int sig)
{
fprintf(stderr, "Exiting...\n");
quit_flag++;
}
static void print_usage(int argc, char **argv)
{
char *name = NULL;
name = strrchr(argv[0], '/');
printf("Usage: %s [OPTIONS] COMMAND\n", (name ? name + 1: argv[0]));
printf("\n");
printf("Post or observe notifications on a device.\n");
printf("\n");
printf("Where COMMAND is one of:\n");
printf(" post ID [...]\t\tpost notification IDs to device and exit\n");
printf(" observe ID [...]\tobserve notification IDs in the foreground until CTRL+C or signal is received\n");
printf("\n");
printf("The following OPTIONS are accepted:\n");
printf(" -d, --debug\t\tenable communication debugging\n");
printf(" -u, --udid UDID\ttarget specific device by UDID\n");
printf(" -h, --help\t\tprints usage information\n");
printf("\n");
printf("Homepage: <" PACKAGE_URL ">\n");
printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n");
}
static void notify_cb(const char *notification, void *user_data)
{
printf("> %s\n", notification);
}
int main(int argc, char *argv[])
{
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
lockdownd_service_descriptor_t service = NULL;
lockdownd_client_t client = NULL;
idevice_t device = NULL;
np_client_t gnp = NULL;
int result = -1;
int i;
const char* udid = NULL;
int cmd = CMD_NONE;
char* cmd_arg = NULL;
int count = 0;
char **nspec = NULL;
char **nspectmp = NULL;
signal(SIGINT, clean_exit);
signal(SIGTERM, clean_exit);
#ifndef WIN32
signal(SIGQUIT, clean_exit);
signal(SIGPIPE, SIG_IGN);
#endif
/* parse cmdline args */
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
idevice_set_debug_level(1);
continue;
}
else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) {
i++;
if (!argv[i] || !*argv[i]) {
print_usage(argc, argv);
result = 0;
goto cleanup;
}
udid = argv[i];
continue;
}
else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
print_usage(argc, argv);
result = 0;
goto cleanup;
}
else if (!strcmp(argv[i], "post") || !strcmp(argv[i], "observe")) {
cmd = CMD_POST;
if (!strcmp(argv[i], "observe")) {
cmd = CMD_OBSERVE;
}
i++;
if (!argv[i] || argv[i] == NULL || (!strncmp(argv[i], "-", 1))) {
printf("Please supply a valid notification identifier.\n");
print_usage(argc, argv);
goto cleanup;
}
count = 0;
nspec = malloc(sizeof(char*) * (count+1));
while(1) {
if (argv[i] && (strlen(argv[i]) >= 2) && (strncmp(argv[i], "-", 1) != 0)) {
nspectmp = realloc(nspec, sizeof(char*) * (count+1));
nspectmp[count] = strdup(argv[i]);
nspec = nspectmp;
count = count+1;
i++;
} else {
i--;
break;
}
}
nspectmp = realloc(nspec, sizeof(char*) * (count+1));
nspectmp[count] = NULL;
nspec = nspectmp;
continue;
}
else {
print_usage(argc, argv);
return 0;
}
}
/* verify options */
if (cmd == CMD_NONE) {
print_usage(argc, argv);
goto cleanup;
}
if (IDEVICE_E_SUCCESS != idevice_new(&device, udid)) {
if (udid) {
printf("No device found with udid %s, is it plugged in?\n", udid);
} else {
printf("No device found, is it plugged in?\n");
}
goto cleanup;
}
if (LOCKDOWN_E_SUCCESS != (ret = lockdownd_client_new_with_handshake(device, &client, "idevicenotificationproxy"))) {
fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", ret);
goto cleanup;
}
ret = lockdownd_start_service(client, NP_SERVICE_NAME, &service);
lockdownd_client_free(client);
if ((ret == LOCKDOWN_E_SUCCESS) && (service->port > 0)) {
if (np_client_new(device, service, &gnp) != NP_E_SUCCESS) {
printf("Could not connect to notification_proxy!\n");
result = -1;
} else {
np_set_notify_callback(gnp, notify_cb, NULL);
switch (cmd) {
case CMD_POST:
i = 0;
while(nspec[i] != NULL && i < (count+1)) {
printf("< posting \"%s\"\n", nspec[i]);
np_post_notification(gnp, nspec[i]);
i++;
}
break;
case CMD_OBSERVE:
default:
i = 0;
while(nspec[i] != NULL && i < (count+1)) {
printf("! observing \"%s\"\n", nspec[i]);
np_observe_notification(gnp, nspec[i]);
i++;
}
/* just sleep and wait for notifications */
while (!quit_flag) {
sleep(1);
}
break;
}
result = EXIT_SUCCESS;
if (gnp) {
np_client_free(gnp);
gnp = NULL;
}
}
} else {
printf("Could not start notification_proxy service on device.\n");
}
if (service) {
lockdownd_service_descriptor_free(service);
service = NULL;
}
cleanup:
if (nspec) {
i = 0;
while(nspec[i] != NULL && i < (count+1)) {
free(nspec[i]);
i++;
}
free(nspec);
}
if (cmd_arg) {
free(cmd_arg);
}
if (device)
idevice_free(device);
return result;
}
|