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 enum connman_session_type {
63 CONNMAN_SESSION_TYPE_ANY = 0,
64 CONNMAN_SESSION_TYPE_LOCAL = 1,
65 CONNMAN_SESSION_TYPE_INTERNET = 2,
68 enum connman_session_roaming_policy {
69 CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN = 0,
70 CONNMAN_SESSION_ROAMING_POLICY_DEFAULT = 1,
71 CONNMAN_SESSION_ROAMING_POLICY_ALWAYS = 2,
72 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN = 3,
73 CONNMAN_SESSION_ROAMING_POLICY_NATIONAL = 4,
74 CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL = 5,
77 struct service_entry {
78 /* track why this service was selected */
79 enum connman_session_reason reason;
80 enum connman_service_state state;
82 struct connman_service *service;
85 GSList *pending_timeouts;
89 enum connman_session_state state;
90 enum connman_session_type type;
91 connman_bool_t priority;
92 GSList *allowed_bearers;
93 enum connman_session_roaming_policy roaming_policy;
95 struct service_entry *entry;
96 enum connman_session_reason reason;
99 struct connman_session {
105 struct connman_session_policy *policy;
107 connman_bool_t append_all;
108 struct session_info *info;
109 struct session_info *info_last;
111 connman_bool_t ecall;
113 GSequence *service_list;
114 GHashTable *service_hash;
119 connman_bool_t match_all;
120 enum connman_service_type service_type;
123 static const char *trigger2string(enum connman_session_trigger trigger)
126 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
128 case CONNMAN_SESSION_TRIGGER_SETTING:
130 case CONNMAN_SESSION_TRIGGER_CONNECT:
132 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
134 case CONNMAN_SESSION_TRIGGER_SERVICE:
136 case CONNMAN_SESSION_TRIGGER_ECALL:
143 static const char *reason2string(enum connman_session_reason reason)
146 case CONNMAN_SESSION_REASON_UNKNOWN:
148 case CONNMAN_SESSION_REASON_CONNECT:
150 case CONNMAN_SESSION_REASON_FREE_RIDE:
157 static const char *state2string(enum connman_session_state state)
160 case CONNMAN_SESSION_STATE_DISCONNECTED:
161 return "disconnected";
162 case CONNMAN_SESSION_STATE_CONNECTED:
164 case CONNMAN_SESSION_STATE_ONLINE:
171 static const char *type2string(enum connman_session_type type)
174 case CONNMAN_SESSION_TYPE_ANY:
176 case CONNMAN_SESSION_TYPE_LOCAL:
178 case CONNMAN_SESSION_TYPE_INTERNET:
185 static enum connman_session_type string2type(const char *type)
187 if (g_strcmp0(type, "local") == 0)
188 return CONNMAN_SESSION_TYPE_LOCAL;
189 else if (g_strcmp0(type, "internet") == 0)
190 return CONNMAN_SESSION_TYPE_INTERNET;
192 return CONNMAN_SESSION_TYPE_ANY;
195 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
197 if (g_strcmp0(policy, "default") == 0)
198 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
199 else if (g_strcmp0(policy, "always") == 0)
200 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
201 else if (g_strcmp0(policy, "forbidden") == 0)
202 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
203 else if (g_strcmp0(policy, "national") == 0)
204 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
205 else if (g_strcmp0(policy, "international") == 0)
206 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
208 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
211 static enum connman_service_type bearer2service(const char *bearer)
214 return CONNMAN_SERVICE_TYPE_UNKNOWN;
216 if (g_strcmp0(bearer, "ethernet") == 0)
217 return CONNMAN_SERVICE_TYPE_ETHERNET;
218 else if (g_strcmp0(bearer, "wifi") == 0)
219 return CONNMAN_SERVICE_TYPE_WIFI;
220 else if (g_strcmp0(bearer, "wimax") == 0)
221 return CONNMAN_SERVICE_TYPE_WIMAX;
222 else if (g_strcmp0(bearer, "bluetooth") == 0)
223 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
224 else if (g_strcmp0(bearer, "cellular") == 0)
225 return CONNMAN_SERVICE_TYPE_CELLULAR;
226 else if (g_strcmp0(bearer, "vpn") == 0)
227 return CONNMAN_SERVICE_TYPE_VPN;
229 return CONNMAN_SERVICE_TYPE_UNKNOWN;
232 static char *service2bearer(enum connman_service_type type)
235 case CONNMAN_SERVICE_TYPE_ETHERNET:
237 case CONNMAN_SERVICE_TYPE_WIFI:
239 case CONNMAN_SERVICE_TYPE_WIMAX:
241 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
243 case CONNMAN_SERVICE_TYPE_CELLULAR:
245 case CONNMAN_SERVICE_TYPE_VPN:
247 case CONNMAN_SERVICE_TYPE_UNKNOWN:
248 case CONNMAN_SERVICE_TYPE_SYSTEM:
249 case CONNMAN_SERVICE_TYPE_GPS:
250 case CONNMAN_SERVICE_TYPE_GADGET:
257 static int policy_get_bool(struct connman_session *session, const char *id,
258 const char *key, connman_bool_t *val)
260 if (session->policy == NULL) {
265 return (*session->policy->get_bool)(id, key, val);
268 static int policy_get_string(struct connman_session *session, const char *id,
269 const char *key, char **val)
271 if (session->policy == NULL) {
276 return (*session->policy->get_string)(id, key, val);
279 static int assign_policy_plugin(struct connman_session *session)
282 struct connman_session_policy *policy;
284 if (session->policy != NULL)
287 for (list = policy_list; list != NULL; list = list->next) {
290 session->policy = policy;
297 static void probe_policy(struct connman_session_policy *policy)
302 struct connman_session *session;
304 DBG("policy %p name %s", policy, policy->name);
306 g_hash_table_iter_init(&iter, session_hash);
308 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
311 if (session->policy != NULL)
314 assign_policy_plugin(session);
318 static void remove_policy(struct connman_session_policy *policy)
322 struct connman_session *session;
324 DBG("policy %p name %s", policy, policy->name);
326 g_hash_table_iter_init(&iter, session_hash);
328 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
331 if (session->policy != policy)
334 session->policy = NULL;
335 assign_policy_plugin(session);
339 static gint compare_priority(gconstpointer a, gconstpointer b)
341 const struct connman_session_policy *policy1 = a;
342 const struct connman_session_policy *policy2 = b;
344 return policy2->priority - policy1->priority;
347 static struct connman_session *session_lookup_by_id(const char *id)
349 struct connman_session *session;
355 g_hash_table_iter_init(&iter, session_hash);
357 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
360 if (g_strcmp0(session->owner, id) == FALSE)
366 DBG("No session found by id %s", id);
371 int connman_session_update_bool(const char *id, const char *key,
374 struct connman_session *session;
375 struct session_info *info;
377 session = session_lookup_by_id(id);
381 info = session->info;
385 DBG("%s %d", key, val);
390 int connman_session_update_string(const char *id, const char *key,
393 struct connman_session *session;
394 struct session_info *info;
396 session = session_lookup_by_id(id);
400 info = session->info;
404 DBG("%s %s", key, val);
409 int connman_session_policy_register(struct connman_session_policy *policy)
411 DBG("name %s", policy->name);
413 policy_list = g_slist_insert_sorted(policy_list, policy,
416 probe_policy(policy);
421 void connman_session_policy_unregister(struct connman_session_policy *policy)
423 DBG("name %s", policy->name);
425 policy_list = g_slist_remove(policy_list, policy);
427 remove_policy(policy);
430 static void cleanup_bearer_info(gpointer data, gpointer user_data)
432 struct bearer_info *info = data;
438 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
440 struct bearer_info *info;
441 DBusMessageIter array;
444 dbus_message_iter_recurse(iter, &array);
446 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
449 dbus_message_iter_get_basic(&array, &bearer);
451 info = g_try_new0(struct bearer_info, 1);
453 g_slist_foreach(list, cleanup_bearer_info, NULL);
459 info->name = g_strdup(bearer);
460 info->service_type = bearer2service(info->name);
462 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
463 g_strcmp0(info->name, "*") == 0) {
464 info->match_all = TRUE;
466 info->match_all = FALSE;
469 list = g_slist_append(list, info);
471 dbus_message_iter_next(&array);
477 static GSList *session_allowed_bearers_any(void)
479 struct bearer_info *info;
482 info = g_try_new0(struct bearer_info, 1);
486 info->name = g_strdup("");
487 info->match_all = TRUE;
488 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
490 list = g_slist_append(list, info);
495 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
497 struct session_info *info = user_data;
500 for (list = info->allowed_bearers;
501 list != NULL; list = list->next) {
502 struct bearer_info *bearer_info = list->data;
504 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
509 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
511 struct connman_service *service = user_data;
512 struct connman_ipconfig *ipconfig_ipv4;
517 if (__connman_service_is_connected_state(service,
518 CONNMAN_IPCONFIG_TYPE_IPV4) == FALSE) {
522 ipconfig_ipv4 = __connman_service_get_ip4config(service);
523 if (ipconfig_ipv4 == NULL)
526 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
529 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
531 struct connman_service *service = user_data;
532 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
537 if (__connman_service_is_connected_state(service,
538 CONNMAN_IPCONFIG_TYPE_IPV6) == FALSE) {
542 ipconfig_ipv4 = __connman_service_get_ip4config(service);
543 ipconfig_ipv6 = __connman_service_get_ip6config(service);
544 if (ipconfig_ipv6 == NULL)
547 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
550 static void append_notify(DBusMessageIter *dict,
551 struct connman_session *session)
553 struct session_info *info = session->info;
554 struct session_info *info_last = session->info_last;
555 struct connman_service *service;
556 const char *name, *ifname, *bearer;
558 if (session->append_all == TRUE ||
559 info->state != info_last->state) {
560 const char *state = state2string(info->state);
562 connman_dbus_dict_append_basic(dict, "State",
565 info_last->state = info->state;
568 if (session->append_all == TRUE ||
569 info->entry != info_last->entry) {
570 if (info->entry == NULL) {
576 name = info->entry->name;
577 ifname = info->entry->ifname;
578 service = info->entry->service;
579 bearer = info->entry->bearer;
582 connman_dbus_dict_append_basic(dict, "Name",
586 connman_dbus_dict_append_dict(dict, "IPv4",
587 append_ipconfig_ipv4,
590 connman_dbus_dict_append_dict(dict, "IPv6",
591 append_ipconfig_ipv6,
594 connman_dbus_dict_append_basic(dict, "Interface",
598 connman_dbus_dict_append_basic(dict, "Bearer",
602 info_last->entry = info->entry;
605 if (session->append_all == TRUE || info->type != info_last->type) {
606 const char *type = type2string(info->type);
608 connman_dbus_dict_append_basic(dict, "ConnectionType",
611 info_last->type = info->type;
614 if (session->append_all == TRUE ||
615 info->allowed_bearers != info_last->allowed_bearers) {
616 connman_dbus_dict_append_array(dict, "AllowedBearers",
618 append_allowed_bearers,
620 info_last->allowed_bearers = info->allowed_bearers;
623 session->append_all = FALSE;
626 static connman_bool_t is_type_matching_state(enum connman_session_state *state,
627 enum connman_session_type type)
630 case CONNMAN_SESSION_TYPE_ANY:
632 case CONNMAN_SESSION_TYPE_LOCAL:
633 if (*state >= CONNMAN_SESSION_STATE_CONNECTED) {
634 *state = CONNMAN_SESSION_STATE_CONNECTED;
639 case CONNMAN_SESSION_TYPE_INTERNET:
640 if (*state == CONNMAN_SESSION_STATE_ONLINE)
648 static connman_bool_t compute_notifiable_changes(struct connman_session *session)
650 struct session_info *info_last = session->info_last;
651 struct session_info *info = session->info;
653 if (session->append_all == TRUE)
656 if (info->state != info_last->state)
659 if (info->entry != info_last->entry &&
660 info->state >= CONNMAN_SESSION_STATE_CONNECTED)
663 if (info->allowed_bearers != info_last->allowed_bearers ||
664 info->type != info_last->type)
670 static gboolean session_notify(gpointer user_data)
672 struct connman_session *session = user_data;
674 DBusMessageIter array, dict;
676 if (compute_notifiable_changes(session) == FALSE)
679 DBG("session %p owner %s notify_path %s", session,
680 session->owner, session->notify_path);
682 msg = dbus_message_new_method_call(session->owner, session->notify_path,
683 CONNMAN_NOTIFICATION_INTERFACE,
688 dbus_message_iter_init_append(msg, &array);
689 connman_dbus_dict_open(&array, &dict);
691 append_notify(&dict, session);
693 connman_dbus_dict_close(&array, &dict);
695 g_dbus_send_message(connection, msg);
700 static void ipconfig_ipv4_changed(struct connman_session *session)
702 struct session_info *info = session->info;
704 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
705 "IPv4", append_ipconfig_ipv4,
706 info->entry->service);
709 static void ipconfig_ipv6_changed(struct connman_session *session)
711 struct session_info *info = session->info;
713 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
714 "IPv6", append_ipconfig_ipv6,
715 info->entry->service);
718 static connman_bool_t service_type_match(struct connman_session *session,
719 struct connman_service *service)
721 struct session_info *info = session->info;
724 for (list = info->allowed_bearers;
725 list != NULL; list = list->next) {
726 struct bearer_info *bearer_info = list->data;
727 enum connman_service_type service_type;
729 if (bearer_info->match_all == TRUE)
732 service_type = connman_service_get_type(service);
733 if (bearer_info->service_type == service_type)
740 static connman_bool_t service_match(struct connman_session *session,
741 struct connman_service *service)
743 if (service_type_match(session, service) == FALSE)
749 static int service_type_weight(enum connman_service_type type)
752 * The session doesn't care which service
753 * to use. Nevertheless we have to sort them
754 * according their type. The ordering is
763 case CONNMAN_SERVICE_TYPE_ETHERNET:
765 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
767 case CONNMAN_SERVICE_TYPE_WIFI:
768 case CONNMAN_SERVICE_TYPE_WIMAX:
770 case CONNMAN_SERVICE_TYPE_CELLULAR:
772 case CONNMAN_SERVICE_TYPE_UNKNOWN:
773 case CONNMAN_SERVICE_TYPE_SYSTEM:
774 case CONNMAN_SERVICE_TYPE_GPS:
775 case CONNMAN_SERVICE_TYPE_VPN:
776 case CONNMAN_SERVICE_TYPE_GADGET:
783 static gint sort_allowed_bearers(struct connman_service *service_a,
784 struct connman_service *service_b,
785 struct connman_session *session)
787 struct session_info *info = session->info;
789 enum connman_service_type type_a, type_b;
790 int weight_a, weight_b;
792 type_a = connman_service_get_type(service_a);
793 type_b = connman_service_get_type(service_b);
795 for (list = info->allowed_bearers;
796 list != NULL; list = list->next) {
797 struct bearer_info *bearer_info = list->data;
799 if (bearer_info->match_all == TRUE) {
800 if (type_a != type_b) {
801 weight_a = service_type_weight(type_a);
802 weight_b = service_type_weight(type_b);
804 if (weight_a > weight_b)
807 if (weight_a < weight_b)
814 if (type_a == bearer_info->service_type &&
815 type_b == bearer_info->service_type) {
819 if (type_a == bearer_info->service_type &&
820 type_b != bearer_info->service_type) {
824 if (type_a != bearer_info->service_type &&
825 type_b == bearer_info->service_type) {
833 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
835 struct service_entry *entry_a = (void *)a;
836 struct service_entry *entry_b = (void *)b;
837 struct connman_session *session = user_data;
839 return sort_allowed_bearers(entry_a->service, entry_b->service,
843 static void cleanup_session(gpointer user_data)
845 struct connman_session *session = user_data;
846 struct session_info *info = session->info;
848 DBG("remove %s", session->session_path);
850 g_hash_table_destroy(session->service_hash);
851 g_sequence_free(session->service_list);
853 if (info->entry != NULL &&
854 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
855 __connman_service_disconnect(info->entry->service);
858 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
859 g_slist_free(info->allowed_bearers);
861 g_free(session->owner);
862 g_free(session->session_path);
863 g_free(session->notify_path);
864 g_free(session->info);
865 g_free(session->info_last);
870 static enum connman_session_state service_to_session_state(enum connman_service_state state)
873 case CONNMAN_SERVICE_STATE_UNKNOWN:
874 case CONNMAN_SERVICE_STATE_IDLE:
875 case CONNMAN_SERVICE_STATE_ASSOCIATION:
876 case CONNMAN_SERVICE_STATE_CONFIGURATION:
877 case CONNMAN_SERVICE_STATE_DISCONNECT:
878 case CONNMAN_SERVICE_STATE_FAILURE:
880 case CONNMAN_SERVICE_STATE_READY:
881 return CONNMAN_SESSION_STATE_CONNECTED;
882 case CONNMAN_SERVICE_STATE_ONLINE:
883 return CONNMAN_SESSION_STATE_ONLINE;
886 return CONNMAN_SESSION_STATE_DISCONNECTED;
889 static connman_bool_t is_connected(enum connman_service_state state)
892 case CONNMAN_SERVICE_STATE_UNKNOWN:
893 case CONNMAN_SERVICE_STATE_IDLE:
894 case CONNMAN_SERVICE_STATE_ASSOCIATION:
895 case CONNMAN_SERVICE_STATE_CONFIGURATION:
896 case CONNMAN_SERVICE_STATE_DISCONNECT:
897 case CONNMAN_SERVICE_STATE_FAILURE:
899 case CONNMAN_SERVICE_STATE_READY:
900 case CONNMAN_SERVICE_STATE_ONLINE:
907 static connman_bool_t is_connecting(enum connman_service_state state)
910 case CONNMAN_SERVICE_STATE_UNKNOWN:
911 case CONNMAN_SERVICE_STATE_IDLE:
913 case CONNMAN_SERVICE_STATE_ASSOCIATION:
914 case CONNMAN_SERVICE_STATE_CONFIGURATION:
916 case CONNMAN_SERVICE_STATE_DISCONNECT:
917 case CONNMAN_SERVICE_STATE_FAILURE:
918 case CONNMAN_SERVICE_STATE_READY:
919 case CONNMAN_SERVICE_STATE_ONLINE:
926 static connman_bool_t explicit_connect(enum connman_session_reason reason)
929 case CONNMAN_SESSION_REASON_UNKNOWN:
930 case CONNMAN_SESSION_REASON_FREE_RIDE:
932 case CONNMAN_SESSION_REASON_CONNECT:
939 static connman_bool_t explicit_disconnect(struct session_info *info)
941 if (info->entry == NULL)
944 DBG("reason %s service %p state %d",
945 reason2string(info->entry->reason),
946 info->entry->service, info->entry->state);
948 if (info->entry->reason == CONNMAN_SESSION_REASON_UNKNOWN)
951 if (explicit_connect(info->entry->reason) == FALSE)
954 if (__connman_service_session_dec(info->entry->service) == FALSE)
960 struct pending_data {
961 unsigned int timeout;
962 struct service_entry *entry;
963 gboolean (*cb)(gpointer);
966 static void pending_timeout_free(gpointer data, gpointer user_data)
968 struct pending_data *pending = data;
970 DBG("pending %p timeout %d", pending, pending->timeout);
971 g_source_remove(pending->timeout);
975 static void pending_timeout_remove_all(struct service_entry *entry)
979 g_slist_foreach(entry->pending_timeouts, pending_timeout_free, NULL);
980 g_slist_free(entry->pending_timeouts);
981 entry->pending_timeouts = NULL;
984 static gboolean pending_timeout_cb(gpointer data)
986 struct pending_data *pending = data;
987 struct service_entry *entry = pending->entry;
990 DBG("pending %p timeout %d", pending, pending->timeout);
992 ret = pending->cb(pending->entry);
994 entry->pending_timeouts =
995 g_slist_remove(entry->pending_timeouts,
1002 static connman_bool_t pending_timeout_add(unsigned int seconds,
1003 gboolean (*cb)(gpointer),
1004 struct service_entry *entry)
1006 struct pending_data *pending = g_try_new0(struct pending_data, 1);
1008 if (pending == NULL || cb == NULL || entry == NULL) {
1014 pending->entry = entry;
1015 pending->timeout = g_timeout_add_seconds(seconds, pending_timeout_cb,
1017 entry->pending_timeouts = g_slist_prepend(entry->pending_timeouts,
1020 DBG("pending %p entry %p timeout id %d", pending, entry,
1026 static gboolean call_disconnect(gpointer user_data)
1028 struct service_entry *entry = user_data;
1029 struct connman_service *service = entry->service;
1032 * TODO: We should mark this entry as pending work. In case
1033 * disconnect fails we just unassign this session from the
1034 * service and can't do anything later on it
1036 DBG("disconnect service %p", service);
1037 __connman_service_disconnect(service);
1042 static gboolean call_connect(gpointer user_data)
1044 struct service_entry *entry = user_data;
1045 struct connman_service *service = entry->service;
1047 DBG("connect service %p", service);
1048 __connman_service_connect(service);
1053 static void deselect_service(struct session_info *info)
1055 struct service_entry *entry;
1056 connman_bool_t disconnect, connected;
1060 if (info->entry == NULL)
1063 disconnect = explicit_disconnect(info);
1065 connected = is_connecting(info->entry->state) == TRUE ||
1066 is_connected(info->entry->state) == TRUE;
1068 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1069 info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1071 entry = info->entry;
1074 DBG("disconnect %d connected %d", disconnect, connected);
1076 if (disconnect == TRUE && connected == TRUE)
1077 pending_timeout_add(0, call_disconnect, entry);
1080 static void deselect_and_disconnect(struct connman_session *session)
1082 struct session_info *info = session->info;
1084 deselect_service(info);
1086 info->reason = CONNMAN_SESSION_REASON_FREE_RIDE;
1089 static void select_connected_service(struct session_info *info,
1090 struct service_entry *entry)
1092 enum connman_session_state state;
1094 state = service_to_session_state(entry->state);
1095 if (is_type_matching_state(&state, info->type) == FALSE)
1098 info->state = state;
1100 info->entry = entry;
1101 info->entry->reason = info->reason;
1103 if (explicit_connect(info->reason) == FALSE)
1106 __connman_service_session_inc(info->entry->service);
1109 static void select_offline_service(struct session_info *info,
1110 struct service_entry *entry)
1112 if (explicit_connect(info->reason) == FALSE)
1115 info->state = service_to_session_state(entry->state);
1117 info->entry = entry;
1118 info->entry->reason = info->reason;
1120 __connman_service_session_inc(info->entry->service);
1121 pending_timeout_add(0, call_connect, entry);
1124 static void select_service(struct session_info *info,
1125 struct service_entry *entry)
1127 DBG("service %p", entry->service);
1129 if (is_connected(entry->state) == TRUE)
1130 select_connected_service(info, entry);
1132 select_offline_service(info, entry);
1135 static void select_and_connect(struct connman_session *session,
1136 enum connman_session_reason reason)
1138 struct session_info *info = session->info;
1139 struct service_entry *entry = NULL;
1140 GSequenceIter *iter;
1142 DBG("session %p reason %s", session, reason2string(reason));
1144 info->reason = reason;
1146 iter = g_sequence_get_begin_iter(session->service_list);
1148 while (g_sequence_iter_is_end(iter) == FALSE) {
1149 entry = g_sequence_get(iter);
1151 switch (entry->state) {
1152 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1153 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1154 case CONNMAN_SERVICE_STATE_READY:
1155 case CONNMAN_SERVICE_STATE_ONLINE:
1156 case CONNMAN_SERVICE_STATE_IDLE:
1157 case CONNMAN_SERVICE_STATE_DISCONNECT:
1158 select_service(info, entry);
1160 case CONNMAN_SERVICE_STATE_UNKNOWN:
1161 case CONNMAN_SERVICE_STATE_FAILURE:
1165 iter = g_sequence_iter_next(iter);
1169 static struct service_entry *create_service_entry(struct connman_service *service,
1171 enum connman_service_state state)
1173 struct service_entry *entry;
1174 enum connman_service_type type;
1177 entry = g_try_new0(struct service_entry, 1);
1181 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1182 entry->state = state;
1187 entry->service = service;
1189 idx = __connman_service_get_index(entry->service);
1190 entry->ifname = connman_inet_ifname(idx);
1191 if (entry->ifname == NULL)
1192 entry->ifname = g_strdup("");
1194 type = connman_service_get_type(entry->service);
1195 entry->bearer = service2bearer(type);
1200 static void destroy_service_entry(gpointer data)
1202 struct service_entry *entry = data;
1204 pending_timeout_remove_all(entry);
1205 g_free(entry->ifname);
1210 static void populate_service_list(struct connman_session *session)
1212 struct service_entry *entry;
1213 GSequenceIter *iter;
1215 session->service_hash =
1216 g_hash_table_new_full(g_direct_hash, g_direct_equal,
1218 session->service_list = __connman_service_get_list(session,
1220 create_service_entry,
1221 destroy_service_entry);
1223 g_sequence_sort(session->service_list, sort_services, session);
1225 iter = g_sequence_get_begin_iter(session->service_list);
1227 while (g_sequence_iter_is_end(iter) == FALSE) {
1228 entry = g_sequence_get(iter);
1230 DBG("service %p type %s name %s", entry->service,
1231 service2bearer(connman_service_get_type(entry->service)),
1234 g_hash_table_replace(session->service_hash,
1235 entry->service, iter);
1237 iter = g_sequence_iter_next(iter);
1241 static void session_changed(struct connman_session *session,
1242 enum connman_session_trigger trigger)
1244 struct session_info *info = session->info;
1245 struct session_info *info_last = session->info_last;
1246 GSequenceIter *service_iter = NULL, *service_iter_last = NULL;
1247 GSequence *service_list_last;
1248 GHashTable *service_hash_last;
1251 * TODO: This only a placeholder for the 'real' algorithm to
1252 * play a bit around. So we are going to improve it step by step.
1255 DBG("session %p trigger %s reason %s", session, trigger2string(trigger),
1256 reason2string(info->reason));
1258 if (info->entry != NULL) {
1259 enum connman_session_state state;
1261 state = service_to_session_state(info->entry->state);
1263 if (is_type_matching_state(&state, info->type) == TRUE)
1264 info->state = state;
1268 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
1269 DBG("ignore session changed event");
1271 case CONNMAN_SESSION_TRIGGER_SETTING:
1272 if (info->allowed_bearers != info_last->allowed_bearers) {
1274 service_hash_last = session->service_hash;
1275 service_list_last = session->service_list;
1277 populate_service_list(session);
1279 if (info->entry != NULL) {
1280 service_iter_last = g_hash_table_lookup(
1282 info->entry->service);
1283 service_iter = g_hash_table_lookup(
1284 session->service_hash,
1285 info->entry->service);
1288 if (service_iter == NULL && service_iter_last != NULL) {
1290 * The currently selected service is
1291 * not part of this session anymore.
1293 deselect_and_disconnect(session);
1296 g_hash_table_remove_all(service_hash_last);
1297 g_sequence_free(service_list_last);
1300 if (info->type != info_last->type) {
1301 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED &&
1302 is_type_matching_state(&info->state,
1303 info->type) == FALSE)
1304 deselect_and_disconnect(session);
1307 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED) {
1308 select_and_connect(session,
1309 CONNMAN_SESSION_REASON_FREE_RIDE);
1313 case CONNMAN_SESSION_TRIGGER_ECALL:
1315 * For the time beeing we fallback to normal connect
1318 case CONNMAN_SESSION_TRIGGER_CONNECT:
1319 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED) {
1320 if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
1322 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
1323 __connman_service_session_inc(info->entry->service);
1327 if (info->entry != NULL &&
1328 is_connecting(info->entry->state) == TRUE) {
1332 select_and_connect(session,
1333 CONNMAN_SESSION_REASON_CONNECT);
1336 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
1337 deselect_and_disconnect(session);
1340 case CONNMAN_SESSION_TRIGGER_SERVICE:
1341 if (info->entry != NULL &&
1342 (is_connecting(info->entry->state) == TRUE ||
1343 is_connected(info->entry->state) == TRUE)) {
1347 deselect_and_disconnect(session);
1349 if (info->reason == CONNMAN_SESSION_REASON_FREE_RIDE) {
1350 select_and_connect(session, info->reason);
1356 session_notify(session);
1359 static DBusMessage *connect_session(DBusConnection *conn,
1360 DBusMessage *msg, void *user_data)
1362 struct connman_session *session = user_data;
1364 DBG("session %p", session);
1366 if (ecall_session != NULL) {
1367 if (ecall_session->ecall == TRUE && ecall_session != session)
1368 return __connman_error_failed(msg, EBUSY);
1370 session->ecall = TRUE;
1371 session_changed(session, CONNMAN_SESSION_TRIGGER_ECALL);
1373 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1375 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1378 static DBusMessage *disconnect_session(DBusConnection *conn,
1379 DBusMessage *msg, void *user_data)
1381 struct connman_session *session = user_data;
1383 DBG("session %p", session);
1385 if (ecall_session != NULL) {
1386 if (ecall_session->ecall == TRUE && ecall_session != session)
1387 return __connman_error_failed(msg, EBUSY);
1389 session->ecall = FALSE;
1392 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1394 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1397 static DBusMessage *change_session(DBusConnection *conn,
1398 DBusMessage *msg, void *user_data)
1400 struct connman_session *session = user_data;
1401 struct session_info *info = session->info;
1402 DBusMessageIter iter, value;
1405 GSList *allowed_bearers;
1407 DBG("session %p", session);
1408 if (dbus_message_iter_init(msg, &iter) == FALSE)
1409 return __connman_error_invalid_arguments(msg);
1411 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1412 return __connman_error_invalid_arguments(msg);
1414 dbus_message_iter_get_basic(&iter, &name);
1415 dbus_message_iter_next(&iter);
1417 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1418 return __connman_error_invalid_arguments(msg);
1420 dbus_message_iter_recurse(&iter, &value);
1422 switch (dbus_message_iter_get_arg_type(&value)) {
1423 case DBUS_TYPE_ARRAY:
1424 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1425 allowed_bearers = session_parse_allowed_bearers(&value);
1427 g_slist_foreach(info->allowed_bearers,
1428 cleanup_bearer_info, NULL);
1429 g_slist_free(info->allowed_bearers);
1431 if (allowed_bearers == NULL) {
1432 allowed_bearers = session_allowed_bearers_any();
1434 if (allowed_bearers == NULL)
1435 return __connman_error_failed(msg, ENOMEM);
1438 info->allowed_bearers = allowed_bearers;
1443 case DBUS_TYPE_STRING:
1444 if (g_str_equal(name, "ConnectionType") == TRUE) {
1445 dbus_message_iter_get_basic(&value, &val);
1446 info->type = string2type(val);
1455 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1457 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1460 return __connman_error_invalid_arguments(msg);
1463 static void release_session(gpointer key, gpointer value, gpointer user_data)
1465 struct connman_session *session = value;
1466 DBusMessage *message;
1468 DBG("owner %s path %s", session->owner, session->notify_path);
1470 if (session->notify_watch > 0)
1471 g_dbus_remove_watch(connection, session->notify_watch);
1473 g_dbus_unregister_interface(connection, session->session_path,
1474 CONNMAN_SESSION_INTERFACE);
1476 message = dbus_message_new_method_call(session->owner,
1477 session->notify_path,
1478 CONNMAN_NOTIFICATION_INTERFACE,
1480 if (message == NULL)
1483 dbus_message_set_no_reply(message, TRUE);
1485 g_dbus_send_message(connection, message);
1488 static int session_disconnect(struct connman_session *session)
1490 DBG("session %p, %s", session, session->owner);
1492 if (session->notify_watch > 0)
1493 g_dbus_remove_watch(connection, session->notify_watch);
1495 g_dbus_unregister_interface(connection, session->session_path,
1496 CONNMAN_SESSION_INTERFACE);
1498 deselect_and_disconnect(session);
1500 g_hash_table_remove(session_hash, session->session_path);
1505 static void owner_disconnect(DBusConnection *conn, void *user_data)
1507 struct connman_session *session = user_data;
1509 DBG("session %p, %s died", session, session->owner);
1511 session_disconnect(session);
1514 static DBusMessage *destroy_session(DBusConnection *conn,
1515 DBusMessage *msg, void *user_data)
1517 struct connman_session *session = user_data;
1519 DBG("session %p", session);
1521 if (ecall_session != NULL && ecall_session != session)
1522 return __connman_error_failed(msg, EBUSY);
1524 session_disconnect(session);
1526 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1529 static const GDBusMethodTable session_methods[] = {
1530 { GDBUS_METHOD("Destroy", NULL, NULL, destroy_session) },
1531 { GDBUS_METHOD("Connect", NULL, NULL, connect_session) },
1532 { GDBUS_METHOD("Disconnect", NULL, NULL,
1533 disconnect_session ) },
1534 { GDBUS_METHOD("Change",
1535 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1536 NULL, change_session) },
1540 int __connman_session_create(DBusMessage *msg)
1542 const char *owner, *notify_path;
1543 char *session_path = NULL;
1544 DBusMessageIter iter, array;
1545 struct connman_session *session = NULL;
1546 struct session_info *info, *info_last;
1548 enum connman_session_type type = CONNMAN_SESSION_TYPE_ANY;
1549 connman_bool_t priority;
1550 connman_bool_t ecall_app;
1551 enum connman_session_roaming_policy roaming_policy;
1552 char *roaming_policy_str;
1553 GSList *allowed_bearers = NULL;
1556 owner = dbus_message_get_sender(msg);
1558 DBG("owner %s", owner);
1560 if (ecall_session != NULL && ecall_session->ecall == TRUE) {
1562 * If there is an emergency call already going on,
1563 * ignore session creation attempt
1569 dbus_message_iter_init(msg, &iter);
1570 dbus_message_iter_recurse(&iter, &array);
1572 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1573 DBusMessageIter entry, value;
1574 const char *key, *val;
1576 dbus_message_iter_recurse(&array, &entry);
1577 dbus_message_iter_get_basic(&entry, &key);
1579 dbus_message_iter_next(&entry);
1580 dbus_message_iter_recurse(&entry, &value);
1582 switch (dbus_message_iter_get_arg_type(&value)) {
1583 case DBUS_TYPE_ARRAY:
1584 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1586 session_parse_allowed_bearers(&value);
1591 case DBUS_TYPE_STRING:
1592 if (g_str_equal(key, "ConnectionType") == TRUE) {
1593 dbus_message_iter_get_basic(&value, &val);
1594 type = string2type(val);
1599 dbus_message_iter_next(&array);
1602 dbus_message_iter_next(&iter);
1603 dbus_message_iter_get_basic(&iter, ¬ify_path);
1605 if (notify_path == NULL) {
1610 session_path = g_strdup_printf("/sessions%s", notify_path);
1611 if (session_path == NULL) {
1616 session = g_hash_table_lookup(session_hash, session_path);
1617 if (session != NULL) {
1623 session = g_try_new0(struct connman_session, 1);
1624 if (session == NULL) {
1629 session->info = g_try_new0(struct session_info, 1);
1630 if (session->info == NULL) {
1635 session->info_last = g_try_new0(struct session_info, 1);
1636 if (session->info_last == NULL) {
1641 info = session->info;
1642 info_last = session->info_last;
1644 session->owner = g_strdup(owner);
1645 session->session_path = session_path;
1646 session->notify_path = g_strdup(notify_path);
1647 session->notify_watch =
1648 g_dbus_add_disconnect_watch(connection, session->owner,
1649 owner_disconnect, session, NULL);
1651 assign_policy_plugin(session);
1653 policy_get_bool(session, owner, "Priority", &priority);
1654 policy_get_bool(session, owner, "EmergencyCall", &ecall_app);
1655 policy_get_string(session, owner, "RoamingPolicy", &roaming_policy_str);
1656 roaming_policy = string2roamingpolicy(roaming_policy_str);
1658 if (ecall_app == TRUE)
1659 ecall_session = session;
1661 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1663 info->priority = priority;
1664 info->roaming_policy = roaming_policy;
1667 if (allowed_bearers == NULL) {
1668 info->allowed_bearers =
1669 session_allowed_bearers_any();
1671 if (info->allowed_bearers == NULL) {
1676 info->allowed_bearers = allowed_bearers;
1679 g_hash_table_replace(session_hash, session->session_path, session);
1681 DBG("add %s", session->session_path);
1683 if (g_dbus_register_interface(connection, session->session_path,
1684 CONNMAN_SESSION_INTERFACE,
1685 session_methods, NULL,
1686 NULL, session, NULL) == FALSE) {
1687 connman_error("Failed to register %s", session->session_path);
1688 g_hash_table_remove(session_hash, session->session_path);
1695 g_dbus_send_reply(connection, msg,
1696 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1700 populate_service_list(session);
1702 info_last->state = info->state;
1703 info_last->priority = info->priority;
1704 info_last->roaming_policy = info->roaming_policy;
1705 info_last->entry = info->entry;
1706 info_last->allowed_bearers = info->allowed_bearers;
1708 session->append_all = TRUE;
1710 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1715 connman_error("Failed to create session");
1717 if (session != NULL) {
1718 if (session->info_last != NULL)
1719 g_free(session->info_last);
1720 if (session->info != NULL)
1721 g_free(session->info);
1725 g_free(session_path);
1727 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1728 g_slist_free(allowed_bearers);
1733 int __connman_session_destroy(DBusMessage *msg)
1735 const char *owner, *session_path;
1736 struct connman_session *session;
1738 owner = dbus_message_get_sender(msg);
1740 DBG("owner %s", owner);
1742 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1744 if (session_path == NULL)
1747 session = g_hash_table_lookup(session_hash, session_path);
1748 if (session == NULL)
1751 if (g_strcmp0(owner, session->owner) != 0)
1754 session_disconnect(session);
1759 connman_bool_t __connman_session_mode()
1764 void __connman_session_set_mode(connman_bool_t enable)
1766 DBG("enable %d", enable);
1768 if (sessionmode != enable) {
1769 sessionmode = enable;
1771 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
1772 CONNMAN_MANAGER_INTERFACE, "SessionMode",
1773 DBUS_TYPE_BOOLEAN, &sessionmode);
1776 if (sessionmode == TRUE)
1777 __connman_service_disconnect_all();
1780 static void service_add(struct connman_service *service,
1783 GHashTableIter iter;
1784 GSequenceIter *iter_service_list;
1785 gpointer key, value;
1786 struct connman_session *session;
1787 struct service_entry *entry;
1789 DBG("service %p", service);
1791 g_hash_table_iter_init(&iter, session_hash);
1793 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1796 if (service_match(session, service) == FALSE)
1799 entry = create_service_entry(service, name,
1800 CONNMAN_SERVICE_STATE_IDLE);
1805 g_sequence_insert_sorted(session->service_list,
1806 entry, sort_services,
1809 g_hash_table_replace(session->service_hash, service,
1812 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1816 static void service_remove(struct connman_service *service)
1819 GHashTableIter iter;
1820 gpointer key, value;
1821 struct connman_session *session;
1822 struct session_info *info;
1824 DBG("service %p", service);
1826 g_hash_table_iter_init(&iter, session_hash);
1828 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1829 GSequenceIter *seq_iter;
1831 info = session->info;
1833 seq_iter = g_hash_table_lookup(session->service_hash, service);
1834 if (seq_iter == NULL)
1837 g_sequence_remove(seq_iter);
1839 if (info->entry != NULL && info->entry->service == service)
1841 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1845 static void service_state_changed(struct connman_service *service,
1846 enum connman_service_state state)
1848 GHashTableIter iter;
1849 gpointer key, value;
1851 DBG("service %p state %d", service, state);
1853 g_hash_table_iter_init(&iter, session_hash);
1855 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1856 struct connman_session *session = value;
1857 GSequenceIter *service_iter;
1859 service_iter = g_hash_table_lookup(session->service_hash, service);
1860 if (service_iter != NULL) {
1861 struct service_entry *entry;
1863 entry = g_sequence_get(service_iter);
1864 entry->state = state;
1867 session_changed(session,
1868 CONNMAN_SESSION_TRIGGER_SERVICE);
1872 static void ipconfig_changed(struct connman_service *service,
1873 struct connman_ipconfig *ipconfig)
1875 GHashTableIter iter;
1876 gpointer key, value;
1877 struct connman_session *session;
1878 struct session_info *info;
1879 enum connman_ipconfig_type type;
1881 DBG("service %p ipconfig %p", service, ipconfig);
1883 type = __connman_ipconfig_get_config_type(ipconfig);
1885 g_hash_table_iter_init(&iter, session_hash);
1887 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1889 info = session->info;
1891 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
1894 if (info->entry != NULL && info->entry->service == service) {
1895 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1896 ipconfig_ipv4_changed(session);
1897 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1898 ipconfig_ipv6_changed(session);
1903 static struct connman_notifier session_notifier = {
1905 .service_add = service_add,
1906 .service_remove = service_remove,
1907 .service_state_changed = service_state_changed,
1908 .ipconfig_changed = ipconfig_changed,
1911 int __connman_session_init(void)
1917 connection = connman_dbus_get_connection();
1918 if (connection == NULL)
1921 err = connman_notifier_register(&session_notifier);
1923 dbus_connection_unref(connection);
1927 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1928 NULL, cleanup_session);
1930 sessionmode = FALSE;
1934 void __connman_session_cleanup(void)
1938 if (connection == NULL)
1941 connman_notifier_unregister(&session_notifier);
1943 g_hash_table_foreach(session_hash, release_session, NULL);
1944 g_hash_table_destroy(session_hash);
1945 session_hash = NULL;
1947 dbus_connection_unref(connection);