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_ANY:
152 case CONNMAN_SESSION_TYPE_LOCAL:
154 case CONNMAN_SESSION_TYPE_INTERNET:
161 static enum connman_session_type string2type(const char *type)
163 if (g_strcmp0(type, "local") == 0)
164 return CONNMAN_SESSION_TYPE_LOCAL;
165 else if (g_strcmp0(type, "internet") == 0)
166 return CONNMAN_SESSION_TYPE_INTERNET;
168 return CONNMAN_SESSION_TYPE_ANY;
171 static enum connman_service_type bearer2service(const char *bearer)
174 return CONNMAN_SERVICE_TYPE_UNKNOWN;
176 if (g_strcmp0(bearer, "ethernet") == 0)
177 return CONNMAN_SERVICE_TYPE_ETHERNET;
178 else if (g_strcmp0(bearer, "wifi") == 0)
179 return CONNMAN_SERVICE_TYPE_WIFI;
180 else if (g_strcmp0(bearer, "wimax") == 0)
181 return CONNMAN_SERVICE_TYPE_WIMAX;
182 else if (g_strcmp0(bearer, "bluetooth") == 0)
183 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
184 else if (g_strcmp0(bearer, "cellular") == 0)
185 return CONNMAN_SERVICE_TYPE_CELLULAR;
186 else if (g_strcmp0(bearer, "vpn") == 0)
187 return CONNMAN_SERVICE_TYPE_VPN;
189 return CONNMAN_SERVICE_TYPE_UNKNOWN;
192 static char *service2bearer(enum connman_service_type type)
195 case CONNMAN_SERVICE_TYPE_ETHERNET:
197 case CONNMAN_SERVICE_TYPE_WIFI:
199 case CONNMAN_SERVICE_TYPE_WIMAX:
201 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
203 case CONNMAN_SERVICE_TYPE_CELLULAR:
205 case CONNMAN_SERVICE_TYPE_VPN:
207 case CONNMAN_SERVICE_TYPE_UNKNOWN:
208 case CONNMAN_SERVICE_TYPE_SYSTEM:
209 case CONNMAN_SERVICE_TYPE_GPS:
210 case CONNMAN_SERVICE_TYPE_GADGET:
217 static int assign_policy_plugin(struct connman_session *session)
220 struct connman_session_policy *policy;
222 if (session->policy != NULL)
225 for (list = policy_list; list != NULL; list = list->next) {
228 session->policy = policy;
235 static int create_policy_config(struct connman_session *session)
237 struct connman_session_config *config;
239 config = (*session->policy->create)(session);
243 session->policy_config = config;
248 static void destroy_policy_config(struct connman_session *session)
250 (*session->policy->destroy)(session);
253 static void probe_policy(struct connman_session_policy *policy)
258 struct connman_session *session;
260 DBG("policy %p name %s", policy, policy->name);
262 g_hash_table_iter_init(&iter, session_hash);
264 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
267 if (session->policy != NULL)
270 assign_policy_plugin(session);
274 static void remove_policy(struct connman_session_policy *policy)
278 struct connman_session *session;
280 DBG("policy %p name %s", policy, policy->name);
282 g_hash_table_iter_init(&iter, session_hash);
284 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
287 if (session->policy != policy)
290 session->policy = NULL;
291 assign_policy_plugin(session);
295 static gint compare_priority(gconstpointer a, gconstpointer b)
297 const struct connman_session_policy *policy1 = a;
298 const struct connman_session_policy *policy2 = b;
300 return policy2->priority - policy1->priority;
304 int connman_session_policy_register(struct connman_session_policy *policy)
306 DBG("name %s", policy->name);
308 policy_list = g_slist_insert_sorted(policy_list, policy,
311 probe_policy(policy);
316 void connman_session_policy_unregister(struct connman_session_policy *policy)
318 DBG("name %s", policy->name);
320 policy_list = g_slist_remove(policy_list, policy);
322 remove_policy(policy);
325 static void cleanup_bearer(gpointer data, gpointer user_data)
327 struct connman_session_bearer *bearer = data;
329 g_free(bearer->name);
333 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
335 struct connman_session_bearer *bearer;
336 DBusMessageIter array;
339 dbus_message_iter_recurse(iter, &array);
341 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
342 char *bearer_name = NULL;
344 dbus_message_iter_get_basic(&array, &bearer_name);
346 bearer = g_try_new0(struct connman_session_bearer, 1);
347 if (bearer == NULL) {
348 g_slist_foreach(list, cleanup_bearer, NULL);
354 bearer->name = g_strdup(bearer_name);
355 bearer->service_type = bearer2service(bearer->name);
357 if (bearer->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
358 g_strcmp0(bearer->name, "*") == 0) {
359 bearer->match_all = TRUE;
361 bearer->match_all = FALSE;
364 list = g_slist_append(list, bearer);
366 dbus_message_iter_next(&array);
372 GSList *connman_session_allowed_bearers_any(void)
374 struct connman_session_bearer *bearer;
377 bearer = g_try_new0(struct connman_session_bearer, 1);
381 bearer->name = g_strdup("");
382 bearer->match_all = TRUE;
383 bearer->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
385 list = g_slist_append(list, bearer);
390 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
392 struct session_info *info = user_data;
395 for (list = info->config.allowed_bearers;
396 list != NULL; list = list->next) {
397 struct connman_session_bearer *bearer = list->data;
399 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
404 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
406 struct connman_service *service = user_data;
407 struct connman_ipconfig *ipconfig_ipv4;
412 if (__connman_service_is_connected_state(service,
413 CONNMAN_IPCONFIG_TYPE_IPV4) == FALSE) {
417 ipconfig_ipv4 = __connman_service_get_ip4config(service);
418 if (ipconfig_ipv4 == NULL)
421 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
424 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
426 struct connman_service *service = user_data;
427 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
432 if (__connman_service_is_connected_state(service,
433 CONNMAN_IPCONFIG_TYPE_IPV6) == FALSE) {
437 ipconfig_ipv4 = __connman_service_get_ip4config(service);
438 ipconfig_ipv6 = __connman_service_get_ip6config(service);
439 if (ipconfig_ipv6 == NULL)
442 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
445 static void append_notify(DBusMessageIter *dict,
446 struct connman_session *session)
448 struct session_info *info = session->info;
449 struct session_info *info_last = session->info_last;
450 struct connman_service *service;
451 const char *name, *ifname, *bearer;
453 if (session->append_all == TRUE ||
454 info->state != info_last->state) {
455 const char *state = state2string(info->state);
457 connman_dbus_dict_append_basic(dict, "State",
460 info_last->state = info->state;
463 if (session->append_all == TRUE ||
464 info->entry != info_last->entry) {
465 if (info->entry == NULL) {
471 name = info->entry->name;
472 ifname = info->entry->ifname;
473 service = info->entry->service;
474 bearer = info->entry->bearer;
477 connman_dbus_dict_append_basic(dict, "Name",
481 connman_dbus_dict_append_dict(dict, "IPv4",
482 append_ipconfig_ipv4,
485 connman_dbus_dict_append_dict(dict, "IPv6",
486 append_ipconfig_ipv6,
489 connman_dbus_dict_append_basic(dict, "Interface",
493 connman_dbus_dict_append_basic(dict, "Bearer",
497 info_last->entry = info->entry;
500 if (session->append_all == TRUE ||
501 info->config.type != info_last->config.type) {
502 const char *type = type2string(info->config.type);
504 connman_dbus_dict_append_basic(dict, "ConnectionType",
507 info_last->config.type = info->config.type;
510 if (session->append_all == TRUE ||
511 info->config.allowed_bearers != info_last->config.allowed_bearers) {
512 connman_dbus_dict_append_array(dict, "AllowedBearers",
514 append_allowed_bearers,
516 info_last->config.allowed_bearers = info->config.allowed_bearers;
519 session->append_all = FALSE;
522 static connman_bool_t is_type_matching_state(enum connman_session_state *state,
523 enum connman_session_type type)
526 case CONNMAN_SESSION_TYPE_ANY:
528 case CONNMAN_SESSION_TYPE_LOCAL:
529 if (*state >= CONNMAN_SESSION_STATE_CONNECTED) {
530 *state = CONNMAN_SESSION_STATE_CONNECTED;
535 case CONNMAN_SESSION_TYPE_INTERNET:
536 if (*state == CONNMAN_SESSION_STATE_ONLINE)
544 static connman_bool_t compute_notifiable_changes(struct connman_session *session)
546 struct session_info *info_last = session->info_last;
547 struct session_info *info = session->info;
549 if (session->append_all == TRUE)
552 if (info->state != info_last->state)
555 if (info->entry != info_last->entry &&
556 info->state >= CONNMAN_SESSION_STATE_CONNECTED)
559 if (info->config.allowed_bearers != info_last->config.allowed_bearers ||
560 info->config.type != info_last->config.type)
566 static gboolean session_notify(gpointer user_data)
568 struct connman_session *session = user_data;
570 DBusMessageIter array, dict;
572 if (compute_notifiable_changes(session) == FALSE)
575 DBG("session %p owner %s notify_path %s", session,
576 session->owner, session->notify_path);
578 msg = dbus_message_new_method_call(session->owner, session->notify_path,
579 CONNMAN_NOTIFICATION_INTERFACE,
584 dbus_message_iter_init_append(msg, &array);
585 connman_dbus_dict_open(&array, &dict);
587 append_notify(&dict, session);
589 connman_dbus_dict_close(&array, &dict);
591 g_dbus_send_message(connection, msg);
596 static void ipconfig_ipv4_changed(struct connman_session *session)
598 struct session_info *info = session->info;
600 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
601 "IPv4", append_ipconfig_ipv4,
602 info->entry->service);
605 static void ipconfig_ipv6_changed(struct connman_session *session)
607 struct session_info *info = session->info;
609 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
610 "IPv6", append_ipconfig_ipv6,
611 info->entry->service);
614 static connman_bool_t service_type_match(struct connman_session *session,
615 struct connman_service *service)
617 struct session_info *info = session->info;
620 for (list = info->config.allowed_bearers;
621 list != NULL; list = list->next) {
622 struct connman_session_bearer *bearer = list->data;
623 enum connman_service_type service_type;
625 if (bearer->match_all == TRUE)
628 service_type = connman_service_get_type(service);
629 if (bearer->service_type == service_type)
636 static connman_bool_t service_match(struct connman_session *session,
637 struct connman_service *service)
639 if (service_type_match(session, service) == FALSE)
645 static int service_type_weight(enum connman_service_type type)
648 * The session doesn't care which service
649 * to use. Nevertheless we have to sort them
650 * according their type. The ordering is
659 case CONNMAN_SERVICE_TYPE_ETHERNET:
661 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
663 case CONNMAN_SERVICE_TYPE_WIFI:
664 case CONNMAN_SERVICE_TYPE_WIMAX:
666 case CONNMAN_SERVICE_TYPE_CELLULAR:
668 case CONNMAN_SERVICE_TYPE_UNKNOWN:
669 case CONNMAN_SERVICE_TYPE_SYSTEM:
670 case CONNMAN_SERVICE_TYPE_GPS:
671 case CONNMAN_SERVICE_TYPE_VPN:
672 case CONNMAN_SERVICE_TYPE_GADGET:
679 static gint sort_allowed_bearers(struct connman_service *service_a,
680 struct connman_service *service_b,
681 struct connman_session *session)
683 struct session_info *info = session->info;
685 enum connman_service_type type_a, type_b;
686 int weight_a, weight_b;
688 type_a = connman_service_get_type(service_a);
689 type_b = connman_service_get_type(service_b);
691 for (list = info->config.allowed_bearers;
692 list != NULL; list = list->next) {
693 struct connman_session_bearer *bearer = list->data;
695 if (bearer->match_all == TRUE) {
696 if (type_a != type_b) {
697 weight_a = service_type_weight(type_a);
698 weight_b = service_type_weight(type_b);
700 if (weight_a > weight_b)
703 if (weight_a < weight_b)
710 if (type_a == bearer->service_type &&
711 type_b == bearer->service_type) {
715 if (type_a == bearer->service_type &&
716 type_b != bearer->service_type) {
720 if (type_a != bearer->service_type &&
721 type_b == bearer->service_type) {
729 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
731 struct service_entry *entry_a = (void *)a;
732 struct service_entry *entry_b = (void *)b;
733 struct connman_session *session = user_data;
735 return sort_allowed_bearers(entry_a->service, entry_b->service,
739 static void cleanup_session(gpointer user_data)
741 struct connman_session *session = user_data;
742 struct session_info *info = session->info;
744 DBG("remove %s", session->session_path);
746 g_hash_table_destroy(session->service_hash);
747 g_sequence_free(session->service_list);
749 if (info->entry != NULL &&
750 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
751 __connman_service_disconnect(info->entry->service);
754 destroy_policy_config(session);
755 g_slist_foreach(info->config.allowed_bearers, cleanup_bearer, NULL);
756 g_slist_free(info->config.allowed_bearers);
758 g_free(session->owner);
759 g_free(session->session_path);
760 g_free(session->notify_path);
761 g_free(session->info);
762 g_free(session->info_last);
767 static enum connman_session_state service_to_session_state(enum connman_service_state state)
770 case CONNMAN_SERVICE_STATE_UNKNOWN:
771 case CONNMAN_SERVICE_STATE_IDLE:
772 case CONNMAN_SERVICE_STATE_ASSOCIATION:
773 case CONNMAN_SERVICE_STATE_CONFIGURATION:
774 case CONNMAN_SERVICE_STATE_DISCONNECT:
775 case CONNMAN_SERVICE_STATE_FAILURE:
777 case CONNMAN_SERVICE_STATE_READY:
778 return CONNMAN_SESSION_STATE_CONNECTED;
779 case CONNMAN_SERVICE_STATE_ONLINE:
780 return CONNMAN_SESSION_STATE_ONLINE;
783 return CONNMAN_SESSION_STATE_DISCONNECTED;
786 static connman_bool_t is_connected(enum connman_service_state state)
789 case CONNMAN_SERVICE_STATE_UNKNOWN:
790 case CONNMAN_SERVICE_STATE_IDLE:
791 case CONNMAN_SERVICE_STATE_ASSOCIATION:
792 case CONNMAN_SERVICE_STATE_CONFIGURATION:
793 case CONNMAN_SERVICE_STATE_DISCONNECT:
794 case CONNMAN_SERVICE_STATE_FAILURE:
796 case CONNMAN_SERVICE_STATE_READY:
797 case CONNMAN_SERVICE_STATE_ONLINE:
804 static connman_bool_t is_connecting(enum connman_service_state state)
807 case CONNMAN_SERVICE_STATE_UNKNOWN:
808 case CONNMAN_SERVICE_STATE_IDLE:
810 case CONNMAN_SERVICE_STATE_ASSOCIATION:
811 case CONNMAN_SERVICE_STATE_CONFIGURATION:
813 case CONNMAN_SERVICE_STATE_DISCONNECT:
814 case CONNMAN_SERVICE_STATE_FAILURE:
815 case CONNMAN_SERVICE_STATE_READY:
816 case CONNMAN_SERVICE_STATE_ONLINE:
823 static connman_bool_t explicit_connect(enum connman_session_reason reason)
826 case CONNMAN_SESSION_REASON_UNKNOWN:
827 case CONNMAN_SESSION_REASON_FREE_RIDE:
829 case CONNMAN_SESSION_REASON_CONNECT:
836 static connman_bool_t explicit_disconnect(struct session_info *info)
838 if (info->entry == NULL)
841 DBG("reason %s service %p state %d",
842 reason2string(info->entry->reason),
843 info->entry->service, info->entry->state);
845 if (info->entry->reason == CONNMAN_SESSION_REASON_UNKNOWN)
848 if (explicit_connect(info->entry->reason) == FALSE)
851 if (__connman_service_session_dec(info->entry->service) == FALSE)
857 struct pending_data {
858 unsigned int timeout;
859 struct service_entry *entry;
860 gboolean (*cb)(gpointer);
863 static void pending_timeout_free(gpointer data, gpointer user_data)
865 struct pending_data *pending = data;
867 DBG("pending %p timeout %d", pending, pending->timeout);
868 g_source_remove(pending->timeout);
872 static void pending_timeout_remove_all(struct service_entry *entry)
876 g_slist_foreach(entry->pending_timeouts, pending_timeout_free, NULL);
877 g_slist_free(entry->pending_timeouts);
878 entry->pending_timeouts = NULL;
881 static gboolean pending_timeout_cb(gpointer data)
883 struct pending_data *pending = data;
884 struct service_entry *entry = pending->entry;
887 DBG("pending %p timeout %d", pending, pending->timeout);
889 ret = pending->cb(pending->entry);
891 entry->pending_timeouts =
892 g_slist_remove(entry->pending_timeouts,
899 static connman_bool_t pending_timeout_add(unsigned int seconds,
900 gboolean (*cb)(gpointer),
901 struct service_entry *entry)
903 struct pending_data *pending = g_try_new0(struct pending_data, 1);
905 if (pending == NULL || cb == NULL || entry == NULL) {
911 pending->entry = entry;
912 pending->timeout = g_timeout_add_seconds(seconds, pending_timeout_cb,
914 entry->pending_timeouts = g_slist_prepend(entry->pending_timeouts,
917 DBG("pending %p entry %p timeout id %d", pending, entry,
923 static gboolean call_disconnect(gpointer user_data)
925 struct service_entry *entry = user_data;
926 struct connman_service *service = entry->service;
929 * TODO: We should mark this entry as pending work. In case
930 * disconnect fails we just unassign this session from the
931 * service and can't do anything later on it
933 DBG("disconnect service %p", service);
934 __connman_service_disconnect(service);
939 static gboolean call_connect(gpointer user_data)
941 struct service_entry *entry = user_data;
942 struct connman_service *service = entry->service;
944 DBG("connect service %p", service);
945 __connman_service_connect(service);
950 static void deselect_service(struct session_info *info)
952 struct service_entry *entry;
953 connman_bool_t disconnect, connected;
957 if (info->entry == NULL)
960 disconnect = explicit_disconnect(info);
962 connected = is_connecting(info->entry->state) == TRUE ||
963 is_connected(info->entry->state) == TRUE;
965 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
966 info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
971 DBG("disconnect %d connected %d", disconnect, connected);
973 if (disconnect == TRUE && connected == TRUE)
974 pending_timeout_add(0, call_disconnect, entry);
977 static void deselect_and_disconnect(struct connman_session *session)
979 struct session_info *info = session->info;
981 deselect_service(info);
983 info->reason = CONNMAN_SESSION_REASON_FREE_RIDE;
986 static void select_connected_service(struct session_info *info,
987 struct service_entry *entry)
989 enum connman_session_state state;
991 state = service_to_session_state(entry->state);
992 if (is_type_matching_state(&state, info->config.type) == FALSE)
998 info->entry->reason = info->reason;
1000 if (explicit_connect(info->reason) == FALSE)
1003 __connman_service_session_inc(info->entry->service);
1006 static void select_offline_service(struct session_info *info,
1007 struct service_entry *entry)
1009 if (explicit_connect(info->reason) == FALSE)
1012 info->state = service_to_session_state(entry->state);
1014 info->entry = entry;
1015 info->entry->reason = info->reason;
1017 __connman_service_session_inc(info->entry->service);
1018 pending_timeout_add(0, call_connect, entry);
1021 static void select_service(struct session_info *info,
1022 struct service_entry *entry)
1024 DBG("service %p", entry->service);
1026 if (is_connected(entry->state) == TRUE)
1027 select_connected_service(info, entry);
1029 select_offline_service(info, entry);
1032 static void select_and_connect(struct connman_session *session,
1033 enum connman_session_reason reason)
1035 struct session_info *info = session->info;
1036 struct service_entry *entry = NULL;
1037 GSequenceIter *iter;
1039 DBG("session %p reason %s", session, reason2string(reason));
1041 info->reason = reason;
1043 iter = g_sequence_get_begin_iter(session->service_list);
1045 while (g_sequence_iter_is_end(iter) == FALSE) {
1046 entry = g_sequence_get(iter);
1048 switch (entry->state) {
1049 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1050 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1051 case CONNMAN_SERVICE_STATE_READY:
1052 case CONNMAN_SERVICE_STATE_ONLINE:
1053 case CONNMAN_SERVICE_STATE_IDLE:
1054 case CONNMAN_SERVICE_STATE_DISCONNECT:
1055 select_service(info, entry);
1057 case CONNMAN_SERVICE_STATE_UNKNOWN:
1058 case CONNMAN_SERVICE_STATE_FAILURE:
1062 iter = g_sequence_iter_next(iter);
1066 static struct service_entry *create_service_entry(struct connman_service *service,
1068 enum connman_service_state state)
1070 struct service_entry *entry;
1071 enum connman_service_type type;
1074 entry = g_try_new0(struct service_entry, 1);
1078 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1079 entry->state = state;
1084 entry->service = service;
1086 idx = __connman_service_get_index(entry->service);
1087 entry->ifname = connman_inet_ifname(idx);
1088 if (entry->ifname == NULL)
1089 entry->ifname = g_strdup("");
1091 type = connman_service_get_type(entry->service);
1092 entry->bearer = service2bearer(type);
1097 static void destroy_service_entry(gpointer data)
1099 struct service_entry *entry = data;
1101 pending_timeout_remove_all(entry);
1102 g_free(entry->ifname);
1107 static void populate_service_list(struct connman_session *session)
1109 struct service_entry *entry;
1110 GSequenceIter *iter;
1112 session->service_hash =
1113 g_hash_table_new_full(g_direct_hash, g_direct_equal,
1115 session->service_list = __connman_service_get_list(session,
1117 create_service_entry,
1118 destroy_service_entry);
1120 g_sequence_sort(session->service_list, sort_services, session);
1122 iter = g_sequence_get_begin_iter(session->service_list);
1124 while (g_sequence_iter_is_end(iter) == FALSE) {
1125 entry = g_sequence_get(iter);
1127 DBG("service %p type %s name %s", entry->service,
1128 service2bearer(connman_service_get_type(entry->service)),
1131 g_hash_table_replace(session->service_hash,
1132 entry->service, iter);
1134 iter = g_sequence_iter_next(iter);
1138 static void session_changed(struct connman_session *session,
1139 enum connman_session_trigger trigger)
1141 struct session_info *info = session->info;
1142 struct session_info *info_last = session->info_last;
1143 GSequenceIter *service_iter = NULL, *service_iter_last = NULL;
1144 GSequence *service_list_last;
1145 GHashTable *service_hash_last;
1148 * TODO: This only a placeholder for the 'real' algorithm to
1149 * play a bit around. So we are going to improve it step by step.
1152 DBG("session %p trigger %s reason %s", session, trigger2string(trigger),
1153 reason2string(info->reason));
1155 if (info->entry != NULL) {
1156 enum connman_session_state state;
1158 state = service_to_session_state(info->entry->state);
1160 if (is_type_matching_state(&state, info->config.type) == TRUE)
1161 info->state = state;
1165 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
1166 DBG("ignore session changed event");
1168 case CONNMAN_SESSION_TRIGGER_SETTING:
1169 if (info->config.allowed_bearers != info_last->config.allowed_bearers) {
1171 service_hash_last = session->service_hash;
1172 service_list_last = session->service_list;
1174 populate_service_list(session);
1176 if (info->entry != NULL) {
1177 service_iter_last = g_hash_table_lookup(
1179 info->entry->service);
1180 service_iter = g_hash_table_lookup(
1181 session->service_hash,
1182 info->entry->service);
1185 if (service_iter == NULL && service_iter_last != NULL) {
1187 * The currently selected service is
1188 * not part of this session anymore.
1190 deselect_and_disconnect(session);
1193 g_hash_table_remove_all(service_hash_last);
1194 g_sequence_free(service_list_last);
1197 if (info->config.type != info_last->config.type) {
1198 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED &&
1199 is_type_matching_state(&info->state,
1200 info->config.type) == FALSE)
1201 deselect_and_disconnect(session);
1204 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED) {
1205 select_and_connect(session,
1206 CONNMAN_SESSION_REASON_FREE_RIDE);
1210 case CONNMAN_SESSION_TRIGGER_ECALL:
1212 * For the time beeing we fallback to normal connect
1215 case CONNMAN_SESSION_TRIGGER_CONNECT:
1216 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED) {
1217 if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
1219 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
1220 __connman_service_session_inc(info->entry->service);
1224 if (info->entry != NULL &&
1225 is_connecting(info->entry->state) == TRUE) {
1229 select_and_connect(session,
1230 CONNMAN_SESSION_REASON_CONNECT);
1233 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
1234 deselect_and_disconnect(session);
1237 case CONNMAN_SESSION_TRIGGER_SERVICE:
1238 if (info->entry != NULL &&
1239 (is_connecting(info->entry->state) == TRUE ||
1240 is_connected(info->entry->state) == TRUE)) {
1244 deselect_and_disconnect(session);
1246 if (info->reason == CONNMAN_SESSION_REASON_FREE_RIDE) {
1247 select_and_connect(session, info->reason);
1253 session_notify(session);
1256 static DBusMessage *connect_session(DBusConnection *conn,
1257 DBusMessage *msg, void *user_data)
1259 struct connman_session *session = user_data;
1261 DBG("session %p", session);
1263 if (ecall_session != NULL) {
1264 if (ecall_session->ecall == TRUE && ecall_session != session)
1265 return __connman_error_failed(msg, EBUSY);
1267 session->ecall = TRUE;
1268 session_changed(session, CONNMAN_SESSION_TRIGGER_ECALL);
1270 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1272 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1275 static DBusMessage *disconnect_session(DBusConnection *conn,
1276 DBusMessage *msg, void *user_data)
1278 struct connman_session *session = user_data;
1280 DBG("session %p", session);
1282 if (ecall_session != NULL) {
1283 if (ecall_session->ecall == TRUE && ecall_session != session)
1284 return __connman_error_failed(msg, EBUSY);
1286 session->ecall = FALSE;
1289 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1291 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1294 static DBusMessage *change_session(DBusConnection *conn,
1295 DBusMessage *msg, void *user_data)
1297 struct connman_session *session = user_data;
1298 struct session_info *info = session->info;
1299 DBusMessageIter iter, value;
1302 GSList *allowed_bearers;
1304 DBG("session %p", session);
1305 if (dbus_message_iter_init(msg, &iter) == FALSE)
1306 return __connman_error_invalid_arguments(msg);
1308 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1309 return __connman_error_invalid_arguments(msg);
1311 dbus_message_iter_get_basic(&iter, &name);
1312 dbus_message_iter_next(&iter);
1314 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1315 return __connman_error_invalid_arguments(msg);
1317 dbus_message_iter_recurse(&iter, &value);
1319 switch (dbus_message_iter_get_arg_type(&value)) {
1320 case DBUS_TYPE_ARRAY:
1321 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1322 allowed_bearers = session_parse_allowed_bearers(&value);
1324 g_slist_foreach(info->config.allowed_bearers,
1325 cleanup_bearer, NULL);
1326 g_slist_free(info->config.allowed_bearers);
1328 if (allowed_bearers == NULL) {
1329 allowed_bearers = connman_session_allowed_bearers_any();
1331 if (allowed_bearers == NULL)
1332 return __connman_error_failed(msg, ENOMEM);
1335 info->config.allowed_bearers = allowed_bearers;
1340 case DBUS_TYPE_STRING:
1341 if (g_str_equal(name, "ConnectionType") == TRUE) {
1342 dbus_message_iter_get_basic(&value, &val);
1343 info->config.type = string2type(val);
1352 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1354 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1357 return __connman_error_invalid_arguments(msg);
1360 static void release_session(gpointer key, gpointer value, gpointer user_data)
1362 struct connman_session *session = value;
1363 DBusMessage *message;
1365 DBG("owner %s path %s", session->owner, session->notify_path);
1367 if (session->notify_watch > 0)
1368 g_dbus_remove_watch(connection, session->notify_watch);
1370 g_dbus_unregister_interface(connection, session->session_path,
1371 CONNMAN_SESSION_INTERFACE);
1373 message = dbus_message_new_method_call(session->owner,
1374 session->notify_path,
1375 CONNMAN_NOTIFICATION_INTERFACE,
1377 if (message == NULL)
1380 dbus_message_set_no_reply(message, TRUE);
1382 g_dbus_send_message(connection, message);
1385 static int session_disconnect(struct connman_session *session)
1387 DBG("session %p, %s", session, session->owner);
1389 if (session->notify_watch > 0)
1390 g_dbus_remove_watch(connection, session->notify_watch);
1392 g_dbus_unregister_interface(connection, session->session_path,
1393 CONNMAN_SESSION_INTERFACE);
1395 deselect_and_disconnect(session);
1397 g_hash_table_remove(session_hash, session->session_path);
1402 static void owner_disconnect(DBusConnection *conn, void *user_data)
1404 struct connman_session *session = user_data;
1406 DBG("session %p, %s died", session, session->owner);
1408 session_disconnect(session);
1411 static DBusMessage *destroy_session(DBusConnection *conn,
1412 DBusMessage *msg, void *user_data)
1414 struct connman_session *session = user_data;
1416 DBG("session %p", session);
1418 if (ecall_session != NULL && ecall_session != session)
1419 return __connman_error_failed(msg, EBUSY);
1421 session_disconnect(session);
1423 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1426 static const GDBusMethodTable session_methods[] = {
1427 { GDBUS_METHOD("Destroy", NULL, NULL, destroy_session) },
1428 { GDBUS_METHOD("Connect", NULL, NULL, connect_session) },
1429 { GDBUS_METHOD("Disconnect", NULL, NULL,
1430 disconnect_session ) },
1431 { GDBUS_METHOD("Change",
1432 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1433 NULL, change_session) },
1437 int __connman_session_create(DBusMessage *msg)
1439 const char *owner, *notify_path;
1440 char *session_path = NULL;
1441 DBusMessageIter iter, array;
1442 struct connman_session *session = NULL;
1443 struct session_info *info, *info_last;
1444 enum connman_session_type type = CONNMAN_SESSION_TYPE_ANY;
1445 GSList *allowed_bearers = NULL;
1448 owner = dbus_message_get_sender(msg);
1450 DBG("owner %s", owner);
1452 if (ecall_session != NULL && ecall_session->ecall == TRUE) {
1454 * If there is an emergency call already going on,
1455 * ignore session creation attempt
1461 dbus_message_iter_init(msg, &iter);
1462 dbus_message_iter_recurse(&iter, &array);
1464 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1465 DBusMessageIter entry, value;
1466 const char *key, *val;
1468 dbus_message_iter_recurse(&array, &entry);
1469 dbus_message_iter_get_basic(&entry, &key);
1471 dbus_message_iter_next(&entry);
1472 dbus_message_iter_recurse(&entry, &value);
1474 switch (dbus_message_iter_get_arg_type(&value)) {
1475 case DBUS_TYPE_ARRAY:
1476 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1478 session_parse_allowed_bearers(&value);
1483 case DBUS_TYPE_STRING:
1484 if (g_str_equal(key, "ConnectionType") == TRUE) {
1485 dbus_message_iter_get_basic(&value, &val);
1486 type = string2type(val);
1491 dbus_message_iter_next(&array);
1494 dbus_message_iter_next(&iter);
1495 dbus_message_iter_get_basic(&iter, ¬ify_path);
1497 if (notify_path == NULL) {
1502 session_path = g_strdup_printf("/sessions%s", notify_path);
1503 if (session_path == NULL) {
1508 session = g_hash_table_lookup(session_hash, session_path);
1509 if (session != NULL) {
1515 session = g_try_new0(struct connman_session, 1);
1516 if (session == NULL) {
1521 session->info = g_try_new0(struct session_info, 1);
1522 if (session->info == NULL) {
1527 session->info_last = g_try_new0(struct session_info, 1);
1528 if (session->info_last == NULL) {
1533 info = session->info;
1534 info_last = session->info_last;
1536 session->owner = g_strdup(owner);
1537 session->session_path = session_path;
1538 session->notify_path = g_strdup(notify_path);
1539 session->notify_watch =
1540 g_dbus_add_disconnect_watch(connection, session->owner,
1541 owner_disconnect, session, NULL);
1543 err = assign_policy_plugin(session);
1546 err = create_policy_config(session);
1550 if (session->policy_config->ecall == TRUE)
1551 ecall_session = session;
1553 info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1554 info->config.type = type;
1555 info->config.priority = session->policy_config->priority;
1556 info->config.roaming_policy = session->policy_config->roaming_policy;
1559 if (allowed_bearers == NULL) {
1560 info->config.allowed_bearers =
1561 connman_session_allowed_bearers_any();
1563 if (info->config.allowed_bearers == NULL) {
1568 info->config.allowed_bearers = allowed_bearers;
1571 g_hash_table_replace(session_hash, session->session_path, session);
1573 DBG("add %s", session->session_path);
1575 if (g_dbus_register_interface(connection, session->session_path,
1576 CONNMAN_SESSION_INTERFACE,
1577 session_methods, NULL,
1578 NULL, session, NULL) == FALSE) {
1579 connman_error("Failed to register %s", session->session_path);
1580 g_hash_table_remove(session_hash, session->session_path);
1587 g_dbus_send_reply(connection, msg,
1588 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1592 populate_service_list(session);
1594 info_last->state = info->state;
1595 info_last->config.priority = info->config.priority;
1596 info_last->config.roaming_policy = info->config.roaming_policy;
1597 info_last->entry = info->entry;
1598 info_last->config.allowed_bearers = info->config.allowed_bearers;
1600 session->append_all = TRUE;
1602 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1607 connman_error("Failed to create session");
1609 if (session != NULL) {
1610 if (session->info_last != NULL)
1611 g_free(session->info_last);
1612 if (session->info != NULL)
1613 g_free(session->info);
1617 g_free(session_path);
1619 g_slist_foreach(allowed_bearers, cleanup_bearer, NULL);
1620 g_slist_free(allowed_bearers);
1625 int __connman_session_destroy(DBusMessage *msg)
1627 const char *owner, *session_path;
1628 struct connman_session *session;
1630 owner = dbus_message_get_sender(msg);
1632 DBG("owner %s", owner);
1634 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1636 if (session_path == NULL)
1639 session = g_hash_table_lookup(session_hash, session_path);
1640 if (session == NULL)
1643 if (g_strcmp0(owner, session->owner) != 0)
1646 session_disconnect(session);
1651 connman_bool_t __connman_session_mode()
1656 void __connman_session_set_mode(connman_bool_t enable)
1658 DBG("enable %d", enable);
1660 if (sessionmode != enable) {
1661 sessionmode = enable;
1663 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
1664 CONNMAN_MANAGER_INTERFACE, "SessionMode",
1665 DBUS_TYPE_BOOLEAN, &sessionmode);
1668 if (sessionmode == TRUE)
1669 __connman_service_disconnect_all();
1672 static void service_add(struct connman_service *service,
1675 GHashTableIter iter;
1676 GSequenceIter *iter_service_list;
1677 gpointer key, value;
1678 struct connman_session *session;
1679 struct service_entry *entry;
1681 DBG("service %p", service);
1683 g_hash_table_iter_init(&iter, session_hash);
1685 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1688 if (service_match(session, service) == FALSE)
1691 entry = create_service_entry(service, name,
1692 CONNMAN_SERVICE_STATE_IDLE);
1697 g_sequence_insert_sorted(session->service_list,
1698 entry, sort_services,
1701 g_hash_table_replace(session->service_hash, service,
1704 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1708 static void service_remove(struct connman_service *service)
1711 GHashTableIter iter;
1712 gpointer key, value;
1713 struct connman_session *session;
1714 struct session_info *info;
1716 DBG("service %p", service);
1718 g_hash_table_iter_init(&iter, session_hash);
1720 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1721 GSequenceIter *seq_iter;
1723 info = session->info;
1725 seq_iter = g_hash_table_lookup(session->service_hash, service);
1726 if (seq_iter == NULL)
1729 g_sequence_remove(seq_iter);
1731 if (info->entry != NULL && info->entry->service == service)
1733 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1737 static void service_state_changed(struct connman_service *service,
1738 enum connman_service_state state)
1740 GHashTableIter iter;
1741 gpointer key, value;
1743 DBG("service %p state %d", service, state);
1745 g_hash_table_iter_init(&iter, session_hash);
1747 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1748 struct connman_session *session = value;
1749 GSequenceIter *service_iter;
1751 service_iter = g_hash_table_lookup(session->service_hash, service);
1752 if (service_iter != NULL) {
1753 struct service_entry *entry;
1755 entry = g_sequence_get(service_iter);
1756 entry->state = state;
1759 session_changed(session,
1760 CONNMAN_SESSION_TRIGGER_SERVICE);
1764 static void ipconfig_changed(struct connman_service *service,
1765 struct connman_ipconfig *ipconfig)
1767 GHashTableIter iter;
1768 gpointer key, value;
1769 struct connman_session *session;
1770 struct session_info *info;
1771 enum connman_ipconfig_type type;
1773 DBG("service %p ipconfig %p", service, ipconfig);
1775 type = __connman_ipconfig_get_config_type(ipconfig);
1777 g_hash_table_iter_init(&iter, session_hash);
1779 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1781 info = session->info;
1783 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
1786 if (info->entry != NULL && info->entry->service == service) {
1787 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1788 ipconfig_ipv4_changed(session);
1789 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1790 ipconfig_ipv6_changed(session);
1795 static struct connman_notifier session_notifier = {
1797 .service_add = service_add,
1798 .service_remove = service_remove,
1799 .service_state_changed = service_state_changed,
1800 .ipconfig_changed = ipconfig_changed,
1803 int __connman_session_init(void)
1809 connection = connman_dbus_get_connection();
1810 if (connection == NULL)
1813 err = connman_notifier_register(&session_notifier);
1815 dbus_connection_unref(connection);
1819 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1820 NULL, cleanup_session);
1822 sessionmode = FALSE;
1826 void __connman_session_cleanup(void)
1830 if (connection == NULL)
1833 connman_notifier_unregister(&session_notifier);
1835 g_hash_table_foreach(session_hash, release_session, NULL);
1836 g_hash_table_destroy(session_hash);
1837 session_hash = NULL;
1839 dbus_connection_unref(connection);