2010-06-14 18:04:22 +00:00
|
|
|
/*
|
2010-10-01 04:51:34 +00:00
|
|
|
*
|
2010-06-14 18:04:22 +00:00
|
|
|
* oFono - Open Source Telephony
|
|
|
|
*
|
2011-10-10 20:39:54 +00:00
|
|
|
* Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
|
2010-06-14 18:04:22 +00:00
|
|
|
* Copyright (C) 2010 ProFUSION embedded systems
|
2010-10-01 04:51:34 +00:00
|
|
|
* Copyright (C) 2010 Gustavo F. Padovan <gustavo@padovan.org>
|
2010-06-14 18:04:22 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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 <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <gdbus.h>
|
|
|
|
|
2011-06-06 06:03:32 +00:00
|
|
|
#define OFONO_API_SUBJECT_TO_CHANGE
|
|
|
|
#include <ofono/plugin.h>
|
|
|
|
#include <ofono/log.h>
|
2010-06-14 18:04:22 +00:00
|
|
|
|
2011-02-08 22:00:58 +00:00
|
|
|
#include <btio.h>
|
2013-01-16 13:31:27 +00:00
|
|
|
#include "bluez4.h"
|
2010-06-14 18:04:22 +00:00
|
|
|
|
|
|
|
static DBusConnection *connection;
|
|
|
|
static GHashTable *uuid_hash = NULL;
|
|
|
|
static GHashTable *adapter_address_hash = NULL;
|
2011-01-20 11:10:12 +00:00
|
|
|
static gint bluetooth_refcount;
|
2011-02-08 22:00:58 +00:00
|
|
|
static GSList *server_list = NULL;
|
2011-02-11 09:31:05 +00:00
|
|
|
static const char *adapter_any_name = "any";
|
|
|
|
static char *adapter_any_path;
|
2011-02-08 22:00:58 +00:00
|
|
|
|
2011-02-08 22:00:59 +00:00
|
|
|
#define TIMEOUT 60 /* Timeout for user response (seconds) */
|
|
|
|
|
2011-02-08 22:00:58 +00:00
|
|
|
struct server {
|
|
|
|
guint8 channel;
|
|
|
|
char *sdp_record;
|
2011-02-11 09:31:05 +00:00
|
|
|
guint32 handle;
|
2011-02-08 22:00:58 +00:00
|
|
|
GIOChannel *io;
|
|
|
|
ConnectFunc connect_cb;
|
|
|
|
gpointer user_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cb_data {
|
|
|
|
struct server *server;
|
|
|
|
char *path;
|
|
|
|
guint source;
|
2011-02-08 22:00:59 +00:00
|
|
|
GIOChannel *io;
|
2011-02-08 22:00:58 +00:00
|
|
|
};
|
2010-06-14 18:04:22 +00:00
|
|
|
|
2010-06-22 03:48:15 +00:00
|
|
|
void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
|
|
|
|
char *buf, int size)
|
2010-06-14 18:04:27 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0, j = 0; adapter_addr[j] && i < size - 1; j++)
|
|
|
|
if (adapter_addr[j] >= '0' && adapter_addr[j] <= '9')
|
|
|
|
buf[i++] = adapter_addr[j];
|
|
|
|
else if (adapter_addr[j] >= 'A' && adapter_addr[j] <= 'F')
|
|
|
|
buf[i++] = adapter_addr[j];
|
|
|
|
|
|
|
|
if (i < size - 1)
|
|
|
|
buf[i++] = '_';
|
|
|
|
|
|
|
|
for (j = 0; dev_addr[j] && i < size - 1; j++)
|
|
|
|
if (dev_addr[j] >= '0' && dev_addr[j] <= '9')
|
|
|
|
buf[i++] = dev_addr[j];
|
|
|
|
else if (dev_addr[j] >= 'A' && dev_addr[j] <= 'F')
|
|
|
|
buf[i++] = dev_addr[j];
|
|
|
|
|
|
|
|
buf[i] = '\0';
|
|
|
|
}
|
|
|
|
|
2010-06-17 10:22:05 +00:00
|
|
|
int bluetooth_send_with_reply(const char *path, const char *interface,
|
2011-09-13 18:58:31 +00:00
|
|
|
const char *method, DBusPendingCall **call,
|
2010-06-14 18:04:23 +00:00
|
|
|
DBusPendingCallNotifyFunction cb,
|
|
|
|
void *user_data, DBusFreeFunction free_func,
|
|
|
|
int timeout, int type, ...)
|
|
|
|
{
|
|
|
|
DBusMessage *msg;
|
2011-09-13 18:58:31 +00:00
|
|
|
DBusPendingCall *c;
|
2010-06-14 18:04:23 +00:00
|
|
|
va_list args;
|
|
|
|
int err;
|
|
|
|
|
2010-06-17 10:22:05 +00:00
|
|
|
msg = dbus_message_new_method_call(BLUEZ_SERVICE, path,
|
|
|
|
interface, method);
|
2010-11-27 19:39:02 +00:00
|
|
|
if (msg == NULL) {
|
2010-06-14 18:04:23 +00:00
|
|
|
ofono_error("Unable to allocate new D-Bus %s message", method);
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(args, type);
|
|
|
|
|
|
|
|
if (!dbus_message_append_args_valist(msg, type, args)) {
|
|
|
|
va_end(args);
|
|
|
|
err = -EIO;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (timeout > 0)
|
|
|
|
timeout *= 1000;
|
|
|
|
|
2011-09-13 18:58:31 +00:00
|
|
|
if (!dbus_connection_send_with_reply(connection, msg, &c, timeout)) {
|
2010-06-14 18:04:23 +00:00
|
|
|
ofono_error("Sending %s failed", method);
|
|
|
|
err = -EIO;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-09-13 18:58:31 +00:00
|
|
|
if (call != NULL)
|
|
|
|
*call = c;
|
|
|
|
|
|
|
|
dbus_pending_call_set_notify(c, cb, user_data, free_func);
|
|
|
|
dbus_pending_call_unref(c);
|
|
|
|
|
2010-06-14 18:04:23 +00:00
|
|
|
dbus_message_unref(msg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (free_func && user_data)
|
|
|
|
free_func(user_data);
|
|
|
|
|
|
|
|
if (msg)
|
|
|
|
dbus_message_unref(msg);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-06-14 18:04:24 +00:00
|
|
|
typedef void (*PropertyHandler)(DBusMessageIter *iter, gpointer user_data);
|
|
|
|
|
|
|
|
struct property_handler {
|
|
|
|
const char *property;
|
|
|
|
PropertyHandler callback;
|
|
|
|
gpointer user_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
static gint property_handler_compare(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
const struct property_handler *handler = a;
|
|
|
|
const char *property = b;
|
|
|
|
|
|
|
|
return strcmp(handler->property, property);
|
|
|
|
}
|
|
|
|
|
2010-06-17 10:30:25 +00:00
|
|
|
void bluetooth_parse_properties(DBusMessage *reply, const char *property, ...)
|
2010-06-14 18:04:24 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
GSList *prop_handlers = NULL;
|
|
|
|
DBusMessageIter array, dict;
|
|
|
|
|
|
|
|
va_start(args, property);
|
|
|
|
|
|
|
|
while (property != NULL) {
|
|
|
|
struct property_handler *handler =
|
|
|
|
g_new0(struct property_handler, 1);
|
|
|
|
|
|
|
|
handler->property = property;
|
|
|
|
handler->callback = va_arg(args, PropertyHandler);
|
|
|
|
handler->user_data = va_arg(args, gpointer);
|
|
|
|
|
|
|
|
property = va_arg(args, const char *);
|
|
|
|
|
|
|
|
prop_handlers = g_slist_prepend(prop_handlers, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (dbus_message_iter_init(reply, &array) == FALSE)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&array, &dict);
|
|
|
|
|
|
|
|
while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
|
|
|
|
DBusMessageIter entry, value;
|
|
|
|
const char *key;
|
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&dict, &entry);
|
|
|
|
|
|
|
|
if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&entry, &key);
|
|
|
|
|
|
|
|
dbus_message_iter_next(&entry);
|
|
|
|
|
|
|
|
if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&entry, &value);
|
|
|
|
|
|
|
|
l = g_slist_find_custom(prop_handlers, key,
|
|
|
|
property_handler_compare);
|
|
|
|
|
|
|
|
if (l) {
|
|
|
|
struct property_handler *handler = l->data;
|
|
|
|
|
|
|
|
handler->callback(&value, handler->user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_message_iter_next(&dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2016-04-22 13:10:06 +00:00
|
|
|
g_slist_free_full(prop_handlers, g_free);
|
2010-06-14 18:04:24 +00:00
|
|
|
}
|
|
|
|
|
2011-07-01 07:12:23 +00:00
|
|
|
static void parse_uuids(DBusMessageIter *array, gpointer user_data)
|
2010-06-20 08:51:20 +00:00
|
|
|
{
|
2011-07-01 07:12:23 +00:00
|
|
|
GSList **uuids = user_data;
|
2010-06-20 08:51:20 +00:00
|
|
|
DBusMessageIter value;
|
|
|
|
|
|
|
|
if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(array, &value);
|
|
|
|
|
|
|
|
while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
|
|
|
|
const char *uuid;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&value, &uuid);
|
|
|
|
|
2011-07-01 07:12:23 +00:00
|
|
|
*uuids = g_slist_prepend(*uuids, (char *) uuid);
|
2010-06-20 08:51:20 +00:00
|
|
|
|
|
|
|
dbus_message_iter_next(&value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-20 08:51:19 +00:00
|
|
|
static void parse_string(DBusMessageIter *iter, gpointer user_data)
|
|
|
|
{
|
|
|
|
char **str = user_data;
|
|
|
|
int arg_type = dbus_message_iter_get_arg_type(iter);
|
|
|
|
|
|
|
|
if (arg_type != DBUS_TYPE_OBJECT_PATH && arg_type != DBUS_TYPE_STRING)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(iter, str);
|
|
|
|
}
|
|
|
|
|
2011-07-04 07:32:16 +00:00
|
|
|
static void bluetooth_probe(GSList *uuids, const char *path,
|
|
|
|
const char *device, const char *adapter,
|
|
|
|
const char *alias)
|
|
|
|
{
|
|
|
|
for (; uuids; uuids = uuids->next) {
|
|
|
|
struct bluetooth_profile *driver;
|
|
|
|
const char *uuid = uuids->data;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
driver = g_hash_table_lookup(uuid_hash, uuid);
|
|
|
|
if (driver == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
err = driver->probe(path, device, adapter, alias);
|
2012-06-25 08:54:35 +00:00
|
|
|
if (err == 0 || err == -EALREADY)
|
2011-07-04 07:32:16 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
ofono_error("%s probe: %s (%d)", driver->name, strerror(-err),
|
|
|
|
-err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-20 08:51:20 +00:00
|
|
|
static void device_properties_cb(DBusPendingCall *call, gpointer user_data)
|
|
|
|
{
|
|
|
|
DBusMessage *reply;
|
|
|
|
const char *path = user_data;
|
|
|
|
const char *adapter = NULL;
|
|
|
|
const char *adapter_addr = NULL;
|
|
|
|
const char *device_addr = NULL;
|
|
|
|
const char *alias = NULL;
|
2011-02-17 22:00:47 +00:00
|
|
|
struct DBusError derr;
|
2011-07-01 07:12:23 +00:00
|
|
|
GSList *uuids = NULL;
|
2010-06-20 08:51:20 +00:00
|
|
|
|
|
|
|
reply = dbus_pending_call_steal_reply(call);
|
|
|
|
|
2011-02-17 22:00:47 +00:00
|
|
|
dbus_error_init(&derr);
|
2010-06-20 08:51:20 +00:00
|
|
|
|
2011-02-17 22:00:47 +00:00
|
|
|
if (dbus_set_error_from_message(&derr, reply)) {
|
|
|
|
ofono_error("Device.GetProperties replied an error: %s, %s",
|
|
|
|
derr.name, derr.message);
|
|
|
|
dbus_error_free(&derr);
|
2010-06-20 08:51:20 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-02-17 22:00:49 +00:00
|
|
|
DBG("");
|
|
|
|
|
2011-07-01 07:12:23 +00:00
|
|
|
bluetooth_parse_properties(reply, "UUIDs", parse_uuids, &uuids,
|
2010-06-20 08:51:20 +00:00
|
|
|
"Adapter", parse_string, &adapter,
|
|
|
|
"Address", parse_string, &device_addr,
|
|
|
|
"Alias", parse_string, &alias, NULL);
|
|
|
|
|
|
|
|
if (adapter)
|
|
|
|
adapter_addr = g_hash_table_lookup(adapter_address_hash,
|
|
|
|
adapter);
|
|
|
|
|
2011-07-04 07:32:16 +00:00
|
|
|
if (!device_addr || !adapter_addr)
|
2011-07-01 07:12:23 +00:00
|
|
|
goto done;
|
|
|
|
|
2011-07-04 07:32:16 +00:00
|
|
|
bluetooth_probe(uuids, path, device_addr, adapter_addr, alias);
|
2010-06-20 08:51:20 +00:00
|
|
|
|
|
|
|
done:
|
2011-07-01 07:12:23 +00:00
|
|
|
g_slist_free(uuids);
|
2010-06-20 08:51:20 +00:00
|
|
|
dbus_message_unref(reply);
|
|
|
|
}
|
|
|
|
|
2010-06-20 08:51:19 +00:00
|
|
|
static void parse_devices(DBusMessageIter *array, gpointer user_data)
|
|
|
|
{
|
|
|
|
DBusMessageIter value;
|
|
|
|
GSList **device_list = user_data;
|
|
|
|
|
|
|
|
DBG("");
|
|
|
|
|
|
|
|
if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(array, &value);
|
|
|
|
|
|
|
|
while (dbus_message_iter_get_arg_type(&value)
|
|
|
|
== DBUS_TYPE_OBJECT_PATH) {
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&value, &path);
|
|
|
|
|
|
|
|
*device_list = g_slist_prepend(*device_list, (gpointer) path);
|
|
|
|
|
|
|
|
dbus_message_iter_next(&value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-15 23:19:47 +00:00
|
|
|
static gboolean property_changed(DBusConnection *conn, DBusMessage *msg,
|
2010-06-20 08:51:22 +00:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
const char *property;
|
|
|
|
DBusMessageIter iter;
|
|
|
|
|
|
|
|
dbus_message_iter_init(msg, &iter);
|
|
|
|
|
|
|
|
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&iter, &property);
|
|
|
|
if (g_str_equal(property, "UUIDs") == TRUE) {
|
2011-07-01 07:12:23 +00:00
|
|
|
GSList *uuids = NULL;
|
2010-06-20 08:51:22 +00:00
|
|
|
const char *path = dbus_message_get_path(msg);
|
|
|
|
DBusMessageIter variant;
|
|
|
|
|
|
|
|
if (!dbus_message_iter_next(&iter))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&iter, &variant);
|
|
|
|
|
2011-07-01 07:12:23 +00:00
|
|
|
parse_uuids(&variant, &uuids);
|
2010-06-20 08:51:22 +00:00
|
|
|
|
|
|
|
/* We need the full set of properties to be able to create
|
|
|
|
* the modem properly, including Adapter and Alias, so
|
|
|
|
* refetch everything again
|
|
|
|
*/
|
2011-07-01 07:12:23 +00:00
|
|
|
if (uuids)
|
2010-06-20 08:51:22 +00:00
|
|
|
bluetooth_send_with_reply(path, BLUEZ_DEVICE_INTERFACE,
|
2011-09-13 18:58:31 +00:00
|
|
|
"GetProperties", NULL,
|
|
|
|
device_properties_cb, g_strdup(path),
|
|
|
|
g_free, -1, DBUS_TYPE_INVALID);
|
2010-06-20 08:51:22 +00:00
|
|
|
} else if (g_str_equal(property, "Alias") == TRUE) {
|
|
|
|
const char *path = dbus_message_get_path(msg);
|
|
|
|
struct bluetooth_profile *profile;
|
|
|
|
const char *alias = NULL;
|
|
|
|
DBusMessageIter variant;
|
|
|
|
GHashTableIter hash_iter;
|
|
|
|
gpointer key, value;
|
|
|
|
|
|
|
|
if (!dbus_message_iter_next(&iter))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&iter, &variant);
|
|
|
|
|
|
|
|
parse_string(&variant, &alias);
|
|
|
|
|
|
|
|
g_hash_table_iter_init(&hash_iter, uuid_hash);
|
|
|
|
while (g_hash_table_iter_next(&hash_iter, &key, &value)) {
|
|
|
|
profile = value;
|
|
|
|
if (profile->set_alias)
|
|
|
|
profile->set_alias(path, alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-06-20 08:51:19 +00:00
|
|
|
static void adapter_properties_cb(DBusPendingCall *call, gpointer user_data)
|
|
|
|
{
|
|
|
|
const char *path = user_data;
|
|
|
|
DBusMessage *reply;
|
2011-02-17 22:00:47 +00:00
|
|
|
DBusError derr;
|
2010-06-20 08:51:19 +00:00
|
|
|
GSList *device_list = NULL;
|
|
|
|
GSList *l;
|
|
|
|
const char *addr;
|
|
|
|
|
|
|
|
reply = dbus_pending_call_steal_reply(call);
|
|
|
|
|
2011-02-17 22:00:47 +00:00
|
|
|
dbus_error_init(&derr);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&derr, reply)) {
|
|
|
|
ofono_error("Adapter.GetProperties replied an error: %s, %s",
|
|
|
|
derr.name, derr.message);
|
|
|
|
dbus_error_free(&derr);
|
2010-06-20 08:51:19 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-02-17 22:00:49 +00:00
|
|
|
DBG("");
|
|
|
|
|
2010-06-22 03:48:15 +00:00
|
|
|
bluetooth_parse_properties(reply,
|
|
|
|
"Devices", parse_devices, &device_list,
|
|
|
|
"Address", parse_string, &addr,
|
|
|
|
NULL);
|
2010-06-20 08:51:19 +00:00
|
|
|
|
|
|
|
DBG("Adapter Address: %s, Path: %s", addr, path);
|
|
|
|
g_hash_table_insert(adapter_address_hash,
|
|
|
|
g_strdup(path), g_strdup(addr));
|
|
|
|
|
|
|
|
for (l = device_list; l; l = l->next) {
|
|
|
|
const char *device = l->data;
|
|
|
|
|
|
|
|
bluetooth_send_with_reply(device, BLUEZ_DEVICE_INTERFACE,
|
2011-09-13 18:58:31 +00:00
|
|
|
"GetProperties", NULL,
|
|
|
|
device_properties_cb, g_strdup(device),
|
|
|
|
g_free, -1, DBUS_TYPE_INVALID);
|
2010-06-20 08:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
g_slist_free(device_list);
|
|
|
|
dbus_message_unref(reply);
|
|
|
|
}
|
|
|
|
|
2011-02-04 21:46:17 +00:00
|
|
|
static void get_adapter_properties(const char *path, const char *handle,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
bluetooth_send_with_reply(path, BLUEZ_ADAPTER_INTERFACE,
|
2011-09-13 18:58:31 +00:00
|
|
|
"GetProperties", NULL, adapter_properties_cb,
|
2011-02-04 21:46:17 +00:00
|
|
|
g_strdup(path), g_free, -1, DBUS_TYPE_INVALID);
|
|
|
|
}
|
|
|
|
|
2011-02-11 09:31:05 +00:00
|
|
|
static void remove_record(struct server *server)
|
2011-02-08 22:00:58 +00:00
|
|
|
{
|
|
|
|
DBusMessage *msg;
|
|
|
|
|
2011-02-17 22:00:48 +00:00
|
|
|
if (server->handle == 0)
|
|
|
|
return;
|
|
|
|
|
2011-02-11 09:31:05 +00:00
|
|
|
msg = dbus_message_new_method_call(BLUEZ_SERVICE, adapter_any_path,
|
2011-02-08 22:00:58 +00:00
|
|
|
BLUEZ_SERVICE_INTERFACE,
|
|
|
|
"RemoveRecord");
|
|
|
|
if (msg == NULL) {
|
|
|
|
ofono_error("Unable to allocate D-Bus RemoveRecord message");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-11 09:31:05 +00:00
|
|
|
dbus_message_append_args(msg, DBUS_TYPE_UINT32, &server->handle,
|
2011-02-08 22:00:58 +00:00
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
g_dbus_send_message(connection, msg);
|
|
|
|
|
2011-02-11 09:31:05 +00:00
|
|
|
ofono_info("Unregistered handle for channel %d: 0x%x",
|
|
|
|
server->channel, server->handle);
|
2011-02-08 22:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cb_data_destroy(gpointer data)
|
|
|
|
{
|
|
|
|
struct cb_data *cb_data = data;
|
|
|
|
|
2011-02-09 02:52:41 +00:00
|
|
|
if (cb_data->source != 0)
|
|
|
|
g_source_remove(cb_data->source);
|
|
|
|
|
|
|
|
g_free(cb_data->path);
|
2011-02-08 22:00:58 +00:00
|
|
|
g_free(cb_data);
|
|
|
|
}
|
|
|
|
|
2011-02-08 22:00:59 +00:00
|
|
|
static void cancel_authorization(struct cb_data *user_data)
|
|
|
|
{
|
|
|
|
DBusMessage *msg;
|
|
|
|
|
|
|
|
msg = dbus_message_new_method_call(BLUEZ_SERVICE, user_data->path,
|
|
|
|
BLUEZ_SERVICE_INTERFACE,
|
|
|
|
"CancelAuthorization");
|
|
|
|
|
|
|
|
if (msg == NULL) {
|
|
|
|
ofono_error("Unable to allocate D-Bus CancelAuthorization"
|
|
|
|
" message");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_dbus_send_message(connection, msg);
|
|
|
|
}
|
|
|
|
|
2011-02-08 22:00:58 +00:00
|
|
|
static gboolean client_event(GIOChannel *chan, GIOCondition cond, gpointer data)
|
|
|
|
{
|
|
|
|
struct cb_data *cb_data = data;
|
2011-02-08 22:00:59 +00:00
|
|
|
|
2011-02-09 02:52:41 +00:00
|
|
|
cancel_authorization(cb_data);
|
|
|
|
cb_data->source = 0;
|
2011-02-08 22:00:59 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void auth_cb(DBusPendingCall *call, gpointer user_data)
|
|
|
|
{
|
|
|
|
struct cb_data *cb_data = user_data;
|
|
|
|
struct server *server = cb_data->server;
|
|
|
|
DBusMessage *reply = dbus_pending_call_steal_reply(call);
|
|
|
|
DBusError derr;
|
|
|
|
GError *err = NULL;
|
|
|
|
|
|
|
|
dbus_error_init(&derr);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&derr, reply)) {
|
|
|
|
ofono_error("RequestAuthorization error: %s, %s",
|
|
|
|
derr.name, derr.message);
|
|
|
|
|
|
|
|
if (dbus_error_has_name(&derr, DBUS_ERROR_NO_REPLY))
|
|
|
|
cancel_authorization(cb_data);
|
|
|
|
|
|
|
|
dbus_error_free(&derr);
|
2011-02-09 02:52:41 +00:00
|
|
|
} else {
|
|
|
|
ofono_info("RequestAuthorization succeeded");
|
2011-02-08 22:00:59 +00:00
|
|
|
|
2011-02-09 02:52:41 +00:00
|
|
|
if (!bt_io_accept(cb_data->io, server->connect_cb,
|
|
|
|
server->user_data, NULL, &err)) {
|
|
|
|
ofono_error("%s", err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
}
|
2011-02-08 22:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dbus_message_unref(reply);
|
2011-02-08 22:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void new_connection(GIOChannel *io, gpointer user_data)
|
|
|
|
{
|
|
|
|
struct server *server = user_data;
|
2011-02-09 03:10:27 +00:00
|
|
|
struct cb_data *cbd;
|
2011-02-08 22:00:59 +00:00
|
|
|
const char *addr;
|
2011-02-08 22:00:58 +00:00
|
|
|
GError *err = NULL;
|
|
|
|
char laddress[18], raddress[18];
|
|
|
|
guint8 channel;
|
2011-02-08 22:00:59 +00:00
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer key, value;
|
2011-02-09 03:10:27 +00:00
|
|
|
const char *path;
|
2011-02-08 22:00:58 +00:00
|
|
|
|
|
|
|
bt_io_get(io, BT_IO_RFCOMM, &err, BT_IO_OPT_SOURCE, laddress,
|
|
|
|
BT_IO_OPT_DEST, raddress,
|
|
|
|
BT_IO_OPT_CHANNEL, &channel,
|
|
|
|
BT_IO_OPT_INVALID);
|
|
|
|
if (err) {
|
|
|
|
ofono_error("%s", err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ofono_info("New connection for %s on channel %u from: %s,", laddress,
|
|
|
|
channel, raddress);
|
|
|
|
|
2011-02-09 03:10:27 +00:00
|
|
|
path = NULL;
|
2011-02-08 22:00:59 +00:00
|
|
|
g_hash_table_iter_init(&iter, adapter_address_hash);
|
|
|
|
|
|
|
|
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
2011-02-09 03:10:27 +00:00
|
|
|
if (g_str_equal(laddress, value) == TRUE) {
|
|
|
|
path = key;
|
2011-02-08 22:00:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-09 03:10:27 +00:00
|
|
|
if (path == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cbd = g_try_new0(struct cb_data, 1);
|
|
|
|
if (cbd == NULL) {
|
|
|
|
ofono_error("Unable to allocate client cb_data structure");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cbd->path = g_strdup(path);
|
|
|
|
cbd->server = server;
|
|
|
|
cbd->io = io;
|
2011-02-08 22:00:59 +00:00
|
|
|
|
|
|
|
addr = raddress;
|
2011-02-09 03:10:27 +00:00
|
|
|
|
|
|
|
if (bluetooth_send_with_reply(path, BLUEZ_SERVICE_INTERFACE,
|
2011-09-13 18:58:31 +00:00
|
|
|
"RequestAuthorization", NULL,
|
2011-02-09 03:10:27 +00:00
|
|
|
auth_cb, cbd, cb_data_destroy,
|
2011-02-09 02:52:41 +00:00
|
|
|
TIMEOUT, DBUS_TYPE_STRING, &addr,
|
2011-02-11 09:31:05 +00:00
|
|
|
DBUS_TYPE_UINT32, &server->handle,
|
2011-02-09 03:10:27 +00:00
|
|
|
DBUS_TYPE_INVALID) < 0) {
|
2011-02-08 22:00:59 +00:00
|
|
|
ofono_error("Request Bluetooth authorization failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-11 09:31:05 +00:00
|
|
|
ofono_info("RequestAuthorization(%s, 0x%x)", raddress, server->handle);
|
2011-02-08 22:00:59 +00:00
|
|
|
|
2011-02-09 03:10:27 +00:00
|
|
|
cbd->source = g_io_add_watch(io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
|
|
|
|
client_event, cbd);
|
2011-02-08 22:00:58 +00:00
|
|
|
}
|
|
|
|
|
2011-02-17 22:00:48 +00:00
|
|
|
static void remove_service_handle(gpointer data, gpointer user_data)
|
|
|
|
{
|
|
|
|
struct server *server = data;
|
|
|
|
|
|
|
|
server->handle = 0;
|
|
|
|
}
|
|
|
|
|
2011-02-08 22:00:58 +00:00
|
|
|
static void add_record_cb(DBusPendingCall *call, gpointer user_data)
|
|
|
|
{
|
2011-02-11 09:31:05 +00:00
|
|
|
struct server *server = user_data;
|
2011-02-08 22:00:58 +00:00
|
|
|
DBusMessage *reply = dbus_pending_call_steal_reply(call);
|
|
|
|
DBusError derr;
|
|
|
|
|
|
|
|
dbus_error_init(&derr);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&derr, reply)) {
|
|
|
|
ofono_error("Replied with an error: %s, %s",
|
|
|
|
derr.name, derr.message);
|
|
|
|
dbus_error_free(&derr);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-02-11 09:31:05 +00:00
|
|
|
dbus_message_get_args(reply, NULL, DBUS_TYPE_UINT32, &server->handle,
|
2011-02-08 22:00:58 +00:00
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
2011-02-11 09:31:05 +00:00
|
|
|
ofono_info("Registered handle for channel %d: 0x%x",
|
|
|
|
server->channel, server->handle);
|
2011-02-08 22:00:58 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
dbus_message_unref(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_record(gpointer data, gpointer user_data)
|
|
|
|
{
|
|
|
|
struct server *server = data;
|
|
|
|
|
|
|
|
if (server->sdp_record == NULL)
|
|
|
|
return;
|
|
|
|
|
2011-02-11 09:31:05 +00:00
|
|
|
bluetooth_send_with_reply(adapter_any_path,
|
|
|
|
BLUEZ_SERVICE_INTERFACE, "AddRecord",
|
2011-09-13 18:58:31 +00:00
|
|
|
NULL, add_record_cb, server, NULL, -1,
|
2011-02-11 09:31:05 +00:00
|
|
|
DBUS_TYPE_STRING, &server->sdp_record,
|
|
|
|
DBUS_TYPE_INVALID);
|
2011-02-08 22:00:58 +00:00
|
|
|
}
|
|
|
|
|
2011-02-17 22:00:45 +00:00
|
|
|
static void find_adapter_cb(DBusPendingCall *call, gpointer user_data)
|
|
|
|
{
|
|
|
|
DBusMessage *reply = dbus_pending_call_steal_reply(call);
|
|
|
|
DBusError derr;
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
dbus_error_init(&derr);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&derr, reply)) {
|
|
|
|
ofono_error("Replied with an error: %s, %s",
|
|
|
|
derr.name, derr.message);
|
|
|
|
dbus_error_free(&derr);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_message_get_args(reply, NULL, DBUS_TYPE_OBJECT_PATH, &path,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
adapter_any_path = g_strdup(path);
|
|
|
|
|
2016-04-22 13:10:06 +00:00
|
|
|
g_slist_foreach(server_list, add_record, NULL);
|
2011-02-17 22:00:45 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
dbus_message_unref(reply);
|
|
|
|
}
|
|
|
|
|
2012-07-15 23:19:47 +00:00
|
|
|
static gboolean adapter_added(DBusConnection *conn, DBusMessage *message,
|
2010-06-20 08:51:21 +00:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
2011-05-27 17:10:04 +00:00
|
|
|
bluetooth_send_with_reply(path, BLUEZ_ADAPTER_INTERFACE,
|
2011-09-13 18:58:31 +00:00
|
|
|
"GetProperties", NULL, adapter_properties_cb,
|
|
|
|
g_strdup(path), g_free, -1, DBUS_TYPE_INVALID);
|
2010-06-20 08:51:21 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-07-04 07:32:16 +00:00
|
|
|
static void bluetooth_remove(gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
|
|
|
struct bluetooth_profile *profile = value;
|
|
|
|
|
|
|
|
profile->remove(user_data);
|
|
|
|
}
|
|
|
|
|
2012-07-15 23:19:47 +00:00
|
|
|
static gboolean adapter_removed(DBusConnection *conn,
|
2010-06-20 08:51:21 +00:00
|
|
|
DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
if (dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
|
2011-07-04 07:32:16 +00:00
|
|
|
DBUS_TYPE_INVALID) == FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
g_hash_table_foreach(uuid_hash, bluetooth_remove, (gpointer) path);
|
|
|
|
g_hash_table_remove(adapter_address_hash, path);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-07-15 23:19:47 +00:00
|
|
|
static gboolean device_removed(DBusConnection *conn,
|
2011-07-04 07:32:16 +00:00
|
|
|
DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
if (dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
|
|
|
|
DBUS_TYPE_INVALID) == FALSE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
g_hash_table_foreach(uuid_hash, bluetooth_remove, (gpointer) path);
|
2010-06-20 08:51:21 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-06-20 08:51:19 +00:00
|
|
|
static void parse_adapters(DBusMessageIter *array, gpointer user_data)
|
|
|
|
{
|
|
|
|
DBusMessageIter value;
|
|
|
|
|
|
|
|
DBG("");
|
|
|
|
|
|
|
|
if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(array, &value);
|
|
|
|
|
|
|
|
while (dbus_message_iter_get_arg_type(&value)
|
|
|
|
== DBUS_TYPE_OBJECT_PATH) {
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&value, &path);
|
|
|
|
|
|
|
|
DBG("Calling GetProperties on %s", path);
|
|
|
|
|
|
|
|
bluetooth_send_with_reply(path, BLUEZ_ADAPTER_INTERFACE,
|
2011-09-13 18:58:31 +00:00
|
|
|
"GetProperties", NULL, adapter_properties_cb,
|
2010-06-20 08:51:19 +00:00
|
|
|
g_strdup(path), g_free, -1, DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
dbus_message_iter_next(&value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void manager_properties_cb(DBusPendingCall *call, gpointer user_data)
|
|
|
|
{
|
|
|
|
DBusMessage *reply;
|
2011-02-17 22:00:47 +00:00
|
|
|
DBusError derr;
|
2010-06-20 08:51:19 +00:00
|
|
|
|
|
|
|
reply = dbus_pending_call_steal_reply(call);
|
|
|
|
|
2011-02-17 22:00:47 +00:00
|
|
|
dbus_error_init(&derr);
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&derr, reply)) {
|
|
|
|
ofono_error("Manager.GetProperties() replied an error: %s, %s",
|
|
|
|
derr.name, derr.message);
|
|
|
|
dbus_error_free(&derr);
|
2010-06-20 08:51:19 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG("");
|
|
|
|
|
2010-06-22 03:48:15 +00:00
|
|
|
bluetooth_parse_properties(reply, "Adapters", parse_adapters, NULL,
|
|
|
|
NULL);
|
2010-06-20 08:51:19 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
dbus_message_unref(reply);
|
|
|
|
}
|
|
|
|
|
2012-07-15 23:19:47 +00:00
|
|
|
static void bluetooth_connect(DBusConnection *conn, void *user_data)
|
2011-02-17 22:00:46 +00:00
|
|
|
{
|
|
|
|
bluetooth_send_with_reply("/", BLUEZ_MANAGER_INTERFACE, "GetProperties",
|
2011-09-13 18:58:31 +00:00
|
|
|
NULL, manager_properties_cb, NULL, NULL, -1,
|
2011-02-17 22:00:46 +00:00
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
|
|
|
bluetooth_send_with_reply("/", BLUEZ_MANAGER_INTERFACE, "FindAdapter",
|
2011-09-13 18:58:31 +00:00
|
|
|
NULL, find_adapter_cb, NULL, NULL, -1,
|
2011-02-17 22:00:46 +00:00
|
|
|
DBUS_TYPE_STRING, &adapter_any_name,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
}
|
|
|
|
|
2012-07-15 23:19:47 +00:00
|
|
|
static void bluetooth_disconnect(DBusConnection *conn, void *user_data)
|
2010-06-20 08:51:21 +00:00
|
|
|
{
|
2010-11-27 19:39:02 +00:00
|
|
|
if (uuid_hash == NULL)
|
2010-06-20 08:51:21 +00:00
|
|
|
return;
|
|
|
|
|
2011-07-04 07:32:16 +00:00
|
|
|
g_hash_table_foreach(uuid_hash, bluetooth_remove, NULL);
|
2011-02-17 22:00:48 +00:00
|
|
|
|
2016-04-22 13:10:06 +00:00
|
|
|
g_slist_foreach(server_list, remove_service_handle, NULL);
|
2010-06-20 08:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint bluetooth_watch;
|
|
|
|
static guint adapter_added_watch;
|
|
|
|
static guint adapter_removed_watch;
|
2011-07-04 07:32:16 +00:00
|
|
|
static guint device_removed_watch;
|
2010-06-20 08:51:22 +00:00
|
|
|
static guint property_watch;
|
2010-06-20 08:51:21 +00:00
|
|
|
|
2011-01-20 11:10:12 +00:00
|
|
|
static void bluetooth_ref(void)
|
2010-06-14 18:04:22 +00:00
|
|
|
{
|
2011-01-20 11:10:12 +00:00
|
|
|
if (bluetooth_refcount > 0)
|
|
|
|
goto increment;
|
2010-06-14 18:04:22 +00:00
|
|
|
|
|
|
|
connection = ofono_dbus_get_connection();
|
|
|
|
|
2010-06-20 08:51:21 +00:00
|
|
|
bluetooth_watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
|
2011-02-17 22:00:46 +00:00
|
|
|
bluetooth_connect,
|
|
|
|
bluetooth_disconnect, NULL, NULL);
|
2010-06-20 08:51:21 +00:00
|
|
|
|
2012-06-28 13:59:19 +00:00
|
|
|
adapter_added_watch = g_dbus_add_signal_watch(connection, BLUEZ_SERVICE,
|
|
|
|
NULL, BLUEZ_MANAGER_INTERFACE,
|
2010-06-20 08:51:21 +00:00
|
|
|
"AdapterAdded",
|
|
|
|
adapter_added, NULL, NULL);
|
|
|
|
|
2012-06-28 13:59:19 +00:00
|
|
|
adapter_removed_watch = g_dbus_add_signal_watch(connection,
|
|
|
|
BLUEZ_SERVICE, NULL,
|
2010-06-20 08:51:21 +00:00
|
|
|
BLUEZ_MANAGER_INTERFACE,
|
|
|
|
"AdapterRemoved",
|
|
|
|
adapter_removed, NULL, NULL);
|
|
|
|
|
2012-06-28 13:59:19 +00:00
|
|
|
device_removed_watch = g_dbus_add_signal_watch(connection,
|
|
|
|
BLUEZ_SERVICE, NULL,
|
2011-07-04 07:32:16 +00:00
|
|
|
BLUEZ_ADAPTER_INTERFACE,
|
|
|
|
"DeviceRemoved",
|
|
|
|
device_removed, NULL, NULL);
|
|
|
|
|
2012-06-28 13:59:19 +00:00
|
|
|
property_watch = g_dbus_add_signal_watch(connection,
|
|
|
|
BLUEZ_SERVICE, NULL,
|
2010-06-20 08:51:22 +00:00
|
|
|
BLUEZ_DEVICE_INTERFACE,
|
|
|
|
"PropertyChanged",
|
|
|
|
property_changed, NULL, NULL);
|
|
|
|
|
2010-06-20 08:51:21 +00:00
|
|
|
if (bluetooth_watch == 0 || adapter_added_watch == 0 ||
|
2010-06-20 08:51:22 +00:00
|
|
|
adapter_removed_watch == 0 || property_watch == 0) {
|
2010-06-20 08:51:21 +00:00
|
|
|
goto remove;
|
|
|
|
}
|
|
|
|
|
2010-06-14 18:04:22 +00:00
|
|
|
uuid_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
|
|
|
|
g_free, NULL);
|
|
|
|
|
|
|
|
adapter_address_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
|
|
|
|
g_free, g_free);
|
|
|
|
|
2011-01-20 11:10:12 +00:00
|
|
|
increment:
|
|
|
|
g_atomic_int_inc(&bluetooth_refcount);
|
2010-06-20 08:51:19 +00:00
|
|
|
|
2011-01-20 11:10:12 +00:00
|
|
|
return;
|
2010-06-20 08:51:21 +00:00
|
|
|
|
|
|
|
remove:
|
|
|
|
g_dbus_remove_watch(connection, bluetooth_watch);
|
|
|
|
g_dbus_remove_watch(connection, adapter_added_watch);
|
|
|
|
g_dbus_remove_watch(connection, adapter_removed_watch);
|
2010-06-20 08:51:22 +00:00
|
|
|
g_dbus_remove_watch(connection, property_watch);
|
2010-06-14 18:04:22 +00:00
|
|
|
}
|
|
|
|
|
2011-01-20 11:10:12 +00:00
|
|
|
static void bluetooth_unref(void)
|
2010-06-14 18:04:22 +00:00
|
|
|
{
|
2011-01-20 11:10:12 +00:00
|
|
|
if (g_atomic_int_dec_and_test(&bluetooth_refcount) == FALSE)
|
2010-06-14 18:04:22 +00:00
|
|
|
return;
|
|
|
|
|
2011-02-11 09:31:05 +00:00
|
|
|
g_free(adapter_any_path);
|
2011-02-15 19:19:07 +00:00
|
|
|
adapter_any_path = NULL;
|
2011-02-11 09:31:05 +00:00
|
|
|
|
2010-06-20 08:51:21 +00:00
|
|
|
g_dbus_remove_watch(connection, bluetooth_watch);
|
|
|
|
g_dbus_remove_watch(connection, adapter_added_watch);
|
|
|
|
g_dbus_remove_watch(connection, adapter_removed_watch);
|
2010-06-20 08:51:22 +00:00
|
|
|
g_dbus_remove_watch(connection, property_watch);
|
2010-06-20 08:51:21 +00:00
|
|
|
|
2010-06-14 18:04:22 +00:00
|
|
|
g_hash_table_destroy(uuid_hash);
|
|
|
|
g_hash_table_destroy(adapter_address_hash);
|
2011-01-20 11:10:12 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 21:04:15 +00:00
|
|
|
void bluetooth_get_properties()
|
|
|
|
{
|
|
|
|
g_hash_table_foreach(adapter_address_hash,
|
|
|
|
(GHFunc) get_adapter_properties, NULL);
|
|
|
|
}
|
|
|
|
|
2011-01-20 11:10:12 +00:00
|
|
|
int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
|
|
|
|
{
|
|
|
|
bluetooth_ref();
|
|
|
|
|
|
|
|
g_hash_table_insert(uuid_hash, g_strdup(uuid), profile);
|
|
|
|
|
2011-02-04 21:46:17 +00:00
|
|
|
g_hash_table_foreach(adapter_address_hash,
|
|
|
|
(GHFunc) get_adapter_properties, NULL);
|
2011-01-20 11:10:12 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bluetooth_unregister_uuid(const char *uuid)
|
|
|
|
{
|
|
|
|
g_hash_table_remove(uuid_hash, uuid);
|
|
|
|
|
|
|
|
bluetooth_unref();
|
2010-06-14 18:04:22 +00:00
|
|
|
}
|
|
|
|
|
2011-02-08 22:00:58 +00:00
|
|
|
struct server *bluetooth_register_server(guint8 channel, const char *sdp_record,
|
|
|
|
ConnectFunc cb, gpointer user_data)
|
|
|
|
{
|
|
|
|
struct server *server;
|
2011-05-31 16:41:42 +00:00
|
|
|
GError *err = NULL;
|
2011-02-08 22:00:58 +00:00
|
|
|
|
|
|
|
server = g_try_new0(struct server, 1);
|
|
|
|
if (!server)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
server->channel = channel;
|
2011-02-08 22:28:58 +00:00
|
|
|
|
2011-02-08 22:00:58 +00:00
|
|
|
server->io = bt_io_listen(BT_IO_RFCOMM, NULL, new_connection,
|
|
|
|
server, NULL, &err,
|
|
|
|
BT_IO_OPT_CHANNEL, server->channel,
|
|
|
|
BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
|
|
|
|
BT_IO_OPT_INVALID);
|
|
|
|
if (server->io == NULL) {
|
|
|
|
g_error_free(err);
|
|
|
|
g_free(server);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bluetooth_ref();
|
|
|
|
|
|
|
|
if (sdp_record != NULL)
|
|
|
|
server->sdp_record = g_strdup(sdp_record);
|
2011-02-08 22:28:58 +00:00
|
|
|
|
2011-02-08 22:00:58 +00:00
|
|
|
server->connect_cb = cb;
|
|
|
|
server->user_data = user_data;
|
|
|
|
|
|
|
|
server_list = g_slist_prepend(server_list, server);
|
|
|
|
|
2011-02-15 19:18:47 +00:00
|
|
|
if (adapter_any_path != NULL)
|
2011-02-11 09:31:05 +00:00
|
|
|
add_record(server, NULL);
|
|
|
|
|
2011-02-08 22:00:58 +00:00
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bluetooth_unregister_server(struct server *server)
|
|
|
|
{
|
|
|
|
server_list = g_slist_remove(server_list, server);
|
2011-02-11 09:31:05 +00:00
|
|
|
|
|
|
|
remove_record(server);
|
|
|
|
|
|
|
|
if (server->io != NULL) {
|
|
|
|
g_io_channel_shutdown(server->io, TRUE, NULL);
|
|
|
|
g_io_channel_unref(server->io);
|
|
|
|
server->io = NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-08 22:00:58 +00:00
|
|
|
g_free(server->sdp_record);
|
|
|
|
g_free(server);
|
|
|
|
|
|
|
|
bluetooth_unref();
|
|
|
|
}
|
|
|
|
|
2013-01-16 13:31:27 +00:00
|
|
|
OFONO_PLUGIN_DEFINE(bluez4, "Bluetooth Utils Plugins", VERSION,
|
|
|
|
OFONO_PLUGIN_PRIORITY_DEFAULT, NULL, NULL)
|