5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
32 static DBusConnection *connection;
33 static GHashTable *session_hash;
34 static GHashTable *bearer_hash;
35 static connman_bool_t sessionmode;
37 struct connman_bearer {
42 struct connman_session {
46 struct connman_bearer *bearer;
47 struct connman_service *service;
50 static enum connman_service_type bearer2service(const char *bearer)
53 return CONNMAN_SERVICE_TYPE_UNKNOWN;
57 if (g_strcmp0(bearer, "ethernet") == 0)
58 return CONNMAN_SERVICE_TYPE_ETHERNET;
59 else if (g_strcmp0(bearer, "wifi") == 0)
60 return CONNMAN_SERVICE_TYPE_WIFI;
61 else if (g_strcmp0(bearer, "wimax") == 0)
62 return CONNMAN_SERVICE_TYPE_WIMAX;
63 else if (g_strcmp0(bearer, "bluetooth") == 0)
64 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
65 else if (g_strcmp0(bearer, "3g") == 0)
66 return CONNMAN_SERVICE_TYPE_CELLULAR;
68 return CONNMAN_SERVICE_TYPE_UNKNOWN;
71 static char *service2bearer(enum connman_service_type type)
76 case CONNMAN_SERVICE_TYPE_ETHERNET:
78 case CONNMAN_SERVICE_TYPE_WIFI:
80 case CONNMAN_SERVICE_TYPE_WIMAX:
82 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
84 case CONNMAN_SERVICE_TYPE_CELLULAR:
86 case CONNMAN_SERVICE_TYPE_UNKNOWN:
87 case CONNMAN_SERVICE_TYPE_SYSTEM:
88 case CONNMAN_SERVICE_TYPE_GPS:
89 case CONNMAN_SERVICE_TYPE_VPN:
90 case CONNMAN_SERVICE_TYPE_GADGET:
97 static void remove_bearer(gpointer user_data)
99 struct connman_bearer *bearer = user_data;
101 g_free(bearer->name);
105 static void remove_session(gpointer user_data)
107 struct connman_session *session = user_data;
109 session->bearer = NULL;
110 if (session->service)
111 connman_service_unref(session->service);
112 g_free(session->owner);
116 static int session_disconnect(struct connman_session *session)
118 struct connman_bearer *bearer = session->bearer;
120 DBG("%s", session->owner);
126 * Once a bearer is no longer referenced we actually disconnect
127 * the corresponding service.
129 if (bearer == NULL || g_atomic_int_dec_and_test(&bearer->refcount)) {
130 struct connman_network *network;
131 struct connman_device *device;
134 * We toggle the reconnect flag to false when releasing a
135 * session. This way a previously connected service will
136 * not autoconnect once we've completely release a session.
138 network = __connman_service_get_network(session->service);
142 device = connman_network_get_device(network);
146 __connman_device_set_reconnect(device, FALSE);
148 __connman_service_disconnect(session->service);
149 connman_service_unref(session->service);
151 g_hash_table_remove(bearer_hash, bearer);
154 if (session->watch > 0)
155 g_dbus_remove_watch(connection, session->watch);
157 g_hash_table_remove(session_hash, session);
162 static void owner_disconnect(DBusConnection *connection, void *user_data)
164 struct connman_session *session = user_data;
166 DBG("%s died", session->owner);
168 session_disconnect(session);
171 int __connman_session_release(const char *owner)
173 struct connman_session *session;
175 DBG("owner %s", owner);
177 session = g_hash_table_lookup(session_hash, owner);
181 if (g_atomic_int_dec_and_test(&session->refcount))
182 return session_disconnect(session);
187 struct connman_service *__connman_session_request(const char *bearer_name,
190 struct connman_session *session;
191 struct connman_bearer *bearer;
192 enum connman_service_type service_type;
193 const char *bearer_name_new;
194 size_t bearer_name_len;
196 if (bearer_name == NULL)
199 DBG("owner %s bearer %s", owner, bearer_name);
201 bearer_name_len = strlen(bearer_name);
203 session = g_hash_table_lookup(session_hash, owner);
205 /* we only support one bearer per process */
206 if (bearer_name_len &&
207 g_strcmp0(session->bearer->name, bearer_name))
210 g_atomic_int_inc(&session->refcount);
212 return session->service;
215 session = g_try_new0(struct connman_session, 1);
219 session->refcount = 1;
220 session->owner = g_strdup(owner);
221 session->service = NULL;
222 g_hash_table_replace(session_hash, session->owner, session);
224 /* Find and connect service */
225 service_type = bearer2service(bearer_name);
227 session->service = __connman_service_connect_type(service_type);
228 if (session->service == NULL)
231 connman_service_ref(session->service);
233 service_type = connman_service_get_type(session->service);
235 /* We might get a different bearer from the one we requested */
236 bearer_name_new = service2bearer(service_type);
238 /* Refcount the exisiting bearer, or create one */
239 bearer = g_hash_table_lookup(bearer_hash, bearer_name_new);
240 if (bearer == NULL) {
241 bearer = g_try_new0(struct connman_bearer, 1);
245 bearer->refcount = 0;
246 bearer->name = g_strdup(bearer_name_new);
247 g_hash_table_replace(bearer_hash, bearer->name, bearer);
250 g_atomic_int_inc(&bearer->refcount);
251 session->bearer = bearer;
253 session->watch = g_dbus_add_disconnect_watch(connection, session->owner,
254 owner_disconnect, session, NULL);
255 return session->service;
258 session_disconnect(session);
261 g_hash_table_remove(session_hash, session);
266 connman_bool_t __connman_session_mode()
271 void __connman_session_set_mode(connman_bool_t enable)
273 DBG("enable %d", enable);
275 if (sessionmode == enable)
278 sessionmode = enable;
280 if (sessionmode == TRUE)
281 __connman_service_disconnect_all();
284 int __connman_session_init(void)
288 connection = connman_dbus_get_connection();
289 if (connection == NULL)
292 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
293 NULL, remove_session);
295 bearer_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
296 NULL, remove_bearer);
302 void __connman_session_cleanup(void)
306 if (connection == NULL)
309 g_hash_table_destroy(bearer_hash);
310 g_hash_table_destroy(session_hash);
311 dbus_connection_unref(connection);