Commit 7aa5128b authored by Emilio Pozuelo Monfort's avatar Emilio Pozuelo Monfort Committed by Thomas Haller

bluez: move org.bluez Connection() handling to NMBluezDevice

parent e45fdcfd
...@@ -21,6 +21,9 @@ ...@@ -21,6 +21,9 @@
#ifndef NM_BLUEZ_COMMON_H #ifndef NM_BLUEZ_COMMON_H
#define NM_BLUEZ_COMMON_H #define NM_BLUEZ_COMMON_H
#define BLUETOOTH_CONNECT_DUN "dun"
#define BLUETOOTH_CONNECT_NAP "nap"
#define BLUEZ_SERVICE "org.bluez" #define BLUEZ_SERVICE "org.bluez"
#define BLUEZ_MANAGER_PATH "/" #define BLUEZ_MANAGER_PATH "/"
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
*/ */
#include <glib.h> #include <glib.h>
#include <gio/gio.h>
#include <string.h> #include <string.h>
#include <net/ethernet.h> #include <net/ethernet.h>
#include <netinet/ether.h> #include <netinet/ether.h>
...@@ -40,6 +41,8 @@ G_DEFINE_TYPE (NMBluezDevice, nm_bluez_device, G_TYPE_OBJECT) ...@@ -40,6 +41,8 @@ G_DEFINE_TYPE (NMBluezDevice, nm_bluez_device, G_TYPE_OBJECT)
typedef struct { typedef struct {
char *path; char *path;
DBusGProxy *proxy; DBusGProxy *proxy;
DBusGProxy *type_proxy;
gboolean initialized; gboolean initialized;
gboolean usable; gboolean usable;
...@@ -50,6 +53,8 @@ typedef struct { ...@@ -50,6 +53,8 @@ typedef struct {
gint rssi; gint rssi;
gboolean connected; gboolean connected;
char *rfcomm_iface;
NMConnectionProvider *provider; NMConnectionProvider *provider;
GSList *connections; GSList *connections;
} NMBluezDevicePrivate; } NMBluezDevicePrivate;
...@@ -246,6 +251,121 @@ cp_connections_loaded (NMConnectionProvider *provider, NMBluezDevice *self) ...@@ -246,6 +251,121 @@ cp_connections_loaded (NMConnectionProvider *provider, NMBluezDevice *self)
/***********************************************************/ /***********************************************************/
void
nm_bluez_device_call_disconnect (NMBluezDevice *self, gboolean dun)
{
NMBluezDevicePrivate *priv = NM_BLUEZ_DEVICE_GET_PRIVATE (self);
if (!priv->type_proxy)
return;
if (dun) {
/* Don't ever pass NULL through dbus; rfcomm_iface
* might happen to be NULL for some reason.
*/
if (priv->rfcomm_iface)
dbus_g_proxy_call_no_reply (priv->type_proxy, "Disconnect",
G_TYPE_STRING, priv->rfcomm_iface,
G_TYPE_INVALID);
} else {
dbus_g_proxy_call_no_reply (priv->type_proxy, "Disconnect",
G_TYPE_INVALID);
}
g_clear_object (&priv->type_proxy);
}
static void
bluez_connect_cb (DBusGProxy *proxy,
DBusGProxyCall *call_id,
gpointer user_data)
{
GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (user_data);
NMBluezDevice *self = NM_BLUEZ_DEVICE (g_async_result_get_source_object (G_ASYNC_RESULT (result)));
NMBluezDevicePrivate *priv = NM_BLUEZ_DEVICE_GET_PRIVATE (self);
GError *error = NULL;
char *device;
if (dbus_g_proxy_end_call (proxy, call_id, &error,
G_TYPE_STRING, &device,
G_TYPE_INVALID) == FALSE)
g_simple_async_result_take_error (result, error);
else if (!device || !strlen (device)) {
g_simple_async_result_set_error(result, G_IO_ERROR, G_IO_ERROR_FAILED,
"Invalid argument received");
} else {
g_simple_async_result_set_op_res_gpointer (result,
g_strdup (device),
g_free);
priv->rfcomm_iface = device;
}
g_simple_async_result_complete (result);
g_object_unref (result);
}
void
nm_bluez_device_connect_async (NMBluezDevice *self,
gboolean dun,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *simple;
NMBluezDevicePrivate *priv = NM_BLUEZ_DEVICE_GET_PRIVATE (self);
DBusGConnection *connection;
connection = nm_dbus_manager_get_connection (nm_dbus_manager_get ());
simple = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
nm_bluez_device_connect_async);
priv->type_proxy = dbus_g_proxy_new_for_name (connection,
BLUEZ_SERVICE,
priv->path,
dun ? BLUEZ_SERIAL_INTERFACE : BLUEZ_NETWORK_INTERFACE);
if (!priv->type_proxy)
g_simple_async_report_error_in_idle (G_OBJECT (self),
callback,
user_data,
G_IO_ERROR,
G_IO_ERROR_FAILED,
"Unable to create proxy");
else
dbus_g_proxy_begin_call_with_timeout (priv->type_proxy, "Connect",
bluez_connect_cb,
simple,
NULL,
20000,
G_TYPE_STRING, dun ? BLUETOOTH_CONNECT_DUN : BLUETOOTH_CONNECT_NAP,
G_TYPE_INVALID);
}
const char *
nm_bluez_device_connect_finish (NMBluezDevice *self,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
const char *device;
g_return_val_if_fail (g_simple_async_result_is_valid (result,
G_OBJECT (self),
nm_bluez_device_connect_async),
NULL);
simple = (GSimpleAsyncResult *) result;
if (g_simple_async_result_propagate_error (simple, error))
return NULL;
device = (const char *) g_simple_async_result_get_op_res_gpointer (simple);
return device;
}
/***********************************************************/
static guint32 static guint32
convert_uuids_to_capabilities (const char **strings) convert_uuids_to_capabilities (const char **strings)
{ {
...@@ -469,6 +589,8 @@ dispose (GObject *object) ...@@ -469,6 +589,8 @@ dispose (GObject *object)
g_signal_handlers_disconnect_by_func (priv->provider, cp_connection_updated, self); g_signal_handlers_disconnect_by_func (priv->provider, cp_connection_updated, self);
g_signal_handlers_disconnect_by_func (priv->provider, cp_connections_loaded, self); g_signal_handlers_disconnect_by_func (priv->provider, cp_connections_loaded, self);
g_clear_object (&priv->type_proxy);
G_OBJECT_CLASS (nm_bluez_device_parent_class)->dispose (object); G_OBJECT_CLASS (nm_bluez_device_parent_class)->dispose (object);
} }
...@@ -480,6 +602,7 @@ finalize (GObject *object) ...@@ -480,6 +602,7 @@ finalize (GObject *object)
g_free (priv->path); g_free (priv->path);
g_free (priv->address); g_free (priv->address);
g_free (priv->name); g_free (priv->name);
g_free (priv->rfcomm_iface);
g_object_unref (priv->proxy); g_object_unref (priv->proxy);
G_OBJECT_CLASS (nm_bluez_device_parent_class)->finalize (object); G_OBJECT_CLASS (nm_bluez_device_parent_class)->finalize (object);
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <glib.h> #include <glib.h>
#include <glib-object.h> #include <glib-object.h>
#include <gio/gio.h>
#include "nm-connection.h" #include "nm-connection.h"
#include "nm-connection-provider.h" #include "nm-connection-provider.h"
...@@ -77,5 +78,20 @@ gint nm_bluez_device_get_rssi (NMBluezDevice *self); ...@@ -77,5 +78,20 @@ gint nm_bluez_device_get_rssi (NMBluezDevice *self);
gboolean nm_bluez_device_get_connected (NMBluezDevice *self); gboolean nm_bluez_device_get_connected (NMBluezDevice *self);
void
nm_bluez_device_connect_async (NMBluezDevice *self,
gboolean dun,
GAsyncReadyCallback callback,
gpointer user_data);
const char *
nm_bluez_device_connect_finish (NMBluezDevice *self,
GAsyncResult *result,
GError **error);
void
nm_bluez_device_call_disconnect (NMBluezDevice *self,
gboolean dun);
#endif /* NM_BLUEZ_DEVICE_H */ #endif /* NM_BLUEZ_DEVICE_H */
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <netinet/ether.h> #include <netinet/ether.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <gio/gio.h>
#include "nm-glib-compat.h" #include "nm-glib-compat.h"
#include "nm-bluez-common.h" #include "nm-bluez-common.h"
...@@ -48,8 +49,6 @@ ...@@ -48,8 +49,6 @@
#define MM_OLD_DBUS_SERVICE "org.freedesktop.ModemManager" #define MM_OLD_DBUS_SERVICE "org.freedesktop.ModemManager"
#define MM_NEW_DBUS_SERVICE "org.freedesktop.ModemManager1" #define MM_NEW_DBUS_SERVICE "org.freedesktop.ModemManager1"
#define BLUETOOTH_DUN_UUID "dun"
#define BLUETOOTH_NAP_UUID "nap"
G_DEFINE_TYPE (NMDeviceBt, nm_device_bt, NM_TYPE_DEVICE) G_DEFINE_TYPE (NMDeviceBt, nm_device_bt, NM_TYPE_DEVICE)
...@@ -71,8 +70,6 @@ typedef struct { ...@@ -71,8 +70,6 @@ typedef struct {
gboolean connected; gboolean connected;
gboolean have_iface; gboolean have_iface;
DBusGProxy *type_proxy;
char *rfcomm_iface; char *rfcomm_iface;
NMModem *modem; NMModem *modem;
guint32 timeout_id; guint32 timeout_id;
...@@ -711,18 +708,19 @@ check_connect_continue (NMDeviceBt *self) ...@@ -711,18 +708,19 @@ check_connect_continue (NMDeviceBt *self)
} }
static void static void
bluez_connect_cb (DBusGProxy *proxy, bluez_connect_cb (GObject *object,
DBusGProxyCall *call_id, GAsyncResult *res,
void *user_data) void *user_data)
{ {
NMDeviceBt *self = NM_DEVICE_BT (user_data); NMDeviceBt *self = NM_DEVICE_BT (user_data);
NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (self); NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (self);
GError *error = NULL; GError *error = NULL;
char *device; const char *device;
device = nm_bluez_device_connect_finish (NM_BLUEZ_DEVICE (object),
res, &error);
if (dbus_g_proxy_end_call (proxy, call_id, &error, if (!device) {
G_TYPE_STRING, &device,
G_TYPE_INVALID) == FALSE) {
nm_log_warn (LOGD_BT, "Error connecting with bluez: %s", nm_log_warn (LOGD_BT, "Error connecting with bluez: %s",
error && error->message ? error->message : "(unknown)"); error && error->message ? error->message : "(unknown)");
g_clear_error (&error); g_clear_error (&error);
...@@ -733,20 +731,11 @@ bluez_connect_cb (DBusGProxy *proxy, ...@@ -733,20 +731,11 @@ bluez_connect_cb (DBusGProxy *proxy,
return; return;
} }
if (!device || !strlen (device)) {
nm_log_warn (LOGD_BT, "Invalid network device returned by bluez");
nm_device_state_changed (NM_DEVICE (self),
NM_DEVICE_STATE_FAILED,
NM_DEVICE_STATE_REASON_BT_FAILED);
}
if (priv->bt_type == NM_BT_CAPABILITY_DUN) { if (priv->bt_type == NM_BT_CAPABILITY_DUN) {
g_free (priv->rfcomm_iface); g_free (priv->rfcomm_iface);
priv->rfcomm_iface = device; priv->rfcomm_iface = g_strdup (device);
} else if (priv->bt_type == NM_BT_CAPABILITY_NAP) { } else if (priv->bt_type == NM_BT_CAPABILITY_NAP) {
nm_device_set_ip_iface (NM_DEVICE (self), device); nm_device_set_ip_iface (NM_DEVICE (self), device);
g_free (device);
} }
nm_log_dbg (LOGD_BT, "(%s): connect request successful", nm_log_dbg (LOGD_BT, "(%s): connect request successful",
...@@ -819,7 +808,6 @@ static NMActStageReturn ...@@ -819,7 +808,6 @@ static NMActStageReturn
act_stage2_config (NMDevice *device, NMDeviceStateReason *reason) act_stage2_config (NMDevice *device, NMDeviceStateReason *reason)
{ {
NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (device); NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (device);
DBusGConnection *bus;
gboolean dun = FALSE; gboolean dun = FALSE;
NMConnection *connection; NMConnection *connection;
...@@ -843,28 +831,11 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *reason) ...@@ -843,28 +831,11 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *reason)
else else
g_assert_not_reached (); g_assert_not_reached ();
bus = nm_dbus_manager_get_connection (priv->dbus_mgr);
priv->type_proxy = dbus_g_proxy_new_for_name (bus,
BLUEZ_SERVICE,
nm_device_get_udi (device),
dun ? BLUEZ_SERIAL_INTERFACE : BLUEZ_NETWORK_INTERFACE);
if (!priv->type_proxy) {
// FIXME: set a reason code
return NM_ACT_STAGE_RETURN_FAILURE;
}
nm_log_dbg (LOGD_BT, "(%s): requesting connection to the device", nm_log_dbg (LOGD_BT, "(%s): requesting connection to the device",
nm_device_get_iface (device)); nm_device_get_iface (device));
/* Connect to the BT device */ /* Connect to the BT device */
dbus_g_proxy_begin_call_with_timeout (priv->type_proxy, "Connect", nm_bluez_device_connect_async (priv->bt_device, dun, bluez_connect_cb, device);
bluez_connect_cb,
device,
NULL,
20000,
G_TYPE_STRING, dun ? BLUETOOTH_DUN_UUID : BLUETOOTH_NAP_UUID,
G_TYPE_INVALID);
if (priv->timeout_id) if (priv->timeout_id)
g_source_remove (priv->timeout_id); g_source_remove (priv->timeout_id);
...@@ -915,11 +886,14 @@ static void ...@@ -915,11 +886,14 @@ static void
deactivate (NMDevice *device) deactivate (NMDevice *device)
{ {
NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (device); NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (device);
gboolean dun;
dun = priv->bt_type == NM_BT_CAPABILITY_DUN;
priv->have_iface = FALSE; priv->have_iface = FALSE;
priv->connected = FALSE; priv->connected = FALSE;
if (priv->bt_type == NM_BT_CAPABILITY_DUN) { if (dun) {
if (priv->modem) { if (priv->modem) {
nm_modem_deactivate (priv->modem, device); nm_modem_deactivate (priv->modem, device);
...@@ -934,28 +908,10 @@ deactivate (NMDevice *device) ...@@ -934,28 +908,10 @@ deactivate (NMDevice *device)
g_object_unref (priv->modem); g_object_unref (priv->modem);
priv->modem = NULL; priv->modem = NULL;
} }
if (priv->type_proxy) {
/* Don't ever pass NULL through dbus; rfcomm_iface
* might happen to be NULL for some reason.
*/
if (priv->rfcomm_iface) {
dbus_g_proxy_call_no_reply (priv->type_proxy, "Disconnect",
G_TYPE_STRING, priv->rfcomm_iface,
G_TYPE_INVALID);
}
g_object_unref (priv->type_proxy);
priv->type_proxy = NULL;
}
} else if (priv->bt_type == NM_BT_CAPABILITY_NAP) {
if (priv->type_proxy) {
dbus_g_proxy_call_no_reply (priv->type_proxy, "Disconnect",
G_TYPE_INVALID);
g_object_unref (priv->type_proxy);
priv->type_proxy = NULL;
}
} }
nm_bluez_device_call_disconnect (priv->bt_device, dun);
if (priv->timeout_id) { if (priv->timeout_id) {
g_source_remove (priv->timeout_id); g_source_remove (priv->timeout_id);
priv->timeout_id = 0; priv->timeout_id = 0;
...@@ -1209,7 +1165,6 @@ dispose (GObject *object) ...@@ -1209,7 +1165,6 @@ dispose (GObject *object)
} }
priv->dbus_mgr = NULL; priv->dbus_mgr = NULL;
g_clear_object (&priv->type_proxy);
g_clear_object (&priv->modem); g_clear_object (&priv->modem);
g_clear_object (&priv->bt_device); g_clear_object (&priv->bt_device);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment