9fed4515ea865fb922fb67e7354bd6a2772fc354
[connman] / src / service.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 "connman.h"
27
28 static DBusConnection *connection = NULL;
29
30 static GSequence *service_list = NULL;
31 static GHashTable *service_hash = NULL;
32
33 struct connman_service {
34         gint refcount;
35         char *identifier;
36         char *path;
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;
43         char *name;
44 };
45
46 static void service_free(gpointer data)
47 {
48         struct connman_service *service = data;
49
50         DBG("service %p", service);
51
52         g_hash_table_remove(service_hash, service->identifier);
53
54         g_free(service->name);
55         g_free(service->path);
56         g_free(service->identifier);
57         g_free(service);
58 }
59
60 static gint service_compare(gconstpointer a, gconstpointer b,
61                                                         gpointer user_data)
62 {
63         struct connman_service *service_a = (void *) a;
64         struct connman_service *service_b = (void *) b;
65
66         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
67                 return -1;
68
69         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
70                 return 1;
71
72         return (gint) service_b->strength - (gint) service_a->strength;
73 }
74
75 struct connman_service *connman_service_get(const char *identifier)
76 {
77         struct connman_service *service;
78         GSequenceIter *iter;
79
80         iter = g_hash_table_lookup(service_hash, identifier);
81         if (iter != NULL) {
82                 service = g_sequence_get(iter);
83                 if (service != NULL)
84                         g_atomic_int_inc(&service->refcount);
85                 return service;
86         }
87
88         service = g_try_new0(struct connman_service, 1);
89         if (service == NULL)
90                 return NULL;
91
92         DBG("service %p", service);
93
94         service->refcount = 1;
95         service->identifier = g_strdup(identifier);
96
97         iter = g_sequence_insert_sorted(service_list, service,
98                                                 service_compare, NULL);
99
100         g_hash_table_insert(service_hash, service->identifier, iter);
101
102         return service;
103 }
104
105 void connman_service_put(struct connman_service *service)
106 {
107         DBG("service %p", service);
108
109         if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
110                 GSequenceIter *iter;
111
112                 iter = g_hash_table_lookup(service_hash, service->identifier);
113                 if (iter != NULL)
114                         g_sequence_remove(iter);
115                 else
116                         service_free(service);
117         }
118 }
119
120 int __connman_service_init(void)
121 {
122         DBG("");
123
124         connection = connman_dbus_get_connection();
125
126         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
127                                                                 NULL, NULL);
128
129         service_list = g_sequence_new(service_free);
130
131         return 0;
132 }
133
134 void __connman_service_cleanup(void)
135 {
136         DBG("");
137
138         g_sequence_free(service_list);
139         service_list = NULL;
140
141         g_hash_table_destroy(service_hash);
142         service_hash = NULL;
143
144         dbus_connection_unref(connection);
145 }