Release counter and session watches whe unregistering
[framework/connectivity/connman.git] / src / session.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 <string.h>
27
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 static DBusConnection *connection;
33 static GHashTable *session_hash;
34 static GHashTable *bearer_hash;
35
36 struct connman_bearer {
37         gint refcount;
38         char *name;
39 };
40
41 struct connman_session {
42         gint refcount;
43         char *owner;
44         guint watch;
45         struct connman_bearer *bearer;
46         struct connman_service *service;
47 };
48
49 static enum connman_service_type bearer2service(const char *bearer)
50 {
51         if (bearer == NULL)
52                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
53
54         DBG("%s", bearer);
55
56         if (g_strcmp0(bearer, "ethernet") == 0)
57                 return CONNMAN_SERVICE_TYPE_ETHERNET;
58         else if (g_strcmp0(bearer, "wifi") == 0)
59                 return CONNMAN_SERVICE_TYPE_WIFI;
60         else if (g_strcmp0(bearer, "wimax") == 0)
61                 return CONNMAN_SERVICE_TYPE_WIMAX;
62         else if (g_strcmp0(bearer, "bluetooth") == 0)
63                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
64         else if (g_strcmp0(bearer, "3g") == 0)
65                 return CONNMAN_SERVICE_TYPE_CELLULAR;
66         else
67                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
68 }
69
70 static char *service2bearer(enum connman_service_type type)
71 {
72         DBG("%d", type);
73
74         switch (type) {
75         case CONNMAN_SERVICE_TYPE_ETHERNET:
76                 return "ethernet";
77         case CONNMAN_SERVICE_TYPE_WIFI:
78                 return "wifi";
79         case CONNMAN_SERVICE_TYPE_WIMAX:
80                 return "wimax";
81         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
82                 return "bluetooth";
83         case CONNMAN_SERVICE_TYPE_CELLULAR:
84                 return "3g";
85         case CONNMAN_SERVICE_TYPE_UNKNOWN:
86         case CONNMAN_SERVICE_TYPE_SYSTEM:
87         case CONNMAN_SERVICE_TYPE_GPS:
88         case CONNMAN_SERVICE_TYPE_VPN:
89                 return NULL;
90         }
91
92         return NULL;
93 }
94
95 static void remove_bearer(gpointer user_data)
96 {
97         struct connman_bearer *bearer = user_data;
98
99         g_free(bearer->name);
100         g_free(bearer);
101 }
102
103 static void remove_session(gpointer user_data)
104 {
105         struct connman_session *session = user_data;
106
107         session->bearer = NULL;
108         if (session->service)
109                 connman_service_unref(session->service);
110         g_free(session->owner);
111         g_free(session);
112 }
113
114 static int session_disconnect(struct connman_session *session)
115 {
116         struct connman_bearer *bearer = session->bearer;
117
118         DBG("%s", session->owner);
119
120         if (session == NULL)
121                 return -EINVAL;
122
123         /*
124          * Once a bearer is no longer referenced we actually disconnect
125          * the corresponding service.
126          */
127         if (bearer == NULL || g_atomic_int_dec_and_test(&bearer->refcount)) {
128                 struct connman_network *network;
129                 struct connman_device *device;
130
131                 /*
132                  * We toggle the reconnect flag to false when releasing a
133                  * session. This way a previously connected service will
134                  * not autoconnect once we've completely release a session.
135                  */
136                 network = __connman_service_get_network(session->service);
137                 if (network == NULL)
138                         return -EINVAL;
139
140                 device = connman_network_get_device(network);
141                 if (device == NULL)
142                         return -EINVAL;
143
144                 __connman_device_set_reconnect(device, FALSE);
145
146                 __connman_service_disconnect(session->service);
147                 connman_service_unref(session->service);
148
149                 g_hash_table_remove(bearer_hash, bearer);
150         }
151
152         if (session->watch > 0)
153                 g_dbus_remove_watch(connection, session->watch);
154
155         g_hash_table_remove(session_hash, session);
156
157         return 0;
158 }
159
160 static void owner_disconnect(DBusConnection *connection, void *user_data)
161 {
162         struct connman_session *session;
163         char *owner = user_data;
164
165         DBG("%s died", owner);
166
167         session = g_hash_table_lookup(session_hash, owner);
168         if (session == NULL) {
169                 connman_error("No session");
170                 return;
171         }
172
173         session_disconnect(session);
174 }
175
176 int __connman_session_release(const char *owner)
177 {
178         struct connman_session *session;
179
180         DBG("owner %s", owner);
181
182         session = g_hash_table_lookup(session_hash, owner);
183         if (session == NULL)
184                 return -EINVAL;
185
186         if (g_atomic_int_dec_and_test(&session->refcount))
187                 return session_disconnect(session);
188
189         return 0;
190 }
191
192 struct connman_service *__connman_session_request(const char *bearer_name,
193                                                         const char *owner)
194 {
195         struct connman_session *session;
196         struct connman_bearer *bearer;
197         enum connman_service_type service_type;
198         const char *bearer_name_new;
199         size_t bearer_name_len;
200
201         if (bearer_name == NULL)
202                 return NULL;
203
204         DBG("owner %s bearer %s", owner, bearer_name);
205
206         bearer_name_len = strlen(bearer_name);
207
208         session = g_hash_table_lookup(session_hash, owner);
209         if (session) {
210                 /* we only support one bearer per process */
211                 if (bearer_name_len &&
212                         g_strcmp0(session->bearer->name, bearer_name))
213                                 return NULL;
214
215                 g_atomic_int_inc(&session->refcount);
216
217                 return session->service;
218         }
219
220         session = g_try_new0(struct connman_session, 1);
221         if (session == NULL)
222                 return NULL;
223
224         session->refcount = 1;
225         session->owner = g_strdup(owner);
226         session->service = NULL;
227         g_hash_table_replace(session_hash, session->owner, session);
228
229         /* Find and connect service */
230         service_type = bearer2service(bearer_name);
231
232         session->service = __connman_service_connect_type(service_type);
233         if (session->service == NULL)
234                 goto failed_connect;
235
236         connman_service_ref(session->service);
237
238         service_type = connman_service_get_type(session->service);
239
240         /* We might get a different bearer from the one we requested */
241         bearer_name_new = service2bearer(service_type);
242
243         /* Refcount the exisiting bearer, or create one */
244         bearer = g_hash_table_lookup(bearer_hash, bearer_name_new);
245         if (bearer == NULL) {
246                 bearer = g_try_new0(struct connman_bearer, 1);
247                 if (bearer == NULL)
248                         goto failed_bearer;
249
250                 bearer->refcount = 0;
251                 bearer->name = g_strdup(bearer_name_new);
252                 g_hash_table_replace(bearer_hash, bearer->name, bearer);
253         }
254
255         g_atomic_int_inc(&bearer->refcount);
256         session->bearer = bearer;
257
258         session->watch = g_dbus_add_disconnect_watch(connection, session->owner,
259                                         owner_disconnect, session->owner, NULL);
260         return session->service;
261
262 failed_bearer:
263         session_disconnect(session);
264
265 failed_connect:
266         g_hash_table_remove(session_hash, session);
267
268         return NULL;
269 }
270
271 int __connman_session_init(void)
272 {
273         DBG("");
274
275         connection = connman_dbus_get_connection();
276         if (connection == NULL)
277                 return -1;
278
279         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
280                                                 NULL, remove_session);
281
282         bearer_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
283                                                 NULL, remove_bearer);
284
285         return 0;
286 }
287
288 void __connman_session_cleanup(void)
289 {
290         DBG("");
291
292         if (connection == NULL)
293                 return;
294
295         g_hash_table_destroy(bearer_hash);
296         g_hash_table_destroy(session_hash);
297         dbus_connection_unref(connection);
298 }