401fa3298b88455154435598e9d8bbc4ea13c582
[profile/ivi/genivi/genivi-audio-manager.git] / PluginRoutingInterfaceDbus / DBUSMessageHandler.cpp
1
2
3 #include "headers.h"
4 #include <stdlib.h>
5
6 DBUSMessageHandler::DBUSMessageHandler()
7 : m_MessageIter()
8 , m_pReply(0)
9 , m_serial(0)
10 , m_pConnection(0)
11 {
12     dbus_error_init(&m_err);
13
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))
17     {
18         DLT_LOG(DBusPlugin,DLT_LOG_INFO, DLT_STRING("DBUSCommunicator Connection error"));
19         dbus_error_free(&m_err);
20     }
21     if (NULL == m_pConnection)
22     {
23         DLT_LOG(DBusPlugin,DLT_LOG_INFO, DLT_STRING("DBUSCommunicator Connection is null"));
24         exit(1);
25     }
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))
28     {
29         DLT_LOG(DBusPlugin,DLT_LOG_INFO, DLT_STRING("DBUSCommunicator Name Error"),DLT_STRING(m_err.message));
30         dbus_error_free(&m_err);
31     }
32     if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
33     {
34         DLT_LOG(DBusPlugin,DLT_LOG_INFO, DLT_STRING("DBUSCommunicatorNot Primary Owner"), DLT_INT(ret));
35         exit(1);
36     }
37 }
38
39 DBUSMessageHandler::~DBUSMessageHandler()
40 {
41     DBusError err;
42     dbus_error_init(&err);
43     bool errorset = dbus_error_is_set(&err);
44     if (errorset)
45     {
46         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("there was an dbus error"));
47     }
48     dbus_bus_name_has_owner(m_pConnection, DBUS_SERVICE_PREFIX, &err);
49     errorset = dbus_error_is_set(&err);
50     if (errorset)
51     {
52         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("there was an dbus error"));
53     }
54     dbus_error_init(&err);
55     dbus_bus_release_name(m_pConnection, DBUS_SERVICE_PREFIX, &err);
56 }
57
58 void DBUSMessageHandler::initReceive(DBusMessage* msg)
59 {
60     if (!dbus_message_iter_init(msg, &m_MessageIter))
61     {
62         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS Message has no arguments!"));
63     }
64 }
65
66 void DBUSMessageHandler::initReply(DBusMessage* msg)
67 {
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);
71 }
72
73 void DBUSMessageHandler::closeReply()
74 {
75     // send the reply && flush the connection
76     if (!dbus_connection_send(m_pConnection, m_pReply, &m_serial))
77     {
78         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
79         exit(1);
80     }
81         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler sending reply!"));
82     dbus_connection_flush(m_pConnection);
83
84     // free the reply
85     dbus_message_unref(m_pReply);
86     m_pReply = NULL;
87 }
88
89 void DBUSMessageHandler::ReplyError(DBusMessage* msg, const char* errorname, const char* errorMsg)
90 {
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))
94     {
95         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
96         exit(1);
97     }
98         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler sending reply with error!"));
99     dbus_connection_flush(m_pConnection);
100
101     // free the reply
102     dbus_message_unref(m_pReply);
103 }
104
105 char* DBUSMessageHandler::getString()
106 {
107     char* param;
108
109     if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&m_MessageIter))
110     {
111         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no string!"));
112     }
113     else
114     {
115         dbus_message_iter_get_basic(&m_MessageIter, &param);
116         dbus_message_iter_next(&m_MessageIter);
117     }
118     return param;
119 }
120
121 dbus_bool_t DBUSMessageHandler::getBool()
122 {
123     dbus_bool_t boolparam;
124
125     if (DBUS_TYPE_BOOLEAN != dbus_message_iter_get_arg_type(&m_MessageIter))
126     {
127         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no bool!"));
128     }
129     else
130     {
131         dbus_message_iter_get_basic(&m_MessageIter, &boolparam);
132         dbus_message_iter_next(&m_MessageIter);
133     }
134     return boolparam;
135 }
136
137 char DBUSMessageHandler::getByte()
138 {
139     char param;
140
141     if (DBUS_TYPE_BYTE != dbus_message_iter_get_arg_type(&m_MessageIter))
142     {
143         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no byte!"));
144     }
145     else
146     {
147         dbus_message_iter_get_basic(&m_MessageIter, &param);
148         dbus_message_iter_next(&m_MessageIter);
149     }
150     return param;
151 }
152
153 dbus_uint32_t DBUSMessageHandler::getUInt()
154 {
155     dbus_uint32_t param;
156
157     if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&m_MessageIter))
158     {
159         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no uint32_t!"));
160     }
161     else
162     {
163         dbus_message_iter_get_basic(&m_MessageIter, &param);
164         dbus_message_iter_next(&m_MessageIter);
165     }
166     return param;
167 }
168
169 double DBUSMessageHandler::getDouble()
170 {
171     double param;
172
173     if (DBUS_TYPE_DOUBLE != dbus_message_iter_get_arg_type(&m_MessageIter))
174     {
175         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no double!"));
176     }
177     else
178     {
179         dbus_message_iter_get_basic(&m_MessageIter, &param);
180         dbus_message_iter_next(&m_MessageIter);
181     }
182     return param;
183 }
184
185 void DBUSMessageHandler::getArrayOfUInt(int* pLength, unsigned int** ppArray)
186 {
187     if (DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type(&m_MessageIter))
188     {
189         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no array!"));
190         return;
191     }
192
193     DBusMessageIter arrayIter;
194     dbus_message_iter_recurse(&m_MessageIter, &arrayIter);
195
196     uint* localArray;
197     dbus_message_iter_get_fixed_array(&arrayIter, &localArray, pLength);
198
199     *ppArray = new uint[*pLength];
200     for (int i = 0; i < *pLength; i++)
201     {
202         (*ppArray)[i] = localArray[i];
203     }
204 }
205
206 void DBUSMessageHandler::getArrayOfString(std::vector<std::string>* stringVector)
207 {
208     if (DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type(&m_MessageIter))
209     {
210         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no array!"));
211         return;
212     }
213
214     DBusMessageIter arrayIter;
215     dbus_message_iter_recurse(&m_MessageIter, &arrayIter);
216     bool hasNext = true;
217     while (hasNext)
218     {
219         if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&arrayIter))
220         {
221                 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler argument is no string!"));
222         }
223         char* param;
224         dbus_message_iter_get_basic(&arrayIter, &param);
225
226         stringVector->push_back(std::string(param));
227
228         if (dbus_message_iter_has_next(&arrayIter))
229         {
230             dbus_message_iter_next(&arrayIter);
231         }
232         else
233         {
234             hasNext = false;
235         }
236     }
237 }
238
239 void DBUSMessageHandler::appendBool(dbus_bool_t toAppend)
240 {
241     if (!dbus_message_iter_append_basic(&m_MessageIter, DBUS_TYPE_BOOLEAN, &toAppend))
242     {
243         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
244         exit(1);
245     }
246 }
247
248 void DBUSMessageHandler::appendUInt(dbus_uint32_t toAppend)
249 {
250     if (!dbus_message_iter_append_basic(&m_MessageIter, DBUS_TYPE_UINT32, &toAppend))
251     {
252         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
253         exit(1);
254     }
255 }
256
257 void DBUSMessageHandler::appendDouble(double toAppend)
258 {
259     if (!dbus_message_iter_append_basic(&m_MessageIter, DBUS_TYPE_DOUBLE, &toAppend))
260     {
261         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
262         exit(1);
263     }
264 }
265
266 void DBUSMessageHandler::appendByte(char toAppend)
267 {
268     if (!dbus_message_iter_append_basic(&m_MessageIter, DBUS_TYPE_BYTE, &toAppend))
269     {
270         DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("DBUS handler Out Of Memory!"));
271         exit(1);
272     }
273 }
274
275 void DBUSMessageHandler::appendArrayOfUInt(unsigned int length, unsigned int *IDs)
276 {
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++)
280     {
281         dbus_message_iter_append_basic(&arrayIter, DBUS_TYPE_UINT32, &IDs[i]);
282     }
283     dbus_message_iter_close_container(&m_MessageIter, &arrayIter);
284 }
285
286 void DBUSMessageHandler::sendSignal(const char* name, const char* signal) {
287         dbus_uint32_t serial = 0;
288         DBusMessage* msg;
289         DBusMessageIter args;
290
291         msg = dbus_message_new_signal(DBUS_SERVICE_PREFIX, signal, name);
292
293         if (NULL == msg)
294         {
295                 DLT_LOG(DBusPlugin,DLT_LOG_ERROR, DLT_STRING("Message null!"));
296                 this->~DBUSMessageHandler();
297         }
298
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();
302         }
303
304         dbus_connection_flush(m_pConnection);
305
306         // free the message
307         dbus_message_unref(msg);
308 }