From f40f19078a9a694558126d8e1da5e5b109ea5e5b Mon Sep 17 00:00:00 2001 From: Martin Szulecki Date: Sun, 27 Mar 2011 11:35:48 +0200 Subject: Add new idevicedate tool to get or set the clock on iDevices --- tools/idevicedate.c | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 tools/idevicedate.c (limited to 'tools/idevicedate.c') diff --git a/tools/idevicedate.c b/tools/idevicedate.c new file mode 100644 index 0000000..b3e7334 --- /dev/null +++ b/tools/idevicedate.c @@ -0,0 +1,201 @@ +/* + * idevicedate.c + * Simple utility to get and set the clock on an iDevice + * + * Copyright (c) 2011 Martin Szulecki All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#if HAVE_LANGINFO_CODESET +# include +#endif + +#include +#include + +#ifdef _DATE_FMT +# define DATE_FMT_LANGINFO() nl_langinfo (_DATE_FMT) +#else +# define DATE_FMT_LANGINFO() "" +#endif + +static void print_usage(int argc, char **argv) +{ + char *name = NULL; + + name = strrchr(argv[0], '/'); + printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); + printf("Display the current date or set it on an iDevice.\n\n"); + printf(" -d, --debug\t\tenable communication debugging\n"); + printf(" -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n"); + printf(" -s, --set TIMESTAMP\tset UTC time described by TIMESTAMP\n"); + printf(" -c, --sync\t\tset time of device to current system time\n"); + printf(" -h, --help\t\tprints usage information\n"); + printf("\n"); +} + +int main(int argc, char *argv[]) +{ + lockdownd_client_t client = NULL; + idevice_t phone = NULL; + idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; + int i; + char uuid[41]; + time_t setdate = 0; + plist_t node = NULL; + uuid[0] = 0; + uint64_t datetime = 0; + time_t rawtime; + double offset_from_utc = 0.0; + struct tm * tmp; + char const *format = NULL; + char buffer[80]; + int tzshift = 0; + + /* parse cmdline args */ + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { + idevice_set_debug_level(1); + continue; + } + else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) { + i++; + if (!argv[i] || (strlen(argv[i]) != 40)) { + print_usage(argc, argv); + return 0; + } + strcpy(uuid, argv[i]); + continue; + } + else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--set")) { + i++; + if (!argv[i] || (strlen(argv[i]) <= 1)) { + print_usage(argc, argv); + return 0; + } + setdate = atoi(argv[i]); + if (setdate == 0) { + printf("ERROR: Invalid timestamp value.\n"); + print_usage(argc, argv); + return 0; + } + tzshift = 1; + continue; + } + else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--sync")) { + i++; + /* get current time */ + setdate = time(NULL); + /* convert it to local time which sets timezone/daylight variables */ + tmp = localtime(&setdate); + /* recalculate to make it UTC */ + setdate = mktime(tmp) - timezone - (daylight ? 3600 : 0 ); + tzshift = 0; + continue; + } + else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + print_usage(argc, argv); + return 0; + } + else { + print_usage(argc, argv); + return 0; + } + } + + /* determine a date format */ + if (!format) { + format = DATE_FMT_LANGINFO (); + if (!*format) { + format = "%a %b %e %H:%M:%S %Z %Y"; + } + } + + if (uuid[0] != 0) { + ret = idevice_new(&phone, uuid); + if (ret != IDEVICE_E_SUCCESS) { + printf("No device found with uuid %s, is it plugged in?\n", uuid); + return -1; + } + } + else + { + ret = idevice_new(&phone, NULL); + if (ret != IDEVICE_E_SUCCESS) { + printf("No device found, is it plugged in?\n"); + return -1; + } + } + + if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "idevicedate")) { + idevice_free(phone); + return -1; + } + + /* read timezone offset of device, needed for conversions */ + if (lockdownd_get_value(client, NULL, "TimeZoneOffsetFromUTC", &node) == LOCKDOWN_E_SUCCESS) { + if (node) { + plist_get_real_val(node, &offset_from_utc); + plist_free(node); + node = NULL; + } + } + + /* get or set? */ + if (setdate == 0) { + /* get time value from device */ + if(lockdownd_get_value(client, NULL, "TimeIntervalSince1970", &node) == LOCKDOWN_E_SUCCESS) { + if (node) { + plist_get_uint_val(node, &datetime); + plist_free(node); + node = NULL; + + /* date/time calculations */ + rawtime = (time_t)datetime; + tmp = localtime(&rawtime); + tmp->tm_gmtoff = offset_from_utc; + + /* finally we format and print the current date */ + strftime(buffer, 80, format, tmp); + puts(buffer); + } + } + } else { + if (tzshift) { + /* if we had provided a timestamp and have to adjust according to the device's timezone */ + setdate = setdate - offset_from_utc; + } + datetime = setdate; + + if(lockdownd_set_value(client, NULL, "TimeIntervalSince1970", plist_new_uint(datetime)) == LOCKDOWN_E_SUCCESS) { + tmp = localtime(&setdate); + strftime(buffer, 80, format, tmp); + puts(buffer); + } else { + printf("ERROR: Failed to set date on device.\n"); + } + } + + lockdownd_client_free(client); + idevice_free(phone); + + return 0; +} + -- cgit v1.1-32-gdbae