summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Matt Colyer2009-03-09 18:44:30 -0700
committerGravatar Matt Colyer2009-03-09 18:44:30 -0700
commitd85a44994876b2c1f5b457e8f72c6edb3e32a365 (patch)
tree6aa2e4f85035d95acf1961a29e6f97561ec9557f /src
parent774c3ed001a48a52cceb450bca292dd6112d683f (diff)
downloadlibimobiledevice-d85a44994876b2c1f5b457e8f72c6edb3e32a365.tar.gz
libimobiledevice-d85a44994876b2c1f5b457e8f72c6edb3e32a365.tar.bz2
Add forgotten file.
Diffstat (limited to 'src')
-rw-r--r--src/NotificationProxy.c256
1 files changed, 256 insertions, 0 deletions
diff --git a/src/NotificationProxy.c b/src/NotificationProxy.c
new file mode 100644
index 0000000..b0c10ea
--- /dev/null
+++ b/src/NotificationProxy.c
@@ -0,0 +1,256 @@
1/*
2 * NotificationProxy.c
3 * Notification Proxy implementation.
4 *
5 * Copyright (c) 2009 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 "NotificationProxy.h"
24#include "plist.h"
25#include "utils.h"
26
27/** Locks an NP client, done for thread safety stuff.
28 *
29 * @param client The NP
30 */
31static void np_lock(iphone_np_client_t client)
32{
33 log_debug_msg("NP: Locked\n");
34 g_mutex_lock(client->mutex);
35}
36
37/** Unlocks an NP client, done for thread safety stuff.
38 *
39 * @param client The NP
40 */
41static void np_unlock(iphone_np_client_t client)
42{
43 log_debug_msg("NP: Unlocked\n");
44 g_mutex_unlock(client->mutex);
45}
46
47/** Makes a connection to the NP service on the phone.
48 *
49 * @param phone The iPhone to connect on.
50 * @param s_port The source port.
51 * @param d_port The destination port.
52 *
53 * @return A handle to the newly-connected client or NULL upon error.
54 */
55iphone_error_t iphone_np_new_client ( iphone_device_t device, int src_port, int dst_port, iphone_np_client_t *client )
56{
57 int ret = IPHONE_E_SUCCESS;
58
59 //makes sure thread environment is available
60 if (!g_thread_supported())
61 g_thread_init(NULL);
62 iphone_np_client_t client_loc = (iphone_np_client_t) malloc(sizeof(struct iphone_np_client_int));
63
64 if (!device)
65 return IPHONE_E_INVALID_ARG;
66
67 // Attempt connection
68 client_loc->connection = NULL;
69 ret = iphone_mux_new_client(device, src_port, dst_port, &client_loc->connection);
70 if (IPHONE_E_SUCCESS != ret || !client_loc->connection) {
71 free(client_loc);
72 return ret;
73 }
74
75 client_loc->mutex = g_mutex_new();
76
77 *client = client_loc;
78 return IPHONE_E_SUCCESS;
79}
80
81/** Disconnects an NP client from the phone.
82 *
83 * @param client The client to disconnect.
84 */
85iphone_error_t iphone_np_free_client ( iphone_np_client_t client )
86{
87 if (!client || !client->connection )
88 return IPHONE_E_INVALID_ARG;
89
90 iphone_mux_free_client(client->connection);
91 free(client);
92 return IPHONE_E_SUCCESS;
93}
94
95/** Sends a notification to the NP client.
96 *
97 * notification messages seen so far:
98 * com.apple.itunes-mobdev.syncWillStart
99 * com.apple.itunes-mobdev.syncDidStart
100 *
101 * @param client The client to send to
102 * @param notification The notification Message
103 */
104iphone_error_t iphone_np_post_notification( iphone_np_client_t client, const char *notification )
105{
106 xmlDocPtr plist;
107 xmlNode *dict, *key;
108 char *XML_content;
109 uint32_t length;
110 int bytes;
111 iphone_error_t ret;
112 unsigned char sndbuf[4096];
113 int sndlen = 0;
114 int nlen;
115
116 if (!client || !notification) {
117 return IPHONE_E_INVALID_ARG;
118 }
119 np_lock(client);
120
121 plist = new_plist();
122 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0);
123 key = add_key_str_dict_element(plist, dict, "Command", "PostNotification", 1);
124 key = add_key_str_dict_element(plist, dict, "Name", notification, 1);
125 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length);
126
127 nlen = htonl(length);
128
129 memcpy(sndbuf+sndlen, &nlen, 4);
130 sndlen += 4;
131 memcpy(sndbuf+sndlen, XML_content, length);
132 sndlen += length;
133
134 xmlFree(XML_content);
135 xmlFreeDoc(plist);
136
137 plist = new_plist();
138 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0);
139 key = add_key_str_dict_element(plist, dict, "Command", "Shutdown", 1);
140 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length);
141
142 nlen = htonl(length);
143
144 memcpy(sndbuf+sndlen, &nlen, 4);
145 sndlen+=4;
146
147 memcpy(sndbuf+sndlen, XML_content, length);
148 sndlen+=length;
149
150 xmlFree(XML_content);
151 xmlFreeDoc(plist);
152 plist = NULL;
153
154 log_debug_buffer(sndbuf, sndlen);
155
156 iphone_mux_send(client->connection, sndbuf, sndlen, &bytes);
157 if (bytes <= 0) {
158 np_unlock(client);
159 return bytes;
160 }
161
162 np_unlock(client);
163 return bytes;
164}
165
166/** Notifies the iphone to send a notification on certain events.
167 *
168 * observation messages seen so far:
169 * com.apple.itunes-client.syncCancelRequest
170 * com.apple.itunes-client.syncSuspendRequest
171 * com.apple.itunes-client.syncResumeRequest
172 * com.apple.mobile.lockdown.phone_number_changed
173 * com.apple.mobile.lockdown.device_name_changed
174 * com.apple.springboard.attemptactivation
175 * com.apple.mobile.data_sync.domain_changed
176 * com.apple.mobile.application_installed
177 * com.apple.mobile.application_uninstalled
178 *
179 * @param client The client to send to
180 */
181iphone_error_t iphone_np_observe_notification( iphone_np_client_t client )
182{
183 xmlDocPtr plist;
184 xmlNode *dict, *key;
185 char *XML_content;
186 uint32_t length;
187 int bytes;
188 iphone_error_t ret;
189 unsigned char sndbuf[4096];
190 int sndlen = 0;
191 int nlen;
192 int i=0;
193 char *notifications[10] = {
194 "com.apple.itunes-client.syncCancelRequest",
195 "com.apple.itunes-client.syncSuspendRequest",
196 "com.apple.itunes-client.syncResumeRequest",
197 "com.apple.mobile.lockdown.phone_number_changed",
198 "com.apple.mobile.lockdown.device_name_changed",
199 "com.apple.springboard.attemptactivation",
200 "com.apple.mobile.data_sync.domain_changed",
201 "com.apple.mobile.application_installed",
202 "com.apple.mobile.application_uninstalled",
203 NULL};
204
205 sndlen = 0;
206
207 if (!client) {
208 return IPHONE_E_INVALID_ARG;
209 }
210 np_lock(client);
211
212 while (notifications[i]) {
213 plist = new_plist();
214 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0);
215 key = add_key_str_dict_element(plist, dict, "Command", "ObserveNotification", 1);
216 key = add_key_str_dict_element(plist, dict, "Name", notifications[i++], 1);
217 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length);
218
219 nlen = htonl(length);
220 memcpy(sndbuf+sndlen, &nlen, 4);
221 sndlen += 4;
222 memcpy(sndbuf+sndlen, XML_content, length);
223 sndlen += length;
224
225 xmlFree(XML_content);
226 xmlFreeDoc(plist);
227 }
228
229 plist = new_plist();
230 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0);
231 key = add_key_str_dict_element(plist, dict, "Command", "Shutdown", 1);
232 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length);
233
234 nlen = htonl(length);
235
236 memcpy(sndbuf+sndlen, &nlen, 4);
237 sndlen+=4;
238
239 memcpy(sndbuf+sndlen, XML_content, length);
240 sndlen+=length;
241
242 xmlFree(XML_content);
243 xmlFreeDoc(plist);
244 plist = NULL;
245
246 log_debug_buffer(sndbuf, sndlen);
247
248 iphone_mux_send(client->connection, sndbuf, sndlen, &bytes);
249 if (bytes <= 0) {
250 np_unlock(client);
251 return bytes;
252 }
253
254 np_unlock(client);
255 return bytes;
256}