summaryrefslogtreecommitdiff
path: root/profiles/network/manager.c
blob: 42b521436d137f7ec683eed4cfb56b2c2bcbf233 (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
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  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 <stdbool.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/bnep.h>
#include <bluetooth/sdp.h>
#include <bluetooth/uuid.h>

#include <glib.h>
#include <gdbus.h>

#include "log.h"

#include "adapter.h"
#include "device.h"
#include "profile.h"
#include "manager.h"
#include "common.h"
#include "connection.h"
#include "server.h"

static gboolean conf_security = TRUE;

static void read_config(const char *file)
{
	GKeyFile *keyfile;
	GError *err = NULL;

	keyfile = g_key_file_new();

	if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
		g_clear_error(&err);
		goto done;
	}

	conf_security = !g_key_file_get_boolean(keyfile, "General",
						"DisableSecurity", &err);
	if (err) {
		DBG("%s: %s", file, err->message);
		g_clear_error(&err);
	}

done:
	g_key_file_free(keyfile);

	DBG("Config options: Security=%s",
				conf_security ? "true" : "false");
}

static int network_probe(struct btd_profile *p, struct btd_device *device,
								GSList *uuids)
{
	struct btd_adapter *adapter = device_get_adapter(device);
	const gchar *path = device_get_path(device);
	const bdaddr_t *src;
	bdaddr_t dst;

	DBG("path %s", path);

	src = adapter_get_address(adapter);
	device_get_address(device, &dst, NULL);

	if (g_slist_find_custom(uuids, PANU_UUID, bt_uuid_strcmp))
		connection_register(device, path, src, &dst, BNEP_SVC_PANU);
	if (g_slist_find_custom(uuids, GN_UUID, bt_uuid_strcmp))
		connection_register(device, path, src, &dst, BNEP_SVC_GN);
	if (g_slist_find_custom(uuids, NAP_UUID, bt_uuid_strcmp))
		connection_register(device, path, src, &dst, BNEP_SVC_NAP);

	return 0;
}

static void network_remove(struct btd_profile *p, struct btd_device *device)
{
	const gchar *path = device_get_path(device);

	DBG("path %s", path);

	connection_unregister(path);
}

static int network_server_probe(struct btd_profile *p,
						struct btd_adapter *adapter)
{
	const gchar *path = adapter_get_path(adapter);

	DBG("path %s", path);

	return server_register(adapter);
}

static void network_server_remove(struct btd_profile *p,
						struct btd_adapter *adapter)
{
	const gchar *path = adapter_get_path(adapter);

	DBG("path %s", path);

	server_unregister(adapter);
}

static struct btd_profile network_profile = {
	.name		= "network",
	.remote_uuids	= BTD_UUIDS(PANU_UUID, GN_UUID, NAP_UUID),
	.device_probe	= network_probe,
	.device_remove	= network_remove,

	.adapter_probe	= network_server_probe,
	.adapter_remove	= network_server_remove,
};

int network_manager_init(void)
{
	read_config(CONFIGDIR "/network.conf");

	if (bnep_init()) {
		error("Can't init bnep module");
		return -1;
	}

	/*
	 * There is one socket to handle the incoming connections. NAP,
	 * GN and PANU servers share the same PSM. The initial BNEP message
	 * (setup connection request) contains the destination service
	 * field that defines which service the source is connecting to.
	 */

	if (server_init(conf_security) < 0)
		return -1;

	btd_profile_register(&network_profile);

	return 0;
}

void network_manager_exit(void)
{
	server_exit();

	btd_profile_unregister(&network_profile);

	bnep_cleanup();
}