Check for valid default route when updating profile details
[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 void __connman_profile_changed(void)
69 {
70         const char *path = __connman_profile_active_path();
71         DBusMessage *signal;
72         DBusMessageIter entry;
73
74         __connman_connection_update_gateway();
75
76         signal = dbus_message_new_signal(path,
77                                 CONNMAN_PROFILE_INTERFACE, "PropertyChanged");
78         if (signal == NULL)
79                 return;
80
81         dbus_message_iter_init_append(signal, &entry);
82         append_services(&entry);
83         g_dbus_send_message(connection, signal);
84
85         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
86                                 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
87         if (signal == NULL)
88                 return;
89
90         dbus_message_iter_init_append(signal, &entry);
91         append_services(&entry);
92         g_dbus_send_message(connection, signal);
93 }
94
95 int __connman_profile_add_device(struct connman_device *device)
96 {
97         struct connman_service *service;
98
99         DBG("device %p", device);
100
101         service = __connman_service_create_from_device(device);
102         if (service == NULL)
103                 return -EINVAL;
104
105         return 0;
106 }
107
108 int __connman_profile_remove_device(struct connman_device *device)
109 {
110         struct connman_service *service;
111
112         DBG("device %p", device);
113
114         service = __connman_service_lookup_from_device(device);
115         if (service == NULL)
116                 return -EINVAL;
117
118         __connman_service_put(service);
119
120         return 0;
121 }
122
123 int __connman_profile_add_network(struct connman_network *network)
124 {
125         struct connman_service *service;
126
127         DBG("network %p", network);
128
129         service = __connman_service_create_from_network(network);
130         if (service == NULL)
131                 return -EINVAL;
132
133         return 0;
134 }
135
136 int __connman_profile_remove_network(struct connman_network *network)
137 {
138         struct connman_service *service;
139
140         DBG("network %p", network);
141
142         service = __connman_service_lookup_from_network(network);
143         if (service == NULL)
144                 return -EINVAL;
145
146         __connman_service_put(service);
147
148         return 0;
149 }
150
151 void __connman_profile_list(DBusMessageIter *iter)
152 {
153         const char *path = __connman_profile_active_path();
154
155         DBG("");
156
157         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
158 }
159
160 static DBusMessage *get_properties(DBusConnection *conn,
161                                         DBusMessage *msg, void *data)
162 {
163         const char *name = "Default";
164         DBusMessage *reply;
165         DBusMessageIter array, dict, entry;
166
167         DBG("conn %p", conn);
168
169         reply = dbus_message_new_method_return(msg);
170         if (reply == NULL)
171                 return NULL;
172
173         dbus_message_iter_init_append(reply, &array);
174
175         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
176                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
177                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
178                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
179
180         connman_dbus_dict_append_variant(&dict, "Name",
181                                                 DBUS_TYPE_STRING, &name);
182
183         dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
184                                                                 NULL, &entry);
185         append_services(&entry);
186         dbus_message_iter_close_container(&dict, &entry);
187
188         dbus_message_iter_close_container(&array, &dict);
189
190         return reply;
191 }
192
193 static GDBusMethodTable profile_methods[] = {
194         { "GetProperties", "", "a{sv}", get_properties },
195         { },
196 };
197
198 static GDBusSignalTable profile_signals[] = {
199         { "PropertyChanged", "sv" },
200         { },
201 };
202
203 int __connman_profile_init(DBusConnection *conn)
204 {
205         const char *path = __connman_profile_active_path();
206
207         DBG("conn %p", conn);
208
209         connection = dbus_connection_ref(conn);
210         if (connection == NULL)
211                 return -1;
212
213         g_dbus_register_interface(connection, path,
214                                         CONNMAN_PROFILE_INTERFACE,
215                                         profile_methods, profile_signals,
216                                                         NULL, NULL, NULL);
217
218         return 0;
219 }
220
221 void __connman_profile_cleanup(void)
222 {
223         const char *path = __connman_profile_active_path();
224
225         DBG("conn %p", connection);
226
227         g_dbus_unregister_interface(connection, path,
228                                                 CONNMAN_PROFILE_INTERFACE);
229
230         if (connection == NULL)
231                 return;
232
233         dbus_connection_unref(connection);
234 }