summaryrefslogtreecommitdiff
path: root/src/bt-monitor.c
blob: 253d7e1f70b4190d59bc11e5d6ca1227e370a11c (plain)
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
/*
 *
 *  bluez-tools - a set of tools to manage bluetooth devices for linux
 *
 *  Copyright (C) 2010  Alexander Orlenko <zxteam@gmail.com>
 *
 *
 *  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 <stdlib.h>
#include <glib.h>

#include "lib/dbus-common.h"
#include "lib/helpers.h"
#include "lib/adapter.h"
#include "lib/device.h"
#include "lib/manager.h"

static gchar *capture_adapter_name = NULL;
static GPtrArray *captured_adapters = NULL;
static GPtrArray *captured_devices = NULL;

static void capture_adapter(Adapter *adapter);
static void release_adapter(Adapter *adapter);

/*
 *  Manager signals
 */
static void manager_adapter_added(Manager *manager, const gchar *adapter_path, gpointer data)
{
	Adapter *adapter = g_object_new(ADAPTER_TYPE, "DBusObjectPath", adapter_path, NULL);

	if (capture_adapter_name == NULL) {
		g_print("[MANAGER] adapter added: %s (%s)\n", adapter_get_name(adapter), adapter_path);
		capture_adapter(adapter);
	} else {
		g_object_unref(adapter);
	}
}

static void manager_adapter_removed(Manager *manager, const gchar *adapter_path, gpointer data)
{
	for (int i = 0; i < captured_adapters->len; i++) {
		Adapter *adapter = ADAPTER(g_ptr_array_index(captured_adapters, i));

		if (g_strcmp0(adapter_path, adapter_get_dbus_object_path(adapter)) == 0) {
			g_print("[MANAGER] adapter removed: %s (%s)\n", adapter_get_name(adapter), adapter_path);
			release_adapter(adapter);
			break;
		}
	}
}

static void manager_default_adapter_changed(Manager *manager, const gchar *adapter_path, gpointer data)
{
	g_print("[MANAGER] default adapter changed: %s\n", adapter_path);
}

static void manager_property_changed(Manager *manager, const gchar *name, const GValue *value, gpointer data)
{
	g_print("[MANAGER] property changed: %s -> %s\n", name, g_strdup_value_contents(value));
}

/*
 * Adapter signals
 */
static void adapter_device_created(Adapter *adapter, const gchar *device_path, gpointer data)
{
	g_print("[ADAPTER] device created: %s\n", device_path);
}

static void adapter_device_disappeared(Adapter *adapter, const gchar *address, gpointer data)
{
	g_print("[ADAPTER] device disappeared: %s\n", address);
}

static void adapter_device_found(Adapter *adapter, const gchar *address, gpointer data)
{
	g_print("[ADAPTER] device found: %s\n", address);
}

static void adapter_device_removed(Adapter *adapter, const gchar *device_path, gpointer data)
{
	g_print("[ADAPTER] device removed: %s\n", device_path);
}

static void adapter_property_changed(Adapter *adapter, const gchar *name, const GValue *value, gpointer data)
{
	g_print("[ADAPTER] property changed: %s -> %s\n", name, g_strdup_value_contents(value));
}

/*
 * Device signals
 */
static void device_disconnect_requested(Device *device, gpointer data)
{
	g_print("[DEVICE] disconnect requested\n");
}

static void device_node_created(Device *device, const gchar *node, gpointer data)
{
	g_print("[DEVICE] node created: %s\n", node);
}

static void device_node_removed(Device *device, const gchar *node, gpointer data)
{
	g_print("[DEVICE] node removed: %s\n", node);
}

static void device_property_changed(Device *device, const gchar *name, const GValue *value, gpointer data)
{
	g_print("[DEVICE] property changed: %s -> %s\n", name, g_strdup_value_contents(value));
}

/*
 * Service signals
 */
//static void audio_property_changed()
//{
//
//}

//static void input_property_changed()
//{
//
//}

//static void network_property_changed()
//{
//
//}

static void capture_adapter(Adapter *adapter)
{
	g_assert(ADAPTER_IS(adapter));

	g_print("capture adapter: %s (%s)\n", adapter_get_name(adapter), adapter_get_dbus_object_path(adapter));

	g_signal_connect(adapter, "DeviceCreated", G_CALLBACK(adapter_device_created), NULL);
	g_signal_connect(adapter, "DeviceDisappeared", G_CALLBACK(adapter_device_disappeared), NULL);
	g_signal_connect(adapter, "DeviceFound", G_CALLBACK(adapter_device_found), NULL);
	g_signal_connect(adapter, "DeviceRemoved", G_CALLBACK(adapter_device_removed), NULL);
	g_signal_connect(adapter, "PropertyChanged", G_CALLBACK(adapter_property_changed), NULL);

	// Capturing signals from devices
	const GPtrArray *devices_list = adapter_get_devices(adapter);
	g_assert(devices_list != NULL);
	for (int i = 0; i < devices_list->len; i++) {
		Device *device = g_object_new(DEVICE_TYPE, "DBusObjectPath", g_ptr_array_index(devices_list, i), NULL);

		g_print("capture device: %s (%s)\n", device_get_name(device), device_get_dbus_object_path(device));

		g_signal_connect(device, "DisconnectRequested", G_CALLBACK(device_disconnect_requested), NULL);
		g_signal_connect(device, "NodeCreated", G_CALLBACK(device_node_created), NULL);
		g_signal_connect(device, "NodeRemoved", G_CALLBACK(device_node_removed), NULL);
		g_signal_connect(device, "PropertyChanged", G_CALLBACK(device_property_changed), NULL);

		g_ptr_array_add(captured_devices, device);
	}

	g_ptr_array_add(captured_adapters, adapter);
}

static void release_adapter(Adapter *adapter)
{
	g_assert(ADAPTER_IS(adapter));

	for (int i = 0; i < captured_adapters->len; i++) {
		Adapter *adapter_t = ADAPTER(g_ptr_array_index(captured_adapters, i));
		if (adapter == adapter_t) {
			GPtrArray *devices_to_remove = g_ptr_array_new();
			for (int j = 0; j < captured_devices->len; j++) {
				Device *device_t = DEVICE(g_ptr_array_index(captured_devices, j));
				if (g_strcmp0(adapter_get_dbus_object_path(adapter_t), device_get_adapter(device_t)) == 0) {
					int *p = g_new(int, 1);
					*p = j;
					g_ptr_array_add(devices_to_remove, p);
					g_print("release device: %s (%s)\n", device_get_name(device_t), device_get_dbus_object_path(device_t));
					g_object_unref(device_t);
				}
			}
			for (int j = 0; j < devices_to_remove->len; j++) {
				int *p = g_ptr_array_index(devices_to_remove, j);
				g_ptr_array_remove_index(captured_devices, *p);
			}
			g_ptr_array_unref(devices_to_remove);
			g_print("release adapter: %s (%s)\n", adapter_get_name(adapter_t), adapter_get_dbus_object_path(adapter_t));
			g_object_unref(adapter_t);
			g_ptr_array_remove_index(captured_adapters, i);
			break;
		}
	}
}

static GOptionEntry entries[] = {
	{ "adapter", 'a', 0, G_OPTION_ARG_STRING, &capture_adapter_name, "Adapter name or MAC", NULL},
	{ NULL}
};

int main(int argc, char *argv[])
{
	GError *error = NULL;
	GOptionContext *context;

	g_type_init();

	context = g_option_context_new("- a bluetooth monitor");
	g_option_context_add_main_entries(context, entries, NULL);
	if (!g_option_context_parse(context, &argc, &argv, &error)) {
		g_print("%s: %s\n", g_get_prgname(), error->message);
		g_print("Try `%s --help` for more information.\n", g_get_prgname());
		exit(EXIT_FAILURE);
	}
	g_option_context_free(context);

	if (!dbus_connect(&error)) {
		g_printerr("Couldn't connect to dbus: %s", error->message);
		exit(EXIT_FAILURE);
	}

	captured_adapters = g_ptr_array_new();
	captured_devices = g_ptr_array_new();

	Manager *manager = g_object_new(MANAGER_TYPE, NULL);

	if (capture_adapter_name != NULL) {
		Adapter *adapter = find_adapter_by_name(capture_adapter_name, &error);
		exit_if_error(error);
		capture_adapter(adapter);
	} else {
		const GPtrArray *adapters_list = manager_get_adapters(manager);
		g_return_val_if_fail(adapters_list != NULL, EXIT_FAILURE);

		if (adapters_list->len == 0) {
			g_print("no adapters found\n");
		}

		for (int i = 0; i < adapters_list->len; i++) {
			Adapter *adapter = g_object_new(ADAPTER_TYPE, "DBusObjectPath", g_ptr_array_index(adapters_list, i), NULL);
			capture_adapter(adapter);
		}
	}

	g_signal_connect(manager, "AdapterAdded", G_CALLBACK(manager_adapter_added), NULL);
	g_signal_connect(manager, "AdapterRemoved", G_CALLBACK(manager_adapter_removed), NULL);
	g_signal_connect(manager, "DefaultAdapterChanged", G_CALLBACK(manager_default_adapter_changed), NULL);
	g_signal_connect(manager, "PropertyChanged", G_CALLBACK(manager_property_changed), NULL);

	GMainLoop *mainloop;
	mainloop = g_main_loop_new(NULL, FALSE);
	g_main_loop_run(mainloop);

	exit(EXIT_SUCCESS);
}