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;
45 connman_bool_t hidden;
51 struct connman_ipconfig *ipconfig;
52 struct connman_device *device;
53 struct connman_network *network;
58 static void append_path(gpointer value, gpointer user_data)
60 struct connman_service *service = value;
61 DBusMessageIter *iter = user_data;
63 if (service->path == NULL)
66 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
70 void __connman_service_list(DBusMessageIter *iter)
74 g_sequence_foreach(service_list, append_path, iter);
79 struct connman_service *service;
82 static void compare_path(gpointer value, gpointer user_data)
84 struct connman_service *service = value;
85 struct find_data *data = user_data;
87 if (data->service != NULL)
90 if (g_strcmp0(service->path, data->path) == 0)
91 data->service = service;
94 static struct connman_service *find_service(const char *path)
96 struct find_data data = { .path = path, .service = NULL };
100 g_sequence_foreach(service_list, compare_path, &data);
105 static const char *type2string(enum connman_service_type type)
108 case CONNMAN_SERVICE_TYPE_UNKNOWN:
110 case CONNMAN_SERVICE_TYPE_ETHERNET:
112 case CONNMAN_SERVICE_TYPE_WIFI:
114 case CONNMAN_SERVICE_TYPE_WIMAX:
116 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
118 case CONNMAN_SERVICE_TYPE_CELLULAR:
125 static const char *mode2string(enum connman_service_mode mode)
128 case CONNMAN_SERVICE_MODE_UNKNOWN:
130 case CONNMAN_SERVICE_MODE_MANAGED:
132 case CONNMAN_SERVICE_MODE_ADHOC:
139 static const char *security2string(enum connman_service_security security)
142 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
144 case CONNMAN_SERVICE_SECURITY_NONE:
146 case CONNMAN_SERVICE_SECURITY_WEP:
148 case CONNMAN_SERVICE_SECURITY_WPA:
150 case CONNMAN_SERVICE_SECURITY_RSN:
157 static const char *state2string(enum connman_service_state state)
160 case CONNMAN_SERVICE_STATE_UNKNOWN:
162 case CONNMAN_SERVICE_STATE_IDLE:
164 case CONNMAN_SERVICE_STATE_CARRIER:
166 case CONNMAN_SERVICE_STATE_ASSOCIATION:
167 return "association";
168 case CONNMAN_SERVICE_STATE_CONFIGURATION:
169 return "configuration";
170 case CONNMAN_SERVICE_STATE_READY:
172 case CONNMAN_SERVICE_STATE_DISCONNECT:
174 case CONNMAN_SERVICE_STATE_FAILURE:
181 static void state_changed(struct connman_service *service)
184 DBusMessageIter entry, value;
185 const char *str, *key = "State";
187 if (service->path == NULL)
190 str = state2string(service->state);
194 signal = dbus_message_new_signal(service->path,
195 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
199 dbus_message_iter_init_append(signal, &entry);
201 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
203 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
204 DBUS_TYPE_STRING_AS_STRING, &value);
205 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
206 dbus_message_iter_close_container(&entry, &value);
208 g_dbus_send_message(connection, signal);
211 static void strength_changed(struct connman_service *service)
214 DBusMessageIter entry, value;
215 const char *key = "Strength";
217 if (service->path == NULL)
220 if (service->strength == 0)
223 signal = dbus_message_new_signal(service->path,
224 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
228 dbus_message_iter_init_append(signal, &entry);
230 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
232 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
233 DBUS_TYPE_BYTE_AS_STRING, &value);
234 dbus_message_iter_append_basic(&value, DBUS_TYPE_BYTE,
236 dbus_message_iter_close_container(&entry, &value);
238 g_dbus_send_message(connection, signal);
241 static DBusMessage *get_properties(DBusConnection *conn,
242 DBusMessage *msg, void *user_data)
244 struct connman_service *service = user_data;
246 DBusMessageIter array, dict;
249 DBG("service %p", service);
251 reply = dbus_message_new_method_return(msg);
255 dbus_message_iter_init_append(reply, &array);
257 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
258 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
259 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
260 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
262 str = type2string(service->type);
264 connman_dbus_dict_append_variant(&dict, "Type",
265 DBUS_TYPE_STRING, &str);
267 str = mode2string(service->mode);
269 connman_dbus_dict_append_variant(&dict, "Mode",
270 DBUS_TYPE_STRING, &str);
272 str = security2string(service->security);
274 connman_dbus_dict_append_variant(&dict, "Security",
275 DBUS_TYPE_STRING, &str);
277 str = state2string(service->state);
279 connman_dbus_dict_append_variant(&dict, "State",
280 DBUS_TYPE_STRING, &str);
282 if (service->strength > 0)
283 connman_dbus_dict_append_variant(&dict, "Strength",
284 DBUS_TYPE_BYTE, &service->strength);
286 connman_dbus_dict_append_variant(&dict, "Favorite",
287 DBUS_TYPE_BOOLEAN, &service->favorite);
289 if (service->name != NULL)
290 connman_dbus_dict_append_variant(&dict, "Name",
291 DBUS_TYPE_STRING, &service->name);
293 if (service->passphrase != NULL &&
294 __connman_security_check_privilege(msg,
295 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
296 connman_dbus_dict_append_variant(&dict, "Passphrase",
297 DBUS_TYPE_STRING, &service->passphrase);
299 dbus_message_iter_close_container(&array, &dict);
304 static DBusMessage *set_property(DBusConnection *conn,
305 DBusMessage *msg, void *user_data)
307 struct connman_service *service = user_data;
308 DBusMessageIter iter, value;
312 DBG("service %p", service);
314 if (dbus_message_iter_init(msg, &iter) == FALSE)
315 return __connman_error_invalid_arguments(msg);
317 dbus_message_iter_get_basic(&iter, &name);
318 dbus_message_iter_next(&iter);
319 dbus_message_iter_recurse(&iter, &value);
321 if (__connman_security_check_privilege(msg,
322 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
323 return __connman_error_permission_denied(msg);
325 type = dbus_message_iter_get_arg_type(&value);
327 if (g_str_equal(name, "Passphrase") == TRUE) {
328 const char *passphrase;
330 if (type != DBUS_TYPE_STRING)
331 return __connman_error_invalid_arguments(msg);
333 if (__connman_security_check_privilege(msg,
334 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
335 return __connman_error_permission_denied(msg);
337 dbus_message_iter_get_basic(&value, &passphrase);
339 g_free(service->passphrase);
340 service->passphrase = g_strdup(passphrase);
342 if (service->network != NULL)
343 connman_network_set_string(service->network,
344 "WiFi.Passphrase", service->passphrase);
346 __connman_storage_save_service(service);
348 return __connman_error_invalid_property(msg);
350 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
353 static gboolean connect_timeout(gpointer user_data)
355 struct connman_service *service = user_data;
357 DBG("service %p", service);
359 service->timeout = 0;
361 if (service->network != NULL)
362 __connman_network_disconnect(service->network);
364 if (service->pending != NULL) {
367 reply = __connman_error_operation_timeout(service->pending);
369 g_dbus_send_message(connection, reply);
371 dbus_message_unref(service->pending);
372 service->pending = NULL;
374 __connman_service_indicate_state(service,
375 CONNMAN_SERVICE_STATE_FAILURE);
381 static DBusMessage *connect_service(DBusConnection *conn,
382 DBusMessage *msg, void *user_data)
384 struct connman_service *service = user_data;
386 DBG("service %p", service);
388 if (service->pending != NULL)
389 return __connman_error_in_progress(msg);
391 if (service->state == CONNMAN_SERVICE_STATE_READY)
392 return __connman_error_already_connected(msg);
394 if (service->network != NULL) {
397 if (service->hidden == TRUE)
398 return __connman_error_invalid_service(msg);
400 connman_network_set_string(service->network,
401 "WiFi.Passphrase", service->passphrase);
403 err = __connman_network_connect(service->network);
404 if (err < 0 && err != -EINPROGRESS)
405 return __connman_error_failed(msg, -err);
407 service->pending = dbus_message_ref(msg);
409 service->timeout = g_timeout_add_seconds(45,
410 connect_timeout, service);
413 } else if (service->device != NULL) {
414 if (service->favorite == FALSE)
415 return __connman_error_no_carrier(msg);
417 if (__connman_device_connect(service->device) < 0)
418 return __connman_error_failed(msg, EINVAL);
420 service->pending = dbus_message_ref(msg);
421 service->timeout = g_timeout_add_seconds(15,
422 connect_timeout, service);
427 return __connman_error_not_supported(msg);
430 static DBusMessage *disconnect_service(DBusConnection *conn,
431 DBusMessage *msg, void *user_data)
433 struct connman_service *service = user_data;
435 DBG("service %p", service);
437 if (service->pending != NULL) {
440 reply = __connman_error_operation_aborted(service->pending);
442 g_dbus_send_message(conn, reply);
444 dbus_message_unref(service->pending);
445 service->pending = NULL;
447 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
450 if (service->network != NULL) {
453 err = __connman_network_disconnect(service->network);
454 if (err < 0 && err != -EINPROGRESS)
455 return __connman_error_failed(msg, -err);
457 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
458 } else if (service->device != NULL) {
461 if (service->favorite == FALSE)
462 return __connman_error_no_carrier(msg);
464 err = __connman_device_disconnect(service->device);
466 return __connman_error_failed(msg, -err);
468 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
471 return __connman_error_not_supported(msg);
474 static DBusMessage *remove_service(DBusConnection *conn,
475 DBusMessage *msg, void *user_data)
477 struct connman_service *service = user_data;
479 DBG("service %p", service);
481 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
482 return __connman_error_not_supported(msg);
484 if (service->favorite == FALSE)
485 return __connman_error_not_supported(msg);
487 if (service->network != NULL)
488 __connman_network_disconnect(service->network);
490 connman_service_set_favorite(service, FALSE);
491 __connman_storage_save_service(service);
493 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
496 static DBusMessage *move_before(DBusConnection *conn,
497 DBusMessage *msg, void *user_data)
499 struct connman_service *service = user_data;
500 struct connman_service *target;
502 GSequenceIter *src, *dst;
504 DBG("service %p", service);
506 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
509 if (service->favorite == FALSE)
510 return __connman_error_not_supported(msg);
512 target = find_service(path);
513 if (target == NULL || target->favorite == FALSE || target == service)
514 return __connman_error_invalid_service(msg);
516 DBG("target %s", target->identifier);
518 g_get_current_time(&service->modified);
519 __connman_storage_save_service(service);
521 src = g_hash_table_lookup(service_hash, service->identifier);
522 dst = g_hash_table_lookup(service_hash, target->identifier);
525 g_sequence_move(src, dst);
527 __connman_profile_changed();
529 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
531 return __connman_error_not_implemented(msg);
534 static DBusMessage *move_after(DBusConnection *conn,
535 DBusMessage *msg, void *user_data)
537 struct connman_service *service = user_data;
538 struct connman_service *target;
541 DBG("service %p", service);
543 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
546 if (service->favorite == FALSE)
547 return __connman_error_not_supported(msg);
549 target = find_service(path);
550 if (target == NULL || target->favorite == FALSE || target == service)
551 return __connman_error_invalid_service(msg);
553 DBG("target %s", target->identifier);
555 g_get_current_time(&service->modified);
556 __connman_storage_save_service(service);
558 return __connman_error_not_implemented(msg);
561 static GDBusMethodTable service_methods[] = {
562 { "GetProperties", "", "a{sv}", get_properties },
563 { "SetProperty", "sv", "", set_property },
564 { "Connect", "", "", connect_service,
565 G_DBUS_METHOD_FLAG_ASYNC },
566 { "Disconnect", "", "", disconnect_service },
567 { "Remove", "", "", remove_service },
568 { "MoveBefore", "o", "", move_before },
569 { "MoveAfter", "o", "", move_after },
573 static GDBusSignalTable service_signals[] = {
574 { "PropertyChanged", "sv" },
578 static void service_free(gpointer user_data)
580 struct connman_service *service = user_data;
581 char *path = service->path;
583 DBG("service %p", service);
585 g_hash_table_remove(service_hash, service->identifier);
587 if (service->timeout > 0) {
588 g_source_remove(service->timeout);
589 service->timeout = 0;
592 if (service->pending != NULL) {
593 dbus_message_unref(service->pending);
594 service->pending = NULL;
597 service->path = NULL;
600 __connman_profile_changed();
602 g_dbus_unregister_interface(connection, path,
603 CONNMAN_SERVICE_INTERFACE);
607 if (service->network != NULL)
608 connman_network_unref(service->network);
610 connman_ipconfig_unref(service->ipconfig);
612 g_free(service->profile);
613 g_free(service->name);
614 g_free(service->passphrase);
615 g_free(service->identifier);
620 * __connman_service_put:
621 * @service: service structure
623 * Release service if no longer needed
625 void __connman_service_put(struct connman_service *service)
627 DBG("service %p", service);
629 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
632 iter = g_hash_table_lookup(service_hash, service->identifier);
634 g_sequence_remove(iter);
636 service_free(service);
640 static void __connman_service_initialize(struct connman_service *service)
642 DBG("service %p", service);
644 service->refcount = 1;
646 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
647 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
648 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
649 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
651 service->favorite = FALSE;
652 service->hidden = FALSE;
658 * connman_service_create:
660 * Allocate a new service.
662 * Returns: a newly-allocated #connman_service structure
664 struct connman_service *connman_service_create(void)
666 struct connman_service *service;
668 service = g_try_new0(struct connman_service, 1);
672 DBG("service %p", service);
674 __connman_service_initialize(service);
676 service->ipconfig = connman_ipconfig_create();
677 if (service->ipconfig == NULL) {
686 * connman_service_ref:
687 * @service: service structure
689 * Increase reference counter of service
691 struct connman_service *connman_service_ref(struct connman_service *service)
693 g_atomic_int_inc(&service->refcount);
699 * connman_service_unref:
700 * @service: service structure
702 * Decrease reference counter of service
704 void connman_service_unref(struct connman_service *service)
706 __connman_service_put(service);
709 static gint service_compare(gconstpointer a, gconstpointer b,
712 struct connman_service *service_a = (void *) a;
713 struct connman_service *service_b = (void *) b;
715 if (service_a->state != service_b->state) {
716 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
718 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
722 if (service_a->order > service_b->order)
725 if (service_a->order < service_b->order)
728 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
731 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
734 return (gint) service_b->strength - (gint) service_a->strength;
738 * connman_service_set_favorite:
739 * @service: service structure
740 * @favorite: favorite value
742 * Change the favorite setting of service
744 int connman_service_set_favorite(struct connman_service *service,
745 connman_bool_t favorite)
749 iter = g_hash_table_lookup(service_hash, service->identifier);
753 if (service->favorite == favorite)
756 service->favorite = favorite;
758 g_sequence_sort_changed(iter, service_compare, NULL);
760 __connman_profile_changed();
765 int __connman_service_set_carrier(struct connman_service *service,
766 connman_bool_t carrier)
768 DBG("service %p carrier %d", service, carrier);
773 switch (service->type) {
774 case CONNMAN_SERVICE_TYPE_UNKNOWN:
775 case CONNMAN_SERVICE_TYPE_WIFI:
776 case CONNMAN_SERVICE_TYPE_WIMAX:
777 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
778 case CONNMAN_SERVICE_TYPE_CELLULAR:
780 case CONNMAN_SERVICE_TYPE_ETHERNET:
784 if (carrier == FALSE) {
785 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
786 state_changed(service);
788 service->state = CONNMAN_SERVICE_STATE_IDLE;
789 state_changed(service);
791 service->state = CONNMAN_SERVICE_STATE_CARRIER;
792 state_changed(service);
795 return connman_service_set_favorite(service, carrier);
798 int __connman_service_indicate_state(struct connman_service *service,
799 enum connman_service_state state)
803 DBG("service %p state %d", service, state);
808 if (state == CONNMAN_SERVICE_STATE_CARRIER)
809 return __connman_service_set_carrier(service, TRUE);
811 if (service->state == state)
814 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
815 state == CONNMAN_SERVICE_STATE_DISCONNECT)
818 if (state == CONNMAN_SERVICE_STATE_IDLE &&
819 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
820 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
821 state_changed(service);
824 service->state = state;
825 state_changed(service);
827 if (state == CONNMAN_SERVICE_STATE_READY) {
828 connman_service_set_favorite(service, TRUE);
829 __connman_storage_save_service(service);
831 if (service->timeout > 0) {
832 g_source_remove(service->timeout);
833 service->timeout = 0;
836 if (service->pending != NULL) {
837 g_dbus_send_reply(connection, service->pending,
840 dbus_message_unref(service->pending);
841 service->pending = NULL;
844 g_get_current_time(&service->modified);
845 __connman_storage_save_service(service);
848 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
849 if (service->timeout > 0) {
850 g_source_remove(service->timeout);
851 service->timeout = 0;
854 if (service->pending != NULL) {
857 reply = __connman_error_failed(service->pending, EIO);
859 g_dbus_send_message(connection, reply);
861 dbus_message_unref(service->pending);
862 service->pending = NULL;
865 service->state = CONNMAN_SERVICE_STATE_IDLE;
866 state_changed(service);
869 iter = g_hash_table_lookup(service_hash, service->identifier);
871 g_sequence_sort_changed(iter, service_compare, NULL);
873 __connman_profile_changed();
878 int __connman_service_indicate_default(struct connman_service *service)
880 DBG("service %p", service);
886 * __connman_service_lookup:
887 * @identifier: service identifier
889 * Look up a service by identifier (reference count will not be increased)
891 static struct connman_service *__connman_service_lookup(const char *identifier)
895 iter = g_hash_table_lookup(service_hash, identifier);
897 return g_sequence_get(iter);
903 * __connman_service_get:
904 * @identifier: service identifier
906 * Look up a service by identifier or create a new one if not found
908 static struct connman_service *__connman_service_get(const char *identifier)
910 struct connman_service *service;
913 iter = g_hash_table_lookup(service_hash, identifier);
915 service = g_sequence_get(iter);
917 g_atomic_int_inc(&service->refcount);
921 service = connman_service_create();
925 DBG("service %p", service);
927 service->identifier = g_strdup(identifier);
929 service->profile = g_strdup(__connman_profile_active_ident());
931 __connman_storage_load_service(service);
933 iter = g_sequence_insert_sorted(service_list, service,
934 service_compare, NULL);
936 g_hash_table_insert(service_hash, service->identifier, iter);
941 static int service_register(struct connman_service *service)
943 const char *path = __connman_profile_active_path();
946 DBG("service %p", service);
948 if (service->path != NULL)
951 service->path = g_strdup_printf("%s/%s", path, service->identifier);
953 DBG("path %s", service->path);
955 g_dbus_register_interface(connection, service->path,
956 CONNMAN_SERVICE_INTERFACE,
957 service_methods, service_signals,
958 NULL, service, NULL);
960 __connman_storage_load_service(service);
962 iter = g_hash_table_lookup(service_hash, service->identifier);
964 g_sequence_sort_changed(iter, service_compare, NULL);
966 __connman_profile_changed();
972 * connman_service_lookup_from_device:
973 * @device: device structure
975 * Look up a service by device (reference count will not be increased)
977 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
979 struct connman_service *service;
983 ident = __connman_device_get_ident(device);
987 name = g_strdup_printf("%s_%s",
988 __connman_device_get_type(device), ident);
990 service = __connman_service_lookup(name);
997 static enum connman_service_type convert_device_type(struct connman_device *device)
999 enum connman_device_type type = connman_device_get_type(device);
1002 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1003 case CONNMAN_DEVICE_TYPE_VENDOR:
1004 case CONNMAN_DEVICE_TYPE_WIFI:
1005 case CONNMAN_DEVICE_TYPE_WIMAX:
1006 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1007 case CONNMAN_DEVICE_TYPE_GPS:
1008 case CONNMAN_DEVICE_TYPE_HSO:
1009 case CONNMAN_DEVICE_TYPE_NOZOMI:
1010 case CONNMAN_DEVICE_TYPE_HUAWEI:
1011 case CONNMAN_DEVICE_TYPE_NOVATEL:
1013 case CONNMAN_DEVICE_TYPE_ETHERNET:
1014 return CONNMAN_SERVICE_TYPE_ETHERNET;
1017 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1021 * connman_service_create_from_device:
1022 * @device: device structure
1024 * Look up service by device and if not found, create one
1026 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
1028 struct connman_service *service;
1032 ident = __connman_device_get_ident(device);
1036 name = g_strdup_printf("%s_%s",
1037 __connman_device_get_type(device), ident);
1039 service = __connman_service_get(name);
1040 if (service == NULL)
1043 if (service->path != NULL) {
1044 __connman_service_put(service);
1049 service->type = convert_device_type(device);
1051 service->device = device;
1053 service_register(service);
1062 * connman_service_lookup_from_network:
1063 * @network: network structure
1065 * Look up a service by network (reference count will not be increased)
1067 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
1069 struct connman_service *service;
1070 const char *ident, *group;
1073 ident = __connman_network_get_ident(network);
1077 group = __connman_network_get_group(network);
1081 name = g_strdup_printf("%s_%s_%s",
1082 __connman_network_get_type(network), ident, group);
1084 service = __connman_service_lookup(name);
1091 unsigned int __connman_service_get_order(struct connman_service *service)
1093 return service->order;
1096 static enum connman_service_type convert_network_type(struct connman_network *network)
1098 enum connman_network_type type = connman_network_get_type(network);
1101 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1102 case CONNMAN_NETWORK_TYPE_VENDOR:
1103 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1104 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1105 case CONNMAN_NETWORK_TYPE_HSO:
1107 case CONNMAN_NETWORK_TYPE_WIFI:
1108 return CONNMAN_SERVICE_TYPE_WIFI;
1109 case CONNMAN_NETWORK_TYPE_WIMAX:
1110 return CONNMAN_SERVICE_TYPE_WIMAX;
1113 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1116 static enum connman_service_mode convert_wifi_mode(const char *mode)
1119 return CONNMAN_SERVICE_MODE_UNKNOWN;
1120 else if (g_str_equal(mode, "managed") == TRUE)
1121 return CONNMAN_SERVICE_MODE_MANAGED;
1122 else if (g_str_equal(mode, "adhoc") == TRUE)
1123 return CONNMAN_SERVICE_MODE_ADHOC;
1125 return CONNMAN_SERVICE_MODE_UNKNOWN;
1128 static enum connman_service_mode convert_wifi_security(const char *security)
1130 if (security == NULL)
1131 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1132 else if (g_str_equal(security, "none") == TRUE)
1133 return CONNMAN_SERVICE_SECURITY_NONE;
1134 else if (g_str_equal(security, "wep") == TRUE)
1135 return CONNMAN_SERVICE_SECURITY_WEP;
1136 else if (g_str_equal(security, "wpa") == TRUE)
1137 return CONNMAN_SERVICE_SECURITY_WPA;
1138 else if (g_str_equal(security, "rsn") == TRUE)
1139 return CONNMAN_SERVICE_SECURITY_RSN;
1141 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1144 static void update_from_network(struct connman_service *service,
1145 struct connman_network *network)
1147 connman_uint8_t strength = service->strength;
1148 GSequenceIter *iter;
1151 str = connman_network_get_string(network, "Name");
1153 g_free(service->name);
1154 service->name = g_strdup(str);
1155 service->hidden = FALSE;
1157 g_free(service->name);
1158 service->name = NULL;
1159 service->hidden = TRUE;
1162 service->strength = connman_network_get_uint8(network, "Strength");
1164 str = connman_network_get_string(network, "WiFi.Mode");
1165 service->mode = convert_wifi_mode(str);
1167 str = connman_network_get_string(network, "WiFi.Security");
1168 service->security = convert_wifi_security(str);
1170 if (service->strength > strength && service->network != NULL) {
1171 connman_network_unref(service->network);
1172 service->network = NULL;
1174 strength_changed(service);
1177 if (service->network == NULL) {
1178 service->network = connman_network_ref(network);
1180 str = connman_network_get_string(network, "WiFi.Passphrase");
1182 g_free(service->passphrase);
1183 service->passphrase = g_strdup(str);
1187 iter = g_hash_table_lookup(service_hash, service->identifier);
1189 g_sequence_sort_changed(iter, service_compare, NULL);
1193 * connman_service_create_from_network:
1194 * @network: network structure
1196 * Look up service by network and if not found, create one
1198 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
1200 struct connman_service *service;
1201 const char *ident, *group;
1204 if (__connman_service_lookup_from_network(network) != NULL) {
1205 connman_error("Service already exists");
1209 ident = __connman_network_get_ident(network);
1213 group = __connman_network_get_group(network);
1217 name = g_strdup_printf("%s_%s_%s",
1218 __connman_network_get_type(network), ident, group);
1220 service = __connman_service_get(name);
1221 if (service == NULL)
1224 if (service->path != NULL) {
1225 update_from_network(service, network);
1227 __connman_profile_changed();
1229 __connman_service_put(service);
1234 service->type = convert_network_type(network);
1236 service->state = CONNMAN_SERVICE_STATE_IDLE;
1238 update_from_network(service, network);
1240 service_register(service);
1248 static int service_load(struct connman_service *service)
1251 gchar *pathname, *data = NULL;
1255 DBG("service %p", service);
1257 if (service->profile == NULL)
1260 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1261 if (pathname == NULL)
1264 keyfile = g_key_file_new();
1266 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1273 if (g_key_file_load_from_data(keyfile, data, length,
1274 0, NULL) == FALSE) {
1281 switch (service->type) {
1282 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1283 case CONNMAN_SERVICE_TYPE_ETHERNET:
1285 case CONNMAN_SERVICE_TYPE_WIFI:
1286 case CONNMAN_SERVICE_TYPE_WIMAX:
1287 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1288 case CONNMAN_SERVICE_TYPE_CELLULAR:
1289 service->favorite = g_key_file_get_boolean(keyfile,
1290 service->identifier, "Favorite", NULL);
1294 str = g_key_file_get_string(keyfile,
1295 service->identifier, "Modified", NULL);
1297 g_time_val_from_iso8601(str, &service->modified);
1301 str = g_key_file_get_string(keyfile,
1302 service->identifier, "Passphrase", NULL);
1304 g_free(service->passphrase);
1305 service->passphrase = str;
1308 g_key_file_free(keyfile);
1313 static int service_save(struct connman_service *service)
1316 gchar *pathname, *data = NULL;
1320 DBG("service %p", service);
1322 if (service->profile == NULL)
1325 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1326 if (pathname == NULL)
1329 keyfile = g_key_file_new();
1331 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1335 if (g_key_file_load_from_data(keyfile, data, length,
1343 if (service->name != NULL)
1344 g_key_file_set_string(keyfile, service->identifier,
1345 "Name", service->name);
1347 switch (service->type) {
1348 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1349 case CONNMAN_SERVICE_TYPE_ETHERNET:
1351 case CONNMAN_SERVICE_TYPE_WIFI:
1352 case CONNMAN_SERVICE_TYPE_WIMAX:
1353 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1354 case CONNMAN_SERVICE_TYPE_CELLULAR:
1355 g_key_file_set_boolean(keyfile, service->identifier,
1356 "Favorite", service->favorite);
1360 str = g_time_val_to_iso8601(&service->modified);
1362 g_key_file_set_string(keyfile, service->identifier,
1367 if (service->passphrase != NULL)
1368 g_key_file_set_string(keyfile, service->identifier,
1369 "Passphrase", service->passphrase);
1371 data = g_key_file_to_data(keyfile, &length, NULL);
1373 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1374 connman_error("Failed to store service information");
1379 g_key_file_free(keyfile);
1386 static struct connman_storage service_storage = {
1388 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1389 .service_load = service_load,
1390 .service_save = service_save,
1393 int __connman_service_init(void)
1397 connection = connman_dbus_get_connection();
1399 if (connman_storage_register(&service_storage) < 0)
1400 connman_error("Failed to register service storage");
1402 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1405 service_list = g_sequence_new(service_free);
1410 void __connman_service_cleanup(void)
1414 g_sequence_free(service_list);
1415 service_list = NULL;
1417 g_hash_table_destroy(service_hash);
1418 service_hash = NULL;
1420 connman_storage_unregister(&service_storage);
1422 dbus_connection_unref(connection);