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 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 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 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 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 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_counters(DBusConnection *conn,
1959 DBusMessage *msg, void *user_data)
1961 struct connman_service *service = user_data;
1963 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_counters },
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 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->provider != NULL)
2015 connman_provider_unref(service->provider);
2017 if (service->ipconfig != NULL) {
2018 connman_ipconfig_unref(service->ipconfig);
2019 service->ipconfig = NULL;
2022 if (service->location != NULL)
2023 connman_location_unref(service->location);
2025 g_strfreev(service->nameservers);
2026 g_strfreev(service->domains);
2028 g_free(service->nameserver);
2029 g_free(service->domainname);
2030 g_free(service->mcc);
2031 g_free(service->mnc);
2032 g_free(service->apn);
2033 g_free(service->username);
2034 g_free(service->password);
2035 g_free(service->profile);
2036 g_free(service->name);
2037 g_free(service->passphrase);
2038 g_free(service->identifier);
2039 g_free(service->eap);
2040 g_free(service->identity);
2041 g_free(service->ca_cert_file);
2042 g_free(service->client_cert_file);
2043 g_free(service->private_key_file);
2044 g_free(service->private_key_passphrase);
2045 g_free(service->phase2);
2047 if (service->stats.timer != NULL)
2048 g_timer_destroy(service->stats.timer);
2054 * __connman_service_put:
2055 * @service: service structure
2057 * Release service if no longer needed
2059 void __connman_service_put(struct connman_service *service)
2061 DBG("service %p", service);
2063 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2064 GSequenceIter *iter;
2066 iter = g_hash_table_lookup(service_hash, service->identifier);
2068 reply_pending(service, ECONNABORTED);
2070 __connman_service_disconnect(service);
2072 g_sequence_remove(iter);
2074 service_free(service);
2078 static void service_initialize(struct connman_service *service)
2080 DBG("service %p", service);
2082 service->refcount = 1;
2084 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
2085 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
2086 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
2087 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
2089 service->favorite = FALSE;
2090 service->immutable = FALSE;
2091 service->hidden = FALSE;
2093 service->ignore = FALSE;
2095 service->userconnect = FALSE;
2099 service->stats.valid = FALSE;
2100 service->stats.enabled = FALSE;
2101 service->stats.timer = g_timer_new();
2103 service->provider = NULL;
2107 * connman_service_create:
2109 * Allocate a new service.
2111 * Returns: a newly-allocated #connman_service structure
2113 struct connman_service *connman_service_create(void)
2115 struct connman_service *service;
2117 service = g_try_new0(struct connman_service, 1);
2118 if (service == NULL)
2121 DBG("service %p", service);
2123 service_initialize(service);
2125 service->location = __connman_location_create(service);
2130 struct connman_location *__connman_service_get_location(struct connman_service *service)
2132 return service->location;
2136 * connman_service_ref:
2137 * @service: service structure
2139 * Increase reference counter of service
2141 struct connman_service *connman_service_ref(struct connman_service *service)
2143 g_atomic_int_inc(&service->refcount);
2149 * connman_service_unref:
2150 * @service: service structure
2152 * Decrease reference counter of service
2154 void connman_service_unref(struct connman_service *service)
2156 __connman_service_put(service);
2159 static gint service_compare(gconstpointer a, gconstpointer b,
2162 struct connman_service *service_a = (void *) a;
2163 struct connman_service *service_b = (void *) b;
2165 if (service_a->state != service_b->state) {
2166 if (is_connected(service_a) == TRUE)
2168 if (is_connected(service_b) == TRUE)
2172 if (service_a->order > service_b->order)
2175 if (service_a->order < service_b->order)
2178 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
2181 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
2184 if (service_a->type != service_b->type) {
2185 switch (service_a->type) {
2186 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2187 case CONNMAN_SERVICE_TYPE_SYSTEM:
2188 case CONNMAN_SERVICE_TYPE_ETHERNET:
2189 case CONNMAN_SERVICE_TYPE_GPS:
2190 case CONNMAN_SERVICE_TYPE_VPN:
2192 case CONNMAN_SERVICE_TYPE_WIFI:
2194 case CONNMAN_SERVICE_TYPE_WIMAX:
2195 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2196 case CONNMAN_SERVICE_TYPE_CELLULAR:
2201 return (gint) service_b->strength - (gint) service_a->strength;
2205 * connman_service_get_type:
2206 * @service: service structure
2208 * Get the type of service
2210 enum connman_service_type connman_service_get_type(struct connman_service *service)
2212 if (service == NULL)
2213 return CONNMAN_SERVICE_TYPE_UNKNOWN;
2215 return service->type;
2219 * connman_service_get_interface:
2220 * @service: service structure
2222 * Get network interface of service
2224 char *connman_service_get_interface(struct connman_service *service)
2228 if (service == NULL)
2231 if (service->network == NULL)
2234 index = connman_network_get_index(service->network);
2236 return connman_inet_ifname(index);
2240 * connman_service_get_network:
2241 * @service: service structure
2243 * Get the service network
2245 struct connman_network *
2246 __connman_service_get_network(struct connman_service *service)
2248 if (service == NULL)
2251 return service->network;
2254 struct connman_ipconfig *__connman_service_get_ipconfig(struct connman_service *service)
2256 if (service == NULL)
2259 return service->ipconfig;
2263 * __connman_service_set_favorite:
2264 * @service: service structure
2265 * @favorite: favorite value
2267 * Change the favorite setting of service
2269 int __connman_service_set_favorite(struct connman_service *service,
2270 connman_bool_t favorite)
2272 GSequenceIter *iter;
2274 iter = g_hash_table_lookup(service_hash, service->identifier);
2278 if (service->favorite == favorite)
2281 service->favorite = favorite;
2283 favorite_changed(service);
2285 g_sequence_sort_changed(iter, service_compare, NULL);
2287 __connman_profile_changed(FALSE);
2292 int __connman_service_set_immutable(struct connman_service *service,
2293 connman_bool_t immutable)
2295 service->immutable = immutable;
2297 immutable_changed(service);
2302 void __connman_service_set_string(struct connman_service *service,
2303 const char *key, const char *value)
2305 if (g_str_equal(key, "EAP") == TRUE) {
2306 g_free(service->eap);
2307 service->eap = g_strdup(value);
2308 } else if (g_str_equal(key, "Identity") == TRUE) {
2309 g_free(service->identity);
2310 service->identity = g_strdup(value);
2311 } else if (g_str_equal(key, "CACertFile") == TRUE) {
2312 g_free(service->ca_cert_file);
2313 service->ca_cert_file = g_strdup(value);
2314 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
2315 g_free(service->client_cert_file);
2316 service->client_cert_file = g_strdup(value);
2317 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
2318 g_free(service->private_key_file);
2319 service->private_key_file = g_strdup(value);
2320 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
2321 g_free(service->private_key_passphrase);
2322 service->private_key_passphrase = g_strdup(value);
2323 } else if (g_str_equal(key, "Phase2") == TRUE) {
2324 g_free(service->phase2);
2325 service->phase2 = g_strdup(value);
2326 } else if (g_str_equal(key, "Passphrase") == TRUE) {
2327 g_free(service->passphrase);
2328 service->passphrase = g_strdup(value);
2332 int __connman_service_indicate_state(struct connman_service *service,
2333 enum connman_service_state state)
2335 GSequenceIter *iter;
2337 DBG("service %p state %d", service, state);
2339 if (service == NULL)
2342 if (service->state == state)
2345 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
2346 state == CONNMAN_SERVICE_STATE_IDLE)
2349 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
2350 state == CONNMAN_SERVICE_STATE_DISCONNECT)
2353 if (state == CONNMAN_SERVICE_STATE_IDLE &&
2354 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
2355 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
2356 state_changed(service);
2358 reply_pending(service, ECONNABORTED);
2360 __connman_service_disconnect(service);
2363 if (state == CONNMAN_SERVICE_STATE_CONFIGURATION)
2364 __connman_ipconfig_enable(service->ipconfig);
2366 service->state = state;
2367 state_changed(service);
2369 if (state == CONNMAN_SERVICE_STATE_ONLINE) {
2370 if (service->login_required == TRUE) {
2371 service->login_required = FALSE;
2372 login_changed(service);
2375 connman_timeserver_sync();
2378 if (state == CONNMAN_SERVICE_STATE_IDLE) {
2379 connman_bool_t reconnect;
2381 reconnect = get_reconnect_state(service);
2382 if (reconnect == TRUE)
2383 __connman_service_auto_connect();
2386 if (state == CONNMAN_SERVICE_STATE_READY) {
2387 set_reconnect_state(service, TRUE);
2389 __connman_service_set_favorite(service, TRUE);
2391 reply_pending(service, 0);
2393 service->userconnect = FALSE;
2395 g_get_current_time(&service->modified);
2396 __connman_storage_save_service(service);
2398 update_nameservers(service);
2399 dns_changed(service);
2401 __connman_wpad_start(service);
2403 __connman_notifier_connect(service->type);
2406 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
2407 __connman_location_finish(service);
2411 __connman_wpad_stop(service);
2413 update_nameservers(service);
2414 dns_changed(service);
2416 __connman_notifier_disconnect(service->type);
2419 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
2420 reply_pending(service, EIO);
2422 if (service->userconnect == FALSE)
2423 __connman_service_auto_connect();
2425 g_get_current_time(&service->modified);
2426 __connman_storage_save_service(service);
2428 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2430 iter = g_hash_table_lookup(service_hash, service->identifier);
2432 g_sequence_sort_changed(iter, service_compare, NULL);
2434 __connman_profile_changed(FALSE);
2436 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
2437 service->state == CONNMAN_SERVICE_STATE_FAILURE)
2438 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
2443 int __connman_service_indicate_error(struct connman_service *service,
2444 enum connman_service_error error)
2446 DBG("service %p error %d", service, error);
2448 if (service == NULL)
2451 service->error = error;
2453 return __connman_service_indicate_state(service,
2454 CONNMAN_SERVICE_STATE_FAILURE);
2457 int __connman_service_indicate_default(struct connman_service *service)
2459 DBG("service %p", service);
2463 __connman_location_detect(service);
2468 int __connman_service_request_login(struct connman_service *service)
2470 DBG("service %p", service);
2472 if (service == NULL)
2475 service->login_required = TRUE;
2476 login_changed(service);
2481 static connman_bool_t prepare_network(struct connman_service *service)
2483 enum connman_network_type type;
2484 unsigned int ssid_len;
2486 type = connman_network_get_type(service->network);
2489 case CONNMAN_NETWORK_TYPE_UNKNOWN:
2490 case CONNMAN_NETWORK_TYPE_VENDOR:
2492 case CONNMAN_NETWORK_TYPE_WIFI:
2493 if (connman_network_get_blob(service->network, "WiFi.SSID",
2497 connman_network_set_string(service->network,
2498 "WiFi.Passphrase", service->passphrase);
2500 case CONNMAN_NETWORK_TYPE_ETHERNET:
2501 case CONNMAN_NETWORK_TYPE_WIMAX:
2502 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
2503 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
2505 case CONNMAN_NETWORK_TYPE_CELLULAR:
2506 connman_network_set_string(service->network,
2507 "Cellular.APN", service->apn);
2509 connman_network_set_string(service->network,
2510 "Cellular.Username", service->username);
2511 connman_network_set_string(service->network,
2512 "Cellular.Password", service->password);
2519 static void prepare_8021x(struct connman_service *service)
2521 if (service->eap != NULL)
2522 connman_network_set_string(service->network, "WiFi.EAP",
2525 if (service->identity != NULL)
2526 connman_network_set_string(service->network, "WiFi.Identity",
2529 if (service->ca_cert_file != NULL)
2530 connman_network_set_string(service->network, "WiFi.CACertFile",
2531 service->ca_cert_file);
2533 if (service->client_cert_file != NULL)
2534 connman_network_set_string(service->network,
2535 "WiFi.ClientCertFile",
2536 service->client_cert_file);
2538 if (service->private_key_file != NULL)
2539 connman_network_set_string(service->network,
2540 "WiFi.PrivateKeyFile",
2541 service->private_key_file);
2543 if (service->private_key_passphrase != NULL)
2544 connman_network_set_string(service->network,
2545 "WiFi.PrivateKeyPassphrase",
2546 service->private_key_passphrase);
2548 if (service->phase2 != NULL)
2549 connman_network_set_string(service->network, "WiFi.Phase2",
2553 int __connman_service_connect(struct connman_service *service)
2557 DBG("service %p", service);
2559 if (is_connected(service) == TRUE)
2562 if (is_connecting(service) == TRUE)
2565 switch (service->type) {
2566 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2567 case CONNMAN_SERVICE_TYPE_SYSTEM:
2568 case CONNMAN_SERVICE_TYPE_GPS:
2570 case CONNMAN_SERVICE_TYPE_ETHERNET:
2571 case CONNMAN_SERVICE_TYPE_WIMAX:
2572 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2573 case CONNMAN_SERVICE_TYPE_VPN:
2575 case CONNMAN_SERVICE_TYPE_CELLULAR:
2576 if (service->apn == NULL)
2579 case CONNMAN_SERVICE_TYPE_WIFI:
2580 switch (service->security) {
2581 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2582 case CONNMAN_SERVICE_SECURITY_NONE:
2584 case CONNMAN_SERVICE_SECURITY_WEP:
2585 case CONNMAN_SERVICE_SECURITY_PSK:
2586 case CONNMAN_SERVICE_SECURITY_WPA:
2587 case CONNMAN_SERVICE_SECURITY_RSN:
2588 if (service->passphrase == NULL)
2591 case CONNMAN_SERVICE_SECURITY_8021X:
2597 if (service->network != NULL) {
2598 if (prepare_network(service) == FALSE)
2601 switch (service->security) {
2602 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2603 case CONNMAN_SERVICE_SECURITY_NONE:
2604 case CONNMAN_SERVICE_SECURITY_WEP:
2605 case CONNMAN_SERVICE_SECURITY_PSK:
2606 case CONNMAN_SERVICE_SECURITY_WPA:
2607 case CONNMAN_SERVICE_SECURITY_RSN:
2609 case CONNMAN_SERVICE_SECURITY_8021X:
2610 prepare_8021x(service);
2614 __connman_ipconfig_enable(service->ipconfig);
2616 err = __connman_network_connect(service->network);
2617 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
2618 service->provider != NULL)
2619 err = __connman_provider_connect(service->provider);
2624 if (err != -EINPROGRESS) {
2625 __connman_ipconfig_disable(service->ipconfig);
2629 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
2630 connect_timeout, service);
2632 return -EINPROGRESS;
2638 int __connman_service_disconnect(struct connman_service *service)
2640 struct connman_ipconfig *ipv6config;
2643 DBG("service %p", service);
2645 if (service->network != NULL) {
2646 err = __connman_network_disconnect(service->network);
2647 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
2648 service->provider != NULL)
2649 err = __connman_provider_disconnect(service->provider);
2653 __connman_ipconfig_clear_address(service->ipconfig);
2655 ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
2657 __connman_ipconfig_clear_address(ipv6config);
2659 __connman_ipconfig_disable(service->ipconfig);
2662 if (err != -EINPROGRESS)
2665 return -EINPROGRESS;
2672 * __connman_service_lookup:
2673 * @pattern: search pattern
2674 * @path: return object path
2676 * Look up a service path from a search pattern
2678 int __connman_service_lookup(const char *pattern, const char **path)
2680 GHashTableIter iter;
2681 gpointer key, value;
2683 g_hash_table_iter_init(&iter, service_hash);
2685 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
2686 GSequenceIter *iter = value;
2687 struct connman_service *service = g_sequence_get(iter);
2689 if (g_strcmp0(service->identifier, pattern) == 0 ||
2690 g_strcmp0(service->name, pattern) == 0) {
2691 *path = (const char *) service->path;
2700 * lookup_by_identifier:
2701 * @identifier: service identifier
2703 * Look up a service by identifier (reference count will not be increased)
2705 static struct connman_service *lookup_by_identifier(const char *identifier)
2707 GSequenceIter *iter;
2709 iter = g_hash_table_lookup(service_hash, identifier);
2711 return g_sequence_get(iter);
2716 static struct connman_network *create_hidden_wifi(struct connman_device *device,
2717 const char *ssid, const char *mode, const char *security)
2719 struct connman_network *network;
2722 unsigned int i, ssid_len;
2724 ssid_len = strlen(ssid);
2728 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
2729 if (network == NULL)
2732 connman_network_set_blob(network, "WiFi.SSID",
2733 (unsigned char *) ssid, ssid_len);
2735 connman_network_set_string(network, "WiFi.Mode", mode);
2736 connman_network_set_string(network, "WiFi.Security", security);
2738 name = g_try_malloc0(ssid_len + 1);
2740 connman_network_unref(network);
2744 for (i = 0; i < ssid_len; i++) {
2745 if (g_ascii_isprint(ssid[i]))
2751 connman_network_set_name(network, name);
2755 index = connman_device_get_index(device);
2756 connman_network_set_index(network, index);
2758 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
2760 if (connman_device_add_network(device, network) < 0) {
2761 connman_network_unref(network);
2765 connman_network_set_available(network, TRUE);
2770 int __connman_service_create_and_connect(DBusMessage *msg)
2772 struct connman_service *service;
2773 struct connman_network *network;
2774 struct connman_device *device;
2775 DBusMessageIter iter, array;
2776 const char *mode = "managed", *security = "none", *group_security;
2777 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
2778 unsigned int ssid_len = 0;
2781 gboolean created = FALSE;
2784 dbus_message_iter_init(msg, &iter);
2785 dbus_message_iter_recurse(&iter, &array);
2787 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
2788 DBusMessageIter entry, value;
2791 dbus_message_iter_recurse(&array, &entry);
2792 dbus_message_iter_get_basic(&entry, &key);
2794 dbus_message_iter_next(&entry);
2795 dbus_message_iter_recurse(&entry, &value);
2797 switch (dbus_message_iter_get_arg_type(&value)) {
2798 case DBUS_TYPE_STRING:
2799 if (g_str_equal(key, "Type") == TRUE)
2800 dbus_message_iter_get_basic(&value, &type);
2801 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
2802 g_str_equal(key, "Mode") == TRUE)
2803 dbus_message_iter_get_basic(&value, &mode);
2804 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
2805 g_str_equal(key, "Security") == TRUE)
2806 dbus_message_iter_get_basic(&value, &security);
2807 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
2808 g_str_equal(key, "Passphrase") == TRUE)
2809 dbus_message_iter_get_basic(&value, &passphrase);
2810 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
2811 g_str_equal(key, "SSID") == TRUE)
2812 dbus_message_iter_get_basic(&value, &ssid);
2815 dbus_message_iter_next(&array);
2821 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
2827 ssid_len = strlen(ssid);
2831 if (g_strcmp0(security, "none") != 0 &&
2832 g_strcmp0(security, "wep") != 0 &&
2833 g_strcmp0(security, "psk") != 0 &&
2834 g_strcmp0(security, "wpa") != 0 &&
2835 g_strcmp0(security, "rsn") != 0 &&
2836 g_strcmp0(security, "ieee8021x") != 0)
2839 device = __connman_element_find_device(CONNMAN_DEVICE_TYPE_WIFI);
2843 ident = __connman_device_get_ident(device);
2848 if (!g_strcmp0(security, "wpa") ||
2849 !g_strcmp0(security, "rsn"))
2850 group_security = "psk";
2852 group_security = security;
2854 group = connman_wifi_build_group_name((unsigned char *) ssid,
2855 ssid_len, mode, group_security);
2859 name = g_strdup_printf("%s_%s_%s", type, ident, group);
2861 service = lookup_by_identifier(name);
2863 if (service != NULL)
2866 network = create_hidden_wifi(device, ssid, mode, security);
2867 if (network != NULL) {
2868 connman_network_set_group(network, group);
2872 service = lookup_by_identifier(name);
2878 if (service == NULL) {
2883 set_reconnect_state(service, FALSE);
2885 __connman_device_disconnect(device);
2887 if (passphrase != NULL) {
2888 g_free(service->passphrase);
2889 service->passphrase = g_strdup(passphrase);
2892 service->userconnect = TRUE;
2894 err = __connman_service_connect(service);
2895 if (err < 0 && err != -EINPROGRESS)
2898 g_dbus_send_reply(connection, msg,
2899 DBUS_TYPE_OBJECT_PATH, &service->path,
2905 if (service != NULL && created == TRUE) {
2906 struct connman_network *network = service->network;
2908 if (network != NULL) {
2909 connman_network_set_available(network, FALSE);
2910 __connman_device_cleanup_networks(device);
2912 __connman_service_put(service);
2919 * __connman_service_get:
2920 * @identifier: service identifier
2922 * Look up a service by identifier or create a new one if not found
2924 static struct connman_service *service_get(const char *identifier)
2926 struct connman_service *service;
2927 GSequenceIter *iter;
2929 iter = g_hash_table_lookup(service_hash, identifier);
2931 service = g_sequence_get(iter);
2932 if (service != NULL)
2933 g_atomic_int_inc(&service->refcount);
2937 service = connman_service_create();
2938 if (service == NULL)
2941 DBG("service %p", service);
2943 service->identifier = g_strdup(identifier);
2945 service->profile = g_strdup(__connman_profile_active_ident());
2947 iter = g_sequence_insert_sorted(service_list, service,
2948 service_compare, NULL);
2950 g_hash_table_insert(service_hash, service->identifier, iter);
2955 static int service_register(struct connman_service *service)
2957 const char *path = __connman_profile_active_path();
2958 GSequenceIter *iter;
2960 DBG("service %p", service);
2962 if (service->path != NULL)
2965 service->path = g_strdup_printf("%s/%s", path, service->identifier);
2967 DBG("path %s", service->path);
2969 __connman_config_provision_service(service);
2971 __connman_storage_load_service(service);
2973 g_dbus_register_interface(connection, service->path,
2974 CONNMAN_SERVICE_INTERFACE,
2975 service_methods, service_signals,
2976 NULL, service, NULL);
2978 iter = g_hash_table_lookup(service_hash, service->identifier);
2980 g_sequence_sort_changed(iter, service_compare, NULL);
2982 __connman_profile_changed(TRUE);
2987 static void service_up(struct connman_ipconfig *ipconfig)
2989 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
2991 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
2993 service->stats.valid = FALSE;
2996 static void service_down(struct connman_ipconfig *ipconfig)
2998 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
3001 static void service_lower_up(struct connman_ipconfig *ipconfig)
3003 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3005 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
3007 stats_start(service);
3010 static void service_lower_down(struct connman_ipconfig *ipconfig)
3012 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3014 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
3016 stats_stop(service);
3017 __connman_storage_save_service(service);
3020 static void service_ip_bound(struct connman_ipconfig *ipconfig)
3022 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3024 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
3026 settings_changed(service);
3029 static void service_ip_release(struct connman_ipconfig *ipconfig)
3031 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3033 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
3035 settings_changed(service);
3038 static const struct connman_ipconfig_ops service_ops = {
3040 .down = service_down,
3041 .lower_up = service_lower_up,
3042 .lower_down = service_lower_down,
3043 .ip_bound = service_ip_bound,
3044 .ip_release = service_ip_release,
3047 static void setup_ipconfig(struct connman_service *service, int index)
3052 service->ipconfig = connman_ipconfig_create(index);
3053 if (service->ipconfig == NULL)
3056 connman_ipconfig_set_method(service->ipconfig,
3057 CONNMAN_IPCONFIG_METHOD_DHCP);
3059 connman_ipconfig_set_data(service->ipconfig, service);
3061 connman_ipconfig_set_ops(service->ipconfig, &service_ops);
3064 void __connman_service_create_ipconfig(struct connman_service *service,
3067 struct connman_ipconfig *ipv6config;
3068 const char *ident = service->profile;
3071 if (service->ipconfig != NULL)
3074 setup_ipconfig(service, index);
3079 keyfile = __connman_storage_open_profile(ident);
3080 if (keyfile == NULL)
3084 ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
3085 if (ipv6config != NULL)
3086 __connman_ipconfig_load(ipv6config, keyfile,
3087 service->identifier, "IPv6.");
3089 __connman_ipconfig_load(service->ipconfig, keyfile,
3090 service->identifier, "IPv4.");
3091 g_key_file_free(keyfile);
3095 * __connman_service_lookup_from_network:
3096 * @network: network structure
3098 * Look up a service by network (reference count will not be increased)
3100 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
3102 struct connman_service *service;
3103 const char *ident, *group;
3106 DBG("network %p", network);
3108 ident = __connman_network_get_ident(network);
3112 group = connman_network_get_group(network);
3116 name = g_strdup_printf("%s_%s_%s",
3117 __connman_network_get_type(network), ident, group);
3118 service = lookup_by_identifier(name);
3124 const char *__connman_service_get_path(struct connman_service *service)
3126 return service->path;
3129 unsigned int __connman_service_get_order(struct connman_service *service)
3131 GSequenceIter *iter;
3133 if (service == NULL)
3136 if (service->favorite == FALSE) {
3141 iter = g_hash_table_lookup(service_hash, service->identifier);
3143 if (g_sequence_iter_get_position(iter) == 0)
3150 return service->order;
3153 static enum connman_service_type convert_network_type(struct connman_network *network)
3155 enum connman_network_type type = connman_network_get_type(network);
3158 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3159 case CONNMAN_NETWORK_TYPE_VENDOR:
3161 case CONNMAN_NETWORK_TYPE_ETHERNET:
3162 return CONNMAN_SERVICE_TYPE_ETHERNET;
3163 case CONNMAN_NETWORK_TYPE_WIFI:
3164 return CONNMAN_SERVICE_TYPE_WIFI;
3165 case CONNMAN_NETWORK_TYPE_WIMAX:
3166 return CONNMAN_SERVICE_TYPE_WIMAX;
3167 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3168 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3169 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
3170 case CONNMAN_NETWORK_TYPE_CELLULAR:
3171 return CONNMAN_SERVICE_TYPE_CELLULAR;
3174 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3177 static enum connman_service_mode convert_wifi_mode(const char *mode)
3180 return CONNMAN_SERVICE_MODE_UNKNOWN;
3181 else if (g_str_equal(mode, "managed") == TRUE)
3182 return CONNMAN_SERVICE_MODE_MANAGED;
3183 else if (g_str_equal(mode, "adhoc") == TRUE)
3184 return CONNMAN_SERVICE_MODE_ADHOC;
3186 return CONNMAN_SERVICE_MODE_UNKNOWN;
3189 static enum connman_service_security convert_wifi_security(const char *security)
3191 if (security == NULL)
3192 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3193 else if (g_str_equal(security, "none") == TRUE)
3194 return CONNMAN_SERVICE_SECURITY_NONE;
3195 else if (g_str_equal(security, "wep") == TRUE)
3196 return CONNMAN_SERVICE_SECURITY_WEP;
3197 else if (g_str_equal(security, "psk") == TRUE)
3198 return CONNMAN_SERVICE_SECURITY_PSK;
3199 else if (g_str_equal(security, "ieee8021x") == TRUE)
3200 return CONNMAN_SERVICE_SECURITY_8021X;
3201 else if (g_str_equal(security, "wpa") == TRUE)
3202 return CONNMAN_SERVICE_SECURITY_WPA;
3203 else if (g_str_equal(security, "rsn") == TRUE)
3204 return CONNMAN_SERVICE_SECURITY_RSN;
3206 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3209 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
3214 return CONNMAN_SERVICE_MODE_GPRS;
3216 return CONNMAN_SERVICE_MODE_EDGE;
3221 return CONNMAN_SERVICE_MODE_UMTS;
3224 return CONNMAN_SERVICE_MODE_UNKNOWN;
3227 static void update_from_network(struct connman_service *service,
3228 struct connman_network *network)
3230 connman_uint8_t strength = service->strength;
3231 GSequenceIter *iter;
3234 DBG("service %p network %p", service, network);
3236 if (is_connected(service) == TRUE)
3239 if (is_connecting(service) == TRUE)
3242 str = connman_network_get_string(network, "Name");
3244 g_free(service->name);
3245 service->name = g_strdup(str);
3246 service->hidden = FALSE;
3248 g_free(service->name);
3249 service->name = NULL;
3250 service->hidden = TRUE;
3253 service->strength = connman_network_get_uint8(network, "Strength");
3254 service->roaming = connman_network_get_bool(network, "Roaming");
3256 if (service->strength == 0) {
3258 * Filter out 0-values; it's unclear what they mean
3259 * and they cause anomalous sorting of the priority list.
3261 service->strength = strength;
3264 str = connman_network_get_string(network, "WiFi.Mode");
3265 service->mode = convert_wifi_mode(str);
3267 str = connman_network_get_string(network, "WiFi.Security");
3268 service->security = convert_wifi_security(str);
3270 str = connman_network_get_string(network, "Cellular.MCC");
3271 g_free(service->mcc);
3272 service->mcc = g_strdup(str);
3274 str = connman_network_get_string(network, "Cellular.MNC");
3275 g_free(service->mnc);
3276 service->mnc = g_strdup(str);
3278 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
3279 connman_uint8_t value = connman_network_get_uint8(network,
3282 service->mode = convert_cellular_mode(value);
3285 if (service->strength > strength && service->network != NULL) {
3286 connman_network_unref(service->network);
3287 service->network = connman_network_ref(network);
3289 strength_changed(service);
3292 if (service->network == NULL)
3293 service->network = connman_network_ref(network);
3295 iter = g_hash_table_lookup(service_hash, service->identifier);
3297 g_sequence_sort_changed(iter, service_compare, NULL);
3301 * __connman_service_create_from_network:
3302 * @network: network structure
3304 * Look up service by network and if not found, create one
3306 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
3308 struct connman_service *service;
3309 const char *ident, *group;
3312 DBG("network %p", network);
3314 ident = __connman_network_get_ident(network);
3318 group = connman_network_get_group(network);
3322 name = g_strdup_printf("%s_%s_%s",
3323 __connman_network_get_type(network), ident, group);
3324 service = service_get(name);
3327 if (service == NULL)
3330 if (__connman_network_get_weakness(network) == TRUE)
3333 if (service->path != NULL) {
3334 update_from_network(service, network);
3335 __connman_profile_changed(TRUE);
3339 service->type = convert_network_type(network);
3341 switch (service->type) {
3342 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3343 case CONNMAN_SERVICE_TYPE_SYSTEM:
3344 case CONNMAN_SERVICE_TYPE_ETHERNET:
3345 case CONNMAN_SERVICE_TYPE_WIMAX:
3346 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3347 case CONNMAN_SERVICE_TYPE_GPS:
3348 case CONNMAN_SERVICE_TYPE_VPN:
3349 service->autoconnect = FALSE;
3351 case CONNMAN_SERVICE_TYPE_WIFI:
3352 case CONNMAN_SERVICE_TYPE_CELLULAR:
3353 service->autoconnect = TRUE;
3357 service->state = CONNMAN_SERVICE_STATE_IDLE;
3359 update_from_network(service, network);
3361 setup_ipconfig(service, connman_network_get_index(network));
3363 service_register(service);
3365 if (service->favorite == TRUE)
3366 __connman_service_auto_connect();
3371 void __connman_service_update_from_network(struct connman_network *network)
3373 struct connman_service *service;
3374 enum connman_service_mode mode;
3375 connman_uint8_t strength, value;
3376 connman_bool_t roaming;
3377 GSequenceIter *iter;
3380 DBG("network %p", network);
3382 service = __connman_service_lookup_from_network(network);
3383 if (service == NULL)
3386 if (service->network == NULL)
3389 name = connman_network_get_string(service->network, "Name");
3390 if (g_strcmp0(service->name, name) != 0) {
3391 g_free(service->name);
3392 service->name = g_strdup(name);
3393 connman_dbus_property_changed_basic(service->path,
3394 CONNMAN_SERVICE_INTERFACE, "Name",
3395 DBUS_TYPE_STRING, &service->name);
3398 strength = connman_network_get_uint8(service->network, "Strength");
3399 if (strength == service->strength)
3402 service->strength = strength;
3404 strength_changed(service);
3407 roaming = connman_network_get_bool(service->network, "Roaming");
3408 if (roaming == service->roaming)
3411 service->roaming = roaming;
3413 roaming_changed(service);
3415 iter = g_hash_table_lookup(service_hash, service->identifier);
3417 g_sequence_sort_changed(iter, service_compare, NULL);
3420 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
3423 value = connman_network_get_uint8(service->network, "Cellular.Mode");
3424 mode = convert_cellular_mode(value);
3426 if (mode == service->mode)
3429 service->mode = mode;
3431 mode_changed(service);
3434 void __connman_service_remove_from_network(struct connman_network *network)
3436 struct connman_service *service;
3438 DBG("network %p", network);
3440 service = __connman_service_lookup_from_network(network);
3441 if (service == NULL)
3444 __connman_service_put(service);
3448 * __connman_service_create_from_provider:
3449 * @provider: provider structure
3451 * Look up service by provider and if not found, create one
3453 struct connman_service *
3454 __connman_service_create_from_provider(struct connman_provider *provider)
3456 struct connman_service *service;
3457 const char *ident, *str;
3459 int index = connman_provider_get_index(provider);
3461 DBG("provider %p", provider);
3463 ident = __connman_provider_get_ident(provider);
3467 name = g_strdup_printf("vpn_%s", ident);
3468 service = service_get(name);
3471 if (service == NULL)
3474 service->type = CONNMAN_SERVICE_TYPE_VPN;
3475 service->provider = connman_provider_ref(provider);
3476 service->autoconnect = FALSE;
3478 service->state = CONNMAN_SERVICE_STATE_IDLE;
3480 str = connman_provider_get_string(provider, "Name");
3482 g_free(service->name);
3483 service->name = g_strdup(str);
3484 service->hidden = FALSE;
3486 g_free(service->name);
3487 service->name = NULL;
3488 service->hidden = TRUE;
3491 service->strength = 0;
3493 service->ipconfig = connman_ipconfig_create(index);
3494 if (service->ipconfig == NULL)
3497 connman_ipconfig_set_method(service->ipconfig,
3498 CONNMAN_IPCONFIG_METHOD_MANUAL);
3500 connman_ipconfig_set_data(service->ipconfig, service);
3502 connman_ipconfig_set_ops(service->ipconfig, &service_ops);
3504 service_register(service);
3509 void __connman_service_stats_update(struct connman_service *service,
3510 unsigned int rx_packets, unsigned int tx_packets,
3511 unsigned int rx_bytes, unsigned int tx_bytes,
3512 unsigned int rx_errors, unsigned int tx_errors,
3513 unsigned int rx_dropped, unsigned int tx_dropped)
3515 unsigned int seconds;
3516 struct connman_stats *stats = &service->stats;
3518 DBG("service %p", service);
3520 if (is_connected(service) == FALSE)
3523 if (stats->valid == TRUE) {
3524 stats->rx_packets +=
3525 rx_packets - stats->rx_packets_last;
3526 stats->tx_packets +=
3527 tx_packets - stats->tx_packets_last;
3529 rx_bytes - stats->rx_bytes_last;
3531 tx_bytes - stats->tx_bytes_last;
3533 rx_errors - stats->rx_errors_last;
3535 tx_errors - stats->tx_errors_last;
3536 stats->rx_dropped +=
3537 rx_dropped - stats->rx_dropped_last;
3538 stats->tx_dropped +=
3539 tx_dropped - stats->tx_dropped_last;
3541 stats->valid = TRUE;
3544 stats->rx_packets_last = rx_packets;
3545 stats->tx_packets_last = tx_packets;
3546 stats->rx_bytes_last = rx_bytes;
3547 stats->tx_bytes_last = tx_bytes;
3548 stats->rx_errors_last = rx_errors;
3549 stats->tx_errors_last = tx_errors;
3550 stats->rx_dropped_last = rx_dropped;
3551 stats->tx_dropped_last = tx_dropped;
3553 seconds = g_timer_elapsed(stats->timer, NULL);
3554 stats->time = stats->time_start + seconds;
3557 static int service_load(struct connman_service *service)
3559 const char *ident = service->profile;
3561 GError *error = NULL;
3562 gchar *pathname, *data = NULL;
3565 connman_bool_t autoconnect;
3566 unsigned int ssid_len;
3569 DBG("service %p", service);
3574 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
3575 if (pathname == NULL)
3578 keyfile = g_key_file_new();
3580 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
3587 if (g_key_file_load_from_data(keyfile, data, length,
3588 0, NULL) == FALSE) {
3595 switch (service->type) {
3596 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3597 case CONNMAN_SERVICE_TYPE_SYSTEM:
3598 case CONNMAN_SERVICE_TYPE_ETHERNET:
3599 case CONNMAN_SERVICE_TYPE_GPS:
3600 case CONNMAN_SERVICE_TYPE_VPN:
3602 case CONNMAN_SERVICE_TYPE_WIFI:
3603 if (service->name == NULL) {
3606 name = g_key_file_get_string(keyfile,
3607 service->identifier, "Name", NULL);
3609 g_free(service->name);
3610 service->name = name;
3613 if (service->network != NULL)
3614 connman_network_set_name(service->network,
3618 if (service->network &&
3619 connman_network_get_blob(service->network,
3620 "WiFi.SSID", &ssid_len) == NULL) {
3623 hex_ssid = g_key_file_get_string(keyfile,
3624 service->identifier,
3627 if (hex_ssid != NULL) {
3629 unsigned int i, j = 0, hex;
3630 size_t hex_ssid_len = strlen(hex_ssid);
3632 ssid = g_try_malloc0(hex_ssid_len / 2);
3639 for (i = 0; i < hex_ssid_len; i += 2) {
3640 sscanf(hex_ssid + i, "%02x", &hex);
3644 connman_network_set_blob(service->network,
3645 "WiFi.SSID", ssid, hex_ssid_len / 2);
3652 case CONNMAN_SERVICE_TYPE_WIMAX:
3653 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3654 case CONNMAN_SERVICE_TYPE_CELLULAR:
3655 service->apn = g_key_file_get_string(keyfile,
3656 service->identifier, "APN", NULL);
3658 service->username = g_key_file_get_string(keyfile,
3659 service->identifier, "Username", NULL);
3661 service->password = g_key_file_get_string(keyfile,
3662 service->identifier, "Password", NULL);
3664 service->favorite = g_key_file_get_boolean(keyfile,
3665 service->identifier, "Favorite", NULL);
3667 autoconnect = g_key_file_get_boolean(keyfile,
3668 service->identifier, "AutoConnect", &error);
3670 service->autoconnect = autoconnect;
3671 g_clear_error(&error);
3673 str = g_key_file_get_string(keyfile,
3674 service->identifier, "Failure", NULL);
3676 service->state = CONNMAN_SERVICE_STATE_FAILURE;
3677 service->error = string2error(str);
3682 str = g_key_file_get_string(keyfile,
3683 service->identifier, "Modified", NULL);
3685 g_time_val_from_iso8601(str, &service->modified);
3689 str = g_key_file_get_string(keyfile,
3690 service->identifier, "Passphrase", NULL);
3692 g_free(service->passphrase);
3693 service->passphrase = str;
3696 if (service->ipconfig != NULL) {
3697 struct connman_ipconfig *ipv6config;
3699 ipv6config = connman_ipconfig_get_ipv6config(
3701 if (ipv6config != NULL)
3702 __connman_ipconfig_load(ipv6config, keyfile,
3703 service->identifier, "IPv6.");
3705 __connman_ipconfig_load(service->ipconfig, keyfile,
3706 service->identifier, "IPv4.");
3709 service->nameservers = g_key_file_get_string_list(keyfile,
3710 service->identifier, "Nameservers", &length, NULL);
3711 if (service->nameservers != NULL && length == 0) {
3712 g_strfreev(service->nameservers);
3713 service->nameservers = NULL;
3716 service->domains = g_key_file_get_string_list(keyfile,
3717 service->identifier, "Domains", &length, NULL);
3718 if (service->domains != NULL && length == 0) {
3719 g_strfreev(service->domains);
3720 service->domains = NULL;
3723 stats_load(service, keyfile, service->identifier);
3725 g_key_file_free(keyfile);
3730 static int service_save(struct connman_service *service)
3732 const char *ident = service->profile;
3734 gchar *pathname, *data = NULL;
3739 DBG("service %p", service);
3744 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
3745 if (pathname == NULL)
3748 keyfile = g_key_file_new();
3750 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
3754 if (g_key_file_load_from_data(keyfile, data, length,
3762 if (service->name != NULL)
3763 g_key_file_set_string(keyfile, service->identifier,
3764 "Name", service->name);
3766 switch (service->type) {
3767 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3768 case CONNMAN_SERVICE_TYPE_SYSTEM:
3769 case CONNMAN_SERVICE_TYPE_ETHERNET:
3770 case CONNMAN_SERVICE_TYPE_GPS:
3771 case CONNMAN_SERVICE_TYPE_VPN:
3773 case CONNMAN_SERVICE_TYPE_WIFI:
3774 if (service->network) {
3775 const unsigned char *ssid;
3776 unsigned int ssid_len = 0;
3778 ssid = connman_network_get_blob(service->network,
3779 "WiFi.SSID", &ssid_len);
3781 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
3782 char *identifier = service->identifier;
3786 str = g_string_sized_new(ssid_len * 2);
3792 for (i = 0; i < ssid_len; i++)
3793 g_string_append_printf(str,
3796 g_key_file_set_string(keyfile, identifier,
3799 g_string_free(str, TRUE);
3804 case CONNMAN_SERVICE_TYPE_WIMAX:
3805 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3806 case CONNMAN_SERVICE_TYPE_CELLULAR:
3807 if (service->apn != NULL)
3808 g_key_file_set_string(keyfile, service->identifier,
3809 "APN", service->apn);
3811 if (service->username != NULL)
3812 g_key_file_set_string(keyfile, service->identifier,
3813 "Username", service->username);
3815 if (service->password != NULL)
3816 g_key_file_set_string(keyfile, service->identifier,
3817 "Password", service->password);
3819 g_key_file_set_boolean(keyfile, service->identifier,
3820 "Favorite", service->favorite);
3822 if (service->favorite == TRUE)
3823 g_key_file_set_boolean(keyfile, service->identifier,
3824 "AutoConnect", service->autoconnect);
3826 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
3827 const char *failure = error2string(service->error);
3828 if (failure != NULL)
3829 g_key_file_set_string(keyfile,
3830 service->identifier,
3831 "Failure", failure);
3833 g_key_file_remove_key(keyfile, service->identifier,
3839 str = g_time_val_to_iso8601(&service->modified);
3841 g_key_file_set_string(keyfile, service->identifier,
3846 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
3847 g_key_file_set_string(keyfile, service->identifier,
3848 "Passphrase", service->passphrase);
3850 g_key_file_remove_key(keyfile, service->identifier,
3851 "Passphrase", NULL);
3853 if (service->ipconfig != NULL) {
3854 struct connman_ipconfig *ipv6config;
3856 ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
3857 if (ipv6config != NULL)
3858 __connman_ipconfig_save(ipv6config, keyfile,
3859 service->identifier, "IPv6.");
3861 __connman_ipconfig_save(service->ipconfig, keyfile,
3862 service->identifier, "IPv4.");
3865 if (service->nameservers != NULL) {
3866 guint len = g_strv_length(service->nameservers);
3868 g_key_file_set_string_list(keyfile, service->identifier,
3870 (const gchar **) service->nameservers, len);
3872 g_key_file_remove_key(keyfile, service->identifier,
3873 "Nameservers", NULL);
3875 if (service->domains != NULL) {
3876 guint len = g_strv_length(service->domains);
3878 g_key_file_set_string_list(keyfile, service->identifier,
3880 (const gchar **) service->domains, len);
3882 g_key_file_remove_key(keyfile, service->identifier,
3885 stats_save(service, keyfile, service->identifier);
3887 data = g_key_file_to_data(keyfile, &length, NULL);
3889 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
3890 connman_error("Failed to store service information");
3895 g_key_file_free(keyfile);
3902 static struct connman_storage service_storage = {
3904 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
3905 .service_load = service_load,
3906 .service_save = service_save,
3909 int __connman_service_init(void)
3913 connection = connman_dbus_get_connection();
3915 if (connman_storage_register(&service_storage) < 0)
3916 connman_error("Failed to register service storage");
3918 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
3921 service_list = g_sequence_new(service_free);
3926 void __connman_service_cleanup(void)
3930 g_sequence_free(service_list);
3931 service_list = NULL;
3933 g_hash_table_destroy(service_hash);
3934 service_hash = NULL;
3936 connman_storage_unregister(&service_storage);
3938 dbus_connection_unref(connection);