Skip to content

Latest commit

 

History

History
798 lines (709 loc) · 23.1 KB

usb_moded-dbus.c

File metadata and controls

798 lines (709 loc) · 23.1 KB
 
Mar 22, 2011
Mar 22, 2011
1
2
3
4
/**
@file usb_moded-dbus.c
Copyright (C) 2010 Nokia Corporation. All rights reserved.
Nov 7, 2016
Nov 7, 2016
5
Copyright (C) 2012-2016 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>
Nov 7, 2016
Nov 7, 2016
9
@author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
Mar 22, 2011
Mar 22, 2011
10
11
This program is free software; you can redistribute it and/or
Dec 15, 2015
Dec 15, 2015
12
13
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
14
15
16
17
18
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
19
Mar 22, 2011
Mar 22, 2011
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
37
#include "usb_moded-modesetting.h"
May 26, 2015
May 26, 2015
38
#include "usb_moded-config.h"
Sep 11, 2011
Sep 11, 2011
39
#include "usb_moded-config-private.h"
Oct 24, 2013
Oct 24, 2013
40
#include "usb_moded-network.h"
Mar 22, 2011
Mar 22, 2011
41
42
#include "usb_moded-log.h"
Oct 18, 2016
Oct 18, 2016
43
44
45
46
#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
47
static DBusConnection *dbus_connection_sys = NULL;
Nov 7, 2016
Nov 7, 2016
48
49
static gboolean have_service_name = FALSE;
Nov 26, 2012
Nov 26, 2012
50
extern gboolean rescue_mode;
Mar 22, 2011
Mar 22, 2011
51
May 26, 2015
May 26, 2015
52
53
54
55
56
/**
* Issues "sig_usb_config_ind" signal.
*/
static void usb_moded_send_config_signal(const char *section, const char *key, const char *value)
{
Aug 17, 2017
Aug 17, 2017
57
58
log_debug("broadcast signal %s(%s, %s, %s)\n", USB_MODE_CONFIG_SIGNAL_NAME, section, key, value);
Nov 7, 2016
Nov 7, 2016
59
60
61
62
63
64
if( !have_service_name )
{
log_err("config notification without service: [%s] %s=%s",
section, key, value);
}
else if (dbus_connection_sys)
May 26, 2015
May 26, 2015
65
66
67
68
69
70
71
72
73
74
75
76
77
{
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);
}
}
}
Nov 3, 2016
Nov 3, 2016
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/** Introspect xml format string for parents of USB_MODE_OBJECT */
static const char intospect_template[] =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node name=\"%s\">\n"
" <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
" <method name=\"Introspect\">\n"
" <arg direction=\"out\" name=\"data\" type=\"s\"/>\n"
" </method>\n"
" </interface>\n"
" <interface name=\"org.freedesktop.DBus.Peer\">\n"
" <method name=\"Ping\"/>\n"
" <method name=\"GetMachineId\">\n"
" <arg direction=\"out\" name=\"machine_uuid\" type=\"s\" />\n"
" </method>\n"
" </interface>\n"
" <node name=\"%s\"/>\n"
"</node>\n";
/** Introspect xml data for object path USB_MODE_OBJECT */
static const char introspect_usb_moded[] =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" "
"\"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=\"org.freedesktop.DBus.Peer\">\n"
" <method name=\"Ping\"/>\n"
" <method name=\"GetMachineId\">\n"
" <arg direction=\"out\" name=\"machine_uuid\" type=\"s\" />\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"
" <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"
" </interface>\n"
"</node>\n";
Mar 22, 2011
Mar 22, 2011
159
160
161
162
163
164
165
166
167
168
169
170
171
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);
(void)user_data;
if(!interface || !member || !object) goto EXIT;
Oct 18, 2016
Oct 18, 2016
172
173
174
175
176
177
178
179
180
181
182
183
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
184
185
if( type == DBUS_MESSAGE_TYPE_METHOD_CALL && !strcmp(interface, USB_MODE_INTERFACE) && !strcmp(object, USB_MODE_OBJECT))
{
Dec 15, 2015
Dec 15, 2015
186
187
188
189
status = DBUS_HANDLER_RESULT_HANDLED;
if(!strcmp(member, USB_MODE_STATE_REQUEST))
{
Oct 29, 2013
Oct 29, 2013
190
const char *mode = get_usb_mode();
Mar 22, 2011
Mar 22, 2011
191
Aug 26, 2014
Aug 26, 2014
192
193
/* 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
194
mode = MODE_CHARGING;
Dec 15, 2015
Dec 15, 2015
195
196
197
198
199
200
201
202
203
204
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
205
206
207
208
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
209
210
{
log_warning("USB not connected, not changing mode!\n");
Mar 22, 2011
Mar 22, 2011
211
goto error_reply;
Jan 11, 2013
Jan 11, 2013
212
}
May 10, 2011
May 10, 2011
213
214
215
/* check if the mode exists */
if(valid_mode(use))
goto error_reply;
Mar 22, 2011
Mar 22, 2011
216
217
/* 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
218
219
{
usb_moded_mode_cleanup(get_usb_module());
Mar 22, 2011
Mar 22, 2011
220
set_usb_mode(use);
Mar 9, 2015
Mar 9, 2015
221
}
Dec 15, 2015
Dec 15, 2015
222
223
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
224
else
Mar 22, 2011
Mar 22, 2011
225
error_reply:
Dec 15, 2015
Dec 15, 2015
226
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
Mar 22, 2011
Mar 22, 2011
227
228
229
230
231
}
dbus_error_free(&err);
}
else if(!strcmp(member, USB_MODE_CONFIG_SET))
{
Dec 15, 2015
Dec 15, 2015
232
233
char *config = 0;
DBusError err = DBUS_ERROR_INIT;
Mar 22, 2011
Mar 22, 2011
234
Dec 15, 2015
Dec 15, 2015
235
236
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
237
238
else
{
Oct 25, 2013
Oct 25, 2013
239
/* error checking is done when setting configuration */
May 26, 2015
May 26, 2015
240
241
242
243
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
244
{
Dec 15, 2015
Dec 15, 2015
245
246
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
247
248
}
else
Dec 15, 2015
Dec 15, 2015
249
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
Dec 15, 2015
Dec 15, 2015
250
}
Dec 15, 2015
Dec 15, 2015
251
dbus_error_free(&err);
Dec 15, 2015
Dec 15, 2015
252
253
254
}
else if(!strcmp(member, USB_MODE_HIDE))
{
Dec 15, 2015
Dec 15, 2015
255
256
char *config = 0;
DBusError err = DBUS_ERROR_INIT;
Dec 15, 2015
Dec 15, 2015
257
Dec 15, 2015
Dec 15, 2015
258
259
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
260
261
262
263
264
265
266
267
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
268
269
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
270
271
}
else
Dec 15, 2015
Dec 15, 2015
272
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
Dec 15, 2015
Dec 15, 2015
273
}
Dec 15, 2015
Dec 15, 2015
274
dbus_error_free(&err);
Dec 15, 2015
Dec 15, 2015
275
276
277
}
else if(!strcmp(member, USB_MODE_UNHIDE))
{
Dec 15, 2015
Dec 15, 2015
278
279
char *config = 0;
DBusError err = DBUS_ERROR_INIT;
Dec 15, 2015
Dec 15, 2015
280
Dec 15, 2015
Dec 15, 2015
281
282
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
283
284
285
286
287
288
289
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
290
{
Dec 15, 2015
Dec 15, 2015
291
292
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
293
294
}
else
Dec 15, 2015
Dec 15, 2015
295
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
Nov 19, 2012
Nov 19, 2012
296
}
Dec 15, 2015
Dec 15, 2015
297
dbus_error_free(&err);
Nov 19, 2012
Nov 19, 2012
298
}
Apr 4, 2016
Apr 4, 2016
299
300
301
302
else if(!strcmp(member, USB_MODE_HIDDEN_GET))
{
char *config = get_hidden_modes();
if(!config)
Jul 7, 2016
Jul 7, 2016
303
config = g_strdup("");
Apr 4, 2016
Apr 4, 2016
304
305
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
306
g_free(config);
Apr 4, 2016
Apr 4, 2016
307
}
Nov 19, 2012
Nov 19, 2012
308
309
else if(!strcmp(member, USB_MODE_NETWORK_SET))
{
Dec 15, 2015
Dec 15, 2015
310
311
char *config = 0, *setting = 0;
DBusError err = DBUS_ERROR_INIT;
Nov 19, 2012
Nov 19, 2012
312
Dec 15, 2015
Dec 15, 2015
313
314
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
315
316
else
{
Aug 31, 2013
Aug 31, 2013
317
/* error checking is done when setting configuration */
May 26, 2015
May 26, 2015
318
319
320
321
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
322
{
Dec 15, 2015
Dec 15, 2015
323
324
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
325
usb_network_update();
Nov 19, 2012
Nov 19, 2012
326
327
}
else
Dec 15, 2015
Dec 15, 2015
328
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
Mar 22, 2011
Mar 22, 2011
329
}
Dec 15, 2015
Dec 15, 2015
330
dbus_error_free(&err);
Mar 22, 2011
Mar 22, 2011
331
}
Aug 31, 2013
Aug 31, 2013
332
333
334
else if(!strcmp(member, USB_MODE_NETWORK_GET))
{
char *config = 0;
Nov 7, 2014
Nov 7, 2014
335
char *setting = 0;
Aug 31, 2013
Aug 31, 2013
336
337
338
339
340
341
342
343
344
345
346
347
348
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
349
free(setting);
Aug 31, 2013
Aug 31, 2013
350
351
352
353
354
}
else
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, config);
}
}
Feb 15, 2013
Feb 15, 2013
355
356
else if(!strcmp(member, USB_MODE_CONFIG_GET))
{
Nov 7, 2014
Nov 7, 2014
357
char *config = get_mode_setting();
Dec 15, 2015
Dec 15, 2015
358
Apr 29, 2016
Apr 29, 2016
359
360
361
362
363
364
365
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
366
367
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
368
g_free(config);
Feb 15, 2013
Feb 15, 2013
369
}
Mar 4, 2013
Mar 4, 2013
370
371
else if(!strcmp(member, USB_MODE_LIST))
{
May 27, 2013
May 27, 2013
372
gchar *mode_list = get_mode_list();
Mar 4, 2013
Mar 4, 2013
373
374
if((reply = dbus_message_new_method_return(msg)))
May 27, 2013
May 27, 2013
375
376
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
377
}
Nov 26, 2012
Nov 26, 2012
378
379
380
else if(!strcmp(member, USB_MODE_RESCUE_OFF))
{
rescue_mode = FALSE;
Oct 10, 2013
Oct 10, 2013
381
log_debug("Rescue mode off\n ");
Nov 26, 2012
Nov 26, 2012
382
383
reply = dbus_message_new_method_return(msg);
}
Dec 15, 2015
Dec 15, 2015
384
385
386
387
388
389
390
391
392
393
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
394
}
Nov 3, 2016
Nov 3, 2016
395
396
397
else if( type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
!strcmp(interface, "org.freedesktop.DBus.Introspectable") &&
!strcmp(member, "Introspect"))
Mar 22, 2011
Mar 22, 2011
398
{
Nov 3, 2016
Nov 3, 2016
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
const gchar *xml = 0;
gchar *tmp = 0;
gchar *err = 0;
size_t len = strlen(object);
const char *pos = USB_MODE_OBJECT;
if( !strncmp(object, pos, len) )
{
if( pos[len] == 0 )
{
/* Full length USB_MODE_OBJECT requested */
xml = introspect_usb_moded;
}
else if( pos[len] == '/' )
{
/* Leading part of USB_MODE_OBJECT requested */
gchar *parent = 0;
gchar *child = 0;
parent = g_strndup(pos, len);
pos += len + 1;
len = strcspn(pos, "/");
child = g_strndup(pos, len);
xml = tmp = g_strdup_printf(intospect_template,
parent, child);
g_free(child);
g_free(parent);
}
else if( !strcmp(object, "/") )
{
/* Root object needs to be handled separately */
const char *parent = "/";
gchar *child = 0;
pos += 1;
len = strcspn(pos, "/");
child = g_strndup(pos, len);
xml = tmp = g_strdup_printf(intospect_template,
parent, child);
g_free(child);
}
}
Mar 22, 2011
Mar 22, 2011
439
Nov 3, 2016
Nov 3, 2016
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
if( xml )
{
if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply,
DBUS_TYPE_STRING, &xml,
DBUS_TYPE_INVALID);
}
else
{
err = g_strdup_printf("Object '%s' does not exist", object);
reply = dbus_message_new_error(msg, DBUS_ERROR_UNKNOWN_OBJECT,
err);
}
g_free(err);
g_free(tmp);
if( reply )
{
status = DBUS_HANDLER_RESULT_HANDLED;
}
Mar 22, 2011
Mar 22, 2011
461
462
463
464
465
466
}
EXIT:
if(reply)
{
Dec 15, 2015
Dec 15, 2015
467
468
469
470
471
472
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
473
474
475
476
477
}
return status;
}
Nov 7, 2016
Nov 7, 2016
478
479
480
481
482
483
484
485
486
487
DBusConnection *usb_moded_dbus_get_connection(void)
{
DBusConnection *connection = 0;
if( dbus_connection_sys )
connection = dbus_connection_ref(dbus_connection_sys);
else
log_err("something asked for connection ref while unconnected");
return connection;
}
Mar 22, 2011
Mar 22, 2011
488
/**
Nov 7, 2016
Nov 7, 2016
489
* Establish D-Bus SystemBus connection
Mar 22, 2011
Mar 22, 2011
490
491
492
*
* @return TRUE when everything went ok
*/
Nov 7, 2016
Nov 7, 2016
493
gboolean usb_moded_dbus_init_connection(void)
Mar 22, 2011
Mar 22, 2011
494
495
{
gboolean status = FALSE;
Nov 7, 2016
Nov 7, 2016
496
DBusError error = DBUS_ERROR_INIT;
Mar 22, 2011
Mar 22, 2011
497
498
/* connect to system bus */
Dec 15, 2015
Dec 15, 2015
499
if ((dbus_connection_sys = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == NULL)
Mar 22, 2011
Mar 22, 2011
500
{
Dec 15, 2015
Dec 15, 2015
501
log_debug("Failed to open connection to system message bus; %s\n", error.message);
Mar 22, 2011
Mar 22, 2011
502
503
504
505
goto EXIT;
}
/* Initialise message handlers */
Dec 15, 2015
Dec 15, 2015
506
507
if (!dbus_connection_add_filter(dbus_connection_sys, msg_handler, NULL, NULL))
goto EXIT;
Mar 22, 2011
Mar 22, 2011
508
Nov 7, 2016
Nov 7, 2016
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* Listen to init-done signals */
dbus_bus_add_match(dbus_connection_sys, INIT_DONE_MATCH, 0);
/* Connect D-Bus to the mainloop */
dbus_connection_setup_with_g_main(dbus_connection_sys, NULL);
/* everything went fine */
status = TRUE;
EXIT:
dbus_error_free(&error);
return status;
}
/**
* Reserve "com.meego.usb_moded" D-Bus Service Name
*
* @return TRUE when everything went ok
*/
gboolean usb_moded_dbus_init_service(void)
{
gboolean status = FALSE;
DBusError error = DBUS_ERROR_INIT;
int ret;
if( !dbus_connection_sys ) {
goto EXIT;
}
Mar 22, 2011
Mar 22, 2011
538
539
/* 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
540
if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
Mar 22, 2011
Mar 22, 2011
541
542
543
544
545
546
{
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;
}
Nov 7, 2016
Nov 7, 2016
547
548
log_debug("claimed name %s", USB_MODE_SERVICE);
have_service_name = TRUE;
Mar 22, 2011
Mar 22, 2011
549
550
551
/* everything went fine */
status = TRUE;
Dec 15, 2015
Dec 15, 2015
552
EXIT:
Mar 22, 2011
Mar 22, 2011
553
554
555
556
dbus_error_free(&error);
return status;
}
Nov 7, 2016
Nov 7, 2016
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/** Release "com.meego.usb_moded" D-Bus Service Name
*/
static void usb_moded_dbus_cleanup_service(void)
{
if( !have_service_name )
goto EXIT;
have_service_name = FALSE;
log_debug("release name %s", USB_MODE_SERVICE);
if( dbus_connection_sys &&
dbus_connection_get_is_connected(dbus_connection_sys) )
{
dbus_bus_release_name(dbus_connection_sys, USB_MODE_SERVICE, NULL);
}
EXIT:
return;
}
Mar 22, 2011
Mar 22, 2011
577
578
579
580
581
582
/**
* Clean up the dbus connections on exit
*
*/
void usb_moded_dbus_cleanup(void)
{
Nov 7, 2016
Nov 7, 2016
583
584
585
586
587
588
589
590
591
592
/* clean up system bus connection */
if (dbus_connection_sys != NULL)
{
usb_moded_dbus_cleanup_service();
dbus_connection_remove_filter(dbus_connection_sys, msg_handler, NULL);
dbus_connection_unref(dbus_connection_sys),
dbus_connection_sys = NULL;
}
Mar 22, 2011
Mar 22, 2011
593
594
595
}
/**
Mar 18, 2014
Mar 18, 2014
596
* Helper function for sending the different signals
Mar 22, 2011
Mar 22, 2011
597
*
Mar 21, 2014
Mar 21, 2014
598
* @return 0 on success, 1 on failure
Mar 18, 2014
Mar 18, 2014
599
600
* @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
601
*/
Mar 18, 2014
Mar 18, 2014
602
static int usb_moded_dbus_signal(const char *signal_type, const char *content)
Mar 22, 2011
Mar 22, 2011
603
604
605
606
{
int result = 1;
DBusMessage* msg = 0;
Aug 17, 2017
Aug 17, 2017
607
608
log_debug("broadcast signal %s(%s)\n", signal_type, content);
Nov 7, 2016
Nov 7, 2016
609
610
611
612
613
614
if( !have_service_name )
{
log_err("sending signal without service: %s(%s)",
signal_type, content);
goto EXIT;
}
May 22, 2014
May 22, 2014
615
616
617
618
619
if(!dbus_connection_sys)
{
log_err("Dbus system connection broken!\n");
goto EXIT;
}
Dec 15, 2015
Dec 15, 2015
620
// create a signal and check for errors
Mar 18, 2014
Mar 18, 2014
621
msg = dbus_message_new_signal(USB_MODE_OBJECT, USB_MODE_INTERFACE, signal_type );
Dec 15, 2015
Dec 15, 2015
622
623
624
if (NULL == msg)
{
log_debug("Message Null\n");
Mar 22, 2011
Mar 22, 2011
625
626
627
628
goto EXIT;
}
// append arguments onto signal
Dec 15, 2015
Dec 15, 2015
629
630
631
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
632
633
634
635
goto EXIT;
}
// send the message on the correct bus and flush the connection
Dec 15, 2015
Dec 15, 2015
636
if (!dbus_connection_send(dbus_connection_sys, msg, 0))
Mar 22, 2011
Mar 22, 2011
637
{
Dec 15, 2015
Dec 15, 2015
638
log_debug("Failed sending message. Out Of Memory!\n");
Mar 22, 2011
Mar 22, 2011
639
640
641
642
goto EXIT;
}
result = 0;
Dec 15, 2015
Dec 15, 2015
643
644
EXIT:
// free the message
Mar 22, 2011
Mar 22, 2011
645
646
647
648
649
650
if(msg != 0)
dbus_message_unref(msg);
return result;
}
Mar 18, 2014
Mar 18, 2014
651
652
653
/**
* Send regular usb_moded state signal
*
Mar 21, 2014
Mar 21, 2014
654
* @return 0 on success, 1 on failure
Mar 18, 2014
Mar 18, 2014
655
656
657
658
* @param state_ind the signal name
*
*/
int usb_moded_send_signal(const char *state_ind)
Mar 22, 2011
Mar 22, 2011
659
{
Mar 18, 2014
Mar 18, 2014
660
661
return(usb_moded_dbus_signal(USB_MODE_SIGNAL_NAME, state_ind));
}
Mar 22, 2011
Mar 22, 2011
662
Mar 18, 2014
Mar 18, 2014
663
664
665
/**
* Send regular usb_moded error signal
*
Mar 21, 2014
Mar 21, 2014
666
* @return 0 on success, 1 on failure
Sep 15, 2014
Sep 15, 2014
667
* @param error the error to be signalled
Mar 18, 2014
Mar 18, 2014
668
669
670
671
672
*
*/
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
673
}
Mar 18, 2014
Mar 18, 2014
674
Mar 18, 2014
Mar 18, 2014
675
676
677
/**
* Send regular usb_moded mode list signal
*
Mar 21, 2014
Mar 21, 2014
678
* @return 0 on success, 1 on failure
Sep 15, 2014
Sep 15, 2014
679
* @param supported_modes list of supported modes
Mar 18, 2014
Mar 18, 2014
680
681
*
*/
Mar 18, 2014
Mar 18, 2014
682
683
int usb_moded_send_supported_modes_signal(const char *supported_modes)
{
Mar 18, 2014
Mar 18, 2014
684
return(usb_moded_dbus_signal(USB_MODE_SUPPORTED_MODES_SIGNAL_NAME, supported_modes));
Mar 18, 2014
Mar 18, 2014
685
}
Apr 4, 2016
Apr 4, 2016
686
687
688
689
690
691
692
693
694
695
696
697
/**
* 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));
}
Nov 7, 2016
Nov 7, 2016
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/** Async reply handler for usb_moded_get_name_owner_async()
*
* @param pc Pending call object pointer
* @param aptr Notify function to call (as a void pointer)
*/
static void usb_moded_get_name_owner_cb(DBusPendingCall *pc, void *aptr)
{
usb_moded_get_name_owner_fn cb = aptr;
DBusMessage *rsp = 0;
const char *dta = 0;
DBusError err = DBUS_ERROR_INIT;
if( !(rsp = dbus_pending_call_steal_reply(pc)) ) {
log_err("did not get reply");
goto EXIT;
}
if( dbus_set_error_from_message(&err, rsp) )
{
if( strcmp(err.name, DBUS_ERROR_NAME_HAS_NO_OWNER) )
log_err("error reply: %s: %s", err.name, err.message);
goto EXIT;
}
if( !dbus_message_get_args(rsp, &err,
DBUS_TYPE_STRING, &dta,
DBUS_TYPE_INVALID) )
{
if( strcmp(err.name, DBUS_ERROR_NAME_HAS_NO_OWNER) )
Nov 7, 2016
Nov 7, 2016
729
log_err("parse error: %s: %s", err.name, err.message);
Nov 7, 2016
Nov 7, 2016
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
goto EXIT;
}
EXIT:
/* Allways call the notification function. Equate any error
* situations with "service does not have an owner". */
cb(dta ?: "");
if( rsp ) dbus_message_unref(rsp);
dbus_error_free(&err);
}
/** Helper function for making async dbus name owner queries
*
* @param name D-Bus name to query
* @param cb Function to call when async reply is received
* @param ppc Where to store pending call object, or NULL
*
* @return TRUE if method call was sent, FALSE otherwise
*/
gboolean usb_moded_get_name_owner_async(const char *name,
usb_moded_get_name_owner_fn cb,
DBusPendingCall **ppc)
{
gboolean ack = FALSE;
DBusMessage *req = 0;
DBusPendingCall *pc = 0;
if(!dbus_connection_sys)
goto EXIT;
req = dbus_message_new_method_call(DBUS_INTERFACE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
DBUS_GET_NAME_OWNER_REQ);
if( !req ) {
log_err("could not create method call message");
goto EXIT;
}
if( !dbus_message_append_args(req,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID) ) {
log_err("could not add method call parameters");
goto EXIT;
}
if( !dbus_connection_send_with_reply(dbus_connection_sys, req, &pc, -1) )
goto EXIT;
if( !pc )
goto EXIT;
if( !dbus_pending_call_set_notify(pc, usb_moded_get_name_owner_cb, cb, 0) )
goto EXIT;
ack = TRUE;
if( ppc )
*ppc = pc, pc = 0;
EXIT:
if( pc ) dbus_pending_call_unref(pc);
if( req ) dbus_message_unref(req);
return ack;
}