Skip to content

Commit

Permalink
[linux] Add GDBus support in PlatformMgr to drive DBUS communication. (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
yufengwangca authored Jul 15, 2020
1 parent dc05393 commit bca8b6e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
20 changes: 20 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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}
Expand Down
28 changes: 28 additions & 0 deletions src/platform/Linux/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,36 @@
#include <platform/PlatformManager.h>
#include <platform/internal/GenericPlatformManagerImpl_POSIX.ipp>

#include <thread>

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);
Expand All @@ -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
19 changes: 17 additions & 2 deletions src/platform/Linux/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
#ifndef PLATFORM_MANAGER_IMPL_H
#define PLATFORM_MANAGER_IMPL_H

#include <memory>
#include <platform/internal/GenericPlatformManagerImpl_POSIX.h>

#if CHIP_WITH_GIO
#include <gio/gio.h>
#endif

namespace chip {
namespace DeviceLayer {

Expand All @@ -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.
Expand All @@ -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<GDBusConnection, GDBusConnectionDeleter>;
UniqueGDBusConnection mpGDBusConnection;
#endif
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/platform/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ if CHIP_DEVICE_LAYER_TARGET_LINUX
SUBDIRS += tests

libDeviceLayer_a_CPPFLAGS += \
$(GIO_CFLAGS) \
$(INIPP_CPPFLAGS) \
$(NULL)

Expand Down
17 changes: 13 additions & 4 deletions src/platform/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down

0 comments on commit bca8b6e

Please sign in to comment.