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;
39 static GSList *policy_list;
41 enum connman_session_trigger {
42 CONNMAN_SESSION_TRIGGER_UNKNOWN = 0,
43 CONNMAN_SESSION_TRIGGER_SETTING = 1,
44 CONNMAN_SESSION_TRIGGER_CONNECT = 2,
45 CONNMAN_SESSION_TRIGGER_DISCONNECT = 3,
46 CONNMAN_SESSION_TRIGGER_SERVICE = 4,
47 CONNMAN_SESSION_TRIGGER_ECALL = 5,
50 enum connman_session_reason {
51 CONNMAN_SESSION_REASON_UNKNOWN = 0,
52 CONNMAN_SESSION_REASON_CONNECT = 1,
53 CONNMAN_SESSION_REASON_FREE_RIDE = 2,
56 enum connman_session_state {
57 CONNMAN_SESSION_STATE_DISCONNECTED = 0,
58 CONNMAN_SESSION_STATE_CONNECTED = 1,
59 CONNMAN_SESSION_STATE_ONLINE = 2,
62 struct service_entry {
63 /* track why this service was selected */
64 enum connman_session_reason reason;
65 enum connman_service_state state;
67 struct connman_service *service;
70 GSList *pending_timeouts;
74 struct connman_session_config config;
75 enum connman_session_state state;
76 struct service_entry *entry;
77 enum connman_session_reason reason;
80 struct connman_session {
86 struct connman_session_policy *policy;
88 connman_bool_t append_all;
89 struct session_info *info;
90 struct session_info *info_last;
91 struct connman_session_config *policy_config;
95 GSequence *service_list;
96 GHashTable *service_hash;
99 static const char *trigger2string(enum connman_session_trigger trigger)
102 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
104 case CONNMAN_SESSION_TRIGGER_SETTING:
106 case CONNMAN_SESSION_TRIGGER_CONNECT:
108 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
110 case CONNMAN_SESSION_TRIGGER_SERVICE:
112 case CONNMAN_SESSION_TRIGGER_ECALL:
119 static const char *reason2string(enum connman_session_reason reason)
122 case CONNMAN_SESSION_REASON_UNKNOWN:
124 case CONNMAN_SESSION_REASON_CONNECT:
126 case CONNMAN_SESSION_REASON_FREE_RIDE:
133 static const char *state2string(enum connman_session_state state)
136 case CONNMAN_SESSION_STATE_DISCONNECTED:
137 return "disconnected";
138 case CONNMAN_SESSION_STATE_CONNECTED:
140 case CONNMAN_SESSION_STATE_ONLINE:
147 static const char *type2string(enum connman_session_type type)
150 case CONNMAN_SESSION_TYPE_UNKNOWN:
152 case CONNMAN_SESSION_TYPE_ANY:
154 case CONNMAN_SESSION_TYPE_LOCAL:
156 case CONNMAN_SESSION_TYPE_INTERNET:
163 static enum connman_session_type string2type(const char *type)
165 if (g_strcmp0(type, "any") == 0)
166 return CONNMAN_SESSION_TYPE_ANY;
167 if (g_strcmp0(type, "local") == 0)
168 return CONNMAN_SESSION_TYPE_LOCAL;
169 else if (g_strcmp0(type, "internet") == 0)
170 return CONNMAN_SESSION_TYPE_INTERNET;
172 return CONNMAN_SESSION_TYPE_UNKNOWN;
175 static int bearer2service(const char *bearer, enum connman_service_type *type)
177 if (g_strcmp0(bearer, "ethernet") == 0)
178 *type = CONNMAN_SERVICE_TYPE_ETHERNET;
179 else if (g_strcmp0(bearer, "wifi") == 0)
180 *type = CONNMAN_SERVICE_TYPE_WIFI;
181 else if (g_strcmp0(bearer, "wimax") == 0)
182 *type = CONNMAN_SERVICE_TYPE_WIMAX;
183 else if (g_strcmp0(bearer, "bluetooth") == 0)
184 *type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
185 else if (g_strcmp0(bearer, "cellular") == 0)
186 *type = CONNMAN_SERVICE_TYPE_CELLULAR;
187 else if (g_strcmp0(bearer, "vpn") == 0)
188 *type = CONNMAN_SERVICE_TYPE_VPN;
189 else if (g_strcmp0(bearer, "*") == 0)
190 *type = CONNMAN_SERVICE_TYPE_UNKNOWN;
197 static char *service2bearer(enum connman_service_type type)
200 case CONNMAN_SERVICE_TYPE_ETHERNET:
202 case CONNMAN_SERVICE_TYPE_WIFI:
204 case CONNMAN_SERVICE_TYPE_WIMAX:
206 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
208 case CONNMAN_SERVICE_TYPE_CELLULAR:
210 case CONNMAN_SERVICE_TYPE_VPN:
212 case CONNMAN_SERVICE_TYPE_UNKNOWN:
213 case CONNMAN_SERVICE_TYPE_SYSTEM:
214 case CONNMAN_SERVICE_TYPE_GPS:
215 case CONNMAN_SERVICE_TYPE_GADGET:
222 static int assign_policy_plugin(struct connman_session *session)
224 if (session->policy != NULL)
227 if (policy_list == NULL)
230 session->policy = policy_list->data;
235 static int create_policy_config(struct connman_session *session,
236 connman_session_config_cb callback,
239 struct connman_session_config *config;
241 if (session->policy == NULL) {
242 config = connman_session_create_default_config();
246 session->policy_config = config;
251 return (*session->policy->create)(session, callback, user_data);
254 static void destroy_policy_config(struct connman_session *session)
256 if (session->policy == NULL) {
257 connman_session_free_bearers(
258 session->policy_config->allowed_bearers);
259 g_free(session->policy_config);
261 (*session->policy->destroy)(session);
265 static void probe_policy(struct connman_session_policy *policy)
270 struct connman_session *session;
272 DBG("policy %p name %s", policy, policy->name);
274 g_hash_table_iter_init(&iter, session_hash);
276 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
279 if (session->policy != NULL)
282 assign_policy_plugin(session);
286 static void remove_policy(struct connman_session_policy *policy)
290 struct connman_session *session;
292 DBG("policy %p name %s", policy, policy->name);
294 g_hash_table_iter_init(&iter, session_hash);
296 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
299 if (session->policy != policy)
302 session->policy = NULL;
303 assign_policy_plugin(session);
307 static gint compare_priority(gconstpointer a, gconstpointer b)
309 const struct connman_session_policy *policy1 = a;
310 const struct connman_session_policy *policy2 = b;
312 return policy2->priority - policy1->priority;
316 int connman_session_policy_register(struct connman_session_policy *policy)
318 DBG("name %s", policy->name);
320 if (policy->create == NULL || policy->destroy == NULL)
323 policy_list = g_slist_insert_sorted(policy_list, policy,
326 probe_policy(policy);
331 void connman_session_policy_unregister(struct connman_session_policy *policy)
333 DBG("name %s", policy->name);
335 policy_list = g_slist_remove(policy_list, policy);
337 remove_policy(policy);
340 struct connman_session_config *connman_session_create_default_config(void)
342 struct connman_session_config *config;
344 config = g_try_new0(struct connman_session_config, 1);
348 config->priority = FALSE;
349 config->roaming_policy = CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
350 config->type = CONNMAN_SESSION_TYPE_ANY;
351 config->ecall = FALSE;
352 config->allowed_bearers = connman_session_allowed_bearers_any();
353 if (config->allowed_bearers == NULL) {
361 static enum connman_session_type apply_policy_on_type(
362 enum connman_session_type policy,
363 enum connman_session_type type)
365 if (type == CONNMAN_SESSION_TYPE_UNKNOWN)
366 return CONNMAN_SESSION_TYPE_UNKNOWN;
368 if (policy == CONNMAN_SESSION_TYPE_ANY)
371 if (policy == CONNMAN_SESSION_TYPE_LOCAL)
372 return CONNMAN_SESSION_TYPE_LOCAL;
374 return CONNMAN_SESSION_TYPE_INTERNET;
377 static void cleanup_bearer(gpointer data)
379 struct connman_session_bearer *bearer = data;
384 void connman_session_free_bearers(GSList *bearers)
386 g_slist_free_full(bearers, cleanup_bearer);
389 static int session_parse_allowed_bearers(DBusMessageIter *iter, GSList **list)
391 struct connman_session_bearer *bearer;
392 DBusMessageIter array;
395 dbus_message_iter_recurse(iter, &array);
399 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
400 char *bearer_name = NULL;
402 dbus_message_iter_get_basic(&array, &bearer_name);
404 bearer = g_try_new0(struct connman_session_bearer, 1);
405 if (bearer == NULL) {
406 connman_session_free_bearers(*list);
411 err = bearer2service(bearer_name, &bearer->service_type);
413 connman_session_free_bearers(*list);
418 *list = g_slist_append(*list, bearer);
420 dbus_message_iter_next(&array);
426 static struct connman_session_bearer *clone_bearer(
427 struct connman_session_bearer *orig)
429 struct connman_session_bearer *bearer;
431 bearer = g_try_new0(struct connman_session_bearer, 1);
435 bearer->service_type = orig->service_type;
440 static int filter_bearer(GSList *policy_bearers,
441 struct connman_session_bearer *bearer,
444 struct connman_session_bearer *policy, *tmp;
447 if (policy_bearers == NULL)
450 for (it = policy_bearers; it != NULL; it = it->next) {
453 if (policy->service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
454 bearer->service_type != policy->service_type)
465 tmp = clone_bearer(bearer);
467 connman_session_free_bearers(*list);
471 *list = g_slist_append(*list, tmp);
476 static int apply_policy_on_bearers(GSList *policy_bearers, GSList *bearers,
479 struct connman_session_bearer *bearer;
485 for (it = bearers; it != NULL; it = it->next) {
488 err = filter_bearer(policy_bearers, bearer, list);
496 GSList *connman_session_allowed_bearers_any(void)
498 struct connman_session_bearer *bearer;
501 bearer = g_try_new0(struct connman_session_bearer, 1);
505 bearer->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
507 list = g_slist_append(list, bearer);
512 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
514 struct session_info *info = user_data;
517 for (list = info->config.allowed_bearers;
518 list != NULL; list = list->next) {
519 struct connman_session_bearer *bearer = list->data;
521 __connman_service_type2string(bearer->service_type);
526 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
531 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
533 struct connman_service *service = user_data;
534 struct connman_ipconfig *ipconfig_ipv4;
539 if (__connman_service_is_connected_state(service,
540 CONNMAN_IPCONFIG_TYPE_IPV4) == FALSE) {
544 ipconfig_ipv4 = __connman_service_get_ip4config(service);
545 if (ipconfig_ipv4 == NULL)
548 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
551 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
553 struct connman_service *service = user_data;
554 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
559 if (__connman_service_is_connected_state(service,
560 CONNMAN_IPCONFIG_TYPE_IPV6) == FALSE) {
564 ipconfig_ipv4 = __connman_service_get_ip4config(service);
565 ipconfig_ipv6 = __connman_service_get_ip6config(service);
566 if (ipconfig_ipv6 == NULL)
569 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
572 static void append_notify(DBusMessageIter *dict,
573 struct connman_session *session)
575 struct session_info *info = session->info;
576 struct session_info *info_last = session->info_last;
577 struct connman_service *service;
578 const char *name, *ifname, *bearer;
580 if (session->append_all == TRUE ||
581 info->state != info_last->state) {
582 const char *state = state2string(info->state);
584 connman_dbus_dict_append_basic(dict, "State",
587 info_last->state = info->state;
590 if (session->append_all == TRUE ||
591 info->entry != info_last->entry) {
592 if (info->entry == NULL) {
598 name = info->entry->name;
599 ifname = info->entry->ifname;
600 service = info->entry->service;
601 bearer = info->entry->bearer;
604 connman_dbus_dict_append_basic(dict, "Name",
608 connman_dbus_dict_append_dict(dict, "IPv4",
609 append_ipconfig_ipv4,
612 connman_dbus_dict_append_dict(dict, "IPv6",
613 append_ipconfig_ipv6,
616 connman_dbus_dict_append_basic(dict, "Interface",
620 connman_dbus_dict_append_basic(dict, "Bearer",
624 info_last->entry = info->entry;
627 if (session->append_all == TRUE ||
628 info->config.type != info_last->config.type) {
629 const char *type = type2string(info->config.type);
631 connman_dbus_dict_append_basic(dict, "ConnectionType",
634 info_last->config.type = info->config.type;
637 if (session->append_all == TRUE ||
638 info->config.allowed_bearers != info_last->config.allowed_bearers) {
639 connman_dbus_dict_append_array(dict, "AllowedBearers",
641 append_allowed_bearers,
643 info_last->config.allowed_bearers = info->config.allowed_bearers;
646 session->append_all = FALSE;
649 static connman_bool_t is_type_matching_state(enum connman_session_state *state,
650 enum connman_session_type type)
653 case CONNMAN_SESSION_TYPE_UNKNOWN:
655 case CONNMAN_SESSION_TYPE_ANY:
657 case CONNMAN_SESSION_TYPE_LOCAL:
658 if (*state >= CONNMAN_SESSION_STATE_CONNECTED) {
659 *state = CONNMAN_SESSION_STATE_CONNECTED;
664 case CONNMAN_SESSION_TYPE_INTERNET:
665 if (*state == CONNMAN_SESSION_STATE_ONLINE)
673 static connman_bool_t compute_notifiable_changes(struct connman_session *session)
675 struct session_info *info_last = session->info_last;
676 struct session_info *info = session->info;
678 if (session->append_all == TRUE)
681 if (info->state != info_last->state)
684 if (info->entry != info_last->entry &&
685 info->state >= CONNMAN_SESSION_STATE_CONNECTED)
688 if (info->config.allowed_bearers != info_last->config.allowed_bearers ||
689 info->config.type != info_last->config.type)
695 static gboolean session_notify(gpointer user_data)
697 struct connman_session *session = user_data;
699 DBusMessageIter array, dict;
701 if (compute_notifiable_changes(session) == FALSE)
704 DBG("session %p owner %s notify_path %s", session,
705 session->owner, session->notify_path);
707 msg = dbus_message_new_method_call(session->owner, session->notify_path,
708 CONNMAN_NOTIFICATION_INTERFACE,
713 dbus_message_iter_init_append(msg, &array);
714 connman_dbus_dict_open(&array, &dict);
716 append_notify(&dict, session);
718 connman_dbus_dict_close(&array, &dict);
720 g_dbus_send_message(connection, msg);
725 static void ipconfig_ipv4_changed(struct connman_session *session)
727 struct session_info *info = session->info;
729 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
730 "IPv4", append_ipconfig_ipv4,
731 info->entry->service);
734 static void ipconfig_ipv6_changed(struct connman_session *session)
736 struct session_info *info = session->info;
738 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
739 "IPv6", append_ipconfig_ipv6,
740 info->entry->service);
743 static connman_bool_t service_type_match(struct connman_session *session,
744 struct connman_service *service)
746 struct session_info *info = session->info;
749 for (list = info->config.allowed_bearers;
750 list != NULL; list = list->next) {
751 struct connman_session_bearer *bearer = list->data;
752 enum connman_service_type service_type;
754 if (bearer->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN)
757 service_type = connman_service_get_type(service);
758 if (bearer->service_type == service_type)
765 static connman_bool_t service_match(struct connman_session *session,
766 struct connman_service *service)
768 if (service_type_match(session, service) == FALSE)
774 static int service_type_weight(enum connman_service_type type)
777 * The session doesn't care which service
778 * to use. Nevertheless we have to sort them
779 * according their type. The ordering is
788 case CONNMAN_SERVICE_TYPE_ETHERNET:
790 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
792 case CONNMAN_SERVICE_TYPE_WIFI:
793 case CONNMAN_SERVICE_TYPE_WIMAX:
795 case CONNMAN_SERVICE_TYPE_CELLULAR:
797 case CONNMAN_SERVICE_TYPE_UNKNOWN:
798 case CONNMAN_SERVICE_TYPE_SYSTEM:
799 case CONNMAN_SERVICE_TYPE_GPS:
800 case CONNMAN_SERVICE_TYPE_VPN:
801 case CONNMAN_SERVICE_TYPE_GADGET:
808 static gint sort_allowed_bearers(struct connman_service *service_a,
809 struct connman_service *service_b,
810 struct connman_session *session)
812 struct session_info *info = session->info;
814 enum connman_service_type type_a, type_b;
815 int weight_a, weight_b;
817 type_a = connman_service_get_type(service_a);
818 type_b = connman_service_get_type(service_b);
820 for (list = info->config.allowed_bearers;
821 list != NULL; list = list->next) {
822 struct connman_session_bearer *bearer = list->data;
824 if (bearer->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN) {
825 if (type_a != type_b) {
826 weight_a = service_type_weight(type_a);
827 weight_b = service_type_weight(type_b);
829 if (weight_a > weight_b)
832 if (weight_a < weight_b)
839 if (type_a == bearer->service_type &&
840 type_b == bearer->service_type) {
844 if (type_a == bearer->service_type &&
845 type_b != bearer->service_type) {
849 if (type_a != bearer->service_type &&
850 type_b == bearer->service_type) {
858 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
860 struct service_entry *entry_a = (void *)a;
861 struct service_entry *entry_b = (void *)b;
862 struct connman_session *session = user_data;
864 return sort_allowed_bearers(entry_a->service, entry_b->service,
868 static void free_session(struct connman_session *session)
870 destroy_policy_config(session);
871 connman_session_free_bearers(session->info->config.allowed_bearers);
872 g_free(session->owner);
873 g_free(session->session_path);
874 g_free(session->notify_path);
875 g_free(session->info);
876 g_free(session->info_last);
881 static void cleanup_session(gpointer user_data)
883 struct connman_session *session = user_data;
884 struct session_info *info = session->info;
886 DBG("remove %s", session->session_path);
888 g_hash_table_destroy(session->service_hash);
889 g_sequence_free(session->service_list);
891 if (info->entry != NULL &&
892 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
893 __connman_service_disconnect(info->entry->service);
896 free_session(session);
899 static enum connman_session_state service_to_session_state(enum connman_service_state state)
902 case CONNMAN_SERVICE_STATE_UNKNOWN:
903 case CONNMAN_SERVICE_STATE_IDLE:
904 case CONNMAN_SERVICE_STATE_ASSOCIATION:
905 case CONNMAN_SERVICE_STATE_CONFIGURATION:
906 case CONNMAN_SERVICE_STATE_DISCONNECT:
907 case CONNMAN_SERVICE_STATE_FAILURE:
909 case CONNMAN_SERVICE_STATE_READY:
910 return CONNMAN_SESSION_STATE_CONNECTED;
911 case CONNMAN_SERVICE_STATE_ONLINE:
912 return CONNMAN_SESSION_STATE_ONLINE;
915 return CONNMAN_SESSION_STATE_DISCONNECTED;
918 static connman_bool_t is_connected(enum connman_service_state state)
921 case CONNMAN_SERVICE_STATE_UNKNOWN:
922 case CONNMAN_SERVICE_STATE_IDLE:
923 case CONNMAN_SERVICE_STATE_ASSOCIATION:
924 case CONNMAN_SERVICE_STATE_CONFIGURATION:
925 case CONNMAN_SERVICE_STATE_DISCONNECT:
926 case CONNMAN_SERVICE_STATE_FAILURE:
928 case CONNMAN_SERVICE_STATE_READY:
929 case CONNMAN_SERVICE_STATE_ONLINE:
936 static connman_bool_t is_connecting(enum connman_service_state state)
939 case CONNMAN_SERVICE_STATE_UNKNOWN:
940 case CONNMAN_SERVICE_STATE_IDLE:
942 case CONNMAN_SERVICE_STATE_ASSOCIATION:
943 case CONNMAN_SERVICE_STATE_CONFIGURATION:
945 case CONNMAN_SERVICE_STATE_DISCONNECT:
946 case CONNMAN_SERVICE_STATE_FAILURE:
947 case CONNMAN_SERVICE_STATE_READY:
948 case CONNMAN_SERVICE_STATE_ONLINE:
955 static connman_bool_t explicit_connect(enum connman_session_reason reason)
958 case CONNMAN_SESSION_REASON_UNKNOWN:
959 case CONNMAN_SESSION_REASON_FREE_RIDE:
961 case CONNMAN_SESSION_REASON_CONNECT:
968 static connman_bool_t explicit_disconnect(struct session_info *info)
970 if (info->entry == NULL)
973 DBG("reason %s service %p state %d",
974 reason2string(info->entry->reason),
975 info->entry->service, info->entry->state);
977 if (info->entry->reason == CONNMAN_SESSION_REASON_UNKNOWN)
980 if (explicit_connect(info->entry->reason) == FALSE)
983 if (__connman_service_session_dec(info->entry->service) == FALSE)
989 struct pending_data {
990 unsigned int timeout;
991 struct service_entry *entry;
992 gboolean (*cb)(gpointer);
995 static void pending_timeout_free(gpointer data, gpointer user_data)
997 struct pending_data *pending = data;
999 DBG("pending %p timeout %d", pending, pending->timeout);
1000 g_source_remove(pending->timeout);
1004 static void pending_timeout_remove_all(struct service_entry *entry)
1008 g_slist_foreach(entry->pending_timeouts, pending_timeout_free, NULL);
1009 g_slist_free(entry->pending_timeouts);
1010 entry->pending_timeouts = NULL;
1013 static gboolean pending_timeout_cb(gpointer data)
1015 struct pending_data *pending = data;
1016 struct service_entry *entry = pending->entry;
1019 DBG("pending %p timeout %d", pending, pending->timeout);
1021 ret = pending->cb(pending->entry);
1023 entry->pending_timeouts =
1024 g_slist_remove(entry->pending_timeouts,
1031 static connman_bool_t pending_timeout_add(unsigned int seconds,
1032 gboolean (*cb)(gpointer),
1033 struct service_entry *entry)
1035 struct pending_data *pending = g_try_new0(struct pending_data, 1);
1037 if (pending == NULL || cb == NULL || entry == NULL) {
1043 pending->entry = entry;
1044 pending->timeout = g_timeout_add_seconds(seconds, pending_timeout_cb,
1046 entry->pending_timeouts = g_slist_prepend(entry->pending_timeouts,
1049 DBG("pending %p entry %p timeout id %d", pending, entry,
1055 static gboolean call_disconnect(gpointer user_data)
1057 struct service_entry *entry = user_data;
1058 struct connman_service *service = entry->service;
1061 * TODO: We should mark this entry as pending work. In case
1062 * disconnect fails we just unassign this session from the
1063 * service and can't do anything later on it
1065 DBG("disconnect service %p", service);
1066 __connman_service_disconnect(service);
1071 static gboolean call_connect(gpointer user_data)
1073 struct service_entry *entry = user_data;
1074 struct connman_service *service = entry->service;
1076 DBG("connect service %p", service);
1077 __connman_service_connect(service);
1082 static void deselect_service(struct session_info *info)
1084 struct service_entry *entry;
1085 connman_bool_t disconnect, connected;
1089 if (info->entry == NULL)
1092 disconnect = explicit_disconnect(info);
1094 connected = is_connecting(info->entry->state) == TRUE ||
1095 is_connected(info->entry->state) == TRUE;
1097 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1098 info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1100 entry = info->entry;
1103 DBG("disconnect %d connected %d", disconnect, connected);
1105 if (disconnect == TRUE && connected == TRUE)
1106 pending_timeout_add(0, call_disconnect, entry);
1109 static void deselect_and_disconnect(struct connman_session *session)
1111 struct session_info *info = session->info;
1113 deselect_service(info);
1115 info->reason = CONNMAN_SESSION_REASON_FREE_RIDE;
1118 static void select_connected_service(struct session_info *info,
1119 struct service_entry *entry)
1121 enum connman_session_state state;
1123 state = service_to_session_state(entry->state);
1124 if (is_type_matching_state(&state, info->config.type) == FALSE)
1127 info->state = state;
1129 info->entry = entry;
1130 info->entry->reason = info->reason;
1132 if (explicit_connect(info->reason) == FALSE)
1135 __connman_service_session_inc(info->entry->service);
1138 static void select_offline_service(struct session_info *info,
1139 struct service_entry *entry)
1141 if (explicit_connect(info->reason) == FALSE)
1144 info->state = service_to_session_state(entry->state);
1146 info->entry = entry;
1147 info->entry->reason = info->reason;
1149 __connman_service_session_inc(info->entry->service);
1150 pending_timeout_add(0, call_connect, entry);
1153 static void select_service(struct session_info *info,
1154 struct service_entry *entry)
1156 DBG("service %p", entry->service);
1158 if (is_connected(entry->state) == TRUE)
1159 select_connected_service(info, entry);
1161 select_offline_service(info, entry);
1164 static void select_and_connect(struct connman_session *session,
1165 enum connman_session_reason reason)
1167 struct session_info *info = session->info;
1168 struct service_entry *entry = NULL;
1169 GSequenceIter *iter;
1171 DBG("session %p reason %s", session, reason2string(reason));
1173 info->reason = reason;
1175 iter = g_sequence_get_begin_iter(session->service_list);
1177 while (g_sequence_iter_is_end(iter) == FALSE) {
1178 entry = g_sequence_get(iter);
1180 switch (entry->state) {
1181 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1182 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1183 case CONNMAN_SERVICE_STATE_READY:
1184 case CONNMAN_SERVICE_STATE_ONLINE:
1185 case CONNMAN_SERVICE_STATE_IDLE:
1186 case CONNMAN_SERVICE_STATE_DISCONNECT:
1187 select_service(info, entry);
1189 case CONNMAN_SERVICE_STATE_UNKNOWN:
1190 case CONNMAN_SERVICE_STATE_FAILURE:
1194 iter = g_sequence_iter_next(iter);
1198 static struct service_entry *create_service_entry(struct connman_service *service,
1200 enum connman_service_state state)
1202 struct service_entry *entry;
1203 enum connman_service_type type;
1206 entry = g_try_new0(struct service_entry, 1);
1210 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1211 entry->state = state;
1216 entry->service = service;
1218 idx = __connman_service_get_index(entry->service);
1219 entry->ifname = connman_inet_ifname(idx);
1220 if (entry->ifname == NULL)
1221 entry->ifname = g_strdup("");
1223 type = connman_service_get_type(entry->service);
1224 entry->bearer = service2bearer(type);
1229 static void destroy_service_entry(gpointer data)
1231 struct service_entry *entry = data;
1233 pending_timeout_remove_all(entry);
1234 g_free(entry->ifname);
1239 static void populate_service_list(struct connman_session *session)
1241 struct service_entry *entry;
1242 GSequenceIter *iter;
1244 session->service_hash =
1245 g_hash_table_new_full(g_direct_hash, g_direct_equal,
1247 session->service_list = __connman_service_get_list(session,
1249 create_service_entry,
1250 destroy_service_entry);
1252 g_sequence_sort(session->service_list, sort_services, session);
1254 iter = g_sequence_get_begin_iter(session->service_list);
1256 while (g_sequence_iter_is_end(iter) == FALSE) {
1257 entry = g_sequence_get(iter);
1259 DBG("service %p type %s name %s", entry->service,
1260 service2bearer(connman_service_get_type(entry->service)),
1263 g_hash_table_replace(session->service_hash,
1264 entry->service, iter);
1266 iter = g_sequence_iter_next(iter);
1270 static void session_changed(struct connman_session *session,
1271 enum connman_session_trigger trigger)
1273 struct session_info *info = session->info;
1274 struct session_info *info_last = session->info_last;
1275 GSequenceIter *service_iter = NULL, *service_iter_last = NULL;
1276 GSequence *service_list_last;
1277 GHashTable *service_hash_last;
1280 * TODO: This only a placeholder for the 'real' algorithm to
1281 * play a bit around. So we are going to improve it step by step.
1284 DBG("session %p trigger %s reason %s", session, trigger2string(trigger),
1285 reason2string(info->reason));
1287 if (info->entry != NULL) {
1288 enum connman_session_state state;
1290 state = service_to_session_state(info->entry->state);
1292 if (is_type_matching_state(&state, info->config.type) == TRUE)
1293 info->state = state;
1297 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
1298 DBG("ignore session changed event");
1300 case CONNMAN_SESSION_TRIGGER_SETTING:
1301 if (info->config.allowed_bearers != info_last->config.allowed_bearers) {
1303 service_hash_last = session->service_hash;
1304 service_list_last = session->service_list;
1306 populate_service_list(session);
1308 if (info->entry != NULL) {
1309 service_iter_last = g_hash_table_lookup(
1311 info->entry->service);
1312 service_iter = g_hash_table_lookup(
1313 session->service_hash,
1314 info->entry->service);
1317 if (service_iter == NULL && service_iter_last != NULL) {
1319 * The currently selected service is
1320 * not part of this session anymore.
1322 deselect_and_disconnect(session);
1325 g_hash_table_remove_all(service_hash_last);
1326 g_sequence_free(service_list_last);
1329 if (info->config.type != info_last->config.type) {
1330 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED &&
1331 is_type_matching_state(&info->state,
1332 info->config.type) == FALSE)
1333 deselect_and_disconnect(session);
1336 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED) {
1337 select_and_connect(session,
1338 CONNMAN_SESSION_REASON_FREE_RIDE);
1342 case CONNMAN_SESSION_TRIGGER_ECALL:
1344 * For the time beeing we fallback to normal connect
1347 case CONNMAN_SESSION_TRIGGER_CONNECT:
1348 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED) {
1349 if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
1351 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
1352 __connman_service_session_inc(info->entry->service);
1356 if (info->entry != NULL &&
1357 is_connecting(info->entry->state) == TRUE) {
1361 select_and_connect(session,
1362 CONNMAN_SESSION_REASON_CONNECT);
1365 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
1366 deselect_and_disconnect(session);
1369 case CONNMAN_SESSION_TRIGGER_SERVICE:
1370 if (info->entry != NULL &&
1371 (is_connecting(info->entry->state) == TRUE ||
1372 is_connected(info->entry->state) == TRUE)) {
1376 deselect_and_disconnect(session);
1378 if (info->reason == CONNMAN_SESSION_REASON_FREE_RIDE) {
1379 select_and_connect(session, info->reason);
1385 session_notify(session);
1388 static DBusMessage *connect_session(DBusConnection *conn,
1389 DBusMessage *msg, void *user_data)
1391 struct connman_session *session = user_data;
1393 DBG("session %p", session);
1395 if (ecall_session != NULL) {
1396 if (ecall_session->ecall == TRUE && ecall_session != session)
1397 return __connman_error_failed(msg, EBUSY);
1399 session->ecall = TRUE;
1400 session_changed(session, CONNMAN_SESSION_TRIGGER_ECALL);
1402 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1404 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1407 static DBusMessage *disconnect_session(DBusConnection *conn,
1408 DBusMessage *msg, void *user_data)
1410 struct connman_session *session = user_data;
1412 DBG("session %p", session);
1414 if (ecall_session != NULL) {
1415 if (ecall_session->ecall == TRUE && ecall_session != session)
1416 return __connman_error_failed(msg, EBUSY);
1418 session->ecall = FALSE;
1421 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1423 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1426 static DBusMessage *change_session(DBusConnection *conn,
1427 DBusMessage *msg, void *user_data)
1429 struct connman_session *session = user_data;
1430 struct session_info *info = session->info;
1431 DBusMessageIter iter, value;
1434 GSList *allowed_bearers;
1437 DBG("session %p", session);
1438 if (dbus_message_iter_init(msg, &iter) == FALSE)
1439 return __connman_error_invalid_arguments(msg);
1441 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1442 return __connman_error_invalid_arguments(msg);
1444 dbus_message_iter_get_basic(&iter, &name);
1445 dbus_message_iter_next(&iter);
1447 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1448 return __connman_error_invalid_arguments(msg);
1450 dbus_message_iter_recurse(&iter, &value);
1452 switch (dbus_message_iter_get_arg_type(&value)) {
1453 case DBUS_TYPE_ARRAY:
1454 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1455 err = session_parse_allowed_bearers(&value,
1458 return __connman_error_failed(msg, err);
1460 connman_session_free_bearers(info->config.allowed_bearers);
1461 err = apply_policy_on_bearers(
1462 session->policy_config->allowed_bearers,
1464 &info->config.allowed_bearers);
1466 connman_session_free_bearers(allowed_bearers);
1468 return __connman_error_failed(msg, err);
1473 case DBUS_TYPE_STRING:
1474 if (g_str_equal(name, "ConnectionType") == TRUE) {
1475 dbus_message_iter_get_basic(&value, &val);
1476 info->config.type = apply_policy_on_type(
1477 session->policy_config->type,
1487 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1489 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1492 return __connman_error_invalid_arguments(msg);
1495 static void release_session(gpointer key, gpointer value, gpointer user_data)
1497 struct connman_session *session = value;
1498 DBusMessage *message;
1500 DBG("owner %s path %s", session->owner, session->notify_path);
1502 if (session->notify_watch > 0)
1503 g_dbus_remove_watch(connection, session->notify_watch);
1505 g_dbus_unregister_interface(connection, session->session_path,
1506 CONNMAN_SESSION_INTERFACE);
1508 message = dbus_message_new_method_call(session->owner,
1509 session->notify_path,
1510 CONNMAN_NOTIFICATION_INTERFACE,
1512 if (message == NULL)
1515 dbus_message_set_no_reply(message, TRUE);
1517 g_dbus_send_message(connection, message);
1520 static int session_disconnect(struct connman_session *session)
1522 DBG("session %p, %s", session, session->owner);
1524 if (session->notify_watch > 0)
1525 g_dbus_remove_watch(connection, session->notify_watch);
1527 g_dbus_unregister_interface(connection, session->session_path,
1528 CONNMAN_SESSION_INTERFACE);
1530 deselect_and_disconnect(session);
1532 g_hash_table_remove(session_hash, session->session_path);
1537 static void owner_disconnect(DBusConnection *conn, void *user_data)
1539 struct connman_session *session = user_data;
1541 DBG("session %p, %s died", session, session->owner);
1543 session_disconnect(session);
1546 static DBusMessage *destroy_session(DBusConnection *conn,
1547 DBusMessage *msg, void *user_data)
1549 struct connman_session *session = user_data;
1551 DBG("session %p", session);
1553 if (ecall_session != NULL && ecall_session != session)
1554 return __connman_error_failed(msg, EBUSY);
1556 session_disconnect(session);
1558 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1561 static const GDBusMethodTable session_methods[] = {
1562 { GDBUS_METHOD("Destroy", NULL, NULL, destroy_session) },
1563 { GDBUS_METHOD("Connect", NULL, NULL, connect_session) },
1564 { GDBUS_METHOD("Disconnect", NULL, NULL,
1565 disconnect_session ) },
1566 { GDBUS_METHOD("Change",
1567 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1568 NULL, change_session) },
1572 static void session_create_cb(struct connman_session *session,
1573 struct connman_session_config *config,
1574 void *user_data, int err)
1576 DBG("session %p config %p", session, config);
1581 session->policy_config = config;
1584 int __connman_session_create(DBusMessage *msg)
1586 const char *owner, *notify_path;
1587 char *session_path = NULL;
1588 DBusMessageIter iter, array;
1589 struct connman_session *session = NULL;
1590 struct session_info *info, *info_last;
1591 enum connman_session_type type = CONNMAN_SESSION_TYPE_ANY;
1592 GSList *allowed_bearers = NULL;
1593 connman_bool_t allowed_bearers_valid = FALSE;
1594 connman_bool_t type_valid = FALSE;
1597 owner = dbus_message_get_sender(msg);
1599 DBG("owner %s", owner);
1601 if (ecall_session != NULL && ecall_session->ecall == TRUE) {
1603 * If there is an emergency call already going on,
1604 * ignore session creation attempt
1610 dbus_message_iter_init(msg, &iter);
1611 dbus_message_iter_recurse(&iter, &array);
1613 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1614 DBusMessageIter entry, value;
1615 const char *key, *val;
1617 dbus_message_iter_recurse(&array, &entry);
1618 dbus_message_iter_get_basic(&entry, &key);
1620 dbus_message_iter_next(&entry);
1621 dbus_message_iter_recurse(&entry, &value);
1623 switch (dbus_message_iter_get_arg_type(&value)) {
1624 case DBUS_TYPE_ARRAY:
1625 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1626 err = session_parse_allowed_bearers(&value,
1631 allowed_bearers_valid = TRUE;
1636 case DBUS_TYPE_STRING:
1637 if (g_str_equal(key, "ConnectionType") == TRUE) {
1638 dbus_message_iter_get_basic(&value, &val);
1639 type = string2type(val);
1645 dbus_message_iter_next(&array);
1648 dbus_message_iter_next(&iter);
1649 dbus_message_iter_get_basic(&iter, ¬ify_path);
1651 if (notify_path == NULL) {
1656 session_path = g_strdup_printf("/sessions%s", notify_path);
1657 if (session_path == NULL) {
1662 session = g_hash_table_lookup(session_hash, session_path);
1663 if (session != NULL) {
1669 session = g_try_new0(struct connman_session, 1);
1670 if (session == NULL) {
1675 session->info = g_try_new0(struct session_info, 1);
1676 if (session->info == NULL) {
1681 session->info_last = g_try_new0(struct session_info, 1);
1682 if (session->info_last == NULL) {
1687 info = session->info;
1688 info_last = session->info_last;
1690 session->owner = g_strdup(owner);
1691 session->session_path = session_path;
1692 session->notify_path = g_strdup(notify_path);
1693 session->notify_watch =
1694 g_dbus_add_disconnect_watch(connection, session->owner,
1695 owner_disconnect, session, NULL);
1697 err = assign_policy_plugin(session);
1700 err = create_policy_config(session, session_create_cb, NULL);
1704 if (session->policy_config->ecall == TRUE)
1705 ecall_session = session;
1707 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1708 if (type_valid == FALSE)
1709 type = CONNMAN_SESSION_TYPE_ANY;
1710 info->config.type = apply_policy_on_type(
1711 session->policy_config->type,
1713 info->config.priority = session->policy_config->priority;
1714 info->config.roaming_policy = session->policy_config->roaming_policy;
1717 if (allowed_bearers_valid == FALSE) {
1718 allowed_bearers = connman_session_allowed_bearers_any();
1719 if (allowed_bearers == NULL) {
1725 err = apply_policy_on_bearers(
1726 session->policy_config->allowed_bearers,
1728 &info->config.allowed_bearers);
1732 g_hash_table_replace(session_hash, session->session_path, session);
1734 DBG("add %s", session->session_path);
1736 if (g_dbus_register_interface(connection, session->session_path,
1737 CONNMAN_SESSION_INTERFACE,
1738 session_methods, NULL,
1739 NULL, session, NULL) == FALSE) {
1740 connman_error("Failed to register %s", session->session_path);
1741 g_hash_table_remove(session_hash, session->session_path);
1748 g_dbus_send_reply(connection, msg,
1749 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1753 populate_service_list(session);
1755 info_last->state = info->state;
1756 info_last->config.priority = info->config.priority;
1757 info_last->config.roaming_policy = info->config.roaming_policy;
1758 info_last->entry = info->entry;
1759 info_last->config.allowed_bearers = info->config.allowed_bearers;
1761 session->append_all = TRUE;
1763 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1768 connman_error("Failed to create session");
1770 if (session != NULL) {
1771 if (session->info_last != NULL)
1772 g_free(session->info_last);
1773 if (session->info != NULL)
1774 g_free(session->info);
1778 g_free(session_path);
1780 connman_session_free_bearers(allowed_bearers);
1785 int __connman_session_destroy(DBusMessage *msg)
1787 const char *owner, *session_path;
1788 struct connman_session *session;
1790 owner = dbus_message_get_sender(msg);
1792 DBG("owner %s", owner);
1794 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1796 if (session_path == NULL)
1799 session = g_hash_table_lookup(session_hash, session_path);
1800 if (session == NULL)
1803 if (g_strcmp0(owner, session->owner) != 0)
1806 session_disconnect(session);
1811 connman_bool_t __connman_session_mode()
1816 void __connman_session_set_mode(connman_bool_t enable)
1818 DBG("enable %d", enable);
1820 if (sessionmode != enable) {
1821 sessionmode = enable;
1823 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
1824 CONNMAN_MANAGER_INTERFACE, "SessionMode",
1825 DBUS_TYPE_BOOLEAN, &sessionmode);
1828 if (sessionmode == TRUE)
1829 __connman_service_disconnect_all();
1832 static void service_add(struct connman_service *service,
1835 GHashTableIter iter;
1836 GSequenceIter *iter_service_list;
1837 gpointer key, value;
1838 struct connman_session *session;
1839 struct service_entry *entry;
1841 DBG("service %p", service);
1843 g_hash_table_iter_init(&iter, session_hash);
1845 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1848 if (service_match(session, service) == FALSE)
1851 entry = create_service_entry(service, name,
1852 CONNMAN_SERVICE_STATE_IDLE);
1857 g_sequence_insert_sorted(session->service_list,
1858 entry, sort_services,
1861 g_hash_table_replace(session->service_hash, service,
1864 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1868 static void service_remove(struct connman_service *service)
1871 GHashTableIter iter;
1872 gpointer key, value;
1873 struct connman_session *session;
1874 struct session_info *info;
1876 DBG("service %p", service);
1878 g_hash_table_iter_init(&iter, session_hash);
1880 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1881 GSequenceIter *seq_iter;
1883 info = session->info;
1885 seq_iter = g_hash_table_lookup(session->service_hash, service);
1886 if (seq_iter == NULL)
1889 g_sequence_remove(seq_iter);
1891 if (info->entry != NULL && info->entry->service == service)
1893 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1897 static void service_state_changed(struct connman_service *service,
1898 enum connman_service_state state)
1900 GHashTableIter iter;
1901 gpointer key, value;
1903 DBG("service %p state %d", service, state);
1905 g_hash_table_iter_init(&iter, session_hash);
1907 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1908 struct connman_session *session = value;
1909 GSequenceIter *service_iter;
1911 service_iter = g_hash_table_lookup(session->service_hash, service);
1912 if (service_iter != NULL) {
1913 struct service_entry *entry;
1915 entry = g_sequence_get(service_iter);
1916 entry->state = state;
1919 session_changed(session,
1920 CONNMAN_SESSION_TRIGGER_SERVICE);
1924 static void ipconfig_changed(struct connman_service *service,
1925 struct connman_ipconfig *ipconfig)
1927 GHashTableIter iter;
1928 gpointer key, value;
1929 struct connman_session *session;
1930 struct session_info *info;
1931 enum connman_ipconfig_type type;
1933 DBG("service %p ipconfig %p", service, ipconfig);
1935 type = __connman_ipconfig_get_config_type(ipconfig);
1937 g_hash_table_iter_init(&iter, session_hash);
1939 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1941 info = session->info;
1943 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
1946 if (info->entry != NULL && info->entry->service == service) {
1947 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1948 ipconfig_ipv4_changed(session);
1949 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1950 ipconfig_ipv6_changed(session);
1955 static struct connman_notifier session_notifier = {
1957 .service_add = service_add,
1958 .service_remove = service_remove,
1959 .service_state_changed = service_state_changed,
1960 .ipconfig_changed = ipconfig_changed,
1963 int __connman_session_init(void)
1969 connection = connman_dbus_get_connection();
1970 if (connection == NULL)
1973 err = connman_notifier_register(&session_notifier);
1975 dbus_connection_unref(connection);
1979 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1980 NULL, cleanup_session);
1982 sessionmode = FALSE;
1986 void __connman_session_cleanup(void)
1990 if (connection == NULL)
1993 connman_notifier_unregister(&session_notifier);
1995 g_hash_table_foreach(session_hash, release_session, NULL);
1996 g_hash_table_destroy(session_hash);
1997 session_hash = NULL;
1999 dbus_connection_unref(connection);