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_VPN:
149 static const char *mode2string(enum connman_service_mode mode)
152 case CONNMAN_SERVICE_MODE_UNKNOWN:
154 case CONNMAN_SERVICE_MODE_MANAGED:
156 case CONNMAN_SERVICE_MODE_ADHOC:
158 case CONNMAN_SERVICE_MODE_GPRS:
160 case CONNMAN_SERVICE_MODE_EDGE:
162 case CONNMAN_SERVICE_MODE_UMTS:
169 static const char *security2string(enum connman_service_security security)
172 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
174 case CONNMAN_SERVICE_SECURITY_NONE:
176 case CONNMAN_SERVICE_SECURITY_WEP:
178 case CONNMAN_SERVICE_SECURITY_PSK:
180 case CONNMAN_SERVICE_SECURITY_8021X:
182 case CONNMAN_SERVICE_SECURITY_WPA:
184 case CONNMAN_SERVICE_SECURITY_RSN:
191 static const char *state2string(enum connman_service_state state)
194 case CONNMAN_SERVICE_STATE_UNKNOWN:
196 case CONNMAN_SERVICE_STATE_IDLE:
198 case CONNMAN_SERVICE_STATE_ASSOCIATION:
199 return "association";
200 case CONNMAN_SERVICE_STATE_CONFIGURATION:
201 return "configuration";
202 case CONNMAN_SERVICE_STATE_READY:
204 case CONNMAN_SERVICE_STATE_DISCONNECT:
206 case CONNMAN_SERVICE_STATE_FAILURE:
213 static const char *error2string(enum connman_service_error error)
216 case CONNMAN_SERVICE_ERROR_UNKNOWN:
218 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
219 return "out-of-range";
220 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
221 return "pin-missing";
222 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
223 return "dhcp-failed";
224 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
225 return "connect-failed";
231 static enum connman_service_error string2error(const char *error)
233 if (g_strcmp0(error, "dhcp-failed") == 0)
234 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
235 else if (g_strcmp0(error, "pin-missing") == 0)
236 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
238 return CONNMAN_SERVICE_ERROR_UNKNOWN;
241 static struct connman_service *get_default(void)
243 struct connman_service *service;
246 iter = g_sequence_get_begin_iter(service_list);
248 if (g_sequence_iter_is_end(iter) == TRUE)
251 service = g_sequence_get(iter);
253 if (service->state != CONNMAN_SERVICE_STATE_READY)
259 static void default_changed(void)
261 struct connman_service *service = get_default();
263 __connman_notifier_default_changed(service);
266 const char *__connman_service_default(void)
268 struct connman_service *service;
270 service = get_default();
274 return __connman_service_type2string(service->type);
277 static void mode_changed(struct connman_service *service)
281 str = mode2string(service->mode);
285 connman_dbus_property_changed_basic(service->path,
286 CONNMAN_SERVICE_INTERFACE, "Mode",
287 DBUS_TYPE_STRING, &str);
290 static void state_changed(struct connman_service *service)
294 str = state2string(service->state);
298 connman_dbus_property_changed_basic(service->path,
299 CONNMAN_SERVICE_INTERFACE, "State",
300 DBUS_TYPE_STRING, &str);
303 static void strength_changed(struct connman_service *service)
305 if (service->strength == 0)
308 connman_dbus_property_changed_basic(service->path,
309 CONNMAN_SERVICE_INTERFACE, "Strength",
310 DBUS_TYPE_BYTE, &service->strength);
313 static void favorite_changed(struct connman_service *service)
315 connman_dbus_property_changed_basic(service->path,
316 CONNMAN_SERVICE_INTERFACE, "Favorite",
317 DBUS_TYPE_BOOLEAN, &service->favorite);
320 static void roaming_changed(struct connman_service *service)
322 connman_dbus_property_changed_basic(service->path,
323 CONNMAN_SERVICE_INTERFACE, "Roaming",
324 DBUS_TYPE_BOOLEAN, &service->roaming);
327 static void autoconnect_changed(struct connman_service *service)
329 connman_dbus_property_changed_basic(service->path,
330 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
331 DBUS_TYPE_BOOLEAN, &service->autoconnect);
334 static void passphrase_changed(struct connman_service *service)
336 dbus_bool_t required;
338 switch (service->type) {
339 case CONNMAN_SERVICE_TYPE_UNKNOWN:
340 case CONNMAN_SERVICE_TYPE_SYSTEM:
341 case CONNMAN_SERVICE_TYPE_ETHERNET:
342 case CONNMAN_SERVICE_TYPE_WIMAX:
343 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
344 case CONNMAN_SERVICE_TYPE_CELLULAR:
345 case CONNMAN_SERVICE_TYPE_VPN:
347 case CONNMAN_SERVICE_TYPE_WIFI:
350 switch (service->security) {
351 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
352 case CONNMAN_SERVICE_SECURITY_NONE:
354 case CONNMAN_SERVICE_SECURITY_WEP:
355 case CONNMAN_SERVICE_SECURITY_PSK:
356 case CONNMAN_SERVICE_SECURITY_WPA:
357 case CONNMAN_SERVICE_SECURITY_RSN:
358 case CONNMAN_SERVICE_SECURITY_8021X:
359 if (service->passphrase == NULL)
366 connman_dbus_property_changed_basic(service->path,
367 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
368 DBUS_TYPE_BOOLEAN, &required);
371 static void apn_changed(struct connman_service *service)
373 dbus_bool_t required;
375 switch (service->type) {
376 case CONNMAN_SERVICE_TYPE_UNKNOWN:
377 case CONNMAN_SERVICE_TYPE_SYSTEM:
378 case CONNMAN_SERVICE_TYPE_ETHERNET:
379 case CONNMAN_SERVICE_TYPE_WIMAX:
380 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
381 case CONNMAN_SERVICE_TYPE_WIFI:
382 case CONNMAN_SERVICE_TYPE_VPN:
384 case CONNMAN_SERVICE_TYPE_CELLULAR:
388 required = (service->apn == NULL) ? TRUE : FALSE;
390 connman_dbus_property_changed_basic(service->path,
391 CONNMAN_SERVICE_INTERFACE, "SetupRequired",
392 DBUS_TYPE_BOOLEAN, &required);
395 static void append_ethernet(DBusMessageIter *iter, void *user_data)
397 struct connman_service *service = user_data;
399 switch (service->state) {
400 case CONNMAN_SERVICE_STATE_UNKNOWN:
401 case CONNMAN_SERVICE_STATE_IDLE:
402 case CONNMAN_SERVICE_STATE_FAILURE:
403 case CONNMAN_SERVICE_STATE_DISCONNECT:
405 case CONNMAN_SERVICE_STATE_ASSOCIATION:
406 case CONNMAN_SERVICE_STATE_CONFIGURATION:
407 case CONNMAN_SERVICE_STATE_READY:
411 if (service->ipconfig != NULL)
412 __connman_ipconfig_append_ethernet(service->ipconfig, iter);
415 static void append_ipv4(DBusMessageIter *iter, void *user_data)
417 struct connman_service *service = user_data;
419 switch (service->state) {
420 case CONNMAN_SERVICE_STATE_UNKNOWN:
421 case CONNMAN_SERVICE_STATE_IDLE:
422 case CONNMAN_SERVICE_STATE_FAILURE:
423 case CONNMAN_SERVICE_STATE_DISCONNECT:
424 case CONNMAN_SERVICE_STATE_ASSOCIATION:
425 case CONNMAN_SERVICE_STATE_CONFIGURATION:
427 case CONNMAN_SERVICE_STATE_READY:
431 if (service->ipconfig != NULL)
432 __connman_ipconfig_append_ipv4(service->ipconfig, iter);
435 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
437 struct connman_service *service = user_data;
439 if (service->ipconfig != NULL)
440 __connman_ipconfig_append_ipv4config(service->ipconfig, iter);
443 static void append_proxy(DBusMessageIter *iter, void *user_data)
445 struct connman_service *service = user_data;
447 switch (service->state) {
448 case CONNMAN_SERVICE_STATE_UNKNOWN:
449 case CONNMAN_SERVICE_STATE_IDLE:
450 case CONNMAN_SERVICE_STATE_FAILURE:
451 case CONNMAN_SERVICE_STATE_DISCONNECT:
452 case CONNMAN_SERVICE_STATE_ASSOCIATION:
453 case CONNMAN_SERVICE_STATE_CONFIGURATION:
455 case CONNMAN_SERVICE_STATE_READY:
459 if (service->ipconfig != NULL)
460 __connman_ipconfig_append_proxy(service->ipconfig, iter);
463 static void settings_changed(struct connman_service *service)
465 connman_dbus_property_changed_dict(service->path,
466 CONNMAN_SERVICE_INTERFACE, "IPv4",
467 append_ipv4, service);
470 static void ipv4_configuration_changed(struct connman_service *service)
472 connman_dbus_property_changed_dict(service->path,
473 CONNMAN_SERVICE_INTERFACE,
474 "IPv4.Configuration",
479 static DBusMessage *get_properties(DBusConnection *conn,
480 DBusMessage *msg, void *user_data)
482 struct connman_service *service = user_data;
484 DBusMessageIter array, dict;
485 dbus_bool_t required;
488 DBG("service %p", service);
490 reply = dbus_message_new_method_return(msg);
494 dbus_message_iter_init_append(reply, &array);
496 connman_dbus_dict_open(&array, &dict);
498 str = __connman_service_type2string(service->type);
500 connman_dbus_dict_append_basic(&dict, "Type",
501 DBUS_TYPE_STRING, &str);
503 str = mode2string(service->mode);
505 connman_dbus_dict_append_basic(&dict, "Mode",
506 DBUS_TYPE_STRING, &str);
508 str = security2string(service->security);
510 connman_dbus_dict_append_basic(&dict, "Security",
511 DBUS_TYPE_STRING, &str);
513 str = state2string(service->state);
515 connman_dbus_dict_append_basic(&dict, "State",
516 DBUS_TYPE_STRING, &str);
518 str = error2string(service->error);
520 connman_dbus_dict_append_basic(&dict, "Error",
521 DBUS_TYPE_STRING, &str);
523 if (service->strength > 0)
524 connman_dbus_dict_append_basic(&dict, "Strength",
525 DBUS_TYPE_BYTE, &service->strength);
527 connman_dbus_dict_append_basic(&dict, "Favorite",
528 DBUS_TYPE_BOOLEAN, &service->favorite);
530 connman_dbus_dict_append_basic(&dict, "Immutable",
531 DBUS_TYPE_BOOLEAN, &service->immutable);
533 if (service->favorite == TRUE)
534 connman_dbus_dict_append_basic(&dict, "AutoConnect",
535 DBUS_TYPE_BOOLEAN, &service->autoconnect);
537 connman_dbus_dict_append_basic(&dict, "AutoConnect",
538 DBUS_TYPE_BOOLEAN, &service->favorite);
540 if (service->name != NULL)
541 connman_dbus_dict_append_basic(&dict, "Name",
542 DBUS_TYPE_STRING, &service->name);
544 switch (service->type) {
545 case CONNMAN_SERVICE_TYPE_UNKNOWN:
546 case CONNMAN_SERVICE_TYPE_SYSTEM:
547 case CONNMAN_SERVICE_TYPE_WIMAX:
548 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
549 case CONNMAN_SERVICE_TYPE_VPN:
551 case CONNMAN_SERVICE_TYPE_CELLULAR:
552 connman_dbus_dict_append_basic(&dict, "Roaming",
553 DBUS_TYPE_BOOLEAN, &service->roaming);
555 if (service->mcc != NULL && service->mnc != NULL) {
556 connman_dbus_dict_append_basic(&dict, "MCC",
557 DBUS_TYPE_STRING, &service->mcc);
558 connman_dbus_dict_append_basic(&dict, "MNC",
559 DBUS_TYPE_STRING, &service->mnc);
562 if (service->apn != NULL) {
563 connman_dbus_dict_append_basic(&dict, "APN",
564 DBUS_TYPE_STRING, &service->apn);
566 if (service->username != NULL)
567 connman_dbus_dict_append_basic(&dict,
568 "Username", DBUS_TYPE_STRING,
571 if (service->password != NULL)
572 connman_dbus_dict_append_basic(&dict,
573 "Password", DBUS_TYPE_STRING,
580 connman_dbus_dict_append_basic(&dict, "SetupRequired",
581 DBUS_TYPE_BOOLEAN, &required);
583 case CONNMAN_SERVICE_TYPE_WIFI:
584 if (service->passphrase != NULL &&
585 __connman_security_check_privilege(msg,
586 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
587 connman_dbus_dict_append_basic(&dict, "Passphrase",
588 DBUS_TYPE_STRING, &service->passphrase);
592 switch (service->security) {
593 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
594 case CONNMAN_SERVICE_SECURITY_NONE:
596 case CONNMAN_SERVICE_SECURITY_WEP:
597 case CONNMAN_SERVICE_SECURITY_PSK:
598 case CONNMAN_SERVICE_SECURITY_WPA:
599 case CONNMAN_SERVICE_SECURITY_RSN:
600 case CONNMAN_SERVICE_SECURITY_8021X:
601 if (service->passphrase == NULL)
606 connman_dbus_dict_append_basic(&dict, "PassphraseRequired",
607 DBUS_TYPE_BOOLEAN, &required);
609 case CONNMAN_SERVICE_TYPE_ETHERNET:
610 connman_dbus_dict_append_dict(&dict, "Ethernet",
611 append_ethernet, service);
615 connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4, service);
617 connman_dbus_dict_append_dict(&dict, "IPv4.Configuration",
618 append_ipv4config, service);
620 connman_dbus_dict_append_dict(&dict, "Proxy", append_proxy, service);
622 connman_dbus_dict_close(&array, &dict);
627 static DBusMessage *set_property(DBusConnection *conn,
628 DBusMessage *msg, void *user_data)
630 struct connman_service *service = user_data;
631 DBusMessageIter iter, value;
635 DBG("service %p", service);
637 if (dbus_message_iter_init(msg, &iter) == FALSE)
638 return __connman_error_invalid_arguments(msg);
640 dbus_message_iter_get_basic(&iter, &name);
641 dbus_message_iter_next(&iter);
642 dbus_message_iter_recurse(&iter, &value);
644 if (__connman_security_check_privilege(msg,
645 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
646 return __connman_error_permission_denied(msg);
648 type = dbus_message_iter_get_arg_type(&value);
650 if (g_str_has_prefix(name, "AutoConnect") == TRUE) {
651 connman_bool_t autoconnect;
653 if (type != DBUS_TYPE_BOOLEAN)
654 return __connman_error_invalid_arguments(msg);
656 if (service->favorite == FALSE)
657 return __connman_error_invalid_service(msg);
659 dbus_message_iter_get_basic(&value, &autoconnect);
661 if (service->autoconnect == autoconnect)
662 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
664 service->autoconnect = autoconnect;
666 autoconnect_changed(service);
668 __connman_storage_save_service(service);
669 } else if (g_str_equal(name, "Passphrase") == TRUE) {
670 const char *passphrase;
672 if (type != DBUS_TYPE_STRING)
673 return __connman_error_invalid_arguments(msg);
675 if (__connman_security_check_privilege(msg,
676 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
677 return __connman_error_permission_denied(msg);
679 dbus_message_iter_get_basic(&value, &passphrase);
681 g_free(service->passphrase);
682 service->passphrase = g_strdup(passphrase);
684 passphrase_changed(service);
686 if (service->network != NULL)
687 connman_network_set_string(service->network,
688 "WiFi.Passphrase", service->passphrase);
690 __connman_storage_save_service(service);
691 } else if (g_str_equal(name, "APN") == TRUE) {
694 if (type != DBUS_TYPE_STRING)
695 return __connman_error_invalid_arguments(msg);
697 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
698 return __connman_error_invalid_service(msg);
700 dbus_message_iter_get_basic(&value, &apn);
702 g_free(service->apn);
703 service->apn = g_strdup(apn);
705 apn_changed(service);
707 if (service->network != NULL)
708 connman_network_set_string(service->network,
709 "Cellular.APN", service->apn);
711 __connman_storage_save_service(service);
712 } else if (g_str_equal(name, "Username") == TRUE) {
713 const char *username;
715 if (type != DBUS_TYPE_STRING)
716 return __connman_error_invalid_arguments(msg);
718 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
719 return __connman_error_invalid_service(msg);
721 dbus_message_iter_get_basic(&value, &username);
723 g_free(service->username);
724 service->username = g_strdup(username);
726 if (service->network != NULL)
727 connman_network_set_string(service->network,
728 "Cellular.Username", service->username);
730 __connman_storage_save_service(service);
731 } else if (g_str_equal(name, "Password") == TRUE) {
732 const char *password;
734 if (type != DBUS_TYPE_STRING)
735 return __connman_error_invalid_arguments(msg);
737 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
738 return __connman_error_invalid_service(msg);
740 dbus_message_iter_get_basic(&value, &password);
742 g_free(service->password);
743 service->password = g_strdup(password);
745 if (service->network != NULL)
746 connman_network_set_string(service->network,
747 "Cellular.Password", service->password);
749 __connman_storage_save_service(service);
750 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
753 if (service->ipconfig == NULL)
754 return __connman_error_invalid_property(msg);
756 err = __connman_ipconfig_set_ipv4config(service->ipconfig,
759 return __connman_error_failed(msg, -err);
761 ipv4_configuration_changed(service);
763 __connman_storage_save_service(service);
765 return __connman_error_invalid_property(msg);
767 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
770 static void set_idle(struct connman_service *service)
772 service->state = CONNMAN_SERVICE_STATE_IDLE;
773 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
774 state_changed(service);
777 static DBusMessage *clear_property(DBusConnection *conn,
778 DBusMessage *msg, void *user_data)
780 struct connman_service *service = user_data;
783 DBG("service %p", service);
785 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
788 if (__connman_security_check_privilege(msg,
789 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
790 return __connman_error_permission_denied(msg);
792 if (g_str_equal(name, "Error") == TRUE) {
795 g_get_current_time(&service->modified);
796 __connman_storage_save_service(service);
797 } else if (g_str_equal(name, "Passphrase") == TRUE) {
798 g_free(service->passphrase);
799 service->passphrase = NULL;
801 passphrase_changed(service);
803 __connman_storage_save_service(service);
805 return __connman_error_invalid_property(msg);
807 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
810 static connman_bool_t is_connecting(struct connman_service *service)
812 switch (service->state) {
813 case CONNMAN_SERVICE_STATE_UNKNOWN:
814 case CONNMAN_SERVICE_STATE_IDLE:
815 case CONNMAN_SERVICE_STATE_FAILURE:
816 case CONNMAN_SERVICE_STATE_DISCONNECT:
817 case CONNMAN_SERVICE_STATE_READY:
819 case CONNMAN_SERVICE_STATE_ASSOCIATION:
820 case CONNMAN_SERVICE_STATE_CONFIGURATION:
827 static connman_bool_t is_ignore(struct connman_service *service)
829 if (service->autoconnect == FALSE)
832 if (service->roaming == TRUE)
835 if (service->ignore == TRUE)
838 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
844 void __connman_service_auto_connect(void)
846 struct connman_service *service = NULL;
851 iter = g_sequence_get_begin_iter(service_list);
853 while (g_sequence_iter_is_end(iter) == FALSE) {
854 service = g_sequence_get(iter);
856 if (service->pending != NULL)
859 if (is_connecting(service) == TRUE)
862 if (service->favorite == FALSE)
865 if (service->state == CONNMAN_SERVICE_STATE_READY)
868 if (is_ignore(service) == FALSE &&
869 service->state == CONNMAN_SERVICE_STATE_IDLE)
874 iter = g_sequence_iter_next(iter);
877 if (service != NULL) {
878 service->userconnect = FALSE;
879 __connman_service_connect(service);
883 static void remove_timeout(struct connman_service *service)
885 if (service->timeout > 0) {
886 g_source_remove(service->timeout);
887 service->timeout = 0;
891 static void reply_pending(struct connman_service *service, int error)
893 remove_timeout(service);
895 if (service->pending != NULL) {
899 reply = __connman_error_failed(service->pending,
902 g_dbus_send_message(connection, reply);
904 g_dbus_send_reply(connection, service->pending,
907 dbus_message_unref(service->pending);
908 service->pending = NULL;
912 static gboolean connect_timeout(gpointer user_data)
914 struct connman_service *service = user_data;
915 connman_bool_t autoconnect = FALSE;
917 DBG("service %p", service);
919 service->timeout = 0;
921 if (service->network != NULL)
922 __connman_network_disconnect(service->network);
924 __connman_ipconfig_disable(service->ipconfig);
926 if (service->pending != NULL) {
929 reply = __connman_error_operation_timeout(service->pending);
931 g_dbus_send_message(connection, reply);
933 dbus_message_unref(service->pending);
934 service->pending = NULL;
938 __connman_service_indicate_state(service,
939 CONNMAN_SERVICE_STATE_FAILURE);
941 if (autoconnect == TRUE && service->userconnect == FALSE)
942 __connman_service_auto_connect();
947 static void set_reconnect_state(struct connman_service *service,
948 connman_bool_t reconnect)
950 struct connman_device *device;
952 if (service->network == NULL)
955 device = connman_network_get_device(service->network);
959 __connman_device_set_reconnect(device, reconnect);
962 static connman_bool_t get_reconnect_state(struct connman_service *service)
964 struct connman_device *device;
966 if (service->network == NULL)
969 device = connman_network_get_device(service->network);
973 return __connman_device_get_reconnect(device);
976 static DBusMessage *connect_service(DBusConnection *conn,
977 DBusMessage *msg, void *user_data)
979 struct connman_service *service = user_data;
983 DBG("service %p", service);
985 if (service->pending != NULL)
986 return __connman_error_in_progress(msg);
988 iter = g_sequence_get_begin_iter(service_list);
990 while (g_sequence_iter_is_end(iter) == FALSE) {
991 struct connman_service *temp = g_sequence_get(iter);
993 if (service->type == temp->type &&
994 is_connecting(temp) == TRUE)
995 return __connman_error_in_progress(msg);
997 iter = g_sequence_iter_next(iter);
1000 service->ignore = FALSE;
1002 service->userconnect = TRUE;
1004 service->pending = dbus_message_ref(msg);
1006 set_reconnect_state(service, FALSE);
1008 err = __connman_service_connect(service);
1010 if (err == -ENOKEY) {
1011 if (__connman_agent_request_passphrase(service,
1016 if (err != -EINPROGRESS) {
1017 dbus_message_unref(service->pending);
1018 service->pending = NULL;
1020 return __connman_error_failed(msg, -err);
1026 dbus_message_unref(service->pending);
1027 service->pending = NULL;
1029 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1032 static DBusMessage *disconnect_service(DBusConnection *conn,
1033 DBusMessage *msg, void *user_data)
1035 struct connman_service *service = user_data;
1038 DBG("service %p", service);
1040 reply_pending(service, ECONNABORTED);
1042 service->ignore = TRUE;
1044 set_reconnect_state(service, FALSE);
1046 err = __connman_service_disconnect(service);
1048 if (err != -EINPROGRESS)
1049 return __connman_error_failed(msg, -err);
1054 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1057 static DBusMessage *remove_service(DBusConnection *conn,
1058 DBusMessage *msg, void *user_data)
1060 struct connman_service *service = user_data;
1062 DBG("service %p", service);
1064 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
1065 return __connman_error_not_supported(msg);
1067 if (service->favorite == FALSE &&
1068 service->state != CONNMAN_SERVICE_STATE_FAILURE)
1069 return __connman_error_not_supported(msg);
1071 if (service->network != NULL) {
1072 set_reconnect_state(service, FALSE);
1074 __connman_network_disconnect(service->network);
1077 g_free(service->passphrase);
1078 service->passphrase = NULL;
1080 passphrase_changed(service);
1082 g_free(service->apn);
1083 service->apn = NULL;
1085 g_free(service->username);
1086 service->username = NULL;
1088 g_free(service->password);
1089 service->password = NULL;
1091 apn_changed(service);
1095 connman_service_set_favorite(service, FALSE);
1096 __connman_storage_save_service(service);
1098 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1101 static DBusMessage *move_before(DBusConnection *conn,
1102 DBusMessage *msg, void *user_data)
1104 struct connman_service *service = user_data;
1105 struct connman_service *target;
1107 GSequenceIter *src, *dst;
1109 DBG("service %p", service);
1111 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1114 if (service->favorite == FALSE)
1115 return __connman_error_not_supported(msg);
1117 target = find_service(path);
1118 if (target == NULL || target->favorite == FALSE || target == service)
1119 return __connman_error_invalid_service(msg);
1121 DBG("target %s", target->identifier);
1123 if (target->state != service->state)
1124 return __connman_error_invalid_service(msg);
1126 g_get_current_time(&service->modified);
1127 __connman_storage_save_service(service);
1129 src = g_hash_table_lookup(service_hash, service->identifier);
1130 dst = g_hash_table_lookup(service_hash, target->identifier);
1132 g_sequence_move(src, dst);
1134 __connman_profile_changed(FALSE);
1136 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1139 static DBusMessage *move_after(DBusConnection *conn,
1140 DBusMessage *msg, void *user_data)
1142 struct connman_service *service = user_data;
1143 struct connman_service *target;
1146 DBG("service %p", service);
1148 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1151 if (service->favorite == FALSE)
1152 return __connman_error_not_supported(msg);
1154 target = find_service(path);
1155 if (target == NULL || target->favorite == FALSE || target == service)
1156 return __connman_error_invalid_service(msg);
1158 DBG("target %s", target->identifier);
1160 if (target->state != service->state)
1161 return __connman_error_invalid_service(msg);
1163 g_get_current_time(&service->modified);
1164 __connman_storage_save_service(service);
1166 return __connman_error_not_implemented(msg);
1169 static GDBusMethodTable service_methods[] = {
1170 { "GetProperties", "", "a{sv}", get_properties },
1171 { "SetProperty", "sv", "", set_property },
1172 { "ClearProperty", "s", "", clear_property },
1173 { "Connect", "", "", connect_service,
1174 G_DBUS_METHOD_FLAG_ASYNC },
1175 { "Disconnect", "", "", disconnect_service },
1176 { "Remove", "", "", remove_service },
1177 { "MoveBefore", "o", "", move_before },
1178 { "MoveAfter", "o", "", move_after },
1182 static GDBusSignalTable service_signals[] = {
1183 { "PropertyChanged", "sv" },
1187 static void service_free(gpointer user_data)
1189 struct connman_service *service = user_data;
1190 char *path = service->path;
1192 DBG("service %p", service);
1194 reply_pending(service, ENOENT);
1196 g_hash_table_remove(service_hash, service->identifier);
1198 service->path = NULL;
1201 __connman_profile_changed(FALSE);
1203 g_dbus_unregister_interface(connection, path,
1204 CONNMAN_SERVICE_INTERFACE);
1208 if (service->network != NULL)
1209 connman_network_unref(service->network);
1211 if (service->ipconfig != NULL) {
1212 connman_ipconfig_unref(service->ipconfig);
1213 service->ipconfig = NULL;
1216 g_free(service->mcc);
1217 g_free(service->mnc);
1218 g_free(service->apn);
1219 g_free(service->username);
1220 g_free(service->password);
1221 g_free(service->profile);
1222 g_free(service->name);
1223 g_free(service->passphrase);
1224 g_free(service->identifier);
1225 g_free(service->eap);
1226 g_free(service->identity);
1227 g_free(service->ca_cert_file);
1228 g_free(service->client_cert_file);
1229 g_free(service->private_key_file);
1230 g_free(service->private_key_passphrase);
1231 g_free(service->phase2);
1236 * __connman_service_put:
1237 * @service: service structure
1239 * Release service if no longer needed
1241 void __connman_service_put(struct connman_service *service)
1243 DBG("service %p", service);
1245 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
1246 GSequenceIter *iter;
1248 iter = g_hash_table_lookup(service_hash, service->identifier);
1250 reply_pending(service, ECONNABORTED);
1252 __connman_service_disconnect(service);
1254 g_sequence_remove(iter);
1256 service_free(service);
1260 static void __connman_service_initialize(struct connman_service *service)
1262 DBG("service %p", service);
1264 service->refcount = 1;
1266 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
1267 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
1268 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
1269 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
1271 service->favorite = FALSE;
1272 service->immutable = FALSE;
1273 service->hidden = FALSE;
1275 service->ignore = FALSE;
1277 service->userconnect = FALSE;
1283 * connman_service_create:
1285 * Allocate a new service.
1287 * Returns: a newly-allocated #connman_service structure
1289 struct connman_service *connman_service_create(void)
1291 struct connman_service *service;
1293 service = g_try_new0(struct connman_service, 1);
1294 if (service == NULL)
1297 DBG("service %p", service);
1299 __connman_service_initialize(service);
1305 * connman_service_ref:
1306 * @service: service structure
1308 * Increase reference counter of service
1310 struct connman_service *connman_service_ref(struct connman_service *service)
1312 g_atomic_int_inc(&service->refcount);
1318 * connman_service_unref:
1319 * @service: service structure
1321 * Decrease reference counter of service
1323 void connman_service_unref(struct connman_service *service)
1325 __connman_service_put(service);
1328 static gint service_compare(gconstpointer a, gconstpointer b,
1331 struct connman_service *service_a = (void *) a;
1332 struct connman_service *service_b = (void *) b;
1334 if (service_a->state != service_b->state) {
1335 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
1337 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
1341 if (service_a->order > service_b->order)
1344 if (service_a->order < service_b->order)
1347 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
1350 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
1353 if (service_a->type != service_b->type) {
1354 switch (service_a->type) {
1355 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1356 case CONNMAN_SERVICE_TYPE_SYSTEM:
1357 case CONNMAN_SERVICE_TYPE_ETHERNET:
1358 case CONNMAN_SERVICE_TYPE_VPN:
1360 case CONNMAN_SERVICE_TYPE_WIFI:
1362 case CONNMAN_SERVICE_TYPE_WIMAX:
1363 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1364 case CONNMAN_SERVICE_TYPE_CELLULAR:
1369 return (gint) service_b->strength - (gint) service_a->strength;
1373 * connman_service_get_type:
1374 * @service: service structure
1376 * Get the type of service
1378 enum connman_service_type connman_service_get_type(struct connman_service *service)
1380 if (service == NULL)
1381 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1383 return service->type;
1387 * connman_service_get_interface:
1388 * @service: service structure
1390 * Get network interface of service
1392 char *connman_service_get_interface(struct connman_service *service)
1396 if (service == NULL)
1399 if (service->network == NULL)
1402 index = connman_network_get_index(service->network);
1404 return connman_inet_ifname(index);
1408 * connman_service_get_network:
1409 * @service: service structure
1411 * Get the service network
1413 struct connman_network *
1414 __connman_service_get_network(struct connman_service *service)
1416 if (service == NULL)
1419 return service->network;
1423 * connman_service_set_favorite:
1424 * @service: service structure
1425 * @favorite: favorite value
1427 * Change the favorite setting of service
1429 int connman_service_set_favorite(struct connman_service *service,
1430 connman_bool_t favorite)
1432 GSequenceIter *iter;
1434 iter = g_hash_table_lookup(service_hash, service->identifier);
1438 if (service->favorite == favorite)
1441 service->favorite = favorite;
1443 favorite_changed(service);
1445 g_sequence_sort_changed(iter, service_compare, NULL);
1447 __connman_profile_changed(FALSE);
1452 void __connman_service_set_string(struct connman_service *service,
1453 const char *key, const char *value)
1455 if (g_str_equal(key, "EAP") == TRUE) {
1456 g_free(service->eap);
1457 service->eap = g_strdup(value);
1458 } else if (g_str_equal(key, "Identity") == TRUE) {
1459 g_free(service->identity);
1460 service->identity = g_strdup(value);
1461 } else if (g_str_equal(key, "CACertFile") == TRUE) {
1462 g_free(service->ca_cert_file);
1463 service->ca_cert_file = g_strdup(value);
1464 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
1465 g_free(service->client_cert_file);
1466 service->client_cert_file = g_strdup(value);
1467 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
1468 g_free(service->private_key_file);
1469 service->private_key_file = g_strdup(value);
1470 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
1471 g_free(service->private_key_passphrase);
1472 service->private_key_passphrase = g_strdup(value);
1473 } else if (g_str_equal(key, "Phase2") == TRUE) {
1474 g_free(service->phase2);
1475 service->phase2 = g_strdup(value);
1476 } else if (g_str_equal(key, "Passphrase") == TRUE) {
1477 g_free(service->passphrase);
1478 service->passphrase = g_strdup(value);
1482 int __connman_service_indicate_state(struct connman_service *service,
1483 enum connman_service_state state)
1485 GSequenceIter *iter;
1487 DBG("service %p state %d", service, state);
1489 if (service == NULL)
1492 if (service->state == state)
1495 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
1496 state == CONNMAN_SERVICE_STATE_IDLE)
1499 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
1500 state == CONNMAN_SERVICE_STATE_DISCONNECT)
1503 if (state == CONNMAN_SERVICE_STATE_IDLE &&
1504 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
1505 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
1506 state_changed(service);
1508 reply_pending(service, ECONNABORTED);
1510 __connman_service_disconnect(service);
1513 if (state == CONNMAN_SERVICE_STATE_CONFIGURATION)
1514 __connman_ipconfig_enable(service->ipconfig);
1516 service->state = state;
1517 state_changed(service);
1519 if (state == CONNMAN_SERVICE_STATE_IDLE) {
1520 connman_bool_t reconnect;
1522 reconnect = get_reconnect_state(service);
1523 if (reconnect == TRUE)
1524 __connman_service_auto_connect();
1527 if (state == CONNMAN_SERVICE_STATE_READY) {
1528 set_reconnect_state(service, TRUE);
1530 connman_service_set_favorite(service, TRUE);
1532 reply_pending(service, 0);
1534 service->userconnect = FALSE;
1536 g_get_current_time(&service->modified);
1537 __connman_storage_save_service(service);
1539 __connman_notifier_connect(service->type);
1542 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
1545 __connman_notifier_disconnect(service->type);
1548 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
1549 reply_pending(service, EIO);
1551 if (service->userconnect == FALSE)
1552 __connman_service_auto_connect();
1554 g_get_current_time(&service->modified);
1555 __connman_storage_save_service(service);
1557 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1559 iter = g_hash_table_lookup(service_hash, service->identifier);
1561 g_sequence_sort_changed(iter, service_compare, NULL);
1563 __connman_profile_changed(FALSE);
1565 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
1566 service->state == CONNMAN_SERVICE_STATE_FAILURE)
1567 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
1572 int __connman_service_indicate_error(struct connman_service *service,
1573 enum connman_service_error error)
1575 DBG("service %p error %d", service, error);
1577 if (service == NULL)
1580 service->error = error;
1582 return __connman_service_indicate_state(service,
1583 CONNMAN_SERVICE_STATE_FAILURE);
1586 int __connman_service_indicate_default(struct connman_service *service)
1588 DBG("service %p", service);
1595 static connman_bool_t prepare_network(struct connman_service *service)
1597 enum connman_network_type type;
1598 unsigned int ssid_len;
1600 type = connman_network_get_type(service->network);
1603 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1604 case CONNMAN_NETWORK_TYPE_VENDOR:
1606 case CONNMAN_NETWORK_TYPE_WIFI:
1607 if (connman_network_get_blob(service->network, "WiFi.SSID",
1611 connman_network_set_string(service->network,
1612 "WiFi.Passphrase", service->passphrase);
1614 case CONNMAN_NETWORK_TYPE_ETHERNET:
1615 case CONNMAN_NETWORK_TYPE_WIMAX:
1616 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1617 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1618 case CONNMAN_NETWORK_TYPE_CELLULAR:
1620 case CONNMAN_NETWORK_TYPE_MBM:
1621 case CONNMAN_NETWORK_TYPE_HSO:
1622 connman_network_set_string(service->network,
1623 "Cellular.APN", service->apn);
1625 connman_network_set_string(service->network,
1626 "Cellular.Username", service->username);
1627 connman_network_set_string(service->network,
1628 "Cellular.Password", service->password);
1635 static void prepare_8021x(struct connman_service *service)
1638 connman_network_set_string(service->network, "WiFi.EAP",
1641 if (service->identity)
1642 connman_network_set_string(service->network, "WiFi.Identity",
1645 if (service->ca_cert_file)
1646 connman_network_set_string(service->network, "WiFi.CACertFile",
1647 service->ca_cert_file);
1649 if (service->client_cert_file)
1650 connman_network_set_string(service->network,
1651 "WiFi.ClientCertFile",
1652 service->client_cert_file);
1654 if (service->private_key_file)
1655 connman_network_set_string(service->network,
1656 "WiFi.PrivateKeyFile",
1657 service->private_key_file);
1659 if (service->private_key_passphrase)
1660 connman_network_set_string(service->network,
1661 "WiFi.PrivateKeyPassphrase",
1662 service->private_key_passphrase);
1664 if (service->phase2)
1665 connman_network_set_string(service->network, "WiFi.Phase2",
1669 int __connman_service_connect(struct connman_service *service)
1673 DBG("service %p", service);
1675 if (service->state == CONNMAN_SERVICE_STATE_READY)
1678 if (is_connecting(service) == TRUE)
1681 switch (service->type) {
1682 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1683 case CONNMAN_SERVICE_TYPE_SYSTEM:
1684 case CONNMAN_SERVICE_TYPE_VPN:
1686 case CONNMAN_SERVICE_TYPE_ETHERNET:
1687 case CONNMAN_SERVICE_TYPE_WIMAX:
1688 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1690 case CONNMAN_SERVICE_TYPE_CELLULAR:
1691 if (service->apn == NULL)
1694 case CONNMAN_SERVICE_TYPE_WIFI:
1695 switch (service->security) {
1696 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1697 case CONNMAN_SERVICE_SECURITY_NONE:
1699 case CONNMAN_SERVICE_SECURITY_WEP:
1700 case CONNMAN_SERVICE_SECURITY_PSK:
1701 case CONNMAN_SERVICE_SECURITY_WPA:
1702 case CONNMAN_SERVICE_SECURITY_RSN:
1703 case CONNMAN_SERVICE_SECURITY_8021X:
1704 if (service->passphrase == NULL)
1711 if (service->network != NULL) {
1712 if (prepare_network(service) == FALSE)
1715 switch (service->security) {
1716 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1717 case CONNMAN_SERVICE_SECURITY_NONE:
1718 case CONNMAN_SERVICE_SECURITY_WEP:
1719 case CONNMAN_SERVICE_SECURITY_PSK:
1720 case CONNMAN_SERVICE_SECURITY_WPA:
1721 case CONNMAN_SERVICE_SECURITY_RSN:
1723 case CONNMAN_SERVICE_SECURITY_8021X:
1724 prepare_8021x(service);
1728 __connman_ipconfig_enable(service->ipconfig);
1730 err = __connman_network_connect(service->network);
1735 if (err != -EINPROGRESS) {
1736 __connman_ipconfig_disable(service->ipconfig);
1740 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
1741 connect_timeout, service);
1743 return -EINPROGRESS;
1749 int __connman_service_disconnect(struct connman_service *service)
1753 DBG("service %p", service);
1755 if (service->network != NULL) {
1756 err = __connman_network_disconnect(service->network);
1760 __connman_ipconfig_disable(service->ipconfig);
1763 if (err != -EINPROGRESS)
1766 return -EINPROGRESS;
1773 * __connman_service_lookup:
1774 * @identifier: service identifier
1776 * Look up a service by identifier (reference count will not be increased)
1778 static struct connman_service *__connman_service_lookup(const char *identifier)
1780 GSequenceIter *iter;
1782 iter = g_hash_table_lookup(service_hash, identifier);
1784 return g_sequence_get(iter);
1789 static struct connman_network *create_hidden_wifi(struct connman_device *device,
1790 const char *ssid, const char *mode, const char *security)
1792 struct connman_network *network;
1795 unsigned int i, ssid_len;
1797 ssid_len = strlen(ssid);
1801 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
1802 if (network == NULL)
1805 connman_network_set_blob(network, "WiFi.SSID",
1806 (unsigned char *) ssid, ssid_len);
1808 connman_network_set_string(network, "WiFi.Mode", mode);
1809 connman_network_set_string(network, "WiFi.Security", security);
1811 name = g_try_malloc0(ssid_len + 1);
1813 connman_network_unref(network);
1817 for (i = 0; i < ssid_len; i++) {
1818 if (g_ascii_isprint(ssid[i]))
1824 connman_network_set_name(network, name);
1828 index = connman_device_get_index(device);
1829 connman_network_set_index(network, index);
1831 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
1833 if (connman_device_add_network(device, network) < 0) {
1834 connman_network_unref(network);
1838 connman_network_set_available(network, TRUE);
1843 int __connman_service_create_and_connect(DBusMessage *msg)
1845 struct connman_service *service;
1846 struct connman_network *network;
1847 struct connman_device *device;
1848 DBusMessageIter iter, array;
1849 const char *mode = "managed", *security = "none";
1850 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
1851 unsigned int ssid_len = 0;
1854 gboolean created = FALSE;
1857 dbus_message_iter_init(msg, &iter);
1858 dbus_message_iter_recurse(&iter, &array);
1860 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1861 DBusMessageIter entry, value;
1864 dbus_message_iter_recurse(&array, &entry);
1865 dbus_message_iter_get_basic(&entry, &key);
1867 dbus_message_iter_next(&entry);
1868 dbus_message_iter_recurse(&entry, &value);
1870 switch (dbus_message_iter_get_arg_type(&value)) {
1871 case DBUS_TYPE_STRING:
1872 if (g_str_equal(key, "Type") == TRUE)
1873 dbus_message_iter_get_basic(&value, &type);
1874 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
1875 g_str_equal(key, "Mode") == TRUE)
1876 dbus_message_iter_get_basic(&value, &mode);
1877 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
1878 g_str_equal(key, "Security") == TRUE)
1879 dbus_message_iter_get_basic(&value, &security);
1880 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
1881 g_str_equal(key, "Passphrase") == TRUE)
1882 dbus_message_iter_get_basic(&value, &passphrase);
1883 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
1884 g_str_equal(key, "SSID") == TRUE)
1885 dbus_message_iter_get_basic(&value, &ssid);
1888 dbus_message_iter_next(&array);
1894 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
1900 ssid_len = strlen(ssid);
1904 if (g_strcmp0(security, "none") != 0 &&
1905 g_strcmp0(security, "wep") != 0 &&
1906 g_strcmp0(security, "psk") != 0 &&
1907 g_strcmp0(security, "wpa") != 0 &&
1908 g_strcmp0(security, "rsn") != 0 &&
1909 g_strcmp0(security, "ieee8021x") != 0)
1912 device = __connman_element_find_device(CONNMAN_DEVICE_TYPE_WIFI);
1916 ident = __connman_device_get_ident(device);
1920 group = connman_wifi_build_group_name((unsigned char *) ssid,
1921 ssid_len, mode, security);
1925 name = g_strdup_printf("%s_%s_%s", type, ident, group);
1927 service = __connman_service_lookup(name);
1929 if (service != NULL)
1932 network = create_hidden_wifi(device, ssid, mode, security);
1933 if (network != NULL) {
1934 connman_network_set_group(network, group);
1938 service = __connman_service_lookup(name);
1944 if (service == NULL) {
1949 set_reconnect_state(service, FALSE);
1951 __connman_device_disconnect(device);
1953 if (passphrase != NULL) {
1954 g_free(service->passphrase);
1955 service->passphrase = g_strdup(passphrase);
1958 service->userconnect = TRUE;
1960 err = __connman_service_connect(service);
1961 if (err < 0 && err != -EINPROGRESS)
1964 g_dbus_send_reply(connection, msg,
1965 DBUS_TYPE_OBJECT_PATH, &service->path,
1971 if (service != NULL && created == TRUE) {
1972 struct connman_network *network = service->network;
1974 if (network != NULL) {
1975 connman_network_set_available(network, FALSE);
1976 __connman_device_cleanup_networks(device);
1979 __connman_service_put(service);
1986 * __connman_service_get:
1987 * @identifier: service identifier
1989 * Look up a service by identifier or create a new one if not found
1991 static struct connman_service *__connman_service_get(const char *identifier)
1993 struct connman_service *service;
1994 GSequenceIter *iter;
1996 iter = g_hash_table_lookup(service_hash, identifier);
1998 service = g_sequence_get(iter);
1999 if (service != NULL)
2000 g_atomic_int_inc(&service->refcount);
2004 service = connman_service_create();
2005 if (service == NULL)
2008 DBG("service %p", service);
2010 service->identifier = g_strdup(identifier);
2012 service->profile = g_strdup(__connman_profile_active_ident());
2014 iter = g_sequence_insert_sorted(service_list, service,
2015 service_compare, NULL);
2017 g_hash_table_insert(service_hash, service->identifier, iter);
2022 static int service_register(struct connman_service *service)
2024 const char *path = __connman_profile_active_path();
2025 GSequenceIter *iter;
2027 DBG("service %p", service);
2029 if (service->path != NULL)
2032 service->path = g_strdup_printf("%s/%s", path, service->identifier);
2034 DBG("path %s", service->path);
2036 __connman_config_provision_service(service);
2038 __connman_storage_load_service(service);
2040 g_dbus_register_interface(connection, service->path,
2041 CONNMAN_SERVICE_INTERFACE,
2042 service_methods, service_signals,
2043 NULL, service, NULL);
2045 iter = g_hash_table_lookup(service_hash, service->identifier);
2047 g_sequence_sort_changed(iter, service_compare, NULL);
2049 __connman_profile_changed(TRUE);
2054 static void service_up(struct connman_ipconfig *ipconfig)
2056 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
2059 static void service_down(struct connman_ipconfig *ipconfig)
2061 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
2064 static void service_lower_up(struct connman_ipconfig *ipconfig)
2066 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
2069 static void service_lower_down(struct connman_ipconfig *ipconfig)
2071 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
2074 static void service_ip_bound(struct connman_ipconfig *ipconfig)
2076 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
2078 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
2080 settings_changed(service);
2083 static void service_ip_release(struct connman_ipconfig *ipconfig)
2085 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
2087 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
2089 settings_changed(service);
2092 static const struct connman_ipconfig_ops service_ops = {
2094 .down = service_down,
2095 .lower_up = service_lower_up,
2096 .lower_down = service_lower_down,
2097 .ip_bound = service_ip_bound,
2098 .ip_release = service_ip_release,
2101 static void setup_ipconfig(struct connman_service *service, int index)
2106 service->ipconfig = connman_ipconfig_create(index);
2107 if (service->ipconfig == NULL)
2110 connman_ipconfig_set_method(service->ipconfig,
2111 CONNMAN_IPCONFIG_METHOD_DHCP);
2113 connman_ipconfig_set_data(service->ipconfig, service);
2115 connman_ipconfig_set_ops(service->ipconfig, &service_ops);
2119 * __connman_service_lookup_from_network:
2120 * @network: network structure
2122 * Look up a service by network (reference count will not be increased)
2124 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
2126 struct connman_service *service;
2127 const char *ident, *group;
2130 DBG("network %p", network);
2132 ident = __connman_network_get_ident(network);
2136 group = connman_network_get_group(network);
2140 name = g_strdup_printf("%s_%s_%s",
2141 __connman_network_get_type(network), ident, group);
2142 service = __connman_service_lookup(name);
2148 const char *__connman_service_get_path(struct connman_service *service)
2150 return service->path;
2153 unsigned int __connman_service_get_order(struct connman_service *service)
2155 GSequenceIter *iter;
2157 if (service == NULL)
2160 if (service->favorite == FALSE) {
2165 iter = g_hash_table_lookup(service_hash, service->identifier);
2167 if (g_sequence_iter_get_position(iter) == 0)
2174 return service->order;
2177 static enum connman_service_type convert_network_type(struct connman_network *network)
2179 enum connman_network_type type = connman_network_get_type(network);
2182 case CONNMAN_NETWORK_TYPE_UNKNOWN:
2183 case CONNMAN_NETWORK_TYPE_VENDOR:
2185 case CONNMAN_NETWORK_TYPE_ETHERNET:
2186 return CONNMAN_SERVICE_TYPE_ETHERNET;
2187 case CONNMAN_NETWORK_TYPE_WIFI:
2188 return CONNMAN_SERVICE_TYPE_WIFI;
2189 case CONNMAN_NETWORK_TYPE_WIMAX:
2190 return CONNMAN_SERVICE_TYPE_WIMAX;
2191 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
2192 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
2193 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
2194 case CONNMAN_NETWORK_TYPE_CELLULAR:
2195 case CONNMAN_NETWORK_TYPE_MBM:
2196 case CONNMAN_NETWORK_TYPE_HSO:
2197 return CONNMAN_SERVICE_TYPE_CELLULAR;
2200 return CONNMAN_SERVICE_TYPE_UNKNOWN;
2203 static enum connman_service_mode convert_wifi_mode(const char *mode)
2206 return CONNMAN_SERVICE_MODE_UNKNOWN;
2207 else if (g_str_equal(mode, "managed") == TRUE)
2208 return CONNMAN_SERVICE_MODE_MANAGED;
2209 else if (g_str_equal(mode, "adhoc") == TRUE)
2210 return CONNMAN_SERVICE_MODE_ADHOC;
2212 return CONNMAN_SERVICE_MODE_UNKNOWN;
2215 static enum connman_service_security convert_wifi_security(const char *security)
2217 if (security == NULL)
2218 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2219 else if (g_str_equal(security, "none") == TRUE)
2220 return CONNMAN_SERVICE_SECURITY_NONE;
2221 else if (g_str_equal(security, "wep") == TRUE)
2222 return CONNMAN_SERVICE_SECURITY_WEP;
2223 else if (g_str_equal(security, "wep") == TRUE)
2224 return CONNMAN_SERVICE_SECURITY_PSK;
2225 else if (g_str_equal(security, "ieee8021x") == TRUE)
2226 return CONNMAN_SERVICE_SECURITY_8021X;
2227 else if (g_str_equal(security, "wpa") == TRUE)
2228 return CONNMAN_SERVICE_SECURITY_WPA;
2229 else if (g_str_equal(security, "rsn") == TRUE)
2230 return CONNMAN_SERVICE_SECURITY_RSN;
2232 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2235 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
2240 return CONNMAN_SERVICE_MODE_GPRS;
2242 return CONNMAN_SERVICE_MODE_EDGE;
2247 return CONNMAN_SERVICE_MODE_UMTS;
2250 return CONNMAN_SERVICE_MODE_UNKNOWN;
2253 static void update_from_network(struct connman_service *service,
2254 struct connman_network *network)
2256 connman_uint8_t strength = service->strength;
2257 GSequenceIter *iter;
2260 DBG("service %p network %p", service, network);
2262 if (service->state == CONNMAN_SERVICE_STATE_READY)
2265 if (is_connecting(service) == TRUE)
2268 str = connman_network_get_string(network, "Name");
2270 g_free(service->name);
2271 service->name = g_strdup(str);
2272 service->hidden = FALSE;
2274 g_free(service->name);
2275 service->name = NULL;
2276 service->hidden = TRUE;
2279 service->strength = connman_network_get_uint8(network, "Strength");
2280 service->roaming = connman_network_get_bool(network, "Roaming");
2282 if (service->strength == 0) {
2284 * Filter out 0-values; it's unclear what they mean
2285 * and they cause anomalous sorting of the priority list.
2287 service->strength = strength;
2290 str = connman_network_get_string(network, "WiFi.Mode");
2291 service->mode = convert_wifi_mode(str);
2293 str = connman_network_get_string(network, "WiFi.Security");
2294 service->security = convert_wifi_security(str);
2296 str = connman_network_get_string(network, "Cellular.MCC");
2297 g_free(service->mcc);
2298 service->mcc = g_strdup(str);
2300 str = connman_network_get_string(network, "Cellular.MNC");
2301 g_free(service->mnc);
2302 service->mnc = g_strdup(str);
2304 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
2305 connman_uint8_t value = connman_network_get_uint8(network,
2308 service->mode = convert_cellular_mode(value);
2311 if (service->strength > strength && service->network != NULL) {
2312 connman_network_unref(service->network);
2313 service->network = connman_network_ref(network);
2315 strength_changed(service);
2318 if (service->network == NULL)
2319 service->network = connman_network_ref(network);
2321 iter = g_hash_table_lookup(service_hash, service->identifier);
2323 g_sequence_sort_changed(iter, service_compare, NULL);
2327 * __connman_service_create_from_network:
2328 * @network: network structure
2330 * Look up service by network and if not found, create one
2332 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
2334 struct connman_service *service;
2335 const char *ident, *group;
2338 DBG("network %p", network);
2340 ident = __connman_network_get_ident(network);
2344 group = connman_network_get_group(network);
2348 name = g_strdup_printf("%s_%s_%s",
2349 __connman_network_get_type(network), ident, group);
2350 service = __connman_service_get(name);
2353 if (service == NULL)
2356 if (__connman_network_get_weakness(network) == TRUE)
2359 if (service->path != NULL) {
2360 update_from_network(service, network);
2361 __connman_profile_changed(TRUE);
2365 service->type = convert_network_type(network);
2367 switch (service->type) {
2368 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2369 case CONNMAN_SERVICE_TYPE_SYSTEM:
2370 case CONNMAN_SERVICE_TYPE_ETHERNET:
2371 case CONNMAN_SERVICE_TYPE_WIMAX:
2372 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2373 case CONNMAN_SERVICE_TYPE_VPN:
2374 service->autoconnect = FALSE;
2376 case CONNMAN_SERVICE_TYPE_WIFI:
2377 case CONNMAN_SERVICE_TYPE_CELLULAR:
2378 service->autoconnect = TRUE;
2382 service->state = CONNMAN_SERVICE_STATE_IDLE;
2384 update_from_network(service, network);
2386 setup_ipconfig(service, connman_network_get_index(network));
2388 service_register(service);
2390 if (service->favorite == TRUE)
2391 __connman_service_auto_connect();
2396 void __connman_service_update_from_network(struct connman_network *network)
2398 struct connman_service *service;
2399 enum connman_service_mode mode;
2400 connman_uint8_t strength, value;
2401 connman_bool_t roaming;
2402 GSequenceIter *iter;
2404 DBG("network %p", network);
2406 service = __connman_service_lookup_from_network(network);
2407 if (service == NULL)
2410 if (service->network == NULL)
2413 strength = connman_network_get_uint8(service->network, "Strength");
2414 if (strength == service->strength)
2417 service->strength = strength;
2419 strength_changed(service);
2422 roaming = connman_network_get_bool(service->network, "Roaming");
2423 if (roaming == service->roaming)
2426 service->roaming = roaming;
2428 roaming_changed(service);
2430 iter = g_hash_table_lookup(service_hash, service->identifier);
2432 g_sequence_sort_changed(iter, service_compare, NULL);
2435 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
2438 value = connman_network_get_uint8(service->network, "Cellular.Mode");
2439 mode = convert_cellular_mode(value);
2441 if (mode == service->mode)
2444 service->mode = mode;
2446 mode_changed(service);
2449 void __connman_service_remove_from_network(struct connman_network *network)
2451 struct connman_service *service;
2453 DBG("network %p", network);
2455 service = __connman_service_lookup_from_network(network);
2456 if (service == NULL)
2459 __connman_service_put(service);
2462 static int service_load(struct connman_service *service)
2464 const char *ident = service->profile;
2466 GError *error = NULL;
2467 gchar *pathname, *data = NULL;
2470 connman_bool_t autoconnect;
2471 unsigned int ssid_len;
2474 DBG("service %p", service);
2479 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2480 if (pathname == NULL)
2483 keyfile = g_key_file_new();
2485 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
2492 if (g_key_file_load_from_data(keyfile, data, length,
2493 0, NULL) == FALSE) {
2500 switch (service->type) {
2501 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2502 case CONNMAN_SERVICE_TYPE_SYSTEM:
2503 case CONNMAN_SERVICE_TYPE_ETHERNET:
2504 case CONNMAN_SERVICE_TYPE_VPN:
2506 case CONNMAN_SERVICE_TYPE_WIFI:
2507 if (service->name == NULL) {
2510 name = g_key_file_get_string(keyfile,
2511 service->identifier, "Name", NULL);
2513 g_free(service->name);
2514 service->name = name;
2517 if (service->network != NULL)
2518 connman_network_set_name(service->network,
2522 if (service->network &&
2523 connman_network_get_blob(service->network,
2524 "WiFi.SSID", &ssid_len) == NULL) {
2527 hex_ssid = g_key_file_get_string(keyfile,
2528 service->identifier,
2531 if (hex_ssid != NULL) {
2533 unsigned int i, j = 0, hex;
2534 size_t hex_ssid_len = strlen(hex_ssid);
2536 ssid = g_try_malloc0(hex_ssid_len / 2);
2543 for (i = 0; i < hex_ssid_len; i += 2) {
2544 sscanf(hex_ssid + i, "%02x", &hex);
2548 connman_network_set_blob(service->network,
2549 "WiFi.SSID", ssid, hex_ssid_len / 2);
2556 case CONNMAN_SERVICE_TYPE_WIMAX:
2557 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2558 case CONNMAN_SERVICE_TYPE_CELLULAR:
2559 service->apn = g_key_file_get_string(keyfile,
2560 service->identifier, "APN", NULL);
2562 service->username = g_key_file_get_string(keyfile,
2563 service->identifier, "Username", NULL);
2565 service->username = g_key_file_get_string(keyfile,
2566 service->identifier, "Password", NULL);
2568 service->favorite = g_key_file_get_boolean(keyfile,
2569 service->identifier, "Favorite", NULL);
2571 autoconnect = g_key_file_get_boolean(keyfile,
2572 service->identifier, "AutoConnect", &error);
2574 service->autoconnect = autoconnect;
2575 g_clear_error(&error);
2577 str = g_key_file_get_string(keyfile,
2578 service->identifier, "Failure", NULL);
2580 service->state = CONNMAN_SERVICE_STATE_FAILURE;
2581 service->error = string2error(str);
2586 str = g_key_file_get_string(keyfile,
2587 service->identifier, "Modified", NULL);
2589 g_time_val_from_iso8601(str, &service->modified);
2593 str = g_key_file_get_string(keyfile,
2594 service->identifier, "Passphrase", NULL);
2596 g_free(service->passphrase);
2597 service->passphrase = str;
2600 if (service->ipconfig != NULL)
2601 __connman_ipconfig_load(service->ipconfig, keyfile,
2602 service->identifier, "IPv4.");
2605 g_key_file_free(keyfile);
2610 static int service_save(struct connman_service *service)
2612 const char *ident = service->profile;
2614 gchar *pathname, *data = NULL;
2619 DBG("service %p", service);
2624 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2625 if (pathname == NULL)
2628 keyfile = g_key_file_new();
2630 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
2634 if (g_key_file_load_from_data(keyfile, data, length,
2642 if (service->name != NULL)
2643 g_key_file_set_string(keyfile, service->identifier,
2644 "Name", service->name);
2646 switch (service->type) {
2647 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2648 case CONNMAN_SERVICE_TYPE_SYSTEM:
2649 case CONNMAN_SERVICE_TYPE_ETHERNET:
2650 case CONNMAN_SERVICE_TYPE_VPN:
2652 case CONNMAN_SERVICE_TYPE_WIFI:
2653 if (service->network) {
2654 const unsigned char *ssid;
2655 unsigned int ssid_len = 0;
2657 ssid = connman_network_get_blob(service->network,
2658 "WiFi.SSID", &ssid_len);
2660 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
2661 char *identifier = service->identifier;
2665 str = g_string_sized_new(ssid_len * 2);
2671 for (i = 0; i < ssid_len; i++)
2672 g_string_append_printf(str,
2675 g_key_file_set_string(keyfile, identifier,
2678 g_string_free(str, TRUE);
2683 case CONNMAN_SERVICE_TYPE_WIMAX:
2684 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2685 case CONNMAN_SERVICE_TYPE_CELLULAR:
2686 if (service->apn != NULL)
2687 g_key_file_set_string(keyfile, service->identifier,
2688 "APN", service->apn);
2690 if (service->username != NULL)
2691 g_key_file_set_string(keyfile, service->identifier,
2692 "Username", service->username);
2694 if (service->password != NULL)
2695 g_key_file_set_string(keyfile, service->identifier,
2696 "Password", service->password);
2698 g_key_file_set_boolean(keyfile, service->identifier,
2699 "Favorite", service->favorite);
2701 if (service->favorite == TRUE)
2702 g_key_file_set_boolean(keyfile, service->identifier,
2703 "AutoConnect", service->autoconnect);
2705 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
2706 const char *failure = error2string(service->error);
2707 if (failure != NULL)
2708 g_key_file_set_string(keyfile,
2709 service->identifier,
2710 "Failure", failure);
2712 g_key_file_remove_key(keyfile, service->identifier,
2718 str = g_time_val_to_iso8601(&service->modified);
2720 g_key_file_set_string(keyfile, service->identifier,
2725 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
2726 g_key_file_set_string(keyfile, service->identifier,
2727 "Passphrase", service->passphrase);
2729 g_key_file_remove_key(keyfile, service->identifier,
2730 "Passphrase", NULL);
2732 if (service->ipconfig != NULL)
2733 __connman_ipconfig_save(service->ipconfig, keyfile,
2734 service->identifier, "IPv4.");
2736 data = g_key_file_to_data(keyfile, &length, NULL);
2738 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
2739 connman_error("Failed to store service information");
2744 g_key_file_free(keyfile);
2751 static struct connman_storage service_storage = {
2753 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
2754 .service_load = service_load,
2755 .service_save = service_save,
2758 int __connman_service_init(void)
2762 connection = connman_dbus_get_connection();
2764 if (connman_storage_register(&service_storage) < 0)
2765 connman_error("Failed to register service storage");
2767 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2770 service_list = g_sequence_new(service_free);
2775 void __connman_service_cleanup(void)
2779 g_sequence_free(service_list);
2780 service_list = NULL;
2782 g_hash_table_destroy(service_hash);
2783 service_hash = NULL;
2785 connman_storage_unregister(&service_storage);
2787 dbus_connection_unref(connection);