nm-setting-wpan.c 11.1 KB
Newer Older
Lubomir Rintel's avatar
Lubomir Rintel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright 2018 Lubomir Rintel <lkundrak@v3.sk>
 */

#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"

29 30 31 32 33 34 35 36
/**
 * 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.
 **/

37
/* Ideally we'll be able to get these from a public header. */
Lubomir Rintel's avatar
Lubomir Rintel committed
38 39 40 41
#ifndef IEEE802154_ADDR_LEN
#define IEEE802154_ADDR_LEN 8
#endif

42 43 44 45 46 47 48 49
#ifndef IEEE802154_MAX_PAGE
#define IEEE802154_MAX_PAGE 31
#endif

#ifndef IEEE802154_MAX_CHANNEL
#define IEEE802154_MAX_CHANNEL 26
#endif

50 51
/*****************************************************************************/

52
NM_GOBJECT_PROPERTIES_DEFINE_BASE (
Lubomir Rintel's avatar
Lubomir Rintel committed
53 54 55
	PROP_MAC_ADDRESS,
	PROP_PAN_ID,
	PROP_SHORT_ADDRESS,
56 57
	PROP_PAGE,
	PROP_CHANNEL,
58
);
Lubomir Rintel's avatar
Lubomir Rintel committed
59 60 61 62 63

typedef struct {
	char *mac_address;
	guint16 pan_id;
	guint16 short_address;
64 65
	gint16 page;
	gint16 channel;
Lubomir Rintel's avatar
Lubomir Rintel committed
66 67 68 69 70 71 72 73 74 75 76
} NMSettingWpanPrivate;

/**
 * NMSettingWpan:
 *
 * IEEE 802.15.4 (WPAN) MAC Settings
 */
struct _NMSettingWpan {
        NMSetting parent;
};

77
struct _NMSettingWpanClass {
Lubomir Rintel's avatar
Lubomir Rintel committed
78
        NMSettingClass parent;
79
};
Lubomir Rintel's avatar
Lubomir Rintel committed
80

81
G_DEFINE_TYPE (NMSettingWpan, nm_setting_wpan, NM_TYPE_SETTING)
Lubomir Rintel's avatar
Lubomir Rintel committed
82 83 84

#define NM_SETTING_WPAN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_WPAN, NMSettingWpanPrivate))

85 86
/*****************************************************************************/

Lubomir Rintel's avatar
Lubomir Rintel committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/**
 * 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;
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/**
 * 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;
}

Lubomir Rintel's avatar
Lubomir Rintel committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180
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;
	}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	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;
	}

Lubomir Rintel's avatar
Lubomir Rintel committed
212 213 214
	return TRUE;
}

215 216
/*****************************************************************************/

Lubomir Rintel's avatar
Lubomir Rintel committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
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;
232 233 234 235 236 237
	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;
Lubomir Rintel's avatar
Lubomir Rintel committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	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;
261 262 263 264 265 266
	case PROP_PAGE:
		priv->page = g_value_get_int (value);
		break;
	case PROP_CHANNEL:
		priv->channel = g_value_get_int (value);
		break;
Lubomir Rintel's avatar
Lubomir Rintel committed
267 268 269 270 271 272
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

273 274
/*****************************************************************************/

Lubomir Rintel's avatar
Lubomir Rintel committed
275 276 277 278 279 280
static void
nm_setting_wpan_init (NMSettingWpan *setting)
{
	NMSettingWpanPrivate *priv = NM_SETTING_WPAN_GET_PRIVATE (setting);

	priv->pan_id = G_MAXUINT16;
281 282 283
	priv->short_address = G_MAXUINT16;
	priv->page = NM_SETTING_WPAN_PAGE_DEFAULT;
	priv->channel = NM_SETTING_WPAN_CHANNEL_DEFAULT;
Lubomir Rintel's avatar
Lubomir Rintel committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
}

/**
 * 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
312
nm_setting_wpan_class_init (NMSettingWpanClass *klass)
Lubomir Rintel's avatar
Lubomir Rintel committed
313
{
314 315
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	NMSettingClass *setting_class = NM_SETTING_CLASS (klass);
Lubomir Rintel's avatar
Lubomir Rintel committed
316 317 318 319

	g_type_class_add_private (setting_class, sizeof (NMSettingWpanPrivate));

	object_class->get_property = get_property;
320
	object_class->set_property = set_property;
321 322
	object_class->finalize     = finalize;

323
	setting_class->verify = verify;
Lubomir Rintel's avatar
Lubomir Rintel committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337

	/**
	 * 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---
	 */
338 339 340 341 342
	obj_properties[PROP_MAC_ADDRESS] =
	    g_param_spec_string (NM_SETTING_WPAN_MAC_ADDRESS, "", "",
	                         NULL,
	                         G_PARAM_READWRITE |
	                         G_PARAM_STATIC_STRINGS);
Lubomir Rintel's avatar
Lubomir Rintel committed
343 344 345 346 347 348

	/**
	 * NMSettingWpan:pan-id:
	 *
	 * IEEE 802.15.4 Personal Area Network (PAN) identifier.
	 **/
349 350 351 352 353
	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);
Lubomir Rintel's avatar
Lubomir Rintel committed
354 355 356 357 358 359

	/**
	 * NMSettingWpan:short-address:
	 *
	 * Short IEEE 802.15.4 address to be used within a restricted environment.
	 **/
360 361 362 363 364
	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);
365

366 367 368 369 370 371 372 373
	/**
	 * 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
	 **/
374 375 376 377 378 379 380
	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);
381 382 383 384 385 386 387 388 389

	/**
	 * 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
	 **/
390 391 392 393 394 395 396 397 398
	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);
399

400
	_nm_setting_class_commit (setting_class, NM_META_SETTING_TYPE_WPAN);
Lubomir Rintel's avatar
Lubomir Rintel committed
401
}