nm-device-p2p-wifi.c 11.3 KB
Newer Older
Benjamin Berg's avatar
Benjamin Berg committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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.
 *
17
 * Copyright 2018 - 2019 Red Hat, Inc.
Benjamin Berg's avatar
Benjamin Berg committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 */

#include "nm-default.h"

#include "nm-device-p2p-wifi.h"

#include <string.h>

#include "nm-setting-connection.h"
#include "nm-setting-p2p-wireless.h"
#include "nm-utils.h"

#include "nm-p2p-peer.h"
#include "nm-object-private.h"
#include "nm-core-internal.h"
#include "nm-dbus-helpers.h"

#include "introspection/org.freedesktop.NetworkManager.Device.P2PWireless.h"

37
/*****************************************************************************/
Benjamin Berg's avatar
Benjamin Berg committed
38 39 40 41 42 43

typedef struct {
	NMDeviceP2PWifi *device;
	GSimpleAsyncResult *simple;
} RequestScanInfo;

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
NM_GOBJECT_PROPERTIES_DEFINE_BASE (
	PROP_HW_ADDRESS,
	PROP_GROUP_OWNER,
	PROP_WFDIES,
	PROP_PEERS,
);

enum {
	PEER_ADDED,
	PEER_REMOVED,

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

Benjamin Berg's avatar
Benjamin Berg committed
60 61 62 63 64 65 66
typedef struct {
	NMDBusDeviceP2PWifi *proxy;

	char *hw_address;

	GByteArray  *wfd_ies;
	GPtrArray   *peers;
67 68

	gboolean group_owner;
Benjamin Berg's avatar
Benjamin Berg committed
69 70
} NMDeviceP2PWifiPrivate;

71 72 73 74 75 76 77 78 79
/**
 * NMDeviceP2PWifi:
 *
 * Since: 1.16
 */
struct _NMDeviceP2PWifi {
	NMDevice parent;
	NMDeviceP2PWifiPrivate _priv;
};
Benjamin Berg's avatar
Benjamin Berg committed
80

81 82
struct _NMDeviceP2PWifiClass {
	NMDeviceClass parent;
Benjamin Berg's avatar
Benjamin Berg committed
83 84
};

85
G_DEFINE_TYPE (NMDeviceP2PWifi, nm_device_p2p_wifi, NM_TYPE_DEVICE)
Benjamin Berg's avatar
Benjamin Berg committed
86

87 88 89 90
#define NM_DEVICE_P2P_WIFI_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMDeviceP2PWifi, NM_IS_DEVICE_P2P_WIFI, NMDevice, NMObject)

/*****************************************************************************/

Benjamin Berg's avatar
Benjamin Berg committed
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 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/**
 * nm_device_p2p_wifi_get_hw_address:
 * @device: a #NMDeviceP2PWifi
 *
 * Gets the actual hardware (MAC) address of the #NMDeviceP2PWifi
 *
 * Returns: the actual hardware address. This is the internal string used by the
 * device, and must not be modified.
 *
 * Since: 1.16
 **/
const char *
nm_device_p2p_wifi_get_hw_address (NMDeviceP2PWifi *device)
{
	g_return_val_if_fail (NM_IS_DEVICE_P2P_WIFI (device), NULL);

	return nm_str_not_empty (NM_DEVICE_P2P_WIFI_GET_PRIVATE (device)->hw_address);
}

/**
 * nm_device_p2p_wifi_get_group_owner:
 * @device: a #NMDeviceP2PWifi
 *
 * Gets whether the device is currently the P2P group owner. This is only
 * valid when a connection is established.
 *
 * Returns: Whether the device is the P2P group owner.
 *
 * Since: 1.16
 **/
gboolean
nm_device_p2p_wifi_get_group_owner (NMDeviceP2PWifi *device)
{
	g_return_val_if_fail (NM_IS_DEVICE_P2P_WIFI (device), FALSE);

	return NM_DEVICE_P2P_WIFI_GET_PRIVATE (device)->group_owner;
}

/**
 * nm_device_p2p_wifi_get_peers:
 * @device: a #NMDeviceP2PWifi
 *
 * Gets all the found peers of the #NMDeviceP2PWifi.
 *
 * Returns: (element-type NMP2PPeer): a #GPtrArray containing all the
 *          found #NMP2PPeers.
 * The returned array is owned by the client and should not be modified.
 *
 * Since: 1.16
 **/
const GPtrArray *
nm_device_p2p_wifi_get_peers (NMDeviceP2PWifi *device)
{
	g_return_val_if_fail (NM_IS_DEVICE_P2P_WIFI (device), NULL);

	return NM_DEVICE_P2P_WIFI_GET_PRIVATE (device)->peers;
}

/**
 * nm_device_p2p_wifi_get_peer_by_path:
 * @device: a #NMDeviceP2PWifi
 * @path: the object path of the peer
 *
 * Gets a #NMP2PPeer by path.
 *
 * Returns: (transfer none): the peer or %NULL if none is found.
 *
 * Since: 1.16
 **/
NMP2PPeer *
nm_device_p2p_wifi_get_peer_by_path (NMDeviceP2PWifi *device,
                                     const char *path)
{
	const GPtrArray *peers;
	int i;
	NMP2PPeer *peer = NULL;

	g_return_val_if_fail (NM_IS_DEVICE_P2P_WIFI (device), NULL);
	g_return_val_if_fail (path != NULL, NULL);

	peers = nm_device_p2p_wifi_get_peers (device);
	if (!peers)
		return NULL;

	for (i = 0; i < peers->len; i++) {
		NMP2PPeer *candidate = g_ptr_array_index (peers, i);
		if (!strcmp (nm_object_get_path (NM_OBJECT (candidate)), path)) {
			peer = candidate;
			break;
		}
	}

	return peer;
}

static void
187
clean_up_peers (NMDeviceP2PWifi *self)
Benjamin Berg's avatar
Benjamin Berg committed
188
{
189
	NMDeviceP2PWifiPrivate *priv = NM_DEVICE_P2P_WIFI_GET_PRIVATE (self);
Benjamin Berg's avatar
Benjamin Berg committed
190

191 192
	while (priv->peers->len > 0) {
		NMP2PPeer *peer;
Benjamin Berg's avatar
Benjamin Berg committed
193

194 195
		peer = priv->peers->pdata[priv->peers->len - 1];
		g_ptr_array_remove_index (priv->peers, priv->peers->len - 1);
Benjamin Berg's avatar
Benjamin Berg committed
196

197
		g_signal_emit (self, signals[PEER_REMOVED], 0, peer);
Benjamin Berg's avatar
Benjamin Berg committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	}
}

static gboolean
connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
{
	if (!NM_DEVICE_CLASS (nm_device_p2p_wifi_parent_class)->connection_compatible (device, connection, error))
		return FALSE;

	if (!nm_connection_is_type (connection, NM_SETTING_P2P_WIRELESS_SETTING_NAME)) {
		g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
		                     _("The connection was not a P2P Wi-Fi connection."));
		return FALSE;
	}

	return TRUE;
}

static GType
get_setting_type (NMDevice *device)
{
	return NM_TYPE_SETTING_WIRELESS;
}

static const char *
get_hw_address (NMDevice *device)
{
	return nm_device_p2p_wifi_get_hw_address (NM_DEVICE_P2P_WIFI (device));
}

static GVariant *
nm_device_p2p_wifi_get_wfdies_as_variant (const NMDeviceP2PWifi *self)
{
	const NMDeviceP2PWifiPrivate *priv = NM_DEVICE_P2P_WIFI_GET_PRIVATE (self);

	if (priv->wfd_ies) {
		return g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
		                                  priv->wfd_ies->data, priv->wfd_ies->len, 1);
	} else
		return g_variant_new_array (G_VARIANT_TYPE_BYTE, NULL, 0);
}

/*****************************************************************************/

static void
get_property (GObject *object,
              guint prop_id,
              GValue *value,
              GParamSpec *pspec)
{
	NMDeviceP2PWifi *self = NM_DEVICE_P2P_WIFI (object);

	switch (prop_id) {
	case PROP_HW_ADDRESS:
		g_value_set_string (value, nm_device_p2p_wifi_get_hw_address (self));
		break;
	case PROP_GROUP_OWNER:
		g_value_set_enum (value, nm_device_p2p_wifi_get_group_owner (self));
		break;
	case PROP_WFDIES:
		g_value_take_variant (value, nm_device_p2p_wifi_get_wfdies_as_variant (self));
		break;
	case PROP_PEERS:
		g_value_take_boxed (value, _nm_utils_copy_object_array (nm_device_p2p_wifi_get_peers (self)));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nm_device_p2p_wifi_init (NMDeviceP2PWifi *device)
{
	NMDeviceP2PWifiPrivate *priv = NM_DEVICE_P2P_WIFI_GET_PRIVATE (device);

	priv->peers = g_ptr_array_new ();
}

static void
init_dbus (NMObject *object)
{
	NMDeviceP2PWifiPrivate *priv = NM_DEVICE_P2P_WIFI_GET_PRIVATE (object);
	const NMPropertiesInfo property_info[] = {
		{ NM_DEVICE_P2P_WIFI_HW_ADDRESS,           &priv->hw_address },
		{ NM_DEVICE_P2P_WIFI_GROUP_OWNER,          &priv->group_owner },
		{ NM_DEVICE_P2P_WIFI_WFDIES,               &priv->wfd_ies },
		{ NM_DEVICE_P2P_WIFI_PEERS,                &priv->peers, NULL, NM_TYPE_P2P_PEER, "peer" },
		{ NULL },
	};

	NM_OBJECT_CLASS (nm_device_p2p_wifi_parent_class)->init_dbus (object);

	priv->proxy = NMDBUS_DEVICE_P2P_WIFI (_nm_object_get_proxy (object, NM_DBUS_INTERFACE_DEVICE_P2P_WIRELESS));
	_nm_object_register_properties (object,
	                                NM_DBUS_INTERFACE_DEVICE_P2P_WIRELESS,
	                                property_info);
}

static void
dispose (GObject *object)
{
300
	clean_up_peers (NM_DEVICE_P2P_WIFI (object));
Benjamin Berg's avatar
Benjamin Berg committed
301 302 303 304 305 306 307 308 309

	G_OBJECT_CLASS (nm_device_p2p_wifi_parent_class)->dispose (object);
}

static void
finalize (GObject *object)
{
	NMDeviceP2PWifiPrivate *priv = NM_DEVICE_P2P_WIFI_GET_PRIVATE (object);

310
	g_clear_object (&priv->proxy);
Benjamin Berg's avatar
Benjamin Berg committed
311
	g_free (priv->hw_address);
312 313 314 315
	if (priv->wfd_ies)
		g_byte_array_unref (priv->wfd_ies);
	if (priv->peers)
		g_ptr_array_unref (priv->peers);
Benjamin Berg's avatar
Benjamin Berg committed
316 317 318 319 320 321 322 323 324 325 326 327

	G_OBJECT_CLASS (nm_device_p2p_wifi_parent_class)->finalize (object);
}

static void
nm_device_p2p_wifi_class_init (NMDeviceP2PWifiClass *wifi_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (wifi_class);
	NMObjectClass *nm_object_class = NM_OBJECT_CLASS (wifi_class);
	NMDeviceClass *device_class = NM_DEVICE_CLASS (wifi_class);

	object_class->get_property = get_property;
328 329
	object_class->dispose      = dispose;
	object_class->finalize     = finalize;
Benjamin Berg's avatar
Benjamin Berg committed
330 331

	device_class->connection_compatible = connection_compatible;
332 333
	device_class->get_setting_type      = get_setting_type;
	device_class->get_hw_address        = get_hw_address;
Benjamin Berg's avatar
Benjamin Berg committed
334

335
	nm_object_class->init_dbus = init_dbus;
Benjamin Berg's avatar
Benjamin Berg committed
336 337 338 339 340 341 342 343

	/**
	 * NMDeviceP2PWifi:hw-address:
	 *
	 * The hardware (MAC) address of the device.
	 *
	 * Since: 1.16
	 **/
344 345 346 347 348
	obj_properties[PROP_HW_ADDRESS] =
	    g_param_spec_string (NM_DEVICE_P2P_WIFI_HW_ADDRESS, "", "",
	                         NULL,
	                         G_PARAM_READABLE |
	                         G_PARAM_STATIC_STRINGS);
Benjamin Berg's avatar
Benjamin Berg committed
349 350 351 352 353 354 355 356 357


	/**
	 * NMDeviceP2PWifi:group-owner:
	 *
	 * Whether the device is currently the group owner.
	 *
	 * Since: 1.16
	 **/
358 359 360 361 362
	obj_properties[PROP_GROUP_OWNER] =
	    g_param_spec_boolean (NM_DEVICE_P2P_WIFI_GROUP_OWNER, "", "",
	                          FALSE,
	                          G_PARAM_READABLE |
	                          G_PARAM_STATIC_STRINGS);
Benjamin Berg's avatar
Benjamin Berg committed
363 364 365 366 367 368 369 370

	/**
	 * NMDeviceP2PWifi:wfd-ies:
	 *
	 * Whether the device is currently the group owner.
	 *
	 * Since: 1.16
	 **/
371 372 373 374 375 376
	obj_properties[PROP_WFDIES] =
	    g_param_spec_variant (NM_DEVICE_P2P_WIFI_WFDIES, "", "",
	                          G_VARIANT_TYPE ("ay"),
	                          NULL,
	                          G_PARAM_READABLE |
	                          G_PARAM_STATIC_STRINGS);
Benjamin Berg's avatar
Benjamin Berg committed
377 378 379 380 381 382 383 384

	/**
	 * NMDeviceP2PWifi:peers: (type GPtrArray(NMP2PPeer))
	 *
	 * List of all P2P Wi-Fi peers the device can see.
	 *
	 * Since: 1.16
	 **/
385 386 387 388 389
	obj_properties[PROP_PEERS] =
	    g_param_spec_boxed (NM_DEVICE_P2P_WIFI_PEERS, "", "",
	                        G_TYPE_PTR_ARRAY,
	                        G_PARAM_READABLE |
	                        G_PARAM_STATIC_STRINGS);
Benjamin Berg's avatar
Benjamin Berg committed
390

391
	g_object_class_install_properties (object_class, _PROPERTY_ENUMS_LAST, obj_properties);
Benjamin Berg's avatar
Benjamin Berg committed
392 393 394 395 396 397 398 399 400 401 402

	/**
	 * NMDeviceP2PWifi::peer-added:
	 * @device: the P2P Wi-Fi device that received the signal
	 * @peer: the new access point
	 *
	 * Notifies that a #NMP2PPeer is added to the P2P Wi-Fi device.
	 *
	 * Since: 1.16
	 **/
	signals[PEER_ADDED] =
403 404 405 406 407 408 409
	    g_signal_new ("peer-added",
	                 G_OBJECT_CLASS_TYPE (object_class),
	                 G_SIGNAL_RUN_FIRST,
	                 0, NULL, NULL,
	                 g_cclosure_marshal_VOID__OBJECT,
	                 G_TYPE_NONE, 1,
	                 G_TYPE_OBJECT);
Benjamin Berg's avatar
Benjamin Berg committed
410 411 412 413 414 415 416 417 418 419 420

	/**
	 * NMDeviceP2PWifi::peer-removed:
	 * @device: the P2P Wi-Fi device that received the signal
	 * @peer: the removed access point
	 *
	 * Notifies that a #NMP2PPeer is removed from the P2P Wi-Fi device.
	 *
	 * Since: 1.16
	 **/
	signals[PEER_REMOVED] =
421 422 423 424 425 426 427
	    g_signal_new ("peer-removed",
	                 G_OBJECT_CLASS_TYPE (object_class),
	                 G_SIGNAL_RUN_FIRST,
	                 0, NULL, NULL,
	                 g_cclosure_marshal_VOID__OBJECT,
	                 G_TYPE_NONE, 1,
	                 G_TYPE_OBJECT);
Benjamin Berg's avatar
Benjamin Berg committed
428
}