5 * Copyright (C) 2007-2009 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 MAX_CONNECT_RETRIES 2
34 static DBusConnection *connection = NULL;
36 static GSequence *service_list = NULL;
37 static GHashTable *service_hash = NULL;
39 struct connman_service {
43 enum connman_service_type type;
44 enum connman_service_mode mode;
45 enum connman_service_security security;
46 enum connman_service_state state;
47 enum connman_service_error error;
48 connman_uint8_t strength;
49 connman_bool_t favorite;
50 connman_bool_t hidden;
51 connman_bool_t ignore;
52 connman_bool_t autoconnect;
53 connman_bool_t userconnect;
56 unsigned int failcounter;
65 connman_bool_t roaming;
66 struct connman_ipconfig *ipconfig;
67 struct connman_device *device;
68 struct connman_network *network;
73 static void append_path(gpointer value, gpointer user_data)
75 struct connman_service *service = value;
76 DBusMessageIter *iter = user_data;
78 if (service->path == NULL || service->hidden == TRUE)
81 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
85 void __connman_service_list(DBusMessageIter *iter)
89 g_sequence_foreach(service_list, append_path, iter);
94 struct connman_service *service;
97 static void compare_path(gpointer value, gpointer user_data)
99 struct connman_service *service = value;
100 struct find_data *data = user_data;
102 if (data->service != NULL)
105 if (g_strcmp0(service->path, data->path) == 0)
106 data->service = service;
109 static struct connman_service *find_service(const char *path)
111 struct find_data data = { .path = path, .service = NULL };
113 DBG("path %s", path);
115 g_sequence_foreach(service_list, compare_path, &data);
120 static const char *type2string(enum connman_service_type type)
123 case CONNMAN_SERVICE_TYPE_UNKNOWN:
125 case CONNMAN_SERVICE_TYPE_ETHERNET:
127 case CONNMAN_SERVICE_TYPE_WIFI:
129 case CONNMAN_SERVICE_TYPE_WIMAX:
131 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
133 case CONNMAN_SERVICE_TYPE_CELLULAR:
140 static const char *mode2string(enum connman_service_mode mode)
143 case CONNMAN_SERVICE_MODE_UNKNOWN:
145 case CONNMAN_SERVICE_MODE_MANAGED:
147 case CONNMAN_SERVICE_MODE_ADHOC:
149 case CONNMAN_SERVICE_MODE_GPRS:
151 case CONNMAN_SERVICE_MODE_EDGE:
153 case CONNMAN_SERVICE_MODE_UMTS:
160 static const char *security2string(enum connman_service_security security)
163 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
165 case CONNMAN_SERVICE_SECURITY_NONE:
167 case CONNMAN_SERVICE_SECURITY_WEP:
169 case CONNMAN_SERVICE_SECURITY_WPA:
171 case CONNMAN_SERVICE_SECURITY_RSN:
178 static const char *state2string(enum connman_service_state state)
181 case CONNMAN_SERVICE_STATE_UNKNOWN:
183 case CONNMAN_SERVICE_STATE_IDLE:
185 case CONNMAN_SERVICE_STATE_CARRIER:
187 case CONNMAN_SERVICE_STATE_ASSOCIATION:
188 return "association";
189 case CONNMAN_SERVICE_STATE_CONFIGURATION:
190 return "configuration";
191 case CONNMAN_SERVICE_STATE_READY:
193 case CONNMAN_SERVICE_STATE_DISCONNECT:
195 case CONNMAN_SERVICE_STATE_FAILURE:
202 static const char *error2string(enum connman_service_error error)
205 case CONNMAN_SERVICE_ERROR_UNKNOWN:
207 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
208 return "out-of-range";
209 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
210 return "pin-missing";
211 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
212 return "dhcp-failed";
213 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
214 return "connect-failed";
220 static enum connman_service_error string2error(const char *error)
222 if (g_strcmp0(error, "dhcp-failed") == 0)
223 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
224 else if (g_strcmp0(error, "pin-missing") == 0)
225 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
227 return CONNMAN_SERVICE_ERROR_UNKNOWN;
230 const char *__connman_service_default(void)
232 struct connman_service *service;
235 iter = g_sequence_get_begin_iter(service_list);
237 if (g_sequence_iter_is_end(iter) == TRUE)
240 service = g_sequence_get(iter);
244 if (service->state != CONNMAN_SERVICE_STATE_READY)
247 return type2string(service->type);
250 static void mode_changed(struct connman_service *service)
253 DBusMessageIter entry, value;
254 const char *str, *key = "Mode";
256 if (service->path == NULL)
259 str = mode2string(service->mode);
263 signal = dbus_message_new_signal(service->path,
264 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
268 dbus_message_iter_init_append(signal, &entry);
270 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
272 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
273 DBUS_TYPE_STRING_AS_STRING, &value);
274 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
275 dbus_message_iter_close_container(&entry, &value);
277 g_dbus_send_message(connection, signal);
280 static void state_changed(struct connman_service *service)
283 DBusMessageIter entry, value;
284 const char *str, *key = "State";
286 if (service->path == NULL)
289 str = state2string(service->state);
293 signal = dbus_message_new_signal(service->path,
294 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
298 dbus_message_iter_init_append(signal, &entry);
300 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
302 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
303 DBUS_TYPE_STRING_AS_STRING, &value);
304 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
305 dbus_message_iter_close_container(&entry, &value);
307 g_dbus_send_message(connection, signal);
310 static void strength_changed(struct connman_service *service)
313 DBusMessageIter entry, value;
314 const char *key = "Strength";
316 if (service->path == NULL)
319 if (service->strength == 0)
322 signal = dbus_message_new_signal(service->path,
323 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
327 dbus_message_iter_init_append(signal, &entry);
329 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
331 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
332 DBUS_TYPE_BYTE_AS_STRING, &value);
333 dbus_message_iter_append_basic(&value, DBUS_TYPE_BYTE,
335 dbus_message_iter_close_container(&entry, &value);
337 g_dbus_send_message(connection, signal);
340 static void autoconnect_changed(struct connman_service *service)
343 DBusMessageIter entry, value;
344 const char *key = "AutoConnect";
346 if (service->path == NULL)
349 signal = dbus_message_new_signal(service->path,
350 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
354 dbus_message_iter_init_append(signal, &entry);
356 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
358 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
359 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
360 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
361 &service->autoconnect);
362 dbus_message_iter_close_container(&entry, &value);
364 g_dbus_send_message(connection, signal);
367 static void passphrase_changed(struct connman_service *service)
370 DBusMessageIter entry, value;
371 dbus_bool_t required;
372 const char *key = "PassphraseRequired";
374 if (service->path == NULL)
377 switch (service->type) {
378 case CONNMAN_SERVICE_TYPE_UNKNOWN:
379 case CONNMAN_SERVICE_TYPE_ETHERNET:
380 case CONNMAN_SERVICE_TYPE_WIMAX:
381 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
382 case CONNMAN_SERVICE_TYPE_CELLULAR:
384 case CONNMAN_SERVICE_TYPE_WIFI:
387 switch (service->security) {
388 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
389 case CONNMAN_SERVICE_SECURITY_NONE:
391 case CONNMAN_SERVICE_SECURITY_WEP:
392 case CONNMAN_SERVICE_SECURITY_WPA:
393 case CONNMAN_SERVICE_SECURITY_RSN:
394 if (service->passphrase == NULL)
401 signal = dbus_message_new_signal(service->path,
402 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
406 dbus_message_iter_init_append(signal, &entry);
408 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
410 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
411 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
412 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &required);
413 dbus_message_iter_close_container(&entry, &value);
415 g_dbus_send_message(connection, signal);
418 static void apn_changed(struct connman_service *service)
421 DBusMessageIter entry, value;
422 dbus_bool_t required;
423 const char *key = "SetupRequired";
425 if (service->path == NULL)
428 switch (service->type) {
429 case CONNMAN_SERVICE_TYPE_UNKNOWN:
430 case CONNMAN_SERVICE_TYPE_ETHERNET:
431 case CONNMAN_SERVICE_TYPE_WIMAX:
432 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
433 case CONNMAN_SERVICE_TYPE_WIFI:
435 case CONNMAN_SERVICE_TYPE_CELLULAR:
439 required = (service->apn == NULL) ? TRUE : FALSE;
441 signal = dbus_message_new_signal(service->path,
442 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
446 dbus_message_iter_init_append(signal, &entry);
448 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
450 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
451 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
452 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &required);
453 dbus_message_iter_close_container(&entry, &value);
455 g_dbus_send_message(connection, signal);
458 static DBusMessage *get_properties(DBusConnection *conn,
459 DBusMessage *msg, void *user_data)
461 struct connman_service *service = user_data;
463 DBusMessageIter array, dict;
464 dbus_bool_t required;
467 DBG("service %p", service);
469 reply = dbus_message_new_method_return(msg);
473 dbus_message_iter_init_append(reply, &array);
475 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
476 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
477 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
478 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
480 str = type2string(service->type);
482 connman_dbus_dict_append_variant(&dict, "Type",
483 DBUS_TYPE_STRING, &str);
485 str = mode2string(service->mode);
487 connman_dbus_dict_append_variant(&dict, "Mode",
488 DBUS_TYPE_STRING, &str);
490 str = security2string(service->security);
492 connman_dbus_dict_append_variant(&dict, "Security",
493 DBUS_TYPE_STRING, &str);
495 str = state2string(service->state);
497 connman_dbus_dict_append_variant(&dict, "State",
498 DBUS_TYPE_STRING, &str);
500 str = error2string(service->error);
502 connman_dbus_dict_append_variant(&dict, "Error",
503 DBUS_TYPE_STRING, &str);
505 if (service->strength > 0)
506 connman_dbus_dict_append_variant(&dict, "Strength",
507 DBUS_TYPE_BYTE, &service->strength);
509 connman_dbus_dict_append_variant(&dict, "Favorite",
510 DBUS_TYPE_BOOLEAN, &service->favorite);
512 if (service->favorite == TRUE)
513 connman_dbus_dict_append_variant(&dict, "AutoConnect",
514 DBUS_TYPE_BOOLEAN, &service->autoconnect);
516 connman_dbus_dict_append_variant(&dict, "AutoConnect",
517 DBUS_TYPE_BOOLEAN, &service->favorite);
519 if (service->name != NULL)
520 connman_dbus_dict_append_variant(&dict, "Name",
521 DBUS_TYPE_STRING, &service->name);
523 switch (service->type) {
524 case CONNMAN_SERVICE_TYPE_UNKNOWN:
525 case CONNMAN_SERVICE_TYPE_ETHERNET:
526 case CONNMAN_SERVICE_TYPE_WIMAX:
527 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
529 case CONNMAN_SERVICE_TYPE_CELLULAR:
530 connman_dbus_dict_append_variant(&dict, "Roaming",
531 DBUS_TYPE_BOOLEAN, &service->roaming);
533 if (service->mcc != NULL && service->mnc != NULL) {
534 connman_dbus_dict_append_variant(&dict, "MCC",
535 DBUS_TYPE_STRING, &service->mcc);
536 connman_dbus_dict_append_variant(&dict, "MNC",
537 DBUS_TYPE_STRING, &service->mnc);
540 if (service->apn != NULL) {
541 connman_dbus_dict_append_variant(&dict, "APN",
542 DBUS_TYPE_STRING, &service->apn);
544 if (service->username != NULL)
545 connman_dbus_dict_append_variant(&dict,
546 "Username", DBUS_TYPE_STRING,
549 if (service->password != NULL)
550 connman_dbus_dict_append_variant(&dict,
551 "Password", DBUS_TYPE_STRING,
558 connman_dbus_dict_append_variant(&dict, "SetupRequired",
559 DBUS_TYPE_BOOLEAN, &required);
561 case CONNMAN_SERVICE_TYPE_WIFI:
562 if (service->passphrase != NULL &&
563 __connman_security_check_privilege(msg,
564 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
565 connman_dbus_dict_append_variant(&dict, "Passphrase",
566 DBUS_TYPE_STRING, &service->passphrase);
570 switch (service->security) {
571 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
572 case CONNMAN_SERVICE_SECURITY_NONE:
574 case CONNMAN_SERVICE_SECURITY_WEP:
575 case CONNMAN_SERVICE_SECURITY_WPA:
576 case CONNMAN_SERVICE_SECURITY_RSN:
577 if (service->passphrase == NULL)
582 connman_dbus_dict_append_variant(&dict, "PassphraseRequired",
583 DBUS_TYPE_BOOLEAN, &required);
587 if (service->ipconfig != NULL)
588 __connman_ipconfig_append_ipv4(service->ipconfig,
591 dbus_message_iter_close_container(&array, &dict);
596 static DBusMessage *set_property(DBusConnection *conn,
597 DBusMessage *msg, void *user_data)
599 struct connman_service *service = user_data;
600 DBusMessageIter iter, value;
604 DBG("service %p", service);
606 if (dbus_message_iter_init(msg, &iter) == FALSE)
607 return __connman_error_invalid_arguments(msg);
609 dbus_message_iter_get_basic(&iter, &name);
610 dbus_message_iter_next(&iter);
611 dbus_message_iter_recurse(&iter, &value);
613 if (__connman_security_check_privilege(msg,
614 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
615 return __connman_error_permission_denied(msg);
617 type = dbus_message_iter_get_arg_type(&value);
619 if (g_str_has_prefix(name, "AutoConnect") == TRUE) {
620 connman_bool_t autoconnect;
622 if (type != DBUS_TYPE_BOOLEAN)
623 return __connman_error_invalid_arguments(msg);
625 if (service->favorite == FALSE)
626 return __connman_error_invalid_service(msg);
628 dbus_message_iter_get_basic(&value, &autoconnect);
630 if (service->autoconnect == autoconnect)
631 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
633 service->autoconnect = autoconnect;
635 autoconnect_changed(service);
637 __connman_storage_save_service(service);
638 } else if (g_str_equal(name, "Passphrase") == TRUE) {
639 const char *passphrase;
641 if (type != DBUS_TYPE_STRING)
642 return __connman_error_invalid_arguments(msg);
644 if (__connman_security_check_privilege(msg,
645 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
646 return __connman_error_permission_denied(msg);
648 dbus_message_iter_get_basic(&value, &passphrase);
650 g_free(service->passphrase);
651 service->passphrase = g_strdup(passphrase);
653 passphrase_changed(service);
655 if (service->network != NULL)
656 connman_network_set_string(service->network,
657 "WiFi.Passphrase", service->passphrase);
659 __connman_storage_save_service(service);
660 } else if (g_str_equal(name, "APN") == TRUE) {
663 if (type != DBUS_TYPE_STRING)
664 return __connman_error_invalid_arguments(msg);
666 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
667 return __connman_error_invalid_service(msg);
669 dbus_message_iter_get_basic(&value, &apn);
671 g_free(service->apn);
672 service->apn = g_strdup(apn);
674 apn_changed(service);
676 if (service->network != NULL)
677 connman_network_set_string(service->network,
678 "Cellular.APN", service->apn);
680 __connman_storage_save_service(service);
681 } else if (g_str_equal(name, "Username") == TRUE) {
682 const char *username;
684 if (type != DBUS_TYPE_STRING)
685 return __connman_error_invalid_arguments(msg);
687 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
688 return __connman_error_invalid_service(msg);
690 dbus_message_iter_get_basic(&value, &username);
692 g_free(service->username);
693 service->username = g_strdup(username);
695 if (service->network != NULL)
696 connman_network_set_string(service->network,
697 "Cellular.Username", service->username);
699 __connman_storage_save_service(service);
700 } else if (g_str_equal(name, "Password") == TRUE) {
701 const char *password;
703 if (type != DBUS_TYPE_STRING)
704 return __connman_error_invalid_arguments(msg);
706 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
707 return __connman_error_invalid_service(msg);
709 dbus_message_iter_get_basic(&value, &password);
711 g_free(service->password);
712 service->password = g_strdup(password);
714 if (service->network != NULL)
715 connman_network_set_string(service->network,
716 "Cellular.Password", service->password);
718 __connman_storage_save_service(service);
719 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
722 if (service->ipconfig == NULL)
723 return __connman_error_invalid_property(msg);
725 err = __connman_ipconfig_set_ipv4(service->ipconfig,
728 return __connman_error_failed(msg, -err);
730 return __connman_error_invalid_property(msg);
732 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
735 static void set_idle(struct connman_service *service)
737 service->state = CONNMAN_SERVICE_STATE_IDLE;
738 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
739 state_changed(service);
742 static DBusMessage *clear_property(DBusConnection *conn,
743 DBusMessage *msg, void *user_data)
745 struct connman_service *service = user_data;
748 DBG("service %p", service);
750 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
753 if (__connman_security_check_privilege(msg,
754 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
755 return __connman_error_permission_denied(msg);
757 if (g_str_equal(name, "Error") == TRUE) {
760 g_get_current_time(&service->modified);
761 __connman_storage_save_service(service);
762 } else if (g_str_equal(name, "Passphrase") == TRUE) {
763 g_free(service->passphrase);
764 service->passphrase = NULL;
766 passphrase_changed(service);
768 __connman_storage_save_service(service);
770 return __connman_error_invalid_property(msg);
772 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
775 static connman_bool_t is_connecting(struct connman_service *service)
777 switch (service->state) {
778 case CONNMAN_SERVICE_STATE_UNKNOWN:
779 case CONNMAN_SERVICE_STATE_IDLE:
780 case CONNMAN_SERVICE_STATE_CARRIER:
781 case CONNMAN_SERVICE_STATE_FAILURE:
782 case CONNMAN_SERVICE_STATE_DISCONNECT:
783 case CONNMAN_SERVICE_STATE_READY:
785 case CONNMAN_SERVICE_STATE_ASSOCIATION:
786 case CONNMAN_SERVICE_STATE_CONFIGURATION:
793 static connman_bool_t is_ignore(struct connman_service *service)
795 if (service->autoconnect == FALSE)
798 if (service->ignore == TRUE)
801 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
807 void __connman_service_auto_connect(void)
809 struct connman_service *service = NULL;
814 iter = g_sequence_get_begin_iter(service_list);
816 while (g_sequence_iter_is_end(iter) == FALSE) {
817 service = g_sequence_get(iter);
819 if (service->pending != NULL)
822 if (is_connecting(service) == TRUE)
825 if (service->favorite == FALSE)
828 if (service->state == CONNMAN_SERVICE_STATE_READY)
831 if (is_ignore(service) == FALSE &&
832 service->state == CONNMAN_SERVICE_STATE_IDLE)
837 iter = g_sequence_iter_next(iter);
840 if (service != NULL) {
841 service->userconnect = FALSE;
842 __connman_service_connect(service);
846 static void reply_pending(struct connman_service *service, int error)
848 if (service->timeout > 0) {
849 g_source_remove(service->timeout);
850 service->timeout = 0;
853 if (service->pending != NULL) {
857 reply = __connman_error_failed(service->pending,
860 g_dbus_send_message(connection, reply);
862 g_dbus_send_reply(connection, service->pending,
865 dbus_message_unref(service->pending);
866 service->pending = NULL;
870 static gboolean connect_timeout(gpointer user_data)
872 struct connman_service *service = user_data;
873 connman_bool_t autoconnect = FALSE;
875 DBG("service %p", service);
877 service->timeout = 0;
879 if (service->network != NULL)
880 __connman_network_disconnect(service->network);
881 else if (service->device != NULL)
882 __connman_device_disconnect(service->device);
884 __connman_ipconfig_disable(service->ipconfig);
886 if (service->pending != NULL) {
889 reply = __connman_error_operation_timeout(service->pending);
891 g_dbus_send_message(connection, reply);
893 dbus_message_unref(service->pending);
894 service->pending = NULL;
898 __connman_service_indicate_state(service,
899 CONNMAN_SERVICE_STATE_FAILURE);
901 if (autoconnect == TRUE && service->userconnect == FALSE)
902 __connman_service_auto_connect();
907 static DBusMessage *connect_service(DBusConnection *conn,
908 DBusMessage *msg, void *user_data)
910 struct connman_service *service = user_data;
914 DBG("service %p", service);
916 if (service->pending != NULL)
917 return __connman_error_in_progress(msg);
919 iter = g_sequence_get_begin_iter(service_list);
921 while (g_sequence_iter_is_end(iter) == FALSE) {
922 struct connman_service *temp = g_sequence_get(iter);
924 if (service->type == temp->type &&
925 is_connecting(temp) == TRUE)
926 return __connman_error_in_progress(msg);
928 iter = g_sequence_iter_next(iter);
931 service->ignore = FALSE;
933 service->userconnect = TRUE;
934 service->failcounter = 0;
936 service->pending = dbus_message_ref(msg);
938 err = __connman_service_connect(service);
940 if (err == -ENOKEY) {
941 if (__connman_agent_request_passphrase(service,
946 if (err != -EINPROGRESS) {
947 dbus_message_unref(service->pending);
948 service->pending = NULL;
950 return __connman_error_failed(msg, -err);
956 dbus_message_unref(service->pending);
957 service->pending = NULL;
959 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
962 static DBusMessage *disconnect_service(DBusConnection *conn,
963 DBusMessage *msg, void *user_data)
965 struct connman_service *service = user_data;
968 DBG("service %p", service);
970 reply_pending(service, ECONNABORTED);
972 service->ignore = TRUE;
974 err = __connman_service_disconnect(service);
976 if (err != -EINPROGRESS)
977 return __connman_error_failed(msg, -err);
982 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
985 static DBusMessage *remove_service(DBusConnection *conn,
986 DBusMessage *msg, void *user_data)
988 struct connman_service *service = user_data;
990 DBG("service %p", service);
992 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
993 return __connman_error_not_supported(msg);
995 if (service->favorite == FALSE)
996 return __connman_error_not_supported(msg);
998 if (service->network != NULL)
999 __connman_network_disconnect(service->network);
1001 g_free(service->passphrase);
1002 service->passphrase = NULL;
1004 passphrase_changed(service);
1006 g_free(service->apn);
1007 service->apn = NULL;
1009 g_free(service->username);
1010 service->username = NULL;
1012 g_free(service->password);
1013 service->password = NULL;
1015 apn_changed(service);
1017 connman_service_set_favorite(service, FALSE);
1018 __connman_storage_save_service(service);
1020 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1023 static DBusMessage *move_before(DBusConnection *conn,
1024 DBusMessage *msg, void *user_data)
1026 struct connman_service *service = user_data;
1027 struct connman_service *target;
1029 GSequenceIter *src, *dst;
1031 DBG("service %p", service);
1033 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1036 if (service->favorite == FALSE)
1037 return __connman_error_not_supported(msg);
1039 target = find_service(path);
1040 if (target == NULL || target->favorite == FALSE || target == service)
1041 return __connman_error_invalid_service(msg);
1043 DBG("target %s", target->identifier);
1045 if (target->state != service->state)
1046 return __connman_error_invalid_service(msg);
1048 g_get_current_time(&service->modified);
1049 __connman_storage_save_service(service);
1051 src = g_hash_table_lookup(service_hash, service->identifier);
1052 dst = g_hash_table_lookup(service_hash, target->identifier);
1054 g_sequence_move(src, dst);
1056 __connman_profile_changed(FALSE);
1058 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1061 static DBusMessage *move_after(DBusConnection *conn,
1062 DBusMessage *msg, void *user_data)
1064 struct connman_service *service = user_data;
1065 struct connman_service *target;
1068 DBG("service %p", service);
1070 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1073 if (service->favorite == FALSE)
1074 return __connman_error_not_supported(msg);
1076 target = find_service(path);
1077 if (target == NULL || target->favorite == FALSE || target == service)
1078 return __connman_error_invalid_service(msg);
1080 DBG("target %s", target->identifier);
1082 if (target->state != service->state)
1083 return __connman_error_invalid_service(msg);
1085 g_get_current_time(&service->modified);
1086 __connman_storage_save_service(service);
1088 return __connman_error_not_implemented(msg);
1091 static GDBusMethodTable service_methods[] = {
1092 { "GetProperties", "", "a{sv}", get_properties },
1093 { "SetProperty", "sv", "", set_property },
1094 { "ClearProperty", "s", "", clear_property },
1095 { "Connect", "", "", connect_service,
1096 G_DBUS_METHOD_FLAG_ASYNC },
1097 { "Disconnect", "", "", disconnect_service },
1098 { "Remove", "", "", remove_service },
1099 { "MoveBefore", "o", "", move_before },
1100 { "MoveAfter", "o", "", move_after },
1104 static GDBusSignalTable service_signals[] = {
1105 { "PropertyChanged", "sv" },
1109 static void service_free(gpointer user_data)
1111 struct connman_service *service = user_data;
1112 char *path = service->path;
1114 DBG("service %p", service);
1116 reply_pending(service, ENOENT);
1118 g_hash_table_remove(service_hash, service->identifier);
1120 service->path = NULL;
1123 __connman_profile_changed(FALSE);
1125 g_dbus_unregister_interface(connection, path,
1126 CONNMAN_SERVICE_INTERFACE);
1130 if (service->network != NULL)
1131 connman_network_unref(service->network);
1133 if (service->ipconfig != NULL) {
1134 connman_ipconfig_unref(service->ipconfig);
1135 service->ipconfig = NULL;
1138 g_free(service->mcc);
1139 g_free(service->mnc);
1140 g_free(service->apn);
1141 g_free(service->username);
1142 g_free(service->password);
1143 g_free(service->profile);
1144 g_free(service->name);
1145 g_free(service->passphrase);
1146 g_free(service->identifier);
1151 * __connman_service_put:
1152 * @service: service structure
1154 * Release service if no longer needed
1156 void __connman_service_put(struct connman_service *service)
1158 DBG("service %p", service);
1160 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
1161 GSequenceIter *iter;
1163 iter = g_hash_table_lookup(service_hash, service->identifier);
1165 reply_pending(service, ECONNABORTED);
1167 __connman_service_disconnect(service);
1169 g_sequence_remove(iter);
1171 service_free(service);
1175 static void __connman_service_initialize(struct connman_service *service)
1177 DBG("service %p", service);
1179 service->refcount = 1;
1181 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
1182 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
1183 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
1184 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
1186 service->favorite = FALSE;
1187 service->hidden = FALSE;
1189 service->ignore = FALSE;
1191 service->userconnect = FALSE;
1197 * connman_service_create:
1199 * Allocate a new service.
1201 * Returns: a newly-allocated #connman_service structure
1203 struct connman_service *connman_service_create(void)
1205 struct connman_service *service;
1207 service = g_try_new0(struct connman_service, 1);
1208 if (service == NULL)
1211 DBG("service %p", service);
1213 __connman_service_initialize(service);
1219 * connman_service_ref:
1220 * @service: service structure
1222 * Increase reference counter of service
1224 struct connman_service *connman_service_ref(struct connman_service *service)
1226 g_atomic_int_inc(&service->refcount);
1232 * connman_service_unref:
1233 * @service: service structure
1235 * Decrease reference counter of service
1237 void connman_service_unref(struct connman_service *service)
1239 __connman_service_put(service);
1242 static gint service_compare(gconstpointer a, gconstpointer b,
1245 struct connman_service *service_a = (void *) a;
1246 struct connman_service *service_b = (void *) b;
1248 if (service_a->state != service_b->state) {
1249 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
1251 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
1255 if (service_a->order > service_b->order)
1258 if (service_a->order < service_b->order)
1261 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
1264 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
1267 if (service_a->type != service_b->type) {
1268 switch (service_a->type) {
1269 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1270 case CONNMAN_SERVICE_TYPE_ETHERNET:
1272 case CONNMAN_SERVICE_TYPE_WIFI:
1274 case CONNMAN_SERVICE_TYPE_WIMAX:
1275 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1276 case CONNMAN_SERVICE_TYPE_CELLULAR:
1281 return (gint) service_b->strength - (gint) service_a->strength;
1285 * connman_service_set_favorite:
1286 * @service: service structure
1287 * @favorite: favorite value
1289 * Change the favorite setting of service
1291 int connman_service_set_favorite(struct connman_service *service,
1292 connman_bool_t favorite)
1294 GSequenceIter *iter;
1296 iter = g_hash_table_lookup(service_hash, service->identifier);
1300 if (service->favorite == favorite)
1303 service->favorite = favorite;
1305 g_sequence_sort_changed(iter, service_compare, NULL);
1307 __connman_profile_changed(FALSE);
1312 static void default_changed(void)
1314 DBusMessage *signal;
1315 DBusMessageIter entry, value;
1316 const char *key = "DefaultTechnology";
1317 const char *str = __connman_service_default();
1319 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1320 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1324 dbus_message_iter_init_append(signal, &entry);
1326 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1328 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1329 DBUS_TYPE_STRING_AS_STRING, &value);
1330 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
1331 dbus_message_iter_close_container(&entry, &value);
1333 g_dbus_send_message(connection, signal);
1336 int __connman_service_indicate_state(struct connman_service *service,
1337 enum connman_service_state state)
1339 GSequenceIter *iter;
1341 DBG("service %p state %d", service, state);
1343 if (service == NULL)
1346 if (service->state == state)
1349 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
1350 state == CONNMAN_SERVICE_STATE_IDLE)
1353 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
1354 state == CONNMAN_SERVICE_STATE_DISCONNECT)
1357 if (state == CONNMAN_SERVICE_STATE_IDLE &&
1358 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
1359 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
1360 state_changed(service);
1362 reply_pending(service, ECONNABORTED);
1364 __connman_service_disconnect(service);
1367 service->state = state;
1368 state_changed(service);
1370 if (state == CONNMAN_SERVICE_STATE_READY) {
1371 connman_service_set_favorite(service, TRUE);
1373 reply_pending(service, 0);
1375 service->userconnect = FALSE;
1376 service->failcounter = 0;
1378 g_get_current_time(&service->modified);
1379 __connman_storage_save_service(service);
1381 __connman_notifier_connect(service->type);
1384 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
1387 __connman_notifier_disconnect(service->type);
1390 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
1391 if (service->failcounter++ < MAX_CONNECT_RETRIES) {
1392 connman_warn("Connecting again (try %d)",
1393 service->failcounter);
1394 __connman_service_disconnect(service);
1395 __connman_service_connect(service);
1399 reply_pending(service, EIO);
1401 if (service->userconnect == FALSE)
1402 __connman_service_auto_connect();
1404 g_get_current_time(&service->modified);
1405 __connman_storage_save_service(service);
1407 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1409 iter = g_hash_table_lookup(service_hash, service->identifier);
1411 g_sequence_sort_changed(iter, service_compare, NULL);
1413 __connman_profile_changed(FALSE);
1415 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
1416 service->state == CONNMAN_SERVICE_STATE_FAILURE)
1417 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
1422 int __connman_service_indicate_error(struct connman_service *service,
1423 enum connman_service_error error)
1425 DBG("service %p error %d", service, error);
1427 if (service == NULL)
1430 service->error = error;
1432 return __connman_service_indicate_state(service,
1433 CONNMAN_SERVICE_STATE_FAILURE);
1436 int __connman_service_indicate_default(struct connman_service *service)
1438 DBG("service %p", service);
1445 static connman_bool_t prepare_network(struct connman_service *service)
1447 enum connman_network_type type;
1448 unsigned int ssid_len;
1450 type = connman_network_get_type(service->network);
1453 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1454 case CONNMAN_NETWORK_TYPE_VENDOR:
1456 case CONNMAN_NETWORK_TYPE_WIFI:
1457 if (connman_network_get_blob(service->network, "WiFi.SSID",
1461 connman_network_set_string(service->network,
1462 "WiFi.Passphrase", service->passphrase);
1464 case CONNMAN_NETWORK_TYPE_WIMAX:
1465 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1466 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1468 case CONNMAN_NETWORK_TYPE_MBM:
1469 case CONNMAN_NETWORK_TYPE_HSO:
1470 connman_network_set_string(service->network,
1471 "Cellular.APN", service->apn);
1473 connman_network_set_string(service->network,
1474 "Cellular.Username", service->username);
1475 connman_network_set_string(service->network,
1476 "Cellular.Password", service->password);
1483 int __connman_service_connect(struct connman_service *service)
1487 DBG("service %p", service);
1489 if (service->state == CONNMAN_SERVICE_STATE_READY)
1492 if (is_connecting(service) == TRUE)
1495 switch (service->type) {
1496 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1498 case CONNMAN_SERVICE_TYPE_ETHERNET:
1499 case CONNMAN_SERVICE_TYPE_WIMAX:
1500 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1502 case CONNMAN_SERVICE_TYPE_CELLULAR:
1503 if (service->apn == NULL)
1506 case CONNMAN_SERVICE_TYPE_WIFI:
1507 switch (service->security) {
1508 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1509 case CONNMAN_SERVICE_SECURITY_NONE:
1511 case CONNMAN_SERVICE_SECURITY_WEP:
1512 case CONNMAN_SERVICE_SECURITY_WPA:
1513 case CONNMAN_SERVICE_SECURITY_RSN:
1514 if (service->passphrase == NULL)
1521 if (service->network != NULL) {
1522 if (prepare_network(service) == FALSE)
1525 __connman_ipconfig_enable(service->ipconfig);
1527 err = __connman_network_connect(service->network);
1528 } else if (service->device != NULL) {
1529 if (service->favorite == FALSE)
1532 __connman_ipconfig_enable(service->ipconfig);
1534 err = __connman_device_connect(service->device);
1539 if (err != -EINPROGRESS) {
1540 __connman_ipconfig_disable(service->ipconfig);
1544 service->timeout = g_timeout_add_seconds(45,
1545 connect_timeout, service);
1547 return -EINPROGRESS;
1553 int __connman_service_disconnect(struct connman_service *service)
1557 DBG("service %p", service);
1559 if (service->network != NULL) {
1560 err = __connman_network_disconnect(service->network);
1561 } else if (service->device != NULL) {
1562 if (service->favorite == FALSE)
1564 err = __connman_device_disconnect(service->device);
1568 __connman_ipconfig_disable(service->ipconfig);
1571 if (err != -EINPROGRESS)
1574 return -EINPROGRESS;
1581 * __connman_service_lookup:
1582 * @identifier: service identifier
1584 * Look up a service by identifier (reference count will not be increased)
1586 static struct connman_service *__connman_service_lookup(const char *identifier)
1588 GSequenceIter *iter;
1590 iter = g_hash_table_lookup(service_hash, identifier);
1592 return g_sequence_get(iter);
1597 static struct connman_network *create_hidden_wifi(struct connman_device *device,
1598 const char *ssid, const char *mode, const char *security)
1600 struct connman_network *network;
1603 unsigned int i, ssid_len;
1605 ssid_len = strlen(ssid);
1609 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
1610 if (network == NULL)
1613 connman_network_set_blob(network, "WiFi.SSID",
1614 (unsigned char *) ssid, ssid_len);
1616 connman_network_set_string(network, "WiFi.Mode", mode);
1617 connman_network_set_string(network, "WiFi.Security", security);
1619 name = g_try_malloc0(ssid_len + 1);
1621 connman_network_unref(network);
1625 for (i = 0; i < ssid_len; i++) {
1626 if (g_ascii_isprint(ssid[i]))
1632 connman_network_set_name(network, name);
1636 index = connman_device_get_index(device);
1637 connman_network_set_index(network, index);
1639 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
1641 if (connman_device_add_network(device, network) < 0) {
1642 connman_network_unref(network);
1646 connman_network_set_available(network, TRUE);
1651 int __connman_service_create_and_connect(DBusMessage *msg)
1653 struct connman_service *service;
1654 struct connman_network *network;
1655 struct connman_device *device;
1656 DBusMessageIter iter, array;
1657 const char *mode = "managed", *security = "none";
1658 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
1659 unsigned int ssid_len = 0;
1662 gboolean created = FALSE;
1665 dbus_message_iter_init(msg, &iter);
1666 dbus_message_iter_recurse(&iter, &array);
1668 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1669 DBusMessageIter entry, value;
1672 dbus_message_iter_recurse(&array, &entry);
1673 dbus_message_iter_get_basic(&entry, &key);
1675 dbus_message_iter_next(&entry);
1676 dbus_message_iter_recurse(&entry, &value);
1678 switch (dbus_message_iter_get_arg_type(&value)) {
1679 case DBUS_TYPE_STRING:
1680 if (g_str_equal(key, "Type") == TRUE)
1681 dbus_message_iter_get_basic(&value, &type);
1682 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
1683 g_str_equal(key, "Mode") == TRUE)
1684 dbus_message_iter_get_basic(&value, &mode);
1685 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
1686 g_str_equal(key, "Security") == TRUE)
1687 dbus_message_iter_get_basic(&value, &security);
1688 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
1689 g_str_equal(key, "Passphrase") == TRUE)
1690 dbus_message_iter_get_basic(&value, &passphrase);
1691 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
1692 g_str_equal(key, "SSID") == TRUE)
1693 dbus_message_iter_get_basic(&value, &ssid);
1696 dbus_message_iter_next(&array);
1702 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
1708 ssid_len = strlen(ssid);
1712 if (g_strcmp0(security, "none") != 0 &&
1713 g_strcmp0(security, "wep") != 0 &&
1714 g_strcmp0(security, "wpa") != 0 &&
1715 g_strcmp0(security, "rsn") != 0)
1718 device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
1722 ident = __connman_device_get_ident(device);
1726 group = connman_wifi_build_group_name((unsigned char *) ssid,
1727 ssid_len, mode, security);
1731 name = g_strdup_printf("%s_%s_%s", type, ident, group);
1733 service = __connman_service_lookup(name);
1735 if (service != NULL)
1738 network = create_hidden_wifi(device, ssid, mode, security);
1739 if (network != NULL) {
1740 connman_network_set_group(network, group);
1744 service = __connman_service_lookup(name);
1750 if (service == NULL) {
1755 __connman_device_disconnect(device);
1757 if (passphrase != NULL) {
1758 g_free(service->passphrase);
1759 service->passphrase = g_strdup(passphrase);
1762 service->userconnect = TRUE;
1764 err = __connman_service_connect(service);
1765 if (err < 0 && err != -EINPROGRESS)
1768 g_dbus_send_reply(connection, msg,
1769 DBUS_TYPE_OBJECT_PATH, &service->path,
1775 if (service != NULL && created == TRUE) {
1776 struct connman_network *network = service->network;
1778 if (network != NULL) {
1779 connman_network_set_available(network, FALSE);
1780 __connman_device_cleanup_networks(device);
1783 __connman_service_put(service);
1790 * __connman_service_get:
1791 * @identifier: service identifier
1793 * Look up a service by identifier or create a new one if not found
1795 static struct connman_service *__connman_service_get(const char *identifier)
1797 struct connman_service *service;
1798 GSequenceIter *iter;
1800 iter = g_hash_table_lookup(service_hash, identifier);
1802 service = g_sequence_get(iter);
1803 if (service != NULL)
1804 g_atomic_int_inc(&service->refcount);
1808 service = connman_service_create();
1809 if (service == NULL)
1812 DBG("service %p", service);
1814 service->identifier = g_strdup(identifier);
1816 service->profile = g_strdup(__connman_profile_active_ident());
1818 __connman_storage_load_service(service);
1820 iter = g_sequence_insert_sorted(service_list, service,
1821 service_compare, NULL);
1823 g_hash_table_insert(service_hash, service->identifier, iter);
1828 static int service_register(struct connman_service *service)
1830 const char *path = __connman_profile_active_path();
1831 GSequenceIter *iter;
1833 DBG("service %p", service);
1835 if (service->path != NULL)
1838 service->path = g_strdup_printf("%s/%s", path, service->identifier);
1840 DBG("path %s", service->path);
1842 g_dbus_register_interface(connection, service->path,
1843 CONNMAN_SERVICE_INTERFACE,
1844 service_methods, service_signals,
1845 NULL, service, NULL);
1847 __connman_storage_load_service(service);
1849 iter = g_hash_table_lookup(service_hash, service->identifier);
1851 g_sequence_sort_changed(iter, service_compare, NULL);
1853 __connman_profile_changed(TRUE);
1858 static void service_up(struct connman_ipconfig *ipconfig)
1860 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
1863 static void service_down(struct connman_ipconfig *ipconfig)
1865 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
1868 static void service_lower_up(struct connman_ipconfig *ipconfig)
1870 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
1873 static void service_lower_down(struct connman_ipconfig *ipconfig)
1875 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
1878 static void service_ip_bound(struct connman_ipconfig *ipconfig)
1880 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
1883 static void service_ip_release(struct connman_ipconfig *ipconfig)
1885 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
1888 static const struct connman_ipconfig_ops service_ops = {
1890 .down = service_down,
1891 .lower_up = service_lower_up,
1892 .lower_down = service_lower_down,
1893 .ip_bound = service_ip_bound,
1894 .ip_release = service_ip_release,
1897 static void setup_ipconfig(struct connman_service *service, int index)
1902 service->ipconfig = connman_ipconfig_create(index);
1903 if (service->ipconfig == NULL)
1906 connman_ipconfig_set_method(service->ipconfig,
1907 CONNMAN_IPCONFIG_METHOD_DHCP);
1909 __connman_storage_load_service(service);
1911 connman_ipconfig_set_data(service->ipconfig, service);
1913 connman_ipconfig_set_ops(service->ipconfig, &service_ops);
1914 connman_ipconfig_set_ops(service->ipconfig, NULL);
1918 * __connman_service_lookup_from_device:
1919 * @device: device structure
1921 * Look up a service by device (reference count will not be increased)
1923 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
1925 struct connman_service *service;
1929 ident = __connman_device_get_ident(device);
1933 name = g_strdup_printf("%s_%s",
1934 __connman_device_get_type(device), ident);
1935 service = __connman_service_lookup(name);
1942 * __connman_service_create_from_device:
1943 * @device: device structure
1945 * Look up service by device and if not found, create one
1947 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
1949 struct connman_service *service;
1953 ident = __connman_device_get_ident(device);
1957 name = g_strdup_printf("%s_%s",
1958 __connman_device_get_type(device), ident);
1959 service = __connman_service_get(name);
1962 if (service == NULL)
1965 if (service->path != NULL) {
1966 __connman_profile_changed(TRUE);
1970 service->type = __connman_device_get_service_type(device);
1972 service->autoconnect = FALSE;
1974 service->device = device;
1976 setup_ipconfig(service, connman_device_get_index(device));
1978 service_register(service);
1980 __connman_profile_changed(TRUE);
1982 if (service->favorite == TRUE)
1983 __connman_service_auto_connect();
1988 void __connman_service_remove_from_device(struct connman_device *device)
1990 struct connman_service *service;
1991 enum connman_service_type type;
1993 service = __connman_service_lookup_from_device(device);
1994 if (service == NULL)
1997 type = service->type;
1999 __connman_service_put(service);
2003 __connman_notifier_disconnect(type);
2005 __connman_service_auto_connect();
2009 * __connman_service_lookup_from_network:
2010 * @network: network structure
2012 * Look up a service by network (reference count will not be increased)
2014 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
2016 struct connman_service *service;
2017 const char *ident, *group;
2020 ident = __connman_network_get_ident(network);
2024 group = connman_network_get_group(network);
2028 name = g_strdup_printf("%s_%s_%s",
2029 __connman_network_get_type(network), ident, group);
2030 service = __connman_service_lookup(name);
2036 const char *__connman_service_get_path(struct connman_service *service)
2038 return service->path;
2041 unsigned int __connman_service_get_order(struct connman_service *service)
2043 GSequenceIter *iter;
2045 if (service == NULL)
2048 if (service->favorite == FALSE) {
2053 iter = g_hash_table_lookup(service_hash, service->identifier);
2055 if (g_sequence_iter_get_position(iter) == 0)
2062 return service->order;
2065 static enum connman_service_type convert_network_type(struct connman_network *network)
2067 enum connman_network_type type = connman_network_get_type(network);
2070 case CONNMAN_NETWORK_TYPE_UNKNOWN:
2071 case CONNMAN_NETWORK_TYPE_VENDOR:
2073 case CONNMAN_NETWORK_TYPE_WIFI:
2074 return CONNMAN_SERVICE_TYPE_WIFI;
2075 case CONNMAN_NETWORK_TYPE_WIMAX:
2076 return CONNMAN_SERVICE_TYPE_WIMAX;
2077 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
2078 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
2079 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
2080 case CONNMAN_NETWORK_TYPE_MBM:
2081 case CONNMAN_NETWORK_TYPE_HSO:
2082 return CONNMAN_SERVICE_TYPE_CELLULAR;
2085 return CONNMAN_SERVICE_TYPE_UNKNOWN;
2088 static enum connman_service_mode convert_wifi_mode(const char *mode)
2091 return CONNMAN_SERVICE_MODE_UNKNOWN;
2092 else if (g_str_equal(mode, "managed") == TRUE)
2093 return CONNMAN_SERVICE_MODE_MANAGED;
2094 else if (g_str_equal(mode, "adhoc") == TRUE)
2095 return CONNMAN_SERVICE_MODE_ADHOC;
2097 return CONNMAN_SERVICE_MODE_UNKNOWN;
2100 static enum connman_service_mode convert_wifi_security(const char *security)
2102 if (security == NULL)
2103 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2104 else if (g_str_equal(security, "none") == TRUE)
2105 return CONNMAN_SERVICE_SECURITY_NONE;
2106 else if (g_str_equal(security, "wep") == TRUE)
2107 return CONNMAN_SERVICE_SECURITY_WEP;
2108 else if (g_str_equal(security, "wpa") == TRUE)
2109 return CONNMAN_SERVICE_SECURITY_WPA;
2110 else if (g_str_equal(security, "rsn") == TRUE)
2111 return CONNMAN_SERVICE_SECURITY_RSN;
2113 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2116 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
2121 return CONNMAN_SERVICE_MODE_GPRS;
2123 return CONNMAN_SERVICE_MODE_EDGE;
2128 return CONNMAN_SERVICE_MODE_UMTS;
2131 return CONNMAN_SERVICE_MODE_UNKNOWN;
2134 static void update_from_network(struct connman_service *service,
2135 struct connman_network *network)
2137 connman_uint8_t strength = service->strength;
2138 GSequenceIter *iter;
2141 if (service->state == CONNMAN_SERVICE_STATE_READY)
2144 if (is_connecting(service) == TRUE)
2147 str = connman_network_get_string(network, "Name");
2149 g_free(service->name);
2150 service->name = g_strdup(str);
2151 service->hidden = FALSE;
2153 g_free(service->name);
2154 service->name = NULL;
2155 service->hidden = TRUE;
2158 service->strength = connman_network_get_uint8(network, "Strength");
2160 str = connman_network_get_string(network, "WiFi.Mode");
2161 service->mode = convert_wifi_mode(str);
2163 str = connman_network_get_string(network, "WiFi.Security");
2164 service->security = convert_wifi_security(str);
2166 str = connman_network_get_string(network, "Cellular.MCC");
2167 g_free(service->mcc);
2168 service->mcc = g_strdup(str);
2170 str = connman_network_get_string(network, "Cellular.MNC");
2171 g_free(service->mnc);
2172 service->mnc = g_strdup(str);
2174 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
2175 connman_uint8_t value = connman_network_get_uint8(network,
2178 service->mode = convert_cellular_mode(value);
2181 if (service->strength > strength && service->network != NULL) {
2182 connman_network_unref(service->network);
2183 service->network = connman_network_ref(network);
2185 strength_changed(service);
2188 if (service->network == NULL)
2189 service->network = connman_network_ref(network);
2191 iter = g_hash_table_lookup(service_hash, service->identifier);
2193 g_sequence_sort_changed(iter, service_compare, NULL);
2197 * __connman_service_create_from_network:
2198 * @network: network structure
2200 * Look up service by network and if not found, create one
2202 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
2204 struct connman_service *service;
2205 const char *ident, *group;
2208 ident = __connman_network_get_ident(network);
2212 group = connman_network_get_group(network);
2216 name = g_strdup_printf("%s_%s_%s",
2217 __connman_network_get_type(network), ident, group);
2218 service = __connman_service_get(name);
2221 if (service == NULL)
2224 if (__connman_network_get_weakness(network) == TRUE)
2227 if (service->path != NULL) {
2228 update_from_network(service, network);
2229 __connman_profile_changed(TRUE);
2233 service->type = convert_network_type(network);
2235 switch (service->type) {
2236 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2237 case CONNMAN_SERVICE_TYPE_ETHERNET:
2238 case CONNMAN_SERVICE_TYPE_WIMAX:
2239 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2240 service->autoconnect = FALSE;
2242 case CONNMAN_SERVICE_TYPE_WIFI:
2243 case CONNMAN_SERVICE_TYPE_CELLULAR:
2244 service->autoconnect = TRUE;
2248 service->state = CONNMAN_SERVICE_STATE_IDLE;
2250 update_from_network(service, network);
2252 setup_ipconfig(service, connman_network_get_index(network));
2254 service_register(service);
2256 __connman_profile_changed(TRUE);
2258 if (service->favorite == TRUE)
2259 __connman_service_auto_connect();
2264 void __connman_service_update_from_network(struct connman_network *network)
2266 struct connman_service *service;
2267 enum connman_service_mode mode;
2268 connman_uint8_t strength, value;
2270 service = __connman_service_lookup_from_network(network);
2271 if (service == NULL)
2274 if (service->network == NULL)
2277 strength = connman_network_get_uint8(service->network, "Strength");
2278 if (strength == service->strength)
2281 service->strength = strength;
2283 strength_changed(service);
2286 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
2289 value = connman_network_get_uint8(service->network, "Cellular.Mode");
2290 mode = convert_cellular_mode(value);
2292 if (mode == service->mode)
2295 service->mode = mode;
2297 mode_changed(service);
2300 void __connman_service_remove_from_network(struct connman_network *network)
2302 struct connman_service *service;
2304 service = __connman_service_lookup_from_network(network);
2305 if (service == NULL)
2308 __connman_service_put(service);
2311 static int service_load(struct connman_service *service)
2313 const char *ident = service->profile;
2315 GError *error = NULL;
2316 gchar *pathname, *data = NULL;
2319 connman_bool_t autoconnect;
2320 unsigned int ssid_len;
2323 DBG("service %p", service);
2328 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2329 if (pathname == NULL)
2332 keyfile = g_key_file_new();
2334 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
2341 if (g_key_file_load_from_data(keyfile, data, length,
2342 0, NULL) == FALSE) {
2349 switch (service->type) {
2350 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2351 case CONNMAN_SERVICE_TYPE_ETHERNET:
2353 case CONNMAN_SERVICE_TYPE_WIFI:
2354 if (service->name == NULL) {
2357 name = g_key_file_get_string(keyfile,
2358 service->identifier, "Name", NULL);
2360 g_free(service->name);
2361 service->name = name;
2364 if (service->network != NULL)
2365 connman_network_set_name(service->network,
2369 if (service->network &&
2370 connman_network_get_blob(service->network,
2371 "WiFi.SSID", &ssid_len) == NULL) {
2374 hex_ssid = g_key_file_get_string(keyfile,
2375 service->identifier,
2378 if (hex_ssid != NULL) {
2380 unsigned int i, j = 0, hex;
2381 size_t hex_ssid_len = strlen(hex_ssid);
2383 ssid = g_try_malloc0(hex_ssid_len / 2);
2390 for (i = 0; i < hex_ssid_len; i += 2) {
2391 sscanf(hex_ssid + i, "%02x", &hex);
2395 connman_network_set_blob(service->network,
2396 "WiFi.SSID", ssid, hex_ssid_len / 2);
2403 case CONNMAN_SERVICE_TYPE_WIMAX:
2404 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2405 case CONNMAN_SERVICE_TYPE_CELLULAR:
2406 service->apn = g_key_file_get_string(keyfile,
2407 service->identifier, "APN", NULL);
2409 service->username = g_key_file_get_string(keyfile,
2410 service->identifier, "Username", NULL);
2412 service->username = g_key_file_get_string(keyfile,
2413 service->identifier, "Password", NULL);
2415 service->favorite = g_key_file_get_boolean(keyfile,
2416 service->identifier, "Favorite", NULL);
2418 autoconnect = g_key_file_get_boolean(keyfile,
2419 service->identifier, "AutoConnect", &error);
2421 service->autoconnect = autoconnect;
2422 g_clear_error(&error);
2424 str = g_key_file_get_string(keyfile,
2425 service->identifier, "Failure", NULL);
2427 service->state = CONNMAN_SERVICE_STATE_FAILURE;
2428 service->error = string2error(str);
2433 str = g_key_file_get_string(keyfile,
2434 service->identifier, "Modified", NULL);
2436 g_time_val_from_iso8601(str, &service->modified);
2440 str = g_key_file_get_string(keyfile,
2441 service->identifier, "Passphrase", NULL);
2443 g_free(service->passphrase);
2444 service->passphrase = str;
2447 if (service->ipconfig != NULL)
2448 __connman_ipconfig_load(service->ipconfig, keyfile,
2449 service->identifier, "IPv4.");
2452 g_key_file_free(keyfile);
2457 static int service_save(struct connman_service *service)
2459 const char *ident = service->profile;
2461 gchar *pathname, *data = NULL;
2466 DBG("service %p", service);
2471 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2472 if (pathname == NULL)
2475 keyfile = g_key_file_new();
2477 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
2481 if (g_key_file_load_from_data(keyfile, data, length,
2489 if (service->name != NULL)
2490 g_key_file_set_string(keyfile, service->identifier,
2491 "Name", service->name);
2493 switch (service->type) {
2494 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2495 case CONNMAN_SERVICE_TYPE_ETHERNET:
2497 case CONNMAN_SERVICE_TYPE_WIFI:
2498 if (service->network) {
2499 const unsigned char *ssid;
2500 unsigned int ssid_len = 0;
2502 ssid = connman_network_get_blob(service->network,
2503 "WiFi.SSID", &ssid_len);
2505 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
2506 char *identifier = service->identifier;
2510 str = g_string_sized_new(ssid_len * 2);
2516 for (i = 0; i < ssid_len; i++)
2517 g_string_append_printf(str,
2520 g_key_file_set_string(keyfile, identifier,
2523 g_string_free(str, TRUE);
2528 case CONNMAN_SERVICE_TYPE_WIMAX:
2529 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2530 case CONNMAN_SERVICE_TYPE_CELLULAR:
2531 if (service->apn != NULL)
2532 g_key_file_set_string(keyfile, service->identifier,
2533 "APN", service->apn);
2535 if (service->username != NULL)
2536 g_key_file_set_string(keyfile, service->identifier,
2537 "Username", service->username);
2539 if (service->password != NULL)
2540 g_key_file_set_string(keyfile, service->identifier,
2541 "Password", service->password);
2543 g_key_file_set_boolean(keyfile, service->identifier,
2544 "Favorite", service->favorite);
2546 if (service->favorite == TRUE)
2547 g_key_file_set_boolean(keyfile, service->identifier,
2548 "AutoConnect", service->autoconnect);
2550 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
2551 const char *failure = error2string(service->error);
2552 if (failure != NULL)
2553 g_key_file_set_string(keyfile,
2554 service->identifier,
2555 "Failure", failure);
2557 g_key_file_remove_key(keyfile, service->identifier,
2563 str = g_time_val_to_iso8601(&service->modified);
2565 g_key_file_set_string(keyfile, service->identifier,
2570 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
2571 g_key_file_set_string(keyfile, service->identifier,
2572 "Passphrase", service->passphrase);
2574 g_key_file_remove_key(keyfile, service->identifier,
2575 "Passphrase", NULL);
2577 if (service->ipconfig != NULL)
2578 __connman_ipconfig_save(service->ipconfig, keyfile,
2579 service->identifier, "IPv4.");
2581 data = g_key_file_to_data(keyfile, &length, NULL);
2583 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
2584 connman_error("Failed to store service information");
2589 g_key_file_free(keyfile);
2596 static struct connman_storage service_storage = {
2598 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
2599 .service_load = service_load,
2600 .service_save = service_save,
2603 int __connman_service_init(void)
2607 connection = connman_dbus_get_connection();
2609 if (connman_storage_register(&service_storage) < 0)
2610 connman_error("Failed to register service storage");
2612 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2615 service_list = g_sequence_new(service_free);
2620 void __connman_service_cleanup(void)
2624 g_sequence_free(service_list);
2625 service_list = NULL;
2627 g_hash_table_destroy(service_hash);
2628 service_hash = NULL;
2630 connman_storage_unregister(&service_storage);
2632 dbus_connection_unref(connection);