eeb777f1e5ac65a0879e0259d74ef619b27b94e2
[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_set_carrier(struct connman_device *device,
115                                                 connman_bool_t carrier)
116 {
117         struct connman_service *service;
118
119         DBG("device %p carrier %d", device, carrier);
120
121         service = __connman_service_lookup_from_device(device);
122         if (service == NULL)
123                 return -EINVAL;
124
125         return connman_service_set_favorite(service, carrier);
126 }
127
128 int __connman_profile_add_network(struct connman_network *network)
129 {
130         struct connman_service *service;
131
132         DBG("network %p", network);
133
134         service = __connman_service_create_from_network(network);
135         if (service == NULL)
136                 return -EINVAL;
137
138         return 0;
139 }
140
141 int __connman_profile_remove_network(struct connman_network *network)
142 {
143         struct connman_service *service;
144
145         DBG("network %p", network);
146
147         service = __connman_service_lookup_from_network(network);
148         if (service == NULL)
149                 return -EINVAL;
150
151         connman_service_put(service);
152
153         return 0;
154 }
155
156 void __connman_profile_list(DBusMessageIter *iter)
157 {
158         const char *path = __connman_profile_active();
159
160         DBG("");
161
162         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
163 }
164
165 static DBusMessage *get_properties(DBusConnection *conn,
166                                         DBusMessage *msg, void *data)
167 {
168         const char *name = "Default";
169         DBusMessage *reply;
170         DBusMessageIter array, dict, entry;
171
172         DBG("conn %p", conn);
173
174         reply = dbus_message_new_method_return(msg);
175         if (reply == NULL)
176                 return NULL;
177
178         dbus_message_iter_init_append(reply, &array);
179
180         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
181                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
182                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
183                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
184
185         connman_dbus_dict_append_variant(&dict, "Name",
186                                                 DBUS_TYPE_STRING, &name);
187
188         dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
189                                                                 NULL, &entry);
190         append_services(&entry);
191         dbus_message_iter_close_container(&dict, &entry);
192
193         dbus_message_iter_close_container(&array, &dict);
194
195         return reply;
196 }
197
198 static GDBusMethodTable profile_methods[] = {
199         { "GetProperties", "", "a{sv}", get_properties },
200         { },
201 };
202
203 static GDBusSignalTable profile_signals[] = {
204         { "PropertyChanged", "sv" },
205         { },
206 };
207
208 int __connman_profile_init(DBusConnection *conn)
209 {
210         DBG("conn %p", conn);
211
212         connection = dbus_connection_ref(conn);
213         if (connection == NULL)
214                 return -1;
215
216         g_dbus_register_interface(connection, PROFILE_DEFAULT,
217                                         CONNMAN_PROFILE_INTERFACE,
218                                         profile_methods, profile_signals,
219                                                         NULL, NULL, NULL);
220
221         return 0;
222 }
223
224 void __connman_profile_cleanup(void)
225 {
226         DBG("conn %p", connection);
227
228         g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
229                                                 CONNMAN_PROFILE_INTERFACE);
230
231         if (connection == NULL)
232                 return;
233
234         dbus_connection_unref(connection);
235 }