-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsignal-emit.cpp
168 lines (151 loc) · 4.9 KB
/
signal-emit.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
// 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 signal-emit.cpp
*
* @brief Very simple and generic D-Bus signal sender tool
*/
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include <getopt.h>
#include "../gdbuspp/connection.hpp"
#include "../gdbuspp/glib2/utils.hpp"
#include "../gdbuspp/signals/emit.hpp"
#include "../gdbuspp/signals/target.hpp"
#include "test-utils.hpp"
#include "test-constants.hpp"
using namespace DBus;
using namespace Test;
class EmitOpts : protected TestUtils::OptionParser
{
public:
EmitOpts(const int argc, char **argv)
{
static struct option long_opts[] = {
// clang-format off
{"system", no_argument, nullptr, 'Y'},
{"session", no_argument, nullptr, 'E'},
{"destination", required_argument, nullptr, 'd'},
{"object-path", required_argument, nullptr, 'p'},
{"interface", required_argument, nullptr, 'i'},
{"signal-name", required_argument, nullptr, 's'},
{"repeat-send", required_argument, nullptr, 'r'},
{"delay-send", required_argument, nullptr, 'D'},
{"data-type", required_argument, nullptr, 't'},
{"data-value", required_argument, nullptr, 'v'},
{"quiet", no_argument, nullptr, 'q'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}
// clang-format on
};
int opt;
optind = 1;
std::string destination{};
std::string object_path{Constants::GenPath("signals")};
std::string object_interface{Constants::GenInterface("signals")};
while ((opt = getopt_long(argc, argv, "YEd:p:i:r:D:s:t:v:qh", long_opts, nullptr)) != -1)
{
switch (opt)
{
case 'Y':
bustype = DBus::BusType::SYSTEM;
break;
case 'E':
bustype = DBus::BusType::SESSION;
break;
case 'd':
destination = std::string(optarg);
break;
case 'p':
object_path = std::string(optarg);
break;
case 'i':
object_interface = std::string(optarg);
break;
case 'r':
repeat_send = atoi(optarg);
break;
case 'D':
delay_send = atoi(optarg);
break;
case 's':
signal_name = std::string(optarg);
break;
case 't':
data_type = std::string(optarg);
break;
case 'v':
data_values.push_back(std::string(optarg));
break;
case 'q':
quiet = true;
break;
case 'h':
help(argv[0], long_opts);
exit(0);
}
}
target = Signals::Target::Create(destination, object_path, object_interface);
};
DBus::BusType bustype = DBus::BusType::SESSION;
Signals::Target::Ptr target = nullptr;
std::string signal_name{};
uint32_t repeat_send = 1;
uint32_t delay_send = 0;
std::string data_type{};
std::vector<std::string> data_values{};
bool quiet = false;
};
int main(int argc, char **argv)
{
EmitOpts options(argc, argv);
std::ostringstream log;
try
{
// Process all the --data-value values into a GVariant *
// "package" to be used in the proxy call
GVariant *data = nullptr;
try
{
data = TestUtils::generate_gvariant(log,
options.data_type,
options.data_values,
true);
}
catch (const TestUtils::Exception &excp)
{
std::cerr << "** ERROR ** " << excp.what() << std::endl;
return 2;
}
auto dbuscon = DBus::Connection::Create(options.bustype);
auto sendsignal = Signals::Emit::Create(dbuscon);
sendsignal->AddTarget(options.target);
for (uint32_t i = 0; i < options.repeat_send; ++i)
{
if (options.delay_send > 0)
{
usleep(options.delay_send);
}
sendsignal->SendGVariant(options.signal_name, data);
}
g_variant_unref(data);
if (!options.quiet)
{
std::cout << log.str();
}
return 0;
}
catch (const DBus::Exception &excp)
{
std::cout << log.str() << std::endl;
std::cerr << "** EXCEPTION ** " << excp.what() << std::endl;
return 2;
}
}