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;
38 static GSList *counter_list = NULL;
40 struct connman_stats {
42 connman_bool_t enabled;
43 struct connman_stats_data data_last;
44 struct connman_stats_data data;
48 struct connman_stats_counter {
49 connman_bool_t append_all;
50 struct connman_stats stats;
51 struct connman_stats stats_roaming;
54 struct connman_service {
58 enum connman_service_type type;
59 enum connman_service_mode mode;
60 enum connman_service_security security;
61 enum connman_service_state state;
62 enum connman_service_error error;
63 connman_uint8_t strength;
64 connman_bool_t favorite;
65 connman_bool_t immutable;
66 connman_bool_t hidden;
67 connman_bool_t ignore;
68 connman_bool_t autoconnect;
69 connman_bool_t userconnect;
80 connman_bool_t roaming;
81 connman_bool_t login_required;
82 struct connman_ipconfig *ipconfig_ipv4;
83 struct connman_ipconfig *ipconfig_ipv6;
84 struct connman_network *network;
85 struct connman_provider *provider;
90 /* 802.1x settings from the config files */
94 char *client_cert_file;
95 char *private_key_file;
96 char *private_key_passphrase;
100 struct connman_location *location;
101 struct connman_stats stats;
102 struct connman_stats stats_roaming;
103 GHashTable *counter_table;
104 enum connman_service_proxy_method proxy;
105 enum connman_service_proxy_method proxy_config;
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:
178 case CONNMAN_SERVICE_TYPE_GADGET:
185 static const char *mode2string(enum connman_service_mode mode)
188 case CONNMAN_SERVICE_MODE_UNKNOWN:
190 case CONNMAN_SERVICE_MODE_MANAGED:
192 case CONNMAN_SERVICE_MODE_ADHOC:
194 case CONNMAN_SERVICE_MODE_GPRS:
196 case CONNMAN_SERVICE_MODE_EDGE:
198 case CONNMAN_SERVICE_MODE_UMTS:
205 static const char *security2string(enum connman_service_security security)
208 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
210 case CONNMAN_SERVICE_SECURITY_NONE:
212 case CONNMAN_SERVICE_SECURITY_WEP:
214 case CONNMAN_SERVICE_SECURITY_PSK:
216 case CONNMAN_SERVICE_SECURITY_8021X:
218 case CONNMAN_SERVICE_SECURITY_WPA:
220 case CONNMAN_SERVICE_SECURITY_RSN:
227 static const char *state2string(enum connman_service_state state)
230 case CONNMAN_SERVICE_STATE_UNKNOWN:
232 case CONNMAN_SERVICE_STATE_IDLE:
234 case CONNMAN_SERVICE_STATE_ASSOCIATION:
235 return "association";
236 case CONNMAN_SERVICE_STATE_CONFIGURATION:
237 return "configuration";
238 case CONNMAN_SERVICE_STATE_READY:
240 case CONNMAN_SERVICE_STATE_ONLINE:
242 case CONNMAN_SERVICE_STATE_DISCONNECT:
244 case CONNMAN_SERVICE_STATE_FAILURE:
251 static const char *error2string(enum connman_service_error error)
254 case CONNMAN_SERVICE_ERROR_UNKNOWN:
256 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
257 return "out-of-range";
258 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
259 return "pin-missing";
260 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
261 return "dhcp-failed";
262 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
263 return "connect-failed";
269 static enum connman_service_error string2error(const char *error)
271 if (g_strcmp0(error, "dhcp-failed") == 0)
272 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
273 else if (g_strcmp0(error, "pin-missing") == 0)
274 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
276 return CONNMAN_SERVICE_ERROR_UNKNOWN;
279 static const char *proxymethod2string(enum connman_service_proxy_method method)
282 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
284 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
286 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
288 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
295 static enum connman_service_proxy_method string2proxymethod(const char *method)
297 if (g_strcmp0(method, "direct") == 0)
298 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
299 else if (g_strcmp0(method, "auto") == 0)
300 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
301 else if (g_strcmp0(method, "manual") == 0)
302 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
304 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
307 static connman_bool_t is_connecting(struct connman_service *service)
309 switch (service->state) {
310 case CONNMAN_SERVICE_STATE_UNKNOWN:
311 case CONNMAN_SERVICE_STATE_IDLE:
312 case CONNMAN_SERVICE_STATE_FAILURE:
313 if (service->network != NULL)
314 return __connman_network_get_connecting(
316 case CONNMAN_SERVICE_STATE_DISCONNECT:
317 case CONNMAN_SERVICE_STATE_READY:
318 case CONNMAN_SERVICE_STATE_ONLINE:
320 case CONNMAN_SERVICE_STATE_ASSOCIATION:
321 case CONNMAN_SERVICE_STATE_CONFIGURATION:
328 static connman_bool_t is_connected(const struct connman_service *service)
330 switch (service->state) {
331 case CONNMAN_SERVICE_STATE_UNKNOWN:
332 case CONNMAN_SERVICE_STATE_IDLE:
333 case CONNMAN_SERVICE_STATE_ASSOCIATION:
334 case CONNMAN_SERVICE_STATE_CONFIGURATION:
335 case CONNMAN_SERVICE_STATE_DISCONNECT:
336 case CONNMAN_SERVICE_STATE_FAILURE:
338 case CONNMAN_SERVICE_STATE_READY:
339 case CONNMAN_SERVICE_STATE_ONLINE:
346 static void update_nameservers(struct connman_service *service)
350 if (service->ipconfig_ipv4)
351 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
352 else if (service->ipconfig_ipv6)
353 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
360 switch (service->state) {
361 case CONNMAN_SERVICE_STATE_UNKNOWN:
362 case CONNMAN_SERVICE_STATE_IDLE:
363 case CONNMAN_SERVICE_STATE_ASSOCIATION:
364 case CONNMAN_SERVICE_STATE_CONFIGURATION:
366 case CONNMAN_SERVICE_STATE_FAILURE:
367 case CONNMAN_SERVICE_STATE_DISCONNECT:
368 connman_resolver_remove_all(ifname);
370 case CONNMAN_SERVICE_STATE_READY:
371 case CONNMAN_SERVICE_STATE_ONLINE:
375 connman_resolver_remove_all(ifname);
377 if (service->nameservers != NULL) {
380 for (i = 0; service->nameservers[i]; i++)
381 connman_resolver_append(ifname, NULL,
382 service->nameservers[i]);
383 } else if (service->nameserver != NULL)
384 connman_resolver_append(ifname, NULL, service->nameserver);
386 if (service->domains != NULL) {
389 for (i = 0; service->domains[i]; i++)
390 connman_resolver_append(ifname, service->domains[i],
392 } else if (service->domainname != NULL)
393 connman_resolver_append(ifname, service->domainname, NULL);
395 connman_resolver_flush();
398 void __connman_service_append_nameserver(struct connman_service *service,
399 const char *nameserver)
401 DBG("service %p nameserver %s", service, nameserver);
403 if (nameserver == NULL)
406 g_free(service->nameserver);
407 service->nameserver = g_strdup(nameserver);
409 update_nameservers(service);
412 void __connman_service_remove_nameserver(struct connman_service *service,
413 const char *nameserver)
415 DBG("service %p nameserver %s", service, nameserver);
417 if (nameserver == NULL)
420 g_free(service->nameserver);
421 service->nameserver = NULL;
423 update_nameservers(service);
426 void __connman_service_nameserver_add_routes(struct connman_service *service,
434 index = connman_network_get_index(service->network);
436 if (service->nameservers != NULL) {
440 * We add nameservers host routes for nameservers that
441 * are not on our subnet. For those who are, the subnet
442 * route will be installed by the time the dns proxy code
443 * tries to reach them. The subnet route is installed
444 * when setting the interface IP address.
446 for (i = 0; service->nameservers[i]; i++) {
447 if (connman_inet_compare_subnet(index,
448 service->nameservers[i]))
451 connman_inet_add_host_route(index,
452 service->nameservers[i], gw);
454 } else if (service->nameserver != NULL) {
455 if (connman_inet_compare_subnet(index, service->nameserver))
458 connman_inet_add_host_route(index, service->nameserver, gw);
462 void __connman_service_nameserver_del_routes(struct connman_service *service)
469 index = connman_network_get_index(service->network);
471 if (service->nameservers != NULL) {
474 for (i = 0; service->nameservers[i]; i++)
475 connman_inet_del_host_route(index,
476 service->nameservers[i]);
477 } else if (service->nameserver != NULL) {
478 connman_inet_del_host_route(index, service->nameserver);
482 static struct connman_stats *stats_get(struct connman_service *service)
484 if (service->roaming == TRUE)
485 return &service->stats_roaming;
487 return &service->stats;
490 static connman_bool_t stats_enabled(struct connman_service *service)
492 struct connman_stats *stats = stats_get(service);
494 return stats->enabled;
497 static void stats_start(struct connman_service *service)
499 struct connman_stats *stats = stats_get(service);
501 DBG("service %p", service);
503 if (stats->timer == NULL)
506 stats->enabled = TRUE;
507 stats->data_last.time = stats->data.time;
509 g_timer_start(stats->timer);
512 static void stats_stop(struct connman_service *service)
514 struct connman_stats *stats = stats_get(service);
515 unsigned int seconds;
517 DBG("service %p", service);
519 if (stats->timer == NULL)
522 if (stats->enabled == FALSE)
525 g_timer_stop(stats->timer);
527 seconds = g_timer_elapsed(stats->timer, NULL);
528 stats->data.time = stats->data_last.time + seconds;
530 stats->enabled = FALSE;
533 static void reset_stats(struct connman_service *service)
535 DBG("service %p", service);
538 service->stats.valid = FALSE;
540 service->stats.data.rx_packets = 0;
541 service->stats.data.tx_packets = 0;
542 service->stats.data.rx_bytes = 0;
543 service->stats.data.tx_bytes = 0;
544 service->stats.data.rx_errors = 0;
545 service->stats.data.tx_errors = 0;
546 service->stats.data.rx_dropped = 0;
547 service->stats.data.tx_dropped = 0;
548 service->stats.data.time = 0;
549 service->stats.data_last.time = 0;
551 g_timer_reset(service->stats.timer);
554 service->stats_roaming.valid = FALSE;
556 service->stats_roaming.data.rx_packets = 0;
557 service->stats_roaming.data.tx_packets = 0;
558 service->stats_roaming.data.rx_bytes = 0;
559 service->stats_roaming.data.tx_bytes = 0;
560 service->stats_roaming.data.rx_errors = 0;
561 service->stats_roaming.data.tx_errors = 0;
562 service->stats_roaming.data.rx_dropped = 0;
563 service->stats_roaming.data.tx_dropped = 0;
564 service->stats_roaming.data.time = 0;
565 service->stats_roaming.data_last.time = 0;
567 g_timer_reset(service->stats_roaming.timer);
570 static struct connman_service *get_default(void)
572 struct connman_service *service;
575 iter = g_sequence_get_begin_iter(service_list);
577 if (g_sequence_iter_is_end(iter) == TRUE)
580 service = g_sequence_get(iter);
582 if (is_connected(service) == FALSE)
588 static void default_changed(void)
590 struct connman_service *service = get_default();
592 __connman_notifier_default_changed(service);
595 const char *__connman_service_default(void)
597 struct connman_service *service;
599 service = get_default();
603 return __connman_service_type2string(service->type);
606 static void mode_changed(struct connman_service *service)
610 str = mode2string(service->mode);
614 connman_dbus_property_changed_basic(service->path,
615 CONNMAN_SERVICE_INTERFACE, "Mode",
616 DBUS_TYPE_STRING, &str);
619 static void state_changed(struct connman_service *service)
623 str = state2string(service->state);
627 connman_dbus_property_changed_basic(service->path,
628 CONNMAN_SERVICE_INTERFACE, "State",
629 DBUS_TYPE_STRING, &str);
632 static void strength_changed(struct connman_service *service)
634 if (service->strength == 0)
637 connman_dbus_property_changed_basic(service->path,
638 CONNMAN_SERVICE_INTERFACE, "Strength",
639 DBUS_TYPE_BYTE, &service->strength);
642 static void favorite_changed(struct connman_service *service)
644 if (service->path == NULL)
647 connman_dbus_property_changed_basic(service->path,
648 CONNMAN_SERVICE_INTERFACE, "Favorite",
649 DBUS_TYPE_BOOLEAN, &service->favorite);
652 static void immutable_changed(struct connman_service *service)
654 if (service->path == NULL)
657 connman_dbus_property_changed_basic(service->path,
658 CONNMAN_SERVICE_INTERFACE, "Immutable",
659 DBUS_TYPE_BOOLEAN, &service->immutable);
662 static void roaming_changed(struct connman_service *service)
664 if (service->path == NULL)
667 connman_dbus_property_changed_basic(service->path,
668 CONNMAN_SERVICE_INTERFACE, "Roaming",
669 DBUS_TYPE_BOOLEAN, &service->roaming);
672 static void autoconnect_changed(struct connman_service *service)
674 if (service->path == NULL)
677 connman_dbus_property_changed_basic(service->path,
678 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
679 DBUS_TYPE_BOOLEAN, &service->autoconnect);
682 static void passphrase_changed(struct connman_service *service)
684 dbus_bool_t required;
686 switch (service->type) {
687 case CONNMAN_SERVICE_TYPE_UNKNOWN:
688 case CONNMAN_SERVICE_TYPE_SYSTEM:
689 case CONNMAN_SERVICE_TYPE_ETHERNET:
690 case CONNMAN_SERVICE_TYPE_WIMAX:
691 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
692 case CONNMAN_SERVICE_TYPE_CELLULAR:
693 case CONNMAN_SERVICE_TYPE_GPS:
694 case CONNMAN_SERVICE_TYPE_VPN:
695 case CONNMAN_SERVICE_TYPE_GADGET:
697 case CONNMAN_SERVICE_TYPE_WIFI:
700 switch (service->security) {
701 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
702 case CONNMAN_SERVICE_SECURITY_NONE:
704 case CONNMAN_SERVICE_SECURITY_WEP:
705 case CONNMAN_SERVICE_SECURITY_PSK:
706 case CONNMAN_SERVICE_SECURITY_WPA:
707 case CONNMAN_SERVICE_SECURITY_RSN:
708 if (service->passphrase == NULL)
711 case CONNMAN_SERVICE_SECURITY_8021X:
717 connman_dbus_property_changed_basic(service->path,
718 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
719 DBUS_TYPE_BOOLEAN, &required);
722 static void login_changed(struct connman_service *service)
724 dbus_bool_t required = service->login_required;
726 if (service->path == NULL)
729 connman_dbus_property_changed_basic(service->path,
730 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
731 DBUS_TYPE_BOOLEAN, &required);
734 static void apn_changed(struct connman_service *service)
736 dbus_bool_t required;
738 switch (service->type) {
739 case CONNMAN_SERVICE_TYPE_UNKNOWN:
740 case CONNMAN_SERVICE_TYPE_SYSTEM:
741 case CONNMAN_SERVICE_TYPE_ETHERNET:
742 case CONNMAN_SERVICE_TYPE_WIMAX:
743 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
744 case CONNMAN_SERVICE_TYPE_WIFI:
745 case CONNMAN_SERVICE_TYPE_GPS:
746 case CONNMAN_SERVICE_TYPE_VPN:
747 case CONNMAN_SERVICE_TYPE_GADGET:
749 case CONNMAN_SERVICE_TYPE_CELLULAR:
753 required = (service->apn == NULL) ? TRUE : FALSE;
755 connman_dbus_property_changed_basic(service->path,
756 CONNMAN_SERVICE_INTERFACE, "SetupRequired",
757 DBUS_TYPE_BOOLEAN, &required);
760 static void append_ethernet(DBusMessageIter *iter, void *user_data)
762 struct connman_service *service = user_data;
764 if (service->ipconfig_ipv4 != NULL)
765 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
767 else if (service->ipconfig_ipv6 != NULL)
768 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
772 static void append_ipv4(DBusMessageIter *iter, void *user_data)
774 struct connman_service *service = user_data;
776 if (is_connected(service) == FALSE)
779 if (service->ipconfig_ipv4 != NULL)
780 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
783 static void append_ipv6(DBusMessageIter *iter, void *user_data)
785 struct connman_service *service = user_data;
787 if (is_connected(service) == FALSE)
790 if (service->ipconfig_ipv6 != NULL)
791 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter);
794 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
796 struct connman_service *service = user_data;
798 if (service->ipconfig_ipv4 != NULL)
799 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
803 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
805 struct connman_service *service = user_data;
807 if (service->ipconfig_ipv6 != NULL)
808 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
812 static void append_dns(DBusMessageIter *iter, void *user_data)
814 struct connman_service *service = user_data;
816 if (is_connected(service) == FALSE)
819 if (service->nameservers != NULL) {
822 for (i = 0; service->nameservers[i]; i++)
823 dbus_message_iter_append_basic(iter,
824 DBUS_TYPE_STRING, &service->nameservers[i]);
829 if (service->nameserver == NULL)
832 dbus_message_iter_append_basic(iter,
833 DBUS_TYPE_STRING, &service->nameserver);
836 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
838 struct connman_service *service = user_data;
841 if (service->nameservers == NULL)
844 for (i = 0; service->nameservers[i]; i++)
845 dbus_message_iter_append_basic(iter,
846 DBUS_TYPE_STRING, &service->nameservers[i]);
849 static void append_domain(DBusMessageIter *iter, void *user_data)
851 struct connman_service *service = user_data;
853 if (is_connected(service) == FALSE && is_connecting(service) == FALSE)
856 if (service->domainname == NULL)
859 dbus_message_iter_append_basic(iter,
860 DBUS_TYPE_STRING, &service->domainname);
863 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
865 struct connman_service *service = user_data;
868 if (service->domains == NULL)
871 for (i = 0; service->domains[i]; i++)
872 dbus_message_iter_append_basic(iter,
873 DBUS_TYPE_STRING, &service->domains[i]);
876 static void append_proxies(DBusMessageIter *iter, void *user_data)
878 struct connman_service *service = user_data;
881 if (service->proxies == NULL)
884 for (i = 0; service->proxies[i]; i++)
885 dbus_message_iter_append_basic(iter,
886 DBUS_TYPE_STRING, &service->proxies[i]);
889 static void append_excludes(DBusMessageIter *iter, void *user_data)
891 struct connman_service *service = user_data;
894 if (service->excludes == NULL)
897 for (i = 0; service->excludes[i]; i++)
898 dbus_message_iter_append_basic(iter,
899 DBUS_TYPE_STRING, &service->excludes[i]);
902 static void append_proxy(DBusMessageIter *iter, void *user_data)
904 struct connman_service *service = user_data;
905 enum connman_service_proxy_method proxy;
906 const char *pac = NULL;
907 const char *method = proxymethod2string(
908 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
912 if (is_connected(service) == FALSE)
915 proxy = connman_service_get_proxy_method(service);
918 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
920 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
922 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
923 connman_dbus_dict_append_array(iter, "Servers",
924 DBUS_TYPE_STRING, append_proxies,
927 connman_dbus_dict_append_array(iter, "Excludes",
928 DBUS_TYPE_STRING, append_excludes,
931 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
932 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
933 if (service->ipconfig_ipv4 != NULL)
934 pac = __connman_ipconfig_get_proxy_autoconfig(
935 service->ipconfig_ipv4);
936 else if (service->ipconfig_ipv6 != NULL)
937 pac = __connman_ipconfig_get_proxy_autoconfig(
938 service->ipconfig_ipv6);
940 if (service->pac == NULL && pac == NULL)
943 if (service->pac != NULL)
946 connman_dbus_dict_append_basic(iter, "URL",
947 DBUS_TYPE_STRING, &pac);
951 method = proxymethod2string(proxy);
954 connman_dbus_dict_append_basic(iter, "Method",
955 DBUS_TYPE_STRING, &method);
958 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
960 struct connman_service *service = user_data;
963 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
966 switch (service->proxy_config) {
967 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
969 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
971 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
972 if (service->proxies != NULL)
973 connman_dbus_dict_append_array(iter, "Servers",
975 append_proxies, service);
977 if (service->excludes != NULL)
978 connman_dbus_dict_append_array(iter, "Excludes",
980 append_excludes, service);
982 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
983 if (service->pac != NULL)
984 connman_dbus_dict_append_basic(iter, "URL",
985 DBUS_TYPE_STRING, &service->pac);
989 method = proxymethod2string(service->proxy_config);
991 connman_dbus_dict_append_basic(iter, "Method",
992 DBUS_TYPE_STRING, &method);
995 static void append_provider(DBusMessageIter *iter, void *user_data)
997 struct connman_service *service = user_data;
999 DBG("%p %p", service, service->provider);
1001 if (is_connected(service) == FALSE)
1004 if (service->provider != NULL)
1005 __connman_provider_append_properties(service->provider, iter);
1009 static void settings_changed(struct connman_service *service)
1011 connman_dbus_property_changed_dict(service->path,
1012 CONNMAN_SERVICE_INTERFACE, "IPv4",
1013 append_ipv4, service);
1015 connman_dbus_property_changed_dict(service->path,
1016 CONNMAN_SERVICE_INTERFACE, "IPv6",
1017 append_ipv6, service);
1020 static void ipv4_configuration_changed(struct connman_service *service)
1022 connman_dbus_property_changed_dict(service->path,
1023 CONNMAN_SERVICE_INTERFACE,
1024 "IPv4.Configuration",
1029 static void ipv6_configuration_changed(struct connman_service *service)
1031 connman_dbus_property_changed_dict(service->path,
1032 CONNMAN_SERVICE_INTERFACE,
1033 "IPv6.Configuration",
1038 static void dns_changed(struct connman_service *service)
1040 connman_dbus_property_changed_array(service->path,
1041 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1042 DBUS_TYPE_STRING, append_dns, service);
1045 static void dns_configuration_changed(struct connman_service *service)
1047 connman_dbus_property_changed_array(service->path,
1048 CONNMAN_SERVICE_INTERFACE,
1049 "Nameservers.Configuration",
1050 DBUS_TYPE_STRING, append_dnsconfig, service);
1052 dns_changed(service);
1055 static void domain_changed(struct connman_service *service)
1057 connman_dbus_property_changed_array(service->path,
1058 CONNMAN_SERVICE_INTERFACE, "Domains",
1059 DBUS_TYPE_STRING, append_domain, service);
1062 static void domain_configuration_changed(struct connman_service *service)
1064 connman_dbus_property_changed_array(service->path,
1065 CONNMAN_SERVICE_INTERFACE,
1066 "Domains.Configuration",
1067 DBUS_TYPE_STRING, append_domainconfig, service);
1070 static void proxy_changed(struct connman_service *service)
1072 connman_dbus_property_changed_dict(service->path,
1073 CONNMAN_SERVICE_INTERFACE, "Proxy",
1074 append_proxy, service);
1077 static void proxy_configuration_changed(struct connman_service *service)
1079 connman_dbus_property_changed_dict(service->path,
1080 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1081 append_proxyconfig, service);
1083 proxy_changed(service);
1086 static void link_changed(struct connman_service *service)
1088 connman_dbus_property_changed_dict(service->path,
1089 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1090 append_ethernet, service);
1093 static void stats_append_counters(DBusMessageIter *dict,
1094 struct connman_stats_data *stats,
1095 struct connman_stats_data *counters,
1096 connman_bool_t append_all)
1098 if (counters->rx_packets != stats->rx_packets || append_all) {
1099 counters->rx_packets = stats->rx_packets;
1100 connman_dbus_dict_append_basic(dict, "RX.Packets",
1101 DBUS_TYPE_UINT32, &stats->rx_packets);
1104 if (counters->tx_packets != stats->tx_packets || append_all) {
1105 counters->tx_packets = stats->tx_packets;
1106 connman_dbus_dict_append_basic(dict, "TX.Packets",
1107 DBUS_TYPE_UINT32, &stats->tx_packets);
1110 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1111 counters->rx_bytes = stats->rx_bytes;
1112 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1113 DBUS_TYPE_UINT32, &stats->rx_bytes);
1116 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1117 counters->tx_bytes = stats->tx_bytes;
1118 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1119 DBUS_TYPE_UINT32, &stats->tx_bytes);
1122 if (counters->rx_errors != stats->rx_errors || append_all) {
1123 counters->rx_errors = stats->rx_errors;
1124 connman_dbus_dict_append_basic(dict, "RX.Errors",
1125 DBUS_TYPE_UINT32, &stats->rx_errors);
1128 if (counters->tx_errors != stats->tx_errors || append_all) {
1129 counters->tx_errors = stats->tx_errors;
1130 connman_dbus_dict_append_basic(dict, "TX.Errors",
1131 DBUS_TYPE_UINT32, &stats->tx_errors);
1134 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1135 counters->rx_dropped = stats->rx_dropped;
1136 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1137 DBUS_TYPE_UINT32, &stats->rx_dropped);
1140 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1141 counters->tx_dropped = stats->tx_dropped;
1142 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1143 DBUS_TYPE_UINT32, &stats->tx_dropped);
1146 if (counters->time != stats->time || append_all) {
1147 counters->time = stats->time;
1148 connman_dbus_dict_append_basic(dict, "Time",
1149 DBUS_TYPE_UINT32, &stats->time);
1153 static void stats_append(struct connman_service *service,
1154 const char *counter,
1155 struct connman_stats_counter *counters,
1156 connman_bool_t append_all)
1158 DBusMessageIter array, dict;
1161 DBG("service %p counter %s", service, counter);
1163 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1167 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1168 &service->path, DBUS_TYPE_INVALID);
1170 dbus_message_iter_init_append(msg, &array);
1173 connman_dbus_dict_open(&array, &dict);
1175 stats_append_counters(&dict, &service->stats.data,
1176 &counters->stats.data, append_all);
1178 connman_dbus_dict_close(&array, &dict);
1180 /* roaming counter */
1181 connman_dbus_dict_open(&array, &dict);
1183 stats_append_counters(&dict, &service->stats_roaming.data,
1184 &counters->stats_roaming.data, append_all);
1186 connman_dbus_dict_close(&array, &dict);
1188 __connman_counter_send_usage(counter, msg);
1191 static void stats_update(struct connman_service *service,
1192 unsigned int rx_packets, unsigned int tx_packets,
1193 unsigned int rx_bytes, unsigned int tx_bytes,
1194 unsigned int rx_errors, unsigned int tx_errors,
1195 unsigned int rx_dropped, unsigned int tx_dropped)
1197 struct connman_stats *stats = stats_get(service);
1198 struct connman_stats_data *data_last = &stats->data_last;
1199 struct connman_stats_data *data = &stats->data;
1200 unsigned int seconds;
1202 DBG("service %p", service);
1204 if (stats->valid == TRUE) {
1206 rx_packets - data_last->rx_packets;
1208 tx_packets - data_last->tx_packets;
1210 rx_bytes - data_last->rx_bytes;
1212 tx_bytes - data_last->tx_bytes;
1214 rx_errors - data_last->rx_errors;
1216 tx_errors - data_last->tx_errors;
1218 rx_dropped - data_last->rx_dropped;
1220 tx_dropped - data_last->tx_dropped;
1222 stats->valid = TRUE;
1225 data_last->rx_packets = rx_packets;
1226 data_last->tx_packets = tx_packets;
1227 data_last->rx_bytes = rx_bytes;
1228 data_last->tx_bytes = tx_bytes;
1229 data_last->rx_errors = rx_errors;
1230 data_last->tx_errors = tx_errors;
1231 data_last->rx_dropped = rx_dropped;
1232 data_last->tx_dropped = tx_dropped;
1234 seconds = g_timer_elapsed(stats->timer, NULL);
1235 stats->data.time = stats->data_last.time + seconds;
1238 void __connman_service_notify(struct connman_service *service,
1239 unsigned int rx_packets, unsigned int tx_packets,
1240 unsigned int rx_bytes, unsigned int tx_bytes,
1241 unsigned int rx_errors, unsigned int tx_errors,
1242 unsigned int rx_dropped, unsigned int tx_dropped)
1244 GHashTableIter iter;
1245 gpointer key, value;
1246 const char *counter;
1247 struct connman_stats_counter *counters;
1248 struct connman_stats_data *data;
1251 if (service == NULL)
1254 if (is_connected(service) == FALSE)
1257 stats_update(service,
1258 rx_packets, tx_packets,
1260 rx_errors, tx_errors,
1261 rx_dropped, tx_dropped);
1263 data = &stats_get(service)->data;
1264 err = __connman_stats_update(service, service->roaming, data);
1266 connman_error("Failed to store statistics for %s",
1267 service->identifier);
1269 g_hash_table_iter_init(&iter, service->counter_table);
1270 while (g_hash_table_iter_next(&iter, &key, &value)) {
1274 stats_append(service, counter, counters, counters->append_all);
1275 counters->append_all = FALSE;
1279 int __connman_service_counter_register(const char *counter)
1281 struct connman_service *service;
1282 GSequenceIter *iter;
1283 struct connman_stats_counter *counters;
1285 DBG("counter %s", counter);
1287 counter_list = g_slist_append(counter_list, (gpointer)counter);
1289 iter = g_sequence_get_begin_iter(service_list);
1291 while (g_sequence_iter_is_end(iter) == FALSE) {
1292 service = g_sequence_get(iter);
1294 counters = g_try_new0(struct connman_stats_counter, 1);
1295 if (counters == NULL)
1298 counters->append_all = TRUE;
1300 g_hash_table_replace(service->counter_table, (gpointer)counter,
1303 iter = g_sequence_iter_next(iter);
1309 void __connman_service_counter_unregister(const char *counter)
1311 struct connman_service *service;
1312 GSequenceIter *iter;
1314 DBG("counter %s", counter);
1316 iter = g_sequence_get_begin_iter(service_list);
1318 while (g_sequence_iter_is_end(iter) == FALSE) {
1319 service = g_sequence_get(iter);
1321 g_hash_table_remove(service->counter_table, counter);
1323 iter = g_sequence_iter_next(iter);
1326 counter_list = g_slist_remove(counter_list, counter);
1329 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1330 struct connman_service *service)
1332 dbus_bool_t required;
1335 str = __connman_service_type2string(service->type);
1337 connman_dbus_dict_append_basic(dict, "Type",
1338 DBUS_TYPE_STRING, &str);
1340 str = mode2string(service->mode);
1342 connman_dbus_dict_append_basic(dict, "Mode",
1343 DBUS_TYPE_STRING, &str);
1345 str = security2string(service->security);
1347 connman_dbus_dict_append_basic(dict, "Security",
1348 DBUS_TYPE_STRING, &str);
1350 str = state2string(service->state);
1352 connman_dbus_dict_append_basic(dict, "State",
1353 DBUS_TYPE_STRING, &str);
1355 str = error2string(service->error);
1357 connman_dbus_dict_append_basic(dict, "Error",
1358 DBUS_TYPE_STRING, &str);
1360 if (service->strength > 0)
1361 connman_dbus_dict_append_basic(dict, "Strength",
1362 DBUS_TYPE_BYTE, &service->strength);
1364 connman_dbus_dict_append_basic(dict, "Favorite",
1365 DBUS_TYPE_BOOLEAN, &service->favorite);
1367 connman_dbus_dict_append_basic(dict, "Immutable",
1368 DBUS_TYPE_BOOLEAN, &service->immutable);
1370 if (service->favorite == TRUE)
1371 connman_dbus_dict_append_basic(dict, "AutoConnect",
1372 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1374 connman_dbus_dict_append_basic(dict, "AutoConnect",
1375 DBUS_TYPE_BOOLEAN, &service->favorite);
1377 if (service->name != NULL)
1378 connman_dbus_dict_append_basic(dict, "Name",
1379 DBUS_TYPE_STRING, &service->name);
1381 connman_dbus_dict_append_basic(dict, "LoginRequired",
1382 DBUS_TYPE_BOOLEAN, &service->login_required);
1384 switch (service->type) {
1385 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1386 case CONNMAN_SERVICE_TYPE_SYSTEM:
1387 case CONNMAN_SERVICE_TYPE_GPS:
1388 case CONNMAN_SERVICE_TYPE_VPN:
1389 case CONNMAN_SERVICE_TYPE_GADGET:
1391 case CONNMAN_SERVICE_TYPE_CELLULAR:
1392 connman_dbus_dict_append_basic(dict, "Roaming",
1393 DBUS_TYPE_BOOLEAN, &service->roaming);
1395 if (service->apn != NULL) {
1396 connman_dbus_dict_append_basic(dict, "APN",
1397 DBUS_TYPE_STRING, &service->apn);
1399 if (service->username != NULL)
1400 connman_dbus_dict_append_basic(dict,
1401 "Username", DBUS_TYPE_STRING,
1402 &service->username);
1404 if (service->password != NULL)
1405 connman_dbus_dict_append_basic(dict,
1406 "Password", DBUS_TYPE_STRING,
1407 &service->password);
1413 connman_dbus_dict_append_basic(dict, "SetupRequired",
1414 DBUS_TYPE_BOOLEAN, &required);
1415 connman_dbus_dict_append_dict(dict, "Ethernet",
1416 append_ethernet, service);
1418 case CONNMAN_SERVICE_TYPE_WIFI:
1419 if (service->passphrase != NULL && limited == FALSE)
1420 connman_dbus_dict_append_basic(dict, "Passphrase",
1421 DBUS_TYPE_STRING, &service->passphrase);
1425 switch (service->security) {
1426 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1427 case CONNMAN_SERVICE_SECURITY_NONE:
1429 case CONNMAN_SERVICE_SECURITY_WEP:
1430 case CONNMAN_SERVICE_SECURITY_PSK:
1431 case CONNMAN_SERVICE_SECURITY_WPA:
1432 case CONNMAN_SERVICE_SECURITY_RSN:
1433 if (service->passphrase == NULL)
1436 case CONNMAN_SERVICE_SECURITY_8021X:
1440 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1441 DBUS_TYPE_BOOLEAN, &required);
1443 connman_dbus_dict_append_basic(dict, "WPS",
1444 DBUS_TYPE_BOOLEAN, &service->wps);
1446 case CONNMAN_SERVICE_TYPE_ETHERNET:
1447 case CONNMAN_SERVICE_TYPE_WIMAX:
1448 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1449 connman_dbus_dict_append_dict(dict, "Ethernet",
1450 append_ethernet, service);
1454 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1456 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1457 append_ipv4config, service);
1459 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1461 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1462 append_ipv6config, service);
1464 connman_dbus_dict_append_array(dict, "Nameservers",
1465 DBUS_TYPE_STRING, append_dns, service);
1467 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1468 DBUS_TYPE_STRING, append_dnsconfig, service);
1470 connman_dbus_dict_append_array(dict, "Domains",
1471 DBUS_TYPE_STRING, append_domain, service);
1473 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1474 DBUS_TYPE_STRING, append_domainconfig, service);
1476 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1478 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1479 append_proxyconfig, service);
1481 connman_dbus_dict_append_dict(dict, "Provider",
1482 append_provider, service);
1485 static void append_struct(gpointer value, gpointer user_data)
1487 struct connman_service *service = value;
1488 DBusMessageIter *iter = user_data;
1489 DBusMessageIter entry, dict;
1491 if (service->path == NULL || service->hidden == TRUE)
1494 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1496 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1499 connman_dbus_dict_open(&entry, &dict);
1500 append_properties(&dict, TRUE, service);
1501 connman_dbus_dict_close(&entry, &dict);
1503 dbus_message_iter_close_container(iter, &entry);
1506 void __connman_service_list_struct(DBusMessageIter *iter)
1508 g_sequence_foreach(service_list, append_struct, iter);
1511 int __connman_service_get_index(struct connman_service *service)
1513 if (service == NULL)
1516 if (service->network == NULL)
1519 return connman_network_get_index(service->network);
1522 void __connman_service_set_domainname(struct connman_service *service,
1523 const char *domainname)
1525 if (service == NULL)
1528 g_free(service->domainname);
1529 service->domainname = g_strdup(domainname);
1531 domain_changed(service);
1534 const char *connman_service_get_domainname(struct connman_service *service)
1536 if (service == NULL)
1539 if (service->domains != NULL)
1540 return service->domains[0];
1542 return service->domainname;
1545 const char *connman_service_get_nameserver(struct connman_service *service)
1547 if (service == NULL)
1550 return service->nameserver;
1553 void connman_service_set_proxy_method(struct connman_service *service,
1554 enum connman_service_proxy_method method)
1556 if (service == NULL)
1559 service->proxy = method;
1561 proxy_changed(service);
1563 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1564 __connman_notifier_proxy_changed(service);
1567 enum connman_service_proxy_method connman_service_get_proxy_method(
1568 struct connman_service *service)
1570 if (service == NULL)
1571 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1573 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1574 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1575 service->pac == NULL)
1576 return service->proxy;
1578 return service->proxy_config;
1581 return service->proxy;
1584 char **connman_service_get_proxy_servers(struct connman_service *service)
1586 return g_strdupv(service->proxies);
1589 char **connman_service_get_proxy_excludes(struct connman_service *service)
1591 return g_strdupv(service->excludes);
1594 const char *connman_service_get_proxy_url(struct connman_service *service)
1596 if (service == NULL)
1599 return service->pac;
1602 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1605 if (service == NULL)
1608 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1610 if (service->ipconfig_ipv4) {
1611 if (__connman_ipconfig_set_proxy_autoconfig(
1612 service->ipconfig_ipv4, url) < 0)
1614 } else if (service->ipconfig_ipv6) {
1615 if (__connman_ipconfig_set_proxy_autoconfig(
1616 service->ipconfig_ipv6, url) < 0)
1621 proxy_changed(service);
1623 __connman_notifier_proxy_changed(service);
1626 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1628 if (service == NULL)
1631 if (service->ipconfig_ipv4)
1632 return __connman_ipconfig_get_proxy_autoconfig(
1633 service->ipconfig_ipv4);
1634 else if (service->ipconfig_ipv6)
1635 return __connman_ipconfig_get_proxy_autoconfig(
1636 service->ipconfig_ipv6);
1640 void __connman_service_set_passphrase(struct connman_service *service,
1641 const char* passphrase)
1643 if (service->immutable == TRUE)
1646 g_free(service->passphrase);
1647 service->passphrase = g_strdup(passphrase);
1649 passphrase_changed(service);
1651 if (service->network != NULL)
1652 connman_network_set_string(service->network,
1654 service->passphrase);
1656 __connman_storage_save_service(service);
1659 static DBusMessage *get_properties(DBusConnection *conn,
1660 DBusMessage *msg, void *user_data)
1662 struct connman_service *service = user_data;
1664 DBusMessageIter array, dict;
1666 DBG("service %p", service);
1668 reply = dbus_message_new_method_return(msg);
1672 dbus_message_iter_init_append(reply, &array);
1674 connman_dbus_dict_open(&array, &dict);
1675 append_properties(&dict, FALSE, service);
1676 connman_dbus_dict_close(&array, &dict);
1681 static int update_proxy_configuration(struct connman_service *service,
1682 DBusMessageIter *array)
1684 DBusMessageIter dict;
1685 enum connman_service_proxy_method method;
1686 GString *servers_str = NULL;
1687 GString *excludes_str = NULL;
1688 const char *url = NULL;
1690 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1692 dbus_message_iter_recurse(array, &dict);
1694 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1695 DBusMessageIter entry, variant;
1699 dbus_message_iter_recurse(&dict, &entry);
1701 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1704 dbus_message_iter_get_basic(&entry, &key);
1705 dbus_message_iter_next(&entry);
1707 if (dbus_message_iter_get_arg_type(&entry) !=
1711 dbus_message_iter_recurse(&entry, &variant);
1712 type = dbus_message_iter_get_arg_type(&variant);
1714 if (g_str_equal(key, "Method") == TRUE) {
1717 if (type != DBUS_TYPE_STRING)
1720 dbus_message_iter_get_basic(&variant, &val);
1721 method = string2proxymethod(val);
1722 } else if (g_str_equal(key, "URL") == TRUE) {
1723 if (type != DBUS_TYPE_STRING)
1726 dbus_message_iter_get_basic(&variant, &url);
1727 } else if (g_str_equal(key, "Servers") == TRUE) {
1728 DBusMessageIter str_array;
1730 if (type != DBUS_TYPE_ARRAY)
1733 servers_str = g_string_new(NULL);
1734 if (servers_str == NULL)
1737 dbus_message_iter_recurse(&variant, &str_array);
1739 while (dbus_message_iter_get_arg_type(&str_array) ==
1743 dbus_message_iter_get_basic(&str_array, &val);
1745 if (servers_str->len > 0)
1746 g_string_append_printf(servers_str,
1749 g_string_append(servers_str, val);
1751 dbus_message_iter_next(&str_array);
1753 } else if (g_str_equal(key, "Excludes") == TRUE) {
1754 DBusMessageIter str_array;
1756 if (type != DBUS_TYPE_ARRAY)
1759 excludes_str = g_string_new(NULL);
1760 if (excludes_str == NULL)
1763 dbus_message_iter_recurse(&variant, &str_array);
1765 while (dbus_message_iter_get_arg_type(&str_array) ==
1769 dbus_message_iter_get_basic(&str_array, &val);
1771 if (excludes_str->len > 0)
1772 g_string_append_printf(excludes_str,
1775 g_string_append(excludes_str, val);
1777 dbus_message_iter_next(&str_array);
1781 dbus_message_iter_next(&dict);
1785 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1787 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1788 if (servers_str == NULL && service->proxies == NULL)
1791 if (servers_str != NULL) {
1792 g_strfreev(service->proxies);
1794 if (servers_str->len > 0)
1795 service->proxies = g_strsplit_set(
1796 servers_str->str, " ", 0);
1798 service->proxies = NULL;
1801 if (excludes_str != NULL) {
1802 g_strfreev(service->excludes);
1804 if (excludes_str->len > 0)
1805 service->excludes = g_strsplit_set(
1806 excludes_str->str, " ", 0);
1808 service->excludes = NULL;
1811 if (service->proxies == NULL)
1812 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
1815 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1816 g_free(service->pac);
1818 if (url != NULL && strlen(url) > 0)
1819 service->pac = g_strdup(url);
1821 service->pac = NULL;
1823 /* if we are connected:
1824 - if service->pac == NULL
1825 - if __connman_ipconfig_get_proxy_autoconfig(
1826 service->ipconfig) == NULL
1827 --> We should start WPAD */
1830 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1834 if (servers_str != NULL)
1835 g_string_free(servers_str, TRUE);
1837 if (excludes_str != NULL)
1838 g_string_free(excludes_str, TRUE);
1840 service->proxy_config = method;
1845 if (servers_str != NULL)
1846 g_string_free(servers_str, TRUE);
1848 if (excludes_str != NULL)
1849 g_string_free(excludes_str, TRUE);
1854 static DBusMessage *set_property(DBusConnection *conn,
1855 DBusMessage *msg, void *user_data)
1857 struct connman_service *service = user_data;
1858 DBusMessageIter iter, value;
1862 DBG("service %p", service);
1864 if (dbus_message_iter_init(msg, &iter) == FALSE)
1865 return __connman_error_invalid_arguments(msg);
1867 dbus_message_iter_get_basic(&iter, &name);
1868 dbus_message_iter_next(&iter);
1869 dbus_message_iter_recurse(&iter, &value);
1871 type = dbus_message_iter_get_arg_type(&value);
1873 if (g_str_equal(name, "AutoConnect") == TRUE) {
1874 connman_bool_t autoconnect;
1876 if (type != DBUS_TYPE_BOOLEAN)
1877 return __connman_error_invalid_arguments(msg);
1879 if (service->favorite == FALSE)
1880 return __connman_error_invalid_service(msg);
1882 dbus_message_iter_get_basic(&value, &autoconnect);
1884 if (service->autoconnect == autoconnect)
1885 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1887 service->autoconnect = autoconnect;
1889 autoconnect_changed(service);
1891 __connman_storage_save_service(service);
1892 } else if (g_str_equal(name, "Passphrase") == TRUE) {
1893 const char *passphrase;
1895 if (type != DBUS_TYPE_STRING)
1896 return __connman_error_invalid_arguments(msg);
1898 if (service->immutable == TRUE)
1899 return __connman_error_not_supported(msg);
1901 dbus_message_iter_get_basic(&value, &passphrase);
1903 __connman_service_set_passphrase(service, passphrase);
1904 } else if (g_str_equal(name, "APN") == TRUE) {
1907 if (type != DBUS_TYPE_STRING)
1908 return __connman_error_invalid_arguments(msg);
1910 if (service->immutable == TRUE)
1911 return __connman_error_not_supported(msg);
1913 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1914 return __connman_error_invalid_service(msg);
1916 dbus_message_iter_get_basic(&value, &apn);
1918 g_free(service->apn);
1919 service->apn = g_strdup(apn);
1921 apn_changed(service);
1923 if (service->network != NULL)
1924 connman_network_set_string(service->network,
1925 "Cellular.APN", service->apn);
1927 __connman_storage_save_service(service);
1928 } else if (g_str_equal(name, "Username") == TRUE) {
1929 const char *username;
1931 if (type != DBUS_TYPE_STRING)
1932 return __connman_error_invalid_arguments(msg);
1934 if (service->immutable == TRUE)
1935 return __connman_error_not_supported(msg);
1937 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1938 return __connman_error_invalid_service(msg);
1940 dbus_message_iter_get_basic(&value, &username);
1942 g_free(service->username);
1943 service->username = g_strdup(username);
1945 if (service->network != NULL)
1946 connman_network_set_string(service->network,
1947 "Cellular.Username", service->username);
1949 __connman_storage_save_service(service);
1950 } else if (g_str_equal(name, "Password") == TRUE) {
1951 const char *password;
1953 if (type != DBUS_TYPE_STRING)
1954 return __connman_error_invalid_arguments(msg);
1956 if (service->immutable == TRUE)
1957 return __connman_error_not_supported(msg);
1959 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1960 return __connman_error_invalid_service(msg);
1962 dbus_message_iter_get_basic(&value, &password);
1964 g_free(service->password);
1965 service->password = g_strdup(password);
1967 if (service->network != NULL)
1968 connman_network_set_string(service->network,
1969 "Cellular.Password", service->password);
1971 __connman_storage_save_service(service);
1972 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
1973 DBusMessageIter entry;
1978 if (type != DBUS_TYPE_ARRAY)
1979 return __connman_error_invalid_arguments(msg);
1981 str = g_string_new(NULL);
1983 return __connman_error_invalid_arguments(msg);
1985 index = connman_network_get_index(service->network);
1986 gw = __connman_ipconfig_get_gateway(index);
1988 if (gw && strlen(gw))
1989 __connman_service_nameserver_del_routes(service);
1991 dbus_message_iter_recurse(&value, &entry);
1993 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1995 dbus_message_iter_get_basic(&entry, &val);
1996 dbus_message_iter_next(&entry);
1998 g_string_append_printf(str, " %s", val);
2000 g_string_append(str, val);
2003 g_strfreev(service->nameservers);
2006 service->nameservers = g_strsplit_set(str->str, " ", 0);
2008 service->nameservers = NULL;
2010 g_string_free(str, TRUE);
2012 if (gw && strlen(gw))
2013 __connman_service_nameserver_add_routes(service, gw);
2015 update_nameservers(service);
2016 dns_configuration_changed(service);
2018 __connman_storage_save_service(service);
2019 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2020 DBusMessageIter entry;
2023 if (type != DBUS_TYPE_ARRAY)
2024 return __connman_error_invalid_arguments(msg);
2026 str = g_string_new(NULL);
2028 return __connman_error_invalid_arguments(msg);
2030 dbus_message_iter_recurse(&value, &entry);
2032 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2034 dbus_message_iter_get_basic(&entry, &val);
2035 dbus_message_iter_next(&entry);
2037 g_string_append_printf(str, " %s", val);
2039 g_string_append(str, val);
2042 g_strfreev(service->domains);
2045 service->domains = g_strsplit_set(str->str, " ", 0);
2047 service->domains = NULL;
2049 g_string_free(str, TRUE);
2051 update_nameservers(service);
2052 domain_configuration_changed(service);
2054 __connman_storage_save_service(service);
2055 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2058 if (type != DBUS_TYPE_ARRAY)
2059 return __connman_error_invalid_arguments(msg);
2061 err = update_proxy_configuration(service, &value);
2064 return __connman_error_failed(msg, -err);
2066 proxy_configuration_changed(service);
2068 __connman_storage_save_service(service);
2069 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2070 g_str_equal(name, "IPv6.Configuration")) {
2072 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2077 if (service->ipconfig_ipv4 == NULL &&
2078 service->ipconfig_ipv6 == NULL)
2079 return __connman_error_invalid_property(msg);
2081 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2082 if (is_connecting(service) || is_connected(service))
2083 __connman_network_clear_ipconfig(
2085 service->ipconfig_ipv4);
2087 ipv4 = service->ipconfig_ipv4;
2088 err = __connman_ipconfig_set_config(ipv4, &value);
2090 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2091 if (is_connecting(service) || is_connected(service))
2092 __connman_network_clear_ipconfig(
2094 service->ipconfig_ipv6);
2096 ipv6 = service->ipconfig_ipv6;
2097 err = __connman_ipconfig_set_config(ipv6, &value);
2101 if (is_connected(service) || is_connecting(service))
2102 __connman_network_set_ipconfig(service->network,
2104 return __connman_error_failed(msg, -err);
2108 ipv4_configuration_changed(service);
2110 ipv6_configuration_changed(service);
2112 if (is_connecting(service) || is_connected(service))
2113 __connman_network_set_ipconfig(service->network,
2116 __connman_storage_save_service(service);
2118 return __connman_error_invalid_property(msg);
2120 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2123 static void set_idle(struct connman_service *service)
2125 service->state = CONNMAN_SERVICE_STATE_IDLE;
2126 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2127 state_changed(service);
2130 static DBusMessage *clear_property(DBusConnection *conn,
2131 DBusMessage *msg, void *user_data)
2133 struct connman_service *service = user_data;
2136 DBG("service %p", service);
2138 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2141 if (g_str_equal(name, "Error") == TRUE) {
2144 g_get_current_time(&service->modified);
2145 __connman_storage_save_service(service);
2146 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2147 if (service->immutable == TRUE)
2148 return __connman_error_not_supported(msg);
2150 g_free(service->passphrase);
2151 service->passphrase = NULL;
2153 passphrase_changed(service);
2155 __connman_storage_save_service(service);
2157 return __connman_error_invalid_property(msg);
2159 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2162 static connman_bool_t is_ignore(struct connman_service *service)
2164 if (service->autoconnect == FALSE)
2167 if (service->roaming == TRUE)
2170 if (service->ignore == TRUE)
2173 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2179 void __connman_service_auto_connect(void)
2181 struct connman_service *service = NULL;
2182 GSequenceIter *iter;
2186 iter = g_sequence_get_begin_iter(service_list);
2188 while (g_sequence_iter_is_end(iter) == FALSE) {
2189 service = g_sequence_get(iter);
2191 if (service->pending != NULL)
2194 if (is_connecting(service) == TRUE)
2197 if (service->favorite == FALSE)
2200 if (is_connected(service) == TRUE)
2203 if (is_ignore(service) == FALSE &&
2204 service->state == CONNMAN_SERVICE_STATE_IDLE)
2209 iter = g_sequence_iter_next(iter);
2212 if (service != NULL) {
2213 service->userconnect = FALSE;
2214 __connman_service_connect(service);
2218 static void remove_timeout(struct connman_service *service)
2220 if (service->timeout > 0) {
2221 g_source_remove(service->timeout);
2222 service->timeout = 0;
2226 static void reply_pending(struct connman_service *service, int error)
2228 remove_timeout(service);
2230 if (service->pending != NULL) {
2234 reply = __connman_error_failed(service->pending,
2237 g_dbus_send_message(connection, reply);
2239 g_dbus_send_reply(connection, service->pending,
2242 dbus_message_unref(service->pending);
2243 service->pending = NULL;
2247 static gboolean connect_timeout(gpointer user_data)
2249 struct connman_service *service = user_data;
2250 connman_bool_t autoconnect = FALSE;
2252 DBG("service %p", service);
2254 service->timeout = 0;
2256 if (service->network != NULL)
2257 __connman_network_disconnect(service->network);
2259 __connman_ipconfig_disable(service->ipconfig_ipv4);
2260 __connman_ipconfig_disable(service->ipconfig_ipv6);
2262 __connman_stats_service_unregister(service);
2264 if (service->pending != NULL) {
2267 reply = __connman_error_operation_timeout(service->pending);
2269 g_dbus_send_message(connection, reply);
2271 dbus_message_unref(service->pending);
2272 service->pending = NULL;
2276 __connman_service_indicate_state(service,
2277 CONNMAN_SERVICE_STATE_FAILURE);
2279 if (autoconnect == TRUE && service->userconnect == FALSE)
2280 __connman_service_auto_connect();
2285 static void set_reconnect_state(struct connman_service *service,
2286 connman_bool_t reconnect)
2288 struct connman_device *device;
2290 if (service->network == NULL)
2293 device = connman_network_get_device(service->network);
2297 __connman_device_set_reconnect(device, reconnect);
2300 static connman_bool_t get_reconnect_state(struct connman_service *service)
2302 struct connman_device *device;
2304 if (service->network == NULL)
2307 device = connman_network_get_device(service->network);
2311 return __connman_device_get_reconnect(device);
2314 static void request_input_cb (struct connman_service *service,
2315 const char *passphrase, void *user_data)
2317 DBG ("RequestInput return, %p", service);
2319 if (passphrase == NULL)
2321 __connman_service_set_passphrase(service, passphrase);
2322 __connman_service_connect(service);
2325 struct connman_service *
2326 __connman_service_connect_type(enum connman_service_type type)
2328 struct connman_service *service;
2329 GSequenceIter *iter;
2332 DBG("type %d", type);
2335 * We go through the already sorted service list.
2336 * We pick the first one matching our type, or just
2337 * the first available one if we have no type.
2339 iter = g_sequence_get_begin_iter(service_list);
2340 if (g_sequence_iter_is_end(iter))
2342 service = g_sequence_get(iter);
2345 * If the first service is connected or about to be
2346 * connected, we return it, regardless of the type.
2348 if ((g_sequence_iter_is_end(iter) == FALSE) &&
2349 (is_connecting(service) == TRUE ||
2350 is_connected(service) == TRUE))
2353 while (g_sequence_iter_is_end(iter) == FALSE) {
2354 if (service->type == type ||
2355 type == CONNMAN_SERVICE_TYPE_UNKNOWN)
2358 iter = g_sequence_iter_next(iter);
2359 service = g_sequence_get(iter);
2362 if (g_sequence_iter_is_end(iter))
2365 service->ignore = FALSE;
2367 service->userconnect = TRUE;
2369 set_reconnect_state(service, FALSE);
2371 err = __connman_service_connect(service);
2374 if ( __connman_agent_request_input(service,
2379 if (err != -EINPROGRESS)
2386 static DBusMessage *connect_service(DBusConnection *conn,
2387 DBusMessage *msg, void *user_data)
2389 struct connman_service *service = user_data;
2390 GSequenceIter *iter;
2393 DBG("service %p", service);
2395 if (service->pending != NULL)
2396 return __connman_error_in_progress(msg);
2398 iter = g_sequence_get_begin_iter(service_list);
2400 while (g_sequence_iter_is_end(iter) == FALSE) {
2401 struct connman_service *temp = g_sequence_get(iter);
2403 if (service->type == temp->type &&
2404 is_connecting(temp) == TRUE)
2405 return __connman_error_in_progress(msg);
2407 iter = g_sequence_iter_next(iter);
2410 service->ignore = FALSE;
2412 service->userconnect = TRUE;
2414 service->pending = dbus_message_ref(msg);
2416 set_reconnect_state(service, FALSE);
2418 err = __connman_service_connect(service);
2420 if (err == -ENOKEY) {
2421 if (__connman_agent_request_input(service,
2427 if (service->pending == NULL)
2430 if (err != -EINPROGRESS) {
2431 dbus_message_unref(service->pending);
2432 service->pending = NULL;
2434 return __connman_error_failed(msg, -err);
2440 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2443 static DBusMessage *disconnect_service(DBusConnection *conn,
2444 DBusMessage *msg, void *user_data)
2446 struct connman_service *service = user_data;
2449 DBG("service %p", service);
2451 reply_pending(service, ECONNABORTED);
2453 service->ignore = TRUE;
2455 set_reconnect_state(service, FALSE);
2457 err = __connman_service_disconnect(service);
2459 if (err != -EINPROGRESS)
2460 return __connman_error_failed(msg, -err);
2465 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2468 static DBusMessage *remove_service(DBusConnection *conn,
2469 DBusMessage *msg, void *user_data)
2471 struct connman_service *service = user_data;
2473 DBG("service %p", service);
2475 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2476 return __connman_error_not_supported(msg);
2478 if (service->immutable == TRUE)
2479 return __connman_error_not_supported(msg);
2481 if (service->favorite == FALSE &&
2482 service->state != CONNMAN_SERVICE_STATE_FAILURE)
2483 return __connman_error_not_supported(msg);
2485 if (service->network != NULL) {
2486 set_reconnect_state(service, FALSE);
2488 __connman_network_disconnect(service->network);
2491 g_hash_table_destroy(service->counter_table);
2493 g_free(service->passphrase);
2494 service->passphrase = NULL;
2496 passphrase_changed(service);
2498 g_free(service->apn);
2499 service->apn = NULL;
2501 g_free(service->username);
2502 service->username = NULL;
2504 g_free(service->password);
2505 service->password = NULL;
2507 apn_changed(service);
2511 __connman_service_set_favorite(service, FALSE);
2512 __connman_storage_save_service(service);
2514 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2517 static DBusMessage *move_service(DBusConnection *conn,
2518 DBusMessage *msg, void *user_data,
2521 struct connman_service *service = user_data;
2522 struct connman_service *target;
2524 GSequenceIter *src, *dst;
2526 DBG("service %p", service);
2528 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2531 if (service->favorite == FALSE)
2532 return __connman_error_not_supported(msg);
2534 target = find_service(path);
2535 if (target == NULL || target->favorite == FALSE || target == service)
2536 return __connman_error_invalid_service(msg);
2538 DBG("target %s", target->identifier);
2540 if (target->state != service->state)
2541 return __connman_error_invalid_service(msg);
2543 g_get_current_time(&service->modified);
2544 __connman_storage_save_service(service);
2546 src = g_hash_table_lookup(service_hash, service->identifier);
2547 dst = g_hash_table_lookup(service_hash, target->identifier);
2549 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2551 __connman_profile_changed(FALSE);
2553 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2556 static DBusMessage *move_before(DBusConnection *conn,
2557 DBusMessage *msg, void *user_data)
2559 return move_service(conn, msg, user_data, TRUE);
2562 static DBusMessage *move_after(DBusConnection *conn,
2563 DBusMessage *msg, void *user_data)
2565 return move_service(conn, msg, user_data, FALSE);
2568 static DBusMessage *reset_counters(DBusConnection *conn,
2569 DBusMessage *msg, void *user_data)
2571 struct connman_service *service = user_data;
2573 reset_stats(service);
2575 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2578 static GDBusMethodTable service_methods[] = {
2579 { "GetProperties", "", "a{sv}", get_properties },
2580 { "SetProperty", "sv", "", set_property },
2581 { "ClearProperty", "s", "", clear_property },
2582 { "Connect", "", "", connect_service,
2583 G_DBUS_METHOD_FLAG_ASYNC },
2584 { "Disconnect", "", "", disconnect_service },
2585 { "Remove", "", "", remove_service },
2586 { "MoveBefore", "o", "", move_before },
2587 { "MoveAfter", "o", "", move_after },
2588 { "ResetCounters", "", "", reset_counters },
2592 static GDBusSignalTable service_signals[] = {
2593 { "PropertyChanged", "sv" },
2597 static void service_free(gpointer user_data)
2599 struct connman_service *service = user_data;
2600 char *path = service->path;
2602 DBG("service %p", service);
2604 reply_pending(service, ENOENT);
2606 g_hash_table_remove(service_hash, service->identifier);
2608 stats_stop(service);
2609 __connman_storage_save_service(service);
2611 service->path = NULL;
2614 __connman_profile_changed(FALSE);
2616 g_dbus_unregister_interface(connection, path,
2617 CONNMAN_SERVICE_INTERFACE);
2621 if (service->network != NULL)
2622 connman_network_unref(service->network);
2624 if (service->provider != NULL)
2625 connman_provider_unref(service->provider);
2627 if (service->ipconfig_ipv4 != NULL) {
2628 connman_ipconfig_unref(service->ipconfig_ipv4);
2629 service->ipconfig_ipv4 = NULL;
2632 if (service->ipconfig_ipv6 != NULL) {
2633 connman_ipconfig_unref(service->ipconfig_ipv6);
2634 service->ipconfig_ipv6 = NULL;
2637 if (service->location != NULL)
2638 connman_location_unref(service->location);
2640 g_strfreev(service->nameservers);
2641 g_strfreev(service->domains);
2642 g_strfreev(service->proxies);
2643 g_strfreev(service->excludes);
2645 g_free(service->nameserver);
2646 g_free(service->domainname);
2647 g_free(service->pac);
2648 g_free(service->mcc);
2649 g_free(service->mnc);
2650 g_free(service->apn);
2651 g_free(service->username);
2652 g_free(service->password);
2653 g_free(service->profile);
2654 g_free(service->name);
2655 g_free(service->passphrase);
2656 g_free(service->identifier);
2657 g_free(service->eap);
2658 g_free(service->identity);
2659 g_free(service->ca_cert_file);
2660 g_free(service->client_cert_file);
2661 g_free(service->private_key_file);
2662 g_free(service->private_key_passphrase);
2663 g_free(service->phase2);
2665 if (service->stats.timer != NULL)
2666 g_timer_destroy(service->stats.timer);
2667 if (service->stats_roaming.timer != NULL)
2668 g_timer_destroy(service->stats_roaming.timer);
2674 * __connman_service_put:
2675 * @service: service structure
2677 * Release service if no longer needed
2679 void __connman_service_put(struct connman_service *service)
2681 DBG("service %p", service);
2683 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2684 GSequenceIter *iter;
2686 iter = g_hash_table_lookup(service_hash, service->identifier);
2688 reply_pending(service, ECONNABORTED);
2690 __connman_service_disconnect(service);
2692 g_sequence_remove(iter);
2694 service_free(service);
2698 static void stats_init(struct connman_service *service)
2701 service->stats.valid = FALSE;
2702 service->stats.enabled = FALSE;
2703 service->stats.timer = g_timer_new();
2706 service->stats_roaming.valid = FALSE;
2707 service->stats_roaming.enabled = FALSE;
2708 service->stats_roaming.timer = g_timer_new();
2711 static void service_initialize(struct connman_service *service)
2713 DBG("service %p", service);
2715 service->refcount = 1;
2717 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
2718 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
2719 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
2720 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
2722 service->favorite = FALSE;
2723 service->immutable = FALSE;
2724 service->hidden = FALSE;
2726 service->ignore = FALSE;
2728 service->userconnect = FALSE;
2732 stats_init(service);
2734 service->provider = NULL;
2736 service->wps = FALSE;
2740 * connman_service_create:
2742 * Allocate a new service.
2744 * Returns: a newly-allocated #connman_service structure
2746 struct connman_service *connman_service_create(void)
2749 struct connman_stats_counter *counters;
2750 const char *counter;
2752 struct connman_service *service;
2754 service = g_try_new0(struct connman_service, 1);
2755 if (service == NULL)
2758 DBG("service %p", service);
2760 service->counter_table = g_hash_table_new_full(g_str_hash,
2761 g_str_equal, NULL, g_free);
2763 for (list = counter_list; list; list = list->next) {
2764 counter = list->data;
2766 counters = g_try_new0(struct connman_stats_counter, 1);
2767 if (counters == NULL) {
2768 g_hash_table_destroy(service->counter_table);
2773 counters->append_all = TRUE;
2775 g_hash_table_replace(service->counter_table, (gpointer)counter,
2779 service_initialize(service);
2781 service->location = __connman_location_create(service);
2786 struct connman_location *__connman_service_get_location(struct connman_service *service)
2788 return service->location;
2792 * connman_service_ref:
2793 * @service: service structure
2795 * Increase reference counter of service
2797 struct connman_service *connman_service_ref(struct connman_service *service)
2801 g_atomic_int_inc(&service->refcount);
2807 * connman_service_unref:
2808 * @service: service structure
2810 * Decrease reference counter of service
2812 void connman_service_unref(struct connman_service *service)
2814 __connman_service_put(service);
2817 static gint service_compare(gconstpointer a, gconstpointer b,
2820 struct connman_service *service_a = (void *) a;
2821 struct connman_service *service_b = (void *) b;
2823 if (service_a->state != service_b->state) {
2824 if (is_connected(service_a) == TRUE)
2826 if (is_connected(service_b) == TRUE)
2829 if (is_connecting(service_a) == TRUE)
2831 if (is_connecting(service_b) == TRUE)
2835 if (service_a->order > service_b->order)
2838 if (service_a->order < service_b->order)
2841 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
2844 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
2847 if (service_a->type != service_b->type) {
2848 switch (service_a->type) {
2849 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2850 case CONNMAN_SERVICE_TYPE_SYSTEM:
2851 case CONNMAN_SERVICE_TYPE_ETHERNET:
2852 case CONNMAN_SERVICE_TYPE_GPS:
2853 case CONNMAN_SERVICE_TYPE_VPN:
2854 case CONNMAN_SERVICE_TYPE_GADGET:
2856 case CONNMAN_SERVICE_TYPE_WIFI:
2858 case CONNMAN_SERVICE_TYPE_WIMAX:
2859 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2860 case CONNMAN_SERVICE_TYPE_CELLULAR:
2865 return (gint) service_b->strength - (gint) service_a->strength;
2869 * connman_service_get_type:
2870 * @service: service structure
2872 * Get the type of service
2874 enum connman_service_type connman_service_get_type(struct connman_service *service)
2876 if (service == NULL)
2877 return CONNMAN_SERVICE_TYPE_UNKNOWN;
2879 return service->type;
2883 * connman_service_get_interface:
2884 * @service: service structure
2886 * Get network interface of service
2888 char *connman_service_get_interface(struct connman_service *service)
2892 if (service == NULL)
2895 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
2896 if (service->ipconfig_ipv4)
2897 index = connman_ipconfig_get_index(
2898 service->ipconfig_ipv4);
2899 else if (service->ipconfig_ipv6)
2900 index = connman_ipconfig_get_index(
2901 service->ipconfig_ipv6);
2905 return connman_inet_ifname(index);
2908 if (service->network == NULL)
2911 index = connman_network_get_index(service->network);
2913 return connman_inet_ifname(index);
2917 * connman_service_get_network:
2918 * @service: service structure
2920 * Get the service network
2922 struct connman_network *
2923 __connman_service_get_network(struct connman_service *service)
2925 if (service == NULL)
2928 return service->network;
2931 struct connman_ipconfig *
2932 __connman_service_get_ip4config(struct connman_service *service)
2934 if (service == NULL)
2937 return service->ipconfig_ipv4;
2940 struct connman_ipconfig *
2941 __connman_service_get_ip6config(struct connman_service *service)
2943 if (service == NULL)
2946 return service->ipconfig_ipv6;
2949 enum connman_service_security __connman_service_get_security(struct connman_service *service)
2951 if (service == NULL)
2952 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2954 return service->security;
2957 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
2959 if (service == NULL)
2962 return service->wps;
2966 * __connman_service_set_favorite:
2967 * @service: service structure
2968 * @favorite: favorite value
2970 * Change the favorite setting of service
2972 int __connman_service_set_favorite(struct connman_service *service,
2973 connman_bool_t favorite)
2975 GSequenceIter *iter;
2977 iter = g_hash_table_lookup(service_hash, service->identifier);
2981 if (service->favorite == favorite)
2984 service->favorite = favorite;
2986 favorite_changed(service);
2988 g_sequence_sort_changed(iter, service_compare, NULL);
2990 __connman_profile_changed(FALSE);
2995 int __connman_service_set_immutable(struct connman_service *service,
2996 connman_bool_t immutable)
2998 service->immutable = immutable;
3000 immutable_changed(service);
3005 void __connman_service_set_string(struct connman_service *service,
3006 const char *key, const char *value)
3008 if (g_str_equal(key, "EAP") == TRUE) {
3009 g_free(service->eap);
3010 service->eap = g_strdup(value);
3011 } else if (g_str_equal(key, "Identity") == TRUE) {
3012 g_free(service->identity);
3013 service->identity = g_strdup(value);
3014 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3015 g_free(service->ca_cert_file);
3016 service->ca_cert_file = g_strdup(value);
3017 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3018 g_free(service->client_cert_file);
3019 service->client_cert_file = g_strdup(value);
3020 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3021 g_free(service->private_key_file);
3022 service->private_key_file = g_strdup(value);
3023 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3024 g_free(service->private_key_passphrase);
3025 service->private_key_passphrase = g_strdup(value);
3026 } else if (g_str_equal(key, "Phase2") == TRUE) {
3027 g_free(service->phase2);
3028 service->phase2 = g_strdup(value);
3029 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3030 g_free(service->passphrase);
3031 service->passphrase = g_strdup(value);
3035 static void service_complete(struct connman_service *service)
3037 reply_pending(service, EIO);
3039 if (service->userconnect == FALSE)
3040 __connman_service_auto_connect();
3042 g_get_current_time(&service->modified);
3043 __connman_storage_save_service(service);
3046 static void report_error_cb(struct connman_service *service,
3047 gboolean retry, void *user_data)
3050 __connman_service_connect(service);
3052 service_complete(service);
3053 __connman_profile_changed(FALSE);
3054 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3058 int __connman_service_indicate_state(struct connman_service *service,
3059 enum connman_service_state state)
3061 GSequenceIter *iter;
3063 DBG("service %p state %d", service, state);
3065 if (service == NULL)
3068 if (service->state == state)
3071 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
3072 state == CONNMAN_SERVICE_STATE_IDLE)
3075 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
3076 state == CONNMAN_SERVICE_STATE_DISCONNECT)
3079 if (state == CONNMAN_SERVICE_STATE_IDLE &&
3080 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3081 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
3082 state_changed(service);
3084 reply_pending(service, ECONNABORTED);
3086 __connman_service_disconnect(service);
3089 if (state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3090 if (__connman_stats_service_register(service) == 0) {
3091 __connman_stats_get(service, FALSE,
3092 &service->stats.data);
3093 __connman_stats_get(service, TRUE,
3094 &service->stats_roaming.data);
3097 if (service->ipconfig_ipv4)
3098 __connman_ipconfig_enable(service->ipconfig_ipv4);
3099 if (service->ipconfig_ipv6)
3100 __connman_ipconfig_enable(service->ipconfig_ipv6);
3103 service->state = state;
3104 state_changed(service);
3106 if (state == CONNMAN_SERVICE_STATE_ONLINE) {
3107 if (service->login_required == TRUE) {
3108 service->login_required = FALSE;
3109 login_changed(service);
3112 connman_timeserver_sync();
3115 if (state == CONNMAN_SERVICE_STATE_IDLE) {
3116 connman_bool_t reconnect;
3118 reconnect = get_reconnect_state(service);
3119 if (reconnect == TRUE)
3120 __connman_service_auto_connect();
3123 if (state == CONNMAN_SERVICE_STATE_READY) {
3124 enum connman_service_proxy_method proxy_config;
3126 set_reconnect_state(service, TRUE);
3128 __connman_service_set_favorite(service, TRUE);
3130 reply_pending(service, 0);
3132 service->userconnect = FALSE;
3134 g_get_current_time(&service->modified);
3135 __connman_storage_save_service(service);
3137 update_nameservers(service);
3138 dns_changed(service);
3139 domain_changed(service);
3141 proxy_config = service->proxy_config;
3144 * We start WPAD if we haven't got a PAC URL from DHCP and
3145 * if our proxy manual configuration is either empty or set
3146 * to AUTO with an empty URL.
3148 if (service->proxy == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3149 (proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN ||
3151 CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3152 service->pac == NULL)))
3153 if (__connman_wpad_start(service) < 0) {
3155 CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3157 __connman_notifier_proxy_changed(service);
3160 __connman_notifier_connect(service->type);
3162 if (connman_network_get_bool(service->network,
3163 "WiFi.UseWPS") == TRUE) {
3166 pass = connman_network_get_string(service->network,
3169 __connman_service_set_passphrase(service, pass);
3171 connman_network_set_bool(service->network,
3172 "WiFi.UseWPS", FALSE);
3176 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3177 __connman_location_finish(service);
3181 __connman_wpad_stop(service);
3183 update_nameservers(service);
3184 dns_changed(service);
3185 domain_changed(service);
3187 __connman_notifier_disconnect(service->type);
3190 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
3191 if (service->userconnect == TRUE &&
3192 __connman_agent_report_error(service,
3193 error2string(service->error),
3194 report_error_cb, NULL) == -EIO)
3196 service_complete(service);
3198 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3200 iter = g_hash_table_lookup(service_hash, service->identifier);
3202 g_sequence_sort_changed(iter, service_compare, NULL);
3204 __connman_profile_changed(FALSE);
3206 if (service->state == CONNMAN_SERVICE_STATE_ONLINE)
3209 if (service->state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3210 struct connman_service *def_service = get_default();
3212 if (__connman_notifier_count_connected() == 0 &&
3213 def_service != NULL &&
3214 def_service->provider != NULL)
3215 __connman_provider_disconnect(def_service->provider);
3218 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
3219 service->state == CONNMAN_SERVICE_STATE_FAILURE)
3220 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3225 int __connman_service_indicate_error(struct connman_service *service,
3226 enum connman_service_error error)
3228 DBG("service %p error %d", service, error);
3230 if (service == NULL)
3233 service->error = error;
3235 return __connman_service_indicate_state(service,
3236 CONNMAN_SERVICE_STATE_FAILURE);
3239 int __connman_service_clear_error(struct connman_service *service)
3241 DBG("service %p", service);
3243 if (service == NULL)
3246 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
3249 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3250 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3252 if (service->favorite == TRUE)
3253 set_reconnect_state(service, TRUE);
3255 return __connman_service_indicate_state(service,
3256 CONNMAN_SERVICE_STATE_IDLE);
3259 int __connman_service_indicate_default(struct connman_service *service)
3261 DBG("service %p", service);
3265 __connman_location_detect(service);
3270 int __connman_service_request_login(struct connman_service *service)
3272 DBG("service %p", service);
3274 if (service == NULL)
3277 service->login_required = TRUE;
3278 login_changed(service);
3283 static connman_bool_t prepare_network(struct connman_service *service)
3285 enum connman_network_type type;
3286 unsigned int ssid_len;
3288 type = connman_network_get_type(service->network);
3291 case CONNMAN_NETWORK_TYPE_UNKNOWN:
3292 case CONNMAN_NETWORK_TYPE_VENDOR:
3294 case CONNMAN_NETWORK_TYPE_WIFI:
3295 if (connman_network_get_blob(service->network, "WiFi.SSID",
3299 connman_network_set_string(service->network,
3300 "WiFi.Passphrase", service->passphrase);
3302 case CONNMAN_NETWORK_TYPE_ETHERNET:
3303 case CONNMAN_NETWORK_TYPE_WIMAX:
3304 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3305 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3307 case CONNMAN_NETWORK_TYPE_CELLULAR:
3308 connman_network_set_string(service->network,
3309 "Cellular.APN", service->apn);
3311 connman_network_set_string(service->network,
3312 "Cellular.Username", service->username);
3313 connman_network_set_string(service->network,
3314 "Cellular.Password", service->password);
3321 static void prepare_8021x(struct connman_service *service)
3323 if (service->eap != NULL)
3324 connman_network_set_string(service->network, "WiFi.EAP",
3327 if (service->identity != NULL)
3328 connman_network_set_string(service->network, "WiFi.Identity",
3331 if (service->ca_cert_file != NULL)
3332 connman_network_set_string(service->network, "WiFi.CACertFile",
3333 service->ca_cert_file);
3335 if (service->client_cert_file != NULL)
3336 connman_network_set_string(service->network,
3337 "WiFi.ClientCertFile",
3338 service->client_cert_file);
3340 if (service->private_key_file != NULL)
3341 connman_network_set_string(service->network,
3342 "WiFi.PrivateKeyFile",
3343 service->private_key_file);
3345 if (service->private_key_passphrase != NULL)
3346 connman_network_set_string(service->network,
3347 "WiFi.PrivateKeyPassphrase",
3348 service->private_key_passphrase);
3350 if (service->phase2 != NULL)
3351 connman_network_set_string(service->network, "WiFi.Phase2",
3355 static int service_connect(struct connman_service *service)
3359 switch (service->type) {
3360 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3361 case CONNMAN_SERVICE_TYPE_SYSTEM:
3362 case CONNMAN_SERVICE_TYPE_GPS:
3363 case CONNMAN_SERVICE_TYPE_GADGET:
3365 case CONNMAN_SERVICE_TYPE_ETHERNET:
3366 case CONNMAN_SERVICE_TYPE_WIMAX:
3367 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3368 case CONNMAN_SERVICE_TYPE_VPN:
3370 case CONNMAN_SERVICE_TYPE_CELLULAR:
3371 if (service->apn == NULL)
3374 case CONNMAN_SERVICE_TYPE_WIFI:
3375 switch (service->security) {
3376 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3377 case CONNMAN_SERVICE_SECURITY_NONE:
3379 case CONNMAN_SERVICE_SECURITY_WEP:
3380 case CONNMAN_SERVICE_SECURITY_PSK:
3381 case CONNMAN_SERVICE_SECURITY_WPA:
3382 case CONNMAN_SERVICE_SECURITY_RSN:
3383 if (service->passphrase == NULL)
3386 case CONNMAN_SERVICE_SECURITY_8021X:
3392 if (service->network != NULL) {
3393 if (prepare_network(service) == FALSE)
3396 switch (service->security) {
3397 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3398 case CONNMAN_SERVICE_SECURITY_NONE:
3399 case CONNMAN_SERVICE_SECURITY_WEP:
3400 case CONNMAN_SERVICE_SECURITY_PSK:
3401 case CONNMAN_SERVICE_SECURITY_WPA:
3402 case CONNMAN_SERVICE_SECURITY_RSN:
3404 case CONNMAN_SERVICE_SECURITY_8021X:
3405 prepare_8021x(service);
3409 if (__connman_stats_service_register(service) == 0) {
3410 __connman_stats_get(service, FALSE,
3411 &service->stats.data);
3412 __connman_stats_get(service, TRUE,
3413 &service->stats_roaming.data);
3416 if (service->ipconfig_ipv4)
3417 __connman_ipconfig_enable(service->ipconfig_ipv4);
3418 if (service->ipconfig_ipv6)
3419 __connman_ipconfig_enable(service->ipconfig_ipv6);
3421 err = __connman_network_connect(service->network);
3422 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3423 service->provider != NULL)
3424 err = __connman_provider_connect(service->provider);
3429 if (err != -EINPROGRESS) {
3430 __connman_ipconfig_disable(service->ipconfig_ipv4);
3431 __connman_ipconfig_disable(service->ipconfig_ipv6);
3432 __connman_stats_service_unregister(service);
3440 int __connman_service_connect(struct connman_service *service)
3444 DBG("service %p", service);
3446 if (is_connected(service) == TRUE)
3449 if (is_connecting(service) == TRUE)
3452 switch (service->type) {
3453 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3454 case CONNMAN_SERVICE_TYPE_SYSTEM:
3455 case CONNMAN_SERVICE_TYPE_GPS:
3456 case CONNMAN_SERVICE_TYPE_GADGET:
3459 err = service_connect(service);
3465 if (err == -EINPROGRESS) {
3466 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
3467 connect_timeout, service);
3469 return -EINPROGRESS;
3475 if (service->userconnect == TRUE)
3476 reply_pending(service, err);
3478 __connman_service_indicate_state(service,
3479 CONNMAN_SERVICE_STATE_FAILURE);
3484 int __connman_service_disconnect(struct connman_service *service)
3488 DBG("service %p", service);
3490 if (service->network != NULL) {
3491 err = __connman_network_disconnect(service->network);
3492 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3493 service->provider != NULL)
3494 err = __connman_provider_disconnect(service->provider);
3498 if (err < 0 && err != -EINPROGRESS)
3501 if (service->ipconfig_ipv4)
3502 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3505 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3508 __connman_ipconfig_clear_address(service->ipconfig_ipv4);
3509 __connman_ipconfig_clear_address(service->ipconfig_ipv6);
3511 __connman_ipconfig_disable(service->ipconfig_ipv4);
3512 __connman_ipconfig_disable(service->ipconfig_ipv6);
3514 __connman_stats_service_unregister(service);
3520 * __connman_service_lookup:
3521 * @pattern: search pattern
3522 * @path: return object path
3524 * Look up a service path from a search pattern
3526 int __connman_service_lookup(const char *pattern, const char **path)
3528 GHashTableIter iter;
3529 gpointer key, value;
3531 g_hash_table_iter_init(&iter, service_hash);
3533 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
3534 GSequenceIter *iter = value;
3535 struct connman_service *service = g_sequence_get(iter);
3537 if (g_strcmp0(service->identifier, pattern) == 0 ||
3538 g_strcmp0(service->name, pattern) == 0) {
3539 *path = (const char *) service->path;
3548 * lookup_by_identifier:
3549 * @identifier: service identifier
3551 * Look up a service by identifier (reference count will not be increased)
3553 static struct connman_service *lookup_by_identifier(const char *identifier)
3555 GSequenceIter *iter;
3557 iter = g_hash_table_lookup(service_hash, identifier);
3559 return g_sequence_get(iter);
3564 static struct connman_network *create_hidden_wifi(struct connman_device *device,
3565 const char *ssid, const char *mode, const char *security)
3567 struct connman_network *network;
3570 unsigned int i, ssid_len;
3572 ssid_len = strlen(ssid);
3576 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
3577 if (network == NULL)
3580 connman_network_set_blob(network, "WiFi.SSID",
3581 (unsigned char *) ssid, ssid_len);
3583 connman_network_set_string(network, "WiFi.Mode", mode);
3584 connman_network_set_string(network, "WiFi.Security", security);
3586 name = g_try_malloc0(ssid_len + 1);
3588 connman_network_unref(network);
3592 for (i = 0; i < ssid_len; i++) {
3593 if (g_ascii_isprint(ssid[i]))
3599 connman_network_set_name(network, name);
3603 index = connman_device_get_index(device);
3604 connman_network_set_index(network, index);
3606 if (connman_device_add_network(device, network) < 0) {
3607 connman_network_unref(network);
3611 connman_network_set_available(network, TRUE);
3616 int __connman_service_create_and_connect(DBusMessage *msg)
3618 struct connman_service *service;
3619 struct connman_network *network;
3620 struct connman_device *device;
3621 DBusMessageIter iter, array;
3622 const char *mode = "managed", *security = "none", *group_security;
3623 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
3624 unsigned int ssid_len = 0;
3627 gboolean created = FALSE;
3630 dbus_message_iter_init(msg, &iter);
3631 dbus_message_iter_recurse(&iter, &array);
3633 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
3634 DBusMessageIter entry, value;
3637 dbus_message_iter_recurse(&array, &entry);
3638 dbus_message_iter_get_basic(&entry, &key);
3640 dbus_message_iter_next(&entry);
3641 dbus_message_iter_recurse(&entry, &value);
3643 switch (dbus_message_iter_get_arg_type(&value)) {
3644 case DBUS_TYPE_STRING:
3645 if (g_str_equal(key, "Type") == TRUE)
3646 dbus_message_iter_get_basic(&value, &type);
3647 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
3648 g_str_equal(key, "Mode") == TRUE)
3649 dbus_message_iter_get_basic(&value, &mode);
3650 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
3651 g_str_equal(key, "Security") == TRUE)
3652 dbus_message_iter_get_basic(&value, &security);
3653 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
3654 g_str_equal(key, "Passphrase") == TRUE)
3655 dbus_message_iter_get_basic(&value, &passphrase);
3656 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
3657 g_str_equal(key, "SSID") == TRUE)
3658 dbus_message_iter_get_basic(&value, &ssid);
3661 dbus_message_iter_next(&array);
3667 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
3673 ssid_len = strlen(ssid);
3677 if (g_strcmp0(security, "none") != 0 &&
3678 g_strcmp0(security, "wep") != 0 &&
3679 g_strcmp0(security, "psk") != 0 &&
3680 g_strcmp0(security, "wpa") != 0 &&
3681 g_strcmp0(security, "rsn") != 0 &&
3682 g_strcmp0(security, "ieee8021x") != 0)
3685 device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
3689 ident = connman_device_get_ident(device);
3694 if (!g_strcmp0(security, "wpa") ||
3695 !g_strcmp0(security, "rsn"))
3696 group_security = "psk";
3698 group_security = security;
3700 group = connman_wifi_build_group_name((unsigned char *) ssid,
3701 ssid_len, mode, group_security);
3705 name = g_strdup_printf("%s_%s_%s", type, ident, group);
3707 service = lookup_by_identifier(name);
3709 if (service != NULL)
3712 network = create_hidden_wifi(device, ssid, mode, security);
3713 if (network != NULL) {
3714 connman_network_set_group(network, group);
3718 service = lookup_by_identifier(name);
3724 if (service == NULL) {
3729 if (is_connected(service) == TRUE) {
3734 if (is_connecting(service) == TRUE) {
3739 set_reconnect_state(service, FALSE);
3741 __connman_device_disconnect(device);
3743 if (passphrase != NULL) {
3744 g_free(service->passphrase);
3745 service->passphrase = g_strdup(passphrase);
3748 service->userconnect = TRUE;
3750 err = __connman_service_connect(service);
3751 if (err < 0 && err != -EINPROGRESS)
3754 g_dbus_send_reply(connection, msg,
3755 DBUS_TYPE_OBJECT_PATH, &service->path,
3761 if (service != NULL && created == TRUE) {
3762 struct connman_network *network = service->network;
3764 if (network != NULL) {
3765 connman_network_set_available(network, FALSE);
3766 __connman_device_cleanup_networks(device);
3768 __connman_service_put(service);
3775 * __connman_service_get:
3776 * @identifier: service identifier
3778 * Look up a service by identifier or create a new one if not found
3780 static struct connman_service *service_get(const char *identifier)
3782 struct connman_service *service;
3783 GSequenceIter *iter;
3785 iter = g_hash_table_lookup(service_hash, identifier);
3787 service = g_sequence_get(iter);
3788 if (service != NULL)
3789 connman_service_ref(service);
3793 service = connman_service_create();
3794 if (service == NULL)
3797 DBG("service %p", service);
3799 service->identifier = g_strdup(identifier);
3801 service->profile = g_strdup(__connman_profile_active_ident());
3803 iter = g_sequence_insert_sorted(service_list, service,
3804 service_compare, NULL);
3806 g_hash_table_insert(service_hash, service->identifier, iter);
3811 static int service_register(struct connman_service *service)
3813 const char *path = __connman_profile_active_path();
3814 GSequenceIter *iter;
3816 DBG("service %p", service);
3818 if (service->path != NULL)
3821 service->path = g_strdup_printf("%s/%s", path, service->identifier);
3823 DBG("path %s", service->path);
3825 __connman_config_provision_service(service);
3827 __connman_storage_load_service(service);
3829 g_dbus_register_interface(connection, service->path,
3830 CONNMAN_SERVICE_INTERFACE,
3831 service_methods, service_signals,
3832 NULL, service, NULL);
3834 iter = g_hash_table_lookup(service_hash, service->identifier);
3836 g_sequence_sort_changed(iter, service_compare, NULL);
3838 __connman_profile_changed(TRUE);
3843 static void service_up(struct connman_ipconfig *ipconfig)
3845 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3847 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
3849 link_changed(service);
3851 service->stats.valid = FALSE;
3852 service->stats_roaming.valid = FALSE;
3855 static void service_down(struct connman_ipconfig *ipconfig)
3857 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
3860 static void service_lower_up(struct connman_ipconfig *ipconfig)
3862 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3864 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
3866 stats_start(service);
3869 static void service_lower_down(struct connman_ipconfig *ipconfig)
3871 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3873 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
3875 stats_stop(service);
3876 __connman_storage_save_service(service);
3879 static void service_ip_bound(struct connman_ipconfig *ipconfig)
3881 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3883 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
3885 settings_changed(service);
3888 static void service_ip_release(struct connman_ipconfig *ipconfig)
3890 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3892 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
3894 settings_changed(service);
3897 static const struct connman_ipconfig_ops service_ops = {
3899 .down = service_down,
3900 .lower_up = service_lower_up,
3901 .lower_down = service_lower_down,
3902 .ip_bound = service_ip_bound,
3903 .ip_release = service_ip_release,
3906 static void setup_ip4config(struct connman_service *service, int index)
3911 service->ipconfig_ipv4 = connman_ipconfig_create(index,
3912 CONNMAN_IPCONFIG_TYPE_IPV4);
3913 if (service->ipconfig_ipv4 == NULL)
3916 connman_ipconfig_set_method(service->ipconfig_ipv4,
3917 CONNMAN_IPCONFIG_METHOD_DHCP);
3919 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
3921 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
3924 static void setup_ip6config(struct connman_service *service, int index)
3929 service->ipconfig_ipv6 = connman_ipconfig_create(index,
3930 CONNMAN_IPCONFIG_TYPE_IPV6);
3931 if (service->ipconfig_ipv6 == NULL)
3934 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
3936 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
3939 void __connman_service_create_ip4config(struct connman_service *service,
3942 const char *ident = service->profile;
3945 DBG("ipv4 %p", service->ipconfig_ipv4);
3947 if (service->ipconfig_ipv4 != NULL)
3950 setup_ip4config(service, index);
3955 keyfile = __connman_storage_open_profile(ident);
3956 if (keyfile == NULL)
3959 if (service->ipconfig_ipv4)
3960 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
3961 service->identifier, "IPv4.");
3962 g_key_file_free(keyfile);
3965 void __connman_service_create_ip6config(struct connman_service *service,
3968 const char *ident = service->profile;
3971 DBG("ipv6 %p", service->ipconfig_ipv6);
3973 if (service->ipconfig_ipv6 != NULL)
3976 setup_ip6config(service, index);
3981 keyfile = __connman_storage_open_profile(ident);
3982 if (keyfile == NULL)
3985 if (service->ipconfig_ipv6 != NULL)
3986 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
3987 service->identifier, "IPv6.");
3989 g_key_file_free(keyfile);
3993 * __connman_service_lookup_from_network:
3994 * @network: network structure
3996 * Look up a service by network (reference count will not be increased)
3998 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4000 struct connman_service *service;
4001 const char *ident, *group;
4004 DBG("network %p", network);
4006 ident = __connman_network_get_ident(network);
4010 group = connman_network_get_group(network);
4014 name = g_strdup_printf("%s_%s_%s",
4015 __connman_network_get_type(network), ident, group);
4016 service = lookup_by_identifier(name);
4022 struct connman_service *__connman_service_lookup_from_index(int index)
4024 struct connman_service *service;
4025 GSequenceIter *iter;
4027 iter = g_sequence_get_begin_iter(service_list);
4029 while (g_sequence_iter_is_end(iter) == FALSE) {
4030 service = g_sequence_get(iter);
4032 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4036 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4040 iter = g_sequence_iter_next(iter);
4046 const char *__connman_service_get_ident(struct connman_service *service)
4048 return service->identifier;
4051 const char *__connman_service_get_path(struct connman_service *service)
4053 return service->path;
4056 unsigned int __connman_service_get_order(struct connman_service *service)
4058 GSequenceIter *iter;
4060 if (service == NULL)
4063 if (service->favorite == FALSE) {
4068 iter = g_hash_table_lookup(service_hash, service->identifier);
4070 if (g_sequence_iter_get_position(iter) == 0)
4072 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4073 service->order = 10;
4079 return service->order;
4082 static enum connman_service_type convert_network_type(struct connman_network *network)
4084 enum connman_network_type type = connman_network_get_type(network);
4087 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4088 case CONNMAN_NETWORK_TYPE_VENDOR:
4090 case CONNMAN_NETWORK_TYPE_ETHERNET:
4091 return CONNMAN_SERVICE_TYPE_ETHERNET;
4092 case CONNMAN_NETWORK_TYPE_WIFI:
4093 return CONNMAN_SERVICE_TYPE_WIFI;
4094 case CONNMAN_NETWORK_TYPE_WIMAX:
4095 return CONNMAN_SERVICE_TYPE_WIMAX;
4096 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4097 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4098 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4099 case CONNMAN_NETWORK_TYPE_CELLULAR:
4100 return CONNMAN_SERVICE_TYPE_CELLULAR;
4103 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4106 static enum connman_service_mode convert_wifi_mode(const char *mode)
4109 return CONNMAN_SERVICE_MODE_UNKNOWN;
4110 else if (g_str_equal(mode, "managed") == TRUE)
4111 return CONNMAN_SERVICE_MODE_MANAGED;
4112 else if (g_str_equal(mode, "adhoc") == TRUE)
4113 return CONNMAN_SERVICE_MODE_ADHOC;
4115 return CONNMAN_SERVICE_MODE_UNKNOWN;
4118 static enum connman_service_security convert_wifi_security(const char *security)
4120 if (security == NULL)
4121 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4122 else if (g_str_equal(security, "none") == TRUE)
4123 return CONNMAN_SERVICE_SECURITY_NONE;
4124 else if (g_str_equal(security, "wep") == TRUE)
4125 return CONNMAN_SERVICE_SECURITY_WEP;
4126 else if (g_str_equal(security, "psk") == TRUE)
4127 return CONNMAN_SERVICE_SECURITY_PSK;
4128 else if (g_str_equal(security, "ieee8021x") == TRUE)
4129 return CONNMAN_SERVICE_SECURITY_8021X;
4130 else if (g_str_equal(security, "wpa") == TRUE)
4131 return CONNMAN_SERVICE_SECURITY_WPA;
4132 else if (g_str_equal(security, "rsn") == TRUE)
4133 return CONNMAN_SERVICE_SECURITY_RSN;
4135 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4138 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
4143 return CONNMAN_SERVICE_MODE_GPRS;
4145 return CONNMAN_SERVICE_MODE_EDGE;
4150 return CONNMAN_SERVICE_MODE_UMTS;
4153 return CONNMAN_SERVICE_MODE_UNKNOWN;
4156 static void update_from_network(struct connman_service *service,
4157 struct connman_network *network)
4159 connman_uint8_t strength = service->strength;
4160 GSequenceIter *iter;
4163 DBG("service %p network %p", service, network);
4165 if (is_connected(service) == TRUE)
4168 if (is_connecting(service) == TRUE)
4171 str = connman_network_get_string(network, "Name");
4173 g_free(service->name);
4174 service->name = g_strdup(str);
4175 service->hidden = FALSE;
4177 g_free(service->name);
4178 service->name = NULL;
4179 service->hidden = TRUE;
4182 service->strength = connman_network_get_uint8(network, "Strength");
4183 service->roaming = connman_network_get_bool(network, "Roaming");
4185 if (service->strength == 0) {
4187 * Filter out 0-values; it's unclear what they mean
4188 * and they cause anomalous sorting of the priority list.
4190 service->strength = strength;
4193 str = connman_network_get_string(network, "WiFi.Mode");
4194 service->mode = convert_wifi_mode(str);
4196 str = connman_network_get_string(network, "WiFi.Security");
4197 service->security = convert_wifi_security(str);
4199 str = connman_network_get_string(network, "Cellular.MCC");
4200 g_free(service->mcc);
4201 service->mcc = g_strdup(str);
4203 str = connman_network_get_string(network, "Cellular.MNC");
4204 g_free(service->mnc);
4205 service->mnc = g_strdup(str);
4207 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
4208 connman_uint8_t value = connman_network_get_uint8(network,
4211 service->mode = convert_cellular_mode(value);
4212 } else if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4213 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4215 if (service->strength > strength && service->network != NULL) {
4216 connman_network_unref(service->network);
4217 service->network = connman_network_ref(network);
4219 strength_changed(service);
4222 if (service->network == NULL)
4223 service->network = connman_network_ref(network);
4225 iter = g_hash_table_lookup(service_hash, service->identifier);
4227 g_sequence_sort_changed(iter, service_compare, NULL);
4231 * __connman_service_create_from_network:
4232 * @network: network structure
4234 * Look up service by network and if not found, create one
4236 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4238 struct connman_service *service;
4239 struct connman_device *device;
4240 const char *ident, *group;
4244 DBG("network %p", network);
4246 ident = __connman_network_get_ident(network);
4250 group = connman_network_get_group(network);
4254 name = g_strdup_printf("%s_%s_%s",
4255 __connman_network_get_type(network), ident, group);
4256 service = service_get(name);
4259 if (service == NULL)
4262 if (__connman_network_get_weakness(network) == TRUE)
4265 if (service->path != NULL) {
4266 update_from_network(service, network);
4267 __connman_profile_changed(TRUE);
4271 service->type = convert_network_type(network);
4273 switch (service->type) {
4274 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4275 case CONNMAN_SERVICE_TYPE_SYSTEM:
4276 case CONNMAN_SERVICE_TYPE_ETHERNET:
4277 case CONNMAN_SERVICE_TYPE_WIMAX:
4278 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4279 case CONNMAN_SERVICE_TYPE_GPS:
4280 case CONNMAN_SERVICE_TYPE_VPN:
4281 case CONNMAN_SERVICE_TYPE_GADGET:
4282 service->autoconnect = FALSE;
4284 case CONNMAN_SERVICE_TYPE_WIFI:
4285 case CONNMAN_SERVICE_TYPE_CELLULAR:
4286 service->autoconnect = TRUE;
4290 service->state = CONNMAN_SERVICE_STATE_IDLE;
4292 update_from_network(service, network);
4294 index = connman_network_get_index(network);
4296 if (service->ipconfig_ipv4 == NULL)
4297 setup_ip4config(service, index);
4299 if (service->ipconfig_ipv6 == NULL)
4300 setup_ip6config(service, index);
4302 service_register(service);
4304 if (service->favorite == TRUE) {
4305 device = connman_network_get_device(service->network);
4306 if (device && __connman_device_scanning(device) == FALSE)
4307 __connman_service_auto_connect();
4313 void __connman_service_update_from_network(struct connman_network *network)
4315 struct connman_service *service;
4316 enum connman_service_mode mode;
4317 connman_uint8_t strength, value;
4318 connman_bool_t roaming;
4319 GSequenceIter *iter;
4321 connman_bool_t stats_enable;
4323 DBG("network %p", network);
4325 service = __connman_service_lookup_from_network(network);
4326 if (service == NULL)
4329 if (service->network == NULL)
4332 name = connman_network_get_string(service->network, "Name");
4333 if (g_strcmp0(service->name, name) != 0) {
4334 g_free(service->name);
4335 service->name = g_strdup(name);
4336 connman_dbus_property_changed_basic(service->path,
4337 CONNMAN_SERVICE_INTERFACE, "Name",
4338 DBUS_TYPE_STRING, &service->name);
4341 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4342 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4344 strength = connman_network_get_uint8(service->network, "Strength");
4345 if (strength == service->strength)
4348 service->strength = strength;
4350 strength_changed(service);
4353 roaming = connman_network_get_bool(service->network, "Roaming");
4354 if (roaming == service->roaming)
4357 stats_enable = stats_enabled(service);
4358 if (stats_enable == TRUE)
4359 stats_stop(service);
4361 service->roaming = roaming;
4363 if (stats_enable == TRUE)
4364 stats_start(service);
4366 roaming_changed(service);
4368 iter = g_hash_table_lookup(service_hash, service->identifier);
4370 g_sequence_sort_changed(iter, service_compare, NULL);
4373 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
4376 value = connman_network_get_uint8(service->network, "Cellular.Mode");
4377 mode = convert_cellular_mode(value);
4379 if (mode == service->mode)
4382 service->mode = mode;
4384 mode_changed(service);
4387 void __connman_service_remove_from_network(struct connman_network *network)
4389 struct connman_service *service;
4391 DBG("network %p", network);
4393 service = __connman_service_lookup_from_network(network);
4394 if (service == NULL)
4397 __connman_service_put(service);
4401 * __connman_service_create_from_provider:
4402 * @provider: provider structure
4404 * Look up service by provider and if not found, create one
4406 struct connman_service *
4407 __connman_service_create_from_provider(struct connman_provider *provider)
4409 struct connman_service *service;
4410 const char *ident, *str;
4412 int index = connman_provider_get_index(provider);
4414 DBG("provider %p", provider);
4416 ident = __connman_provider_get_ident(provider);
4420 name = g_strdup_printf("vpn_%s", ident);
4421 service = service_get(name);
4424 if (service == NULL)
4427 service->type = CONNMAN_SERVICE_TYPE_VPN;
4428 service->provider = connman_provider_ref(provider);
4429 service->autoconnect = FALSE;
4431 service->state = CONNMAN_SERVICE_STATE_IDLE;
4433 str = connman_provider_get_string(provider, "Name");
4435 g_free(service->name);
4436 service->name = g_strdup(str);
4437 service->hidden = FALSE;
4439 g_free(service->name);
4440 service->name = NULL;
4441 service->hidden = TRUE;
4444 service->strength = 0;
4446 if (service->ipconfig_ipv4 == NULL)
4447 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4448 CONNMAN_IPCONFIG_TYPE_IPV4);
4449 if (service->ipconfig_ipv4 == NULL)
4452 connman_ipconfig_set_method(service->ipconfig_ipv4,
4453 CONNMAN_IPCONFIG_METHOD_MANUAL);
4454 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4455 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4457 if (service->ipconfig_ipv6 == NULL)
4458 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4459 CONNMAN_IPCONFIG_TYPE_IPV6);
4460 if (service->ipconfig_ipv6 == NULL)
4463 connman_ipconfig_set_method(service->ipconfig_ipv6,
4464 CONNMAN_IPCONFIG_METHOD_OFF);
4465 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4466 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4468 service_register(service);
4473 static int service_load(struct connman_service *service)
4475 const char *ident = service->profile;
4477 GError *error = NULL;
4478 gchar *pathname, *data = NULL;
4481 connman_bool_t autoconnect;
4482 unsigned int ssid_len;
4485 DBG("service %p", service);
4490 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4491 if (pathname == NULL)
4494 keyfile = g_key_file_new();
4496 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
4503 if (g_key_file_load_from_data(keyfile, data, length,
4504 0, NULL) == FALSE) {
4511 switch (service->type) {
4512 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4513 case CONNMAN_SERVICE_TYPE_SYSTEM:
4514 case CONNMAN_SERVICE_TYPE_ETHERNET:
4515 case CONNMAN_SERVICE_TYPE_GPS:
4516 case CONNMAN_SERVICE_TYPE_VPN:
4517 case CONNMAN_SERVICE_TYPE_GADGET:
4519 case CONNMAN_SERVICE_TYPE_WIFI:
4520 if (service->name == NULL) {
4523 name = g_key_file_get_string(keyfile,
4524 service->identifier, "Name", NULL);
4526 g_free(service->name);
4527 service->name = name;
4530 if (service->network != NULL)
4531 connman_network_set_name(service->network,
4535 if (service->network &&
4536 connman_network_get_blob(service->network,
4537 "WiFi.SSID", &ssid_len) == NULL) {
4540 hex_ssid = g_key_file_get_string(keyfile,
4541 service->identifier,
4544 if (hex_ssid != NULL) {
4546 unsigned int i, j = 0, hex;
4547 size_t hex_ssid_len = strlen(hex_ssid);
4549 ssid = g_try_malloc0(hex_ssid_len / 2);
4556 for (i = 0; i < hex_ssid_len; i += 2) {
4557 sscanf(hex_ssid + i, "%02x", &hex);
4561 connman_network_set_blob(service->network,
4562 "WiFi.SSID", ssid, hex_ssid_len / 2);
4569 case CONNMAN_SERVICE_TYPE_WIMAX:
4570 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4571 case CONNMAN_SERVICE_TYPE_CELLULAR:
4572 service->apn = g_key_file_get_string(keyfile,
4573 service->identifier, "APN", NULL);
4575 service->username = g_key_file_get_string(keyfile,
4576 service->identifier, "Username", NULL);
4578 service->password = g_key_file_get_string(keyfile,
4579 service->identifier, "Password", NULL);
4581 service->favorite = g_key_file_get_boolean(keyfile,
4582 service->identifier, "Favorite", NULL);
4584 autoconnect = g_key_file_get_boolean(keyfile,
4585 service->identifier, "AutoConnect", &error);
4587 service->autoconnect = autoconnect;
4588 g_clear_error(&error);
4590 str = g_key_file_get_string(keyfile,
4591 service->identifier, "Failure", NULL);
4593 if (service->favorite == FALSE)
4594 service->state = CONNMAN_SERVICE_STATE_FAILURE;
4595 service->error = string2error(str);
4600 str = g_key_file_get_string(keyfile,
4601 service->identifier, "Modified", NULL);
4603 g_time_val_from_iso8601(str, &service->modified);
4607 str = g_key_file_get_string(keyfile,
4608 service->identifier, "Passphrase", NULL);
4610 g_free(service->passphrase);
4611 service->passphrase = str;
4614 if (service->ipconfig_ipv4 != NULL)
4615 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4616 service->identifier, "IPv4.");
4618 if (service->ipconfig_ipv6 != NULL)
4619 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4620 service->identifier, "IPv6.");
4622 service->nameservers = g_key_file_get_string_list(keyfile,
4623 service->identifier, "Nameservers", &length, NULL);
4624 if (service->nameservers != NULL && length == 0) {
4625 g_strfreev(service->nameservers);
4626 service->nameservers = NULL;
4629 service->domains = g_key_file_get_string_list(keyfile,
4630 service->identifier, "Domains", &length, NULL);
4631 if (service->domains != NULL && length == 0) {
4632 g_strfreev(service->domains);
4633 service->domains = NULL;
4636 str = g_key_file_get_string(keyfile,
4637 service->identifier, "Proxy.Method", NULL);
4639 service->proxy_config = string2proxymethod(str);
4643 service->proxies = g_key_file_get_string_list(keyfile,
4644 service->identifier, "Proxy.Servers", &length, NULL);
4645 if (service->proxies != NULL && length == 0) {
4646 g_strfreev(service->proxies);
4647 service->proxies = NULL;
4650 service->excludes = g_key_file_get_string_list(keyfile,
4651 service->identifier, "Proxy.Excludes", &length, NULL);
4652 if (service->excludes != NULL && length == 0) {
4653 g_strfreev(service->excludes);
4654 service->excludes = NULL;
4657 str = g_key_file_get_string(keyfile,
4658 service->identifier, "Proxy.URL", NULL);
4660 g_free(service->pac);
4665 g_key_file_free(keyfile);
4670 static int service_save(struct connman_service *service)
4672 const char *ident = service->profile;
4674 gchar *pathname, *data = NULL;
4677 const char *cst_str = NULL;
4680 DBG("service %p", service);
4685 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4686 if (pathname == NULL)
4689 keyfile = g_key_file_new();
4691 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
4695 if (g_key_file_load_from_data(keyfile, data, length,
4703 if (service->name != NULL)
4704 g_key_file_set_string(keyfile, service->identifier,
4705 "Name", service->name);
4707 switch (service->type) {
4708 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4709 case CONNMAN_SERVICE_TYPE_SYSTEM:
4710 case CONNMAN_SERVICE_TYPE_ETHERNET:
4711 case CONNMAN_SERVICE_TYPE_GPS:
4712 case CONNMAN_SERVICE_TYPE_VPN:
4713 case CONNMAN_SERVICE_TYPE_GADGET:
4715 case CONNMAN_SERVICE_TYPE_WIFI:
4716 if (service->network) {
4717 const unsigned char *ssid;
4718 unsigned int ssid_len = 0;
4720 ssid = connman_network_get_blob(service->network,
4721 "WiFi.SSID", &ssid_len);
4723 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
4724 char *identifier = service->identifier;
4728 str = g_string_sized_new(ssid_len * 2);
4734 for (i = 0; i < ssid_len; i++)
4735 g_string_append_printf(str,
4738 g_key_file_set_string(keyfile, identifier,
4741 g_string_free(str, TRUE);
4746 case CONNMAN_SERVICE_TYPE_WIMAX:
4747 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4748 case CONNMAN_SERVICE_TYPE_CELLULAR:
4749 if (service->apn != NULL)
4750 g_key_file_set_string(keyfile, service->identifier,
4751 "APN", service->apn);
4753 if (service->username != NULL)
4754 g_key_file_set_string(keyfile, service->identifier,
4755 "Username", service->username);
4757 if (service->password != NULL)
4758 g_key_file_set_string(keyfile, service->identifier,
4759 "Password", service->password);
4761 g_key_file_set_boolean(keyfile, service->identifier,
4762 "Favorite", service->favorite);
4764 if (service->favorite == TRUE)
4765 g_key_file_set_boolean(keyfile, service->identifier,
4766 "AutoConnect", service->autoconnect);
4768 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
4769 const char *failure = error2string(service->error);
4770 if (failure != NULL)
4771 g_key_file_set_string(keyfile,
4772 service->identifier,
4773 "Failure", failure);
4775 g_key_file_remove_key(keyfile, service->identifier,
4781 str = g_time_val_to_iso8601(&service->modified);
4783 g_key_file_set_string(keyfile, service->identifier,
4788 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
4789 g_key_file_set_string(keyfile, service->identifier,
4790 "Passphrase", service->passphrase);
4792 g_key_file_remove_key(keyfile, service->identifier,
4793 "Passphrase", NULL);
4795 if (service->ipconfig_ipv4 != NULL)
4796 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
4797 service->identifier, "IPv4.");
4799 if (service->ipconfig_ipv6 != NULL)
4800 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
4801 service->identifier, "IPv6.");
4803 if (service->nameservers != NULL) {
4804 guint len = g_strv_length(service->nameservers);
4806 g_key_file_set_string_list(keyfile, service->identifier,
4808 (const gchar **) service->nameservers, len);
4810 g_key_file_remove_key(keyfile, service->identifier,
4811 "Nameservers", NULL);
4813 if (service->domains != NULL) {
4814 guint len = g_strv_length(service->domains);
4816 g_key_file_set_string_list(keyfile, service->identifier,
4818 (const gchar **) service->domains, len);
4820 g_key_file_remove_key(keyfile, service->identifier,
4823 cst_str = proxymethod2string(service->proxy_config);
4824 if (cst_str != NULL)
4825 g_key_file_set_string(keyfile, service->identifier,
4826 "Proxy.Method", cst_str);
4828 if (service->proxies != NULL) {
4829 guint len = g_strv_length(service->proxies);
4831 g_key_file_set_string_list(keyfile, service->identifier,
4833 (const gchar **) service->proxies, len);
4835 g_key_file_remove_key(keyfile, service->identifier,
4836 "Proxy.Servers", NULL);
4838 if (service->excludes != NULL) {
4839 guint len = g_strv_length(service->excludes);
4841 g_key_file_set_string_list(keyfile, service->identifier,
4843 (const gchar **) service->excludes, len);
4845 g_key_file_remove_key(keyfile, service->identifier,
4846 "Proxy.Excludes", NULL);
4848 if (service->pac != NULL && strlen(service->pac) > 0)
4849 g_key_file_set_string(keyfile, service->identifier,
4850 "Proxy.URL", service->pac);
4852 g_key_file_remove_key(keyfile, service->identifier,
4855 data = g_key_file_to_data(keyfile, &length, NULL);
4857 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
4858 connman_error("Failed to store service information");
4863 g_key_file_free(keyfile);
4870 static struct connman_storage service_storage = {
4872 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
4873 .service_load = service_load,
4874 .service_save = service_save,
4877 int __connman_service_init(void)
4881 connection = connman_dbus_get_connection();
4883 if (connman_storage_register(&service_storage) < 0)
4884 connman_error("Failed to register service storage");
4886 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
4889 service_list = g_sequence_new(service_free);
4894 void __connman_service_cleanup(void)
4898 g_sequence_free(service_list);
4899 service_list = NULL;
4901 g_hash_table_destroy(service_hash);
4902 service_hash = NULL;
4904 g_slist_free(counter_list);
4905 counter_list = NULL;
4907 connman_storage_unregister(&service_storage);
4909 dbus_connection_unref(connection);