5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 static DBusConnection *connection = NULL;
32 static GSequence *service_list = NULL;
33 static GHashTable *service_hash = NULL;
35 struct connman_service {
39 enum connman_service_type type;
40 enum connman_service_mode mode;
41 enum connman_service_security security;
42 enum connman_service_state state;
43 enum connman_service_error error;
44 connman_uint8_t strength;
45 connman_bool_t favorite;
46 connman_bool_t hidden;
52 struct connman_ipconfig *ipconfig;
53 struct connman_device *device;
54 struct connman_network *network;
59 static void append_path(gpointer value, gpointer user_data)
61 struct connman_service *service = value;
62 DBusMessageIter *iter = user_data;
64 if (service->path == NULL)
67 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
71 void __connman_service_list(DBusMessageIter *iter)
75 g_sequence_foreach(service_list, append_path, iter);
80 struct connman_service *service;
83 static void compare_path(gpointer value, gpointer user_data)
85 struct connman_service *service = value;
86 struct find_data *data = user_data;
88 if (data->service != NULL)
91 if (g_strcmp0(service->path, data->path) == 0)
92 data->service = service;
95 static struct connman_service *find_service(const char *path)
97 struct find_data data = { .path = path, .service = NULL };
101 g_sequence_foreach(service_list, compare_path, &data);
106 static const char *type2string(enum connman_service_type type)
109 case CONNMAN_SERVICE_TYPE_UNKNOWN:
111 case CONNMAN_SERVICE_TYPE_ETHERNET:
113 case CONNMAN_SERVICE_TYPE_WIFI:
115 case CONNMAN_SERVICE_TYPE_WIMAX:
117 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
119 case CONNMAN_SERVICE_TYPE_CELLULAR:
126 static const char *mode2string(enum connman_service_mode mode)
129 case CONNMAN_SERVICE_MODE_UNKNOWN:
131 case CONNMAN_SERVICE_MODE_MANAGED:
133 case CONNMAN_SERVICE_MODE_ADHOC:
140 static const char *security2string(enum connman_service_security security)
143 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
145 case CONNMAN_SERVICE_SECURITY_NONE:
147 case CONNMAN_SERVICE_SECURITY_WEP:
149 case CONNMAN_SERVICE_SECURITY_WPA:
151 case CONNMAN_SERVICE_SECURITY_RSN:
158 static const char *state2string(enum connman_service_state state)
161 case CONNMAN_SERVICE_STATE_UNKNOWN:
163 case CONNMAN_SERVICE_STATE_IDLE:
165 case CONNMAN_SERVICE_STATE_CARRIER:
167 case CONNMAN_SERVICE_STATE_ASSOCIATION:
168 return "association";
169 case CONNMAN_SERVICE_STATE_CONFIGURATION:
170 return "configuration";
171 case CONNMAN_SERVICE_STATE_READY:
173 case CONNMAN_SERVICE_STATE_DISCONNECT:
175 case CONNMAN_SERVICE_STATE_FAILURE:
182 static const char *error2string(enum connman_service_error error)
185 case CONNMAN_SERVICE_ERROR_UNKNOWN:
187 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
188 return "dhcp-failed";
194 static enum connman_service_error string2error(const char *error)
196 if (g_strcmp0(error, "dhcp-failed") == 0)
197 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
199 return CONNMAN_SERVICE_ERROR_UNKNOWN;
202 static void state_changed(struct connman_service *service)
205 DBusMessageIter entry, value;
206 const char *str, *key = "State";
208 if (service->path == NULL)
211 str = state2string(service->state);
215 signal = dbus_message_new_signal(service->path,
216 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
220 dbus_message_iter_init_append(signal, &entry);
222 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
224 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
225 DBUS_TYPE_STRING_AS_STRING, &value);
226 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
227 dbus_message_iter_close_container(&entry, &value);
229 g_dbus_send_message(connection, signal);
232 static void strength_changed(struct connman_service *service)
235 DBusMessageIter entry, value;
236 const char *key = "Strength";
238 if (service->path == NULL)
241 if (service->strength == 0)
244 signal = dbus_message_new_signal(service->path,
245 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
249 dbus_message_iter_init_append(signal, &entry);
251 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
253 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
254 DBUS_TYPE_BYTE_AS_STRING, &value);
255 dbus_message_iter_append_basic(&value, DBUS_TYPE_BYTE,
257 dbus_message_iter_close_container(&entry, &value);
259 g_dbus_send_message(connection, signal);
262 static DBusMessage *get_properties(DBusConnection *conn,
263 DBusMessage *msg, void *user_data)
265 struct connman_service *service = user_data;
267 DBusMessageIter array, dict;
270 DBG("service %p", service);
272 reply = dbus_message_new_method_return(msg);
276 dbus_message_iter_init_append(reply, &array);
278 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
279 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
280 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
281 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
283 str = type2string(service->type);
285 connman_dbus_dict_append_variant(&dict, "Type",
286 DBUS_TYPE_STRING, &str);
288 str = mode2string(service->mode);
290 connman_dbus_dict_append_variant(&dict, "Mode",
291 DBUS_TYPE_STRING, &str);
293 str = security2string(service->security);
295 connman_dbus_dict_append_variant(&dict, "Security",
296 DBUS_TYPE_STRING, &str);
298 str = state2string(service->state);
300 connman_dbus_dict_append_variant(&dict, "State",
301 DBUS_TYPE_STRING, &str);
303 str = error2string(service->error);
305 connman_dbus_dict_append_variant(&dict, "Error",
306 DBUS_TYPE_STRING, &str);
308 if (service->strength > 0)
309 connman_dbus_dict_append_variant(&dict, "Strength",
310 DBUS_TYPE_BYTE, &service->strength);
312 connman_dbus_dict_append_variant(&dict, "Favorite",
313 DBUS_TYPE_BOOLEAN, &service->favorite);
315 if (service->name != NULL)
316 connman_dbus_dict_append_variant(&dict, "Name",
317 DBUS_TYPE_STRING, &service->name);
319 if (service->passphrase != NULL &&
320 __connman_security_check_privilege(msg,
321 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
322 connman_dbus_dict_append_variant(&dict, "Passphrase",
323 DBUS_TYPE_STRING, &service->passphrase);
325 __connman_ipconfig_append_ipv4(service->ipconfig, &dict, "IPv4.");
327 dbus_message_iter_close_container(&array, &dict);
332 static DBusMessage *set_property(DBusConnection *conn,
333 DBusMessage *msg, void *user_data)
335 struct connman_service *service = user_data;
336 DBusMessageIter iter, value;
340 DBG("service %p", service);
342 if (dbus_message_iter_init(msg, &iter) == FALSE)
343 return __connman_error_invalid_arguments(msg);
345 dbus_message_iter_get_basic(&iter, &name);
346 dbus_message_iter_next(&iter);
347 dbus_message_iter_recurse(&iter, &value);
349 if (__connman_security_check_privilege(msg,
350 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
351 return __connman_error_permission_denied(msg);
353 type = dbus_message_iter_get_arg_type(&value);
355 if (g_str_equal(name, "Passphrase") == TRUE) {
356 const char *passphrase;
358 if (type != DBUS_TYPE_STRING)
359 return __connman_error_invalid_arguments(msg);
361 if (__connman_security_check_privilege(msg,
362 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
363 return __connman_error_permission_denied(msg);
365 dbus_message_iter_get_basic(&value, &passphrase);
367 g_free(service->passphrase);
368 service->passphrase = g_strdup(passphrase);
370 if (service->network != NULL)
371 connman_network_set_string(service->network,
372 "WiFi.Passphrase", service->passphrase);
374 __connman_storage_save_service(service);
375 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
378 err = __connman_ipconfig_set_ipv4(service->ipconfig,
381 return __connman_error_failed(msg, -err);
383 return __connman_error_invalid_property(msg);
385 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
388 static DBusMessage *clear_property(DBusConnection *conn,
389 DBusMessage *msg, void *user_data)
391 struct connman_service *service = user_data;
394 DBG("service %p", service);
396 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
399 if (__connman_security_check_privilege(msg,
400 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
401 return __connman_error_permission_denied(msg);
403 if (g_str_equal(name, "Error") == TRUE) {
404 service->state = CONNMAN_SERVICE_STATE_IDLE;
405 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
406 state_changed(service);
408 g_get_current_time(&service->modified);
409 __connman_storage_save_service(service);
411 return __connman_error_invalid_property(msg);
413 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
416 static gboolean connect_timeout(gpointer user_data)
418 struct connman_service *service = user_data;
420 DBG("service %p", service);
422 service->timeout = 0;
424 if (service->network != NULL)
425 __connman_network_disconnect(service->network);
427 if (service->pending != NULL) {
430 reply = __connman_error_operation_timeout(service->pending);
432 g_dbus_send_message(connection, reply);
434 dbus_message_unref(service->pending);
435 service->pending = NULL;
437 __connman_service_indicate_state(service,
438 CONNMAN_SERVICE_STATE_FAILURE);
444 static DBusMessage *connect_service(DBusConnection *conn,
445 DBusMessage *msg, void *user_data)
447 struct connman_service *service = user_data;
449 DBG("service %p", service);
451 if (service->pending != NULL)
452 return __connman_error_in_progress(msg);
454 if (service->state == CONNMAN_SERVICE_STATE_READY)
455 return __connman_error_already_connected(msg);
457 if (service->network != NULL) {
460 if (service->hidden == TRUE)
461 return __connman_error_invalid_service(msg);
463 connman_network_set_string(service->network,
464 "WiFi.Passphrase", service->passphrase);
466 err = __connman_network_connect(service->network);
468 if (err != -EINPROGRESS)
469 return __connman_error_failed(msg, -err);
471 service->pending = dbus_message_ref(msg);
473 service->timeout = g_timeout_add_seconds(45,
474 connect_timeout, service);
478 } else if (service->device != NULL) {
479 if (service->favorite == FALSE)
480 return __connman_error_no_carrier(msg);
482 if (__connman_device_connect(service->device) < 0)
483 return __connman_error_failed(msg, EINVAL);
485 service->pending = dbus_message_ref(msg);
486 service->timeout = g_timeout_add_seconds(15,
487 connect_timeout, service);
491 return __connman_error_not_supported(msg);
493 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
496 static DBusMessage *disconnect_service(DBusConnection *conn,
497 DBusMessage *msg, void *user_data)
499 struct connman_service *service = user_data;
501 DBG("service %p", service);
503 if (service->pending != NULL) {
506 reply = __connman_error_operation_aborted(service->pending);
508 g_dbus_send_message(conn, reply);
510 dbus_message_unref(service->pending);
511 service->pending = NULL;
513 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
516 if (service->network != NULL) {
519 err = __connman_network_disconnect(service->network);
520 if (err < 0 && err != -EINPROGRESS)
521 return __connman_error_failed(msg, -err);
522 } else if (service->device != NULL) {
525 if (service->favorite == FALSE)
526 return __connman_error_no_carrier(msg);
528 err = __connman_device_disconnect(service->device);
530 return __connman_error_failed(msg, -err);
532 return __connman_error_not_supported(msg);
534 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
537 static DBusMessage *remove_service(DBusConnection *conn,
538 DBusMessage *msg, void *user_data)
540 struct connman_service *service = user_data;
542 DBG("service %p", service);
544 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
545 return __connman_error_not_supported(msg);
547 if (service->favorite == FALSE)
548 return __connman_error_not_supported(msg);
550 if (service->network != NULL)
551 __connman_network_disconnect(service->network);
553 g_free (service->passphrase);
554 service->passphrase = NULL;
556 connman_service_set_favorite(service, FALSE);
557 __connman_storage_save_service(service);
559 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
562 static DBusMessage *move_before(DBusConnection *conn,
563 DBusMessage *msg, void *user_data)
565 struct connman_service *service = user_data;
566 struct connman_service *target;
568 GSequenceIter *src, *dst;
570 DBG("service %p", service);
572 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
575 if (service->favorite == FALSE)
576 return __connman_error_not_supported(msg);
578 target = find_service(path);
579 if (target == NULL || target->favorite == FALSE || target == service)
580 return __connman_error_invalid_service(msg);
582 DBG("target %s", target->identifier);
584 g_get_current_time(&service->modified);
585 __connman_storage_save_service(service);
587 src = g_hash_table_lookup(service_hash, service->identifier);
588 dst = g_hash_table_lookup(service_hash, target->identifier);
591 g_sequence_move(src, dst);
593 __connman_profile_changed();
595 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
597 return __connman_error_not_implemented(msg);
600 static DBusMessage *move_after(DBusConnection *conn,
601 DBusMessage *msg, void *user_data)
603 struct connman_service *service = user_data;
604 struct connman_service *target;
607 DBG("service %p", service);
609 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
612 if (service->favorite == FALSE)
613 return __connman_error_not_supported(msg);
615 target = find_service(path);
616 if (target == NULL || target->favorite == FALSE || target == service)
617 return __connman_error_invalid_service(msg);
619 DBG("target %s", target->identifier);
621 g_get_current_time(&service->modified);
622 __connman_storage_save_service(service);
624 return __connman_error_not_implemented(msg);
627 static GDBusMethodTable service_methods[] = {
628 { "GetProperties", "", "a{sv}", get_properties },
629 { "SetProperty", "sv", "", set_property },
630 { "ClearProperty", "s", "", clear_property },
631 { "Connect", "", "", connect_service,
632 G_DBUS_METHOD_FLAG_ASYNC },
633 { "Disconnect", "", "", disconnect_service },
634 { "Remove", "", "", remove_service },
635 { "MoveBefore", "o", "", move_before },
636 { "MoveAfter", "o", "", move_after },
640 static GDBusSignalTable service_signals[] = {
641 { "PropertyChanged", "sv" },
645 static void service_free(gpointer user_data)
647 struct connman_service *service = user_data;
648 char *path = service->path;
650 DBG("service %p", service);
652 g_hash_table_remove(service_hash, service->identifier);
654 if (service->timeout > 0) {
655 g_source_remove(service->timeout);
656 service->timeout = 0;
659 if (service->pending != NULL) {
660 dbus_message_unref(service->pending);
661 service->pending = NULL;
664 service->path = NULL;
667 __connman_profile_changed();
669 g_dbus_unregister_interface(connection, path,
670 CONNMAN_SERVICE_INTERFACE);
674 if (service->network != NULL)
675 connman_network_unref(service->network);
677 connman_ipconfig_unref(service->ipconfig);
679 g_free(service->profile);
680 g_free(service->name);
681 g_free(service->passphrase);
682 g_free(service->identifier);
687 * __connman_service_put:
688 * @service: service structure
690 * Release service if no longer needed
692 void __connman_service_put(struct connman_service *service)
694 DBG("service %p", service);
696 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
699 iter = g_hash_table_lookup(service_hash, service->identifier);
701 g_sequence_remove(iter);
703 service_free(service);
707 static void __connman_service_initialize(struct connman_service *service)
709 DBG("service %p", service);
711 service->refcount = 1;
713 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
714 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
715 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
716 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
718 service->favorite = FALSE;
719 service->hidden = FALSE;
725 * connman_service_create:
727 * Allocate a new service.
729 * Returns: a newly-allocated #connman_service structure
731 struct connman_service *connman_service_create(void)
733 struct connman_service *service;
735 service = g_try_new0(struct connman_service, 1);
739 DBG("service %p", service);
741 __connman_service_initialize(service);
743 service->ipconfig = connman_ipconfig_create();
744 if (service->ipconfig == NULL) {
749 connman_ipconfig_set_method(service->ipconfig,
750 CONNMAN_IPCONFIG_METHOD_DHCP);
756 * connman_service_ref:
757 * @service: service structure
759 * Increase reference counter of service
761 struct connman_service *connman_service_ref(struct connman_service *service)
763 g_atomic_int_inc(&service->refcount);
769 * connman_service_unref:
770 * @service: service structure
772 * Decrease reference counter of service
774 void connman_service_unref(struct connman_service *service)
776 __connman_service_put(service);
779 static gint service_compare(gconstpointer a, gconstpointer b,
782 struct connman_service *service_a = (void *) a;
783 struct connman_service *service_b = (void *) b;
785 if (service_a->state != service_b->state) {
786 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
788 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
792 if (service_a->order > service_b->order)
795 if (service_a->order < service_b->order)
798 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
801 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
804 return (gint) service_b->strength - (gint) service_a->strength;
808 * connman_service_set_favorite:
809 * @service: service structure
810 * @favorite: favorite value
812 * Change the favorite setting of service
814 int connman_service_set_favorite(struct connman_service *service,
815 connman_bool_t favorite)
819 iter = g_hash_table_lookup(service_hash, service->identifier);
823 if (service->favorite == favorite)
826 service->favorite = favorite;
828 g_sequence_sort_changed(iter, service_compare, NULL);
830 __connman_profile_changed();
835 int __connman_service_set_carrier(struct connman_service *service,
836 connman_bool_t carrier)
838 DBG("service %p carrier %d", service, carrier);
843 switch (service->type) {
844 case CONNMAN_SERVICE_TYPE_UNKNOWN:
845 case CONNMAN_SERVICE_TYPE_WIFI:
846 case CONNMAN_SERVICE_TYPE_WIMAX:
847 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
848 case CONNMAN_SERVICE_TYPE_CELLULAR:
850 case CONNMAN_SERVICE_TYPE_ETHERNET:
854 if (carrier == FALSE) {
855 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
856 state_changed(service);
858 service->state = CONNMAN_SERVICE_STATE_IDLE;
859 state_changed(service);
861 service->state = CONNMAN_SERVICE_STATE_CARRIER;
862 state_changed(service);
865 return connman_service_set_favorite(service, carrier);
868 int __connman_service_indicate_state(struct connman_service *service,
869 enum connman_service_state state)
873 DBG("service %p state %d", service, state);
878 if (state == CONNMAN_SERVICE_STATE_CARRIER)
879 return __connman_service_set_carrier(service, TRUE);
881 if (service->state == state)
884 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
885 state == CONNMAN_SERVICE_STATE_DISCONNECT)
888 if (state == CONNMAN_SERVICE_STATE_IDLE &&
889 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
890 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
891 state_changed(service);
894 service->state = state;
895 state_changed(service);
897 if (state == CONNMAN_SERVICE_STATE_READY) {
898 connman_service_set_favorite(service, TRUE);
900 if (service->timeout > 0) {
901 g_source_remove(service->timeout);
902 service->timeout = 0;
905 if (service->pending != NULL) {
906 g_dbus_send_reply(connection, service->pending,
909 dbus_message_unref(service->pending);
910 service->pending = NULL;
913 g_get_current_time(&service->modified);
914 __connman_storage_save_service(service);
917 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
918 if (service->timeout > 0) {
919 g_source_remove(service->timeout);
920 service->timeout = 0;
923 if (service->pending != NULL) {
926 reply = __connman_error_failed(service->pending, EIO);
928 g_dbus_send_message(connection, reply);
930 dbus_message_unref(service->pending);
931 service->pending = NULL;
934 g_get_current_time(&service->modified);
935 __connman_storage_save_service(service);
937 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
939 iter = g_hash_table_lookup(service_hash, service->identifier);
941 g_sequence_sort_changed(iter, service_compare, NULL);
943 __connman_profile_changed();
948 int __connman_service_indicate_error(struct connman_service *service,
949 enum connman_service_error error)
951 DBG("service %p error %d", service, error);
956 service->error = error;
958 return __connman_service_indicate_state(service,
959 CONNMAN_SERVICE_STATE_FAILURE);
962 int __connman_service_indicate_default(struct connman_service *service)
964 DBG("service %p", service);
970 * __connman_service_lookup:
971 * @identifier: service identifier
973 * Look up a service by identifier (reference count will not be increased)
975 static struct connman_service *__connman_service_lookup(const char *identifier)
979 iter = g_hash_table_lookup(service_hash, identifier);
981 return g_sequence_get(iter);
987 * __connman_service_get:
988 * @identifier: service identifier
990 * Look up a service by identifier or create a new one if not found
992 static struct connman_service *__connman_service_get(const char *identifier)
994 struct connman_service *service;
997 iter = g_hash_table_lookup(service_hash, identifier);
999 service = g_sequence_get(iter);
1000 if (service != NULL)
1001 g_atomic_int_inc(&service->refcount);
1005 service = connman_service_create();
1006 if (service == NULL)
1009 DBG("service %p", service);
1011 service->identifier = g_strdup(identifier);
1013 service->profile = g_strdup(__connman_profile_active_ident());
1015 __connman_storage_load_service(service);
1017 iter = g_sequence_insert_sorted(service_list, service,
1018 service_compare, NULL);
1020 g_hash_table_insert(service_hash, service->identifier, iter);
1025 static int service_register(struct connman_service *service)
1027 const char *path = __connman_profile_active_path();
1028 GSequenceIter *iter;
1030 DBG("service %p", service);
1032 if (service->path != NULL)
1035 service->path = g_strdup_printf("%s/%s", path, service->identifier);
1037 DBG("path %s", service->path);
1039 g_dbus_register_interface(connection, service->path,
1040 CONNMAN_SERVICE_INTERFACE,
1041 service_methods, service_signals,
1042 NULL, service, NULL);
1044 __connman_storage_load_service(service);
1046 iter = g_hash_table_lookup(service_hash, service->identifier);
1048 g_sequence_sort_changed(iter, service_compare, NULL);
1050 __connman_profile_changed();
1056 * connman_service_lookup_from_device:
1057 * @device: device structure
1059 * Look up a service by device (reference count will not be increased)
1061 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
1063 struct connman_service *service;
1067 ident = __connman_device_get_ident(device);
1071 name = g_strdup_printf("%s_%s",
1072 __connman_device_get_type(device), ident);
1074 service = __connman_service_lookup(name);
1081 static enum connman_service_type convert_device_type(struct connman_device *device)
1083 enum connman_device_type type = connman_device_get_type(device);
1086 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1087 case CONNMAN_DEVICE_TYPE_VENDOR:
1088 case CONNMAN_DEVICE_TYPE_WIFI:
1089 case CONNMAN_DEVICE_TYPE_WIMAX:
1090 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1091 case CONNMAN_DEVICE_TYPE_GPS:
1092 case CONNMAN_DEVICE_TYPE_MBM:
1093 case CONNMAN_DEVICE_TYPE_HSO:
1094 case CONNMAN_DEVICE_TYPE_NOZOMI:
1095 case CONNMAN_DEVICE_TYPE_HUAWEI:
1096 case CONNMAN_DEVICE_TYPE_NOVATEL:
1098 case CONNMAN_DEVICE_TYPE_ETHERNET:
1099 return CONNMAN_SERVICE_TYPE_ETHERNET;
1102 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1106 * connman_service_create_from_device:
1107 * @device: device structure
1109 * Look up service by device and if not found, create one
1111 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
1113 struct connman_service *service;
1117 ident = __connman_device_get_ident(device);
1121 name = g_strdup_printf("%s_%s",
1122 __connman_device_get_type(device), ident);
1124 service = __connman_service_get(name);
1125 if (service == NULL)
1128 if (service->path != NULL) {
1129 __connman_service_put(service);
1134 service->type = convert_device_type(device);
1136 service->device = device;
1138 service_register(service);
1147 * connman_service_lookup_from_network:
1148 * @network: network structure
1150 * Look up a service by network (reference count will not be increased)
1152 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
1154 struct connman_service *service;
1155 const char *ident, *group;
1158 ident = __connman_network_get_ident(network);
1162 group = __connman_network_get_group(network);
1166 name = g_strdup_printf("%s_%s_%s",
1167 __connman_network_get_type(network), ident, group);
1169 service = __connman_service_lookup(name);
1176 unsigned int __connman_service_get_order(struct connman_service *service)
1178 return service->order;
1181 static enum connman_service_type convert_network_type(struct connman_network *network)
1183 enum connman_network_type type = connman_network_get_type(network);
1186 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1187 case CONNMAN_NETWORK_TYPE_VENDOR:
1188 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1189 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1190 case CONNMAN_NETWORK_TYPE_HSO:
1192 case CONNMAN_NETWORK_TYPE_WIFI:
1193 return CONNMAN_SERVICE_TYPE_WIFI;
1194 case CONNMAN_NETWORK_TYPE_WIMAX:
1195 return CONNMAN_SERVICE_TYPE_WIMAX;
1198 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1201 static enum connman_service_mode convert_wifi_mode(const char *mode)
1204 return CONNMAN_SERVICE_MODE_UNKNOWN;
1205 else if (g_str_equal(mode, "managed") == TRUE)
1206 return CONNMAN_SERVICE_MODE_MANAGED;
1207 else if (g_str_equal(mode, "adhoc") == TRUE)
1208 return CONNMAN_SERVICE_MODE_ADHOC;
1210 return CONNMAN_SERVICE_MODE_UNKNOWN;
1213 static enum connman_service_mode convert_wifi_security(const char *security)
1215 if (security == NULL)
1216 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1217 else if (g_str_equal(security, "none") == TRUE)
1218 return CONNMAN_SERVICE_SECURITY_NONE;
1219 else if (g_str_equal(security, "wep") == TRUE)
1220 return CONNMAN_SERVICE_SECURITY_WEP;
1221 else if (g_str_equal(security, "wpa") == TRUE)
1222 return CONNMAN_SERVICE_SECURITY_WPA;
1223 else if (g_str_equal(security, "rsn") == TRUE)
1224 return CONNMAN_SERVICE_SECURITY_RSN;
1226 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1229 static void update_from_network(struct connman_service *service,
1230 struct connman_network *network)
1232 connman_uint8_t strength = service->strength;
1233 GSequenceIter *iter;
1236 str = connman_network_get_string(network, "Name");
1238 g_free(service->name);
1239 service->name = g_strdup(str);
1240 service->hidden = FALSE;
1242 g_free(service->name);
1243 service->name = NULL;
1244 service->hidden = TRUE;
1247 service->strength = connman_network_get_uint8(network, "Strength");
1249 str = connman_network_get_string(network, "WiFi.Mode");
1250 service->mode = convert_wifi_mode(str);
1252 str = connman_network_get_string(network, "WiFi.Security");
1253 service->security = convert_wifi_security(str);
1255 if (service->strength > strength && service->network != NULL) {
1256 connman_network_unref(service->network);
1257 service->network = NULL;
1259 strength_changed(service);
1262 if (service->network == NULL) {
1263 service->network = connman_network_ref(network);
1265 str = connman_network_get_string(network, "WiFi.Passphrase");
1267 g_free(service->passphrase);
1268 service->passphrase = g_strdup(str);
1272 iter = g_hash_table_lookup(service_hash, service->identifier);
1274 g_sequence_sort_changed(iter, service_compare, NULL);
1278 * connman_service_create_from_network:
1279 * @network: network structure
1281 * Look up service by network and if not found, create one
1283 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
1285 struct connman_service *service;
1286 const char *ident, *group;
1289 service = __connman_service_lookup_from_network(network);
1290 if (service != NULL) {
1291 update_from_network(service, network);
1295 ident = __connman_network_get_ident(network);
1299 group = __connman_network_get_group(network);
1303 name = g_strdup_printf("%s_%s_%s",
1304 __connman_network_get_type(network), ident, group);
1306 service = __connman_service_get(name);
1307 if (service == NULL)
1310 if (service->path != NULL) {
1311 update_from_network(service, network);
1313 __connman_profile_changed();
1315 __connman_service_put(service);
1320 service->type = convert_network_type(network);
1322 service->state = CONNMAN_SERVICE_STATE_IDLE;
1324 update_from_network(service, network);
1326 service_register(service);
1334 static int service_load(struct connman_service *service)
1337 gchar *pathname, *data = NULL;
1341 DBG("service %p", service);
1343 if (service->profile == NULL)
1346 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1347 if (pathname == NULL)
1350 keyfile = g_key_file_new();
1352 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1359 if (g_key_file_load_from_data(keyfile, data, length,
1360 0, NULL) == FALSE) {
1367 switch (service->type) {
1368 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1369 case CONNMAN_SERVICE_TYPE_ETHERNET:
1371 case CONNMAN_SERVICE_TYPE_WIFI:
1372 case CONNMAN_SERVICE_TYPE_WIMAX:
1373 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1374 case CONNMAN_SERVICE_TYPE_CELLULAR:
1375 service->favorite = g_key_file_get_boolean(keyfile,
1376 service->identifier, "Favorite", NULL);
1378 str = g_key_file_get_string(keyfile,
1379 service->identifier, "Failure", NULL);
1381 service->state = CONNMAN_SERVICE_STATE_FAILURE;
1382 service->error = string2error(str);
1387 str = g_key_file_get_string(keyfile,
1388 service->identifier, "Modified", NULL);
1390 g_time_val_from_iso8601(str, &service->modified);
1394 str = g_key_file_get_string(keyfile,
1395 service->identifier, "Passphrase", NULL);
1397 g_free(service->passphrase);
1398 service->passphrase = str;
1401 __connman_ipconfig_load(service->ipconfig, keyfile,
1402 service->identifier, "IPv4.");
1404 g_key_file_free(keyfile);
1409 static int service_save(struct connman_service *service)
1412 gchar *pathname, *data = NULL;
1416 DBG("service %p", service);
1418 if (service->profile == NULL)
1421 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1422 if (pathname == NULL)
1425 keyfile = g_key_file_new();
1427 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1431 if (g_key_file_load_from_data(keyfile, data, length,
1439 if (service->name != NULL)
1440 g_key_file_set_string(keyfile, service->identifier,
1441 "Name", service->name);
1443 switch (service->type) {
1444 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1445 case CONNMAN_SERVICE_TYPE_ETHERNET:
1447 case CONNMAN_SERVICE_TYPE_WIFI:
1448 case CONNMAN_SERVICE_TYPE_WIMAX:
1449 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1450 case CONNMAN_SERVICE_TYPE_CELLULAR:
1451 g_key_file_set_boolean(keyfile, service->identifier,
1452 "Favorite", service->favorite);
1454 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
1455 const char *failure = error2string(service->error);
1456 if (failure != NULL)
1457 g_key_file_set_string(keyfile,
1458 service->identifier,
1459 "Failure", failure);
1461 g_key_file_remove_key(keyfile, service->identifier,
1467 str = g_time_val_to_iso8601(&service->modified);
1469 g_key_file_set_string(keyfile, service->identifier,
1474 if (service->passphrase != NULL)
1475 g_key_file_set_string(keyfile, service->identifier,
1476 "Passphrase", service->passphrase);
1478 g_key_file_remove_key(keyfile, service->identifier,
1479 "Passphrase", NULL);
1481 __connman_ipconfig_save(service->ipconfig, keyfile,
1482 service->identifier, "IPv4.");
1484 data = g_key_file_to_data(keyfile, &length, NULL);
1486 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1487 connman_error("Failed to store service information");
1492 g_key_file_free(keyfile);
1499 static struct connman_storage service_storage = {
1501 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1502 .service_load = service_load,
1503 .service_save = service_save,
1506 int __connman_service_init(void)
1510 connection = connman_dbus_get_connection();
1512 if (connman_storage_register(&service_storage) < 0)
1513 connman_error("Failed to register service storage");
1515 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1518 service_list = g_sequence_new(service_free);
1523 void __connman_service_cleanup(void)
1527 g_sequence_free(service_list);
1528 service_list = NULL;
1530 g_hash_table_destroy(service_hash);
1531 service_hash = NULL;
1533 connman_storage_unregister(&service_storage);
1535 dbus_connection_unref(connection);