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 CONNECT_TIMEOUT 120
34 static DBusConnection *connection = NULL;
36 static GSequence *service_list = NULL;
37 static GHashTable *service_hash = NULL;
39 struct connman_service {
43 enum connman_service_type type;
44 enum connman_service_mode mode;
45 enum connman_service_security security;
46 enum connman_service_state state;
47 enum connman_service_error error;
48 connman_uint8_t strength;
49 connman_bool_t favorite;
50 connman_bool_t hidden;
51 connman_bool_t ignore;
52 connman_bool_t autoconnect;
53 connman_bool_t userconnect;
64 connman_bool_t roaming;
65 struct connman_ipconfig *ipconfig;
66 struct connman_network *network;
71 static void append_path(gpointer value, gpointer user_data)
73 struct connman_service *service = value;
74 DBusMessageIter *iter = user_data;
76 if (service->path == NULL || service->hidden == TRUE)
79 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
83 void __connman_service_list(DBusMessageIter *iter, void *user_data)
85 g_sequence_foreach(service_list, append_path, iter);
90 struct connman_service *service;
93 static void compare_path(gpointer value, gpointer user_data)
95 struct connman_service *service = value;
96 struct find_data *data = user_data;
98 if (data->service != NULL)
101 if (g_strcmp0(service->path, data->path) == 0)
102 data->service = service;
105 static struct connman_service *find_service(const char *path)
107 struct find_data data = { .path = path, .service = NULL };
109 DBG("path %s", path);
111 g_sequence_foreach(service_list, compare_path, &data);
116 const char *__connman_service_type2string(enum connman_service_type type)
119 case CONNMAN_SERVICE_TYPE_UNKNOWN:
121 case CONNMAN_SERVICE_TYPE_SYSTEM:
123 case CONNMAN_SERVICE_TYPE_ETHERNET:
125 case CONNMAN_SERVICE_TYPE_WIFI:
127 case CONNMAN_SERVICE_TYPE_WIMAX:
129 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
131 case CONNMAN_SERVICE_TYPE_CELLULAR:
133 case CONNMAN_SERVICE_TYPE_VPN:
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_PSK:
171 case CONNMAN_SERVICE_SECURITY_8021X:
173 case CONNMAN_SERVICE_SECURITY_WPA:
175 case CONNMAN_SERVICE_SECURITY_RSN:
182 static const char *state2string(enum connman_service_state state)
185 case CONNMAN_SERVICE_STATE_UNKNOWN:
187 case CONNMAN_SERVICE_STATE_IDLE:
189 case CONNMAN_SERVICE_STATE_ASSOCIATION:
190 return "association";
191 case CONNMAN_SERVICE_STATE_CONFIGURATION:
192 return "configuration";
193 case CONNMAN_SERVICE_STATE_READY:
195 case CONNMAN_SERVICE_STATE_DISCONNECT:
197 case CONNMAN_SERVICE_STATE_FAILURE:
204 static const char *error2string(enum connman_service_error error)
207 case CONNMAN_SERVICE_ERROR_UNKNOWN:
209 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
210 return "out-of-range";
211 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
212 return "pin-missing";
213 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
214 return "dhcp-failed";
215 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
216 return "connect-failed";
222 static enum connman_service_error string2error(const char *error)
224 if (g_strcmp0(error, "dhcp-failed") == 0)
225 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
226 else if (g_strcmp0(error, "pin-missing") == 0)
227 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
229 return CONNMAN_SERVICE_ERROR_UNKNOWN;
232 static struct connman_service *get_default(void)
234 struct connman_service *service;
237 iter = g_sequence_get_begin_iter(service_list);
239 if (g_sequence_iter_is_end(iter) == TRUE)
242 service = g_sequence_get(iter);
244 if (service->state != CONNMAN_SERVICE_STATE_READY)
250 static void default_changed(void)
252 struct connman_service *service = get_default();
254 __connman_notifier_default_changed(service);
257 const char *__connman_service_default(void)
259 struct connman_service *service;
261 service = get_default();
265 return __connman_service_type2string(service->type);
268 static void mode_changed(struct connman_service *service)
272 str = mode2string(service->mode);
276 connman_dbus_property_changed_basic(service->path,
277 CONNMAN_SERVICE_INTERFACE, "Mode",
278 DBUS_TYPE_STRING, &str);
281 static void state_changed(struct connman_service *service)
285 str = state2string(service->state);
289 connman_dbus_property_changed_basic(service->path,
290 CONNMAN_SERVICE_INTERFACE, "State",
291 DBUS_TYPE_STRING, &str);
294 static void strength_changed(struct connman_service *service)
296 if (service->strength == 0)
299 connman_dbus_property_changed_basic(service->path,
300 CONNMAN_SERVICE_INTERFACE, "Strength",
301 DBUS_TYPE_BYTE, &service->strength);
304 static void favorite_changed(struct connman_service *service)
306 connman_dbus_property_changed_basic(service->path,
307 CONNMAN_SERVICE_INTERFACE, "Favorite",
308 DBUS_TYPE_BOOLEAN, &service->favorite);
311 static void roaming_changed(struct connman_service *service)
313 connman_dbus_property_changed_basic(service->path,
314 CONNMAN_SERVICE_INTERFACE, "Roaming",
315 DBUS_TYPE_BOOLEAN, &service->roaming);
318 static void autoconnect_changed(struct connman_service *service)
320 connman_dbus_property_changed_basic(service->path,
321 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
322 DBUS_TYPE_BOOLEAN, &service->autoconnect);
325 static void passphrase_changed(struct connman_service *service)
327 dbus_bool_t required;
329 switch (service->type) {
330 case CONNMAN_SERVICE_TYPE_UNKNOWN:
331 case CONNMAN_SERVICE_TYPE_SYSTEM:
332 case CONNMAN_SERVICE_TYPE_ETHERNET:
333 case CONNMAN_SERVICE_TYPE_WIMAX:
334 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
335 case CONNMAN_SERVICE_TYPE_CELLULAR:
336 case CONNMAN_SERVICE_TYPE_VPN:
338 case CONNMAN_SERVICE_TYPE_WIFI:
341 switch (service->security) {
342 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
343 case CONNMAN_SERVICE_SECURITY_NONE:
344 case CONNMAN_SERVICE_SECURITY_8021X:
346 case CONNMAN_SERVICE_SECURITY_WEP:
347 case CONNMAN_SERVICE_SECURITY_PSK:
348 case CONNMAN_SERVICE_SECURITY_WPA:
349 case CONNMAN_SERVICE_SECURITY_RSN:
350 if (service->passphrase == NULL)
357 connman_dbus_property_changed_basic(service->path,
358 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
359 DBUS_TYPE_BOOLEAN, &required);
362 static void apn_changed(struct connman_service *service)
364 dbus_bool_t required;
366 switch (service->type) {
367 case CONNMAN_SERVICE_TYPE_UNKNOWN:
368 case CONNMAN_SERVICE_TYPE_SYSTEM:
369 case CONNMAN_SERVICE_TYPE_ETHERNET:
370 case CONNMAN_SERVICE_TYPE_WIMAX:
371 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
372 case CONNMAN_SERVICE_TYPE_WIFI:
373 case CONNMAN_SERVICE_TYPE_VPN:
375 case CONNMAN_SERVICE_TYPE_CELLULAR:
379 required = (service->apn == NULL) ? TRUE : FALSE;
381 connman_dbus_property_changed_basic(service->path,
382 CONNMAN_SERVICE_INTERFACE, "SetupRequired",
383 DBUS_TYPE_BOOLEAN, &required);
386 static void append_settings(DBusMessageIter *iter, void *user_data)
388 struct connman_service *service = user_data;
390 switch (service->state) {
391 case CONNMAN_SERVICE_STATE_UNKNOWN:
392 case CONNMAN_SERVICE_STATE_IDLE:
393 case CONNMAN_SERVICE_STATE_FAILURE:
394 case CONNMAN_SERVICE_STATE_DISCONNECT:
395 case CONNMAN_SERVICE_STATE_ASSOCIATION:
396 case CONNMAN_SERVICE_STATE_CONFIGURATION:
398 case CONNMAN_SERVICE_STATE_READY:
402 if (service->ipconfig != NULL)
403 __connman_ipconfig_append_ipv4(service->ipconfig, iter);
406 static void settings_changed(struct connman_service *service)
408 connman_dbus_property_changed_dict(service->path,
409 CONNMAN_SERVICE_INTERFACE, "IPv4",
410 append_settings, service);
413 static DBusMessage *get_properties(DBusConnection *conn,
414 DBusMessage *msg, void *user_data)
416 struct connman_service *service = user_data;
418 DBusMessageIter array, dict;
419 dbus_bool_t required;
422 DBG("service %p", service);
424 reply = dbus_message_new_method_return(msg);
428 dbus_message_iter_init_append(reply, &array);
430 connman_dbus_dict_open(&array, &dict);
432 str = __connman_service_type2string(service->type);
434 connman_dbus_dict_append_basic(&dict, "Type",
435 DBUS_TYPE_STRING, &str);
437 str = mode2string(service->mode);
439 connman_dbus_dict_append_basic(&dict, "Mode",
440 DBUS_TYPE_STRING, &str);
442 str = security2string(service->security);
444 connman_dbus_dict_append_basic(&dict, "Security",
445 DBUS_TYPE_STRING, &str);
447 str = state2string(service->state);
449 connman_dbus_dict_append_basic(&dict, "State",
450 DBUS_TYPE_STRING, &str);
452 str = error2string(service->error);
454 connman_dbus_dict_append_basic(&dict, "Error",
455 DBUS_TYPE_STRING, &str);
457 if (service->strength > 0)
458 connman_dbus_dict_append_basic(&dict, "Strength",
459 DBUS_TYPE_BYTE, &service->strength);
461 connman_dbus_dict_append_basic(&dict, "Favorite",
462 DBUS_TYPE_BOOLEAN, &service->favorite);
464 if (service->favorite == TRUE)
465 connman_dbus_dict_append_basic(&dict, "AutoConnect",
466 DBUS_TYPE_BOOLEAN, &service->autoconnect);
468 connman_dbus_dict_append_basic(&dict, "AutoConnect",
469 DBUS_TYPE_BOOLEAN, &service->favorite);
471 if (service->name != NULL)
472 connman_dbus_dict_append_basic(&dict, "Name",
473 DBUS_TYPE_STRING, &service->name);
475 switch (service->type) {
476 case CONNMAN_SERVICE_TYPE_UNKNOWN:
477 case CONNMAN_SERVICE_TYPE_SYSTEM:
478 case CONNMAN_SERVICE_TYPE_ETHERNET:
479 case CONNMAN_SERVICE_TYPE_WIMAX:
480 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
481 case CONNMAN_SERVICE_TYPE_VPN:
483 case CONNMAN_SERVICE_TYPE_CELLULAR:
484 connman_dbus_dict_append_basic(&dict, "Roaming",
485 DBUS_TYPE_BOOLEAN, &service->roaming);
487 if (service->mcc != NULL && service->mnc != NULL) {
488 connman_dbus_dict_append_basic(&dict, "MCC",
489 DBUS_TYPE_STRING, &service->mcc);
490 connman_dbus_dict_append_basic(&dict, "MNC",
491 DBUS_TYPE_STRING, &service->mnc);
494 if (service->apn != NULL) {
495 connman_dbus_dict_append_basic(&dict, "APN",
496 DBUS_TYPE_STRING, &service->apn);
498 if (service->username != NULL)
499 connman_dbus_dict_append_basic(&dict,
500 "Username", DBUS_TYPE_STRING,
503 if (service->password != NULL)
504 connman_dbus_dict_append_basic(&dict,
505 "Password", DBUS_TYPE_STRING,
512 connman_dbus_dict_append_basic(&dict, "SetupRequired",
513 DBUS_TYPE_BOOLEAN, &required);
515 case CONNMAN_SERVICE_TYPE_WIFI:
516 if (service->passphrase != NULL &&
517 __connman_security_check_privilege(msg,
518 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
519 connman_dbus_dict_append_basic(&dict, "Passphrase",
520 DBUS_TYPE_STRING, &service->passphrase);
524 switch (service->security) {
525 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
526 case CONNMAN_SERVICE_SECURITY_NONE:
527 case CONNMAN_SERVICE_SECURITY_8021X:
529 case CONNMAN_SERVICE_SECURITY_WEP:
530 case CONNMAN_SERVICE_SECURITY_PSK:
531 case CONNMAN_SERVICE_SECURITY_WPA:
532 case CONNMAN_SERVICE_SECURITY_RSN:
533 if (service->passphrase == NULL)
538 connman_dbus_dict_append_basic(&dict, "PassphraseRequired",
539 DBUS_TYPE_BOOLEAN, &required);
543 connman_dbus_dict_append_dict(&dict, "Settings",
544 append_settings, service);
546 connman_dbus_dict_close(&array, &dict);
551 static DBusMessage *set_property(DBusConnection *conn,
552 DBusMessage *msg, void *user_data)
554 struct connman_service *service = user_data;
555 DBusMessageIter iter, value;
559 DBG("service %p", service);
561 if (dbus_message_iter_init(msg, &iter) == FALSE)
562 return __connman_error_invalid_arguments(msg);
564 dbus_message_iter_get_basic(&iter, &name);
565 dbus_message_iter_next(&iter);
566 dbus_message_iter_recurse(&iter, &value);
568 if (__connman_security_check_privilege(msg,
569 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
570 return __connman_error_permission_denied(msg);
572 type = dbus_message_iter_get_arg_type(&value);
574 if (g_str_has_prefix(name, "AutoConnect") == TRUE) {
575 connman_bool_t autoconnect;
577 if (type != DBUS_TYPE_BOOLEAN)
578 return __connman_error_invalid_arguments(msg);
580 if (service->favorite == FALSE)
581 return __connman_error_invalid_service(msg);
583 dbus_message_iter_get_basic(&value, &autoconnect);
585 if (service->autoconnect == autoconnect)
586 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
588 service->autoconnect = autoconnect;
590 autoconnect_changed(service);
592 __connman_storage_save_service(service);
593 } else if (g_str_equal(name, "Passphrase") == TRUE) {
594 const char *passphrase;
596 if (type != DBUS_TYPE_STRING)
597 return __connman_error_invalid_arguments(msg);
599 if (__connman_security_check_privilege(msg,
600 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
601 return __connman_error_permission_denied(msg);
603 dbus_message_iter_get_basic(&value, &passphrase);
605 g_free(service->passphrase);
606 service->passphrase = g_strdup(passphrase);
608 passphrase_changed(service);
610 if (service->network != NULL)
611 connman_network_set_string(service->network,
612 "WiFi.Passphrase", service->passphrase);
614 __connman_storage_save_service(service);
615 } else if (g_str_equal(name, "APN") == TRUE) {
618 if (type != DBUS_TYPE_STRING)
619 return __connman_error_invalid_arguments(msg);
621 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
622 return __connman_error_invalid_service(msg);
624 dbus_message_iter_get_basic(&value, &apn);
626 g_free(service->apn);
627 service->apn = g_strdup(apn);
629 apn_changed(service);
631 if (service->network != NULL)
632 connman_network_set_string(service->network,
633 "Cellular.APN", service->apn);
635 __connman_storage_save_service(service);
636 } else if (g_str_equal(name, "Username") == TRUE) {
637 const char *username;
639 if (type != DBUS_TYPE_STRING)
640 return __connman_error_invalid_arguments(msg);
642 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
643 return __connman_error_invalid_service(msg);
645 dbus_message_iter_get_basic(&value, &username);
647 g_free(service->username);
648 service->username = g_strdup(username);
650 if (service->network != NULL)
651 connman_network_set_string(service->network,
652 "Cellular.Username", service->username);
654 __connman_storage_save_service(service);
655 } else if (g_str_equal(name, "Password") == TRUE) {
656 const char *password;
658 if (type != DBUS_TYPE_STRING)
659 return __connman_error_invalid_arguments(msg);
661 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
662 return __connman_error_invalid_service(msg);
664 dbus_message_iter_get_basic(&value, &password);
666 g_free(service->password);
667 service->password = g_strdup(password);
669 if (service->network != NULL)
670 connman_network_set_string(service->network,
671 "Cellular.Password", service->password);
673 __connman_storage_save_service(service);
674 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
677 if (service->ipconfig == NULL)
678 return __connman_error_invalid_property(msg);
680 err = __connman_ipconfig_set_ipv4(service->ipconfig,
683 return __connman_error_failed(msg, -err);
685 return __connman_error_invalid_property(msg);
687 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
690 static void set_idle(struct connman_service *service)
692 service->state = CONNMAN_SERVICE_STATE_IDLE;
693 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
694 state_changed(service);
697 static DBusMessage *clear_property(DBusConnection *conn,
698 DBusMessage *msg, void *user_data)
700 struct connman_service *service = user_data;
703 DBG("service %p", service);
705 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
708 if (__connman_security_check_privilege(msg,
709 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
710 return __connman_error_permission_denied(msg);
712 if (g_str_equal(name, "Error") == TRUE) {
715 g_get_current_time(&service->modified);
716 __connman_storage_save_service(service);
717 } else if (g_str_equal(name, "Passphrase") == TRUE) {
718 g_free(service->passphrase);
719 service->passphrase = NULL;
721 passphrase_changed(service);
723 __connman_storage_save_service(service);
725 return __connman_error_invalid_property(msg);
727 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
730 static connman_bool_t is_connecting(struct connman_service *service)
732 switch (service->state) {
733 case CONNMAN_SERVICE_STATE_UNKNOWN:
734 case CONNMAN_SERVICE_STATE_IDLE:
735 case CONNMAN_SERVICE_STATE_FAILURE:
736 case CONNMAN_SERVICE_STATE_DISCONNECT:
737 case CONNMAN_SERVICE_STATE_READY:
739 case CONNMAN_SERVICE_STATE_ASSOCIATION:
740 case CONNMAN_SERVICE_STATE_CONFIGURATION:
747 static connman_bool_t is_ignore(struct connman_service *service)
749 if (service->autoconnect == FALSE)
752 if (service->roaming == TRUE)
755 if (service->ignore == TRUE)
758 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
764 void __connman_service_auto_connect(void)
766 struct connman_service *service = NULL;
771 iter = g_sequence_get_begin_iter(service_list);
773 while (g_sequence_iter_is_end(iter) == FALSE) {
774 service = g_sequence_get(iter);
776 if (service->pending != NULL)
779 if (is_connecting(service) == TRUE)
782 if (service->favorite == FALSE)
785 if (service->state == CONNMAN_SERVICE_STATE_READY)
788 if (is_ignore(service) == FALSE &&
789 service->state == CONNMAN_SERVICE_STATE_IDLE)
794 iter = g_sequence_iter_next(iter);
797 if (service != NULL) {
798 service->userconnect = FALSE;
799 __connman_service_connect(service);
803 static void remove_timeout(struct connman_service *service)
805 if (service->timeout > 0) {
806 g_source_remove(service->timeout);
807 service->timeout = 0;
811 static void reply_pending(struct connman_service *service, int error)
813 remove_timeout(service);
815 if (service->pending != NULL) {
819 reply = __connman_error_failed(service->pending,
822 g_dbus_send_message(connection, reply);
824 g_dbus_send_reply(connection, service->pending,
827 dbus_message_unref(service->pending);
828 service->pending = NULL;
832 static gboolean connect_timeout(gpointer user_data)
834 struct connman_service *service = user_data;
835 connman_bool_t autoconnect = FALSE;
837 DBG("service %p", service);
839 service->timeout = 0;
841 if (service->network != NULL)
842 __connman_network_disconnect(service->network);
844 __connman_ipconfig_disable(service->ipconfig);
846 if (service->pending != NULL) {
849 reply = __connman_error_operation_timeout(service->pending);
851 g_dbus_send_message(connection, reply);
853 dbus_message_unref(service->pending);
854 service->pending = NULL;
858 __connman_service_indicate_state(service,
859 CONNMAN_SERVICE_STATE_FAILURE);
861 if (autoconnect == TRUE && service->userconnect == FALSE)
862 __connman_service_auto_connect();
867 static void set_reconnect_state(struct connman_service *service,
868 connman_bool_t reconnect)
870 struct connman_device *device;
872 if (service->network == NULL)
875 device = connman_network_get_device(service->network);
879 __connman_device_set_reconnect(device, reconnect);
882 static connman_bool_t get_reconnect_state(struct connman_service *service)
884 struct connman_device *device;
886 if (service->network == NULL)
889 device = connman_network_get_device(service->network);
893 return __connman_device_get_reconnect(device);
896 static DBusMessage *connect_service(DBusConnection *conn,
897 DBusMessage *msg, void *user_data)
899 struct connman_service *service = user_data;
903 DBG("service %p", service);
905 if (service->pending != NULL)
906 return __connman_error_in_progress(msg);
908 iter = g_sequence_get_begin_iter(service_list);
910 while (g_sequence_iter_is_end(iter) == FALSE) {
911 struct connman_service *temp = g_sequence_get(iter);
913 if (service->type == temp->type &&
914 is_connecting(temp) == TRUE)
915 return __connman_error_in_progress(msg);
917 iter = g_sequence_iter_next(iter);
920 service->ignore = FALSE;
922 service->userconnect = TRUE;
924 service->pending = dbus_message_ref(msg);
926 set_reconnect_state(service, FALSE);
928 err = __connman_service_connect(service);
930 if (err == -ENOKEY) {
931 if (__connman_agent_request_passphrase(service,
936 if (err != -EINPROGRESS) {
937 dbus_message_unref(service->pending);
938 service->pending = NULL;
940 return __connman_error_failed(msg, -err);
946 dbus_message_unref(service->pending);
947 service->pending = NULL;
949 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
952 static DBusMessage *disconnect_service(DBusConnection *conn,
953 DBusMessage *msg, void *user_data)
955 struct connman_service *service = user_data;
958 DBG("service %p", service);
960 reply_pending(service, ECONNABORTED);
962 service->ignore = TRUE;
964 set_reconnect_state(service, FALSE);
966 err = __connman_service_disconnect(service);
968 if (err != -EINPROGRESS)
969 return __connman_error_failed(msg, -err);
974 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
977 static DBusMessage *remove_service(DBusConnection *conn,
978 DBusMessage *msg, void *user_data)
980 struct connman_service *service = user_data;
982 DBG("service %p", service);
984 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
985 return __connman_error_not_supported(msg);
987 if (service->favorite == FALSE &&
988 service->state != CONNMAN_SERVICE_STATE_FAILURE)
989 return __connman_error_not_supported(msg);
991 if (service->network != NULL) {
992 set_reconnect_state(service, FALSE);
994 __connman_network_disconnect(service->network);
997 g_free(service->passphrase);
998 service->passphrase = NULL;
1000 passphrase_changed(service);
1002 g_free(service->apn);
1003 service->apn = NULL;
1005 g_free(service->username);
1006 service->username = NULL;
1008 g_free(service->password);
1009 service->password = NULL;
1011 apn_changed(service);
1015 connman_service_set_favorite(service, FALSE);
1016 __connman_storage_save_service(service);
1018 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1021 static DBusMessage *move_before(DBusConnection *conn,
1022 DBusMessage *msg, void *user_data)
1024 struct connman_service *service = user_data;
1025 struct connman_service *target;
1027 GSequenceIter *src, *dst;
1029 DBG("service %p", service);
1031 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1034 if (service->favorite == FALSE)
1035 return __connman_error_not_supported(msg);
1037 target = find_service(path);
1038 if (target == NULL || target->favorite == FALSE || target == service)
1039 return __connman_error_invalid_service(msg);
1041 DBG("target %s", target->identifier);
1043 if (target->state != service->state)
1044 return __connman_error_invalid_service(msg);
1046 g_get_current_time(&service->modified);
1047 __connman_storage_save_service(service);
1049 src = g_hash_table_lookup(service_hash, service->identifier);
1050 dst = g_hash_table_lookup(service_hash, target->identifier);
1052 g_sequence_move(src, dst);
1054 __connman_profile_changed(FALSE);
1056 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1059 static DBusMessage *move_after(DBusConnection *conn,
1060 DBusMessage *msg, void *user_data)
1062 struct connman_service *service = user_data;
1063 struct connman_service *target;
1066 DBG("service %p", service);
1068 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1071 if (service->favorite == FALSE)
1072 return __connman_error_not_supported(msg);
1074 target = find_service(path);
1075 if (target == NULL || target->favorite == FALSE || target == service)
1076 return __connman_error_invalid_service(msg);
1078 DBG("target %s", target->identifier);
1080 if (target->state != service->state)
1081 return __connman_error_invalid_service(msg);
1083 g_get_current_time(&service->modified);
1084 __connman_storage_save_service(service);
1086 return __connman_error_not_implemented(msg);
1089 static GDBusMethodTable service_methods[] = {
1090 { "GetProperties", "", "a{sv}", get_properties },
1091 { "SetProperty", "sv", "", set_property },
1092 { "ClearProperty", "s", "", clear_property },
1093 { "Connect", "", "", connect_service,
1094 G_DBUS_METHOD_FLAG_ASYNC },
1095 { "Disconnect", "", "", disconnect_service },
1096 { "Remove", "", "", remove_service },
1097 { "MoveBefore", "o", "", move_before },
1098 { "MoveAfter", "o", "", move_after },
1102 static GDBusSignalTable service_signals[] = {
1103 { "PropertyChanged", "sv" },
1107 static void service_free(gpointer user_data)
1109 struct connman_service *service = user_data;
1110 char *path = service->path;
1112 DBG("service %p", service);
1114 reply_pending(service, ENOENT);
1116 g_hash_table_remove(service_hash, service->identifier);
1118 service->path = NULL;
1121 __connman_profile_changed(FALSE);
1123 g_dbus_unregister_interface(connection, path,
1124 CONNMAN_SERVICE_INTERFACE);
1128 if (service->network != NULL)
1129 connman_network_unref(service->network);
1131 if (service->ipconfig != NULL) {
1132 connman_ipconfig_unref(service->ipconfig);
1133 service->ipconfig = NULL;
1136 g_free(service->mcc);
1137 g_free(service->mnc);
1138 g_free(service->apn);
1139 g_free(service->username);
1140 g_free(service->password);
1141 g_free(service->profile);
1142 g_free(service->name);
1143 g_free(service->passphrase);
1144 g_free(service->identifier);
1149 * __connman_service_put:
1150 * @service: service structure
1152 * Release service if no longer needed
1154 void __connman_service_put(struct connman_service *service)
1156 DBG("service %p", service);
1158 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
1159 GSequenceIter *iter;
1161 iter = g_hash_table_lookup(service_hash, service->identifier);
1163 reply_pending(service, ECONNABORTED);
1165 __connman_service_disconnect(service);
1167 g_sequence_remove(iter);
1169 service_free(service);
1173 static void __connman_service_initialize(struct connman_service *service)
1175 DBG("service %p", service);
1177 service->refcount = 1;
1179 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
1180 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
1181 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
1182 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
1184 service->favorite = FALSE;
1185 service->hidden = FALSE;
1187 service->ignore = FALSE;
1189 service->userconnect = FALSE;
1195 * connman_service_create:
1197 * Allocate a new service.
1199 * Returns: a newly-allocated #connman_service structure
1201 struct connman_service *connman_service_create(void)
1203 struct connman_service *service;
1205 service = g_try_new0(struct connman_service, 1);
1206 if (service == NULL)
1209 DBG("service %p", service);
1211 __connman_service_initialize(service);
1217 * connman_service_ref:
1218 * @service: service structure
1220 * Increase reference counter of service
1222 struct connman_service *connman_service_ref(struct connman_service *service)
1224 g_atomic_int_inc(&service->refcount);
1230 * connman_service_unref:
1231 * @service: service structure
1233 * Decrease reference counter of service
1235 void connman_service_unref(struct connman_service *service)
1237 __connman_service_put(service);
1240 static gint service_compare(gconstpointer a, gconstpointer b,
1243 struct connman_service *service_a = (void *) a;
1244 struct connman_service *service_b = (void *) b;
1246 if (service_a->state != service_b->state) {
1247 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
1249 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
1253 if (service_a->order > service_b->order)
1256 if (service_a->order < service_b->order)
1259 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
1262 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
1265 if (service_a->type != service_b->type) {
1266 switch (service_a->type) {
1267 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1268 case CONNMAN_SERVICE_TYPE_SYSTEM:
1269 case CONNMAN_SERVICE_TYPE_ETHERNET:
1270 case CONNMAN_SERVICE_TYPE_VPN:
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_get_type:
1286 * @service: service structure
1288 * Get the type of service
1290 enum connman_service_type connman_service_get_type(struct connman_service *service)
1292 if (service == NULL)
1293 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1295 return service->type;
1299 * connman_service_get_interface:
1300 * @service: service structure
1302 * Get network interface of service
1304 char *connman_service_get_interface(struct connman_service *service)
1308 if (service == NULL)
1311 if (service->network == NULL)
1314 index = connman_network_get_index(service->network);
1316 return connman_inet_ifname(index);
1320 * connman_service_set_favorite:
1321 * @service: service structure
1322 * @favorite: favorite value
1324 * Change the favorite setting of service
1326 int connman_service_set_favorite(struct connman_service *service,
1327 connman_bool_t favorite)
1329 GSequenceIter *iter;
1331 iter = g_hash_table_lookup(service_hash, service->identifier);
1335 if (service->favorite == favorite)
1338 service->favorite = favorite;
1340 favorite_changed(service);
1342 g_sequence_sort_changed(iter, service_compare, NULL);
1344 __connman_profile_changed(FALSE);
1349 int __connman_service_indicate_state(struct connman_service *service,
1350 enum connman_service_state state)
1352 GSequenceIter *iter;
1354 DBG("service %p state %d", service, state);
1356 if (service == NULL)
1359 if (service->state == state)
1362 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
1363 state == CONNMAN_SERVICE_STATE_IDLE)
1366 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
1367 state == CONNMAN_SERVICE_STATE_DISCONNECT)
1370 if (state == CONNMAN_SERVICE_STATE_IDLE &&
1371 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
1372 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
1373 state_changed(service);
1375 reply_pending(service, ECONNABORTED);
1377 __connman_service_disconnect(service);
1380 service->state = state;
1381 state_changed(service);
1383 if (state == CONNMAN_SERVICE_STATE_IDLE) {
1384 connman_bool_t reconnect;
1386 reconnect = get_reconnect_state(service);
1387 if (reconnect == TRUE)
1388 __connman_service_auto_connect();
1391 if (state == CONNMAN_SERVICE_STATE_READY) {
1392 set_reconnect_state(service, TRUE);
1394 connman_service_set_favorite(service, TRUE);
1396 reply_pending(service, 0);
1398 service->userconnect = FALSE;
1400 g_get_current_time(&service->modified);
1401 __connman_storage_save_service(service);
1403 settings_changed(service);
1405 __connman_notifier_connect(service->type);
1408 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
1411 __connman_notifier_disconnect(service->type);
1414 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
1415 reply_pending(service, EIO);
1417 if (service->userconnect == FALSE)
1418 __connman_service_auto_connect();
1420 g_get_current_time(&service->modified);
1421 __connman_storage_save_service(service);
1423 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1425 iter = g_hash_table_lookup(service_hash, service->identifier);
1427 g_sequence_sort_changed(iter, service_compare, NULL);
1429 __connman_profile_changed(FALSE);
1431 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
1432 service->state == CONNMAN_SERVICE_STATE_FAILURE)
1433 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
1438 int __connman_service_indicate_error(struct connman_service *service,
1439 enum connman_service_error error)
1441 DBG("service %p error %d", service, error);
1443 if (service == NULL)
1446 service->error = error;
1448 return __connman_service_indicate_state(service,
1449 CONNMAN_SERVICE_STATE_FAILURE);
1452 int __connman_service_indicate_default(struct connman_service *service)
1454 DBG("service %p", service);
1461 static connman_bool_t prepare_network(struct connman_service *service)
1463 enum connman_network_type type;
1464 unsigned int ssid_len;
1466 type = connman_network_get_type(service->network);
1469 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1470 case CONNMAN_NETWORK_TYPE_VENDOR:
1472 case CONNMAN_NETWORK_TYPE_WIFI:
1473 if (connman_network_get_blob(service->network, "WiFi.SSID",
1477 connman_network_set_string(service->network,
1478 "WiFi.Passphrase", service->passphrase);
1480 case CONNMAN_NETWORK_TYPE_ETHERNET:
1481 case CONNMAN_NETWORK_TYPE_WIMAX:
1482 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1483 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1484 case CONNMAN_NETWORK_TYPE_CELLULAR:
1486 case CONNMAN_NETWORK_TYPE_MBM:
1487 case CONNMAN_NETWORK_TYPE_HSO:
1488 connman_network_set_string(service->network,
1489 "Cellular.APN", service->apn);
1491 connman_network_set_string(service->network,
1492 "Cellular.Username", service->username);
1493 connman_network_set_string(service->network,
1494 "Cellular.Password", service->password);
1501 int __connman_service_connect(struct connman_service *service)
1505 DBG("service %p", service);
1507 if (service->state == CONNMAN_SERVICE_STATE_READY)
1510 if (is_connecting(service) == TRUE)
1513 switch (service->type) {
1514 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1515 case CONNMAN_SERVICE_TYPE_SYSTEM:
1516 case CONNMAN_SERVICE_TYPE_VPN:
1518 case CONNMAN_SERVICE_TYPE_ETHERNET:
1519 case CONNMAN_SERVICE_TYPE_WIMAX:
1520 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1522 case CONNMAN_SERVICE_TYPE_CELLULAR:
1523 if (service->apn == NULL)
1526 case CONNMAN_SERVICE_TYPE_WIFI:
1527 switch (service->security) {
1528 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1529 case CONNMAN_SERVICE_SECURITY_NONE:
1530 case CONNMAN_SERVICE_SECURITY_8021X:
1532 case CONNMAN_SERVICE_SECURITY_WEP:
1533 case CONNMAN_SERVICE_SECURITY_PSK:
1534 case CONNMAN_SERVICE_SECURITY_WPA:
1535 case CONNMAN_SERVICE_SECURITY_RSN:
1536 if (service->passphrase == NULL)
1543 if (service->network != NULL) {
1544 if (prepare_network(service) == FALSE)
1547 __connman_ipconfig_enable(service->ipconfig);
1549 err = __connman_network_connect(service->network);
1554 if (err != -EINPROGRESS) {
1555 __connman_ipconfig_disable(service->ipconfig);
1559 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
1560 connect_timeout, service);
1562 return -EINPROGRESS;
1568 int __connman_service_disconnect(struct connman_service *service)
1572 DBG("service %p", service);
1574 if (service->network != NULL) {
1575 err = __connman_network_disconnect(service->network);
1579 __connman_ipconfig_disable(service->ipconfig);
1582 if (err != -EINPROGRESS)
1585 return -EINPROGRESS;
1592 * __connman_service_lookup:
1593 * @identifier: service identifier
1595 * Look up a service by identifier (reference count will not be increased)
1597 static struct connman_service *__connman_service_lookup(const char *identifier)
1599 GSequenceIter *iter;
1601 iter = g_hash_table_lookup(service_hash, identifier);
1603 return g_sequence_get(iter);
1608 static struct connman_network *create_hidden_wifi(struct connman_device *device,
1609 const char *ssid, const char *mode, const char *security)
1611 struct connman_network *network;
1614 unsigned int i, ssid_len;
1616 ssid_len = strlen(ssid);
1620 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
1621 if (network == NULL)
1624 connman_network_set_blob(network, "WiFi.SSID",
1625 (unsigned char *) ssid, ssid_len);
1627 connman_network_set_string(network, "WiFi.Mode", mode);
1628 connman_network_set_string(network, "WiFi.Security", security);
1630 name = g_try_malloc0(ssid_len + 1);
1632 connman_network_unref(network);
1636 for (i = 0; i < ssid_len; i++) {
1637 if (g_ascii_isprint(ssid[i]))
1643 connman_network_set_name(network, name);
1647 index = connman_device_get_index(device);
1648 connman_network_set_index(network, index);
1650 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
1652 if (connman_device_add_network(device, network) < 0) {
1653 connman_network_unref(network);
1657 connman_network_set_available(network, TRUE);
1662 int __connman_service_create_and_connect(DBusMessage *msg)
1664 struct connman_service *service;
1665 struct connman_network *network;
1666 struct connman_device *device;
1667 DBusMessageIter iter, array;
1668 const char *mode = "managed", *security = "none";
1669 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
1670 unsigned int ssid_len = 0;
1673 gboolean created = FALSE;
1676 dbus_message_iter_init(msg, &iter);
1677 dbus_message_iter_recurse(&iter, &array);
1679 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1680 DBusMessageIter entry, value;
1683 dbus_message_iter_recurse(&array, &entry);
1684 dbus_message_iter_get_basic(&entry, &key);
1686 dbus_message_iter_next(&entry);
1687 dbus_message_iter_recurse(&entry, &value);
1689 switch (dbus_message_iter_get_arg_type(&value)) {
1690 case DBUS_TYPE_STRING:
1691 if (g_str_equal(key, "Type") == TRUE)
1692 dbus_message_iter_get_basic(&value, &type);
1693 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
1694 g_str_equal(key, "Mode") == TRUE)
1695 dbus_message_iter_get_basic(&value, &mode);
1696 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
1697 g_str_equal(key, "Security") == TRUE)
1698 dbus_message_iter_get_basic(&value, &security);
1699 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
1700 g_str_equal(key, "Passphrase") == TRUE)
1701 dbus_message_iter_get_basic(&value, &passphrase);
1702 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
1703 g_str_equal(key, "SSID") == TRUE)
1704 dbus_message_iter_get_basic(&value, &ssid);
1707 dbus_message_iter_next(&array);
1713 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
1719 ssid_len = strlen(ssid);
1723 if (g_strcmp0(security, "none") != 0 &&
1724 g_strcmp0(security, "wep") != 0 &&
1725 g_strcmp0(security, "psk") != 0 &&
1726 g_strcmp0(security, "wpa") != 0 &&
1727 g_strcmp0(security, "rsn") != 0)
1730 device = __connman_element_find_device(CONNMAN_DEVICE_TYPE_WIFI);
1734 ident = __connman_device_get_ident(device);
1738 group = connman_wifi_build_group_name((unsigned char *) ssid,
1739 ssid_len, mode, security);
1743 name = g_strdup_printf("%s_%s_%s", type, ident, group);
1745 service = __connman_service_lookup(name);
1747 if (service != NULL)
1750 network = create_hidden_wifi(device, ssid, mode, security);
1751 if (network != NULL) {
1752 connman_network_set_group(network, group);
1756 service = __connman_service_lookup(name);
1762 if (service == NULL) {
1767 set_reconnect_state(service, FALSE);
1769 __connman_device_disconnect(device);
1771 if (passphrase != NULL) {
1772 g_free(service->passphrase);
1773 service->passphrase = g_strdup(passphrase);
1776 service->userconnect = TRUE;
1778 err = __connman_service_connect(service);
1779 if (err < 0 && err != -EINPROGRESS)
1782 g_dbus_send_reply(connection, msg,
1783 DBUS_TYPE_OBJECT_PATH, &service->path,
1789 if (service != NULL && created == TRUE) {
1790 struct connman_network *network = service->network;
1792 if (network != NULL) {
1793 connman_network_set_available(network, FALSE);
1794 __connman_device_cleanup_networks(device);
1797 __connman_service_put(service);
1804 * __connman_service_get:
1805 * @identifier: service identifier
1807 * Look up a service by identifier or create a new one if not found
1809 static struct connman_service *__connman_service_get(const char *identifier)
1811 struct connman_service *service;
1812 GSequenceIter *iter;
1814 iter = g_hash_table_lookup(service_hash, identifier);
1816 service = g_sequence_get(iter);
1817 if (service != NULL)
1818 g_atomic_int_inc(&service->refcount);
1822 service = connman_service_create();
1823 if (service == NULL)
1826 DBG("service %p", service);
1828 service->identifier = g_strdup(identifier);
1830 service->profile = g_strdup(__connman_profile_active_ident());
1832 __connman_storage_load_service(service);
1834 iter = g_sequence_insert_sorted(service_list, service,
1835 service_compare, NULL);
1837 g_hash_table_insert(service_hash, service->identifier, iter);
1842 static int service_register(struct connman_service *service)
1844 const char *path = __connman_profile_active_path();
1845 GSequenceIter *iter;
1847 DBG("service %p", service);
1849 if (service->path != NULL)
1852 service->path = g_strdup_printf("%s/%s", path, service->identifier);
1854 DBG("path %s", service->path);
1856 g_dbus_register_interface(connection, service->path,
1857 CONNMAN_SERVICE_INTERFACE,
1858 service_methods, service_signals,
1859 NULL, service, NULL);
1861 __connman_storage_load_service(service);
1863 iter = g_hash_table_lookup(service_hash, service->identifier);
1865 g_sequence_sort_changed(iter, service_compare, NULL);
1867 __connman_profile_changed(TRUE);
1872 static void service_up(struct connman_ipconfig *ipconfig)
1874 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
1877 static void service_down(struct connman_ipconfig *ipconfig)
1879 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
1882 static void service_lower_up(struct connman_ipconfig *ipconfig)
1884 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
1887 static void service_lower_down(struct connman_ipconfig *ipconfig)
1889 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
1892 static void service_ip_bound(struct connman_ipconfig *ipconfig)
1894 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
1897 static void service_ip_release(struct connman_ipconfig *ipconfig)
1899 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
1902 static const struct connman_ipconfig_ops service_ops = {
1904 .down = service_down,
1905 .lower_up = service_lower_up,
1906 .lower_down = service_lower_down,
1907 .ip_bound = service_ip_bound,
1908 .ip_release = service_ip_release,
1911 static void setup_ipconfig(struct connman_service *service, int index)
1916 service->ipconfig = connman_ipconfig_create(index);
1917 if (service->ipconfig == NULL)
1920 connman_ipconfig_set_method(service->ipconfig,
1921 CONNMAN_IPCONFIG_METHOD_DHCP);
1923 __connman_storage_load_service(service);
1925 connman_ipconfig_set_data(service->ipconfig, service);
1927 connman_ipconfig_set_ops(service->ipconfig, &service_ops);
1928 connman_ipconfig_set_ops(service->ipconfig, NULL);
1932 * __connman_service_lookup_from_network:
1933 * @network: network structure
1935 * Look up a service by network (reference count will not be increased)
1937 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
1939 struct connman_service *service;
1940 const char *ident, *group;
1943 DBG("network %p", network);
1945 ident = __connman_network_get_ident(network);
1949 group = connman_network_get_group(network);
1953 name = g_strdup_printf("%s_%s_%s",
1954 __connman_network_get_type(network), ident, group);
1955 service = __connman_service_lookup(name);
1961 const char *__connman_service_get_path(struct connman_service *service)
1963 return service->path;
1966 unsigned int __connman_service_get_order(struct connman_service *service)
1968 GSequenceIter *iter;
1970 if (service == NULL)
1973 if (service->favorite == FALSE) {
1978 iter = g_hash_table_lookup(service_hash, service->identifier);
1980 if (g_sequence_iter_get_position(iter) == 0)
1987 return service->order;
1990 static enum connman_service_type convert_network_type(struct connman_network *network)
1992 enum connman_network_type type = connman_network_get_type(network);
1995 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1996 case CONNMAN_NETWORK_TYPE_VENDOR:
1998 case CONNMAN_NETWORK_TYPE_ETHERNET:
1999 return CONNMAN_SERVICE_TYPE_ETHERNET;
2000 case CONNMAN_NETWORK_TYPE_WIFI:
2001 return CONNMAN_SERVICE_TYPE_WIFI;
2002 case CONNMAN_NETWORK_TYPE_WIMAX:
2003 return CONNMAN_SERVICE_TYPE_WIMAX;
2004 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
2005 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
2006 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
2007 case CONNMAN_NETWORK_TYPE_CELLULAR:
2008 case CONNMAN_NETWORK_TYPE_MBM:
2009 case CONNMAN_NETWORK_TYPE_HSO:
2010 return CONNMAN_SERVICE_TYPE_CELLULAR;
2013 return CONNMAN_SERVICE_TYPE_UNKNOWN;
2016 static enum connman_service_mode convert_wifi_mode(const char *mode)
2019 return CONNMAN_SERVICE_MODE_UNKNOWN;
2020 else if (g_str_equal(mode, "managed") == TRUE)
2021 return CONNMAN_SERVICE_MODE_MANAGED;
2022 else if (g_str_equal(mode, "adhoc") == TRUE)
2023 return CONNMAN_SERVICE_MODE_ADHOC;
2025 return CONNMAN_SERVICE_MODE_UNKNOWN;
2028 static enum connman_service_security convert_wifi_security(const char *security)
2030 if (security == NULL)
2031 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2032 else if (g_str_equal(security, "none") == TRUE)
2033 return CONNMAN_SERVICE_SECURITY_NONE;
2034 else if (g_str_equal(security, "wep") == TRUE)
2035 return CONNMAN_SERVICE_SECURITY_WEP;
2036 else if (g_str_equal(security, "wep") == TRUE)
2037 return CONNMAN_SERVICE_SECURITY_PSK;
2038 else if (g_str_equal(security, "ieee8021x") == TRUE)
2039 return CONNMAN_SERVICE_SECURITY_8021X;
2040 else if (g_str_equal(security, "wpa") == TRUE)
2041 return CONNMAN_SERVICE_SECURITY_WPA;
2042 else if (g_str_equal(security, "rsn") == TRUE)
2043 return CONNMAN_SERVICE_SECURITY_RSN;
2045 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2048 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
2053 return CONNMAN_SERVICE_MODE_GPRS;
2055 return CONNMAN_SERVICE_MODE_EDGE;
2060 return CONNMAN_SERVICE_MODE_UMTS;
2063 return CONNMAN_SERVICE_MODE_UNKNOWN;
2066 static void update_from_network(struct connman_service *service,
2067 struct connman_network *network)
2069 connman_uint8_t strength = service->strength;
2070 GSequenceIter *iter;
2073 DBG("service %p network %p", service, network);
2075 if (service->state == CONNMAN_SERVICE_STATE_READY)
2078 if (is_connecting(service) == TRUE)
2081 str = connman_network_get_string(network, "Name");
2083 g_free(service->name);
2084 service->name = g_strdup(str);
2085 service->hidden = FALSE;
2087 g_free(service->name);
2088 service->name = NULL;
2089 service->hidden = TRUE;
2092 service->strength = connman_network_get_uint8(network, "Strength");
2093 service->roaming = connman_network_get_bool(network, "Roaming");
2095 if (service->strength == 0) {
2097 * Filter out 0-values; it's unclear what they mean
2098 * and they cause anomalous sorting of the priority list.
2100 service->strength = strength;
2103 str = connman_network_get_string(network, "WiFi.Mode");
2104 service->mode = convert_wifi_mode(str);
2106 str = connman_network_get_string(network, "WiFi.Security");
2107 service->security = convert_wifi_security(str);
2109 str = connman_network_get_string(network, "Cellular.MCC");
2110 g_free(service->mcc);
2111 service->mcc = g_strdup(str);
2113 str = connman_network_get_string(network, "Cellular.MNC");
2114 g_free(service->mnc);
2115 service->mnc = g_strdup(str);
2117 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
2118 connman_uint8_t value = connman_network_get_uint8(network,
2121 service->mode = convert_cellular_mode(value);
2124 if (service->strength > strength && service->network != NULL) {
2125 connman_network_unref(service->network);
2126 service->network = connman_network_ref(network);
2128 strength_changed(service);
2131 if (service->network == NULL)
2132 service->network = connman_network_ref(network);
2134 iter = g_hash_table_lookup(service_hash, service->identifier);
2136 g_sequence_sort_changed(iter, service_compare, NULL);
2140 * __connman_service_create_from_network:
2141 * @network: network structure
2143 * Look up service by network and if not found, create one
2145 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
2147 struct connman_service *service;
2148 const char *ident, *group;
2151 DBG("network %p", network);
2153 ident = __connman_network_get_ident(network);
2157 group = connman_network_get_group(network);
2161 name = g_strdup_printf("%s_%s_%s",
2162 __connman_network_get_type(network), ident, group);
2163 service = __connman_service_get(name);
2166 if (service == NULL)
2169 if (__connman_network_get_weakness(network) == TRUE)
2172 if (service->path != NULL) {
2173 update_from_network(service, network);
2174 __connman_profile_changed(TRUE);
2178 service->type = convert_network_type(network);
2180 switch (service->type) {
2181 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2182 case CONNMAN_SERVICE_TYPE_SYSTEM:
2183 case CONNMAN_SERVICE_TYPE_ETHERNET:
2184 case CONNMAN_SERVICE_TYPE_WIMAX:
2185 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2186 case CONNMAN_SERVICE_TYPE_VPN:
2187 service->autoconnect = FALSE;
2189 case CONNMAN_SERVICE_TYPE_WIFI:
2190 case CONNMAN_SERVICE_TYPE_CELLULAR:
2191 service->autoconnect = TRUE;
2195 service->state = CONNMAN_SERVICE_STATE_IDLE;
2197 update_from_network(service, network);
2199 setup_ipconfig(service, connman_network_get_index(network));
2201 service_register(service);
2203 __connman_profile_changed(TRUE);
2205 if (service->favorite == TRUE)
2206 __connman_service_auto_connect();
2211 void __connman_service_update_from_network(struct connman_network *network)
2213 struct connman_service *service;
2214 enum connman_service_mode mode;
2215 connman_uint8_t strength, value;
2216 connman_bool_t roaming;
2217 GSequenceIter *iter;
2219 DBG("network %p", network);
2221 service = __connman_service_lookup_from_network(network);
2222 if (service == NULL)
2225 if (service->network == NULL)
2228 strength = connman_network_get_uint8(service->network, "Strength");
2229 if (strength == service->strength)
2232 service->strength = strength;
2234 strength_changed(service);
2237 roaming = connman_network_get_bool(service->network, "Roaming");
2238 if (roaming == service->roaming)
2241 service->roaming = roaming;
2243 roaming_changed(service);
2245 iter = g_hash_table_lookup(service_hash, service->identifier);
2247 g_sequence_sort_changed(iter, service_compare, NULL);
2250 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
2253 value = connman_network_get_uint8(service->network, "Cellular.Mode");
2254 mode = convert_cellular_mode(value);
2256 if (mode == service->mode)
2259 service->mode = mode;
2261 mode_changed(service);
2264 void __connman_service_remove_from_network(struct connman_network *network)
2266 struct connman_service *service;
2268 DBG("network %p", network);
2270 service = __connman_service_lookup_from_network(network);
2271 if (service == NULL)
2274 __connman_service_put(service);
2277 static int service_load(struct connman_service *service)
2279 const char *ident = service->profile;
2281 GError *error = NULL;
2282 gchar *pathname, *data = NULL;
2285 connman_bool_t autoconnect;
2286 unsigned int ssid_len;
2289 DBG("service %p", service);
2294 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2295 if (pathname == NULL)
2298 keyfile = g_key_file_new();
2300 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
2307 if (g_key_file_load_from_data(keyfile, data, length,
2308 0, NULL) == FALSE) {
2315 switch (service->type) {
2316 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2317 case CONNMAN_SERVICE_TYPE_SYSTEM:
2318 case CONNMAN_SERVICE_TYPE_ETHERNET:
2319 case CONNMAN_SERVICE_TYPE_VPN:
2321 case CONNMAN_SERVICE_TYPE_WIFI:
2322 if (service->name == NULL) {
2325 name = g_key_file_get_string(keyfile,
2326 service->identifier, "Name", NULL);
2328 g_free(service->name);
2329 service->name = name;
2332 if (service->network != NULL)
2333 connman_network_set_name(service->network,
2337 if (service->network &&
2338 connman_network_get_blob(service->network,
2339 "WiFi.SSID", &ssid_len) == NULL) {
2342 hex_ssid = g_key_file_get_string(keyfile,
2343 service->identifier,
2346 if (hex_ssid != NULL) {
2348 unsigned int i, j = 0, hex;
2349 size_t hex_ssid_len = strlen(hex_ssid);
2351 ssid = g_try_malloc0(hex_ssid_len / 2);
2358 for (i = 0; i < hex_ssid_len; i += 2) {
2359 sscanf(hex_ssid + i, "%02x", &hex);
2363 connman_network_set_blob(service->network,
2364 "WiFi.SSID", ssid, hex_ssid_len / 2);
2371 case CONNMAN_SERVICE_TYPE_WIMAX:
2372 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2373 case CONNMAN_SERVICE_TYPE_CELLULAR:
2374 service->apn = g_key_file_get_string(keyfile,
2375 service->identifier, "APN", NULL);
2377 service->username = g_key_file_get_string(keyfile,
2378 service->identifier, "Username", NULL);
2380 service->username = g_key_file_get_string(keyfile,
2381 service->identifier, "Password", NULL);
2383 service->favorite = g_key_file_get_boolean(keyfile,
2384 service->identifier, "Favorite", NULL);
2386 autoconnect = g_key_file_get_boolean(keyfile,
2387 service->identifier, "AutoConnect", &error);
2389 service->autoconnect = autoconnect;
2390 g_clear_error(&error);
2392 str = g_key_file_get_string(keyfile,
2393 service->identifier, "Failure", NULL);
2395 service->state = CONNMAN_SERVICE_STATE_FAILURE;
2396 service->error = string2error(str);
2401 str = g_key_file_get_string(keyfile,
2402 service->identifier, "Modified", NULL);
2404 g_time_val_from_iso8601(str, &service->modified);
2408 str = g_key_file_get_string(keyfile,
2409 service->identifier, "Passphrase", NULL);
2411 g_free(service->passphrase);
2412 service->passphrase = str;
2415 if (service->ipconfig != NULL)
2416 __connman_ipconfig_load(service->ipconfig, keyfile,
2417 service->identifier, "IPv4.");
2420 g_key_file_free(keyfile);
2425 static int service_save(struct connman_service *service)
2427 const char *ident = service->profile;
2429 gchar *pathname, *data = NULL;
2434 DBG("service %p", service);
2439 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2440 if (pathname == NULL)
2443 keyfile = g_key_file_new();
2445 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
2449 if (g_key_file_load_from_data(keyfile, data, length,
2457 if (service->name != NULL)
2458 g_key_file_set_string(keyfile, service->identifier,
2459 "Name", service->name);
2461 switch (service->type) {
2462 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2463 case CONNMAN_SERVICE_TYPE_SYSTEM:
2464 case CONNMAN_SERVICE_TYPE_ETHERNET:
2465 case CONNMAN_SERVICE_TYPE_VPN:
2467 case CONNMAN_SERVICE_TYPE_WIFI:
2468 if (service->network) {
2469 const unsigned char *ssid;
2470 unsigned int ssid_len = 0;
2472 ssid = connman_network_get_blob(service->network,
2473 "WiFi.SSID", &ssid_len);
2475 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
2476 char *identifier = service->identifier;
2480 str = g_string_sized_new(ssid_len * 2);
2486 for (i = 0; i < ssid_len; i++)
2487 g_string_append_printf(str,
2490 g_key_file_set_string(keyfile, identifier,
2493 g_string_free(str, TRUE);
2498 case CONNMAN_SERVICE_TYPE_WIMAX:
2499 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2500 case CONNMAN_SERVICE_TYPE_CELLULAR:
2501 if (service->apn != NULL)
2502 g_key_file_set_string(keyfile, service->identifier,
2503 "APN", service->apn);
2505 if (service->username != NULL)
2506 g_key_file_set_string(keyfile, service->identifier,
2507 "Username", service->username);
2509 if (service->password != NULL)
2510 g_key_file_set_string(keyfile, service->identifier,
2511 "Password", service->password);
2513 g_key_file_set_boolean(keyfile, service->identifier,
2514 "Favorite", service->favorite);
2516 if (service->favorite == TRUE)
2517 g_key_file_set_boolean(keyfile, service->identifier,
2518 "AutoConnect", service->autoconnect);
2520 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
2521 const char *failure = error2string(service->error);
2522 if (failure != NULL)
2523 g_key_file_set_string(keyfile,
2524 service->identifier,
2525 "Failure", failure);
2527 g_key_file_remove_key(keyfile, service->identifier,
2533 str = g_time_val_to_iso8601(&service->modified);
2535 g_key_file_set_string(keyfile, service->identifier,
2540 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
2541 g_key_file_set_string(keyfile, service->identifier,
2542 "Passphrase", service->passphrase);
2544 g_key_file_remove_key(keyfile, service->identifier,
2545 "Passphrase", NULL);
2547 if (service->ipconfig != NULL)
2548 __connman_ipconfig_save(service->ipconfig, keyfile,
2549 service->identifier, "IPv4.");
2551 data = g_key_file_to_data(keyfile, &length, NULL);
2553 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
2554 connman_error("Failed to store service information");
2559 g_key_file_free(keyfile);
2566 static struct connman_storage service_storage = {
2568 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
2569 .service_load = service_load,
2570 .service_save = service_save,
2573 int __connman_service_init(void)
2577 connection = connman_dbus_get_connection();
2579 if (connman_storage_register(&service_storage) < 0)
2580 connman_error("Failed to register service storage");
2582 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2585 service_list = g_sequence_new(service_free);
2590 void __connman_service_cleanup(void)
2594 g_sequence_free(service_list);
2595 service_list = NULL;
2597 g_hash_table_destroy(service_hash);
2598 service_hash = NULL;
2600 connman_storage_unregister(&service_storage);
2602 dbus_connection_unref(connection);