File: maemo6.c

package info (click to toggle)
bluez 4.99-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,572 kB
  • sloc: ansic: 123,574; sh: 10,588; python: 2,183; makefile: 432; xml: 126; lex: 120; yacc: 119
file content (284 lines) | stat: -rw-r--r-- 7,273 bytes parent folder | download
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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2006-2010  Nokia Corporation
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the 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
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <dbus/dbus.h>

#include "adapter.h"
#include "plugin.h"
#include "log.h"
#include "gdbus.h"

/* from mce/mode-names.h */
#define MCE_RADIO_STATE_BLUETOOTH	(1 << 3)

/* from mce/dbus-names.h */
#define MCE_SERVICE			"com.nokia.mce"
#define MCE_REQUEST_IF			"com.nokia.mce.request"
#define MCE_SIGNAL_IF			"com.nokia.mce.signal"
#define MCE_REQUEST_PATH		"/com/nokia/mce/request"
#define MCE_SIGNAL_PATH			"/com/nokia/mce/signal"
#define MCE_RADIO_STATES_CHANGE_REQ	"req_radio_states_change"
#define MCE_RADIO_STATES_GET		"get_radio_states"
#define MCE_RADIO_STATES_SIG		"radio_states_ind"
#define MCE_TKLOCK_MODE_SIG		"tklock_mode_ind"

static guint watch_id;
static guint tklock_watch_id;
static DBusConnection *conn = NULL;
static gboolean mce_bt_set = FALSE;
static gboolean mce_bt_on = FALSE;

static gboolean mce_tklock_mode_cb(DBusConnection *connection,
					DBusMessage *message, void *user_data)
{
	struct btd_adapter *adapter = user_data;
	DBusMessageIter args;
	const char *sigvalue;

	if (!dbus_message_iter_init(message, &args)) {
		error("message has no arguments");
	} else if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_STRING) {
		error("argument is not string");
	} else {

		dbus_message_iter_get_basic(&args, &sigvalue);
		DBG("got signal with value %s", sigvalue);

		if (g_strcmp0("unlocked", sigvalue) == 0 && mce_bt_on)
			btd_adapter_enable_auto_connect(adapter);
	}

	return TRUE;
}

static gboolean mce_signal_callback(DBusConnection *connection,
					DBusMessage *message, void *user_data)
{
	DBusMessageIter args;
	uint32_t sigvalue;
	struct btd_adapter *adapter = user_data;
	int err;

	DBG("received mce signal");

	if (!dbus_message_iter_init(message, &args))
		error("message has no arguments");
	else if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&args))
		error("argument is not uint32");
	else {
		dbus_message_iter_get_basic(&args, &sigvalue);
		DBG("got signal with value %u", sigvalue);

		/* set the adapter according to the mce signal
		   and remember the value */
		mce_bt_on = sigvalue & MCE_RADIO_STATE_BLUETOOTH ? TRUE : FALSE;

		if (mce_bt_on)
			err = btd_adapter_switch_online(adapter);
		else
			err = btd_adapter_switch_offline(adapter);

		if (err == 0)
			mce_bt_set = TRUE;

	}

	return TRUE;
}

static void read_radio_states_cb(DBusPendingCall *call, void *user_data)
{
	DBusError derr;
	DBusMessage *reply;
	dbus_uint32_t radio_states;
	struct btd_adapter *adapter = user_data;
	int err;

	reply = dbus_pending_call_steal_reply(call);

	dbus_error_init(&derr);
	if (dbus_set_error_from_message(&derr, reply)) {
		error("mce replied with an error: %s, %s",
				derr.name, derr.message);
		dbus_error_free(&derr);
		goto done;
	}

	dbus_error_init(&derr);
	if (dbus_message_get_args(reply, &derr,
				DBUS_TYPE_UINT32, &radio_states,
				DBUS_TYPE_INVALID) == FALSE) {
		error("unable to parse get_radio_states reply: %s, %s",
							derr.name, derr.message);
		dbus_error_free(&derr);
		goto done;
	}

	DBG("radio_states: %d", radio_states);

	mce_bt_on = radio_states & MCE_RADIO_STATE_BLUETOOTH ? TRUE : FALSE;

	if (mce_bt_on)
		err = btd_adapter_switch_online(adapter);
	else
		err = btd_adapter_switch_offline(adapter);

	if (err == 0)
		mce_bt_set = TRUE;

done:
	dbus_message_unref(reply);
}

static void adapter_powered(struct btd_adapter *adapter, gboolean powered)
{
	DBusMessage *msg;
	static gboolean startup = TRUE;

	DBG("adapter_powered called with %d", powered);

	if (startup) {
		DBusPendingCall *call;

		/* Initialization: sync adapter state and MCE radio state */

		DBG("Startup: reading MCE Bluetooth radio state...");
		startup = FALSE;

		msg = dbus_message_new_method_call(MCE_SERVICE,
					MCE_REQUEST_PATH, MCE_REQUEST_IF,
					MCE_RADIO_STATES_GET);

		if (!dbus_connection_send_with_reply(conn, msg, &call, -1)) {
			error("calling %s failed", MCE_RADIO_STATES_GET);
			dbus_message_unref(msg);
			return;
		}

		dbus_pending_call_set_notify(call, read_radio_states_cb,
								adapter, NULL);
		dbus_pending_call_unref(call);
		dbus_message_unref(msg);
		return;
	}

	/* MCE initiated operation */
	if (mce_bt_set == TRUE) {
		mce_bt_set = FALSE;
		return;
	}

	/* Non MCE operation: set MCE according to adapter state */
	if (mce_bt_on != powered) {
		dbus_uint32_t radio_states;
		dbus_uint32_t radio_mask = MCE_RADIO_STATE_BLUETOOTH;

		msg = dbus_message_new_method_call(MCE_SERVICE,
					MCE_REQUEST_PATH, MCE_REQUEST_IF,
					MCE_RADIO_STATES_CHANGE_REQ);

		radio_states = (powered ? MCE_RADIO_STATE_BLUETOOTH : 0);

		DBG("Changing MCE Bluetooth radio state to: %d", radio_states);

		dbus_message_append_args(msg, DBUS_TYPE_UINT32, &radio_states,
					DBUS_TYPE_UINT32, &radio_mask,
					DBUS_TYPE_INVALID);

		if (dbus_connection_send(conn, msg, NULL))
			mce_bt_on = powered;
		else
			error("calling %s failed", MCE_RADIO_STATES_CHANGE_REQ);

		dbus_message_unref(msg);
	}
}

static int mce_probe(struct btd_adapter *adapter)
{

	DBG("path %s", adapter_get_path(adapter));

	watch_id = g_dbus_add_signal_watch(conn, NULL, MCE_SIGNAL_PATH,
					MCE_SIGNAL_IF, MCE_RADIO_STATES_SIG,
					mce_signal_callback, adapter, NULL);

	tklock_watch_id = g_dbus_add_signal_watch(conn, NULL, MCE_SIGNAL_PATH,
					MCE_SIGNAL_IF, MCE_TKLOCK_MODE_SIG,
					mce_tklock_mode_cb, adapter, NULL);

	btd_adapter_register_powered_callback(adapter, adapter_powered);

	return 0;
}

static void mce_remove(struct btd_adapter *adapter)
{
	DBG("path %s", adapter_get_path(adapter));

	if (watch_id > 0)
		g_dbus_remove_watch(conn, watch_id);

	if (tklock_watch_id > 0)
		g_dbus_remove_watch(conn, tklock_watch_id);

	btd_adapter_unregister_powered_callback(adapter, adapter_powered);
}

static struct btd_adapter_driver mce_driver = {
	.name	= "mce",
	.probe	= mce_probe,
	.remove	= mce_remove,
};

static int maemo6_init(void)
{
	DBG("init maemo6 plugin");

	conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
	if (conn == NULL) {
		error("Unable to connect to D-Bus");
		return -1;
	}

	return btd_register_adapter_driver(&mce_driver);
}

static void maemo6_exit(void)
{
	DBG("exit maemo6 plugin");

	if (conn != NULL)
		dbus_connection_unref(conn);

	btd_unregister_adapter_driver(&mce_driver);
}

BLUETOOTH_PLUGIN_DEFINE(maemo6, VERSION,
		BLUETOOTH_PLUGIN_PRIORITY_DEFAULT, maemo6_init, maemo6_exit)