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;
63 connman_bool_t roaming;
64 struct connman_ipconfig *ipconfig;
65 struct connman_device *device;
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)
87 g_sequence_foreach(service_list, append_path, iter);
92 struct connman_service *service;
95 static void compare_path(gpointer value, gpointer user_data)
97 struct connman_service *service = value;
98 struct find_data *data = user_data;
100 if (data->service != NULL)
103 if (g_strcmp0(service->path, data->path) == 0)
104 data->service = service;
107 static struct connman_service *find_service(const char *path)
109 struct find_data data = { .path = path, .service = NULL };
111 DBG("path %s", path);
113 g_sequence_foreach(service_list, compare_path, &data);
118 static const char *type2string(enum connman_service_type type)
121 case CONNMAN_SERVICE_TYPE_UNKNOWN:
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:
138 static const char *mode2string(enum connman_service_mode mode)
141 case CONNMAN_SERVICE_MODE_UNKNOWN:
143 case CONNMAN_SERVICE_MODE_MANAGED:
145 case CONNMAN_SERVICE_MODE_ADHOC:
152 static const char *security2string(enum connman_service_security security)
155 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
157 case CONNMAN_SERVICE_SECURITY_NONE:
159 case CONNMAN_SERVICE_SECURITY_WEP:
161 case CONNMAN_SERVICE_SECURITY_WPA:
163 case CONNMAN_SERVICE_SECURITY_RSN:
170 static const char *state2string(enum connman_service_state state)
173 case CONNMAN_SERVICE_STATE_UNKNOWN:
175 case CONNMAN_SERVICE_STATE_IDLE:
177 case CONNMAN_SERVICE_STATE_CARRIER:
179 case CONNMAN_SERVICE_STATE_ASSOCIATION:
180 return "association";
181 case CONNMAN_SERVICE_STATE_CONFIGURATION:
182 return "configuration";
183 case CONNMAN_SERVICE_STATE_READY:
185 case CONNMAN_SERVICE_STATE_DISCONNECT:
187 case CONNMAN_SERVICE_STATE_FAILURE:
194 static const char *error2string(enum connman_service_error error)
197 case CONNMAN_SERVICE_ERROR_UNKNOWN:
199 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
200 return "out-of-range";
201 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
202 return "pin-missing";
203 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
204 return "dhcp-failed";
205 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
206 return "connect-failed";
212 static enum connman_service_error string2error(const char *error)
214 if (g_strcmp0(error, "dhcp-failed") == 0)
215 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
216 else if (g_strcmp0(error, "pin-missing") == 0)
217 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
219 return CONNMAN_SERVICE_ERROR_UNKNOWN;
222 const char *__connman_service_default(void)
224 struct connman_service *service;
227 iter = g_sequence_get_begin_iter(service_list);
229 if (g_sequence_iter_is_end(iter) == TRUE)
232 service = g_sequence_get(iter);
236 if (service->state != CONNMAN_SERVICE_STATE_READY)
239 return type2string(service->type);
242 static void state_changed(struct connman_service *service)
245 DBusMessageIter entry, value;
246 const char *str, *key = "State";
248 if (service->path == NULL)
251 str = state2string(service->state);
255 signal = dbus_message_new_signal(service->path,
256 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
260 dbus_message_iter_init_append(signal, &entry);
262 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
264 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
265 DBUS_TYPE_STRING_AS_STRING, &value);
266 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
267 dbus_message_iter_close_container(&entry, &value);
269 g_dbus_send_message(connection, signal);
272 static void strength_changed(struct connman_service *service)
275 DBusMessageIter entry, value;
276 const char *key = "Strength";
278 if (service->path == NULL)
281 if (service->strength == 0)
284 signal = dbus_message_new_signal(service->path,
285 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
289 dbus_message_iter_init_append(signal, &entry);
291 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
293 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
294 DBUS_TYPE_BYTE_AS_STRING, &value);
295 dbus_message_iter_append_basic(&value, DBUS_TYPE_BYTE,
297 dbus_message_iter_close_container(&entry, &value);
299 g_dbus_send_message(connection, signal);
302 static void autoconnect_changed(struct connman_service *service)
305 DBusMessageIter entry, value;
306 const char *key = "AutoConnect";
308 if (service->path == NULL)
311 signal = dbus_message_new_signal(service->path,
312 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
316 dbus_message_iter_init_append(signal, &entry);
318 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
320 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
321 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
322 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
323 &service->autoconnect);
324 dbus_message_iter_close_container(&entry, &value);
326 g_dbus_send_message(connection, signal);
329 static void passphrase_changed(struct connman_service *service)
332 DBusMessageIter entry, value;
333 dbus_bool_t required;
334 const char *key = "PassphraseRequired";
336 if (service->path == NULL)
339 switch (service->type) {
340 case CONNMAN_SERVICE_TYPE_UNKNOWN:
341 case CONNMAN_SERVICE_TYPE_ETHERNET:
342 case CONNMAN_SERVICE_TYPE_WIMAX:
343 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
344 case CONNMAN_SERVICE_TYPE_CELLULAR:
346 case CONNMAN_SERVICE_TYPE_WIFI:
349 switch (service->security) {
350 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
351 case CONNMAN_SERVICE_SECURITY_NONE:
353 case CONNMAN_SERVICE_SECURITY_WEP:
354 case CONNMAN_SERVICE_SECURITY_WPA:
355 case CONNMAN_SERVICE_SECURITY_RSN:
356 if (service->passphrase == NULL)
363 signal = dbus_message_new_signal(service->path,
364 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
368 dbus_message_iter_init_append(signal, &entry);
370 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
372 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
373 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
374 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &required);
375 dbus_message_iter_close_container(&entry, &value);
377 g_dbus_send_message(connection, signal);
380 static void apn_changed(struct connman_service *service)
383 DBusMessageIter entry, value;
384 dbus_bool_t required;
385 const char *key = "SetupRequired";
387 if (service->path == NULL)
390 switch (service->type) {
391 case CONNMAN_SERVICE_TYPE_UNKNOWN:
392 case CONNMAN_SERVICE_TYPE_ETHERNET:
393 case CONNMAN_SERVICE_TYPE_WIMAX:
394 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
395 case CONNMAN_SERVICE_TYPE_WIFI:
397 case CONNMAN_SERVICE_TYPE_CELLULAR:
401 required = (service->apn == NULL) ? TRUE : FALSE;
403 signal = dbus_message_new_signal(service->path,
404 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
408 dbus_message_iter_init_append(signal, &entry);
410 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
412 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
413 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
414 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &required);
415 dbus_message_iter_close_container(&entry, &value);
417 g_dbus_send_message(connection, signal);
420 static DBusMessage *get_properties(DBusConnection *conn,
421 DBusMessage *msg, void *user_data)
423 struct connman_service *service = user_data;
425 DBusMessageIter array, dict;
426 dbus_bool_t required;
429 DBG("service %p", service);
431 reply = dbus_message_new_method_return(msg);
435 dbus_message_iter_init_append(reply, &array);
437 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
438 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
439 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
440 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
442 str = type2string(service->type);
444 connman_dbus_dict_append_variant(&dict, "Type",
445 DBUS_TYPE_STRING, &str);
447 str = mode2string(service->mode);
449 connman_dbus_dict_append_variant(&dict, "Mode",
450 DBUS_TYPE_STRING, &str);
452 str = security2string(service->security);
454 connman_dbus_dict_append_variant(&dict, "Security",
455 DBUS_TYPE_STRING, &str);
457 str = state2string(service->state);
459 connman_dbus_dict_append_variant(&dict, "State",
460 DBUS_TYPE_STRING, &str);
462 str = error2string(service->error);
464 connman_dbus_dict_append_variant(&dict, "Error",
465 DBUS_TYPE_STRING, &str);
467 if (service->strength > 0)
468 connman_dbus_dict_append_variant(&dict, "Strength",
469 DBUS_TYPE_BYTE, &service->strength);
471 connman_dbus_dict_append_variant(&dict, "Favorite",
472 DBUS_TYPE_BOOLEAN, &service->favorite);
474 if (service->favorite == TRUE)
475 connman_dbus_dict_append_variant(&dict, "AutoConnect",
476 DBUS_TYPE_BOOLEAN, &service->autoconnect);
478 connman_dbus_dict_append_variant(&dict, "AutoConnect",
479 DBUS_TYPE_BOOLEAN, &service->favorite);
481 if (service->name != NULL)
482 connman_dbus_dict_append_variant(&dict, "Name",
483 DBUS_TYPE_STRING, &service->name);
485 switch (service->type) {
486 case CONNMAN_SERVICE_TYPE_UNKNOWN:
487 case CONNMAN_SERVICE_TYPE_ETHERNET:
488 case CONNMAN_SERVICE_TYPE_WIMAX:
489 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
491 case CONNMAN_SERVICE_TYPE_CELLULAR:
492 connman_dbus_dict_append_variant(&dict, "Roaming",
493 DBUS_TYPE_BOOLEAN, &service->roaming);
495 if (service->apn != NULL) {
496 connman_dbus_dict_append_variant(&dict, "APN",
497 DBUS_TYPE_STRING, &service->apn);
502 connman_dbus_dict_append_variant(&dict, "SetupRequired",
503 DBUS_TYPE_BOOLEAN, &required);
505 case CONNMAN_SERVICE_TYPE_WIFI:
506 if (service->passphrase != NULL &&
507 __connman_security_check_privilege(msg,
508 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
509 connman_dbus_dict_append_variant(&dict, "Passphrase",
510 DBUS_TYPE_STRING, &service->passphrase);
514 switch (service->security) {
515 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
516 case CONNMAN_SERVICE_SECURITY_NONE:
518 case CONNMAN_SERVICE_SECURITY_WEP:
519 case CONNMAN_SERVICE_SECURITY_WPA:
520 case CONNMAN_SERVICE_SECURITY_RSN:
521 if (service->passphrase == NULL)
526 connman_dbus_dict_append_variant(&dict, "PassphraseRequired",
527 DBUS_TYPE_BOOLEAN, &required);
531 if (service->ipconfig != NULL)
532 __connman_ipconfig_append_ipv4(service->ipconfig,
535 dbus_message_iter_close_container(&array, &dict);
540 static DBusMessage *set_property(DBusConnection *conn,
541 DBusMessage *msg, void *user_data)
543 struct connman_service *service = user_data;
544 DBusMessageIter iter, value;
548 DBG("service %p", service);
550 if (dbus_message_iter_init(msg, &iter) == FALSE)
551 return __connman_error_invalid_arguments(msg);
553 dbus_message_iter_get_basic(&iter, &name);
554 dbus_message_iter_next(&iter);
555 dbus_message_iter_recurse(&iter, &value);
557 if (__connman_security_check_privilege(msg,
558 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
559 return __connman_error_permission_denied(msg);
561 type = dbus_message_iter_get_arg_type(&value);
563 if (g_str_has_prefix(name, "AutoConnect") == TRUE) {
564 connman_bool_t autoconnect;
566 if (type != DBUS_TYPE_BOOLEAN)
567 return __connman_error_invalid_arguments(msg);
569 if (service->favorite == FALSE)
570 return __connman_error_invalid_service(msg);
572 dbus_message_iter_get_basic(&value, &autoconnect);
574 if (service->autoconnect == autoconnect)
575 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
577 service->autoconnect = autoconnect;
579 autoconnect_changed(service);
581 __connman_storage_save_service(service);
582 } else if (g_str_equal(name, "Passphrase") == TRUE) {
583 const char *passphrase;
585 if (type != DBUS_TYPE_STRING)
586 return __connman_error_invalid_arguments(msg);
588 if (__connman_security_check_privilege(msg,
589 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
590 return __connman_error_permission_denied(msg);
592 dbus_message_iter_get_basic(&value, &passphrase);
594 g_free(service->passphrase);
595 service->passphrase = g_strdup(passphrase);
597 passphrase_changed(service);
599 if (service->network != NULL)
600 connman_network_set_string(service->network,
601 "WiFi.Passphrase", service->passphrase);
603 __connman_storage_save_service(service);
604 } else if (g_str_equal(name, "APN") == TRUE) {
607 if (type != DBUS_TYPE_STRING)
608 return __connman_error_invalid_arguments(msg);
610 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
611 return __connman_error_invalid_service(msg);
613 dbus_message_iter_get_basic(&value, &apn);
615 g_free(service->apn);
616 service->apn = g_strdup(apn);
618 apn_changed(service);
620 if (service->network != NULL)
621 connman_network_set_string(service->network,
622 "Cellular.APN", service->apn);
624 __connman_storage_save_service(service);
625 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
628 if (service->ipconfig == NULL)
629 return __connman_error_invalid_property(msg);
631 err = __connman_ipconfig_set_ipv4(service->ipconfig,
634 return __connman_error_failed(msg, -err);
636 return __connman_error_invalid_property(msg);
638 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
641 static void set_idle(struct connman_service *service)
643 service->state = CONNMAN_SERVICE_STATE_IDLE;
644 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
645 state_changed(service);
648 static DBusMessage *clear_property(DBusConnection *conn,
649 DBusMessage *msg, void *user_data)
651 struct connman_service *service = user_data;
654 DBG("service %p", service);
656 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
659 if (__connman_security_check_privilege(msg,
660 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
661 return __connman_error_permission_denied(msg);
663 if (g_str_equal(name, "Error") == TRUE) {
666 g_get_current_time(&service->modified);
667 __connman_storage_save_service(service);
668 } else if (g_str_equal(name, "Passphrase") == TRUE) {
669 g_free(service->passphrase);
670 service->passphrase = NULL;
672 passphrase_changed(service);
674 __connman_storage_save_service(service);
676 return __connman_error_invalid_property(msg);
678 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
681 static connman_bool_t is_connecting(struct connman_service *service)
683 switch (service->state) {
684 case CONNMAN_SERVICE_STATE_UNKNOWN:
685 case CONNMAN_SERVICE_STATE_IDLE:
686 case CONNMAN_SERVICE_STATE_CARRIER:
687 case CONNMAN_SERVICE_STATE_FAILURE:
688 case CONNMAN_SERVICE_STATE_DISCONNECT:
689 case CONNMAN_SERVICE_STATE_READY:
691 case CONNMAN_SERVICE_STATE_ASSOCIATION:
692 case CONNMAN_SERVICE_STATE_CONFIGURATION:
699 static connman_bool_t is_ignore(struct connman_service *service)
701 if (service->autoconnect == FALSE)
704 if (service->ignore == TRUE)
707 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
713 void __connman_service_auto_connect(void)
715 struct connman_service *service = NULL;
720 iter = g_sequence_get_begin_iter(service_list);
722 while (g_sequence_iter_is_end(iter) == FALSE) {
723 service = g_sequence_get(iter);
725 if (service->pending != NULL)
728 if (is_connecting(service) == TRUE)
731 if (service->favorite == FALSE)
734 if (service->state == CONNMAN_SERVICE_STATE_READY)
737 if (is_ignore(service) == FALSE &&
738 service->state == CONNMAN_SERVICE_STATE_IDLE)
743 iter = g_sequence_iter_next(iter);
746 if (service != NULL) {
747 service->userconnect = FALSE;
748 __connman_service_connect(service);
752 static void reply_pending(struct connman_service *service, int error)
754 if (service->timeout > 0) {
755 g_source_remove(service->timeout);
756 service->timeout = 0;
759 if (service->pending != NULL) {
763 reply = __connman_error_failed(service->pending,
766 g_dbus_send_message(connection, reply);
768 g_dbus_send_reply(connection, service->pending,
771 dbus_message_unref(service->pending);
772 service->pending = NULL;
776 static gboolean connect_timeout(gpointer user_data)
778 struct connman_service *service = user_data;
779 connman_bool_t autoconnect = FALSE;
781 DBG("service %p", service);
783 service->timeout = 0;
785 if (service->network != NULL)
786 __connman_network_disconnect(service->network);
787 else if (service->device != NULL)
788 __connman_device_disconnect(service->device);
790 __connman_ipconfig_disable(service->ipconfig);
792 if (service->pending != NULL) {
795 reply = __connman_error_operation_timeout(service->pending);
797 g_dbus_send_message(connection, reply);
799 dbus_message_unref(service->pending);
800 service->pending = NULL;
804 __connman_service_indicate_state(service,
805 CONNMAN_SERVICE_STATE_FAILURE);
807 if (autoconnect == TRUE && service->userconnect == FALSE)
808 __connman_service_auto_connect();
813 static DBusMessage *connect_service(DBusConnection *conn,
814 DBusMessage *msg, void *user_data)
816 struct connman_service *service = user_data;
820 DBG("service %p", service);
822 if (service->pending != NULL)
823 return __connman_error_in_progress(msg);
825 iter = g_sequence_get_begin_iter(service_list);
827 while (g_sequence_iter_is_end(iter) == FALSE) {
828 struct connman_service *temp = g_sequence_get(iter);
830 if (service->type == temp->type &&
831 is_connecting(temp) == TRUE)
832 return __connman_error_in_progress(msg);
834 iter = g_sequence_iter_next(iter);
837 service->ignore = FALSE;
839 service->userconnect = TRUE;
840 service->failcounter = 0;
842 service->pending = dbus_message_ref(msg);
844 err = __connman_service_connect(service);
846 if (err == -ENOKEY) {
847 if (__connman_agent_request_passphrase(service,
852 if (err != -EINPROGRESS) {
853 dbus_message_unref(service->pending);
854 service->pending = NULL;
856 return __connman_error_failed(msg, -err);
862 dbus_message_unref(service->pending);
863 service->pending = NULL;
865 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
868 static DBusMessage *disconnect_service(DBusConnection *conn,
869 DBusMessage *msg, void *user_data)
871 struct connman_service *service = user_data;
874 DBG("service %p", service);
876 reply_pending(service, ECONNABORTED);
878 service->ignore = TRUE;
880 err = __connman_service_disconnect(service);
882 if (err != -EINPROGRESS)
883 return __connman_error_failed(msg, -err);
888 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
891 static DBusMessage *remove_service(DBusConnection *conn,
892 DBusMessage *msg, void *user_data)
894 struct connman_service *service = user_data;
896 DBG("service %p", service);
898 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
899 return __connman_error_not_supported(msg);
901 if (service->favorite == FALSE)
902 return __connman_error_not_supported(msg);
904 if (service->network != NULL)
905 __connman_network_disconnect(service->network);
907 g_free(service->passphrase);
908 service->passphrase = NULL;
910 passphrase_changed(service);
912 connman_service_set_favorite(service, FALSE);
913 __connman_storage_save_service(service);
915 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
918 static DBusMessage *move_before(DBusConnection *conn,
919 DBusMessage *msg, void *user_data)
921 struct connman_service *service = user_data;
922 struct connman_service *target;
924 GSequenceIter *src, *dst;
926 DBG("service %p", service);
928 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
931 if (service->favorite == FALSE)
932 return __connman_error_not_supported(msg);
934 target = find_service(path);
935 if (target == NULL || target->favorite == FALSE || target == service)
936 return __connman_error_invalid_service(msg);
938 DBG("target %s", target->identifier);
940 if (target->state != service->state)
941 return __connman_error_invalid_service(msg);
943 g_get_current_time(&service->modified);
944 __connman_storage_save_service(service);
946 src = g_hash_table_lookup(service_hash, service->identifier);
947 dst = g_hash_table_lookup(service_hash, target->identifier);
949 g_sequence_move(src, dst);
951 __connman_profile_changed(FALSE);
953 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
956 static DBusMessage *move_after(DBusConnection *conn,
957 DBusMessage *msg, void *user_data)
959 struct connman_service *service = user_data;
960 struct connman_service *target;
963 DBG("service %p", service);
965 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
968 if (service->favorite == FALSE)
969 return __connman_error_not_supported(msg);
971 target = find_service(path);
972 if (target == NULL || target->favorite == FALSE || target == service)
973 return __connman_error_invalid_service(msg);
975 DBG("target %s", target->identifier);
977 if (target->state != service->state)
978 return __connman_error_invalid_service(msg);
980 g_get_current_time(&service->modified);
981 __connman_storage_save_service(service);
983 return __connman_error_not_implemented(msg);
986 static GDBusMethodTable service_methods[] = {
987 { "GetProperties", "", "a{sv}", get_properties },
988 { "SetProperty", "sv", "", set_property },
989 { "ClearProperty", "s", "", clear_property },
990 { "Connect", "", "", connect_service,
991 G_DBUS_METHOD_FLAG_ASYNC },
992 { "Disconnect", "", "", disconnect_service },
993 { "Remove", "", "", remove_service },
994 { "MoveBefore", "o", "", move_before },
995 { "MoveAfter", "o", "", move_after },
999 static GDBusSignalTable service_signals[] = {
1000 { "PropertyChanged", "sv" },
1004 static void service_free(gpointer user_data)
1006 struct connman_service *service = user_data;
1007 char *path = service->path;
1009 DBG("service %p", service);
1011 reply_pending(service, ENOENT);
1013 g_hash_table_remove(service_hash, service->identifier);
1015 service->path = NULL;
1018 __connman_profile_changed(FALSE);
1020 g_dbus_unregister_interface(connection, path,
1021 CONNMAN_SERVICE_INTERFACE);
1025 if (service->network != NULL)
1026 connman_network_unref(service->network);
1028 if (service->ipconfig != NULL) {
1029 connman_ipconfig_unref(service->ipconfig);
1030 service->ipconfig = NULL;
1033 g_free(service->apn);
1034 g_free(service->username);
1035 g_free(service->passphrase);
1036 g_free(service->profile);
1037 g_free(service->name);
1038 g_free(service->passphrase);
1039 g_free(service->identifier);
1044 * __connman_service_put:
1045 * @service: service structure
1047 * Release service if no longer needed
1049 void __connman_service_put(struct connman_service *service)
1051 DBG("service %p", service);
1053 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
1054 GSequenceIter *iter;
1056 iter = g_hash_table_lookup(service_hash, service->identifier);
1058 reply_pending(service, ECONNABORTED);
1060 __connman_service_disconnect(service);
1062 g_sequence_remove(iter);
1064 service_free(service);
1068 static void __connman_service_initialize(struct connman_service *service)
1070 DBG("service %p", service);
1072 service->refcount = 1;
1074 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
1075 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
1076 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
1077 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
1079 service->favorite = FALSE;
1080 service->hidden = FALSE;
1082 service->ignore = FALSE;
1084 service->userconnect = FALSE;
1090 * connman_service_create:
1092 * Allocate a new service.
1094 * Returns: a newly-allocated #connman_service structure
1096 struct connman_service *connman_service_create(void)
1098 struct connman_service *service;
1100 service = g_try_new0(struct connman_service, 1);
1101 if (service == NULL)
1104 DBG("service %p", service);
1106 __connman_service_initialize(service);
1112 * connman_service_ref:
1113 * @service: service structure
1115 * Increase reference counter of service
1117 struct connman_service *connman_service_ref(struct connman_service *service)
1119 g_atomic_int_inc(&service->refcount);
1125 * connman_service_unref:
1126 * @service: service structure
1128 * Decrease reference counter of service
1130 void connman_service_unref(struct connman_service *service)
1132 __connman_service_put(service);
1135 static gint service_compare(gconstpointer a, gconstpointer b,
1138 struct connman_service *service_a = (void *) a;
1139 struct connman_service *service_b = (void *) b;
1141 if (service_a->state != service_b->state) {
1142 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
1144 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
1148 if (service_a->order > service_b->order)
1151 if (service_a->order < service_b->order)
1154 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
1157 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
1160 if (service_a->type != service_b->type) {
1161 switch (service_a->type) {
1162 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1163 case CONNMAN_SERVICE_TYPE_ETHERNET:
1165 case CONNMAN_SERVICE_TYPE_WIFI:
1167 case CONNMAN_SERVICE_TYPE_WIMAX:
1168 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1169 case CONNMAN_SERVICE_TYPE_CELLULAR:
1174 return (gint) service_b->strength - (gint) service_a->strength;
1178 * connman_service_set_favorite:
1179 * @service: service structure
1180 * @favorite: favorite value
1182 * Change the favorite setting of service
1184 int connman_service_set_favorite(struct connman_service *service,
1185 connman_bool_t favorite)
1187 GSequenceIter *iter;
1189 iter = g_hash_table_lookup(service_hash, service->identifier);
1193 if (service->favorite == favorite)
1196 service->favorite = favorite;
1198 g_sequence_sort_changed(iter, service_compare, NULL);
1200 __connman_profile_changed(FALSE);
1205 static void default_changed(void)
1207 DBusMessage *signal;
1208 DBusMessageIter entry, value;
1209 const char *key = "DefaultTechnology";
1210 const char *str = __connman_service_default();
1212 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1213 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1217 dbus_message_iter_init_append(signal, &entry);
1219 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1221 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1222 DBUS_TYPE_STRING_AS_STRING, &value);
1223 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
1224 dbus_message_iter_close_container(&entry, &value);
1226 g_dbus_send_message(connection, signal);
1229 int __connman_service_indicate_state(struct connman_service *service,
1230 enum connman_service_state state)
1232 GSequenceIter *iter;
1234 DBG("service %p state %d", service, state);
1236 if (service == NULL)
1239 if (service->state == state)
1242 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
1243 state == CONNMAN_SERVICE_STATE_IDLE)
1246 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
1247 state == CONNMAN_SERVICE_STATE_DISCONNECT)
1250 if (state == CONNMAN_SERVICE_STATE_IDLE &&
1251 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
1252 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
1253 state_changed(service);
1255 reply_pending(service, ECONNABORTED);
1257 __connman_service_disconnect(service);
1260 service->state = state;
1261 state_changed(service);
1263 if (state == CONNMAN_SERVICE_STATE_READY) {
1264 connman_service_set_favorite(service, TRUE);
1266 reply_pending(service, 0);
1268 service->userconnect = FALSE;
1269 service->failcounter = 0;
1271 g_get_current_time(&service->modified);
1272 __connman_storage_save_service(service);
1274 __connman_notifier_connect(service->type);
1277 } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
1280 __connman_notifier_disconnect(service->type);
1283 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
1284 if (service->failcounter++ < MAX_CONNECT_RETRIES) {
1285 connman_warn("Connecting again (try %d)",
1286 service->failcounter);
1287 __connman_service_disconnect(service);
1288 __connman_service_connect(service);
1292 reply_pending(service, EIO);
1294 if (service->userconnect == FALSE)
1295 __connman_service_auto_connect();
1297 g_get_current_time(&service->modified);
1298 __connman_storage_save_service(service);
1300 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1302 iter = g_hash_table_lookup(service_hash, service->identifier);
1304 g_sequence_sort_changed(iter, service_compare, NULL);
1306 __connman_profile_changed(FALSE);
1308 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
1309 service->state == CONNMAN_SERVICE_STATE_FAILURE)
1310 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
1315 int __connman_service_indicate_error(struct connman_service *service,
1316 enum connman_service_error error)
1318 DBG("service %p error %d", service, error);
1320 if (service == NULL)
1323 service->error = error;
1325 return __connman_service_indicate_state(service,
1326 CONNMAN_SERVICE_STATE_FAILURE);
1329 int __connman_service_indicate_default(struct connman_service *service)
1331 DBG("service %p", service);
1338 static connman_bool_t prepare_network(struct connman_service *service)
1340 enum connman_network_type type;
1341 unsigned int ssid_len;
1343 type = connman_network_get_type(service->network);
1346 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1347 case CONNMAN_NETWORK_TYPE_VENDOR:
1349 case CONNMAN_NETWORK_TYPE_WIFI:
1350 if (connman_network_get_blob(service->network, "WiFi.SSID",
1354 connman_network_set_string(service->network,
1355 "WiFi.Passphrase", service->passphrase);
1357 case CONNMAN_NETWORK_TYPE_WIMAX:
1358 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1359 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1361 case CONNMAN_NETWORK_TYPE_MBM:
1362 case CONNMAN_NETWORK_TYPE_HSO:
1363 connman_network_set_string(service->network,
1364 "Cellular.APN", service->apn);
1371 int __connman_service_connect(struct connman_service *service)
1375 DBG("service %p", service);
1377 if (service->state == CONNMAN_SERVICE_STATE_READY)
1380 if (is_connecting(service) == TRUE)
1383 switch (service->type) {
1384 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1386 case CONNMAN_SERVICE_TYPE_ETHERNET:
1387 case CONNMAN_SERVICE_TYPE_WIMAX:
1388 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1390 case CONNMAN_SERVICE_TYPE_CELLULAR:
1391 if (service->apn == NULL)
1394 case CONNMAN_SERVICE_TYPE_WIFI:
1395 switch (service->security) {
1396 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1397 case CONNMAN_SERVICE_SECURITY_NONE:
1399 case CONNMAN_SERVICE_SECURITY_WEP:
1400 case CONNMAN_SERVICE_SECURITY_WPA:
1401 case CONNMAN_SERVICE_SECURITY_RSN:
1402 if (service->passphrase == NULL)
1409 if (service->network != NULL) {
1410 if (prepare_network(service) == FALSE)
1413 __connman_ipconfig_enable(service->ipconfig);
1415 err = __connman_network_connect(service->network);
1416 } else if (service->device != NULL) {
1417 if (service->favorite == FALSE)
1420 __connman_ipconfig_enable(service->ipconfig);
1422 err = __connman_device_connect(service->device);
1427 if (err != -EINPROGRESS) {
1428 __connman_ipconfig_disable(service->ipconfig);
1432 service->timeout = g_timeout_add_seconds(45,
1433 connect_timeout, service);
1435 return -EINPROGRESS;
1441 int __connman_service_disconnect(struct connman_service *service)
1445 DBG("service %p", service);
1447 if (service->network != NULL) {
1448 err = __connman_network_disconnect(service->network);
1449 } else if (service->device != NULL) {
1450 if (service->favorite == FALSE)
1452 err = __connman_device_disconnect(service->device);
1456 __connman_ipconfig_disable(service->ipconfig);
1459 if (err != -EINPROGRESS)
1462 return -EINPROGRESS;
1469 * __connman_service_lookup:
1470 * @identifier: service identifier
1472 * Look up a service by identifier (reference count will not be increased)
1474 static struct connman_service *__connman_service_lookup(const char *identifier)
1476 GSequenceIter *iter;
1478 iter = g_hash_table_lookup(service_hash, identifier);
1480 return g_sequence_get(iter);
1485 static struct connman_network *create_hidden_wifi(struct connman_device *device,
1486 const char *ssid, const char *mode, const char *security)
1488 struct connman_network *network;
1491 unsigned int i, ssid_len;
1493 ssid_len = strlen(ssid);
1497 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
1498 if (network == NULL)
1501 connman_network_set_blob(network, "WiFi.SSID",
1502 (unsigned char *) ssid, ssid_len);
1504 connman_network_set_string(network, "WiFi.Mode", mode);
1505 connman_network_set_string(network, "WiFi.Security", security);
1507 name = g_try_malloc0(ssid_len + 1);
1509 connman_network_unref(network);
1513 for (i = 0; i < ssid_len; i++) {
1514 if (g_ascii_isprint(ssid[i]))
1520 connman_network_set_name(network, name);
1524 index = connman_device_get_index(device);
1525 connman_network_set_index(network, index);
1527 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
1529 if (connman_device_add_network(device, network) < 0) {
1530 connman_network_unref(network);
1534 connman_network_set_available(network, TRUE);
1539 int __connman_service_create_and_connect(DBusMessage *msg)
1541 struct connman_service *service;
1542 struct connman_network *network;
1543 struct connman_device *device;
1544 DBusMessageIter iter, array;
1545 const char *mode = "managed", *security = "none";
1546 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
1547 unsigned int ssid_len = 0;
1550 gboolean created = FALSE;
1553 dbus_message_iter_init(msg, &iter);
1554 dbus_message_iter_recurse(&iter, &array);
1556 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1557 DBusMessageIter entry, value;
1560 dbus_message_iter_recurse(&array, &entry);
1561 dbus_message_iter_get_basic(&entry, &key);
1563 dbus_message_iter_next(&entry);
1564 dbus_message_iter_recurse(&entry, &value);
1566 switch (dbus_message_iter_get_arg_type(&value)) {
1567 case DBUS_TYPE_STRING:
1568 if (g_str_equal(key, "Type") == TRUE)
1569 dbus_message_iter_get_basic(&value, &type);
1570 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
1571 g_str_equal(key, "Mode") == TRUE)
1572 dbus_message_iter_get_basic(&value, &mode);
1573 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
1574 g_str_equal(key, "Security") == TRUE)
1575 dbus_message_iter_get_basic(&value, &security);
1576 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
1577 g_str_equal(key, "Passphrase") == TRUE)
1578 dbus_message_iter_get_basic(&value, &passphrase);
1579 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
1580 g_str_equal(key, "SSID") == TRUE)
1581 dbus_message_iter_get_basic(&value, &ssid);
1584 dbus_message_iter_next(&array);
1590 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
1596 ssid_len = strlen(ssid);
1600 if (g_strcmp0(security, "none") != 0 &&
1601 g_strcmp0(security, "wep") != 0 &&
1602 g_strcmp0(security, "wpa") != 0 &&
1603 g_strcmp0(security, "rsn") != 0)
1606 device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
1610 ident = __connman_device_get_ident(device);
1614 group = connman_wifi_build_group_name((unsigned char *) ssid,
1615 ssid_len, mode, security);
1619 name = g_strdup_printf("%s_%s_%s", type, ident, group);
1621 service = __connman_service_lookup(name);
1623 if (service != NULL)
1626 network = create_hidden_wifi(device, ssid, mode, security);
1627 if (network != NULL) {
1628 connman_network_set_group(network, group);
1632 service = __connman_service_lookup(name);
1638 if (service == NULL) {
1643 __connman_device_disconnect(device);
1645 if (passphrase != NULL) {
1646 g_free(service->passphrase);
1647 service->passphrase = g_strdup(passphrase);
1650 service->userconnect = TRUE;
1652 err = __connman_service_connect(service);
1653 if (err < 0 && err != -EINPROGRESS)
1656 g_dbus_send_reply(connection, msg,
1657 DBUS_TYPE_OBJECT_PATH, &service->path,
1663 if (service != NULL && created == TRUE) {
1664 struct connman_network *network = service->network;
1666 if (network != NULL) {
1667 connman_network_set_available(network, FALSE);
1668 __connman_device_cleanup_networks(device);
1671 __connman_service_put(service);
1678 * __connman_service_get:
1679 * @identifier: service identifier
1681 * Look up a service by identifier or create a new one if not found
1683 static struct connman_service *__connman_service_get(const char *identifier)
1685 struct connman_service *service;
1686 GSequenceIter *iter;
1688 iter = g_hash_table_lookup(service_hash, identifier);
1690 service = g_sequence_get(iter);
1691 if (service != NULL)
1692 g_atomic_int_inc(&service->refcount);
1696 service = connman_service_create();
1697 if (service == NULL)
1700 DBG("service %p", service);
1702 service->identifier = g_strdup(identifier);
1704 service->profile = g_strdup(__connman_profile_active_ident());
1706 __connman_storage_load_service(service);
1708 iter = g_sequence_insert_sorted(service_list, service,
1709 service_compare, NULL);
1711 g_hash_table_insert(service_hash, service->identifier, iter);
1716 static int service_register(struct connman_service *service)
1718 const char *path = __connman_profile_active_path();
1719 GSequenceIter *iter;
1721 DBG("service %p", service);
1723 if (service->path != NULL)
1726 service->path = g_strdup_printf("%s/%s", path, service->identifier);
1728 DBG("path %s", service->path);
1730 g_dbus_register_interface(connection, service->path,
1731 CONNMAN_SERVICE_INTERFACE,
1732 service_methods, service_signals,
1733 NULL, service, NULL);
1735 __connman_storage_load_service(service);
1737 iter = g_hash_table_lookup(service_hash, service->identifier);
1739 g_sequence_sort_changed(iter, service_compare, NULL);
1741 __connman_profile_changed(TRUE);
1746 static void service_up(struct connman_ipconfig *ipconfig)
1748 connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
1751 static void service_down(struct connman_ipconfig *ipconfig)
1753 connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
1756 static void service_lower_up(struct connman_ipconfig *ipconfig)
1758 connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
1761 static void service_lower_down(struct connman_ipconfig *ipconfig)
1763 connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
1766 static void service_ip_bound(struct connman_ipconfig *ipconfig)
1768 connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
1771 static void service_ip_release(struct connman_ipconfig *ipconfig)
1773 connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
1776 static const struct connman_ipconfig_ops service_ops = {
1778 .down = service_down,
1779 .lower_up = service_lower_up,
1780 .lower_down = service_lower_down,
1781 .ip_bound = service_ip_bound,
1782 .ip_release = service_ip_release,
1785 static void setup_ipconfig(struct connman_service *service, int index)
1790 service->ipconfig = connman_ipconfig_create(index);
1791 if (service->ipconfig == NULL)
1794 connman_ipconfig_set_method(service->ipconfig,
1795 CONNMAN_IPCONFIG_METHOD_DHCP);
1797 __connman_storage_load_service(service);
1799 connman_ipconfig_set_data(service->ipconfig, service);
1801 connman_ipconfig_set_ops(service->ipconfig, &service_ops);
1802 connman_ipconfig_set_ops(service->ipconfig, NULL);
1806 * __connman_service_lookup_from_device:
1807 * @device: device structure
1809 * Look up a service by device (reference count will not be increased)
1811 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
1813 struct connman_service *service;
1817 ident = __connman_device_get_ident(device);
1821 name = g_strdup_printf("%s_%s",
1822 __connman_device_get_type(device), ident);
1823 service = __connman_service_lookup(name);
1830 * __connman_service_create_from_device:
1831 * @device: device structure
1833 * Look up service by device and if not found, create one
1835 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
1837 struct connman_service *service;
1841 ident = __connman_device_get_ident(device);
1845 name = g_strdup_printf("%s_%s",
1846 __connman_device_get_type(device), ident);
1847 service = __connman_service_get(name);
1850 if (service == NULL)
1853 if (service->path != NULL) {
1854 __connman_profile_changed(TRUE);
1858 service->type = __connman_device_get_service_type(device);
1860 service->autoconnect = FALSE;
1862 service->device = device;
1864 setup_ipconfig(service, connman_device_get_index(device));
1866 service_register(service);
1868 __connman_profile_changed(TRUE);
1870 if (service->favorite == TRUE)
1871 __connman_service_auto_connect();
1876 void __connman_service_remove_from_device(struct connman_device *device)
1878 struct connman_service *service;
1879 enum connman_service_type type;
1881 service = __connman_service_lookup_from_device(device);
1882 if (service == NULL)
1885 type = service->type;
1887 __connman_service_put(service);
1891 __connman_notifier_disconnect(type);
1893 __connman_service_auto_connect();
1897 * __connman_service_lookup_from_network:
1898 * @network: network structure
1900 * Look up a service by network (reference count will not be increased)
1902 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
1904 struct connman_service *service;
1905 const char *ident, *group;
1908 ident = __connman_network_get_ident(network);
1912 group = connman_network_get_group(network);
1916 name = g_strdup_printf("%s_%s_%s",
1917 __connman_network_get_type(network), ident, group);
1918 service = __connman_service_lookup(name);
1924 const char *__connman_service_get_path(struct connman_service *service)
1926 return service->path;
1929 unsigned int __connman_service_get_order(struct connman_service *service)
1931 GSequenceIter *iter;
1933 if (service == NULL)
1936 if (service->favorite == FALSE) {
1941 iter = g_hash_table_lookup(service_hash, service->identifier);
1943 if (g_sequence_iter_get_position(iter) == 0)
1950 return service->order;
1953 static enum connman_service_type convert_network_type(struct connman_network *network)
1955 enum connman_network_type type = connman_network_get_type(network);
1958 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1959 case CONNMAN_NETWORK_TYPE_VENDOR:
1961 case CONNMAN_NETWORK_TYPE_WIFI:
1962 return CONNMAN_SERVICE_TYPE_WIFI;
1963 case CONNMAN_NETWORK_TYPE_WIMAX:
1964 return CONNMAN_SERVICE_TYPE_WIMAX;
1965 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1966 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1967 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
1968 case CONNMAN_NETWORK_TYPE_MBM:
1969 case CONNMAN_NETWORK_TYPE_HSO:
1970 return CONNMAN_SERVICE_TYPE_CELLULAR;
1973 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1976 static enum connman_service_mode convert_wifi_mode(const char *mode)
1979 return CONNMAN_SERVICE_MODE_UNKNOWN;
1980 else if (g_str_equal(mode, "managed") == TRUE)
1981 return CONNMAN_SERVICE_MODE_MANAGED;
1982 else if (g_str_equal(mode, "adhoc") == TRUE)
1983 return CONNMAN_SERVICE_MODE_ADHOC;
1985 return CONNMAN_SERVICE_MODE_UNKNOWN;
1988 static enum connman_service_mode convert_wifi_security(const char *security)
1990 if (security == NULL)
1991 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1992 else if (g_str_equal(security, "none") == TRUE)
1993 return CONNMAN_SERVICE_SECURITY_NONE;
1994 else if (g_str_equal(security, "wep") == TRUE)
1995 return CONNMAN_SERVICE_SECURITY_WEP;
1996 else if (g_str_equal(security, "wpa") == TRUE)
1997 return CONNMAN_SERVICE_SECURITY_WPA;
1998 else if (g_str_equal(security, "rsn") == TRUE)
1999 return CONNMAN_SERVICE_SECURITY_RSN;
2001 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2004 static void update_from_network(struct connman_service *service,
2005 struct connman_network *network)
2007 connman_uint8_t strength = service->strength;
2008 GSequenceIter *iter;
2011 if (service->state == CONNMAN_SERVICE_STATE_READY)
2014 if (is_connecting(service) == TRUE)
2017 str = connman_network_get_string(network, "Name");
2019 g_free(service->name);
2020 service->name = g_strdup(str);
2021 service->hidden = FALSE;
2023 g_free(service->name);
2024 service->name = NULL;
2025 service->hidden = TRUE;
2028 service->strength = connman_network_get_uint8(network, "Strength");
2030 str = connman_network_get_string(network, "WiFi.Mode");
2031 service->mode = convert_wifi_mode(str);
2033 str = connman_network_get_string(network, "WiFi.Security");
2034 service->security = convert_wifi_security(str);
2036 if (service->strength > strength && service->network != NULL) {
2037 connman_network_unref(service->network);
2038 service->network = connman_network_ref(network);
2040 strength_changed(service);
2043 if (service->network == NULL)
2044 service->network = connman_network_ref(network);
2046 iter = g_hash_table_lookup(service_hash, service->identifier);
2048 g_sequence_sort_changed(iter, service_compare, NULL);
2052 * __connman_service_create_from_network:
2053 * @network: network structure
2055 * Look up service by network and if not found, create one
2057 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
2059 struct connman_service *service;
2060 const char *ident, *group;
2063 ident = __connman_network_get_ident(network);
2067 group = connman_network_get_group(network);
2071 name = g_strdup_printf("%s_%s_%s",
2072 __connman_network_get_type(network), ident, group);
2073 service = __connman_service_get(name);
2076 if (service == NULL)
2079 if (__connman_network_get_weakness(network) == TRUE)
2082 if (service->path != NULL) {
2083 update_from_network(service, network);
2084 __connman_profile_changed(TRUE);
2088 service->type = convert_network_type(network);
2090 switch (service->type) {
2091 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2092 case CONNMAN_SERVICE_TYPE_ETHERNET:
2093 case CONNMAN_SERVICE_TYPE_WIMAX:
2094 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2095 case CONNMAN_SERVICE_TYPE_CELLULAR:
2096 service->autoconnect = FALSE;
2098 case CONNMAN_SERVICE_TYPE_WIFI:
2099 service->autoconnect = TRUE;
2103 service->state = CONNMAN_SERVICE_STATE_IDLE;
2105 update_from_network(service, network);
2107 setup_ipconfig(service, connman_network_get_index(network));
2109 service_register(service);
2111 __connman_profile_changed(TRUE);
2113 if (service->favorite == TRUE)
2114 __connman_service_auto_connect();
2119 void __connman_service_update_from_network(struct connman_network *network)
2121 struct connman_service *service;
2122 connman_uint8_t strength;
2124 service = __connman_service_lookup_from_network(network);
2125 if (service == NULL)
2128 if (service->network == NULL)
2131 strength = connman_network_get_uint8(service->network, "Strength");
2132 if (strength == service->strength)
2135 service->strength = strength;
2137 strength_changed(service);
2140 void __connman_service_remove_from_network(struct connman_network *network)
2142 struct connman_service *service;
2144 service = __connman_service_lookup_from_network(network);
2145 if (service == NULL)
2148 __connman_service_put(service);
2151 static int service_load(struct connman_service *service)
2153 const char *ident = service->profile;
2155 GError *error = NULL;
2156 gchar *pathname, *data = NULL;
2159 connman_bool_t autoconnect;
2160 unsigned int ssid_len;
2163 DBG("service %p", service);
2168 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2169 if (pathname == NULL)
2172 keyfile = g_key_file_new();
2174 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
2181 if (g_key_file_load_from_data(keyfile, data, length,
2182 0, NULL) == FALSE) {
2189 switch (service->type) {
2190 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2191 case CONNMAN_SERVICE_TYPE_ETHERNET:
2193 case CONNMAN_SERVICE_TYPE_WIFI:
2194 if (service->name == NULL) {
2197 name = g_key_file_get_string(keyfile,
2198 service->identifier, "Name", NULL);
2200 g_free(service->name);
2201 service->name = name;
2204 if (service->network != NULL)
2205 connman_network_set_name(service->network,
2209 if (service->network &&
2210 connman_network_get_blob(service->network,
2211 "WiFi.SSID", &ssid_len) == NULL) {
2214 hex_ssid = g_key_file_get_string(keyfile,
2215 service->identifier,
2218 if (hex_ssid != NULL) {
2220 unsigned int i, j = 0, hex;
2221 size_t hex_ssid_len = strlen(hex_ssid);
2223 ssid = g_try_malloc0(hex_ssid_len / 2);
2230 for (i = 0; i < hex_ssid_len; i += 2) {
2231 sscanf(hex_ssid + i, "%02x", &hex);
2235 connman_network_set_blob(service->network,
2236 "WiFi.SSID", ssid, hex_ssid_len / 2);
2243 case CONNMAN_SERVICE_TYPE_WIMAX:
2244 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2245 case CONNMAN_SERVICE_TYPE_CELLULAR:
2246 service->apn = g_key_file_get_string(keyfile,
2247 service->identifier, "APN", NULL);
2249 service->favorite = g_key_file_get_boolean(keyfile,
2250 service->identifier, "Favorite", NULL);
2252 autoconnect = g_key_file_get_boolean(keyfile,
2253 service->identifier, "AutoConnect", &error);
2255 service->autoconnect = autoconnect;
2256 g_clear_error(&error);
2258 str = g_key_file_get_string(keyfile,
2259 service->identifier, "Failure", NULL);
2261 service->state = CONNMAN_SERVICE_STATE_FAILURE;
2262 service->error = string2error(str);
2267 str = g_key_file_get_string(keyfile,
2268 service->identifier, "Modified", NULL);
2270 g_time_val_from_iso8601(str, &service->modified);
2274 str = g_key_file_get_string(keyfile,
2275 service->identifier, "Passphrase", NULL);
2277 g_free(service->passphrase);
2278 service->passphrase = str;
2281 if (service->ipconfig != NULL)
2282 __connman_ipconfig_load(service->ipconfig, keyfile,
2283 service->identifier, "IPv4.");
2286 g_key_file_free(keyfile);
2291 static int service_save(struct connman_service *service)
2293 const char *ident = service->profile;
2295 gchar *pathname, *data = NULL;
2300 DBG("service %p", service);
2305 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
2306 if (pathname == NULL)
2309 keyfile = g_key_file_new();
2311 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
2315 if (g_key_file_load_from_data(keyfile, data, length,
2323 if (service->name != NULL)
2324 g_key_file_set_string(keyfile, service->identifier,
2325 "Name", service->name);
2327 switch (service->type) {
2328 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2329 case CONNMAN_SERVICE_TYPE_ETHERNET:
2331 case CONNMAN_SERVICE_TYPE_WIFI:
2332 if (service->network) {
2333 const unsigned char *ssid;
2334 unsigned int ssid_len = 0;
2336 ssid = connman_network_get_blob(service->network,
2337 "WiFi.SSID", &ssid_len);
2339 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
2340 char *identifier = service->identifier;
2344 str = g_string_sized_new(ssid_len * 2);
2350 for (i = 0; i < ssid_len; i++)
2351 g_string_append_printf(str,
2354 g_key_file_set_string(keyfile, identifier,
2357 g_string_free(str, TRUE);
2362 case CONNMAN_SERVICE_TYPE_WIMAX:
2363 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2364 case CONNMAN_SERVICE_TYPE_CELLULAR:
2365 if (service->apn != NULL)
2366 g_key_file_set_string(keyfile, service->identifier,
2367 "APN", service->apn);
2369 g_key_file_set_boolean(keyfile, service->identifier,
2370 "Favorite", service->favorite);
2372 if (service->favorite == TRUE)
2373 g_key_file_set_boolean(keyfile, service->identifier,
2374 "AutoConnect", service->autoconnect);
2376 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
2377 const char *failure = error2string(service->error);
2378 if (failure != NULL)
2379 g_key_file_set_string(keyfile,
2380 service->identifier,
2381 "Failure", failure);
2383 g_key_file_remove_key(keyfile, service->identifier,
2389 str = g_time_val_to_iso8601(&service->modified);
2391 g_key_file_set_string(keyfile, service->identifier,
2396 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
2397 g_key_file_set_string(keyfile, service->identifier,
2398 "Passphrase", service->passphrase);
2400 g_key_file_remove_key(keyfile, service->identifier,
2401 "Passphrase", NULL);
2403 if (service->ipconfig != NULL)
2404 __connman_ipconfig_save(service->ipconfig, keyfile,
2405 service->identifier, "IPv4.");
2407 data = g_key_file_to_data(keyfile, &length, NULL);
2409 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
2410 connman_error("Failed to store service information");
2415 g_key_file_free(keyfile);
2422 static struct connman_storage service_storage = {
2424 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
2425 .service_load = service_load,
2426 .service_save = service_save,
2429 int __connman_service_init(void)
2433 connection = connman_dbus_get_connection();
2435 if (connman_storage_register(&service_storage) < 0)
2436 connman_error("Failed to register service storage");
2438 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2441 service_list = g_sequence_new(service_free);
2446 void __connman_service_cleanup(void)
2450 g_sequence_free(service_list);
2451 service_list = NULL;
2453 g_hash_table_destroy(service_hash);
2454 service_hash = NULL;
2456 connman_storage_unregister(&service_storage);
2458 dbus_connection_unref(connection);