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 void connman_service_put(struct connman_service *service)
62 DBG("service %p", service);
64 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
67 iter = g_hash_table_lookup(service_hash, service->identifier);
69 g_sequence_remove(iter);
71 service_free(service);
75 static void __connman_service_initialize(struct connman_service *service)
77 DBG("service %p", service);
79 service->refcount = 1;
81 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
82 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
83 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
84 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
86 service->favorite = FALSE;
90 * connman_service_create:
92 * Allocate a new service.
94 * Returns: a newly-allocated #connman_service structure
96 struct connman_service *connman_service_create(void)
98 struct connman_service *service;
100 service = g_try_new0(struct connman_service, 1);
104 DBG("service %p", service);
106 __connman_service_initialize(service);
112 * connman_service_ref:
113 * @service: service structure
115 * Increase reference counter of service
117 struct connman_service *connman_service_ref(struct connman_service *service)
119 g_atomic_int_inc(&service->refcount);
125 * connman_service_unref:
126 * @service: service structure
128 * Decrease reference counter of service
130 void connman_service_unref(struct connman_service *service)
132 connman_service_put(service);
135 static gint service_compare(gconstpointer a, gconstpointer b,
138 struct connman_service *service_a = (void *) a;
139 struct connman_service *service_b = (void *) b;
141 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
144 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
147 return (gint) service_b->strength - (gint) service_a->strength;
150 struct connman_service *connman_service_get(const char *identifier)
152 struct connman_service *service;
155 iter = g_hash_table_lookup(service_hash, identifier);
157 service = g_sequence_get(iter);
159 g_atomic_int_inc(&service->refcount);
163 service = g_try_new0(struct connman_service, 1);
167 DBG("service %p", service);
169 __connman_service_initialize(service);
171 service->identifier = g_strdup(identifier);
173 iter = g_sequence_insert_sorted(service_list, service,
174 service_compare, NULL);
176 g_hash_table_insert(service_hash, service->identifier, iter);
181 int __connman_service_init(void)
185 connection = connman_dbus_get_connection();
187 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
190 service_list = g_sequence_new(service_free);
195 void __connman_service_cleanup(void)
199 g_sequence_free(service_list);
202 g_hash_table_destroy(service_hash);
205 dbus_connection_unref(connection);