6 DBUSMessageHandler::DBUSMessageHandler()
12 dbus_error_init(&m_err);
14 // connect to the bus and check for errors
15 m_pConnection = dbus_bus_get(DBUS_BUS_SESSION, &m_err);
16 if (dbus_error_is_set(&m_err))
18 DLT_LOG(DBusPlugin,DLT_LOG_INFO, DLT_STRING("DBUSCommunicator Connection error"));
19 dbus_error_free(&m_err);
21 if (NULL == m_pConnection)
23 DLT_LOG(DBusPlugin,DLT_LOG_INFO, DLT_STRING("DBUSCommunicator Connection is null"));
26 int ret = dbus_bus_request_name(m_pConnection, DBUS_SERVICE_PREFIX, DBUS_NAME_FLAG_REPLACE_EXISTING, &m_err);
27 if (dbus_error_is_set(&m_err))
29 DLT_LOG(DBusPlugin,DLT_LOG_INFO, DLT_STRING("DBUSCommunicator Name Error"),DLT_STRING(m_err.message));
30 dbus_error_free(&m_err);
32 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
34 DLT_LOG(DBusPlugin,DLT_LOG_INFO, DLT_STRING("DBUSCommunicatorNot Primary Owner"), DLT_INT(ret));
39 DBUSMessageHandler::~DBUSMessageHandler()
42 dbus_error_init(&err);
43 bool errorset = dbus_error_is_set(&err);
46 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("there was an dbus error"));
48 dbus_bus_name_has_owner(m_pConnection, DBUS_SERVICE_PREFIX, &err);
49 errorset = dbus_error_is_set(&err);
52 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("there was an dbus error"));
54 dbus_error_init(&err);
55 dbus_bus_release_name(m_pConnection, DBUS_SERVICE_PREFIX, &err);
58 void DBUSMessageHandler::initReceive(DBusMessage* msg)
60 if (!dbus_message_iter_init(msg, &m_MessageIter))
62 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS Message has no arguments!"));
66 void DBUSMessageHandler::initReply(DBusMessage* msg)
68 // create a reply from the message
69 m_pReply = dbus_message_new_method_return(msg);
70 dbus_message_iter_init_append(m_pReply, &m_MessageIter);
73 void DBUSMessageHandler::closeReply()
75 // send the reply && flush the connection
76 if (!dbus_connection_send(m_pConnection, m_pReply, &m_serial))
78 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
81 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler sending reply!"));
82 dbus_connection_flush(m_pConnection);
85 dbus_message_unref(m_pReply);
89 void DBUSMessageHandler::ReplyError(DBusMessage* msg, const char* errorname, const char* errorMsg)
91 m_pReply = dbus_message_new_error(msg, errorname, errorMsg);
92 // send the reply && flush the connection
93 if (!dbus_connection_send(m_pConnection, m_pReply, &m_serial))
95 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
98 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler sending reply with error!"));
99 dbus_connection_flush(m_pConnection);
102 dbus_message_unref(m_pReply);
105 char* DBUSMessageHandler::getString()
109 if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&m_MessageIter))
111 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no string!"));
115 dbus_message_iter_get_basic(&m_MessageIter, ¶m);
116 dbus_message_iter_next(&m_MessageIter);
121 dbus_bool_t DBUSMessageHandler::getBool()
123 dbus_bool_t boolparam;
125 if (DBUS_TYPE_BOOLEAN != dbus_message_iter_get_arg_type(&m_MessageIter))
127 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no bool!"));
131 dbus_message_iter_get_basic(&m_MessageIter, &boolparam);
132 dbus_message_iter_next(&m_MessageIter);
137 char DBUSMessageHandler::getByte()
141 if (DBUS_TYPE_BYTE != dbus_message_iter_get_arg_type(&m_MessageIter))
143 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no byte!"));
147 dbus_message_iter_get_basic(&m_MessageIter, ¶m);
148 dbus_message_iter_next(&m_MessageIter);
153 dbus_uint32_t DBUSMessageHandler::getUInt()
157 if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&m_MessageIter))
159 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no uint32_t!"));
163 dbus_message_iter_get_basic(&m_MessageIter, ¶m);
164 dbus_message_iter_next(&m_MessageIter);
169 double DBUSMessageHandler::getDouble()
173 if (DBUS_TYPE_DOUBLE != dbus_message_iter_get_arg_type(&m_MessageIter))
175 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no double!"));
179 dbus_message_iter_get_basic(&m_MessageIter, ¶m);
180 dbus_message_iter_next(&m_MessageIter);
185 void DBUSMessageHandler::getArrayOfUInt(int* pLength, unsigned int** ppArray)
187 if (DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type(&m_MessageIter))
189 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no array!"));
193 DBusMessageIter arrayIter;
194 dbus_message_iter_recurse(&m_MessageIter, &arrayIter);
197 dbus_message_iter_get_fixed_array(&arrayIter, &localArray, pLength);
199 *ppArray = new uint[*pLength];
200 for (int i = 0; i < *pLength; i++)
202 (*ppArray)[i] = localArray[i];
206 void DBUSMessageHandler::getArrayOfString(std::vector<std::string>* stringVector)
208 if (DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type(&m_MessageIter))
210 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no array!"));
214 DBusMessageIter arrayIter;
215 dbus_message_iter_recurse(&m_MessageIter, &arrayIter);
219 if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&arrayIter))
221 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no string!"));
224 dbus_message_iter_get_basic(&arrayIter, ¶m);
226 stringVector->push_back(std::string(param));
228 if (dbus_message_iter_has_next(&arrayIter))
230 dbus_message_iter_next(&arrayIter);
239 void DBUSMessageHandler::appendBool(dbus_bool_t toAppend)
241 if (!dbus_message_iter_append_basic(&m_MessageIter, DBUS_TYPE_BOOLEAN, &toAppend))
243 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
248 void DBUSMessageHandler::appendUInt(dbus_uint32_t toAppend)
250 if (!dbus_message_iter_append_basic(&m_MessageIter, DBUS_TYPE_UINT32, &toAppend))
252 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
257 void DBUSMessageHandler::appendDouble(double toAppend)
259 if (!dbus_message_iter_append_basic(&m_MessageIter, DBUS_TYPE_DOUBLE, &toAppend))
261 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
266 void DBUSMessageHandler::appendByte(char toAppend)
268 if (!dbus_message_iter_append_basic(&m_MessageIter, DBUS_TYPE_BYTE, &toAppend))
270 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
275 void DBUSMessageHandler::appendArrayOfUInt(unsigned int length, unsigned int *IDs)
277 DBusMessageIter arrayIter;
278 dbus_message_iter_open_container(&m_MessageIter, DBUS_TYPE_ARRAY, "u", &arrayIter);
279 for(unsigned int i = 0; i < length; i++)
281 dbus_message_iter_append_basic(&arrayIter, DBUS_TYPE_UINT32, &IDs[i]);
283 dbus_message_iter_close_container(&m_MessageIter, &arrayIter);
286 void DBUSMessageHandler::sendSignal(const char* name, const char* signal) {
287 dbus_uint32_t serial = 0;
289 DBusMessageIter args;
291 msg = dbus_message_new_signal(DBUS_SERVICE_PREFIX, signal, name);
295 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("Message null!"));
296 this->~DBUSMessageHandler();
299 if (!dbus_connection_send(m_pConnection, msg, &serial)) {
300 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
301 this->~DBUSMessageHandler();
304 dbus_connection_flush(m_pConnection);
307 dbus_message_unref(msg);