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_stats {
41 connman_bool_t enabled;
42 unsigned int rx_packets_last;
43 unsigned int tx_packets_last;
44 unsigned int rx_packets;
45 unsigned int tx_packets;
46 unsigned int rx_bytes_last;
47 unsigned int tx_bytes_last;
48 unsigned int rx_bytes;
49 unsigned int tx_bytes;
50 unsigned int rx_errors_last;
51 unsigned int tx_errors_last;
52 unsigned int rx_errors;
53 unsigned int tx_errors;
54 unsigned int rx_dropped_last;
55 unsigned int tx_dropped_last;
56 unsigned int rx_dropped;
57 unsigned int tx_dropped;
58 unsigned int time_start;
63 struct connman_service {
67 enum connman_service_type type;
68 enum connman_service_mode mode;
69 enum connman_service_security security;
70 enum connman_service_state state;
71 enum connman_service_error error;
72 connman_uint8_t strength;
73 connman_bool_t favorite;
74 connman_bool_t immutable;
75 connman_bool_t hidden;
76 connman_bool_t ignore;
77 connman_bool_t autoconnect;
78 connman_bool_t userconnect;
89 connman_bool_t roaming;
90 connman_bool_t login_required;
91 struct connman_ipconfig *ipconfig;
92 struct connman_network *network;
93 struct connman_provider *provider;
98 /* 802.1x settings from the config files */
102 char *client_cert_file;
103 char *private_key_file;
104 char *private_key_passphrase;
106 DBusMessage *pending;
108 struct connman_location *location;
109 struct connman_stats stats;
112 static void append_path(gpointer value, gpointer user_data)
114 struct connman_service *service = value;
115 DBusMessageIter *iter = user_data;
117 if (service->path == NULL || service->hidden == TRUE)
120 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
124 void __connman_service_list(DBusMessageIter *iter, void *user_data)
126 g_sequence_foreach(service_list, append_path, iter);
131 struct connman_service *service;
134 static void compare_path(gpointer value, gpointer user_data)
136 struct connman_service *service = value;
137 struct find_data *data = user_data;
139 if (data->service != NULL)
142 if (g_strcmp0(service->path, data->path) == 0)
143 data->service = service;
146 static struct connman_service *find_service(const char *path)
148 struct find_data data = { .path = path, .service = NULL };
150 DBG("path %s", path);
152 g_sequence_foreach(service_list, compare_path, &data);
157 const char *__connman_service_type2string(enum connman_service_type type)
160 case CONNMAN_SERVICE_TYPE_UNKNOWN:
162 case CONNMAN_SERVICE_TYPE_SYSTEM:
164 case CONNMAN_SERVICE_TYPE_ETHERNET:
166 case CONNMAN_SERVICE_TYPE_WIFI:
168 case CONNMAN_SERVICE_TYPE_WIMAX:
170 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
172 case CONNMAN_SERVICE_TYPE_CELLULAR:
174 case CONNMAN_SERVICE_TYPE_GPS:
176 case CONNMAN_SERVICE_TYPE_VPN:
183 static const char *mode2string(enum connman_service_mode mode)
186 case CONNMAN_SERVICE_MODE_UNKNOWN:
188 case CONNMAN_SERVICE_MODE_MANAGED:
190 case CONNMAN_SERVICE_MODE_ADHOC:
192 case CONNMAN_SERVICE_MODE_GPRS:
194 case CONNMAN_SERVICE_MODE_EDGE:
196 case CONNMAN_SERVICE_MODE_UMTS:
203 static const char *security2string(enum connman_service_security security)
206 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
208 case CONNMAN_SERVICE_SECURITY_NONE:
210 case CONNMAN_SERVICE_SECURITY_WEP:
212 case CONNMAN_SERVICE_SECURITY_PSK:
214 case CONNMAN_SERVICE_SECURITY_8021X:
216 case CONNMAN_SERVICE_SECURITY_WPA:
218 case CONNMAN_SERVICE_SECURITY_RSN:
225 static const char *state2string(enum connman_service_state state)
228 case CONNMAN_SERVICE_STATE_UNKNOWN:
230 case CONNMAN_SERVICE_STATE_IDLE:
232 case CONNMAN_SERVICE_STATE_ASSOCIATION:
233 return "association";
234 case CONNMAN_SERVICE_STATE_CONFIGURATION:
235 return "configuration";
236 case CONNMAN_SERVICE_STATE_READY:
238 case CONNMAN_SERVICE_STATE_ONLINE:
240 case CONNMAN_SERVICE_STATE_DISCONNECT:
242 case CONNMAN_SERVICE_STATE_FAILURE:
249 static const char *error2string(enum connman_service_error error)
252 case CONNMAN_SERVICE_ERROR_UNKNOWN:
254 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
255 return "out-of-range";
256 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
257 return "pin-missing";
258 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
259 return "dhcp-failed";
260 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
261 return "connect-failed";
267 static enum connman_service_error string2error(const char *error)
269 if (g_strcmp0(error, "dhcp-failed") == 0)
270 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
271 else if (g_strcmp0(error, "pin-missing") == 0)
272 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
274 return CONNMAN_SERVICE_ERROR_UNKNOWN;
277 static connman_bool_t is_connecting(struct connman_service *service)
279 switch (service->state) {
280 case CONNMAN_SERVICE_STATE_UNKNOWN:
281 case CONNMAN_SERVICE_STATE_IDLE:
282 case CONNMAN_SERVICE_STATE_FAILURE:
283 case CONNMAN_SERVICE_STATE_DISCONNECT:
284 case CONNMAN_SERVICE_STATE_READY:
285 case CONNMAN_SERVICE_STATE_ONLINE:
287 case CONNMAN_SERVICE_STATE_ASSOCIATION:
288 case CONNMAN_SERVICE_STATE_CONFIGURATION:
295 static connman_bool_t is_connected(const struct connman_service *service)
297 switch (service->state) {
298 case CONNMAN_SERVICE_STATE_UNKNOWN:
299 case CONNMAN_SERVICE_STATE_IDLE:
300 case CONNMAN_SERVICE_STATE_ASSOCIATION:
301 case CONNMAN_SERVICE_STATE_CONFIGURATION:
302 case CONNMAN_SERVICE_STATE_DISCONNECT:
303 case CONNMAN_SERVICE_STATE_FAILURE:
305 case CONNMAN_SERVICE_STATE_READY:
306 case CONNMAN_SERVICE_STATE_ONLINE:
313 static void update_nameservers(struct connman_service *service)
315 const char *ifname = connman_ipconfig_get_ifname(service->ipconfig);
320 switch (service->state) {
321 case CONNMAN_SERVICE_STATE_UNKNOWN:
322 case CONNMAN_SERVICE_STATE_IDLE:
323 case CONNMAN_SERVICE_STATE_ASSOCIATION:
324 case CONNMAN_SERVICE_STATE_CONFIGURATION:
326 case CONNMAN_SERVICE_STATE_FAILURE:
327 case CONNMAN_SERVICE_STATE_DISCONNECT:
328 connman_resolver_remove_all(ifname);
330 case CONNMAN_SERVICE_STATE_READY:
331 case CONNMAN_SERVICE_STATE_ONLINE:
335 connman_resolver_remove_all(ifname);
337 if (service->nameservers != NULL) {
340 for (i = 0; service->nameservers[i]; i++)
341 connman_resolver_append(ifname, NULL,
342 service->nameservers[i]);
343 } else if (service->nameserver != NULL)
344 connman_resolver_append(ifname, NULL, service->nameserver);
346 connman_resolver_flush();
349 void __connman_service_append_nameserver(struct connman_service *service,
350 const char *nameserver)
352 DBG("service %p nameserver %s", service, nameserver);
354 if (nameserver == NULL)
357 g_free(service->nameserver);
358 service->nameserver = g_strdup(nameserver);
360 update_nameservers(service);
363 void __connman_service_remove_nameserver(struct connman_service *service,
364 const char *nameserver)
366 DBG("service %p nameserver %s", service, nameserver);
368 if (nameserver == NULL)
371 g_free(service->nameserver);
372 service->nameserver = NULL;
374 update_nameservers(service);
377 void __connman_service_nameserver_add_routes(struct connman_service *service,
385 index = connman_network_get_index(service->network);
387 if (service->nameservers != NULL) {
391 * We add nameservers host routes for nameservers that
392 * are not on our subnet. For those who are, the subnet
393 * route will be installed by the time the dns proxy code
394 * tries to reach them. The subnet route is installed
395 * when setting the interface IP address.
397 for (i = 0; service->nameservers[i]; i++) {
398 if (connman_inet_compare_subnet(index,
399 service->nameservers[i]))
402 connman_inet_add_host_route(index,
403 service->nameservers[i], gw);
405 } else if (service->nameserver != NULL) {
406 if (connman_inet_compare_subnet(index, service->nameserver))
409 connman_inet_add_host_route(index, service->nameserver, gw);
413 void __connman_service_nameserver_del_routes(struct connman_service *service)
420 index = connman_network_get_index(service->network);
422 if (service->nameservers != NULL) {
425 for (i = 0; service->nameservers[i]; i++)
426 connman_inet_del_host_route(index,
427 service->nameservers[i]);
428 } else if (service->nameserver != NULL) {
429 connman_inet_del_host_route(index, service->nameserver);
433 static void __connman_service_stats_start(struct connman_service *service)
435 DBG("service %p", service);
437 if (service->stats.timer == NULL)
440 service->stats.enabled = TRUE;
442 service->stats.time_start = service->stats.time;
444 g_timer_start(service->stats.timer);
446 __connman_counter_add_service(service);
449 static void __connman_service_stats_stop(struct connman_service *service)
451 unsigned int seconds;
453 DBG("service %p", service);
455 if (service->stats.timer == NULL)
458 if (service->stats.enabled == FALSE)
461 __connman_counter_remove_service(service);
463 g_timer_stop(service->stats.timer);
465 seconds = g_timer_elapsed(service->stats.timer, NULL);
466 service->stats.time = service->stats.time_start + seconds;
468 service->stats.enabled = FALSE;
471 static int __connman_service_stats_load(struct connman_service *service,
472 GKeyFile *keyfile, const char *identifier)
474 service->stats.rx_packets = g_key_file_get_integer(keyfile,
475 identifier, "rx_packets", NULL);
476 service->stats.tx_packets = g_key_file_get_integer(keyfile,
477 identifier, "tx_packets", NULL);
478 service->stats.rx_bytes = g_key_file_get_integer(keyfile,
479 identifier, "rx_bytes", NULL);
480 service->stats.tx_bytes = g_key_file_get_integer(keyfile,
481 identifier, "tx_bytes", NULL);
482 service->stats.rx_errors = g_key_file_get_integer(keyfile,
483 identifier, "rx_errors", NULL);
484 service->stats.tx_errors = g_key_file_get_integer(keyfile,
485 identifier, "tx_errors", NULL);
486 service->stats.rx_dropped = g_key_file_get_integer(keyfile,
487 identifier, "rx_dropped", NULL);
488 service->stats.tx_dropped = g_key_file_get_integer(keyfile,
489 identifier, "tx_dropped", NULL);
490 service->stats.time = g_key_file_get_integer(keyfile,
491 identifier, "time", NULL);
496 static int __connman_service_stats_save(struct connman_service *service,
497 GKeyFile *keyfile, const char *identifier)
499 g_key_file_set_integer(keyfile, identifier, "rx_packets",
500 service->stats.rx_packets);
501 g_key_file_set_integer(keyfile, identifier, "tx_packets",
502 service->stats.tx_packets);
503 g_key_file_set_integer(keyfile, identifier, "rx_bytes",
504 service->stats.rx_bytes);
505 g_key_file_set_integer(keyfile, identifier, "tx_bytes",
506 service->stats.tx_bytes);
507 g_key_file_set_integer(keyfile, identifier, "rx_errors",
508 service->stats.rx_errors);
509 g_key_file_set_integer(keyfile, identifier, "tx_errors",
510 service->stats.tx_errors);
511 g_key_file_set_integer(keyfile, identifier, "rx_dropped",
512 service->stats.rx_dropped);
513 g_key_file_set_integer(keyfile, identifier, "tx_dropped",
514 service->stats.tx_dropped);
515 g_key_file_set_integer(keyfile, identifier, "time",
516 service->stats.time);
521 static void __connman_service_reset_stats(struct connman_service *service)
523 DBG("service %p", service);
525 service->stats.valid = FALSE;
526 service->stats.rx_packets = 0;
527 service->stats.tx_packets = 0;
528 service->stats.rx_bytes = 0;
529 service->stats.tx_bytes = 0;
530 service->stats.rx_errors = 0;
531 service->stats.tx_errors = 0;
532 service->stats.rx_dropped = 0;
533 service->stats.tx_dropped = 0;
534 service->stats.time = 0;
535 service->stats.time_start = 0;
536 g_timer_reset(service->stats.timer);
540 unsigned long __connman_service_stats_get_rx_packets(struct connman_service *service)
542 return service->stats.rx_packets;
545 unsigned long __connman_service_stats_get_tx_packets(struct connman_service *service)
547 return service->stats.tx_packets;
550 unsigned long __connman_service_stats_get_rx_bytes(struct connman_service *service)
552 return service->stats.rx_bytes;
555 unsigned long __connman_service_stats_get_tx_bytes(struct connman_service *service)
557 return service->stats.tx_bytes;
560 unsigned long __connman_service_stats_get_rx_errors(struct connman_service *service)
562 return service->stats.rx_errors;
565 unsigned long __connman_service_stats_get_tx_errors(struct connman_service *service)
567 return service->stats.tx_errors;
570 unsigned long __connman_service_stats_get_rx_dropped(struct connman_service *service)
572 return service->stats.rx_dropped;
575 unsigned long __connman_service_stats_get_tx_dropped(struct connman_service *service)
577 return service->stats.tx_dropped;
580 unsigned long __connman_service_stats_get_time(struct connman_service *service)
582 return service->stats.time;
585 static struct connman_service *get_default(void)
587 struct connman_service *service;
590 iter = g_sequence_get_begin_iter(service_list);
592 if (g_sequence_iter_is_end(iter) == TRUE)
595 service = g_sequence_get(iter);
597 if (is_connected(service) == FALSE)
603 static void default_changed(void)
605 struct connman_service *service = get_default();
607 __connman_notifier_default_changed(service);
610 const char *__connman_service_default(void)
612 struct connman_service *service;
614 service = get_default();
618 return __connman_service_type2string(service->type);
621 static void mode_changed(struct connman_service *service)
625 str = mode2string(service->mode);
629 connman_dbus_property_changed_basic(service->path,
630 CONNMAN_SERVICE_INTERFACE, "Mode",
631 DBUS_TYPE_STRING, &str);
634 static void state_changed(struct connman_service *service)
638 str = state2string(service->state);
642 connman_dbus_property_changed_basic(service->path,
643 CONNMAN_SERVICE_INTERFACE, "State",
644 DBUS_TYPE_STRING, &str);
647 static void strength_changed(struct connman_service *service)
649 if (service->strength == 0)
652 connman_dbus_property_changed_basic(service->path,
653 CONNMAN_SERVICE_INTERFACE, "Strength",
654 DBUS_TYPE_BYTE, &service->strength);
657 static void favorite_changed(struct connman_service *service)
659 if (service->path == NULL)
662 connman_dbus_property_changed_basic(service->path,
663 CONNMAN_SERVICE_INTERFACE, "Favorite",
664 DBUS_TYPE_BOOLEAN, &service->favorite);
667 static void immutable_changed(struct connman_service *service)
669 if (service->path == NULL)
672 connman_dbus_property_changed_basic(service->path,
673 CONNMAN_SERVICE_INTERFACE, "Immutable",
674 DBUS_TYPE_BOOLEAN, &service->immutable);
677 static void roaming_changed(struct connman_service *service)
679 if (service->path == NULL)
682 connman_dbus_property_changed_basic(service->path,
683 CONNMAN_SERVICE_INTERFACE, "Roaming",
684 DBUS_TYPE_BOOLEAN, &service->roaming);
687 static void autoconnect_changed(struct connman_service *service)
689 if (service->path == NULL)
692 connman_dbus_property_changed_basic(service->path,
693 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
694 DBUS_TYPE_BOOLEAN, &service->autoconnect);
697 static void passphrase_changed(struct connman_service *service)
699 dbus_bool_t required;
701 switch (service->type) {
702 case CONNMAN_SERVICE_TYPE_UNKNOWN:
703 case CONNMAN_SERVICE_TYPE_SYSTEM:
704 case CONNMAN_SERVICE_TYPE_ETHERNET:
705 case CONNMAN_SERVICE_TYPE_WIMAX:
706 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
707 case CONNMAN_SERVICE_TYPE_CELLULAR:
708 case CONNMAN_SERVICE_TYPE_GPS:
709 case CONNMAN_SERVICE_TYPE_VPN:
711 case CONNMAN_SERVICE_TYPE_WIFI:
714 switch (service->security) {
715 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
716 case CONNMAN_SERVICE_SECURITY_NONE:
718 case CONNMAN_SERVICE_SECURITY_WEP:
719 case CONNMAN_SERVICE_SECURITY_PSK:
720 case CONNMAN_SERVICE_SECURITY_WPA:
721 case CONNMAN_SERVICE_SECURITY_RSN:
722 if (service->passphrase == NULL)
725 case CONNMAN_SERVICE_SECURITY_8021X:
731 connman_dbus_property_changed_basic(service->path,
732 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
733 DBUS_TYPE_BOOLEAN, &required);
736 static void login_changed(struct connman_service *service)
738 dbus_bool_t required = service->login_required;
740 if (service->path == NULL)
743 connman_dbus_property_changed_basic(service->path,
744 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
745 DBUS_TYPE_BOOLEAN, &required);
748 static void apn_changed(struct connman_service *service)
750 dbus_bool_t required;
752 switch (service->type) {
753 case CONNMAN_SERVICE_TYPE_UNKNOWN:
754 case CONNMAN_SERVICE_TYPE_SYSTEM:
755 case CONNMAN_SERVICE_TYPE_ETHERNET:
756 case CONNMAN_SERVICE_TYPE_WIMAX:
757 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
758 case CONNMAN_SERVICE_TYPE_WIFI:
759 case CONNMAN_SERVICE_TYPE_GPS:
760 case CONNMAN_SERVICE_TYPE_VPN:
762 case CONNMAN_SERVICE_TYPE_CELLULAR:
766 required = (service->apn == NULL) ? TRUE : FALSE;
768 connman_dbus_property_changed_basic(service->path,
769 CONNMAN_SERVICE_INTERFACE, "SetupRequired",
770 DBUS_TYPE_BOOLEAN, &required);
773 static void append_ethernet(DBusMessageIter *iter, void *user_data)
775 struct connman_service *service = user_data;
777 if (is_connecting(service) == FALSE &&
778 is_connected(service) == FALSE)
781 if (service->ipconfig != NULL)
782 __connman_ipconfig_append_ethernet(service->ipconfig, iter);
785 static void append_ipv4(DBusMessageIter *iter, void *user_data)
787 struct connman_service *service = user_data;
789 if (is_connected(service) == FALSE)
792 if (service->ipconfig != NULL)
793 __connman_ipconfig_append_ipv4(service->ipconfig, iter);
796 static void append_ipv6(DBusMessageIter *iter, void *user_data)
798 struct connman_service *service = user_data;
799 struct connman_ipconfig *ipv6config;
801 if (is_connected(service) == FALSE)
804 if (service->ipconfig == NULL)
807 ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
808 if (ipv6config == NULL)
811 __connman_ipconfig_append_ipv6(ipv6config, iter);
814 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
816 struct connman_service *service = user_data;
818 if (service->ipconfig != NULL)
819 __connman_ipconfig_append_ipv4config(service->ipconfig, iter);
822 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
824 struct connman_service *service = user_data;
825 struct connman_ipconfig *ipv6config;
827 if (service->ipconfig == NULL)
830 ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
831 if (ipv6config == NULL)
834 __connman_ipconfig_append_ipv6config(ipv6config, iter);
837 static void append_dns(DBusMessageIter *iter, void *user_data)
839 struct connman_service *service = user_data;
841 if (is_connected(service) == FALSE)
844 if (service->nameservers != NULL) {
847 for (i = 0; service->nameservers[i]; i++)
848 dbus_message_iter_append_basic(iter,
849 DBUS_TYPE_STRING, &service->nameservers[i]);
854 if (service->nameserver == NULL)
857 dbus_message_iter_append_basic(iter,
858 DBUS_TYPE_STRING, &service->nameserver);
861 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
863 struct connman_service *service = user_data;
866 if (service->nameservers == NULL)
869 for (i = 0; service->nameservers[i]; i++)
870 dbus_message_iter_append_basic(iter,
871 DBUS_TYPE_STRING, &service->nameservers[i]);
874 static void append_domain(DBusMessageIter *iter, void *user_data)
876 struct connman_service *service = user_data;
878 if (is_connected(service) == FALSE && is_connecting(service) == FALSE)
881 if (service->domainname == NULL)
884 dbus_message_iter_append_basic(iter,
885 DBUS_TYPE_STRING, &service->domainname);
888 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
890 struct connman_service *service = user_data;
893 if (service->domains == NULL)
896 for (i = 0; service->domains[i]; i++)
897 dbus_message_iter_append_basic(iter,
898 DBUS_TYPE_STRING, &service->domains[i]);
901 static void append_proxy(DBusMessageIter *iter, void *user_data)
903 struct connman_service *service = user_data;
905 if (is_connected(service) == FALSE)
908 if (service->ipconfig != NULL)
909 __connman_ipconfig_append_proxy(service->ipconfig, iter);
912 static void append_provider(DBusMessageIter *iter, void *user_data)
914 struct connman_service *service = user_data;
916 DBG("%p %p", service, service->provider);
918 if (is_connected(service) == FALSE)
921 if (service->provider != NULL)
922 __connman_provider_append_properties(service->provider, iter);
926 static void settings_changed(struct connman_service *service)
928 connman_dbus_property_changed_dict(service->path,
929 CONNMAN_SERVICE_INTERFACE, "IPv4",
930 append_ipv4, service);
932 connman_dbus_property_changed_dict(service->path,
933 CONNMAN_SERVICE_INTERFACE, "IPv6",
934 append_ipv6, service);
937 static void ipv4_configuration_changed(struct connman_service *service)
939 connman_dbus_property_changed_dict(service->path,
940 CONNMAN_SERVICE_INTERFACE,
941 "IPv4.Configuration",
946 static void ipv6_configuration_changed(struct connman_service *service)
948 connman_dbus_property_changed_dict(service->path,
949 CONNMAN_SERVICE_INTERFACE,
950 "IPv6.Configuration",
955 static void dns_changed(struct connman_service *service)
957 connman_dbus_property_changed_array(service->path,
958 CONNMAN_SERVICE_INTERFACE, "Nameservers",
959 DBUS_TYPE_STRING, append_dns, service);
962 static void dns_configuration_changed(struct connman_service *service)
964 connman_dbus_property_changed_array(service->path,
965 CONNMAN_SERVICE_INTERFACE,
966 "Nameservers.Configuration",
967 DBUS_TYPE_STRING, append_dnsconfig, service);
969 dns_changed(service);
972 static void domain_changed(struct connman_service *service)
974 connman_dbus_property_changed_array(service->path,
975 CONNMAN_SERVICE_INTERFACE, "Domains",
976 DBUS_TYPE_STRING, append_domain, service);
979 static void domain_configuration_changed(struct connman_service *service)
981 connman_dbus_property_changed_array(service->path,
982 CONNMAN_SERVICE_INTERFACE,
983 "Domains.Configuration",
984 DBUS_TYPE_STRING, append_domainconfig, service);
987 static void proxy_changed(struct connman_service *service)
989 connman_dbus_property_changed_dict(service->path,
990 CONNMAN_SERVICE_INTERFACE, "Proxy",
991 append_proxy, service);
994 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
995 struct connman_service *service)
997 dbus_bool_t required;
1000 str = __connman_service_type2string(service->type);
1002 connman_dbus_dict_append_basic(dict, "Type",
1003 DBUS_TYPE_STRING, &str);
1005 str = mode2string(service->mode);
1007 connman_dbus_dict_append_basic(dict, "Mode",
1008 DBUS_TYPE_STRING, &str);
1010 str = security2string(service->security);
1012 connman_dbus_dict_append_basic(dict, "Security",
1013 DBUS_TYPE_STRING, &str);
1015 str = state2string(service->state);
1017 connman_dbus_dict_append_basic(dict, "State",
1018 DBUS_TYPE_STRING, &str);
1020 str = error2string(service->error);
1022 connman_dbus_dict_append_basic(dict, "Error",
1023 DBUS_TYPE_STRING, &str);
1025 if (service->strength > 0)
1026 connman_dbus_dict_append_basic(dict, "Strength",
1027 DBUS_TYPE_BYTE, &service->strength);
1029 connman_dbus_dict_append_basic(dict, "Favorite",
1030 DBUS_TYPE_BOOLEAN, &service->favorite);
1032 connman_dbus_dict_append_basic(dict, "Immutable",
1033 DBUS_TYPE_BOOLEAN, &service->immutable);
1035 if (service->favorite == TRUE)
1036 connman_dbus_dict_append_basic(dict, "AutoConnect",
1037 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1039 connman_dbus_dict_append_basic(dict, "AutoConnect",
1040 DBUS_TYPE_BOOLEAN, &service->favorite);
1042 if (service->name != NULL)
1043 connman_dbus_dict_append_basic(dict, "Name",
1044 DBUS_TYPE_STRING, &service->name);
1046 connman_dbus_dict_append_basic(dict, "LoginRequired",
1047 DBUS_TYPE_BOOLEAN, &service->login_required);
1049 switch (service->type) {
1050 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1051 case CONNMAN_SERVICE_TYPE_SYSTEM:
1052 case CONNMAN_SERVICE_TYPE_GPS:
1053 case CONNMAN_SERVICE_TYPE_VPN:
1055 case CONNMAN_SERVICE_TYPE_CELLULAR:
1056 connman_dbus_dict_append_basic(dict, "Roaming",
1057 DBUS_TYPE_BOOLEAN, &service->roaming);
1059 if (service->mcc != NULL && service->mnc != NULL) {
1060 connman_dbus_dict_append_basic(dict, "MCC",
1061 DBUS_TYPE_STRING, &service->mcc);
1062 connman_dbus_dict_append_basic(dict, "MNC",
1063 DBUS_TYPE_STRING, &service->mnc);
1066 if (service->apn != NULL) {
1067 connman_dbus_dict_append_basic(dict, "APN",
1068 DBUS_TYPE_STRING, &service->apn);
1070 if (service->username != NULL)
1071 connman_dbus_dict_append_basic(dict,
1072 "Username", DBUS_TYPE_STRING,
1073 &service->username);
1075 if (service->password != NULL)
1076 connman_dbus_dict_append_basic(dict,
1077 "Password", DBUS_TYPE_STRING,
1078 &service->password);
1084 connman_dbus_dict_append_basic(dict, "SetupRequired",
1085 DBUS_TYPE_BOOLEAN, &required);
1086 connman_dbus_dict_append_dict(dict, "Ethernet",
1087 append_ethernet, service);
1089 case CONNMAN_SERVICE_TYPE_WIFI:
1090 if (service->passphrase != NULL && limited == FALSE)
1091 connman_dbus_dict_append_basic(dict, "Passphrase",
1092 DBUS_TYPE_STRING, &service->passphrase);
1096 switch (service->security) {
1097 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1098 case CONNMAN_SERVICE_SECURITY_NONE:
1100 case CONNMAN_SERVICE_SECURITY_WEP:
1101 case CONNMAN_SERVICE_SECURITY_PSK:
1102 case CONNMAN_SERVICE_SECURITY_WPA:
1103 case CONNMAN_SERVICE_SECURITY_RSN:
1104 if (service->passphrase == NULL)
1107 case CONNMAN_SERVICE_SECURITY_8021X:
1111 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1112 DBUS_TYPE_BOOLEAN, &required);
1114 case CONNMAN_SERVICE_TYPE_ETHERNET:
1115 case CONNMAN_SERVICE_TYPE_WIMAX:
1116 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1117 connman_dbus_dict_append_dict(dict, "Ethernet",
1118 append_ethernet, service);
1122 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1124 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1125 append_ipv4config, service);
1127 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1129 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1130 append_ipv6config, service);
1132 connman_dbus_dict_append_array(dict, "Nameservers",
1133 DBUS_TYPE_STRING, append_dns, service);
1135 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1136 DBUS_TYPE_STRING, append_dnsconfig, service);
1138 connman_dbus_dict_append_array(dict, "Domains",
1139 DBUS_TYPE_STRING, append_domain, service);
1141 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1142 DBUS_TYPE_STRING, append_domainconfig, service);
1144 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1146 connman_dbus_dict_append_dict(dict, "Provider", append_provider, service);
1149 static void append_struct(gpointer value, gpointer user_data)
1151 struct connman_service *service = value;
1152 DBusMessageIter *iter = user_data;
1153 DBusMessageIter entry, dict;
1155 if (service->path == NULL || service->hidden == TRUE)
1158 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1160 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1163 connman_dbus_dict_open(&entry, &dict);
1164 append_properties(&dict, TRUE, service);
1165 connman_dbus_dict_close(&entry, &dict);
1167 dbus_message_iter_close_container(iter, &entry);
1170 void __connman_service_list_struct(DBusMessageIter *iter)
1172 g_sequence_foreach(service_list, append_struct, iter);
1175 int __connman_service_get_index(struct connman_service *service)
1177 if (service == NULL)
1180 if (service->network == NULL)
1183 return connman_network_get_index(service->network);
1186 void __connman_service_set_domainname(struct connman_service *service,
1187 const char *domainname)
1189 if (service == NULL)
1192 g_free(service->domainname);
1193 service->domainname = g_strdup(domainname);
1195 domain_changed(service);
1198 const char *__connman_service_get_domainname(struct connman_service *service)
1200 if (service == NULL)
1203 return service->domainname;
1206 const char *__connman_service_get_nameserver(struct connman_service *service)
1208 if (service == NULL)
1211 return service->nameserver;
1214 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1217 if (service == NULL)
1220 if (__connman_ipconfig_set_proxy_autoconfig(service->ipconfig,
1224 proxy_changed(service);
1227 static DBusMessage *get_properties(DBusConnection *conn,
1228 DBusMessage *msg, void *user_data)
1230 struct connman_service *service = user_data;
1232 DBusMessageIter array, dict;
1233 dbus_bool_t limited = TRUE;
1235 DBG("service %p", service);
1237 if (__connman_security_check_privilege(msg,
1238 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
1241 reply = dbus_message_new_method_return(msg);
1245 dbus_message_iter_init_append(reply, &array);
1247 connman_dbus_dict_open(&array, &dict);
1248 append_properties(&dict, limited, service);
1249 connman_dbus_dict_close(&array, &dict);
1254 static DBusMessage *set_property(DBusConnection *conn,
1255 DBusMessage *msg, void *user_data)
1257 struct connman_service *service = user_data;
1258 DBusMessageIter iter, value;
1262 DBG("service %p", service);
1264 if (dbus_message_iter_init(msg, &iter) == FALSE)
1265 return __connman_error_invalid_arguments(msg);
1267 dbus_message_iter_get_basic(&iter, &name);
1268 dbus_message_iter_next(&iter);
1269 dbus_message_iter_recurse(&iter, &value);
1271 if (__connman_security_check_privilege(msg,
1272 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
1273 return __connman_error_permission_denied(msg);
1275 type = dbus_message_iter_get_arg_type(&value);
1277 if (g_str_has_prefix(name, "AutoConnect") == TRUE) {
1278 connman_bool_t autoconnect;
1280 if (type != DBUS_TYPE_BOOLEAN)
1281 return __connman_error_invalid_arguments(msg);
1283 if (service->favorite == FALSE)
1284 return __connman_error_invalid_service(msg);
1286 dbus_message_iter_get_basic(&value, &autoconnect);
1288 if (service->autoconnect == autoconnect)
1289 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1291 service->autoconnect = autoconnect;
1293 autoconnect_changed(service);
1295 __connman_storage_save_service(service);
1296 } else if (g_str_equal(name, "Passphrase") == TRUE) {
1297 const char *passphrase;
1299 if (type != DBUS_TYPE_STRING)
1300 return __connman_error_invalid_arguments(msg);
1302 if (service->immutable == TRUE)
1303 return __connman_error_not_supported(msg);
1305 if (__connman_security_check_privilege(msg,
1306 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
1307 return __connman_error_permission_denied(msg);
1309 dbus_message_iter_get_basic(&value, &passphrase);
1311 g_free(service->passphrase);
1312 service->passphrase = g_strdup(passphrase);
1314 passphrase_changed(service);
1316 if (service->network != NULL)
1317 connman_network_set_string(service->network,
1318 "WiFi.Passphrase", service->passphrase);
1320 __connman_storage_save_service(service);
1321 } else if (g_str_equal(name, "APN") == TRUE) {
1324 if (type != DBUS_TYPE_STRING)
1325 return __connman_error_invalid_arguments(msg);
1327 if (service->immutable == TRUE)
1328 return __connman_error_not_supported(msg);
1330 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1331 return __connman_error_invalid_service(msg);
1333 dbus_message_iter_get_basic(&value, &apn);
1335 g_free(service->apn);
1336 service->apn = g_strdup(apn);
1338 apn_changed(service);
1340 if (service->network != NULL)
1341 connman_network_set_string(service->network,
1342 "Cellular.APN", service->apn);
1344 __connman_storage_save_service(service);
1345 } else if (g_str_equal(name, "Username") == TRUE) {
1346 const char *username;
1348 if (type != DBUS_TYPE_STRING)
1349 return __connman_error_invalid_arguments(msg);
1351 if (service->immutable == TRUE)
1352 return __connman_error_not_supported(msg);
1354 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1355 return __connman_error_invalid_service(msg);
1357 dbus_message_iter_get_basic(&value, &username);
1359 g_free(service->username);
1360 service->username = g_strdup(username);
1362 if (service->network != NULL)
1363 connman_network_set_string(service->network,
1364 "Cellular.Username", service->username);
1366 __connman_storage_save_service(service);
1367 } else if (g_str_equal(name, "Password") == TRUE) {
1368 const char *password;
1370 if (type != DBUS_TYPE_STRING)
1371 return __connman_error_invalid_arguments(msg);
1373 if (service->immutable == TRUE)
1374 return __connman_error_not_supported(msg);
1376 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1377 return __connman_error_invalid_service(msg);
1379 dbus_message_iter_get_basic(&value, &password);
1381 g_free(service->password);
1382 service->password = g_strdup(password);
1384 if (service->network != NULL)
1385 connman_network_set_string(service->network,
1386 "Cellular.Password", service->password);
1388 __connman_storage_save_service(service);
1389 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
1390 DBusMessageIter entry;
1395 if (type != DBUS_TYPE_ARRAY)
1396 return __connman_error_invalid_arguments(msg);
1398 str = g_string_new(NULL);
1400 return __connman_error_invalid_arguments(msg);
1402 index = connman_network_get_index(service->network);
1403 gw = __connman_ipconfig_get_gateway(index);
1405 if (gw && strlen(gw))
1406 __connman_service_nameserver_del_routes(service);
1408 dbus_message_iter_recurse(&value, &entry);
1410 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1412 dbus_message_iter_get_basic(&entry, &val);
1413 dbus_message_iter_next(&entry);
1415 g_string_append_printf(str, " %s", val);
1417 g_string_append(str, val);
1420 g_strfreev(service->nameservers);
1423 service->nameservers = g_strsplit_set(str->str, " ", 0);
1425 service->nameservers = NULL;
1427 g_string_free(str, TRUE);
1429 if (gw && strlen(gw))
1430 __connman_service_nameserver_add_routes(service, gw);
1432 update_nameservers(service);
1433 dns_configuration_changed(service);
1435 __connman_storage_save_service(service);
1436 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
1437 DBusMessageIter entry;
1440 if (type != DBUS_TYPE_ARRAY)
1441 return __connman_error_invalid_arguments(msg);
1443 str = g_string_new(NULL);
1445 return __connman_error_invalid_arguments(msg);
1447 dbus_message_iter_recurse(&value, &entry);
1449 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1451 dbus_message_iter_get_basic(&entry, &val);
1452 dbus_message_iter_next(&entry);
1454 g_string_append_printf(str, " %s", val);
1456 g_string_append(str, val);
1459 g_strfreev(service->domains);
1462 service->domains = g_strsplit_set(str->str, " ", 0);
1464 service->domains = NULL;
1466 g_string_free(str, TRUE);
1468 //update_domains(service);
1469 domain_configuration_changed(service);
1471 __connman_storage_save_service(service);
1472 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
1473 g_str_equal(name, "IPv6.Configuration")) {
1475 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1477 struct connman_ipconfig *ipv6config;
1481 ipv6config = connman_ipconfig_get_ipv6config(
1483 if (service->ipconfig == NULL)
1484 return __connman_error_invalid_property(msg);
1486 if (is_connecting(service) ||
1487 is_connected(service)) {
1488 __connman_network_clear_ipconfig(service->network,
1490 __connman_network_clear_ipconfig(service->network,
1494 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
1495 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1496 err = __connman_ipconfig_set_config(
1497 service->ipconfig, type, &value);
1498 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
1499 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1500 err = __connman_ipconfig_set_config(
1501 ipv6config, type, &value);
1505 if (is_connected(service) ||
1506 is_connecting(service))
1507 __connman_network_set_ipconfig(
1510 return __connman_error_failed(msg, -err);
1513 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1514 ipv4_configuration_changed(service);
1515 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1516 ipv6_configuration_changed(service);
1518 if (is_connecting(service) ||
1519 is_connected(service))
1520 __connman_network_set_ipconfig(service->network,
1523 __connman_storage_save_service(service);
1525 return __connman_error_invalid_property(msg);
1527 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1530 static void set_idle(struct connman_service *service)
1532 service->state = CONNMAN_SERVICE_STATE_IDLE;
1533 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1534 state_changed(service);
1537 static DBusMessage *clear_property(DBusConnection *conn,
1538 DBusMessage *msg, void *user_data)
1540 struct connman_service *service = user_data;
1543 DBG("service %p", service);
1545 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
1548 if (__connman_security_check_privilege(msg,
1549 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
1550 return __connman_error_permission_denied(msg);
1552 if (g_str_equal(name, "Error") == TRUE) {
1555 g_get_current_time(&service->modified);
1556 __connman_storage_save_service(service);
1557 } else if (g_str_equal(name, "Passphrase") == TRUE) {
1558 if (service->immutable == TRUE)
1559 return __connman_error_not_supported(msg);
1561 g_free(service->passphrase);
1562 service->passphrase = NULL;
1564 passphrase_changed(service);
1566 __connman_storage_save_service(service);
1568 return __connman_error_invalid_property(msg);
1570 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1573 static connman_bool_t is_ignore(struct connman_service *service)
1575 if (service->autoconnect == FALSE)
1578 if (service->roaming == TRUE)
1581 if (service->ignore == TRUE)
1584 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
1590 void __connman_service_auto_connect(void)
1592 struct connman_service *service = NULL;
1593 GSequenceIter *iter;
1597 iter = g_sequence_get_begin_iter(service_list);
1599 while (g_sequence_iter_is_end(iter) == FALSE) {
1600 service = g_sequence_get(iter);
1602 if (service->pending != NULL)
1605 if (is_connecting(service) == TRUE)
1608 if (service->favorite == FALSE)
1611 if (is_connected(service) == TRUE)
1614 if (is_ignore(service) == FALSE &&
1615 service->state == CONNMAN_SERVICE_STATE_IDLE)
1620 iter = g_sequence_iter_next(iter);
1623 if (service != NULL) {
1624 service->userconnect = FALSE;
1625 __connman_service_connect(service);
1629 static void remove_timeout(struct connman_service *service)
1631 if (service->timeout > 0) {
1632 g_source_remove(service->timeout);
1633 service->timeout = 0;
1637 static void reply_pending(struct connman_service *service, int error)
1639 remove_timeout(service);
1641 if (service->pending != NULL) {
1645 reply = __connman_error_failed(service->pending,
1648 g_dbus_send_message(connection, reply);
1650 g_dbus_send_reply(connection, service->pending,
1653 dbus_message_unref(service->pending);
1654 service->pending = NULL;
1658 static gboolean connect_timeout(gpointer user_data)
1660 struct connman_service *service = user_data;
1661 connman_bool_t autoconnect = FALSE;
1663 DBG("service %p", service);
1665 service->timeout = 0;
1667 if (service->network != NULL)
1668 __connman_network_disconnect(service->network);
1670 __connman_ipconfig_disable(service->ipconfig);
1672 if (service->pending != NULL) {
1675 reply = __connman_error_operation_timeout(service->pending);
1677 g_dbus_send_message(connection, reply);
1679 dbus_message_unref(service->pending);
1680 service->pending = NULL;
1684 __connman_service_indicate_state(service,
1685 CONNMAN_SERVICE_STATE_FAILURE);
1687 if (autoconnect == TRUE && service->userconnect == FALSE)
1688 __connman_service_auto_connect();
1693 static void set_reconnect_state(struct connman_service *service,
1694 connman_bool_t reconnect)
1696 struct connman_device *device;
1698 if (service->network == NULL)
1701 device = connman_network_get_device(service->network);
1705 __connman_device_set_reconnect(device, reconnect);
1708 static connman_bool_t get_reconnect_state(struct connman_service *service)
1710 struct connman_device *device;
1712 if (service->network == NULL)
1715 device = connman_network_get_device(service->network);
1719 return __connman_device_get_reconnect(device);
1722 struct connman_service *
1723 __connman_service_connect_type(enum connman_service_type type)
1725 struct connman_service *service;
1726 GSequenceIter *iter;
1729 DBG("type %d", type);
1732 * We go through the already sorted service list.
1733 * We pick the first one matching our type, or just
1734 * the first available one if we have no type.
1736 iter = g_sequence_get_begin_iter(service_list);
1737 if (g_sequence_iter_is_end(iter))
1739 service = g_sequence_get(iter);
1742 * If the first service is connected or about to be
1743 * connected, we return it, regardless of the type.
1745 if ((g_sequence_iter_is_end(iter) == FALSE) &&
1746 (is_connecting(service) == TRUE ||
1747 is_connected(service) == TRUE))
1750 while (g_sequence_iter_is_end(iter) == FALSE) {
1751 if (service->type == type ||
1752 type == CONNMAN_SERVICE_TYPE_UNKNOWN)
1755 iter = g_sequence_iter_next(iter);
1756 service = g_sequence_get(iter);
1759 if (g_sequence_iter_is_end(iter))
1762 service->ignore = FALSE;
1764 service->userconnect = TRUE;
1766 set_reconnect_state(service, FALSE);
1768 err = __connman_service_connect(service);
1771 if (__connman_agent_request_passphrase(service,
1775 if (err != -EINPROGRESS)
1782 static DBusMessage *connect_service(DBusConnection *conn,
1783 DBusMessage *msg, void *user_data)
1785 struct connman_service *service = user_data;
1786 GSequenceIter *iter;
1789 DBG("service %p", service);
1791 if (service->pending != NULL)
1792 return __connman_error_in_progress(msg);
1794 iter = g_sequence_get_begin_iter(service_list);
1796 while (g_sequence_iter_is_end(iter) == FALSE) {
1797 struct connman_service *temp = g_sequence_get(iter);
1799 if (service->type == temp->type &&
1800 is_connecting(temp) == TRUE)
1801 return __connman_error_in_progress(msg);
1803 iter = g_sequence_iter_next(iter);
1806 service->ignore = FALSE;
1808 service->userconnect = TRUE;
1810 service->pending = dbus_message_ref(msg);
1812 set_reconnect_state(service, FALSE);
1814 err = __connman_service_connect(service);
1816 if (err == -ENOKEY) {
1817 if (__connman_agent_request_passphrase(service,
1822 if (err != -EINPROGRESS) {
1823 dbus_message_unref(service->pending);
1824 service->pending = NULL;
1826 return __connman_error_failed(msg, -err);
1832 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1835 static DBusMessage *disconnect_service(DBusConnection *conn,
1836 DBusMessage *msg, void *user_data)
1838 struct connman_service *service = user_data;
1841 DBG("service %p", service);
1843 reply_pending(service, ECONNABORTED);
1845 service->ignore = TRUE;
1847 set_reconnect_state(service, FALSE);
1849 err = __connman_service_disconnect(service);
1851 if (err != -EINPROGRESS)
1852 return __connman_error_failed(msg, -err);
1857 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1860 static DBusMessage *remove_service(DBusConnection *conn,
1861 DBusMessage *msg, void *user_data)
1863 struct connman_service *service = user_data;
1865 DBG("service %p", service);
1867 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
1868 return __connman_error_not_supported(msg);
1870 if (service->immutable == TRUE)
1871 return __connman_error_not_supported(msg);
1873 if (service->favorite == FALSE &&
1874 service->state != CONNMAN_SERVICE_STATE_FAILURE)
1875 return __connman_error_not_supported(msg);
1877 if (service->network != NULL) {
1878 set_reconnect_state(service, FALSE);
1880 __connman_network_disconnect(service->network);
1883 g_free(service->passphrase);
1884 service->passphrase = NULL;
1886 passphrase_changed(service);
1888 g_free(service->apn);
1889 service->apn = NULL;
1891 g_free(service->username);
1892 service->username = NULL;
1894 g_free(service->password);
1895 service->password = NULL;
1897 apn_changed(service);
1901 __connman_service_set_favorite(service, FALSE);
1902 __connman_storage_save_service(service);
1904 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1907 static DBusMessage *move_service(DBusConnection *conn,
1908 DBusMessage *msg, void *user_data,
1911 struct connman_service *service = user_data;
1912 struct connman_service *target;
1914 GSequenceIter *src, *dst;
1916 DBG("service %p", service);
1918 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1921 if (service->favorite == FALSE)
1922 return __connman_error_not_supported(msg);
1924 target = find_service(path);
1925 if (target == NULL || target->favorite == FALSE || target == service)
1926 return __connman_error_invalid_service(msg);
1928 DBG("target %s", target->identifier);
1930 if (target->state != service->state)
1931 return __connman_error_invalid_service(msg);
1933 g_get_current_time(&service->modified);
1934 __connman_storage_save_service(service);
1936 src = g_hash_table_lookup(service_hash, service->identifier);
1937 dst = g_hash_table_lookup(service_hash, target->identifier);
1939 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
1941 __connman_profile_changed(FALSE);
1943 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1946 static DBusMessage *move_before(DBusConnection *conn,
1947 DBusMessage *msg, void *user_data)
1949 return move_service(conn, msg, user_data, TRUE);
1952 static DBusMessage *move_after(DBusConnection *conn,
1953 DBusMessage *msg, void *user_data)
1955 return move_service(conn, msg, user_data, FALSE);
1958 static DBusMessage *reset_stats(DBusConnection *conn,
1959 DBusMessage *msg, void *user_data)
1961 struct connman_service *service = user_data;
1963 __connman_service_reset_stats(service);
1965 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1968 static GDBusMethodTable service_methods[] = {
1969 { "GetProperties", "", "a{sv}", get_properties },
1970 { "SetProperty", "sv", "", set_property },
1971 { "ClearProperty", "s", "", clear_property },
1972 { "Connect", "", "", connect_service,
1973 G_DBUS_METHOD_FLAG_ASYNC },
1974 { "Disconnect", "", "", disconnect_service },
1975 { "Remove", "", "", remove_service },
1976 { "MoveBefore", "o", "", move_before },
1977 { "MoveAfter", "o", "", move_after },
1978 { "ResetCounters", "", "", reset_stats },
1982 static GDBusSignalTable service_signals[] = {
1983 { "PropertyChanged", "sv" },
1987 static void service_free(gpointer user_data)
1989 struct connman_service *service = user_data;
1990 char *path = service->path;
1992 DBG("service %p", service);
1994 reply_pending(service, ENOENT);
1996 g_hash_table_remove(service_hash, service->identifier);
1998 __connman_service_stats_stop(service);
1999 __connman_storage_save_service(service);
2001 service->path = NULL;
2004 __connman_profile_changed(FALSE);
2006 g_dbus_unregister_interface(connection, path,
2007 CONNMAN_SERVICE_INTERFACE);
2011 if (service->network != NULL)
2012 connman_network_unref(service->network);
2014 if (service->ipconfig != NULL) {
2015 connman_ipconfig_unref(service->ipconfig);
2016 service->ipconfig = NULL;
2019 if (service->location != NULL)
2020 connman_location_unref(service->location);
2022 g_strfreev(service->nameservers);
2023 g_strfreev(service->domains);
2025 g_free(service->nameserver);
2026 g_free(service->domainname);
2027 g_free(service->mcc);
2028 g_free(service->mnc);
2029 g_free(service->apn);
2030 g_free(service->username);
2031 g_free(service->password);
2032 g_free(service->profile);
2033 g_free(service->name);
2034 g_free(service->passphrase);
2035 g_free(service->identifier);
2036 g_free(service->eap);
2037 g_free(service->identity);
2038 g_free(service->ca_cert_file);
2039 g_free(service->client_cert_file);
2040 g_free(service->private_key_file);
2041 g_free(service->private_key_passphrase);
2042 g_free(service->phase2);
2044 if (service->stats.timer != NULL)
2045 g_timer_destroy(service->stats.timer);
2051 * __connman_service_put:
2052 * @service: service structure
2054 * Release service if no longer needed
2056 void __connman_service_put(struct connman_service *service)
2058 DBG("service %p", service);
2060 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2061 GSequenceIter *iter;
2063 iter = g_hash_table_lookup(service_hash, service->identifier);
2065 reply_pending(service, ECONNABORTED);
2067 __connman_service_disconnect(service);
2069 g_sequence_remove(iter);
2071 service_free(service);
2075 static void __connman_service_initialize(struct connman_service *service)
2077 DBG("service %p", service);
2079 service->refcount = 1;
2081 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
2082 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
2083 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
2084 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
2086 service->favorite = FALSE;
2087 service->immutable = FALSE;
2088 service->hidden = FALSE;
2090 service->ignore = FALSE;
2092 service->userconnect = FALSE;
2096 service->stats.valid = FALSE;
2097 service->stats.enabled = FALSE;
2098 service->stats.timer = g_timer_new();
2100 service->provider = NULL;
2104 * connman_service_create:
2106 * Allocate a new service.
2108 * Returns: a newly-allocated #connman_service structure
2110 struct connman_service *connman_service_create(void)
2112 struct connman_service *service;
2114 service = g_try_new0(struct connman_service, 1);
2115 if (service == NULL)
2118 DBG("service %p", service);
2120 __connman_service_initialize(service);
2122 service->location = __connman_location_create(service);
2127 struct connman_location *__connman_service_get_location(struct connman_service *service)
2129 return service->location;
2133 * connman_service_ref:
2134 * @service: service structure
2136 * Increase reference counter of service
2138 struct connman_service *connman_service_ref(struct connman_service *service)
2140 g_atomic_int_inc(&service->refcount);
2146 * connman_service_unref:
2147 * @service: service structure
2149 * Decrease reference counter of service
2151 void connman_service_unref(struct connman_service *service)
2153 __connman_service_put(service);
2156 static gint service_compare(gconstpointer a, gconstpointer b,
2159 struct connman_service *service_a = (void *) a;
2160 struct connman_service *service_b = (void *) b;
2162 if (service_a->state != service_b->state) {
2163 if (is_connected(service_a) == TRUE)
2165 if (is_connected(service_b) == TRUE)
2169 if (service_a->order > service_b->order)
2172 if (service_a->order < service_b->order)
2175 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
2178 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
2181 if (service_a->type != service_b->type) {
2182 switch (service_a->type) {
2183 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2184 case CONNMAN_SERVICE_TYPE_SYSTEM:
2185 case CONNMAN_SERVICE_TYPE_ETHERNET:
2186 case CONNMAN_SERVICE_TYPE_GPS:
2187 case CONNMAN_SERVICE_TYPE_VPN:
2189 case CONNMAN_SERVICE_TYPE_WIFI:
2191 case CONNMAN_SERVICE_TYPE_WIMAX:
2192 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2193 case CONNMAN_SERVICE_TYPE_CELLULAR:
2198 return (gint) service_b->strength - (gint) service_a->strength;
2202 * connman_service_get_type:
2203 * @service: service structure
2205 * Get the type of service
2207 enum connman_service_type connman_service_get_type(struct connman_service *service)
2209 if (service == NULL)
2210 return CONNMAN_SERVICE_TYPE_UNKNOWN;
2212 return service->type;
2216 * connman_service_get_interface:
2217 * @service: service structure
2219 * Get network interface of service
2221 char *connman_service_get_interface(struct connman_service *service)
2225 if (service == NULL)
2228 if (service->network == NULL)
2231 index = connman_network_get_index(service->network);
2233 return connman_inet_ifname(index);
2237 * connman_service_get_network:
2238 * @service: service structure
2240 * Get the service network
2242 struct connman_network *
2243 __connman_service_get_network(struct connman_service *service)
2245 if (service == NULL)
2248 return service->network;
2251 struct connman_ipconfig *__connman_service_get_ipconfig(struct connman_service *service)
2253 if (service == NULL)
2256 return service->ipconfig;
2260 * __connman_service_set_favorite:
2261 * @service: service structure
2262 * @favorite: favorite value
2264 * Change the favorite setting of service
2266 int __connman_service_set_favorite(struct connman_service *service,
2267 connman_bool_t favorite)
2269 GSequenceIter *iter;
2271 iter = g_hash_table_lookup(service_hash, service->identifier);
2275 if (service->favorite == favorite)
2278 service->favorite = favorite;
2280 favorite_changed(service);
2282 g_sequence_sort_changed(iter, service_compare, NULL);
2284 __connman_profile_changed(FALSE);
2289 int __connman_service_set_immutable(struct connman_service *service,
2290 connman_bool_t immutable)
2292 service->immutable = immutable;
2294 immutable_changed(service);
2299 void __connman_service_set_string(struct connman_service *service,
2300 const char *key, const char *value)
2302 if (g_str_equal(key, "EAP") == TRUE) {
2303 g_free(service->eap);
2304 service->eap = g_strdup(value);
2305 } else if (g_str_equal(key, "Identity") == TRUE) {
2306 g_free(service->identity);
2307 service->identity = g_strdup(value);
2308 } else if (g_str_equal(key, "CACertFile") == TRUE) {
2309 g_free(service->ca_cert_file);
2310 service->ca_cert_file = g_strdup(value);
2311 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
2312 g_free(service->client_cert_file);
2313 service->client_cert_file = g_strdup(value);
2314 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
2315 g_free(service->private_key_file);
2316 service->private_key_file = g_strdup(value);
2317 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
2318 g_free(service->private_key_passphrase);
2319 service->private_key_passphrase = g_strdup(value);
2320 } else if (g_str_equal(key, "Phase2") == TRUE) {
2321 g_free(service->phase2);
2322 service->phase2 = g_strdup(value);
2323 } else if (g_str_equal(key, "Passphrase") == TRUE) {
2324 g_free(service->passphrase);
2325 service->passphrase = g_strdup(value);
2329 int __connman_service_indicate_state(struct connman_service *service,
2330 enum connman_service_state state)
2332 GSequenceIter *iter;
2334 DBG("service %p state %d", service, state);
2336 if (service == NULL)
2339 if (service->state == state)
2342 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
2343 state == CONNMAN_SERVICE_STATE_IDLE)
2346 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
2347 state == CONNMAN_SERVICE_STATE_DISCONNECT)
2350 if (state == CONNMAN_SERVICE_STATE_IDLE &&
2351 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
2352 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
2353 state_changed(service);
2355 reply_pending(service, ECONNABORTED);
2357 __connman_service_disconnect(service);
2360 if (state == CONNMAN_SERVICE_STATE_CONFIGURATION)
2361 __connman_ipconfig_enable(service->ipconfig);
2363 service->state = state;
2364 state_changed(service);
2366 if (state == CONNMAN_SERVICE_STATE_ONLINE) {
2367 if (service->login_required == TRUE) {
2368 service->login_required = FALSE;
2369 login_changed(service);
2372 connman_timeserver_sync();
2375 if (state == CONNMAN_SERVICE_STATE_IDLE) {
2376 connman_bool_t reconnect;
2378 reconnect = get_reconnect_state(service);
2379 if (reconnect == TRUE)
2380 __connman_service_auto_connect();
2383 if (state == CONNMAN_SERVICE_STATE_READY) {
2384 set_reconnect_state(service, TRUE);
2386 __connman_service_set_favorite(service, TRUE);
2388 reply_pending(service, 0);
2390 service->userconnect = FALSE;
2392 g_get_current_time(&service->modified);
2393 __connman_storage_save_service(service);
2395 update_nameservers(service);
2396 dns_changed(service);
2398 __connman_wpad_start(service);
2400 __connman_notifier_connect(service->type);
2403 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
2404 __connman_location_finish(service);
2408 __connman_wpad_stop(service);
2410 update_nameservers(service);
2411 dns_changed(service);
2413 __connman_notifier_disconnect(service->type);
2416 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
2417 reply_pending(service, EIO);
2419 if (service->userconnect == FALSE)
2420 __connman_service_auto_connect();
2422 g_get_current_time(&service->modified);
2423 __connman_storage_save_service(service);
2425 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2427 iter = g_hash_table_lookup(service_hash, service->identifier);
2429 g_sequence_sort_changed(iter, service_compare, NULL);
2431 __connman_profile_changed(FALSE);
2433 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
2434 service->state == CONNMAN_SERVICE_STATE_FAILURE)
2435 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
2440 int __connman_service_indicate_error(struct connman_service *service,
2441 enum connman_service_error error)
2443 DBG("service %p error %d", service, error);
2445 if (service == NULL)
2448 service->error = error;
2450 return __connman_service_indicate_state(service,
2451 CONNMAN_SERVICE_STATE_FAILURE);
2454 int __connman_service_indicate_default(struct connman_service *service)
2456 DBG("service %p", service);
2460 __connman_location_detect(service);
2465 int __connman_service_request_login(struct connman_service *service)
2467 DBG("service %p", service);
2469 if (service == NULL)
2472 service->login_required = TRUE;
2473 login_changed(service);
2478 static connman_bool_t prepare_network(struct connman_service *service)
2480 enum connman_network_type type;
2481 unsigned int ssid_len;
2483 type = connman_network_get_type(service->network);
2486 case CONNMAN_NETWORK_TYPE_UNKNOWN:
2487 case CONNMAN_NETWORK_TYPE_VENDOR:
2489 case CONNMAN_NETWORK_TYPE_WIFI:
2490 if (connman_network_get_blob(service->network, "WiFi.SSID",
2494 connman_network_set_string(service->network,
2495 "WiFi.Passphrase", service->passphrase);
2497 case CONNMAN_NETWORK_TYPE_ETHERNET:
2498 case CONNMAN_NETWORK_TYPE_WIMAX:
2499 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
2500 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
2502 case CONNMAN_NETWORK_TYPE_CELLULAR:
2503 connman_network_set_string(service->network,
2504 "Cellular.APN", service->apn);
2506 connman_network_set_string(service->network,
2507 "Cellular.Username", service->username);
2508 connman_network_set_string(service->network,
2509 "Cellular.Password", service->password);
2516 static void prepare_8021x(struct connman_service *service)
2518 if (service->eap != NULL)
2519 connman_network_set_string(service->network, "WiFi.EAP",
2522 if (service->identity != NULL)
2523 connman_network_set_string(service->network, "WiFi.Identity",
2526 if (service->ca_cert_file != NULL)
2527 connman_network_set_string(service->network, "WiFi.CACertFile",
2528 service->ca_cert_file);
2530 if (service->client_cert_file != NULL)
2531 connman_network_set_string(service->network,
2532 "WiFi.ClientCertFile",
2533 service->client_cert_file);
2535 if (service->private_key_file != NULL)
2536 connman_network_set_string(service->network,
2537 "WiFi.PrivateKeyFile",
2538 service->private_key_file);
2540 if (service->private_key_passphrase != NULL)
2541 connman_network_set_string(service->network,
2542 "WiFi.PrivateKeyPassphrase",
2543 service->private_key_passphrase);
2545 if (service->phase2 != NULL)
2546 connman_network_set_string(service->network, "WiFi.Phase2",
2550 int __connman_service_connect(struct connman_service *service)
2554 DBG("service %p", service);
2556 if (is_connected(service) == TRUE)
2559 if (is_connecting(service) == TRUE)
2562 switch (service->type) {
2563 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2564 case CONNMAN_SERVICE_TYPE_SYSTEM:
2565 case CONNMAN_SERVICE_TYPE_GPS:
2567 case CONNMAN_SERVICE_TYPE_ETHERNET:
2568 case CONNMAN_SERVICE_TYPE_WIMAX:
2569 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2570 case CONNMAN_SERVICE_TYPE_VPN:
2572 case CONNMAN_SERVICE_TYPE_CELLULAR:
2573 if (service->apn == NULL)
2576 case CONNMAN_SERVICE_TYPE_WIFI:
2577 switch (service->security) {
2578 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2579 case CONNMAN_SERVICE_SECURITY_NONE:
2581 case CONNMAN_SERVICE_SECURITY_WEP:
2582 case CONNMAN_SERVICE_SECURITY_PSK:
2583 case CONNMAN_SERVICE_SECURITY_WPA:
2584 case CONNMAN_SERVICE_SECURITY_RSN:
2585 if (service->passphrase == NULL)
2588 case CONNMAN_SERVICE_SECURITY_8021X:
2594 if (service->network != NULL) {
2595 if (prepare_network(service) == FALSE)
2598 switch (service->security) {
2599 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2600 case CONNMAN_SERVICE_SECURITY_NONE:
2601 case CONNMAN_SERVICE_SECURITY_WEP:
2602 case CONNMAN_SERVICE_SECURITY_PSK:
2603 case CONNMAN_SERVICE_SECURITY_WPA:
2604 case CONNMAN_SERVICE_SECURITY_RSN:
2606 case CONNMAN_SERVICE_SECURITY_8021X:
2607 prepare_8021x(service);
2611 __connman_ipconfig_enable(service->ipconfig);
2613 err = __connman_network_connect(service->network);
2614 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
2615 service->provider != NULL)
2616 err = __connman_provider_connect(service->provider);
2621 if (err != -EINPROGRESS) {
2622 __connman_ipconfig_disable(service->ipconfig);
2626 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
2627 connect_timeout, service);
2629 return -EINPROGRESS;
2635 int __connman_service_disconnect(struct connman_service *service)
2637 struct connman_ipconfig *ipv6config;
2640 DBG("service %p", service);
2642 if (service->network != NULL) {
2643 err = __connman_network_disconnect(service->network);
2644 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
2645 service->provider != NULL)
2646 err = __connman_provider_disconnect(service->provider);
2650 __connman_ipconfig_clear_address(service->ipconfig);
2652 ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
2654 __connman_ipconfig_clear_address(ipv6config);
2656 __connman_ipconfig_disable(service->ipconfig);
2659 if (err != -EINPROGRESS)
2662 return -EINPROGRESS;
2669 * __connman_service_lookup:
2670 * @pattern: search pattern
2671 * @path: return object path
2673 * Look up a service path from a search pattern
2675 int __connman_service_lookup(const char *pattern, const char **path)
2677 GHashTableIter iter;
2678 gpointer key, value;
2680 g_hash_table_iter_init(&iter, service_hash);
2682 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
2683 GSequenceIter *iter = value;
2684 struct connman_service *service = g_sequence_get(iter);
2686 if (g_strcmp0(service->identifier, pattern) == 0 ||
2687 g_strcmp0(service->name, pattern) == 0) {
2688 *path = (const char *) service->path;
2697 * lookup_by_identifier:
2698 * @identifier: service identifier
2700 * Look up a service by identifier (reference count will not be increased)
2702 static struct connman_service *lookup_by_identifier(const char *identifier)
2704 GSequenceIter *iter;
2706 iter = g_hash_table_lookup(service_hash, identifier);
2708 return g_sequence_get(iter);
2713 static struct connman_network *create_hidden_wifi(struct connman_device *device,
2714 const char *ssid, const char *mode, const char *security)
2716 struct connman_network *network;
2719 unsigned int i, ssid_len;
2721 ssid_len = strlen(ssid);
2725 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
2726 if (network == NULL)
2729 connman_network_set_blob(network, "WiFi.SSID",
2730 (unsigned char *) ssid, ssid_len);
2732 connman_network_set_string(network, "WiFi.Mode", mode);
2733 connman_network_set_string(network, "WiFi.Security", security);
2735 name = g_try_malloc0(ssid_len + 1);
2737 connman_network_unref(network);
2741 for (i = 0; i < ssid_len; i++) {
2742 if (g_ascii_isprint(ssid[i]))
2748 connman_network_set_name(network, name);
2752 index = connman_device_get_index(device);
2753 connman_network_set_index(network, index);
2755 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
2757 if (connman_device_add_network(device, network) < 0) {
2758 connman_network_unref(network);
2762 connman_network_set_available(network, TRUE);
2767 int __connman_service_create_and_connect(DBusMessage *msg)
2769 struct connman_service *service;
2770 struct connman_network *network;
2771 struct connman_device *device;
2772 DBusMessageIter iter, array;
2773 const char *mode = "managed", *security = "none", *group_security;
2774 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
2775 unsigned int ssid_len = 0;
2778 gboolean created = FALSE;
2781 dbus_message_iter_init(msg, &iter);
2782 dbus_message_iter_recurse(&iter, &array);
2784 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
2785 DBusMessageIter entry, value;
2788 dbus_message_iter_recurse(&array, &entry);
2789 dbus_message_iter_get_basic(&entry, &key);
2791 dbus_message_iter_next(&entry);
2792 dbus_message_iter_recurse(&entry, &value);
2794 switch (dbus_message_iter_get_arg_type(&value)) {
2795 case DBUS_TYPE_STRING:
2796 if (g_str_equal(key, "Type") == TRUE)
2797 dbus_message_iter_get_basic(&value, &type);
2798 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
2799 g_str_equal(key, "Mode") == TRUE)
2800 dbus_message_iter_get_basic(&value, &mode);
2801 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
2802 g_str_equal(key, "Security") == TRUE)
2803 dbus_message_iter_get_basic(&value, &security);
2804 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
2805 g_str_equal(key, "Passphrase") == TRUE)
2806 dbus_message_iter_get_basic(&value, &passphrase);
2807 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
2808 g_str_equal(key, "SSID") == TRUE)
2809 dbus_message_iter_get_basic(&value, &ssid);
2812 dbus_message_iter_next(&array);
2818 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
2824 ssid_len = strlen(ssid);
2828 if (g_strcmp0(security, "none") != 0 &&
2829 g_strcmp0(security, "wep") != 0 &&
2830 g_strcmp0(security, "psk") != 0 &&
2831 g_strcmp0(security, "wpa") != 0 &&
2832 g_strcmp0(security, "rsn") != 0 &&
2833 g_strcmp0(security, "ieee8021x") != 0)
2836 device = __connman_element_find_device(CONNMAN_DEVICE_TYPE_WIFI);
2840 ident = __connman_device_get_ident(device);
2845 if (!g_strcmp0(security, "wpa") ||
2846 !g_strcmp0(security, "rsn"))
2847 group_security = "psk";
2849 group_security = security;
2851 group = connman_wifi_build_group_name((unsigned char *) ssid,
2852 ssid_len, mode, group_security);
2856 name = g_strdup_printf("%s_%s_%s", type, ident, group);
2858 service = lookup_by_identifier(name);
2860 if (service != NULL)
2863 network = create_hidden_wifi(device, ssid, mode, security);
2864 if (network != NULL) {
2865 connman_network_set_group(network, group);
2869 service = lookup_by_identifier(name);
2875 if (service == NULL) {
2880 set_reconnect_state(service, FALSE);
2882 __connman_device_disconnect(device);
2884 if (passphrase != NULL) {
2885 g_free(service->passphrase);
2886 service->passphrase = g_strdup(passphrase);
2889 service->userconnect = TRUE;
2891 err = __connman_service_connect(service);
2892 if (err < 0 && err != -EINPROGRESS)
2895 g_dbus_send_reply(connection, msg,
2896 DBUS_TYPE_OBJECT_PATH, &service->path,
2902 if (service != NULL && created == TRUE) {
2903 struct connman_network *network = service->network;
2905 if (network != NULL) {
2906 connman_network_set_available(network, FALSE);
2907 __connman_device_cleanup_networks(device);
2909 __connman_service_put(service);
2916 * __connman_service_get:
2917 * @identifier: service identifier
2919 * Look up a service by identifier or create a new one if not found
2921 static struct connman_service *__connman_service_get(const char *identifier)
2923 struct connman_service *service;
2924 GSequenceIter *iter;
2926 iter = g_hash_table_lookup(service_hash, identifier);
2928 service = g_sequence_get(iter);
2929 if (service != NULL)
2930 g_atomic_int_inc(&service->refcount);
2934 service = connman_service_create();
2935 if (service == NULL)
2938 DBG("service %p", service);
2940 service->identifier = g_strdup(identifier);
2942 service->profile = g_strdup(__connman_profile_active_ident());
2944 iter = g_sequence_insert_sorted(service_list, service,
2945 service_compare, NULL);
2947 g_hash_table_insert(service_hash, service->identifier, iter);
2952 static int service_register(struct connman_service *service)
2954 const char *path = __connman_profile_active_path();
2955 GSequenceIter *iter;
2957 DBG("service %p", service);
2959 if (service->path != NULL)
2962 service->path = g_strdup_printf("%s/%s", path, service->identifier);
2964 DBG("path %s", service->path);
2966 __connman_config_provision_service(service);
2968 __connman_storage_load_service(service);
2970 g_dbus_register_interface(connection, service->path,
2971 CONNMAN_SERVICE_INTERFACE,
2972 service_methods, service_signals,
2973 NULL, service, NULL);
2975 iter = g_hash_table_lookup(service_hash, service->identifier);
2977 g_sequence_sort_changed(iter, service_compare, NULL);
2979 __connman_profile_changed(TRUE);
2984 static void service_up(struct connman_ipconfig *ipconfig)
2986 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
2988 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
2990 service->stats.valid = FALSE;
2993 static void service_down(struct connman_ipconfig *ipconfig)
2995 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
2998 static void service_lower_up(struct connman_ipconfig *ipconfig)
3000 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3002 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
3004 __connman_service_stats_start(service);
3007 static void service_lower_down(struct connman_ipconfig *ipconfig)
3009 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3011 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
3013 __connman_service_stats_stop(service);
3014 __connman_storage_save_service(service);
3017 static void service_ip_bound(struct connman_ipconfig *ipconfig)
3019 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3021 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
3023 settings_changed(service);
3026 static void service_ip_release(struct connman_ipconfig *ipconfig)
3028 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3030 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
3032 settings_changed(service);
3035 static const struct connman_ipconfig_ops service_ops = {
3037 .down = service_down,
3038 .lower_up = service_lower_up,
3039 .lower_down = service_lower_down,
3040 .ip_bound = service_ip_bound,
3041 .ip_release = service_ip_release,
3044 static void setup_ipconfig(struct connman_service *service, int index)
3049 service->ipconfig = connman_ipconfig_create(index);
3050 if (service->ipconfig == NULL)
3053 connman_ipconfig_set_method(service->ipconfig,
3054 CONNMAN_IPCONFIG_METHOD_DHCP);
3056 connman_ipconfig_set_data(service->ipconfig, service);
3058 connman_ipconfig_set_ops(service->ipconfig, &service_ops);
3061 void __connman_service_create_ipconfig(struct connman_service *service,
3064 struct connman_ipconfig *ipv6config;
3065 const char *ident = service->profile;
3068 if (service->ipconfig != NULL)
3071 setup_ipconfig(service, index);
3076 keyfile = __connman_storage_open_profile(ident);
3077 if (keyfile == NULL)
3081 ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
3082 if (ipv6config != NULL)
3083 __connman_ipconfig_load(ipv6config, keyfile,
3084 service->identifier, "IPv6.");
3086 __connman_ipconfig_load(service->ipconfig, keyfile,
3087 service->identifier, "IPv4.");
3088 g_key_file_free(keyfile);
3092 * __connman_service_lookup_from_network:
3093 * @network: network structure
3095 * Look up a service by network (reference count will not be increased)
3097 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
3099 struct connman_service *service;
3100 const char *ident, *group;
3103 DBG("network %p", network);
3105 ident = __connman_network_get_ident(network);
3109 group = connman_network_get_group(network);
3113 name = g_strdup_printf("%s_%s_%s",
3114 __connman_network_get_type(network), ident, group);
3115 service = lookup_by_identifier(name);
3121 const char *__connman_service_get_path(struct connman_service *service)
3123 return service->path;
3126 unsigned int __connman_service_get_order(struct connman_service *service)
3128 GSequenceIter *iter;
3130 if (service == NULL)
3133 if (service->favorite == FALSE) {
3138 iter = g_hash_table_lookup(service_hash, service->identifier);
3140 if (g_sequence_iter_get_position(iter) == 0)
3147 return service->order;
3150 static enum connman_service_type convert_network_type(struct connman_network *network)
3152 enum connman_network_type type = connman_network_get_type(network);
3155 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3156 case CONNMAN_NETWORK_TYPE_VENDOR:
3158 case CONNMAN_NETWORK_TYPE_ETHERNET:
3159 return CONNMAN_SERVICE_TYPE_ETHERNET;
3160 case CONNMAN_NETWORK_TYPE_WIFI:
3161 return CONNMAN_SERVICE_TYPE_WIFI;
3162 case CONNMAN_NETWORK_TYPE_WIMAX:
3163 return CONNMAN_SERVICE_TYPE_WIMAX;
3164 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3165 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3166 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
3167 case CONNMAN_NETWORK_TYPE_CELLULAR:
3168 return CONNMAN_SERVICE_TYPE_CELLULAR;
3171 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3174 static enum connman_service_mode convert_wifi_mode(const char *mode)
3177 return CONNMAN_SERVICE_MODE_UNKNOWN;
3178 else if (g_str_equal(mode, "managed") == TRUE)
3179 return CONNMAN_SERVICE_MODE_MANAGED;
3180 else if (g_str_equal(mode, "adhoc") == TRUE)
3181 return CONNMAN_SERVICE_MODE_ADHOC;
3183 return CONNMAN_SERVICE_MODE_UNKNOWN;
3186 static enum connman_service_security convert_wifi_security(const char *security)
3188 if (security == NULL)
3189 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3190 else if (g_str_equal(security, "none") == TRUE)
3191 return CONNMAN_SERVICE_SECURITY_NONE;
3192 else if (g_str_equal(security, "wep") == TRUE)
3193 return CONNMAN_SERVICE_SECURITY_WEP;
3194 else if (g_str_equal(security, "psk") == TRUE)
3195 return CONNMAN_SERVICE_SECURITY_PSK;
3196 else if (g_str_equal(security, "ieee8021x") == TRUE)
3197 return CONNMAN_SERVICE_SECURITY_8021X;
3198 else if (g_str_equal(security, "wpa") == TRUE)
3199 return CONNMAN_SERVICE_SECURITY_WPA;
3200 else if (g_str_equal(security, "rsn") == TRUE)
3201 return CONNMAN_SERVICE_SECURITY_RSN;
3203 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3206 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
3211 return CONNMAN_SERVICE_MODE_GPRS;
3213 return CONNMAN_SERVICE_MODE_EDGE;
3218 return CONNMAN_SERVICE_MODE_UMTS;
3221 return CONNMAN_SERVICE_MODE_UNKNOWN;
3224 static void update_from_network(struct connman_service *service,
3225 struct connman_network *network)
3227 connman_uint8_t strength = service->strength;
3228 GSequenceIter *iter;
3231 DBG("service %p network %p", service, network);
3233 if (is_connected(service) == TRUE)
3236 if (is_connecting(service) == TRUE)
3239 str = connman_network_get_string(network, "Name");
3241 g_free(service->name);
3242 service->name = g_strdup(str);
3243 service->hidden = FALSE;
3245 g_free(service->name);
3246 service->name = NULL;
3247 service->hidden = TRUE;
3250 service->strength = connman_network_get_uint8(network, "Strength");
3251 service->roaming = connman_network_get_bool(network, "Roaming");
3253 if (service->strength == 0) {
3255 * Filter out 0-values; it's unclear what they mean
3256 * and they cause anomalous sorting of the priority list.
3258 service->strength = strength;
3261 str = connman_network_get_string(network, "WiFi.Mode");
3262 service->mode = convert_wifi_mode(str);
3264 str = connman_network_get_string(network, "WiFi.Security");
3265 service->security = convert_wifi_security(str);
3267 str = connman_network_get_string(network, "Cellular.MCC");
3268 g_free(service->mcc);
3269 service->mcc = g_strdup(str);
3271 str = connman_network_get_string(network, "Cellular.MNC");
3272 g_free(service->mnc);
3273 service->mnc = g_strdup(str);
3275 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
3276 connman_uint8_t value = connman_network_get_uint8(network,
3279 service->mode = convert_cellular_mode(value);
3282 if (service->strength > strength && service->network != NULL) {
3283 connman_network_unref(service->network);
3284 service->network = connman_network_ref(network);
3286 strength_changed(service);
3289 if (service->network == NULL)
3290 service->network = connman_network_ref(network);
3292 iter = g_hash_table_lookup(service_hash, service->identifier);
3294 g_sequence_sort_changed(iter, service_compare, NULL);
3298 * __connman_service_create_from_network:
3299 * @network: network structure
3301 * Look up service by network and if not found, create one
3303 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
3305 struct connman_service *service;
3306 const char *ident, *group;
3309 DBG("network %p", network);
3311 ident = __connman_network_get_ident(network);
3315 group = connman_network_get_group(network);
3319 name = g_strdup_printf("%s_%s_%s",
3320 __connman_network_get_type(network), ident, group);
3321 service = __connman_service_get(name);
3324 if (service == NULL)
3327 if (__connman_network_get_weakness(network) == TRUE)
3330 if (service->path != NULL) {
3331 update_from_network(service, network);
3332 __connman_profile_changed(TRUE);
3336 service->type = convert_network_type(network);
3338 switch (service->type) {
3339 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3340 case CONNMAN_SERVICE_TYPE_SYSTEM:
3341 case CONNMAN_SERVICE_TYPE_ETHERNET:
3342 case CONNMAN_SERVICE_TYPE_WIMAX:
3343 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3344 case CONNMAN_SERVICE_TYPE_GPS:
3345 case CONNMAN_SERVICE_TYPE_VPN:
3346 service->autoconnect = FALSE;
3348 case CONNMAN_SERVICE_TYPE_WIFI:
3349 case CONNMAN_SERVICE_TYPE_CELLULAR:
3350 service->autoconnect = TRUE;
3354 service->state = CONNMAN_SERVICE_STATE_IDLE;
3356 update_from_network(service, network);
3358 setup_ipconfig(service, connman_network_get_index(network));
3360 service_register(service);
3362 if (service->favorite == TRUE)
3363 __connman_service_auto_connect();
3368 void __connman_service_update_from_network(struct connman_network *network)
3370 struct connman_service *service;
3371 enum connman_service_mode mode;
3372 connman_uint8_t strength, value;
3373 connman_bool_t roaming;
3374 GSequenceIter *iter;
3377 DBG("network %p", network);
3379 service = __connman_service_lookup_from_network(network);
3380 if (service == NULL)
3383 if (service->network == NULL)
3386 name = connman_network_get_string(service->network, "Name");
3387 if (g_strcmp0(service->name, name) != 0) {
3388 g_free(service->name);
3389 service->name = g_strdup(name);
3390 connman_dbus_property_changed_basic(service->path,
3391 CONNMAN_SERVICE_INTERFACE, "Name",
3392 DBUS_TYPE_STRING, &service->name);
3395 strength = connman_network_get_uint8(service->network, "Strength");
3396 if (strength == service->strength)
3399 service->strength = strength;
3401 strength_changed(service);
3404 roaming = connman_network_get_bool(service->network, "Roaming");
3405 if (roaming == service->roaming)
3408 service->roaming = roaming;
3410 roaming_changed(service);
3412 iter = g_hash_table_lookup(service_hash, service->identifier);
3414 g_sequence_sort_changed(iter, service_compare, NULL);
3417 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
3420 value = connman_network_get_uint8(service->network, "Cellular.Mode");
3421 mode = convert_cellular_mode(value);
3423 if (mode == service->mode)
3426 service->mode = mode;
3428 mode_changed(service);
3431 void __connman_service_remove_from_network(struct connman_network *network)
3433 struct connman_service *service;
3435 DBG("network %p", network);
3437 service = __connman_service_lookup_from_network(network);
3438 if (service == NULL)
3441 __connman_service_put(service);
3445 * __connman_service_create_from_provider:
3446 * @provider: provider structure
3448 * Look up service by provider and if not found, create one
3450 struct connman_service *
3451 __connman_service_create_from_provider(struct connman_provider *provider)
3453 struct connman_service *service;
3454 const char *ident, *str;
3456 int index = connman_provider_get_index(provider);
3458 DBG("provider %p", provider);
3460 ident = __connman_provider_get_ident(provider);
3464 name = g_strdup_printf("vpn_%s", ident);
3465 service = __connman_service_get(name);
3468 if (service == NULL)
3471 service->type = CONNMAN_SERVICE_TYPE_VPN;
3472 service->provider = provider;
3473 service->autoconnect = FALSE;
3475 service->state = CONNMAN_SERVICE_STATE_IDLE;
3477 str = connman_provider_get_string(provider, "Name");
3479 g_free(service->name);
3480 service->name = g_strdup(str);
3481 service->hidden = FALSE;
3483 g_free(service->name);
3484 service->name = NULL;
3485 service->hidden = TRUE;
3488 service->strength = 0;
3490 service->ipconfig = connman_ipconfig_create(index);
3491 if (service->ipconfig == NULL)
3494 connman_ipconfig_set_method(service->ipconfig,
3495 CONNMAN_IPCONFIG_METHOD_MANUAL);
3497 connman_ipconfig_set_data(service->ipconfig, service);
3499 connman_ipconfig_set_ops(service->ipconfig, &service_ops);
3501 service_register(service);
3506 void __connman_service_stats_update(struct connman_service *service,
3507 unsigned int rx_packets, unsigned int tx_packets,
3508 unsigned int rx_bytes, unsigned int tx_bytes,
3509 unsigned int rx_errors, unsigned int tx_errors,
3510 unsigned int rx_dropped, unsigned int tx_dropped)
3512 unsigned int seconds;
3513 struct connman_stats *stats = &service->stats;
3515 DBG("service %p", service);
3517 if (is_connected(service) == FALSE)
3520 if (stats->valid == TRUE) {
3521 stats->rx_packets +=
3522 rx_packets - stats->rx_packets_last;
3523 stats->tx_packets +=
3524 tx_packets - stats->tx_packets_last;
3526 rx_bytes - stats->rx_bytes_last;
3528 tx_bytes - stats->tx_bytes_last;
3530 rx_errors - stats->rx_errors_last;
3532 tx_errors - stats->tx_errors_last;
3533 stats->rx_dropped +=
3534 rx_dropped - stats->rx_dropped_last;
3535 stats->tx_dropped +=
3536 tx_dropped - stats->tx_dropped_last;
3538 stats->valid = TRUE;
3541 stats->rx_packets_last = rx_packets;
3542 stats->tx_packets_last = tx_packets;
3543 stats->rx_bytes_last = rx_bytes;
3544 stats->tx_bytes_last = tx_bytes;
3545 stats->rx_errors_last = rx_errors;
3546 stats->tx_errors_last = tx_errors;
3547 stats->rx_dropped_last = rx_dropped;
3548 stats->tx_dropped_last = tx_dropped;
3550 seconds = g_timer_elapsed(stats->timer, NULL);
3551 stats->time = stats->time_start + seconds;
3554 static int service_load(struct connman_service *service)
3556 const char *ident = service->profile;
3558 GError *error = NULL;
3559 gchar *pathname, *data = NULL;
3562 connman_bool_t autoconnect;
3563 unsigned int ssid_len;
3566 DBG("service %p", service);
3571 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
3572 if (pathname == NULL)
3575 keyfile = g_key_file_new();
3577 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
3584 if (g_key_file_load_from_data(keyfile, data, length,
3585 0, NULL) == FALSE) {
3592 switch (service->type) {
3593 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3594 case CONNMAN_SERVICE_TYPE_SYSTEM:
3595 case CONNMAN_SERVICE_TYPE_ETHERNET:
3596 case CONNMAN_SERVICE_TYPE_GPS:
3597 case CONNMAN_SERVICE_TYPE_VPN:
3599 case CONNMAN_SERVICE_TYPE_WIFI:
3600 if (service->name == NULL) {
3603 name = g_key_file_get_string(keyfile,
3604 service->identifier, "Name", NULL);
3606 g_free(service->name);
3607 service->name = name;
3610 if (service->network != NULL)
3611 connman_network_set_name(service->network,
3615 if (service->network &&
3616 connman_network_get_blob(service->network,
3617 "WiFi.SSID", &ssid_len) == NULL) {
3620 hex_ssid = g_key_file_get_string(keyfile,
3621 service->identifier,
3624 if (hex_ssid != NULL) {
3626 unsigned int i, j = 0, hex;
3627 size_t hex_ssid_len = strlen(hex_ssid);
3629 ssid = g_try_malloc0(hex_ssid_len / 2);
3636 for (i = 0; i < hex_ssid_len; i += 2) {
3637 sscanf(hex_ssid + i, "%02x", &hex);
3641 connman_network_set_blob(service->network,
3642 "WiFi.SSID", ssid, hex_ssid_len / 2);
3649 case CONNMAN_SERVICE_TYPE_WIMAX:
3650 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3651 case CONNMAN_SERVICE_TYPE_CELLULAR:
3652 service->apn = g_key_file_get_string(keyfile,
3653 service->identifier, "APN", NULL);
3655 service->username = g_key_file_get_string(keyfile,
3656 service->identifier, "Username", NULL);
3658 service->password = g_key_file_get_string(keyfile,
3659 service->identifier, "Password", NULL);
3661 service->favorite = g_key_file_get_boolean(keyfile,
3662 service->identifier, "Favorite", NULL);
3664 autoconnect = g_key_file_get_boolean(keyfile,
3665 service->identifier, "AutoConnect", &error);
3667 service->autoconnect = autoconnect;
3668 g_clear_error(&error);
3670 str = g_key_file_get_string(keyfile,
3671 service->identifier, "Failure", NULL);
3673 service->state = CONNMAN_SERVICE_STATE_FAILURE;
3674 service->error = string2error(str);
3679 str = g_key_file_get_string(keyfile,
3680 service->identifier, "Modified", NULL);
3682 g_time_val_from_iso8601(str, &service->modified);
3686 str = g_key_file_get_string(keyfile,
3687 service->identifier, "Passphrase", NULL);
3689 g_free(service->passphrase);
3690 service->passphrase = str;
3693 if (service->ipconfig != NULL) {
3694 struct connman_ipconfig *ipv6config;
3696 ipv6config = connman_ipconfig_get_ipv6config(
3698 if (ipv6config != NULL)
3699 __connman_ipconfig_load(ipv6config, keyfile,
3700 service->identifier, "IPv6.");
3702 __connman_ipconfig_load(service->ipconfig, keyfile,
3703 service->identifier, "IPv4.");
3706 service->nameservers = g_key_file_get_string_list(keyfile,
3707 service->identifier, "Nameservers", &length, NULL);
3708 if (service->nameservers != NULL && length == 0) {
3709 g_strfreev(service->nameservers);
3710 service->nameservers = NULL;
3713 service->domains = g_key_file_get_string_list(keyfile,
3714 service->identifier, "Domains", &length, NULL);
3715 if (service->domains != NULL && length == 0) {
3716 g_strfreev(service->domains);
3717 service->domains = NULL;
3720 __connman_service_stats_load(service, keyfile, service->identifier);
3722 g_key_file_free(keyfile);
3727 static int service_save(struct connman_service *service)
3729 const char *ident = service->profile;
3731 gchar *pathname, *data = NULL;
3736 DBG("service %p", service);
3741 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
3742 if (pathname == NULL)
3745 keyfile = g_key_file_new();
3747 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
3751 if (g_key_file_load_from_data(keyfile, data, length,
3759 if (service->name != NULL)
3760 g_key_file_set_string(keyfile, service->identifier,
3761 "Name", service->name);
3763 switch (service->type) {
3764 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3765 case CONNMAN_SERVICE_TYPE_SYSTEM:
3766 case CONNMAN_SERVICE_TYPE_ETHERNET:
3767 case CONNMAN_SERVICE_TYPE_GPS:
3768 case CONNMAN_SERVICE_TYPE_VPN:
3770 case CONNMAN_SERVICE_TYPE_WIFI:
3771 if (service->network) {
3772 const unsigned char *ssid;
3773 unsigned int ssid_len = 0;
3775 ssid = connman_network_get_blob(service->network,
3776 "WiFi.SSID", &ssid_len);
3778 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
3779 char *identifier = service->identifier;
3783 str = g_string_sized_new(ssid_len * 2);
3789 for (i = 0; i < ssid_len; i++)
3790 g_string_append_printf(str,
3793 g_key_file_set_string(keyfile, identifier,
3796 g_string_free(str, TRUE);
3801 case CONNMAN_SERVICE_TYPE_WIMAX:
3802 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3803 case CONNMAN_SERVICE_TYPE_CELLULAR:
3804 if (service->apn != NULL)
3805 g_key_file_set_string(keyfile, service->identifier,
3806 "APN", service->apn);
3808 if (service->username != NULL)
3809 g_key_file_set_string(keyfile, service->identifier,
3810 "Username", service->username);
3812 if (service->password != NULL)
3813 g_key_file_set_string(keyfile, service->identifier,
3814 "Password", service->password);
3816 g_key_file_set_boolean(keyfile, service->identifier,
3817 "Favorite", service->favorite);
3819 if (service->favorite == TRUE)
3820 g_key_file_set_boolean(keyfile, service->identifier,
3821 "AutoConnect", service->autoconnect);
3823 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
3824 const char *failure = error2string(service->error);
3825 if (failure != NULL)
3826 g_key_file_set_string(keyfile,
3827 service->identifier,
3828 "Failure", failure);
3830 g_key_file_remove_key(keyfile, service->identifier,
3836 str = g_time_val_to_iso8601(&service->modified);
3838 g_key_file_set_string(keyfile, service->identifier,
3843 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
3844 g_key_file_set_string(keyfile, service->identifier,
3845 "Passphrase", service->passphrase);
3847 g_key_file_remove_key(keyfile, service->identifier,
3848 "Passphrase", NULL);
3850 if (service->ipconfig != NULL) {
3851 struct connman_ipconfig *ipv6config;
3853 ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
3854 if (ipv6config != NULL)
3855 __connman_ipconfig_save(ipv6config, keyfile,
3856 service->identifier, "IPv6.");
3858 __connman_ipconfig_save(service->ipconfig, keyfile,
3859 service->identifier, "IPv4.");
3862 if (service->nameservers != NULL) {
3863 guint len = g_strv_length(service->nameservers);
3865 g_key_file_set_string_list(keyfile, service->identifier,
3867 (const gchar **) service->nameservers, len);
3869 g_key_file_remove_key(keyfile, service->identifier,
3870 "Nameservers", NULL);
3872 if (service->domains != NULL) {
3873 guint len = g_strv_length(service->domains);
3875 g_key_file_set_string_list(keyfile, service->identifier,
3877 (const gchar **) service->domains, len);
3879 g_key_file_remove_key(keyfile, service->identifier,
3882 __connman_service_stats_save(service, keyfile, service->identifier);
3884 data = g_key_file_to_data(keyfile, &length, NULL);
3886 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
3887 connman_error("Failed to store service information");
3892 g_key_file_free(keyfile);
3899 static struct connman_storage service_storage = {
3901 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
3902 .service_load = service_load,
3903 .service_save = service_save,
3906 int __connman_service_init(void)
3910 connection = connman_dbus_get_connection();
3912 if (connman_storage_register(&service_storage) < 0)
3913 connman_error("Failed to register service storage");
3915 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
3918 service_list = g_sequence_new(service_free);
3923 void __connman_service_cleanup(void)
3927 g_sequence_free(service_list);
3928 service_list = NULL;
3930 g_hash_table_destroy(service_hash);
3931 service_hash = NULL;
3933 connman_storage_unregister(&service_storage);
3935 dbus_connection_unref(connection);