From bca8b6e5c317e846034f76d66489e2fcbe24589c Mon Sep 17 00:00:00 2001 From: Yufeng Wang <44623591+yufengwangca@users.noreply.github.com> Date: Wed, 15 Jul 2020 07:01:51 -0700 Subject: [PATCH] [linux] Add GDBus support in PlatformMgr to drive DBUS communication. (#1557) * [linux] Add GDBus support in PlatformMgr to drive DBUS communication. * Add GDBus support in PlatformMgr to drive DBUS: Thread, BLE, WiFi. * Add dedicated I/O thread for DBUS. * Define build flag CHIP_WITH_GIO based on if gio-2.0 lib is available --- configure.ac | 20 ++++++++++++++++ src/platform/Linux/PlatformManagerImpl.cpp | 28 ++++++++++++++++++++++ src/platform/Linux/PlatformManagerImpl.h | 19 +++++++++++++-- src/platform/Makefile.am | 1 + src/platform/tests/Makefile.am | 17 +++++++++---- 5 files changed, 79 insertions(+), 6 deletions(-) diff --git a/configure.ac b/configure.ac index 0ef74005fcbb35..7de7422dba5c07 100644 --- a/configure.ac +++ b/configure.ac @@ -1998,6 +1998,25 @@ if test "${CHIP_DEVICE_LAYER_TARGET_LINUX}" = 1; then ) fi +# +# +# GIO +# + +if test "${CHIP_DEVICE_LAYER_TARGET_LINUX}" = 1; then + PKG_CHECK_MODULES([GIO], [gio-2.0]) + + # Check for GIO library is available. + AC_CHECK_LIB([gio-2.0], [g_bus_get_sync], + [ + AC_DEFINE([CHIP_WITH_GIO], [1], [Define to 1 to build CHIP with GIO]) + ], + [ + AC_DEFINE([CHIP_WITH_GIO], [0], [Define to 0 to build CHIP without GIO]) + ] + ) +fi + # # Sockets # @@ -2426,6 +2445,7 @@ AC_MSG_NOTICE([ PThreads compile flags : ${PTHREAD_CFLAGS:--} PThreads link libraries : ${PTHREAD_LIBS:--} IniPP compile flags : ${INIPP_CPPFLAGS:--} + GIO compile flags : ${GIO_CFLAGS:--} C Preprocessor : ${CPP} C Compiler : ${CC} C++ Preprocessor : ${CXXCPP} diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index d4f6a1d892f2dd..6a8be2e5438a92 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -27,15 +27,36 @@ #include #include +#include + namespace chip { namespace DeviceLayer { PlatformManagerImpl PlatformManagerImpl::sInstance; +#if CHIP_WITH_GIO +static void GDBus_Thread() +{ + GMainLoop * loop = g_main_loop_new(nullptr, false); + + g_main_loop_run(loop); + g_main_loop_unref(loop); +} +#endif + CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) { CHIP_ERROR err; +#if CHIP_WITH_GIO + GError * error = NULL; + + this->mpGDBusConnection = UniqueGDBusConnection(g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error)); + + std::thread gdbusThread(GDBus_Thread); + gdbusThread.detach(); +#endif + // Initialize the configuration system. err = Internal::PosixConfig::Init(); SuccessOrExit(err); @@ -49,5 +70,12 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) return err; } +#if CHIP_WITH_GIO +GDBusConnection * PlatformManagerImpl::GetGDBusConnection() +{ + return this->mpGDBusConnection.get(); +} +#endif + } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Linux/PlatformManagerImpl.h b/src/platform/Linux/PlatformManagerImpl.h index 355f51f9a20bbf..39cc3840e8664a 100644 --- a/src/platform/Linux/PlatformManagerImpl.h +++ b/src/platform/Linux/PlatformManagerImpl.h @@ -24,8 +24,13 @@ #ifndef PLATFORM_MANAGER_IMPL_H #define PLATFORM_MANAGER_IMPL_H +#include #include +#if CHIP_WITH_GIO +#include +#endif + namespace chip { namespace DeviceLayer { @@ -44,8 +49,9 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener public: // ===== Platform-specific members that may be accessed directly by the application. - - /* none so far */ +#if CHIP_WITH_GIO + GDBusConnection * GetGDBusConnection(); +#endif private: // ===== Methods that implement the PlatformManager abstract interface. @@ -59,6 +65,15 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener friend class Internal::BLEManagerImpl; static PlatformManagerImpl sInstance; + +#if CHIP_WITH_GIO + struct GDBusConnectionDeleter + { + void operator()(GDBusConnection * conn) { g_object_unref(conn); } + }; + using UniqueGDBusConnection = std::unique_ptr; + UniqueGDBusConnection mpGDBusConnection; +#endif }; /** diff --git a/src/platform/Makefile.am b/src/platform/Makefile.am index dbc8b725b9f76f..2acb4e2716c317 100644 --- a/src/platform/Makefile.am +++ b/src/platform/Makefile.am @@ -154,6 +154,7 @@ if CHIP_DEVICE_LAYER_TARGET_LINUX SUBDIRS += tests libDeviceLayer_a_CPPFLAGS += \ + $(GIO_CFLAGS) \ $(INIPP_CPPFLAGS) \ $(NULL) diff --git a/src/platform/tests/Makefile.am b/src/platform/tests/Makefile.am index 02cae2222929cc..0f54d8d2bc56d1 100644 --- a/src/platform/tests/Makefile.am +++ b/src/platform/tests/Makefile.am @@ -69,14 +69,19 @@ AM_CPPFLAGS = \ $(NULL) if CHIP_DEVICE_LAYER_TARGET_LINUX +AM_CPPFLAGS += \ + $(GIO_CFLAGS) \ + $(NULL) + if CHIP_WITH_OT_BR_POSIX AM_CPPFLAGS += \ $(DBUS_CFLAGS) \ $(OT_BR_POSIX_CPPFLAGS) \ $(NULL) libPlatformTests_a_SOURCES += TestThreadStackMgr.cpp -endif -endif +endif # CHIP_WITH_OT_BR_POSIX +endif # CHIP_DEVICE_LAYER_TARGET_LINUX + CHIP_LDADD = \ $(top_builddir)/src/platform/libDeviceLayer.a \ @@ -86,13 +91,17 @@ CHIP_LDADD = \ $(NULL) if CHIP_DEVICE_LAYER_TARGET_LINUX +CHIP_LDADD += \ + $(GIO_CFLAGS) $(GIO_LIBS) \ + $(NULL) + if CHIP_WITH_OT_BR_POSIX CHIP_LDADD += \ $(DBUS_LIBS) \ $(OT_BR_POSIX_LDFLAGS) $(OT_BR_POSIX_LIBS) \ $(NULL) -endif -endif +endif # CHIP_WITH_OT_BR_POSIX +endif # CHIP_DEVICE_LAYER_TARGET_LINUX COMMON_LDADD = \ libPlatformTests.a \