shared/mainloop: Add ell-based mainloop implementation 43/220743/1
authorInga Stotland <inga.stotland@intel.com>
Sun, 11 Aug 2019 07:00:45 +0000 (00:00 -0700)
committerAnupam Roy <anupam.r@samsung.com>
Fri, 20 Dec 2019 17:01:34 +0000 (22:31 +0530)
This adds implementation of wrapper mainloop functions to interact with
ell main APIs, enabling support for the applications that link with ell
library and use mainloop functionality.

Change-Id: I9a52c5589097d0acb7a488af88600e9a99d38070
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
Makefile.am
src/shared/mainloop-ell.c [new file with mode: 0644]
src/shared/timeout-ell.c [new file with mode: 0644]

index 74e1b43..7bc2774 100755 (executable)
@@ -179,6 +179,10 @@ CLEANFILES += $(ell_built_sources)
 
 noinst_LTLIBRARIES += src/libshared-glib.la src/libshared-mainloop.la
 
+if LIBSHARED_ELL
+noinst_LTLIBRARIES += src/libshared-ell.la
+endif
+
 shared_sources = src/shared/io.h src/shared/timeout.h \
                        src/shared/queue.h src/shared/queue.c \
                        src/shared/util.h src/shared/util.c \
@@ -208,10 +212,11 @@ shared_sources += src/shared/shell.c src/shared/shell.h
 endif
 
 if LIBSHARED_ELL
-noinst_LTLIBRARIES += src/libshared-ell.la
-
 src_libshared_ell_la_SOURCES = $(shared_sources) \
-                                src/shared/io-ell.c
+                               src/shared/io-ell.c \
+                               src/shared/timeout-ell.c \
+                               src/shared/mainloop.h \
+                               src/shared/mainloop-ell.c
 endif
 
 src_libshared_glib_la_SOURCES = $(shared_sources) \
diff --git a/src/shared/mainloop-ell.c b/src/shared/mainloop-ell.c
new file mode 100644 (file)
index 0000000..ec88507
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2019  Intel Corporation
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <ell/ell.h>
+
+#include "mainloop.h"
+
+static bool is_initialized;
+static int exit_status;
+static mainloop_signal_func sig_func;
+
+static void l_sig_func(uint32_t signo, void *user_data)
+{
+       if (sig_func)
+               sig_func(signo, user_data);
+}
+
+void mainloop_init(void)
+{
+       is_initialized = l_main_init();
+}
+
+void mainloop_quit(void)
+{
+       l_main_quit();
+}
+
+void mainloop_exit_success(void)
+{
+       exit_status = EXIT_SUCCESS;
+       l_main_quit();
+}
+
+void mainloop_exit_failure(void)
+{
+       exit_status = EXIT_FAILURE;
+       l_main_quit();
+}
+
+int mainloop_run(void)
+{
+       if (!is_initialized)
+               return -EINVAL;
+
+       l_main_run();
+
+       is_initialized = false;
+       sig_func = NULL;
+
+       return exit_status;
+}
+
+int mainloop_run_with_signal(mainloop_signal_func func, void *user_data)
+{
+       if (!is_initialized || !func)
+               return -EINVAL;
+
+       /* Workaround for sign discrepancy in ell and bluez */
+       sig_func = func;
+
+       return l_main_run_with_signal(l_sig_func, user_data);
+}
+
+int mainloop_add_fd(int fd, uint32_t events, mainloop_event_func callback,
+                               void *user_data, mainloop_destroy_func destroy)
+{
+       return -ENOSYS;
+}
+
+int mainloop_modify_fd(int fd, uint32_t events)
+{
+       return -ENOSYS;
+}
+
+int mainloop_remove_fd(int fd)
+{
+       return -ENOSYS;
+}
+
+int mainloop_add_timeout(unsigned int msec, mainloop_timeout_func callback,
+                               void *user_data, mainloop_destroy_func destroy)
+{
+       return -ENOSYS;
+}
+
+int mainloop_modify_timeout(int fd, unsigned int msec)
+{
+       return -ENOSYS;
+}
+
+int mainloop_remove_timeout(int id)
+{
+       return -ENOSYS;
+}
+
+int mainloop_set_signal(sigset_t *mask, mainloop_signal_func callback,
+                               void *user_data, mainloop_destroy_func destroy)
+{
+       return -ENOSYS;
+}
diff --git a/src/shared/timeout-ell.c b/src/shared/timeout-ell.c
new file mode 100644 (file)
index 0000000..8419d46
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2019  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ */
+
+#include <ell/ell.h>
+
+#include "timeout.h"
+
+struct timeout_data {
+       timeout_func_t func;
+       timeout_destroy_func_t destroy;
+       unsigned int timeout;
+       void *user_data;
+};
+
+static void timeout_callback(struct l_timeout *timeout, void *user_data)
+{
+       struct timeout_data *data = user_data;
+
+       if (data->func)
+               data->func(data->user_data);
+
+       l_timeout_modify(timeout, data->timeout);
+}
+
+static void timeout_destroy(void *user_data)
+{
+       struct timeout_data *data = user_data;
+
+       if (data->destroy)
+               data->destroy(data->user_data);
+
+       l_free(data);
+}
+
+unsigned int timeout_add(unsigned int timeout, timeout_func_t func,
+                       void *user_data, timeout_destroy_func_t destroy)
+{
+       struct timeout_data *data;
+       uint32_t id;
+
+       data = l_new(struct timeout_data, 1);
+
+       data->func = func;
+       data->destroy = destroy;
+       data->user_data = user_data;
+       data->timeout = timeout;
+
+       id = L_PTR_TO_UINT(l_timeout_create(timeout, timeout_callback,
+                                               user_data, timeout_destroy));
+       return id;
+}
+
+void timeout_remove(unsigned int id)
+{
+       l_timeout_remove(L_UINT_TO_PTR(id));
+}