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
30 static DBusConnection *connection = NULL;
32 static GSequence *service_list = NULL;
33 static GHashTable *service_hash = NULL;
35 struct connman_service {
39 enum connman_service_type type;
40 enum connman_service_mode mode;
41 enum connman_service_security security;
42 enum connman_service_state state;
43 connman_uint8_t strength;
44 connman_bool_t favorite;
47 struct connman_device *device;
50 static void append_path(gpointer value, gpointer user_data)
52 struct connman_service *service = value;
53 DBusMessageIter *iter = user_data;
55 if (service->path == NULL)
58 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
62 void __connman_service_list(DBusMessageIter *iter)
66 g_sequence_foreach(service_list, append_path, iter);
69 static const char *type2string(enum connman_service_type type)
72 case CONNMAN_SERVICE_TYPE_UNKNOWN:
74 case CONNMAN_SERVICE_TYPE_ETHERNET:
76 case CONNMAN_SERVICE_TYPE_WIFI:
78 case CONNMAN_SERVICE_TYPE_WIMAX:
85 static const char *mode2string(enum connman_service_mode mode)
88 case CONNMAN_SERVICE_MODE_UNKNOWN:
90 case CONNMAN_SERVICE_MODE_MANAGED:
92 case CONNMAN_SERVICE_MODE_ADHOC:
99 static const char *security2string(enum connman_service_security security)
102 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
104 case CONNMAN_SERVICE_SECURITY_NONE:
106 case CONNMAN_SERVICE_SECURITY_WEP:
108 case CONNMAN_SERVICE_SECURITY_WPA:
110 case CONNMAN_SERVICE_SECURITY_WPA2:
117 static const char *state2string(enum connman_service_state state)
120 case CONNMAN_SERVICE_STATE_UNKNOWN:
122 case CONNMAN_SERVICE_STATE_IDLE:
124 case CONNMAN_SERVICE_STATE_ASSOCIATION:
125 return "association";
126 case CONNMAN_SERVICE_STATE_CONFIGURATION:
127 return "configuration";
128 case CONNMAN_SERVICE_STATE_READY:
130 case CONNMAN_SERVICE_STATE_DISCONNECT:
132 case CONNMAN_SERVICE_STATE_FAILURE:
139 static DBusMessage *get_properties(DBusConnection *conn,
140 DBusMessage *msg, void *data)
142 struct connman_service *service = data;
144 DBusMessageIter array, dict;
147 DBG("conn %p", conn);
149 reply = dbus_message_new_method_return(msg);
153 dbus_message_iter_init_append(reply, &array);
155 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
156 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
157 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
158 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
160 str = type2string(service->type);
162 connman_dbus_dict_append_variant(&dict, "Type",
163 DBUS_TYPE_STRING, &str);
165 str = mode2string(service->mode);
167 connman_dbus_dict_append_variant(&dict, "Mode",
168 DBUS_TYPE_STRING, &str);
170 str = security2string(service->security);
172 connman_dbus_dict_append_variant(&dict, "Security",
173 DBUS_TYPE_STRING, &str);
175 str = state2string(service->state);
177 connman_dbus_dict_append_variant(&dict, "State",
178 DBUS_TYPE_STRING, &str);
180 if (service->strength > 0)
181 connman_dbus_dict_append_variant(&dict, "Strength",
182 DBUS_TYPE_BYTE, &service->strength);
184 connman_dbus_dict_append_variant(&dict, "Favorite",
185 DBUS_TYPE_BOOLEAN, &service->favorite);
187 if (service->name != NULL)
188 connman_dbus_dict_append_variant(&dict, "Name",
189 DBUS_TYPE_STRING, &service->name);
191 dbus_message_iter_close_container(&array, &dict);
196 static DBusMessage *connect_service(DBusConnection *conn,
197 DBusMessage *msg, void *data)
199 struct connman_service *service = data;
201 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
202 return __connman_error_not_supported(msg);
204 return __connman_error_not_implemented(msg);
207 static DBusMessage *disconnect_service(DBusConnection *conn,
208 DBusMessage *msg, void *data)
210 struct connman_service *service = data;
212 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
213 return __connman_error_not_supported(msg);
215 return __connman_error_not_implemented(msg);
218 static DBusMessage *remove_service(DBusConnection *conn,
219 DBusMessage *msg, void *data)
221 struct connman_service *service = data;
223 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
224 return __connman_error_not_supported(msg);
226 service->favorite = FALSE;
228 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
231 static DBusMessage *move_before(DBusConnection *conn,
232 DBusMessage *msg, void *data)
234 struct connman_service *service = data;
236 if (service->favorite == FALSE)
237 return __connman_error_not_supported(msg);
239 return __connman_error_not_implemented(msg);
242 static DBusMessage *move_after(DBusConnection *conn,
243 DBusMessage *msg, void *data)
245 struct connman_service *service = data;
247 if (service->favorite == FALSE)
248 return __connman_error_not_supported(msg);
250 return __connman_error_not_implemented(msg);
253 static GDBusMethodTable service_methods[] = {
254 { "GetProperties", "", "a{sv}", get_properties },
255 { "Connect", "", "", connect_service },
256 { "Disconnect", "", "", disconnect_service },
257 { "Remove", "", "", remove_service },
258 { "MoveBefore", "o", "", move_before },
259 { "MoveAfter", "o", "", move_after },
263 static GDBusSignalTable service_signals[] = {
264 { "PropertyChanged", "sv" },
268 static void service_free(gpointer data)
270 struct connman_service *service = data;
271 char *path = service->path;
273 DBG("service %p", service);
275 g_hash_table_remove(service_hash, service->identifier);
277 service->path = NULL;
280 __connman_profile_changed();
282 g_dbus_unregister_interface(connection, path,
283 CONNMAN_SERVICE_INTERFACE);
287 g_free(service->name);
288 g_free(service->identifier);
293 * connman_service_put:
294 * @service: service structure
296 * Release service if no longer needed
298 void connman_service_put(struct connman_service *service)
300 DBG("service %p", service);
302 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
305 iter = g_hash_table_lookup(service_hash, service->identifier);
307 g_sequence_remove(iter);
309 service_free(service);
313 static void __connman_service_initialize(struct connman_service *service)
315 DBG("service %p", service);
317 service->refcount = 1;
319 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
320 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
321 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
322 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
324 service->favorite = FALSE;
330 * connman_service_create:
332 * Allocate a new service.
334 * Returns: a newly-allocated #connman_service structure
336 struct connman_service *connman_service_create(void)
338 struct connman_service *service;
340 service = g_try_new0(struct connman_service, 1);
344 DBG("service %p", service);
346 __connman_service_initialize(service);
352 * connman_service_ref:
353 * @service: service structure
355 * Increase reference counter of service
357 struct connman_service *connman_service_ref(struct connman_service *service)
359 g_atomic_int_inc(&service->refcount);
365 * connman_service_unref:
366 * @service: service structure
368 * Decrease reference counter of service
370 void connman_service_unref(struct connman_service *service)
372 connman_service_put(service);
375 static gint service_compare(gconstpointer a, gconstpointer b,
378 struct connman_service *service_a = (void *) a;
379 struct connman_service *service_b = (void *) b;
381 if (service_a->order > service_b->order)
384 if (service_a->order < service_b->order)
387 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
390 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
393 return (gint) service_b->strength - (gint) service_a->strength;
397 * connman_service_set_favorite:
398 * @service: service structure
399 * @favorite: favorite value
401 * Change the favorite setting of service
403 int connman_service_set_favorite(struct connman_service *service,
404 connman_bool_t favorite)
408 iter = g_hash_table_lookup(service_hash, service->identifier);
412 if (service->favorite)
415 service->favorite = favorite;
417 g_sequence_sort_changed(iter, service_compare, NULL);
419 __connman_profile_changed();
425 * connman_service_lookup:
426 * @identifier: service identifier
428 * Look up a service by identifier (reference count will not be increased)
430 struct connman_service *connman_service_lookup(const char *identifier)
434 iter = g_hash_table_lookup(service_hash, identifier);
436 return g_sequence_get(iter);
442 * connman_service_get:
443 * @identifier: service identifier
445 * Look up a service by identifier or create a new one if not found
447 struct connman_service *connman_service_get(const char *identifier)
449 struct connman_service *service;
452 iter = g_hash_table_lookup(service_hash, identifier);
454 service = g_sequence_get(iter);
456 g_atomic_int_inc(&service->refcount);
460 service = g_try_new0(struct connman_service, 1);
464 DBG("service %p", service);
466 __connman_service_initialize(service);
468 service->identifier = g_strdup(identifier);
470 iter = g_sequence_insert_sorted(service_list, service,
471 service_compare, NULL);
473 g_hash_table_insert(service_hash, service->identifier, iter);
478 static int service_register(struct connman_service *service)
480 const char *path = __connman_profile_active();
482 if (service->path != NULL)
485 service->path = g_strdup_printf("%s/%s", path, service->identifier);
487 g_dbus_register_interface(connection, service->path,
488 CONNMAN_SERVICE_INTERFACE,
489 service_methods, service_signals,
490 NULL, service, NULL);
492 __connman_profile_changed();
498 * connman_service_lookup_from_device:
499 * @device: device structure
501 * Look up a service by device (reference count will not be increased)
503 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
505 struct connman_service *service;
508 name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
509 connman_device_get_index(device));
511 service = connman_service_lookup(name);
518 static enum connman_service_type convert_device_type(struct connman_device *device)
520 enum connman_device_type type = connman_device_get_type(device);
523 case CONNMAN_DEVICE_TYPE_UNKNOWN:
524 case CONNMAN_DEVICE_TYPE_VENDOR:
525 case CONNMAN_DEVICE_TYPE_WIFI:
526 case CONNMAN_DEVICE_TYPE_WIMAX:
527 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
528 case CONNMAN_DEVICE_TYPE_GPS:
529 case CONNMAN_DEVICE_TYPE_HSO:
530 case CONNMAN_DEVICE_TYPE_NOZOMI:
531 case CONNMAN_DEVICE_TYPE_HUAWEI:
532 case CONNMAN_DEVICE_TYPE_NOVATEL:
534 case CONNMAN_DEVICE_TYPE_ETHERNET:
535 return CONNMAN_SERVICE_TYPE_ETHERNET;
538 return CONNMAN_SERVICE_TYPE_UNKNOWN;
542 * connman_service_create_from_device:
543 * @device: device structure
545 * Look up service by device and if not found, create one
547 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
549 struct connman_service *service;
552 name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
553 connman_device_get_index(device));
555 service = connman_service_get(name);
559 if (service->path != NULL) {
560 connman_service_put(service);
565 service->type = convert_device_type(device);
567 service->device = device;
569 service_register(service);
578 * connman_service_lookup_from_network:
579 * @device: device structure
581 * Look up a service by network (reference count will not be increased)
583 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
585 struct connman_service *service;
588 name = g_strdup_printf("%s_%s", __connman_network_get_type(network),
589 __connman_network_get_group(network));
591 service = connman_service_lookup(name);
598 static enum connman_service_type convert_network_type(struct connman_network *network)
600 enum connman_network_type type = connman_network_get_type(network);
603 case CONNMAN_NETWORK_TYPE_UNKNOWN:
604 case CONNMAN_NETWORK_TYPE_VENDOR:
605 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
606 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
607 case CONNMAN_NETWORK_TYPE_HSO:
609 case CONNMAN_NETWORK_TYPE_WIFI:
610 return CONNMAN_SERVICE_TYPE_WIFI;
611 case CONNMAN_NETWORK_TYPE_WIMAX:
612 return CONNMAN_SERVICE_TYPE_WIMAX;
615 return CONNMAN_SERVICE_TYPE_UNKNOWN;
619 * connman_service_create_from_network:
620 * @device: device structure
622 * Look up service by network and if not found, create one
624 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
626 struct connman_service *service;
629 name = g_strdup_printf("%s_%s", __connman_network_get_type(network),
630 __connman_network_get_group(network));
632 service = connman_service_get(name);
636 if (service->path != NULL) {
637 connman_service_put(service);
642 service->type = convert_network_type(network);
644 service_register(service);
652 int __connman_service_init(void)
656 connection = connman_dbus_get_connection();
658 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
661 service_list = g_sequence_new(service_free);
666 void __connman_service_cleanup(void)
670 g_sequence_free(service_list);
673 g_hash_table_destroy(service_hash);
676 dbus_connection_unref(connection);