summaryrefslogtreecommitdiffstats
path: root/src/AFC.c
diff options
context:
space:
mode:
authorGravatar Martin Szulecki2010-01-12 19:09:36 +0100
committerGravatar Martin Szulecki2010-01-12 19:09:36 +0100
commit342f4e929888c0aaa088e39fb98a74957bf45fa7 (patch)
tree905bafb1b353b8ac21e3fb1f9c773d218a5c878e /src/AFC.c
parentbf3dc421b2b5ccfe2fcd3cd4ec1ef90f39599600 (diff)
downloadlibimobiledevice-342f4e929888c0aaa088e39fb98a74957bf45fa7.tar.gz
libimobiledevice-342f4e929888c0aaa088e39fb98a74957bf45fa7.tar.bz2
Refactor and unify internal debug system for ease of use and verbosity
This introduces a new debug_info macro which automatically prints the calling function, file and line number information instead of having that information passed to every old log_debug_msg call.
Diffstat (limited to 'src/AFC.c')
-rw-r--r--src/AFC.c109
1 files changed, 53 insertions, 56 deletions
diff --git a/src/AFC.c b/src/AFC.c
index fe8e1af..d430e08 100644
--- a/src/AFC.c
+++ b/src/AFC.c
@@ -36,7 +36,7 @@ static const int MAXIMUM_PACKET_SIZE = (2 << 15);
36 */ 36 */
37static void afc_lock(afc_client_t client) 37static void afc_lock(afc_client_t client)
38{ 38{
39 log_debug_msg("%s: Locked\n", __func__); 39 debug_info("Locked");
40 g_mutex_lock(client->mutex); 40 g_mutex_lock(client->mutex);
41} 41}
42 42
@@ -46,7 +46,7 @@ static void afc_lock(afc_client_t client)
46 */ 46 */
47static void afc_unlock(afc_client_t client) 47static void afc_unlock(afc_client_t client)
48{ 48{
49 log_debug_msg("%s: Unlocked\n", __func__); 49 debug_info("Unlocked");
50 g_mutex_unlock(client->mutex); 50 g_mutex_unlock(client->mutex);
51} 51}
52 52
@@ -155,12 +155,11 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, ui
155 if (client->afc_packet->this_length != client->afc_packet->entire_length) { 155 if (client->afc_packet->this_length != client->afc_packet->entire_length) {
156 offset = client->afc_packet->this_length - sizeof(AFCPacket); 156 offset = client->afc_packet->this_length - sizeof(AFCPacket);
157 157
158 log_debug_msg("%s: Offset: %i\n", __func__, offset); 158 debug_info("Offset: %i", offset);
159 if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { 159 if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) {
160 log_debug_msg("%s: Length did not resemble what it was supposed", __func__); 160 debug_info("Length did not resemble what it was supposed to based on packet");
161 log_debug_msg("to based on the packet.\n"); 161 debug_info("length minus offset: %i", length - offset);
162 log_debug_msg("%s: length minus offset: %i\n", __func__, length - offset); 162 debug_info("rest of packet: %i\n", client->afc_packet->entire_length - client->afc_packet->this_length);
163 log_debug_msg("%s: rest of packet: %i\n", __func__, client->afc_packet->entire_length - client->afc_packet->this_length);
164 return AFC_E_INTERNAL_ERROR; 163 return AFC_E_INTERNAL_ERROR;
165 } 164 }
166 165
@@ -182,10 +181,10 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, ui
182 } 181 }
183 *bytes_sent += sent; 182 *bytes_sent += sent;
184 183
185 log_debug_msg("%s: sent the first now go with the second\n", __func__); 184 debug_info("sent the first now go with the second");
186 log_debug_msg("%s: Length: %i\n", __func__, length - offset); 185 debug_info("Length: %i", length - offset);
187 log_debug_msg("%s: Buffer: \n", __func__); 186 debug_info("Buffer: ");
188 log_debug_buffer(data + offset, length - offset); 187 debug_buffer(data + offset, length - offset);
189 188
190 sent = 0; 189 sent = 0;
191 iphone_device_send(client->connection, data + offset, length - offset, &sent); 190 iphone_device_send(client->connection, data + offset, length - offset, &sent);
@@ -193,11 +192,10 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, ui
193 *bytes_sent = sent; 192 *bytes_sent = sent;
194 return AFC_E_SUCCESS; 193 return AFC_E_SUCCESS;
195 } else { 194 } else {
196 log_debug_msg("%s: doin things the old way\n", __func__); 195 debug_info("doin things the old way");
197 log_debug_msg("%s: packet length = %i\n", __func__, client->afc_packet->this_length); 196 debug_info("packet length = %i", client->afc_packet->this_length);
198 197
199 log_debug_buffer((char*)client->afc_packet, sizeof(AFCPacket)); 198 debug_buffer((char*)client->afc_packet, sizeof(AFCPacket));
200 log_debug_msg("\n");
201 199
202 /* send AFC packet header */ 200 /* send AFC packet header */
203 AFCPacket_to_LE(client->afc_packet); 201 AFCPacket_to_LE(client->afc_packet);
@@ -209,10 +207,9 @@ static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, ui
209 *bytes_sent += sent; 207 *bytes_sent += sent;
210 /* send AFC packet data (if there's data to send) */ 208 /* send AFC packet data (if there's data to send) */
211 if (length > 0) { 209 if (length > 0) {
212 log_debug_msg("%s: packet data follows\n", __func__); 210 debug_info("packet data follows");
213 211
214 log_debug_buffer(data, length); 212 debug_buffer(data, length);
215 log_debug_msg("\n");
216 iphone_device_send(client->connection, data, length, &sent); 213 iphone_device_send(client->connection, data, length, &sent);
217 *bytes_sent += sent; 214 *bytes_sent += sent;
218 } 215 }
@@ -244,36 +241,36 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint3
244 iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), bytes_recv); 241 iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), bytes_recv);
245 AFCPacket_from_LE(&header); 242 AFCPacket_from_LE(&header);
246 if (*bytes_recv == 0) { 243 if (*bytes_recv == 0) {
247 log_debug_msg("%s: Just didn't get enough.\n", __func__); 244 debug_info("Just didn't get enough.");
248 *dump_here = NULL; 245 *dump_here = NULL;
249 return AFC_E_MUX_ERROR; 246 return AFC_E_MUX_ERROR;
250 } else if (*bytes_recv < sizeof(AFCPacket)) { 247 } else if (*bytes_recv < sizeof(AFCPacket)) {
251 log_debug_msg("%s: Did not even get the AFCPacket header\n", __func__); 248 debug_info("Did not even get the AFCPacket header");
252 *dump_here = NULL; 249 *dump_here = NULL;
253 return AFC_E_MUX_ERROR; 250 return AFC_E_MUX_ERROR;
254 } 251 }
255 252
256 /* check if it's a valid AFC header */ 253 /* check if it's a valid AFC header */
257 if (strncmp(header.magic, AFC_MAGIC, AFC_MAGIC_LEN)) { 254 if (strncmp(header.magic, AFC_MAGIC, AFC_MAGIC_LEN)) {
258 log_debug_msg("%s: Invalid AFC packet received (magic != " AFC_MAGIC ")!\n", __func__); 255 debug_info("Invalid AFC packet received (magic != " AFC_MAGIC ")!");
259 } 256 }
260 257
261 /* check if it has the correct packet number */ 258 /* check if it has the correct packet number */
262 if (header.packet_num != client->afc_packet->packet_num) { 259 if (header.packet_num != client->afc_packet->packet_num) {
263 /* otherwise print a warning but do not abort */ 260 /* otherwise print a warning but do not abort */
264 log_debug_msg("%s: ERROR: Unexpected packet number (%lld != %lld) aborting.\n", __func__, header.packet_num, client->afc_packet->packet_num); 261 debug_info("ERROR: Unexpected packet number (%lld != %lld) aborting.", header.packet_num, client->afc_packet->packet_num);
265 *dump_here = NULL; 262 *dump_here = NULL;
266 return AFC_E_OP_HEADER_INVALID; 263 return AFC_E_OP_HEADER_INVALID;
267 } 264 }
268 265
269 /* then, read the attached packet */ 266 /* then, read the attached packet */
270 if (header.this_length < sizeof(AFCPacket)) { 267 if (header.this_length < sizeof(AFCPacket)) {
271 log_debug_msg("%s: Invalid AFCPacket header received!\n", __func__); 268 debug_info("Invalid AFCPacket header received!");
272 *dump_here = NULL; 269 *dump_here = NULL;
273 return AFC_E_OP_HEADER_INVALID; 270 return AFC_E_OP_HEADER_INVALID;
274 } else if ((header.this_length == header.entire_length) 271 } else if ((header.this_length == header.entire_length)
275 && header.entire_length == sizeof(AFCPacket)) { 272 && header.entire_length == sizeof(AFCPacket)) {
276 log_debug_msg("%s: Empty AFCPacket received!\n", __func__); 273 debug_info("Empty AFCPacket received!");
277 *dump_here = NULL; 274 *dump_here = NULL;
278 *bytes_recv = 0; 275 *bytes_recv = 0;
279 if (header.operation == AFC_OP_DATA) { 276 if (header.operation == AFC_OP_DATA) {
@@ -283,14 +280,14 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint3
283 } 280 }
284 } 281 }
285 282
286 log_debug_msg("%s: received AFC packet, full len=%lld, this len=%lld, operation=0x%llx\n", __func__, header.entire_length, header.this_length, header.operation); 283 debug_info("received AFC packet, full len=%lld, this len=%lld, operation=0x%llx", header.entire_length, header.this_length, header.operation);
287 284
288 entire_len = (uint32_t)header.entire_length - sizeof(AFCPacket); 285 entire_len = (uint32_t)header.entire_length - sizeof(AFCPacket);
289 this_len = (uint32_t)header.this_length - sizeof(AFCPacket); 286 this_len = (uint32_t)header.this_length - sizeof(AFCPacket);
290 287
291 /* this is here as a check (perhaps a different upper limit is good?) */ 288 /* this is here as a check (perhaps a different upper limit is good?) */
292 if (entire_len > (uint32_t)MAXIMUM_PACKET_SIZE) { 289 if (entire_len > (uint32_t)MAXIMUM_PACKET_SIZE) {
293 fprintf(stderr, "%s: entire_len is larger than MAXIMUM_PACKET_SIZE, (%d > %d)!\n", __func__, entire_len, MAXIMUM_PACKET_SIZE); 290 fprintf(stderr, "%s: entire_len is larger than MAXIMUM_PACKET_SIZE, (%d > %d)!", __func__, entire_len, MAXIMUM_PACKET_SIZE);
294 } 291 }
295 292
296 *dump_here = (char*)malloc(entire_len); 293 *dump_here = (char*)malloc(entire_len);
@@ -299,12 +296,12 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint3
299 if (*bytes_recv <= 0) { 296 if (*bytes_recv <= 0) {
300 free(*dump_here); 297 free(*dump_here);
301 *dump_here = NULL; 298 *dump_here = NULL;
302 log_debug_msg("%s: Did not get packet contents!\n", __func__); 299 debug_info("Did not get packet contents!");
303 return AFC_E_NOT_ENOUGH_DATA; 300 return AFC_E_NOT_ENOUGH_DATA;
304 } else if (*bytes_recv < this_len) { 301 } else if (*bytes_recv < this_len) {
305 free(*dump_here); 302 free(*dump_here);
306 *dump_here = NULL; 303 *dump_here = NULL;
307 log_debug_msg("%s: Could not receive this_len=%d bytes\n", __func__, this_len); 304 debug_info("Could not receive this_len=%d bytes", this_len);
308 return AFC_E_NOT_ENOUGH_DATA; 305 return AFC_E_NOT_ENOUGH_DATA;
309 } 306 }
310 } 307 }
@@ -315,13 +312,13 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint3
315 while (current_count < entire_len) { 312 while (current_count < entire_len) {
316 iphone_device_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, bytes_recv); 313 iphone_device_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, bytes_recv);
317 if (*bytes_recv <= 0) { 314 if (*bytes_recv <= 0) {
318 log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes_recv); 315 debug_info("Error receiving data (recv returned %d)", *bytes_recv);
319 break; 316 break;
320 } 317 }
321 current_count += *bytes_recv; 318 current_count += *bytes_recv;
322 } 319 }
323 if (current_count < entire_len) { 320 if (current_count < entire_len) {
324 log_debug_msg("%s: WARNING: could not receive full packet (read %s, size %d)\n", __func__, current_count, entire_len); 321 debug_info("WARNING: could not receive full packet (read %s, size %d)", current_count, entire_len);
325 } 322 }
326 } 323 }
327 324
@@ -329,14 +326,14 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint3
329 param1 = *(uint64_t*)(*dump_here); 326 param1 = *(uint64_t*)(*dump_here);
330 } 327 }
331 328
332 log_debug_msg("%s: packet data size = %i\n", __func__, current_count); 329 debug_info("packet data size = %i", current_count);
333 log_debug_msg("%s: packet data follows\n", __func__); 330 debug_info("packet data follows");
334 log_debug_buffer(*dump_here, current_count); 331 debug_buffer(*dump_here, current_count);
335 332
336 /* check operation types */ 333 /* check operation types */
337 if (header.operation == AFC_OP_STATUS) { 334 if (header.operation == AFC_OP_STATUS) {
338 /* status response */ 335 /* status response */
339 log_debug_msg("%s: got a status response, code=%lld\n", __func__, param1); 336 debug_info("got a status response, code=%lld", param1);
340 337
341 if (param1 != AFC_E_SUCCESS) { 338 if (param1 != AFC_E_SUCCESS) {
342 /* error status */ 339 /* error status */
@@ -347,21 +344,21 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint3
347 } 344 }
348 } else if (header.operation == AFC_OP_DATA) { 345 } else if (header.operation == AFC_OP_DATA) {
349 /* data response */ 346 /* data response */
350 log_debug_msg("%s: got a data response\n", __func__); 347 debug_info("got a data response");
351 } else if (header.operation == AFC_OP_FILE_OPEN_RES) { 348 } else if (header.operation == AFC_OP_FILE_OPEN_RES) {
352 /* file handle response */ 349 /* file handle response */
353 log_debug_msg("%s: got a file handle response, handle=%lld\n", __func__, param1); 350 debug_info("got a file handle response, handle=%lld", param1);
354 } else if (header.operation == AFC_OP_FILE_TELL_RES) { 351 } else if (header.operation == AFC_OP_FILE_TELL_RES) {
355 /* tell response */ 352 /* tell response */
356 log_debug_msg("%s: got a tell response, position=%lld\n", __func__, param1); 353 debug_info("got a tell response, position=%lld", param1);
357 } else { 354 } else {
358 /* unknown operation code received */ 355 /* unknown operation code received */
359 free(*dump_here); 356 free(*dump_here);
360 *dump_here = NULL; 357 *dump_here = NULL;
361 *bytes_recv = 0; 358 *bytes_recv = 0;
362 359
363 log_debug_msg("%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, header.operation, param1); 360 debug_info("WARNING: Unknown operation code received 0x%llx param1=%lld", header.operation, param1);
364 fprintf(stderr, "%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, (long long)header.operation, (long long)param1); 361 fprintf(stderr, "%s: WARNING: Unknown operation code received 0x%llx param1=%lld", __func__, (long long)header.operation, (long long)param1);
365 362
366 return AFC_E_OP_NOT_SUPPORTED; 363 return AFC_E_OP_NOT_SUPPORTED;
367 } 364 }
@@ -728,7 +725,7 @@ afc_file_open(afc_client_t client, const char *filename,
728 free(data); 725 free(data);
729 726
730 if (ret != AFC_E_SUCCESS) { 727 if (ret != AFC_E_SUCCESS) {
731 log_debug_msg("%s: Didn't receive a response to the command\n", __func__); 728 debug_info("Didn't receive a response to the command");
732 afc_unlock(client); 729 afc_unlock(client);
733 return AFC_E_NOT_ENOUGH_DATA; 730 return AFC_E_NOT_ENOUGH_DATA;
734 } 731 }
@@ -743,7 +740,7 @@ afc_file_open(afc_client_t client, const char *filename,
743 return ret; 740 return ret;
744 } 741 }
745 742
746 log_debug_msg("%s: Didn't get any further data\n", __func__); 743 debug_info("Didn't get any further data");
747 744
748 afc_unlock(client); 745 afc_unlock(client);
749 746
@@ -770,14 +767,14 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length,
770 767
771 if (!client || !client->afc_packet || !client->connection || handle == 0) 768 if (!client || !client->afc_packet || !client->connection || handle == 0)
772 return AFC_E_INVALID_ARGUMENT; 769 return AFC_E_INVALID_ARGUMENT;
773 log_debug_msg("%s: called for length %i\n", __func__, length); 770 debug_info("called for length %i", length);
774 771
775 afc_lock(client); 772 afc_lock(client);
776 773
777 // Looping here to get around the maximum amount of data that 774 // Looping here to get around the maximum amount of data that
778 // afc_receive_data can handle 775 // afc_receive_data can handle
779 while (current_count < length) { 776 while (current_count < length) {
780 log_debug_msg("%s: current count is %i but length is %i\n", __func__, current_count, length); 777 debug_info("current count is %i but length is %i", current_count, length);
781 778
782 // Send the read command 779 // Send the read command
783 AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); 780 AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket));
@@ -794,8 +791,8 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length,
794 } 791 }
795 // Receive the data 792 // Receive the data
796 ret = afc_receive_data(client, &input, &bytes_loc); 793 ret = afc_receive_data(client, &input, &bytes_loc);
797 log_debug_msg("%s: afc_receive_data returned error: %d\n", __func__, ret); 794 debug_info("afc_receive_data returned error: %d", ret);
798 log_debug_msg("%s: bytes returned: %i\n", __func__, bytes_loc); 795 debug_info("bytes returned: %i", bytes_loc);
799 if (ret != AFC_E_SUCCESS) { 796 if (ret != AFC_E_SUCCESS) {
800 afc_unlock(client); 797 afc_unlock(client);
801 return ret; 798 return ret;
@@ -808,7 +805,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length,
808 return ret; 805 return ret;
809 } else { 806 } else {
810 if (input) { 807 if (input) {
811 log_debug_msg("%s: %d\n", __func__, bytes_loc); 808 debug_info("%d", bytes_loc);
812 memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc); 809 memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc);
813 free(input); 810 free(input);
814 input = NULL; 811 input = NULL;
@@ -816,7 +813,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length,
816 } 813 }
817 } 814 }
818 } 815 }
819 log_debug_msg("%s: returning current_count as %i\n", __func__, current_count); 816 debug_info("returning current_count as %i", current_count);
820 817
821 afc_unlock(client); 818 afc_unlock(client);
822 *bytes_read = current_count; 819 *bytes_read = current_count;
@@ -849,7 +846,7 @@ afc_file_write(afc_client_t client, uint64_t handle, const char *data, uint32_t
849 846
850 afc_lock(client); 847 afc_lock(client);
851 848
852 log_debug_msg("%s: Write length: %i\n", __func__, length); 849 debug_info("Write length: %i", length);
853 850
854 // Divide the file into segments. 851 // Divide the file into segments.
855 for (i = 0; i < segments; i++) { 852 for (i = 0; i < segments; i++) {
@@ -909,7 +906,7 @@ afc_file_write(afc_client_t client, uint64_t handle, const char *data, uint32_t
909 ret = afc_receive_data(client, &acknowledgement, &bytes_loc); 906 ret = afc_receive_data(client, &acknowledgement, &bytes_loc);
910 afc_unlock(client); 907 afc_unlock(client);
911 if (ret != AFC_E_SUCCESS) { 908 if (ret != AFC_E_SUCCESS) {
912 log_debug_msg("%s: uh oh?\n", __func__); 909 debug_info("uh oh?");
913 } else { 910 } else {
914 free(acknowledgement); 911 free(acknowledgement);
915 } 912 }
@@ -933,7 +930,7 @@ afc_error_t afc_file_close(afc_client_t client, uint64_t handle)
933 930
934 afc_lock(client); 931 afc_lock(client);
935 932
936 log_debug_msg("%s: File handle %i\n", __func__, handle); 933 debug_info("File handle %i", handle);
937 934
938 // Send command 935 // Send command
939 memcpy(buffer, &handle, sizeof(uint64_t)); 936 memcpy(buffer, &handle, sizeof(uint64_t));
@@ -981,7 +978,7 @@ afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t op
981 978
982 afc_lock(client); 979 afc_lock(client);
983 980
984 log_debug_msg("%s: file handle %i\n", __func__, handle); 981 debug_info("file handle %i", handle);
985 982
986 // Send command 983 // Send command
987 memcpy(buffer, &handle, sizeof(uint64_t)); 984 memcpy(buffer, &handle, sizeof(uint64_t));
@@ -995,13 +992,13 @@ afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t op
995 992
996 if (ret != AFC_E_SUCCESS) { 993 if (ret != AFC_E_SUCCESS) {
997 afc_unlock(client); 994 afc_unlock(client);
998 log_debug_msg("%s: could not send lock command\n", __func__); 995 debug_info("could not send lock command");
999 return AFC_E_UNKNOWN_ERROR; 996 return AFC_E_UNKNOWN_ERROR;
1000 } 997 }
1001 // Receive the response 998 // Receive the response
1002 ret = afc_receive_data(client, &buffer, &bytes); 999 ret = afc_receive_data(client, &buffer, &bytes);
1003 if (buffer) { 1000 if (buffer) {
1004 log_debug_buffer(buffer, bytes); 1001 debug_buffer(buffer, bytes);
1005 free(buffer); 1002 free(buffer);
1006 } 1003 }
1007 afc_unlock(client); 1004 afc_unlock(client);
@@ -1214,9 +1211,9 @@ afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const c
1214 1211
1215 afc_lock(client); 1212 afc_lock(client);
1216 1213
1217 log_debug_msg("%s: link type: %lld\n", __func__, type); 1214 debug_info("link type: %lld", type);
1218 log_debug_msg("%s: target: %s, length:%d\n", __func__, target, strlen(target)); 1215 debug_info("target: %s, length:%d", target, strlen(target));
1219 log_debug_msg("%s: linkname: %s, length:%d\n", __func__, linkname, strlen(linkname)); 1216 debug_info("linkname: %s, length:%d", linkname, strlen(linkname));
1220 1217
1221 // Send command 1218 // Send command
1222 memcpy(send, &type, 8); 1219 memcpy(send, &type, 8);