1a080b981a0a1aaf5f09aae4c04bd0bc2bd8b164
[platform/upstream/connman.git] / src / network.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  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_object(connection, path);
74
75         return 0;
76 }
77
78 static DBusMessage *get_identifier(DBusConnection *conn,
79                                         DBusMessage *msg, void *data)
80 {
81         struct connman_network *network = data;
82         DBusMessage *reply;
83
84         DBG("conn %p", conn);
85
86         reply = dbus_message_new_method_return(msg);
87         if (reply == NULL)
88                 return NULL;
89
90         dbus_message_append_args(reply, DBUS_TYPE_STRING, &network->identifier,
91                                                         DBUS_TYPE_INVALID);
92
93         return reply;
94 }
95
96 static DBusMessage *get_passphrase(DBusConnection *conn,
97                                         DBusMessage *msg, void *data)
98 {
99         struct connman_network *network = data;
100         DBusMessage *reply;
101
102         DBG("conn %p", conn);
103
104         reply = dbus_message_new_method_return(msg);
105         if (reply == NULL)
106                 return NULL;
107
108         dbus_message_append_args(reply, DBUS_TYPE_STRING, &network->passphrase,
109                                                         DBUS_TYPE_INVALID);
110
111         return reply;
112 }
113
114 static GDBusMethodTable network_methods[] = {
115         { "GetIdentifier", "", "s", get_identifier },
116         { "GetPassphrase", "", "s", get_passphrase },
117         { },
118 };
119
120 static void network_free(void *data)
121 {
122         struct connman_network *network = data;
123
124         DBG("");
125
126         networks = g_slist_remove(networks, network);
127
128         g_free(network->path);
129         g_free(network->identifier);
130         g_free(network->passphrase);
131         g_free(network);
132 }
133
134 const char *__connman_iface_add_network(struct connman_iface *iface,
135                                 const char *identifier, const char *passphrase)
136 {
137         struct connman_network *network;
138         gchar *path;
139
140         DBG("iface %p", iface);
141
142         network = g_try_new0(struct connman_network, 1);
143         if (network == NULL)
144                 return NULL;
145
146         path = g_strdup_printf("%s/net_%d", iface->path, index++);
147         if (path == NULL) {
148                 g_free(network);
149                 return NULL;
150         }
151
152         network->iface = iface;
153
154         network->path = path;
155         network->identifier = g_strdup(identifier);
156         network->passphrase = g_strdup(passphrase ? passphrase : "");
157
158         networks = g_slist_append(networks, network);
159
160         g_dbus_register_object(connection, path, network, network_free);
161
162         g_dbus_register_interface(connection, path, CONNMAN_NETWORK_INTERFACE,
163                                                 network_methods, NULL, NULL);
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 }