From: Daniel Wagner <[email protected]>
---
Makefile.am | 4 +-
elect/device.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 143 insertions(+), 3 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index d02fd21..045ea2e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -651,8 +651,8 @@ if ELECT
sbin_PROGRAMS += elect/electd
-elect_electd_SOURCES = $(gdbus_sources) \
- src/log.c src/dbus.c \
+elect_electd_SOURCES = $(gdbus_sources) $(btio_sources) \
+ src/log.c src/dbus.c plugins/bluetooth.c \
elect/main.c elect/manager.c elect/device.c
elect_electd_LDADD = $(builtin_libadd) @GLIB_LIBS@ @DBUS_LIBS@ @CAPNG_LIBS@
-ldl
diff --git a/elect/device.c b/elect/device.c
index 35ac702..8f4d6a0 100644
--- a/elect/device.c
+++ b/elect/device.c
@@ -31,18 +31,30 @@
#include <glib.h>
#include <gdbus.h>
+#include "plugins/bluetooth.h"
+
#include "elect.h"
static DBusConnection *connection;
+static int next_device_id = 0;
static GHashTable *device_hash;
struct elect_device {
+ /* The D-Bus path to this device */
+ char *elect_path;
+
+ /* BlueZ device information */
+ char *path;
+ char *address;
+ char *name;
+
+ DBusPendingCall *call;
};
const char *__elect_device_get_path(struct elect_device *device)
{
- return "/";
+ return device->elect_path;
}
void __elect_device_append_properties(struct elect_device *device,
@@ -66,10 +78,129 @@ void __elect_device_foreach(elect_device_foreach_func
func, void *userdata)
}
}
+static int bt_probe(const char *path, const char *dev_addr,
+ const char *adapter_addr, const char *alias)
+{
+ struct elect_device *device;
+ char buf[256];
+
+ DBG("");
+
+ /* We already have this device in our hash, ignore */
+ if (g_hash_table_lookup(device_hash, path) != NULL)
+ return -EALREADY;
+
+ ofono_info("Using device: %s, devaddr: %s, adapter: %s",
+ path, dev_addr, adapter_addr);
+
+ strcpy(buf, "dun/");
+ bluetooth_create_path(dev_addr, adapter_addr, buf + 4, sizeof(buf) - 4);
+
+ device = g_try_new0(struct elect_device, 1);
+ if (device == 0) {
+ DBG("Failed to create device data structure");
+ return -ENOMEM;
+ }
+
+ DBG("%p", device);
+
+ device->elect_path = g_strdup_printf("/device%d", next_device_id);
+ if (device->elect_path == NULL)
+ goto free;
+
+ device->path = g_strdup(path);
+ if (device->path == NULL)
+ goto free;
+
+ device->address = g_strdup(dev_addr);
+ if (device->address == NULL)
+ goto free;
+
+ device->name = g_strdup(alias);
+ if (device->name == NULL)
+ goto free;
+
+ g_hash_table_insert(device_hash, g_strdup(path), device);
+
+ next_device_id += 1;
+
+ return 0;
+
+free:
+ g_free(device->path);
+ g_free(device->address);
+ g_free(device->name);
+ g_free(device->elect_path);
+ g_free(device);
+
+ return -ENOMEM;
+}
+
+static gboolean bt_remove_device(gpointer key, gpointer value,
+ gpointer user_data)
+{
+ struct elect_device *device = value;
+ const char *path = key;
+ const char *prefix = user_data;
+
+ DBG("%p", device);
+
+ if (device->call != NULL)
+ dbus_pending_call_cancel(device->call);
+
+ if (prefix && g_str_has_prefix(path, prefix) == FALSE)
+ return FALSE;
+
+ return TRUE;
+}
+
+static void bt_remove(const char *prefix)
+{
+ DBG("%s", prefix);
+
+ if (device_hash == NULL)
+ return;
+
+ g_hash_table_foreach_remove(device_hash, bt_remove_device,
+ (gpointer) prefix);
+}
+
+static void bt_set_alias(const char *path, const char *alias)
+{
+ struct elect_device *device;
+
+ DBG("");
+
+ if (path == NULL || alias == NULL)
+ return;
+
+ device = g_hash_table_lookup(device_hash, path);
+ if (device == NULL)
+ return;
+
+ g_free(device->name);
+ device->name = g_strdup(alias);
+}
+
+static struct bluetooth_profile dun_profile = {
+ .name = "dun_dt",
+ .probe = bt_probe,
+ .remove = bt_remove,
+ .set_alias = bt_set_alias,
+};
+
static void destroy_device(gpointer user)
{
struct elect_device *device = user;
+ if (device->call)
+ dbus_pending_call_cancel(device->call);
+
+ g_free(device->elect_path);
+ g_free(device->path);
+ g_free(device->address);
+ g_free(device->name);
+
g_free(device);
}
@@ -86,10 +217,17 @@ void __elect_device_shutdown(void)
int __elect_device_init(DBusConnection *conn)
{
+ int err;
+
DBG("");
connection = conn;
+ err = bluetooth_register_uuid(DUN_GW_UUID, &dun_profile);
+ if (err < 0)
+ return err;
+
+
device_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, destroy_device);
@@ -100,5 +238,7 @@ void __elect_device_cleanup(void)
{
DBG("");
+ bluetooth_unregister_uuid(DUN_GW_UUID);
+
g_hash_table_destroy(device_hash);
}
--
1.7.8.110.g4cb5d1
_______________________________________________
ofono mailing list
[email protected]
http://lists.ofono.org/listinfo/ofono