summaryrefslogtreecommitdiff
path: root/libnm/nm-device-wifi-p2p.c
blob: 191d30eb37007eae5f9329bdf0c30d0ba616e1a0 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
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
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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// SPDX-License-Identifier: LGPL-2.1+
/*
 * Copyright (C) 2018 - 2019 Red Hat, Inc.
 */

#include "nm-default.h"

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

#include "nm-glib-aux/nm-dbus-aux.h"
#include "nm-setting-connection.h"
#include "nm-setting-wifi-p2p.h"
#include "nm-utils.h"
#include "nm-wifi-p2p-peer.h"
#include "nm-object-private.h"
#include "nm-core-internal.h"
#include "nm-dbus-helpers.h"

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

NM_GOBJECT_PROPERTIES_DEFINE_BASE (
	PROP_HW_ADDRESS,
	PROP_PEERS,
);

enum {
	PEER_ADDED,
	PEER_REMOVED,

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

typedef struct {
	NMLDBusPropertyAO peers;
	char *hw_address;
} NMDeviceWifiP2PPrivate;

struct _NMDeviceWifiP2P {
	NMDevice parent;
	NMDeviceWifiP2PPrivate _priv;
};

struct _NMDeviceWifiP2PClass {
	NMDeviceClass parent;
};

G_DEFINE_TYPE (NMDeviceWifiP2P, nm_device_wifi_p2p, NM_TYPE_DEVICE)

#define NM_DEVICE_WIFI_P2P_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMDeviceWifiP2P, NM_IS_DEVICE_WIFI_P2P, NMDevice, NMObject)

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

/**
 * nm_device_wifi_p2p_get_hw_address:
 * @device: a #NMDeviceWifiP2P
 *
 * Gets the actual hardware (MAC) address of the #NMDeviceWifiP2P
 *
 * 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_wifi_p2p_get_hw_address (NMDeviceWifiP2P *device)
{
	g_return_val_if_fail (NM_IS_DEVICE_WIFI_P2P (device), NULL);

	return _nml_coerce_property_str_not_empty (NM_DEVICE_WIFI_P2P_GET_PRIVATE (device)->hw_address);
}

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

	return nml_dbus_property_ao_get_objs_as_ptrarray (&NM_DEVICE_WIFI_P2P_GET_PRIVATE (device)->peers);
}

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

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

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

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

	return peer;
}

/**
 * nm_device_wifi_p2p_start_find:
 * @device: a #NMDeviceWifiP2P
 * @options: (allow-none): optional options passed to StartFind.
 * @cancellable: a #GCancellable, or %NULL
 * @callback: a #GAsyncReadyCallback, or %NULL
 * @user_data: user_data for @callback
 *
 * Request NM to search for Wi-Fi P2P peers on @device. Note that the call
 * returns immediately after requesting the find, and it may take some time
 * after that for peers to be found.
 *
 * The find operation will run for 30s by default. You can stop it earlier
 * using nm_device_p2p_wifi_stop_find().
 *
 * Since: 1.16
 **/
void
nm_device_wifi_p2p_start_find (NMDeviceWifiP2P *device,
                               GVariant *options,
                               GCancellable *cancellable,
                               GAsyncReadyCallback callback,
                               gpointer user_data)
{
	g_return_if_fail (NM_IS_DEVICE_WIFI_P2P (device));
	g_return_if_fail (!options || g_variant_is_of_type (options, G_VARIANT_TYPE_VARDICT));
	g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));

	if (!options)
		options = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);

	_nm_client_dbus_call (_nm_object_get_client (device),
	                      device,
	                      nm_device_wifi_p2p_start_find,
	                      cancellable,
	                      callback,
	                      user_data,
	                      NM_DBUS_PATH,
	                      NM_DBUS_INTERFACE,
	                      "StartFind",
	                      g_variant_new ("(@a{sv})", options),
	                      G_VARIANT_TYPE ("()"),
	                      G_DBUS_CALL_FLAGS_NONE,
	                      NM_DBUS_DEFAULT_TIMEOUT_MSEC,
	                      nm_dbus_connection_call_finish_void_cb);
}

/**
 * nm_device_wifi_p2p_start_find_finish:
 * @device: a #NMDeviceWifiP2P
 * @result: the #GAsyncResult
 * @error: #GError return address
 *
 * Finish an operation started by nm_device_wifi_p2p_start_find().
 *
 * Returns: %TRUE if the call was successful
 *
 * Since: 1.16
 **/
gboolean
nm_device_wifi_p2p_start_find_finish (NMDeviceWifiP2P  *device,
                                      GAsyncResult     *result,
                                      GError          **error)
{
	g_return_val_if_fail (NM_IS_DEVICE_WIFI_P2P (device), FALSE);
	g_return_val_if_fail (nm_g_task_is_valid (result, device, nm_device_wifi_p2p_start_find), FALSE);

	return g_task_propagate_boolean (G_TASK (result), error);
}

/**
 * nm_device_wifi_p2p_stop_find:
 * @device: a #NMDeviceWifiP2P
 * @cancellable: a #GCancellable, or %NULL
 * @callback: a #GAsyncReadyCallback, or %NULL
 * @user_data: user_data for @callback
 *
 * Request NM to stop any ongoing find operation for Wi-Fi P2P peers on @device.
 *
 * Since: 1.16
 **/
void
nm_device_wifi_p2p_stop_find (NMDeviceWifiP2P     *device,
                              GCancellable        *cancellable,
                              GAsyncReadyCallback  callback,
                              gpointer             user_data)
{
	g_return_if_fail (NM_IS_DEVICE_WIFI_P2P (device));
	g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));

	_nm_client_dbus_call (_nm_object_get_client (device),
	                      device,
	                      nm_device_wifi_p2p_stop_find,
	                      cancellable,
	                      callback,
	                      user_data,
	                      NM_DBUS_PATH,
	                      NM_DBUS_INTERFACE,
	                      "StopFind",
	                      g_variant_new ("()"),
	                      G_VARIANT_TYPE ("()"),
	                      G_DBUS_CALL_FLAGS_NONE,
	                      NM_DBUS_DEFAULT_TIMEOUT_MSEC,
	                      nm_dbus_connection_call_finish_void_cb);
}

/**
 * nm_device_wifi_p2p_stop_find_finish:
 * @device: a #NMDeviceWifiP2P
 * @result: the #GAsyncResult
 * @error: #GError return address
 *
 * Finish an operation started by nm_device_wifi_p2p_stop_find().
 *
 * Returns: %TRUE if the call was successful
 *
 * Since: 1.16
 **/
gboolean
nm_device_wifi_p2p_stop_find_finish (NMDeviceWifiP2P  *device,
                                      GAsyncResult     *result,
                                      GError          **error)
{
	g_return_val_if_fail (NM_IS_DEVICE_WIFI_P2P (device), FALSE);
	g_return_val_if_fail (nm_g_task_is_valid (result, device, nm_device_wifi_p2p_stop_find), FALSE);

	return g_task_propagate_boolean (G_TASK (result), error);
}

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

	if (!nm_connection_is_type (connection, NM_SETTING_WIFI_P2P_SETTING_NAME)) {
		g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
		                     _("The connection was not a Wi-Fi P2P 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_wifi_p2p_get_hw_address (NM_DEVICE_WIFI_P2P (device));
}

static const char *
get_type_description (NMDevice *device)
{
	return "wifi-p2p";
}

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

static void
_property_ao_notify_changed_peers_cb (NMLDBusPropertyAO *pr_ao,
                                      NMClient *client,
                                      NMObject *nmobj,
                                      gboolean is_added /* or else removed */)
{
	_nm_client_notify_event_queue_emit_obj_signal (client,
	                                               G_OBJECT (pr_ao->owner_dbobj->nmobj),
	                                               nmobj,
	                                               is_added,
	                                               10,
	                                                 is_added
	                                               ? signals[PEER_ADDED]
	                                               : signals[PEER_REMOVED]);
}

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

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

	switch (prop_id) {
	case PROP_HW_ADDRESS:
		g_value_set_string (value, nm_device_wifi_p2p_get_hw_address (self));
		break;
	case PROP_PEERS:
		g_value_take_boxed (value, _nm_utils_copy_object_array (nm_device_wifi_p2p_get_peers (self)));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

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

static void
nm_device_wifi_p2p_init (NMDeviceWifiP2P *device)
{
}

static void
finalize (GObject *object)
{
	NMDeviceWifiP2PPrivate *priv = NM_DEVICE_WIFI_P2P_GET_PRIVATE (object);

	g_free (priv->hw_address);

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

const NMLDBusMetaIface _nml_dbus_meta_iface_nm_device_wifip2p = NML_DBUS_META_IFACE_INIT_PROP (
	NM_DBUS_INTERFACE_DEVICE_WIFI_P2P,
	nm_device_wifi_p2p_get_type,
	NML_DBUS_META_INTERFACE_PRIO_INSTANTIATE_HIGH,
	NML_DBUS_META_IFACE_DBUS_PROPERTIES (
		NML_DBUS_META_PROPERTY_INIT_S       ("HwAddress", PROP_HW_ADDRESS, NMDeviceWifiP2P, _priv.hw_address                                                                                      ),
		NML_DBUS_META_PROPERTY_INIT_AO_PROP ("Peers",     PROP_PEERS,      NMDeviceWifiP2P, _priv.peers,     nm_wifi_p2p_peer_get_type, .notify_changed_ao = _property_ao_notify_changed_peers_cb ),
	),
);

static void
nm_device_wifi_p2p_class_init (NMDeviceWifiP2PClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	NMObjectClass *nm_object_class = NM_OBJECT_CLASS (klass);
	NMDeviceClass *device_class = NM_DEVICE_CLASS (klass);

	object_class->get_property = get_property;
	object_class->finalize     = finalize;

	_NM_OBJECT_CLASS_INIT_PRIV_PTR_DIRECT (nm_object_class, NMDeviceWifiP2P);

	_NM_OBJECT_CLASS_INIT_PROPERTY_AO_FIELDS_1 (nm_object_class, NMDeviceWifiP2PPrivate, peers);

	device_class->connection_compatible = connection_compatible;
	device_class->get_setting_type      = get_setting_type;
	device_class->get_hw_address        = get_hw_address;
	device_class->get_type_description  = get_type_description;

	/**
	 * NMDeviceWifiP2P:hw-address:
	 *
	 * The hardware (MAC) address of the device.
	 *
	 * Since: 1.16
	 **/
	obj_properties[PROP_HW_ADDRESS] =
	    g_param_spec_string (NM_DEVICE_WIFI_P2P_HW_ADDRESS, "", "",
	                         NULL,
	                         G_PARAM_READABLE |
	                         G_PARAM_STATIC_STRINGS);

	/**
	 * NMDeviceWifiP2P:peers: (type GPtrArray(NMWifiP2PPeer))
	 *
	 * List of all Wi-Fi P2P peers the device can see.
	 *
	 * Since: 1.16
	 **/
	obj_properties[PROP_PEERS] =
	    g_param_spec_boxed (NM_DEVICE_WIFI_P2P_PEERS, "", "",
	                        G_TYPE_PTR_ARRAY,
	                        G_PARAM_READABLE |
	                        G_PARAM_STATIC_STRINGS);

	_nml_dbus_meta_class_init_with_properties (object_class, &_nml_dbus_meta_iface_nm_device_wifip2p);

	/**
	 * NMDeviceWifiP2P::peer-added:
	 * @device: the Wi-Fi P2P device that received the signal
	 * @peer: the new access point
	 *
	 * Notifies that a #NMWifiP2PPeer is added to the Wi-Fi P2P device.
	 *
	 * Since: 1.16
	 **/
	signals[PEER_ADDED] =
	    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);

	/**
	 * NMDeviceWifiP2P::peer-removed:
	 * @device: the Wi-Fi P2P device that received the signal
	 * @peer: the removed access point
	 *
	 * Notifies that a #NMWifiP2PPeer is removed from the Wi-Fi P2P device.
	 *
	 * Since: 1.16
	 **/
	signals[PEER_REMOVED] =
	    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);
}