idevice_id.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * idevice_id.c
  3. * Prints device name or a list of attached devices
  4. *
  5. * Copyright (C) 2010 Nikias Bassen <nikias@gmx.li>
  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 <libimobiledevice/libimobiledevice.h>
  25. #include <libimobiledevice/lockdown.h>
  26. #define MODE_NONE 0
  27. #define MODE_SHOW_ID 1
  28. #define MODE_LIST_DEVICES 2
  29. static void print_usage(int argc, char **argv)
  30. {
  31. char *name = NULL;
  32. name = strrchr(argv[0], '/');
  33. printf("Usage: %s [OPTIONS] [UDID]\n", (name ? name + 1: argv[0]));
  34. printf("Prints device name or a list of attached devices.\n\n");
  35. printf(" The UDID is a 40-digit hexadecimal number of the device\n");
  36. printf(" for which the name should be retrieved.\n\n");
  37. printf(" -l, --list\t\tlist UDID of all attached devices\n");
  38. printf(" -d, --debug\t\tenable communication debugging\n");
  39. printf(" -h, --help\t\tprints usage information\n");
  40. printf("\n");
  41. printf("Homepage: <http://libimobiledevice.org>\n");
  42. }
  43. int main(int argc, char **argv)
  44. {
  45. idevice_t device = NULL;
  46. lockdownd_client_t client = NULL;
  47. char **dev_list = NULL;
  48. char *device_name = NULL;
  49. int ret = 0;
  50. int i;
  51. int mode = MODE_SHOW_ID;
  52. const char* udid = NULL;
  53. /* parse cmdline args */
  54. for (i = 1; i < argc; i++) {
  55. if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
  56. idevice_set_debug_level(1);
  57. continue;
  58. }
  59. else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--list")) {
  60. mode = MODE_LIST_DEVICES;
  61. continue;
  62. }
  63. else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
  64. print_usage(argc, argv);
  65. return 0;
  66. }
  67. }
  68. /* check if udid was passed */
  69. if (mode == MODE_SHOW_ID) {
  70. i--;
  71. if (!argv[i] || (strlen(argv[i]) != 40)) {
  72. print_usage(argc, argv);
  73. return 0;
  74. }
  75. udid = argv[i];
  76. }
  77. switch (mode) {
  78. case MODE_SHOW_ID:
  79. idevice_new(&device, udid);
  80. if (!device) {
  81. fprintf(stderr, "ERROR: No device with UDID=%s attached.\n", udid);
  82. return -2;
  83. }
  84. if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(device, &client, "idevice_id")) {
  85. idevice_free(device);
  86. fprintf(stderr, "ERROR: Connecting to device failed!\n");
  87. return -2;
  88. }
  89. if ((LOCKDOWN_E_SUCCESS != lockdownd_get_device_name(client, &device_name)) || !device_name) {
  90. fprintf(stderr, "ERROR: Could not get device name!\n");
  91. ret = -2;
  92. }
  93. lockdownd_client_free(client);
  94. idevice_free(device);
  95. if (ret == 0) {
  96. printf("%s\n", device_name);
  97. }
  98. if (device_name) {
  99. free(device_name);
  100. }
  101. return ret;
  102. case MODE_LIST_DEVICES:
  103. default:
  104. if (idevice_get_device_list(&dev_list, &i) < 0) {
  105. fprintf(stderr, "ERROR: Unable to retrieve device list!\n");
  106. return -1;
  107. }
  108. for (i = 0; dev_list[i] != NULL; i++) {
  109. printf("%s\n", dev_list[i]);
  110. }
  111. idevice_device_list_free(dev_list);
  112. return 0;
  113. }
  114. }