summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libusbmuxd/libusbmuxd.c75
-rw-r--r--libusbmuxd/usbmuxd.h2
2 files changed, 42 insertions, 35 deletions
diff --git a/libusbmuxd/libusbmuxd.c b/libusbmuxd/libusbmuxd.c
index 409026a..6417543 100644
--- a/libusbmuxd/libusbmuxd.c
+++ b/libusbmuxd/libusbmuxd.c
@@ -65,6 +65,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
65// misc utility functions 65// misc utility functions
66#include "utils.h" 66#include "utils.h"
67 67
68static int libusbmuxd_debug = 0;
69#define DEBUG(x, y, ...) if (x <= libusbmuxd_debug) fprintf(stderr, (y), __VA_ARGS__);
70
68static struct collection devices; 71static struct collection devices;
69static usbmuxd_event_cb_t event_cb = NULL; 72static usbmuxd_event_cb_t event_cb = NULL;
70#ifdef WIN32 73#ifdef WIN32
@@ -134,7 +137,7 @@ static int receive_packet(int sfd, struct usbmuxd_header *header, void **payload
134 if (payload_size > 0) { 137 if (payload_size > 0) {
135 payload_loc = (char*)malloc(payload_size); 138 payload_loc = (char*)malloc(payload_size);
136 if (recv_buf_timeout(sfd, payload_loc, payload_size, 0, 5000) != payload_size) { 139 if (recv_buf_timeout(sfd, payload_loc, payload_size, 0, 5000) != payload_size) {
137 fprintf(stderr, "%s: Error receiving payload of size %d\n", __func__, payload_size); 140 DEBUG(1, "%s: Error receiving payload of size %d\n", __func__, payload_size);
138 free(payload_loc); 141 free(payload_loc);
139 return -EBADMSG; 142 return -EBADMSG;
140 } 143 }
@@ -148,13 +151,13 @@ static int receive_packet(int sfd, struct usbmuxd_header *header, void **payload
148 free(payload_loc); 151 free(payload_loc);
149 152
150 if (!plist) { 153 if (!plist) {
151 fprintf(stderr, "%s: Error getting plist from payload!\n", __func__); 154 DEBUG(1, "%s: Error getting plist from payload!\n", __func__);
152 return -EBADMSG; 155 return -EBADMSG;
153 } 156 }
154 157
155 plist_t node = plist_dict_get_item(plist, "MessageType"); 158 plist_t node = plist_dict_get_item(plist, "MessageType");
156 if (plist_get_node_type(node) != PLIST_STRING) { 159 if (plist_get_node_type(node) != PLIST_STRING) {
157 fprintf(stderr, "%s: Error getting message type from plist!\n", __func__); 160 DEBUG(1, "%s: Error getting message type from plist!\n", __func__);
158 free(plist); 161 free(plist);
159 return -EBADMSG; 162 return -EBADMSG;
160 } 163 }
@@ -177,7 +180,7 @@ static int receive_packet(int sfd, struct usbmuxd_header *header, void **payload
177 struct usbmuxd_device_record *dev = NULL; 180 struct usbmuxd_device_record *dev = NULL;
178 plist_t props = plist_dict_get_item(plist, "Properties"); 181 plist_t props = plist_dict_get_item(plist, "Properties");
179 if (!props) { 182 if (!props) {
180 fprintf(stderr, "%s: Could not get properties for message '%s' from plist!\n", __func__, message); 183 DEBUG(1, "%s: Could not get properties for message '%s' from plist!\n", __func__, message);
181 plist_free(plist); 184 plist_free(plist);
182 return -EBADMSG; 185 return -EBADMSG;
183 } 186 }
@@ -218,7 +221,7 @@ static int receive_packet(int sfd, struct usbmuxd_header *header, void **payload
218 hdr.message = MESSAGE_DEVICE_REMOVE; 221 hdr.message = MESSAGE_DEVICE_REMOVE;
219 } 222 }
220 } else { 223 } else {
221 fprintf(stderr, "%s: Unexpected message '%s' in plist!\n", __func__, message); 224 DEBUG(1, "%s: Unexpected message '%s' in plist!\n", __func__, message);
222 plist_free(plist); 225 plist_free(plist);
223 return -EBADMSG; 226 return -EBADMSG;
224 } 227 }
@@ -250,13 +253,13 @@ static int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t * result)
250 *result = -1; 253 *result = -1;
251 254
252 if ((recv_len = receive_packet(sfd, &hdr, (void**)&res, 5000)) < 0) { 255 if ((recv_len = receive_packet(sfd, &hdr, (void**)&res, 5000)) < 0) {
253 fprintf(stderr, "%s: Error receiving packet: %d\n", __func__, errno); 256 DEBUG(1, "%s: Error receiving packet: %d\n", __func__, errno);
254 if (res) 257 if (res)
255 free(res); 258 free(res);
256 return -errno; 259 return -errno;
257 } 260 }
258 if (recv_len < sizeof(hdr)) { 261 if (recv_len < sizeof(hdr)) {
259 fprintf(stderr, "%s: Received packet is too small!\n", __func__); 262 DEBUG(1, "%s: Received packet is too small!\n", __func__);
260 if (res) 263 if (res)
261 free(res); 264 free(res);
262 return -EPROTO; 265 return -EPROTO;
@@ -272,7 +275,7 @@ static int usbmuxd_get_result(int sfd, uint32_t tag, uint32_t * result)
272 free(res); 275 free(res);
273 return ret; 276 return ret;
274 } 277 }
275 fprintf(stderr, "%s: Unexpected message of type %d received!\n", __func__, hdr.message); 278 DEBUG(1, "%s: Unexpected message of type %d received!\n", __func__, hdr.message);
276 if (res) 279 if (res)
277 free(res); 280 free(res);
278 return -EPROTO; 281 return -EPROTO;
@@ -291,14 +294,14 @@ static int send_packet(int sfd, uint32_t message, uint32_t tag, void *payload, u
291 } 294 }
292 int sent = send_buf(sfd, &header, sizeof(header)); 295 int sent = send_buf(sfd, &header, sizeof(header));
293 if (sent != sizeof(header)) { 296 if (sent != sizeof(header)) {
294 fprintf(stderr, "%s: ERROR: could not send packet header\n", __func__); 297 DEBUG(1, "%s: ERROR: could not send packet header\n", __func__);
295 return -1; 298 return -1;
296 } 299 }
297 if (payload && (payload_size > 0)) { 300 if (payload && (payload_size > 0)) {
298 sent += send_buf(sfd, payload, payload_size); 301 sent += send_buf(sfd, payload, payload_size);
299 } 302 }
300 if (sent != (int)header.length) { 303 if (sent != (int)header.length) {
301 fprintf(stderr, "%s: ERROR: could not send whole packet\n", __func__); 304 DEBUG(1, "%s: ERROR: could not send whole packet\n", __func__);
302 close_socket(sfd); 305 close_socket(sfd);
303 return -1; 306 return -1;
304 } 307 }
@@ -427,14 +430,14 @@ static int usbmuxd_listen_inotify()
427 sfd = -1; 430 sfd = -1;
428 inot_fd = inotify_init (); 431 inot_fd = inotify_init ();
429 if (inot_fd < 0) { 432 if (inot_fd < 0) {
430 fprintf (stderr, "Failed to setup inotify\n"); 433 DEBUG(1, "%s: Failed to setup inotify\n", __func__);
431 return -2; 434 return -2;
432 } 435 }
433 436
434 /* inotify is setup, listen for events that concern us */ 437 /* inotify is setup, listen for events that concern us */
435 watch_d = inotify_add_watch (inot_fd, USBMUXD_DIRNAME, IN_CREATE); 438 watch_d = inotify_add_watch (inot_fd, USBMUXD_DIRNAME, IN_CREATE);
436 if (watch_d < 0) { 439 if (watch_d < 0) {
437 fprintf (stderr, "Failed to setup watch descriptor for socket dir\n"); 440 DEBUG(1, "%s: Failed to setup watch descriptor for socket dir\n", __func__);
438 close (inot_fd); 441 close (inot_fd);
439 return -2; 442 return -2;
440 } 443 }
@@ -491,7 +494,7 @@ retry:
491#endif 494#endif
492 495
493 if (sfd < 0) { 496 if (sfd < 0) {
494 fprintf(stderr, "%s: ERROR: usbmuxd was supposed to be running here...\n", __func__); 497 DEBUG(1, "%s: ERROR: usbmuxd was supposed to be running here...\n", __func__);
495 return sfd; 498 return sfd;
496 } 499 }
497 500
@@ -499,7 +502,7 @@ retry:
499 LOCK; 502 LOCK;
500 if (send_listen_packet(sfd, use_tag) <= 0) { 503 if (send_listen_packet(sfd, use_tag) <= 0) {
501 UNLOCK; 504 UNLOCK;
502 fprintf(stderr, "%s: ERROR: could not send listen packet\n", __func__); 505 DEBUG(1, "%s: ERROR: could not send listen packet\n", __func__);
503 close_socket(sfd); 506 close_socket(sfd);
504 return -1; 507 return -1;
505 } 508 }
@@ -512,7 +515,7 @@ retry:
512 goto retry; 515 goto retry;
513 } 516 }
514#endif 517#endif
515 fprintf(stderr, "%s: ERROR: did not get OK but %d\n", __func__, res); 518 DEBUG(1, "%s: ERROR: did not get OK but %d\n", __func__, res);
516 return -1; 519 return -1;
517 } 520 }
518 UNLOCK; 521 UNLOCK;
@@ -543,7 +546,7 @@ int get_next_event(int sfd, usbmuxd_event_cb_t callback, void *user_data)
543 } 546 }
544 547
545 if ((hdr.length > sizeof(hdr)) && !payload) { 548 if ((hdr.length > sizeof(hdr)) && !payload) {
546 fprintf(stderr, "%s: Invalid packet received, payload is missing!\n", __func__); 549 DEBUG(1, "%s: Invalid packet received, payload is missing!\n", __func__);
547 return -EBADMSG; 550 return -EBADMSG;
548 } 551 }
549 552
@@ -551,7 +554,7 @@ int get_next_event(int sfd, usbmuxd_event_cb_t callback, void *user_data)
551 struct usbmuxd_device_record *dev = payload; 554 struct usbmuxd_device_record *dev = payload;
552 usbmuxd_device_info_t *devinfo = (usbmuxd_device_info_t*)malloc(sizeof(usbmuxd_device_info_t)); 555 usbmuxd_device_info_t *devinfo = (usbmuxd_device_info_t*)malloc(sizeof(usbmuxd_device_info_t));
553 if (!devinfo) { 556 if (!devinfo) {
554 fprintf(stderr, "%s: Out of memory!\n", __func__); 557 DEBUG(1, "%s: Out of memory!\n", __func__);
555 free(payload); 558 free(payload);
556 return -1; 559 return -1;
557 } 560 }
@@ -575,14 +578,14 @@ int get_next_event(int sfd, usbmuxd_event_cb_t callback, void *user_data)
575 578
576 devinfo = devices_find(handle); 579 devinfo = devices_find(handle);
577 if (!devinfo) { 580 if (!devinfo) {
578 fprintf(stderr, "%s: WARNING: got device remove message for handle %d, but couldn't find the corresponding handle in the device list. This event will be ignored.\n", __func__, handle); 581 DEBUG(1, "%s: WARNING: got device remove message for handle %d, but couldn't find the corresponding handle in the device list. This event will be ignored.\n", __func__, handle);
579 } else { 582 } else {
580 generate_event(callback, devinfo, UE_DEVICE_REMOVE, user_data); 583 generate_event(callback, devinfo, UE_DEVICE_REMOVE, user_data);
581 collection_remove(&devices, devinfo); 584 collection_remove(&devices, devinfo);
582 free(devinfo); 585 free(devinfo);
583 } 586 }
584 } else if (hdr.length > 0) { 587 } else if (hdr.length > 0) {
585 fprintf(stderr, "%s: Unexpected message type %d length %d received!\n", __func__, hdr.message, hdr.length); 588 DEBUG(1, "%s: Unexpected message type %d length %d received!\n", __func__, hdr.message, hdr.length);
586 } 589 }
587 if (payload) { 590 if (payload) {
588 free(payload); 591 free(payload);
@@ -652,7 +655,7 @@ int usbmuxd_subscribe(usbmuxd_event_cb_t callback, void *user_data)
652 res = pthread_create(&devmon, NULL, device_monitor, user_data); 655 res = pthread_create(&devmon, NULL, device_monitor, user_data);
653#endif 656#endif
654 if (res != 0) { 657 if (res != 0) {
655 fprintf(stderr, "%s: ERROR: Could not start device watcher thread!\n", __func__); 658 DEBUG(1, "%s: ERROR: Could not start device watcher thread!\n", __func__);
656 return res; 659 return res;
657 } 660 }
658 return 0; 661 return 0;
@@ -697,7 +700,7 @@ retry:
697#endif 700#endif
698 sfd = connect_usbmuxd_socket(); 701 sfd = connect_usbmuxd_socket();
699 if (sfd < 0) { 702 if (sfd < 0) {
700 fprintf(stderr, "%s: error opening socket!\n", __func__); 703 DEBUG(1, "%s: error opening socket!\n", __func__);
701 return sfd; 704 return sfd;
702 } 705 }
703 706
@@ -717,16 +720,14 @@ retry:
717 goto retry; 720 goto retry;
718 } 721 }
719#endif 722#endif
720 fprintf(stderr, 723 DEBUG(1, "%s: Did not get response to scan request (with result=0)...\n", __func__);
721 "%s: Did not get response to scan request (with result=0)...\n",
722 __func__);
723 return res; 724 return res;
724 } 725 }
725 } 726 }
726 727
727 if (!listen_success) { 728 if (!listen_success) {
728 UNLOCK; 729 UNLOCK;
729 fprintf(stderr, "%s: Could not send listen request!\n", __func__); 730 DEBUG(1, "%s: Could not send listen request!\n", __func__);
730 return -1; 731 return -1;
731 } 732 }
732 733
@@ -740,7 +741,7 @@ retry:
740 usbmuxd_device_info_t *devinfo = (usbmuxd_device_info_t*)malloc(sizeof(usbmuxd_device_info_t)); 741 usbmuxd_device_info_t *devinfo = (usbmuxd_device_info_t*)malloc(sizeof(usbmuxd_device_info_t));
741 if (!devinfo) { 742 if (!devinfo) {
742 UNLOCK; 743 UNLOCK;
743 fprintf(stderr, "%s: Out of memory!\n", __func__); 744 DEBUG(1, "%s: Out of memory!\n", __func__);
744 free(payload); 745 free(payload);
745 return -1; 746 return -1;
746 } 747 }
@@ -773,7 +774,7 @@ retry:
773 free(devinfo); 774 free(devinfo);
774 } 775 }
775 } else { 776 } else {
776 fprintf(stderr, "%s: Unexpected message %d\n", __func__, hdr.message); 777 DEBUG(1, "%s: Unexpected message %d\n", __func__, hdr.message);
777 } 778 }
778 if (payload) 779 if (payload)
779 free(payload); 780 free(payload);
@@ -860,20 +861,20 @@ retry:
860#endif 861#endif
861 sfd = connect_usbmuxd_socket(); 862 sfd = connect_usbmuxd_socket();
862 if (sfd < 0) { 863 if (sfd < 0) {
863 fprintf(stderr, "%s: Error: Connection to usbmuxd failed: %s\n", 864 DEBUG(1, "%s: Error: Connection to usbmuxd failed: %s\n",
864 __func__, strerror(errno)); 865 __func__, strerror(errno));
865 return sfd; 866 return sfd;
866 } 867 }
867 868
868 use_tag++; 869 use_tag++;
869 if (send_connect_packet(sfd, use_tag, (uint32_t)handle, (uint16_t)port) <= 0) { 870 if (send_connect_packet(sfd, use_tag, (uint32_t)handle, (uint16_t)port) <= 0) {
870 fprintf(stderr, "%s: Error sending connect message!\n", __func__); 871 DEBUG(1, "%s: Error sending connect message!\n", __func__);
871 } else { 872 } else {
872 // read ACK 873 // read ACK
873 //fprintf(stderr, "%s: Reading connect result...\n", __func__); 874 DEBUG(2, "%s: Reading connect result...\n", __func__);
874 if (usbmuxd_get_result(sfd, use_tag, &res)) { 875 if (usbmuxd_get_result(sfd, use_tag, &res)) {
875 if (res == 0) { 876 if (res == 0) {
876 //fprintf(stderr, "%s: Connect success!\n", __func__); 877 DEBUG(2, "%s: Connect success!\n", __func__);
877 connected = 1; 878 connected = 1;
878 } else { 879 } else {
879#ifdef HAVE_PLIST 880#ifdef HAVE_PLIST
@@ -883,8 +884,7 @@ retry:
883 goto retry; 884 goto retry;
884 } 885 }
885#endif 886#endif
886 fprintf(stderr, "%s: Connect failed, Error code=%d\n", 887 DEBUG(1, "%s: Connect failed, Error code=%d\n", __func__, res);
887 __func__, res);
888 } 888 }
889 } 889 }
890 } 890 }
@@ -914,10 +914,10 @@ int usbmuxd_send(int sfd, const char *data, uint32_t len, uint32_t *sent_bytes)
914 num_sent = send(sfd, (void*)data, len, 0); 914 num_sent = send(sfd, (void*)data, len, 0);
915 if (num_sent < 0) { 915 if (num_sent < 0) {
916 *sent_bytes = 0; 916 *sent_bytes = 0;
917 fprintf(stderr, "%s: Error %d when sending: %s\n", __func__, num_sent, strerror(errno)); 917 DEBUG(1, "%s: Error %d when sending: %s\n", __func__, num_sent, strerror(errno));
918 return num_sent; 918 return num_sent;
919 } else if ((uint32_t)num_sent < len) { 919 } else if ((uint32_t)num_sent < len) {
920 fprintf(stderr, "%s: Warning: Did not send enough (only %d of %d)\n", __func__, num_sent, len); 920 DEBUG(1, "%s: Warning: Did not send enough (only %d of %d)\n", __func__, num_sent, len);
921 } 921 }
922 922
923 *sent_bytes = num_sent; 923 *sent_bytes = num_sent;
@@ -943,3 +943,8 @@ int usbmuxd_recv(int sfd, char *data, uint32_t len, uint32_t *recv_bytes)
943 return usbmuxd_recv_timeout(sfd, data, len, recv_bytes, 5000); 943 return usbmuxd_recv_timeout(sfd, data, len, recv_bytes, 5000);
944} 944}
945 945
946void libusbmuxd_set_debug_level(int level)
947{
948 libusbmuxd_debug = level;
949 sock_stuff_set_verbose(level);
950}
diff --git a/libusbmuxd/usbmuxd.h b/libusbmuxd/usbmuxd.h
index b8283d6..d0dccef 100644
--- a/libusbmuxd/usbmuxd.h
+++ b/libusbmuxd/usbmuxd.h
@@ -174,6 +174,8 @@ int usbmuxd_recv_timeout(int sfd, char *data, uint32_t len, uint32_t *recv_bytes
174 */ 174 */
175int usbmuxd_recv(int sfd, char *data, uint32_t len, uint32_t *recv_bytes); 175int usbmuxd_recv(int sfd, char *data, uint32_t len, uint32_t *recv_bytes);
176 176
177void libusbmuxd_set_debug_level(int level);
178
177#ifdef __cplusplus 179#ifdef __cplusplus
178} 180}
179#endif 181#endif