1264d2d7faee6e60a8545a50c97d330488020e0d
[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 <gdbus.h>
27
28 #include "connman.h"
29
30 static DBusConnection *connection = NULL;
31 static unsigned int index = 0;
32
33 static GSList *networks = NULL;
34
35 void __connman_iface_network_list(struct connman_iface *iface,
36                                                 DBusMessageIter *iter)
37 {
38         GSList *list;
39
40         DBG("");
41
42         for (list = networks; list; list = list->next) {
43                 struct connman_network *network = list->data;
44
45                 if (network->iface != iface)
46                         continue;
47
48                 dbus_message_iter_append_basic(iter,
49                                 DBUS_TYPE_OBJECT_PATH, &network->path);
50         }
51 }
52
53 struct connman_network *__connman_iface_find_network(struct connman_iface *iface,
54                                                                 const char *path)
55 {
56         GSList *list;
57
58         DBG("");
59
60         for (list = networks; list; list = list->next) {
61                 struct connman_network *network = list->data;
62
63                 if (network->iface == iface &&
64                                 g_str_equal(network->path, path) == TRUE)
65                         return network;
66         }
67
68         return NULL;
69 }
70
71 int __connman_iface_remove_network(struct connman_iface *iface, const char *path)
72 {
73         g_dbus_unregister_interface(connection, path,
74                                         CONNMAN_NETWORK_INTERFACE);
75
76         return 0;
77 }
78
79 static DBusMessage *get_identifier(DBusConnection *conn,
80                                         DBusMessage *msg, void *data)
81 {
82         struct connman_network *network = data;
83         DBusMessage *reply;
84
85         DBG("conn %p", conn);
86
87         reply = dbus_message_new_method_return(msg);
88         if (reply == NULL)
89                 return NULL;
90
91         dbus_message_append_args(reply, DBUS_TYPE_STRING, &network->identifier,
92                                                         DBUS_TYPE_INVALID);
93
94         return reply;
95 }
96
97 static DBusMessage *get_passphrase(DBusConnection *conn,
98                                         DBusMessage *msg, void *data)
99 {
100         struct connman_network *network = data;
101         DBusMessage *reply;
102
103         DBG("conn %p", conn);
104
105         reply = dbus_message_new_method_return(msg);
106         if (reply == NULL)
107                 return NULL;
108
109         dbus_message_append_args(reply, DBUS_TYPE_STRING, &network->passphrase,
110                                                         DBUS_TYPE_INVALID);
111
112         return reply;
113 }
114
115 static GDBusMethodTable network_methods[] = {
116         { "GetIdentifier", "", "s", get_identifier },
117         { "GetPassphrase", "", "s", get_passphrase },
118         { },
119 };
120
121 static void network_free(void *data)
122 {
123         struct connman_network *network = data;
124
125         DBG("");
126
127         networks = g_slist_remove(networks, network);
128
129         g_free(network->path);
130         g_free(network->identifier);
131         g_free(network->passphrase);
132         g_free(network);
133 }
134
135 const char *__connman_iface_add_network(struct connman_iface *iface,
136                                 const char *identifier, const char *passphrase)
137 {
138         struct connman_network *network;
139         gchar *path;
140
141         DBG("iface %p", iface);
142
143         network = g_try_new0(struct connman_network, 1);
144         if (network == NULL)
145                 return NULL;
146
147         path = g_strdup_printf("%s/net_%d", iface->path, index++);
148         if (path == NULL) {
149                 g_free(network);
150                 return NULL;
151         }
152
153         network->iface = iface;
154
155         network->path = path;
156         network->identifier = g_strdup(identifier);
157         network->passphrase = g_strdup(passphrase ? passphrase : "");
158
159         networks = g_slist_append(networks, network);
160
161         g_dbus_register_interface(connection, path, CONNMAN_NETWORK_INTERFACE,
162                                                 network_methods, NULL, NULL,
163                                                         network, network_free);
164
165         return path;
166 }
167
168 int __connman_network_init(DBusConnection *conn)
169 {
170         DBG("conn %p", conn);
171
172         connection = dbus_connection_ref(conn);
173         if (connection == NULL)
174                 return -1;
175
176         return 0;
177 }
178
179 void __connman_network_cleanup(void)
180 {
181         DBG("conn %p", connection);
182
183         dbus_connection_unref(connection);
184 }