idevicenotificationproxy.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * idevicenotificationproxy.c
  3. * Simple client for the notification_proxy service
  4. *
  5. * Copyright (c) 2009-2015 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. #include <stdio.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <signal.h>
  25. #include <stdlib.h>
  26. #ifdef WIN32
  27. #include <windows.h>
  28. #define sleep(x) Sleep(x*1000)
  29. #else
  30. #include <unistd.h>
  31. #endif
  32. #include <libimobiledevice/libimobiledevice.h>
  33. #include <libimobiledevice/lockdown.h>
  34. #include <libimobiledevice/notification_proxy.h>
  35. enum cmd_mode {
  36. CMD_NONE = 0,
  37. CMD_OBSERVE,
  38. CMD_POST
  39. };
  40. static int quit_flag = 0;
  41. /**
  42. * signal handler function for cleaning up properly
  43. */
  44. static void clean_exit(int sig)
  45. {
  46. fprintf(stderr, "Exiting...\n");
  47. quit_flag++;
  48. }
  49. static void print_usage(int argc, char **argv)
  50. {
  51. char *name = NULL;
  52. name = strrchr(argv[0], '/');
  53. printf("Usage: %s [OPTIONS] COMMAND\n", (name ? name + 1: argv[0]));
  54. printf("Post or observe notifications on a device.\n\n");
  55. printf(" Where COMMAND is one of:\n");
  56. printf(" post ID [...]\t\tpost notification IDs to device and exit\n");
  57. printf(" observe ID [...]\tobserve notification IDs in the foreground until CTRL+C or signal is received\n");
  58. printf("\n");
  59. printf(" The following OPTIONS are accepted:\n");
  60. printf(" -d, --debug\t\tenable communication debugging\n");
  61. printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n");
  62. printf(" -h, --help\t\tprints usage information\n");
  63. printf("\n");
  64. printf("Homepage: <http://libimobiledevice.org>\n");
  65. }
  66. static void notify_cb(const char *notification, void *user_data)
  67. {
  68. printf("> %s\n", notification);
  69. }
  70. int main(int argc, char *argv[])
  71. {
  72. lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
  73. lockdownd_service_descriptor_t service = NULL;
  74. lockdownd_client_t client = NULL;
  75. idevice_t device = NULL;
  76. np_client_t gnp = NULL;
  77. int result = -1;
  78. int i;
  79. const char* udid = NULL;
  80. int cmd = CMD_NONE;
  81. char* cmd_arg = NULL;
  82. int count = 0;
  83. char **nspec = NULL;
  84. char **nspectmp = NULL;
  85. signal(SIGINT, clean_exit);
  86. signal(SIGTERM, clean_exit);
  87. #ifndef WIN32
  88. signal(SIGQUIT, clean_exit);
  89. signal(SIGPIPE, SIG_IGN);
  90. #endif
  91. /* parse cmdline args */
  92. for (i = 1; i < argc; i++) {
  93. if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
  94. idevice_set_debug_level(1);
  95. continue;
  96. }
  97. else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) {
  98. i++;
  99. if (!argv[i] || (strlen(argv[i]) != 40)) {
  100. print_usage(argc, argv);
  101. result = 0;
  102. goto cleanup;
  103. }
  104. udid = argv[i];
  105. continue;
  106. }
  107. else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
  108. print_usage(argc, argv);
  109. result = 0;
  110. goto cleanup;
  111. }
  112. else if (!strcmp(argv[i], "post") || !strcmp(argv[i], "observe")) {
  113. cmd = CMD_POST;
  114. if (!strcmp(argv[i], "observe")) {
  115. cmd = CMD_OBSERVE;
  116. }
  117. i++;
  118. if (!argv[i] || argv[i] == NULL || (!strncmp(argv[i], "-", 1))) {
  119. printf("Please supply a valid notification identifier.\n");
  120. print_usage(argc, argv);
  121. goto cleanup;
  122. }
  123. count = 0;
  124. nspec = malloc(sizeof(char*) * (count+1));
  125. while(1) {
  126. if (argv[i] && (strlen(argv[i]) >= 2) && (strncmp(argv[i], "-", 1) != 0)) {
  127. nspectmp = realloc(nspec, sizeof(char*) * (count+1));
  128. nspectmp[count] = strdup(argv[i]);
  129. nspec = nspectmp;
  130. count = count+1;
  131. i++;
  132. } else {
  133. i--;
  134. break;
  135. }
  136. }
  137. nspectmp = realloc(nspec, sizeof(char*) * (count+1));
  138. nspectmp[count] = NULL;
  139. nspec = nspectmp;
  140. continue;
  141. }
  142. else {
  143. print_usage(argc, argv);
  144. return 0;
  145. }
  146. }
  147. /* verify options */
  148. if (cmd == CMD_NONE) {
  149. print_usage(argc, argv);
  150. goto cleanup;
  151. }
  152. if (IDEVICE_E_SUCCESS != idevice_new(&device, udid)) {
  153. if (udid) {
  154. printf("No device found with udid %s, is it plugged in?\n", udid);
  155. } else {
  156. printf("No device found, is it plugged in?\n");
  157. }
  158. goto cleanup;
  159. }
  160. if (LOCKDOWN_E_SUCCESS != (ret = lockdownd_client_new_with_handshake(device, &client, "idevicenotificationproxy"))) {
  161. fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", ret);
  162. goto cleanup;
  163. }
  164. ret = lockdownd_start_service(client, NP_SERVICE_NAME, &service);
  165. lockdownd_client_free(client);
  166. if ((ret == LOCKDOWN_E_SUCCESS) && (service->port > 0)) {
  167. if (np_client_new(device, service, &gnp) != NP_E_SUCCESS) {
  168. printf("Could not connect to notification_proxy!\n");
  169. result = -1;
  170. } else {
  171. np_set_notify_callback(gnp, notify_cb, NULL);
  172. switch (cmd) {
  173. case CMD_POST:
  174. i = 0;
  175. while(nspec[i] != NULL && i < (count+1)) {
  176. printf("< posting \"%s\"\n", nspec[i]);
  177. np_post_notification(gnp, nspec[i]);
  178. i++;
  179. }
  180. break;
  181. case CMD_OBSERVE:
  182. default:
  183. i = 0;
  184. while(nspec[i] != NULL && i < (count+1)) {
  185. printf("! observing \"%s\"\n", nspec[i]);
  186. np_observe_notification(gnp, nspec[i]);
  187. i++;
  188. }
  189. /* just sleep and wait for notifications */
  190. while (!quit_flag) {
  191. sleep(1);
  192. }
  193. break;
  194. }
  195. result = EXIT_SUCCESS;
  196. if (gnp) {
  197. np_client_free(gnp);
  198. gnp = NULL;
  199. }
  200. }
  201. } else {
  202. printf("Could not start notification_proxy service on device.\n");
  203. }
  204. if (service) {
  205. lockdownd_service_descriptor_free(service);
  206. service = NULL;
  207. }
  208. cleanup:
  209. if (nspec) {
  210. i = 0;
  211. while(nspec[i] != NULL && i < (count+1)) {
  212. free(nspec[i]);
  213. i++;
  214. }
  215. free(nspec);
  216. }
  217. if (cmd_arg) {
  218. free(cmd_arg);
  219. }
  220. if (device)
  221. idevice_free(device);
  222. return result;
  223. }