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
32 static DBusConnection *connection = NULL;
34 static GSequence *service_list = NULL;
35 static GHashTable *service_hash = NULL;
37 struct connman_service {
41 enum connman_service_type type;
42 enum connman_service_mode mode;
43 enum connman_service_security security;
44 enum connman_service_state state;
45 enum connman_service_error error;
46 connman_uint8_t strength;
47 connman_bool_t favorite;
48 connman_bool_t hidden;
49 connman_bool_t ignore;
55 struct connman_ipconfig *ipconfig;
56 struct connman_device *device;
57 struct connman_network *network;
62 static void append_path(gpointer value, gpointer user_data)
64 struct connman_service *service = value;
65 DBusMessageIter *iter = user_data;
67 if (service->path == NULL || service->hidden == TRUE)
70 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
74 void __connman_service_list(DBusMessageIter *iter)
78 g_sequence_foreach(service_list, append_path, iter);
83 struct connman_service *service;
86 static void compare_path(gpointer value, gpointer user_data)
88 struct connman_service *service = value;
89 struct find_data *data = user_data;
91 if (data->service != NULL)
94 if (g_strcmp0(service->path, data->path) == 0)
95 data->service = service;
98 static struct connman_service *find_service(const char *path)
100 struct find_data data = { .path = path, .service = NULL };
102 DBG("path %s", path);
104 g_sequence_foreach(service_list, compare_path, &data);
109 static const char *type2string(enum connman_service_type type)
112 case CONNMAN_SERVICE_TYPE_UNKNOWN:
114 case CONNMAN_SERVICE_TYPE_ETHERNET:
116 case CONNMAN_SERVICE_TYPE_WIFI:
118 case CONNMAN_SERVICE_TYPE_WIMAX:
120 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
122 case CONNMAN_SERVICE_TYPE_CELLULAR:
129 static const char *mode2string(enum connman_service_mode mode)
132 case CONNMAN_SERVICE_MODE_UNKNOWN:
134 case CONNMAN_SERVICE_MODE_MANAGED:
136 case CONNMAN_SERVICE_MODE_ADHOC:
143 static const char *security2string(enum connman_service_security security)
146 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
148 case CONNMAN_SERVICE_SECURITY_NONE:
150 case CONNMAN_SERVICE_SECURITY_WEP:
152 case CONNMAN_SERVICE_SECURITY_WPA:
154 case CONNMAN_SERVICE_SECURITY_RSN:
161 static const char *state2string(enum connman_service_state state)
164 case CONNMAN_SERVICE_STATE_UNKNOWN:
166 case CONNMAN_SERVICE_STATE_IDLE:
168 case CONNMAN_SERVICE_STATE_CARRIER:
170 case CONNMAN_SERVICE_STATE_ASSOCIATION:
171 return "association";
172 case CONNMAN_SERVICE_STATE_CONFIGURATION:
173 return "configuration";
174 case CONNMAN_SERVICE_STATE_READY:
176 case CONNMAN_SERVICE_STATE_DISCONNECT:
178 case CONNMAN_SERVICE_STATE_FAILURE:
185 static const char *error2string(enum connman_service_error error)
188 case CONNMAN_SERVICE_ERROR_UNKNOWN:
190 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
191 return "out-of-range";
192 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
193 return "pin-missing";
194 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
195 return "dhcp-failed";
196 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
197 return "connect-failed";
203 static enum connman_service_error string2error(const char *error)
205 if (g_strcmp0(error, "dhcp-failed") == 0)
206 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
207 else if (g_strcmp0(error, "pin-missing") == 0)
208 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
210 return CONNMAN_SERVICE_ERROR_UNKNOWN;
213 const char *__connman_service_default(void)
215 struct connman_service *service;
218 iter = g_sequence_get_begin_iter(service_list);
220 if (g_sequence_iter_is_end(iter) == TRUE)
223 service = g_sequence_get(iter);
227 if (service->state != CONNMAN_SERVICE_STATE_READY)
230 return type2string(service->type);
233 static void state_changed(struct connman_service *service)
236 DBusMessageIter entry, value;
237 const char *str, *key = "State";
239 if (service->path == NULL)
242 str = state2string(service->state);
246 signal = dbus_message_new_signal(service->path,
247 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
251 dbus_message_iter_init_append(signal, &entry);
253 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
255 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
256 DBUS_TYPE_STRING_AS_STRING, &value);
257 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
258 dbus_message_iter_close_container(&entry, &value);
260 g_dbus_send_message(connection, signal);
263 static void strength_changed(struct connman_service *service)
266 DBusMessageIter entry, value;
267 const char *key = "Strength";
269 if (service->path == NULL)
272 if (service->strength == 0)
275 signal = dbus_message_new_signal(service->path,
276 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
280 dbus_message_iter_init_append(signal, &entry);
282 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
284 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
285 DBUS_TYPE_BYTE_AS_STRING, &value);
286 dbus_message_iter_append_basic(&value, DBUS_TYPE_BYTE,
288 dbus_message_iter_close_container(&entry, &value);
290 g_dbus_send_message(connection, signal);
293 static void passphrase_changed(struct connman_service *service)
296 DBusMessageIter entry, value;
297 dbus_bool_t required;
298 const char *key = "PassphraseRequired";
300 if (service->path == NULL)
303 switch (service->type) {
304 case CONNMAN_SERVICE_TYPE_UNKNOWN:
305 case CONNMAN_SERVICE_TYPE_ETHERNET:
306 case CONNMAN_SERVICE_TYPE_WIMAX:
307 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
308 case CONNMAN_SERVICE_TYPE_CELLULAR:
310 case CONNMAN_SERVICE_TYPE_WIFI:
313 switch (service->security) {
314 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
315 case CONNMAN_SERVICE_SECURITY_NONE:
317 case CONNMAN_SERVICE_SECURITY_WEP:
318 case CONNMAN_SERVICE_SECURITY_WPA:
319 case CONNMAN_SERVICE_SECURITY_RSN:
320 if (service->passphrase == NULL)
327 signal = dbus_message_new_signal(service->path,
328 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
332 dbus_message_iter_init_append(signal, &entry);
334 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
336 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
337 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
338 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &required);
339 dbus_message_iter_close_container(&entry, &value);
341 g_dbus_send_message(connection, signal);
344 static DBusMessage *get_properties(DBusConnection *conn,
345 DBusMessage *msg, void *user_data)
347 struct connman_service *service = user_data;
349 DBusMessageIter array, dict;
350 dbus_bool_t required;
353 DBG("service %p", service);
355 reply = dbus_message_new_method_return(msg);
359 dbus_message_iter_init_append(reply, &array);
361 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
362 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
363 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
364 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
366 str = type2string(service->type);
368 connman_dbus_dict_append_variant(&dict, "Type",
369 DBUS_TYPE_STRING, &str);
371 str = mode2string(service->mode);
373 connman_dbus_dict_append_variant(&dict, "Mode",
374 DBUS_TYPE_STRING, &str);
376 str = security2string(service->security);
378 connman_dbus_dict_append_variant(&dict, "Security",
379 DBUS_TYPE_STRING, &str);
381 str = state2string(service->state);
383 connman_dbus_dict_append_variant(&dict, "State",
384 DBUS_TYPE_STRING, &str);
386 str = error2string(service->error);
388 connman_dbus_dict_append_variant(&dict, "Error",
389 DBUS_TYPE_STRING, &str);
391 if (service->strength > 0)
392 connman_dbus_dict_append_variant(&dict, "Strength",
393 DBUS_TYPE_BYTE, &service->strength);
395 connman_dbus_dict_append_variant(&dict, "Favorite",
396 DBUS_TYPE_BOOLEAN, &service->favorite);
398 if (service->name != NULL)
399 connman_dbus_dict_append_variant(&dict, "Name",
400 DBUS_TYPE_STRING, &service->name);
402 switch (service->type) {
403 case CONNMAN_SERVICE_TYPE_UNKNOWN:
404 case CONNMAN_SERVICE_TYPE_ETHERNET:
405 case CONNMAN_SERVICE_TYPE_WIMAX:
406 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
407 case CONNMAN_SERVICE_TYPE_CELLULAR:
409 case CONNMAN_SERVICE_TYPE_WIFI:
410 if (service->passphrase != NULL &&
411 __connman_security_check_privilege(msg,
412 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
413 connman_dbus_dict_append_variant(&dict, "Passphrase",
414 DBUS_TYPE_STRING, &service->passphrase);
418 switch (service->security) {
419 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
420 case CONNMAN_SERVICE_SECURITY_NONE:
422 case CONNMAN_SERVICE_SECURITY_WEP:
423 case CONNMAN_SERVICE_SECURITY_WPA:
424 case CONNMAN_SERVICE_SECURITY_RSN:
425 if (service->passphrase == NULL)
430 connman_dbus_dict_append_variant(&dict, "PassphraseRequired",
431 DBUS_TYPE_BOOLEAN, &required);
435 __connman_ipconfig_append_ipv4(service->ipconfig, &dict, "IPv4.");
437 dbus_message_iter_close_container(&array, &dict);
442 static DBusMessage *set_property(DBusConnection *conn,
443 DBusMessage *msg, void *user_data)
445 struct connman_service *service = user_data;
446 DBusMessageIter iter, value;
450 DBG("service %p", service);
452 if (dbus_message_iter_init(msg, &iter) == FALSE)
453 return __connman_error_invalid_arguments(msg);
455 dbus_message_iter_get_basic(&iter, &name);
456 dbus_message_iter_next(&iter);
457 dbus_message_iter_recurse(&iter, &value);
459 if (__connman_security_check_privilege(msg,
460 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
461 return __connman_error_permission_denied(msg);
463 type = dbus_message_iter_get_arg_type(&value);
465 if (g_str_equal(name, "Passphrase") == TRUE) {
466 const char *passphrase;
468 if (type != DBUS_TYPE_STRING)
469 return __connman_error_invalid_arguments(msg);
471 if (__connman_security_check_privilege(msg,
472 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
473 return __connman_error_permission_denied(msg);
475 dbus_message_iter_get_basic(&value, &passphrase);
477 g_free(service->passphrase);
478 service->passphrase = g_strdup(passphrase);
480 passphrase_changed(service);
482 if (service->network != NULL)
483 connman_network_set_string(service->network,
484 "WiFi.Passphrase", service->passphrase);
486 __connman_storage_save_service(service);
487 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
490 err = __connman_ipconfig_set_ipv4(service->ipconfig,
493 return __connman_error_failed(msg, -err);
495 return __connman_error_invalid_property(msg);
497 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
500 static void set_idle(struct connman_service *service)
502 service->state = CONNMAN_SERVICE_STATE_IDLE;
503 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
504 state_changed(service);
507 static DBusMessage *clear_property(DBusConnection *conn,
508 DBusMessage *msg, void *user_data)
510 struct connman_service *service = user_data;
513 DBG("service %p", service);
515 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
518 if (__connman_security_check_privilege(msg,
519 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
520 return __connman_error_permission_denied(msg);
522 if (g_str_equal(name, "Error") == TRUE) {
525 g_get_current_time(&service->modified);
526 __connman_storage_save_service(service);
527 } else if (g_str_equal(name, "Passphrase") == TRUE) {
528 g_free(service->passphrase);
529 service->passphrase = NULL;
531 passphrase_changed(service);
533 __connman_storage_save_service(service);
535 return __connman_error_invalid_property(msg);
537 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
540 static connman_bool_t is_connecting(struct connman_service *service)
542 switch (service->state) {
543 case CONNMAN_SERVICE_STATE_UNKNOWN:
544 case CONNMAN_SERVICE_STATE_IDLE:
545 case CONNMAN_SERVICE_STATE_CARRIER:
546 case CONNMAN_SERVICE_STATE_FAILURE:
547 case CONNMAN_SERVICE_STATE_DISCONNECT:
548 case CONNMAN_SERVICE_STATE_READY:
550 case CONNMAN_SERVICE_STATE_ASSOCIATION:
551 case CONNMAN_SERVICE_STATE_CONFIGURATION:
558 static connman_bool_t is_ignore(struct connman_service *service)
560 if (service->ignore == TRUE)
563 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
569 void __connman_service_auto_connect(void)
571 struct connman_service *service = NULL;
576 iter = g_sequence_get_begin_iter(service_list);
578 while (g_sequence_iter_is_end(iter) == FALSE) {
579 service = g_sequence_get(iter);
581 if (service->pending != NULL)
584 if (is_connecting(service) == TRUE)
587 if (service->favorite == FALSE)
590 if (service->state == CONNMAN_SERVICE_STATE_READY)
593 if (is_ignore(service) == FALSE &&
594 service->state == CONNMAN_SERVICE_STATE_IDLE)
599 iter = g_sequence_iter_next(iter);
603 __connman_service_connect(service);
606 static void reply_pending(struct connman_service *service, int error)
608 if (service->timeout > 0) {
609 g_source_remove(service->timeout);
610 service->timeout = 0;
613 if (service->pending != NULL) {
617 reply = __connman_error_failed(service->pending,
620 g_dbus_send_message(connection, reply);
622 g_dbus_send_reply(connection, service->pending,
625 dbus_message_unref(service->pending);
626 service->pending = NULL;
630 static gboolean connect_timeout(gpointer user_data)
632 struct connman_service *service = user_data;
633 connman_bool_t auto_connect = FALSE;
635 DBG("service %p", service);
637 service->timeout = 0;
639 if (service->network != NULL) {
640 connman_bool_t connected;
642 connected = connman_network_get_connected(service->network);
643 if (connected == TRUE) {
644 __connman_service_indicate_state(service,
645 CONNMAN_SERVICE_STATE_READY);
649 __connman_network_disconnect(service->network);
650 } else if (service->device != NULL) {
651 connman_bool_t disconnected;
653 disconnected = connman_device_get_disconnected(service->device);
654 if (disconnected == FALSE) {
655 __connman_service_indicate_state(service,
656 CONNMAN_SERVICE_STATE_READY);
660 __connman_device_disconnect(service->device);
663 if (service->pending != NULL) {
666 reply = __connman_error_operation_timeout(service->pending);
668 g_dbus_send_message(connection, reply);
670 dbus_message_unref(service->pending);
671 service->pending = NULL;
675 __connman_service_indicate_state(service,
676 CONNMAN_SERVICE_STATE_FAILURE);
678 if (auto_connect == TRUE)
679 __connman_service_auto_connect();
684 static DBusMessage *connect_service(DBusConnection *conn,
685 DBusMessage *msg, void *user_data)
687 struct connman_service *service = user_data;
691 DBG("service %p", service);
693 if (service->pending != NULL)
694 return __connman_error_in_progress(msg);
696 iter = g_sequence_get_begin_iter(service_list);
698 while (g_sequence_iter_is_end(iter) == FALSE) {
699 struct connman_service *temp = g_sequence_get(iter);
701 if (service->type == temp->type &&
702 is_connecting(temp) == TRUE)
703 return __connman_error_in_progress(msg);
705 iter = g_sequence_iter_next(iter);
708 service->ignore = FALSE;
710 service->pending = dbus_message_ref(msg);
712 err = __connman_service_connect(service);
714 if (err != -EINPROGRESS) {
715 dbus_message_unref(service->pending);
716 service->pending = NULL;
718 return __connman_error_failed(msg, -err);
724 dbus_message_unref(service->pending);
725 service->pending = NULL;
727 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
730 static DBusMessage *disconnect_service(DBusConnection *conn,
731 DBusMessage *msg, void *user_data)
733 struct connman_service *service = user_data;
736 DBG("service %p", service);
738 service->ignore = TRUE;
740 err = __connman_service_disconnect(service);
742 if (err != -EINPROGRESS)
743 return __connman_error_failed(msg, -err);
748 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
751 static DBusMessage *remove_service(DBusConnection *conn,
752 DBusMessage *msg, void *user_data)
754 struct connman_service *service = user_data;
756 DBG("service %p", service);
758 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
759 return __connman_error_not_supported(msg);
761 if (service->favorite == FALSE)
762 return __connman_error_not_supported(msg);
764 if (service->network != NULL)
765 __connman_network_disconnect(service->network);
767 g_free(service->passphrase);
768 service->passphrase = NULL;
770 passphrase_changed(service);
772 connman_service_set_favorite(service, FALSE);
773 __connman_storage_save_service(service);
775 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
778 static DBusMessage *move_before(DBusConnection *conn,
779 DBusMessage *msg, void *user_data)
781 struct connman_service *service = user_data;
782 struct connman_service *target;
784 GSequenceIter *src, *dst;
786 DBG("service %p", service);
788 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
791 if (service->favorite == FALSE)
792 return __connman_error_not_supported(msg);
794 target = find_service(path);
795 if (target == NULL || target->favorite == FALSE || target == service)
796 return __connman_error_invalid_service(msg);
798 DBG("target %s", target->identifier);
800 g_get_current_time(&service->modified);
801 __connman_storage_save_service(service);
803 src = g_hash_table_lookup(service_hash, service->identifier);
804 dst = g_hash_table_lookup(service_hash, target->identifier);
806 g_sequence_move(src, dst);
808 __connman_profile_changed(FALSE);
810 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
813 static DBusMessage *move_after(DBusConnection *conn,
814 DBusMessage *msg, void *user_data)
816 struct connman_service *service = user_data;
817 struct connman_service *target;
820 DBG("service %p", service);
822 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
825 if (service->favorite == FALSE)
826 return __connman_error_not_supported(msg);
828 target = find_service(path);
829 if (target == NULL || target->favorite == FALSE || target == service)
830 return __connman_error_invalid_service(msg);
832 DBG("target %s", target->identifier);
834 g_get_current_time(&service->modified);
835 __connman_storage_save_service(service);
837 return __connman_error_not_implemented(msg);
840 static GDBusMethodTable service_methods[] = {
841 { "GetProperties", "", "a{sv}", get_properties },
842 { "SetProperty", "sv", "", set_property },
843 { "ClearProperty", "s", "", clear_property },
844 { "Connect", "", "", connect_service,
845 G_DBUS_METHOD_FLAG_ASYNC },
846 { "Disconnect", "", "", disconnect_service },
847 { "Remove", "", "", remove_service },
848 { "MoveBefore", "o", "", move_before },
849 { "MoveAfter", "o", "", move_after },
853 static GDBusSignalTable service_signals[] = {
854 { "PropertyChanged", "sv" },
858 static void service_free(gpointer user_data)
860 struct connman_service *service = user_data;
861 char *path = service->path;
863 DBG("service %p", service);
865 reply_pending(service, ENOENT);
867 g_hash_table_remove(service_hash, service->identifier);
869 service->path = NULL;
872 __connman_profile_changed(FALSE);
874 g_dbus_unregister_interface(connection, path,
875 CONNMAN_SERVICE_INTERFACE);
879 if (service->network != NULL)
880 connman_network_unref(service->network);
882 connman_ipconfig_unref(service->ipconfig);
884 g_free(service->profile);
885 g_free(service->name);
886 g_free(service->passphrase);
887 g_free(service->identifier);
892 * __connman_service_put:
893 * @service: service structure
895 * Release service if no longer needed
897 void __connman_service_put(struct connman_service *service)
899 DBG("service %p", service);
901 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
904 iter = g_hash_table_lookup(service_hash, service->identifier);
906 __connman_service_disconnect(service);
907 g_sequence_remove(iter);
909 service_free(service);
913 static void __connman_service_initialize(struct connman_service *service)
915 DBG("service %p", service);
917 service->refcount = 1;
919 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
920 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
921 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
922 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
924 service->favorite = FALSE;
925 service->hidden = FALSE;
927 service->ignore = FALSE;
933 * connman_service_create:
935 * Allocate a new service.
937 * Returns: a newly-allocated #connman_service structure
939 struct connman_service *connman_service_create(void)
941 struct connman_service *service;
943 service = g_try_new0(struct connman_service, 1);
947 DBG("service %p", service);
949 __connman_service_initialize(service);
951 service->ipconfig = connman_ipconfig_create();
952 if (service->ipconfig == NULL) {
957 connman_ipconfig_set_method(service->ipconfig,
958 CONNMAN_IPCONFIG_METHOD_DHCP);
964 * connman_service_ref:
965 * @service: service structure
967 * Increase reference counter of service
969 struct connman_service *connman_service_ref(struct connman_service *service)
971 g_atomic_int_inc(&service->refcount);
977 * connman_service_unref:
978 * @service: service structure
980 * Decrease reference counter of service
982 void connman_service_unref(struct connman_service *service)
984 __connman_service_put(service);
987 static gint service_compare(gconstpointer a, gconstpointer b,
990 struct connman_service *service_a = (void *) a;
991 struct connman_service *service_b = (void *) b;
993 if (service_a->state != service_b->state) {
994 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
996 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
1000 if (service_a->order > service_b->order)
1003 if (service_a->order < service_b->order)
1006 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
1009 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
1012 return (gint) service_b->strength - (gint) service_a->strength;
1016 * connman_service_set_favorite:
1017 * @service: service structure
1018 * @favorite: favorite value
1020 * Change the favorite setting of service
1022 int connman_service_set_favorite(struct connman_service *service,
1023 connman_bool_t favorite)
1025 GSequenceIter *iter;
1027 iter = g_hash_table_lookup(service_hash, service->identifier);
1031 if (service->favorite == favorite)
1034 service->favorite = favorite;
1036 g_sequence_sort_changed(iter, service_compare, NULL);
1038 __connman_profile_changed(FALSE);
1043 static void default_changed(void)
1045 DBusMessage *signal;
1046 DBusMessageIter entry, value;
1047 const char *key = "DefaultTechnology";
1048 const char *str = __connman_service_default();
1050 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1051 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1055 dbus_message_iter_init_append(signal, &entry);
1057 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1059 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1060 DBUS_TYPE_STRING_AS_STRING, &value);
1061 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
1062 dbus_message_iter_close_container(&entry, &value);
1064 g_dbus_send_message(connection, signal);
1067 int __connman_service_indicate_state(struct connman_service *service,
1068 enum connman_service_state state)
1070 GSequenceIter *iter;
1072 DBG("service %p state %d", service, state);
1074 if (service == NULL)
1077 if (service->state == state)
1080 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
1081 state == CONNMAN_SERVICE_STATE_IDLE)
1084 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
1085 state == CONNMAN_SERVICE_STATE_DISCONNECT)
1088 if (state == CONNMAN_SERVICE_STATE_IDLE &&
1089 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
1090 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
1091 state_changed(service);
1093 __connman_service_disconnect(service);
1096 service->state = state;
1097 state_changed(service);
1099 if (state == CONNMAN_SERVICE_STATE_READY) {
1100 connman_service_set_favorite(service, TRUE);
1102 reply_pending(service, 0);
1104 g_get_current_time(&service->modified);
1105 __connman_storage_save_service(service);
1107 __connman_notifier_connect(service->type);
1110 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
1113 __connman_notifier_disconnect(service->type);
1116 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
1117 reply_pending(service, EIO);
1119 g_get_current_time(&service->modified);
1120 __connman_storage_save_service(service);
1122 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1124 iter = g_hash_table_lookup(service_hash, service->identifier);
1126 g_sequence_sort_changed(iter, service_compare, NULL);
1128 __connman_profile_changed(FALSE);
1130 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
1131 service->state == CONNMAN_SERVICE_STATE_FAILURE)
1132 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
1137 int __connman_service_indicate_error(struct connman_service *service,
1138 enum connman_service_error error)
1140 DBG("service %p error %d", service, error);
1142 if (service == NULL)
1145 service->error = error;
1147 return __connman_service_indicate_state(service,
1148 CONNMAN_SERVICE_STATE_FAILURE);
1151 int __connman_service_indicate_default(struct connman_service *service)
1153 DBG("service %p", service);
1160 static connman_bool_t prepare_network(struct connman_service *service)
1162 enum connman_network_type type;
1163 unsigned int ssid_len;
1165 type = connman_network_get_type(service->network);
1168 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1169 case CONNMAN_NETWORK_TYPE_VENDOR:
1171 case CONNMAN_NETWORK_TYPE_WIFI:
1172 if (connman_network_get_blob(service->network, "WiFi.SSID",
1176 connman_network_set_string(service->network,
1177 "WiFi.Passphrase", service->passphrase);
1179 case CONNMAN_NETWORK_TYPE_WIMAX:
1180 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1181 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1182 case CONNMAN_NETWORK_TYPE_MBM:
1183 case CONNMAN_NETWORK_TYPE_HSO:
1190 int __connman_service_connect(struct connman_service *service)
1194 DBG("service %p", service);
1196 if (service->state == CONNMAN_SERVICE_STATE_READY)
1199 if (is_connecting(service) == TRUE)
1202 if (service->network != NULL) {
1203 if (prepare_network(service) == FALSE)
1206 err = __connman_network_connect(service->network);
1207 } else if (service->device != NULL) {
1208 if (service->favorite == FALSE)
1211 err = __connman_device_connect(service->device);
1216 if (err != -EINPROGRESS)
1219 service->timeout = g_timeout_add_seconds(45,
1220 connect_timeout, service);
1222 return -EINPROGRESS;
1228 int __connman_service_disconnect(struct connman_service *service)
1232 DBG("service %p", service);
1234 reply_pending(service, ECONNABORTED);
1236 if (service->network != NULL) {
1237 err = __connman_network_disconnect(service->network);
1238 } else if (service->device != NULL) {
1239 if (service->favorite == FALSE)
1241 err = __connman_device_disconnect(service->device);
1246 if (err != -EINPROGRESS)
1249 return -EINPROGRESS;
1256 * __connman_service_lookup:
1257 * @identifier: service identifier
1259 * Look up a service by identifier (reference count will not be increased)
1261 static struct connman_service *__connman_service_lookup(const char *identifier)
1263 GSequenceIter *iter;
1265 iter = g_hash_table_lookup(service_hash, identifier);
1267 return g_sequence_get(iter);
1272 static struct connman_network *create_hidden_wifi(struct connman_device *device,
1273 const char *ssid, const char *mode, const char *security)
1275 struct connman_network *network;
1278 unsigned int i, ssid_len;
1280 ssid_len = strlen(ssid);
1284 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
1285 if (network == NULL)
1288 connman_network_set_blob(network, "WiFi.SSID",
1289 (unsigned char *) ssid, ssid_len);
1291 connman_network_set_string(network, "WiFi.Mode", mode);
1292 connman_network_set_string(network, "WiFi.Security", security);
1294 name = g_try_malloc0(ssid_len + 1);
1296 connman_network_unref(network);
1300 for (i = 0; i < ssid_len; i++) {
1301 if (g_ascii_isprint(ssid[i]))
1307 connman_network_set_name(network, name);
1311 index = connman_device_get_index(device);
1312 connman_network_set_index(network, index);
1314 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
1316 if (connman_device_add_network(device, network) < 0) {
1317 connman_network_unref(network);
1321 connman_network_set_available(network, TRUE);
1326 int __connman_service_create_and_connect(DBusMessage *msg)
1328 struct connman_service *service;
1329 struct connman_network *network;
1330 struct connman_device *device;
1331 DBusMessageIter iter, array;
1332 const char *mode = "managed", *security = "none";
1333 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
1334 unsigned int ssid_len = 0;
1337 gboolean created = FALSE;
1340 dbus_message_iter_init(msg, &iter);
1341 dbus_message_iter_recurse(&iter, &array);
1343 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1344 DBusMessageIter entry, value;
1347 dbus_message_iter_recurse(&array, &entry);
1348 dbus_message_iter_get_basic(&entry, &key);
1350 dbus_message_iter_next(&entry);
1351 dbus_message_iter_recurse(&entry, &value);
1353 switch (dbus_message_iter_get_arg_type(&value)) {
1354 case DBUS_TYPE_STRING:
1355 if (g_str_equal(key, "Type") == TRUE)
1356 dbus_message_iter_get_basic(&value, &type);
1357 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
1358 g_str_equal(key, "Mode") == TRUE)
1359 dbus_message_iter_get_basic(&value, &mode);
1360 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
1361 g_str_equal(key, "Security") == TRUE)
1362 dbus_message_iter_get_basic(&value, &security);
1363 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
1364 g_str_equal(key, "Passphrase") == TRUE)
1365 dbus_message_iter_get_basic(&value, &passphrase);
1366 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
1367 g_str_equal(key, "SSID") == TRUE)
1368 dbus_message_iter_get_basic(&value, &ssid);
1371 dbus_message_iter_next(&array);
1377 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
1383 ssid_len = strlen(ssid);
1387 if (g_strcmp0(security, "none") != 0 &&
1388 g_strcmp0(mode, "wep") != 0 &&
1389 g_strcmp0(mode, "wpa") != 0 &&
1390 g_strcmp0(mode, "rsn") != 0)
1393 device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
1397 ident = __connman_device_get_ident(device);
1401 group = connman_wifi_build_group_name((unsigned char *) ssid,
1402 ssid_len, mode, security);
1406 name = g_strdup_printf("%s_%s_%s", type, ident, group);
1408 service = __connman_service_lookup(name);
1410 if (service != NULL)
1413 network = create_hidden_wifi(device, ssid, mode, security);
1414 if (network != NULL) {
1415 connman_network_set_group(network, group);
1419 service = __connman_service_lookup(name);
1425 if (service == NULL) {
1430 __connman_device_disconnect(device);
1432 if (passphrase != NULL) {
1433 g_free(service->passphrase);
1434 service->passphrase = g_strdup(passphrase);
1437 err = __connman_service_connect(service);
1438 if (err < 0 && err != -EINPROGRESS)
1441 g_dbus_send_reply(connection, msg,
1442 DBUS_TYPE_OBJECT_PATH, &service->path,
1448 if (service != NULL && created == TRUE) {
1449 struct connman_network *network = service->network;
1451 if (network != NULL) {
1452 connman_network_set_available(network, FALSE);
1453 __connman_device_cleanup_networks(device);
1456 __connman_service_put(service);
1463 * __connman_service_get:
1464 * @identifier: service identifier
1466 * Look up a service by identifier or create a new one if not found
1468 static struct connman_service *__connman_service_get(const char *identifier)
1470 struct connman_service *service;
1471 GSequenceIter *iter;
1473 iter = g_hash_table_lookup(service_hash, identifier);
1475 service = g_sequence_get(iter);
1476 if (service != NULL)
1477 g_atomic_int_inc(&service->refcount);
1481 service = connman_service_create();
1482 if (service == NULL)
1485 DBG("service %p", service);
1487 service->identifier = g_strdup(identifier);
1489 service->profile = g_strdup(__connman_profile_active_ident());
1491 __connman_storage_load_service(service);
1493 iter = g_sequence_insert_sorted(service_list, service,
1494 service_compare, NULL);
1496 g_hash_table_insert(service_hash, service->identifier, iter);
1501 static int service_register(struct connman_service *service)
1503 const char *path = __connman_profile_active_path();
1504 GSequenceIter *iter;
1506 DBG("service %p", service);
1508 if (service->path != NULL)
1511 service->path = g_strdup_printf("%s/%s", path, service->identifier);
1513 DBG("path %s", service->path);
1515 g_dbus_register_interface(connection, service->path,
1516 CONNMAN_SERVICE_INTERFACE,
1517 service_methods, service_signals,
1518 NULL, service, NULL);
1520 __connman_storage_load_service(service);
1522 iter = g_hash_table_lookup(service_hash, service->identifier);
1524 g_sequence_sort_changed(iter, service_compare, NULL);
1526 __connman_profile_changed(TRUE);
1532 * __connman_service_lookup_from_device:
1533 * @device: device structure
1535 * Look up a service by device (reference count will not be increased)
1537 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
1539 struct connman_service *service;
1543 ident = __connman_device_get_ident(device);
1547 name = g_strdup_printf("%s_%s",
1548 __connman_device_get_type(device), ident);
1549 service = __connman_service_lookup(name);
1556 * __connman_service_create_from_device:
1557 * @device: device structure
1559 * Look up service by device and if not found, create one
1561 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
1563 struct connman_service *service;
1567 ident = __connman_device_get_ident(device);
1571 name = g_strdup_printf("%s_%s",
1572 __connman_device_get_type(device), ident);
1573 service = __connman_service_get(name);
1576 if (service == NULL)
1579 if (service->path != NULL) {
1580 __connman_profile_changed(TRUE);
1584 service->type = __connman_device_get_service_type(device);
1586 service->device = device;
1588 service_register(service);
1590 __connman_profile_changed(TRUE);
1592 if (service->favorite == TRUE)
1593 __connman_service_auto_connect();
1598 void __connman_service_remove_from_device(struct connman_device *device)
1600 struct connman_service *service;
1601 enum connman_service_type type;
1603 service = __connman_service_lookup_from_device(device);
1604 if (service == NULL)
1607 type = service->type;
1609 __connman_service_put(service);
1613 __connman_notifier_disconnect(type);
1615 __connman_service_auto_connect();
1619 * __connman_service_lookup_from_network:
1620 * @network: network structure
1622 * Look up a service by network (reference count will not be increased)
1624 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
1626 struct connman_service *service;
1627 const char *ident, *group;
1630 ident = __connman_network_get_ident(network);
1634 group = connman_network_get_group(network);
1638 name = g_strdup_printf("%s_%s_%s",
1639 __connman_network_get_type(network), ident, group);
1640 service = __connman_service_lookup(name);
1646 unsigned int __connman_service_get_order(struct connman_service *service)
1648 GSequenceIter *iter;
1650 if (service == NULL)
1653 if (service->favorite == FALSE) {
1658 iter = g_hash_table_lookup(service_hash, service->identifier);
1660 if (g_sequence_iter_get_position(iter) == 0)
1667 return service->order;
1670 static enum connman_service_type convert_network_type(struct connman_network *network)
1672 enum connman_network_type type = connman_network_get_type(network);
1675 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1676 case CONNMAN_NETWORK_TYPE_VENDOR:
1677 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1678 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1680 case CONNMAN_NETWORK_TYPE_MBM:
1681 case CONNMAN_NETWORK_TYPE_HSO:
1682 return CONNMAN_SERVICE_TYPE_CELLULAR;
1683 case CONNMAN_NETWORK_TYPE_WIFI:
1684 return CONNMAN_SERVICE_TYPE_WIFI;
1685 case CONNMAN_NETWORK_TYPE_WIMAX:
1686 return CONNMAN_SERVICE_TYPE_WIMAX;
1689 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1692 static enum connman_service_mode convert_wifi_mode(const char *mode)
1695 return CONNMAN_SERVICE_MODE_UNKNOWN;
1696 else if (g_str_equal(mode, "managed") == TRUE)
1697 return CONNMAN_SERVICE_MODE_MANAGED;
1698 else if (g_str_equal(mode, "adhoc") == TRUE)
1699 return CONNMAN_SERVICE_MODE_ADHOC;
1701 return CONNMAN_SERVICE_MODE_UNKNOWN;
1704 static enum connman_service_mode convert_wifi_security(const char *security)
1706 if (security == NULL)
1707 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1708 else if (g_str_equal(security, "none") == TRUE)
1709 return CONNMAN_SERVICE_SECURITY_NONE;
1710 else if (g_str_equal(security, "wep") == TRUE)
1711 return CONNMAN_SERVICE_SECURITY_WEP;
1712 else if (g_str_equal(security, "wpa") == TRUE)
1713 return CONNMAN_SERVICE_SECURITY_WPA;
1714 else if (g_str_equal(security, "rsn") == TRUE)
1715 return CONNMAN_SERVICE_SECURITY_RSN;
1717 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1720 static void update_from_network(struct connman_service *service,
1721 struct connman_network *network)
1723 connman_uint8_t strength = service->strength;
1724 GSequenceIter *iter;
1727 if (service->state == CONNMAN_SERVICE_STATE_READY)
1730 if (is_connecting(service) == TRUE)
1733 str = connman_network_get_string(network, "Name");
1735 g_free(service->name);
1736 service->name = g_strdup(str);
1737 service->hidden = FALSE;
1739 g_free(service->name);
1740 service->name = NULL;
1741 service->hidden = TRUE;
1744 service->strength = connman_network_get_uint8(network, "Strength");
1746 str = connman_network_get_string(network, "WiFi.Mode");
1747 service->mode = convert_wifi_mode(str);
1749 str = connman_network_get_string(network, "WiFi.Security");
1750 service->security = convert_wifi_security(str);
1752 if (service->strength > strength && service->network != NULL) {
1753 connman_network_unref(service->network);
1754 service->network = connman_network_ref(network);
1756 strength_changed(service);
1759 if (service->network == NULL)
1760 service->network = connman_network_ref(network);
1762 iter = g_hash_table_lookup(service_hash, service->identifier);
1764 g_sequence_sort_changed(iter, service_compare, NULL);
1768 * __connman_service_create_from_network:
1769 * @network: network structure
1771 * Look up service by network and if not found, create one
1773 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
1775 struct connman_service *service;
1776 const char *ident, *group;
1779 ident = __connman_network_get_ident(network);
1783 group = connman_network_get_group(network);
1787 name = g_strdup_printf("%s_%s_%s",
1788 __connman_network_get_type(network), ident, group);
1789 service = __connman_service_get(name);
1792 if (service == NULL)
1795 if (__connman_network_get_weakness(network) == TRUE)
1798 if (service->path != NULL) {
1799 update_from_network(service, network);
1800 __connman_profile_changed(TRUE);
1804 service->type = convert_network_type(network);
1806 service->state = CONNMAN_SERVICE_STATE_IDLE;
1808 update_from_network(service, network);
1810 service_register(service);
1812 __connman_profile_changed(TRUE);
1814 if (service->favorite == TRUE)
1815 __connman_service_auto_connect();
1820 void __connman_service_update_from_network(struct connman_network *network)
1822 struct connman_service *service;
1823 connman_uint8_t strength;
1825 service = __connman_service_lookup_from_network(network);
1826 if (service == NULL)
1829 if (service->network == NULL)
1832 strength = connman_network_get_uint8(service->network, "Strength");
1833 if (strength == service->strength)
1836 service->strength = strength;
1838 strength_changed(service);
1841 void __connman_service_remove_from_network(struct connman_network *network)
1843 struct connman_service *service;
1845 service = __connman_service_lookup_from_network(network);
1846 if (service == NULL)
1849 __connman_service_put(service);
1852 static int service_load(struct connman_service *service)
1855 gchar *pathname, *data = NULL;
1858 unsigned int ssid_len;
1861 DBG("service %p", service);
1863 if (service->profile == NULL)
1866 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1867 if (pathname == NULL)
1870 keyfile = g_key_file_new();
1872 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1879 if (g_key_file_load_from_data(keyfile, data, length,
1880 0, NULL) == FALSE) {
1887 switch (service->type) {
1888 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1889 case CONNMAN_SERVICE_TYPE_ETHERNET:
1891 case CONNMAN_SERVICE_TYPE_WIFI:
1892 if (service->name == NULL) {
1895 name = g_key_file_get_string(keyfile,
1896 service->identifier, "Name", NULL);
1898 g_free(service->name);
1899 service->name = name;
1902 if (service->network != NULL)
1903 connman_network_set_name(service->network,
1907 if (service->network &&
1908 connman_network_get_blob(service->network,
1909 "WiFi.SSID", &ssid_len) == NULL) {
1912 hex_ssid = g_key_file_get_string(keyfile,
1913 service->identifier,
1916 if (hex_ssid != NULL) {
1918 unsigned int i, j = 0, hex;
1919 size_t hex_ssid_len = strlen(hex_ssid);
1921 ssid = g_try_malloc0(hex_ssid_len / 2);
1928 for (i = 0; i < hex_ssid_len; i += 2) {
1929 sscanf(hex_ssid + i, "%02x", &hex);
1933 connman_network_set_blob(service->network,
1934 "WiFi.SSID", ssid, hex_ssid_len / 2);
1941 case CONNMAN_SERVICE_TYPE_WIMAX:
1942 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1943 case CONNMAN_SERVICE_TYPE_CELLULAR:
1944 service->favorite = g_key_file_get_boolean(keyfile,
1945 service->identifier, "Favorite", NULL);
1947 str = g_key_file_get_string(keyfile,
1948 service->identifier, "Failure", NULL);
1950 service->state = CONNMAN_SERVICE_STATE_FAILURE;
1951 service->error = string2error(str);
1956 str = g_key_file_get_string(keyfile,
1957 service->identifier, "Modified", NULL);
1959 g_time_val_from_iso8601(str, &service->modified);
1963 str = g_key_file_get_string(keyfile,
1964 service->identifier, "Passphrase", NULL);
1966 g_free(service->passphrase);
1967 service->passphrase = str;
1970 __connman_ipconfig_load(service->ipconfig, keyfile,
1971 service->identifier, "IPv4.");
1974 g_key_file_free(keyfile);
1979 static int service_save(struct connman_service *service)
1982 gchar *pathname, *data = NULL;
1987 DBG("service %p", service);
1989 if (service->profile == NULL)
1992 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1993 if (pathname == NULL)
1996 keyfile = g_key_file_new();
1998 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
2002 if (g_key_file_load_from_data(keyfile, data, length,
2010 if (service->name != NULL)
2011 g_key_file_set_string(keyfile, service->identifier,
2012 "Name", service->name);
2014 switch (service->type) {
2015 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2016 case CONNMAN_SERVICE_TYPE_ETHERNET:
2018 case CONNMAN_SERVICE_TYPE_WIFI:
2019 if (service->network) {
2020 const unsigned char *ssid;
2021 unsigned int ssid_len = 0;
2023 ssid = connman_network_get_blob(service->network,
2024 "WiFi.SSID", &ssid_len);
2026 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
2027 char *identifier = service->identifier;
2031 str = g_string_sized_new(ssid_len * 2);
2037 for (i = 0; i < ssid_len; i++)
2038 g_string_append_printf(str,
2041 g_key_file_set_string(keyfile, identifier,
2044 g_string_free(str, TRUE);
2049 case CONNMAN_SERVICE_TYPE_WIMAX:
2050 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2051 case CONNMAN_SERVICE_TYPE_CELLULAR:
2052 g_key_file_set_boolean(keyfile, service->identifier,
2053 "Favorite", service->favorite);
2055 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
2056 const char *failure = error2string(service->error);
2057 if (failure != NULL)
2058 g_key_file_set_string(keyfile,
2059 service->identifier,
2060 "Failure", failure);
2062 g_key_file_remove_key(keyfile, service->identifier,
2068 str = g_time_val_to_iso8601(&service->modified);
2070 g_key_file_set_string(keyfile, service->identifier,
2075 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
2076 g_key_file_set_string(keyfile, service->identifier,
2077 "Passphrase", service->passphrase);
2079 g_key_file_remove_key(keyfile, service->identifier,
2080 "Passphrase", NULL);
2082 __connman_ipconfig_save(service->ipconfig, keyfile,
2083 service->identifier, "IPv4.");
2085 data = g_key_file_to_data(keyfile, &length, NULL);
2087 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
2088 connman_error("Failed to store service information");
2093 g_key_file_free(keyfile);
2100 static struct connman_storage service_storage = {
2102 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
2103 .service_load = service_load,
2104 .service_save = service_save,
2107 int __connman_service_init(void)
2111 connection = connman_dbus_get_connection();
2113 if (connman_storage_register(&service_storage) < 0)
2114 connman_error("Failed to register service storage");
2116 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2119 service_list = g_sequence_new(service_free);
2124 void __connman_service_cleanup(void)
2128 g_sequence_free(service_list);
2129 service_list = NULL;
2131 g_hash_table_destroy(service_hash);
2132 service_hash = NULL;
2134 connman_storage_unregister(&service_storage);
2136 dbus_connection_unref(connection);