summaryrefslogtreecommitdiffstats
path: root/src/NotificationProxy.c
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2009-03-16 21:40:33 +0100
committerGravatar Jonathan Beck2009-03-16 21:40:33 +0100
commit19992c668afeb53a28e08a1f61572b5379f87590 (patch)
tree4f1784b81afe88a6240889961b5fc3d2b0d6a82e /src/NotificationProxy.c
parent0d05f8de79ee91e9be80c6296eff9ce216582ba4 (diff)
parent201e1ff5bf2054fcb1c0cc3cd7ba1e229dbde3fa (diff)
downloadlibimobiledevice-19992c668afeb53a28e08a1f61572b5379f87590.tar.gz
libimobiledevice-19992c668afeb53a28e08a1f61572b5379f87590.tar.bz2
Merge branch 'master' into contact_sync
Conflicts: configure.ac dev/Makefile.am include/libiphone/libiphone.h src/AFC.c src/AFC.h src/Makefile.am
Diffstat (limited to 'src/NotificationProxy.c')
-rw-r--r--src/NotificationProxy.c263
1 files changed, 263 insertions, 0 deletions
diff --git a/src/NotificationProxy.c b/src/NotificationProxy.c
new file mode 100644
index 0000000..eec7857
--- /dev/null
+++ b/src/NotificationProxy.c
@@ -0,0 +1,263 @@
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 <plist/plist.h>
24#include "NotificationProxy.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 char *XML_content = NULL;
107 uint32_t length = 0;
108 int bytes = 0;
109 iphone_error_t ret;
110 unsigned char sndbuf[4096];
111 int sndlen = 0;
112 int nlen = 0;
113 plist_t dict = NULL;
114
115 if (!client || !notification) {
116 return IPHONE_E_INVALID_ARG;
117 }
118 np_lock(client);
119
120 dict = plist_new_dict();
121 plist_add_sub_key_el(dict, "Command");
122 plist_add_sub_string_el(dict, "PostNotification");
123 plist_add_sub_key_el(dict, "Name");
124 plist_add_sub_string_el(dict, notification);
125 plist_to_xml(dict, &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 plist_free(dict);
135 dict = NULL;
136 free(XML_content);
137 XML_content = NULL;
138
139 dict = plist_new_dict();
140 plist_add_sub_key_el(dict, "Command");
141 plist_add_sub_string_el(dict, "Shutdown");
142 plist_to_xml(dict, &XML_content, &length);
143
144 nlen = htonl(length);
145
146 memcpy(sndbuf+sndlen, &nlen, 4);
147 sndlen+=4;
148
149 memcpy(sndbuf+sndlen, XML_content, length);
150 sndlen+=length;
151
152 plist_free(dict);
153 dict = NULL;
154 free(XML_content);
155 XML_content = NULL;
156
157 log_debug_buffer(sndbuf, sndlen);
158
159 iphone_mux_send(client->connection, sndbuf, sndlen, &bytes);
160 if (bytes <= 0) {
161 np_unlock(client);
162 return bytes;
163 }
164
165 np_unlock(client);
166 return bytes;
167}
168
169/** Notifies the iphone to send a notification on certain events.
170 *
171 * observation messages seen so far:
172 * com.apple.itunes-client.syncCancelRequest
173 * com.apple.itunes-client.syncSuspendRequest
174 * com.apple.itunes-client.syncResumeRequest
175 * com.apple.mobile.lockdown.phone_number_changed
176 * com.apple.mobile.lockdown.device_name_changed
177 * com.apple.springboard.attemptactivation
178 * com.apple.mobile.data_sync.domain_changed
179 * com.apple.mobile.application_installed
180 * com.apple.mobile.application_uninstalled
181 *
182 * @param client The client to send to
183 */
184iphone_error_t iphone_np_observe_notification( iphone_np_client_t client )
185{
186 plist_t dict = NULL;
187 char *XML_content = NULL;
188 uint32_t length = 0;
189 int bytes = 0;
190 iphone_error_t ret;
191 unsigned char sndbuf[4096];
192 int sndlen = 0;
193 int nlen = 0;
194 int i=0;
195 const char *notifications[10] = {
196 "com.apple.itunes-client.syncCancelRequest",
197 "com.apple.itunes-client.syncSuspendRequest",
198 "com.apple.itunes-client.syncResumeRequest",
199 "com.apple.mobile.lockdown.phone_number_changed",
200 "com.apple.mobile.lockdown.device_name_changed",
201 "com.apple.springboard.attemptactivation",
202 "com.apple.mobile.data_sync.domain_changed",
203 "com.apple.mobile.application_installed",
204 "com.apple.mobile.application_uninstalled",
205 NULL};
206
207 sndlen = 0;
208
209 if (!client) {
210 return IPHONE_E_INVALID_ARG;
211 }
212 np_lock(client);
213
214 while (notifications[i]) {
215
216 dict = plist_new_dict();
217 plist_add_sub_key_el(dict, "Command");
218 plist_add_sub_string_el(dict, "ObserveNotification");
219 plist_add_sub_key_el(dict, "Name");
220 plist_add_sub_string_el(dict, notifications[i++]);
221 plist_to_xml(dict, &XML_content, &length);
222
223 nlen = htonl(length);
224 memcpy(sndbuf+sndlen, &nlen, 4);
225 sndlen += 4;
226 memcpy(sndbuf+sndlen, XML_content, length);
227 sndlen += length;
228
229 plist_free(dict);
230 dict = NULL;
231 free(XML_content);
232 XML_content = NULL;
233 }
234
235 dict = plist_new_dict();
236 plist_add_sub_key_el(dict, "Command");
237 plist_add_sub_string_el(dict, "Shutdown");
238 plist_to_xml(dict, &XML_content, &length);
239
240 nlen = htonl(length);
241
242 memcpy(sndbuf+sndlen, &nlen, 4);
243 sndlen+=4;
244
245 memcpy(sndbuf+sndlen, XML_content, length);
246 sndlen+=length;
247
248 plist_free(dict);
249 dict = NULL;
250 free(XML_content);
251 XML_content = NULL;
252
253 log_debug_buffer(sndbuf, sndlen);
254
255 iphone_mux_send(client->connection, sndbuf, sndlen, &bytes);
256 if (bytes <= 0) {
257 np_unlock(client);
258 return bytes;
259 }
260
261 np_unlock(client);
262 return bytes;
263}