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 void reply_pending(struct connman_service *service, int error)
500 if (service->timeout > 0) {
501 g_source_remove(service->timeout);
502 service->timeout = 0;
505 if (service->pending != NULL) {
509 reply = __connman_error_failed(service->pending,
512 g_dbus_send_message(connection, reply);
514 g_dbus_send_reply(connection, service->pending,
517 dbus_message_unref(service->pending);
518 service->pending = NULL;
522 static gboolean connect_timeout(gpointer user_data)
524 struct connman_service *service = user_data;
525 connman_bool_t auto_connect = FALSE;
527 DBG("service %p", service);
529 service->timeout = 0;
531 if (service->network != NULL) {
532 connman_bool_t connected;
534 connected = connman_network_get_connected(service->network);
535 if (connected == TRUE) {
536 __connman_service_indicate_state(service,
537 CONNMAN_SERVICE_STATE_READY);
541 __connman_network_disconnect(service->network);
542 } else if (service->device != NULL) {
543 connman_bool_t disconnected;
545 disconnected = connman_device_get_disconnected(service->device);
546 if (disconnected == FALSE) {
547 __connman_service_indicate_state(service,
548 CONNMAN_SERVICE_STATE_READY);
552 __connman_device_disconnect(service->device);
555 if (service->pending != NULL) {
558 reply = __connman_error_operation_timeout(service->pending);
560 g_dbus_send_message(connection, reply);
562 dbus_message_unref(service->pending);
563 service->pending = NULL;
567 __connman_service_indicate_state(service,
568 CONNMAN_SERVICE_STATE_FAILURE);
570 if (auto_connect == TRUE)
571 __connman_service_auto_connect();
576 static DBusMessage *connect_service(DBusConnection *conn,
577 DBusMessage *msg, void *user_data)
579 struct connman_service *service = user_data;
583 DBG("service %p", service);
585 if (service->pending != NULL)
586 return __connman_error_in_progress(msg);
588 iter = g_sequence_get_begin_iter(service_list);
590 while (g_sequence_iter_is_end(iter) == FALSE) {
591 struct connman_service *temp = g_sequence_get(iter);
593 if (service->type == temp->type &&
594 is_connecting(temp) == TRUE)
595 return __connman_error_in_progress(msg);
597 iter = g_sequence_iter_next(iter);
600 service->ignore = FALSE;
602 service->pending = dbus_message_ref(msg);
604 err = __connman_service_connect(service);
606 if (err != -EINPROGRESS) {
607 dbus_message_unref(service->pending);
608 service->pending = NULL;
610 return __connman_error_failed(msg, -err);
616 dbus_message_unref(service->pending);
617 service->pending = NULL;
619 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
622 static DBusMessage *disconnect_service(DBusConnection *conn,
623 DBusMessage *msg, void *user_data)
625 struct connman_service *service = user_data;
628 DBG("service %p", service);
630 service->ignore = TRUE;
632 err = __connman_service_disconnect(service);
634 if (err != -EINPROGRESS)
635 return __connman_error_failed(msg, -err);
640 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
643 static DBusMessage *remove_service(DBusConnection *conn,
644 DBusMessage *msg, void *user_data)
646 struct connman_service *service = user_data;
648 DBG("service %p", service);
650 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
651 return __connman_error_not_supported(msg);
653 if (service->favorite == FALSE)
654 return __connman_error_not_supported(msg);
656 if (service->network != NULL)
657 __connman_network_disconnect(service->network);
659 g_free(service->passphrase);
660 service->passphrase = NULL;
662 connman_service_set_favorite(service, FALSE);
663 __connman_storage_save_service(service);
665 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
668 static DBusMessage *move_before(DBusConnection *conn,
669 DBusMessage *msg, void *user_data)
671 struct connman_service *service = user_data;
672 struct connman_service *target;
674 GSequenceIter *src, *dst;
676 DBG("service %p", service);
678 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
681 if (service->favorite == FALSE)
682 return __connman_error_not_supported(msg);
684 target = find_service(path);
685 if (target == NULL || target->favorite == FALSE || target == service)
686 return __connman_error_invalid_service(msg);
688 DBG("target %s", target->identifier);
690 g_get_current_time(&service->modified);
691 __connman_storage_save_service(service);
693 src = g_hash_table_lookup(service_hash, service->identifier);
694 dst = g_hash_table_lookup(service_hash, target->identifier);
696 g_sequence_move(src, dst);
698 __connman_profile_changed(FALSE);
700 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
703 static DBusMessage *move_after(DBusConnection *conn,
704 DBusMessage *msg, void *user_data)
706 struct connman_service *service = user_data;
707 struct connman_service *target;
710 DBG("service %p", service);
712 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
715 if (service->favorite == FALSE)
716 return __connman_error_not_supported(msg);
718 target = find_service(path);
719 if (target == NULL || target->favorite == FALSE || target == service)
720 return __connman_error_invalid_service(msg);
722 DBG("target %s", target->identifier);
724 g_get_current_time(&service->modified);
725 __connman_storage_save_service(service);
727 return __connman_error_not_implemented(msg);
730 static GDBusMethodTable service_methods[] = {
731 { "GetProperties", "", "a{sv}", get_properties },
732 { "SetProperty", "sv", "", set_property },
733 { "ClearProperty", "s", "", clear_property },
734 { "Connect", "", "", connect_service,
735 G_DBUS_METHOD_FLAG_ASYNC },
736 { "Disconnect", "", "", disconnect_service },
737 { "Remove", "", "", remove_service },
738 { "MoveBefore", "o", "", move_before },
739 { "MoveAfter", "o", "", move_after },
743 static GDBusSignalTable service_signals[] = {
744 { "PropertyChanged", "sv" },
748 static void service_free(gpointer user_data)
750 struct connman_service *service = user_data;
751 char *path = service->path;
753 DBG("service %p", service);
755 reply_pending(service, ENOENT);
757 g_hash_table_remove(service_hash, service->identifier);
759 service->path = NULL;
762 __connman_profile_changed(FALSE);
764 g_dbus_unregister_interface(connection, path,
765 CONNMAN_SERVICE_INTERFACE);
769 if (service->network != NULL)
770 connman_network_unref(service->network);
772 connman_ipconfig_unref(service->ipconfig);
774 g_free(service->profile);
775 g_free(service->name);
776 g_free(service->passphrase);
777 g_free(service->identifier);
782 * __connman_service_put:
783 * @service: service structure
785 * Release service if no longer needed
787 void __connman_service_put(struct connman_service *service)
789 DBG("service %p", service);
791 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
794 iter = g_hash_table_lookup(service_hash, service->identifier);
796 __connman_service_disconnect(service);
797 g_sequence_remove(iter);
799 service_free(service);
803 static void __connman_service_initialize(struct connman_service *service)
805 DBG("service %p", service);
807 service->refcount = 1;
809 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
810 service->mode = CONNMAN_SERVICE_MODE_UNKNOWN;
811 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
812 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
814 service->favorite = FALSE;
815 service->hidden = FALSE;
817 service->ignore = FALSE;
823 * connman_service_create:
825 * Allocate a new service.
827 * Returns: a newly-allocated #connman_service structure
829 struct connman_service *connman_service_create(void)
831 struct connman_service *service;
833 service = g_try_new0(struct connman_service, 1);
837 DBG("service %p", service);
839 __connman_service_initialize(service);
841 service->ipconfig = connman_ipconfig_create();
842 if (service->ipconfig == NULL) {
847 connman_ipconfig_set_method(service->ipconfig,
848 CONNMAN_IPCONFIG_METHOD_DHCP);
854 * connman_service_ref:
855 * @service: service structure
857 * Increase reference counter of service
859 struct connman_service *connman_service_ref(struct connman_service *service)
861 g_atomic_int_inc(&service->refcount);
867 * connman_service_unref:
868 * @service: service structure
870 * Decrease reference counter of service
872 void connman_service_unref(struct connman_service *service)
874 __connman_service_put(service);
877 static gint service_compare(gconstpointer a, gconstpointer b,
880 struct connman_service *service_a = (void *) a;
881 struct connman_service *service_b = (void *) b;
883 if (service_a->state != service_b->state) {
884 if (service_a->state == CONNMAN_SERVICE_STATE_READY)
886 if (service_b->state == CONNMAN_SERVICE_STATE_READY)
890 if (service_a->order > service_b->order)
893 if (service_a->order < service_b->order)
896 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
899 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
902 return (gint) service_b->strength - (gint) service_a->strength;
906 * connman_service_set_favorite:
907 * @service: service structure
908 * @favorite: favorite value
910 * Change the favorite setting of service
912 int connman_service_set_favorite(struct connman_service *service,
913 connman_bool_t favorite)
917 iter = g_hash_table_lookup(service_hash, service->identifier);
921 if (service->favorite == favorite)
924 service->favorite = favorite;
926 g_sequence_sort_changed(iter, service_compare, NULL);
928 __connman_profile_changed(FALSE);
933 int __connman_service_set_carrier(struct connman_service *service,
934 connman_bool_t carrier)
936 DBG("service %p carrier %d", service, carrier);
941 switch (service->type) {
942 case CONNMAN_SERVICE_TYPE_UNKNOWN:
943 case CONNMAN_SERVICE_TYPE_WIFI:
944 case CONNMAN_SERVICE_TYPE_WIMAX:
945 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
946 case CONNMAN_SERVICE_TYPE_CELLULAR:
948 case CONNMAN_SERVICE_TYPE_ETHERNET:
952 if (carrier == FALSE) {
953 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
954 state_changed(service);
956 service->state = CONNMAN_SERVICE_STATE_IDLE;
957 state_changed(service);
959 service->state = CONNMAN_SERVICE_STATE_CARRIER;
960 state_changed(service);
963 return connman_service_set_favorite(service, carrier);
966 int __connman_service_indicate_state(struct connman_service *service,
967 enum connman_service_state state)
971 DBG("service %p state %d", service, state);
976 if (state == CONNMAN_SERVICE_STATE_CARRIER)
977 return __connman_service_set_carrier(service, TRUE);
979 if (service->state == state)
982 if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
983 state == CONNMAN_SERVICE_STATE_IDLE)
986 if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
987 state == CONNMAN_SERVICE_STATE_DISCONNECT)
990 if (state == CONNMAN_SERVICE_STATE_IDLE &&
991 service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
992 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
993 state_changed(service);
995 __connman_service_disconnect(service);
998 service->state = state;
999 state_changed(service);
1001 if (state == CONNMAN_SERVICE_STATE_READY) {
1002 connman_service_set_favorite(service, TRUE);
1004 reply_pending(service, 0);
1006 g_get_current_time(&service->modified);
1007 __connman_storage_save_service(service);
1010 if (state == CONNMAN_SERVICE_STATE_FAILURE) {
1011 reply_pending(service, EIO);
1013 g_get_current_time(&service->modified);
1014 __connman_storage_save_service(service);
1016 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1018 iter = g_hash_table_lookup(service_hash, service->identifier);
1020 g_sequence_sort_changed(iter, service_compare, NULL);
1022 __connman_profile_changed(FALSE);
1024 if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
1025 service->state == CONNMAN_SERVICE_STATE_FAILURE)
1026 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
1031 int __connman_service_indicate_error(struct connman_service *service,
1032 enum connman_service_error error)
1034 DBG("service %p error %d", service, error);
1036 if (service == NULL)
1039 service->error = error;
1041 return __connman_service_indicate_state(service,
1042 CONNMAN_SERVICE_STATE_FAILURE);
1045 int __connman_service_indicate_default(struct connman_service *service)
1047 DBG("service %p", service);
1052 int __connman_service_connect(struct connman_service *service)
1056 DBG("service %p", service);
1058 if (service->state == CONNMAN_SERVICE_STATE_READY)
1061 if (is_connecting(service) == TRUE)
1064 if (service->network != NULL) {
1065 unsigned int ssid_len;
1067 if (connman_network_get_blob(service->network, "WiFi.SSID",
1071 connman_network_set_string(service->network,
1072 "WiFi.Passphrase", service->passphrase);
1074 err = __connman_network_connect(service->network);
1075 } else if (service->device != NULL) {
1076 if (service->favorite == FALSE)
1079 err = __connman_device_connect(service->device);
1084 if (err != -EINPROGRESS)
1087 service->timeout = g_timeout_add_seconds(45,
1088 connect_timeout, service);
1090 return -EINPROGRESS;
1096 int __connman_service_disconnect(struct connman_service *service)
1100 DBG("service %p", service);
1102 reply_pending(service, ECONNABORTED);
1104 if (service->network != NULL) {
1105 err = __connman_network_disconnect(service->network);
1106 } else if (service->device != NULL) {
1107 if (service->favorite == FALSE)
1109 err = __connman_device_disconnect(service->device);
1114 if (err != -EINPROGRESS)
1117 return -EINPROGRESS;
1124 * __connman_service_lookup:
1125 * @identifier: service identifier
1127 * Look up a service by identifier (reference count will not be increased)
1129 static struct connman_service *__connman_service_lookup(const char *identifier)
1131 GSequenceIter *iter;
1133 iter = g_hash_table_lookup(service_hash, identifier);
1135 return g_sequence_get(iter);
1140 static struct connman_network *create_hidden_wifi(struct connman_device *device,
1141 const char *ssid, const char *mode, const char *security)
1143 struct connman_network *network;
1146 unsigned int i, ssid_len;
1148 ssid_len = strlen(ssid);
1152 network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
1153 if (network == NULL)
1156 connman_network_set_blob(network, "WiFi.SSID",
1157 (unsigned char *) ssid, ssid_len);
1159 connman_network_set_string(network, "WiFi.Mode", mode);
1160 connman_network_set_string(network, "WiFi.Security", security);
1162 name = g_try_malloc0(ssid_len + 1);
1164 connman_network_unref(network);
1168 for (i = 0; i < ssid_len; i++) {
1169 if (g_ascii_isprint(ssid[i]))
1175 connman_network_set_name(network, name);
1179 index = connman_device_get_index(device);
1180 connman_network_set_index(network, index);
1182 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
1184 if (connman_device_add_network(device, network) < 0) {
1185 connman_network_unref(network);
1189 connman_network_set_available(network, TRUE);
1194 int __connman_service_create_and_connect(DBusMessage *msg)
1196 struct connman_service *service;
1197 struct connman_network *network;
1198 struct connman_device *device;
1199 DBusMessageIter iter, array;
1200 const char *mode = "managed", *security = "none";
1201 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
1202 unsigned int ssid_len = 0;
1205 gboolean created = FALSE;
1208 dbus_message_iter_init(msg, &iter);
1209 dbus_message_iter_recurse(&iter, &array);
1211 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1212 DBusMessageIter entry, value;
1215 dbus_message_iter_recurse(&array, &entry);
1216 dbus_message_iter_get_basic(&entry, &key);
1218 dbus_message_iter_next(&entry);
1219 dbus_message_iter_recurse(&entry, &value);
1221 switch (dbus_message_iter_get_arg_type(&value)) {
1222 case DBUS_TYPE_STRING:
1223 if (g_str_equal(key, "Type") == TRUE)
1224 dbus_message_iter_get_basic(&value, &type);
1225 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
1226 g_str_equal(key, "Mode") == TRUE)
1227 dbus_message_iter_get_basic(&value, &mode);
1228 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
1229 g_str_equal(key, "Security") == TRUE)
1230 dbus_message_iter_get_basic(&value, &security);
1231 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
1232 g_str_equal(key, "Passphrase") == TRUE)
1233 dbus_message_iter_get_basic(&value, &passphrase);
1234 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
1235 g_str_equal(key, "SSID") == TRUE)
1236 dbus_message_iter_get_basic(&value, &ssid);
1239 dbus_message_iter_next(&array);
1245 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
1251 ssid_len = strlen(ssid);
1255 device = __connman_element_find_device(CONNMAN_DEVICE_TYPE_WIFI);
1259 ident = __connman_device_get_ident(device);
1263 group = connman_wifi_build_group_name((unsigned char *) ssid,
1264 ssid_len, mode, security);
1268 name = g_strdup_printf("%s_%s_%s", type, ident, group);
1270 service = __connman_service_lookup(name);
1272 if (service != NULL)
1275 network = create_hidden_wifi(device, ssid, mode, security);
1276 if (network != NULL) {
1277 connman_network_set_group(network, group);
1281 service = __connman_service_lookup(name);
1287 if (service == NULL) {
1292 __connman_device_disconnect(device);
1294 if (passphrase != NULL) {
1295 g_free(service->passphrase);
1296 service->passphrase = g_strdup(passphrase);
1299 err = __connman_service_connect(service);
1300 if (err < 0 && err != -EINPROGRESS)
1303 g_dbus_send_reply(connection, msg,
1304 DBUS_TYPE_OBJECT_PATH, &service->path,
1310 if (service != NULL && created == TRUE) {
1311 struct connman_network *network = service->network;
1313 if (network != NULL) {
1314 connman_network_set_available(network, FALSE);
1315 __connman_device_cleanup_networks(device);
1318 __connman_service_put(service);
1325 * __connman_service_get:
1326 * @identifier: service identifier
1328 * Look up a service by identifier or create a new one if not found
1330 static struct connman_service *__connman_service_get(const char *identifier)
1332 struct connman_service *service;
1333 GSequenceIter *iter;
1335 iter = g_hash_table_lookup(service_hash, identifier);
1337 service = g_sequence_get(iter);
1338 if (service != NULL)
1339 g_atomic_int_inc(&service->refcount);
1343 service = connman_service_create();
1344 if (service == NULL)
1347 DBG("service %p", service);
1349 service->identifier = g_strdup(identifier);
1351 service->profile = g_strdup(__connman_profile_active_ident());
1353 __connman_storage_load_service(service);
1355 iter = g_sequence_insert_sorted(service_list, service,
1356 service_compare, NULL);
1358 g_hash_table_insert(service_hash, service->identifier, iter);
1363 static int service_register(struct connman_service *service)
1365 const char *path = __connman_profile_active_path();
1366 GSequenceIter *iter;
1368 DBG("service %p", service);
1370 if (service->path != NULL)
1373 service->path = g_strdup_printf("%s/%s", path, service->identifier);
1375 DBG("path %s", service->path);
1377 g_dbus_register_interface(connection, service->path,
1378 CONNMAN_SERVICE_INTERFACE,
1379 service_methods, service_signals,
1380 NULL, service, NULL);
1382 __connman_storage_load_service(service);
1384 iter = g_hash_table_lookup(service_hash, service->identifier);
1386 g_sequence_sort_changed(iter, service_compare, NULL);
1388 __connman_profile_changed(TRUE);
1394 * __connman_service_lookup_from_device:
1395 * @device: device structure
1397 * Look up a service by device (reference count will not be increased)
1399 struct connman_service *__connman_service_lookup_from_device(struct connman_device *device)
1401 struct connman_service *service;
1405 ident = __connman_device_get_ident(device);
1409 name = g_strdup_printf("%s_%s",
1410 __connman_device_get_type(device), ident);
1411 service = __connman_service_lookup(name);
1418 * __connman_service_create_from_device:
1419 * @device: device structure
1421 * Look up service by device and if not found, create one
1423 struct connman_service *__connman_service_create_from_device(struct connman_device *device)
1425 struct connman_service *service;
1429 ident = __connman_device_get_ident(device);
1433 name = g_strdup_printf("%s_%s",
1434 __connman_device_get_type(device), ident);
1435 service = __connman_service_get(name);
1438 if (service == NULL)
1441 if (service->path != NULL) {
1442 __connman_profile_changed(TRUE);
1446 service->type = __connman_device_get_service_type(device);
1448 service->device = device;
1450 service_register(service);
1452 __connman_profile_changed(TRUE);
1454 if (service->favorite == TRUE)
1455 __connman_service_auto_connect();
1460 void __connman_service_remove_from_device(struct connman_device *device)
1462 struct connman_service *service;
1464 service = __connman_service_lookup_from_device(device);
1465 if (service == NULL)
1468 __connman_service_put(service);
1472 * __connman_service_lookup_from_network:
1473 * @network: network structure
1475 * Look up a service by network (reference count will not be increased)
1477 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
1479 struct connman_service *service;
1480 const char *ident, *group;
1483 ident = __connman_network_get_ident(network);
1487 group = connman_network_get_group(network);
1491 name = g_strdup_printf("%s_%s_%s",
1492 __connman_network_get_type(network), ident, group);
1493 service = __connman_service_lookup(name);
1499 unsigned int __connman_service_get_order(struct connman_service *service)
1501 GSequenceIter *iter;
1503 if (service == NULL)
1506 if (service->favorite == FALSE) {
1511 iter = g_hash_table_lookup(service_hash, service->identifier);
1513 if (g_sequence_iter_get_position(iter) == 0)
1520 return service->order;
1523 static enum connman_service_type convert_network_type(struct connman_network *network)
1525 enum connman_network_type type = connman_network_get_type(network);
1528 case CONNMAN_NETWORK_TYPE_UNKNOWN:
1529 case CONNMAN_NETWORK_TYPE_VENDOR:
1530 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1531 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1533 case CONNMAN_NETWORK_TYPE_MBM:
1534 case CONNMAN_NETWORK_TYPE_HSO:
1535 return CONNMAN_SERVICE_TYPE_CELLULAR;
1536 case CONNMAN_NETWORK_TYPE_WIFI:
1537 return CONNMAN_SERVICE_TYPE_WIFI;
1538 case CONNMAN_NETWORK_TYPE_WIMAX:
1539 return CONNMAN_SERVICE_TYPE_WIMAX;
1542 return CONNMAN_SERVICE_TYPE_UNKNOWN;
1545 static enum connman_service_mode convert_wifi_mode(const char *mode)
1548 return CONNMAN_SERVICE_MODE_UNKNOWN;
1549 else if (g_str_equal(mode, "managed") == TRUE)
1550 return CONNMAN_SERVICE_MODE_MANAGED;
1551 else if (g_str_equal(mode, "adhoc") == TRUE)
1552 return CONNMAN_SERVICE_MODE_ADHOC;
1554 return CONNMAN_SERVICE_MODE_UNKNOWN;
1557 static enum connman_service_mode convert_wifi_security(const char *security)
1559 if (security == NULL)
1560 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1561 else if (g_str_equal(security, "none") == TRUE)
1562 return CONNMAN_SERVICE_SECURITY_NONE;
1563 else if (g_str_equal(security, "wep") == TRUE)
1564 return CONNMAN_SERVICE_SECURITY_WEP;
1565 else if (g_str_equal(security, "wpa") == TRUE)
1566 return CONNMAN_SERVICE_SECURITY_WPA;
1567 else if (g_str_equal(security, "rsn") == TRUE)
1568 return CONNMAN_SERVICE_SECURITY_RSN;
1570 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
1573 static void update_from_network(struct connman_service *service,
1574 struct connman_network *network)
1576 connman_uint8_t strength = service->strength;
1577 GSequenceIter *iter;
1580 if (service->state == CONNMAN_SERVICE_STATE_READY)
1583 if (is_connecting(service) == TRUE)
1586 str = connman_network_get_string(network, "Name");
1588 g_free(service->name);
1589 service->name = g_strdup(str);
1590 service->hidden = FALSE;
1592 g_free(service->name);
1593 service->name = NULL;
1594 service->hidden = TRUE;
1597 service->strength = connman_network_get_uint8(network, "Strength");
1599 str = connman_network_get_string(network, "WiFi.Mode");
1600 service->mode = convert_wifi_mode(str);
1602 str = connman_network_get_string(network, "WiFi.Security");
1603 service->security = convert_wifi_security(str);
1605 if (service->strength > strength && service->network != NULL) {
1606 connman_network_unref(service->network);
1607 service->network = connman_network_ref(network);
1609 strength_changed(service);
1612 if (service->network == NULL)
1613 service->network = connman_network_ref(network);
1615 iter = g_hash_table_lookup(service_hash, service->identifier);
1617 g_sequence_sort_changed(iter, service_compare, NULL);
1621 * __connman_service_create_from_network:
1622 * @network: network structure
1624 * Look up service by network and if not found, create one
1626 struct connman_service *__connman_service_create_from_network(struct connman_network *network)
1628 struct connman_service *service;
1629 const char *ident, *group;
1632 ident = __connman_network_get_ident(network);
1636 group = connman_network_get_group(network);
1640 name = g_strdup_printf("%s_%s_%s",
1641 __connman_network_get_type(network), ident, group);
1642 service = __connman_service_get(name);
1645 if (service == NULL)
1648 if (__connman_network_get_weakness(network) == TRUE)
1651 if (service->path != NULL) {
1652 update_from_network(service, network);
1653 __connman_profile_changed(TRUE);
1657 service->type = convert_network_type(network);
1659 service->state = CONNMAN_SERVICE_STATE_IDLE;
1661 update_from_network(service, network);
1663 service_register(service);
1665 __connman_profile_changed(TRUE);
1667 if (service->favorite == TRUE)
1668 __connman_service_auto_connect();
1673 void __connman_service_remove_from_network(struct connman_network *network)
1675 struct connman_service *service;
1677 service = __connman_service_lookup_from_network(network);
1678 if (service == NULL)
1681 __connman_service_put(service);
1684 static int service_load(struct connman_service *service)
1687 gchar *pathname, *data = NULL;
1690 unsigned int ssid_len;
1693 DBG("service %p", service);
1695 if (service->profile == NULL)
1698 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1699 if (pathname == NULL)
1702 keyfile = g_key_file_new();
1704 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1711 if (g_key_file_load_from_data(keyfile, data, length,
1712 0, NULL) == FALSE) {
1719 switch (service->type) {
1720 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1721 case CONNMAN_SERVICE_TYPE_ETHERNET:
1723 case CONNMAN_SERVICE_TYPE_WIFI:
1724 if (service->name == NULL) {
1727 name = g_key_file_get_string(keyfile,
1728 service->identifier, "Name", NULL);
1730 g_free(service->name);
1731 service->name = name;
1734 if (service->network != NULL)
1735 connman_network_set_name(service->network,
1739 if (service->network &&
1740 connman_network_get_blob(service->network,
1741 "WiFi.SSID", &ssid_len) == NULL) {
1744 hex_ssid = g_key_file_get_string(keyfile,
1745 service->identifier,
1748 if (hex_ssid != NULL) {
1750 unsigned int i, j = 0, hex;
1751 size_t hex_ssid_len = strlen(hex_ssid);
1753 ssid = g_try_malloc0(hex_ssid_len / 2);
1760 for (i = 0; i < hex_ssid_len; i += 2) {
1761 sscanf(hex_ssid + i, "%02x", &hex);
1765 connman_network_set_blob(service->network,
1766 "WiFi.SSID", ssid, hex_ssid_len / 2);
1773 case CONNMAN_SERVICE_TYPE_WIMAX:
1774 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1775 case CONNMAN_SERVICE_TYPE_CELLULAR:
1776 service->favorite = g_key_file_get_boolean(keyfile,
1777 service->identifier, "Favorite", NULL);
1779 str = g_key_file_get_string(keyfile,
1780 service->identifier, "Failure", NULL);
1782 service->state = CONNMAN_SERVICE_STATE_FAILURE;
1783 service->error = string2error(str);
1788 str = g_key_file_get_string(keyfile,
1789 service->identifier, "Modified", NULL);
1791 g_time_val_from_iso8601(str, &service->modified);
1795 str = g_key_file_get_string(keyfile,
1796 service->identifier, "Passphrase", NULL);
1798 g_free(service->passphrase);
1799 service->passphrase = str;
1802 __connman_ipconfig_load(service->ipconfig, keyfile,
1803 service->identifier, "IPv4.");
1806 g_key_file_free(keyfile);
1811 static int service_save(struct connman_service *service)
1814 gchar *pathname, *data = NULL;
1819 DBG("service %p", service);
1821 if (service->profile == NULL)
1824 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, service->profile);
1825 if (pathname == NULL)
1828 keyfile = g_key_file_new();
1830 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1834 if (g_key_file_load_from_data(keyfile, data, length,
1842 if (service->name != NULL)
1843 g_key_file_set_string(keyfile, service->identifier,
1844 "Name", service->name);
1846 switch (service->type) {
1847 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1848 case CONNMAN_SERVICE_TYPE_ETHERNET:
1850 case CONNMAN_SERVICE_TYPE_WIFI:
1851 if (service->network) {
1852 const unsigned char *ssid;
1853 unsigned int ssid_len = 0;
1855 ssid = connman_network_get_blob(service->network,
1856 "WiFi.SSID", &ssid_len);
1858 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
1859 char *identifier = service->identifier;
1863 str = g_string_sized_new(ssid_len * 2);
1869 for (i = 0; i < ssid_len; i++)
1870 g_string_append_printf(str,
1873 g_key_file_set_string(keyfile, identifier,
1876 g_string_free(str, TRUE);
1881 case CONNMAN_SERVICE_TYPE_WIMAX:
1882 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1883 case CONNMAN_SERVICE_TYPE_CELLULAR:
1884 g_key_file_set_boolean(keyfile, service->identifier,
1885 "Favorite", service->favorite);
1887 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
1888 const char *failure = error2string(service->error);
1889 if (failure != NULL)
1890 g_key_file_set_string(keyfile,
1891 service->identifier,
1892 "Failure", failure);
1894 g_key_file_remove_key(keyfile, service->identifier,
1900 str = g_time_val_to_iso8601(&service->modified);
1902 g_key_file_set_string(keyfile, service->identifier,
1907 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
1908 g_key_file_set_string(keyfile, service->identifier,
1909 "Passphrase", service->passphrase);
1911 g_key_file_remove_key(keyfile, service->identifier,
1912 "Passphrase", NULL);
1914 __connman_ipconfig_save(service->ipconfig, keyfile,
1915 service->identifier, "IPv4.");
1917 data = g_key_file_to_data(keyfile, &length, NULL);
1919 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1920 connman_error("Failed to store service information");
1925 g_key_file_free(keyfile);
1932 static struct connman_storage service_storage = {
1934 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1935 .service_load = service_load,
1936 .service_save = service_save,
1939 int __connman_service_init(void)
1943 connection = connman_dbus_get_connection();
1945 if (connman_storage_register(&service_storage) < 0)
1946 connman_error("Failed to register service storage");
1948 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1951 service_list = g_sequence_new(service_free);
1956 void __connman_service_cleanup(void)
1960 g_sequence_free(service_list);
1961 service_list = NULL;
1963 g_hash_table_destroy(service_hash);
1964 service_hash = NULL;
1966 connman_storage_unregister(&service_storage);
1968 dbus_connection_unref(connection);