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)
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_PIN_MISSING:
191 return "pin-missing";
192 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
193 return "dhcp-failed";
194 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
195 return "connect-failed";
201 static enum connman_service_error string2error(const char *error)
203 if (g_strcmp0(error, "dhcp-failed") == 0)
204 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
205 else if (g_strcmp0(error, "pin-missing") == 0)
206 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
208 return CONNMAN_SERVICE_ERROR_UNKNOWN;
211 static void state_changed(struct connman_service *service)
214 DBusMessageIter entry, value;
215 const char *str, *key = "State";
217 if (service->path == NULL)
220 str = state2string(service->state);
224 signal = dbus_message_new_signal(service->path,
225 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
229 dbus_message_iter_init_append(signal, &entry);
231 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
233 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
234 DBUS_TYPE_STRING_AS_STRING, &value);
235 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
236 dbus_message_iter_close_container(&entry, &value);
238 g_dbus_send_message(connection, signal);
241 static void strength_changed(struct connman_service *service)
244 DBusMessageIter entry, value;
245 const char *key = "Strength";
247 if (service->path == NULL)
250 if (service->strength == 0)
253 signal = dbus_message_new_signal(service->path,
254 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
258 dbus_message_iter_init_append(signal, &entry);
260 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
262 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
263 DBUS_TYPE_BYTE_AS_STRING, &value);
264 dbus_message_iter_append_basic(&value, DBUS_TYPE_BYTE,
266 dbus_message_iter_close_container(&entry, &value);
268 g_dbus_send_message(connection, signal);
271 static DBusMessage *get_properties(DBusConnection *conn,
272 DBusMessage *msg, void *user_data)
274 struct connman_service *service = user_data;
276 DBusMessageIter array, dict;
279 DBG("service %p", service);
281 reply = dbus_message_new_method_return(msg);
285 dbus_message_iter_init_append(reply, &array);
287 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
288 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
289 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
290 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
292 str = type2string(service->type);
294 connman_dbus_dict_append_variant(&dict, "Type",
295 DBUS_TYPE_STRING, &str);
297 str = mode2string(service->mode);
299 connman_dbus_dict_append_variant(&dict, "Mode",
300 DBUS_TYPE_STRING, &str);
302 str = security2string(service->security);
304 connman_dbus_dict_append_variant(&dict, "Security",
305 DBUS_TYPE_STRING, &str);
307 str = state2string(service->state);
309 connman_dbus_dict_append_variant(&dict, "State",
310 DBUS_TYPE_STRING, &str);
312 str = error2string(service->error);
314 connman_dbus_dict_append_variant(&dict, "Error",
315 DBUS_TYPE_STRING, &str);
317 if (service->strength > 0)
318 connman_dbus_dict_append_variant(&dict, "Strength",
319 DBUS_TYPE_BYTE, &service->strength);
321 connman_dbus_dict_append_variant(&dict, "Favorite",
322 DBUS_TYPE_BOOLEAN, &service->favorite);
324 if (service->name != NULL)
325 connman_dbus_dict_append_variant(&dict, "Name",
326 DBUS_TYPE_STRING, &service->name);
328 if (service->passphrase != NULL &&
329 __connman_security_check_privilege(msg,
330 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
331 connman_dbus_dict_append_variant(&dict, "Passphrase",
332 DBUS_TYPE_STRING, &service->passphrase);
334 __connman_ipconfig_append_ipv4(service->ipconfig, &dict, "IPv4.");
336 dbus_message_iter_close_container(&array, &dict);
341 static DBusMessage *set_property(DBusConnection *conn,
342 DBusMessage *msg, void *user_data)
344 struct connman_service *service = user_data;
345 DBusMessageIter iter, value;
349 DBG("service %p", service);
351 if (dbus_message_iter_init(msg, &iter) == FALSE)
352 return __connman_error_invalid_arguments(msg);
354 dbus_message_iter_get_basic(&iter, &name);
355 dbus_message_iter_next(&iter);
356 dbus_message_iter_recurse(&iter, &value);
358 if (__connman_security_check_privilege(msg,
359 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
360 return __connman_error_permission_denied(msg);
362 type = dbus_message_iter_get_arg_type(&value);
364 if (g_str_equal(name, "Passphrase") == TRUE) {
365 const char *passphrase;
367 if (type != DBUS_TYPE_STRING)
368 return __connman_error_invalid_arguments(msg);
370 if (__connman_security_check_privilege(msg,
371 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
372 return __connman_error_permission_denied(msg);
374 dbus_message_iter_get_basic(&value, &passphrase);
376 g_free(service->passphrase);
377 service->passphrase = g_strdup(passphrase);
379 if (service->network != NULL)
380 connman_network_set_string(service->network,
381 "WiFi.Passphrase", service->passphrase);
383 __connman_storage_save_service(service);
384 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
387 err = __connman_ipconfig_set_ipv4(service->ipconfig,
390 return __connman_error_failed(msg, -err);
392 return __connman_error_invalid_property(msg);
394 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
397 static DBusMessage *clear_property(DBusConnection *conn,
398 DBusMessage *msg, void *user_data)
400 struct connman_service *service = user_data;
403 DBG("service %p", service);
405 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
408 if (__connman_security_check_privilege(msg,
409 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
410 return __connman_error_permission_denied(msg);
412 if (g_str_equal(name, "Error") == TRUE) {
413 service->state = CONNMAN_SERVICE_STATE_IDLE;
414 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
415 state_changed(service);
417 g_get_current_time(&service->modified);
418 __connman_storage_save_service(service);
420 return __connman_error_invalid_property(msg);
422 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
425 static connman_bool_t is_connecting(struct connman_service *service)
427 switch (service->state) {
428 case CONNMAN_SERVICE_STATE_UNKNOWN:
429 case CONNMAN_SERVICE_STATE_IDLE:
430 case CONNMAN_SERVICE_STATE_CARRIER:
431 case CONNMAN_SERVICE_STATE_FAILURE:
432 case CONNMAN_SERVICE_STATE_DISCONNECT:
433 case CONNMAN_SERVICE_STATE_READY:
435 case CONNMAN_SERVICE_STATE_ASSOCIATION:
436 case CONNMAN_SERVICE_STATE_CONFIGURATION:
443 static struct connman_service *find_pending_service(void)
445 struct connman_service *service;
448 iter = g_sequence_get_begin_iter(service_list);
450 while (g_sequence_iter_is_end(iter) == FALSE) {
451 service = g_sequence_get(iter);
452 if (service->pending != NULL)
455 iter = g_sequence_iter_next(iter);
461 static connman_bool_t is_ignore(struct connman_service *service)
463 if (service->ignore == TRUE)
466 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
472 static void __connman_service_auto_connect(void)
474 struct connman_service *service;
479 service = find_pending_service();
483 iter = g_sequence_get_begin_iter(service_list);
484 if (g_sequence_iter_is_end(iter) == TRUE)
487 service = g_sequence_get(iter);
489 while (is_ignore(service) == TRUE) {
490 iter = g_sequence_iter_next(iter);
491 if (g_sequence_iter_is_end(iter))
493 service = g_sequence_get(iter);
496 if (service->favorite == FALSE)
499 if (service->state == CONNMAN_SERVICE_STATE_READY)
502 if (is_connecting(service) == TRUE)
505 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
506 __connman_service_connect(service);
509 static gboolean connect_timeout(gpointer user_data)
511 struct connman_service *service = user_data;
512 connman_bool_t auto_connect = FALSE;
514 DBG("service %p", service);
516 service->timeout = 0;
518 if (service->network != NULL) {
519 connman_bool_t connected;
521 connected = connman_network_get_connected(service->network);
522 if (connected == TRUE) {
523 __connman_service_indicate_state(service,
524 CONNMAN_SERVICE_STATE_READY);
528 __connman_network_disconnect(service->network);
529 } else if (service->device != NULL) {
530 connman_bool_t disconnected;
532 disconnected = connman_device_get_disconnected(service->device);
533 if (disconnected == FALSE) {
534 __connman_service_indicate_state(service,
535 CONNMAN_SERVICE_STATE_READY);
539 __connman_device_disconnect(service->device);
542 if (service->pending != NULL) {
545 reply = __connman_error_operation_timeout(service->pending);
547 g_dbus_send_message(connection, reply);
549 dbus_message_unref(service->pending);
550 service->pending = NULL;
554 __connman_service_indicate_state(service,
555 CONNMAN_SERVICE_STATE_FAILURE);
557 if (auto_connect == TRUE)
558 __connman_service_auto_connect();
563 static DBusMessage *connect_service(DBusConnection *conn,
564 DBusMessage *msg, void *user_data)
566 struct connman_service *service = user_data;
569 DBG("service %p", service);
571 if (service->pending != NULL)
572 return __connman_error_in_progress(msg);
574 service->ignore = FALSE;
576 service->pending = dbus_message_ref(msg);
578 err = __connman_service_connect(service);
580 if (err != -EINPROGRESS) {
581 dbus_message_unref(service->pending);
582 service->pending = NULL;
584 return __connman_error_failed(msg, -err);
590 dbus_message_unref(service->pending);
591 service->pending = NULL;
593 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
596 static DBusMessage *disconnect_service(DBusConnection *conn,
597 DBusMessage *msg, void *user_data)
599 struct connman_service *service = user_data;
602 DBG("service %p", service);
604 if (service->pending != NULL) {
607 reply = __connman_error_operation_aborted(service->pending);
609 g_dbus_send_message(conn, reply);
611 dbus_message_unref(service->pending);
612 service->pending = NULL;
614 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
617 service->ignore = TRUE;
619 err = __connman_service_disconnect(service);
621 if (err != -EINPROGRESS)
622 return __connman_error_failed(msg, -err);
627 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
630 static DBusMessage *remove_service(DBusConnection *conn,
631 DBusMessage *msg, void *user_data)
633 struct connman_service *service = user_data;
635 DBG("service %p", service);
637 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
638 return __connman_error_not_supported(msg);
640 if (service->favorite == FALSE)
641 return __connman_error_not_supported(msg);
643 if (service->network != NULL)
644 __connman_network_disconnect(service->network);
646 g_free(service->passphrase);
647 service->passphrase = NULL;
649 connman_service_set_favorite(service, FALSE);
650 __connman_storage_save_service(service);
652 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
655 static DBusMessage *move_before(DBusConnection *conn,
656 DBusMessage *msg, void *user_data)
658 struct connman_service *service = user_data;
659 struct connman_service *target;
661 GSequenceIter *src, *dst;
663 DBG("service %p", service);
665 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
668 if (service->favorite == FALSE)
669 return __connman_error_not_supported(msg);
671 target = find_service(path);
672 if (target == NULL || target->favorite == FALSE || target == service)
673 return __connman_error_invalid_service(msg);
675 DBG("target %s", target->identifier);
677 g_get_current_time(&service->modified);
678 __connman_storage_save_service(service);
680 src = g_hash_table_lookup(service_hash, service->identifier);
681 dst = g_hash_table_lookup(service_hash, target->identifier);
683 g_sequence_move(src, dst);
685 __connman_profile_changed();
687 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
690 static DBusMessage *move_after(DBusConnection *conn,
691 DBusMessage *msg, void *user_data)
693 struct connman_service *service = user_data;
694 struct connman_service *target;
697 DBG("service %p", service);
699 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
702 if (service->favorite == FALSE)
703 return __connman_error_not_supported(msg);
705 target = find_service(path);
706 if (target == NULL || target->favorite == FALSE || target == service)
707 return __connman_error_invalid_service(msg);
709 DBG("target %s", target->identifier);
711 g_get_current_time(&service->modified);
712 __connman_storage_save_service(service);
714 return __connman_error_not_implemented(msg);
717 static GDBusMethodTable service_methods[] = {
718 { "GetProperties", "", "a{sv}", get_properties },
719 { "SetProperty", "sv", "", set_property },
720 { "ClearProperty", "s", "", clear_property },
721 { "Connect", "", "", connect_service,
722 G_DBUS_METHOD_FLAG_ASYNC },
723 { "Disconnect", "", "", disconnect_service },
724 { "Remove", "", "", remove_service },
725 { "MoveBefore", "o", "", move_before },
726 { "MoveAfter", "o", "", move_after },
730 static GDBusSignalTable service_signals[] = {
731 { "PropertyChanged", "sv" },
735 static void service_free(gpointer user_data)
737 struct connman_service *service = user_data;
738 char *path = service->path;
740 DBG("service %p", service);
742 g_hash_table_remove(service_hash, service->identifier);
744 if (service->timeout > 0) {
745 g_source_remove(service->timeout);
746 service->timeout = 0;
749 if (service->pending != NULL) {
750 dbus_message_unref(service->pending);
751 service->pending = NULL;
754 service->path = NULL;
757 __connman_profile_changed();
759 g_dbus_unregister_interface(connection, path,
760 CONNMAN_SERVICE_INTERFACE);
764 if (service->network != NULL)
765 connman_network_unref(service->network);
767 connman_ipconfig_unref(service->ipconfig);
769 g_free(service->profile);
770 g_free(service->name);
771 g_free(service->passphrase);
772 g_free(service->identifier);
777 * __connman_service_put:
778 * @service: service structure
780 * Release service if no longer needed
782 void __connman_service_put(struct connman_service *service)
784 DBG("service %p", service);
786 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
789 iter = g_hash_table_lookup(service_hash, service->identifier);
791 g_sequence_remove(iter);
793 service_free(service);
797 static void __connman_service_initialize(struct connman_service *service)
799 DBG("service %p", service);
801 service->refcount = 1;
803 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
804 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
805 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
806 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
808 service->favorite = FALSE;
809 service->hidden = FALSE;
811 service->ignore = FALSE;
817 * connman_service_create:
819 * Allocate a new service.
821 * Returns: a newly-allocated #connman_service structure
823 struct connman_service *connman_service_create(void)
825 struct connman_service *service;
827 service = g_try_new0(struct connman_service, 1);
831 DBG("service %p", service);
833 __connman_service_initialize(service);
835 service->ipconfig = connman_ipconfig_create();
836 if (service->ipconfig == NULL) {
841 connman_ipconfig_set_method(service->ipconfig,
842 CONNMAN_IPCONFIG_METHOD_DHCP);
848 * connman_service_ref:
849 * @service: service structure
851 * Increase reference counter of service
853 struct connman_service *connman_service_ref(struct connman_service *service)
855 g_atomic_int_inc(&service->refcount);
861 * connman_service_unref:
862 * @service: service structure
864 * Decrease reference counter of service
866 void connman_service_unref(struct connman_service *service)
868 __connman_service_put(service);
871 static gint service_compare(gconstpointer a, gconstpointer b,
874 struct connman_service *service_a = (void *) a;
875 struct connman_service *service_b = (void *) b;
877 if (service_a->state != service_b->state) {
878 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
880 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
884 if (service_a->order > service_b->order)
887 if (service_a->order < service_b->order)
890 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
893 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
896 return (gint) service_b->strength - (gint) service_a->strength;
900 * connman_service_set_favorite:
901 * @service: service structure
902 * @favorite: favorite value
904 * Change the favorite setting of service
906 int connman_service_set_favorite(struct connman_service *service,
907 connman_bool_t favorite)
911 iter = g_hash_table_lookup(service_hash, service->identifier);
915 if (service->favorite == favorite)
918 service->favorite = favorite;
920 g_sequence_sort_changed(iter, service_compare, NULL);
922 __connman_profile_changed();
927 int __connman_service_set_carrier(struct connman_service *service,
928 connman_bool_t carrier)
930 DBG("service %p carrier %d", service, carrier);
935 switch (service->type) {
936 case CONNMAN_SERVICE_TYPE_UNKNOWN:
937 case CONNMAN_SERVICE_TYPE_WIFI:
938 case CONNMAN_SERVICE_TYPE_WIMAX:
939 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
940 case CONNMAN_SERVICE_TYPE_CELLULAR:
942 case CONNMAN_SERVICE_TYPE_ETHERNET:
946 if (carrier == FALSE) {
947 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
948 state_changed(service);
950 service->state = CONNMAN_SERVICE_STATE_IDLE;
951 state_changed(service);
953 service->state = CONNMAN_SERVICE_STATE_CARRIER;
954 state_changed(service);
957 return connman_service_set_favorite(service, carrier);
960 int __connman_service_indicate_state(struct connman_service *service,
961 enum connman_service_state state)
965 DBG("service %p state %d", service, state);
970 if (state == CONNMAN_SERVICE_STATE_CARRIER)
971 return __connman_service_set_carrier(service, TRUE);
973 if (service->state == state)
976 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
977 state == CONNMAN_SERVICE_STATE_DISCONNECT)
980 if (state == CONNMAN_SERVICE_STATE_IDLE &&
981 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
982 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
983 state_changed(service);
986 service->state = state;
987 state_changed(service);
989 if (state == CONNMAN_SERVICE_STATE_READY) {
990 connman_service_set_favorite(service, TRUE);
992 if (service->timeout > 0) {
993 g_source_remove(service->timeout);
994 service->timeout = 0;
997 if (service->pending != NULL) {
998 g_dbus_send_reply(connection, service->pending,
1001 dbus_message_unref(service->pending);
1002 service->pending = NULL;
1005 g_get_current_time(&service->modified);
1006 __connman_storage_save_service(service);
1009 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
1010 if (service->timeout > 0) {
1011 g_source_remove(service->timeout);
1012 service->timeout = 0;
1015 if (service->pending != NULL) {
1018 reply = __connman_error_failed(service->pending, EIO);
1020 g_dbus_send_message(connection, reply);
1022 dbus_message_unref(service->pending);
1023 service->pending = NULL;
1026 g_get_current_time(&service->modified);
1027 __connman_storage_save_service(service);
1029 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1031 iter = g_hash_table_lookup(service_hash, service->identifier);
1033 g_sequence_sort_changed(iter, service_compare, NULL);
1035 __connman_profile_changed();
1040 int __connman_service_indicate_error(struct connman_service *service,
1041 enum connman_service_error error)
1043 DBG("service %p error %d", service, error);
1045 if (service == NULL)
1048 service->error = error;
1050 return __connman_service_indicate_state(service,
1051 CONNMAN_SERVICE_STATE_FAILURE);
1054 int __connman_service_indicate_default(struct connman_service *service)
1056 DBG("service %p", service);
1061 int __connman_service_connect(struct connman_service *service)
1065 DBG("service %p", service);
1067 if (service->state == CONNMAN_SERVICE_STATE_READY)
1070 if (service->network != NULL) {
1071 unsigned int ssid_len;
1073 if (connman_network_get_blob(service->network, "WiFi.SSID",
1076 connman_network_set_string(service->network,
1077 "WiFi.Passphrase", service->passphrase);
1079 err = __connman_network_connect(service->network);
1080 } else if (service->device != NULL) {
1081 if (service->favorite == FALSE)
1084 err = __connman_device_connect(service->device);
1089 if (err != -EINPROGRESS)
1092 service->timeout = g_timeout_add_seconds(45,
1093 connect_timeout, service);
1095 return -EINPROGRESS;
1101 int __connman_service_disconnect(struct connman_service *service)
1105 DBG("service %p", service);
1107 if (service->network != NULL) {
1108 err = __connman_network_disconnect(service->network);
1109 } else if (service->device != NULL) {
1110 if (service->favorite == FALSE)
1112 err = __connman_device_disconnect(service->device);
1117 if (err != -EINPROGRESS)
1120 return -EINPROGRESS;
1127 * __connman_service_lookup:
1128 * @identifier: service identifier
1130 * Look up a service by identifier (reference count will not be increased)
1132 static struct connman_service *__connman_service_lookup(const char *identifier)
1134 GSequenceIter *iter;
1136 iter = g_hash_table_lookup(service_hash, identifier);
1138 return g_sequence_get(iter);
1143 int __connman_service_create_and_connect(DBusMessage *msg)
1145 struct connman_service *service;
1146 struct connman_device *device;
1147 DBusMessageIter iter, array;
1148 const char *mode = "managed", *security = "none";
1149 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
1154 dbus_message_iter_init(msg, &iter);
1155 dbus_message_iter_recurse(&iter, &array);
1157 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1158 DBusMessageIter entry, value;
1161 dbus_message_iter_recurse(&array, &entry);
1162 dbus_message_iter_get_basic(&entry, &key);
1164 dbus_message_iter_next(&entry);
1165 dbus_message_iter_recurse(&entry, &value);
1167 switch (dbus_message_iter_get_arg_type(&value)) {
1168 case DBUS_TYPE_STRING:
1169 if (g_str_equal(key, "Type") == TRUE)
1170 dbus_message_iter_get_basic(&value, &type);
1171 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
1172 g_str_equal(key, "Mode") == TRUE)
1173 dbus_message_iter_get_basic(&value, &mode);
1174 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
1175 g_str_equal(key, "Security") == TRUE)
1176 dbus_message_iter_get_basic(&value, &security);
1177 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
1178 g_str_equal(key, "Passphrase") == TRUE)
1179 dbus_message_iter_get_basic(&value, &passphrase);
1180 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
1181 g_str_equal(key, "SSID") == TRUE)
1182 dbus_message_iter_get_basic(&value, &ssid);
1185 dbus_message_iter_next(&array);
1191 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
1197 device = __connman_element_find_device(CONNMAN_DEVICE_TYPE_WIFI);
1201 ident = __connman_device_get_ident(device);
1205 group = connman_wifi_build_group_name((unsigned char *) ssid,
1206 strlen(ssid), mode, security);
1210 name = g_strdup_printf("%s_%s_%s", type, ident, group);
1214 service = __connman_service_lookup(name);
1218 if (service != NULL) {
1219 if (passphrase != NULL) {
1220 g_free(service->passphrase);
1221 service->passphrase = g_strdup(passphrase);
1224 err = __connman_service_connect(service);
1225 if (err < 0 && err != -EINPROGRESS)
1228 g_dbus_send_reply(connection, msg,
1229 DBUS_TYPE_OBJECT_PATH, &service->path,
1239 * __connman_service_get:
1240 * @identifier: service identifier
1242 * Look up a service by identifier or create a new one if not found
1244 static struct connman_service *__connman_service_get(const char *identifier)
1246 struct connman_service *service;
1247 GSequenceIter *iter;
1249 iter = g_hash_table_lookup(service_hash, identifier);
1251 service = g_sequence_get(iter);
1252 if (service != NULL)
1253 g_atomic_int_inc(&service->refcount);
1257 service = connman_service_create();
1258 if (service == NULL)
1261 DBG("service %p", service);
1263 service->identifier = g_strdup(identifier);
1265 service->profile = g_strdup(__connman_profile_active_ident());
1267 __connman_storage_load_service(service);
1269 iter = g_sequence_insert_sorted(service_list, service,
1270 service_compare, NULL);
1272 g_hash_table_insert(service_hash, service->identifier, iter);
1277 static int service_register(struct connman_service *service)
1279 const char *path = __connman_profile_active_path();
1280 GSequenceIter *iter;
1282 DBG("service %p", service);
1284 if (service->path != NULL)
1287 service->path = g_strdup_printf("%s/%s", path, service->identifier);
1289 DBG("path %s", service->path);
1291 g_dbus_register_interface(connection, service->path,
1292 CONNMAN_SERVICE_INTERFACE,
1293 service_methods, service_signals,
1294 NULL, service, NULL);
1296 __connman_storage_load_service(service);
1298 iter = g_hash_table_lookup(service_hash, service->identifier);
1300 g_sequence_sort_changed(iter, service_compare, NULL);
1302 __connman_profile_changed();
1308 * connman_service_lookup_from_device:
1309 * @device: device structure
1311 * Look up a service by device (reference count will not be increased)
1313 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
1315 struct connman_service *service;
1319 ident = __connman_device_get_ident(device);
1323 name = g_strdup_printf("%s_%s",
1324 __connman_device_get_type(device), ident);
1326 service = __connman_service_lookup(name);
1333 static enum connman_service_type convert_device_type(struct connman_device *device)
1335 enum connman_device_type type = connman_device_get_type(device);
1338 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1339 case CONNMAN_DEVICE_TYPE_VENDOR:
1340 case CONNMAN_DEVICE_TYPE_WIFI:
1341 case CONNMAN_DEVICE_TYPE_WIMAX:
1342 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1343 case CONNMAN_DEVICE_TYPE_GPS:
1344 case CONNMAN_DEVICE_TYPE_MBM:
1345 case CONNMAN_DEVICE_TYPE_HSO:
1346 case CONNMAN_DEVICE_TYPE_NOZOMI:
1347 case CONNMAN_DEVICE_TYPE_HUAWEI:
1348 case CONNMAN_DEVICE_TYPE_NOVATEL:
1350 case CONNMAN_DEVICE_TYPE_ETHERNET:
1351 return CONNMAN_SERVICE_TYPE_ETHERNET;
1354 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1358 * connman_service_create_from_device:
1359 * @device: device structure
1361 * Look up service by device and if not found, create one
1363 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
1365 struct connman_service *service;
1369 ident = __connman_device_get_ident(device);
1373 name = g_strdup_printf("%s_%s",
1374 __connman_device_get_type(device), ident);
1376 service = __connman_service_get(name);
1377 if (service == NULL)
1380 if (service->path != NULL) {
1381 __connman_service_put(service);
1386 service->type = convert_device_type(device);
1388 service->device = device;
1390 service_register(service);
1392 if (service->favorite == TRUE)
1393 __connman_service_auto_connect();
1402 * connman_service_lookup_from_network:
1403 * @network: network structure
1405 * Look up a service by network (reference count will not be increased)
1407 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
1409 struct connman_service *service;
1410 const char *ident, *group;
1413 ident = __connman_network_get_ident(network);
1417 group = connman_network_get_group(network);
1421 name = g_strdup_printf("%s_%s_%s",
1422 __connman_network_get_type(network), ident, group);
1424 service = __connman_service_lookup(name);
1431 unsigned int __connman_service_get_order(struct connman_service *service)
1433 GSequenceIter *iter;
1435 if (service == NULL)
1438 if (service->favorite == FALSE)
1441 iter = g_hash_table_lookup(service_hash, service->identifier);
1443 if (g_sequence_iter_get_position(iter) == 0)
1447 return service->order;
1450 static enum connman_service_type convert_network_type(struct connman_network *network)
1452 enum connman_network_type type = connman_network_get_type(network);
1455 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1456 case CONNMAN_NETWORK_TYPE_VENDOR:
1457 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1458 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1460 case CONNMAN_NETWORK_TYPE_MBM:
1461 case CONNMAN_NETWORK_TYPE_HSO:
1462 return CONNMAN_SERVICE_TYPE_CELLULAR;
1463 case CONNMAN_NETWORK_TYPE_WIFI:
1464 return CONNMAN_SERVICE_TYPE_WIFI;
1465 case CONNMAN_NETWORK_TYPE_WIMAX:
1466 return CONNMAN_SERVICE_TYPE_WIMAX;
1469 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1472 static enum connman_service_mode convert_wifi_mode(const char *mode)
1475 return CONNMAN_SERVICE_MODE_UNKNOWN;
1476 else if (g_str_equal(mode, "managed") == TRUE)
1477 return CONNMAN_SERVICE_MODE_MANAGED;
1478 else if (g_str_equal(mode, "adhoc") == TRUE)
1479 return CONNMAN_SERVICE_MODE_ADHOC;
1481 return CONNMAN_SERVICE_MODE_UNKNOWN;
1484 static enum connman_service_mode convert_wifi_security(const char *security)
1486 if (security == NULL)
1487 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1488 else if (g_str_equal(security, "none") == TRUE)
1489 return CONNMAN_SERVICE_SECURITY_NONE;
1490 else if (g_str_equal(security, "wep") == TRUE)
1491 return CONNMAN_SERVICE_SECURITY_WEP;
1492 else if (g_str_equal(security, "wpa") == TRUE)
1493 return CONNMAN_SERVICE_SECURITY_WPA;
1494 else if (g_str_equal(security, "rsn") == TRUE)
1495 return CONNMAN_SERVICE_SECURITY_RSN;
1497 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1500 static void update_from_network(struct connman_service *service,
1501 struct connman_network *network)
1503 connman_uint8_t strength = service->strength;
1504 GSequenceIter *iter;
1507 if (service->state == CONNMAN_SERVICE_STATE_READY)
1510 if (is_connecting(service) == TRUE)
1513 str = connman_network_get_string(network, "Name");
1515 g_free(service->name);
1516 service->name = g_strdup(str);
1517 service->hidden = FALSE;
1519 g_free(service->name);
1520 service->name = NULL;
1521 service->hidden = TRUE;
1524 service->strength = connman_network_get_uint8(network, "Strength");
1526 str = connman_network_get_string(network, "WiFi.Mode");
1527 service->mode = convert_wifi_mode(str);
1529 str = connman_network_get_string(network, "WiFi.Security");
1530 service->security = convert_wifi_security(str);
1532 if (service->strength > strength && service->network != NULL) {
1533 connman_network_unref(service->network);
1534 service->network = NULL;
1536 strength_changed(service);
1539 if (service->network == NULL) {
1540 service->network = connman_network_ref(network);
1542 str = connman_network_get_string(network, "WiFi.Passphrase");
1544 g_free(service->passphrase);
1545 service->passphrase = g_strdup(str);
1549 iter = g_hash_table_lookup(service_hash, service->identifier);
1551 g_sequence_sort_changed(iter, service_compare, NULL);
1555 * connman_service_create_from_network:
1556 * @network: network structure
1558 * Look up service by network and if not found, create one
1560 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
1562 struct connman_service *service;
1563 const char *ident, *group;
1566 service = __connman_service_lookup_from_network(network);
1567 if (service != NULL) {
1568 update_from_network(service, network);
1572 ident = __connman_network_get_ident(network);
1576 group = connman_network_get_group(network);
1580 name = g_strdup_printf("%s_%s_%s",
1581 __connman_network_get_type(network), ident, group);
1583 service = __connman_service_get(name);
1584 if (service == NULL)
1587 if (service->path != NULL) {
1588 update_from_network(service, network);
1590 __connman_profile_changed();
1592 __connman_service_put(service);
1597 service->type = convert_network_type(network);
1599 service->state = CONNMAN_SERVICE_STATE_IDLE;
1601 update_from_network(service, network);
1603 service_register(service);
1605 if (service->favorite == TRUE)
1606 __connman_service_auto_connect();
1614 static int service_load(struct connman_service *service)
1617 gchar *pathname, *data = NULL;
1620 unsigned int ssid_len;
1623 DBG("service %p", service);
1625 if (service->profile == NULL)
1628 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1629 if (pathname == NULL)
1632 keyfile = g_key_file_new();
1634 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1641 if (g_key_file_load_from_data(keyfile, data, length,
1642 0, NULL) == FALSE) {
1649 switch (service->type) {
1650 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1651 case CONNMAN_SERVICE_TYPE_ETHERNET:
1653 case CONNMAN_SERVICE_TYPE_WIFI:
1654 if (service->name == NULL) {
1657 name = g_key_file_get_string(keyfile,
1658 service->identifier, "Name", NULL);
1660 g_free(service->name);
1661 service->name = name;
1664 if (service->network != NULL)
1665 connman_network_set_name(service->network,
1669 if (service->network &&
1670 connman_network_get_blob(service->network,
1671 "WiFi.SSID", &ssid_len) == NULL) {
1674 hex_ssid = g_key_file_get_string(keyfile,
1675 service->identifier,
1678 if (hex_ssid != NULL) {
1680 unsigned int i, j = 0, hex;
1681 size_t hex_ssid_len = strlen(hex_ssid);
1683 ssid = g_try_malloc0(hex_ssid_len / 2);
1690 for (i = 0; i < hex_ssid_len; i += 2) {
1691 sscanf(hex_ssid + i, "%02x", &hex);
1695 connman_network_set_blob(service->network,
1696 "WiFi.SSID", ssid, hex_ssid_len / 2);
1703 case CONNMAN_SERVICE_TYPE_WIMAX:
1704 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1705 case CONNMAN_SERVICE_TYPE_CELLULAR:
1706 service->favorite = g_key_file_get_boolean(keyfile,
1707 service->identifier, "Favorite", NULL);
1709 str = g_key_file_get_string(keyfile,
1710 service->identifier, "Failure", NULL);
1712 service->state = CONNMAN_SERVICE_STATE_FAILURE;
1713 service->error = string2error(str);
1718 str = g_key_file_get_string(keyfile,
1719 service->identifier, "Modified", NULL);
1721 g_time_val_from_iso8601(str, &service->modified);
1725 str = g_key_file_get_string(keyfile,
1726 service->identifier, "Passphrase", NULL);
1728 g_free(service->passphrase);
1729 service->passphrase = str;
1732 __connman_ipconfig_load(service->ipconfig, keyfile,
1733 service->identifier, "IPv4.");
1736 g_key_file_free(keyfile);
1741 static int service_save(struct connman_service *service)
1744 gchar *pathname, *data = NULL;
1749 DBG("service %p", service);
1751 if (service->profile == NULL)
1754 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1755 if (pathname == NULL)
1758 keyfile = g_key_file_new();
1760 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1764 if (g_key_file_load_from_data(keyfile, data, length,
1772 if (service->name != NULL)
1773 g_key_file_set_string(keyfile, service->identifier,
1774 "Name", service->name);
1776 switch (service->type) {
1777 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1778 case CONNMAN_SERVICE_TYPE_ETHERNET:
1780 case CONNMAN_SERVICE_TYPE_WIFI:
1781 if (service->network) {
1782 const unsigned char *ssid;
1783 unsigned int ssid_len = 0;
1785 ssid = connman_network_get_blob(service->network,
1786 "WiFi.SSID", &ssid_len);
1788 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
1789 char *identifier = service->identifier;
1793 str = g_string_sized_new(ssid_len * 2);
1799 for (i = 0; i < ssid_len; i++)
1800 g_string_append_printf(str,
1803 g_key_file_set_string(keyfile, identifier,
1806 g_string_free(str, TRUE);
1811 case CONNMAN_SERVICE_TYPE_WIMAX:
1812 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1813 case CONNMAN_SERVICE_TYPE_CELLULAR:
1814 g_key_file_set_boolean(keyfile, service->identifier,
1815 "Favorite", service->favorite);
1817 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
1818 const char *failure = error2string(service->error);
1819 if (failure != NULL)
1820 g_key_file_set_string(keyfile,
1821 service->identifier,
1822 "Failure", failure);
1824 g_key_file_remove_key(keyfile, service->identifier,
1830 str = g_time_val_to_iso8601(&service->modified);
1832 g_key_file_set_string(keyfile, service->identifier,
1837 if (service->passphrase != NULL)
1838 g_key_file_set_string(keyfile, service->identifier,
1839 "Passphrase", service->passphrase);
1841 g_key_file_remove_key(keyfile, service->identifier,
1842 "Passphrase", NULL);
1844 __connman_ipconfig_save(service->ipconfig, keyfile,
1845 service->identifier, "IPv4.");
1847 data = g_key_file_to_data(keyfile, &length, NULL);
1849 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1850 connman_error("Failed to store service information");
1855 g_key_file_free(keyfile);
1862 static struct connman_storage service_storage = {
1864 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1865 .service_load = service_load,
1866 .service_save = service_save,
1869 int __connman_service_init(void)
1873 connection = connman_dbus_get_connection();
1875 if (connman_storage_register(&service_storage) < 0)
1876 connman_error("Failed to register service storage");
1878 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1881 service_list = g_sequence_new(service_free);
1886 void __connman_service_cleanup(void)
1890 g_sequence_free(service_list);
1891 service_list = NULL;
1893 g_hash_table_destroy(service_hash);
1894 service_hash = NULL;
1896 connman_storage_unregister(&service_storage);
1898 dbus_connection_unref(connection);