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 enum connman_service_type bearer2service(const char *bearer)
178 return CONNMAN_SERVICE_TYPE_UNKNOWN;
180 if (g_strcmp0(bearer, "ethernet") == 0)
181 return CONNMAN_SERVICE_TYPE_ETHERNET;
182 else if (g_strcmp0(bearer, "wifi") == 0)
183 return CONNMAN_SERVICE_TYPE_WIFI;
184 else if (g_strcmp0(bearer, "wimax") == 0)
185 return CONNMAN_SERVICE_TYPE_WIMAX;
186 else if (g_strcmp0(bearer, "bluetooth") == 0)
187 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
188 else if (g_strcmp0(bearer, "cellular") == 0)
189 return CONNMAN_SERVICE_TYPE_CELLULAR;
190 else if (g_strcmp0(bearer, "vpn") == 0)
191 return CONNMAN_SERVICE_TYPE_VPN;
193 return CONNMAN_SERVICE_TYPE_UNKNOWN;
196 static char *service2bearer(enum connman_service_type type)
199 case CONNMAN_SERVICE_TYPE_ETHERNET:
201 case CONNMAN_SERVICE_TYPE_WIFI:
203 case CONNMAN_SERVICE_TYPE_WIMAX:
205 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
207 case CONNMAN_SERVICE_TYPE_CELLULAR:
209 case CONNMAN_SERVICE_TYPE_VPN:
211 case CONNMAN_SERVICE_TYPE_UNKNOWN:
212 case CONNMAN_SERVICE_TYPE_SYSTEM:
213 case CONNMAN_SERVICE_TYPE_GPS:
214 case CONNMAN_SERVICE_TYPE_GADGET:
221 static int assign_policy_plugin(struct connman_session *session)
224 struct connman_session_policy *policy;
226 if (session->policy != NULL)
229 for (list = policy_list; list != NULL; list = list->next) {
232 session->policy = policy;
239 static int create_policy_config(struct connman_session *session)
241 struct connman_session_config *config;
243 config = (*session->policy->create)(session);
247 session->policy_config = config;
252 static void destroy_policy_config(struct connman_session *session)
254 (*session->policy->destroy)(session);
257 static void probe_policy(struct connman_session_policy *policy)
262 struct connman_session *session;
264 DBG("policy %p name %s", policy, policy->name);
266 g_hash_table_iter_init(&iter, session_hash);
268 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
271 if (session->policy != NULL)
274 assign_policy_plugin(session);
278 static void remove_policy(struct connman_session_policy *policy)
282 struct connman_session *session;
284 DBG("policy %p name %s", policy, policy->name);
286 g_hash_table_iter_init(&iter, session_hash);
288 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
291 if (session->policy != policy)
294 session->policy = NULL;
295 assign_policy_plugin(session);
299 static gint compare_priority(gconstpointer a, gconstpointer b)
301 const struct connman_session_policy *policy1 = a;
302 const struct connman_session_policy *policy2 = b;
304 return policy2->priority - policy1->priority;
308 int connman_session_policy_register(struct connman_session_policy *policy)
310 DBG("name %s", policy->name);
312 policy_list = g_slist_insert_sorted(policy_list, policy,
315 probe_policy(policy);
320 void connman_session_policy_unregister(struct connman_session_policy *policy)
322 DBG("name %s", policy->name);
324 policy_list = g_slist_remove(policy_list, policy);
326 remove_policy(policy);
329 static enum connman_session_type apply_policy_on_type(
330 enum connman_session_type policy,
331 enum connman_session_type type)
333 if (type == CONNMAN_SESSION_TYPE_UNKNOWN)
334 return CONNMAN_SESSION_TYPE_UNKNOWN;
336 if (policy == CONNMAN_SESSION_TYPE_ANY)
339 if (policy == CONNMAN_SESSION_TYPE_LOCAL)
340 return CONNMAN_SESSION_TYPE_LOCAL;
342 return CONNMAN_SESSION_TYPE_INTERNET;
345 static void cleanup_bearer(gpointer data)
347 struct connman_session_bearer *bearer = data;
349 g_free(bearer->name);
353 void connman_session_free_bearers(GSList *bearers)
355 g_slist_free_full(bearers, cleanup_bearer);
358 static int session_parse_allowed_bearers(DBusMessageIter *iter, GSList **list)
360 struct connman_session_bearer *bearer;
361 DBusMessageIter array;
363 dbus_message_iter_recurse(iter, &array);
367 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
368 char *bearer_name = NULL;
370 dbus_message_iter_get_basic(&array, &bearer_name);
372 bearer = g_try_new0(struct connman_session_bearer, 1);
373 if (bearer == NULL) {
374 connman_session_free_bearers(*list);
379 bearer->name = g_strdup(bearer_name);
380 bearer->service_type = bearer2service(bearer->name);
382 if (bearer->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
383 g_strcmp0(bearer->name, "*") == 0) {
384 bearer->match_all = TRUE;
386 bearer->match_all = FALSE;
389 *list = g_slist_append(*list, bearer);
391 dbus_message_iter_next(&array);
397 static struct connman_session_bearer *clone_bearer(
398 struct connman_session_bearer *orig)
400 struct connman_session_bearer *bearer;
402 bearer = g_try_new0(struct connman_session_bearer, 1);
406 bearer->name = g_strdup(orig->name);
407 bearer->match_all = orig->match_all;
408 bearer->service_type = orig->service_type;
413 static int filter_bearer(GSList *policy_bearers,
414 struct connman_session_bearer *bearer,
417 struct connman_session_bearer *policy, *tmp;
420 if (policy_bearers == NULL)
423 for (it = policy_bearers; it != NULL; it = it->next) {
426 if (policy->match_all == FALSE &&
427 bearer->service_type != policy->service_type)
438 tmp = clone_bearer(bearer);
440 connman_session_free_bearers(*list);
444 *list = g_slist_append(*list, tmp);
449 static connman_bool_t is_bearer_valid(struct connman_session_bearer *bearer)
451 if (bearer->match_all == FALSE &&
452 bearer->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN)
458 static int apply_policy_on_bearers(GSList *policy_bearers, GSList *bearers,
461 struct connman_session_bearer *bearer;
467 for (it = bearers; it != NULL; it = it->next) {
470 if (is_bearer_valid(bearer) == FALSE)
473 err = filter_bearer(policy_bearers, bearer, list);
481 GSList *connman_session_allowed_bearers_any(void)
483 struct connman_session_bearer *bearer;
486 bearer = g_try_new0(struct connman_session_bearer, 1);
490 bearer->name = g_strdup("");
491 bearer->match_all = TRUE;
492 bearer->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
494 list = g_slist_append(list, bearer);
499 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
501 struct session_info *info = user_data;
504 for (list = info->config.allowed_bearers;
505 list != NULL; list = list->next) {
506 struct connman_session_bearer *bearer = list->data;
508 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
513 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
515 struct connman_service *service = user_data;
516 struct connman_ipconfig *ipconfig_ipv4;
521 if (__connman_service_is_connected_state(service,
522 CONNMAN_IPCONFIG_TYPE_IPV4) == FALSE) {
526 ipconfig_ipv4 = __connman_service_get_ip4config(service);
527 if (ipconfig_ipv4 == NULL)
530 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
533 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
535 struct connman_service *service = user_data;
536 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
541 if (__connman_service_is_connected_state(service,
542 CONNMAN_IPCONFIG_TYPE_IPV6) == FALSE) {
546 ipconfig_ipv4 = __connman_service_get_ip4config(service);
547 ipconfig_ipv6 = __connman_service_get_ip6config(service);
548 if (ipconfig_ipv6 == NULL)
551 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
554 static void append_notify(DBusMessageIter *dict,
555 struct connman_session *session)
557 struct session_info *info = session->info;
558 struct session_info *info_last = session->info_last;
559 struct connman_service *service;
560 const char *name, *ifname, *bearer;
562 if (session->append_all == TRUE ||
563 info->state != info_last->state) {
564 const char *state = state2string(info->state);
566 connman_dbus_dict_append_basic(dict, "State",
569 info_last->state = info->state;
572 if (session->append_all == TRUE ||
573 info->entry != info_last->entry) {
574 if (info->entry == NULL) {
580 name = info->entry->name;
581 ifname = info->entry->ifname;
582 service = info->entry->service;
583 bearer = info->entry->bearer;
586 connman_dbus_dict_append_basic(dict, "Name",
590 connman_dbus_dict_append_dict(dict, "IPv4",
591 append_ipconfig_ipv4,
594 connman_dbus_dict_append_dict(dict, "IPv6",
595 append_ipconfig_ipv6,
598 connman_dbus_dict_append_basic(dict, "Interface",
602 connman_dbus_dict_append_basic(dict, "Bearer",
606 info_last->entry = info->entry;
609 if (session->append_all == TRUE ||
610 info->config.type != info_last->config.type) {
611 const char *type = type2string(info->config.type);
613 connman_dbus_dict_append_basic(dict, "ConnectionType",
616 info_last->config.type = info->config.type;
619 if (session->append_all == TRUE ||
620 info->config.allowed_bearers != info_last->config.allowed_bearers) {
621 connman_dbus_dict_append_array(dict, "AllowedBearers",
623 append_allowed_bearers,
625 info_last->config.allowed_bearers = info->config.allowed_bearers;
628 session->append_all = FALSE;
631 static connman_bool_t is_type_matching_state(enum connman_session_state *state,
632 enum connman_session_type type)
635 case CONNMAN_SESSION_TYPE_UNKNOWN:
637 case CONNMAN_SESSION_TYPE_ANY:
639 case CONNMAN_SESSION_TYPE_LOCAL:
640 if (*state >= CONNMAN_SESSION_STATE_CONNECTED) {
641 *state = CONNMAN_SESSION_STATE_CONNECTED;
646 case CONNMAN_SESSION_TYPE_INTERNET:
647 if (*state == CONNMAN_SESSION_STATE_ONLINE)
655 static connman_bool_t compute_notifiable_changes(struct connman_session *session)
657 struct session_info *info_last = session->info_last;
658 struct session_info *info = session->info;
660 if (session->append_all == TRUE)
663 if (info->state != info_last->state)
666 if (info->entry != info_last->entry &&
667 info->state >= CONNMAN_SESSION_STATE_CONNECTED)
670 if (info->config.allowed_bearers != info_last->config.allowed_bearers ||
671 info->config.type != info_last->config.type)
677 static gboolean session_notify(gpointer user_data)
679 struct connman_session *session = user_data;
681 DBusMessageIter array, dict;
683 if (compute_notifiable_changes(session) == FALSE)
686 DBG("session %p owner %s notify_path %s", session,
687 session->owner, session->notify_path);
689 msg = dbus_message_new_method_call(session->owner, session->notify_path,
690 CONNMAN_NOTIFICATION_INTERFACE,
695 dbus_message_iter_init_append(msg, &array);
696 connman_dbus_dict_open(&array, &dict);
698 append_notify(&dict, session);
700 connman_dbus_dict_close(&array, &dict);
702 g_dbus_send_message(connection, msg);
707 static void ipconfig_ipv4_changed(struct connman_session *session)
709 struct session_info *info = session->info;
711 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
712 "IPv4", append_ipconfig_ipv4,
713 info->entry->service);
716 static void ipconfig_ipv6_changed(struct connman_session *session)
718 struct session_info *info = session->info;
720 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
721 "IPv6", append_ipconfig_ipv6,
722 info->entry->service);
725 static connman_bool_t service_type_match(struct connman_session *session,
726 struct connman_service *service)
728 struct session_info *info = session->info;
731 for (list = info->config.allowed_bearers;
732 list != NULL; list = list->next) {
733 struct connman_session_bearer *bearer = list->data;
734 enum connman_service_type service_type;
736 if (bearer->match_all == TRUE)
739 service_type = connman_service_get_type(service);
740 if (bearer->service_type == service_type)
747 static connman_bool_t service_match(struct connman_session *session,
748 struct connman_service *service)
750 if (service_type_match(session, service) == FALSE)
756 static int service_type_weight(enum connman_service_type type)
759 * The session doesn't care which service
760 * to use. Nevertheless we have to sort them
761 * according their type. The ordering is
770 case CONNMAN_SERVICE_TYPE_ETHERNET:
772 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
774 case CONNMAN_SERVICE_TYPE_WIFI:
775 case CONNMAN_SERVICE_TYPE_WIMAX:
777 case CONNMAN_SERVICE_TYPE_CELLULAR:
779 case CONNMAN_SERVICE_TYPE_UNKNOWN:
780 case CONNMAN_SERVICE_TYPE_SYSTEM:
781 case CONNMAN_SERVICE_TYPE_GPS:
782 case CONNMAN_SERVICE_TYPE_VPN:
783 case CONNMAN_SERVICE_TYPE_GADGET:
790 static gint sort_allowed_bearers(struct connman_service *service_a,
791 struct connman_service *service_b,
792 struct connman_session *session)
794 struct session_info *info = session->info;
796 enum connman_service_type type_a, type_b;
797 int weight_a, weight_b;
799 type_a = connman_service_get_type(service_a);
800 type_b = connman_service_get_type(service_b);
802 for (list = info->config.allowed_bearers;
803 list != NULL; list = list->next) {
804 struct connman_session_bearer *bearer = list->data;
806 if (bearer->match_all == TRUE) {
807 if (type_a != type_b) {
808 weight_a = service_type_weight(type_a);
809 weight_b = service_type_weight(type_b);
811 if (weight_a > weight_b)
814 if (weight_a < weight_b)
821 if (type_a == bearer->service_type &&
822 type_b == bearer->service_type) {
826 if (type_a == bearer->service_type &&
827 type_b != bearer->service_type) {
831 if (type_a != bearer->service_type &&
832 type_b == bearer->service_type) {
840 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
842 struct service_entry *entry_a = (void *)a;
843 struct service_entry *entry_b = (void *)b;
844 struct connman_session *session = user_data;
846 return sort_allowed_bearers(entry_a->service, entry_b->service,
850 static void cleanup_session(gpointer user_data)
852 struct connman_session *session = user_data;
853 struct session_info *info = session->info;
855 DBG("remove %s", session->session_path);
857 g_hash_table_destroy(session->service_hash);
858 g_sequence_free(session->service_list);
860 if (info->entry != NULL &&
861 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
862 __connman_service_disconnect(info->entry->service);
865 destroy_policy_config(session);
866 connman_session_free_bearers(session->info->config.allowed_bearers);
867 g_free(session->owner);
868 g_free(session->session_path);
869 g_free(session->notify_path);
870 g_free(session->info);
871 g_free(session->info_last);
876 static enum connman_session_state service_to_session_state(enum connman_service_state state)
879 case CONNMAN_SERVICE_STATE_UNKNOWN:
880 case CONNMAN_SERVICE_STATE_IDLE:
881 case CONNMAN_SERVICE_STATE_ASSOCIATION:
882 case CONNMAN_SERVICE_STATE_CONFIGURATION:
883 case CONNMAN_SERVICE_STATE_DISCONNECT:
884 case CONNMAN_SERVICE_STATE_FAILURE:
886 case CONNMAN_SERVICE_STATE_READY:
887 return CONNMAN_SESSION_STATE_CONNECTED;
888 case CONNMAN_SERVICE_STATE_ONLINE:
889 return CONNMAN_SESSION_STATE_ONLINE;
892 return CONNMAN_SESSION_STATE_DISCONNECTED;
895 static connman_bool_t is_connected(enum connman_service_state state)
898 case CONNMAN_SERVICE_STATE_UNKNOWN:
899 case CONNMAN_SERVICE_STATE_IDLE:
900 case CONNMAN_SERVICE_STATE_ASSOCIATION:
901 case CONNMAN_SERVICE_STATE_CONFIGURATION:
902 case CONNMAN_SERVICE_STATE_DISCONNECT:
903 case CONNMAN_SERVICE_STATE_FAILURE:
905 case CONNMAN_SERVICE_STATE_READY:
906 case CONNMAN_SERVICE_STATE_ONLINE:
913 static connman_bool_t is_connecting(enum connman_service_state state)
916 case CONNMAN_SERVICE_STATE_UNKNOWN:
917 case CONNMAN_SERVICE_STATE_IDLE:
919 case CONNMAN_SERVICE_STATE_ASSOCIATION:
920 case CONNMAN_SERVICE_STATE_CONFIGURATION:
922 case CONNMAN_SERVICE_STATE_DISCONNECT:
923 case CONNMAN_SERVICE_STATE_FAILURE:
924 case CONNMAN_SERVICE_STATE_READY:
925 case CONNMAN_SERVICE_STATE_ONLINE:
932 static connman_bool_t explicit_connect(enum connman_session_reason reason)
935 case CONNMAN_SESSION_REASON_UNKNOWN:
936 case CONNMAN_SESSION_REASON_FREE_RIDE:
938 case CONNMAN_SESSION_REASON_CONNECT:
945 static connman_bool_t explicit_disconnect(struct session_info *info)
947 if (info->entry == NULL)
950 DBG("reason %s service %p state %d",
951 reason2string(info->entry->reason),
952 info->entry->service, info->entry->state);
954 if (info->entry->reason == CONNMAN_SESSION_REASON_UNKNOWN)
957 if (explicit_connect(info->entry->reason) == FALSE)
960 if (__connman_service_session_dec(info->entry->service) == FALSE)
966 struct pending_data {
967 unsigned int timeout;
968 struct service_entry *entry;
969 gboolean (*cb)(gpointer);
972 static void pending_timeout_free(gpointer data, gpointer user_data)
974 struct pending_data *pending = data;
976 DBG("pending %p timeout %d", pending, pending->timeout);
977 g_source_remove(pending->timeout);
981 static void pending_timeout_remove_all(struct service_entry *entry)
985 g_slist_foreach(entry->pending_timeouts, pending_timeout_free, NULL);
986 g_slist_free(entry->pending_timeouts);
987 entry->pending_timeouts = NULL;
990 static gboolean pending_timeout_cb(gpointer data)
992 struct pending_data *pending = data;
993 struct service_entry *entry = pending->entry;
996 DBG("pending %p timeout %d", pending, pending->timeout);
998 ret = pending->cb(pending->entry);
1000 entry->pending_timeouts =
1001 g_slist_remove(entry->pending_timeouts,
1008 static connman_bool_t pending_timeout_add(unsigned int seconds,
1009 gboolean (*cb)(gpointer),
1010 struct service_entry *entry)
1012 struct pending_data *pending = g_try_new0(struct pending_data, 1);
1014 if (pending == NULL || cb == NULL || entry == NULL) {
1020 pending->entry = entry;
1021 pending->timeout = g_timeout_add_seconds(seconds, pending_timeout_cb,
1023 entry->pending_timeouts = g_slist_prepend(entry->pending_timeouts,
1026 DBG("pending %p entry %p timeout id %d", pending, entry,
1032 static gboolean call_disconnect(gpointer user_data)
1034 struct service_entry *entry = user_data;
1035 struct connman_service *service = entry->service;
1038 * TODO: We should mark this entry as pending work. In case
1039 * disconnect fails we just unassign this session from the
1040 * service and can't do anything later on it
1042 DBG("disconnect service %p", service);
1043 __connman_service_disconnect(service);
1048 static gboolean call_connect(gpointer user_data)
1050 struct service_entry *entry = user_data;
1051 struct connman_service *service = entry->service;
1053 DBG("connect service %p", service);
1054 __connman_service_connect(service);
1059 static void deselect_service(struct session_info *info)
1061 struct service_entry *entry;
1062 connman_bool_t disconnect, connected;
1066 if (info->entry == NULL)
1069 disconnect = explicit_disconnect(info);
1071 connected = is_connecting(info->entry->state) == TRUE ||
1072 is_connected(info->entry->state) == TRUE;
1074 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1075 info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1077 entry = info->entry;
1080 DBG("disconnect %d connected %d", disconnect, connected);
1082 if (disconnect == TRUE && connected == TRUE)
1083 pending_timeout_add(0, call_disconnect, entry);
1086 static void deselect_and_disconnect(struct connman_session *session)
1088 struct session_info *info = session->info;
1090 deselect_service(info);
1092 info->reason = CONNMAN_SESSION_REASON_FREE_RIDE;
1095 static void select_connected_service(struct session_info *info,
1096 struct service_entry *entry)
1098 enum connman_session_state state;
1100 state = service_to_session_state(entry->state);
1101 if (is_type_matching_state(&state, info->config.type) == FALSE)
1104 info->state = state;
1106 info->entry = entry;
1107 info->entry->reason = info->reason;
1109 if (explicit_connect(info->reason) == FALSE)
1112 __connman_service_session_inc(info->entry->service);
1115 static void select_offline_service(struct session_info *info,
1116 struct service_entry *entry)
1118 if (explicit_connect(info->reason) == FALSE)
1121 info->state = service_to_session_state(entry->state);
1123 info->entry = entry;
1124 info->entry->reason = info->reason;
1126 __connman_service_session_inc(info->entry->service);
1127 pending_timeout_add(0, call_connect, entry);
1130 static void select_service(struct session_info *info,
1131 struct service_entry *entry)
1133 DBG("service %p", entry->service);
1135 if (is_connected(entry->state) == TRUE)
1136 select_connected_service(info, entry);
1138 select_offline_service(info, entry);
1141 static void select_and_connect(struct connman_session *session,
1142 enum connman_session_reason reason)
1144 struct session_info *info = session->info;
1145 struct service_entry *entry = NULL;
1146 GSequenceIter *iter;
1148 DBG("session %p reason %s", session, reason2string(reason));
1150 info->reason = reason;
1152 iter = g_sequence_get_begin_iter(session->service_list);
1154 while (g_sequence_iter_is_end(iter) == FALSE) {
1155 entry = g_sequence_get(iter);
1157 switch (entry->state) {
1158 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1159 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1160 case CONNMAN_SERVICE_STATE_READY:
1161 case CONNMAN_SERVICE_STATE_ONLINE:
1162 case CONNMAN_SERVICE_STATE_IDLE:
1163 case CONNMAN_SERVICE_STATE_DISCONNECT:
1164 select_service(info, entry);
1166 case CONNMAN_SERVICE_STATE_UNKNOWN:
1167 case CONNMAN_SERVICE_STATE_FAILURE:
1171 iter = g_sequence_iter_next(iter);
1175 static struct service_entry *create_service_entry(struct connman_service *service,
1177 enum connman_service_state state)
1179 struct service_entry *entry;
1180 enum connman_service_type type;
1183 entry = g_try_new0(struct service_entry, 1);
1187 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1188 entry->state = state;
1193 entry->service = service;
1195 idx = __connman_service_get_index(entry->service);
1196 entry->ifname = connman_inet_ifname(idx);
1197 if (entry->ifname == NULL)
1198 entry->ifname = g_strdup("");
1200 type = connman_service_get_type(entry->service);
1201 entry->bearer = service2bearer(type);
1206 static void destroy_service_entry(gpointer data)
1208 struct service_entry *entry = data;
1210 pending_timeout_remove_all(entry);
1211 g_free(entry->ifname);
1216 static void populate_service_list(struct connman_session *session)
1218 struct service_entry *entry;
1219 GSequenceIter *iter;
1221 session->service_hash =
1222 g_hash_table_new_full(g_direct_hash, g_direct_equal,
1224 session->service_list = __connman_service_get_list(session,
1226 create_service_entry,
1227 destroy_service_entry);
1229 g_sequence_sort(session->service_list, sort_services, session);
1231 iter = g_sequence_get_begin_iter(session->service_list);
1233 while (g_sequence_iter_is_end(iter) == FALSE) {
1234 entry = g_sequence_get(iter);
1236 DBG("service %p type %s name %s", entry->service,
1237 service2bearer(connman_service_get_type(entry->service)),
1240 g_hash_table_replace(session->service_hash,
1241 entry->service, iter);
1243 iter = g_sequence_iter_next(iter);
1247 static void session_changed(struct connman_session *session,
1248 enum connman_session_trigger trigger)
1250 struct session_info *info = session->info;
1251 struct session_info *info_last = session->info_last;
1252 GSequenceIter *service_iter = NULL, *service_iter_last = NULL;
1253 GSequence *service_list_last;
1254 GHashTable *service_hash_last;
1257 * TODO: This only a placeholder for the 'real' algorithm to
1258 * play a bit around. So we are going to improve it step by step.
1261 DBG("session %p trigger %s reason %s", session, trigger2string(trigger),
1262 reason2string(info->reason));
1264 if (info->entry != NULL) {
1265 enum connman_session_state state;
1267 state = service_to_session_state(info->entry->state);
1269 if (is_type_matching_state(&state, info->config.type) == TRUE)
1270 info->state = state;
1274 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
1275 DBG("ignore session changed event");
1277 case CONNMAN_SESSION_TRIGGER_SETTING:
1278 if (info->config.allowed_bearers != info_last->config.allowed_bearers) {
1280 service_hash_last = session->service_hash;
1281 service_list_last = session->service_list;
1283 populate_service_list(session);
1285 if (info->entry != NULL) {
1286 service_iter_last = g_hash_table_lookup(
1288 info->entry->service);
1289 service_iter = g_hash_table_lookup(
1290 session->service_hash,
1291 info->entry->service);
1294 if (service_iter == NULL && service_iter_last != NULL) {
1296 * The currently selected service is
1297 * not part of this session anymore.
1299 deselect_and_disconnect(session);
1302 g_hash_table_remove_all(service_hash_last);
1303 g_sequence_free(service_list_last);
1306 if (info->config.type != info_last->config.type) {
1307 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED &&
1308 is_type_matching_state(&info->state,
1309 info->config.type) == FALSE)
1310 deselect_and_disconnect(session);
1313 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED) {
1314 select_and_connect(session,
1315 CONNMAN_SESSION_REASON_FREE_RIDE);
1319 case CONNMAN_SESSION_TRIGGER_ECALL:
1321 * For the time beeing we fallback to normal connect
1324 case CONNMAN_SESSION_TRIGGER_CONNECT:
1325 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED) {
1326 if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
1328 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
1329 __connman_service_session_inc(info->entry->service);
1333 if (info->entry != NULL &&
1334 is_connecting(info->entry->state) == TRUE) {
1338 select_and_connect(session,
1339 CONNMAN_SESSION_REASON_CONNECT);
1342 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
1343 deselect_and_disconnect(session);
1346 case CONNMAN_SESSION_TRIGGER_SERVICE:
1347 if (info->entry != NULL &&
1348 (is_connecting(info->entry->state) == TRUE ||
1349 is_connected(info->entry->state) == TRUE)) {
1353 deselect_and_disconnect(session);
1355 if (info->reason == CONNMAN_SESSION_REASON_FREE_RIDE) {
1356 select_and_connect(session, info->reason);
1362 session_notify(session);
1365 static DBusMessage *connect_session(DBusConnection *conn,
1366 DBusMessage *msg, void *user_data)
1368 struct connman_session *session = user_data;
1370 DBG("session %p", session);
1372 if (ecall_session != NULL) {
1373 if (ecall_session->ecall == TRUE && ecall_session != session)
1374 return __connman_error_failed(msg, EBUSY);
1376 session->ecall = TRUE;
1377 session_changed(session, CONNMAN_SESSION_TRIGGER_ECALL);
1379 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1381 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1384 static DBusMessage *disconnect_session(DBusConnection *conn,
1385 DBusMessage *msg, void *user_data)
1387 struct connman_session *session = user_data;
1389 DBG("session %p", session);
1391 if (ecall_session != NULL) {
1392 if (ecall_session->ecall == TRUE && ecall_session != session)
1393 return __connman_error_failed(msg, EBUSY);
1395 session->ecall = FALSE;
1398 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1400 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1403 static DBusMessage *change_session(DBusConnection *conn,
1404 DBusMessage *msg, void *user_data)
1406 struct connman_session *session = user_data;
1407 struct session_info *info = session->info;
1408 DBusMessageIter iter, value;
1411 GSList *allowed_bearers;
1414 DBG("session %p", session);
1415 if (dbus_message_iter_init(msg, &iter) == FALSE)
1416 return __connman_error_invalid_arguments(msg);
1418 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1419 return __connman_error_invalid_arguments(msg);
1421 dbus_message_iter_get_basic(&iter, &name);
1422 dbus_message_iter_next(&iter);
1424 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1425 return __connman_error_invalid_arguments(msg);
1427 dbus_message_iter_recurse(&iter, &value);
1429 switch (dbus_message_iter_get_arg_type(&value)) {
1430 case DBUS_TYPE_ARRAY:
1431 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1432 err = session_parse_allowed_bearers(&value,
1435 return __connman_error_failed(msg, err);
1437 connman_session_free_bearers(info->config.allowed_bearers);
1438 err = apply_policy_on_bearers(
1439 session->policy_config->allowed_bearers,
1441 &info->config.allowed_bearers);
1443 connman_session_free_bearers(allowed_bearers);
1445 return __connman_error_failed(msg, err);
1450 case DBUS_TYPE_STRING:
1451 if (g_str_equal(name, "ConnectionType") == TRUE) {
1452 dbus_message_iter_get_basic(&value, &val);
1453 info->config.type = apply_policy_on_type(
1454 session->policy_config->type,
1464 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1466 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1469 return __connman_error_invalid_arguments(msg);
1472 static void release_session(gpointer key, gpointer value, gpointer user_data)
1474 struct connman_session *session = value;
1475 DBusMessage *message;
1477 DBG("owner %s path %s", session->owner, session->notify_path);
1479 if (session->notify_watch > 0)
1480 g_dbus_remove_watch(connection, session->notify_watch);
1482 g_dbus_unregister_interface(connection, session->session_path,
1483 CONNMAN_SESSION_INTERFACE);
1485 message = dbus_message_new_method_call(session->owner,
1486 session->notify_path,
1487 CONNMAN_NOTIFICATION_INTERFACE,
1489 if (message == NULL)
1492 dbus_message_set_no_reply(message, TRUE);
1494 g_dbus_send_message(connection, message);
1497 static int session_disconnect(struct connman_session *session)
1499 DBG("session %p, %s", session, session->owner);
1501 if (session->notify_watch > 0)
1502 g_dbus_remove_watch(connection, session->notify_watch);
1504 g_dbus_unregister_interface(connection, session->session_path,
1505 CONNMAN_SESSION_INTERFACE);
1507 deselect_and_disconnect(session);
1509 g_hash_table_remove(session_hash, session->session_path);
1514 static void owner_disconnect(DBusConnection *conn, void *user_data)
1516 struct connman_session *session = user_data;
1518 DBG("session %p, %s died", session, session->owner);
1520 session_disconnect(session);
1523 static DBusMessage *destroy_session(DBusConnection *conn,
1524 DBusMessage *msg, void *user_data)
1526 struct connman_session *session = user_data;
1528 DBG("session %p", session);
1530 if (ecall_session != NULL && ecall_session != session)
1531 return __connman_error_failed(msg, EBUSY);
1533 session_disconnect(session);
1535 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1538 static const GDBusMethodTable session_methods[] = {
1539 { GDBUS_METHOD("Destroy", NULL, NULL, destroy_session) },
1540 { GDBUS_METHOD("Connect", NULL, NULL, connect_session) },
1541 { GDBUS_METHOD("Disconnect", NULL, NULL,
1542 disconnect_session ) },
1543 { GDBUS_METHOD("Change",
1544 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1545 NULL, change_session) },
1549 int __connman_session_create(DBusMessage *msg)
1551 const char *owner, *notify_path;
1552 char *session_path = NULL;
1553 DBusMessageIter iter, array;
1554 struct connman_session *session = NULL;
1555 struct session_info *info, *info_last;
1556 enum connman_session_type type = CONNMAN_SESSION_TYPE_ANY;
1557 GSList *allowed_bearers = NULL;
1558 connman_bool_t allowed_bearers_valid = FALSE;
1559 connman_bool_t type_valid = FALSE;
1562 owner = dbus_message_get_sender(msg);
1564 DBG("owner %s", owner);
1566 if (ecall_session != NULL && ecall_session->ecall == TRUE) {
1568 * If there is an emergency call already going on,
1569 * ignore session creation attempt
1575 dbus_message_iter_init(msg, &iter);
1576 dbus_message_iter_recurse(&iter, &array);
1578 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1579 DBusMessageIter entry, value;
1580 const char *key, *val;
1582 dbus_message_iter_recurse(&array, &entry);
1583 dbus_message_iter_get_basic(&entry, &key);
1585 dbus_message_iter_next(&entry);
1586 dbus_message_iter_recurse(&entry, &value);
1588 switch (dbus_message_iter_get_arg_type(&value)) {
1589 case DBUS_TYPE_ARRAY:
1590 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1591 err = session_parse_allowed_bearers(&value,
1596 allowed_bearers_valid = TRUE;
1601 case DBUS_TYPE_STRING:
1602 if (g_str_equal(key, "ConnectionType") == TRUE) {
1603 dbus_message_iter_get_basic(&value, &val);
1604 type = string2type(val);
1610 dbus_message_iter_next(&array);
1613 dbus_message_iter_next(&iter);
1614 dbus_message_iter_get_basic(&iter, ¬ify_path);
1616 if (notify_path == NULL) {
1621 session_path = g_strdup_printf("/sessions%s", notify_path);
1622 if (session_path == NULL) {
1627 session = g_hash_table_lookup(session_hash, session_path);
1628 if (session != NULL) {
1634 session = g_try_new0(struct connman_session, 1);
1635 if (session == NULL) {
1640 session->info = g_try_new0(struct session_info, 1);
1641 if (session->info == NULL) {
1646 session->info_last = g_try_new0(struct session_info, 1);
1647 if (session->info_last == NULL) {
1652 info = session->info;
1653 info_last = session->info_last;
1655 session->owner = g_strdup(owner);
1656 session->session_path = session_path;
1657 session->notify_path = g_strdup(notify_path);
1658 session->notify_watch =
1659 g_dbus_add_disconnect_watch(connection, session->owner,
1660 owner_disconnect, session, NULL);
1662 err = assign_policy_plugin(session);
1665 err = create_policy_config(session);
1669 if (session->policy_config->ecall == TRUE)
1670 ecall_session = session;
1672 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1673 if (type_valid == FALSE)
1674 type = CONNMAN_SESSION_TYPE_ANY;
1675 info->config.type = apply_policy_on_type(
1676 session->policy_config->type,
1678 info->config.priority = session->policy_config->priority;
1679 info->config.roaming_policy = session->policy_config->roaming_policy;
1682 if (allowed_bearers_valid == FALSE) {
1683 allowed_bearers = connman_session_allowed_bearers_any();
1684 if (allowed_bearers == NULL) {
1690 err = apply_policy_on_bearers(
1691 session->policy_config->allowed_bearers,
1693 &info->config.allowed_bearers);
1697 g_hash_table_replace(session_hash, session->session_path, session);
1699 DBG("add %s", session->session_path);
1701 if (g_dbus_register_interface(connection, session->session_path,
1702 CONNMAN_SESSION_INTERFACE,
1703 session_methods, NULL,
1704 NULL, session, NULL) == FALSE) {
1705 connman_error("Failed to register %s", session->session_path);
1706 g_hash_table_remove(session_hash, session->session_path);
1713 g_dbus_send_reply(connection, msg,
1714 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1718 populate_service_list(session);
1720 info_last->state = info->state;
1721 info_last->config.priority = info->config.priority;
1722 info_last->config.roaming_policy = info->config.roaming_policy;
1723 info_last->entry = info->entry;
1724 info_last->config.allowed_bearers = info->config.allowed_bearers;
1726 session->append_all = TRUE;
1728 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1733 connman_error("Failed to create session");
1735 if (session != NULL) {
1736 if (session->info_last != NULL)
1737 g_free(session->info_last);
1738 if (session->info != NULL)
1739 g_free(session->info);
1743 g_free(session_path);
1745 connman_session_free_bearers(allowed_bearers);
1750 int __connman_session_destroy(DBusMessage *msg)
1752 const char *owner, *session_path;
1753 struct connman_session *session;
1755 owner = dbus_message_get_sender(msg);
1757 DBG("owner %s", owner);
1759 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1761 if (session_path == NULL)
1764 session = g_hash_table_lookup(session_hash, session_path);
1765 if (session == NULL)
1768 if (g_strcmp0(owner, session->owner) != 0)
1771 session_disconnect(session);
1776 connman_bool_t __connman_session_mode()
1781 void __connman_session_set_mode(connman_bool_t enable)
1783 DBG("enable %d", enable);
1785 if (sessionmode != enable) {
1786 sessionmode = enable;
1788 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
1789 CONNMAN_MANAGER_INTERFACE, "SessionMode",
1790 DBUS_TYPE_BOOLEAN, &sessionmode);
1793 if (sessionmode == TRUE)
1794 __connman_service_disconnect_all();
1797 static void service_add(struct connman_service *service,
1800 GHashTableIter iter;
1801 GSequenceIter *iter_service_list;
1802 gpointer key, value;
1803 struct connman_session *session;
1804 struct service_entry *entry;
1806 DBG("service %p", service);
1808 g_hash_table_iter_init(&iter, session_hash);
1810 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1813 if (service_match(session, service) == FALSE)
1816 entry = create_service_entry(service, name,
1817 CONNMAN_SERVICE_STATE_IDLE);
1822 g_sequence_insert_sorted(session->service_list,
1823 entry, sort_services,
1826 g_hash_table_replace(session->service_hash, service,
1829 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1833 static void service_remove(struct connman_service *service)
1836 GHashTableIter iter;
1837 gpointer key, value;
1838 struct connman_session *session;
1839 struct session_info *info;
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) {
1846 GSequenceIter *seq_iter;
1848 info = session->info;
1850 seq_iter = g_hash_table_lookup(session->service_hash, service);
1851 if (seq_iter == NULL)
1854 g_sequence_remove(seq_iter);
1856 if (info->entry != NULL && info->entry->service == service)
1858 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1862 static void service_state_changed(struct connman_service *service,
1863 enum connman_service_state state)
1865 GHashTableIter iter;
1866 gpointer key, value;
1868 DBG("service %p state %d", service, state);
1870 g_hash_table_iter_init(&iter, session_hash);
1872 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1873 struct connman_session *session = value;
1874 GSequenceIter *service_iter;
1876 service_iter = g_hash_table_lookup(session->service_hash, service);
1877 if (service_iter != NULL) {
1878 struct service_entry *entry;
1880 entry = g_sequence_get(service_iter);
1881 entry->state = state;
1884 session_changed(session,
1885 CONNMAN_SESSION_TRIGGER_SERVICE);
1889 static void ipconfig_changed(struct connman_service *service,
1890 struct connman_ipconfig *ipconfig)
1892 GHashTableIter iter;
1893 gpointer key, value;
1894 struct connman_session *session;
1895 struct session_info *info;
1896 enum connman_ipconfig_type type;
1898 DBG("service %p ipconfig %p", service, ipconfig);
1900 type = __connman_ipconfig_get_config_type(ipconfig);
1902 g_hash_table_iter_init(&iter, session_hash);
1904 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1906 info = session->info;
1908 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
1911 if (info->entry != NULL && info->entry->service == service) {
1912 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1913 ipconfig_ipv4_changed(session);
1914 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1915 ipconfig_ipv6_changed(session);
1920 static struct connman_notifier session_notifier = {
1922 .service_add = service_add,
1923 .service_remove = service_remove,
1924 .service_state_changed = service_state_changed,
1925 .ipconfig_changed = ipconfig_changed,
1928 int __connman_session_init(void)
1934 connection = connman_dbus_get_connection();
1935 if (connection == NULL)
1938 err = connman_notifier_register(&session_notifier);
1940 dbus_connection_unref(connection);
1944 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1945 NULL, cleanup_session);
1947 sessionmode = FALSE;
1951 void __connman_session_cleanup(void)
1955 if (connection == NULL)
1958 connman_notifier_unregister(&session_notifier);
1960 g_hash_table_foreach(session_hash, release_session, NULL);
1961 g_hash_table_destroy(session_hash);
1962 session_hash = NULL;
1964 dbus_connection_unref(connection);