summaryrefslogtreecommitdiffstats
path: root/dev/ideviceheartbeat.c
diff options
context:
space:
mode:
authorGravatar Martin Szulecki2013-02-26 03:27:20 +0100
committerGravatar Martin Szulecki2013-02-26 03:27:20 +0100
commit037d53e3b8d276733eba780338ac2ab26f3f4869 (patch)
treeab48cacf0baba281c713de2cf9ad32454c710ed7 /dev/ideviceheartbeat.c
parentc5d46be5fb2103c6e9ecbd684f910e00fe16e4cd (diff)
downloadlibimobiledevice-037d53e3b8d276733eba780338ac2ab26f3f4869.tar.gz
libimobiledevice-037d53e3b8d276733eba780338ac2ab26f3f4869.tar.bz2
ideviceheartbeat: Add simpe tool which keeps heartbeat service connection alive
Diffstat (limited to 'dev/ideviceheartbeat.c')
-rw-r--r--dev/ideviceheartbeat.c153
1 files changed, 153 insertions, 0 deletions
diff --git a/dev/ideviceheartbeat.c b/dev/ideviceheartbeat.c
new file mode 100644
index 0000000..aecf9a0
--- /dev/null
+++ b/dev/ideviceheartbeat.c
@@ -0,0 +1,153 @@
1/*
2 * ideviceheartbeat.c
3 * Simple utility which keeps a "heartbeat service" connection alive
4 *
5 * Copyright (c) 2013 Martin Szulecki All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <string.h>
24#include <signal.h>
25#include <stdlib.h>
26
27#include <libimobiledevice/libimobiledevice.h>
28#include <libimobiledevice/heartbeat.h>
29
30static int quit_flag = 0;
31
32/**
33 * signal handler function for cleaning up properly
34 */
35static void clean_exit(int sig)
36{
37 fprintf(stderr, "Exiting...\n");
38 quit_flag++;
39}
40
41static void print_usage(int argc, char **argv)
42{
43 char *name = NULL;
44
45 name = strrchr(argv[0], '/');
46 printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0]));
47 printf("Runs in the foreground and keeps a \"heartbeat\" connection alive.\n\n");
48 printf(" -d, --debug\t\tenable communication debugging\n");
49 printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n");
50 printf(" -h, --help\t\tprints usage information\n");
51 printf("\n");
52}
53
54int main(int argc, char *argv[])
55{
56 heartbeat_client_t heartbeat = NULL;
57 idevice_t device = NULL;
58 idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
59 int i;
60 const char* udid = NULL;
61
62 signal(SIGINT, clean_exit);
63 signal(SIGTERM, clean_exit);
64#ifndef WIN32
65 signal(SIGQUIT, clean_exit);
66 signal(SIGPIPE, SIG_IGN);
67#endif
68 /* parse cmdline args */
69 for (i = 1; i < argc; i++) {
70 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
71 idevice_set_debug_level(1);
72 continue;
73 }
74 else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) {
75 i++;
76 if (!argv[i] || (strlen(argv[i]) != 40)) {
77 print_usage(argc, argv);
78 return 0;
79 }
80 udid = argv[i];
81 continue;
82 }
83 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
84 print_usage(argc, argv);
85 return 0;
86 }
87 else {
88 print_usage(argc, argv);
89 return 0;
90 }
91 }
92
93 ret = idevice_new(&device, udid);
94 if (ret != IDEVICE_E_SUCCESS) {
95 if (udid) {
96 printf("No device found with udid %s, is it plugged in?\n", udid);
97 } else {
98 printf("No device found, is it plugged in?\n");
99 }
100 return -1;
101 }
102
103 /* start heartbeat service on device */
104 heartbeat_start_service(device, &heartbeat);
105
106 if (heartbeat) {
107 printf("< heartbeat started, listening...\n");
108 }
109
110 /* main loop */
111 uint8_t b = 0;
112 uint64_t interval = 10000;
113 plist_t message = NULL;
114 plist_t node = NULL;
115 do {
116 /* await a "ping" message from the device every interval seconds */
117 heartbeat_receive_with_timeout(heartbeat, &message, (uint32_t)interval);
118 if (message) {
119 /* report device beat settings */
120 node = plist_dict_get_item(message, "SupportsSleepyTime");
121 if (node && plist_get_node_type(node) == PLIST_BOOLEAN) {
122 plist_get_bool_val(node, &b);
123 }
124 node = plist_dict_get_item(message, "Interval");
125 if (node && plist_get_node_type(node) == PLIST_UINT) {
126 plist_get_uint_val(node, &interval);
127 }
128
129 printf("> marco: supports_sleepy_time %d, interval %llu\n", b, interval);
130
131 plist_free(message);
132 message = NULL;
133
134 /* answer with a "pong" message */
135 message = plist_new_dict();
136 plist_dict_insert_item(message, "Command", plist_new_string("Polo"));
137 heartbeat_send(heartbeat, message);
138
139 printf("< polo\n");
140
141 if (message) {
142 plist_free(message);
143 message = NULL;
144 }
145 }
146 } while(!quit_flag);
147
148 heartbeat_client_free(heartbeat);
149
150 idevice_free(device);
151
152 return 0;
153}