Commit 00fe31f5 authored by Dan Williams's avatar Dan Williams

core: move device factory type function into factory object

In preparation for internal device types exposing factories too, it's
easier to have the device type that the factory creates be returned
by the factory object instead of the plugin, because internal device
types don't have plugins.

This requires that we create the factory objects earlier, which
further requires that any operations that trigger signals must be
moved out of each factory's construction path to a separate start()
function.
parent beb18050
{ {
global: global:
nm_device_factory_create; nm_device_factory_create;
nm_device_factory_get_device_type;
local: local:
*; *;
}; };
...@@ -32,7 +32,6 @@ ...@@ -32,7 +32,6 @@
typedef struct { typedef struct {
GUdevClient *client; GUdevClient *client;
GSList *devices; GSList *devices;
guint start_id;
} NMAtmManagerPrivate; } NMAtmManagerPrivate;
#define NM_ATM_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_ATM_MANAGER, NMAtmManagerPrivate)) #define NM_ATM_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_ATM_MANAGER, NMAtmManagerPrivate))
...@@ -46,20 +45,12 @@ G_DEFINE_TYPE_EXTENDED (NMAtmManager, nm_atm_manager, G_TYPE_OBJECT, 0, ...@@ -46,20 +45,12 @@ G_DEFINE_TYPE_EXTENDED (NMAtmManager, nm_atm_manager, G_TYPE_OBJECT, 0,
/**************************************************************************/ /**************************************************************************/
#define PLUGIN_TYPE NM_DEVICE_TYPE_ADSL
G_MODULE_EXPORT NMDeviceFactory * G_MODULE_EXPORT NMDeviceFactory *
nm_device_factory_create (GError **error) nm_device_factory_create (GError **error)
{ {
return (NMDeviceFactory *) g_object_new (NM_TYPE_ATM_MANAGER, NULL); return (NMDeviceFactory *) g_object_new (NM_TYPE_ATM_MANAGER, NULL);
} }
G_MODULE_EXPORT NMDeviceType
nm_device_factory_get_device_type (void)
{
return PLUGIN_TYPE;
}
/************************************************************************/ /************************************************************************/
static gboolean static gboolean
...@@ -163,9 +154,10 @@ adsl_remove (NMAtmManager *self, GUdevDevice *udev_device) ...@@ -163,9 +154,10 @@ adsl_remove (NMAtmManager *self, GUdevDevice *udev_device)
} }
} }
static gboolean static void
query_devices (NMAtmManager *self) start (NMDeviceFactory *factory)
{ {
NMAtmManager *self = NM_ATM_MANAGER (factory);
NMAtmManagerPrivate *priv = NM_ATM_MANAGER_GET_PRIVATE (self); NMAtmManagerPrivate *priv = NM_ATM_MANAGER_GET_PRIVATE (self);
GUdevEnumerator *enumerator; GUdevEnumerator *enumerator;
GList *devices, *iter; GList *devices, *iter;
...@@ -180,8 +172,6 @@ query_devices (NMAtmManager *self) ...@@ -180,8 +172,6 @@ query_devices (NMAtmManager *self)
} }
g_list_free (devices); g_list_free (devices);
g_object_unref (enumerator); g_object_unref (enumerator);
return G_SOURCE_REMOVE;
} }
static void static void
...@@ -212,6 +202,12 @@ handle_uevent (GUdevClient *client, ...@@ -212,6 +202,12 @@ handle_uevent (GUdevClient *client,
adsl_remove (self, device); adsl_remove (self, device);
} }
static NMDeviceType
get_device_type (NMDeviceFactory *factory)
{
return NM_DEVICE_TYPE_ADSL;
}
/*********************************************************************/ /*********************************************************************/
static void static void
...@@ -222,13 +218,13 @@ nm_atm_manager_init (NMAtmManager *self) ...@@ -222,13 +218,13 @@ nm_atm_manager_init (NMAtmManager *self)
priv->client = g_udev_client_new (subsys); priv->client = g_udev_client_new (subsys);
g_signal_connect (priv->client, "uevent", G_CALLBACK (handle_uevent), self); g_signal_connect (priv->client, "uevent", G_CALLBACK (handle_uevent), self);
priv->start_id = g_idle_add ((GSourceFunc) query_devices, self);
} }
static void static void
device_factory_interface_init (NMDeviceFactory *factory_iface) device_factory_interface_init (NMDeviceFactory *factory_iface)
{ {
factory_iface->get_device_type = get_device_type;
factory_iface->start = start;
} }
static void static void
...@@ -242,11 +238,6 @@ dispose (GObject *object) ...@@ -242,11 +238,6 @@ dispose (GObject *object)
g_signal_handlers_disconnect_by_func (priv->client, handle_uevent, self); g_signal_handlers_disconnect_by_func (priv->client, handle_uevent, self);
g_clear_object (&priv->client); g_clear_object (&priv->client);
if (priv->start_id) {
g_source_remove (priv->start_id);
priv->start_id = 0;
}
for (iter = priv->devices; iter; iter = iter->next) for (iter = priv->devices; iter; iter = iter->next)
g_object_weak_unref (G_OBJECT (iter->data), device_destroyed, self); g_object_weak_unref (G_OBJECT (iter->data), device_destroyed, self);
g_clear_pointer (&priv->devices, g_slist_free); g_clear_pointer (&priv->devices, g_slist_free);
......
{ {
global: global:
nm_device_factory_create; nm_device_factory_create;
nm_device_factory_get_device_type;
local: local:
*; *;
}; };
...@@ -62,20 +62,12 @@ static void check_bluez_and_try_setup (NMBluezManager *self); ...@@ -62,20 +62,12 @@ static void check_bluez_and_try_setup (NMBluezManager *self);
/**************************************************************************/ /**************************************************************************/
#define PLUGIN_TYPE NM_DEVICE_TYPE_BT
G_MODULE_EXPORT NMDeviceFactory * G_MODULE_EXPORT NMDeviceFactory *
nm_device_factory_create (GError **error) nm_device_factory_create (GError **error)
{ {
return (NMDeviceFactory *) g_object_new (NM_TYPE_BLUEZ_MANAGER, NULL); return (NMDeviceFactory *) g_object_new (NM_TYPE_BLUEZ_MANAGER, NULL);
} }
G_MODULE_EXPORT NMDeviceType
nm_device_factory_get_device_type (void)
{
return PLUGIN_TYPE;
}
/************************************************************************/ /************************************************************************/
struct AsyncData { struct AsyncData {
...@@ -368,6 +360,18 @@ check_bluez_and_try_setup (NMBluezManager *self) ...@@ -368,6 +360,18 @@ check_bluez_and_try_setup (NMBluezManager *self)
async_data_pack (self)); async_data_pack (self));
} }
static void
start (NMDeviceFactory *factory)
{
check_bluez_and_try_setup (NM_BLUEZ_MANAGER (factory));
}
static NMDeviceType
get_device_type (NMDeviceFactory *factory)
{
return NM_DEVICE_TYPE_BT;
}
/*********************************************************************/ /*********************************************************************/
static void static void
...@@ -390,16 +394,6 @@ dispose (GObject *object) ...@@ -390,16 +394,6 @@ dispose (GObject *object)
priv->bluez_version = 0; priv->bluez_version = 0;
} }
static void
constructed (GObject *object)
{
NMBluezManager *self = NM_BLUEZ_MANAGER (object);
G_OBJECT_CLASS (nm_bluez_manager_parent_class)->constructed (object);
check_bluez_and_try_setup (self);
}
static void static void
nm_bluez_manager_init (NMBluezManager *self) nm_bluez_manager_init (NMBluezManager *self)
{ {
...@@ -412,6 +406,8 @@ nm_bluez_manager_init (NMBluezManager *self) ...@@ -412,6 +406,8 @@ nm_bluez_manager_init (NMBluezManager *self)
static void static void
device_factory_interface_init (NMDeviceFactory *factory_iface) device_factory_interface_init (NMDeviceFactory *factory_iface)
{ {
factory_iface->get_device_type = get_device_type;
factory_iface->start = start;
} }
static void static void
...@@ -423,6 +419,5 @@ nm_bluez_manager_class_init (NMBluezManagerClass *klass) ...@@ -423,6 +419,5 @@ nm_bluez_manager_class_init (NMBluezManagerClass *klass)
/* virtual methods */ /* virtual methods */
object_class->dispose = dispose; object_class->dispose = dispose;
object_class->constructed = constructed;
} }
...@@ -91,6 +91,23 @@ nm_device_factory_get_type (void) ...@@ -91,6 +91,23 @@ nm_device_factory_get_type (void)
return device_factory_type; return device_factory_type;
} }
NMDeviceType
nm_device_factory_get_device_type (NMDeviceFactory *factory)
{
g_return_val_if_fail (factory != NULL, NM_DEVICE_TYPE_UNKNOWN);
return NM_DEVICE_FACTORY_GET_INTERFACE (factory)->get_device_type (factory);
}
void
nm_device_factory_start (NMDeviceFactory *factory)
{
g_return_if_fail (factory != NULL);
if (NM_DEVICE_FACTORY_GET_INTERFACE (factory)->start)
NM_DEVICE_FACTORY_GET_INTERFACE (factory)->start (factory);
}
NMDevice * NMDevice *
nm_device_factory_new_link (NMDeviceFactory *factory, nm_device_factory_new_link (NMDeviceFactory *factory,
NMPlatformLink *plink, NMPlatformLink *plink,
......
...@@ -42,7 +42,7 @@ typedef struct _NMDeviceFactory NMDeviceFactory; ...@@ -42,7 +42,7 @@ typedef struct _NMDeviceFactory NMDeviceFactory;
* Creates a #GObject that implements the #NMDeviceFactory interface. This * Creates a #GObject that implements the #NMDeviceFactory interface. This
* function must not emit any signals or perform any actions that would cause * function must not emit any signals or perform any actions that would cause
* devices or components to be created immediately. Instead these should be * devices or components to be created immediately. Instead these should be
* deferred to an idle handler. * deferred to the "start" interface method.
* *
* Returns: the #GObject implementing #NMDeviceFactory or %NULL * Returns: the #GObject implementing #NMDeviceFactory or %NULL
*/ */
...@@ -51,16 +51,6 @@ NMDeviceFactory *nm_device_factory_create (GError **error); ...@@ -51,16 +51,6 @@ NMDeviceFactory *nm_device_factory_create (GError **error);
/* Should match nm_device_factory_create() */ /* Should match nm_device_factory_create() */
typedef NMDeviceFactory * (*NMDeviceFactoryCreateFunc) (GError **error); typedef NMDeviceFactory * (*NMDeviceFactoryCreateFunc) (GError **error);
/**
* nm_device_factory_get_device_type:
*
* Returns: the #NMDeviceType that this plugin creates
*/
NMDeviceType nm_device_factory_get_device_type (void);
/* Should match nm_device_factory_get_device_type() */
typedef NMDeviceType (*NMDeviceFactoryDeviceTypeFunc) (void);
/********************************************************************/ /********************************************************************/
#define NM_TYPE_DEVICE_FACTORY (nm_device_factory_get_type ()) #define NM_TYPE_DEVICE_FACTORY (nm_device_factory_get_type ())
...@@ -75,6 +65,25 @@ typedef NMDeviceType (*NMDeviceFactoryDeviceTypeFunc) (void); ...@@ -75,6 +65,25 @@ typedef NMDeviceType (*NMDeviceFactoryDeviceTypeFunc) (void);
struct _NMDeviceFactory { struct _NMDeviceFactory {
GTypeInterface g_iface; GTypeInterface g_iface;
/**
* get_device_type:
* @factory: the #NMDeviceFactory
*
* This function MUST be implemented.
*
* Returns: the #NMDeviceType that this plugin creates
*/
NMDeviceType (*get_device_type) (NMDeviceFactory *factory);
/**
* start:
* @factory: the #NMDeviceFactory
*
* Start the factory and discover any existing devices that the factory
* can manage.
*/
void (*start) (NMDeviceFactory *factory);
/** /**
* new_link: * new_link:
* @factory: the #NMDeviceFactory * @factory: the #NMDeviceFactory
...@@ -141,6 +150,10 @@ struct _NMDeviceFactory { ...@@ -141,6 +150,10 @@ struct _NMDeviceFactory {
GType nm_device_factory_get_type (void); GType nm_device_factory_get_type (void);
NMDeviceType nm_device_factory_get_device_type (NMDeviceFactory *factory);
void nm_device_factory_start (NMDeviceFactory *factory);
NMDevice * nm_device_factory_new_link (NMDeviceFactory *factory, NMDevice * nm_device_factory_new_link (NMDeviceFactory *factory,
NMPlatformLink *plink, NMPlatformLink *plink,
GError **error); GError **error);
......
{ {
global: global:
nm_device_factory_create; nm_device_factory_create;
nm_device_factory_get_device_type;
local: local:
*; *;
}; };
...@@ -35,12 +35,13 @@ static void device_factory_interface_init (NMDeviceFactory *factory_iface); ...@@ -35,12 +35,13 @@ static void device_factory_interface_init (NMDeviceFactory *factory_iface);
G_DEFINE_TYPE_EXTENDED (NMTeamFactory, nm_team_factory, G_TYPE_OBJECT, 0, G_DEFINE_TYPE_EXTENDED (NMTeamFactory, nm_team_factory, G_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init)) G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init))
#define NM_TEAM_FACTORY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_TEAM_FACTORY, NMTeamFactoryPrivate)) /************************************************************************/
typedef struct {
char dummy;
} NMTeamFactoryPrivate;
G_MODULE_EXPORT NMDeviceFactory *
nm_device_factory_create (GError **error)
{
return (NMDeviceFactory *) g_object_new (NM_TYPE_TEAM_FACTORY, NULL);
}
/************************************************************************/ /************************************************************************/
...@@ -62,20 +63,10 @@ create_virtual_device_for_connection (NMDeviceFactory *factory, ...@@ -62,20 +63,10 @@ create_virtual_device_for_connection (NMDeviceFactory *factory,
return NULL; return NULL;
} }
/************************************************************************/ static NMDeviceType
get_device_type (NMDeviceFactory *factory)
#define PLUGIN_TYPE NM_DEVICE_TYPE_TEAM
G_MODULE_EXPORT NMDeviceFactory *
nm_device_factory_create (GError **error)
{
return (NMDeviceFactory *) g_object_new (NM_TYPE_TEAM_FACTORY, NULL);
}
G_MODULE_EXPORT NMDeviceType
nm_device_factory_get_device_type (void)
{ {
return PLUGIN_TYPE; return NM_DEVICE_TYPE_TEAM;
} }
/************************************************************************/ /************************************************************************/
...@@ -90,21 +81,10 @@ device_factory_interface_init (NMDeviceFactory *factory_iface) ...@@ -90,21 +81,10 @@ device_factory_interface_init (NMDeviceFactory *factory_iface)
{ {
factory_iface->new_link = new_link; factory_iface->new_link = new_link;
factory_iface->create_virtual_device_for_connection = create_virtual_device_for_connection; factory_iface->create_virtual_device_for_connection = create_virtual_device_for_connection;
} factory_iface->get_device_type = get_device_type;
static void
dispose (GObject *object)
{
/* Chain up to the parent class */
G_OBJECT_CLASS (nm_team_factory_parent_class)->dispose (object);
} }
static void static void
nm_team_factory_class_init (NMTeamFactoryClass *klass) nm_team_factory_class_init (NMTeamFactoryClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (object_class, sizeof (NMTeamFactoryPrivate));
object_class->dispose = dispose;
} }
{ {
global: global:
nm_device_factory_create; nm_device_factory_create;
nm_device_factory_get_device_type;
local: local:
*; *;
}; };
...@@ -46,20 +46,12 @@ G_DEFINE_TYPE_EXTENDED (NMWifiFactory, nm_wifi_factory, G_TYPE_OBJECT, 0, ...@@ -46,20 +46,12 @@ G_DEFINE_TYPE_EXTENDED (NMWifiFactory, nm_wifi_factory, G_TYPE_OBJECT, 0,
/**************************************************************************/ /**************************************************************************/
#define PLUGIN_TYPE NM_DEVICE_TYPE_WIFI
G_MODULE_EXPORT NMDeviceFactory * G_MODULE_EXPORT NMDeviceFactory *
nm_device_factory_create (GError **error) nm_device_factory_create (GError **error)
{ {
return (NMDeviceFactory *) g_object_new (NM_TYPE_WIFI_FACTORY, NULL); return (NMDeviceFactory *) g_object_new (NM_TYPE_WIFI_FACTORY, NULL);
} }
G_MODULE_EXPORT NMDeviceType
nm_device_factory_get_device_type (void)
{
return PLUGIN_TYPE;
}
/**************************************************************************/ /**************************************************************************/
static NMDevice * static NMDevice *
...@@ -72,10 +64,17 @@ new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) ...@@ -72,10 +64,17 @@ new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error)
return NULL; return NULL;
} }
static NMDeviceType
get_device_type (NMDeviceFactory *factory)
{
return NM_DEVICE_TYPE_WIFI;
}
static void static void
device_factory_interface_init (NMDeviceFactory *factory_iface) device_factory_interface_init (NMDeviceFactory *factory_iface)
{ {
factory_iface->new_link = new_link; factory_iface->new_link = new_link;
factory_iface->get_device_type = get_device_type;
} }
static void static void
......
{ {
global: global:
nm_device_factory_create; nm_device_factory_create;
nm_device_factory_get_device_type;
local: local:
*; *;
}; };
...@@ -44,20 +44,12 @@ G_DEFINE_TYPE_EXTENDED (NMWimaxFactory, nm_wimax_factory, G_TYPE_OBJECT, 0, ...@@ -44,20 +44,12 @@ G_DEFINE_TYPE_EXTENDED (NMWimaxFactory, nm_wimax_factory, G_TYPE_OBJECT, 0,
/**************************************************************************/ /**************************************************************************/
#define PLUGIN_TYPE NM_DEVICE_TYPE_WIMAX
G_MODULE_EXPORT NMDeviceFactory * G_MODULE_EXPORT NMDeviceFactory *
nm_device_factory_create (GError **error) nm_device_factory_create (GError **error)
{ {
return (NMDeviceFactory *) g_object_new (NM_TYPE_WIMAX_FACTORY, NULL); return (NMDeviceFactory *) g_object_new (NM_TYPE_WIMAX_FACTORY, NULL);
} }
G_MODULE_EXPORT NMDeviceType
nm_device_factory_get_device_type (void)
{
return PLUGIN_TYPE;
}
/**************************************************************************/ /**************************************************************************/
static NMDevice * static NMDevice *
...@@ -72,10 +64,17 @@ new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) ...@@ -72,10 +64,17 @@ new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error)
return (NMDevice *) nm_device_wimax_new (plink); return (NMDevice *) nm_device_wimax_new (plink);
} }
static NMDeviceType
get_device_type (NMDeviceFactory *factory)
{
return NM_DEVICE_TYPE_WIMAX;
}
static void static void
device_factory_interface_init (NMDeviceFactory *factory_iface) device_factory_interface_init (NMDeviceFactory *factory_iface)
{ {
factory_iface->new_link = new_link; factory_iface->new_link = new_link;
factory_iface->get_device_type = get_device_type;
} }
static void static void
......
{ {
global: global:
nm_device_factory_create; nm_device_factory_create;
nm_device_factory_get_device_type;
local: local:
*; *;
}; };
...@@ -43,20 +43,12 @@ typedef struct { ...@@ -43,20 +43,12 @@ typedef struct {
/************************************************************************/ /************************************************************************/
#define PLUGIN_TYPE NM_DEVICE_TYPE_MODEM
G_MODULE_EXPORT NMDeviceFactory * G_MODULE_EXPORT NMDeviceFactory *
nm_device_factory_create (GError **error) nm_device_factory_create (GError **error)
{ {
return (NMDeviceFactory *) g_object_new (NM_TYPE_WWAN_FACTORY, NULL); return (NMDeviceFactory *) g_object_new (NM_TYPE_WWAN_FACTORY, NULL);
} }
G_MODULE_EXPORT NMDeviceType
nm_device_factory_get_device_type (void)
{
return PLUGIN_TYPE;
}
/************************************************************************/ /************************************************************************/
static void static void
...@@ -93,9 +85,17 @@ modem_added_cb (NMModemManager *manager, ...@@ -93,9 +85,17 @@ modem_added_cb (NMModemManager *manager,
g_object_unref (device); g_object_unref (device);
} }
static NMDeviceType
get_device_type (NMDeviceFactory *factory)
{
return NM_DEVICE_TYPE_MODEM;
}
static void static void
nm_wwan_factory_init (NMWwanFactory *self) start (NMDeviceFactory *factory)
{ {
NMWwanFactory *self = NM_WWAN_FACTORY (factory);
NMWwanFactoryPrivate *priv = NM_WWAN_FACTORY_GET_PRIVATE (self); NMWwanFactoryPrivate *priv = NM_WWAN_FACTORY_GET_PRIVATE (self);
priv->mm = g_object_new (NM_TYPE_MODEM_MANAGER, NULL); priv->mm = g_object_new (NM_TYPE_MODEM_MANAGER, NULL);
...@@ -106,9 +106,16 @@ nm_wwan_factory_init (NMWwanFactory *self) ...@@ -106,9 +106,16 @@ nm_wwan_factory_init (NMWwanFactory *self)
self); self);
} }
static void
nm_wwan_factory_init (NMWwanFactory *self)
{
}
static void static void
device_factory_interface_init (NMDeviceFactory *factory_iface) device_factory_interface_init (NMDeviceFactory *factory_iface)
{ {
factory_iface->get_device_type = get_device_type;
factory_iface->start = start;
} }
static void static void
......
...@@ -1873,7 +1873,6 @@ factory_component_added_cb (NMDeviceFactory *factory, ...@@ -1873,7 +1873,6 @@ factory_component_added_cb (NMDeviceFactory *factory,
#define PLUGIN_PREFIX "libnm-device-plugin-" #define PLUGIN_PREFIX "libnm-device-plugin-"
#define PLUGIN_PATH_TAG "NMManager-plugin-path" #define PLUGIN_PATH_TAG "NMManager-plugin-path"
#define PLUGIN_TYPEFUNC_TAG "typefunc"
struct read_device_factory_paths_data { struct read_device_factory_paths_data {
char *path; char *path;
...@@ -1980,8 +1979,7 @@ load_device_factories (NMManager *self) ...@@ -1980,8 +1979,7 @@ load_device_factories (NMManager *self)
GModule *plugin; GModule *plugin;
NMDeviceFactory *factory; NMDeviceFactory *factory;
NMDeviceFactoryCreateFunc create_func; NMDeviceFactoryCreateFunc create_func;
NMDeviceFactoryDeviceTypeFunc type_func; NMDeviceType plugin_type;
NMDeviceType dev_type;
const char *found = NULL; const char *found = NULL;
GSList *iter; GSList *iter;
const char *item; const char *item;
...@@ -1996,30 +1994,6 @@ load_device_factories (NMManager *self) ...@@ -1996,30 +1994,6 @@ load_device_factories (NMManager *self)
continue; continue;
} }
if (!g_module_symbol (plugin, "nm_device_factory_get_device_type", (gpointer) &type_func)) {
nm_log_warn (LOGD_HW, "(%s): failed to find device factory type: %s", item, g_module_error ());
g_module_close (plugin);
continue;
}
/* Make sure we don't double-load plugins */
dev_type = type_func ();
for (iter = priv->factories; iter; iter = iter->next) {
NMDeviceFactoryDeviceTypeFunc loaded_type_func;
loaded_type_func = g_object_get_data (G_OBJECT (iter->data), PLUGIN_TYPEFUNC_TAG);