Add service creation and reference counting functions
[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 void connman_service_put(struct connman_service *service)
61 {
62         DBG("service %p", service);
63
64         if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
65                 GSequenceIter *iter;
66
67                 iter = g_hash_table_lookup(service_hash, service->identifier);
68                 if (iter != NULL)
69                         g_sequence_remove(iter);
70                 else
71                         service_free(service);
72         }
73 }
74
75 static void __connman_service_initialize(struct connman_service *service)
76 {
77         DBG("service %p", service);
78
79         service->refcount = 1;
80
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;
85
86         service->favorite = FALSE;
87 }
88
89 /**
90  * connman_service_create:
91  *
92  * Allocate a new service.
93  *
94  * Returns: a newly-allocated #connman_service structure
95  */
96 struct connman_service *connman_service_create(void)
97 {
98         struct connman_service *service;
99
100         service = g_try_new0(struct connman_service, 1);
101         if (service == NULL)
102                 return NULL;
103
104         DBG("service %p", service);
105
106         __connman_service_initialize(service);
107
108         return service;
109 }
110
111 /**
112  * connman_service_ref:
113  * @service: service structure
114  *
115  * Increase reference counter of service
116  */
117 struct connman_service *connman_service_ref(struct connman_service *service)
118 {
119         g_atomic_int_inc(&service->refcount);
120
121         return service;
122 }
123
124 /**
125  * connman_service_unref:
126  * @service: service structure
127  *
128  * Decrease reference counter of service
129  */
130 void connman_service_unref(struct connman_service *service)
131 {
132         connman_service_put(service);
133 }
134
135 static gint service_compare(gconstpointer a, gconstpointer b,
136                                                         gpointer user_data)
137 {
138         struct connman_service *service_a = (void *) a;
139         struct connman_service *service_b = (void *) b;
140
141         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
142                 return -1;
143
144         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
145                 return 1;
146
147         return (gint) service_b->strength - (gint) service_a->strength;
148 }
149
150 struct connman_service *connman_service_get(const char *identifier)
151 {
152         struct connman_service *service;
153         GSequenceIter *iter;
154
155         iter = g_hash_table_lookup(service_hash, identifier);
156         if (iter != NULL) {
157                 service = g_sequence_get(iter);
158                 if (service != NULL)
159                         g_atomic_int_inc(&service->refcount);
160                 return service;
161         }
162
163         service = g_try_new0(struct connman_service, 1);
164         if (service == NULL)
165                 return NULL;
166
167         DBG("service %p", service);
168
169         __connman_service_initialize(service);
170
171         service->identifier = g_strdup(identifier);
172
173         iter = g_sequence_insert_sorted(service_list, service,
174                                                 service_compare, NULL);
175
176         g_hash_table_insert(service_hash, service->identifier, iter);
177
178         return service;
179 }
180
181 int __connman_service_init(void)
182 {
183         DBG("");
184
185         connection = connman_dbus_get_connection();
186
187         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
188                                                                 NULL, NULL);
189
190         service_list = g_sequence_new(service_free);
191
192         return 0;
193 }
194
195 void __connman_service_cleanup(void)
196 {
197         DBG("");
198
199         g_sequence_free(service_list);
200         service_list = NULL;
201
202         g_hash_table_destroy(service_hash);
203         service_hash = NULL;
204
205         dbus_connection_unref(connection);
206 }