aboutsummaryrefslogtreecommitdiff
path: root/src/DBusMethod.h
blob: 89bc7e30e0d0faa3c729de00e7d589de158dfec0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 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 <http://www.gnu.org/licenses/>. 
 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// 
// >> 
// >>>  INSIDE THIS FILE 
// >> 
// 
// This is a representation of a D-Bus interface method. 
// 
// >> 
// >>>  DISCUSSION 
// >> 
// 
// See the discussion at the top of DBusMethod.cpp 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// This file contains a representation of a D-Bus interface method. 
// 
// Methods are identified by their name (such as "Get" or "Set"). They have argument definitions (defined as part of their 
// interface) that describe the type of arguments passed into the method and returned from the method. 
// 
// In addition to the method itself, we also store a callback that can be called whenever the method is invoked. 
 
#pragma once
 
#include <gio/gio.h>
#include <string>
#include <vector>
 
#include "Globals.h"
#include "DBusObjectPath.h"
#include "Logger.h"
 
struct DBusInterface;
 
struct DBusMethod
{
// A method callback delegate 
typedef void (*Callback)(const DBusInterface &self, GDBusConnection *pConnection, const std::string &methodName, GVariant *pParameters, GDBusMethodInvocation *pInvocation, void *pUserData);
 
// Instantiate a named method on a given interface (pOwner) with a given set of arguments and a callback delegate 
DBusMethod(const DBusInterface *pOwner, const std::string &name, const char *pInArgs[], const char *pOutArgs, Callback callback);
 
// 
// Accessors 
// 
 
// Returns the name of the method 
const std::string &getName() const { return name; }
 
// Sets the name of the method 
// 
// This method should generally not be called directly. Rather, the name should be set by the constructor 
DBusMethod &setName(const std::string &name) { this->name = name; return *this}
 
// Get the input argument type string (a GVariant type string format) 
const std::vector<std::string> &getInArgs() const { return inArgs; }
 
// Get the output argument type string (a GVariant type string format) 
const std::string &getOutArgs() const { return outArgs; }
 
// Set the argument types for this method 
// 
// This method should generally not be called directly. Rather, the arguments should be set by the constructor 
DBusMethod &setArgs(const std::vector<std::string> &inArgs, const std::string &outArgs)
{
this->inArgs = inArgs;
this->outArgs = outArgs;
return *this;
}
 
// 
// Call the method 
// 
 
// Calls the method 
// 
// If a callback delegate has been set, then this method will call that delegate, otherwise this method will do nothing 
template<typename T>
void call(GDBusConnection *pConnection, const DBusObjectPath &path, const std::string &interfaceName, const std::string &methodName, GVariant *pParameters, GDBusMethodInvocation *pInvocation, void *pUserData) const
{
// This should never happen, but technically possible if instantiated with a nullptr for `callback` 
if (!callback)
{
Logger::error(SSTR << "DBusMethod contains no callback: [" << path << "]:[" << interfaceName << "]:[" << methodName << "]");
g_dbus_method_invocation_return_dbus_error(pInvocation, kErrorNotImplemented.c_str(), "This method is not implemented");
return;
}
 
Logger::info(SSTR << "Calling method: [" << path << "]:[" << interfaceName << "]:[" << methodName << "]");
callback(*static_cast<const T *>(pOwner), pConnection, methodName, pParameters, pInvocation, pUserData);
}
 
// Internal method used to generate introspection XML used to describe our services on D-Bus 
std::string generateIntrospectionXML(int depth) const;
 
private:
const DBusInterface *pOwner;
std::string name;
std::vector<std::string> inArgs;
std::string outArgs;
Callback callback;
};