summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorGravatar Hector Martin2009-04-28 02:02:55 +0200
committerGravatar Hector Martin2009-04-28 02:02:55 +0200
commitcc9e6a2318352a8fd3a35c25fcb294331ff54288 (patch)
tree75b891a06a7eddf9674327ae387784b0c64967b0 /main.c
parentd982007a7350df35c5aeba820a520779694514a7 (diff)
downloadusbmuxd-cc9e6a2318352a8fd3a35c25fcb294331ff54288.tar.gz
usbmuxd-cc9e6a2318352a8fd3a35c25fcb294331ff54288.tar.bz2
USB mostly complete, main loop added, polls for devices
Diffstat (limited to 'main.c')
-rw-r--r--main.c66
1 files changed, 57 insertions, 9 deletions
diff --git a/main.c b/main.c
index c2467a2..6773d0e 100644
--- a/main.c
+++ b/main.c
@@ -42,13 +42,13 @@ int create_socket(void) {
42 int listenfd; 42 int listenfd;
43 43
44 if(unlink(socket_path) == -1 && errno != ENOENT) { 44 if(unlink(socket_path) == -1 && errno != ENOENT) {
45 usbmuxd_log(LOG_FATAL, "unlink(%s) failed: %s", socket_path, strerror(errno)); 45 usbmuxd_log(LL_FATAL, "unlink(%s) failed: %s", socket_path, strerror(errno));
46 return -1; 46 return -1;
47 } 47 }
48 48
49 listenfd = socket(AF_UNIX, SOCK_STREAM, 0); 49 listenfd = socket(AF_UNIX, SOCK_STREAM, 0);
50 if (listenfd == -1) { 50 if (listenfd == -1) {
51 usbmuxd_log(LOG_FATAL, "socket() failed: %s", strerror(errno)); 51 usbmuxd_log(LL_FATAL, "socket() failed: %s", strerror(errno));
52 return -1; 52 return -1;
53 } 53 }
54 54
@@ -56,37 +56,85 @@ int create_socket(void) {
56 bind_addr.sun_family = AF_UNIX; 56 bind_addr.sun_family = AF_UNIX;
57 strcpy(bind_addr.sun_path, socket_path); 57 strcpy(bind_addr.sun_path, socket_path);
58 if (bind(listenfd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) != 0) { 58 if (bind(listenfd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) != 0) {
59 usbmuxd_log(LOG_FATAL, "bind() failed: %s", strerror(errno)); 59 usbmuxd_log(LL_FATAL, "bind() failed: %s", strerror(errno));
60 return -1; 60 return -1;
61 } 61 }
62 62
63 // Start listening 63 // Start listening
64 if (listen(listenfd, 5) != 0) { 64 if (listen(listenfd, 5) != 0) {
65 usbmuxd_log(LOG_FATAL, "listen() failed: %s", strerror(errno)); 65 usbmuxd_log(LL_FATAL, "listen() failed: %s", strerror(errno));
66 return -1; 66 return -1;
67 } 67 }
68 68
69 return listenfd; 69 return listenfd;
70} 70}
71 71
72int main_loop(int listenfd)
73{
74 int to, cnt, i;
75 struct fdlist pollfds;
76
77 while(1) {
78 usbmuxd_log(LL_SPEW, "main_loop iteration");
79 to = usb_get_timeout();
80 usbmuxd_log(LL_SPEW, "USB timeout is %d ms", to);
81
82 fdlist_create(&pollfds);
83 usb_get_fds(&pollfds);
84 usbmuxd_log(LL_SPEW, "fd count is %d", pollfds.count);
85
86 cnt = poll(pollfds.fds, pollfds.count, to);
87 usbmuxd_log(LL_SPEW, "poll() returned %d", cnt);
88
89 if(cnt == 0) {
90 if(usb_process() < 0) {
91 usbmuxd_log(LL_FATAL, "usb_process() failed");
92 return -1;
93 }
94 } else {
95 for(i=0; i<pollfds.count; i++) {
96 if(pollfds.fds[i].revents) {
97 if(pollfds.owners[i] == FD_USB) {
98 if(usb_process() < 0) {
99 usbmuxd_log(LL_FATAL, "usb_process() failed");
100 return -1;
101 }
102 }
103 }
104 }
105 }
106 fdlist_free(&pollfds);
107 }
108}
109
72int main(int argc, char *argv[]) 110int main(int argc, char *argv[])
73{ 111{
74 int listenfd; 112 int listenfd;
75 int res; 113 int res;
76 114
77 usbmuxd_log(LOG_NOTICE, "usbmux v0.1 starting up"); 115 usbmuxd_log(LL_NOTICE, "usbmux v0.1 starting up");
78 116
79 usbmuxd_log(LOG_INFO, "Creating socket"); 117 usbmuxd_log(LL_INFO, "Creating socket");
80 listenfd = create_socket(); 118 listenfd = create_socket();
81 if(listenfd < 0) 119 if(listenfd < 0)
82 return 1; 120 return 1;
83 121
84 usbmuxd_log(LOG_INFO, "Initializing USB"); 122 usbmuxd_log(LL_INFO, "Initializing USB");
85 if((res = usb_init()) < 0) 123 if((res = usb_init()) < 0)
86 return 2; 124 return 2;
87 usbmuxd_log(LOG_INFO, "%d device%s detected", res, (res==1)?"":"s"); 125 usbmuxd_log(LL_INFO, "%d device%s detected", res, (res==1)?"":"s");
88 126
89 usbmuxd_log(LOG_NOTICE, "initialization complete"); 127 usbmuxd_log(LL_NOTICE, "Initialization complete");
128
129 res = main_loop(listenfd);
130 if(res < 0)
131 usbmuxd_log(LL_FATAL, "main_loop failed");
132
133 usbmuxd_log(LL_NOTICE, "usbmux shutting down");
134 usb_shutdown();
135 usbmuxd_log(LL_NOTICE, "Shutdown complete");
90 136
137 if(res < 0)
138 return -res;
91 return 0; 139 return 0;
92} 140}