make use of the mainloop code

Use the mainloop we added in the previous patch.
Some reorg is needed with the functions, they are moved up and down
to prevent forward declarations.

Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
diff --git a/mainloop.c b/mainloop.c
index d63d703..02dda98 100644
--- a/mainloop.c
+++ b/mainloop.c
@@ -32,7 +32,7 @@
 
 #define MAX_EVENTS 10
 
-int _mainloop(unsigned int timeout)
+int mainloop(unsigned int timeout)
 {
         int i, nfds;
         struct epoll_event events[MAX_EVENTS];