Modify configure files to match IVI feature
[profile/ivi/bluez.git] / network / manager.c
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 #include <bluetooth/uuid.h>
32
33 #include <glib.h>
34 #include <gdbus.h>
35
36 #include "log.h"
37
38 #include "adapter.h"
39 #include "device.h"
40 #include "manager.h"
41 #include "common.h"
42 #include "connection.h"
43 #include "server.h"
44
45 static DBusConnection *connection = NULL;
46
47 static gboolean conf_security = TRUE;
48
49 static void read_config(const char *file)
50 {
51         GKeyFile *keyfile;
52         GError *err = NULL;
53
54         keyfile = g_key_file_new();
55
56         if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
57                 g_clear_error(&err);
58                 goto done;
59         }
60
61         conf_security = !g_key_file_get_boolean(keyfile, "General",
62                                                 "DisableSecurity", &err);
63         if (err) {
64                 DBG("%s: %s", file, err->message);
65                 g_clear_error(&err);
66         }
67
68 done:
69         g_key_file_free(keyfile);
70
71         DBG("Config options: Security=%s",
72                                 conf_security ? "true" : "false");
73 }
74
75 static int network_probe(struct btd_device *device, GSList *uuids, uint16_t id)
76 {
77         struct btd_adapter *adapter = device_get_adapter(device);
78         const gchar *path = device_get_path(device);
79         bdaddr_t src, dst;
80
81         DBG("path %s", path);
82
83         adapter_get_address(adapter, &src);
84         device_get_address(device, &dst, NULL);
85
86         return connection_register(device, path, &src, &dst, id);
87 }
88
89 static void network_remove(struct btd_device *device, uint16_t id)
90 {
91         const gchar *path = device_get_path(device);
92
93         DBG("path %s", path);
94
95         connection_unregister(path, id);
96 }
97
98 static int panu_probe(struct btd_device *device, GSList *uuids)
99 {
100         return network_probe(device, uuids, BNEP_SVC_PANU);
101 }
102
103 static void panu_remove(struct btd_device *device)
104 {
105         network_remove(device, BNEP_SVC_PANU);
106 }
107
108 static int gn_probe(struct btd_device *device, GSList *uuids)
109 {
110         return network_probe(device, uuids, BNEP_SVC_GN);
111 }
112
113 static void gn_remove(struct btd_device *device)
114 {
115         network_remove(device, BNEP_SVC_GN);
116 }
117
118 static int nap_probe(struct btd_device *device, GSList *uuids)
119 {
120         return network_probe(device, uuids, BNEP_SVC_NAP);
121 }
122
123 static void nap_remove(struct btd_device *device)
124 {
125         network_remove(device, BNEP_SVC_NAP);
126 }
127
128 static int network_server_probe(struct btd_adapter *adapter)
129 {
130         const gchar *path = adapter_get_path(adapter);
131
132         DBG("path %s", path);
133
134         return server_register(adapter);
135 }
136
137 static void network_server_remove(struct btd_adapter *adapter)
138 {
139         const gchar *path = adapter_get_path(adapter);
140
141         DBG("path %s", path);
142
143         server_unregister(adapter);
144 }
145
146 static struct btd_device_driver network_panu_driver = {
147         .name   = "network-panu",
148         .uuids  = BTD_UUIDS(PANU_UUID),
149         .probe  = panu_probe,
150         .remove = panu_remove,
151 };
152
153 static struct btd_device_driver network_gn_driver = {
154         .name   = "network-gn",
155         .uuids  = BTD_UUIDS(GN_UUID),
156         .probe  = gn_probe,
157         .remove = gn_remove,
158 };
159
160 static struct btd_device_driver network_nap_driver = {
161         .name   = "network-nap",
162         .uuids  = BTD_UUIDS(NAP_UUID),
163         .probe  = nap_probe,
164         .remove = nap_remove,
165 };
166
167 static struct btd_adapter_driver network_server_driver = {
168         .name   = "network-server",
169         .probe  = network_server_probe,
170         .remove = network_server_remove,
171 };
172
173 int network_manager_init(DBusConnection *conn)
174 {
175         read_config(CONFIGDIR "/network.conf");
176
177         if (bnep_init()) {
178                 error("Can't init bnep module");
179                 return -1;
180         }
181
182         /*
183          * There is one socket to handle the incoming connections. NAP,
184          * GN and PANU servers share the same PSM. The initial BNEP message
185          * (setup connection request) contains the destination service
186          * field that defines which service the source is connecting to.
187          */
188
189         if (server_init(conn, conf_security) < 0)
190                 return -1;
191
192         /* Register network server if it doesn't exist */
193         btd_register_adapter_driver(&network_server_driver);
194
195         if (connection_init(conn) < 0)
196                 return -1;
197
198         btd_register_device_driver(&network_panu_driver);
199         btd_register_device_driver(&network_gn_driver);
200         btd_register_device_driver(&network_nap_driver);
201
202         connection = dbus_connection_ref(conn);
203
204         return 0;
205 }
206
207 void network_manager_exit(void)
208 {
209         server_exit();
210
211         btd_unregister_device_driver(&network_panu_driver);
212         btd_unregister_device_driver(&network_gn_driver);
213         btd_unregister_device_driver(&network_nap_driver);
214
215         connection_exit();
216
217         btd_unregister_adapter_driver(&network_server_driver);
218
219         dbus_connection_unref(connection);
220         connection = NULL;
221
222         bnep_cleanup();
223 }