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 if (session->policy == NULL)
244 config = connman_session_create_default_config();
246 config = (*session->policy->create)(session);
251 session->policy_config = config;
256 static void destroy_policy_config(struct connman_session *session)
258 if (session->policy == NULL) {
259 connman_session_free_bearers(
260 session->policy_config->allowed_bearers);
261 g_free(session->policy_config);
263 (*session->policy->destroy)(session);
267 static void probe_policy(struct connman_session_policy *policy)
272 struct connman_session *session;
274 DBG("policy %p name %s", policy, policy->name);
276 g_hash_table_iter_init(&iter, session_hash);
278 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
281 if (session->policy != NULL)
284 assign_policy_plugin(session);
288 static void remove_policy(struct connman_session_policy *policy)
292 struct connman_session *session;
294 DBG("policy %p name %s", policy, policy->name);
296 g_hash_table_iter_init(&iter, session_hash);
298 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
301 if (session->policy != policy)
304 session->policy = NULL;
305 assign_policy_plugin(session);
309 static gint compare_priority(gconstpointer a, gconstpointer b)
311 const struct connman_session_policy *policy1 = a;
312 const struct connman_session_policy *policy2 = b;
314 return policy2->priority - policy1->priority;
318 int connman_session_policy_register(struct connman_session_policy *policy)
320 DBG("name %s", policy->name);
322 policy_list = g_slist_insert_sorted(policy_list, policy,
325 probe_policy(policy);
330 void connman_session_policy_unregister(struct connman_session_policy *policy)
332 DBG("name %s", policy->name);
334 policy_list = g_slist_remove(policy_list, policy);
336 remove_policy(policy);
339 struct connman_session_config *connman_session_create_default_config(void)
341 struct connman_session_config *config;
343 config = g_try_new0(struct connman_session_config, 1);
347 config->priority = FALSE;
348 config->roaming_policy = CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
349 config->type = CONNMAN_SESSION_TYPE_ANY;
350 config->ecall = FALSE;
351 config->allowed_bearers = connman_session_allowed_bearers_any();
352 if (config->allowed_bearers == NULL) {
360 static enum connman_session_type apply_policy_on_type(
361 enum connman_session_type policy,
362 enum connman_session_type type)
364 if (type == CONNMAN_SESSION_TYPE_UNKNOWN)
365 return CONNMAN_SESSION_TYPE_UNKNOWN;
367 if (policy == CONNMAN_SESSION_TYPE_ANY)
370 if (policy == CONNMAN_SESSION_TYPE_LOCAL)
371 return CONNMAN_SESSION_TYPE_LOCAL;
373 return CONNMAN_SESSION_TYPE_INTERNET;
376 static void cleanup_bearer(gpointer data)
378 struct connman_session_bearer *bearer = data;
380 g_free(bearer->name);
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;
394 dbus_message_iter_recurse(iter, &array);
398 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
399 char *bearer_name = NULL;
401 dbus_message_iter_get_basic(&array, &bearer_name);
403 bearer = g_try_new0(struct connman_session_bearer, 1);
404 if (bearer == NULL) {
405 connman_session_free_bearers(*list);
410 bearer->name = g_strdup(bearer_name);
411 bearer->service_type = bearer2service(bearer->name);
413 if (bearer->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
414 g_strcmp0(bearer->name, "*") == 0) {
415 bearer->match_all = TRUE;
417 bearer->match_all = FALSE;
420 *list = g_slist_append(*list, bearer);
422 dbus_message_iter_next(&array);
428 static struct connman_session_bearer *clone_bearer(
429 struct connman_session_bearer *orig)
431 struct connman_session_bearer *bearer;
433 bearer = g_try_new0(struct connman_session_bearer, 1);
437 bearer->name = g_strdup(orig->name);
438 bearer->match_all = orig->match_all;
439 bearer->service_type = orig->service_type;
444 static int filter_bearer(GSList *policy_bearers,
445 struct connman_session_bearer *bearer,
448 struct connman_session_bearer *policy, *tmp;
451 if (policy_bearers == NULL)
454 for (it = policy_bearers; it != NULL; it = it->next) {
457 if (policy->match_all == FALSE &&
458 bearer->service_type != policy->service_type)
469 tmp = clone_bearer(bearer);
471 connman_session_free_bearers(*list);
475 *list = g_slist_append(*list, tmp);
480 static connman_bool_t is_bearer_valid(struct connman_session_bearer *bearer)
482 if (bearer->match_all == FALSE &&
483 bearer->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN)
489 static int apply_policy_on_bearers(GSList *policy_bearers, GSList *bearers,
492 struct connman_session_bearer *bearer;
498 for (it = bearers; it != NULL; it = it->next) {
501 if (is_bearer_valid(bearer) == FALSE)
504 err = filter_bearer(policy_bearers, bearer, list);
512 GSList *connman_session_allowed_bearers_any(void)
514 struct connman_session_bearer *bearer;
517 bearer = g_try_new0(struct connman_session_bearer, 1);
521 bearer->name = g_strdup("");
522 bearer->match_all = TRUE;
523 bearer->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
525 list = g_slist_append(list, bearer);
530 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
532 struct session_info *info = user_data;
535 for (list = info->config.allowed_bearers;
536 list != NULL; list = list->next) {
537 struct connman_session_bearer *bearer = list->data;
539 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
544 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
546 struct connman_service *service = user_data;
547 struct connman_ipconfig *ipconfig_ipv4;
552 if (__connman_service_is_connected_state(service,
553 CONNMAN_IPCONFIG_TYPE_IPV4) == FALSE) {
557 ipconfig_ipv4 = __connman_service_get_ip4config(service);
558 if (ipconfig_ipv4 == NULL)
561 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
564 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
566 struct connman_service *service = user_data;
567 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
572 if (__connman_service_is_connected_state(service,
573 CONNMAN_IPCONFIG_TYPE_IPV6) == FALSE) {
577 ipconfig_ipv4 = __connman_service_get_ip4config(service);
578 ipconfig_ipv6 = __connman_service_get_ip6config(service);
579 if (ipconfig_ipv6 == NULL)
582 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
585 static void append_notify(DBusMessageIter *dict,
586 struct connman_session *session)
588 struct session_info *info = session->info;
589 struct session_info *info_last = session->info_last;
590 struct connman_service *service;
591 const char *name, *ifname, *bearer;
593 if (session->append_all == TRUE ||
594 info->state != info_last->state) {
595 const char *state = state2string(info->state);
597 connman_dbus_dict_append_basic(dict, "State",
600 info_last->state = info->state;
603 if (session->append_all == TRUE ||
604 info->entry != info_last->entry) {
605 if (info->entry == NULL) {
611 name = info->entry->name;
612 ifname = info->entry->ifname;
613 service = info->entry->service;
614 bearer = info->entry->bearer;
617 connman_dbus_dict_append_basic(dict, "Name",
621 connman_dbus_dict_append_dict(dict, "IPv4",
622 append_ipconfig_ipv4,
625 connman_dbus_dict_append_dict(dict, "IPv6",
626 append_ipconfig_ipv6,
629 connman_dbus_dict_append_basic(dict, "Interface",
633 connman_dbus_dict_append_basic(dict, "Bearer",
637 info_last->entry = info->entry;
640 if (session->append_all == TRUE ||
641 info->config.type != info_last->config.type) {
642 const char *type = type2string(info->config.type);
644 connman_dbus_dict_append_basic(dict, "ConnectionType",
647 info_last->config.type = info->config.type;
650 if (session->append_all == TRUE ||
651 info->config.allowed_bearers != info_last->config.allowed_bearers) {
652 connman_dbus_dict_append_array(dict, "AllowedBearers",
654 append_allowed_bearers,
656 info_last->config.allowed_bearers = info->config.allowed_bearers;
659 session->append_all = FALSE;
662 static connman_bool_t is_type_matching_state(enum connman_session_state *state,
663 enum connman_session_type type)
666 case CONNMAN_SESSION_TYPE_UNKNOWN:
668 case CONNMAN_SESSION_TYPE_ANY:
670 case CONNMAN_SESSION_TYPE_LOCAL:
671 if (*state >= CONNMAN_SESSION_STATE_CONNECTED) {
672 *state = CONNMAN_SESSION_STATE_CONNECTED;
677 case CONNMAN_SESSION_TYPE_INTERNET:
678 if (*state == CONNMAN_SESSION_STATE_ONLINE)
686 static connman_bool_t compute_notifiable_changes(struct connman_session *session)
688 struct session_info *info_last = session->info_last;
689 struct session_info *info = session->info;
691 if (session->append_all == TRUE)
694 if (info->state != info_last->state)
697 if (info->entry != info_last->entry &&
698 info->state >= CONNMAN_SESSION_STATE_CONNECTED)
701 if (info->config.allowed_bearers != info_last->config.allowed_bearers ||
702 info->config.type != info_last->config.type)
708 static gboolean session_notify(gpointer user_data)
710 struct connman_session *session = user_data;
712 DBusMessageIter array, dict;
714 if (compute_notifiable_changes(session) == FALSE)
717 DBG("session %p owner %s notify_path %s", session,
718 session->owner, session->notify_path);
720 msg = dbus_message_new_method_call(session->owner, session->notify_path,
721 CONNMAN_NOTIFICATION_INTERFACE,
726 dbus_message_iter_init_append(msg, &array);
727 connman_dbus_dict_open(&array, &dict);
729 append_notify(&dict, session);
731 connman_dbus_dict_close(&array, &dict);
733 g_dbus_send_message(connection, msg);
738 static void ipconfig_ipv4_changed(struct connman_session *session)
740 struct session_info *info = session->info;
742 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
743 "IPv4", append_ipconfig_ipv4,
744 info->entry->service);
747 static void ipconfig_ipv6_changed(struct connman_session *session)
749 struct session_info *info = session->info;
751 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
752 "IPv6", append_ipconfig_ipv6,
753 info->entry->service);
756 static connman_bool_t service_type_match(struct connman_session *session,
757 struct connman_service *service)
759 struct session_info *info = session->info;
762 for (list = info->config.allowed_bearers;
763 list != NULL; list = list->next) {
764 struct connman_session_bearer *bearer = list->data;
765 enum connman_service_type service_type;
767 if (bearer->match_all == TRUE)
770 service_type = connman_service_get_type(service);
771 if (bearer->service_type == service_type)
778 static connman_bool_t service_match(struct connman_session *session,
779 struct connman_service *service)
781 if (service_type_match(session, service) == FALSE)
787 static int service_type_weight(enum connman_service_type type)
790 * The session doesn't care which service
791 * to use. Nevertheless we have to sort them
792 * according their type. The ordering is
801 case CONNMAN_SERVICE_TYPE_ETHERNET:
803 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
805 case CONNMAN_SERVICE_TYPE_WIFI:
806 case CONNMAN_SERVICE_TYPE_WIMAX:
808 case CONNMAN_SERVICE_TYPE_CELLULAR:
810 case CONNMAN_SERVICE_TYPE_UNKNOWN:
811 case CONNMAN_SERVICE_TYPE_SYSTEM:
812 case CONNMAN_SERVICE_TYPE_GPS:
813 case CONNMAN_SERVICE_TYPE_VPN:
814 case CONNMAN_SERVICE_TYPE_GADGET:
821 static gint sort_allowed_bearers(struct connman_service *service_a,
822 struct connman_service *service_b,
823 struct connman_session *session)
825 struct session_info *info = session->info;
827 enum connman_service_type type_a, type_b;
828 int weight_a, weight_b;
830 type_a = connman_service_get_type(service_a);
831 type_b = connman_service_get_type(service_b);
833 for (list = info->config.allowed_bearers;
834 list != NULL; list = list->next) {
835 struct connman_session_bearer *bearer = list->data;
837 if (bearer->match_all == TRUE) {
838 if (type_a != type_b) {
839 weight_a = service_type_weight(type_a);
840 weight_b = service_type_weight(type_b);
842 if (weight_a > weight_b)
845 if (weight_a < weight_b)
852 if (type_a == bearer->service_type &&
853 type_b == bearer->service_type) {
857 if (type_a == bearer->service_type &&
858 type_b != bearer->service_type) {
862 if (type_a != bearer->service_type &&
863 type_b == bearer->service_type) {
871 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
873 struct service_entry *entry_a = (void *)a;
874 struct service_entry *entry_b = (void *)b;
875 struct connman_session *session = user_data;
877 return sort_allowed_bearers(entry_a->service, entry_b->service,
881 static void free_session(struct connman_session *session)
883 destroy_policy_config(session);
884 connman_session_free_bearers(session->info->config.allowed_bearers);
885 g_free(session->owner);
886 g_free(session->session_path);
887 g_free(session->notify_path);
888 g_free(session->info);
889 g_free(session->info_last);
894 static void cleanup_session(gpointer user_data)
896 struct connman_session *session = user_data;
897 struct session_info *info = session->info;
899 DBG("remove %s", session->session_path);
901 g_hash_table_destroy(session->service_hash);
902 g_sequence_free(session->service_list);
904 if (info->entry != NULL &&
905 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
906 __connman_service_disconnect(info->entry->service);
909 free_session(session);
912 static enum connman_session_state service_to_session_state(enum connman_service_state state)
915 case CONNMAN_SERVICE_STATE_UNKNOWN:
916 case CONNMAN_SERVICE_STATE_IDLE:
917 case CONNMAN_SERVICE_STATE_ASSOCIATION:
918 case CONNMAN_SERVICE_STATE_CONFIGURATION:
919 case CONNMAN_SERVICE_STATE_DISCONNECT:
920 case CONNMAN_SERVICE_STATE_FAILURE:
922 case CONNMAN_SERVICE_STATE_READY:
923 return CONNMAN_SESSION_STATE_CONNECTED;
924 case CONNMAN_SERVICE_STATE_ONLINE:
925 return CONNMAN_SESSION_STATE_ONLINE;
928 return CONNMAN_SESSION_STATE_DISCONNECTED;
931 static connman_bool_t is_connected(enum connman_service_state state)
934 case CONNMAN_SERVICE_STATE_UNKNOWN:
935 case CONNMAN_SERVICE_STATE_IDLE:
936 case CONNMAN_SERVICE_STATE_ASSOCIATION:
937 case CONNMAN_SERVICE_STATE_CONFIGURATION:
938 case CONNMAN_SERVICE_STATE_DISCONNECT:
939 case CONNMAN_SERVICE_STATE_FAILURE:
941 case CONNMAN_SERVICE_STATE_READY:
942 case CONNMAN_SERVICE_STATE_ONLINE:
949 static connman_bool_t is_connecting(enum connman_service_state state)
952 case CONNMAN_SERVICE_STATE_UNKNOWN:
953 case CONNMAN_SERVICE_STATE_IDLE:
955 case CONNMAN_SERVICE_STATE_ASSOCIATION:
956 case CONNMAN_SERVICE_STATE_CONFIGURATION:
958 case CONNMAN_SERVICE_STATE_DISCONNECT:
959 case CONNMAN_SERVICE_STATE_FAILURE:
960 case CONNMAN_SERVICE_STATE_READY:
961 case CONNMAN_SERVICE_STATE_ONLINE:
968 static connman_bool_t explicit_connect(enum connman_session_reason reason)
971 case CONNMAN_SESSION_REASON_UNKNOWN:
972 case CONNMAN_SESSION_REASON_FREE_RIDE:
974 case CONNMAN_SESSION_REASON_CONNECT:
981 static connman_bool_t explicit_disconnect(struct session_info *info)
983 if (info->entry == NULL)
986 DBG("reason %s service %p state %d",
987 reason2string(info->entry->reason),
988 info->entry->service, info->entry->state);
990 if (info->entry->reason == CONNMAN_SESSION_REASON_UNKNOWN)
993 if (explicit_connect(info->entry->reason) == FALSE)
996 if (__connman_service_session_dec(info->entry->service) == FALSE)
1002 struct pending_data {
1003 unsigned int timeout;
1004 struct service_entry *entry;
1005 gboolean (*cb)(gpointer);
1008 static void pending_timeout_free(gpointer data, gpointer user_data)
1010 struct pending_data *pending = data;
1012 DBG("pending %p timeout %d", pending, pending->timeout);
1013 g_source_remove(pending->timeout);
1017 static void pending_timeout_remove_all(struct service_entry *entry)
1021 g_slist_foreach(entry->pending_timeouts, pending_timeout_free, NULL);
1022 g_slist_free(entry->pending_timeouts);
1023 entry->pending_timeouts = NULL;
1026 static gboolean pending_timeout_cb(gpointer data)
1028 struct pending_data *pending = data;
1029 struct service_entry *entry = pending->entry;
1032 DBG("pending %p timeout %d", pending, pending->timeout);
1034 ret = pending->cb(pending->entry);
1036 entry->pending_timeouts =
1037 g_slist_remove(entry->pending_timeouts,
1044 static connman_bool_t pending_timeout_add(unsigned int seconds,
1045 gboolean (*cb)(gpointer),
1046 struct service_entry *entry)
1048 struct pending_data *pending = g_try_new0(struct pending_data, 1);
1050 if (pending == NULL || cb == NULL || entry == NULL) {
1056 pending->entry = entry;
1057 pending->timeout = g_timeout_add_seconds(seconds, pending_timeout_cb,
1059 entry->pending_timeouts = g_slist_prepend(entry->pending_timeouts,
1062 DBG("pending %p entry %p timeout id %d", pending, entry,
1068 static gboolean call_disconnect(gpointer user_data)
1070 struct service_entry *entry = user_data;
1071 struct connman_service *service = entry->service;
1074 * TODO: We should mark this entry as pending work. In case
1075 * disconnect fails we just unassign this session from the
1076 * service and can't do anything later on it
1078 DBG("disconnect service %p", service);
1079 __connman_service_disconnect(service);
1084 static gboolean call_connect(gpointer user_data)
1086 struct service_entry *entry = user_data;
1087 struct connman_service *service = entry->service;
1089 DBG("connect service %p", service);
1090 __connman_service_connect(service);
1095 static void deselect_service(struct session_info *info)
1097 struct service_entry *entry;
1098 connman_bool_t disconnect, connected;
1102 if (info->entry == NULL)
1105 disconnect = explicit_disconnect(info);
1107 connected = is_connecting(info->entry->state) == TRUE ||
1108 is_connected(info->entry->state) == TRUE;
1110 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1111 info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1113 entry = info->entry;
1116 DBG("disconnect %d connected %d", disconnect, connected);
1118 if (disconnect == TRUE && connected == TRUE)
1119 pending_timeout_add(0, call_disconnect, entry);
1122 static void deselect_and_disconnect(struct connman_session *session)
1124 struct session_info *info = session->info;
1126 deselect_service(info);
1128 info->reason = CONNMAN_SESSION_REASON_FREE_RIDE;
1131 static void select_connected_service(struct session_info *info,
1132 struct service_entry *entry)
1134 enum connman_session_state state;
1136 state = service_to_session_state(entry->state);
1137 if (is_type_matching_state(&state, info->config.type) == FALSE)
1140 info->state = state;
1142 info->entry = entry;
1143 info->entry->reason = info->reason;
1145 if (explicit_connect(info->reason) == FALSE)
1148 __connman_service_session_inc(info->entry->service);
1151 static void select_offline_service(struct session_info *info,
1152 struct service_entry *entry)
1154 if (explicit_connect(info->reason) == FALSE)
1157 info->state = service_to_session_state(entry->state);
1159 info->entry = entry;
1160 info->entry->reason = info->reason;
1162 __connman_service_session_inc(info->entry->service);
1163 pending_timeout_add(0, call_connect, entry);
1166 static void select_service(struct session_info *info,
1167 struct service_entry *entry)
1169 DBG("service %p", entry->service);
1171 if (is_connected(entry->state) == TRUE)
1172 select_connected_service(info, entry);
1174 select_offline_service(info, entry);
1177 static void select_and_connect(struct connman_session *session,
1178 enum connman_session_reason reason)
1180 struct session_info *info = session->info;
1181 struct service_entry *entry = NULL;
1182 GSequenceIter *iter;
1184 DBG("session %p reason %s", session, reason2string(reason));
1186 info->reason = reason;
1188 iter = g_sequence_get_begin_iter(session->service_list);
1190 while (g_sequence_iter_is_end(iter) == FALSE) {
1191 entry = g_sequence_get(iter);
1193 switch (entry->state) {
1194 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1195 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1196 case CONNMAN_SERVICE_STATE_READY:
1197 case CONNMAN_SERVICE_STATE_ONLINE:
1198 case CONNMAN_SERVICE_STATE_IDLE:
1199 case CONNMAN_SERVICE_STATE_DISCONNECT:
1200 select_service(info, entry);
1202 case CONNMAN_SERVICE_STATE_UNKNOWN:
1203 case CONNMAN_SERVICE_STATE_FAILURE:
1207 iter = g_sequence_iter_next(iter);
1211 static struct service_entry *create_service_entry(struct connman_service *service,
1213 enum connman_service_state state)
1215 struct service_entry *entry;
1216 enum connman_service_type type;
1219 entry = g_try_new0(struct service_entry, 1);
1223 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1224 entry->state = state;
1229 entry->service = service;
1231 idx = __connman_service_get_index(entry->service);
1232 entry->ifname = connman_inet_ifname(idx);
1233 if (entry->ifname == NULL)
1234 entry->ifname = g_strdup("");
1236 type = connman_service_get_type(entry->service);
1237 entry->bearer = service2bearer(type);
1242 static void destroy_service_entry(gpointer data)
1244 struct service_entry *entry = data;
1246 pending_timeout_remove_all(entry);
1247 g_free(entry->ifname);
1252 static void populate_service_list(struct connman_session *session)
1254 struct service_entry *entry;
1255 GSequenceIter *iter;
1257 session->service_hash =
1258 g_hash_table_new_full(g_direct_hash, g_direct_equal,
1260 session->service_list = __connman_service_get_list(session,
1262 create_service_entry,
1263 destroy_service_entry);
1265 g_sequence_sort(session->service_list, sort_services, session);
1267 iter = g_sequence_get_begin_iter(session->service_list);
1269 while (g_sequence_iter_is_end(iter) == FALSE) {
1270 entry = g_sequence_get(iter);
1272 DBG("service %p type %s name %s", entry->service,
1273 service2bearer(connman_service_get_type(entry->service)),
1276 g_hash_table_replace(session->service_hash,
1277 entry->service, iter);
1279 iter = g_sequence_iter_next(iter);
1283 static void session_changed(struct connman_session *session,
1284 enum connman_session_trigger trigger)
1286 struct session_info *info = session->info;
1287 struct session_info *info_last = session->info_last;
1288 GSequenceIter *service_iter = NULL, *service_iter_last = NULL;
1289 GSequence *service_list_last;
1290 GHashTable *service_hash_last;
1293 * TODO: This only a placeholder for the 'real' algorithm to
1294 * play a bit around. So we are going to improve it step by step.
1297 DBG("session %p trigger %s reason %s", session, trigger2string(trigger),
1298 reason2string(info->reason));
1300 if (info->entry != NULL) {
1301 enum connman_session_state state;
1303 state = service_to_session_state(info->entry->state);
1305 if (is_type_matching_state(&state, info->config.type) == TRUE)
1306 info->state = state;
1310 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
1311 DBG("ignore session changed event");
1313 case CONNMAN_SESSION_TRIGGER_SETTING:
1314 if (info->config.allowed_bearers != info_last->config.allowed_bearers) {
1316 service_hash_last = session->service_hash;
1317 service_list_last = session->service_list;
1319 populate_service_list(session);
1321 if (info->entry != NULL) {
1322 service_iter_last = g_hash_table_lookup(
1324 info->entry->service);
1325 service_iter = g_hash_table_lookup(
1326 session->service_hash,
1327 info->entry->service);
1330 if (service_iter == NULL && service_iter_last != NULL) {
1332 * The currently selected service is
1333 * not part of this session anymore.
1335 deselect_and_disconnect(session);
1338 g_hash_table_remove_all(service_hash_last);
1339 g_sequence_free(service_list_last);
1342 if (info->config.type != info_last->config.type) {
1343 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED &&
1344 is_type_matching_state(&info->state,
1345 info->config.type) == FALSE)
1346 deselect_and_disconnect(session);
1349 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED) {
1350 select_and_connect(session,
1351 CONNMAN_SESSION_REASON_FREE_RIDE);
1355 case CONNMAN_SESSION_TRIGGER_ECALL:
1357 * For the time beeing we fallback to normal connect
1360 case CONNMAN_SESSION_TRIGGER_CONNECT:
1361 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED) {
1362 if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
1364 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
1365 __connman_service_session_inc(info->entry->service);
1369 if (info->entry != NULL &&
1370 is_connecting(info->entry->state) == TRUE) {
1374 select_and_connect(session,
1375 CONNMAN_SESSION_REASON_CONNECT);
1378 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
1379 deselect_and_disconnect(session);
1382 case CONNMAN_SESSION_TRIGGER_SERVICE:
1383 if (info->entry != NULL &&
1384 (is_connecting(info->entry->state) == TRUE ||
1385 is_connected(info->entry->state) == TRUE)) {
1389 deselect_and_disconnect(session);
1391 if (info->reason == CONNMAN_SESSION_REASON_FREE_RIDE) {
1392 select_and_connect(session, info->reason);
1398 session_notify(session);
1401 static DBusMessage *connect_session(DBusConnection *conn,
1402 DBusMessage *msg, void *user_data)
1404 struct connman_session *session = user_data;
1406 DBG("session %p", session);
1408 if (ecall_session != NULL) {
1409 if (ecall_session->ecall == TRUE && ecall_session != session)
1410 return __connman_error_failed(msg, EBUSY);
1412 session->ecall = TRUE;
1413 session_changed(session, CONNMAN_SESSION_TRIGGER_ECALL);
1415 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1417 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1420 static DBusMessage *disconnect_session(DBusConnection *conn,
1421 DBusMessage *msg, void *user_data)
1423 struct connman_session *session = user_data;
1425 DBG("session %p", session);
1427 if (ecall_session != NULL) {
1428 if (ecall_session->ecall == TRUE && ecall_session != session)
1429 return __connman_error_failed(msg, EBUSY);
1431 session->ecall = FALSE;
1434 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1436 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1439 static DBusMessage *change_session(DBusConnection *conn,
1440 DBusMessage *msg, void *user_data)
1442 struct connman_session *session = user_data;
1443 struct session_info *info = session->info;
1444 DBusMessageIter iter, value;
1447 GSList *allowed_bearers;
1450 DBG("session %p", session);
1451 if (dbus_message_iter_init(msg, &iter) == FALSE)
1452 return __connman_error_invalid_arguments(msg);
1454 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1455 return __connman_error_invalid_arguments(msg);
1457 dbus_message_iter_get_basic(&iter, &name);
1458 dbus_message_iter_next(&iter);
1460 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1461 return __connman_error_invalid_arguments(msg);
1463 dbus_message_iter_recurse(&iter, &value);
1465 switch (dbus_message_iter_get_arg_type(&value)) {
1466 case DBUS_TYPE_ARRAY:
1467 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1468 err = session_parse_allowed_bearers(&value,
1471 return __connman_error_failed(msg, err);
1473 connman_session_free_bearers(info->config.allowed_bearers);
1474 err = apply_policy_on_bearers(
1475 session->policy_config->allowed_bearers,
1477 &info->config.allowed_bearers);
1479 connman_session_free_bearers(allowed_bearers);
1481 return __connman_error_failed(msg, err);
1486 case DBUS_TYPE_STRING:
1487 if (g_str_equal(name, "ConnectionType") == TRUE) {
1488 dbus_message_iter_get_basic(&value, &val);
1489 info->config.type = apply_policy_on_type(
1490 session->policy_config->type,
1500 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1502 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1505 return __connman_error_invalid_arguments(msg);
1508 static void release_session(gpointer key, gpointer value, gpointer user_data)
1510 struct connman_session *session = value;
1511 DBusMessage *message;
1513 DBG("owner %s path %s", session->owner, session->notify_path);
1515 if (session->notify_watch > 0)
1516 g_dbus_remove_watch(connection, session->notify_watch);
1518 g_dbus_unregister_interface(connection, session->session_path,
1519 CONNMAN_SESSION_INTERFACE);
1521 message = dbus_message_new_method_call(session->owner,
1522 session->notify_path,
1523 CONNMAN_NOTIFICATION_INTERFACE,
1525 if (message == NULL)
1528 dbus_message_set_no_reply(message, TRUE);
1530 g_dbus_send_message(connection, message);
1533 static int session_disconnect(struct connman_session *session)
1535 DBG("session %p, %s", session, session->owner);
1537 if (session->notify_watch > 0)
1538 g_dbus_remove_watch(connection, session->notify_watch);
1540 g_dbus_unregister_interface(connection, session->session_path,
1541 CONNMAN_SESSION_INTERFACE);
1543 deselect_and_disconnect(session);
1545 g_hash_table_remove(session_hash, session->session_path);
1550 static void owner_disconnect(DBusConnection *conn, void *user_data)
1552 struct connman_session *session = user_data;
1554 DBG("session %p, %s died", session, session->owner);
1556 session_disconnect(session);
1559 static DBusMessage *destroy_session(DBusConnection *conn,
1560 DBusMessage *msg, void *user_data)
1562 struct connman_session *session = user_data;
1564 DBG("session %p", session);
1566 if (ecall_session != NULL && ecall_session != session)
1567 return __connman_error_failed(msg, EBUSY);
1569 session_disconnect(session);
1571 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1574 static const GDBusMethodTable session_methods[] = {
1575 { GDBUS_METHOD("Destroy", NULL, NULL, destroy_session) },
1576 { GDBUS_METHOD("Connect", NULL, NULL, connect_session) },
1577 { GDBUS_METHOD("Disconnect", NULL, NULL,
1578 disconnect_session ) },
1579 { GDBUS_METHOD("Change",
1580 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1581 NULL, change_session) },
1585 int __connman_session_create(DBusMessage *msg)
1587 const char *owner, *notify_path;
1588 char *session_path = NULL;
1589 DBusMessageIter iter, array;
1590 struct connman_session *session = NULL;
1591 struct session_info *info, *info_last;
1592 enum connman_session_type type = CONNMAN_SESSION_TYPE_ANY;
1593 GSList *allowed_bearers = NULL;
1594 connman_bool_t allowed_bearers_valid = FALSE;
1595 connman_bool_t type_valid = FALSE;
1598 owner = dbus_message_get_sender(msg);
1600 DBG("owner %s", owner);
1602 if (ecall_session != NULL && ecall_session->ecall == TRUE) {
1604 * If there is an emergency call already going on,
1605 * ignore session creation attempt
1611 dbus_message_iter_init(msg, &iter);
1612 dbus_message_iter_recurse(&iter, &array);
1614 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1615 DBusMessageIter entry, value;
1616 const char *key, *val;
1618 dbus_message_iter_recurse(&array, &entry);
1619 dbus_message_iter_get_basic(&entry, &key);
1621 dbus_message_iter_next(&entry);
1622 dbus_message_iter_recurse(&entry, &value);
1624 switch (dbus_message_iter_get_arg_type(&value)) {
1625 case DBUS_TYPE_ARRAY:
1626 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1627 err = session_parse_allowed_bearers(&value,
1632 allowed_bearers_valid = TRUE;
1637 case DBUS_TYPE_STRING:
1638 if (g_str_equal(key, "ConnectionType") == TRUE) {
1639 dbus_message_iter_get_basic(&value, &val);
1640 type = string2type(val);
1646 dbus_message_iter_next(&array);
1649 dbus_message_iter_next(&iter);
1650 dbus_message_iter_get_basic(&iter, ¬ify_path);
1652 if (notify_path == NULL) {
1657 session_path = g_strdup_printf("/sessions%s", notify_path);
1658 if (session_path == NULL) {
1663 session = g_hash_table_lookup(session_hash, session_path);
1664 if (session != NULL) {
1670 session = g_try_new0(struct connman_session, 1);
1671 if (session == NULL) {
1676 session->info = g_try_new0(struct session_info, 1);
1677 if (session->info == NULL) {
1682 session->info_last = g_try_new0(struct session_info, 1);
1683 if (session->info_last == NULL) {
1688 info = session->info;
1689 info_last = session->info_last;
1691 session->owner = g_strdup(owner);
1692 session->session_path = session_path;
1693 session->notify_path = g_strdup(notify_path);
1694 session->notify_watch =
1695 g_dbus_add_disconnect_watch(connection, session->owner,
1696 owner_disconnect, session, NULL);
1698 err = assign_policy_plugin(session);
1701 err = create_policy_config(session);
1705 if (session->policy_config->ecall == TRUE)
1706 ecall_session = session;
1708 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1709 if (type_valid == FALSE)
1710 type = CONNMAN_SESSION_TYPE_ANY;
1711 info->config.type = apply_policy_on_type(
1712 session->policy_config->type,
1714 info->config.priority = session->policy_config->priority;
1715 info->config.roaming_policy = session->policy_config->roaming_policy;
1718 if (allowed_bearers_valid == FALSE) {
1719 allowed_bearers = connman_session_allowed_bearers_any();
1720 if (allowed_bearers == NULL) {
1726 err = apply_policy_on_bearers(
1727 session->policy_config->allowed_bearers,
1729 &info->config.allowed_bearers);
1733 g_hash_table_replace(session_hash, session->session_path, session);
1735 DBG("add %s", session->session_path);
1737 if (g_dbus_register_interface(connection, session->session_path,
1738 CONNMAN_SESSION_INTERFACE,
1739 session_methods, NULL,
1740 NULL, session, NULL) == FALSE) {
1741 connman_error("Failed to register %s", session->session_path);
1742 g_hash_table_remove(session_hash, session->session_path);
1749 g_dbus_send_reply(connection, msg,
1750 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1754 populate_service_list(session);
1756 info_last->state = info->state;
1757 info_last->config.priority = info->config.priority;
1758 info_last->config.roaming_policy = info->config.roaming_policy;
1759 info_last->entry = info->entry;
1760 info_last->config.allowed_bearers = info->config.allowed_bearers;
1762 session->append_all = TRUE;
1764 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1769 connman_error("Failed to create session");
1771 if (session != NULL) {
1772 if (session->info_last != NULL)
1773 g_free(session->info_last);
1774 if (session->info != NULL)
1775 g_free(session->info);
1779 g_free(session_path);
1781 connman_session_free_bearers(allowed_bearers);
1786 int __connman_session_destroy(DBusMessage *msg)
1788 const char *owner, *session_path;
1789 struct connman_session *session;
1791 owner = dbus_message_get_sender(msg);
1793 DBG("owner %s", owner);
1795 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1797 if (session_path == NULL)
1800 session = g_hash_table_lookup(session_hash, session_path);
1801 if (session == NULL)
1804 if (g_strcmp0(owner, session->owner) != 0)
1807 session_disconnect(session);
1812 connman_bool_t __connman_session_mode()
1817 void __connman_session_set_mode(connman_bool_t enable)
1819 DBG("enable %d", enable);
1821 if (sessionmode != enable) {
1822 sessionmode = enable;
1824 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
1825 CONNMAN_MANAGER_INTERFACE, "SessionMode",
1826 DBUS_TYPE_BOOLEAN, &sessionmode);
1829 if (sessionmode == TRUE)
1830 __connman_service_disconnect_all();
1833 static void service_add(struct connman_service *service,
1836 GHashTableIter iter;
1837 GSequenceIter *iter_service_list;
1838 gpointer key, value;
1839 struct connman_session *session;
1840 struct service_entry *entry;
1842 DBG("service %p", service);
1844 g_hash_table_iter_init(&iter, session_hash);
1846 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1849 if (service_match(session, service) == FALSE)
1852 entry = create_service_entry(service, name,
1853 CONNMAN_SERVICE_STATE_IDLE);
1858 g_sequence_insert_sorted(session->service_list,
1859 entry, sort_services,
1862 g_hash_table_replace(session->service_hash, service,
1865 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1869 static void service_remove(struct connman_service *service)
1872 GHashTableIter iter;
1873 gpointer key, value;
1874 struct connman_session *session;
1875 struct session_info *info;
1877 DBG("service %p", service);
1879 g_hash_table_iter_init(&iter, session_hash);
1881 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1882 GSequenceIter *seq_iter;
1884 info = session->info;
1886 seq_iter = g_hash_table_lookup(session->service_hash, service);
1887 if (seq_iter == NULL)
1890 g_sequence_remove(seq_iter);
1892 if (info->entry != NULL && info->entry->service == service)
1894 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1898 static void service_state_changed(struct connman_service *service,
1899 enum connman_service_state state)
1901 GHashTableIter iter;
1902 gpointer key, value;
1904 DBG("service %p state %d", service, state);
1906 g_hash_table_iter_init(&iter, session_hash);
1908 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1909 struct connman_session *session = value;
1910 GSequenceIter *service_iter;
1912 service_iter = g_hash_table_lookup(session->service_hash, service);
1913 if (service_iter != NULL) {
1914 struct service_entry *entry;
1916 entry = g_sequence_get(service_iter);
1917 entry->state = state;
1920 session_changed(session,
1921 CONNMAN_SESSION_TRIGGER_SERVICE);
1925 static void ipconfig_changed(struct connman_service *service,
1926 struct connman_ipconfig *ipconfig)
1928 GHashTableIter iter;
1929 gpointer key, value;
1930 struct connman_session *session;
1931 struct session_info *info;
1932 enum connman_ipconfig_type type;
1934 DBG("service %p ipconfig %p", service, ipconfig);
1936 type = __connman_ipconfig_get_config_type(ipconfig);
1938 g_hash_table_iter_init(&iter, session_hash);
1940 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1942 info = session->info;
1944 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
1947 if (info->entry != NULL && info->entry->service == service) {
1948 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1949 ipconfig_ipv4_changed(session);
1950 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1951 ipconfig_ipv6_changed(session);
1956 static struct connman_notifier session_notifier = {
1958 .service_add = service_add,
1959 .service_remove = service_remove,
1960 .service_state_changed = service_state_changed,
1961 .ipconfig_changed = ipconfig_changed,
1964 int __connman_session_init(void)
1970 connection = connman_dbus_get_connection();
1971 if (connection == NULL)
1974 err = connman_notifier_register(&session_notifier);
1976 dbus_connection_unref(connection);
1980 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1981 NULL, cleanup_session);
1983 sessionmode = FALSE;
1987 void __connman_session_cleanup(void)
1991 if (connection == NULL)
1994 connman_notifier_unregister(&session_notifier);
1996 g_hash_table_foreach(session_hash, release_session, NULL);
1997 g_hash_table_destroy(session_hash);
1998 session_hash = NULL;
2000 dbus_connection_unref(connection);