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;
36 struct connman_bearer {
41 struct connman_session {
45 struct connman_bearer *bearer;
46 struct connman_service *service;
49 static enum connman_service_type bearer2service(const char *bearer)
52 return CONNMAN_SERVICE_TYPE_UNKNOWN;
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;
67 return CONNMAN_SERVICE_TYPE_UNKNOWN;
70 static char *service2bearer(enum connman_service_type type)
75 case CONNMAN_SERVICE_TYPE_ETHERNET:
77 case CONNMAN_SERVICE_TYPE_WIFI:
79 case CONNMAN_SERVICE_TYPE_WIMAX:
81 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
83 case CONNMAN_SERVICE_TYPE_CELLULAR:
85 case CONNMAN_SERVICE_TYPE_UNKNOWN:
86 case CONNMAN_SERVICE_TYPE_SYSTEM:
87 case CONNMAN_SERVICE_TYPE_GPS:
88 case CONNMAN_SERVICE_TYPE_VPN:
95 static void remove_bearer(gpointer user_data)
97 struct connman_bearer *bearer = user_data;
103 static void remove_session(gpointer user_data)
105 struct connman_session *session = user_data;
107 if (session->watch > 0)
108 g_dbus_remove_watch(connection, session->watch);
110 session->bearer = NULL;
111 if (session->service)
112 connman_service_unref(session->service);
113 g_free(session->owner);
117 static int session_disconnect(struct connman_session *session)
119 struct connman_bearer *bearer = session->bearer;
121 DBG("%s", session->owner);
127 * Once a bearer is no longer referenced we actually disconnect
128 * the corresponding service.
130 if (bearer == NULL || g_atomic_int_dec_and_test(&bearer->refcount)) {
131 struct connman_network *network;
132 struct connman_device *device;
135 * We toggle the reconnect flag to false when releasing a
136 * session. This way a previously connected service will
137 * not autoconnect once we've completely release a session.
139 network = __connman_service_get_network(session->service);
143 device = connman_network_get_device(network);
147 __connman_device_set_reconnect(device, FALSE);
149 __connman_service_disconnect(session->service);
150 connman_service_unref(session->service);
152 g_hash_table_remove(bearer_hash, bearer);
155 g_hash_table_remove(session_hash, session);
160 static void owner_disconnect(DBusConnection *connection, void *user_data)
162 struct connman_session *session;
163 char *owner = user_data;
165 DBG("%s died", owner);
167 session = g_hash_table_lookup(session_hash, owner);
168 if (session == NULL) {
169 connman_error("No session");
173 session_disconnect(session);
176 int __connman_session_release(const char *owner)
178 struct connman_session *session;
180 DBG("owner %s", owner);
182 session = g_hash_table_lookup(session_hash, owner);
186 if (g_atomic_int_dec_and_test(&session->refcount))
187 return session_disconnect(session);
192 struct connman_service *__connman_session_request(const char *bearer_name,
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;
201 if (bearer_name == NULL)
204 DBG("owner %s bearer %s", owner, bearer_name);
206 bearer_name_len = strlen(bearer_name);
208 session = g_hash_table_lookup(session_hash, owner);
210 /* we only support one bearer per process */
211 if (bearer_name_len &&
212 g_strcmp0(session->bearer->name, bearer_name))
215 g_atomic_int_inc(&session->refcount);
217 return session->service;
220 session = g_try_new0(struct connman_session, 1);
224 session->refcount = 1;
225 session->owner = g_strdup(owner);
226 session->service = NULL;
227 g_hash_table_replace(session_hash, session->owner, session);
229 /* Find and connect service */
230 service_type = bearer2service(bearer_name);
232 session->service = __connman_service_connect_type(service_type);
233 if (session->service == NULL)
236 connman_service_ref(session->service);
238 service_type = connman_service_get_type(session->service);
240 /* We might get a different bearer from the one we requested */
241 bearer_name_new = service2bearer(service_type);
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);
250 bearer->refcount = 0;
251 bearer->name = g_strdup(bearer_name_new);
252 g_hash_table_replace(bearer_hash, bearer->name, bearer);
255 g_atomic_int_inc(&bearer->refcount);
256 session->bearer = bearer;
258 session->watch = g_dbus_add_disconnect_watch(connection, session->owner,
259 owner_disconnect, session->owner, NULL);
260 return session->service;
263 session_disconnect(session);
266 g_hash_table_remove(session_hash, session);
271 int __connman_session_init(void)
275 connection = connman_dbus_get_connection();
276 if (connection == NULL)
279 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
280 NULL, remove_session);
282 bearer_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
283 NULL, remove_bearer);
288 void __connman_session_cleanup(void)
292 if (connection == NULL)
295 g_hash_table_destroy(bearer_hash);
296 g_hash_table_destroy(session_hash);
297 dbus_connection_unref(connection);