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 enum connman_service_error error;
44 connman_uint8_t strength;
45 connman_bool_t favorite;
46 connman_bool_t hidden;
52 struct connman_ipconfig *ipconfig;
53 struct connman_device *device;
54 struct connman_network *network;
59 static void append_path(gpointer value, gpointer user_data)
61 struct connman_service *service = value;
62 DBusMessageIter *iter = user_data;
64 if (service->path == NULL)
67 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
71 void __connman_service_list(DBusMessageIter *iter)
75 g_sequence_foreach(service_list, append_path, iter);
80 struct connman_service *service;
83 static void compare_path(gpointer value, gpointer user_data)
85 struct connman_service *service = value;
86 struct find_data *data = user_data;
88 if (data->service != NULL)
91 if (g_strcmp0(service->path, data->path) == 0)
92 data->service = service;
95 static struct connman_service *find_service(const char *path)
97 struct find_data data = { .path = path, .service = NULL };
101 g_sequence_foreach(service_list, compare_path, &data);
106 static const char *type2string(enum connman_service_type type)
109 case CONNMAN_SERVICE_TYPE_UNKNOWN:
111 case CONNMAN_SERVICE_TYPE_ETHERNET:
113 case CONNMAN_SERVICE_TYPE_WIFI:
115 case CONNMAN_SERVICE_TYPE_WIMAX:
117 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
119 case CONNMAN_SERVICE_TYPE_CELLULAR:
126 static const char *mode2string(enum connman_service_mode mode)
129 case CONNMAN_SERVICE_MODE_UNKNOWN:
131 case CONNMAN_SERVICE_MODE_MANAGED:
133 case CONNMAN_SERVICE_MODE_ADHOC:
140 static const char *security2string(enum connman_service_security security)
143 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
145 case CONNMAN_SERVICE_SECURITY_NONE:
147 case CONNMAN_SERVICE_SECURITY_WEP:
149 case CONNMAN_SERVICE_SECURITY_WPA:
151 case CONNMAN_SERVICE_SECURITY_RSN:
158 static const char *state2string(enum connman_service_state state)
161 case CONNMAN_SERVICE_STATE_UNKNOWN:
163 case CONNMAN_SERVICE_STATE_IDLE:
165 case CONNMAN_SERVICE_STATE_CARRIER:
167 case CONNMAN_SERVICE_STATE_ASSOCIATION:
168 return "association";
169 case CONNMAN_SERVICE_STATE_CONFIGURATION:
170 return "configuration";
171 case CONNMAN_SERVICE_STATE_READY:
173 case CONNMAN_SERVICE_STATE_DISCONNECT:
175 case CONNMAN_SERVICE_STATE_FAILURE:
182 static const char *error2string(enum connman_service_error error)
185 case CONNMAN_SERVICE_ERROR_UNKNOWN:
187 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
188 return "dhcp-failed";
189 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
190 return "pin-missing";
196 static enum connman_service_error string2error(const char *error)
198 if (g_strcmp0(error, "dhcp-failed") == 0)
199 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
200 else if (g_strcmp0(error, "pin-missing") == 0)
201 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
203 return CONNMAN_SERVICE_ERROR_UNKNOWN;
206 static void state_changed(struct connman_service *service)
209 DBusMessageIter entry, value;
210 const char *str, *key = "State";
212 if (service->path == NULL)
215 str = state2string(service->state);
219 signal = dbus_message_new_signal(service->path,
220 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
224 dbus_message_iter_init_append(signal, &entry);
226 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
228 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
229 DBUS_TYPE_STRING_AS_STRING, &value);
230 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
231 dbus_message_iter_close_container(&entry, &value);
233 g_dbus_send_message(connection, signal);
236 static void strength_changed(struct connman_service *service)
239 DBusMessageIter entry, value;
240 const char *key = "Strength";
242 if (service->path == NULL)
245 if (service->strength == 0)
248 signal = dbus_message_new_signal(service->path,
249 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
253 dbus_message_iter_init_append(signal, &entry);
255 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
257 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
258 DBUS_TYPE_BYTE_AS_STRING, &value);
259 dbus_message_iter_append_basic(&value, DBUS_TYPE_BYTE,
261 dbus_message_iter_close_container(&entry, &value);
263 g_dbus_send_message(connection, signal);
266 static DBusMessage *get_properties(DBusConnection *conn,
267 DBusMessage *msg, void *user_data)
269 struct connman_service *service = user_data;
271 DBusMessageIter array, dict;
274 DBG("service %p", service);
276 reply = dbus_message_new_method_return(msg);
280 dbus_message_iter_init_append(reply, &array);
282 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
283 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
284 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
285 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
287 str = type2string(service->type);
289 connman_dbus_dict_append_variant(&dict, "Type",
290 DBUS_TYPE_STRING, &str);
292 str = mode2string(service->mode);
294 connman_dbus_dict_append_variant(&dict, "Mode",
295 DBUS_TYPE_STRING, &str);
297 str = security2string(service->security);
299 connman_dbus_dict_append_variant(&dict, "Security",
300 DBUS_TYPE_STRING, &str);
302 str = state2string(service->state);
304 connman_dbus_dict_append_variant(&dict, "State",
305 DBUS_TYPE_STRING, &str);
307 str = error2string(service->error);
309 connman_dbus_dict_append_variant(&dict, "Error",
310 DBUS_TYPE_STRING, &str);
312 if (service->strength > 0)
313 connman_dbus_dict_append_variant(&dict, "Strength",
314 DBUS_TYPE_BYTE, &service->strength);
316 connman_dbus_dict_append_variant(&dict, "Favorite",
317 DBUS_TYPE_BOOLEAN, &service->favorite);
319 if (service->name != NULL)
320 connman_dbus_dict_append_variant(&dict, "Name",
321 DBUS_TYPE_STRING, &service->name);
323 if (service->passphrase != NULL &&
324 __connman_security_check_privilege(msg,
325 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
326 connman_dbus_dict_append_variant(&dict, "Passphrase",
327 DBUS_TYPE_STRING, &service->passphrase);
329 __connman_ipconfig_append_ipv4(service->ipconfig, &dict, "IPv4.");
331 dbus_message_iter_close_container(&array, &dict);
336 static DBusMessage *set_property(DBusConnection *conn,
337 DBusMessage *msg, void *user_data)
339 struct connman_service *service = user_data;
340 DBusMessageIter iter, value;
344 DBG("service %p", service);
346 if (dbus_message_iter_init(msg, &iter) == FALSE)
347 return __connman_error_invalid_arguments(msg);
349 dbus_message_iter_get_basic(&iter, &name);
350 dbus_message_iter_next(&iter);
351 dbus_message_iter_recurse(&iter, &value);
353 if (__connman_security_check_privilege(msg,
354 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
355 return __connman_error_permission_denied(msg);
357 type = dbus_message_iter_get_arg_type(&value);
359 if (g_str_equal(name, "Passphrase") == TRUE) {
360 const char *passphrase;
362 if (type != DBUS_TYPE_STRING)
363 return __connman_error_invalid_arguments(msg);
365 if (__connman_security_check_privilege(msg,
366 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
367 return __connman_error_permission_denied(msg);
369 dbus_message_iter_get_basic(&value, &passphrase);
371 g_free(service->passphrase);
372 service->passphrase = g_strdup(passphrase);
374 if (service->network != NULL)
375 connman_network_set_string(service->network,
376 "WiFi.Passphrase", service->passphrase);
378 __connman_storage_save_service(service);
379 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
382 err = __connman_ipconfig_set_ipv4(service->ipconfig,
385 return __connman_error_failed(msg, -err);
387 return __connman_error_invalid_property(msg);
389 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
392 static DBusMessage *clear_property(DBusConnection *conn,
393 DBusMessage *msg, void *user_data)
395 struct connman_service *service = user_data;
398 DBG("service %p", service);
400 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
403 if (__connman_security_check_privilege(msg,
404 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
405 return __connman_error_permission_denied(msg);
407 if (g_str_equal(name, "Error") == TRUE) {
408 service->state = CONNMAN_SERVICE_STATE_IDLE;
409 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
410 state_changed(service);
412 g_get_current_time(&service->modified);
413 __connman_storage_save_service(service);
415 return __connman_error_invalid_property(msg);
417 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
420 static gboolean connect_timeout(gpointer user_data)
422 struct connman_service *service = user_data;
424 DBG("service %p", service);
426 service->timeout = 0;
428 if (service->network != NULL) {
429 connman_bool_t connected;
431 connected = connman_network_get_connected(service->network);
432 if (connected == TRUE) {
433 __connman_service_indicate_state(service,
434 CONNMAN_SERVICE_STATE_READY);
438 __connman_network_disconnect(service->network);
439 } else if (service->device != NULL) {
440 connman_bool_t disconnected;
442 disconnected = connman_device_get_disconnected(service->device);
443 if (disconnected == FALSE) {
444 __connman_service_indicate_state(service,
445 CONNMAN_SERVICE_STATE_READY);
449 __connman_device_disconnect(service->device);
452 if (service->pending != NULL) {
455 reply = __connman_error_operation_timeout(service->pending);
457 g_dbus_send_message(connection, reply);
459 dbus_message_unref(service->pending);
460 service->pending = NULL;
463 __connman_service_indicate_state(service,
464 CONNMAN_SERVICE_STATE_FAILURE);
469 static DBusMessage *connect_service(DBusConnection *conn,
470 DBusMessage *msg, void *user_data)
472 struct connman_service *service = user_data;
475 DBG("service %p", service);
477 if (service->pending != NULL)
478 return __connman_error_in_progress(msg);
480 service->pending = dbus_message_ref(msg);
482 err = __connman_service_connect(service);
484 if (err != -EINPROGRESS) {
485 dbus_message_unref(service->pending);
486 service->pending = NULL;
488 return __connman_error_failed(msg, -err);
494 dbus_message_unref(service->pending);
495 service->pending = NULL;
497 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
500 static DBusMessage *disconnect_service(DBusConnection *conn,
501 DBusMessage *msg, void *user_data)
503 struct connman_service *service = user_data;
506 DBG("service %p", service);
508 if (service->pending != NULL) {
511 reply = __connman_error_operation_aborted(service->pending);
513 g_dbus_send_message(conn, reply);
515 dbus_message_unref(service->pending);
516 service->pending = NULL;
518 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
521 err = __connman_service_disconnect(service);
523 if (err != -EINPROGRESS)
524 return __connman_error_failed(msg, -err);
529 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
532 static DBusMessage *remove_service(DBusConnection *conn,
533 DBusMessage *msg, void *user_data)
535 struct connman_service *service = user_data;
537 DBG("service %p", service);
539 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
540 return __connman_error_not_supported(msg);
542 if (service->favorite == FALSE)
543 return __connman_error_not_supported(msg);
545 if (service->network != NULL)
546 __connman_network_disconnect(service->network);
548 g_free(service->passphrase);
549 service->passphrase = NULL;
551 connman_service_set_favorite(service, FALSE);
552 __connman_storage_save_service(service);
554 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
557 static DBusMessage *move_before(DBusConnection *conn,
558 DBusMessage *msg, void *user_data)
560 struct connman_service *service = user_data;
561 struct connman_service *target;
563 GSequenceIter *src, *dst;
565 DBG("service %p", service);
567 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
570 if (service->favorite == FALSE)
571 return __connman_error_not_supported(msg);
573 target = find_service(path);
574 if (target == NULL || target->favorite == FALSE || target == service)
575 return __connman_error_invalid_service(msg);
577 DBG("target %s", target->identifier);
579 g_get_current_time(&service->modified);
580 __connman_storage_save_service(service);
582 src = g_hash_table_lookup(service_hash, service->identifier);
583 dst = g_hash_table_lookup(service_hash, target->identifier);
586 g_sequence_move(src, dst);
588 __connman_profile_changed();
590 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
592 return __connman_error_not_implemented(msg);
595 static DBusMessage *move_after(DBusConnection *conn,
596 DBusMessage *msg, void *user_data)
598 struct connman_service *service = user_data;
599 struct connman_service *target;
602 DBG("service %p", service);
604 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
607 if (service->favorite == FALSE)
608 return __connman_error_not_supported(msg);
610 target = find_service(path);
611 if (target == NULL || target->favorite == FALSE || target == service)
612 return __connman_error_invalid_service(msg);
614 DBG("target %s", target->identifier);
616 g_get_current_time(&service->modified);
617 __connman_storage_save_service(service);
619 return __connman_error_not_implemented(msg);
622 static GDBusMethodTable service_methods[] = {
623 { "GetProperties", "", "a{sv}", get_properties },
624 { "SetProperty", "sv", "", set_property },
625 { "ClearProperty", "s", "", clear_property },
626 { "Connect", "", "", connect_service,
627 G_DBUS_METHOD_FLAG_ASYNC },
628 { "Disconnect", "", "", disconnect_service },
629 { "Remove", "", "", remove_service },
630 { "MoveBefore", "o", "", move_before },
631 { "MoveAfter", "o", "", move_after },
635 static GDBusSignalTable service_signals[] = {
636 { "PropertyChanged", "sv" },
640 static void service_free(gpointer user_data)
642 struct connman_service *service = user_data;
643 char *path = service->path;
645 DBG("service %p", service);
647 g_hash_table_remove(service_hash, service->identifier);
649 if (service->timeout > 0) {
650 g_source_remove(service->timeout);
651 service->timeout = 0;
654 if (service->pending != NULL) {
655 dbus_message_unref(service->pending);
656 service->pending = NULL;
659 service->path = NULL;
662 __connman_profile_changed();
664 g_dbus_unregister_interface(connection, path,
665 CONNMAN_SERVICE_INTERFACE);
669 if (service->network != NULL)
670 connman_network_unref(service->network);
672 connman_ipconfig_unref(service->ipconfig);
674 g_free(service->profile);
675 g_free(service->name);
676 g_free(service->passphrase);
677 g_free(service->identifier);
682 * __connman_service_put:
683 * @service: service structure
685 * Release service if no longer needed
687 void __connman_service_put(struct connman_service *service)
689 DBG("service %p", service);
691 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
694 iter = g_hash_table_lookup(service_hash, service->identifier);
696 g_sequence_remove(iter);
698 service_free(service);
702 static void __connman_service_initialize(struct connman_service *service)
704 DBG("service %p", service);
706 service->refcount = 1;
708 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
709 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
710 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
711 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
713 service->favorite = FALSE;
714 service->hidden = FALSE;
720 * connman_service_create:
722 * Allocate a new service.
724 * Returns: a newly-allocated #connman_service structure
726 struct connman_service *connman_service_create(void)
728 struct connman_service *service;
730 service = g_try_new0(struct connman_service, 1);
734 DBG("service %p", service);
736 __connman_service_initialize(service);
738 service->ipconfig = connman_ipconfig_create();
739 if (service->ipconfig == NULL) {
744 connman_ipconfig_set_method(service->ipconfig,
745 CONNMAN_IPCONFIG_METHOD_DHCP);
751 * connman_service_ref:
752 * @service: service structure
754 * Increase reference counter of service
756 struct connman_service *connman_service_ref(struct connman_service *service)
758 g_atomic_int_inc(&service->refcount);
764 * connman_service_unref:
765 * @service: service structure
767 * Decrease reference counter of service
769 void connman_service_unref(struct connman_service *service)
771 __connman_service_put(service);
774 static gint service_compare(gconstpointer a, gconstpointer b,
777 struct connman_service *service_a = (void *) a;
778 struct connman_service *service_b = (void *) b;
780 if (service_a->state != service_b->state) {
781 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
783 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
787 if (service_a->order > service_b->order)
790 if (service_a->order < service_b->order)
793 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
796 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
799 return (gint) service_b->strength - (gint) service_a->strength;
803 * connman_service_set_favorite:
804 * @service: service structure
805 * @favorite: favorite value
807 * Change the favorite setting of service
809 int connman_service_set_favorite(struct connman_service *service,
810 connman_bool_t favorite)
814 iter = g_hash_table_lookup(service_hash, service->identifier);
818 if (service->favorite == favorite)
821 service->favorite = favorite;
823 g_sequence_sort_changed(iter, service_compare, NULL);
825 __connman_profile_changed();
830 int __connman_service_set_carrier(struct connman_service *service,
831 connman_bool_t carrier)
833 DBG("service %p carrier %d", service, carrier);
838 switch (service->type) {
839 case CONNMAN_SERVICE_TYPE_UNKNOWN:
840 case CONNMAN_SERVICE_TYPE_WIFI:
841 case CONNMAN_SERVICE_TYPE_WIMAX:
842 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
843 case CONNMAN_SERVICE_TYPE_CELLULAR:
845 case CONNMAN_SERVICE_TYPE_ETHERNET:
849 if (carrier == FALSE) {
850 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
851 state_changed(service);
853 service->state = CONNMAN_SERVICE_STATE_IDLE;
854 state_changed(service);
856 service->state = CONNMAN_SERVICE_STATE_CARRIER;
857 state_changed(service);
860 return connman_service_set_favorite(service, carrier);
863 int __connman_service_indicate_state(struct connman_service *service,
864 enum connman_service_state state)
868 DBG("service %p state %d", service, state);
873 if (state == CONNMAN_SERVICE_STATE_CARRIER)
874 return __connman_service_set_carrier(service, TRUE);
876 if (service->state == state)
879 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
880 state == CONNMAN_SERVICE_STATE_DISCONNECT)
883 if (state == CONNMAN_SERVICE_STATE_IDLE &&
884 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
885 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
886 state_changed(service);
889 service->state = state;
890 state_changed(service);
892 if (state == CONNMAN_SERVICE_STATE_READY) {
893 connman_service_set_favorite(service, TRUE);
895 if (service->timeout > 0) {
896 g_source_remove(service->timeout);
897 service->timeout = 0;
900 if (service->pending != NULL) {
901 g_dbus_send_reply(connection, service->pending,
904 dbus_message_unref(service->pending);
905 service->pending = NULL;
908 g_get_current_time(&service->modified);
909 __connman_storage_save_service(service);
912 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
913 if (service->timeout > 0) {
914 g_source_remove(service->timeout);
915 service->timeout = 0;
918 if (service->pending != NULL) {
921 reply = __connman_error_failed(service->pending, EIO);
923 g_dbus_send_message(connection, reply);
925 dbus_message_unref(service->pending);
926 service->pending = NULL;
929 g_get_current_time(&service->modified);
930 __connman_storage_save_service(service);
932 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
934 iter = g_hash_table_lookup(service_hash, service->identifier);
936 g_sequence_sort_changed(iter, service_compare, NULL);
938 __connman_profile_changed();
943 int __connman_service_indicate_error(struct connman_service *service,
944 enum connman_service_error error)
946 DBG("service %p error %d", service, error);
951 service->error = error;
953 return __connman_service_indicate_state(service,
954 CONNMAN_SERVICE_STATE_FAILURE);
957 int __connman_service_indicate_default(struct connman_service *service)
959 DBG("service %p", service);
964 int __connman_service_connect(struct connman_service *service)
968 DBG("service %p", service);
970 if (service->state == CONNMAN_SERVICE_STATE_READY)
973 if (service->network != NULL) {
974 if (service->hidden == TRUE)
977 connman_network_set_string(service->network,
978 "WiFi.Passphrase", service->passphrase);
980 err = __connman_network_connect(service->network);
981 } else if (service->device != NULL) {
982 if (service->favorite == FALSE)
985 err = __connman_device_connect(service->device);
990 if (err != -EINPROGRESS)
993 service->timeout = g_timeout_add_seconds(45,
994 connect_timeout, service);
1002 int __connman_service_disconnect(struct connman_service *service)
1006 DBG("service %p", service);
1008 if (service->network != NULL) {
1009 err = __connman_network_disconnect(service->network);
1010 } else if (service->device != NULL) {
1011 if (service->favorite == FALSE)
1013 err = __connman_device_disconnect(service->device);
1018 if (err != -EINPROGRESS)
1021 return -EINPROGRESS;
1028 * __connman_service_lookup:
1029 * @identifier: service identifier
1031 * Look up a service by identifier (reference count will not be increased)
1033 static struct connman_service *__connman_service_lookup(const char *identifier)
1035 GSequenceIter *iter;
1037 iter = g_hash_table_lookup(service_hash, identifier);
1039 return g_sequence_get(iter);
1045 * __connman_service_get:
1046 * @identifier: service identifier
1048 * Look up a service by identifier or create a new one if not found
1050 static struct connman_service *__connman_service_get(const char *identifier)
1052 struct connman_service *service;
1053 GSequenceIter *iter;
1055 iter = g_hash_table_lookup(service_hash, identifier);
1057 service = g_sequence_get(iter);
1058 if (service != NULL)
1059 g_atomic_int_inc(&service->refcount);
1063 service = connman_service_create();
1064 if (service == NULL)
1067 DBG("service %p", service);
1069 service->identifier = g_strdup(identifier);
1071 service->profile = g_strdup(__connman_profile_active_ident());
1073 __connman_storage_load_service(service);
1075 iter = g_sequence_insert_sorted(service_list, service,
1076 service_compare, NULL);
1078 g_hash_table_insert(service_hash, service->identifier, iter);
1083 static int service_register(struct connman_service *service)
1085 const char *path = __connman_profile_active_path();
1086 GSequenceIter *iter;
1088 DBG("service %p", service);
1090 if (service->path != NULL)
1093 service->path = g_strdup_printf("%s/%s", path, service->identifier);
1095 DBG("path %s", service->path);
1097 g_dbus_register_interface(connection, service->path,
1098 CONNMAN_SERVICE_INTERFACE,
1099 service_methods, service_signals,
1100 NULL, service, NULL);
1102 __connman_storage_load_service(service);
1104 iter = g_hash_table_lookup(service_hash, service->identifier);
1106 g_sequence_sort_changed(iter, service_compare, NULL);
1108 __connman_profile_changed();
1113 static connman_bool_t is_connecting(struct connman_service *service)
1115 switch (service->state) {
1116 case CONNMAN_SERVICE_STATE_UNKNOWN:
1117 case CONNMAN_SERVICE_STATE_IDLE:
1118 case CONNMAN_SERVICE_STATE_CARRIER:
1119 case CONNMAN_SERVICE_STATE_FAILURE:
1120 case CONNMAN_SERVICE_STATE_DISCONNECT:
1121 case CONNMAN_SERVICE_STATE_READY:
1123 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1124 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1131 static void __connman_service_auto_connect(void)
1133 struct connman_service *service;
1134 GSequenceIter *iter;
1138 iter = g_sequence_get_begin_iter(service_list);
1139 if (g_sequence_iter_is_end(iter) == TRUE)
1142 service = g_sequence_get(iter);
1144 while (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
1145 iter = g_sequence_iter_next(iter);
1146 if (g_sequence_iter_is_end(iter))
1148 service = g_sequence_get(iter);
1151 if (service->favorite == FALSE)
1154 if (service->state == CONNMAN_SERVICE_STATE_READY)
1157 if (is_connecting(service) == TRUE)
1160 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
1161 __connman_service_connect(service);
1165 * connman_service_lookup_from_device:
1166 * @device: device structure
1168 * Look up a service by device (reference count will not be increased)
1170 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
1172 struct connman_service *service;
1176 ident = __connman_device_get_ident(device);
1180 name = g_strdup_printf("%s_%s",
1181 __connman_device_get_type(device), ident);
1183 service = __connman_service_lookup(name);
1190 static enum connman_service_type convert_device_type(struct connman_device *device)
1192 enum connman_device_type type = connman_device_get_type(device);
1195 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1196 case CONNMAN_DEVICE_TYPE_VENDOR:
1197 case CONNMAN_DEVICE_TYPE_WIFI:
1198 case CONNMAN_DEVICE_TYPE_WIMAX:
1199 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1200 case CONNMAN_DEVICE_TYPE_GPS:
1201 case CONNMAN_DEVICE_TYPE_MBM:
1202 case CONNMAN_DEVICE_TYPE_HSO:
1203 case CONNMAN_DEVICE_TYPE_NOZOMI:
1204 case CONNMAN_DEVICE_TYPE_HUAWEI:
1205 case CONNMAN_DEVICE_TYPE_NOVATEL:
1207 case CONNMAN_DEVICE_TYPE_ETHERNET:
1208 return CONNMAN_SERVICE_TYPE_ETHERNET;
1211 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1215 * connman_service_create_from_device:
1216 * @device: device structure
1218 * Look up service by device and if not found, create one
1220 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
1222 struct connman_service *service;
1226 ident = __connman_device_get_ident(device);
1230 name = g_strdup_printf("%s_%s",
1231 __connman_device_get_type(device), ident);
1233 service = __connman_service_get(name);
1234 if (service == NULL)
1237 if (service->path != NULL) {
1238 __connman_service_put(service);
1243 service->type = convert_device_type(device);
1245 service->device = device;
1247 service_register(service);
1249 if (service->favorite == TRUE)
1250 __connman_service_auto_connect();
1259 * connman_service_lookup_from_network:
1260 * @network: network structure
1262 * Look up a service by network (reference count will not be increased)
1264 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
1266 struct connman_service *service;
1267 const char *ident, *group;
1270 ident = __connman_network_get_ident(network);
1274 group = __connman_network_get_group(network);
1278 name = g_strdup_printf("%s_%s_%s",
1279 __connman_network_get_type(network), ident, group);
1281 service = __connman_service_lookup(name);
1288 unsigned int __connman_service_get_order(struct connman_service *service)
1290 return service->order;
1293 static enum connman_service_type convert_network_type(struct connman_network *network)
1295 enum connman_network_type type = connman_network_get_type(network);
1298 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1299 case CONNMAN_NETWORK_TYPE_VENDOR:
1300 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1301 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1302 case CONNMAN_NETWORK_TYPE_HSO:
1304 case CONNMAN_NETWORK_TYPE_WIFI:
1305 return CONNMAN_SERVICE_TYPE_WIFI;
1306 case CONNMAN_NETWORK_TYPE_WIMAX:
1307 return CONNMAN_SERVICE_TYPE_WIMAX;
1310 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1313 static enum connman_service_mode convert_wifi_mode(const char *mode)
1316 return CONNMAN_SERVICE_MODE_UNKNOWN;
1317 else if (g_str_equal(mode, "managed") == TRUE)
1318 return CONNMAN_SERVICE_MODE_MANAGED;
1319 else if (g_str_equal(mode, "adhoc") == TRUE)
1320 return CONNMAN_SERVICE_MODE_ADHOC;
1322 return CONNMAN_SERVICE_MODE_UNKNOWN;
1325 static enum connman_service_mode convert_wifi_security(const char *security)
1327 if (security == NULL)
1328 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1329 else if (g_str_equal(security, "none") == TRUE)
1330 return CONNMAN_SERVICE_SECURITY_NONE;
1331 else if (g_str_equal(security, "wep") == TRUE)
1332 return CONNMAN_SERVICE_SECURITY_WEP;
1333 else if (g_str_equal(security, "wpa") == TRUE)
1334 return CONNMAN_SERVICE_SECURITY_WPA;
1335 else if (g_str_equal(security, "rsn") == TRUE)
1336 return CONNMAN_SERVICE_SECURITY_RSN;
1338 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1341 static void update_from_network(struct connman_service *service,
1342 struct connman_network *network)
1344 connman_uint8_t strength = service->strength;
1345 GSequenceIter *iter;
1348 str = connman_network_get_string(network, "Name");
1350 g_free(service->name);
1351 service->name = g_strdup(str);
1352 service->hidden = FALSE;
1354 g_free(service->name);
1355 service->name = NULL;
1356 service->hidden = TRUE;
1359 service->strength = connman_network_get_uint8(network, "Strength");
1361 str = connman_network_get_string(network, "WiFi.Mode");
1362 service->mode = convert_wifi_mode(str);
1364 str = connman_network_get_string(network, "WiFi.Security");
1365 service->security = convert_wifi_security(str);
1367 if (service->strength > strength && service->network != NULL) {
1368 connman_network_unref(service->network);
1369 service->network = NULL;
1371 strength_changed(service);
1374 if (service->network == NULL) {
1375 service->network = connman_network_ref(network);
1377 str = connman_network_get_string(network, "WiFi.Passphrase");
1379 g_free(service->passphrase);
1380 service->passphrase = g_strdup(str);
1384 iter = g_hash_table_lookup(service_hash, service->identifier);
1386 g_sequence_sort_changed(iter, service_compare, NULL);
1390 * connman_service_create_from_network:
1391 * @network: network structure
1393 * Look up service by network and if not found, create one
1395 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
1397 struct connman_service *service;
1398 const char *ident, *group;
1401 service = __connman_service_lookup_from_network(network);
1402 if (service != NULL) {
1403 update_from_network(service, network);
1407 ident = __connman_network_get_ident(network);
1411 group = __connman_network_get_group(network);
1415 name = g_strdup_printf("%s_%s_%s",
1416 __connman_network_get_type(network), ident, group);
1418 service = __connman_service_get(name);
1419 if (service == NULL)
1422 if (service->path != NULL) {
1423 update_from_network(service, network);
1425 __connman_profile_changed();
1427 __connman_service_put(service);
1432 service->type = convert_network_type(network);
1434 service->state = CONNMAN_SERVICE_STATE_IDLE;
1436 update_from_network(service, network);
1438 service_register(service);
1440 if (service->favorite == TRUE)
1441 __connman_service_auto_connect();
1449 static int service_load(struct connman_service *service)
1452 gchar *pathname, *data = NULL;
1456 DBG("service %p", service);
1458 if (service->profile == NULL)
1461 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1462 if (pathname == NULL)
1465 keyfile = g_key_file_new();
1467 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1474 if (g_key_file_load_from_data(keyfile, data, length,
1475 0, NULL) == FALSE) {
1482 switch (service->type) {
1483 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1484 case CONNMAN_SERVICE_TYPE_ETHERNET:
1486 case CONNMAN_SERVICE_TYPE_WIFI:
1487 case CONNMAN_SERVICE_TYPE_WIMAX:
1488 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1489 case CONNMAN_SERVICE_TYPE_CELLULAR:
1490 service->favorite = g_key_file_get_boolean(keyfile,
1491 service->identifier, "Favorite", NULL);
1493 str = g_key_file_get_string(keyfile,
1494 service->identifier, "Failure", NULL);
1496 service->state = CONNMAN_SERVICE_STATE_FAILURE;
1497 service->error = string2error(str);
1502 str = g_key_file_get_string(keyfile,
1503 service->identifier, "Modified", NULL);
1505 g_time_val_from_iso8601(str, &service->modified);
1509 str = g_key_file_get_string(keyfile,
1510 service->identifier, "Passphrase", NULL);
1512 g_free(service->passphrase);
1513 service->passphrase = str;
1516 __connman_ipconfig_load(service->ipconfig, keyfile,
1517 service->identifier, "IPv4.");
1519 g_key_file_free(keyfile);
1524 static int service_save(struct connman_service *service)
1527 gchar *pathname, *data = NULL;
1531 DBG("service %p", service);
1533 if (service->profile == NULL)
1536 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1537 if (pathname == NULL)
1540 keyfile = g_key_file_new();
1542 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1546 if (g_key_file_load_from_data(keyfile, data, length,
1554 if (service->name != NULL)
1555 g_key_file_set_string(keyfile, service->identifier,
1556 "Name", service->name);
1558 switch (service->type) {
1559 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1560 case CONNMAN_SERVICE_TYPE_ETHERNET:
1562 case CONNMAN_SERVICE_TYPE_WIFI:
1563 case CONNMAN_SERVICE_TYPE_WIMAX:
1564 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1565 case CONNMAN_SERVICE_TYPE_CELLULAR:
1566 g_key_file_set_boolean(keyfile, service->identifier,
1567 "Favorite", service->favorite);
1569 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
1570 const char *failure = error2string(service->error);
1571 if (failure != NULL)
1572 g_key_file_set_string(keyfile,
1573 service->identifier,
1574 "Failure", failure);
1576 g_key_file_remove_key(keyfile, service->identifier,
1582 str = g_time_val_to_iso8601(&service->modified);
1584 g_key_file_set_string(keyfile, service->identifier,
1589 if (service->passphrase != NULL)
1590 g_key_file_set_string(keyfile, service->identifier,
1591 "Passphrase", service->passphrase);
1593 g_key_file_remove_key(keyfile, service->identifier,
1594 "Passphrase", NULL);
1596 __connman_ipconfig_save(service->ipconfig, keyfile,
1597 service->identifier, "IPv4.");
1599 data = g_key_file_to_data(keyfile, &length, NULL);
1601 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1602 connman_error("Failed to store service information");
1607 g_key_file_free(keyfile);
1614 static struct connman_storage service_storage = {
1616 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1617 .service_load = service_load,
1618 .service_save = service_save,
1621 int __connman_service_init(void)
1625 connection = connman_dbus_get_connection();
1627 if (connman_storage_register(&service_storage) < 0)
1628 connman_error("Failed to register service storage");
1630 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1633 service_list = g_sequence_new(service_free);
1638 void __connman_service_cleanup(void)
1642 g_sequence_free(service_list);
1643 service_list = NULL;
1645 g_hash_table_destroy(service_hash);
1646 service_hash = NULL;
1648 connman_storage_unregister(&service_storage);
1650 dbus_connection_unref(connection);