Blame libnm/nm-device-bt.c

Packit Service a1bd4f
/* SPDX-License-Identifier: LGPL-2.1+ */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2007 - 2008 Novell, Inc.
Packit 5756e2
 * Copyright (C) 2007 - 2012 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
#include "nm-default.h"
Packit 5756e2
Packit 5756e2
#include "nm-device-bt.h"
Packit 5756e2
Packit Service d0b836
#include <linux/if_ether.h>
Packit Service d0b836
Packit 5756e2
#include "nm-setting-connection.h"
Packit 5756e2
#include "nm-setting-bluetooth.h"
Packit 5756e2
#include "nm-utils.h"
Packit 5756e2
#include "nm-object-private.h"
Packit 5756e2
#include "nm-enum-types.h"
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
NM_GOBJECT_PROPERTIES_DEFINE_BASE(PROP_NAME, PROP_BT_CAPABILITIES, );
Packit 5756e2
Packit 5756e2
typedef struct {
Packit Service a1bd4f
    char *  name;
Packit Service a1bd4f
    guint32 bt_capabilities;
Packit 5756e2
} NMDeviceBtPrivate;
Packit 5756e2
Packit 5756e2
struct _NMDeviceBt {
Packit Service a1bd4f
    NMDevice          parent;
Packit Service a1bd4f
    NMDeviceBtPrivate _priv;
Packit 5756e2
};
Packit 5756e2
Packit 5756e2
struct _NMDeviceBtClass {
Packit Service a1bd4f
    NMDeviceClass parent;
Packit 5756e2
};
Packit 5756e2
Packit Service a1bd4f
G_DEFINE_TYPE(NMDeviceBt, nm_device_bt, NM_TYPE_DEVICE)
Packit 5756e2
Packit Service a1bd4f
#define NM_DEVICE_BT_GET_PRIVATE(self) \
Packit Service a1bd4f
    _NM_GET_PRIVATE(self, NMDeviceBt, NM_IS_DEVICE_BT, NMObject, NMDevice)
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_device_bt_get_hw_address: (skip)
Packit 5756e2
 * @device: a #NMDeviceBt
Packit 5756e2
 *
Packit 5756e2
 * Gets the hardware (MAC) address of the #NMDeviceBt
Packit 5756e2
 *
Packit 5756e2
 * Returns: the hardware address. This is the internal string used by the
Packit 5756e2
 * device, and must not be modified.
Packit 5756e2
 *
Packit 5756e2
 * Deprecated: 1.24: Use nm_device_get_hw_address() instead.
Packit 5756e2
 **/
Packit 5756e2
const char *
Packit Service a1bd4f
nm_device_bt_get_hw_address(NMDeviceBt *device)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_DEVICE_BT(device), NULL);
Packit 5756e2
Packit Service a1bd4f
    return nm_device_get_hw_address(NM_DEVICE(device));
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_device_bt_get_name:
Packit 5756e2
 * @device: a #NMDeviceBt
Packit 5756e2
 *
Packit 5756e2
 * Gets the name of the #NMDeviceBt.
Packit 5756e2
 *
Packit 5756e2
 * Returns: the name of the device
Packit 5756e2
 **/
Packit 5756e2
const char *
Packit Service a1bd4f
nm_device_bt_get_name(NMDeviceBt *device)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_DEVICE_BT(device), NULL);
Packit 5756e2
Packit Service a1bd4f
    return NM_DEVICE_BT_GET_PRIVATE(device)->name;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_device_bt_get_capabilities:
Packit 5756e2
 * @device: a #NMDeviceBt
Packit 5756e2
 *
Packit 5756e2
 * Returns the Bluetooth device's usable capabilities.
Packit 5756e2
 *
Packit 5756e2
 * Returns: a combination of #NMBluetoothCapabilities
Packit 5756e2
 **/
Packit 5756e2
NMBluetoothCapabilities
Packit Service a1bd4f
nm_device_bt_get_capabilities(NMDeviceBt *device)
Packit 5756e2
{
Packit Service a1bd4f
    g_return_val_if_fail(NM_IS_DEVICE_BT(device), NM_BT_CAPABILITY_NONE);
Packit 5756e2
Packit Service a1bd4f
    return NM_DEVICE_BT_GET_PRIVATE(device)->bt_capabilities;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static NMBluetoothCapabilities
Packit Service a1bd4f
get_connection_bt_type(NMConnection *connection)
Packit 5756e2
{
Packit Service a1bd4f
    NMSettingBluetooth *s_bt;
Packit Service a1bd4f
    const char *        bt_type;
Packit 5756e2
Packit Service a1bd4f
    s_bt = nm_connection_get_setting_bluetooth(connection);
Packit Service a1bd4f
    if (!s_bt)
Packit Service a1bd4f
        return NM_BT_CAPABILITY_NONE;
Packit 5756e2
Packit Service a1bd4f
    bt_type = nm_setting_bluetooth_get_connection_type(s_bt);
Packit Service a1bd4f
    g_assert(bt_type);
Packit 5756e2
Packit Service a1bd4f
    if (!strcmp(bt_type, NM_SETTING_BLUETOOTH_TYPE_DUN))
Packit Service a1bd4f
        return NM_BT_CAPABILITY_DUN;
Packit Service a1bd4f
    else if (!strcmp(bt_type, NM_SETTING_BLUETOOTH_TYPE_PANU))
Packit Service a1bd4f
        return NM_BT_CAPABILITY_NAP;
Packit 5756e2
Packit Service a1bd4f
    return NM_BT_CAPABILITY_NONE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
connection_compatible(NMDevice *device, NMConnection *connection, GError **error)
Packit 5756e2
{
Packit Service a1bd4f
    NMSettingBluetooth *    s_bt;
Packit Service a1bd4f
    const char *            hw_addr, *setting_addr;
Packit Service a1bd4f
    NMBluetoothCapabilities dev_caps;
Packit Service a1bd4f
    NMBluetoothCapabilities bt_type;
Packit Service a1bd4f
Packit Service a1bd4f
    if (!NM_DEVICE_CLASS(nm_device_bt_parent_class)
Packit Service a1bd4f
             ->connection_compatible(device, connection, error))
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
Packit Service a1bd4f
    if (!nm_connection_is_type(connection, NM_SETTING_BLUETOOTH_SETTING_NAME)
Packit Service a1bd4f
        || !(s_bt = nm_connection_get_setting_bluetooth(connection))) {
Packit Service a1bd4f
        g_set_error(error,
Packit Service a1bd4f
                    NM_DEVICE_ERROR,
Packit Service a1bd4f
                    NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
Packit Service a1bd4f
                    _("The connection was not a Bluetooth connection."));
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (nm_streq0(nm_setting_bluetooth_get_connection_type(s_bt), NM_SETTING_BLUETOOTH_TYPE_NAP)) {
Packit Service a1bd4f
        g_set_error(error,
Packit Service a1bd4f
                    NM_DEVICE_ERROR,
Packit Service a1bd4f
                    NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
Packit Service a1bd4f
                    _("The connection is of Bluetooth NAP type."));
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    /* Check BT address */
Packit Service a1bd4f
    hw_addr = nm_device_get_hw_address(device);
Packit Service a1bd4f
    if (hw_addr) {
Packit Service a1bd4f
        if (!nm_utils_hwaddr_valid(hw_addr, ETH_ALEN)) {
Packit Service a1bd4f
            g_set_error_literal(error,
Packit Service a1bd4f
                                NM_DEVICE_ERROR,
Packit Service a1bd4f
                                NM_DEVICE_ERROR_FAILED,
Packit Service a1bd4f
                                _("Invalid device Bluetooth address."));
Packit Service a1bd4f
            return FALSE;
Packit Service a1bd4f
        }
Packit Service a1bd4f
        setting_addr = nm_setting_bluetooth_get_bdaddr(s_bt);
Packit Service a1bd4f
        if (setting_addr && !nm_utils_hwaddr_matches(setting_addr, -1, hw_addr, -1)) {
Packit Service a1bd4f
            g_set_error_literal(
Packit Service a1bd4f
                error,
Packit Service a1bd4f
                NM_DEVICE_ERROR,
Packit Service a1bd4f
                NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
Packit Service a1bd4f
                _("The Bluetooth addresses of the device and the connection didn't match."));
Packit Service a1bd4f
            return FALSE;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    dev_caps = nm_device_bt_get_capabilities(NM_DEVICE_BT(device));
Packit Service a1bd4f
    bt_type  = get_connection_bt_type(connection);
Packit Service a1bd4f
    if (!(bt_type & dev_caps)) {
Packit Service a1bd4f
        g_set_error_literal(
Packit Service a1bd4f
            error,
Packit Service a1bd4f
            NM_DEVICE_ERROR,
Packit Service a1bd4f
            NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
Packit Service a1bd4f
            _("The device is lacking Bluetooth capabilities required by the connection."));
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static GType
Packit Service a1bd4f
get_setting_type(NMDevice *device)
Packit 5756e2
{
Packit Service a1bd4f
    return NM_TYPE_SETTING_BLUETOOTH;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_device_bt_init(NMDeviceBt *device)
Packit Service a1bd4f
{}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
finalize(GObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE(object);
Packit 5756e2
Packit Service a1bd4f
    g_free(priv->name);
Packit 5756e2
Packit Service a1bd4f
    G_OBJECT_CLASS(nm_device_bt_parent_class)->finalize(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
Packit 5756e2
{
Packit Service a1bd4f
    NMDeviceBt *device = NM_DEVICE_BT(object);
Packit Service a1bd4f
Packit Service a1bd4f
    switch (prop_id) {
Packit Service a1bd4f
    case PROP_NAME:
Packit Service a1bd4f
        g_value_set_string(value, nm_device_bt_get_name(device));
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_BT_CAPABILITIES:
Packit Service a1bd4f
        g_value_set_flags(value, nm_device_bt_get_capabilities(device));
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    default:
Packit Service a1bd4f
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit Service a1bd4f
const NMLDBusMetaIface _nml_dbus_meta_iface_nm_device_bluetooth = NML_DBUS_META_IFACE_INIT_PROP(
Packit Service a1bd4f
    NM_DBUS_INTERFACE_DEVICE_BLUETOOTH,
Packit Service a1bd4f
    nm_device_bt_get_type,
Packit Service a1bd4f
    NML_DBUS_META_INTERFACE_PRIO_INSTANTIATE_30,
Packit Service a1bd4f
    NML_DBUS_META_IFACE_DBUS_PROPERTIES(
Packit Service a1bd4f
        NML_DBUS_META_PROPERTY_INIT_U("BtCapabilities",
Packit Service a1bd4f
                                      PROP_BT_CAPABILITIES,
Packit Service a1bd4f
                                      NMDeviceBt,
Packit Service a1bd4f
                                      _priv.bt_capabilities),
Packit Service a1bd4f
        NML_DBUS_META_PROPERTY_INIT_FCN("HwAddress",
Packit Service a1bd4f
                                        0,
Packit Service a1bd4f
                                        "s",
Packit Service a1bd4f
                                        _nm_device_notify_update_prop_hw_address),
Packit Service a1bd4f
        NML_DBUS_META_PROPERTY_INIT_S("Name", PROP_NAME, NMDeviceBt, _priv.name), ), );
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_device_bt_class_init(NMDeviceBtClass *bt_class)
Packit 5756e2
{
Packit Service a1bd4f
    GObjectClass * object_class = G_OBJECT_CLASS(bt_class);
Packit Service a1bd4f
    NMDeviceClass *device_class = NM_DEVICE_CLASS(bt_class);
Packit Service a1bd4f
Packit Service a1bd4f
    object_class->get_property = get_property;
Packit Service a1bd4f
    object_class->finalize     = finalize;
Packit Service a1bd4f
Packit Service a1bd4f
    device_class->connection_compatible = connection_compatible;
Packit Service a1bd4f
    device_class->get_setting_type      = get_setting_type;
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NMDeviceBt:name:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The name of the bluetooth device.
Packit Service a1bd4f
     **/
Packit Service a1bd4f
    obj_properties[PROP_NAME] = g_param_spec_string(NM_DEVICE_BT_NAME,
Packit Service a1bd4f
                                                    "",
Packit Service a1bd4f
                                                    "",
Packit Service a1bd4f
                                                    NULL,
Packit Service a1bd4f
                                                    G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NMDeviceBt:bt-capabilities:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The device's bluetooth capabilities, a combination of #NMBluetoothCapabilities.
Packit Service a1bd4f
     **/
Packit Service a1bd4f
    obj_properties[PROP_BT_CAPABILITIES] =
Packit Service a1bd4f
        g_param_spec_flags(NM_DEVICE_BT_CAPABILITIES,
Packit Service a1bd4f
                           "",
Packit Service a1bd4f
                           "",
Packit Service a1bd4f
                           NM_TYPE_BLUETOOTH_CAPABILITIES,
Packit Service a1bd4f
                           NM_BT_CAPABILITY_NONE,
Packit Service a1bd4f
                           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
Packit Service a1bd4f
Packit Service a1bd4f
    _nml_dbus_meta_class_init_with_properties(object_class,
Packit Service a1bd4f
                                              &_nml_dbus_meta_iface_nm_device_bluetooth);
Packit 5756e2
}