When network gets remove ensure to disconnect the service
[framework/connectivity/connman.git] / src / profile.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 <glib.h>
27 #include <gdbus.h>
28
29 #include "connman.h"
30
31 #define PROFILE_DEFAULT_IDENT  "default"
32
33 static DBusConnection *connection = NULL;
34
35 const char *__connman_profile_active_ident(void)
36 {
37         DBG("");
38
39         return PROFILE_DEFAULT_IDENT;
40 }
41
42 const char *__connman_profile_active_path(void)
43 {
44         DBG("");
45
46         return "/profile/" PROFILE_DEFAULT_IDENT;
47 }
48
49 static void append_services(DBusMessageIter *entry)
50 {
51         DBusMessageIter value, iter;
52         const char *key = "Services";
53
54         dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
55
56         dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
57                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
58                                                                 &value);
59
60         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
61                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
62         __connman_service_list(&iter);
63         dbus_message_iter_close_container(&value, &iter);
64
65         dbus_message_iter_close_container(entry, &value);
66 }
67
68 static guint changed_timeout = 0;
69
70 static gboolean services_changed(gpointer user_data)
71 {
72         const char *path = __connman_profile_active_path();
73         DBusMessage *signal;
74         DBusMessageIter entry;
75
76         changed_timeout = 0;
77
78         signal = dbus_message_new_signal(path,
79                                 CONNMAN_PROFILE_INTERFACE, "PropertyChanged");
80         if (signal == NULL)
81                 return FALSE;
82
83         dbus_message_iter_init_append(signal, &entry);
84         append_services(&entry);
85         g_dbus_send_message(connection, signal);
86
87         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
88                                 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
89         if (signal == NULL)
90                 return FALSE;
91
92         dbus_message_iter_init_append(signal, &entry);
93         append_services(&entry);
94         g_dbus_send_message(connection, signal);
95
96         return FALSE;
97 }
98
99 void __connman_profile_changed(gboolean delayed)
100 {
101         DBG("");
102
103         if (changed_timeout > 0) {
104                 g_source_remove(changed_timeout);
105                 changed_timeout = 0;
106         }
107
108         if (__connman_connection_update_gateway() == TRUE) {
109                 services_changed(NULL);
110                 return;
111         }
112
113         if (delayed == FALSE) {
114                 services_changed(NULL);
115                 return;
116         }
117
118         changed_timeout = g_timeout_add_seconds(2, services_changed, NULL);
119 }
120
121 int __connman_profile_add_device(struct connman_device *device)
122 {
123         struct connman_service *service;
124
125         DBG("device %p", device);
126
127         service = __connman_service_create_from_device(device);
128         if (service == NULL)
129                 return -EINVAL;
130
131         return 0;
132 }
133
134 int __connman_profile_remove_device(struct connman_device *device)
135 {
136         struct connman_service *service;
137
138         DBG("device %p", device);
139
140         service = __connman_service_lookup_from_device(device);
141         if (service == NULL)
142                 return -EINVAL;
143
144         __connman_service_disconnect(service);
145
146         __connman_service_put(service);
147
148         return 0;
149 }
150
151 int __connman_profile_add_network(struct connman_network *network)
152 {
153         struct connman_service *service;
154
155         DBG("network %p", network);
156
157         service = __connman_service_create_from_network(network);
158         if (service == NULL)
159                 return -EINVAL;
160
161         return 0;
162 }
163
164 int __connman_profile_remove_network(struct connman_network *network)
165 {
166         struct connman_service *service;
167
168         DBG("network %p", network);
169
170         service = __connman_service_lookup_from_network(network);
171         if (service == NULL)
172                 return -EINVAL;
173
174         __connman_service_disconnect(service);
175
176         __connman_service_put(service);
177
178         return 0;
179 }
180
181 void __connman_profile_list(DBusMessageIter *iter)
182 {
183         const char *path = __connman_profile_active_path();
184
185         DBG("");
186
187         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
188 }
189
190 static DBusMessage *get_properties(DBusConnection *conn,
191                                         DBusMessage *msg, void *data)
192 {
193         const char *name = "Default";
194         DBusMessage *reply;
195         DBusMessageIter array, dict, entry;
196
197         DBG("conn %p", conn);
198
199         reply = dbus_message_new_method_return(msg);
200         if (reply == NULL)
201                 return NULL;
202
203         dbus_message_iter_init_append(reply, &array);
204
205         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
206                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
207                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
208                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
209
210         connman_dbus_dict_append_variant(&dict, "Name",
211                                                 DBUS_TYPE_STRING, &name);
212
213         dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
214                                                                 NULL, &entry);
215         append_services(&entry);
216         dbus_message_iter_close_container(&dict, &entry);
217
218         dbus_message_iter_close_container(&array, &dict);
219
220         return reply;
221 }
222
223 static GDBusMethodTable profile_methods[] = {
224         { "GetProperties", "", "a{sv}", get_properties },
225         { },
226 };
227
228 static GDBusSignalTable profile_signals[] = {
229         { "PropertyChanged", "sv" },
230         { },
231 };
232
233 int __connman_profile_init(DBusConnection *conn)
234 {
235         const char *path = __connman_profile_active_path();
236
237         DBG("conn %p", conn);
238
239         connection = dbus_connection_ref(conn);
240         if (connection == NULL)
241                 return -1;
242
243         g_dbus_register_interface(connection, path,
244                                         CONNMAN_PROFILE_INTERFACE,
245                                         profile_methods, profile_signals,
246                                                         NULL, NULL, NULL);
247
248         return 0;
249 }
250
251 void __connman_profile_cleanup(void)
252 {
253         const char *path = __connman_profile_active_path();
254
255         DBG("conn %p", connection);
256
257         g_dbus_unregister_interface(connection, path,
258                                                 CONNMAN_PROFILE_INTERFACE);
259
260         if (connection == NULL)
261                 return;
262
263         dbus_connection_unref(connection);
264 }