1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <bluetooth/bluetooth.h>
29 #include <bluetooth/bnep.h>
30 #include <bluetooth/sdp.h>
31
32 #include <glib.h>
33 #include <gdbus.h>
34
35 #include "log.h"
36
37 #include "adapter.h"
38 #include "device.h"
39 #include "manager.h"
40 #include "common.h"
41 #include "connection.h"
42 #include "server.h"
43
44 static DBusConnection *connection = NULL;
45
46 static gboolean conf_security = TRUE;
47
read_config(const char * file)48 static void read_config(const char *file)
49 {
50 GKeyFile *keyfile;
51 GError *err = NULL;
52
53 keyfile = g_key_file_new();
54
55 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
56 g_clear_error(&err);
57 goto done;
58 }
59
60 conf_security = !g_key_file_get_boolean(keyfile, "General",
61 "DisableSecurity", &err);
62 if (err) {
63 DBG("%s: %s", file, err->message);
64 g_clear_error(&err);
65 }
66
67 done:
68 g_key_file_free(keyfile);
69
70 DBG("Config options: Security=%s",
71 conf_security ? "true" : "false");
72 }
73
network_probe(struct btd_device * device,GSList * uuids,uint16_t id)74 static int network_probe(struct btd_device *device, GSList *uuids, uint16_t id)
75 {
76 struct btd_adapter *adapter = device_get_adapter(device);
77 const gchar *path = device_get_path(device);
78 bdaddr_t src, dst;
79
80 DBG("path %s", path);
81
82 adapter_get_address(adapter, &src);
83 device_get_address(device, &dst);
84
85 return connection_register(device, path, &src, &dst, id);
86 }
87
network_remove(struct btd_device * device,uint16_t id)88 static void network_remove(struct btd_device *device, uint16_t id)
89 {
90 const gchar *path = device_get_path(device);
91
92 DBG("path %s", path);
93
94 connection_unregister(path, id);
95 }
96
panu_probe(struct btd_device * device,GSList * uuids)97 static int panu_probe(struct btd_device *device, GSList *uuids)
98 {
99 return network_probe(device, uuids, BNEP_SVC_PANU);
100 }
101
panu_remove(struct btd_device * device)102 static void panu_remove(struct btd_device *device)
103 {
104 network_remove(device, BNEP_SVC_PANU);
105 }
106
gn_probe(struct btd_device * device,GSList * uuids)107 static int gn_probe(struct btd_device *device, GSList *uuids)
108 {
109 return network_probe(device, uuids, BNEP_SVC_GN);
110 }
111
gn_remove(struct btd_device * device)112 static void gn_remove(struct btd_device *device)
113 {
114 network_remove(device, BNEP_SVC_GN);
115 }
116
nap_probe(struct btd_device * device,GSList * uuids)117 static int nap_probe(struct btd_device *device, GSList *uuids)
118 {
119 return network_probe(device, uuids, BNEP_SVC_NAP);
120 }
121
nap_remove(struct btd_device * device)122 static void nap_remove(struct btd_device *device)
123 {
124 network_remove(device, BNEP_SVC_NAP);
125 }
126
network_server_probe(struct btd_adapter * adapter)127 static int network_server_probe(struct btd_adapter *adapter)
128 {
129 const gchar *path = adapter_get_path(adapter);
130
131 DBG("path %s", path);
132
133 return server_register(adapter);
134 }
135
network_server_remove(struct btd_adapter * adapter)136 static void network_server_remove(struct btd_adapter *adapter)
137 {
138 const gchar *path = adapter_get_path(adapter);
139
140 DBG("path %s", path);
141
142 server_unregister(adapter);
143 }
144
145 static struct btd_device_driver network_panu_driver = {
146 .name = "network-panu",
147 .uuids = BTD_UUIDS(PANU_UUID),
148 .probe = panu_probe,
149 .remove = panu_remove,
150 };
151
152 static struct btd_device_driver network_gn_driver = {
153 .name = "network-gn",
154 .uuids = BTD_UUIDS(GN_UUID),
155 .probe = gn_probe,
156 .remove = gn_remove,
157 };
158
159 static struct btd_device_driver network_nap_driver = {
160 .name = "network-nap",
161 .uuids = BTD_UUIDS(NAP_UUID),
162 .probe = nap_probe,
163 .remove = nap_remove,
164 };
165
166 static struct btd_adapter_driver network_server_driver = {
167 .name = "network-server",
168 .probe = network_server_probe,
169 .remove = network_server_remove,
170 };
171
network_manager_init(DBusConnection * conn)172 int network_manager_init(DBusConnection *conn)
173 {
174 read_config(CONFIGDIR "/network.conf");
175
176 if (bnep_init()) {
177 error("Can't init bnep module");
178 return -1;
179 }
180
181 /*
182 * There is one socket to handle the incomming connections. NAP,
183 * GN and PANU servers share the same PSM. The initial BNEP message
184 * (setup connection request) contains the destination service
185 * field that defines which service the source is connecting to.
186 */
187
188 if (server_init(conn, conf_security) < 0)
189 return -1;
190
191 /* Register network server if it doesn't exist */
192 btd_register_adapter_driver(&network_server_driver);
193
194 if (connection_init(conn) < 0)
195 return -1;
196
197 btd_register_device_driver(&network_panu_driver);
198 btd_register_device_driver(&network_gn_driver);
199 btd_register_device_driver(&network_nap_driver);
200
201 connection = dbus_connection_ref(conn);
202
203 return 0;
204 }
205
network_manager_exit(void)206 void network_manager_exit(void)
207 {
208 server_exit();
209
210 btd_unregister_device_driver(&network_panu_driver);
211 btd_unregister_device_driver(&network_gn_driver);
212 btd_unregister_device_driver(&network_nap_driver);
213
214 connection_exit();
215
216 btd_unregister_adapter_driver(&network_server_driver);
217
218 dbus_connection_unref(connection);
219 connection = NULL;
220
221 bnep_cleanup();
222 }
223