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 static DBusConnection *connection = NULL;
34 static GSequence *service_list = NULL;
35 static GHashTable *service_hash = NULL;
37 struct connman_service {
41 enum connman_service_type type;
42 enum connman_service_mode mode;
43 enum connman_service_security security;
44 enum connman_service_state state;
45 enum connman_service_error error;
46 connman_uint8_t strength;
47 connman_bool_t favorite;
48 connman_bool_t hidden;
49 connman_bool_t ignore;
55 struct connman_ipconfig *ipconfig;
56 struct connman_device *device;
57 struct connman_network *network;
62 static void append_path(gpointer value, gpointer user_data)
64 struct connman_service *service = value;
65 DBusMessageIter *iter = user_data;
67 if (service->path == NULL)
70 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
74 void __connman_service_list(DBusMessageIter *iter)
78 g_sequence_foreach(service_list, append_path, iter);
83 struct connman_service *service;
86 static void compare_path(gpointer value, gpointer user_data)
88 struct connman_service *service = value;
89 struct find_data *data = user_data;
91 if (data->service != NULL)
94 if (g_strcmp0(service->path, data->path) == 0)
95 data->service = service;
98 static struct connman_service *find_service(const char *path)
100 struct find_data data = { .path = path, .service = NULL };
102 DBG("path %s", path);
104 g_sequence_foreach(service_list, compare_path, &data);
109 static const char *type2string(enum connman_service_type type)
112 case CONNMAN_SERVICE_TYPE_UNKNOWN:
114 case CONNMAN_SERVICE_TYPE_ETHERNET:
116 case CONNMAN_SERVICE_TYPE_WIFI:
118 case CONNMAN_SERVICE_TYPE_WIMAX:
120 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
122 case CONNMAN_SERVICE_TYPE_CELLULAR:
129 static const char *mode2string(enum connman_service_mode mode)
132 case CONNMAN_SERVICE_MODE_UNKNOWN:
134 case CONNMAN_SERVICE_MODE_MANAGED:
136 case CONNMAN_SERVICE_MODE_ADHOC:
143 static const char *security2string(enum connman_service_security security)
146 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
148 case CONNMAN_SERVICE_SECURITY_NONE:
150 case CONNMAN_SERVICE_SECURITY_WEP:
152 case CONNMAN_SERVICE_SECURITY_WPA:
154 case CONNMAN_SERVICE_SECURITY_RSN:
161 static const char *state2string(enum connman_service_state state)
164 case CONNMAN_SERVICE_STATE_UNKNOWN:
166 case CONNMAN_SERVICE_STATE_IDLE:
168 case CONNMAN_SERVICE_STATE_CARRIER:
170 case CONNMAN_SERVICE_STATE_ASSOCIATION:
171 return "association";
172 case CONNMAN_SERVICE_STATE_CONFIGURATION:
173 return "configuration";
174 case CONNMAN_SERVICE_STATE_READY:
176 case CONNMAN_SERVICE_STATE_DISCONNECT:
178 case CONNMAN_SERVICE_STATE_FAILURE:
185 static const char *error2string(enum connman_service_error error)
188 case CONNMAN_SERVICE_ERROR_UNKNOWN:
190 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
191 return "out-of-range";
192 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
193 return "pin-missing";
194 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
195 return "dhcp-failed";
196 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
197 return "connect-failed";
203 static enum connman_service_error string2error(const char *error)
205 if (g_strcmp0(error, "dhcp-failed") == 0)
206 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
207 else if (g_strcmp0(error, "pin-missing") == 0)
208 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
210 return CONNMAN_SERVICE_ERROR_UNKNOWN;
213 static void state_changed(struct connman_service *service)
216 DBusMessageIter entry, value;
217 const char *str, *key = "State";
219 if (service->path == NULL)
222 str = state2string(service->state);
226 signal = dbus_message_new_signal(service->path,
227 CONNMAN_SERVICE_INTERFACE, "PropertyChanged");
231 dbus_message_iter_init_append(signal, &entry);
233 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
235 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
236 DBUS_TYPE_STRING_AS_STRING, &value);
237 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
238 dbus_message_iter_close_container(&entry, &value);
240 g_dbus_send_message(connection, signal);
243 static void strength_changed(struct connman_service *service)
246 DBusMessageIter entry, value;
247 const char *key = "Strength";
249 if (service->path == NULL)
252 if (service->strength == 0)
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_BYTE_AS_STRING, &value);
266 dbus_message_iter_append_basic(&value, DBUS_TYPE_BYTE,
268 dbus_message_iter_close_container(&entry, &value);
270 g_dbus_send_message(connection, signal);
273 static DBusMessage *get_properties(DBusConnection *conn,
274 DBusMessage *msg, void *user_data)
276 struct connman_service *service = user_data;
278 DBusMessageIter array, dict;
281 DBG("service %p", service);
283 reply = dbus_message_new_method_return(msg);
287 dbus_message_iter_init_append(reply, &array);
289 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
290 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
291 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
292 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
294 str = type2string(service->type);
296 connman_dbus_dict_append_variant(&dict, "Type",
297 DBUS_TYPE_STRING, &str);
299 str = mode2string(service->mode);
301 connman_dbus_dict_append_variant(&dict, "Mode",
302 DBUS_TYPE_STRING, &str);
304 str = security2string(service->security);
306 connman_dbus_dict_append_variant(&dict, "Security",
307 DBUS_TYPE_STRING, &str);
309 str = state2string(service->state);
311 connman_dbus_dict_append_variant(&dict, "State",
312 DBUS_TYPE_STRING, &str);
314 str = error2string(service->error);
316 connman_dbus_dict_append_variant(&dict, "Error",
317 DBUS_TYPE_STRING, &str);
319 if (service->strength > 0)
320 connman_dbus_dict_append_variant(&dict, "Strength",
321 DBUS_TYPE_BYTE, &service->strength);
323 connman_dbus_dict_append_variant(&dict, "Favorite",
324 DBUS_TYPE_BOOLEAN, &service->favorite);
326 if (service->name != NULL)
327 connman_dbus_dict_append_variant(&dict, "Name",
328 DBUS_TYPE_STRING, &service->name);
330 if (service->passphrase != NULL &&
331 __connman_security_check_privilege(msg,
332 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
333 connman_dbus_dict_append_variant(&dict, "Passphrase",
334 DBUS_TYPE_STRING, &service->passphrase);
336 __connman_ipconfig_append_ipv4(service->ipconfig, &dict, "IPv4.");
338 dbus_message_iter_close_container(&array, &dict);
343 static DBusMessage *set_property(DBusConnection *conn,
344 DBusMessage *msg, void *user_data)
346 struct connman_service *service = user_data;
347 DBusMessageIter iter, value;
351 DBG("service %p", service);
353 if (dbus_message_iter_init(msg, &iter) == FALSE)
354 return __connman_error_invalid_arguments(msg);
356 dbus_message_iter_get_basic(&iter, &name);
357 dbus_message_iter_next(&iter);
358 dbus_message_iter_recurse(&iter, &value);
360 if (__connman_security_check_privilege(msg,
361 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
362 return __connman_error_permission_denied(msg);
364 type = dbus_message_iter_get_arg_type(&value);
366 if (g_str_equal(name, "Passphrase") == TRUE) {
367 const char *passphrase;
369 if (type != DBUS_TYPE_STRING)
370 return __connman_error_invalid_arguments(msg);
372 if (__connman_security_check_privilege(msg,
373 CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
374 return __connman_error_permission_denied(msg);
376 dbus_message_iter_get_basic(&value, &passphrase);
378 g_free(service->passphrase);
379 service->passphrase = g_strdup(passphrase);
381 if (service->network != NULL)
382 connman_network_set_string(service->network,
383 "WiFi.Passphrase", service->passphrase);
385 __connman_storage_save_service(service);
386 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
389 err = __connman_ipconfig_set_ipv4(service->ipconfig,
392 return __connman_error_failed(msg, -err);
394 return __connman_error_invalid_property(msg);
396 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
399 static void set_idle(struct connman_service *service)
401 service->state = CONNMAN_SERVICE_STATE_IDLE;
402 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
403 state_changed(service);
406 static DBusMessage *clear_property(DBusConnection *conn,
407 DBusMessage *msg, void *user_data)
409 struct connman_service *service = user_data;
412 DBG("service %p", service);
414 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
417 if (__connman_security_check_privilege(msg,
418 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
419 return __connman_error_permission_denied(msg);
421 if (g_str_equal(name, "Error") == TRUE) {
424 g_get_current_time(&service->modified);
425 __connman_storage_save_service(service);
427 return __connman_error_invalid_property(msg);
429 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
432 static connman_bool_t is_connecting(struct connman_service *service)
434 switch (service->state) {
435 case CONNMAN_SERVICE_STATE_UNKNOWN:
436 case CONNMAN_SERVICE_STATE_IDLE:
437 case CONNMAN_SERVICE_STATE_CARRIER:
438 case CONNMAN_SERVICE_STATE_FAILURE:
439 case CONNMAN_SERVICE_STATE_DISCONNECT:
440 case CONNMAN_SERVICE_STATE_READY:
442 case CONNMAN_SERVICE_STATE_ASSOCIATION:
443 case CONNMAN_SERVICE_STATE_CONFIGURATION:
450 static connman_bool_t is_ignore(struct connman_service *service)
452 if (service->ignore == TRUE)
455 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
461 void __connman_service_auto_connect(void)
463 struct connman_service *service = NULL;
468 iter = g_sequence_get_begin_iter(service_list);
470 while (g_sequence_iter_is_end(iter) == FALSE) {
471 service = g_sequence_get(iter);
473 if (service->pending != NULL)
476 if (is_connecting(service) == TRUE)
479 if (service->favorite == FALSE)
482 if (service->state == CONNMAN_SERVICE_STATE_READY)
485 if (is_ignore(service) == FALSE &&
486 service->state == CONNMAN_SERVICE_STATE_IDLE)
491 iter = g_sequence_iter_next(iter);
495 __connman_service_connect(service);
498 static gboolean connect_timeout(gpointer user_data)
500 struct connman_service *service = user_data;
501 connman_bool_t auto_connect = FALSE;
503 DBG("service %p", service);
505 service->timeout = 0;
507 if (service->network != NULL) {
508 connman_bool_t connected;
510 connected = connman_network_get_connected(service->network);
511 if (connected == TRUE) {
512 __connman_service_indicate_state(service,
513 CONNMAN_SERVICE_STATE_READY);
517 __connman_network_disconnect(service->network);
518 } else if (service->device != NULL) {
519 connman_bool_t disconnected;
521 disconnected = connman_device_get_disconnected(service->device);
522 if (disconnected == FALSE) {
523 __connman_service_indicate_state(service,
524 CONNMAN_SERVICE_STATE_READY);
528 __connman_device_disconnect(service->device);
531 if (service->pending != NULL) {
534 reply = __connman_error_operation_timeout(service->pending);
536 g_dbus_send_message(connection, reply);
538 dbus_message_unref(service->pending);
539 service->pending = NULL;
543 __connman_service_indicate_state(service,
544 CONNMAN_SERVICE_STATE_FAILURE);
546 if (auto_connect == TRUE)
547 __connman_service_auto_connect();
552 static DBusMessage *connect_service(DBusConnection *conn,
553 DBusMessage *msg, void *user_data)
555 struct connman_service *service = user_data;
558 DBG("service %p", service);
560 if (service->pending != NULL)
561 return __connman_error_in_progress(msg);
563 service->ignore = FALSE;
565 service->pending = dbus_message_ref(msg);
567 err = __connman_service_connect(service);
569 if (err != -EINPROGRESS) {
570 dbus_message_unref(service->pending);
571 service->pending = NULL;
573 return __connman_error_failed(msg, -err);
579 dbus_message_unref(service->pending);
580 service->pending = NULL;
582 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
585 static DBusMessage *disconnect_service(DBusConnection *conn,
586 DBusMessage *msg, void *user_data)
588 struct connman_service *service = user_data;
591 DBG("service %p", service);
593 if (service->pending != NULL) {
596 reply = __connman_error_operation_aborted(service->pending);
598 g_dbus_send_message(conn, reply);
600 dbus_message_unref(service->pending);
601 service->pending = NULL;
603 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
606 service->ignore = TRUE;
608 err = __connman_service_disconnect(service);
610 if (err != -EINPROGRESS)
611 return __connman_error_failed(msg, -err);
616 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
619 static DBusMessage *remove_service(DBusConnection *conn,
620 DBusMessage *msg, void *user_data)
622 struct connman_service *service = user_data;
624 DBG("service %p", service);
626 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
627 return __connman_error_not_supported(msg);
629 if (service->favorite == FALSE)
630 return __connman_error_not_supported(msg);
632 if (service->network != NULL)
633 __connman_network_disconnect(service->network);
635 g_free(service->passphrase);
636 service->passphrase = NULL;
638 connman_service_set_favorite(service, FALSE);
639 __connman_storage_save_service(service);
641 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
644 static DBusMessage *move_before(DBusConnection *conn,
645 DBusMessage *msg, void *user_data)
647 struct connman_service *service = user_data;
648 struct connman_service *target;
650 GSequenceIter *src, *dst;
652 DBG("service %p", service);
654 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
657 if (service->favorite == FALSE)
658 return __connman_error_not_supported(msg);
660 target = find_service(path);
661 if (target == NULL || target->favorite == FALSE || target == service)
662 return __connman_error_invalid_service(msg);
664 DBG("target %s", target->identifier);
666 g_get_current_time(&service->modified);
667 __connman_storage_save_service(service);
669 src = g_hash_table_lookup(service_hash, service->identifier);
670 dst = g_hash_table_lookup(service_hash, target->identifier);
672 g_sequence_move(src, dst);
674 __connman_profile_changed(FALSE);
676 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
679 static DBusMessage *move_after(DBusConnection *conn,
680 DBusMessage *msg, void *user_data)
682 struct connman_service *service = user_data;
683 struct connman_service *target;
686 DBG("service %p", service);
688 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
691 if (service->favorite == FALSE)
692 return __connman_error_not_supported(msg);
694 target = find_service(path);
695 if (target == NULL || target->favorite == FALSE || target == service)
696 return __connman_error_invalid_service(msg);
698 DBG("target %s", target->identifier);
700 g_get_current_time(&service->modified);
701 __connman_storage_save_service(service);
703 return __connman_error_not_implemented(msg);
706 static GDBusMethodTable service_methods[] = {
707 { "GetProperties", "", "a{sv}", get_properties },
708 { "SetProperty", "sv", "", set_property },
709 { "ClearProperty", "s", "", clear_property },
710 { "Connect", "", "", connect_service,
711 G_DBUS_METHOD_FLAG_ASYNC },
712 { "Disconnect", "", "", disconnect_service },
713 { "Remove", "", "", remove_service },
714 { "MoveBefore", "o", "", move_before },
715 { "MoveAfter", "o", "", move_after },
719 static GDBusSignalTable service_signals[] = {
720 { "PropertyChanged", "sv" },
724 static void service_free(gpointer user_data)
726 struct connman_service *service = user_data;
727 char *path = service->path;
729 DBG("service %p", service);
731 g_hash_table_remove(service_hash, service->identifier);
733 if (service->timeout > 0) {
734 g_source_remove(service->timeout);
735 service->timeout = 0;
738 if (service->pending != NULL) {
739 dbus_message_unref(service->pending);
740 service->pending = NULL;
743 service->path = NULL;
746 __connman_profile_changed(TRUE);
748 g_dbus_unregister_interface(connection, path,
749 CONNMAN_SERVICE_INTERFACE);
753 if (service->network != NULL)
754 connman_network_unref(service->network);
756 connman_ipconfig_unref(service->ipconfig);
758 g_free(service->profile);
759 g_free(service->name);
760 g_free(service->passphrase);
761 g_free(service->identifier);
765 static void reply_pending(struct connman_service *service, int error)
767 if (service->timeout > 0) {
768 g_source_remove(service->timeout);
769 service->timeout = 0;
772 if (service->pending != NULL) {
776 reply = __connman_error_failed(service->pending,
779 g_dbus_send_message(connection, reply);
781 g_dbus_send_reply(connection, service->pending,
784 dbus_message_unref(service->pending);
785 service->pending = NULL;
789 static gboolean remove_timeout(gpointer user_data)
791 GSequenceIter *iter = user_data;
793 g_sequence_remove(iter);
799 * __connman_service_put:
800 * @service: service structure
802 * Release service if no longer needed
804 void __connman_service_put(struct connman_service *service)
806 DBG("service %p", service);
808 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
811 iter = g_hash_table_lookup(service_hash, service->identifier);
815 reply_pending(service, EIO);
817 __connman_service_disconnect(service);
819 if (service->network != NULL) {
820 connman_network_unref(service->network);
821 service->network = NULL;
824 service->state = CONNMAN_SERVICE_STATE_FAILURE;
825 service->error = CONNMAN_SERVICE_ERROR_OUT_OF_RANGE;
826 state_changed(service);
828 service->timeout = g_timeout_add_seconds(interval,
829 remove_timeout, iter);
831 service_free(service);
835 static void __connman_service_initialize(struct connman_service *service)
837 DBG("service %p", service);
839 service->refcount = 1;
841 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
842 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
843 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
844 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
846 service->favorite = FALSE;
847 service->hidden = FALSE;
849 service->ignore = FALSE;
855 * connman_service_create:
857 * Allocate a new service.
859 * Returns: a newly-allocated #connman_service structure
861 struct connman_service *connman_service_create(void)
863 struct connman_service *service;
865 service = g_try_new0(struct connman_service, 1);
869 DBG("service %p", service);
871 __connman_service_initialize(service);
873 service->ipconfig = connman_ipconfig_create();
874 if (service->ipconfig == NULL) {
879 connman_ipconfig_set_method(service->ipconfig,
880 CONNMAN_IPCONFIG_METHOD_DHCP);
886 * connman_service_ref:
887 * @service: service structure
889 * Increase reference counter of service
891 struct connman_service *connman_service_ref(struct connman_service *service)
893 g_atomic_int_inc(&service->refcount);
899 * connman_service_unref:
900 * @service: service structure
902 * Decrease reference counter of service
904 void connman_service_unref(struct connman_service *service)
906 __connman_service_put(service);
909 static gint service_compare(gconstpointer a, gconstpointer b,
912 struct connman_service *service_a = (void *) a;
913 struct connman_service *service_b = (void *) b;
915 if (service_a->state != service_b->state) {
916 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
918 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
922 if (service_a->order > service_b->order)
925 if (service_a->order < service_b->order)
928 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
931 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
934 return (gint) service_b->strength - (gint) service_a->strength;
938 * connman_service_set_favorite:
939 * @service: service structure
940 * @favorite: favorite value
942 * Change the favorite setting of service
944 int connman_service_set_favorite(struct connman_service *service,
945 connman_bool_t favorite)
949 iter = g_hash_table_lookup(service_hash, service->identifier);
953 if (service->favorite == favorite)
956 service->favorite = favorite;
958 g_sequence_sort_changed(iter, service_compare, NULL);
960 __connman_profile_changed(FALSE);
965 int __connman_service_set_carrier(struct connman_service *service,
966 connman_bool_t carrier)
968 DBG("service %p carrier %d", service, carrier);
973 switch (service->type) {
974 case CONNMAN_SERVICE_TYPE_UNKNOWN:
975 case CONNMAN_SERVICE_TYPE_WIFI:
976 case CONNMAN_SERVICE_TYPE_WIMAX:
977 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
978 case CONNMAN_SERVICE_TYPE_CELLULAR:
980 case CONNMAN_SERVICE_TYPE_ETHERNET:
984 if (carrier == FALSE) {
985 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
986 state_changed(service);
988 service->state = CONNMAN_SERVICE_STATE_IDLE;
989 state_changed(service);
991 service->state = CONNMAN_SERVICE_STATE_CARRIER;
992 state_changed(service);
995 return connman_service_set_favorite(service, carrier);
998 int __connman_service_indicate_state(struct connman_service *service,
999 enum connman_service_state state)
1001 GSequenceIter *iter;
1003 DBG("service %p state %d", service, state);
1005 if (service == NULL)
1008 if (state == CONNMAN_SERVICE_STATE_CARRIER)
1009 return __connman_service_set_carrier(service, TRUE);
1011 if (service->state == state)
1014 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
1015 state == CONNMAN_SERVICE_STATE_IDLE)
1018 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
1019 state == CONNMAN_SERVICE_STATE_DISCONNECT)
1022 if (state == CONNMAN_SERVICE_STATE_IDLE &&
1023 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
1024 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
1025 state_changed(service);
1027 __connman_service_disconnect(service);
1030 service->state = state;
1031 state_changed(service);
1033 if (state == CONNMAN_SERVICE_STATE_READY) {
1034 connman_service_set_favorite(service, TRUE);
1036 reply_pending(service, 0);
1038 g_get_current_time(&service->modified);
1039 __connman_storage_save_service(service);
1042 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
1043 reply_pending(service, EIO);
1045 g_get_current_time(&service->modified);
1046 __connman_storage_save_service(service);
1048 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1050 iter = g_hash_table_lookup(service_hash, service->identifier);
1052 g_sequence_sort_changed(iter, service_compare, NULL);
1054 __connman_profile_changed(FALSE);
1056 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
1057 service->state == CONNMAN_SERVICE_STATE_FAILURE)
1058 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
1063 int __connman_service_indicate_error(struct connman_service *service,
1064 enum connman_service_error error)
1066 DBG("service %p error %d", service, error);
1068 if (service == NULL)
1071 service->error = error;
1073 return __connman_service_indicate_state(service,
1074 CONNMAN_SERVICE_STATE_FAILURE);
1077 int __connman_service_indicate_default(struct connman_service *service)
1079 DBG("service %p", service);
1084 int __connman_service_connect(struct connman_service *service)
1088 DBG("service %p", service);
1090 if (service->state == CONNMAN_SERVICE_STATE_READY)
1093 if (is_connecting(service) == TRUE)
1096 if (service->network != NULL) {
1097 unsigned int ssid_len;
1099 if (connman_network_get_blob(service->network, "WiFi.SSID",
1103 connman_network_set_string(service->network,
1104 "WiFi.Passphrase", service->passphrase);
1106 err = __connman_network_connect(service->network);
1107 } else if (service->device != NULL) {
1108 if (service->favorite == FALSE)
1111 err = __connman_device_connect(service->device);
1116 if (err != -EINPROGRESS)
1119 service->timeout = g_timeout_add_seconds(45,
1120 connect_timeout, service);
1122 return -EINPROGRESS;
1128 int __connman_service_disconnect(struct connman_service *service)
1132 DBG("service %p", service);
1134 if (service->network != NULL) {
1135 err = __connman_network_disconnect(service->network);
1136 } else if (service->device != NULL) {
1137 if (service->favorite == FALSE)
1139 err = __connman_device_disconnect(service->device);
1144 if (err != -EINPROGRESS)
1147 return -EINPROGRESS;
1154 * __connman_service_lookup:
1155 * @identifier: service identifier
1157 * Look up a service by identifier (reference count will not be increased)
1159 static struct connman_service *__connman_service_lookup(const char *identifier)
1161 GSequenceIter *iter;
1163 iter = g_hash_table_lookup(service_hash, identifier);
1165 return g_sequence_get(iter);
1170 static struct connman_network *create_hidden_wifi(struct connman_device *device,
1171 const char *ssid, const char *mode, const char *security)
1173 struct connman_network *network;
1176 unsigned int i, ssid_len;
1178 ssid_len = strlen(ssid);
1182 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
1183 if (network == NULL)
1186 connman_network_set_blob(network, "WiFi.SSID",
1187 (unsigned char *) ssid, ssid_len);
1189 connman_network_set_string(network, "WiFi.Mode", mode);
1190 connman_network_set_string(network, "WiFi.Security", security);
1192 name = g_try_malloc0(ssid_len + 1);
1194 connman_network_unref(network);
1198 for (i = 0; i < ssid_len; i++) {
1199 if (g_ascii_isprint(ssid[i]))
1205 connman_network_set_name(network, name);
1209 index = connman_device_get_index(device);
1210 connman_network_set_index(network, index);
1212 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
1214 if (connman_device_add_network(device, network) < 0) {
1215 connman_network_unref(network);
1219 connman_network_set_available(network, TRUE);
1224 int __connman_service_create_and_connect(DBusMessage *msg)
1226 struct connman_service *service;
1227 struct connman_network *network;
1228 struct connman_device *device;
1229 DBusMessageIter iter, array;
1230 const char *mode = "managed", *security = "none";
1231 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
1232 unsigned int ssid_len = 0;
1235 gboolean created = FALSE;
1238 dbus_message_iter_init(msg, &iter);
1239 dbus_message_iter_recurse(&iter, &array);
1241 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1242 DBusMessageIter entry, value;
1245 dbus_message_iter_recurse(&array, &entry);
1246 dbus_message_iter_get_basic(&entry, &key);
1248 dbus_message_iter_next(&entry);
1249 dbus_message_iter_recurse(&entry, &value);
1251 switch (dbus_message_iter_get_arg_type(&value)) {
1252 case DBUS_TYPE_STRING:
1253 if (g_str_equal(key, "Type") == TRUE)
1254 dbus_message_iter_get_basic(&value, &type);
1255 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
1256 g_str_equal(key, "Mode") == TRUE)
1257 dbus_message_iter_get_basic(&value, &mode);
1258 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
1259 g_str_equal(key, "Security") == TRUE)
1260 dbus_message_iter_get_basic(&value, &security);
1261 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
1262 g_str_equal(key, "Passphrase") == TRUE)
1263 dbus_message_iter_get_basic(&value, &passphrase);
1264 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
1265 g_str_equal(key, "SSID") == TRUE)
1266 dbus_message_iter_get_basic(&value, &ssid);
1269 dbus_message_iter_next(&array);
1275 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
1281 ssid_len = strlen(ssid);
1285 device = __connman_element_find_device(CONNMAN_DEVICE_TYPE_WIFI);
1289 ident = __connman_device_get_ident(device);
1293 group = connman_wifi_build_group_name((unsigned char *) ssid,
1294 ssid_len, mode, security);
1298 name = g_strdup_printf("%s_%s_%s", type, ident, group);
1300 service = __connman_service_lookup(name);
1302 if (service != NULL)
1305 network = create_hidden_wifi(device, ssid, mode, security);
1306 if (network != NULL) {
1307 connman_network_set_group(network, group);
1311 service = __connman_service_lookup(name);
1317 if (service == NULL) {
1322 if (passphrase != NULL) {
1323 g_free(service->passphrase);
1324 service->passphrase = g_strdup(passphrase);
1327 err = __connman_service_connect(service);
1328 if (err < 0 && err != -EINPROGRESS)
1331 g_dbus_send_reply(connection, msg,
1332 DBUS_TYPE_OBJECT_PATH, &service->path,
1338 if (service != NULL && created == TRUE) {
1339 struct connman_network *network = service->network;
1341 if (network != NULL) {
1342 connman_network_set_available(network, FALSE);
1343 __connman_device_cleanup_networks(device);
1346 __connman_service_put(service);
1353 * __connman_service_get:
1354 * @identifier: service identifier
1356 * Look up a service by identifier or create a new one if not found
1358 static struct connman_service *__connman_service_get(const char *identifier)
1360 struct connman_service *service;
1361 GSequenceIter *iter;
1363 iter = g_hash_table_lookup(service_hash, identifier);
1365 service = g_sequence_get(iter);
1366 if (service != NULL)
1367 g_atomic_int_inc(&service->refcount);
1371 service = connman_service_create();
1372 if (service == NULL)
1375 DBG("service %p", service);
1377 service->identifier = g_strdup(identifier);
1379 service->profile = g_strdup(__connman_profile_active_ident());
1381 __connman_storage_load_service(service);
1383 iter = g_sequence_insert_sorted(service_list, service,
1384 service_compare, NULL);
1386 g_hash_table_insert(service_hash, service->identifier, iter);
1391 static int service_register(struct connman_service *service)
1393 const char *path = __connman_profile_active_path();
1394 GSequenceIter *iter;
1396 DBG("service %p", service);
1398 if (service->path != NULL)
1401 service->path = g_strdup_printf("%s/%s", path, service->identifier);
1403 DBG("path %s", service->path);
1405 g_dbus_register_interface(connection, service->path,
1406 CONNMAN_SERVICE_INTERFACE,
1407 service_methods, service_signals,
1408 NULL, service, NULL);
1410 __connman_storage_load_service(service);
1412 iter = g_hash_table_lookup(service_hash, service->identifier);
1414 g_sequence_sort_changed(iter, service_compare, NULL);
1416 __connman_profile_changed(TRUE);
1422 * __connman_service_lookup_from_device:
1423 * @device: device structure
1425 * Look up a service by device (reference count will not be increased)
1427 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
1429 struct connman_service *service;
1433 ident = __connman_device_get_ident(device);
1437 name = g_strdup_printf("%s_%s",
1438 __connman_device_get_type(device), ident);
1440 service = __connman_service_lookup(name);
1447 static enum connman_service_type convert_device_type(struct connman_device *device)
1449 enum connman_device_type type = connman_device_get_type(device);
1452 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1453 case CONNMAN_DEVICE_TYPE_VENDOR:
1454 case CONNMAN_DEVICE_TYPE_WIFI:
1455 case CONNMAN_DEVICE_TYPE_WIMAX:
1456 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1457 case CONNMAN_DEVICE_TYPE_GPS:
1458 case CONNMAN_DEVICE_TYPE_MBM:
1459 case CONNMAN_DEVICE_TYPE_HSO:
1460 case CONNMAN_DEVICE_TYPE_NOZOMI:
1461 case CONNMAN_DEVICE_TYPE_HUAWEI:
1462 case CONNMAN_DEVICE_TYPE_NOVATEL:
1464 case CONNMAN_DEVICE_TYPE_ETHERNET:
1465 return CONNMAN_SERVICE_TYPE_ETHERNET;
1468 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1472 * __connman_service_create_from_device:
1473 * @device: device structure
1475 * Look up service by device and if not found, create one
1477 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
1479 struct connman_service *service;
1483 ident = __connman_device_get_ident(device);
1487 name = g_strdup_printf("%s_%s",
1488 __connman_device_get_type(device), ident);
1490 service = __connman_service_get(name);
1491 if (service == NULL)
1494 if (service->path != NULL) {
1495 __connman_service_put(service);
1500 service->type = convert_device_type(device);
1502 service->device = device;
1504 service_register(service);
1506 if (service->favorite == TRUE)
1507 __connman_service_auto_connect();
1515 void __connman_service_remove_from_device(struct connman_device *device)
1517 struct connman_service *service;
1519 service = __connman_service_lookup_from_device(device);
1520 if (service == NULL)
1523 __connman_service_put(service);
1527 * __connman_service_lookup_from_network:
1528 * @network: network structure
1530 * Look up a service by network (reference count will not be increased)
1532 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
1534 struct connman_service *service;
1535 const char *ident, *group;
1538 ident = __connman_network_get_ident(network);
1542 group = connman_network_get_group(network);
1546 name = g_strdup_printf("%s_%s_%s",
1547 __connman_network_get_type(network), ident, group);
1549 service = __connman_service_lookup(name);
1556 unsigned int __connman_service_get_order(struct connman_service *service)
1558 GSequenceIter *iter;
1560 if (service == NULL)
1563 if (service->favorite == FALSE) {
1568 iter = g_hash_table_lookup(service_hash, service->identifier);
1570 if (g_sequence_iter_get_position(iter) == 0)
1577 return service->order;
1580 static enum connman_service_type convert_network_type(struct connman_network *network)
1582 enum connman_network_type type = connman_network_get_type(network);
1585 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1586 case CONNMAN_NETWORK_TYPE_VENDOR:
1587 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1588 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1590 case CONNMAN_NETWORK_TYPE_MBM:
1591 case CONNMAN_NETWORK_TYPE_HSO:
1592 return CONNMAN_SERVICE_TYPE_CELLULAR;
1593 case CONNMAN_NETWORK_TYPE_WIFI:
1594 return CONNMAN_SERVICE_TYPE_WIFI;
1595 case CONNMAN_NETWORK_TYPE_WIMAX:
1596 return CONNMAN_SERVICE_TYPE_WIMAX;
1599 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1602 static enum connman_service_mode convert_wifi_mode(const char *mode)
1605 return CONNMAN_SERVICE_MODE_UNKNOWN;
1606 else if (g_str_equal(mode, "managed") == TRUE)
1607 return CONNMAN_SERVICE_MODE_MANAGED;
1608 else if (g_str_equal(mode, "adhoc") == TRUE)
1609 return CONNMAN_SERVICE_MODE_ADHOC;
1611 return CONNMAN_SERVICE_MODE_UNKNOWN;
1614 static enum connman_service_mode convert_wifi_security(const char *security)
1616 if (security == NULL)
1617 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1618 else if (g_str_equal(security, "none") == TRUE)
1619 return CONNMAN_SERVICE_SECURITY_NONE;
1620 else if (g_str_equal(security, "wep") == TRUE)
1621 return CONNMAN_SERVICE_SECURITY_WEP;
1622 else if (g_str_equal(security, "wpa") == TRUE)
1623 return CONNMAN_SERVICE_SECURITY_WPA;
1624 else if (g_str_equal(security, "rsn") == TRUE)
1625 return CONNMAN_SERVICE_SECURITY_RSN;
1627 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1630 static void update_from_network(struct connman_service *service,
1631 struct connman_network *network)
1633 connman_uint8_t strength = service->strength;
1634 GSequenceIter *iter;
1637 if (service->state == CONNMAN_SERVICE_STATE_READY)
1640 if (is_connecting(service) == TRUE)
1643 str = connman_network_get_string(network, "Name");
1645 g_free(service->name);
1646 service->name = g_strdup(str);
1647 service->hidden = FALSE;
1649 g_free(service->name);
1650 service->name = NULL;
1651 service->hidden = TRUE;
1654 service->strength = connman_network_get_uint8(network, "Strength");
1656 str = connman_network_get_string(network, "WiFi.Mode");
1657 service->mode = convert_wifi_mode(str);
1659 str = connman_network_get_string(network, "WiFi.Security");
1660 service->security = convert_wifi_security(str);
1662 if (service->strength > strength && service->network != NULL) {
1663 connman_network_unref(service->network);
1664 service->network = connman_network_ref(network);
1666 strength_changed(service);
1669 if (service->network == NULL)
1670 service->network = connman_network_ref(network);
1672 iter = g_hash_table_lookup(service_hash, service->identifier);
1674 g_sequence_sort_changed(iter, service_compare, NULL);
1678 * __connman_service_create_from_network:
1679 * @network: network structure
1681 * Look up service by network and if not found, create one
1683 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
1685 struct connman_service *service;
1686 const char *ident, *group;
1689 service = __connman_service_lookup_from_network(network);
1690 if (service != NULL) {
1691 if (g_atomic_int_get(&service->refcount) == 0) {
1692 if (service->timeout > 0) {
1693 g_source_remove(service->timeout);
1694 service->timeout = 0;
1700 connman_service_ref(service);
1702 update_from_network(service, network);
1706 ident = __connman_network_get_ident(network);
1710 group = connman_network_get_group(network);
1714 if (__connman_network_get_weakness(network) == TRUE)
1717 name = g_strdup_printf("%s_%s_%s",
1718 __connman_network_get_type(network), ident, group);
1720 service = __connman_service_get(name);
1721 if (service == NULL)
1724 if (service->path != NULL) {
1725 update_from_network(service, network);
1727 __connman_profile_changed(TRUE);
1729 __connman_service_put(service);
1734 service->type = convert_network_type(network);
1736 service->state = CONNMAN_SERVICE_STATE_IDLE;
1738 update_from_network(service, network);
1740 service_register(service);
1742 if (service->favorite == TRUE)
1743 __connman_service_auto_connect();
1751 void __connman_service_remove_from_network(struct connman_network *network)
1753 struct connman_service *service;
1755 service = __connman_service_lookup_from_network(network);
1756 if (service == NULL)
1759 __connman_service_put(service);
1762 static int service_load(struct connman_service *service)
1765 gchar *pathname, *data = NULL;
1768 unsigned int ssid_len;
1771 DBG("service %p", service);
1773 if (service->profile == NULL)
1776 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1777 if (pathname == NULL)
1780 keyfile = g_key_file_new();
1782 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1789 if (g_key_file_load_from_data(keyfile, data, length,
1790 0, NULL) == FALSE) {
1797 switch (service->type) {
1798 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1799 case CONNMAN_SERVICE_TYPE_ETHERNET:
1801 case CONNMAN_SERVICE_TYPE_WIFI:
1802 if (service->name == NULL) {
1805 name = g_key_file_get_string(keyfile,
1806 service->identifier, "Name", NULL);
1808 g_free(service->name);
1809 service->name = name;
1812 if (service->network != NULL)
1813 connman_network_set_name(service->network,
1817 if (service->network &&
1818 connman_network_get_blob(service->network,
1819 "WiFi.SSID", &ssid_len) == NULL) {
1822 hex_ssid = g_key_file_get_string(keyfile,
1823 service->identifier,
1826 if (hex_ssid != NULL) {
1828 unsigned int i, j = 0, hex;
1829 size_t hex_ssid_len = strlen(hex_ssid);
1831 ssid = g_try_malloc0(hex_ssid_len / 2);
1838 for (i = 0; i < hex_ssid_len; i += 2) {
1839 sscanf(hex_ssid + i, "%02x", &hex);
1843 connman_network_set_blob(service->network,
1844 "WiFi.SSID", ssid, hex_ssid_len / 2);
1851 case CONNMAN_SERVICE_TYPE_WIMAX:
1852 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1853 case CONNMAN_SERVICE_TYPE_CELLULAR:
1854 service->favorite = g_key_file_get_boolean(keyfile,
1855 service->identifier, "Favorite", NULL);
1857 str = g_key_file_get_string(keyfile,
1858 service->identifier, "Failure", NULL);
1860 service->state = CONNMAN_SERVICE_STATE_FAILURE;
1861 service->error = string2error(str);
1866 str = g_key_file_get_string(keyfile,
1867 service->identifier, "Modified", NULL);
1869 g_time_val_from_iso8601(str, &service->modified);
1873 str = g_key_file_get_string(keyfile,
1874 service->identifier, "Passphrase", NULL);
1876 g_free(service->passphrase);
1877 service->passphrase = str;
1880 __connman_ipconfig_load(service->ipconfig, keyfile,
1881 service->identifier, "IPv4.");
1884 g_key_file_free(keyfile);
1889 static int service_save(struct connman_service *service)
1892 gchar *pathname, *data = NULL;
1897 DBG("service %p", service);
1899 if (service->profile == NULL)
1902 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1903 if (pathname == NULL)
1906 keyfile = g_key_file_new();
1908 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1912 if (g_key_file_load_from_data(keyfile, data, length,
1920 if (service->name != NULL)
1921 g_key_file_set_string(keyfile, service->identifier,
1922 "Name", service->name);
1924 switch (service->type) {
1925 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1926 case CONNMAN_SERVICE_TYPE_ETHERNET:
1928 case CONNMAN_SERVICE_TYPE_WIFI:
1929 if (service->network) {
1930 const unsigned char *ssid;
1931 unsigned int ssid_len = 0;
1933 ssid = connman_network_get_blob(service->network,
1934 "WiFi.SSID", &ssid_len);
1936 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
1937 char *identifier = service->identifier;
1941 str = g_string_sized_new(ssid_len * 2);
1947 for (i = 0; i < ssid_len; i++)
1948 g_string_append_printf(str,
1951 g_key_file_set_string(keyfile, identifier,
1954 g_string_free(str, TRUE);
1959 case CONNMAN_SERVICE_TYPE_WIMAX:
1960 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1961 case CONNMAN_SERVICE_TYPE_CELLULAR:
1962 g_key_file_set_boolean(keyfile, service->identifier,
1963 "Favorite", service->favorite);
1965 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
1966 const char *failure = error2string(service->error);
1967 if (failure != NULL)
1968 g_key_file_set_string(keyfile,
1969 service->identifier,
1970 "Failure", failure);
1972 g_key_file_remove_key(keyfile, service->identifier,
1978 str = g_time_val_to_iso8601(&service->modified);
1980 g_key_file_set_string(keyfile, service->identifier,
1985 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
1986 g_key_file_set_string(keyfile, service->identifier,
1987 "Passphrase", service->passphrase);
1989 g_key_file_remove_key(keyfile, service->identifier,
1990 "Passphrase", NULL);
1992 __connman_ipconfig_save(service->ipconfig, keyfile,
1993 service->identifier, "IPv4.");
1995 data = g_key_file_to_data(keyfile, &length, NULL);
1997 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1998 connman_error("Failed to store service information");
2003 g_key_file_free(keyfile);
2010 static struct connman_storage service_storage = {
2012 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
2013 .service_load = service_load,
2014 .service_save = service_save,
2017 int __connman_service_init(void)
2021 connection = connman_dbus_get_connection();
2023 if (connman_storage_register(&service_storage) < 0)
2024 connman_error("Failed to register service storage");
2026 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2029 service_list = g_sequence_new(service_free);
2034 void __connman_service_cleanup(void)
2038 g_sequence_free(service_list);
2039 service_list = NULL;
2041 g_hash_table_destroy(service_hash);
2042 service_hash = NULL;
2044 connman_storage_unregister(&service_storage);
2046 dbus_connection_unref(connection);