ideviceinfo.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * ideviceinfo.c
  3. * Simple utility to show information about an attached device
  4. *
  5. * Copyright (c) 2009 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 <stdlib.h>
  25. #include <libimobiledevice/libimobiledevice.h>
  26. #include <libimobiledevice/lockdown.h>
  27. #include "common/utils.h"
  28. #define FORMAT_KEY_VALUE 1
  29. #define FORMAT_XML 2
  30. static const char *domains[] = {
  31. "com.apple.disk_usage",
  32. "com.apple.disk_usage.factory",
  33. "com.apple.mobile.battery",
  34. /* FIXME: For some reason lockdownd segfaults on this, works sometimes though
  35. "com.apple.mobile.debug",. */
  36. "com.apple.iqagent",
  37. "com.apple.purplebuddy",
  38. "com.apple.PurpleBuddy",
  39. "com.apple.mobile.chaperone",
  40. "com.apple.mobile.third_party_termination",
  41. "com.apple.mobile.lockdownd",
  42. "com.apple.mobile.lockdown_cache",
  43. "com.apple.xcode.developerdomain",
  44. "com.apple.international",
  45. "com.apple.mobile.data_sync",
  46. "com.apple.mobile.tethered_sync",
  47. "com.apple.mobile.mobile_application_usage",
  48. "com.apple.mobile.backup",
  49. "com.apple.mobile.nikita",
  50. "com.apple.mobile.restriction",
  51. "com.apple.mobile.user_preferences",
  52. "com.apple.mobile.sync_data_class",
  53. "com.apple.mobile.software_behavior",
  54. "com.apple.mobile.iTunes.SQLMusicLibraryPostProcessCommands",
  55. "com.apple.mobile.iTunes.accessories",
  56. "com.apple.mobile.internal", /**< iOS 4.0+ */
  57. "com.apple.mobile.wireless_lockdown", /**< iOS 4.0+ */
  58. "com.apple.fairplay",
  59. "com.apple.iTunes",
  60. "com.apple.mobile.iTunes.store",
  61. "com.apple.mobile.iTunes",
  62. NULL
  63. };
  64. static int is_domain_known(char *domain)
  65. {
  66. int i = 0;
  67. while (domains[i] != NULL) {
  68. if (strstr(domain, domains[i++])) {
  69. return 1;
  70. }
  71. }
  72. return 0;
  73. }
  74. static void print_usage(int argc, char **argv)
  75. {
  76. int i = 0;
  77. char *name = NULL;
  78. name = strrchr(argv[0], '/');
  79. printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0]));
  80. printf("Show information about a connected device.\n\n");
  81. printf(" -d, --debug\t\tenable communication debugging\n");
  82. printf(" -s, --simple\t\tuse a simple connection to avoid auto-pairing with the device\n");
  83. printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n");
  84. printf(" -q, --domain NAME\tset domain of query to NAME. Default: None\n");
  85. printf(" -k, --key NAME\tonly query key specified by NAME. Default: All keys.\n");
  86. printf(" -x, --xml\t\toutput information as xml plist instead of key/value pairs\n");
  87. printf(" -h, --help\t\tprints usage information\n");
  88. printf("\n");
  89. printf(" Known domains are:\n\n");
  90. while (domains[i] != NULL) {
  91. printf(" %s\n", domains[i++]);
  92. }
  93. printf("\n");
  94. printf("Homepage: <http://libimobiledevice.org>\n");
  95. }
  96. int main(int argc, char *argv[])
  97. {
  98. lockdownd_client_t client = NULL;
  99. lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR;
  100. idevice_t device = NULL;
  101. idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
  102. int i;
  103. int simple = 0;
  104. int format = FORMAT_KEY_VALUE;
  105. const char* udid = NULL;
  106. char *domain = NULL;
  107. char *key = NULL;
  108. char *xml_doc = NULL;
  109. uint32_t xml_length;
  110. plist_t node = NULL;
  111. /* parse cmdline args */
  112. for (i = 1; i < argc; i++) {
  113. if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
  114. idevice_set_debug_level(1);
  115. continue;
  116. }
  117. else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) {
  118. i++;
  119. if (!argv[i] || (strlen(argv[i]) != 40)) {
  120. print_usage(argc, argv);
  121. return 0;
  122. }
  123. udid = argv[i];
  124. continue;
  125. }
  126. else if (!strcmp(argv[i], "-q") || !strcmp(argv[i], "--domain")) {
  127. i++;
  128. if (!argv[i] || (strlen(argv[i]) < 4)) {
  129. print_usage(argc, argv);
  130. return 0;
  131. }
  132. if (!is_domain_known(argv[i])) {
  133. fprintf(stderr, "WARNING: Sending query with unknown domain \"%s\".\n", argv[i]);
  134. }
  135. domain = strdup(argv[i]);
  136. continue;
  137. }
  138. else if (!strcmp(argv[i], "-k") || !strcmp(argv[i], "--key")) {
  139. i++;
  140. if (!argv[i] || (strlen(argv[i]) <= 1)) {
  141. print_usage(argc, argv);
  142. return 0;
  143. }
  144. key = strdup(argv[i]);
  145. continue;
  146. }
  147. else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "--xml")) {
  148. format = FORMAT_XML;
  149. continue;
  150. }
  151. else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--simple")) {
  152. simple = 1;
  153. continue;
  154. }
  155. else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
  156. print_usage(argc, argv);
  157. return 0;
  158. }
  159. else {
  160. print_usage(argc, argv);
  161. return 0;
  162. }
  163. }
  164. ret = idevice_new(&device, udid);
  165. if (ret != IDEVICE_E_SUCCESS) {
  166. if (udid) {
  167. printf("No device found with udid %s, is it plugged in?\n", udid);
  168. } else {
  169. printf("No device found, is it plugged in?\n");
  170. }
  171. return -1;
  172. }
  173. if (LOCKDOWN_E_SUCCESS != (ldret = simple ?
  174. lockdownd_client_new(device, &client, "ideviceinfo"):
  175. lockdownd_client_new_with_handshake(device, &client, "ideviceinfo"))) {
  176. fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", ldret);
  177. idevice_free(device);
  178. return -1;
  179. }
  180. /* run query and output information */
  181. if(lockdownd_get_value(client, domain, key, &node) == LOCKDOWN_E_SUCCESS) {
  182. if (node) {
  183. switch (format) {
  184. case FORMAT_XML:
  185. plist_to_xml(node, &xml_doc, &xml_length);
  186. printf("%s", xml_doc);
  187. free(xml_doc);
  188. break;
  189. case FORMAT_KEY_VALUE:
  190. plist_print_to_stream(node, stdout);
  191. break;
  192. default:
  193. if (key != NULL)
  194. plist_print_to_stream(node, stdout);
  195. break;
  196. }
  197. plist_free(node);
  198. node = NULL;
  199. }
  200. }
  201. if (domain != NULL)
  202. free(domain);
  203. lockdownd_client_free(client);
  204. idevice_free(device);
  205. return 0;
  206. }