summaryrefslogtreecommitdiff
path: root/PluginCommandInterfaceDbus/DbusSend.cpp
blob: 0fa1e1949b0b363c6f7e398287259697b4168131 (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
/*
 * DbusSend.cpp
 *
 *  Created on: Jul 20, 2011
 *      Author: christian
 */

#include "DbusSend.h"

DbusSend::DbusSend (DBusConnection* conn, const char* bus_name,const char* path, const char* interface, const char* method) : m_conn(conn) {
	m_msg=dbus_message_new_method_call(bus_name,path,interface,method);
	if (NULL == m_msg) {
		  DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("Error while creating DBus message"));
	      this->~DbusSend();
	}
}

DbusSend::~DbusSend() {
	// TODO Auto-generated destructor stub
}

void DbusSend::appendString(char* string) {
	dbus_message_iter_init_append(m_msg, &m_args);
	if (!dbus_message_iter_append_basic(&m_args, DBUS_TYPE_STRING, string)) {
		DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("Out of memory"));
	 	this->~DbusSend();
	}
}

void DbusSend::appendInteger(int integer) {
	dbus_message_iter_init_append(m_msg, &m_args);
	if (!dbus_message_iter_append_basic(&m_args, DBUS_TYPE_INT32, &integer)) {
		DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("Out of memory"));
	 	this->~DbusSend();
	}

}

void DbusSend::sendReply(bool* reply) {

	DBusPendingCall* pending;
	DBusMessageIter args;
	if (!dbus_connection_send_with_reply (m_conn, m_msg, &pending, -1)) { // -1 is default timeout
		DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("Out of memory"));
		this->~DbusSend();
	}

	if (NULL == pending) {
		DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("Pending Call Null"));
		this->~DbusSend();
	}
	dbus_connection_flush(m_conn);
	dbus_message_unref(m_msg);
    dbus_pending_call_block(pending);

    DBusMessage* msg=dbus_pending_call_steal_reply(pending);

	if (NULL == msg) {
		DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("DBUS reply NULL"));
		this->~DbusSend();
	}

	dbus_pending_call_unref(pending);

    if (!dbus_message_iter_init(msg, &args)) {
    	DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("DBUS Message without arguments"));
    } else if (DBUS_TYPE_BOOLEAN != dbus_message_iter_get_arg_type(&args)) {
		DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("Argument not boolean"));
	} else {
		dbus_message_iter_get_basic(&args, reply);
	}

    dbus_message_unref(msg);
}

void DbusSend::sendReply(int* reply) {

	DBusPendingCall* pending;
	DBusMessageIter args;
	if (!dbus_connection_send_with_reply (m_conn, m_msg, &pending, -1)) { // -1 is default timeout
		DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("Out of memory"));
		this->~DbusSend();
	}

	if (NULL == pending) {
		DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("Pending Call Null"));
		this->~DbusSend();
	}
	dbus_connection_flush(m_conn);
	dbus_message_unref(m_msg);
    dbus_pending_call_block(pending);

    DBusMessage* msg=dbus_pending_call_steal_reply(pending);

	if (NULL == msg) {
		DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("DBUS reply NULL"));
		this->~DbusSend();
	}

	dbus_pending_call_unref(pending);

    if (!dbus_message_iter_init(msg, &args)) {
    	DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("DBUS Message without arguments"));
    } else if (DBUS_TYPE_INT32 != dbus_message_iter_get_arg_type(&args)) {
		DLT_LOG(DBusCommandPlugin, DLT_LOG_INFO, DLT_STRING("Argument not integer"));
	} else {
		dbus_message_iter_get_basic(&args, reply);
	}

    dbus_message_unref(msg);
}