NetworkManager/libnm-core/nm-setting-wpan.c

388 lines
10 KiB
C
Raw Normal View History

// SPDX-License-Identifier: LGPL-2.1+
2018-03-09 10:51:49 +01:00
/*
* Copyright (C) 2018 Lubomir Rintel <lkundrak@v3.sk>
2018-03-09 10:51:49 +01:00
*/
#include "nm-default.h"
#include "nm-setting-wpan.h"
#include "nm-connection-private.h"
#include "nm-setting-connection.h"
#include "nm-setting-private.h"
#include "nm-utils-private.h"
/**
* SECTION:nm-setting-wpan
* @short_description: Describes connection properties for IEEE 802.15.4 (WPAN) MAC
*
* The #NMSettingWpan object is a #NMSetting subclass that describes properties
* necessary for configuring IEEE 802.15.4 (WPAN) MAC layer devices.
**/
/* Ideally we'll be able to get these from a public header. */
2018-03-09 10:51:49 +01:00
#ifndef IEEE802154_ADDR_LEN
#define IEEE802154_ADDR_LEN 8
#endif
#ifndef IEEE802154_MAX_PAGE
#define IEEE802154_MAX_PAGE 31
#endif
#ifndef IEEE802154_MAX_CHANNEL
#define IEEE802154_MAX_CHANNEL 26
#endif
/*****************************************************************************/
NM_GOBJECT_PROPERTIES_DEFINE_BASE (
2018-03-09 10:51:49 +01:00
PROP_MAC_ADDRESS,
PROP_PAN_ID,
PROP_SHORT_ADDRESS,
PROP_PAGE,
PROP_CHANNEL,
);
2018-03-09 10:51:49 +01:00
typedef struct {
char *mac_address;
guint16 pan_id;
guint16 short_address;
gint16 page;
gint16 channel;
2018-03-09 10:51:49 +01:00
} NMSettingWpanPrivate;
/**
* NMSettingWpan:
*
* IEEE 802.15.4 (WPAN) MAC Settings
*/
struct _NMSettingWpan {
NMSetting parent;
};
struct _NMSettingWpanClass {
2018-03-09 10:51:49 +01:00
NMSettingClass parent;
};
2018-03-09 10:51:49 +01:00
libnm: use NMMetaSettingInfo for tracking setting priority Previously, each (non abstract) NMSetting class had to register its name and priority via _nm_register_setting(). Note, that libnm-core.la already links against "nm-meta-setting.c", which also redundantly keeps track of the settings name and gtype as well. Re-use NMMetaSettingInfo also in libnm-core.la, to track this meta data. The goal is to get rid of private data structures that track meta data about NMSetting classes. In this case, "registered_settings" hash. Instead, we should have one place where all this meta data is tracked. This was, it is also accessible as internal API, which can be useful (for keyfile). Note that NMSettingClass has some overlap with NMMetaSettingInfo. One difference is, that NMMetaSettingInfo is const, while NMSettingClass is only initialized during the class_init() method. Appart from that, it's mostly a matter of taste, whether we attach meta data to NMSettingClass, to NMMetaSettingInfo, or to a static-array indexed by NMMetaSettingType. Note, that previously, _nm_register_setting() was private API. That means, no user could subclass a functioning NMSetting instance. The same is still true: NMMetaSettingInfo is internal API and users cannot access it to create their own NMSetting subclasses. But that is almost desired. libnm is not designed, to be extensible via subclassing, nor is it clear why that would be a useful thing to do. One day, we should remove the NMSetting and NMSettingClass definitions from public headers. Their only use is subclassing the types, which however does not work. While libnm-core was linking already against nm-meta-setting.c, nm_meta_setting_infos was unreferenced. So, this change increases the binary size of libnm and NetworkManager (1032 bytes). Note however that roughly the same information was previously allocated at runtime.
2018-07-27 14:08:14 +02:00
G_DEFINE_TYPE (NMSettingWpan, nm_setting_wpan, NM_TYPE_SETTING)
2018-03-09 10:51:49 +01:00
#define NM_SETTING_WPAN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_WPAN, NMSettingWpanPrivate))
/*****************************************************************************/
2018-03-09 10:51:49 +01:00
/**
* nm_setting_wpan_get_mac_address:
* @setting: the #NMSettingWpan
*
* Returns: the #NMSettingWpan:mac-address property of the setting
*
* Since: 1.14
**/
const char *
nm_setting_wpan_get_mac_address (NMSettingWpan *setting)
{
g_return_val_if_fail (NM_IS_SETTING_WPAN (setting), NULL);
return NM_SETTING_WPAN_GET_PRIVATE (setting)->mac_address;
}
/**
* nm_setting_wpan_get_pan_id:
* @setting: the #NMSettingWpan
*
* Returns: the #NMSettingWpan:pan-id property of the setting
*
* Since: 1.14
**/
guint16
nm_setting_wpan_get_pan_id (NMSettingWpan *setting)
{
g_return_val_if_fail (NM_IS_SETTING_WPAN (setting), G_MAXUINT16);
return NM_SETTING_WPAN_GET_PRIVATE (setting)->pan_id;
}
/**
* nm_setting_wpan_get_short_address:
* @setting: the #NMSettingWpan
*
* Returns: the #NMSettingWpan:short-address property of the setting
*
* Since: 1.14
**/
guint16
nm_setting_wpan_get_short_address (NMSettingWpan *setting)
{
g_return_val_if_fail (NM_IS_SETTING_WPAN (setting), G_MAXUINT16);
return NM_SETTING_WPAN_GET_PRIVATE (setting)->short_address;
}
/**
* nm_setting_wpan_get_page:
* @setting: the #NMSettingWpan
*
* Returns: the #NMSettingWpan:page property of the setting
*
* Since: 1.16
**/
gint16
nm_setting_wpan_get_page (NMSettingWpan *setting)
{
g_return_val_if_fail (NM_IS_SETTING_WPAN (setting), NM_SETTING_WPAN_PAGE_DEFAULT);
return NM_SETTING_WPAN_GET_PRIVATE (setting)->page;
}
/**
* nm_setting_wpan_get_channel:
* @setting: the #NMSettingWpan
*
* Returns: the #NMSettingWpan:channel property of the setting
*
* Since: 1.16
**/
gint16
nm_setting_wpan_get_channel (NMSettingWpan *setting)
{
g_return_val_if_fail (NM_IS_SETTING_WPAN (setting), NM_SETTING_WPAN_CHANNEL_DEFAULT);
return NM_SETTING_WPAN_GET_PRIVATE (setting)->channel;
}
2018-03-09 10:51:49 +01:00
static gboolean
verify (NMSetting *setting, NMConnection *connection, GError **error)
{
NMSettingWpanPrivate *priv = NM_SETTING_WPAN_GET_PRIVATE (setting);
if (priv->mac_address && !nm_utils_hwaddr_valid (priv->mac_address, IEEE802154_ADDR_LEN)) {
g_set_error_literal (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("property is invalid"));
g_prefix_error (error, "%s.%s: ", NM_SETTING_WPAN_SETTING_NAME, NM_SETTING_WPAN_MAC_ADDRESS);
return FALSE;
}
if ((priv->page == NM_SETTING_WPAN_PAGE_DEFAULT) != (priv->channel == NM_SETTING_WPAN_CHANNEL_DEFAULT)) {
g_set_error_literal (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("page must be defined along with a channel"));
g_prefix_error (error, "%s.%s: ", NM_SETTING_WPAN_SETTING_NAME, NM_SETTING_WPAN_PAGE);
return FALSE;
}
if (priv->page < NM_SETTING_WPAN_PAGE_DEFAULT || priv->page > IEEE802154_MAX_PAGE) {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("page must be between %d and %d"),
NM_SETTING_WPAN_PAGE_DEFAULT,
IEEE802154_MAX_PAGE);
g_prefix_error (error, "%s.%s: ", NM_SETTING_WPAN_SETTING_NAME, NM_SETTING_WPAN_PAGE);
return FALSE;
}
if (priv->channel < NM_SETTING_WPAN_CHANNEL_DEFAULT || priv->channel > IEEE802154_MAX_CHANNEL) {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("channel must not be between %d and %d"),
NM_SETTING_WPAN_CHANNEL_DEFAULT,
IEEE802154_MAX_CHANNEL);
g_prefix_error (error, "%s.%s: ", NM_SETTING_WPAN_SETTING_NAME, NM_SETTING_WPAN_CHANNEL);
return FALSE;
}
2018-03-09 10:51:49 +01:00
return TRUE;
}
/*****************************************************************************/
2018-03-09 10:51:49 +01:00
static void
get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
NMSettingWpan *setting = NM_SETTING_WPAN (object);
switch (prop_id) {
case PROP_MAC_ADDRESS:
g_value_set_string (value, nm_setting_wpan_get_mac_address (setting));
break;
case PROP_PAN_ID:
g_value_set_uint (value, nm_setting_wpan_get_pan_id (setting));
break;
case PROP_SHORT_ADDRESS:
g_value_set_uint (value, nm_setting_wpan_get_short_address (setting));
break;
case PROP_PAGE:
g_value_set_int (value, nm_setting_wpan_get_page (setting));
break;
case PROP_CHANNEL:
g_value_set_int (value, nm_setting_wpan_get_channel (setting));
break;
2018-03-09 10:51:49 +01:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
NMSettingWpanPrivate *priv = NM_SETTING_WPAN_GET_PRIVATE (object);
switch (prop_id) {
case PROP_MAC_ADDRESS:
g_free (priv->mac_address);
priv->mac_address = _nm_utils_hwaddr_canonical_or_invalid (g_value_get_string (value),
IEEE802154_ADDR_LEN);
break;
case PROP_PAN_ID:
priv->pan_id = g_value_get_uint (value);
break;
case PROP_SHORT_ADDRESS:
priv->short_address = g_value_get_uint (value);
break;
case PROP_PAGE:
priv->page = g_value_get_int (value);
break;
case PROP_CHANNEL:
priv->channel = g_value_get_int (value);
break;
2018-03-09 10:51:49 +01:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/*****************************************************************************/
2018-03-09 10:51:49 +01:00
static void
nm_setting_wpan_init (NMSettingWpan *setting)
{
NMSettingWpanPrivate *priv = NM_SETTING_WPAN_GET_PRIVATE (setting);
priv->pan_id = G_MAXUINT16;
priv->short_address = G_MAXUINT16;
priv->page = NM_SETTING_WPAN_PAGE_DEFAULT;
priv->channel = NM_SETTING_WPAN_CHANNEL_DEFAULT;
2018-03-09 10:51:49 +01:00
}
/**
* nm_setting_wpan_new:
*
* Creates a new #NMSettingWpan object with default values.
*
* Returns: (transfer full): the new empty #NMSettingWpan object
*
* Since: 1.14
**/
NMSetting *
nm_setting_wpan_new (void)
{
return (NMSetting *) g_object_new (NM_TYPE_SETTING_WPAN, NULL);
}
static void
finalize (GObject *object)
{
NMSettingWpanPrivate *priv = NM_SETTING_WPAN_GET_PRIVATE (object);
g_free (priv->mac_address);
G_OBJECT_CLASS (nm_setting_wpan_parent_class)->finalize (object);
}
static void
nm_setting_wpan_class_init (NMSettingWpanClass *klass)
2018-03-09 10:51:49 +01:00
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
NMSettingClass *setting_class = NM_SETTING_CLASS (klass);
2018-03-09 10:51:49 +01:00
g_type_class_add_private (setting_class, sizeof (NMSettingWpanPrivate));
object_class->get_property = get_property;
object_class->set_property = set_property;
object_class->finalize = finalize;
libnm: rework setting metadata for property handling NMSetting internally already tracked a list of all proper GObject properties and D-Bus-only properties. Rework the tracking of the list, so that: - instead of attaching the data to the GType of the setting via g_type_set_qdata(), it is tracked in a static array indexed by NMMetaSettingType. This allows to find the setting-data by simple pointer arithmetic, instead of taking a look and iterating (like g_type_set_qdata() does). Note, that this is still thread safe, because the static table entry is initialized in the class-init function with _nm_setting_class_commit(). And it only accessed by following a NMSettingClass instance, thus the class constructor already ran (maybe not for all setting classes, but for the particular one that we look up). I think this makes initialization of the metadata simpler to understand. Previously, in a first phase each class would attach the metadata to the GType as setting_property_overrides_quark(). Then during nm_setting_class_ensure_properties() it would merge them and set as setting_properties_quark(). Now, during the first phase, we only incrementally build a properties_override GArray, which we finally hand over during nm_setting_class_commit(). - sort the property infos by name and do binary search. Also expose this meta data types as internal API in nm-setting-private.h. While not accessed yet, it can prove beneficial, to have direct (internal) access to these structures. Also, rename NMSettingProperty to NMSettInfoProperty to use a distinct naming scheme. We already have 40+ subclasses of NMSetting that are called NMSetting*. Likewise, NMMetaSetting* is heavily used already. So, choose a new, distinct name.
2018-07-28 15:26:03 +02:00
setting_class->verify = verify;
2018-03-09 10:51:49 +01:00
/**
* NMSettingWpan:mac-address:
*
* If specified, this connection will only apply to the IEEE 802.15.4 (WPAN)
* MAC layer device whose permanent MAC address matches.
**/
/* ---keyfile---
* property: mac-address
* format: usual hex-digits-and-colons notation
* description: MAC address in hex-digits-and-colons notation
* (e.g. 76:d8:9b:87:66:60:84:ee).
* ---end---
*/
obj_properties[PROP_MAC_ADDRESS] =
g_param_spec_string (NM_SETTING_WPAN_MAC_ADDRESS, "", "",
NULL,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
2018-03-09 10:51:49 +01:00
/**
* NMSettingWpan:pan-id:
*
* IEEE 802.15.4 Personal Area Network (PAN) identifier.
**/
obj_properties[PROP_PAN_ID] =
g_param_spec_uint (NM_SETTING_WPAN_PAN_ID, "", "",
0, G_MAXUINT16, G_MAXUINT16,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
2018-03-09 10:51:49 +01:00
/**
* NMSettingWpan:short-address:
*
* Short IEEE 802.15.4 address to be used within a restricted environment.
**/
obj_properties[PROP_SHORT_ADDRESS] =
g_param_spec_uint (NM_SETTING_WPAN_SHORT_ADDRESS, "", "",
0, G_MAXUINT16, G_MAXUINT16,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
libnm: rework setting metadata for property handling NMSetting internally already tracked a list of all proper GObject properties and D-Bus-only properties. Rework the tracking of the list, so that: - instead of attaching the data to the GType of the setting via g_type_set_qdata(), it is tracked in a static array indexed by NMMetaSettingType. This allows to find the setting-data by simple pointer arithmetic, instead of taking a look and iterating (like g_type_set_qdata() does). Note, that this is still thread safe, because the static table entry is initialized in the class-init function with _nm_setting_class_commit(). And it only accessed by following a NMSettingClass instance, thus the class constructor already ran (maybe not for all setting classes, but for the particular one that we look up). I think this makes initialization of the metadata simpler to understand. Previously, in a first phase each class would attach the metadata to the GType as setting_property_overrides_quark(). Then during nm_setting_class_ensure_properties() it would merge them and set as setting_properties_quark(). Now, during the first phase, we only incrementally build a properties_override GArray, which we finally hand over during nm_setting_class_commit(). - sort the property infos by name and do binary search. Also expose this meta data types as internal API in nm-setting-private.h. While not accessed yet, it can prove beneficial, to have direct (internal) access to these structures. Also, rename NMSettingProperty to NMSettInfoProperty to use a distinct naming scheme. We already have 40+ subclasses of NMSetting that are called NMSetting*. Likewise, NMMetaSetting* is heavily used already. So, choose a new, distinct name.
2018-07-28 15:26:03 +02:00
/**
* NMSettingWpan:page:
*
* IEEE 802.15.4 channel page. A positive integer or -1, meaning "do not
* set, use whatever the device is already set to".
*
* Since: 1.16
**/
obj_properties[PROP_PAGE] =
g_param_spec_int (NM_SETTING_WPAN_PAGE, "", "",
G_MININT16,
G_MAXINT16,
NM_SETTING_WPAN_PAGE_DEFAULT,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
/**
* NMSettingWpan:channel:
*
* IEEE 802.15.4 channel. A positive integer or -1, meaning "do not
* set, use whatever the device is already set to".
*
* Since: 1.16
**/
obj_properties[PROP_CHANNEL] =
g_param_spec_int (NM_SETTING_WPAN_CHANNEL, "", "",
G_MININT16,
G_MAXINT16,
NM_SETTING_WPAN_CHANNEL_DEFAULT,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, _PROPERTY_ENUMS_LAST, obj_properties);
libnm: rework setting metadata for property handling NMSetting internally already tracked a list of all proper GObject properties and D-Bus-only properties. Rework the tracking of the list, so that: - instead of attaching the data to the GType of the setting via g_type_set_qdata(), it is tracked in a static array indexed by NMMetaSettingType. This allows to find the setting-data by simple pointer arithmetic, instead of taking a look and iterating (like g_type_set_qdata() does). Note, that this is still thread safe, because the static table entry is initialized in the class-init function with _nm_setting_class_commit(). And it only accessed by following a NMSettingClass instance, thus the class constructor already ran (maybe not for all setting classes, but for the particular one that we look up). I think this makes initialization of the metadata simpler to understand. Previously, in a first phase each class would attach the metadata to the GType as setting_property_overrides_quark(). Then during nm_setting_class_ensure_properties() it would merge them and set as setting_properties_quark(). Now, during the first phase, we only incrementally build a properties_override GArray, which we finally hand over during nm_setting_class_commit(). - sort the property infos by name and do binary search. Also expose this meta data types as internal API in nm-setting-private.h. While not accessed yet, it can prove beneficial, to have direct (internal) access to these structures. Also, rename NMSettingProperty to NMSettInfoProperty to use a distinct naming scheme. We already have 40+ subclasses of NMSetting that are called NMSetting*. Likewise, NMMetaSetting* is heavily used already. So, choose a new, distinct name.
2018-07-28 15:26:03 +02:00
_nm_setting_class_commit (setting_class, NM_META_SETTING_TYPE_WPAN);
2018-03-09 10:51:49 +01:00
}