idevicediagnostics.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * idevicediagnostics.c
  3. * Retrieves diagnostics information from device
  4. *
  5. * Copyright (c) 2012 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 <stdlib.h>
  24. #include <errno.h>
  25. #include <time.h>
  26. #include <libimobiledevice/libimobiledevice.h>
  27. #include <libimobiledevice/lockdown.h>
  28. #include <libimobiledevice/diagnostics_relay.h>
  29. enum cmd_mode {
  30. CMD_NONE = 0,
  31. CMD_SLEEP,
  32. CMD_RESTART,
  33. CMD_SHUTDOWN,
  34. CMD_DIAGNOSTICS,
  35. CMD_MOBILEGESTALT,
  36. CMD_IOREGISTRY
  37. };
  38. static void print_xml(plist_t node)
  39. {
  40. char *xml = NULL;
  41. uint32_t len = 0;
  42. plist_to_xml(node, &xml, &len);
  43. if (xml) {
  44. puts(xml);
  45. }
  46. }
  47. void print_usage(int argc, char **argv);
  48. int main(int argc, char **argv)
  49. {
  50. idevice_t device = NULL;
  51. lockdownd_client_t lockdown_client = NULL;
  52. diagnostics_relay_client_t diagnostics_client = NULL;
  53. lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
  54. lockdownd_service_descriptor_t service = NULL;
  55. int result = -1;
  56. int i;
  57. const char *udid = NULL;
  58. int cmd = CMD_NONE;
  59. char* cmd_arg = NULL;
  60. plist_t node = NULL;
  61. plist_t keys = NULL;
  62. /* parse cmdline args */
  63. for (i = 1; i < argc; i++) {
  64. if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
  65. idevice_set_debug_level(1);
  66. continue;
  67. }
  68. else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) {
  69. i++;
  70. if (!argv[i] || (strlen(argv[i]) != 40)) {
  71. print_usage(argc, argv);
  72. result = 0;
  73. goto cleanup;
  74. }
  75. udid = argv[i];
  76. continue;
  77. }
  78. else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
  79. print_usage(argc, argv);
  80. result = 0;
  81. goto cleanup;
  82. }
  83. else if (!strcmp(argv[i], "sleep")) {
  84. cmd = CMD_SLEEP;
  85. }
  86. else if (!strcmp(argv[i], "restart")) {
  87. cmd = CMD_RESTART;
  88. }
  89. else if (!strcmp(argv[i], "shutdown")) {
  90. cmd = CMD_SHUTDOWN;
  91. }
  92. else if (!strcmp(argv[i], "diagnostics")) {
  93. cmd = CMD_DIAGNOSTICS;
  94. /* read type */
  95. i++;
  96. if (!argv[i] || ((strcmp(argv[i], "All") != 0) && (strcmp(argv[i], "WiFi") != 0) && (strcmp(argv[i], "GasGauge") != 0) && (strcmp(argv[i], "NAND") != 0))) {
  97. if (argv[i] == NULL) {
  98. cmd_arg = strdup("All");
  99. continue;
  100. }
  101. if (!strncmp(argv[i], "-", 1)) {
  102. cmd_arg = strdup("All");
  103. i--;
  104. continue;
  105. }
  106. printf("Unknown TYPE %s\n", argv[i]);
  107. print_usage(argc, argv);
  108. goto cleanup;
  109. }
  110. cmd_arg = strdup(argv[i]);
  111. continue;
  112. }
  113. else if (!strcmp(argv[i], "mobilegestalt")) {
  114. cmd = CMD_MOBILEGESTALT;
  115. /* read keys */
  116. i++;
  117. if (!argv[i] || argv[i] == NULL || (!strncmp(argv[i], "-", 1))) {
  118. printf("Please supply the key to query.\n");
  119. print_usage(argc, argv);
  120. goto cleanup;
  121. }
  122. keys = plist_new_array();
  123. while(1) {
  124. if (argv[i] && (strlen(argv[i]) >= 2) && (strncmp(argv[i], "-", 1) != 0)) {
  125. plist_array_append_item(keys, plist_new_string(argv[i]));
  126. i++;
  127. } else {
  128. i--;
  129. break;
  130. }
  131. }
  132. continue;
  133. }
  134. else if (!strcmp(argv[i], "ioreg")) {
  135. cmd = CMD_IOREGISTRY;
  136. /* read plane */
  137. i++;
  138. if (argv[i]) {
  139. cmd_arg = strdup(argv[i]);
  140. }
  141. continue;
  142. }
  143. else {
  144. print_usage(argc, argv);
  145. return 0;
  146. }
  147. }
  148. /* verify options */
  149. if (cmd == CMD_NONE) {
  150. print_usage(argc, argv);
  151. goto cleanup;
  152. }
  153. if (IDEVICE_E_SUCCESS != idevice_new(&device, udid)) {
  154. if (udid) {
  155. printf("No device found with udid %s, is it plugged in?\n", udid);
  156. } else {
  157. printf("No device found, is it plugged in?\n");
  158. }
  159. goto cleanup;
  160. }
  161. if (LOCKDOWN_E_SUCCESS != (ret = lockdownd_client_new_with_handshake(device, &lockdown_client, "idevicediagnostics"))) {
  162. idevice_free(device);
  163. printf("ERROR: Could not connect to lockdownd, error code %d\n", ret);
  164. goto cleanup;
  165. }
  166. /* attempt to use newer diagnostics service available on iOS 5 and later */
  167. ret = lockdownd_start_service(lockdown_client, "com.apple.mobile.diagnostics_relay", &service);
  168. if (ret != LOCKDOWN_E_SUCCESS) {
  169. /* attempt to use older diagnostics service */
  170. ret = lockdownd_start_service(lockdown_client, "com.apple.iosdiagnostics.relay", &service);
  171. }
  172. lockdownd_client_free(lockdown_client);
  173. if ((ret == LOCKDOWN_E_SUCCESS) && service && (service->port > 0)) {
  174. if (diagnostics_relay_client_new(device, service, &diagnostics_client) != DIAGNOSTICS_RELAY_E_SUCCESS) {
  175. printf("Could not connect to diagnostics_relay!\n");
  176. result = -1;
  177. } else {
  178. switch (cmd) {
  179. case CMD_SLEEP:
  180. if (diagnostics_relay_sleep(diagnostics_client) == DIAGNOSTICS_RELAY_E_SUCCESS) {
  181. printf("Putting device into deep sleep mode.\n");
  182. result = EXIT_SUCCESS;
  183. } else {
  184. printf("Failed to put device into deep sleep mode.\n");
  185. }
  186. break;
  187. case CMD_RESTART:
  188. if (diagnostics_relay_restart(diagnostics_client, DIAGNOSTICS_RELAY_ACTION_FLAG_WAIT_FOR_DISCONNECT) == DIAGNOSTICS_RELAY_E_SUCCESS) {
  189. printf("Restarting device.\n");
  190. result = EXIT_SUCCESS;
  191. } else {
  192. printf("Failed to restart device.\n");
  193. }
  194. break;
  195. case CMD_SHUTDOWN:
  196. if (diagnostics_relay_shutdown(diagnostics_client, DIAGNOSTICS_RELAY_ACTION_FLAG_WAIT_FOR_DISCONNECT) == DIAGNOSTICS_RELAY_E_SUCCESS) {
  197. printf("Shutting down device.\n");
  198. result = EXIT_SUCCESS;
  199. } else {
  200. printf("Failed to shutdown device.\n");
  201. }
  202. break;
  203. case CMD_MOBILEGESTALT:
  204. if (diagnostics_relay_query_mobilegestalt(diagnostics_client, keys, &node) == DIAGNOSTICS_RELAY_E_SUCCESS) {
  205. if (node) {
  206. print_xml(node);
  207. result = EXIT_SUCCESS;
  208. }
  209. } else {
  210. printf("Unable to query mobilegestalt keys.\n");
  211. }
  212. break;
  213. case CMD_IOREGISTRY:
  214. if (diagnostics_relay_query_ioregistry_plane(diagnostics_client, cmd_arg == NULL ? "": cmd_arg, &node) == DIAGNOSTICS_RELAY_E_SUCCESS) {
  215. if (node) {
  216. print_xml(node);
  217. result = EXIT_SUCCESS;
  218. }
  219. } else {
  220. printf("Unable to retrieve IORegistry from device.\n");
  221. }
  222. break;
  223. case CMD_DIAGNOSTICS:
  224. default:
  225. if (diagnostics_relay_request_diagnostics(diagnostics_client, cmd_arg, &node) == DIAGNOSTICS_RELAY_E_SUCCESS) {
  226. if (node) {
  227. print_xml(node);
  228. result = EXIT_SUCCESS;
  229. }
  230. } else {
  231. printf("Unable to retrieve diagnostics from device.\n");
  232. }
  233. break;
  234. }
  235. diagnostics_relay_goodbye(diagnostics_client);
  236. diagnostics_relay_client_free(diagnostics_client);
  237. }
  238. } else {
  239. printf("Could not start diagnostics service!\n");
  240. }
  241. if (service) {
  242. lockdownd_service_descriptor_free(service);
  243. service = NULL;
  244. }
  245. idevice_free(device);
  246. cleanup:
  247. if (node) {
  248. plist_free(node);
  249. }
  250. if (keys) {
  251. plist_free(keys);
  252. }
  253. if (cmd_arg) {
  254. free(cmd_arg);
  255. }
  256. return result;
  257. }
  258. void print_usage(int argc, char **argv)
  259. {
  260. char *name = NULL;
  261. name = strrchr(argv[0], '/');
  262. printf("Usage: %s COMMAND [OPTIONS]\n", (name ? name + 1: argv[0]));
  263. printf("Use diagnostics interface of a device running iOS 4 or later.\n\n");
  264. printf(" Where COMMAND is one of:\n");
  265. printf(" diagnostics [TYPE]\t\tprint diagnostics information from device by TYPE (All, WiFi, GasGauge, NAND)\n");
  266. printf(" mobilegestalt KEY [...]\tprint mobilegestalt keys passed as arguments seperated by a space.\n");
  267. printf(" ioreg [PLANE]\t\t\tprint IORegistry of device, optionally by PLANE (IODeviceTree, IOPower, IOService) (iOS 5+ only)\n");
  268. printf(" shutdown\t\t\tshutdown device\n");
  269. printf(" restart\t\t\trestart device\n");
  270. printf(" sleep\t\t\t\tput device into sleep mode (disconnects from host)\n\n");
  271. printf(" The following OPTIONS are accepted:\n");
  272. printf(" -d, --debug\t\tenable communication debugging\n");
  273. printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n");
  274. printf(" -h, --help\t\tprints usage information\n");
  275. printf("\n");
  276. printf("Homepage: <http://libimobiledevice.org>\n");
  277. }