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;
49 struct connman_device *device;
50 struct connman_network *network;
55 static void append_path(gpointer value, gpointer user_data)
57 struct connman_service *service = value;
58 DBusMessageIter *iter = user_data;
60 if (service->path == NULL)
63 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
67 void __connman_service_list(DBusMessageIter *iter)
71 g_sequence_foreach(service_list, append_path, iter);
74 static const char *type2string(enum connman_service_type type)
77 case CONNMAN_SERVICE_TYPE_UNKNOWN:
79 case CONNMAN_SERVICE_TYPE_ETHERNET:
81 case CONNMAN_SERVICE_TYPE_WIFI:
83 case CONNMAN_SERVICE_TYPE_WIMAX:
90 static const char *mode2string(enum connman_service_mode mode)
93 case CONNMAN_SERVICE_MODE_UNKNOWN:
95 case CONNMAN_SERVICE_MODE_MANAGED:
97 case CONNMAN_SERVICE_MODE_ADHOC:
104 static const char *security2string(enum connman_service_security security)
107 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
109 case CONNMAN_SERVICE_SECURITY_NONE:
111 case CONNMAN_SERVICE_SECURITY_WEP:
113 case CONNMAN_SERVICE_SECURITY_WPA:
115 case CONNMAN_SERVICE_SECURITY_WPA2:
122 static const char *state2string(enum connman_service_state state)
125 case CONNMAN_SERVICE_STATE_UNKNOWN:
127 case CONNMAN_SERVICE_STATE_IDLE:
129 case CONNMAN_SERVICE_STATE_CARRIER:
131 case CONNMAN_SERVICE_STATE_ASSOCIATION:
132 return "association";
133 case CONNMAN_SERVICE_STATE_CONFIGURATION:
134 return "configuration";
135 case CONNMAN_SERVICE_STATE_READY:
137 case CONNMAN_SERVICE_STATE_DISCONNECT:
139 case CONNMAN_SERVICE_STATE_FAILURE:
146 static void state_changed(struct connman_service *service)
149 DBusMessageIter entry, value;
150 const char *str, *key = "State";
152 if (service->path == NULL)
155 str = state2string(service->state);
159 signal = dbus_message_new_signal(service->path,
160 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
164 dbus_message_iter_init_append(signal, &entry);
166 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
168 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
169 DBUS_TYPE_STRING_AS_STRING, &value);
170 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
171 dbus_message_iter_close_container(&entry, &value);
173 g_dbus_send_message(connection, signal);
176 static DBusMessage *get_properties(DBusConnection *conn,
177 DBusMessage *msg, void *user_data)
179 struct connman_service *service = user_data;
181 DBusMessageIter array, dict;
184 DBG("service %p", service);
186 reply = dbus_message_new_method_return(msg);
190 dbus_message_iter_init_append(reply, &array);
192 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
193 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
194 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
195 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
197 str = type2string(service->type);
199 connman_dbus_dict_append_variant(&dict, "Type",
200 DBUS_TYPE_STRING, &str);
202 str = mode2string(service->mode);
204 connman_dbus_dict_append_variant(&dict, "Mode",
205 DBUS_TYPE_STRING, &str);
207 str = security2string(service->security);
209 connman_dbus_dict_append_variant(&dict, "Security",
210 DBUS_TYPE_STRING, &str);
212 str = state2string(service->state);
214 connman_dbus_dict_append_variant(&dict, "State",
215 DBUS_TYPE_STRING, &str);
217 if (service->strength > 0)
218 connman_dbus_dict_append_variant(&dict, "Strength",
219 DBUS_TYPE_BYTE, &service->strength);
221 connman_dbus_dict_append_variant(&dict, "Favorite",
222 DBUS_TYPE_BOOLEAN, &service->favorite);
224 if (service->name != NULL)
225 connman_dbus_dict_append_variant(&dict, "Name",
226 DBUS_TYPE_STRING, &service->name);
228 if (service->passphrase != NULL &&
229 __connman_security_check_privilege(msg,
230 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
231 connman_dbus_dict_append_variant(&dict, "Passphrase",
232 DBUS_TYPE_STRING, &service->passphrase);
234 dbus_message_iter_close_container(&array, &dict);
239 static DBusMessage *set_property(DBusConnection *conn,
240 DBusMessage *msg, void *user_data)
242 struct connman_service *service = user_data;
243 DBusMessageIter iter, value;
247 DBG("service %p", service);
249 if (dbus_message_iter_init(msg, &iter) == FALSE)
250 return __connman_error_invalid_arguments(msg);
252 dbus_message_iter_get_basic(&iter, &name);
253 dbus_message_iter_next(&iter);
254 dbus_message_iter_recurse(&iter, &value);
256 if (__connman_security_check_privilege(msg,
257 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
258 return __connman_error_permission_denied(msg);
260 type = dbus_message_iter_get_arg_type(&value);
262 if (g_str_equal(name, "Passphrase") == TRUE) {
263 const char *passphrase;
265 if (type != DBUS_TYPE_STRING)
266 return __connman_error_invalid_arguments(msg);
268 if (__connman_security_check_privilege(msg,
269 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
270 return __connman_error_permission_denied(msg);
272 dbus_message_iter_get_basic(&value, &passphrase);
274 g_free(service->passphrase);
275 service->passphrase = g_strdup(passphrase);
277 if (service->network != NULL)
278 connman_network_set_string(service->network,
279 "WiFi.Passphrase", service->passphrase);
281 __connman_storage_save_service(service);
284 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
287 static gboolean connect_timeout(gpointer user_data)
289 struct connman_service *service = user_data;
291 DBG("service %p", service);
293 service->timeout = 0;
295 if (service->network != NULL)
296 __connman_network_disconnect(service->network);
298 if (service->pending != NULL) {
301 reply = __connman_error_operation_timeout(service->pending);
303 g_dbus_send_message(connection, reply);
305 dbus_message_unref(service->pending);
306 service->pending = NULL;
308 __connman_service_indicate_state(service,
309 CONNMAN_SERVICE_STATE_FAILURE);
315 static DBusMessage *connect_service(DBusConnection *conn,
316 DBusMessage *msg, void *user_data)
318 struct connman_service *service = user_data;
320 DBG("service %p", service);
322 if (service->pending != NULL)
323 return __connman_error_in_progress(msg);
325 if (service->state == CONNMAN_SERVICE_STATE_READY)
326 return __connman_error_already_connected(msg);
328 if (service->network != NULL) {
331 connman_network_set_string(service->network,
332 "WiFi.Passphrase", service->passphrase);
334 err = connman_network_connect(service->network);
335 if (err < 0 && err != -EINPROGRESS)
336 return __connman_error_failed(msg, -err);
338 service->pending = dbus_message_ref(msg);
340 service->timeout = g_timeout_add_seconds(45,
341 connect_timeout, service);
344 } else if (service->device != NULL) {
345 if (service->favorite == FALSE)
346 return __connman_error_no_carrier(msg);
348 if (__connman_device_connect(service->device) < 0)
349 return __connman_error_failed(msg, EINVAL);
351 service->pending = dbus_message_ref(msg);
352 service->timeout = g_timeout_add_seconds(15,
353 connect_timeout, service);
358 return __connman_error_not_supported(msg);
361 static DBusMessage *disconnect_service(DBusConnection *conn,
362 DBusMessage *msg, void *user_data)
364 struct connman_service *service = user_data;
366 DBG("service %p", service);
368 if (service->pending != NULL) {
371 reply = __connman_error_operation_aborted(service->pending);
373 g_dbus_send_message(conn, reply);
375 dbus_message_unref(service->pending);
376 service->pending = NULL;
378 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
381 if (service->network != NULL) {
384 err = __connman_network_disconnect(service->network);
385 if (err < 0 && err != -EINPROGRESS)
386 return __connman_error_failed(msg, -err);
388 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
389 } else if (service->device != NULL) {
392 if (service->favorite == FALSE)
393 return __connman_error_no_carrier(msg);
395 err = __connman_device_disconnect(service->device);
397 return __connman_error_failed(msg, -err);
399 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
402 return __connman_error_not_supported(msg);
405 static DBusMessage *remove_service(DBusConnection *conn,
406 DBusMessage *msg, void *user_data)
408 struct connman_service *service = user_data;
410 DBG("service %p", service);
412 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
413 return __connman_error_not_supported(msg);
415 if (service->favorite == FALSE)
416 return __connman_error_not_supported(msg);
418 if (service->network != NULL) {
419 struct connman_device *device;
421 device = connman_network_get_device(service->network);
423 __connman_device_disconnect(device);
425 __connman_network_disconnect(service->network);
428 connman_service_set_favorite(service, FALSE);
429 __connman_storage_save_service(service);
431 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
434 static DBusMessage *move_before(DBusConnection *conn,
435 DBusMessage *msg, void *user_data)
437 struct connman_service *service = user_data;
439 DBG("service %p", service);
441 if (service->favorite == FALSE)
442 return __connman_error_not_supported(msg);
444 return __connman_error_not_implemented(msg);
447 static DBusMessage *move_after(DBusConnection *conn,
448 DBusMessage *msg, void *user_data)
450 struct connman_service *service = user_data;
452 DBG("service %p", service);
454 if (service->favorite == FALSE)
455 return __connman_error_not_supported(msg);
457 return __connman_error_not_implemented(msg);
460 static GDBusMethodTable service_methods[] = {
461 { "GetProperties", "", "a{sv}", get_properties },
462 { "SetProperty", "sv", "", set_property },
463 { "Connect", "", "", connect_service,
464 G_DBUS_METHOD_FLAG_ASYNC },
465 { "Disconnect", "", "", disconnect_service },
466 { "Remove", "", "", remove_service },
467 { "MoveBefore", "o", "", move_before },
468 { "MoveAfter", "o", "", move_after },
472 static GDBusSignalTable service_signals[] = {
473 { "PropertyChanged", "sv" },
477 static void service_free(gpointer user_data)
479 struct connman_service *service = user_data;
480 char *path = service->path;
482 DBG("service %p", service);
484 g_hash_table_remove(service_hash, service->identifier);
486 if (service->timeout > 0)
487 g_source_remove(service->timeout);
489 if (service->pending != NULL) {
490 dbus_message_unref(service->pending);
491 service->pending = NULL;
494 service->path = NULL;
497 __connman_profile_changed();
499 g_dbus_unregister_interface(connection, path,
500 CONNMAN_SERVICE_INTERFACE);
504 if (service->network != NULL)
505 connman_network_unref(service->network);
507 g_free(service->profile);
508 g_free(service->name);
509 g_free(service->passphrase);
510 g_free(service->identifier);
515 * __connman_service_put:
516 * @service: service structure
518 * Release service if no longer needed
520 void __connman_service_put(struct connman_service *service)
522 DBG("service %p", service);
524 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
527 iter = g_hash_table_lookup(service_hash, service->identifier);
529 g_sequence_remove(iter);
531 service_free(service);
535 static void __connman_service_initialize(struct connman_service *service)
537 DBG("service %p", service);
539 service->refcount = 1;
541 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
542 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
543 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
544 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
546 service->favorite = FALSE;
552 * connman_service_create:
554 * Allocate a new service.
556 * Returns: a newly-allocated #connman_service structure
558 struct connman_service *connman_service_create(void)
560 struct connman_service *service;
562 service = g_try_new0(struct connman_service, 1);
566 DBG("service %p", service);
568 __connman_service_initialize(service);
574 * connman_service_ref:
575 * @service: service structure
577 * Increase reference counter of service
579 struct connman_service *connman_service_ref(struct connman_service *service)
581 g_atomic_int_inc(&service->refcount);
587 * connman_service_unref:
588 * @service: service structure
590 * Decrease reference counter of service
592 void connman_service_unref(struct connman_service *service)
594 __connman_service_put(service);
597 static gint service_compare(gconstpointer a, gconstpointer b,
600 struct connman_service *service_a = (void *) a;
601 struct connman_service *service_b = (void *) b;
603 if (service_a->state != service_b->state) {
604 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
606 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
610 if (service_a->order > service_b->order)
613 if (service_a->order < service_b->order)
616 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
619 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
622 return (gint) service_b->strength - (gint) service_a->strength;
626 * connman_service_set_favorite:
627 * @service: service structure
628 * @favorite: favorite value
630 * Change the favorite setting of service
632 int connman_service_set_favorite(struct connman_service *service,
633 connman_bool_t favorite)
637 iter = g_hash_table_lookup(service_hash, service->identifier);
641 if (service->favorite == favorite)
644 service->favorite = favorite;
646 g_sequence_sort_changed(iter, service_compare, NULL);
648 __connman_profile_changed();
653 int __connman_service_set_carrier(struct connman_service *service,
654 connman_bool_t carrier)
656 DBG("service %p carrier %d", service, carrier);
661 switch (service->type) {
662 case CONNMAN_SERVICE_TYPE_UNKNOWN:
663 case CONNMAN_SERVICE_TYPE_WIFI:
664 case CONNMAN_SERVICE_TYPE_WIMAX:
666 case CONNMAN_SERVICE_TYPE_ETHERNET:
670 if (carrier == FALSE) {
671 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
672 state_changed(service);
674 service->state = CONNMAN_SERVICE_STATE_IDLE;
675 state_changed(service);
677 service->state = CONNMAN_SERVICE_STATE_CARRIER;
678 state_changed(service);
681 return connman_service_set_favorite(service, carrier);
684 int __connman_service_indicate_state(struct connman_service *service,
685 enum connman_service_state state)
689 DBG("service %p state %d", service, state);
694 if (state == CONNMAN_SERVICE_STATE_CARRIER)
695 return __connman_service_set_carrier(service, TRUE);
697 if (service->state == state)
700 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
701 state == CONNMAN_SERVICE_STATE_DISCONNECT)
704 if (state == CONNMAN_SERVICE_STATE_IDLE &&
705 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
706 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
707 state_changed(service);
710 service->state = state;
711 state_changed(service);
713 if (state == CONNMAN_SERVICE_STATE_READY) {
714 connman_service_set_favorite(service, TRUE);
715 __connman_storage_save_service(service);
717 if (service->timeout > 0)
718 g_source_remove(service->timeout);
720 if (service->pending != NULL) {
721 g_dbus_send_reply(connection, service->pending,
724 dbus_message_unref(service->pending);
725 service->pending = NULL;
729 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
730 if (service->timeout > 0)
731 g_source_remove(service->timeout);
733 if (service->pending != NULL) {
736 reply = __connman_error_failed(service->pending, EIO);
738 g_dbus_send_message(connection, reply);
740 dbus_message_unref(service->pending);
741 service->pending = NULL;
744 service->state = CONNMAN_SERVICE_STATE_IDLE;
745 state_changed(service);
748 iter = g_hash_table_lookup(service_hash, service->identifier);
750 g_sequence_sort_changed(iter, service_compare, NULL);
752 __connman_profile_changed();
758 * __connman_service_lookup:
759 * @identifier: service identifier
761 * Look up a service by identifier (reference count will not be increased)
763 static struct connman_service *__connman_service_lookup(const char *identifier)
767 iter = g_hash_table_lookup(service_hash, identifier);
769 return g_sequence_get(iter);
775 * __connman_service_get:
776 * @identifier: service identifier
778 * Look up a service by identifier or create a new one if not found
780 static struct connman_service *__connman_service_get(const char *identifier)
782 struct connman_service *service;
785 iter = g_hash_table_lookup(service_hash, identifier);
787 service = g_sequence_get(iter);
789 g_atomic_int_inc(&service->refcount);
793 service = g_try_new0(struct connman_service, 1);
797 DBG("service %p", service);
799 __connman_service_initialize(service);
801 service->identifier = g_strdup(identifier);
803 service->profile = g_strdup(__connman_profile_active_ident());
805 __connman_storage_load_service(service);
807 iter = g_sequence_insert_sorted(service_list, service,
808 service_compare, NULL);
810 g_hash_table_insert(service_hash, service->identifier, iter);
815 static int service_register(struct connman_service *service)
817 const char *path = __connman_profile_active_path();
820 DBG("service %p", service);
822 if (service->path != NULL)
825 service->path = g_strdup_printf("%s/%s", path, service->identifier);
827 DBG("path %s", service->path);
829 g_dbus_register_interface(connection, service->path,
830 CONNMAN_SERVICE_INTERFACE,
831 service_methods, service_signals,
832 NULL, service, NULL);
834 __connman_storage_load_service(service);
836 iter = g_hash_table_lookup(service_hash, service->identifier);
838 g_sequence_sort_changed(iter, service_compare, NULL);
840 __connman_profile_changed();
846 * connman_service_lookup_from_device:
847 * @device: device structure
849 * Look up a service by device (reference count will not be increased)
851 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
853 struct connman_service *service;
857 ident = __connman_device_get_ident(device);
861 name = g_strdup_printf("%s_%s",
862 __connman_device_get_type(device), ident);
864 service = __connman_service_lookup(name);
871 static enum connman_service_type convert_device_type(struct connman_device *device)
873 enum connman_device_type type = connman_device_get_type(device);
876 case CONNMAN_DEVICE_TYPE_UNKNOWN:
877 case CONNMAN_DEVICE_TYPE_VENDOR:
878 case CONNMAN_DEVICE_TYPE_WIFI:
879 case CONNMAN_DEVICE_TYPE_WIMAX:
880 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
881 case CONNMAN_DEVICE_TYPE_GPS:
882 case CONNMAN_DEVICE_TYPE_HSO:
883 case CONNMAN_DEVICE_TYPE_NOZOMI:
884 case CONNMAN_DEVICE_TYPE_HUAWEI:
885 case CONNMAN_DEVICE_TYPE_NOVATEL:
887 case CONNMAN_DEVICE_TYPE_ETHERNET:
888 return CONNMAN_SERVICE_TYPE_ETHERNET;
891 return CONNMAN_SERVICE_TYPE_UNKNOWN;
895 * connman_service_create_from_device:
896 * @device: device structure
898 * Look up service by device and if not found, create one
900 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
902 struct connman_service *service;
906 ident = __connman_device_get_ident(device);
910 name = g_strdup_printf("%s_%s",
911 __connman_device_get_type(device), ident);
913 service = __connman_service_get(name);
917 if (service->path != NULL) {
918 __connman_service_put(service);
923 service->type = convert_device_type(device);
925 service->device = device;
927 service_register(service);
936 * connman_service_lookup_from_network:
937 * @network: network structure
939 * Look up a service by network (reference count will not be increased)
941 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
943 struct connman_service *service;
944 const char *ident, *group;
947 ident = __connman_network_get_ident(network);
951 group = __connman_network_get_group(network);
955 name = g_strdup_printf("%s_%s_%s",
956 __connman_network_get_type(network), ident, group);
958 service = __connman_service_lookup(name);
965 static enum connman_service_type convert_network_type(struct connman_network *network)
967 enum connman_network_type type = connman_network_get_type(network);
970 case CONNMAN_NETWORK_TYPE_UNKNOWN:
971 case CONNMAN_NETWORK_TYPE_VENDOR:
972 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
973 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
974 case CONNMAN_NETWORK_TYPE_HSO:
976 case CONNMAN_NETWORK_TYPE_WIFI:
977 return CONNMAN_SERVICE_TYPE_WIFI;
978 case CONNMAN_NETWORK_TYPE_WIMAX:
979 return CONNMAN_SERVICE_TYPE_WIMAX;
982 return CONNMAN_SERVICE_TYPE_UNKNOWN;
985 static enum connman_service_mode convert_wifi_mode(const char *mode)
988 return CONNMAN_SERVICE_MODE_UNKNOWN;
989 else if (g_str_equal(mode, "managed") == TRUE)
990 return CONNMAN_SERVICE_MODE_MANAGED;
991 else if (g_str_equal(mode, "adhoc") == TRUE)
992 return CONNMAN_SERVICE_MODE_ADHOC;
994 return CONNMAN_SERVICE_MODE_UNKNOWN;
997 static enum connman_service_mode convert_wifi_security(const char *security)
999 if (security == NULL)
1000 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1001 else if (g_str_equal(security, "none") == TRUE)
1002 return CONNMAN_SERVICE_SECURITY_NONE;
1003 else if (g_str_equal(security, "wep") == TRUE)
1004 return CONNMAN_SERVICE_SECURITY_WEP;
1005 else if (g_str_equal(security, "wpa") == TRUE)
1006 return CONNMAN_SERVICE_SECURITY_WPA;
1007 else if (g_str_equal(security, "wpa2") == TRUE)
1008 return CONNMAN_SERVICE_SECURITY_WPA2;
1010 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1013 static void update_from_network(struct connman_service *service,
1014 struct connman_network *network)
1016 connman_uint8_t strength = service->strength;
1017 GSequenceIter *iter;
1020 str = connman_network_get_string(network, "Name");
1022 g_free(service->name);
1023 service->name = g_strdup(str);
1026 service->strength = connman_network_get_uint8(network, "Strength");
1028 str = connman_network_get_string(network, "WiFi.Mode");
1029 service->mode = convert_wifi_mode(str);
1031 str = connman_network_get_string(network, "WiFi.Security");
1032 service->security = convert_wifi_security(str);
1034 if (service->strength > strength && service->network != NULL) {
1035 connman_network_unref(service->network);
1036 service->network = NULL;
1039 if (service->network == NULL) {
1040 service->network = connman_network_ref(network);
1042 str = connman_network_get_string(network, "WiFi.Passphrase");
1044 g_free(service->passphrase);
1045 service->passphrase = g_strdup(str);
1049 iter = g_hash_table_lookup(service_hash, service->identifier);
1051 g_sequence_sort_changed(iter, service_compare, NULL);
1055 * connman_service_create_from_network:
1056 * @network: network structure
1058 * Look up service by network and if not found, create one
1060 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
1062 struct connman_service *service;
1063 const char *ident, *group;
1066 ident = __connman_network_get_ident(network);
1070 group = __connman_network_get_group(network);
1074 name = g_strdup_printf("%s_%s_%s",
1075 __connman_network_get_type(network), ident, group);
1077 service = __connman_service_get(name);
1078 if (service == NULL)
1081 if (service->path != NULL) {
1082 update_from_network(service, network);
1084 __connman_profile_changed();
1086 __connman_service_put(service);
1091 service->type = convert_network_type(network);
1093 service->state = CONNMAN_SERVICE_STATE_IDLE;
1095 update_from_network(service, network);
1097 service_register(service);
1105 static int service_load(struct connman_service *service)
1108 gchar *pathname, *data = NULL;
1112 DBG("service %p", service);
1114 if (service->profile == NULL)
1117 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1118 if (pathname == NULL)
1121 keyfile = g_key_file_new();
1123 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1130 if (g_key_file_load_from_data(keyfile, data, length,
1131 0, NULL) == FALSE) {
1138 switch (service->type) {
1139 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1140 case CONNMAN_SERVICE_TYPE_ETHERNET:
1142 case CONNMAN_SERVICE_TYPE_WIFI:
1143 case CONNMAN_SERVICE_TYPE_WIMAX:
1144 service->favorite = g_key_file_get_boolean(keyfile,
1145 service->identifier, "Favorite", NULL);
1149 str = g_key_file_get_string(keyfile,
1150 service->identifier, "Passphrase", NULL);
1152 g_free(service->passphrase);
1153 service->passphrase = str;
1156 g_key_file_free(keyfile);
1161 static int service_save(struct connman_service *service)
1164 gchar *pathname, *data = NULL;
1167 DBG("service %p", service);
1169 if (service->profile == NULL)
1172 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1173 if (pathname == NULL)
1176 keyfile = g_key_file_new();
1178 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1182 if (g_key_file_load_from_data(keyfile, data, length,
1190 if (service->name != NULL)
1191 g_key_file_set_string(keyfile, service->identifier,
1192 "Name", service->name);
1194 switch (service->type) {
1195 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1196 case CONNMAN_SERVICE_TYPE_ETHERNET:
1198 case CONNMAN_SERVICE_TYPE_WIFI:
1199 case CONNMAN_SERVICE_TYPE_WIMAX:
1200 g_key_file_set_boolean(keyfile, service->identifier,
1201 "Favorite", service->favorite);
1205 if (service->passphrase != NULL)
1206 g_key_file_set_string(keyfile, service->identifier,
1207 "Passphrase", service->passphrase);
1209 data = g_key_file_to_data(keyfile, &length, NULL);
1211 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1212 connman_error("Failed to store service information");
1217 g_key_file_free(keyfile);
1224 static struct connman_storage service_storage = {
1226 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1227 .service_load = service_load,
1228 .service_save = service_save,
1231 int __connman_service_init(void)
1235 connection = connman_dbus_get_connection();
1237 if (connman_storage_register(&service_storage) < 0)
1238 connman_error("Failed to register service storage");
1240 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1243 service_list = g_sequence_new(service_free);
1248 void __connman_service_cleanup(void)
1252 g_sequence_free(service_list);
1253 service_list = NULL;
1255 g_hash_table_destroy(service_hash);
1256 service_hash = NULL;
1258 connman_storage_unregister(&service_storage);
1260 dbus_connection_unref(connection);