diff options
Diffstat (limited to 'dev/ideviceheartbeat.c')
| -rw-r--r-- | dev/ideviceheartbeat.c | 157 |
1 files changed, 0 insertions, 157 deletions
diff --git a/dev/ideviceheartbeat.c b/dev/ideviceheartbeat.c deleted file mode 100644 index a140589..0000000 --- a/dev/ideviceheartbeat.c +++ /dev/null | |||
| @@ -1,157 +0,0 @@ | |||
| 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 <inttypes.h> | ||
| 25 | #include <signal.h> | ||
| 26 | #include <stdlib.h> | ||
| 27 | |||
| 28 | #include <libimobiledevice/libimobiledevice.h> | ||
| 29 | #include <libimobiledevice/heartbeat.h> | ||
| 30 | |||
| 31 | static int quit_flag = 0; | ||
| 32 | |||
| 33 | /** | ||
| 34 | * signal handler function for cleaning up properly | ||
| 35 | */ | ||
| 36 | static void clean_exit(int sig) | ||
| 37 | { | ||
| 38 | fprintf(stderr, "Exiting...\n"); | ||
| 39 | quit_flag++; | ||
| 40 | } | ||
| 41 | |||
| 42 | static void print_usage(int argc, char **argv) | ||
| 43 | { | ||
| 44 | char *name = NULL; | ||
| 45 | |||
| 46 | name = strrchr(argv[0], '/'); | ||
| 47 | printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); | ||
| 48 | printf("Runs in the foreground and keeps a \"heartbeat\" connection alive.\n\n"); | ||
| 49 | printf(" -d, --debug\t\tenable communication debugging\n"); | ||
| 50 | printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n"); | ||
| 51 | printf(" -h, --help\t\tprints usage information\n"); | ||
| 52 | printf("\n"); | ||
| 53 | } | ||
| 54 | |||
| 55 | int main(int argc, char *argv[]) | ||
| 56 | { | ||
| 57 | heartbeat_client_t heartbeat = NULL; | ||
| 58 | idevice_t device = NULL; | ||
| 59 | idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; | ||
| 60 | int i; | ||
| 61 | const char* udid = NULL; | ||
| 62 | |||
| 63 | signal(SIGINT, clean_exit); | ||
| 64 | signal(SIGTERM, clean_exit); | ||
| 65 | #ifndef WIN32 | ||
| 66 | signal(SIGQUIT, clean_exit); | ||
| 67 | signal(SIGPIPE, SIG_IGN); | ||
| 68 | #endif | ||
| 69 | /* parse cmdline args */ | ||
| 70 | for (i = 1; i < argc; i++) { | ||
| 71 | if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { | ||
| 72 | idevice_set_debug_level(1); | ||
| 73 | continue; | ||
| 74 | } | ||
| 75 | else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) { | ||
| 76 | i++; | ||
| 77 | if (!argv[i] || (strlen(argv[i]) != 40)) { | ||
| 78 | print_usage(argc, argv); | ||
| 79 | return 0; | ||
| 80 | } | ||
| 81 | udid = argv[i]; | ||
| 82 | continue; | ||
| 83 | } | ||
| 84 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { | ||
| 85 | print_usage(argc, argv); | ||
| 86 | return 0; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | print_usage(argc, argv); | ||
| 90 | return 0; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | ret = idevice_new(&device, udid); | ||
| 95 | if (ret != IDEVICE_E_SUCCESS) { | ||
| 96 | if (udid) { | ||
| 97 | printf("No device found with udid %s, is it plugged in?\n", udid); | ||
| 98 | } else { | ||
| 99 | printf("No device found, is it plugged in?\n"); | ||
| 100 | } | ||
| 101 | return -1; | ||
| 102 | } | ||
| 103 | |||
| 104 | /* start heartbeat service on device */ | ||
| 105 | heartbeat_client_start_service(device, &heartbeat, "ideviceheartbeat"); | ||
| 106 | if (heartbeat) { | ||
| 107 | printf("< heartbeat started, listening...\n"); | ||
| 108 | } else { | ||
| 109 | printf("Failed to start heartbeat service\n"); | ||
| 110 | idevice_free(device); | ||
| 111 | return -1; | ||
| 112 | } | ||
| 113 | |||
| 114 | /* main loop */ | ||
| 115 | uint8_t b = 0; | ||
| 116 | uint64_t interval = 10000; | ||
| 117 | plist_t message = NULL; | ||
| 118 | plist_t node = NULL; | ||
| 119 | do { | ||
| 120 | /* await a "ping" message from the device every interval seconds */ | ||
| 121 | heartbeat_receive_with_timeout(heartbeat, &message, (uint32_t)interval); | ||
| 122 | if (message) { | ||
| 123 | /* report device beat settings */ | ||
| 124 | node = plist_dict_get_item(message, "SupportsSleepyTime"); | ||
| 125 | if (node && plist_get_node_type(node) == PLIST_BOOLEAN) { | ||
| 126 | plist_get_bool_val(node, &b); | ||
| 127 | } | ||
| 128 | node = plist_dict_get_item(message, "Interval"); | ||
| 129 | if (node && plist_get_node_type(node) == PLIST_UINT) { | ||
| 130 | plist_get_uint_val(node, &interval); | ||
| 131 | } | ||
| 132 | |||
| 133 | printf("> marco: supports_sleepy_time %d, interval %"PRIu64"\n", b, interval); | ||
| 134 | |||
| 135 | plist_free(message); | ||
| 136 | message = NULL; | ||
| 137 | |||
| 138 | /* answer with a "pong" message */ | ||
| 139 | message = plist_new_dict(); | ||
| 140 | plist_dict_set_item(message, "Command", plist_new_string("Polo")); | ||
| 141 | heartbeat_send(heartbeat, message); | ||
| 142 | |||
| 143 | printf("< polo\n"); | ||
| 144 | |||
| 145 | if (message) { | ||
| 146 | plist_free(message); | ||
| 147 | message = NULL; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } while(!quit_flag); | ||
| 151 | |||
| 152 | heartbeat_client_free(heartbeat); | ||
| 153 | |||
| 154 | idevice_free(device); | ||
| 155 | |||
| 156 | return 0; | ||
| 157 | } | ||
