summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/SBServices.c155
1 files changed, 27 insertions, 128 deletions
diff --git a/src/SBServices.c b/src/SBServices.c
index 9849415..1296245 100644
--- a/src/SBServices.c
+++ b/src/SBServices.c
@@ -87,112 +87,39 @@ sbservices_error_t sbservices_client_free(sbservices_client_t client)
87 return SBSERVICES_E_SUCCESS; 87 return SBSERVICES_E_SUCCESS;
88} 88}
89 89
90/**
91 * Sends a binary plist to the device using the connection specified in client.
92 * This function is only used internally.
93 *
94 * @param client InstallationProxy to send data to
95 * @param dict plist to send
96 *
97 * @return SBSERVICES_E_SUCCESS on success, SBSERVICES_E_INVALID_ARG when
98 * client or dict are NULL, SBSERVICES_E_PLIST_ERROR when dict is not a
99 * valid plist, or SBSERVICES_E_UNKNOWN_ERROR when an unspecified error
100 * occurs.
101 */
102static sbservices_error_t sbservices_plist_send(sbservices_client_t client, plist_t dict)
103{
104 char *content = NULL;
105 uint32_t length = 0;
106 uint32_t nlen = 0;
107 int bytes = 0;
108 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR;
109
110 if (!client || !dict) {
111 return SBSERVICES_E_INVALID_ARG;
112 }
113
114 plist_to_bin(dict, &content, &length);
115
116 if (!content || length == 0) {
117 return SBSERVICES_E_PLIST_ERROR;
118 }
119
120 nlen = htonl(length);
121 log_debug_msg("%s: sending %d bytes\n", __func__, length);
122 iphone_device_send(client->connection, (const char*)&nlen, sizeof(nlen), (uint32_t*)&bytes);
123 if (bytes == sizeof(nlen)) {
124 iphone_device_send(client->connection, content, length, (uint32_t*)&bytes);
125 if (bytes > 0) {
126 if ((uint32_t)bytes == length) {
127 res = SBSERVICES_E_SUCCESS;
128 } else {
129 log_debug_msg("%s: ERROR: Could not send all data (%d of %d)!\n", __func__, bytes, length);
130 }
131 }
132 }
133 if (bytes <= 0) {
134 log_debug_msg("%s: ERROR: sending to device failed.\n", __func__);
135 }
136
137 free(content);
138
139 return res;
140}
141
142sbservices_error_t sbservices_get_icon_state(sbservices_client_t client, plist_t *state) 90sbservices_error_t sbservices_get_icon_state(sbservices_client_t client, plist_t *state)
143{ 91{
144 if (!client || !client->connection || !state) 92 if (!client || !client->connection || !state)
145 return SBSERVICES_E_INVALID_ARG; 93 return SBSERVICES_E_INVALID_ARG;
146 94
147 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR; 95 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR;
148 uint32_t pktlen = 0;
149 uint32_t bytes = 0;
150 96
151 plist_t dict = plist_new_dict(); 97 plist_t dict = plist_new_dict();
152 plist_dict_insert_item(dict, "command", plist_new_string("getIconState")); 98 plist_dict_insert_item(dict, "command", plist_new_string("getIconState"));
153 99
154 sbs_lock(client); 100 sbs_lock(client);
155 101
156 res = sbservices_plist_send(client, dict); 102 if (iphone_device_send_binary_plist(client->connection, dict) != IPHONE_E_SUCCESS) {
157 plist_free(dict);
158 if (res != SBSERVICES_E_SUCCESS) {
159 log_debug_msg("%s: could not send plist\n", __func__); 103 log_debug_msg("%s: could not send plist\n", __func__);
160 goto leave_unlock; 104 goto leave_unlock;
161 } 105 }
106 plist_free(dict);
107 dict = NULL;
162 108
163 iphone_device_recv(client->connection, (char*)&pktlen, sizeof(pktlen), &bytes); 109 if (iphone_device_receive_plist(client->connection, state) == IPHONE_E_SUCCESS) {
164 log_debug_msg("%s: initial read=%i\n", __func__, bytes); 110 res = SBSERVICES_E_SUCCESS;
165 if (bytes < 4) {
166 log_debug_msg("%s: initial read failed!\n");
167 res = 0;
168 } else { 111 } else {
169 if ((char)pktlen == 0) { 112 log_debug_msg("%s: could not get icon state!\n", __func__);
170 char *content = NULL; 113 if (*state) {
171 uint32_t curlen = 0; 114 plist_free(*state);
172 pktlen = ntohl(pktlen); 115 *state = NULL;
173 log_debug_msg("%s: %d bytes following\n", __func__, pktlen);
174 content = (char*)malloc(pktlen);
175 log_debug_msg("pointer %p\n", content);
176
177 while (curlen < pktlen) {
178 iphone_device_recv(client->connection, content+curlen, pktlen-curlen, &bytes);
179 if (bytes <= 0) {
180 res = SBSERVICES_E_UNKNOWN_ERROR;
181 break;
182 }
183 log_debug_msg("%s: received %d bytes\n", __func__, bytes);
184 curlen += bytes;
185 }
186 log_debug_buffer(content, pktlen);
187 plist_from_bin(content, pktlen, state);
188 res = SBSERVICES_E_SUCCESS;
189 free(content);
190 } else {
191 res = SBSERVICES_E_UNKNOWN_ERROR;
192 } 116 }
193 } 117 }
194 118
195leave_unlock: 119leave_unlock:
120 if (dict) {
121 plist_free(dict);
122 }
196 sbs_unlock(client); 123 sbs_unlock(client);
197 return res; 124 return res;
198} 125}
@@ -210,15 +137,16 @@ sbservices_error_t sbservices_set_icon_state(sbservices_client_t client, plist_t
210 137
211 sbs_lock(client); 138 sbs_lock(client);
212 139
213 res = sbservices_plist_send(client, dict); 140 if (iphone_device_send_binary_plist(client->connection, dict) != IPHONE_E_SUCCESS) {
214 plist_free(dict);
215 if (res != SBSERVICES_E_SUCCESS) {
216 log_debug_msg("%s: could not send plist\n", __func__); 141 log_debug_msg("%s: could not send plist\n", __func__);
217 goto leave_unlock; 142 goto leave_unlock;
218 } 143 }
219 // NO RESPONSE 144 // NO RESPONSE
220 145
221leave_unlock: 146leave_unlock:
147 if (dict) {
148 plist_free(dict);
149 }
222 sbs_unlock(client); 150 sbs_unlock(client);
223 return res; 151 return res;
224} 152}
@@ -229,8 +157,6 @@ sbservices_error_t sbservices_get_icon_pngdata(sbservices_client_t client, const
229 return SBSERVICES_E_INVALID_ARG; 157 return SBSERVICES_E_INVALID_ARG;
230 158
231 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR; 159 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR;
232 uint32_t pktlen = 0;
233 uint32_t bytes = 0;
234 160
235 plist_t dict = plist_new_dict(); 161 plist_t dict = plist_new_dict();
236 plist_dict_insert_item(dict, "command", plist_new_string("getIconPNGData")); 162 plist_dict_insert_item(dict, "command", plist_new_string("getIconPNGData"));
@@ -238,52 +164,25 @@ sbservices_error_t sbservices_get_icon_pngdata(sbservices_client_t client, const
238 164
239 sbs_lock(client); 165 sbs_lock(client);
240 166
241 res = sbservices_plist_send(client, dict); 167 if (iphone_device_send_binary_plist(client->connection, dict) != IPHONE_E_SUCCESS) {
242 plist_free(dict);
243 if (res != SBSERVICES_E_SUCCESS) {
244 log_debug_msg("%s: could not send plist\n", __func__); 168 log_debug_msg("%s: could not send plist\n", __func__);
245 goto leave_unlock; 169 goto leave_unlock;
246 } 170 }
171 plist_free(dict);
247 172
248 iphone_device_recv(client->connection, (char*)&pktlen, sizeof(pktlen), &bytes); 173 dict = NULL;
249 log_debug_msg("%s: initial read=%i\n", __func__, bytes); 174 if (iphone_device_receive_plist(client->connection, &dict) == IPHONE_E_SUCCESS) {
250 if (bytes < 4) { 175 plist_t node = plist_dict_get_item(dict, "pngData");
251 log_debug_msg("%s: initial read failed!\n"); 176 if (node) {
252 res = 0; 177 plist_get_data_val(node, pngdata, pngsize);
253 } else {
254 if ((char)pktlen == 0) {
255 char *content = NULL;
256 uint32_t curlen = 0;
257 pktlen = ntohl(pktlen);
258 log_debug_msg("%s: %d bytes following\n", __func__, pktlen);
259 content = (char*)malloc(pktlen);
260 log_debug_msg("pointer %p\n", content);
261
262 while (curlen < pktlen) {
263 iphone_device_recv(client->connection, content+curlen, pktlen-curlen, &bytes);
264 if (bytes <= 0) {
265 res = SBSERVICES_E_UNKNOWN_ERROR;
266 break;
267 }
268 log_debug_msg("%s: received %d bytes\n", __func__, bytes);
269 curlen += bytes;
270 }
271 log_debug_buffer(content, pktlen);
272 plist_t pngdict = NULL;
273 plist_from_bin(content, pktlen, &pngdict);
274 plist_t node = plist_dict_get_item(pngdict, "pngData");
275 if (node) {
276 plist_get_data_val(node, pngdata, pngsize);
277 }
278 plist_free(pngdict);
279 res = SBSERVICES_E_SUCCESS;
280 free(content);
281 } else {
282 res = SBSERVICES_E_UNKNOWN_ERROR;
283 } 178 }
179 res = SBSERVICES_E_SUCCESS;
284 } 180 }
285 181
286leave_unlock: 182leave_unlock:
183 if (dict) {
184 plist_free(dict);
185 }
287 sbs_unlock(client); 186 sbs_unlock(client);
288 return res; 187 return res;
289 188