-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbushandler.cpp
111 lines (86 loc) · 2.59 KB
/
dbushandler.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
#include "dbushandler.h"
#include "logger.h"
DBusHandler::DBusHandler(const AppConf::DBusParamsStruct & params):
isConnected(false),
_params(params)
{
}
bool DBusHandler::Init()
{
if(isConnected)
return true;
DBusError err;
dbus_error_init(&err);
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err))
{
//Connection Error
dbus_error_free(&err);
return false;
}
dbus_bus_request_name(conn, _params.name.data(), DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
if (dbus_error_is_set(&err))
{
//Name Error
Logger::instance() << err.message;
dbus_error_free(&err);
return false;
}
isConnected = true;
return true;
}
bool DBusHandler::MethodCall(const char * target, const char * object, const char * interface, const char * method, DBusPendingCall * & pending)
{
DBusMessage* msg;
msg = dbus_message_new_method_call(target, object, interface, method);
if (NULL == msg)
return false;
if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) // -1 is default timeout
return false;
if (NULL == pending)
return false;
dbus_connection_flush(conn);
dbus_message_unref(msg);
return true;
}
bool DBusHandler::PropertyRequest(const char * target, const char * object, const char * interface, const char * property, DBusPendingCall * & pending)
{
DBusMessage* msg;
DBusMessageIter args;
msg = dbus_message_new_method_call(target, object, "org.freedesktop.DBus.Properties", "Get");
if (NULL == msg)
return false;
dbus_message_iter_init_append(msg, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &interface))
return false;
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &property))
return false;
if (!dbus_connection_send_with_reply (conn, msg, &pending, -1))
return false;
if (NULL == pending)
return false;
dbus_connection_flush(conn);
dbus_message_unref(msg);
return true;
}
bool DBusHandler::GetReply(DBusPendingCall* & pending, DBusMessage * & msg)
{
dbus_pending_call_block(pending);
msg = dbus_pending_call_steal_reply(pending);
if (NULL == msg)
return false;
dbus_pending_call_unref(pending);
return true;
}
std::string DBusHandler::GetString(DBusMessageIter & iter)
{
char * cstr;
dbus_message_iter_get_basic(&iter, &cstr);
return std::string(cstr);
}
int DBusHandler::GetInt(DBusMessageIter &iter)
{
int num;
dbus_message_iter_get_basic(&iter, &num);
return num;
}