-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample-service.cpp
173 lines (138 loc) · 5.39 KB
/
example-service.cpp
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// GDBus++ - glib2 GDBus C++ wrapper
//
// SPDX-License-Identifier: AGPL-3.0-only
//
// Copyright (C) OpenVPN Inc <[email protected]>
// Copyright (C) David Sommerseth <[email protected]>
//
/**
* @file example-service.cpp
*
* @brief Simple example service.
* This example code is referenced to from the README.md document.
*
* To compile this program:
*
* $ c++ -std=c++17 $(pkg-config gdbuspp glib-2.0 gio-2.0 gio-unix-2.0 --cflags --libs) -o example-service example-service.cpp
*
*/
#include <iostream>
#include <string>
#include <gdbuspp/service.hpp>
#include <gdbuspp/glib2/utils.hpp>
class MySignalGroup : public DBus::Signals::Group
{
public:
using Ptr = std::shared_ptr<MySignalGroup>;
MySignalGroup(DBus::Connection::Ptr connection)
: DBus::Signals::Group(connection,
"/example/myobject",
"net.example.myinterface")
{
RegisterSignal("MySignal",
{{"message", glib2::DataType::DBus<std::string>()}});
}
void MySignal(const std::string &message_content)
{
// NOTE: D-Bus signals are picky on the data type. Signals
// must always be sent as a tuple, even if it's just a single value
GVariant *message = glib2::Value::CreateTupleWrapped(message_content);
SendGVariant("MySignal", message);
}
};
class MyObject : public DBus::Object::Base
{
public:
MyObject(DBus::Connection::Ptr connection, DBus::Object::Manager::Ptr obj_mgr)
: DBus::Object::Base("/example/myobject",
"net.example.myinterface"),
object_manager(obj_mgr)
{
// Declaring D-Bus methods
AddMethod("MyMethod",
[](DBus::Object::Method::Arguments::Ptr args)
{
// Code performed on access
std::cout << "MyMethod called: " << args << std::endl;
// We have no data to return; so we return nullptr
args->SetMethodReturn(nullptr);
});
auto args = AddMethod("MethodWithArgs",
[](DBus::Object::Method::Arguments::Ptr args)
{
// Extract input arguments
GVariant *params = args->GetMethodParameters();
std::string string1 = glib2::Value::Extract<std::string>(params, 0);
std::string string2 = glib2::Value::Extract<std::string>(params, 1);
// Prepare the response to the caller
std::string result = string1 + " <=> " + string2;
GVariant *ret = glib2::Value::CreateTupleWrapped(result);
args->SetMethodReturn(ret);
});
args->AddInput("string_1", glib2::DataType::DBus<std::string>());
args->AddInput("string_2", glib2::DataType::DBus<std::string>());
args->AddOutput("result", glib2::DataType::DBus<std::string>());
// Declaring a D-Bus property
bool readwrite = true;
AddProperty("my_property", my_property, readwrite);
// Declaring a D-Bus signal
my_signals = DBus::Signals::Group::Create<MySignalGroup>(connection);
my_signals->AddTarget("");
RegisterSignals(my_signals);
}
const bool Authorize(const DBus::Authz::Request::Ptr request) override
{
// code to authorize a D-Bus proxy client accessing this object
// Send a simple D-Bus signal whenever this is called
my_signals->MySignal("Authorize called");
// By returning 'true', access is granted
return true;
}
private:
DBus::Object::Manager::Ptr object_manager = nullptr;
std::string my_property{"My Initial Value"};
MySignalGroup::Ptr my_signals = nullptr;
};
class MyService : public DBus::Service
{
public:
MyService(DBus::Connection::Ptr con)
: DBus::Service(con, "net.example.myservice")
{
}
void BusNameAcquired(const std::string &busname) override
{
// Code run when the name of your service is accepted
std::cout << "Service registered: " << busname << std::endl;
}
void BusNameLost(const std::string &busname) override
{
// Code run when your service could not get or keep the bus name
std::cout << "Could not acquire the bus name: " << busname << std::endl;
// Here we just stop and exit the service
DBus::Service::Stop();
}
};
int main(int argc, char **argv)
{
try
{
// Prepare a connection to the Session D-Bus
auto connection = DBus::Connection::Create(DBus::BusType::SESSION);
// Instantiate and prepare the D-Bus service provided
// via the MyService class
auto my_service = DBus::Service::Create<MyService>(connection);
// Get access to the DBus::Object::Manager object
auto object_manager = my_service->GetObjectManager();
// Add the service root object
my_service->CreateServiceHandler<MyObject>(connection, object_manager);
// Start the service
my_service->Run();
return 0;
}
catch (const DBus::Exception &excp)
{
std::cerr << "EXCEPTION CAUGHT: " << excp.what() << std::endl;
return 2;
}
}