summaryrefslogtreecommitdiff
path: root/src/devices/nm-device-factory.h
blob: 34c056d20f92e1bcade9e481c396fe9b25b30783 (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2007 - 2014 Red Hat, Inc.
 */

#ifndef __NETWORKMANAGER_DEVICE_FACTORY_H__
#define __NETWORKMANAGER_DEVICE_FACTORY_H__

#include <glib.h>
#include <glib-object.h>

#include "nm-dbus-interface.h"
#include "nm-device.h"

/* WARNING: this file is private API between NetworkManager and its internal
 * device plugins.  Its API can change at any time and is not guaranteed to be
 * stable.  NM and device plugins are distributed together and this API is
 * not meant to enable third-party plugins.
 */

typedef struct _NMDeviceFactory NMDeviceFactory;

/**
 * nm_device_factory_create:
 * @error: an error if creation of the factory failed, or %NULL
 *
 * Creates a #GObject that implements the #NMDeviceFactory interface. This
 * function must not emit any signals or perform any actions that would cause
 * devices or components to be created immediately.  Instead these should be
 * deferred to the "start" interface method.
 *
 * Returns: the #GObject implementing #NMDeviceFactory or %NULL
 */
NMDeviceFactory *nm_device_factory_create (GError **error);

/* Should match nm_device_factory_create() */
typedef NMDeviceFactory * (*NMDeviceFactoryCreateFunc) (GError **error);

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

#define NM_TYPE_DEVICE_FACTORY               (nm_device_factory_get_type ())
#define NM_DEVICE_FACTORY(obj)               (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DEVICE_FACTORY, NMDeviceFactory))
#define NM_IS_DEVICE_FACTORY(obj)            (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DEVICE_FACTORY))
#define NM_DEVICE_FACTORY_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), NM_TYPE_DEVICE_FACTORY, NMDeviceFactory))

/* signals */
#define NM_DEVICE_FACTORY_COMPONENT_ADDED "component-added"
#define NM_DEVICE_FACTORY_DEVICE_ADDED    "device-added"

struct _NMDeviceFactory {
	GTypeInterface g_iface;

	/**
	 * get_supported_types:
	 * @factory: the #NMDeviceFactory
	 * @out_link_types: on return, a %NM_LINK_TYPE_NONE terminated
	 *  list of #NMLinkType that the plugin supports
	 * @out_setting_types: on return, a %NULL terminated list of
	 *  base-type #NMSetting names that the plugin can create devices for
	 *
	 * Returns the #NMLinkType and #NMSetting names that this plugin
	 * supports.  This function MUST be implemented.
	 */
	void (*get_supported_types) (NMDeviceFactory *factory,
	                             const NMLinkType **out_link_types,
	                             const char ***out_setting_types);

	/**
	 * start:
	 * @factory: the #NMDeviceFactory
	 *
	 * Start the factory and discover any existing devices that the factory
	 * can manage.
	 */
	void (*start)                   (NMDeviceFactory *factory);

	/**
	 * new_link:
	 * @factory: the #NMDeviceFactory
	 * @plink: the new link
	 * @out_ignore: on return, %TRUE if the link should be ignored
	 * @error: error if the link could be claimed but an error occurred
	 *
	 * The NetworkManager core was notified of a new link which the plugin
	 * may want to claim and create a #NMDevice subclass for.  If the link
	 * represents a device which the factory does not support, or the link
	 * is supported but the device could not be created, %NULL should be
	 * returned and @error should be set.
	 *
	 * If the plugin cannot create a #NMDevice for the link and wants the
	 * core to ignore it, set @out_ignore to %TRUE and return no error.
	 *
	 * @plink is guaranteed to be one of the types the factory returns in
	 * get_supported_types().
	 *
	 * Returns: the #NMDevice if the link was claimed and created, %NULL if not
	 */
	NMDevice * (*new_link)        (NMDeviceFactory *factory,
	                               NMPlatformLink *plink,
	                               gboolean *out_ignore,
	                               GError **error);

	/**
	 * create_virtual_device_for_connection:
	 * @factory: the #NMDeviceFactory
	 * @connection: the #NMConnection
	 * @error: a @GError in case of failure
	 *
	 * Virtual device types (such as team, bond, bridge) may need to be created.
	 * This function tries to create a device based on the given @connection.
	 *
	 * Returns: the newly created #NMDevice. If the factory does not support the
	 * connection type, it should return %NULL and leave @error unset. On error
	 * it should set @error and return %NULL.
	 */
	NMDevice * (*create_virtual_device_for_connection) (NMDeviceFactory *factory,
	                                                    NMConnection *connection,
	                                                    NMDevice *parent,
	                                                    GError **error);

	/**
	 * get_connection_parent:
	 * @factory: the #NMDeviceFactory
	 * @connection: the #NMConnection to return the parent name for, if supported
	 *
	 * Given a connection, returns the a parent interface name, parent connection
	 * UUID, or parent device hardware address for @connection.
	 *
	 * Returns: the parent interface name, parent connection UUID, parent
	 *   device hardware address, or %NULL
	 */
	const char * (*get_connection_parent) (NMDeviceFactory *factory,
	                                       NMConnection *connection);

	/**
	 * get_virtual_iface_name:
	 * @factory: the #NMDeviceFactory
	 * @connection: the #NMConnection to return the virtual interface name for
	 * @parent_iface: parent interface name
	 *
	 * Given a connection, returns the interface name that a device activating
	 * that connection would have.
	 *
	 * Returns: the interface name, or %NULL
	 */
	char * (*get_virtual_iface_name) (NMDeviceFactory *factory,
	                                  NMConnection *connection,
	                                  const char *parent_iface);

	/* Signals */

	/**
	 * device_added:
	 * @factory: the #NMDeviceFactory
	 * @device: the new #NMDevice subclass
	 *
	 * The factory emits this signal if it finds a new device by itself.
	 */
	void       (*device_added)    (NMDeviceFactory *factory, NMDevice *device);

	/**
	 * component_added:
	 * @factory: the #NMDeviceFactory
	 * @component: a new component which existing devices may wish to claim
	 *
	 * The factory emits this signal when it finds a new component.  For example,
	 * the WWAN factory may indicate that a new modem is available, which an
	 * existing Bluetooth device may wish to claim.  If no device claims the
	 * component, the plugin is allowed to create a new #NMDevice instance for
	 * that component and emit the "device-added" signal.
	 *
	 * Returns: %TRUE if the component was claimed by a device, %FALSE if not
	 */
	gboolean   (*component_added) (NMDeviceFactory *factory, GObject *component);
};

GType      nm_device_factory_get_type    (void);

void       nm_device_factory_get_supported_types (NMDeviceFactory *factory,
                                                  const NMLinkType **out_link_types,
                                                  const char ***out_setting_types);

const char *nm_device_factory_get_connection_parent (NMDeviceFactory *factory,
                                                     NMConnection *connection);

char *     nm_device_factory_get_virtual_iface_name (NMDeviceFactory *factory,
                                                     NMConnection *connection,
                                                     const char *parent_iface);

void       nm_device_factory_start       (NMDeviceFactory *factory);

NMDevice * nm_device_factory_new_link    (NMDeviceFactory *factory,
                                          NMPlatformLink *plink,
                                          gboolean *out_ignore,
                                          GError **error);

NMDevice * nm_device_factory_create_virtual_device_for_connection (NMDeviceFactory *factory,
                                                                   NMConnection *connection,
                                                                   NMDevice *parent,
                                                                   GError **error);

/* For use by implementations */
gboolean   nm_device_factory_emit_component_added (NMDeviceFactory *factory,
                                                   GObject *component);

#define NM_DEVICE_FACTORY_DECLARE_LINK_TYPES(...) \
	{ static const NMLinkType _df_links[] = { __VA_ARGS__, NM_LINK_TYPE_NONE }; *out_link_types = _df_links; }
#define NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES(...) \
	{ static const char *_df_settings[] = { __VA_ARGS__, NULL }; *out_setting_types = _df_settings; }

extern const NMLinkType _nm_device_factory_no_default_links[];
extern const char *_nm_device_factory_no_default_settings[];

#define NM_DEVICE_FACTORY_DECLARE_TYPES(...) \
	static void \
	get_supported_types (NMDeviceFactory *factory, \
	                     const NMLinkType **out_link_types, \
	                     const char ***out_setting_types) \
	{ \
		*out_link_types = _nm_device_factory_no_default_links; \
		*out_setting_types = _nm_device_factory_no_default_settings; \
 \
		{ __VA_ARGS__; } \
	} \
 \

/**************************************************************************
 * INTERNAL DEVICE FACTORY FUNCTIONS - devices provided by plugins should
 * not use these functions.
 **************************************************************************/

#define NM_DEVICE_FACTORY_DEFINE_INTERNAL(upper, mixed, lower, st_code, dfi_code) \
	typedef GObject NM##mixed##Factory; \
	typedef GObjectClass NM##mixed##FactoryClass; \
 \
	static GType nm_##lower##_factory_get_type (void); \
	static void device_factory_interface_init (NMDeviceFactory *factory_iface); \
 \
	G_DEFINE_TYPE_EXTENDED (NM##mixed##Factory, nm_##lower##_factory, G_TYPE_OBJECT, 0, \
	                        G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init) \
	                        _nm_device_factory_internal_register_type (g_define_type_id);) \
 \
	/* Use a module constructor to register the factory's GType at load \
	 * time, which then calls _nm_device_factory_internal_register_type() \
	 * to register the factory's GType with the Manager. \
	 */ \
	static void __attribute__((constructor)) \
	register_device_factory_internal_##lower (void) \
	{ \
		g_type_init (); \
		g_type_ensure (NM_TYPE_##upper##_FACTORY); \
	} \
 \
	NM_DEVICE_FACTORY_DECLARE_TYPES(st_code) \
 \
	static void \
	device_factory_interface_init (NMDeviceFactory *factory_iface) \
	{ \
		factory_iface->get_supported_types = get_supported_types; \
		dfi_code \
	} \
 \
	static void \
	nm_##lower##_factory_init (NM##mixed##Factory *self) \
	{ \
	} \
 \
	static void \
	nm_##lower##_factory_class_init (NM##mixed##FactoryClass *lower##_class) \
	{ \
	}

void _nm_device_factory_internal_register_type (GType factory_type);

/**************************************************************************
 * PRIVATE FACTORY FUNCTIONS - for factory consumers (eg, NMManager).
 **************************************************************************/

typedef void (*NMDeviceFactoryManagerFactoryFunc)    (NMDeviceFactory *factory,
                                                      gpointer user_data);

void              nm_device_factory_manager_load_factories (NMDeviceFactoryManagerFactoryFunc callback,
                                                            gpointer user_data);

NMDeviceFactory * nm_device_factory_manager_find_factory_for_link_type  (NMLinkType link_type);

NMDeviceFactory * nm_device_factory_manager_find_factory_for_connection (NMConnection *connection);

void              nm_device_factory_manager_for_each_factory (NMDeviceFactoryManagerFactoryFunc callback,
                                                              gpointer user_data);

#endif /* __NETWORKMANAGER_DEVICE_FACTORY_H__ */