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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "DBusUtil.h"
#include "utils/log.h"
CVariant CDBusUtil::GetVariant(const char *destination, const char *object, const char *interface, const char *property)
{
//dbus-send --system --print-reply --dest=destination object org.freedesktop.DBus.Properties.Get string:interface string:property
CDBusMessage message(destination, object, "org.freedesktop.DBus.Properties", "Get");
CVariant result;
message.AppendArgument(interface);
message.AppendArgument(property);
DBusMessage *reply = message.SendSystem();
if (reply)
{
DBusMessageIter iter;
if (dbus_message_iter_init(reply, &iter))
{
if (!dbus_message_has_signature(reply, "v"))
CLog::Log(LOGERROR, "DBus: wrong signature on Get - should be \"v\" but was {}",
dbus_message_iter_get_signature(&iter));
else
result = ParseVariant(&iter);
}
}
return result;
}
CVariant CDBusUtil::GetAll(const char *destination, const char *object, const char *interface)
{
CDBusMessage message(destination, object, "org.freedesktop.DBus.Properties", "GetAll");
CVariant properties;
message.AppendArgument(interface);
DBusMessage *reply = message.SendSystem();
if (reply)
{
DBusMessageIter iter;
if (dbus_message_iter_init(reply, &iter))
{
if (!dbus_message_has_signature(reply, "a{sv}"))
CLog::Log(LOGERROR, "DBus: wrong signature on GetAll - should be \"a{sv}\" but was {}",
dbus_message_iter_get_signature(&iter));
else
{
do
{
DBusMessageIter sub;
dbus_message_iter_recurse(&iter, &sub);
do
{
DBusMessageIter dict;
dbus_message_iter_recurse(&sub, &dict);
do
{
const char * key = NULL;
dbus_message_iter_get_basic(&dict, &key);
if (!dbus_message_iter_next(&dict))
break;
CVariant value = ParseVariant(&dict);
if (!value.isNull())
properties[key] = value;
} while (dbus_message_iter_next(&dict));
} while (dbus_message_iter_next(&sub));
} while (dbus_message_iter_next(&iter));
}
}
}
return properties;
}
CVariant CDBusUtil::ParseVariant(DBusMessageIter *itr)
{
DBusMessageIter variant;
dbus_message_iter_recurse(itr, &variant);
return ParseType(&variant);
}
CVariant CDBusUtil::ParseType(DBusMessageIter *itr)
{
CVariant value;
const char * string = NULL;
dbus_int32_t int32 = 0;
dbus_uint32_t uint32 = 0;
dbus_int64_t int64 = 0;
dbus_uint64_t uint64 = 0;
dbus_bool_t boolean = false;
double doublev = 0;
int type = dbus_message_iter_get_arg_type(itr);
switch (type)
{
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_STRING:
dbus_message_iter_get_basic(itr, &string);
value = string;
break;
case DBUS_TYPE_UINT32:
dbus_message_iter_get_basic(itr, &uint32);
value = (uint64_t)uint32;
break;
case DBUS_TYPE_BYTE:
case DBUS_TYPE_INT32:
dbus_message_iter_get_basic(itr, &int32);
value = (int64_t)int32;
break;
case DBUS_TYPE_UINT64:
dbus_message_iter_get_basic(itr, &uint64);
value = (uint64_t)uint64;
break;
case DBUS_TYPE_INT64:
dbus_message_iter_get_basic(itr, &int64);
value = (int64_t)int64;
break;
case DBUS_TYPE_BOOLEAN:
dbus_message_iter_get_basic(itr, &boolean);
value = (bool)boolean;
break;
case DBUS_TYPE_DOUBLE:
dbus_message_iter_get_basic(itr, &doublev);
value = doublev;
break;
case DBUS_TYPE_ARRAY:
DBusMessageIter array;
dbus_message_iter_recurse(itr, &array);
value = CVariant::VariantTypeArray;
do
{
CVariant item = ParseType(&array);
if (!item.isNull())
value.push_back(item);
} while (dbus_message_iter_next(&array));
break;
}
return value;
}
bool CDBusUtil::TryMethodCall(DBusBusType bus, const char* destination, const char* object, const char* interface, const char* method)
{
CDBusMessage message(destination, object, interface, method);
CDBusError error;
message.Send(bus, error);
if (error)
{
error.Log(LOGDEBUG, std::string("DBus method call to ") + interface + "." + method + " at " + object + " of " + destination + " failed");
}
return !error;
}
bool CDBusUtil::TryMethodCall(DBusBusType bus, std::string const& destination, std::string const& object, std::string const& interface, std::string const& method)
{
return TryMethodCall(bus, destination.c_str(), object.c_str(), interface.c_str(), method.c_str());
}
CDBusConnection::CDBusConnection() = default;
bool CDBusConnection::Connect(DBusBusType bus, bool openPrivate)
{
CDBusError error;
Connect(bus, error, openPrivate);
if (error)
{
error.Log(LOGWARNING, "DBus connection failed");
return false;
}
return true;
}
bool CDBusConnection::Connect(DBusBusType bus, CDBusError& error, bool openPrivate)
{
if (m_connection)
{
throw std::logic_error("Cannot reopen connected DBus connection");
}
m_connection.get_deleter().closeBeforeUnref = openPrivate;
if (openPrivate)
{
m_connection.reset(dbus_bus_get_private(bus, error));
}
else
{
m_connection.reset(dbus_bus_get(bus, error));
}
return !!m_connection;
}
CDBusConnection::operator DBusConnection*()
{
return m_connection.get();
}
void CDBusConnection::DBusConnectionDeleter::operator()(DBusConnection* connection) const
{
if (closeBeforeUnref)
{
dbus_connection_close(connection);
}
dbus_connection_unref(connection);
}
void CDBusConnection::Destroy()
{
m_connection.reset();
}
CDBusError::CDBusError()
{
dbus_error_init(&m_error);
}
CDBusError::~CDBusError()
{
Reset();
}
void CDBusError::Reset()
{
dbus_error_free(&m_error);
}
CDBusError::operator DBusError*()
{
return &m_error;
}
bool CDBusError::IsSet() const
{
return dbus_error_is_set(&m_error);
}
CDBusError::operator bool()
{
return IsSet();
}
CDBusError::operator bool() const
{
return IsSet();
}
std::string CDBusError::Name() const
{
if (!IsSet())
{
throw std::logic_error("Cannot retrieve name of unset DBus error");
}
return m_error.name;
}
std::string CDBusError::Message() const
{
if (!IsSet())
{
throw std::logic_error("Cannot retrieve message of unset DBus error");
}
return m_error.message;
}
void CDBusError::Log(std::string const& message) const
{
Log(LOGERROR, message);
}
void CDBusError::Log(int level, const std::string& message) const
{
if (!IsSet())
{
throw std::logic_error("Cannot log unset DBus error");
}
CLog::Log(level, "{}: {} - {}", message, m_error.name, m_error.message);
}
|