5 * Copyright (C) 2007-2009 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
28 static DBusConnection *connection = NULL;
30 static GSequence *service_list = NULL;
31 static GHashTable *service_hash = NULL;
33 struct connman_service {
37 enum connman_service_type type;
38 enum connman_service_mode mode;
39 enum connman_service_security security;
40 enum connman_service_state state;
41 connman_uint8_t strength;
42 connman_bool_t favorite;
46 static void service_free(gpointer data)
48 struct connman_service *service = data;
50 DBG("service %p", service);
52 g_hash_table_remove(service_hash, service->identifier);
54 g_free(service->name);
55 g_free(service->path);
56 g_free(service->identifier);
60 static gint service_compare(gconstpointer a, gconstpointer b,
63 struct connman_service *service_a = (void *) a;
64 struct connman_service *service_b = (void *) b;
66 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
69 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
72 return (gint) service_b->strength - (gint) service_a->strength;
75 struct connman_service *connman_service_get(const char *identifier)
77 struct connman_service *service;
80 iter = g_hash_table_lookup(service_hash, identifier);
82 service = g_sequence_get(iter);
84 g_atomic_int_inc(&service->refcount);
88 service = g_try_new0(struct connman_service, 1);
92 DBG("service %p", service);
94 service->refcount = 1;
95 service->identifier = g_strdup(identifier);
97 iter = g_sequence_insert_sorted(service_list, service,
98 service_compare, NULL);
100 g_hash_table_insert(service_hash, service->identifier, iter);
105 void connman_service_put(struct connman_service *service)
107 DBG("service %p", service);
109 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
112 iter = g_hash_table_lookup(service_hash, service->identifier);
114 g_sequence_remove(iter);
116 service_free(service);
120 int __connman_service_init(void)
124 connection = connman_dbus_get_connection();
126 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
129 service_list = g_sequence_new(service_free);
134 void __connman_service_cleanup(void)
138 g_sequence_free(service_list);
141 g_hash_table_destroy(service_hash);
144 dbus_connection_unref(connection);