idevicedate.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * idevicedate.c
  3. * Simple utility to get and set the clock on a device
  4. *
  5. * Copyright (c) 2011 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 <stdlib.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #if HAVE_LANGINFO_CODESET
  26. #include <langinfo.h>
  27. #endif
  28. #include <libimobiledevice/libimobiledevice.h>
  29. #include <libimobiledevice/lockdown.h>
  30. #ifdef _DATE_FMT
  31. #define DATE_FMT_LANGINFO() nl_langinfo (_DATE_FMT)
  32. #else
  33. #define DATE_FMT_LANGINFO() ""
  34. #endif
  35. static void print_usage(int argc, char **argv)
  36. {
  37. char *name = NULL;
  38. name = strrchr(argv[0], '/');
  39. printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0]));
  40. printf("Display the current date or set it on a device.\n\n");
  41. printf("NOTE: Setting the time on iOS 6 and later is only supported\n");
  42. printf(" in the setup wizard screens before device activation.\n\n");
  43. printf(" -d, --debug\t\tenable communication debugging\n");
  44. printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n");
  45. printf(" -s, --set TIMESTAMP\tset UTC time described by TIMESTAMP\n");
  46. printf(" -c, --sync\t\tset time of device to current system time\n");
  47. printf(" -h, --help\t\tprints usage information\n");
  48. printf("\n");
  49. printf("Homepage: <http://libimobiledevice.org>\n");
  50. }
  51. int main(int argc, char *argv[])
  52. {
  53. lockdownd_client_t client = NULL;
  54. lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR;
  55. idevice_t device = NULL;
  56. idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
  57. int i;
  58. const char* udid = NULL;
  59. time_t setdate = 0;
  60. plist_t node = NULL;
  61. int node_type = -1;
  62. uint64_t datetime = 0;
  63. time_t rawtime;
  64. struct tm * tmp;
  65. char const *format = NULL;
  66. char buffer[80];
  67. int result = 0;
  68. /* parse cmdline args */
  69. for (i = 1; i < argc; i++) {
  70. if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
  71. idevice_set_debug_level(1);
  72. continue;
  73. }
  74. else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) {
  75. i++;
  76. if (!argv[i] || (strlen(argv[i]) != 40)) {
  77. print_usage(argc, argv);
  78. return 0;
  79. }
  80. udid = argv[i];
  81. continue;
  82. }
  83. else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--set")) {
  84. i++;
  85. if (!argv[i] || (strlen(argv[i]) <= 1)) {
  86. print_usage(argc, argv);
  87. return 0;
  88. }
  89. setdate = atoi(argv[i]);
  90. if (setdate == 0) {
  91. printf("ERROR: Invalid timestamp value.\n");
  92. print_usage(argc, argv);
  93. return 0;
  94. }
  95. continue;
  96. }
  97. else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--sync")) {
  98. i++;
  99. /* get current time */
  100. setdate = time(NULL);
  101. /* convert it to local time which sets timezone/daylight variables */
  102. tmp = localtime(&setdate);
  103. /* recalculate to make it UTC */
  104. setdate = mktime(tmp);
  105. continue;
  106. }
  107. else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
  108. print_usage(argc, argv);
  109. return 0;
  110. }
  111. else {
  112. print_usage(argc, argv);
  113. return 0;
  114. }
  115. }
  116. /* determine a date format */
  117. if (!format) {
  118. format = DATE_FMT_LANGINFO ();
  119. if (!*format) {
  120. format = "%a %b %e %H:%M:%S %Z %Y";
  121. }
  122. }
  123. ret = idevice_new(&device, udid);
  124. if (ret != IDEVICE_E_SUCCESS) {
  125. if (udid) {
  126. printf("No device found with udid %s, is it plugged in?\n", udid);
  127. } else {
  128. printf("No device found, is it plugged in?\n");
  129. }
  130. return -1;
  131. }
  132. if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new_with_handshake(device, &client, "idevicedate"))) {
  133. fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", ldret);
  134. result = -1;
  135. goto cleanup;
  136. }
  137. if(lockdownd_get_value(client, NULL, "TimeIntervalSince1970", &node) != LOCKDOWN_E_SUCCESS) {
  138. fprintf(stderr, "ERROR: Unable to retrieve 'TimeIntervalSince1970' node from device.\n");
  139. result = -1;
  140. goto cleanup;
  141. }
  142. if (node == NULL) {
  143. fprintf(stderr, "ERROR: Empty node for 'TimeIntervalSince1970' received.\n");
  144. result = -1;
  145. goto cleanup;
  146. }
  147. node_type = plist_get_node_type(node);
  148. /* get or set? */
  149. if (setdate == 0) {
  150. /* get time value from device */
  151. switch (node_type) {
  152. case PLIST_UINT:
  153. plist_get_uint_val(node, &datetime);
  154. break;
  155. case PLIST_REAL:
  156. {
  157. double rv = 0;
  158. plist_get_real_val(node, &rv);
  159. datetime = rv;
  160. }
  161. break;
  162. default:
  163. fprintf(stderr, "ERROR: Unexpected node type for 'TimeIntervalSince1970'\n");
  164. break;
  165. }
  166. plist_free(node);
  167. node = NULL;
  168. /* date/time calculations */
  169. rawtime = (time_t)datetime;
  170. tmp = localtime(&rawtime);
  171. /* finally we format and print the current date */
  172. strftime(buffer, 80, format, tmp);
  173. puts(buffer);
  174. } else {
  175. datetime = setdate;
  176. plist_free(node);
  177. node = NULL;
  178. switch (node_type) {
  179. case PLIST_UINT:
  180. node = plist_new_uint(datetime);
  181. break;
  182. case PLIST_REAL:
  183. node = plist_new_real((double)datetime);
  184. break;
  185. default:
  186. fprintf(stderr, "ERROR: Unexpected node type for 'TimeIntervalSince1970'\n");
  187. break;
  188. }
  189. if(lockdownd_set_value(client, NULL, "TimeIntervalSince1970", node) == LOCKDOWN_E_SUCCESS) {
  190. tmp = localtime(&setdate);
  191. strftime(buffer, 80, format, tmp);
  192. puts(buffer);
  193. } else {
  194. printf("ERROR: Failed to set date on device.\n");
  195. }
  196. node = NULL;
  197. }
  198. cleanup:
  199. if (client)
  200. lockdownd_client_free(client);
  201. if (device)
  202. idevice_free(device);
  203. return result;
  204. }