Skip to content

Latest commit

 

History

History
548 lines (493 loc) · 16.9 KB

usb_moded-dbus.c

File metadata and controls

548 lines (493 loc) · 16.9 KB
 
Mar 22, 2011
Mar 22, 2011
1
2
3
4
/**
@file usb_moded-dbus.c
Copyright (C) 2010 Nokia Corporation. All rights reserved.
Apr 21, 2015
Apr 21, 2015
5
Copyright (C) 2012-2015 Jolla. All rights reserved.
Mar 22, 2011
Mar 22, 2011
6
7
@author: Philippe De Swert <philippe.de-swert@nokia.com>
Dec 6, 2012
Dec 6, 2012
8
@author: Philippe De Swert <philippe.deswert@jollamobile.com>
Mar 22, 2011
Mar 22, 2011
9
10
This program is free software; you can redistribute it and/or
Dec 15, 2015
Dec 15, 2015
11
12
modify it under the terms of the Lesser GNU General Public License
version 2 as published by the Free Software Foundation.
Mar 22, 2011
Mar 22, 2011
13
14
15
16
17
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
Dec 15, 2015
Dec 15, 2015
18
Mar 22, 2011
Mar 22, 2011
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
You should have received a copy of the Lesser GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "usb_moded-dbus.h"
#include "usb_moded-dbus-private.h"
#include "usb_moded.h"
#include "usb_moded-modes.h"
Mar 9, 2015
Mar 9, 2015
36
#include "usb_moded-modesetting.h"
May 26, 2015
May 26, 2015
37
#include "usb_moded-config.h"
Sep 11, 2011
Sep 11, 2011
38
#include "usb_moded-config-private.h"
Oct 24, 2013
Oct 24, 2013
39
#include "usb_moded-network.h"
Mar 22, 2011
Mar 22, 2011
40
41
#include "usb_moded-log.h"
Oct 18, 2016
Oct 18, 2016
42
43
44
45
#define INIT_DONE_INTERFACE "com.nokia.startup.signal"
#define INIT_DONE_SIGNAL "init_done"
#define INIT_DONE_MATCH "type='signal',interface='"INIT_DONE_INTERFACE"',member='"INIT_DONE_SIGNAL"'"
Mar 22, 2011
Mar 22, 2011
46
static DBusConnection *dbus_connection_sys = NULL;
Nov 26, 2012
Nov 26, 2012
47
extern gboolean rescue_mode;
Mar 22, 2011
Mar 22, 2011
48
May 26, 2015
May 26, 2015
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Issues "sig_usb_config_ind" signal.
*/
static void usb_moded_send_config_signal(const char *section, const char *key, const char *value)
{
log_debug(USB_MODE_CONFIG_SIGNAL_NAME ": %s %s %s\n", section, key, value);
if (dbus_connection_sys)
{
DBusMessage* msg = dbus_message_new_signal(USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_CONFIG_SIGNAL_NAME);
if (msg) {
dbus_message_append_args(msg, DBUS_TYPE_STRING, &section,
DBUS_TYPE_STRING, &key,
DBUS_TYPE_STRING, &value,
DBUS_TYPE_INVALID);
dbus_connection_send(dbus_connection_sys, msg, NULL);
dbus_message_unref(msg);
}
}
}
Mar 22, 2011
Mar 22, 2011
69
70
71
72
73
74
75
76
static DBusHandlerResult msg_handler(DBusConnection *const connection, DBusMessage *const msg, gpointer const user_data)
{
DBusHandlerResult status = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
DBusMessage *reply = 0;
const char *interface = dbus_message_get_interface(msg);
const char *member = dbus_message_get_member(msg);
const char *object = dbus_message_get_path(msg);
int type = dbus_message_get_type(msg);
Dec 15, 2015
Dec 15, 2015
77
const char *xml = "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" "
Apr 21, 2015
Apr 21, 2015
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
"\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node name=\"" USB_MODE_OBJECT "\">\n"
" <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
" <method name=\"Introspect\">\n"
" <arg name=\"xml\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" </interface>\n"
" <interface name=\"" USB_MODE_INTERFACE "\">\n"
" <method name=\"" USB_MODE_STATE_REQUEST "\">\n"
" <arg name=\"mode\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"" USB_MODE_STATE_SET "\">\n"
" <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"mode\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"" USB_MODE_CONFIG_SET "\">\n"
" <arg name=\"config\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"config\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"" USB_MODE_NETWORK_SET "\">\n"
" <arg name=\"key\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"value\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"key\" type=\"s\" direction=\"out\"/>\n"
" <arg name=\"value\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"" USB_MODE_NETWORK_GET "\">\n"
" <arg name=\"key\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"key\" type=\"s\" direction=\"out\"/>\n"
" <arg name=\"value\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"" USB_MODE_CONFIG_GET "\">\n"
" <arg name=\"mode\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"" USB_MODE_LIST "\">\n"
" <arg name=\"modes\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"" USB_MODE_RESCUE_OFF "\"/>\n"
" <signal name=\"" USB_MODE_SIGNAL_NAME "\">\n"
" <arg name=\"mode\" type=\"s\"/>\n"
" </signal>\n"
" <signal name=\"" USB_MODE_ERROR_SIGNAL_NAME "\">\n"
" <arg name=\"error\" type=\"s\"/>\n"
" </signal>\n"
" <signal name=\"" USB_MODE_SUPPORTED_MODES_SIGNAL_NAME "\">\n"
" <arg name=\"modes\" type=\"s\"/>\n"
" </signal>\n"
May 26, 2015
May 26, 2015
124
125
126
127
128
" <signal name=\"" USB_MODE_CONFIG_SIGNAL_NAME "\">\n"
" <arg name=\"section\" type=\"s\"/>\n"
" <arg name=\"key\" type=\"s\"/>\n"
" <arg name=\"value\" type=\"s\"/>\n"
" </signal>\n"
Apr 21, 2015
Apr 21, 2015
129
130
" </interface>\n"
"</node>\n";
Mar 22, 2011
Mar 22, 2011
131
132
133
134
135
(void)user_data;
if(!interface || !member || !object) goto EXIT;
Oct 18, 2016
Oct 18, 2016
136
137
138
139
140
141
142
143
144
145
146
147
if( type == DBUS_MESSAGE_TYPE_SIGNAL )
{
if( !strcmp(interface, INIT_DONE_INTERFACE) && !strcmp(member, INIT_DONE_SIGNAL) ) {
/* Auto-disable rescue mode when bootup is finished */
if( rescue_mode ) {
rescue_mode = FALSE;
log_debug("init done reached - rescue mode disabled");
}
}
goto EXIT;
}
Mar 22, 2011
Mar 22, 2011
148
149
if( type == DBUS_MESSAGE_TYPE_METHOD_CALL && !strcmp(interface, USB_MODE_INTERFACE) && !strcmp(object, USB_MODE_OBJECT))
{
Dec 15, 2015
Dec 15, 2015
150
151
152
153
status = DBUS_HANDLER_RESULT_HANDLED;
if(!strcmp(member, USB_MODE_STATE_REQUEST))
{
Oct 29, 2013
Oct 29, 2013
154
const char *mode = get_usb_mode();
Mar 22, 2011
Mar 22, 2011
155
Aug 26, 2014
Aug 26, 2014
156
157
/* To the outside we want to keep CHARGING and CHARGING_FALLBACK the same */
if(!strcmp(MODE_CHARGING_FALLBACK, mode))
Sep 16, 2014
Sep 16, 2014
158
mode = MODE_CHARGING;
Dec 15, 2015
Dec 15, 2015
159
160
161
162
163
164
165
166
167
168
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID);
}
else if(!strcmp(member, USB_MODE_STATE_SET))
{
char *use = 0;
DBusError err = DBUS_ERROR_INIT;
if(!dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &use, DBUS_TYPE_INVALID))
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
Mar 22, 2011
Mar 22, 2011
169
170
171
172
else
{
/* check if usb is connected, since it makes no sense to change mode if it isn't */
if(!get_usb_connection_state())
Jan 11, 2013
Jan 11, 2013
173
174
{
log_warning("USB not connected, not changing mode!\n");
Mar 22, 2011
Mar 22, 2011
175
goto error_reply;
Jan 11, 2013
Jan 11, 2013
176
}
May 10, 2011
May 10, 2011
177
178
179
/* check if the mode exists */
if(valid_mode(use))
goto error_reply;
Mar 22, 2011
Mar 22, 2011
180
181
/* do not change mode if the mode requested is the one already set */
if(strcmp(use, get_usb_mode()) != 0)
Mar 9, 2015
Mar 9, 2015
182
183
{
usb_moded_mode_cleanup(get_usb_module());
Mar 22, 2011
Mar 22, 2011
184
set_usb_mode(use);
Mar 9, 2015
Mar 9, 2015
185
}
Dec 15, 2015
Dec 15, 2015
186
187
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &use, DBUS_TYPE_INVALID);
May 10, 2011
May 10, 2011
188
else
Mar 22, 2011
Mar 22, 2011
189
error_reply:
Dec 15, 2015
Dec 15, 2015
190
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
Mar 22, 2011
Mar 22, 2011
191
192
193
194
195
}
dbus_error_free(&err);
}
else if(!strcmp(member, USB_MODE_CONFIG_SET))
{
Dec 15, 2015
Dec 15, 2015
196
197
char *config = 0;
DBusError err = DBUS_ERROR_INIT;
Mar 22, 2011
Mar 22, 2011
198
Dec 15, 2015
Dec 15, 2015
199
200
if(!dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID))
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
Mar 22, 2011
Mar 22, 2011
201
202
else
{
Oct 25, 2013
Oct 25, 2013
203
/* error checking is done when setting configuration */
May 26, 2015
May 26, 2015
204
205
206
207
int ret = set_mode_setting(config);
if (ret == SET_CONFIG_UPDATED)
usb_moded_send_config_signal(MODE_SETTING_ENTRY, MODE_SETTING_KEY, config);
if (SET_CONFIG_OK(ret))
Dec 15, 2015
Dec 15, 2015
208
{
Dec 15, 2015
Dec 15, 2015
209
210
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID);
Dec 15, 2015
Dec 15, 2015
211
212
}
else
Dec 15, 2015
Dec 15, 2015
213
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
Dec 15, 2015
Dec 15, 2015
214
}
Dec 15, 2015
Dec 15, 2015
215
dbus_error_free(&err);
Dec 15, 2015
Dec 15, 2015
216
217
218
}
else if(!strcmp(member, USB_MODE_HIDE))
{
Dec 15, 2015
Dec 15, 2015
219
220
char *config = 0;
DBusError err = DBUS_ERROR_INIT;
Dec 15, 2015
Dec 15, 2015
221
Dec 15, 2015
Dec 15, 2015
222
223
if(!dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID))
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
Dec 15, 2015
Dec 15, 2015
224
225
226
227
228
229
230
231
else
{
/* error checking is done when setting configuration */
int ret = set_hide_mode_setting(config);
if (ret == SET_CONFIG_UPDATED)
usb_moded_send_config_signal(MODE_SETTING_ENTRY, MODE_HIDE_KEY, config);
if (SET_CONFIG_OK(ret))
{
Dec 15, 2015
Dec 15, 2015
232
233
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID);
Dec 15, 2015
Dec 15, 2015
234
235
}
else
Dec 15, 2015
Dec 15, 2015
236
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
Dec 15, 2015
Dec 15, 2015
237
}
Dec 15, 2015
Dec 15, 2015
238
dbus_error_free(&err);
Dec 15, 2015
Dec 15, 2015
239
240
241
}
else if(!strcmp(member, USB_MODE_UNHIDE))
{
Dec 15, 2015
Dec 15, 2015
242
243
char *config = 0;
DBusError err = DBUS_ERROR_INIT;
Dec 15, 2015
Dec 15, 2015
244
Dec 15, 2015
Dec 15, 2015
245
246
if(!dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID))
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
Dec 15, 2015
Dec 15, 2015
247
248
249
250
251
252
253
else
{
/* error checking is done when setting configuration */
int ret = set_unhide_mode_setting(config);
if (ret == SET_CONFIG_UPDATED)
usb_moded_send_config_signal(MODE_SETTING_ENTRY, MODE_HIDE_KEY, config);
if (SET_CONFIG_OK(ret))
Mar 22, 2011
Mar 22, 2011
254
{
Dec 15, 2015
Dec 15, 2015
255
256
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID);
Mar 22, 2011
Mar 22, 2011
257
258
}
else
Dec 15, 2015
Dec 15, 2015
259
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
Nov 19, 2012
Nov 19, 2012
260
}
Dec 15, 2015
Dec 15, 2015
261
dbus_error_free(&err);
Nov 19, 2012
Nov 19, 2012
262
}
Apr 4, 2016
Apr 4, 2016
263
264
265
266
else if(!strcmp(member, USB_MODE_HIDDEN_GET))
{
char *config = get_hidden_modes();
if(!config)
Jul 7, 2016
Jul 7, 2016
267
config = g_strdup("");
Apr 4, 2016
Apr 4, 2016
268
269
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID);
Jul 7, 2016
Jul 7, 2016
270
g_free(config);
Apr 4, 2016
Apr 4, 2016
271
}
Nov 19, 2012
Nov 19, 2012
272
273
else if(!strcmp(member, USB_MODE_NETWORK_SET))
{
Dec 15, 2015
Dec 15, 2015
274
275
char *config = 0, *setting = 0;
DBusError err = DBUS_ERROR_INIT;
Nov 19, 2012
Nov 19, 2012
276
Dec 15, 2015
Dec 15, 2015
277
278
if(!dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &config, DBUS_TYPE_STRING, &setting, DBUS_TYPE_INVALID))
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
Nov 19, 2012
Nov 19, 2012
279
280
else
{
Aug 31, 2013
Aug 31, 2013
281
/* error checking is done when setting configuration */
May 26, 2015
May 26, 2015
282
283
284
285
int ret = set_network_setting(config, setting);
if (ret == SET_CONFIG_UPDATED)
usb_moded_send_config_signal(NETWORK_ENTRY, config, setting);
if (SET_CONFIG_OK(ret))
Nov 19, 2012
Nov 19, 2012
286
{
Dec 15, 2015
Dec 15, 2015
287
288
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &config, DBUS_TYPE_STRING, &setting, DBUS_TYPE_INVALID);
Oct 24, 2013
Oct 24, 2013
289
usb_network_update();
Nov 19, 2012
Nov 19, 2012
290
291
}
else
Dec 15, 2015
Dec 15, 2015
292
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
Mar 22, 2011
Mar 22, 2011
293
}
Dec 15, 2015
Dec 15, 2015
294
dbus_error_free(&err);
Mar 22, 2011
Mar 22, 2011
295
}
Aug 31, 2013
Aug 31, 2013
296
297
298
else if(!strcmp(member, USB_MODE_NETWORK_GET))
{
char *config = 0;
Nov 7, 2014
Nov 7, 2014
299
char *setting = 0;
Aug 31, 2013
Aug 31, 2013
300
301
302
303
304
305
306
307
308
309
310
311
312
DBusError err = DBUS_ERROR_INIT;
if(!dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID))
{
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
}
else
{
setting = get_network_setting(config);
if(setting)
{
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &config, DBUS_TYPE_STRING, &setting, DBUS_TYPE_INVALID);
Jul 7, 2016
Jul 7, 2016
313
free(setting);
Aug 31, 2013
Aug 31, 2013
314
315
316
317
318
}
else
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
}
}
Feb 15, 2013
Feb 15, 2013
319
320
else if(!strcmp(member, USB_MODE_CONFIG_GET))
{
Nov 7, 2014
Nov 7, 2014
321
char *config = get_mode_setting();
Dec 15, 2015
Dec 15, 2015
322
Apr 29, 2016
Apr 29, 2016
323
324
325
326
327
328
329
if(!config)
{
/* Config is corrupted or we do not have a mode
* configured, fallback to undefined. */
config = g_strdup(MODE_UNDEFINED);
}
Dec 15, 2015
Dec 15, 2015
330
331
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID);
Apr 29, 2016
Apr 29, 2016
332
g_free(config);
Feb 15, 2013
Feb 15, 2013
333
}
Mar 4, 2013
Mar 4, 2013
334
335
else if(!strcmp(member, USB_MODE_LIST))
{
May 27, 2013
May 27, 2013
336
gchar *mode_list = get_mode_list();
Mar 4, 2013
Mar 4, 2013
337
338
if((reply = dbus_message_new_method_return(msg)))
May 27, 2013
May 27, 2013
339
340
dbus_message_append_args (reply, DBUS_TYPE_STRING, (const char *) &mode_list, DBUS_TYPE_INVALID);
g_free(mode_list);
Mar 4, 2013
Mar 4, 2013
341
}
Nov 26, 2012
Nov 26, 2012
342
343
344
else if(!strcmp(member, USB_MODE_RESCUE_OFF))
{
rescue_mode = FALSE;
Oct 10, 2013
Oct 10, 2013
345
log_debug("Rescue mode off\n ");
Nov 26, 2012
Nov 26, 2012
346
347
reply = dbus_message_new_method_return(msg);
}
Dec 15, 2015
Dec 15, 2015
348
349
350
351
352
353
354
355
356
357
else
{
/*unknown methods are handled here */
reply = dbus_message_new_error(msg, DBUS_ERROR_UNKNOWN_METHOD, member);
}
if( !reply )
{
reply = dbus_message_new_error(msg, DBUS_ERROR_FAILED, member);
}
Mar 22, 2011
Mar 22, 2011
358
359
360
361
}
else if( type == DBUS_MESSAGE_TYPE_METHOD_CALL && !strcmp(interface, "org.freedesktop.DBus.Introspectable") &&
!strcmp(object, USB_MODE_OBJECT) && !strcmp(member, "Introspect"))
{
Dec 15, 2015
Dec 15, 2015
362
status = DBUS_HANDLER_RESULT_HANDLED;
Mar 22, 2011
Mar 22, 2011
363
Dec 15, 2015
Dec 15, 2015
364
if((reply = dbus_message_new_method_return(msg)))
Mar 22, 2011
Mar 22, 2011
365
366
367
368
369
370
371
372
dbus_message_append_args (reply, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID);
}
EXIT:
if(reply)
{
Dec 15, 2015
Dec 15, 2015
373
374
375
376
377
378
if( !dbus_message_get_no_reply(msg) )
{
if( !dbus_connection_send(connection, reply, 0) )
log_debug("Failed sending reply. Out Of Memory!\n");
}
dbus_message_unref(reply);
Mar 22, 2011
Mar 22, 2011
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
}
return status;
}
/**
* Init dbus for usb_moded
*
* @return TRUE when everything went ok
*/
gboolean usb_moded_dbus_init(void)
{
gboolean status = FALSE;
DBusError error;
int ret;
dbus_error_init(&error);
/* connect to system bus */
Dec 15, 2015
Dec 15, 2015
398
if ((dbus_connection_sys = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == NULL)
Mar 22, 2011
Mar 22, 2011
399
{
Dec 15, 2015
Dec 15, 2015
400
log_debug("Failed to open connection to system message bus; %s\n", error.message);
Mar 22, 2011
Mar 22, 2011
401
402
403
404
goto EXIT;
}
/* Initialise message handlers */
Dec 15, 2015
Dec 15, 2015
405
406
if (!dbus_connection_add_filter(dbus_connection_sys, msg_handler, NULL, NULL))
goto EXIT;
Mar 22, 2011
Mar 22, 2011
407
408
409
/* Acquire D-Bus service */
ret = dbus_bus_request_name(dbus_connection_sys, USB_MODE_SERVICE, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
Dec 15, 2015
Dec 15, 2015
410
if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
Mar 22, 2011
Mar 22, 2011
411
412
413
414
415
416
417
418
419
420
421
{
log_debug("failed claiming dbus name\n");
if( dbus_error_is_set(&error) )
log_debug("DBUS ERROR: %s, %s \n", error.name, error.message);
goto EXIT;
}
/* only match on signals/methods we support (if needed)
dbus_bus_add_match(dbus_connection_sys, USB_MODE_INTERFACE, &error);
*/
Oct 18, 2016
Oct 18, 2016
422
423
424
/* Listen to init-done signals */
dbus_bus_add_match(dbus_connection_sys, INIT_DONE_MATCH, 0);
Apr 20, 2011
Apr 20, 2011
425
426
dbus_threads_init_default();
Mar 22, 2011
Mar 22, 2011
427
428
429
430
431
432
/* Connect D-Bus to the mainloop */
dbus_connection_setup_with_g_main(dbus_connection_sys, NULL);
/* everything went fine */
status = TRUE;
Dec 15, 2015
Dec 15, 2015
433
EXIT:
Mar 22, 2011
Mar 22, 2011
434
435
436
437
438
439
440
441
442
443
444
dbus_error_free(&error);
return status;
}
/**
* Clean up the dbus connections on exit
*
*/
void usb_moded_dbus_cleanup(void)
{
/* clean up system bus connection */
Dec 15, 2015
Dec 15, 2015
445
if (dbus_connection_sys != NULL)
Mar 22, 2011
Mar 22, 2011
446
{
Oct 22, 2013
Oct 22, 2013
447
dbus_bus_release_name(dbus_connection_sys, USB_MODE_SERVICE, NULL);
Mar 22, 2011
Mar 22, 2011
448
449
450
451
452
453
454
dbus_connection_remove_filter(dbus_connection_sys, msg_handler, NULL);
dbus_connection_unref(dbus_connection_sys);
dbus_connection_sys = NULL;
}
}
/**
Mar 18, 2014
Mar 18, 2014
455
* Helper function for sending the different signals
Mar 22, 2011
Mar 22, 2011
456
*
Mar 21, 2014
Mar 21, 2014
457
* @return 0 on success, 1 on failure
Mar 18, 2014
Mar 18, 2014
458
459
* @param signal_type the type of signal (normal, error, ...)
* @@param content string which can be mode name, error, list of modes, ...
Mar 22, 2011
Mar 22, 2011
460
*/
Mar 18, 2014
Mar 18, 2014
461
static int usb_moded_dbus_signal(const char *signal_type, const char *content)
Mar 22, 2011
Mar 22, 2011
462
463
464
465
{
int result = 1;
DBusMessage* msg = 0;
May 22, 2014
May 22, 2014
466
467
468
469
470
if(!dbus_connection_sys)
{
log_err("Dbus system connection broken!\n");
goto EXIT;
}
Dec 15, 2015
Dec 15, 2015
471
// create a signal and check for errors
Mar 18, 2014
Mar 18, 2014
472
msg = dbus_message_new_signal(USB_MODE_OBJECT, USB_MODE_INTERFACE, signal_type );
Dec 15, 2015
Dec 15, 2015
473
474
475
if (NULL == msg)
{
log_debug("Message Null\n");
Mar 22, 2011
Mar 22, 2011
476
477
478
479
goto EXIT;
}
// append arguments onto signal
Dec 15, 2015
Dec 15, 2015
480
481
482
if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &content, DBUS_TYPE_INVALID))
{
log_debug("Appending arguments failed. Out Of Memory!\n");
Mar 22, 2011
Mar 22, 2011
483
484
485
486
goto EXIT;
}
// send the message on the correct bus and flush the connection
Dec 15, 2015
Dec 15, 2015
487
if (!dbus_connection_send(dbus_connection_sys, msg, 0))
Mar 22, 2011
Mar 22, 2011
488
{
Dec 15, 2015
Dec 15, 2015
489
log_debug("Failed sending message. Out Of Memory!\n");
Mar 22, 2011
Mar 22, 2011
490
491
492
493
goto EXIT;
}
result = 0;
Dec 15, 2015
Dec 15, 2015
494
495
EXIT:
// free the message
Mar 22, 2011
Mar 22, 2011
496
497
498
499
500
501
if(msg != 0)
dbus_message_unref(msg);
return result;
}
Mar 18, 2014
Mar 18, 2014
502
503
504
/**
* Send regular usb_moded state signal
*
Mar 21, 2014
Mar 21, 2014
505
* @return 0 on success, 1 on failure
Mar 18, 2014
Mar 18, 2014
506
507
508
509
* @param state_ind the signal name
*
*/
int usb_moded_send_signal(const char *state_ind)
Mar 22, 2011
Mar 22, 2011
510
{
Mar 18, 2014
Mar 18, 2014
511
512
return(usb_moded_dbus_signal(USB_MODE_SIGNAL_NAME, state_ind));
}
Mar 22, 2011
Mar 22, 2011
513
Mar 18, 2014
Mar 18, 2014
514
515
516
/**
* Send regular usb_moded error signal
*
Mar 21, 2014
Mar 21, 2014
517
* @return 0 on success, 1 on failure
Sep 15, 2014
Sep 15, 2014
518
* @param error the error to be signalled
Mar 18, 2014
Mar 18, 2014
519
520
521
522
523
*
*/
int usb_moded_send_error_signal(const char *error)
{
return(usb_moded_dbus_signal(USB_MODE_ERROR_SIGNAL_NAME, error));
Mar 22, 2011
Mar 22, 2011
524
}
Mar 18, 2014
Mar 18, 2014
525
Mar 18, 2014
Mar 18, 2014
526
527
528
/**
* Send regular usb_moded mode list signal
*
Mar 21, 2014
Mar 21, 2014
529
* @return 0 on success, 1 on failure
Sep 15, 2014
Sep 15, 2014
530
* @param supported_modes list of supported modes
Mar 18, 2014
Mar 18, 2014
531
532
*
*/
Mar 18, 2014
Mar 18, 2014
533
534
int usb_moded_send_supported_modes_signal(const char *supported_modes)
{
Mar 18, 2014
Mar 18, 2014
535
return(usb_moded_dbus_signal(USB_MODE_SUPPORTED_MODES_SIGNAL_NAME, supported_modes));
Mar 18, 2014
Mar 18, 2014
536
}
Apr 4, 2016
Apr 4, 2016
537
538
539
540
541
542
543
544
545
546
547
548
/**
* Send regular usb_moded hidden mode list signal
*
* @return 0 on success, 1 on failure
* @param hidden_modes list of supported modes
*
*/
int usb_moded_send_hidden_modes_signal(const char *hidden_modes)
{
return(usb_moded_dbus_signal(USB_MODE_HIDDEN_MODES_SIGNAL_NAME, hidden_modes));
}