Add hooks for saving and loading service details
[connman] / 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  "/profile/default"
32
33 static DBusConnection *connection = NULL;
34
35 const char *__connman_profile_active(void)
36 {
37         DBG("");
38
39         return PROFILE_DEFAULT;
40 }
41
42 static void append_services(DBusMessageIter *entry)
43 {
44         DBusMessageIter value, iter;
45         const char *key = "Services";
46
47         dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
48
49         dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
50                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
51                                                                 &value);
52
53         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
54                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
55         __connman_service_list(&iter);
56         dbus_message_iter_close_container(&value, &iter);
57
58         dbus_message_iter_close_container(entry, &value);
59 }
60
61 void __connman_profile_changed(void)
62 {
63         const char *path = __connman_profile_active();
64         DBusMessage *signal;
65         DBusMessageIter entry;
66
67         signal = dbus_message_new_signal(path,
68                                 CONNMAN_PROFILE_INTERFACE, "PropertyChanged");
69         if (signal == NULL)
70                 return;
71
72         dbus_message_iter_init_append(signal, &entry);
73         append_services(&entry);
74         g_dbus_send_message(connection, signal);
75
76         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
77                                 CONNMAN_MANAGER_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
86 int __connman_profile_add_device(struct connman_device *device)
87 {
88         struct connman_service *service;
89
90         DBG("device %p", device);
91
92         service = __connman_service_create_from_device(device);
93         if (service == NULL)
94                 return -EINVAL;
95
96         return 0;
97 }
98
99 int __connman_profile_remove_device(struct connman_device *device)
100 {
101         struct connman_service *service;
102
103         DBG("device %p", device);
104
105         service = __connman_service_lookup_from_device(device);
106         if (service == NULL)
107                 return -EINVAL;
108
109         connman_service_put(service);
110
111         return 0;
112 }
113
114 int __connman_profile_add_network(struct connman_network *network)
115 {
116         struct connman_service *service;
117
118         DBG("network %p", network);
119
120         service = __connman_service_create_from_network(network);
121         if (service == NULL)
122                 return -EINVAL;
123
124         return 0;
125 }
126
127 int __connman_profile_remove_network(struct connman_network *network)
128 {
129         struct connman_service *service;
130
131         DBG("network %p", network);
132
133         service = __connman_service_lookup_from_network(network);
134         if (service == NULL)
135                 return -EINVAL;
136
137         connman_service_put(service);
138
139         return 0;
140 }
141
142 void __connman_profile_list(DBusMessageIter *iter)
143 {
144         const char *path = __connman_profile_active();
145
146         DBG("");
147
148         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
149 }
150
151 static DBusMessage *get_properties(DBusConnection *conn,
152                                         DBusMessage *msg, void *data)
153 {
154         const char *name = "Default";
155         DBusMessage *reply;
156         DBusMessageIter array, dict, entry;
157
158         DBG("conn %p", conn);
159
160         reply = dbus_message_new_method_return(msg);
161         if (reply == NULL)
162                 return NULL;
163
164         dbus_message_iter_init_append(reply, &array);
165
166         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
167                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
168                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
169                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
170
171         connman_dbus_dict_append_variant(&dict, "Name",
172                                                 DBUS_TYPE_STRING, &name);
173
174         dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
175                                                                 NULL, &entry);
176         append_services(&entry);
177         dbus_message_iter_close_container(&dict, &entry);
178
179         dbus_message_iter_close_container(&array, &dict);
180
181         return reply;
182 }
183
184 static GDBusMethodTable profile_methods[] = {
185         { "GetProperties", "", "a{sv}", get_properties },
186         { },
187 };
188
189 static GDBusSignalTable profile_signals[] = {
190         { "PropertyChanged", "sv" },
191         { },
192 };
193
194 int __connman_profile_init(DBusConnection *conn)
195 {
196         DBG("conn %p", conn);
197
198         connection = dbus_connection_ref(conn);
199         if (connection == NULL)
200                 return -1;
201
202         g_dbus_register_interface(connection, PROFILE_DEFAULT,
203                                         CONNMAN_PROFILE_INTERFACE,
204                                         profile_methods, profile_signals,
205                                                         NULL, NULL, NULL);
206
207         return 0;
208 }
209
210 void __connman_profile_cleanup(void)
211 {
212         DBG("conn %p", connection);
213
214         g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
215                                                 CONNMAN_PROFILE_INTERFACE);
216
217         if (connection == NULL)
218                 return;
219
220         dbus_connection_unref(connection);
221 }