mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-02-09 05:00:36 +01:00
bluez: store Bluetooth address as an array not a string
It's only ever used as an MAC address array, so we might as well make it one instead of a string. Saves a memory allocation and some cycles converting back and forth. This also fixes a bug, where NMDeviceBt:check_connection_compatible() would not set GError on mismatch of bdaddr. Signed-off-by: Thomas Haller <thaller@redhat.com>
This commit is contained in:
parent
7013014744
commit
4674f10399
1 changed files with 20 additions and 22 deletions
|
|
@ -63,7 +63,7 @@ typedef struct {
|
|||
|
||||
NMBluezDevice *bt_device;
|
||||
|
||||
char *bdaddr;
|
||||
guint8 bdaddr[ETH_ALEN];
|
||||
char *name;
|
||||
guint32 capabilities;
|
||||
|
||||
|
|
@ -170,8 +170,6 @@ check_connection_compatible (NMDevice *device,
|
|||
NMSettingConnection *s_con;
|
||||
NMSettingBluetooth *s_bt;
|
||||
const GByteArray *array;
|
||||
char *str;
|
||||
int addr_match = FALSE;
|
||||
guint32 bt_type;
|
||||
|
||||
if (!NM_DEVICE_CLASS (nm_device_bt_parent_class)->check_connection_compatible (device, connection, error))
|
||||
|
|
@ -195,14 +193,6 @@ check_connection_compatible (NMDevice *device,
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
array = nm_setting_bluetooth_get_bdaddr (s_bt);
|
||||
if (!array || (array->len != ETH_ALEN)) {
|
||||
g_set_error (error,
|
||||
NM_BT_ERROR, NM_BT_ERROR_CONNECTION_INVALID,
|
||||
"The connection did not contain a valid Bluetooth address.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
bt_type = get_connection_bt_type (connection);
|
||||
if (!(bt_type & priv->capabilities)) {
|
||||
g_set_error (error,
|
||||
|
|
@ -211,13 +201,22 @@ check_connection_compatible (NMDevice *device,
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
str = g_strdup_printf ("%02X:%02X:%02X:%02X:%02X:%02X",
|
||||
array->data[0], array->data[1], array->data[2],
|
||||
array->data[3], array->data[4], array->data[5]);
|
||||
addr_match = !strcmp (priv->bdaddr, str);
|
||||
g_free (str);
|
||||
array = nm_setting_bluetooth_get_bdaddr (s_bt);
|
||||
if (!array || (array->len != ETH_ALEN)) {
|
||||
g_set_error (error,
|
||||
NM_BT_ERROR, NM_BT_ERROR_CONNECTION_INVALID,
|
||||
"The connection did not contain a valid Bluetooth address.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return addr_match;
|
||||
if (memcmp (priv->bdaddr, array->data, ETH_ALEN) != 0) {
|
||||
g_set_error (error,
|
||||
NM_BT_ERROR, NM_BT_ERROR_CONNECTION_INVALID,
|
||||
"The connection did not match the device's Bluetooth address.");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
|
|
@ -249,7 +248,6 @@ complete_connection (NMDevice *device,
|
|||
NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (device);
|
||||
NMSettingBluetooth *s_bt;
|
||||
const GByteArray *setting_bdaddr;
|
||||
struct ether_addr *devaddr = ether_aton (priv->bdaddr);
|
||||
const char *ctype;
|
||||
gboolean is_dun = FALSE, is_pan = FALSE;
|
||||
NMSettingGsm *s_gsm;
|
||||
|
|
@ -357,7 +355,7 @@ complete_connection (NMDevice *device,
|
|||
setting_bdaddr = nm_setting_bluetooth_get_bdaddr (s_bt);
|
||||
if (setting_bdaddr) {
|
||||
/* Make sure the setting BT Address (if any) matches the device's */
|
||||
if (memcmp (setting_bdaddr->data, devaddr->ether_addr_octet, ETH_ALEN)) {
|
||||
if (memcmp (setting_bdaddr->data, priv->bdaddr, ETH_ALEN)) {
|
||||
g_set_error_literal (error,
|
||||
NM_SETTING_BLUETOOTH_ERROR,
|
||||
NM_SETTING_BLUETOOTH_ERROR_INVALID_PROPERTY,
|
||||
|
|
@ -369,9 +367,9 @@ complete_connection (NMDevice *device,
|
|||
const guint8 null_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
|
||||
|
||||
/* Lock the connection to this device by default */
|
||||
if (memcmp (devaddr->ether_addr_octet, null_mac, ETH_ALEN)) {
|
||||
if (memcmp (priv->bdaddr, null_mac, ETH_ALEN)) {
|
||||
bdaddr = g_byte_array_sized_new (ETH_ALEN);
|
||||
g_byte_array_append (bdaddr, devaddr->ether_addr_octet, ETH_ALEN);
|
||||
g_byte_array_append (bdaddr, priv->bdaddr, ETH_ALEN);
|
||||
g_object_set (G_OBJECT (s_bt), NM_SETTING_BLUETOOTH_BDADDR, bdaddr, NULL);
|
||||
g_byte_array_free (bdaddr, TRUE);
|
||||
}
|
||||
|
|
@ -1140,7 +1138,7 @@ constructed (GObject *object)
|
|||
my_hwaddr = nm_device_get_hw_address (NM_DEVICE (object), &my_hwaddr_len);
|
||||
g_assert (my_hwaddr);
|
||||
g_assert_cmpint (my_hwaddr_len, ==, ETH_ALEN);
|
||||
priv->bdaddr = nm_utils_hwaddr_ntoa (my_hwaddr, ARPHRD_ETHER);
|
||||
memcpy (priv->bdaddr, my_hwaddr, ETH_ALEN);
|
||||
|
||||
/* Watch for BT device property changes */
|
||||
g_signal_connect (priv->bt_device, "notify::" NM_BLUEZ_DEVICE_CONNECTED,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue