Add support for identifier and driver data
[connman] / src / network.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "connman.h"
27
28 struct connman_network {
29         struct connman_element element;
30         enum connman_network_type type;
31         char *identifier;
32
33         struct connman_network_driver *driver;
34         void *driver_data;
35 };
36
37 static void network_destruct(struct connman_element *element)
38 {
39         struct connman_network *network = element->network;
40
41         DBG("element %p name %s", element, element->name);
42
43         g_free(network->identifier);
44 }
45
46 /**
47  * connman_network_create:
48  * @identifier: network identifier (for example an unqiue name)
49  *
50  * Allocate a new network and assign the #identifier to it.
51  *
52  * Returns: a newly-allocated #connman_network structure
53  */
54 struct connman_network *connman_network_create(const char *identifier,
55                                                 enum connman_network_type type)
56 {
57         struct connman_network *network;
58
59         DBG("identifier %s type %d", identifier, type);
60
61         network = g_try_new0(struct connman_network, 1);
62         if (network == NULL)
63                 return NULL;
64
65         DBG("network %p", network);
66
67         network->element.refcount = 1;
68
69         network->element.name = g_strdup(identifier);
70         network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
71         network->element.index = -1;
72
73         network->element.network = network;
74         network->element.destruct = network_destruct;
75
76         network->type = type;
77         network->identifier = g_strdup(identifier);
78
79         return network;
80 }
81
82 /**
83  * connman_network_ref:
84  * @network: network structure
85  *
86  * Increase reference counter of  network
87  */
88 struct connman_network *connman_network_ref(struct connman_network *network)
89 {
90         if (connman_element_ref(&network->element) == NULL)
91                 return NULL;
92
93         return network;
94 }
95
96 /**
97  * connman_network_unref:
98  * @network: network structure
99  *
100  * Decrease reference counter of network
101  */
102 void connman_network_unref(struct connman_network *network)
103 {
104         connman_element_unref(&network->element);
105 }
106
107 /**
108  * connman_network_get_identifier:
109  * @network: network structure
110  *
111  * Get identifier of network
112  */
113 const char *connman_network_get_identifier(struct connman_network *network)
114 {
115         return network->identifier;
116 }
117
118 /**
119  * connman_network_get_data:
120  * @network: network structure
121  *
122  * Get private network data pointer
123  */
124 void *connman_network_get_data(struct connman_network *network)
125 {
126         return network->driver_data;
127 }
128
129 /**
130  * connman_network_set_data:
131  * @network: network structure
132  * @data: data pointer
133  *
134  * Set private network data pointer
135  */
136 void connman_network_set_data(struct connman_network *network, void *data)
137 {
138         network->driver_data = data;
139 }
140
141 static int network_probe(struct connman_element *element)
142 {
143         return 0;
144 }
145
146 static void network_remove(struct connman_element *element)
147 {
148 }
149
150 static struct connman_driver network_driver = {
151         .name           = "network",
152         .type           = CONNMAN_ELEMENT_TYPE_NETWORK,
153         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
154         .probe          = network_probe,
155         .remove         = network_remove,
156 };
157
158 int __connman_network_init(void)
159 {
160         DBG("");
161
162         return connman_driver_register(&network_driver);
163 }
164
165 void __connman_network_cleanup(void)
166 {
167         DBG("");
168
169         connman_driver_unregister(&network_driver);
170 }