5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
6 * Copyright (C) 2011 BWM CarIT GmbH. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <connman/session.h>
35 static DBusConnection *connection;
36 static GHashTable *session_hash;
37 static connman_bool_t sessionmode;
38 static struct connman_session *ecall_session;
40 enum connman_session_trigger {
41 CONNMAN_SESSION_TRIGGER_UNKNOWN = 0,
42 CONNMAN_SESSION_TRIGGER_SETTING = 1,
43 CONNMAN_SESSION_TRIGGER_CONNECT = 2,
44 CONNMAN_SESSION_TRIGGER_DISCONNECT = 3,
45 CONNMAN_SESSION_TRIGGER_SERVICE = 4,
46 CONNMAN_SESSION_TRIGGER_ECALL = 5,
49 enum connman_session_reason {
50 CONNMAN_SESSION_REASON_UNKNOWN = 0,
51 CONNMAN_SESSION_REASON_CONNECT = 1,
52 CONNMAN_SESSION_REASON_FREE_RIDE = 2,
55 enum connman_session_state {
56 CONNMAN_SESSION_STATE_DISCONNECTED = 0,
57 CONNMAN_SESSION_STATE_CONNECTED = 1,
58 CONNMAN_SESSION_STATE_ONLINE = 2,
61 enum connman_session_type {
62 CONNMAN_SESSION_TYPE_ANY = 0,
63 CONNMAN_SESSION_TYPE_LOCAL = 1,
64 CONNMAN_SESSION_TYPE_INTERNET = 2,
67 enum connman_session_roaming_policy {
68 CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN = 0,
69 CONNMAN_SESSION_ROAMING_POLICY_DEFAULT = 1,
70 CONNMAN_SESSION_ROAMING_POLICY_ALWAYS = 2,
71 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN = 3,
72 CONNMAN_SESSION_ROAMING_POLICY_NATIONAL = 4,
73 CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL = 5,
76 struct service_entry {
77 /* track why this service was selected */
78 enum connman_session_reason reason;
79 enum connman_service_state state;
81 struct connman_service *service;
84 GSList *pending_timeouts;
88 enum connman_session_state state;
89 enum connman_session_type type;
90 connman_bool_t priority;
91 GSList *allowed_bearers;
92 enum connman_session_roaming_policy roaming_policy;
94 struct service_entry *entry;
95 enum connman_session_reason reason;
98 struct connman_session {
104 struct connman_session_policy *policy;
106 connman_bool_t append_all;
107 struct session_info *info;
108 struct session_info *info_last;
110 connman_bool_t ecall;
112 GSequence *service_list;
113 GHashTable *service_hash;
118 connman_bool_t match_all;
119 enum connman_service_type service_type;
122 static const char *trigger2string(enum connman_session_trigger trigger)
125 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
127 case CONNMAN_SESSION_TRIGGER_SETTING:
129 case CONNMAN_SESSION_TRIGGER_CONNECT:
131 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
133 case CONNMAN_SESSION_TRIGGER_SERVICE:
135 case CONNMAN_SESSION_TRIGGER_ECALL:
142 static const char *reason2string(enum connman_session_reason reason)
145 case CONNMAN_SESSION_REASON_UNKNOWN:
147 case CONNMAN_SESSION_REASON_CONNECT:
149 case CONNMAN_SESSION_REASON_FREE_RIDE:
156 static const char *state2string(enum connman_session_state state)
159 case CONNMAN_SESSION_STATE_DISCONNECTED:
160 return "disconnected";
161 case CONNMAN_SESSION_STATE_CONNECTED:
163 case CONNMAN_SESSION_STATE_ONLINE:
170 static const char *type2string(enum connman_session_type type)
173 case CONNMAN_SESSION_TYPE_ANY:
175 case CONNMAN_SESSION_TYPE_LOCAL:
177 case CONNMAN_SESSION_TYPE_INTERNET:
184 static enum connman_session_type string2type(const char *type)
186 if (g_strcmp0(type, "local") == 0)
187 return CONNMAN_SESSION_TYPE_LOCAL;
188 else if (g_strcmp0(type, "internet") == 0)
189 return CONNMAN_SESSION_TYPE_INTERNET;
191 return CONNMAN_SESSION_TYPE_ANY;
194 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
196 if (g_strcmp0(policy, "default") == 0)
197 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
198 else if (g_strcmp0(policy, "always") == 0)
199 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
200 else if (g_strcmp0(policy, "forbidden") == 0)
201 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
202 else if (g_strcmp0(policy, "national") == 0)
203 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
204 else if (g_strcmp0(policy, "international") == 0)
205 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
207 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
210 static enum connman_service_type bearer2service(const char *bearer)
213 return CONNMAN_SERVICE_TYPE_UNKNOWN;
215 if (g_strcmp0(bearer, "ethernet") == 0)
216 return CONNMAN_SERVICE_TYPE_ETHERNET;
217 else if (g_strcmp0(bearer, "wifi") == 0)
218 return CONNMAN_SERVICE_TYPE_WIFI;
219 else if (g_strcmp0(bearer, "wimax") == 0)
220 return CONNMAN_SERVICE_TYPE_WIMAX;
221 else if (g_strcmp0(bearer, "bluetooth") == 0)
222 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
223 else if (g_strcmp0(bearer, "cellular") == 0)
224 return CONNMAN_SERVICE_TYPE_CELLULAR;
225 else if (g_strcmp0(bearer, "vpn") == 0)
226 return CONNMAN_SERVICE_TYPE_VPN;
228 return CONNMAN_SERVICE_TYPE_UNKNOWN;
231 static char *service2bearer(enum connman_service_type type)
234 case CONNMAN_SERVICE_TYPE_ETHERNET:
236 case CONNMAN_SERVICE_TYPE_WIFI:
238 case CONNMAN_SERVICE_TYPE_WIMAX:
240 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
242 case CONNMAN_SERVICE_TYPE_CELLULAR:
244 case CONNMAN_SERVICE_TYPE_VPN:
246 case CONNMAN_SERVICE_TYPE_UNKNOWN:
247 case CONNMAN_SERVICE_TYPE_SYSTEM:
248 case CONNMAN_SERVICE_TYPE_GPS:
249 case CONNMAN_SERVICE_TYPE_GADGET:
256 static int policy_get_bool(struct connman_session *session, const char *id,
257 const char *key, connman_bool_t *val)
259 if (session->policy == NULL) {
264 return (*session->policy->get_bool)(id, key, val);
267 static int policy_get_string(struct connman_session *session, const char *id,
268 const char *key, char **val)
270 if (session->policy == NULL) {
275 return (*session->policy->get_string)(id, key, val);
278 static struct connman_session *session_lookup_by_id(const char *id)
280 struct connman_session *session;
286 g_hash_table_iter_init(&iter, session_hash);
288 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
291 if (g_strcmp0(session->owner, id) == FALSE)
297 DBG("No session found by id %s", id);
302 int connman_session_update_bool(const char *id, const char *key,
305 struct connman_session *session;
306 struct session_info *info;
308 session = session_lookup_by_id(id);
312 info = session->info;
316 DBG("%s %d", key, val);
321 int connman_session_update_string(const char *id, const char *key,
324 struct connman_session *session;
325 struct session_info *info;
327 session = session_lookup_by_id(id);
331 info = session->info;
335 DBG("%s %s", key, val);
340 int connman_session_policy_register(struct connman_session_policy *policy)
342 DBG("name %s", policy->name);
347 void connman_session_policy_unregister(struct connman_session_policy *policy)
349 DBG("name %s", policy->name);
352 static void cleanup_bearer_info(gpointer data, gpointer user_data)
354 struct bearer_info *info = data;
360 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
362 struct bearer_info *info;
363 DBusMessageIter array;
366 dbus_message_iter_recurse(iter, &array);
368 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
371 dbus_message_iter_get_basic(&array, &bearer);
373 info = g_try_new0(struct bearer_info, 1);
375 g_slist_foreach(list, cleanup_bearer_info, NULL);
381 info->name = g_strdup(bearer);
382 info->service_type = bearer2service(info->name);
384 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
385 g_strcmp0(info->name, "*") == 0) {
386 info->match_all = TRUE;
388 info->match_all = FALSE;
391 list = g_slist_append(list, info);
393 dbus_message_iter_next(&array);
399 static GSList *session_allowed_bearers_any(void)
401 struct bearer_info *info;
404 info = g_try_new0(struct bearer_info, 1);
408 info->name = g_strdup("");
409 info->match_all = TRUE;
410 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
412 list = g_slist_append(list, info);
417 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
419 struct session_info *info = user_data;
422 for (list = info->allowed_bearers;
423 list != NULL; list = list->next) {
424 struct bearer_info *bearer_info = list->data;
426 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
431 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
433 struct connman_service *service = user_data;
434 struct connman_ipconfig *ipconfig_ipv4;
439 if (__connman_service_is_connected_state(service,
440 CONNMAN_IPCONFIG_TYPE_IPV4) == FALSE) {
444 ipconfig_ipv4 = __connman_service_get_ip4config(service);
445 if (ipconfig_ipv4 == NULL)
448 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
451 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
453 struct connman_service *service = user_data;
454 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
459 if (__connman_service_is_connected_state(service,
460 CONNMAN_IPCONFIG_TYPE_IPV6) == FALSE) {
464 ipconfig_ipv4 = __connman_service_get_ip4config(service);
465 ipconfig_ipv6 = __connman_service_get_ip6config(service);
466 if (ipconfig_ipv6 == NULL)
469 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
472 static void append_notify(DBusMessageIter *dict,
473 struct connman_session *session)
475 struct session_info *info = session->info;
476 struct session_info *info_last = session->info_last;
477 struct connman_service *service;
478 const char *name, *ifname, *bearer;
480 if (session->append_all == TRUE ||
481 info->state != info_last->state) {
482 const char *state = state2string(info->state);
484 connman_dbus_dict_append_basic(dict, "State",
487 info_last->state = info->state;
490 if (session->append_all == TRUE ||
491 info->entry != info_last->entry) {
492 if (info->entry == NULL) {
498 name = info->entry->name;
499 ifname = info->entry->ifname;
500 service = info->entry->service;
501 bearer = info->entry->bearer;
504 connman_dbus_dict_append_basic(dict, "Name",
508 connman_dbus_dict_append_dict(dict, "IPv4",
509 append_ipconfig_ipv4,
512 connman_dbus_dict_append_dict(dict, "IPv6",
513 append_ipconfig_ipv6,
516 connman_dbus_dict_append_basic(dict, "Interface",
520 connman_dbus_dict_append_basic(dict, "Bearer",
524 info_last->entry = info->entry;
527 if (session->append_all == TRUE || info->type != info_last->type) {
528 const char *type = type2string(info->type);
530 connman_dbus_dict_append_basic(dict, "ConnectionType",
533 info_last->type = info->type;
536 if (session->append_all == TRUE ||
537 info->allowed_bearers != info_last->allowed_bearers) {
538 connman_dbus_dict_append_array(dict, "AllowedBearers",
540 append_allowed_bearers,
542 info_last->allowed_bearers = info->allowed_bearers;
545 session->append_all = FALSE;
548 static connman_bool_t is_type_matching_state(enum connman_session_state *state,
549 enum connman_session_type type)
552 case CONNMAN_SESSION_TYPE_ANY:
554 case CONNMAN_SESSION_TYPE_LOCAL:
555 if (*state >= CONNMAN_SESSION_STATE_CONNECTED) {
556 *state = CONNMAN_SESSION_STATE_CONNECTED;
561 case CONNMAN_SESSION_TYPE_INTERNET:
562 if (*state == CONNMAN_SESSION_STATE_ONLINE)
570 static connman_bool_t compute_notifiable_changes(struct connman_session *session)
572 struct session_info *info_last = session->info_last;
573 struct session_info *info = session->info;
575 if (session->append_all == TRUE)
578 if (info->state != info_last->state)
581 if (info->entry != info_last->entry &&
582 info->state >= CONNMAN_SESSION_STATE_CONNECTED)
585 if (info->allowed_bearers != info_last->allowed_bearers ||
586 info->type != info_last->type)
592 static gboolean session_notify(gpointer user_data)
594 struct connman_session *session = user_data;
596 DBusMessageIter array, dict;
598 if (compute_notifiable_changes(session) == FALSE)
601 DBG("session %p owner %s notify_path %s", session,
602 session->owner, session->notify_path);
604 msg = dbus_message_new_method_call(session->owner, session->notify_path,
605 CONNMAN_NOTIFICATION_INTERFACE,
610 dbus_message_iter_init_append(msg, &array);
611 connman_dbus_dict_open(&array, &dict);
613 append_notify(&dict, session);
615 connman_dbus_dict_close(&array, &dict);
617 g_dbus_send_message(connection, msg);
622 static void ipconfig_ipv4_changed(struct connman_session *session)
624 struct session_info *info = session->info;
626 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
627 "IPv4", append_ipconfig_ipv4,
628 info->entry->service);
631 static void ipconfig_ipv6_changed(struct connman_session *session)
633 struct session_info *info = session->info;
635 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
636 "IPv6", append_ipconfig_ipv6,
637 info->entry->service);
640 static connman_bool_t service_type_match(struct connman_session *session,
641 struct connman_service *service)
643 struct session_info *info = session->info;
646 for (list = info->allowed_bearers;
647 list != NULL; list = list->next) {
648 struct bearer_info *bearer_info = list->data;
649 enum connman_service_type service_type;
651 if (bearer_info->match_all == TRUE)
654 service_type = connman_service_get_type(service);
655 if (bearer_info->service_type == service_type)
662 static connman_bool_t service_match(struct connman_session *session,
663 struct connman_service *service)
665 if (service_type_match(session, service) == FALSE)
671 static int service_type_weight(enum connman_service_type type)
674 * The session doesn't care which service
675 * to use. Nevertheless we have to sort them
676 * according their type. The ordering is
685 case CONNMAN_SERVICE_TYPE_ETHERNET:
687 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
689 case CONNMAN_SERVICE_TYPE_WIFI:
690 case CONNMAN_SERVICE_TYPE_WIMAX:
692 case CONNMAN_SERVICE_TYPE_CELLULAR:
694 case CONNMAN_SERVICE_TYPE_UNKNOWN:
695 case CONNMAN_SERVICE_TYPE_SYSTEM:
696 case CONNMAN_SERVICE_TYPE_GPS:
697 case CONNMAN_SERVICE_TYPE_VPN:
698 case CONNMAN_SERVICE_TYPE_GADGET:
705 static gint sort_allowed_bearers(struct connman_service *service_a,
706 struct connman_service *service_b,
707 struct connman_session *session)
709 struct session_info *info = session->info;
711 enum connman_service_type type_a, type_b;
712 int weight_a, weight_b;
714 type_a = connman_service_get_type(service_a);
715 type_b = connman_service_get_type(service_b);
717 for (list = info->allowed_bearers;
718 list != NULL; list = list->next) {
719 struct bearer_info *bearer_info = list->data;
721 if (bearer_info->match_all == TRUE) {
722 if (type_a != type_b) {
723 weight_a = service_type_weight(type_a);
724 weight_b = service_type_weight(type_b);
726 if (weight_a > weight_b)
729 if (weight_a < weight_b)
736 if (type_a == bearer_info->service_type &&
737 type_b == bearer_info->service_type) {
741 if (type_a == bearer_info->service_type &&
742 type_b != bearer_info->service_type) {
746 if (type_a != bearer_info->service_type &&
747 type_b == bearer_info->service_type) {
755 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
757 struct service_entry *entry_a = (void *)a;
758 struct service_entry *entry_b = (void *)b;
759 struct connman_session *session = user_data;
761 return sort_allowed_bearers(entry_a->service, entry_b->service,
765 static void cleanup_session(gpointer user_data)
767 struct connman_session *session = user_data;
768 struct session_info *info = session->info;
770 DBG("remove %s", session->session_path);
772 g_hash_table_destroy(session->service_hash);
773 g_sequence_free(session->service_list);
775 if (info->entry != NULL &&
776 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
777 __connman_service_disconnect(info->entry->service);
780 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
781 g_slist_free(info->allowed_bearers);
783 g_free(session->owner);
784 g_free(session->session_path);
785 g_free(session->notify_path);
786 g_free(session->info);
787 g_free(session->info_last);
792 static enum connman_session_state service_to_session_state(enum connman_service_state state)
795 case CONNMAN_SERVICE_STATE_UNKNOWN:
796 case CONNMAN_SERVICE_STATE_IDLE:
797 case CONNMAN_SERVICE_STATE_ASSOCIATION:
798 case CONNMAN_SERVICE_STATE_CONFIGURATION:
799 case CONNMAN_SERVICE_STATE_DISCONNECT:
800 case CONNMAN_SERVICE_STATE_FAILURE:
802 case CONNMAN_SERVICE_STATE_READY:
803 return CONNMAN_SESSION_STATE_CONNECTED;
804 case CONNMAN_SERVICE_STATE_ONLINE:
805 return CONNMAN_SESSION_STATE_ONLINE;
808 return CONNMAN_SESSION_STATE_DISCONNECTED;
811 static connman_bool_t is_connected(enum connman_service_state state)
814 case CONNMAN_SERVICE_STATE_UNKNOWN:
815 case CONNMAN_SERVICE_STATE_IDLE:
816 case CONNMAN_SERVICE_STATE_ASSOCIATION:
817 case CONNMAN_SERVICE_STATE_CONFIGURATION:
818 case CONNMAN_SERVICE_STATE_DISCONNECT:
819 case CONNMAN_SERVICE_STATE_FAILURE:
821 case CONNMAN_SERVICE_STATE_READY:
822 case CONNMAN_SERVICE_STATE_ONLINE:
829 static connman_bool_t is_connecting(enum connman_service_state state)
832 case CONNMAN_SERVICE_STATE_UNKNOWN:
833 case CONNMAN_SERVICE_STATE_IDLE:
835 case CONNMAN_SERVICE_STATE_ASSOCIATION:
836 case CONNMAN_SERVICE_STATE_CONFIGURATION:
838 case CONNMAN_SERVICE_STATE_DISCONNECT:
839 case CONNMAN_SERVICE_STATE_FAILURE:
840 case CONNMAN_SERVICE_STATE_READY:
841 case CONNMAN_SERVICE_STATE_ONLINE:
848 static connman_bool_t explicit_connect(enum connman_session_reason reason)
851 case CONNMAN_SESSION_REASON_UNKNOWN:
852 case CONNMAN_SESSION_REASON_FREE_RIDE:
854 case CONNMAN_SESSION_REASON_CONNECT:
861 static connman_bool_t explicit_disconnect(struct session_info *info)
863 if (info->entry == NULL)
866 DBG("reason %s service %p state %d",
867 reason2string(info->entry->reason),
868 info->entry->service, info->entry->state);
870 if (info->entry->reason == CONNMAN_SESSION_REASON_UNKNOWN)
873 if (explicit_connect(info->entry->reason) == FALSE)
876 if (__connman_service_session_dec(info->entry->service) == FALSE)
882 struct pending_data {
883 unsigned int timeout;
884 struct service_entry *entry;
885 gboolean (*cb)(gpointer);
888 static void pending_timeout_free(gpointer data, gpointer user_data)
890 struct pending_data *pending = data;
892 DBG("pending %p timeout %d", pending, pending->timeout);
893 g_source_remove(pending->timeout);
897 static void pending_timeout_remove_all(struct service_entry *entry)
901 g_slist_foreach(entry->pending_timeouts, pending_timeout_free, NULL);
902 g_slist_free(entry->pending_timeouts);
903 entry->pending_timeouts = NULL;
906 static gboolean pending_timeout_cb(gpointer data)
908 struct pending_data *pending = data;
909 struct service_entry *entry = pending->entry;
912 DBG("pending %p timeout %d", pending, pending->timeout);
914 ret = pending->cb(pending->entry);
916 entry->pending_timeouts =
917 g_slist_remove(entry->pending_timeouts,
924 static connman_bool_t pending_timeout_add(unsigned int seconds,
925 gboolean (*cb)(gpointer),
926 struct service_entry *entry)
928 struct pending_data *pending = g_try_new0(struct pending_data, 1);
930 if (pending == NULL || cb == NULL || entry == NULL) {
936 pending->entry = entry;
937 pending->timeout = g_timeout_add_seconds(seconds, pending_timeout_cb,
939 entry->pending_timeouts = g_slist_prepend(entry->pending_timeouts,
942 DBG("pending %p entry %p timeout id %d", pending, entry,
948 static gboolean call_disconnect(gpointer user_data)
950 struct service_entry *entry = user_data;
951 struct connman_service *service = entry->service;
954 * TODO: We should mark this entry as pending work. In case
955 * disconnect fails we just unassign this session from the
956 * service and can't do anything later on it
958 DBG("disconnect service %p", service);
959 __connman_service_disconnect(service);
964 static gboolean call_connect(gpointer user_data)
966 struct service_entry *entry = user_data;
967 struct connman_service *service = entry->service;
969 DBG("connect service %p", service);
970 __connman_service_connect(service);
975 static void deselect_service(struct session_info *info)
977 struct service_entry *entry;
978 connman_bool_t disconnect, connected;
982 if (info->entry == NULL)
985 disconnect = explicit_disconnect(info);
987 connected = is_connecting(info->entry->state) == TRUE ||
988 is_connected(info->entry->state) == TRUE;
990 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
991 info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
996 DBG("disconnect %d connected %d", disconnect, connected);
998 if (disconnect == TRUE && connected == TRUE)
999 pending_timeout_add(0, call_disconnect, entry);
1002 static void deselect_and_disconnect(struct connman_session *session)
1004 struct session_info *info = session->info;
1006 deselect_service(info);
1008 info->reason = CONNMAN_SESSION_REASON_FREE_RIDE;
1011 static void select_connected_service(struct session_info *info,
1012 struct service_entry *entry)
1014 enum connman_session_state state;
1016 state = service_to_session_state(entry->state);
1017 if (is_type_matching_state(&state, info->type) == FALSE)
1020 info->state = state;
1022 info->entry = entry;
1023 info->entry->reason = info->reason;
1025 if (explicit_connect(info->reason) == FALSE)
1028 __connman_service_session_inc(info->entry->service);
1031 static void select_offline_service(struct session_info *info,
1032 struct service_entry *entry)
1034 if (explicit_connect(info->reason) == FALSE)
1037 info->state = service_to_session_state(entry->state);
1039 info->entry = entry;
1040 info->entry->reason = info->reason;
1042 __connman_service_session_inc(info->entry->service);
1043 pending_timeout_add(0, call_connect, entry);
1046 static void select_service(struct session_info *info,
1047 struct service_entry *entry)
1049 DBG("service %p", entry->service);
1051 if (is_connected(entry->state) == TRUE)
1052 select_connected_service(info, entry);
1054 select_offline_service(info, entry);
1057 static void select_and_connect(struct connman_session *session,
1058 enum connman_session_reason reason)
1060 struct session_info *info = session->info;
1061 struct service_entry *entry = NULL;
1062 GSequenceIter *iter;
1064 DBG("session %p reason %s", session, reason2string(reason));
1066 info->reason = reason;
1068 iter = g_sequence_get_begin_iter(session->service_list);
1070 while (g_sequence_iter_is_end(iter) == FALSE) {
1071 entry = g_sequence_get(iter);
1073 switch (entry->state) {
1074 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1075 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1076 case CONNMAN_SERVICE_STATE_READY:
1077 case CONNMAN_SERVICE_STATE_ONLINE:
1078 case CONNMAN_SERVICE_STATE_IDLE:
1079 case CONNMAN_SERVICE_STATE_DISCONNECT:
1080 select_service(info, entry);
1082 case CONNMAN_SERVICE_STATE_UNKNOWN:
1083 case CONNMAN_SERVICE_STATE_FAILURE:
1087 iter = g_sequence_iter_next(iter);
1091 static struct service_entry *create_service_entry(struct connman_service *service,
1093 enum connman_service_state state)
1095 struct service_entry *entry;
1096 enum connman_service_type type;
1099 entry = g_try_new0(struct service_entry, 1);
1103 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1104 entry->state = state;
1109 entry->service = service;
1111 idx = __connman_service_get_index(entry->service);
1112 entry->ifname = connman_inet_ifname(idx);
1113 if (entry->ifname == NULL)
1114 entry->ifname = g_strdup("");
1116 type = connman_service_get_type(entry->service);
1117 entry->bearer = service2bearer(type);
1122 static void destroy_service_entry(gpointer data)
1124 struct service_entry *entry = data;
1126 pending_timeout_remove_all(entry);
1127 g_free(entry->ifname);
1132 static void populate_service_list(struct connman_session *session)
1134 struct service_entry *entry;
1135 GSequenceIter *iter;
1137 session->service_hash =
1138 g_hash_table_new_full(g_direct_hash, g_direct_equal,
1140 session->service_list = __connman_service_get_list(session,
1142 create_service_entry,
1143 destroy_service_entry);
1145 g_sequence_sort(session->service_list, sort_services, session);
1147 iter = g_sequence_get_begin_iter(session->service_list);
1149 while (g_sequence_iter_is_end(iter) == FALSE) {
1150 entry = g_sequence_get(iter);
1152 DBG("service %p type %s name %s", entry->service,
1153 service2bearer(connman_service_get_type(entry->service)),
1156 g_hash_table_replace(session->service_hash,
1157 entry->service, iter);
1159 iter = g_sequence_iter_next(iter);
1163 static void session_changed(struct connman_session *session,
1164 enum connman_session_trigger trigger)
1166 struct session_info *info = session->info;
1167 struct session_info *info_last = session->info_last;
1168 GSequenceIter *service_iter = NULL, *service_iter_last = NULL;
1169 GSequence *service_list_last;
1170 GHashTable *service_hash_last;
1173 * TODO: This only a placeholder for the 'real' algorithm to
1174 * play a bit around. So we are going to improve it step by step.
1177 DBG("session %p trigger %s reason %s", session, trigger2string(trigger),
1178 reason2string(info->reason));
1180 if (info->entry != NULL) {
1181 enum connman_session_state state;
1183 state = service_to_session_state(info->entry->state);
1185 if (is_type_matching_state(&state, info->type) == TRUE)
1186 info->state = state;
1190 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
1191 DBG("ignore session changed event");
1193 case CONNMAN_SESSION_TRIGGER_SETTING:
1194 if (info->allowed_bearers != info_last->allowed_bearers) {
1196 service_hash_last = session->service_hash;
1197 service_list_last = session->service_list;
1199 populate_service_list(session);
1201 if (info->entry != NULL) {
1202 service_iter_last = g_hash_table_lookup(
1204 info->entry->service);
1205 service_iter = g_hash_table_lookup(
1206 session->service_hash,
1207 info->entry->service);
1210 if (service_iter == NULL && service_iter_last != NULL) {
1212 * The currently selected service is
1213 * not part of this session anymore.
1215 deselect_and_disconnect(session);
1218 g_hash_table_remove_all(service_hash_last);
1219 g_sequence_free(service_list_last);
1222 if (info->type != info_last->type) {
1223 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED &&
1224 is_type_matching_state(&info->state,
1225 info->type) == FALSE)
1226 deselect_and_disconnect(session);
1229 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED) {
1230 select_and_connect(session,
1231 CONNMAN_SESSION_REASON_FREE_RIDE);
1235 case CONNMAN_SESSION_TRIGGER_ECALL:
1237 * For the time beeing we fallback to normal connect
1240 case CONNMAN_SESSION_TRIGGER_CONNECT:
1241 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED) {
1242 if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
1244 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
1245 __connman_service_session_inc(info->entry->service);
1249 if (info->entry != NULL &&
1250 is_connecting(info->entry->state) == TRUE) {
1254 select_and_connect(session,
1255 CONNMAN_SESSION_REASON_CONNECT);
1258 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
1259 deselect_and_disconnect(session);
1262 case CONNMAN_SESSION_TRIGGER_SERVICE:
1263 if (info->entry != NULL &&
1264 (is_connecting(info->entry->state) == TRUE ||
1265 is_connected(info->entry->state) == TRUE)) {
1269 deselect_and_disconnect(session);
1271 if (info->reason == CONNMAN_SESSION_REASON_FREE_RIDE) {
1272 select_and_connect(session, info->reason);
1278 session_notify(session);
1281 static DBusMessage *connect_session(DBusConnection *conn,
1282 DBusMessage *msg, void *user_data)
1284 struct connman_session *session = user_data;
1286 DBG("session %p", session);
1288 if (ecall_session != NULL) {
1289 if (ecall_session->ecall == TRUE && ecall_session != session)
1290 return __connman_error_failed(msg, EBUSY);
1292 session->ecall = TRUE;
1293 session_changed(session, CONNMAN_SESSION_TRIGGER_ECALL);
1295 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1297 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1300 static DBusMessage *disconnect_session(DBusConnection *conn,
1301 DBusMessage *msg, void *user_data)
1303 struct connman_session *session = user_data;
1305 DBG("session %p", session);
1307 if (ecall_session != NULL) {
1308 if (ecall_session->ecall == TRUE && ecall_session != session)
1309 return __connman_error_failed(msg, EBUSY);
1311 session->ecall = FALSE;
1314 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1316 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1319 static DBusMessage *change_session(DBusConnection *conn,
1320 DBusMessage *msg, void *user_data)
1322 struct connman_session *session = user_data;
1323 struct session_info *info = session->info;
1324 DBusMessageIter iter, value;
1327 GSList *allowed_bearers;
1329 DBG("session %p", session);
1330 if (dbus_message_iter_init(msg, &iter) == FALSE)
1331 return __connman_error_invalid_arguments(msg);
1333 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1334 return __connman_error_invalid_arguments(msg);
1336 dbus_message_iter_get_basic(&iter, &name);
1337 dbus_message_iter_next(&iter);
1339 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1340 return __connman_error_invalid_arguments(msg);
1342 dbus_message_iter_recurse(&iter, &value);
1344 switch (dbus_message_iter_get_arg_type(&value)) {
1345 case DBUS_TYPE_ARRAY:
1346 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1347 allowed_bearers = session_parse_allowed_bearers(&value);
1349 g_slist_foreach(info->allowed_bearers,
1350 cleanup_bearer_info, NULL);
1351 g_slist_free(info->allowed_bearers);
1353 if (allowed_bearers == NULL) {
1354 allowed_bearers = session_allowed_bearers_any();
1356 if (allowed_bearers == NULL)
1357 return __connman_error_failed(msg, ENOMEM);
1360 info->allowed_bearers = allowed_bearers;
1365 case DBUS_TYPE_STRING:
1366 if (g_str_equal(name, "ConnectionType") == TRUE) {
1367 dbus_message_iter_get_basic(&value, &val);
1368 info->type = string2type(val);
1377 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1379 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1382 return __connman_error_invalid_arguments(msg);
1385 static void release_session(gpointer key, gpointer value, gpointer user_data)
1387 struct connman_session *session = value;
1388 DBusMessage *message;
1390 DBG("owner %s path %s", session->owner, session->notify_path);
1392 if (session->notify_watch > 0)
1393 g_dbus_remove_watch(connection, session->notify_watch);
1395 g_dbus_unregister_interface(connection, session->session_path,
1396 CONNMAN_SESSION_INTERFACE);
1398 message = dbus_message_new_method_call(session->owner,
1399 session->notify_path,
1400 CONNMAN_NOTIFICATION_INTERFACE,
1402 if (message == NULL)
1405 dbus_message_set_no_reply(message, TRUE);
1407 g_dbus_send_message(connection, message);
1410 static int session_disconnect(struct connman_session *session)
1412 DBG("session %p, %s", session, session->owner);
1414 if (session->notify_watch > 0)
1415 g_dbus_remove_watch(connection, session->notify_watch);
1417 g_dbus_unregister_interface(connection, session->session_path,
1418 CONNMAN_SESSION_INTERFACE);
1420 deselect_and_disconnect(session);
1422 g_hash_table_remove(session_hash, session->session_path);
1427 static void owner_disconnect(DBusConnection *conn, void *user_data)
1429 struct connman_session *session = user_data;
1431 DBG("session %p, %s died", session, session->owner);
1433 session_disconnect(session);
1436 static DBusMessage *destroy_session(DBusConnection *conn,
1437 DBusMessage *msg, void *user_data)
1439 struct connman_session *session = user_data;
1441 DBG("session %p", session);
1443 if (ecall_session != NULL && ecall_session != session)
1444 return __connman_error_failed(msg, EBUSY);
1446 session_disconnect(session);
1448 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1451 static const GDBusMethodTable session_methods[] = {
1452 { GDBUS_METHOD("Destroy", NULL, NULL, destroy_session) },
1453 { GDBUS_METHOD("Connect", NULL, NULL, connect_session) },
1454 { GDBUS_METHOD("Disconnect", NULL, NULL,
1455 disconnect_session ) },
1456 { GDBUS_METHOD("Change",
1457 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1458 NULL, change_session) },
1462 int __connman_session_create(DBusMessage *msg)
1464 const char *owner, *notify_path;
1465 char *session_path = NULL;
1466 DBusMessageIter iter, array;
1467 struct connman_session *session = NULL;
1468 struct session_info *info, *info_last;
1470 enum connman_session_type type = CONNMAN_SESSION_TYPE_ANY;
1471 connman_bool_t priority;
1472 connman_bool_t ecall_app;
1473 enum connman_session_roaming_policy roaming_policy;
1474 char *roaming_policy_str;
1475 GSList *allowed_bearers = NULL;
1478 owner = dbus_message_get_sender(msg);
1480 DBG("owner %s", owner);
1482 if (ecall_session != NULL && ecall_session->ecall == TRUE) {
1484 * If there is an emergency call already going on,
1485 * ignore session creation attempt
1491 dbus_message_iter_init(msg, &iter);
1492 dbus_message_iter_recurse(&iter, &array);
1494 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1495 DBusMessageIter entry, value;
1496 const char *key, *val;
1498 dbus_message_iter_recurse(&array, &entry);
1499 dbus_message_iter_get_basic(&entry, &key);
1501 dbus_message_iter_next(&entry);
1502 dbus_message_iter_recurse(&entry, &value);
1504 switch (dbus_message_iter_get_arg_type(&value)) {
1505 case DBUS_TYPE_ARRAY:
1506 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1508 session_parse_allowed_bearers(&value);
1513 case DBUS_TYPE_STRING:
1514 if (g_str_equal(key, "ConnectionType") == TRUE) {
1515 dbus_message_iter_get_basic(&value, &val);
1516 type = string2type(val);
1521 dbus_message_iter_next(&array);
1524 dbus_message_iter_next(&iter);
1525 dbus_message_iter_get_basic(&iter, ¬ify_path);
1527 if (notify_path == NULL) {
1532 session_path = g_strdup_printf("/sessions%s", notify_path);
1533 if (session_path == NULL) {
1538 session = g_hash_table_lookup(session_hash, session_path);
1539 if (session != NULL) {
1545 session = g_try_new0(struct connman_session, 1);
1546 if (session == NULL) {
1551 session->info = g_try_new0(struct session_info, 1);
1552 if (session->info == NULL) {
1557 session->info_last = g_try_new0(struct session_info, 1);
1558 if (session->info_last == NULL) {
1563 info = session->info;
1564 info_last = session->info_last;
1566 policy_get_bool(session, owner, "Priority", &priority);
1567 policy_get_bool(session, owner, "EmergencyCall", &ecall_app);
1568 policy_get_string(session, owner, "RoamingPolicy", &roaming_policy_str);
1569 roaming_policy = string2roamingpolicy(roaming_policy_str);
1571 session->owner = g_strdup(owner);
1572 session->session_path = session_path;
1573 session->notify_path = g_strdup(notify_path);
1574 session->notify_watch =
1575 g_dbus_add_disconnect_watch(connection, session->owner,
1576 owner_disconnect, session, NULL);
1577 if (ecall_app == TRUE)
1578 ecall_session = session;
1580 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1582 info->priority = priority;
1583 info->roaming_policy = roaming_policy;
1586 if (allowed_bearers == NULL) {
1587 info->allowed_bearers =
1588 session_allowed_bearers_any();
1590 if (info->allowed_bearers == NULL) {
1595 info->allowed_bearers = allowed_bearers;
1598 g_hash_table_replace(session_hash, session->session_path, session);
1600 DBG("add %s", session->session_path);
1602 if (g_dbus_register_interface(connection, session->session_path,
1603 CONNMAN_SESSION_INTERFACE,
1604 session_methods, NULL,
1605 NULL, session, NULL) == FALSE) {
1606 connman_error("Failed to register %s", session->session_path);
1607 g_hash_table_remove(session_hash, session->session_path);
1614 g_dbus_send_reply(connection, msg,
1615 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1619 populate_service_list(session);
1621 info_last->state = info->state;
1622 info_last->priority = info->priority;
1623 info_last->roaming_policy = info->roaming_policy;
1624 info_last->entry = info->entry;
1625 info_last->allowed_bearers = info->allowed_bearers;
1627 session->append_all = TRUE;
1629 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1634 connman_error("Failed to create session");
1636 if (session != NULL) {
1637 if (session->info_last != NULL)
1638 g_free(session->info_last);
1639 if (session->info != NULL)
1640 g_free(session->info);
1644 g_free(session_path);
1646 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1647 g_slist_free(allowed_bearers);
1652 int __connman_session_destroy(DBusMessage *msg)
1654 const char *owner, *session_path;
1655 struct connman_session *session;
1657 owner = dbus_message_get_sender(msg);
1659 DBG("owner %s", owner);
1661 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1663 if (session_path == NULL)
1666 session = g_hash_table_lookup(session_hash, session_path);
1667 if (session == NULL)
1670 if (g_strcmp0(owner, session->owner) != 0)
1673 session_disconnect(session);
1678 connman_bool_t __connman_session_mode()
1683 void __connman_session_set_mode(connman_bool_t enable)
1685 DBG("enable %d", enable);
1687 if (sessionmode != enable) {
1688 sessionmode = enable;
1690 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
1691 CONNMAN_MANAGER_INTERFACE, "SessionMode",
1692 DBUS_TYPE_BOOLEAN, &sessionmode);
1695 if (sessionmode == TRUE)
1696 __connman_service_disconnect_all();
1699 static void service_add(struct connman_service *service,
1702 GHashTableIter iter;
1703 GSequenceIter *iter_service_list;
1704 gpointer key, value;
1705 struct connman_session *session;
1706 struct service_entry *entry;
1708 DBG("service %p", service);
1710 g_hash_table_iter_init(&iter, session_hash);
1712 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1715 if (service_match(session, service) == FALSE)
1718 entry = create_service_entry(service, name,
1719 CONNMAN_SERVICE_STATE_IDLE);
1724 g_sequence_insert_sorted(session->service_list,
1725 entry, sort_services,
1728 g_hash_table_replace(session->service_hash, service,
1731 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1735 static void service_remove(struct connman_service *service)
1738 GHashTableIter iter;
1739 gpointer key, value;
1740 struct connman_session *session;
1741 struct session_info *info;
1743 DBG("service %p", service);
1745 g_hash_table_iter_init(&iter, session_hash);
1747 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1748 GSequenceIter *seq_iter;
1750 info = session->info;
1752 seq_iter = g_hash_table_lookup(session->service_hash, service);
1753 if (seq_iter == NULL)
1756 g_sequence_remove(seq_iter);
1758 if (info->entry != NULL && info->entry->service == service)
1760 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1764 static void service_state_changed(struct connman_service *service,
1765 enum connman_service_state state)
1767 GHashTableIter iter;
1768 gpointer key, value;
1770 DBG("service %p state %d", service, state);
1772 g_hash_table_iter_init(&iter, session_hash);
1774 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1775 struct connman_session *session = value;
1776 GSequenceIter *service_iter;
1778 service_iter = g_hash_table_lookup(session->service_hash, service);
1779 if (service_iter != NULL) {
1780 struct service_entry *entry;
1782 entry = g_sequence_get(service_iter);
1783 entry->state = state;
1786 session_changed(session,
1787 CONNMAN_SESSION_TRIGGER_SERVICE);
1791 static void ipconfig_changed(struct connman_service *service,
1792 struct connman_ipconfig *ipconfig)
1794 GHashTableIter iter;
1795 gpointer key, value;
1796 struct connman_session *session;
1797 struct session_info *info;
1798 enum connman_ipconfig_type type;
1800 DBG("service %p ipconfig %p", service, ipconfig);
1802 type = __connman_ipconfig_get_config_type(ipconfig);
1804 g_hash_table_iter_init(&iter, session_hash);
1806 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1808 info = session->info;
1810 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
1813 if (info->entry != NULL && info->entry->service == service) {
1814 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1815 ipconfig_ipv4_changed(session);
1816 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1817 ipconfig_ipv6_changed(session);
1822 static struct connman_notifier session_notifier = {
1824 .service_add = service_add,
1825 .service_remove = service_remove,
1826 .service_state_changed = service_state_changed,
1827 .ipconfig_changed = ipconfig_changed,
1830 int __connman_session_init(void)
1836 connection = connman_dbus_get_connection();
1837 if (connection == NULL)
1840 err = connman_notifier_register(&session_notifier);
1842 dbus_connection_unref(connection);
1846 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1847 NULL, cleanup_session);
1849 sessionmode = FALSE;
1853 void __connman_session_cleanup(void)
1857 if (connection == NULL)
1860 connman_notifier_unregister(&session_notifier);
1862 g_hash_table_foreach(session_hash, release_session, NULL);
1863 g_hash_table_destroy(session_hash);
1864 session_hash = NULL;
1866 dbus_connection_unref(connection);