From 06f646aec4dbce64d28bae1be6111bd833f8e79e Mon Sep 17 00:00:00 2001 From: Paul Nettle Date: Fri, 25 Aug 2017 09:30:39 -0500 Subject: Initial version 1.0 --- src/DBusObject.h | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 src/DBusObject.h (limited to 'src/DBusObject.h') diff --git a/src/DBusObject.h b/src/DBusObject.h new file mode 100644 index 0000000..123964e --- /dev/null +++ b/src/DBusObject.h @@ -0,0 +1,135 @@ +// Copyright 2017 Paul Nettle. +// +// This file is part of Gobbledegook. +// +// Gobbledegook is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Gobbledegook 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Gobbledegook. If not, see . + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// +// >> +// >>> INSIDE THIS FILE +// >> +// +// This is an abstraction of a D-Bus object. +// +// >> +// >>> DISCUSSION +// >> +// +// See the discussino at the top of DBusObject.cpp +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +#pragma once + +#include +#include +#include +#include + +#include "DBusInterface.h" +#include "DBusObjectPath.h" + +struct GattProperty; +struct GattService; +struct GattUuid; + +struct DBusObject +{ + // A convenience typedef for describing our list of interface + typedef std::list > InterfaceList; + + // Construct a root object with no parent + // + // We'll include a publish flag since only root objects can be published + DBusObject(const DBusObjectPath &path, bool publish = true); + + // Construct a node object + // + // Nodes inherit their parent's publish path + DBusObject(DBusObject *pParent, const DBusObjectPath &pathElement); + + // + // Accessors + // + + // Returns the `publish` flag + bool isPublished() const; + + // Returns the path node for this object within the hierarchy + // + // This method only returns the node. To get the full path, use `getPath()` + const DBusObjectPath &getPathNode() const; + + // Returns the full path for this object within the hierarchy + // + // This method returns the full path. To get the current node, use `getPathNode()` + DBusObjectPath getPath() const; + + // Returns the parent object in the hierarchy + DBusObject &getParent(); + + // Returns the list of children objects + const std::list &getChildren() const; + + // Add a child to this object + DBusObject &addChild(const DBusObjectPath &pathElement); + + // Returns a list of interfaces for this object + const InterfaceList &getInterfaces() const; + + // Templated method for adding typed interfaces to the object + template + std::shared_ptr addInterface(std::shared_ptr interface) + { + interfaces.push_back(interface); + return std::static_pointer_cast(interfaces.back()); + } + + // Internal method used to generate introspection XML used to describe our services on D-Bus + std::string generateIntrospectionXML(int depth = 0) const; + + // Convenience functions to add a GATT service to the hierarchy + // + // We simply add a new child at the given path and add an interface configured as a GATT service to it using the given UUID. + // + // To end a service, call `gattServiceEnd()` + GattService &gattServiceBegin(const std::string &pathElement, const GattUuid &uuid); + + // + // Helpful routines for searching objects + // + + // Finds an interface by name within this D-Bus object + std::shared_ptr findInterface(const DBusObjectPath &path, const std::string &interfaceName, const DBusObjectPath &basePath = DBusObjectPath()) const; + + // Finds a BlueZ method by name within the specified D-Bus interface + bool callMethod(const DBusObjectPath &path, const std::string &interfaceName, const std::string &methodName, GDBusConnection *pConnection, GVariant *pParameters, GDBusMethodInvocation *pInvocation, gpointer pUserData, const DBusObjectPath &basePath = DBusObjectPath()) const; + + // Periodic timer tick propagation + void tickEvents(GDBusConnection *pConnection, void *pUserData = nullptr) const; + + // ----------------------------------------------------------------------------------------------------------------------------- + // D-Bus signals + // ----------------------------------------------------------------------------------------------------------------------------- + + // Emits a signal on the bus from the given path, interface name and signal name, containing a GVariant set of parameters + void emitSignal(GDBusConnection *pBusConnection, const std::string &interfaceName, const std::string &signalName, GVariant *pParameters); + +private: + bool publish; + DBusObjectPath path; + InterfaceList interfaces; + std::list children; + DBusObject *pParent; +}; -- cgit v1.2.3