Commit c6c41eb1 authored by Thomas Haller's avatar Thomas Haller
Browse files

libnm: various cleanup of NMP2PPeer and NMDeviceP2PWifi

- fix leaking hw_address in finalize().

- reorder code.

- avoid double tabs in GObject property definitions.

- hide struct definitions from header.

- don't use signal slots in class structure.

- use NM_GOBJECT_PROPERTIES_DEFINE_BASE().

- add missing NM_AVAILABLE_IN_1_16 annotations.
parent 41b2d8c6
......@@ -14,8 +14,7 @@
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Copyright 2007 - 2008 Novell, Inc.
* Copyright 2007 - 2018 Red Hat, Inc.
* Copyright 2018 - 2019 Red Hat, Inc.
*/
#include "nm-default.h"
......@@ -35,45 +34,63 @@
#include "introspection/org.freedesktop.NetworkManager.Device.P2PWireless.h"
G_DEFINE_TYPE (NMDeviceP2PWifi, nm_device_p2p_wifi, NM_TYPE_DEVICE)
#define NM_DEVICE_P2P_WIFI_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_P2P_WIFI, NMDeviceP2PWifiPrivate))
void _nm_device_p2p_wifi_set_p2p_wireless_enabled (NMDeviceP2PWifi *device, gboolean enabled);
static void state_changed_cb (NMDevice *device, GParamSpec *pspec, gpointer user_data);
/*****************************************************************************/
typedef struct {
NMDeviceP2PWifi *device;
GSimpleAsyncResult *simple;
} RequestScanInfo;
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 };
typedef struct {
NMDBusDeviceP2PWifi *proxy;
char *hw_address;
gboolean group_owner;
GByteArray *wfd_ies;
GPtrArray *peers;
gboolean group_owner;
} NMDeviceP2PWifiPrivate;
enum {
PROP_0,
PROP_HW_ADDRESS,
PROP_GROUP_OWNER,
PROP_WFDIES,
PROP_PEERS,
/**
* NMDeviceP2PWifi:
*
* Since: 1.16
*/
struct _NMDeviceP2PWifi {
NMDevice parent;
NMDeviceP2PWifiPrivate _priv;
};
LAST_PROP
struct _NMDeviceP2PWifiClass {
NMDeviceClass parent;
};
enum {
PEER_ADDED,
PEER_REMOVED,
G_DEFINE_TYPE (NMDeviceP2PWifi, nm_device_p2p_wifi, NM_TYPE_DEVICE)
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
#define NM_DEVICE_P2P_WIFI_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMDeviceP2PWifi, NM_IS_DEVICE_P2P_WIFI, NMDevice, NMObject)
/*****************************************************************************/
static void state_changed_cb (NMDevice *device, GParamSpec *pspec, gpointer user_data);
/*****************************************************************************/
/**
* nm_device_p2p_wifi_get_hw_address:
......@@ -406,20 +423,15 @@ nm_device_p2p_wifi_class_init (NMDeviceP2PWifiClass *wifi_class)
NMObjectClass *nm_object_class = NM_OBJECT_CLASS (wifi_class);
NMDeviceClass *device_class = NM_DEVICE_CLASS (wifi_class);
g_type_class_add_private (wifi_class, sizeof (NMDeviceP2PWifiPrivate));
/* virtual methods */
object_class->get_property = get_property;
object_class->dispose = dispose;
object_class->finalize = finalize;
nm_object_class->init_dbus = init_dbus;
object_class->dispose = dispose;
object_class->finalize = finalize;
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_setting_type = get_setting_type;
device_class->get_hw_address = get_hw_address;
/* properties */
nm_object_class->init_dbus = init_dbus;
/**
* NMDeviceP2PWifi:hw-address:
......@@ -428,12 +440,11 @@ nm_device_p2p_wifi_class_init (NMDeviceP2PWifiClass *wifi_class)
*
* Since: 1.16
**/
g_object_class_install_property
(object_class, PROP_HW_ADDRESS,
g_param_spec_string (NM_DEVICE_P2P_WIFI_HW_ADDRESS, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
obj_properties[PROP_HW_ADDRESS] =
g_param_spec_string (NM_DEVICE_P2P_WIFI_HW_ADDRESS, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
/**
......@@ -443,12 +454,11 @@ nm_device_p2p_wifi_class_init (NMDeviceP2PWifiClass *wifi_class)
*
* Since: 1.16
**/
g_object_class_install_property
(object_class, PROP_GROUP_OWNER,
g_param_spec_boolean (NM_DEVICE_P2P_WIFI_GROUP_OWNER, "", "",
FALSE,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
obj_properties[PROP_GROUP_OWNER] =
g_param_spec_boolean (NM_DEVICE_P2P_WIFI_GROUP_OWNER, "", "",
FALSE,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
/**
* NMDeviceP2PWifi:wfd-ies:
......@@ -457,13 +467,12 @@ nm_device_p2p_wifi_class_init (NMDeviceP2PWifiClass *wifi_class)
*
* Since: 1.16
**/
g_object_class_install_property
(object_class, PROP_WFDIES,
g_param_spec_variant (NM_DEVICE_P2P_WIFI_WFDIES, "", "",
G_VARIANT_TYPE ("ay"),
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
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);
/**
* NMDeviceP2PWifi:peers: (type GPtrArray(NMP2PPeer))
......@@ -472,14 +481,13 @@ nm_device_p2p_wifi_class_init (NMDeviceP2PWifiClass *wifi_class)
*
* Since: 1.16
**/
g_object_class_install_property
(object_class, PROP_PEERS,
g_param_spec_boxed (NM_DEVICE_P2P_WIFI_PEERS, "", "",
G_TYPE_PTR_ARRAY,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
obj_properties[PROP_PEERS] =
g_param_spec_boxed (NM_DEVICE_P2P_WIFI_PEERS, "", "",
G_TYPE_PTR_ARRAY,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
/* signals */
g_object_class_install_properties (object_class, _PROPERTY_ENUMS_LAST, obj_properties);
/**
* NMDeviceP2PWifi::peer-added:
......@@ -491,14 +499,13 @@ nm_device_p2p_wifi_class_init (NMDeviceP2PWifiClass *wifi_class)
* Since: 1.16
**/
signals[PEER_ADDED] =
g_signal_new ("peer-added",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (NMDeviceP2PWifiClass, peer_added),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
G_TYPE_OBJECT);
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);
/**
* NMDeviceP2PWifi::peer-removed:
......@@ -510,12 +517,11 @@ nm_device_p2p_wifi_class_init (NMDeviceP2PWifiClass *wifi_class)
* Since: 1.16
**/
signals[PEER_REMOVED] =
g_signal_new ("peer-removed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (NMDeviceP2PWifiClass, peer_removed),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
G_TYPE_OBJECT);
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);
}
......@@ -14,8 +14,7 @@
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Copyright 2007 - 2008 Novell, Inc.
* Copyright 2007 - 2018 Red Hat, Inc.
* Copyright 2018 - 2019 Red Hat, Inc.
*/
#ifndef __NM_DEVICE_P2P_WIFI_H__
......@@ -41,41 +40,34 @@ G_BEGIN_DECLS
#define NM_DEVICE_P2P_WIFI_PEERS "peers"
#define NM_DEVICE_P2P_WIFI_WFDIES "wfdies"
/**
* NMDeviceP2PWifi:
*/
struct _NMDeviceP2PWifi {
NMDevice parent;
};
typedef struct {
NMDeviceClass parent;
/* Signals */
void (*peer_added) (NMDeviceP2PWifi *device, NMP2PPeer *peer);
void (*peer_removed) (NMDeviceP2PWifi *device, NMP2PPeer *peer);
/*< private >*/
gpointer padding[4];
} NMDeviceP2PWifiClass;
typedef struct _NMDeviceP2PWifiClass NMDeviceP2PWifiClass;
NM_AVAILABLE_IN_1_16
GType nm_device_p2p_wifi_get_type (void);
NM_AVAILABLE_IN_1_16
const char * nm_device_p2p_wifi_get_hw_address (NMDeviceP2PWifi *device);
NM_AVAILABLE_IN_1_16
gboolean nm_device_p2p_wifi_get_group_owner (NMDeviceP2PWifi *device);
#if 0
gboolean nm_device_p2p_wifi_get_wfdies (NMDeviceP2PWifi *device);
#endif
NM_AVAILABLE_IN_1_16
NMP2PPeer * nm_device_p2p_wifi_get_peer_by_path (NMDeviceP2PWifi *device,
const char *path);
NM_AVAILABLE_IN_1_16
const GPtrArray * nm_device_p2p_wifi_get_peers (NMDeviceP2PWifi *device);
NM_AVAILABLE_IN_1_16
gboolean nm_device_p2p_wifi_start_find (NMDeviceP2PWifi *device,
GCancellable *cancellable,
GError **error);
NM_AVAILABLE_IN_1_16
gboolean nm_device_p2p_wifi_stop_find (NMDeviceP2PWifi *device,
GCancellable *cancellable,
GError **error);
......
......@@ -14,8 +14,7 @@
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Copyright 2007 - 2008 Novell, Inc.
* Copyright 2007 - 2018 Red Hat, Inc.
* Copyright 2018 - 2019 Red Hat, Inc.
*/
#include "nm-default.h"
......@@ -32,13 +31,22 @@
#include "nm-dbus-interface.h"
#include "nm-object-private.h"
G_DEFINE_TYPE (NMP2PPeer, nm_p2p_peer, NM_TYPE_OBJECT)
/*****************************************************************************/
#define NM_P2P_PEER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_P2P_PEER, NMP2PPeerPrivate))
NM_GOBJECT_PROPERTIES_DEFINE_BASE (
PROP_FLAGS,
PROP_NAME,
PROP_MANUFACTURER,
PROP_MODEL,
PROP_MODEL_NUMBER,
PROP_SERIAL,
PROP_WFD_IES,
PROP_HW_ADDRESS,
PROP_STRENGTH,
PROP_LAST_SEEN,
);
typedef struct {
NM80211ApFlags flags;
char *name;
char *manufacturer;
char *model;
......@@ -49,26 +57,31 @@ typedef struct {
char *hw_address;
guint8 strength;
int last_seen;
NM80211ApFlags flags;
guint8 strength;
} NMP2PPeerPrivate;
enum {
PROP_0,
PROP_FLAGS,
PROP_NAME,
PROP_MANUFACTURER,
PROP_MODEL,
PROP_MODEL_NUMBER,
PROP_SERIAL,
PROP_WFD_IES,
PROP_HW_ADDRESS,
PROP_STRENGTH,
PROP_LAST_SEEN,
/**
* NMP2PPeer:
*/
struct _NMP2PPeer {
NMObject parent;
NMP2PPeerPrivate _priv;
};
LAST_PROP
struct _NMP2PPeerClass {
NMObjectClass parent;
};
G_DEFINE_TYPE (NMP2PPeer, nm_p2p_peer, NM_TYPE_OBJECT)
#define NM_P2P_PEER_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMP2PPeer, NM_IS_P2P_PEER, NMObject)
/*****************************************************************************/
/**
* nm_p2p_peer_get_flags:
* @peer: a #NMP2PPeer
......@@ -279,25 +292,27 @@ nm_p2p_peer_connection_valid (NMP2PPeer *peer, NMConnection *connection)
const char *hw_address;
const char *setting_peer;
s_p2p_wifi = (NMSettingP2PWireless *) nm_connection_get_setting (connection, NM_TYPE_SETTING_P2P_WIRELESS);
if (!s_p2p_wifi)
return FALSE;
s_con = nm_connection_get_setting_connection (connection);
if (!s_con)
return FALSE;
ctype = nm_setting_connection_get_connection_type (s_con);
if (!ctype || !nm_streq (ctype, NM_SETTING_P2P_WIRELESS_SETTING_NAME))
return FALSE;
s_p2p_wifi = (NMSettingP2PWireless*) nm_connection_get_setting (connection, NM_TYPE_SETTING_P2P_WIRELESS);
if (!s_p2p_wifi)
if ( !ctype
|| !nm_streq (ctype, NM_SETTING_P2P_WIRELESS_SETTING_NAME))
return FALSE;
/* HW Address check */
hw_address = nm_p2p_peer_get_hw_address (peer);
if (!hw_address)
return FALSE;
setting_peer = nm_setting_p2p_wireless_get_peer (s_p2p_wifi);
if ( !setting_peer
|| g_strcmp0 (hw_address, setting_peer))
|| !nm_streq (hw_address, setting_peer))
return FALSE;
return TRUE;
......@@ -329,7 +344,7 @@ GPtrArray *
nm_p2p_peer_filter_connections (NMP2PPeer *peer, const GPtrArray *connections)
{
GPtrArray *filtered;
int i;
guint i;
filtered = g_ptr_array_new_with_free_func (g_object_unref);
for (i = 0; i < connections->len; i++) {
......@@ -344,6 +359,33 @@ nm_p2p_peer_filter_connections (NMP2PPeer *peer, const GPtrArray *connections)
/*****************************************************************************/
static void
init_dbus (NMObject *object)
{
NMP2PPeerPrivate *priv = NM_P2P_PEER_GET_PRIVATE (object);
const NMPropertiesInfo property_info[] = {
{ NM_P2P_PEER_FLAGS, &priv->flags },
{ NM_P2P_PEER_NAME, &priv->name },
{ NM_P2P_PEER_MANUFACTURER, &priv->manufacturer },
{ NM_P2P_PEER_MODEL, &priv->model },
{ NM_P2P_PEER_MODEL_NUMBER, &priv->model_number },
{ NM_P2P_PEER_SERIAL, &priv->serial },
{ NM_P2P_PEER_WFD_IES, &priv->wfd_ies },
{ NM_P2P_PEER_HW_ADDRESS, &priv->hw_address },
{ NM_P2P_PEER_STRENGTH, &priv->strength },
{ NM_P2P_PEER_LAST_SEEN, &priv->last_seen },
{ NULL },
};
NM_OBJECT_CLASS (nm_p2p_peer_parent_class)->init_dbus (object);
_nm_object_register_properties (object,
NM_DBUS_INTERFACE_P2P_PEER,
property_info);
}
/*****************************************************************************/
static void
get_property (GObject *object,
guint prop_id,
......@@ -389,6 +431,8 @@ get_property (GObject *object,
}
}
/*****************************************************************************/
static void
nm_p2p_peer_init (NMP2PPeer *peer)
{
......@@ -406,53 +450,24 @@ finalize (GObject *object)
g_free (priv->model_number);
g_free (priv->serial);
if (priv->wfd_ies)
g_bytes_unref (priv->wfd_ies);
g_free (priv->hw_address);
G_OBJECT_CLASS (nm_p2p_peer_parent_class)->finalize (object);
}
g_bytes_unref (priv->wfd_ies);
static void
init_dbus (NMObject *object)
{
NMP2PPeerPrivate *priv = NM_P2P_PEER_GET_PRIVATE (object);
const NMPropertiesInfo property_info[] = {
{ NM_P2P_PEER_FLAGS, &priv->flags },
{ NM_P2P_PEER_NAME, &priv->name },
{ NM_P2P_PEER_MANUFACTURER, &priv->manufacturer },
{ NM_P2P_PEER_MODEL, &priv->model },
{ NM_P2P_PEER_MODEL_NUMBER, &priv->model_number },
{ NM_P2P_PEER_SERIAL, &priv->serial },
{ NM_P2P_PEER_WFD_IES, &priv->wfd_ies },
{ NM_P2P_PEER_HW_ADDRESS, &priv->hw_address },
{ NM_P2P_PEER_STRENGTH, &priv->strength },
{ NM_P2P_PEER_LAST_SEEN, &priv->last_seen },
{ NULL },
};
NM_OBJECT_CLASS (nm_p2p_peer_parent_class)->init_dbus (object);
_nm_object_register_properties (object,
NM_DBUS_INTERFACE_P2P_PEER,
property_info);
G_OBJECT_CLASS (nm_p2p_peer_parent_class)->finalize (object);
}
static void
nm_p2p_peer_class_init (NMP2PPeerClass *peer_class)
nm_p2p_peer_class_init (NMP2PPeerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (peer_class);
NMObjectClass *nm_object_class = NM_OBJECT_CLASS (peer_class);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
NMObjectClass *nm_object_class = NM_OBJECT_CLASS (klass);
g_type_class_add_private (peer_class, sizeof (NMP2PPeerPrivate));
/* virtual methods */
object_class->get_property = get_property;
object_class->finalize = finalize;
object_class->finalize = finalize;
nm_object_class->init_dbus = init_dbus;
/* properties */
/**
* NMP2PPeer:flags:
*
......@@ -460,13 +475,12 @@ nm_p2p_peer_class_init (NMP2PPeerClass *peer_class)
*
* Since: 1.16
**/
g_object_class_install_property
(object_class, PROP_FLAGS,
g_param_spec_flags (NM_P2P_PEER_FLAGS, "", "",
NM_TYPE_802_11_AP_FLAGS,
NM_802_11_AP_FLAGS_NONE,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
obj_properties[PROP_FLAGS] =
g_param_spec_flags (NM_P2P_PEER_FLAGS, "", "",
NM_TYPE_802_11_AP_FLAGS,
NM_802_11_AP_FLAGS_NONE,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
/**
* NMP2PPeer:name:
......@@ -475,12 +489,11 @@ nm_p2p_peer_class_init (NMP2PPeerClass *peer_class)
*
* Since: 1.16
**/
g_object_class_install_property
(object_class, PROP_NAME,
g_param_spec_string (NM_P2P_PEER_NAME, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
obj_properties[PROP_NAME] =
g_param_spec_string (NM_P2P_PEER_NAME, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
/**
* NMP2PPeer:manufacturer:
......@@ -489,12 +502,11 @@ nm_p2p_peer_class_init (NMP2PPeerClass *peer_class)
*
* Since: 1.16
**/
g_object_class_install_property
(object_class, PROP_MANUFACTURER,
g_param_spec_string (NM_P2P_PEER_MANUFACTURER, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
obj_properties[PROP_MANUFACTURER] =
g_param_spec_string (NM_P2P_PEER_MANUFACTURER, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
/**
* NMP2PPeer:model:
......@@ -503,12 +515,11 @@ nm_p2p_peer_class_init (NMP2PPeerClass *peer_class)
*
* Since: 1.16
**/
g_object_class_install_property
(object_class, PROP_MODEL,
g_param_spec_string (NM_P2P_PEER_MODEL, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
obj_properties[PROP_MODEL] =
g_param_spec_string (NM_P2P_PEER_MODEL, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
/**
* NMP2PPeer:model-number:
......@@ -517,12 +528,11 @@ nm_p2p_peer_class_init (NMP2PPeerClass *peer_class)
*
* Since: 1.16
**/
g_object_class_install_property
(object_class, PROP_MODEL_NUMBER,
g_param_spec_string (NM_P2P_PEER_MODEL_NUMBER, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
obj_properties[PROP_MODEL_NUMBER] =
g_param_spec_string (NM_P2P_PEER_MODEL_NUMBER, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
/**
* NMP2PPeer:serial:
......@@ -531,12 +541,11 @@ nm_p2p_peer_class_init (NMP2PPeerClass *peer_class)
*
* Since: 1.16
**/
g_object_class_install_property
(object_class, PROP_SERIAL,
g_param_spec_string (NM_P2P_PEER_SERIAL, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
obj_properties[PROP_SERIAL] =
g_param_spec_string (NM_P2P_PEER_SERIAL, "", "",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
/**
* NMP2PPeer:wfd-ies:
......@@ -545,12 +554,11 @@ nm_p2p_peer_class_init (NMP2PPeerClass *peer_class)
*
* Since: 1.16
**/
g_object_class_install_property
(object_class, PROP_WFD_IES,
g_param_spec_boxed (NM_P2P_PEER_WFD_IES, "", "",
G_TYPE_BYTES,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
obj_properties[PROP_WFD_IES] =