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 #define CONNECT_TIMEOUT 120
34 static DBusConnection *connection = NULL;
36 static GSequence *service_list = NULL;
37 static GHashTable *service_hash = NULL;
39 struct connman_service {
43 enum connman_service_type type;
44 enum connman_service_mode mode;
45 enum connman_service_security security;
46 enum connman_service_state state;
47 enum connman_service_error error;
48 connman_uint8_t strength;
49 connman_bool_t favorite;
50 connman_bool_t hidden;
51 connman_bool_t ignore;
52 connman_bool_t autoconnect;
53 connman_bool_t userconnect;
64 connman_bool_t roaming;
65 struct connman_ipconfig *ipconfig;
66 struct connman_network *network;
71 static void append_path(gpointer value, gpointer user_data)
73 struct connman_service *service = value;
74 DBusMessageIter *iter = user_data;
76 if (service->path == NULL || service->hidden == TRUE)
79 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
83 void __connman_service_list(DBusMessageIter *iter)
87 g_sequence_foreach(service_list, append_path, iter);
92 struct connman_service *service;
95 static void compare_path(gpointer value, gpointer user_data)
97 struct connman_service *service = value;
98 struct find_data *data = user_data;
100 if (data->service != NULL)
103 if (g_strcmp0(service->path, data->path) == 0)
104 data->service = service;
107 static struct connman_service *find_service(const char *path)
109 struct find_data data = { .path = path, .service = NULL };
111 DBG("path %s", path);
113 g_sequence_foreach(service_list, compare_path, &data);
118 const char *__connman_service_type2string(enum connman_service_type type)
121 case CONNMAN_SERVICE_TYPE_UNKNOWN:
123 case CONNMAN_SERVICE_TYPE_SYSTEM:
125 case CONNMAN_SERVICE_TYPE_ETHERNET:
127 case CONNMAN_SERVICE_TYPE_WIFI:
129 case CONNMAN_SERVICE_TYPE_WIMAX:
131 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
133 case CONNMAN_SERVICE_TYPE_CELLULAR:
135 case CONNMAN_SERVICE_TYPE_VPN:
142 static const char *mode2string(enum connman_service_mode mode)
145 case CONNMAN_SERVICE_MODE_UNKNOWN:
147 case CONNMAN_SERVICE_MODE_MANAGED:
149 case CONNMAN_SERVICE_MODE_ADHOC:
151 case CONNMAN_SERVICE_MODE_GPRS:
153 case CONNMAN_SERVICE_MODE_EDGE:
155 case CONNMAN_SERVICE_MODE_UMTS:
162 static const char *security2string(enum connman_service_security security)
165 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
167 case CONNMAN_SERVICE_SECURITY_NONE:
169 case CONNMAN_SERVICE_SECURITY_WEP:
171 case CONNMAN_SERVICE_SECURITY_PSK:
173 case CONNMAN_SERVICE_SECURITY_8021X:
175 case CONNMAN_SERVICE_SECURITY_WPA:
177 case CONNMAN_SERVICE_SECURITY_RSN:
184 static const char *state2string(enum connman_service_state state)
187 case CONNMAN_SERVICE_STATE_UNKNOWN:
189 case CONNMAN_SERVICE_STATE_IDLE:
191 case CONNMAN_SERVICE_STATE_ASSOCIATION:
192 return "association";
193 case CONNMAN_SERVICE_STATE_CONFIGURATION:
194 return "configuration";
195 case CONNMAN_SERVICE_STATE_READY:
197 case CONNMAN_SERVICE_STATE_DISCONNECT:
199 case CONNMAN_SERVICE_STATE_FAILURE:
206 static const char *error2string(enum connman_service_error error)
209 case CONNMAN_SERVICE_ERROR_UNKNOWN:
211 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
212 return "out-of-range";
213 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
214 return "pin-missing";
215 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
216 return "dhcp-failed";
217 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
218 return "connect-failed";
224 static enum connman_service_error string2error(const char *error)
226 if (g_strcmp0(error, "dhcp-failed") == 0)
227 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
228 else if (g_strcmp0(error, "pin-missing") == 0)
229 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
231 return CONNMAN_SERVICE_ERROR_UNKNOWN;
234 static struct connman_service *get_default(void)
236 struct connman_service *service;
239 iter = g_sequence_get_begin_iter(service_list);
241 if (g_sequence_iter_is_end(iter) == TRUE)
244 service = g_sequence_get(iter);
246 if (service->state != CONNMAN_SERVICE_STATE_READY)
252 static void default_changed(void)
254 struct connman_service *service = get_default();
256 __connman_notifier_default_changed(service);
259 const char *__connman_service_default(void)
261 struct connman_service *service;
263 service = get_default();
267 return __connman_service_type2string(service->type);
270 static void mode_changed(struct connman_service *service)
273 DBusMessageIter iter;
276 if (service->path == NULL)
279 str = mode2string(service->mode);
283 signal = dbus_message_new_signal(service->path,
284 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
288 dbus_message_iter_init_append(signal, &iter);
290 connman_dbus_property_append_variant(&iter,
291 "Mode", DBUS_TYPE_STRING, &str);
293 g_dbus_send_message(connection, signal);
296 static void state_changed(struct connman_service *service)
299 DBusMessageIter iter;
302 if (service->path == NULL)
305 str = state2string(service->state);
309 signal = dbus_message_new_signal(service->path,
310 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
314 dbus_message_iter_init_append(signal, &iter);
316 connman_dbus_property_append_variant(&iter,
317 "State", DBUS_TYPE_STRING, &str);
319 g_dbus_send_message(connection, signal);
322 static void strength_changed(struct connman_service *service)
325 DBusMessageIter iter;
327 if (service->path == NULL)
330 if (service->strength == 0)
333 signal = dbus_message_new_signal(service->path,
334 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
338 dbus_message_iter_init_append(signal, &iter);
340 connman_dbus_property_append_variant(&iter,
341 "Strength", DBUS_TYPE_BYTE, &service->strength);
343 g_dbus_send_message(connection, signal);
346 static void roaming_changed(struct connman_service *service)
349 DBusMessageIter iter;
351 if (service->path == NULL)
354 signal = dbus_message_new_signal(service->path,
355 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
359 dbus_message_iter_init_append(signal, &iter);
361 connman_dbus_property_append_variant(&iter,
362 "Roaming", DBUS_TYPE_BOOLEAN, &service->roaming);
364 g_dbus_send_message(connection, signal);
367 static void autoconnect_changed(struct connman_service *service)
370 DBusMessageIter iter;
372 if (service->path == NULL)
375 signal = dbus_message_new_signal(service->path,
376 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
380 dbus_message_iter_init_append(signal, &iter);
382 connman_dbus_property_append_variant(&iter,
383 "AutoConnect", DBUS_TYPE_BOOLEAN, &service->autoconnect);
385 g_dbus_send_message(connection, signal);
388 static void passphrase_changed(struct connman_service *service)
391 DBusMessageIter iter;
392 dbus_bool_t required;
394 if (service->path == NULL)
397 switch (service->type) {
398 case CONNMAN_SERVICE_TYPE_UNKNOWN:
399 case CONNMAN_SERVICE_TYPE_SYSTEM:
400 case CONNMAN_SERVICE_TYPE_ETHERNET:
401 case CONNMAN_SERVICE_TYPE_WIMAX:
402 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
403 case CONNMAN_SERVICE_TYPE_CELLULAR:
404 case CONNMAN_SERVICE_TYPE_VPN:
406 case CONNMAN_SERVICE_TYPE_WIFI:
409 switch (service->security) {
410 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
411 case CONNMAN_SERVICE_SECURITY_NONE:
412 case CONNMAN_SERVICE_SECURITY_8021X:
414 case CONNMAN_SERVICE_SECURITY_WEP:
415 case CONNMAN_SERVICE_SECURITY_PSK:
416 case CONNMAN_SERVICE_SECURITY_WPA:
417 case CONNMAN_SERVICE_SECURITY_RSN:
418 if (service->passphrase == NULL)
425 signal = dbus_message_new_signal(service->path,
426 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
430 dbus_message_iter_init_append(signal, &iter);
432 connman_dbus_property_append_variant(&iter,
433 "PassphraseRequired", DBUS_TYPE_BOOLEAN, &required);
435 g_dbus_send_message(connection, signal);
438 static void apn_changed(struct connman_service *service)
441 DBusMessageIter iter;
442 dbus_bool_t required;
444 if (service->path == NULL)
447 switch (service->type) {
448 case CONNMAN_SERVICE_TYPE_UNKNOWN:
449 case CONNMAN_SERVICE_TYPE_SYSTEM:
450 case CONNMAN_SERVICE_TYPE_ETHERNET:
451 case CONNMAN_SERVICE_TYPE_WIMAX:
452 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
453 case CONNMAN_SERVICE_TYPE_WIFI:
454 case CONNMAN_SERVICE_TYPE_VPN:
456 case CONNMAN_SERVICE_TYPE_CELLULAR:
460 required = (service->apn == NULL) ? TRUE : FALSE;
462 signal = dbus_message_new_signal(service->path,
463 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
467 dbus_message_iter_init_append(signal, &iter);
469 connman_dbus_property_append_variant(&iter,
470 "SetupRequired", DBUS_TYPE_BOOLEAN, &required);
472 g_dbus_send_message(connection, signal);
475 static DBusMessage *get_properties(DBusConnection *conn,
476 DBusMessage *msg, void *user_data)
478 struct connman_service *service = user_data;
480 DBusMessageIter array, dict;
481 dbus_bool_t required;
484 DBG("service %p", service);
486 reply = dbus_message_new_method_return(msg);
490 dbus_message_iter_init_append(reply, &array);
492 connman_dbus_dict_open(&array, &dict);
494 str = __connman_service_type2string(service->type);
496 connman_dbus_dict_append_variant(&dict, "Type",
497 DBUS_TYPE_STRING, &str);
499 str = mode2string(service->mode);
501 connman_dbus_dict_append_variant(&dict, "Mode",
502 DBUS_TYPE_STRING, &str);
504 str = security2string(service->security);
506 connman_dbus_dict_append_variant(&dict, "Security",
507 DBUS_TYPE_STRING, &str);
509 str = state2string(service->state);
511 connman_dbus_dict_append_variant(&dict, "State",
512 DBUS_TYPE_STRING, &str);
514 str = error2string(service->error);
516 connman_dbus_dict_append_variant(&dict, "Error",
517 DBUS_TYPE_STRING, &str);
519 if (service->strength > 0)
520 connman_dbus_dict_append_variant(&dict, "Strength",
521 DBUS_TYPE_BYTE, &service->strength);
523 connman_dbus_dict_append_variant(&dict, "Favorite",
524 DBUS_TYPE_BOOLEAN, &service->favorite);
526 if (service->favorite == TRUE)
527 connman_dbus_dict_append_variant(&dict, "AutoConnect",
528 DBUS_TYPE_BOOLEAN, &service->autoconnect);
530 connman_dbus_dict_append_variant(&dict, "AutoConnect",
531 DBUS_TYPE_BOOLEAN, &service->favorite);
533 if (service->name != NULL)
534 connman_dbus_dict_append_variant(&dict, "Name",
535 DBUS_TYPE_STRING, &service->name);
537 switch (service->type) {
538 case CONNMAN_SERVICE_TYPE_UNKNOWN:
539 case CONNMAN_SERVICE_TYPE_SYSTEM:
540 case CONNMAN_SERVICE_TYPE_ETHERNET:
541 case CONNMAN_SERVICE_TYPE_WIMAX:
542 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
543 case CONNMAN_SERVICE_TYPE_VPN:
545 case CONNMAN_SERVICE_TYPE_CELLULAR:
546 connman_dbus_dict_append_variant(&dict, "Roaming",
547 DBUS_TYPE_BOOLEAN, &service->roaming);
549 if (service->mcc != NULL && service->mnc != NULL) {
550 connman_dbus_dict_append_variant(&dict, "MCC",
551 DBUS_TYPE_STRING, &service->mcc);
552 connman_dbus_dict_append_variant(&dict, "MNC",
553 DBUS_TYPE_STRING, &service->mnc);
556 if (service->apn != NULL) {
557 connman_dbus_dict_append_variant(&dict, "APN",
558 DBUS_TYPE_STRING, &service->apn);
560 if (service->username != NULL)
561 connman_dbus_dict_append_variant(&dict,
562 "Username", DBUS_TYPE_STRING,
565 if (service->password != NULL)
566 connman_dbus_dict_append_variant(&dict,
567 "Password", DBUS_TYPE_STRING,
574 connman_dbus_dict_append_variant(&dict, "SetupRequired",
575 DBUS_TYPE_BOOLEAN, &required);
577 case CONNMAN_SERVICE_TYPE_WIFI:
578 if (service->passphrase != NULL &&
579 __connman_security_check_privilege(msg,
580 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
581 connman_dbus_dict_append_variant(&dict, "Passphrase",
582 DBUS_TYPE_STRING, &service->passphrase);
586 switch (service->security) {
587 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
588 case CONNMAN_SERVICE_SECURITY_NONE:
589 case CONNMAN_SERVICE_SECURITY_8021X:
591 case CONNMAN_SERVICE_SECURITY_WEP:
592 case CONNMAN_SERVICE_SECURITY_PSK:
593 case CONNMAN_SERVICE_SECURITY_WPA:
594 case CONNMAN_SERVICE_SECURITY_RSN:
595 if (service->passphrase == NULL)
600 connman_dbus_dict_append_variant(&dict, "PassphraseRequired",
601 DBUS_TYPE_BOOLEAN, &required);
605 if (service->ipconfig != NULL)
606 __connman_ipconfig_append_ipv4(service->ipconfig,
609 connman_dbus_dict_close(&array, &dict);
614 static DBusMessage *set_property(DBusConnection *conn,
615 DBusMessage *msg, void *user_data)
617 struct connman_service *service = user_data;
618 DBusMessageIter iter, value;
622 DBG("service %p", service);
624 if (dbus_message_iter_init(msg, &iter) == FALSE)
625 return __connman_error_invalid_arguments(msg);
627 dbus_message_iter_get_basic(&iter, &name);
628 dbus_message_iter_next(&iter);
629 dbus_message_iter_recurse(&iter, &value);
631 if (__connman_security_check_privilege(msg,
632 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
633 return __connman_error_permission_denied(msg);
635 type = dbus_message_iter_get_arg_type(&value);
637 if (g_str_has_prefix(name, "AutoConnect") == TRUE) {
638 connman_bool_t autoconnect;
640 if (type != DBUS_TYPE_BOOLEAN)
641 return __connman_error_invalid_arguments(msg);
643 if (service->favorite == FALSE)
644 return __connman_error_invalid_service(msg);
646 dbus_message_iter_get_basic(&value, &autoconnect);
648 if (service->autoconnect == autoconnect)
649 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
651 service->autoconnect = autoconnect;
653 autoconnect_changed(service);
655 __connman_storage_save_service(service);
656 } else if (g_str_equal(name, "Passphrase") == TRUE) {
657 const char *passphrase;
659 if (type != DBUS_TYPE_STRING)
660 return __connman_error_invalid_arguments(msg);
662 if (__connman_security_check_privilege(msg,
663 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
664 return __connman_error_permission_denied(msg);
666 dbus_message_iter_get_basic(&value, &passphrase);
668 g_free(service->passphrase);
669 service->passphrase = g_strdup(passphrase);
671 passphrase_changed(service);
673 if (service->network != NULL)
674 connman_network_set_string(service->network,
675 "WiFi.Passphrase", service->passphrase);
677 __connman_storage_save_service(service);
678 } else if (g_str_equal(name, "APN") == TRUE) {
681 if (type != DBUS_TYPE_STRING)
682 return __connman_error_invalid_arguments(msg);
684 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
685 return __connman_error_invalid_service(msg);
687 dbus_message_iter_get_basic(&value, &apn);
689 g_free(service->apn);
690 service->apn = g_strdup(apn);
692 apn_changed(service);
694 if (service->network != NULL)
695 connman_network_set_string(service->network,
696 "Cellular.APN", service->apn);
698 __connman_storage_save_service(service);
699 } else if (g_str_equal(name, "Username") == TRUE) {
700 const char *username;
702 if (type != DBUS_TYPE_STRING)
703 return __connman_error_invalid_arguments(msg);
705 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
706 return __connman_error_invalid_service(msg);
708 dbus_message_iter_get_basic(&value, &username);
710 g_free(service->username);
711 service->username = g_strdup(username);
713 if (service->network != NULL)
714 connman_network_set_string(service->network,
715 "Cellular.Username", service->username);
717 __connman_storage_save_service(service);
718 } else if (g_str_equal(name, "Password") == TRUE) {
719 const char *password;
721 if (type != DBUS_TYPE_STRING)
722 return __connman_error_invalid_arguments(msg);
724 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
725 return __connman_error_invalid_service(msg);
727 dbus_message_iter_get_basic(&value, &password);
729 g_free(service->password);
730 service->password = g_strdup(password);
732 if (service->network != NULL)
733 connman_network_set_string(service->network,
734 "Cellular.Password", service->password);
736 __connman_storage_save_service(service);
737 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
740 if (service->ipconfig == NULL)
741 return __connman_error_invalid_property(msg);
743 err = __connman_ipconfig_set_ipv4(service->ipconfig,
746 return __connman_error_failed(msg, -err);
748 return __connman_error_invalid_property(msg);
750 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
753 static void set_idle(struct connman_service *service)
755 service->state = CONNMAN_SERVICE_STATE_IDLE;
756 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
757 state_changed(service);
760 static DBusMessage *clear_property(DBusConnection *conn,
761 DBusMessage *msg, void *user_data)
763 struct connman_service *service = user_data;
766 DBG("service %p", service);
768 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
771 if (__connman_security_check_privilege(msg,
772 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
773 return __connman_error_permission_denied(msg);
775 if (g_str_equal(name, "Error") == TRUE) {
778 g_get_current_time(&service->modified);
779 __connman_storage_save_service(service);
780 } else if (g_str_equal(name, "Passphrase") == TRUE) {
781 g_free(service->passphrase);
782 service->passphrase = NULL;
784 passphrase_changed(service);
786 __connman_storage_save_service(service);
788 return __connman_error_invalid_property(msg);
790 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
793 static connman_bool_t is_connecting(struct connman_service *service)
795 switch (service->state) {
796 case CONNMAN_SERVICE_STATE_UNKNOWN:
797 case CONNMAN_SERVICE_STATE_IDLE:
798 case CONNMAN_SERVICE_STATE_FAILURE:
799 case CONNMAN_SERVICE_STATE_DISCONNECT:
800 case CONNMAN_SERVICE_STATE_READY:
802 case CONNMAN_SERVICE_STATE_ASSOCIATION:
803 case CONNMAN_SERVICE_STATE_CONFIGURATION:
810 static connman_bool_t is_ignore(struct connman_service *service)
812 if (service->autoconnect == FALSE)
815 if (service->roaming == TRUE)
818 if (service->ignore == TRUE)
821 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
827 void __connman_service_auto_connect(void)
829 struct connman_service *service = NULL;
834 iter = g_sequence_get_begin_iter(service_list);
836 while (g_sequence_iter_is_end(iter) == FALSE) {
837 service = g_sequence_get(iter);
839 if (service->pending != NULL)
842 if (is_connecting(service) == TRUE)
845 if (service->favorite == FALSE)
848 if (service->state == CONNMAN_SERVICE_STATE_READY)
851 if (is_ignore(service) == FALSE &&
852 service->state == CONNMAN_SERVICE_STATE_IDLE)
857 iter = g_sequence_iter_next(iter);
860 if (service != NULL) {
861 service->userconnect = FALSE;
862 __connman_service_connect(service);
866 static void remove_timeout(struct connman_service *service)
868 if (service->timeout > 0) {
869 g_source_remove(service->timeout);
870 service->timeout = 0;
874 static void reply_pending(struct connman_service *service, int error)
876 remove_timeout(service);
878 if (service->pending != NULL) {
882 reply = __connman_error_failed(service->pending,
885 g_dbus_send_message(connection, reply);
887 g_dbus_send_reply(connection, service->pending,
890 dbus_message_unref(service->pending);
891 service->pending = NULL;
895 static gboolean connect_timeout(gpointer user_data)
897 struct connman_service *service = user_data;
898 connman_bool_t autoconnect = FALSE;
900 DBG("service %p", service);
902 service->timeout = 0;
904 if (service->network != NULL)
905 __connman_network_disconnect(service->network);
907 __connman_ipconfig_disable(service->ipconfig);
909 if (service->pending != NULL) {
912 reply = __connman_error_operation_timeout(service->pending);
914 g_dbus_send_message(connection, reply);
916 dbus_message_unref(service->pending);
917 service->pending = NULL;
921 __connman_service_indicate_state(service,
922 CONNMAN_SERVICE_STATE_FAILURE);
924 if (autoconnect == TRUE && service->userconnect == FALSE)
925 __connman_service_auto_connect();
930 static void set_reconnect_state(struct connman_service *service,
931 connman_bool_t reconnect)
933 struct connman_device *device;
935 if (service->network == NULL)
938 device = connman_network_get_device(service->network);
942 __connman_device_set_reconnect(device, reconnect);
945 static connman_bool_t get_reconnect_state(struct connman_service *service)
947 struct connman_device *device;
949 if (service->network == NULL)
952 device = connman_network_get_device(service->network);
956 return __connman_device_get_reconnect(device);
959 static DBusMessage *connect_service(DBusConnection *conn,
960 DBusMessage *msg, void *user_data)
962 struct connman_service *service = user_data;
966 DBG("service %p", service);
968 if (service->pending != NULL)
969 return __connman_error_in_progress(msg);
971 iter = g_sequence_get_begin_iter(service_list);
973 while (g_sequence_iter_is_end(iter) == FALSE) {
974 struct connman_service *temp = g_sequence_get(iter);
976 if (service->type == temp->type &&
977 is_connecting(temp) == TRUE)
978 return __connman_error_in_progress(msg);
980 iter = g_sequence_iter_next(iter);
983 service->ignore = FALSE;
985 service->userconnect = TRUE;
987 service->pending = dbus_message_ref(msg);
989 set_reconnect_state(service, FALSE);
991 err = __connman_service_connect(service);
993 if (err == -ENOKEY) {
994 if (__connman_agent_request_passphrase(service,
999 if (err != -EINPROGRESS) {
1000 dbus_message_unref(service->pending);
1001 service->pending = NULL;
1003 return __connman_error_failed(msg, -err);
1009 dbus_message_unref(service->pending);
1010 service->pending = NULL;
1012 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1015 static DBusMessage *disconnect_service(DBusConnection *conn,
1016 DBusMessage *msg, void *user_data)
1018 struct connman_service *service = user_data;
1021 DBG("service %p", service);
1023 reply_pending(service, ECONNABORTED);
1025 service->ignore = TRUE;
1027 set_reconnect_state(service, FALSE);
1029 err = __connman_service_disconnect(service);
1031 if (err != -EINPROGRESS)
1032 return __connman_error_failed(msg, -err);
1037 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1040 static DBusMessage *remove_service(DBusConnection *conn,
1041 DBusMessage *msg, void *user_data)
1043 struct connman_service *service = user_data;
1045 DBG("service %p", service);
1047 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
1048 return __connman_error_not_supported(msg);
1050 if (service->favorite == FALSE &&
1051 service->state != CONNMAN_SERVICE_STATE_FAILURE)
1052 return __connman_error_not_supported(msg);
1054 if (service->network != NULL) {
1055 set_reconnect_state(service, FALSE);
1057 __connman_network_disconnect(service->network);
1060 g_free(service->passphrase);
1061 service->passphrase = NULL;
1063 passphrase_changed(service);
1065 g_free(service->apn);
1066 service->apn = NULL;
1068 g_free(service->username);
1069 service->username = NULL;
1071 g_free(service->password);
1072 service->password = NULL;
1074 apn_changed(service);
1078 connman_service_set_favorite(service, FALSE);
1079 __connman_storage_save_service(service);
1081 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1084 static DBusMessage *move_before(DBusConnection *conn,
1085 DBusMessage *msg, void *user_data)
1087 struct connman_service *service = user_data;
1088 struct connman_service *target;
1090 GSequenceIter *src, *dst;
1092 DBG("service %p", service);
1094 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1097 if (service->favorite == FALSE)
1098 return __connman_error_not_supported(msg);
1100 target = find_service(path);
1101 if (target == NULL || target->favorite == FALSE || target == service)
1102 return __connman_error_invalid_service(msg);
1104 DBG("target %s", target->identifier);
1106 if (target->state != service->state)
1107 return __connman_error_invalid_service(msg);
1109 g_get_current_time(&service->modified);
1110 __connman_storage_save_service(service);
1112 src = g_hash_table_lookup(service_hash, service->identifier);
1113 dst = g_hash_table_lookup(service_hash, target->identifier);
1115 g_sequence_move(src, dst);
1117 __connman_profile_changed(FALSE);
1119 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1122 static DBusMessage *move_after(DBusConnection *conn,
1123 DBusMessage *msg, void *user_data)
1125 struct connman_service *service = user_data;
1126 struct connman_service *target;
1129 DBG("service %p", service);
1131 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1134 if (service->favorite == FALSE)
1135 return __connman_error_not_supported(msg);
1137 target = find_service(path);
1138 if (target == NULL || target->favorite == FALSE || target == service)
1139 return __connman_error_invalid_service(msg);
1141 DBG("target %s", target->identifier);
1143 if (target->state != service->state)
1144 return __connman_error_invalid_service(msg);
1146 g_get_current_time(&service->modified);
1147 __connman_storage_save_service(service);
1149 return __connman_error_not_implemented(msg);
1152 static GDBusMethodTable service_methods[] = {
1153 { "GetProperties", "", "a{sv}", get_properties },
1154 { "SetProperty", "sv", "", set_property },
1155 { "ClearProperty", "s", "", clear_property },
1156 { "Connect", "", "", connect_service,
1157 G_DBUS_METHOD_FLAG_ASYNC },
1158 { "Disconnect", "", "", disconnect_service },
1159 { "Remove", "", "", remove_service },
1160 { "MoveBefore", "o", "", move_before },
1161 { "MoveAfter", "o", "", move_after },
1165 static GDBusSignalTable service_signals[] = {
1166 { "PropertyChanged", "sv" },
1170 static void service_free(gpointer user_data)
1172 struct connman_service *service = user_data;
1173 char *path = service->path;
1175 DBG("service %p", service);
1177 reply_pending(service, ENOENT);
1179 g_hash_table_remove(service_hash, service->identifier);
1181 service->path = NULL;
1184 __connman_profile_changed(FALSE);
1186 g_dbus_unregister_interface(connection, path,
1187 CONNMAN_SERVICE_INTERFACE);
1191 if (service->network != NULL)
1192 connman_network_unref(service->network);
1194 if (service->ipconfig != NULL) {
1195 connman_ipconfig_unref(service->ipconfig);
1196 service->ipconfig = NULL;
1199 g_free(service->mcc);
1200 g_free(service->mnc);
1201 g_free(service->apn);
1202 g_free(service->username);
1203 g_free(service->password);
1204 g_free(service->profile);
1205 g_free(service->name);
1206 g_free(service->passphrase);
1207 g_free(service->identifier);
1212 * __connman_service_put:
1213 * @service: service structure
1215 * Release service if no longer needed
1217 void __connman_service_put(struct connman_service *service)
1219 DBG("service %p", service);
1221 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
1222 GSequenceIter *iter;
1224 iter = g_hash_table_lookup(service_hash, service->identifier);
1226 reply_pending(service, ECONNABORTED);
1228 __connman_service_disconnect(service);
1230 g_sequence_remove(iter);
1232 service_free(service);
1236 static void __connman_service_initialize(struct connman_service *service)
1238 DBG("service %p", service);
1240 service->refcount = 1;
1242 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
1243 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
1244 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
1245 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
1247 service->favorite = FALSE;
1248 service->hidden = FALSE;
1250 service->ignore = FALSE;
1252 service->userconnect = FALSE;
1258 * connman_service_create:
1260 * Allocate a new service.
1262 * Returns: a newly-allocated #connman_service structure
1264 struct connman_service *connman_service_create(void)
1266 struct connman_service *service;
1268 service = g_try_new0(struct connman_service, 1);
1269 if (service == NULL)
1272 DBG("service %p", service);
1274 __connman_service_initialize(service);
1280 * connman_service_ref:
1281 * @service: service structure
1283 * Increase reference counter of service
1285 struct connman_service *connman_service_ref(struct connman_service *service)
1287 g_atomic_int_inc(&service->refcount);
1293 * connman_service_unref:
1294 * @service: service structure
1296 * Decrease reference counter of service
1298 void connman_service_unref(struct connman_service *service)
1300 __connman_service_put(service);
1303 static gint service_compare(gconstpointer a, gconstpointer b,
1306 struct connman_service *service_a = (void *) a;
1307 struct connman_service *service_b = (void *) b;
1309 if (service_a->state != service_b->state) {
1310 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
1312 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
1316 if (service_a->order > service_b->order)
1319 if (service_a->order < service_b->order)
1322 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
1325 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
1328 if (service_a->type != service_b->type) {
1329 switch (service_a->type) {
1330 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1331 case CONNMAN_SERVICE_TYPE_SYSTEM:
1332 case CONNMAN_SERVICE_TYPE_ETHERNET:
1333 case CONNMAN_SERVICE_TYPE_VPN:
1335 case CONNMAN_SERVICE_TYPE_WIFI:
1337 case CONNMAN_SERVICE_TYPE_WIMAX:
1338 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1339 case CONNMAN_SERVICE_TYPE_CELLULAR:
1344 return (gint) service_b->strength - (gint) service_a->strength;
1347 static void favorite_changed(struct connman_service *service)
1349 DBusMessage *signal;
1350 DBusMessageIter iter;
1352 if (service->path == NULL)
1355 signal = dbus_message_new_signal(service->path,
1356 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
1360 dbus_message_iter_init_append(signal, &iter);
1362 connman_dbus_property_append_variant(&iter,
1363 "Favorite", DBUS_TYPE_BOOLEAN, &service->favorite);
1365 g_dbus_send_message(connection, signal);
1369 * connman_service_get_type:
1370 * @service: service structure
1372 * Get the type of service
1374 enum connman_service_type connman_service_get_type(struct connman_service *service)
1376 if (service == NULL)
1377 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1379 return service->type;
1383 * connman_service_get_interface:
1384 * @service: service structure
1386 * Get network interface of service
1388 char *connman_service_get_interface(struct connman_service *service)
1392 if (service == NULL)
1395 if (service->network == NULL)
1398 index = connman_network_get_index(service->network);
1400 return connman_inet_ifname(index);
1404 * connman_service_set_favorite:
1405 * @service: service structure
1406 * @favorite: favorite value
1408 * Change the favorite setting of service
1410 int connman_service_set_favorite(struct connman_service *service,
1411 connman_bool_t favorite)
1413 GSequenceIter *iter;
1415 iter = g_hash_table_lookup(service_hash, service->identifier);
1419 if (service->favorite == favorite)
1422 service->favorite = favorite;
1424 favorite_changed(service);
1426 g_sequence_sort_changed(iter, service_compare, NULL);
1428 __connman_profile_changed(FALSE);
1434 int __connman_service_indicate_state(struct connman_service *service,
1435 enum connman_service_state state)
1437 GSequenceIter *iter;
1439 DBG("service %p state %d", service, state);
1441 if (service == NULL)
1444 if (service->state == state)
1447 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
1448 state == CONNMAN_SERVICE_STATE_IDLE)
1451 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
1452 state == CONNMAN_SERVICE_STATE_DISCONNECT)
1455 if (state == CONNMAN_SERVICE_STATE_IDLE &&
1456 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
1457 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
1458 state_changed(service);
1460 reply_pending(service, ECONNABORTED);
1462 __connman_service_disconnect(service);
1465 service->state = state;
1466 state_changed(service);
1468 if (state == CONNMAN_SERVICE_STATE_IDLE) {
1469 connman_bool_t reconnect;
1471 reconnect = get_reconnect_state(service);
1472 if (reconnect == TRUE)
1473 __connman_service_auto_connect();
1476 if (state == CONNMAN_SERVICE_STATE_READY) {
1477 set_reconnect_state(service, TRUE);
1479 connman_service_set_favorite(service, TRUE);
1481 reply_pending(service, 0);
1483 service->userconnect = FALSE;
1485 g_get_current_time(&service->modified);
1486 __connman_storage_save_service(service);
1488 __connman_notifier_connect(service->type);
1491 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
1494 __connman_notifier_disconnect(service->type);
1497 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
1498 reply_pending(service, EIO);
1500 if (service->userconnect == FALSE)
1501 __connman_service_auto_connect();
1503 g_get_current_time(&service->modified);
1504 __connman_storage_save_service(service);
1506 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1508 iter = g_hash_table_lookup(service_hash, service->identifier);
1510 g_sequence_sort_changed(iter, service_compare, NULL);
1512 __connman_profile_changed(FALSE);
1514 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
1515 service->state == CONNMAN_SERVICE_STATE_FAILURE)
1516 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
1521 int __connman_service_indicate_error(struct connman_service *service,
1522 enum connman_service_error error)
1524 DBG("service %p error %d", service, error);
1526 if (service == NULL)
1529 service->error = error;
1531 return __connman_service_indicate_state(service,
1532 CONNMAN_SERVICE_STATE_FAILURE);
1535 int __connman_service_indicate_default(struct connman_service *service)
1537 DBG("service %p", service);
1544 static connman_bool_t prepare_network(struct connman_service *service)
1546 enum connman_network_type type;
1547 unsigned int ssid_len;
1549 type = connman_network_get_type(service->network);
1552 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1553 case CONNMAN_NETWORK_TYPE_VENDOR:
1555 case CONNMAN_NETWORK_TYPE_WIFI:
1556 if (connman_network_get_blob(service->network, "WiFi.SSID",
1560 connman_network_set_string(service->network,
1561 "WiFi.Passphrase", service->passphrase);
1563 case CONNMAN_NETWORK_TYPE_ETHERNET:
1564 case CONNMAN_NETWORK_TYPE_WIMAX:
1565 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1566 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1567 case CONNMAN_NETWORK_TYPE_CELLULAR:
1569 case CONNMAN_NETWORK_TYPE_MBM:
1570 case CONNMAN_NETWORK_TYPE_HSO:
1571 connman_network_set_string(service->network,
1572 "Cellular.APN", service->apn);
1574 connman_network_set_string(service->network,
1575 "Cellular.Username", service->username);
1576 connman_network_set_string(service->network,
1577 "Cellular.Password", service->password);
1584 int __connman_service_connect(struct connman_service *service)
1588 DBG("service %p", service);
1590 if (service->state == CONNMAN_SERVICE_STATE_READY)
1593 if (is_connecting(service) == TRUE)
1596 switch (service->type) {
1597 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1598 case CONNMAN_SERVICE_TYPE_SYSTEM:
1599 case CONNMAN_SERVICE_TYPE_VPN:
1601 case CONNMAN_SERVICE_TYPE_ETHERNET:
1602 case CONNMAN_SERVICE_TYPE_WIMAX:
1603 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1605 case CONNMAN_SERVICE_TYPE_CELLULAR:
1606 if (service->apn == NULL)
1609 case CONNMAN_SERVICE_TYPE_WIFI:
1610 switch (service->security) {
1611 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1612 case CONNMAN_SERVICE_SECURITY_NONE:
1613 case CONNMAN_SERVICE_SECURITY_8021X:
1615 case CONNMAN_SERVICE_SECURITY_WEP:
1616 case CONNMAN_SERVICE_SECURITY_PSK:
1617 case CONNMAN_SERVICE_SECURITY_WPA:
1618 case CONNMAN_SERVICE_SECURITY_RSN:
1619 if (service->passphrase == NULL)
1626 if (service->network != NULL) {
1627 if (prepare_network(service) == FALSE)
1630 __connman_ipconfig_enable(service->ipconfig);
1632 err = __connman_network_connect(service->network);
1637 if (err != -EINPROGRESS) {
1638 __connman_ipconfig_disable(service->ipconfig);
1642 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
1643 connect_timeout, service);
1645 return -EINPROGRESS;
1651 int __connman_service_disconnect(struct connman_service *service)
1655 DBG("service %p", service);
1657 if (service->network != NULL) {
1658 err = __connman_network_disconnect(service->network);
1662 __connman_ipconfig_disable(service->ipconfig);
1665 if (err != -EINPROGRESS)
1668 return -EINPROGRESS;
1675 * __connman_service_lookup:
1676 * @identifier: service identifier
1678 * Look up a service by identifier (reference count will not be increased)
1680 static struct connman_service *__connman_service_lookup(const char *identifier)
1682 GSequenceIter *iter;
1684 iter = g_hash_table_lookup(service_hash, identifier);
1686 return g_sequence_get(iter);
1691 static struct connman_network *create_hidden_wifi(struct connman_device *device,
1692 const char *ssid, const char *mode, const char *security)
1694 struct connman_network *network;
1697 unsigned int i, ssid_len;
1699 ssid_len = strlen(ssid);
1703 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
1704 if (network == NULL)
1707 connman_network_set_blob(network, "WiFi.SSID",
1708 (unsigned char *) ssid, ssid_len);
1710 connman_network_set_string(network, "WiFi.Mode", mode);
1711 connman_network_set_string(network, "WiFi.Security", security);
1713 name = g_try_malloc0(ssid_len + 1);
1715 connman_network_unref(network);
1719 for (i = 0; i < ssid_len; i++) {
1720 if (g_ascii_isprint(ssid[i]))
1726 connman_network_set_name(network, name);
1730 index = connman_device_get_index(device);
1731 connman_network_set_index(network, index);
1733 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
1735 if (connman_device_add_network(device, network) < 0) {
1736 connman_network_unref(network);
1740 connman_network_set_available(network, TRUE);
1745 int __connman_service_create_and_connect(DBusMessage *msg)
1747 struct connman_service *service;
1748 struct connman_network *network;
1749 struct connman_device *device;
1750 DBusMessageIter iter, array;
1751 const char *mode = "managed", *security = "none";
1752 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
1753 unsigned int ssid_len = 0;
1756 gboolean created = FALSE;
1759 dbus_message_iter_init(msg, &iter);
1760 dbus_message_iter_recurse(&iter, &array);
1762 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1763 DBusMessageIter entry, value;
1766 dbus_message_iter_recurse(&array, &entry);
1767 dbus_message_iter_get_basic(&entry, &key);
1769 dbus_message_iter_next(&entry);
1770 dbus_message_iter_recurse(&entry, &value);
1772 switch (dbus_message_iter_get_arg_type(&value)) {
1773 case DBUS_TYPE_STRING:
1774 if (g_str_equal(key, "Type") == TRUE)
1775 dbus_message_iter_get_basic(&value, &type);
1776 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
1777 g_str_equal(key, "Mode") == TRUE)
1778 dbus_message_iter_get_basic(&value, &mode);
1779 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
1780 g_str_equal(key, "Security") == TRUE)
1781 dbus_message_iter_get_basic(&value, &security);
1782 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
1783 g_str_equal(key, "Passphrase") == TRUE)
1784 dbus_message_iter_get_basic(&value, &passphrase);
1785 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
1786 g_str_equal(key, "SSID") == TRUE)
1787 dbus_message_iter_get_basic(&value, &ssid);
1790 dbus_message_iter_next(&array);
1796 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
1802 ssid_len = strlen(ssid);
1806 if (g_strcmp0(security, "none") != 0 &&
1807 g_strcmp0(security, "wep") != 0 &&
1808 g_strcmp0(security, "psk") != 0 &&
1809 g_strcmp0(security, "wpa") != 0 &&
1810 g_strcmp0(security, "rsn") != 0)
1813 device = __connman_element_find_device(CONNMAN_DEVICE_TYPE_WIFI);
1817 ident = __connman_device_get_ident(device);
1821 group = connman_wifi_build_group_name((unsigned char *) ssid,
1822 ssid_len, mode, security);
1826 name = g_strdup_printf("%s_%s_%s", type, ident, group);
1828 service = __connman_service_lookup(name);
1830 if (service != NULL)
1833 network = create_hidden_wifi(device, ssid, mode, security);
1834 if (network != NULL) {
1835 connman_network_set_group(network, group);
1839 service = __connman_service_lookup(name);
1845 if (service == NULL) {
1850 set_reconnect_state(service, FALSE);
1852 __connman_device_disconnect(device);
1854 if (passphrase != NULL) {
1855 g_free(service->passphrase);
1856 service->passphrase = g_strdup(passphrase);
1859 service->userconnect = TRUE;
1861 err = __connman_service_connect(service);
1862 if (err < 0 && err != -EINPROGRESS)
1865 g_dbus_send_reply(connection, msg,
1866 DBUS_TYPE_OBJECT_PATH, &service->path,
1872 if (service != NULL && created == TRUE) {
1873 struct connman_network *network = service->network;
1875 if (network != NULL) {
1876 connman_network_set_available(network, FALSE);
1877 __connman_device_cleanup_networks(device);
1880 __connman_service_put(service);
1887 * __connman_service_get:
1888 * @identifier: service identifier
1890 * Look up a service by identifier or create a new one if not found
1892 static struct connman_service *__connman_service_get(const char *identifier)
1894 struct connman_service *service;
1895 GSequenceIter *iter;
1897 iter = g_hash_table_lookup(service_hash, identifier);
1899 service = g_sequence_get(iter);
1900 if (service != NULL)
1901 g_atomic_int_inc(&service->refcount);
1905 service = connman_service_create();
1906 if (service == NULL)
1909 DBG("service %p", service);
1911 service->identifier = g_strdup(identifier);
1913 service->profile = g_strdup(__connman_profile_active_ident());
1915 __connman_storage_load_service(service);
1917 iter = g_sequence_insert_sorted(service_list, service,
1918 service_compare, NULL);
1920 g_hash_table_insert(service_hash, service->identifier, iter);
1925 static int service_register(struct connman_service *service)
1927 const char *path = __connman_profile_active_path();
1928 GSequenceIter *iter;
1930 DBG("service %p", service);
1932 if (service->path != NULL)
1935 service->path = g_strdup_printf("%s/%s", path, service->identifier);
1937 DBG("path %s", service->path);
1939 g_dbus_register_interface(connection, service->path,
1940 CONNMAN_SERVICE_INTERFACE,
1941 service_methods, service_signals,
1942 NULL, service, NULL);
1944 __connman_storage_load_service(service);
1946 iter = g_hash_table_lookup(service_hash, service->identifier);
1948 g_sequence_sort_changed(iter, service_compare, NULL);
1950 __connman_profile_changed(TRUE);
1955 static void service_up(struct connman_ipconfig *ipconfig)
1957 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
1960 static void service_down(struct connman_ipconfig *ipconfig)
1962 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
1965 static void service_lower_up(struct connman_ipconfig *ipconfig)
1967 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
1970 static void service_lower_down(struct connman_ipconfig *ipconfig)
1972 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
1975 static void service_ip_bound(struct connman_ipconfig *ipconfig)
1977 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
1980 static void service_ip_release(struct connman_ipconfig *ipconfig)
1982 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
1985 static const struct connman_ipconfig_ops service_ops = {
1987 .down = service_down,
1988 .lower_up = service_lower_up,
1989 .lower_down = service_lower_down,
1990 .ip_bound = service_ip_bound,
1991 .ip_release = service_ip_release,
1994 static void setup_ipconfig(struct connman_service *service, int index)
1999 service->ipconfig = connman_ipconfig_create(index);
2000 if (service->ipconfig == NULL)
2003 connman_ipconfig_set_method(service->ipconfig,
2004 CONNMAN_IPCONFIG_METHOD_DHCP);
2006 __connman_storage_load_service(service);
2008 connman_ipconfig_set_data(service->ipconfig, service);
2010 connman_ipconfig_set_ops(service->ipconfig, &service_ops);
2011 connman_ipconfig_set_ops(service->ipconfig, NULL);
2015 * __connman_service_lookup_from_network:
2016 * @network: network structure
2018 * Look up a service by network (reference count will not be increased)
2020 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
2022 struct connman_service *service;
2023 const char *ident, *group;
2026 DBG("network %p", network);
2028 ident = __connman_network_get_ident(network);
2032 group = connman_network_get_group(network);
2036 name = g_strdup_printf("%s_%s_%s",
2037 __connman_network_get_type(network), ident, group);
2038 service = __connman_service_lookup(name);
2044 const char *__connman_service_get_path(struct connman_service *service)
2046 return service->path;
2049 unsigned int __connman_service_get_order(struct connman_service *service)
2051 GSequenceIter *iter;
2053 if (service == NULL)
2056 if (service->favorite == FALSE) {
2061 iter = g_hash_table_lookup(service_hash, service->identifier);
2063 if (g_sequence_iter_get_position(iter) == 0)
2070 return service->order;
2073 static enum connman_service_type convert_network_type(struct connman_network *network)
2075 enum connman_network_type type = connman_network_get_type(network);
2078 case CONNMAN_NETWORK_TYPE_UNKNOWN:
2079 case CONNMAN_NETWORK_TYPE_VENDOR:
2081 case CONNMAN_NETWORK_TYPE_ETHERNET:
2082 return CONNMAN_SERVICE_TYPE_ETHERNET;
2083 case CONNMAN_NETWORK_TYPE_WIFI:
2084 return CONNMAN_SERVICE_TYPE_WIFI;
2085 case CONNMAN_NETWORK_TYPE_WIMAX:
2086 return CONNMAN_SERVICE_TYPE_WIMAX;
2087 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
2088 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
2089 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
2090 case CONNMAN_NETWORK_TYPE_CELLULAR:
2091 case CONNMAN_NETWORK_TYPE_MBM:
2092 case CONNMAN_NETWORK_TYPE_HSO:
2093 return CONNMAN_SERVICE_TYPE_CELLULAR;
2096 return CONNMAN_SERVICE_TYPE_UNKNOWN;
2099 static enum connman_service_mode convert_wifi_mode(const char *mode)
2102 return CONNMAN_SERVICE_MODE_UNKNOWN;
2103 else if (g_str_equal(mode, "managed") == TRUE)
2104 return CONNMAN_SERVICE_MODE_MANAGED;
2105 else if (g_str_equal(mode, "adhoc") == TRUE)
2106 return CONNMAN_SERVICE_MODE_ADHOC;
2108 return CONNMAN_SERVICE_MODE_UNKNOWN;
2111 static enum connman_service_security convert_wifi_security(const char *security)
2113 if (security == NULL)
2114 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2115 else if (g_str_equal(security, "none") == TRUE)
2116 return CONNMAN_SERVICE_SECURITY_NONE;
2117 else if (g_str_equal(security, "wep") == TRUE)
2118 return CONNMAN_SERVICE_SECURITY_WEP;
2119 else if (g_str_equal(security, "wep") == TRUE)
2120 return CONNMAN_SERVICE_SECURITY_PSK;
2121 else if (g_str_equal(security, "ieee8021x") == TRUE)
2122 return CONNMAN_SERVICE_SECURITY_8021X;
2123 else if (g_str_equal(security, "wpa") == TRUE)
2124 return CONNMAN_SERVICE_SECURITY_WPA;
2125 else if (g_str_equal(security, "rsn") == TRUE)
2126 return CONNMAN_SERVICE_SECURITY_RSN;
2128 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2131 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
2136 return CONNMAN_SERVICE_MODE_GPRS;
2138 return CONNMAN_SERVICE_MODE_EDGE;
2143 return CONNMAN_SERVICE_MODE_UMTS;
2146 return CONNMAN_SERVICE_MODE_UNKNOWN;
2149 static void update_from_network(struct connman_service *service,
2150 struct connman_network *network)
2152 connman_uint8_t strength = service->strength;
2153 GSequenceIter *iter;
2156 DBG("service %p network %p", service, network);
2158 if (service->state == CONNMAN_SERVICE_STATE_READY)
2161 if (is_connecting(service) == TRUE)
2164 str = connman_network_get_string(network, "Name");
2166 g_free(service->name);
2167 service->name = g_strdup(str);
2168 service->hidden = FALSE;
2170 g_free(service->name);
2171 service->name = NULL;
2172 service->hidden = TRUE;
2175 service->strength = connman_network_get_uint8(network, "Strength");
2176 service->roaming = connman_network_get_bool(network, "Roaming");
2178 if (service->strength == 0) {
2180 * Filter out 0-values; it's unclear what they mean
2181 * and they cause anomalous sorting of the priority list.
2183 service->strength = strength;
2186 str = connman_network_get_string(network, "WiFi.Mode");
2187 service->mode = convert_wifi_mode(str);
2189 str = connman_network_get_string(network, "WiFi.Security");
2190 service->security = convert_wifi_security(str);
2192 str = connman_network_get_string(network, "Cellular.MCC");
2193 g_free(service->mcc);
2194 service->mcc = g_strdup(str);
2196 str = connman_network_get_string(network, "Cellular.MNC");
2197 g_free(service->mnc);
2198 service->mnc = g_strdup(str);
2200 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
2201 connman_uint8_t value = connman_network_get_uint8(network,
2204 service->mode = convert_cellular_mode(value);
2207 if (service->strength > strength && service->network != NULL) {
2208 connman_network_unref(service->network);
2209 service->network = connman_network_ref(network);
2211 strength_changed(service);
2214 if (service->network == NULL)
2215 service->network = connman_network_ref(network);
2217 iter = g_hash_table_lookup(service_hash, service->identifier);
2219 g_sequence_sort_changed(iter, service_compare, NULL);
2223 * __connman_service_create_from_network:
2224 * @network: network structure
2226 * Look up service by network and if not found, create one
2228 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
2230 struct connman_service *service;
2231 const char *ident, *group;
2234 DBG("network %p", network);
2236 ident = __connman_network_get_ident(network);
2240 group = connman_network_get_group(network);
2244 name = g_strdup_printf("%s_%s_%s",
2245 __connman_network_get_type(network), ident, group);
2246 service = __connman_service_get(name);
2249 if (service == NULL)
2252 if (__connman_network_get_weakness(network) == TRUE)
2255 if (service->path != NULL) {
2256 update_from_network(service, network);
2257 __connman_profile_changed(TRUE);
2261 service->type = convert_network_type(network);
2263 switch (service->type) {
2264 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2265 case CONNMAN_SERVICE_TYPE_SYSTEM:
2266 case CONNMAN_SERVICE_TYPE_ETHERNET:
2267 case CONNMAN_SERVICE_TYPE_WIMAX:
2268 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2269 case CONNMAN_SERVICE_TYPE_VPN:
2270 service->autoconnect = FALSE;
2272 case CONNMAN_SERVICE_TYPE_WIFI:
2273 case CONNMAN_SERVICE_TYPE_CELLULAR:
2274 service->autoconnect = TRUE;
2278 service->state = CONNMAN_SERVICE_STATE_IDLE;
2280 update_from_network(service, network);
2282 setup_ipconfig(service, connman_network_get_index(network));
2284 service_register(service);
2286 __connman_profile_changed(TRUE);
2288 if (service->favorite == TRUE)
2289 __connman_service_auto_connect();
2294 void __connman_service_update_from_network(struct connman_network *network)
2296 struct connman_service *service;
2297 enum connman_service_mode mode;
2298 connman_uint8_t strength, value;
2299 connman_bool_t roaming;
2300 GSequenceIter *iter;
2302 DBG("network %p", network);
2304 service = __connman_service_lookup_from_network(network);
2305 if (service == NULL)
2308 if (service->network == NULL)
2311 strength = connman_network_get_uint8(service->network, "Strength");
2312 if (strength == service->strength)
2315 service->strength = strength;
2317 strength_changed(service);
2320 roaming = connman_network_get_bool(service->network, "Roaming");
2321 if (roaming == service->roaming)
2324 service->roaming = roaming;
2326 roaming_changed(service);
2328 iter = g_hash_table_lookup(service_hash, service->identifier);
2330 g_sequence_sort_changed(iter, service_compare, NULL);
2333 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
2336 value = connman_network_get_uint8(service->network, "Cellular.Mode");
2337 mode = convert_cellular_mode(value);
2339 if (mode == service->mode)
2342 service->mode = mode;
2344 mode_changed(service);
2347 void __connman_service_remove_from_network(struct connman_network *network)
2349 struct connman_service *service;
2351 DBG("network %p", network);
2353 service = __connman_service_lookup_from_network(network);
2354 if (service == NULL)
2357 __connman_service_put(service);
2360 static int service_load(struct connman_service *service)
2362 const char *ident = service->profile;
2364 GError *error = NULL;
2365 gchar *pathname, *data = NULL;
2368 connman_bool_t autoconnect;
2369 unsigned int ssid_len;
2372 DBG("service %p", service);
2377 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2378 if (pathname == NULL)
2381 keyfile = g_key_file_new();
2383 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
2390 if (g_key_file_load_from_data(keyfile, data, length,
2391 0, NULL) == FALSE) {
2398 switch (service->type) {
2399 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2400 case CONNMAN_SERVICE_TYPE_SYSTEM:
2401 case CONNMAN_SERVICE_TYPE_ETHERNET:
2402 case CONNMAN_SERVICE_TYPE_VPN:
2404 case CONNMAN_SERVICE_TYPE_WIFI:
2405 if (service->name == NULL) {
2408 name = g_key_file_get_string(keyfile,
2409 service->identifier, "Name", NULL);
2411 g_free(service->name);
2412 service->name = name;
2415 if (service->network != NULL)
2416 connman_network_set_name(service->network,
2420 if (service->network &&
2421 connman_network_get_blob(service->network,
2422 "WiFi.SSID", &ssid_len) == NULL) {
2425 hex_ssid = g_key_file_get_string(keyfile,
2426 service->identifier,
2429 if (hex_ssid != NULL) {
2431 unsigned int i, j = 0, hex;
2432 size_t hex_ssid_len = strlen(hex_ssid);
2434 ssid = g_try_malloc0(hex_ssid_len / 2);
2441 for (i = 0; i < hex_ssid_len; i += 2) {
2442 sscanf(hex_ssid + i, "%02x", &hex);
2446 connman_network_set_blob(service->network,
2447 "WiFi.SSID", ssid, hex_ssid_len / 2);
2454 case CONNMAN_SERVICE_TYPE_WIMAX:
2455 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2456 case CONNMAN_SERVICE_TYPE_CELLULAR:
2457 service->apn = g_key_file_get_string(keyfile,
2458 service->identifier, "APN", NULL);
2460 service->username = g_key_file_get_string(keyfile,
2461 service->identifier, "Username", NULL);
2463 service->username = g_key_file_get_string(keyfile,
2464 service->identifier, "Password", NULL);
2466 service->favorite = g_key_file_get_boolean(keyfile,
2467 service->identifier, "Favorite", NULL);
2469 autoconnect = g_key_file_get_boolean(keyfile,
2470 service->identifier, "AutoConnect", &error);
2472 service->autoconnect = autoconnect;
2473 g_clear_error(&error);
2475 str = g_key_file_get_string(keyfile,
2476 service->identifier, "Failure", NULL);
2478 service->state = CONNMAN_SERVICE_STATE_FAILURE;
2479 service->error = string2error(str);
2484 str = g_key_file_get_string(keyfile,
2485 service->identifier, "Modified", NULL);
2487 g_time_val_from_iso8601(str, &service->modified);
2491 str = g_key_file_get_string(keyfile,
2492 service->identifier, "Passphrase", NULL);
2494 g_free(service->passphrase);
2495 service->passphrase = str;
2498 if (service->ipconfig != NULL)
2499 __connman_ipconfig_load(service->ipconfig, keyfile,
2500 service->identifier, "IPv4.");
2503 g_key_file_free(keyfile);
2508 static int service_save(struct connman_service *service)
2510 const char *ident = service->profile;
2512 gchar *pathname, *data = NULL;
2517 DBG("service %p", service);
2522 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2523 if (pathname == NULL)
2526 keyfile = g_key_file_new();
2528 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
2532 if (g_key_file_load_from_data(keyfile, data, length,
2540 if (service->name != NULL)
2541 g_key_file_set_string(keyfile, service->identifier,
2542 "Name", service->name);
2544 switch (service->type) {
2545 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2546 case CONNMAN_SERVICE_TYPE_SYSTEM:
2547 case CONNMAN_SERVICE_TYPE_ETHERNET:
2548 case CONNMAN_SERVICE_TYPE_VPN:
2550 case CONNMAN_SERVICE_TYPE_WIFI:
2551 if (service->network) {
2552 const unsigned char *ssid;
2553 unsigned int ssid_len = 0;
2555 ssid = connman_network_get_blob(service->network,
2556 "WiFi.SSID", &ssid_len);
2558 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
2559 char *identifier = service->identifier;
2563 str = g_string_sized_new(ssid_len * 2);
2569 for (i = 0; i < ssid_len; i++)
2570 g_string_append_printf(str,
2573 g_key_file_set_string(keyfile, identifier,
2576 g_string_free(str, TRUE);
2581 case CONNMAN_SERVICE_TYPE_WIMAX:
2582 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2583 case CONNMAN_SERVICE_TYPE_CELLULAR:
2584 if (service->apn != NULL)
2585 g_key_file_set_string(keyfile, service->identifier,
2586 "APN", service->apn);
2588 if (service->username != NULL)
2589 g_key_file_set_string(keyfile, service->identifier,
2590 "Username", service->username);
2592 if (service->password != NULL)
2593 g_key_file_set_string(keyfile, service->identifier,
2594 "Password", service->password);
2596 g_key_file_set_boolean(keyfile, service->identifier,
2597 "Favorite", service->favorite);
2599 if (service->favorite == TRUE)
2600 g_key_file_set_boolean(keyfile, service->identifier,
2601 "AutoConnect", service->autoconnect);
2603 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
2604 const char *failure = error2string(service->error);
2605 if (failure != NULL)
2606 g_key_file_set_string(keyfile,
2607 service->identifier,
2608 "Failure", failure);
2610 g_key_file_remove_key(keyfile, service->identifier,
2616 str = g_time_val_to_iso8601(&service->modified);
2618 g_key_file_set_string(keyfile, service->identifier,
2623 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
2624 g_key_file_set_string(keyfile, service->identifier,
2625 "Passphrase", service->passphrase);
2627 g_key_file_remove_key(keyfile, service->identifier,
2628 "Passphrase", NULL);
2630 if (service->ipconfig != NULL)
2631 __connman_ipconfig_save(service->ipconfig, keyfile,
2632 service->identifier, "IPv4.");
2634 data = g_key_file_to_data(keyfile, &length, NULL);
2636 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
2637 connman_error("Failed to store service information");
2642 g_key_file_free(keyfile);
2649 static struct connman_storage service_storage = {
2651 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
2652 .service_load = service_load,
2653 .service_save = service_save,
2656 int __connman_service_init(void)
2660 connection = connman_dbus_get_connection();
2662 if (connman_storage_register(&service_storage) < 0)
2663 connman_error("Failed to register service storage");
2665 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2668 service_list = g_sequence_new(service_free);
2673 void __connman_service_cleanup(void)
2677 g_sequence_free(service_list);
2678 service_list = NULL;
2680 g_hash_table_destroy(service_hash);
2681 service_hash = NULL;
2683 connman_storage_unregister(&service_storage);
2685 dbus_connection_unref(connection);