summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2014-01-31 23:49:58 +0100
committerGravatar Nikias Bassen2014-01-31 23:49:58 +0100
commit33d197799fee98310695639e055852e2a33d2fe3 (patch)
tree13a758c18e48bef7c77f47dbee35b8d36efe420e /tools
parentd526a089b39e829d565e7f8ddc5b0714eee5b45b (diff)
downloadlibimobiledevice-33d197799fee98310695639e055852e2a33d2fe3.tar.gz
libimobiledevice-33d197799fee98310695639e055852e2a33d2fe3.tar.bz2
tools: add convenience tool 'idevicename' to get or set device name
Diffstat (limited to 'tools')
-rw-r--r--tools/Makefile.am7
-rw-r--r--tools/idevicename.c120
2 files changed, 126 insertions, 1 deletions
diff --git a/tools/Makefile.am b/tools/Makefile.am
index 8c29487..c280715 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -3,13 +3,18 @@ AM_CPPFLAGS = -I$(top_srcdir)/include
3AM_CFLAGS = $(GLOBAL_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgcrypt_CFLAGS) $(openssl_CFLAGS) $(libplist_CFLAGS) $(LFS_CFLAGS) 3AM_CFLAGS = $(GLOBAL_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgcrypt_CFLAGS) $(openssl_CFLAGS) $(libplist_CFLAGS) $(LFS_CFLAGS)
4AM_LDFLAGS = $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgcrypt_LIBS) $(openssl_LIBS) $(libplist_LIBS) 4AM_LDFLAGS = $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgcrypt_LIBS) $(openssl_LIBS) $(libplist_LIBS)
5 5
6bin_PROGRAMS = idevice_id ideviceinfo idevicepair idevicesyslog ideviceimagemounter idevicescreenshot ideviceenterrecovery idevicedate idevicebackup idevicebackup2 ideviceprovision idevicedebugserverproxy idevicediagnostics 6bin_PROGRAMS = idevice_id ideviceinfo idevicename idevicepair idevicesyslog ideviceimagemounter idevicescreenshot ideviceenterrecovery idevicedate idevicebackup idevicebackup2 ideviceprovision idevicedebugserverproxy idevicediagnostics
7 7
8ideviceinfo_SOURCES = ideviceinfo.c 8ideviceinfo_SOURCES = ideviceinfo.c
9ideviceinfo_CFLAGS = $(AM_CFLAGS) 9ideviceinfo_CFLAGS = $(AM_CFLAGS)
10ideviceinfo_LDFLAGS = $(AM_LDFLAGS) 10ideviceinfo_LDFLAGS = $(AM_LDFLAGS)
11ideviceinfo_LDADD = ../src/libimobiledevice.la 11ideviceinfo_LDADD = ../src/libimobiledevice.la
12 12
13idevicename_SOURCES = idevicename.c
14idevicename_CFLAGS = $(AM_CFLAGS)
15idevicename_LDFLAGS = $(AM_LDFLAGS)
16idevicename_LDADD = ../src/libimobiledevice.la
17
13idevicepair_SOURCES = idevicepair.c 18idevicepair_SOURCES = idevicepair.c
14idevicepair_CFLAGS = $(AM_CFLAGS) 19idevicepair_CFLAGS = $(AM_CFLAGS)
15idevicepair_LDFLAGS = $(top_srcdir)/common/libinternalcommon.la $(AM_LDFLAGS) 20idevicepair_LDFLAGS = $(top_srcdir)/common/libinternalcommon.la $(AM_LDFLAGS)
diff --git a/tools/idevicename.c b/tools/idevicename.c
new file mode 100644
index 0000000..a991a8f
--- /dev/null
+++ b/tools/idevicename.c
@@ -0,0 +1,120 @@
1/*
2 * idevicename.c
3 * Simple utility to get or set the device name
4 *
5 * Copyright (c) 2014 Nikias Bassen, 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
22#include <stdio.h>
23#include <string.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <getopt.h>
27
28#include <libimobiledevice/libimobiledevice.h>
29#include <libimobiledevice/lockdown.h>
30
31static void print_usage()
32{
33 printf("\nUsage: idevicename [OPTIONS] [NAME]\n");
34 printf(" --udid|-u UDID use UDID to target a specific device\n");
35 printf("\n");
36}
37
38int main(int argc, char** argv)
39{
40 int res = -1;
41 char* udid = NULL;
42
43 int c = 0;
44 int optidx = 0;
45 const struct option longopts[] = {
46 { "udid", required_argument, NULL, 'u' },
47 { NULL, 0, NULL, 0}
48 };
49
50 signal(SIGPIPE, SIG_IGN);
51
52 while ((c = getopt_long(argc, argv, "u:", longopts, &optidx)) != -1) {
53 switch (c) {
54 case 'u':
55 udid = strdup(optarg);
56 break;
57 default:
58 break;
59 }
60 }
61
62 argc -= optind;
63 argv += optind;
64
65 if (argc > 1) {
66 print_usage();
67 return -1;
68 }
69
70 idevice_t device = NULL;
71 if (idevice_new(&device, udid) != IDEVICE_E_SUCCESS) {
72 fprintf(stderr, "ERROR: Could not connect to device\n");
73 return -1;
74 }
75
76 lockdownd_client_t lockdown = NULL;
77 lockdownd_error_t lerr = lockdownd_client_new_with_handshake(device, &lockdown, "idevicename");
78 if (lerr != LOCKDOWN_E_SUCCESS) {
79 idevice_free(device);
80 fprintf(stderr, "ERROR: lockdown connection failed, lockdown error %d\n", lerr);
81 return -1;
82 }
83
84 plist_t node = NULL;
85
86 if (argc == 0) {
87 // getting device name
88 char* name = NULL;
89 lerr = lockdownd_get_value(lockdown, NULL, "DeviceName", &node);
90 if (node) {
91 plist_get_string_val(node, &name);
92 plist_free(node);
93 }
94 if (name) {
95 printf("%s\n", name);
96 free(name);
97 res = 0;
98 } else {
99 fprintf(stderr, "ERROR: Could not get device name, lockdown error %d\n", lerr);
100 }
101 } else {
102 // setting device name
103 lerr = lockdownd_set_value(lockdown, NULL, "DeviceName", plist_new_string(argv[0]));
104 if (lerr == LOCKDOWN_E_SUCCESS) {
105 printf("device name set to '%s'\n", argv[0]);
106 res = 0;
107 } else {
108 fprintf(stderr, "ERROR: Could not set device name, lockdown error %d\n", lerr);
109 }
110 }
111
112 lockdownd_client_free(lockdown);
113 idevice_free(device);
114
115 if (udid) {
116 free(udid);
117 }
118
119 return res;
120}