5 * Copyright (C) 2007-2010 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 immutable;
51 connman_bool_t hidden;
52 connman_bool_t ignore;
53 connman_bool_t autoconnect;
54 connman_bool_t userconnect;
65 connman_bool_t roaming;
66 struct connman_ipconfig *ipconfig;
67 struct connman_network *network;
68 /* 802.1x settings from the config files */
72 char *client_cert_file;
73 char *private_key_file;
74 char *private_key_passphrase;
80 static void append_path(gpointer value, gpointer user_data)
82 struct connman_service *service = value;
83 DBusMessageIter *iter = user_data;
85 if (service->path == NULL || service->hidden == TRUE)
88 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
92 void __connman_service_list(DBusMessageIter *iter, void *user_data)
94 g_sequence_foreach(service_list, append_path, iter);
99 struct connman_service *service;
102 static void compare_path(gpointer value, gpointer user_data)
104 struct connman_service *service = value;
105 struct find_data *data = user_data;
107 if (data->service != NULL)
110 if (g_strcmp0(service->path, data->path) == 0)
111 data->service = service;
114 static struct connman_service *find_service(const char *path)
116 struct find_data data = { .path = path, .service = NULL };
118 DBG("path %s", path);
120 g_sequence_foreach(service_list, compare_path, &data);
125 const char *__connman_service_type2string(enum connman_service_type type)
128 case CONNMAN_SERVICE_TYPE_UNKNOWN:
130 case CONNMAN_SERVICE_TYPE_SYSTEM:
132 case CONNMAN_SERVICE_TYPE_ETHERNET:
134 case CONNMAN_SERVICE_TYPE_WIFI:
136 case CONNMAN_SERVICE_TYPE_WIMAX:
138 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
140 case CONNMAN_SERVICE_TYPE_CELLULAR:
142 case CONNMAN_SERVICE_TYPE_GPS:
144 case CONNMAN_SERVICE_TYPE_VPN:
151 static const char *mode2string(enum connman_service_mode mode)
154 case CONNMAN_SERVICE_MODE_UNKNOWN:
156 case CONNMAN_SERVICE_MODE_MANAGED:
158 case CONNMAN_SERVICE_MODE_ADHOC:
160 case CONNMAN_SERVICE_MODE_GPRS:
162 case CONNMAN_SERVICE_MODE_EDGE:
164 case CONNMAN_SERVICE_MODE_UMTS:
171 static const char *security2string(enum connman_service_security security)
174 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
176 case CONNMAN_SERVICE_SECURITY_NONE:
178 case CONNMAN_SERVICE_SECURITY_WEP:
180 case CONNMAN_SERVICE_SECURITY_PSK:
182 case CONNMAN_SERVICE_SECURITY_8021X:
184 case CONNMAN_SERVICE_SECURITY_WPA:
186 case CONNMAN_SERVICE_SECURITY_RSN:
193 static const char *state2string(enum connman_service_state state)
196 case CONNMAN_SERVICE_STATE_UNKNOWN:
198 case CONNMAN_SERVICE_STATE_IDLE:
200 case CONNMAN_SERVICE_STATE_ASSOCIATION:
201 return "association";
202 case CONNMAN_SERVICE_STATE_CONFIGURATION:
203 return "configuration";
204 case CONNMAN_SERVICE_STATE_READY:
206 case CONNMAN_SERVICE_STATE_DISCONNECT:
208 case CONNMAN_SERVICE_STATE_FAILURE:
215 static const char *error2string(enum connman_service_error error)
218 case CONNMAN_SERVICE_ERROR_UNKNOWN:
220 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
221 return "out-of-range";
222 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
223 return "pin-missing";
224 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
225 return "dhcp-failed";
226 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
227 return "connect-failed";
233 static enum connman_service_error string2error(const char *error)
235 if (g_strcmp0(error, "dhcp-failed") == 0)
236 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
237 else if (g_strcmp0(error, "pin-missing") == 0)
238 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
240 return CONNMAN_SERVICE_ERROR_UNKNOWN;
243 static struct connman_service *get_default(void)
245 struct connman_service *service;
248 iter = g_sequence_get_begin_iter(service_list);
250 if (g_sequence_iter_is_end(iter) == TRUE)
253 service = g_sequence_get(iter);
255 if (service->state != CONNMAN_SERVICE_STATE_READY)
261 static void default_changed(void)
263 struct connman_service *service = get_default();
265 __connman_notifier_default_changed(service);
268 const char *__connman_service_default(void)
270 struct connman_service *service;
272 service = get_default();
276 return __connman_service_type2string(service->type);
279 static void mode_changed(struct connman_service *service)
283 str = mode2string(service->mode);
287 connman_dbus_property_changed_basic(service->path,
288 CONNMAN_SERVICE_INTERFACE, "Mode",
289 DBUS_TYPE_STRING, &str);
292 static void state_changed(struct connman_service *service)
296 str = state2string(service->state);
300 connman_dbus_property_changed_basic(service->path,
301 CONNMAN_SERVICE_INTERFACE, "State",
302 DBUS_TYPE_STRING, &str);
305 static void strength_changed(struct connman_service *service)
307 if (service->strength == 0)
310 connman_dbus_property_changed_basic(service->path,
311 CONNMAN_SERVICE_INTERFACE, "Strength",
312 DBUS_TYPE_BYTE, &service->strength);
315 static void favorite_changed(struct connman_service *service)
317 if (service->path == NULL)
320 connman_dbus_property_changed_basic(service->path,
321 CONNMAN_SERVICE_INTERFACE, "Favorite",
322 DBUS_TYPE_BOOLEAN, &service->favorite);
325 static void immutable_changed(struct connman_service *service)
327 if (service->path == NULL)
330 connman_dbus_property_changed_basic(service->path,
331 CONNMAN_SERVICE_INTERFACE, "Immutable",
332 DBUS_TYPE_BOOLEAN, &service->immutable);
335 static void roaming_changed(struct connman_service *service)
337 if (service->path == NULL)
340 connman_dbus_property_changed_basic(service->path,
341 CONNMAN_SERVICE_INTERFACE, "Roaming",
342 DBUS_TYPE_BOOLEAN, &service->roaming);
345 static void autoconnect_changed(struct connman_service *service)
347 if (service->path == NULL)
350 connman_dbus_property_changed_basic(service->path,
351 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
352 DBUS_TYPE_BOOLEAN, &service->autoconnect);
355 static void passphrase_changed(struct connman_service *service)
357 dbus_bool_t required;
359 switch (service->type) {
360 case CONNMAN_SERVICE_TYPE_UNKNOWN:
361 case CONNMAN_SERVICE_TYPE_SYSTEM:
362 case CONNMAN_SERVICE_TYPE_ETHERNET:
363 case CONNMAN_SERVICE_TYPE_WIMAX:
364 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
365 case CONNMAN_SERVICE_TYPE_CELLULAR:
366 case CONNMAN_SERVICE_TYPE_GPS:
367 case CONNMAN_SERVICE_TYPE_VPN:
369 case CONNMAN_SERVICE_TYPE_WIFI:
372 switch (service->security) {
373 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
374 case CONNMAN_SERVICE_SECURITY_NONE:
376 case CONNMAN_SERVICE_SECURITY_WEP:
377 case CONNMAN_SERVICE_SECURITY_PSK:
378 case CONNMAN_SERVICE_SECURITY_WPA:
379 case CONNMAN_SERVICE_SECURITY_RSN:
380 if (service->passphrase == NULL)
383 case CONNMAN_SERVICE_SECURITY_8021X:
389 connman_dbus_property_changed_basic(service->path,
390 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
391 DBUS_TYPE_BOOLEAN, &required);
394 static void apn_changed(struct connman_service *service)
396 dbus_bool_t required;
398 switch (service->type) {
399 case CONNMAN_SERVICE_TYPE_UNKNOWN:
400 case CONNMAN_SERVICE_TYPE_SYSTEM:
401 case CONNMAN_SERVICE_TYPE_ETHERNET:
402 case CONNMAN_SERVICE_TYPE_WIMAX:
403 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
404 case CONNMAN_SERVICE_TYPE_WIFI:
405 case CONNMAN_SERVICE_TYPE_GPS:
406 case CONNMAN_SERVICE_TYPE_VPN:
408 case CONNMAN_SERVICE_TYPE_CELLULAR:
412 required = (service->apn == NULL) ? TRUE : FALSE;
414 connman_dbus_property_changed_basic(service->path,
415 CONNMAN_SERVICE_INTERFACE, "SetupRequired",
416 DBUS_TYPE_BOOLEAN, &required);
419 static void append_ethernet(DBusMessageIter *iter, void *user_data)
421 struct connman_service *service = user_data;
423 switch (service->state) {
424 case CONNMAN_SERVICE_STATE_UNKNOWN:
425 case CONNMAN_SERVICE_STATE_IDLE:
426 case CONNMAN_SERVICE_STATE_FAILURE:
427 case CONNMAN_SERVICE_STATE_DISCONNECT:
429 case CONNMAN_SERVICE_STATE_ASSOCIATION:
430 case CONNMAN_SERVICE_STATE_CONFIGURATION:
431 case CONNMAN_SERVICE_STATE_READY:
435 if (service->ipconfig != NULL)
436 __connman_ipconfig_append_ethernet(service->ipconfig, iter);
439 static void append_ipv4(DBusMessageIter *iter, void *user_data)
441 struct connman_service *service = user_data;
443 switch (service->state) {
444 case CONNMAN_SERVICE_STATE_UNKNOWN:
445 case CONNMAN_SERVICE_STATE_IDLE:
446 case CONNMAN_SERVICE_STATE_FAILURE:
447 case CONNMAN_SERVICE_STATE_DISCONNECT:
448 case CONNMAN_SERVICE_STATE_ASSOCIATION:
449 case CONNMAN_SERVICE_STATE_CONFIGURATION:
451 case CONNMAN_SERVICE_STATE_READY:
455 if (service->ipconfig != NULL)
456 __connman_ipconfig_append_ipv4(service->ipconfig, iter);
459 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
461 struct connman_service *service = user_data;
463 if (service->ipconfig != NULL)
464 __connman_ipconfig_append_ipv4config(service->ipconfig, iter);
467 static void append_proxy(DBusMessageIter *iter, void *user_data)
469 struct connman_service *service = user_data;
471 switch (service->state) {
472 case CONNMAN_SERVICE_STATE_UNKNOWN:
473 case CONNMAN_SERVICE_STATE_IDLE:
474 case CONNMAN_SERVICE_STATE_FAILURE:
475 case CONNMAN_SERVICE_STATE_DISCONNECT:
476 case CONNMAN_SERVICE_STATE_ASSOCIATION:
477 case CONNMAN_SERVICE_STATE_CONFIGURATION:
479 case CONNMAN_SERVICE_STATE_READY:
483 if (service->ipconfig != NULL)
484 __connman_ipconfig_append_proxy(service->ipconfig, iter);
487 static void settings_changed(struct connman_service *service)
489 connman_dbus_property_changed_dict(service->path,
490 CONNMAN_SERVICE_INTERFACE, "IPv4",
491 append_ipv4, service);
494 static void ipv4_configuration_changed(struct connman_service *service)
496 connman_dbus_property_changed_dict(service->path,
497 CONNMAN_SERVICE_INTERFACE,
498 "IPv4.Configuration",
503 static DBusMessage *get_properties(DBusConnection *conn,
504 DBusMessage *msg, void *user_data)
506 struct connman_service *service = user_data;
508 DBusMessageIter array, dict;
509 dbus_bool_t required;
512 DBG("service %p", service);
514 reply = dbus_message_new_method_return(msg);
518 dbus_message_iter_init_append(reply, &array);
520 connman_dbus_dict_open(&array, &dict);
522 str = __connman_service_type2string(service->type);
524 connman_dbus_dict_append_basic(&dict, "Type",
525 DBUS_TYPE_STRING, &str);
527 str = mode2string(service->mode);
529 connman_dbus_dict_append_basic(&dict, "Mode",
530 DBUS_TYPE_STRING, &str);
532 str = security2string(service->security);
534 connman_dbus_dict_append_basic(&dict, "Security",
535 DBUS_TYPE_STRING, &str);
537 str = state2string(service->state);
539 connman_dbus_dict_append_basic(&dict, "State",
540 DBUS_TYPE_STRING, &str);
542 str = error2string(service->error);
544 connman_dbus_dict_append_basic(&dict, "Error",
545 DBUS_TYPE_STRING, &str);
547 if (service->strength > 0)
548 connman_dbus_dict_append_basic(&dict, "Strength",
549 DBUS_TYPE_BYTE, &service->strength);
551 connman_dbus_dict_append_basic(&dict, "Favorite",
552 DBUS_TYPE_BOOLEAN, &service->favorite);
554 connman_dbus_dict_append_basic(&dict, "Immutable",
555 DBUS_TYPE_BOOLEAN, &service->immutable);
557 if (service->favorite == TRUE)
558 connman_dbus_dict_append_basic(&dict, "AutoConnect",
559 DBUS_TYPE_BOOLEAN, &service->autoconnect);
561 connman_dbus_dict_append_basic(&dict, "AutoConnect",
562 DBUS_TYPE_BOOLEAN, &service->favorite);
564 if (service->name != NULL)
565 connman_dbus_dict_append_basic(&dict, "Name",
566 DBUS_TYPE_STRING, &service->name);
568 switch (service->type) {
569 case CONNMAN_SERVICE_TYPE_UNKNOWN:
570 case CONNMAN_SERVICE_TYPE_SYSTEM:
571 case CONNMAN_SERVICE_TYPE_GPS:
572 case CONNMAN_SERVICE_TYPE_VPN:
574 case CONNMAN_SERVICE_TYPE_CELLULAR:
575 connman_dbus_dict_append_basic(&dict, "Roaming",
576 DBUS_TYPE_BOOLEAN, &service->roaming);
578 if (service->mcc != NULL && service->mnc != NULL) {
579 connman_dbus_dict_append_basic(&dict, "MCC",
580 DBUS_TYPE_STRING, &service->mcc);
581 connman_dbus_dict_append_basic(&dict, "MNC",
582 DBUS_TYPE_STRING, &service->mnc);
585 if (service->apn != NULL) {
586 connman_dbus_dict_append_basic(&dict, "APN",
587 DBUS_TYPE_STRING, &service->apn);
589 if (service->username != NULL)
590 connman_dbus_dict_append_basic(&dict,
591 "Username", DBUS_TYPE_STRING,
594 if (service->password != NULL)
595 connman_dbus_dict_append_basic(&dict,
596 "Password", DBUS_TYPE_STRING,
603 connman_dbus_dict_append_basic(&dict, "SetupRequired",
604 DBUS_TYPE_BOOLEAN, &required);
606 case CONNMAN_SERVICE_TYPE_WIFI:
607 if (service->passphrase != NULL &&
608 __connman_security_check_privilege(msg,
609 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
610 connman_dbus_dict_append_basic(&dict, "Passphrase",
611 DBUS_TYPE_STRING, &service->passphrase);
615 switch (service->security) {
616 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
617 case CONNMAN_SERVICE_SECURITY_NONE:
619 case CONNMAN_SERVICE_SECURITY_WEP:
620 case CONNMAN_SERVICE_SECURITY_PSK:
621 case CONNMAN_SERVICE_SECURITY_WPA:
622 case CONNMAN_SERVICE_SECURITY_RSN:
623 if (service->passphrase == NULL)
626 case CONNMAN_SERVICE_SECURITY_8021X:
630 connman_dbus_dict_append_basic(&dict, "PassphraseRequired",
631 DBUS_TYPE_BOOLEAN, &required);
633 case CONNMAN_SERVICE_TYPE_ETHERNET:
634 case CONNMAN_SERVICE_TYPE_WIMAX:
635 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
636 connman_dbus_dict_append_dict(&dict, "Ethernet",
637 append_ethernet, service);
641 connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4, service);
643 connman_dbus_dict_append_dict(&dict, "IPv4.Configuration",
644 append_ipv4config, service);
646 connman_dbus_dict_append_dict(&dict, "Proxy", append_proxy, service);
648 connman_dbus_dict_close(&array, &dict);
653 static DBusMessage *set_property(DBusConnection *conn,
654 DBusMessage *msg, void *user_data)
656 struct connman_service *service = user_data;
657 DBusMessageIter iter, value;
661 DBG("service %p", service);
663 if (dbus_message_iter_init(msg, &iter) == FALSE)
664 return __connman_error_invalid_arguments(msg);
666 dbus_message_iter_get_basic(&iter, &name);
667 dbus_message_iter_next(&iter);
668 dbus_message_iter_recurse(&iter, &value);
670 if (__connman_security_check_privilege(msg,
671 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
672 return __connman_error_permission_denied(msg);
674 type = dbus_message_iter_get_arg_type(&value);
676 if (g_str_has_prefix(name, "AutoConnect") == TRUE) {
677 connman_bool_t autoconnect;
679 if (type != DBUS_TYPE_BOOLEAN)
680 return __connman_error_invalid_arguments(msg);
682 if (service->favorite == FALSE)
683 return __connman_error_invalid_service(msg);
685 dbus_message_iter_get_basic(&value, &autoconnect);
687 if (service->autoconnect == autoconnect)
688 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
690 service->autoconnect = autoconnect;
692 autoconnect_changed(service);
694 __connman_storage_save_service(service);
695 } else if (g_str_equal(name, "Passphrase") == TRUE) {
696 const char *passphrase;
698 if (type != DBUS_TYPE_STRING)
699 return __connman_error_invalid_arguments(msg);
701 if (__connman_security_check_privilege(msg,
702 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
703 return __connman_error_permission_denied(msg);
705 dbus_message_iter_get_basic(&value, &passphrase);
707 g_free(service->passphrase);
708 service->passphrase = g_strdup(passphrase);
710 passphrase_changed(service);
712 if (service->network != NULL)
713 connman_network_set_string(service->network,
714 "WiFi.Passphrase", service->passphrase);
716 __connman_storage_save_service(service);
717 } else if (g_str_equal(name, "APN") == TRUE) {
720 if (type != DBUS_TYPE_STRING)
721 return __connman_error_invalid_arguments(msg);
723 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
724 return __connman_error_invalid_service(msg);
726 dbus_message_iter_get_basic(&value, &apn);
728 g_free(service->apn);
729 service->apn = g_strdup(apn);
731 apn_changed(service);
733 if (service->network != NULL)
734 connman_network_set_string(service->network,
735 "Cellular.APN", service->apn);
737 __connman_storage_save_service(service);
738 } else if (g_str_equal(name, "Username") == TRUE) {
739 const char *username;
741 if (type != DBUS_TYPE_STRING)
742 return __connman_error_invalid_arguments(msg);
744 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
745 return __connman_error_invalid_service(msg);
747 dbus_message_iter_get_basic(&value, &username);
749 g_free(service->username);
750 service->username = g_strdup(username);
752 if (service->network != NULL)
753 connman_network_set_string(service->network,
754 "Cellular.Username", service->username);
756 __connman_storage_save_service(service);
757 } else if (g_str_equal(name, "Password") == TRUE) {
758 const char *password;
760 if (type != DBUS_TYPE_STRING)
761 return __connman_error_invalid_arguments(msg);
763 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
764 return __connman_error_invalid_service(msg);
766 dbus_message_iter_get_basic(&value, &password);
768 g_free(service->password);
769 service->password = g_strdup(password);
771 if (service->network != NULL)
772 connman_network_set_string(service->network,
773 "Cellular.Password", service->password);
775 __connman_storage_save_service(service);
776 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
779 if (service->ipconfig == NULL)
780 return __connman_error_invalid_property(msg);
782 err = __connman_ipconfig_set_ipv4config(service->ipconfig,
785 return __connman_error_failed(msg, -err);
787 ipv4_configuration_changed(service);
789 __connman_storage_save_service(service);
791 return __connman_error_invalid_property(msg);
793 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
796 static void set_idle(struct connman_service *service)
798 service->state = CONNMAN_SERVICE_STATE_IDLE;
799 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
800 state_changed(service);
803 static DBusMessage *clear_property(DBusConnection *conn,
804 DBusMessage *msg, void *user_data)
806 struct connman_service *service = user_data;
809 DBG("service %p", service);
811 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
814 if (__connman_security_check_privilege(msg,
815 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
816 return __connman_error_permission_denied(msg);
818 if (g_str_equal(name, "Error") == TRUE) {
821 g_get_current_time(&service->modified);
822 __connman_storage_save_service(service);
823 } else if (g_str_equal(name, "Passphrase") == TRUE) {
824 g_free(service->passphrase);
825 service->passphrase = NULL;
827 passphrase_changed(service);
829 __connman_storage_save_service(service);
831 return __connman_error_invalid_property(msg);
833 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
836 static connman_bool_t is_connecting(struct connman_service *service)
838 switch (service->state) {
839 case CONNMAN_SERVICE_STATE_UNKNOWN:
840 case CONNMAN_SERVICE_STATE_IDLE:
841 case CONNMAN_SERVICE_STATE_FAILURE:
842 case CONNMAN_SERVICE_STATE_DISCONNECT:
843 case CONNMAN_SERVICE_STATE_READY:
845 case CONNMAN_SERVICE_STATE_ASSOCIATION:
846 case CONNMAN_SERVICE_STATE_CONFIGURATION:
853 static connman_bool_t is_ignore(struct connman_service *service)
855 if (service->autoconnect == FALSE)
858 if (service->roaming == TRUE)
861 if (service->ignore == TRUE)
864 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
870 void __connman_service_auto_connect(void)
872 struct connman_service *service = NULL;
877 iter = g_sequence_get_begin_iter(service_list);
879 while (g_sequence_iter_is_end(iter) == FALSE) {
880 service = g_sequence_get(iter);
882 if (service->pending != NULL)
885 if (is_connecting(service) == TRUE)
888 if (service->favorite == FALSE)
891 if (service->state == CONNMAN_SERVICE_STATE_READY)
894 if (is_ignore(service) == FALSE &&
895 service->state == CONNMAN_SERVICE_STATE_IDLE)
900 iter = g_sequence_iter_next(iter);
903 if (service != NULL) {
904 service->userconnect = FALSE;
905 __connman_service_connect(service);
909 static void remove_timeout(struct connman_service *service)
911 if (service->timeout > 0) {
912 g_source_remove(service->timeout);
913 service->timeout = 0;
917 static void reply_pending(struct connman_service *service, int error)
919 remove_timeout(service);
921 if (service->pending != NULL) {
925 reply = __connman_error_failed(service->pending,
928 g_dbus_send_message(connection, reply);
930 g_dbus_send_reply(connection, service->pending,
933 dbus_message_unref(service->pending);
934 service->pending = NULL;
938 static gboolean connect_timeout(gpointer user_data)
940 struct connman_service *service = user_data;
941 connman_bool_t autoconnect = FALSE;
943 DBG("service %p", service);
945 service->timeout = 0;
947 if (service->network != NULL)
948 __connman_network_disconnect(service->network);
950 __connman_ipconfig_disable(service->ipconfig);
952 if (service->pending != NULL) {
955 reply = __connman_error_operation_timeout(service->pending);
957 g_dbus_send_message(connection, reply);
959 dbus_message_unref(service->pending);
960 service->pending = NULL;
964 __connman_service_indicate_state(service,
965 CONNMAN_SERVICE_STATE_FAILURE);
967 if (autoconnect == TRUE && service->userconnect == FALSE)
968 __connman_service_auto_connect();
973 static void set_reconnect_state(struct connman_service *service,
974 connman_bool_t reconnect)
976 struct connman_device *device;
978 if (service->network == NULL)
981 device = connman_network_get_device(service->network);
985 __connman_device_set_reconnect(device, reconnect);
988 static connman_bool_t get_reconnect_state(struct connman_service *service)
990 struct connman_device *device;
992 if (service->network == NULL)
995 device = connman_network_get_device(service->network);
999 return __connman_device_get_reconnect(device);
1002 static DBusMessage *connect_service(DBusConnection *conn,
1003 DBusMessage *msg, void *user_data)
1005 struct connman_service *service = user_data;
1006 GSequenceIter *iter;
1009 DBG("service %p", service);
1011 if (service->pending != NULL)
1012 return __connman_error_in_progress(msg);
1014 iter = g_sequence_get_begin_iter(service_list);
1016 while (g_sequence_iter_is_end(iter) == FALSE) {
1017 struct connman_service *temp = g_sequence_get(iter);
1019 if (service->type == temp->type &&
1020 is_connecting(temp) == TRUE)
1021 return __connman_error_in_progress(msg);
1023 iter = g_sequence_iter_next(iter);
1026 service->ignore = FALSE;
1028 service->userconnect = TRUE;
1030 service->pending = dbus_message_ref(msg);
1032 set_reconnect_state(service, FALSE);
1034 err = __connman_service_connect(service);
1036 if (err == -ENOKEY) {
1037 if (__connman_agent_request_passphrase(service,
1042 if (err != -EINPROGRESS) {
1043 dbus_message_unref(service->pending);
1044 service->pending = NULL;
1046 return __connman_error_failed(msg, -err);
1052 dbus_message_unref(service->pending);
1053 service->pending = NULL;
1055 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1058 static DBusMessage *disconnect_service(DBusConnection *conn,
1059 DBusMessage *msg, void *user_data)
1061 struct connman_service *service = user_data;
1064 DBG("service %p", service);
1066 reply_pending(service, ECONNABORTED);
1068 service->ignore = TRUE;
1070 set_reconnect_state(service, FALSE);
1072 err = __connman_service_disconnect(service);
1074 if (err != -EINPROGRESS)
1075 return __connman_error_failed(msg, -err);
1080 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1083 static DBusMessage *remove_service(DBusConnection *conn,
1084 DBusMessage *msg, void *user_data)
1086 struct connman_service *service = user_data;
1088 DBG("service %p", service);
1090 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
1091 return __connman_error_not_supported(msg);
1093 if (service->favorite == FALSE &&
1094 service->state != CONNMAN_SERVICE_STATE_FAILURE)
1095 return __connman_error_not_supported(msg);
1097 if (service->network != NULL) {
1098 set_reconnect_state(service, FALSE);
1100 __connman_network_disconnect(service->network);
1103 g_free(service->passphrase);
1104 service->passphrase = NULL;
1106 passphrase_changed(service);
1108 g_free(service->apn);
1109 service->apn = NULL;
1111 g_free(service->username);
1112 service->username = NULL;
1114 g_free(service->password);
1115 service->password = NULL;
1117 apn_changed(service);
1121 __connman_service_set_favorite(service, FALSE);
1122 __connman_storage_save_service(service);
1124 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1127 static DBusMessage *move_before(DBusConnection *conn,
1128 DBusMessage *msg, void *user_data)
1130 struct connman_service *service = user_data;
1131 struct connman_service *target;
1133 GSequenceIter *src, *dst;
1135 DBG("service %p", service);
1137 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1140 if (service->favorite == FALSE)
1141 return __connman_error_not_supported(msg);
1143 target = find_service(path);
1144 if (target == NULL || target->favorite == FALSE || target == service)
1145 return __connman_error_invalid_service(msg);
1147 DBG("target %s", target->identifier);
1149 if (target->state != service->state)
1150 return __connman_error_invalid_service(msg);
1152 g_get_current_time(&service->modified);
1153 __connman_storage_save_service(service);
1155 src = g_hash_table_lookup(service_hash, service->identifier);
1156 dst = g_hash_table_lookup(service_hash, target->identifier);
1158 g_sequence_move(src, dst);
1160 __connman_profile_changed(FALSE);
1162 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1165 static DBusMessage *move_after(DBusConnection *conn,
1166 DBusMessage *msg, void *user_data)
1168 struct connman_service *service = user_data;
1169 struct connman_service *target;
1172 DBG("service %p", service);
1174 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1177 if (service->favorite == FALSE)
1178 return __connman_error_not_supported(msg);
1180 target = find_service(path);
1181 if (target == NULL || target->favorite == FALSE || target == service)
1182 return __connman_error_invalid_service(msg);
1184 DBG("target %s", target->identifier);
1186 if (target->state != service->state)
1187 return __connman_error_invalid_service(msg);
1189 g_get_current_time(&service->modified);
1190 __connman_storage_save_service(service);
1192 return __connman_error_not_implemented(msg);
1195 static GDBusMethodTable service_methods[] = {
1196 { "GetProperties", "", "a{sv}", get_properties },
1197 { "SetProperty", "sv", "", set_property },
1198 { "ClearProperty", "s", "", clear_property },
1199 { "Connect", "", "", connect_service,
1200 G_DBUS_METHOD_FLAG_ASYNC },
1201 { "Disconnect", "", "", disconnect_service },
1202 { "Remove", "", "", remove_service },
1203 { "MoveBefore", "o", "", move_before },
1204 { "MoveAfter", "o", "", move_after },
1208 static GDBusSignalTable service_signals[] = {
1209 { "PropertyChanged", "sv" },
1213 static void service_free(gpointer user_data)
1215 struct connman_service *service = user_data;
1216 char *path = service->path;
1218 DBG("service %p", service);
1220 reply_pending(service, ENOENT);
1222 g_hash_table_remove(service_hash, service->identifier);
1224 service->path = NULL;
1227 __connman_profile_changed(FALSE);
1229 g_dbus_unregister_interface(connection, path,
1230 CONNMAN_SERVICE_INTERFACE);
1234 if (service->network != NULL)
1235 connman_network_unref(service->network);
1237 if (service->ipconfig != NULL) {
1238 connman_ipconfig_unref(service->ipconfig);
1239 service->ipconfig = NULL;
1242 g_free(service->mcc);
1243 g_free(service->mnc);
1244 g_free(service->apn);
1245 g_free(service->username);
1246 g_free(service->password);
1247 g_free(service->profile);
1248 g_free(service->name);
1249 g_free(service->passphrase);
1250 g_free(service->identifier);
1251 g_free(service->eap);
1252 g_free(service->identity);
1253 g_free(service->ca_cert_file);
1254 g_free(service->client_cert_file);
1255 g_free(service->private_key_file);
1256 g_free(service->private_key_passphrase);
1257 g_free(service->phase2);
1262 * __connman_service_put:
1263 * @service: service structure
1265 * Release service if no longer needed
1267 void __connman_service_put(struct connman_service *service)
1269 DBG("service %p", service);
1271 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
1272 GSequenceIter *iter;
1274 iter = g_hash_table_lookup(service_hash, service->identifier);
1276 reply_pending(service, ECONNABORTED);
1278 __connman_service_disconnect(service);
1280 g_sequence_remove(iter);
1282 service_free(service);
1286 static void __connman_service_initialize(struct connman_service *service)
1288 DBG("service %p", service);
1290 service->refcount = 1;
1292 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
1293 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
1294 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
1295 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
1297 service->favorite = FALSE;
1298 service->immutable = FALSE;
1299 service->hidden = FALSE;
1301 service->ignore = FALSE;
1303 service->userconnect = FALSE;
1309 * connman_service_create:
1311 * Allocate a new service.
1313 * Returns: a newly-allocated #connman_service structure
1315 struct connman_service *connman_service_create(void)
1317 struct connman_service *service;
1319 service = g_try_new0(struct connman_service, 1);
1320 if (service == NULL)
1323 DBG("service %p", service);
1325 __connman_service_initialize(service);
1331 * connman_service_ref:
1332 * @service: service structure
1334 * Increase reference counter of service
1336 struct connman_service *connman_service_ref(struct connman_service *service)
1338 g_atomic_int_inc(&service->refcount);
1344 * connman_service_unref:
1345 * @service: service structure
1347 * Decrease reference counter of service
1349 void connman_service_unref(struct connman_service *service)
1351 __connman_service_put(service);
1354 static gint service_compare(gconstpointer a, gconstpointer b,
1357 struct connman_service *service_a = (void *) a;
1358 struct connman_service *service_b = (void *) b;
1360 if (service_a->state != service_b->state) {
1361 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
1363 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
1367 if (service_a->order > service_b->order)
1370 if (service_a->order < service_b->order)
1373 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
1376 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
1379 if (service_a->type != service_b->type) {
1380 switch (service_a->type) {
1381 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1382 case CONNMAN_SERVICE_TYPE_SYSTEM:
1383 case CONNMAN_SERVICE_TYPE_ETHERNET:
1384 case CONNMAN_SERVICE_TYPE_GPS:
1385 case CONNMAN_SERVICE_TYPE_VPN:
1387 case CONNMAN_SERVICE_TYPE_WIFI:
1389 case CONNMAN_SERVICE_TYPE_WIMAX:
1390 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1391 case CONNMAN_SERVICE_TYPE_CELLULAR:
1396 return (gint) service_b->strength - (gint) service_a->strength;
1400 * connman_service_get_type:
1401 * @service: service structure
1403 * Get the type of service
1405 enum connman_service_type connman_service_get_type(struct connman_service *service)
1407 if (service == NULL)
1408 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1410 return service->type;
1414 * connman_service_get_interface:
1415 * @service: service structure
1417 * Get network interface of service
1419 char *connman_service_get_interface(struct connman_service *service)
1423 if (service == NULL)
1426 if (service->network == NULL)
1429 index = connman_network_get_index(service->network);
1431 return connman_inet_ifname(index);
1435 * connman_service_get_network:
1436 * @service: service structure
1438 * Get the service network
1440 struct connman_network *
1441 __connman_service_get_network(struct connman_service *service)
1443 if (service == NULL)
1446 return service->network;
1450 * __connman_service_set_favorite:
1451 * @service: service structure
1452 * @favorite: favorite value
1454 * Change the favorite setting of service
1456 int __connman_service_set_favorite(struct connman_service *service,
1457 connman_bool_t favorite)
1459 GSequenceIter *iter;
1461 iter = g_hash_table_lookup(service_hash, service->identifier);
1465 if (service->favorite == favorite)
1468 service->favorite = favorite;
1470 favorite_changed(service);
1472 g_sequence_sort_changed(iter, service_compare, NULL);
1474 __connman_profile_changed(FALSE);
1479 int __connman_service_set_immutable(struct connman_service *service,
1480 connman_bool_t immutable)
1482 service->immutable = immutable;
1484 immutable_changed(service);
1489 void __connman_service_set_string(struct connman_service *service,
1490 const char *key, const char *value)
1492 if (g_str_equal(key, "EAP") == TRUE) {
1493 g_free(service->eap);
1494 service->eap = g_strdup(value);
1495 } else if (g_str_equal(key, "Identity") == TRUE) {
1496 g_free(service->identity);
1497 service->identity = g_strdup(value);
1498 } else if (g_str_equal(key, "CACertFile") == TRUE) {
1499 g_free(service->ca_cert_file);
1500 service->ca_cert_file = g_strdup(value);
1501 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
1502 g_free(service->client_cert_file);
1503 service->client_cert_file = g_strdup(value);
1504 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
1505 g_free(service->private_key_file);
1506 service->private_key_file = g_strdup(value);
1507 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
1508 g_free(service->private_key_passphrase);
1509 service->private_key_passphrase = g_strdup(value);
1510 } else if (g_str_equal(key, "Phase2") == TRUE) {
1511 g_free(service->phase2);
1512 service->phase2 = g_strdup(value);
1513 } else if (g_str_equal(key, "Passphrase") == TRUE) {
1514 g_free(service->passphrase);
1515 service->passphrase = g_strdup(value);
1519 int __connman_service_indicate_state(struct connman_service *service,
1520 enum connman_service_state state)
1522 GSequenceIter *iter;
1524 DBG("service %p state %d", service, state);
1526 if (service == NULL)
1529 if (service->state == state)
1532 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
1533 state == CONNMAN_SERVICE_STATE_IDLE)
1536 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
1537 state == CONNMAN_SERVICE_STATE_DISCONNECT)
1540 if (state == CONNMAN_SERVICE_STATE_IDLE &&
1541 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
1542 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
1543 state_changed(service);
1545 reply_pending(service, ECONNABORTED);
1547 __connman_service_disconnect(service);
1550 if (state == CONNMAN_SERVICE_STATE_CONFIGURATION)
1551 __connman_ipconfig_enable(service->ipconfig);
1553 service->state = state;
1554 state_changed(service);
1556 if (state == CONNMAN_SERVICE_STATE_IDLE) {
1557 connman_bool_t reconnect;
1559 reconnect = get_reconnect_state(service);
1560 if (reconnect == TRUE)
1561 __connman_service_auto_connect();
1564 if (state == CONNMAN_SERVICE_STATE_READY) {
1565 set_reconnect_state(service, TRUE);
1567 __connman_service_set_favorite(service, TRUE);
1569 reply_pending(service, 0);
1571 service->userconnect = FALSE;
1573 g_get_current_time(&service->modified);
1574 __connman_storage_save_service(service);
1576 __connman_notifier_connect(service->type);
1579 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
1582 __connman_notifier_disconnect(service->type);
1585 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
1586 reply_pending(service, EIO);
1588 if (service->userconnect == FALSE)
1589 __connman_service_auto_connect();
1591 g_get_current_time(&service->modified);
1592 __connman_storage_save_service(service);
1594 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1596 iter = g_hash_table_lookup(service_hash, service->identifier);
1598 g_sequence_sort_changed(iter, service_compare, NULL);
1600 __connman_profile_changed(FALSE);
1602 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
1603 service->state == CONNMAN_SERVICE_STATE_FAILURE)
1604 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
1609 int __connman_service_indicate_error(struct connman_service *service,
1610 enum connman_service_error error)
1612 DBG("service %p error %d", service, error);
1614 if (service == NULL)
1617 service->error = error;
1619 return __connman_service_indicate_state(service,
1620 CONNMAN_SERVICE_STATE_FAILURE);
1623 int __connman_service_indicate_default(struct connman_service *service)
1625 DBG("service %p", service);
1632 static connman_bool_t prepare_network(struct connman_service *service)
1634 enum connman_network_type type;
1635 unsigned int ssid_len;
1637 type = connman_network_get_type(service->network);
1640 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1641 case CONNMAN_NETWORK_TYPE_VENDOR:
1643 case CONNMAN_NETWORK_TYPE_WIFI:
1644 if (connman_network_get_blob(service->network, "WiFi.SSID",
1648 connman_network_set_string(service->network,
1649 "WiFi.Passphrase", service->passphrase);
1651 case CONNMAN_NETWORK_TYPE_ETHERNET:
1652 case CONNMAN_NETWORK_TYPE_WIMAX:
1653 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1654 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1655 case CONNMAN_NETWORK_TYPE_CELLULAR:
1657 case CONNMAN_NETWORK_TYPE_MBM:
1658 case CONNMAN_NETWORK_TYPE_HSO:
1659 connman_network_set_string(service->network,
1660 "Cellular.APN", service->apn);
1662 connman_network_set_string(service->network,
1663 "Cellular.Username", service->username);
1664 connman_network_set_string(service->network,
1665 "Cellular.Password", service->password);
1672 static void prepare_8021x(struct connman_service *service)
1674 if (service->eap != NULL)
1675 connman_network_set_string(service->network, "WiFi.EAP",
1678 if (service->identity != NULL)
1679 connman_network_set_string(service->network, "WiFi.Identity",
1682 if (service->ca_cert_file != NULL)
1683 connman_network_set_string(service->network, "WiFi.CACertFile",
1684 service->ca_cert_file);
1686 if (service->client_cert_file != NULL)
1687 connman_network_set_string(service->network,
1688 "WiFi.ClientCertFile",
1689 service->client_cert_file);
1691 if (service->private_key_file != NULL)
1692 connman_network_set_string(service->network,
1693 "WiFi.PrivateKeyFile",
1694 service->private_key_file);
1696 if (service->private_key_passphrase != NULL)
1697 connman_network_set_string(service->network,
1698 "WiFi.PrivateKeyPassphrase",
1699 service->private_key_passphrase);
1701 if (service->phase2 != NULL)
1702 connman_network_set_string(service->network, "WiFi.Phase2",
1706 int __connman_service_connect(struct connman_service *service)
1710 DBG("service %p", service);
1712 if (service->state == CONNMAN_SERVICE_STATE_READY)
1715 if (is_connecting(service) == TRUE)
1718 switch (service->type) {
1719 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1720 case CONNMAN_SERVICE_TYPE_SYSTEM:
1721 case CONNMAN_SERVICE_TYPE_GPS:
1722 case CONNMAN_SERVICE_TYPE_VPN:
1724 case CONNMAN_SERVICE_TYPE_ETHERNET:
1725 case CONNMAN_SERVICE_TYPE_WIMAX:
1726 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1728 case CONNMAN_SERVICE_TYPE_CELLULAR:
1729 if (service->apn == NULL)
1732 case CONNMAN_SERVICE_TYPE_WIFI:
1733 switch (service->security) {
1734 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1735 case CONNMAN_SERVICE_SECURITY_NONE:
1737 case CONNMAN_SERVICE_SECURITY_WEP:
1738 case CONNMAN_SERVICE_SECURITY_PSK:
1739 case CONNMAN_SERVICE_SECURITY_WPA:
1740 case CONNMAN_SERVICE_SECURITY_RSN:
1741 if (service->passphrase == NULL)
1744 case CONNMAN_SERVICE_SECURITY_8021X:
1750 if (service->network != NULL) {
1751 if (prepare_network(service) == FALSE)
1754 switch (service->security) {
1755 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1756 case CONNMAN_SERVICE_SECURITY_NONE:
1757 case CONNMAN_SERVICE_SECURITY_WEP:
1758 case CONNMAN_SERVICE_SECURITY_PSK:
1759 case CONNMAN_SERVICE_SECURITY_WPA:
1760 case CONNMAN_SERVICE_SECURITY_RSN:
1762 case CONNMAN_SERVICE_SECURITY_8021X:
1763 prepare_8021x(service);
1767 __connman_ipconfig_enable(service->ipconfig);
1769 err = __connman_network_connect(service->network);
1774 if (err != -EINPROGRESS) {
1775 __connman_ipconfig_disable(service->ipconfig);
1779 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
1780 connect_timeout, service);
1782 return -EINPROGRESS;
1788 int __connman_service_disconnect(struct connman_service *service)
1792 DBG("service %p", service);
1794 if (service->network != NULL) {
1795 err = __connman_network_disconnect(service->network);
1799 __connman_ipconfig_disable(service->ipconfig);
1802 if (err != -EINPROGRESS)
1805 return -EINPROGRESS;
1812 * __connman_service_lookup:
1813 * @identifier: service identifier
1815 * Look up a service by identifier (reference count will not be increased)
1817 static struct connman_service *__connman_service_lookup(const char *identifier)
1819 GSequenceIter *iter;
1821 iter = g_hash_table_lookup(service_hash, identifier);
1823 return g_sequence_get(iter);
1828 static struct connman_network *create_hidden_wifi(struct connman_device *device,
1829 const char *ssid, const char *mode, const char *security)
1831 struct connman_network *network;
1834 unsigned int i, ssid_len;
1836 ssid_len = strlen(ssid);
1840 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
1841 if (network == NULL)
1844 connman_network_set_blob(network, "WiFi.SSID",
1845 (unsigned char *) ssid, ssid_len);
1847 connman_network_set_string(network, "WiFi.Mode", mode);
1848 connman_network_set_string(network, "WiFi.Security", security);
1850 name = g_try_malloc0(ssid_len + 1);
1852 connman_network_unref(network);
1856 for (i = 0; i < ssid_len; i++) {
1857 if (g_ascii_isprint(ssid[i]))
1863 connman_network_set_name(network, name);
1867 index = connman_device_get_index(device);
1868 connman_network_set_index(network, index);
1870 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
1872 if (connman_device_add_network(device, network) < 0) {
1873 connman_network_unref(network);
1877 connman_network_set_available(network, TRUE);
1882 int __connman_service_create_and_connect(DBusMessage *msg)
1884 struct connman_service *service;
1885 struct connman_network *network;
1886 struct connman_device *device;
1887 DBusMessageIter iter, array;
1888 const char *mode = "managed", *security = "none";
1889 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
1890 unsigned int ssid_len = 0;
1893 gboolean created = FALSE;
1896 dbus_message_iter_init(msg, &iter);
1897 dbus_message_iter_recurse(&iter, &array);
1899 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1900 DBusMessageIter entry, value;
1903 dbus_message_iter_recurse(&array, &entry);
1904 dbus_message_iter_get_basic(&entry, &key);
1906 dbus_message_iter_next(&entry);
1907 dbus_message_iter_recurse(&entry, &value);
1909 switch (dbus_message_iter_get_arg_type(&value)) {
1910 case DBUS_TYPE_STRING:
1911 if (g_str_equal(key, "Type") == TRUE)
1912 dbus_message_iter_get_basic(&value, &type);
1913 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
1914 g_str_equal(key, "Mode") == TRUE)
1915 dbus_message_iter_get_basic(&value, &mode);
1916 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
1917 g_str_equal(key, "Security") == TRUE)
1918 dbus_message_iter_get_basic(&value, &security);
1919 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
1920 g_str_equal(key, "Passphrase") == TRUE)
1921 dbus_message_iter_get_basic(&value, &passphrase);
1922 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
1923 g_str_equal(key, "SSID") == TRUE)
1924 dbus_message_iter_get_basic(&value, &ssid);
1927 dbus_message_iter_next(&array);
1933 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
1939 ssid_len = strlen(ssid);
1943 if (g_strcmp0(security, "none") != 0 &&
1944 g_strcmp0(security, "wep") != 0 &&
1945 g_strcmp0(security, "psk") != 0 &&
1946 g_strcmp0(security, "wpa") != 0 &&
1947 g_strcmp0(security, "rsn") != 0 &&
1948 g_strcmp0(security, "ieee8021x") != 0)
1951 device = __connman_element_find_device(CONNMAN_DEVICE_TYPE_WIFI);
1955 ident = __connman_device_get_ident(device);
1959 group = connman_wifi_build_group_name((unsigned char *) ssid,
1960 ssid_len, mode, security);
1964 name = g_strdup_printf("%s_%s_%s", type, ident, group);
1966 service = __connman_service_lookup(name);
1968 if (service != NULL)
1971 network = create_hidden_wifi(device, ssid, mode, security);
1972 if (network != NULL) {
1973 connman_network_set_group(network, group);
1977 service = __connman_service_lookup(name);
1983 if (service == NULL) {
1988 set_reconnect_state(service, FALSE);
1990 __connman_device_disconnect(device);
1992 if (passphrase != NULL) {
1993 g_free(service->passphrase);
1994 service->passphrase = g_strdup(passphrase);
1997 service->userconnect = TRUE;
1999 err = __connman_service_connect(service);
2000 if (err < 0 && err != -EINPROGRESS)
2003 g_dbus_send_reply(connection, msg,
2004 DBUS_TYPE_OBJECT_PATH, &service->path,
2010 if (service != NULL && created == TRUE) {
2011 struct connman_network *network = service->network;
2013 if (network != NULL) {
2014 connman_network_set_available(network, FALSE);
2015 __connman_device_cleanup_networks(device);
2018 __connman_service_put(service);
2025 * __connman_service_get:
2026 * @identifier: service identifier
2028 * Look up a service by identifier or create a new one if not found
2030 static struct connman_service *__connman_service_get(const char *identifier)
2032 struct connman_service *service;
2033 GSequenceIter *iter;
2035 iter = g_hash_table_lookup(service_hash, identifier);
2037 service = g_sequence_get(iter);
2038 if (service != NULL)
2039 g_atomic_int_inc(&service->refcount);
2043 service = connman_service_create();
2044 if (service == NULL)
2047 DBG("service %p", service);
2049 service->identifier = g_strdup(identifier);
2051 service->profile = g_strdup(__connman_profile_active_ident());
2053 iter = g_sequence_insert_sorted(service_list, service,
2054 service_compare, NULL);
2056 g_hash_table_insert(service_hash, service->identifier, iter);
2061 static int service_register(struct connman_service *service)
2063 const char *path = __connman_profile_active_path();
2064 GSequenceIter *iter;
2066 DBG("service %p", service);
2068 if (service->path != NULL)
2071 service->path = g_strdup_printf("%s/%s", path, service->identifier);
2073 DBG("path %s", service->path);
2075 __connman_config_provision_service(service);
2077 __connman_storage_load_service(service);
2079 g_dbus_register_interface(connection, service->path,
2080 CONNMAN_SERVICE_INTERFACE,
2081 service_methods, service_signals,
2082 NULL, service, NULL);
2084 iter = g_hash_table_lookup(service_hash, service->identifier);
2086 g_sequence_sort_changed(iter, service_compare, NULL);
2088 __connman_profile_changed(TRUE);
2093 static void service_up(struct connman_ipconfig *ipconfig)
2095 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
2098 static void service_down(struct connman_ipconfig *ipconfig)
2100 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
2103 static void service_lower_up(struct connman_ipconfig *ipconfig)
2105 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
2108 static void service_lower_down(struct connman_ipconfig *ipconfig)
2110 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
2113 static void service_ip_bound(struct connman_ipconfig *ipconfig)
2115 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
2117 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
2119 settings_changed(service);
2122 static void service_ip_release(struct connman_ipconfig *ipconfig)
2124 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
2126 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
2128 settings_changed(service);
2131 static const struct connman_ipconfig_ops service_ops = {
2133 .down = service_down,
2134 .lower_up = service_lower_up,
2135 .lower_down = service_lower_down,
2136 .ip_bound = service_ip_bound,
2137 .ip_release = service_ip_release,
2140 static void setup_ipconfig(struct connman_service *service, int index)
2145 service->ipconfig = connman_ipconfig_create(index);
2146 if (service->ipconfig == NULL)
2149 connman_ipconfig_set_method(service->ipconfig,
2150 CONNMAN_IPCONFIG_METHOD_DHCP);
2152 connman_ipconfig_set_data(service->ipconfig, service);
2154 connman_ipconfig_set_ops(service->ipconfig, &service_ops);
2158 * __connman_service_lookup_from_network:
2159 * @network: network structure
2161 * Look up a service by network (reference count will not be increased)
2163 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
2165 struct connman_service *service;
2166 const char *ident, *group;
2169 DBG("network %p", network);
2171 ident = __connman_network_get_ident(network);
2175 group = connman_network_get_group(network);
2179 name = g_strdup_printf("%s_%s_%s",
2180 __connman_network_get_type(network), ident, group);
2181 service = __connman_service_lookup(name);
2187 const char *__connman_service_get_path(struct connman_service *service)
2189 return service->path;
2192 unsigned int __connman_service_get_order(struct connman_service *service)
2194 GSequenceIter *iter;
2196 if (service == NULL)
2199 if (service->favorite == FALSE) {
2204 iter = g_hash_table_lookup(service_hash, service->identifier);
2206 if (g_sequence_iter_get_position(iter) == 0)
2213 return service->order;
2216 static enum connman_service_type convert_network_type(struct connman_network *network)
2218 enum connman_network_type type = connman_network_get_type(network);
2221 case CONNMAN_NETWORK_TYPE_UNKNOWN:
2222 case CONNMAN_NETWORK_TYPE_VENDOR:
2224 case CONNMAN_NETWORK_TYPE_ETHERNET:
2225 return CONNMAN_SERVICE_TYPE_ETHERNET;
2226 case CONNMAN_NETWORK_TYPE_WIFI:
2227 return CONNMAN_SERVICE_TYPE_WIFI;
2228 case CONNMAN_NETWORK_TYPE_WIMAX:
2229 return CONNMAN_SERVICE_TYPE_WIMAX;
2230 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
2231 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
2232 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
2233 case CONNMAN_NETWORK_TYPE_CELLULAR:
2234 case CONNMAN_NETWORK_TYPE_MBM:
2235 case CONNMAN_NETWORK_TYPE_HSO:
2236 return CONNMAN_SERVICE_TYPE_CELLULAR;
2239 return CONNMAN_SERVICE_TYPE_UNKNOWN;
2242 static enum connman_service_mode convert_wifi_mode(const char *mode)
2245 return CONNMAN_SERVICE_MODE_UNKNOWN;
2246 else if (g_str_equal(mode, "managed") == TRUE)
2247 return CONNMAN_SERVICE_MODE_MANAGED;
2248 else if (g_str_equal(mode, "adhoc") == TRUE)
2249 return CONNMAN_SERVICE_MODE_ADHOC;
2251 return CONNMAN_SERVICE_MODE_UNKNOWN;
2254 static enum connman_service_security convert_wifi_security(const char *security)
2256 if (security == NULL)
2257 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2258 else if (g_str_equal(security, "none") == TRUE)
2259 return CONNMAN_SERVICE_SECURITY_NONE;
2260 else if (g_str_equal(security, "wep") == TRUE)
2261 return CONNMAN_SERVICE_SECURITY_WEP;
2262 else if (g_str_equal(security, "wep") == TRUE)
2263 return CONNMAN_SERVICE_SECURITY_PSK;
2264 else if (g_str_equal(security, "ieee8021x") == TRUE)
2265 return CONNMAN_SERVICE_SECURITY_8021X;
2266 else if (g_str_equal(security, "wpa") == TRUE)
2267 return CONNMAN_SERVICE_SECURITY_WPA;
2268 else if (g_str_equal(security, "rsn") == TRUE)
2269 return CONNMAN_SERVICE_SECURITY_RSN;
2271 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2274 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
2279 return CONNMAN_SERVICE_MODE_GPRS;
2281 return CONNMAN_SERVICE_MODE_EDGE;
2286 return CONNMAN_SERVICE_MODE_UMTS;
2289 return CONNMAN_SERVICE_MODE_UNKNOWN;
2292 static void update_from_network(struct connman_service *service,
2293 struct connman_network *network)
2295 connman_uint8_t strength = service->strength;
2296 GSequenceIter *iter;
2299 DBG("service %p network %p", service, network);
2301 if (service->state == CONNMAN_SERVICE_STATE_READY)
2304 if (is_connecting(service) == TRUE)
2307 str = connman_network_get_string(network, "Name");
2309 g_free(service->name);
2310 service->name = g_strdup(str);
2311 service->hidden = FALSE;
2313 g_free(service->name);
2314 service->name = NULL;
2315 service->hidden = TRUE;
2318 service->strength = connman_network_get_uint8(network, "Strength");
2319 service->roaming = connman_network_get_bool(network, "Roaming");
2321 if (service->strength == 0) {
2323 * Filter out 0-values; it's unclear what they mean
2324 * and they cause anomalous sorting of the priority list.
2326 service->strength = strength;
2329 str = connman_network_get_string(network, "WiFi.Mode");
2330 service->mode = convert_wifi_mode(str);
2332 str = connman_network_get_string(network, "WiFi.Security");
2333 service->security = convert_wifi_security(str);
2335 str = connman_network_get_string(network, "Cellular.MCC");
2336 g_free(service->mcc);
2337 service->mcc = g_strdup(str);
2339 str = connman_network_get_string(network, "Cellular.MNC");
2340 g_free(service->mnc);
2341 service->mnc = g_strdup(str);
2343 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
2344 connman_uint8_t value = connman_network_get_uint8(network,
2347 service->mode = convert_cellular_mode(value);
2350 if (service->strength > strength && service->network != NULL) {
2351 connman_network_unref(service->network);
2352 service->network = connman_network_ref(network);
2354 strength_changed(service);
2357 if (service->network == NULL)
2358 service->network = connman_network_ref(network);
2360 iter = g_hash_table_lookup(service_hash, service->identifier);
2362 g_sequence_sort_changed(iter, service_compare, NULL);
2366 * __connman_service_create_from_network:
2367 * @network: network structure
2369 * Look up service by network and if not found, create one
2371 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
2373 struct connman_service *service;
2374 const char *ident, *group;
2377 DBG("network %p", network);
2379 ident = __connman_network_get_ident(network);
2383 group = connman_network_get_group(network);
2387 name = g_strdup_printf("%s_%s_%s",
2388 __connman_network_get_type(network), ident, group);
2389 service = __connman_service_get(name);
2392 if (service == NULL)
2395 if (__connman_network_get_weakness(network) == TRUE)
2398 if (service->path != NULL) {
2399 update_from_network(service, network);
2400 __connman_profile_changed(TRUE);
2404 service->type = convert_network_type(network);
2406 switch (service->type) {
2407 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2408 case CONNMAN_SERVICE_TYPE_SYSTEM:
2409 case CONNMAN_SERVICE_TYPE_ETHERNET:
2410 case CONNMAN_SERVICE_TYPE_WIMAX:
2411 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2412 case CONNMAN_SERVICE_TYPE_GPS:
2413 case CONNMAN_SERVICE_TYPE_VPN:
2414 service->autoconnect = FALSE;
2416 case CONNMAN_SERVICE_TYPE_WIFI:
2417 case CONNMAN_SERVICE_TYPE_CELLULAR:
2418 service->autoconnect = TRUE;
2422 service->state = CONNMAN_SERVICE_STATE_IDLE;
2424 update_from_network(service, network);
2426 setup_ipconfig(service, connman_network_get_index(network));
2428 service_register(service);
2430 if (service->favorite == TRUE)
2431 __connman_service_auto_connect();
2436 void __connman_service_update_from_network(struct connman_network *network)
2438 struct connman_service *service;
2439 enum connman_service_mode mode;
2440 connman_uint8_t strength, value;
2441 connman_bool_t roaming;
2442 GSequenceIter *iter;
2444 DBG("network %p", network);
2446 service = __connman_service_lookup_from_network(network);
2447 if (service == NULL)
2450 if (service->network == NULL)
2453 strength = connman_network_get_uint8(service->network, "Strength");
2454 if (strength == service->strength)
2457 service->strength = strength;
2459 strength_changed(service);
2462 roaming = connman_network_get_bool(service->network, "Roaming");
2463 if (roaming == service->roaming)
2466 service->roaming = roaming;
2468 roaming_changed(service);
2470 iter = g_hash_table_lookup(service_hash, service->identifier);
2472 g_sequence_sort_changed(iter, service_compare, NULL);
2475 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
2478 value = connman_network_get_uint8(service->network, "Cellular.Mode");
2479 mode = convert_cellular_mode(value);
2481 if (mode == service->mode)
2484 service->mode = mode;
2486 mode_changed(service);
2489 void __connman_service_remove_from_network(struct connman_network *network)
2491 struct connman_service *service;
2493 DBG("network %p", network);
2495 service = __connman_service_lookup_from_network(network);
2496 if (service == NULL)
2499 __connman_service_put(service);
2502 static int service_load(struct connman_service *service)
2504 const char *ident = service->profile;
2506 GError *error = NULL;
2507 gchar *pathname, *data = NULL;
2510 connman_bool_t autoconnect;
2511 unsigned int ssid_len;
2514 DBG("service %p", service);
2519 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2520 if (pathname == NULL)
2523 keyfile = g_key_file_new();
2525 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
2532 if (g_key_file_load_from_data(keyfile, data, length,
2533 0, NULL) == FALSE) {
2540 switch (service->type) {
2541 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2542 case CONNMAN_SERVICE_TYPE_SYSTEM:
2543 case CONNMAN_SERVICE_TYPE_ETHERNET:
2544 case CONNMAN_SERVICE_TYPE_GPS:
2545 case CONNMAN_SERVICE_TYPE_VPN:
2547 case CONNMAN_SERVICE_TYPE_WIFI:
2548 if (service->name == NULL) {
2551 name = g_key_file_get_string(keyfile,
2552 service->identifier, "Name", NULL);
2554 g_free(service->name);
2555 service->name = name;
2558 if (service->network != NULL)
2559 connman_network_set_name(service->network,
2563 if (service->network &&
2564 connman_network_get_blob(service->network,
2565 "WiFi.SSID", &ssid_len) == NULL) {
2568 hex_ssid = g_key_file_get_string(keyfile,
2569 service->identifier,
2572 if (hex_ssid != NULL) {
2574 unsigned int i, j = 0, hex;
2575 size_t hex_ssid_len = strlen(hex_ssid);
2577 ssid = g_try_malloc0(hex_ssid_len / 2);
2584 for (i = 0; i < hex_ssid_len; i += 2) {
2585 sscanf(hex_ssid + i, "%02x", &hex);
2589 connman_network_set_blob(service->network,
2590 "WiFi.SSID", ssid, hex_ssid_len / 2);
2597 case CONNMAN_SERVICE_TYPE_WIMAX:
2598 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2599 case CONNMAN_SERVICE_TYPE_CELLULAR:
2600 service->apn = g_key_file_get_string(keyfile,
2601 service->identifier, "APN", NULL);
2603 service->username = g_key_file_get_string(keyfile,
2604 service->identifier, "Username", NULL);
2606 service->username = g_key_file_get_string(keyfile,
2607 service->identifier, "Password", NULL);
2609 service->favorite = g_key_file_get_boolean(keyfile,
2610 service->identifier, "Favorite", NULL);
2612 autoconnect = g_key_file_get_boolean(keyfile,
2613 service->identifier, "AutoConnect", &error);
2615 service->autoconnect = autoconnect;
2616 g_clear_error(&error);
2618 str = g_key_file_get_string(keyfile,
2619 service->identifier, "Failure", NULL);
2621 service->state = CONNMAN_SERVICE_STATE_FAILURE;
2622 service->error = string2error(str);
2627 str = g_key_file_get_string(keyfile,
2628 service->identifier, "Modified", NULL);
2630 g_time_val_from_iso8601(str, &service->modified);
2634 str = g_key_file_get_string(keyfile,
2635 service->identifier, "Passphrase", NULL);
2637 g_free(service->passphrase);
2638 service->passphrase = str;
2641 if (service->ipconfig != NULL)
2642 __connman_ipconfig_load(service->ipconfig, keyfile,
2643 service->identifier, "IPv4.");
2646 g_key_file_free(keyfile);
2651 static int service_save(struct connman_service *service)
2653 const char *ident = service->profile;
2655 gchar *pathname, *data = NULL;
2660 DBG("service %p", service);
2665 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2666 if (pathname == NULL)
2669 keyfile = g_key_file_new();
2671 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
2675 if (g_key_file_load_from_data(keyfile, data, length,
2683 if (service->name != NULL)
2684 g_key_file_set_string(keyfile, service->identifier,
2685 "Name", service->name);
2687 switch (service->type) {
2688 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2689 case CONNMAN_SERVICE_TYPE_SYSTEM:
2690 case CONNMAN_SERVICE_TYPE_ETHERNET:
2691 case CONNMAN_SERVICE_TYPE_GPS:
2692 case CONNMAN_SERVICE_TYPE_VPN:
2694 case CONNMAN_SERVICE_TYPE_WIFI:
2695 if (service->network) {
2696 const unsigned char *ssid;
2697 unsigned int ssid_len = 0;
2699 ssid = connman_network_get_blob(service->network,
2700 "WiFi.SSID", &ssid_len);
2702 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
2703 char *identifier = service->identifier;
2707 str = g_string_sized_new(ssid_len * 2);
2713 for (i = 0; i < ssid_len; i++)
2714 g_string_append_printf(str,
2717 g_key_file_set_string(keyfile, identifier,
2720 g_string_free(str, TRUE);
2725 case CONNMAN_SERVICE_TYPE_WIMAX:
2726 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2727 case CONNMAN_SERVICE_TYPE_CELLULAR:
2728 if (service->apn != NULL)
2729 g_key_file_set_string(keyfile, service->identifier,
2730 "APN", service->apn);
2732 if (service->username != NULL)
2733 g_key_file_set_string(keyfile, service->identifier,
2734 "Username", service->username);
2736 if (service->password != NULL)
2737 g_key_file_set_string(keyfile, service->identifier,
2738 "Password", service->password);
2740 g_key_file_set_boolean(keyfile, service->identifier,
2741 "Favorite", service->favorite);
2743 if (service->favorite == TRUE)
2744 g_key_file_set_boolean(keyfile, service->identifier,
2745 "AutoConnect", service->autoconnect);
2747 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
2748 const char *failure = error2string(service->error);
2749 if (failure != NULL)
2750 g_key_file_set_string(keyfile,
2751 service->identifier,
2752 "Failure", failure);
2754 g_key_file_remove_key(keyfile, service->identifier,
2760 str = g_time_val_to_iso8601(&service->modified);
2762 g_key_file_set_string(keyfile, service->identifier,
2767 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
2768 g_key_file_set_string(keyfile, service->identifier,
2769 "Passphrase", service->passphrase);
2771 g_key_file_remove_key(keyfile, service->identifier,
2772 "Passphrase", NULL);
2774 if (service->ipconfig != NULL)
2775 __connman_ipconfig_save(service->ipconfig, keyfile,
2776 service->identifier, "IPv4.");
2778 data = g_key_file_to_data(keyfile, &length, NULL);
2780 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
2781 connman_error("Failed to store service information");
2786 g_key_file_free(keyfile);
2793 static struct connman_storage service_storage = {
2795 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
2796 .service_load = service_load,
2797 .service_save = service_save,
2800 int __connman_service_init(void)
2804 connection = connman_dbus_get_connection();
2806 if (connman_storage_register(&service_storage) < 0)
2807 connman_error("Failed to register service storage");
2809 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2812 service_list = g_sequence_new(service_free);
2817 void __connman_service_cleanup(void)
2821 g_sequence_free(service_list);
2822 service_list = NULL;
2824 g_hash_table_destroy(service_hash);
2825 service_hash = NULL;
2827 connman_storage_unregister(&service_storage);
2829 dbus_connection_unref(connection);