2006-10-27 22:46:36 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
2008-02-02 11:37:05 +08:00
|
|
|
* Copyright (C) 2004-2008 Marcel Holtmann <marcel@holtmann.org>
|
2006-10-27 22:46:36 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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 <errno.h>
|
2008-05-29 16:35:54 +08:00
|
|
|
|
2007-04-13 02:42:47 +08:00
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
|
2008-05-29 16:35:54 +08:00
|
|
|
#include <gdbus.h>
|
2008-03-22 23:49:07 +08:00
|
|
|
|
|
|
|
#include "plugin.h"
|
2008-05-29 16:35:54 +08:00
|
|
|
#include "device.h"
|
2007-03-10 06:12:19 +08:00
|
|
|
#include "logging.h"
|
2007-03-09 23:37:57 +08:00
|
|
|
#include "manager.h"
|
|
|
|
|
2007-09-28 21:15:53 +08:00
|
|
|
#define IFACE_PREFIX "bnep%d"
|
2008-05-29 16:35:54 +08:00
|
|
|
#define GN_IFACE "pan0"
|
2007-09-28 21:29:59 +08:00
|
|
|
#define NAP_IFACE "pan1"
|
2007-09-28 21:15:53 +08:00
|
|
|
|
2008-05-01 09:05:46 +08:00
|
|
|
#define PANU_UUID "00001115-0000-1000-8000-00805f9b34fb"
|
2008-05-29 16:35:54 +08:00
|
|
|
#define NAP_UUID "00001116-0000-1000-8000-00805f9b34fb"
|
|
|
|
#define GN_UUID "00001117-0000-1000-8000-00805f9b34fb"
|
|
|
|
|
2008-06-09 05:21:54 +08:00
|
|
|
#define NETWORK_INTERFACE "org.bluez.Network"
|
|
|
|
|
|
|
|
static DBusMessage *network_connect(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
const char *target, *device = "bnep0";
|
|
|
|
|
|
|
|
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &target,
|
|
|
|
DBUS_TYPE_INVALID) == FALSE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &device,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *network_disconnect(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *user_data)
|
|
|
|
{
|
|
|
|
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_INVALID) == FALSE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GDBusMethodTable network_methods[] = {
|
|
|
|
{ "Connect", "s", "s", network_connect },
|
|
|
|
{ "Disconnect", "", "", network_disconnect },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static GDBusSignalTable network_signals[] = {
|
|
|
|
{ "Connected", "ss" },
|
|
|
|
{ "Disconnected", "s" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2008-05-29 16:35:54 +08:00
|
|
|
static DBusConnection *conn;
|
|
|
|
|
|
|
|
static int network_probe(const char *path)
|
|
|
|
{
|
2008-06-09 04:54:55 +08:00
|
|
|
DBG("path %s", path);
|
2008-05-29 16:35:54 +08:00
|
|
|
|
2008-06-09 05:21:54 +08:00
|
|
|
if (g_dbus_register_interface(conn, path, NETWORK_INTERFACE,
|
|
|
|
network_methods, network_signals, NULL,
|
|
|
|
NULL, NULL) == FALSE)
|
|
|
|
return -1;
|
|
|
|
|
2008-05-29 16:35:54 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void network_remove(const char *path)
|
|
|
|
{
|
2008-06-09 04:54:55 +08:00
|
|
|
DBG("path %s", path);
|
2008-06-09 05:21:54 +08:00
|
|
|
|
|
|
|
g_dbus_unregister_interface(conn, path, NETWORK_INTERFACE);
|
2008-05-29 16:35:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct btd_device_driver network_driver = {
|
|
|
|
.name = "network",
|
|
|
|
.uuids = BTD_UUIDS(PANU_UUID, NAP_UUID, GN_UUID),
|
|
|
|
.probe = network_probe,
|
|
|
|
.remove = network_remove,
|
2008-05-01 09:05:46 +08:00
|
|
|
};
|
|
|
|
|
2007-09-28 21:15:53 +08:00
|
|
|
static struct network_conf conf = {
|
|
|
|
.connection_enabled = TRUE,
|
|
|
|
.server_enabled = TRUE,
|
|
|
|
.iface_prefix = NULL,
|
2007-09-28 21:23:51 +08:00
|
|
|
.panu_script = NULL,
|
|
|
|
.gn_script = NULL,
|
|
|
|
.nap_script = NULL,
|
|
|
|
.gn_iface = NULL,
|
|
|
|
.nap_iface = NULL,
|
|
|
|
.security = TRUE
|
2007-09-28 21:15:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void read_config(const char *file)
|
|
|
|
{
|
|
|
|
GKeyFile *keyfile;
|
|
|
|
GError *err = NULL;
|
2008-05-16 20:49:57 +08:00
|
|
|
char **disabled;
|
2007-09-28 21:15:53 +08:00
|
|
|
|
|
|
|
keyfile = g_key_file_new();
|
|
|
|
|
|
|
|
if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
|
|
|
|
error("Parsing %s failed: %s", file, err->message);
|
|
|
|
g_error_free(err);
|
2007-09-28 21:29:59 +08:00
|
|
|
goto done;
|
2007-09-28 21:15:53 +08:00
|
|
|
}
|
|
|
|
|
2008-05-16 20:49:57 +08:00
|
|
|
disabled = g_key_file_get_string_list(keyfile, "General",
|
|
|
|
"Disable", NULL, &err);
|
2007-09-28 21:15:53 +08:00
|
|
|
if (err) {
|
|
|
|
debug("%s: %s", file, err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
err = NULL;
|
2007-09-28 21:23:51 +08:00
|
|
|
} else {
|
2008-05-16 20:49:57 +08:00
|
|
|
int i;
|
|
|
|
for (i = 0; disabled[i] != NULL; i++) {
|
|
|
|
if (g_str_equal(disabled[i], "Connection"))
|
|
|
|
conf.connection_enabled = FALSE;
|
|
|
|
else if (g_str_equal(disabled[i], "Server"))
|
|
|
|
conf.server_enabled = FALSE;
|
|
|
|
}
|
|
|
|
g_strfreev(disabled);
|
2007-09-28 21:15:53 +08:00
|
|
|
}
|
|
|
|
|
2007-09-28 21:25:27 +08:00
|
|
|
conf.security = !g_key_file_get_boolean(keyfile, "General",
|
2007-09-28 21:23:51 +08:00
|
|
|
"DisableSecurity", &err);
|
2007-09-28 21:15:53 +08:00
|
|
|
if (err) {
|
|
|
|
debug("%s: %s", file, err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
err = NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:23:51 +08:00
|
|
|
conf.panu_script = g_key_file_get_string(keyfile, "PANU Role",
|
|
|
|
"Script", &err);
|
2007-09-28 21:15:53 +08:00
|
|
|
if (err) {
|
|
|
|
debug("%s: %s", file, err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
err = NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:23:51 +08:00
|
|
|
conf.gn_script = g_key_file_get_string(keyfile, "GN Role",
|
|
|
|
"Script", &err);
|
2007-09-28 21:15:53 +08:00
|
|
|
if (err) {
|
|
|
|
debug("%s: %s", file, err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
err = NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:23:51 +08:00
|
|
|
conf.nap_script = g_key_file_get_string(keyfile, "NAP Role",
|
|
|
|
"Script", &err);
|
2007-09-28 21:15:53 +08:00
|
|
|
if (err) {
|
|
|
|
debug("%s: %s", file, err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
err = NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:29:59 +08:00
|
|
|
conf.iface_prefix = g_key_file_get_string(keyfile, "PANU Role",
|
|
|
|
"Interface", &err);
|
|
|
|
if (err) {
|
|
|
|
debug("%s: %s", file, err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
err = NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:23:51 +08:00
|
|
|
conf.gn_iface = g_key_file_get_string(keyfile, "GN Role",
|
|
|
|
"Interface", &err);
|
2007-09-28 21:15:53 +08:00
|
|
|
if (err) {
|
|
|
|
debug("%s: %s", file, err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
err = NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:23:51 +08:00
|
|
|
conf.nap_iface = g_key_file_get_string(keyfile, "NAP Role",
|
|
|
|
"Interface", &err);
|
2007-09-28 21:15:53 +08:00
|
|
|
if (err) {
|
|
|
|
debug("%s: %s", file, err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
err = NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:29:59 +08:00
|
|
|
done:
|
|
|
|
g_key_file_free(keyfile);
|
|
|
|
|
|
|
|
if (!conf.iface_prefix)
|
|
|
|
conf.iface_prefix = g_strdup(IFACE_PREFIX);
|
|
|
|
if (!conf.gn_iface)
|
|
|
|
conf.gn_iface = g_strdup(GN_IFACE);
|
|
|
|
if (!conf.nap_iface)
|
|
|
|
conf.nap_iface = g_strdup(NAP_IFACE);
|
|
|
|
|
2007-09-28 21:25:27 +08:00
|
|
|
debug("Config options: InterfacePrefix=%s, PANU_Script=%s, "
|
|
|
|
"GN_Script=%s, NAP_Script=%s, GN_Interface=%s, "
|
|
|
|
"NAP_Interface=%s, Security=%s",
|
2007-09-28 21:23:51 +08:00
|
|
|
conf.iface_prefix, conf.panu_script, conf.gn_script,
|
|
|
|
conf.nap_script, conf.gn_iface, conf.nap_iface,
|
|
|
|
conf.security ? "true" : "false");
|
2007-09-28 21:15:53 +08:00
|
|
|
}
|
|
|
|
|
2008-03-22 23:49:07 +08:00
|
|
|
static int network_init(void)
|
|
|
|
{
|
|
|
|
conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
|
|
|
|
if (conn == NULL)
|
|
|
|
return -EIO;
|
2007-03-10 06:12:19 +08:00
|
|
|
|
2007-09-28 21:15:53 +08:00
|
|
|
read_config(CONFIGDIR "/network.conf");
|
|
|
|
|
2008-03-22 23:49:07 +08:00
|
|
|
if (network_manager_init(conn, &conf) < 0) {
|
2007-03-27 18:21:45 +08:00
|
|
|
dbus_connection_unref(conn);
|
2008-03-22 23:49:07 +08:00
|
|
|
return -EIO;
|
2007-03-27 18:21:45 +08:00
|
|
|
}
|
2007-03-19 21:24:41 +08:00
|
|
|
|
2008-05-29 16:35:54 +08:00
|
|
|
btd_register_device_driver(&network_driver);
|
2007-03-13 04:15:15 +08:00
|
|
|
|
2008-03-22 23:49:07 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2007-03-22 03:35:35 +08:00
|
|
|
|
2008-03-22 23:49:07 +08:00
|
|
|
static void network_exit(void)
|
|
|
|
{
|
2008-05-29 16:35:54 +08:00
|
|
|
btd_unregister_device_driver(&network_driver);
|
2008-05-09 01:24:48 +08:00
|
|
|
|
2008-03-22 23:49:07 +08:00
|
|
|
network_manager_exit();
|
2007-03-20 03:46:43 +08:00
|
|
|
|
2007-03-27 18:21:45 +08:00
|
|
|
dbus_connection_unref(conn);
|
2006-10-27 22:46:36 +08:00
|
|
|
}
|
2008-03-22 23:49:07 +08:00
|
|
|
|
|
|
|
BLUETOOTH_PLUGIN_DEFINE("network", network_init, network_exit)
|