5 * Copyright (C) 2007-2010 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
33 static DBusConnection *connection;
34 static GHashTable *session_hash;
35 static connman_bool_t sessionmode;
36 static struct connman_session *ecall_session;
38 enum connman_session_trigger {
39 CONNMAN_SESSION_TRIGGER_UNKNOWN = 0,
40 CONNMAN_SESSION_TRIGGER_SETTING = 1,
41 CONNMAN_SESSION_TRIGGER_CONNECT = 2,
42 CONNMAN_SESSION_TRIGGER_DISCONNECT = 3,
43 CONNMAN_SESSION_TRIGGER_PERIODIC = 4,
44 CONNMAN_SESSION_TRIGGER_SERVICE = 5,
45 CONNMAN_SESSION_TRIGGER_ECALL = 6,
48 enum connman_session_reason {
49 CONNMAN_SESSION_REASON_UNKNOWN = 0,
50 CONNMAN_SESSION_REASON_CONNECT = 1,
51 CONNMAN_SESSION_REASON_FREE_RIDE = 2,
52 CONNMAN_SESSION_REASON_PERIODIC = 3,
55 enum connman_session_roaming_policy {
56 CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN = 0,
57 CONNMAN_SESSION_ROAMING_POLICY_DEFAULT = 1,
58 CONNMAN_SESSION_ROAMING_POLICY_ALWAYS = 2,
59 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN = 3,
60 CONNMAN_SESSION_ROAMING_POLICY_NATIONAL = 4,
61 CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL = 5,
64 struct service_entry {
65 /* track why this service was selected */
66 enum connman_session_reason reason;
67 enum connman_service_state state;
69 struct connman_service *service;
75 connman_bool_t online;
76 connman_bool_t priority;
77 GSList *allowed_bearers;
78 connman_bool_t avoid_handover;
79 connman_bool_t stay_connected;
80 unsigned int periodic_connect;
81 unsigned int idle_timeout;
83 enum connman_session_roaming_policy roaming_policy;
86 struct service_entry *entry;
87 enum connman_session_reason reason;
90 struct connman_session {
96 connman_bool_t append_all;
97 connman_bool_t info_dirty;
98 struct session_info *info;
99 struct session_info *info_last;
101 GSequence *service_list;
102 GHashTable *service_hash;
107 connman_bool_t match_all;
108 enum connman_service_type service_type;
111 static const char *trigger2string(enum connman_session_trigger trigger)
114 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
116 case CONNMAN_SESSION_TRIGGER_SETTING:
118 case CONNMAN_SESSION_TRIGGER_CONNECT:
120 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
122 case CONNMAN_SESSION_TRIGGER_PERIODIC:
124 case CONNMAN_SESSION_TRIGGER_SERVICE:
126 case CONNMAN_SESSION_TRIGGER_ECALL:
133 static const char *reason2string(enum connman_session_reason reason)
136 case CONNMAN_SESSION_REASON_UNKNOWN:
138 case CONNMAN_SESSION_REASON_CONNECT:
140 case CONNMAN_SESSION_REASON_FREE_RIDE:
142 case CONNMAN_SESSION_REASON_PERIODIC:
149 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
152 case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
154 case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
156 case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
158 case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
160 case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
162 case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
163 return "international";
169 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
171 if (g_strcmp0(policy, "default") == 0)
172 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
173 else if (g_strcmp0(policy, "always") == 0)
174 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
175 else if (g_strcmp0(policy, "forbidden") == 0)
176 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
177 else if (g_strcmp0(policy, "national") == 0)
178 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
179 else if (g_strcmp0(policy, "international") == 0)
180 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
182 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
185 static enum connman_service_type bearer2service(const char *bearer)
188 return CONNMAN_SERVICE_TYPE_UNKNOWN;
190 if (g_strcmp0(bearer, "ethernet") == 0)
191 return CONNMAN_SERVICE_TYPE_ETHERNET;
192 else if (g_strcmp0(bearer, "wifi") == 0)
193 return CONNMAN_SERVICE_TYPE_WIFI;
194 else if (g_strcmp0(bearer, "wimax") == 0)
195 return CONNMAN_SERVICE_TYPE_WIMAX;
196 else if (g_strcmp0(bearer, "bluetooth") == 0)
197 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
198 else if (g_strcmp0(bearer, "3g") == 0)
199 return CONNMAN_SERVICE_TYPE_CELLULAR;
200 else if (g_strcmp0(bearer, "vpn") == 0)
201 return CONNMAN_SERVICE_TYPE_VPN;
203 return CONNMAN_SERVICE_TYPE_UNKNOWN;
206 static char *service2bearer(enum connman_service_type type)
209 case CONNMAN_SERVICE_TYPE_ETHERNET:
211 case CONNMAN_SERVICE_TYPE_WIFI:
213 case CONNMAN_SERVICE_TYPE_WIMAX:
215 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
217 case CONNMAN_SERVICE_TYPE_CELLULAR:
219 case CONNMAN_SERVICE_TYPE_VPN:
221 case CONNMAN_SERVICE_TYPE_UNKNOWN:
222 case CONNMAN_SERVICE_TYPE_SYSTEM:
223 case CONNMAN_SERVICE_TYPE_GPS:
224 case CONNMAN_SERVICE_TYPE_GADGET:
231 static void cleanup_bearer_info(gpointer data, gpointer user_data)
233 struct bearer_info *info = data;
239 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
241 struct bearer_info *info;
242 DBusMessageIter array;
245 dbus_message_iter_recurse(iter, &array);
247 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
250 dbus_message_iter_get_basic(&array, &bearer);
252 info = g_try_new0(struct bearer_info, 1);
254 g_slist_foreach(list, cleanup_bearer_info, NULL);
260 info->name = g_strdup(bearer);
261 info->service_type = bearer2service(info->name);
263 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
264 g_strcmp0(info->name, "*") == 0) {
265 info->match_all = TRUE;
267 info->match_all = FALSE;
270 list = g_slist_append(list, info);
272 dbus_message_iter_next(&array);
278 static GSList *session_allowed_bearers_any(void)
280 struct bearer_info *info;
283 info = g_try_new0(struct bearer_info, 1);
290 info->name = g_strdup("");
291 info->match_all = TRUE;
292 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
294 list = g_slist_append(list, info);
299 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
301 struct session_info *info = user_data;
304 for (list = info->allowed_bearers;
305 list != NULL; list = list->next) {
306 struct bearer_info *info = list->data;
308 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
313 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
315 struct connman_service *service = user_data;
316 struct connman_ipconfig *ipconfig_ipv4;
321 ipconfig_ipv4 = __connman_service_get_ip4config(service);
322 if (ipconfig_ipv4 == NULL)
325 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
328 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
330 struct connman_service *service = user_data;
331 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
336 ipconfig_ipv4 = __connman_service_get_ip4config(service);
337 ipconfig_ipv6 = __connman_service_get_ip6config(service);
338 if (ipconfig_ipv6 == NULL)
341 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
344 static void append_notify(DBusMessageIter *dict,
345 struct connman_session *session)
347 struct session_info *info = session->info;
348 struct session_info *info_last = session->info_last;
350 struct connman_service *service;
351 const char *name, *ifname, *bearer;
353 if (session->append_all == TRUE ||
354 info->online != info_last->online) {
355 connman_dbus_dict_append_basic(dict, "Online",
358 info_last->online = info->online;
361 if (session->append_all == TRUE ||
362 info->entry != info_last->entry) {
363 if (info->entry == NULL) {
369 name = info->entry->name;
370 ifname = info->entry->ifname;
371 service = info->entry->service;
372 bearer = info->entry->bearer;
375 connman_dbus_dict_append_basic(dict, "Name",
379 connman_dbus_dict_append_dict(dict, "IPv4",
380 append_ipconfig_ipv4,
383 connman_dbus_dict_append_dict(dict, "IPv6",
384 append_ipconfig_ipv6,
387 connman_dbus_dict_append_basic(dict, "Interface",
391 connman_dbus_dict_append_basic(dict, "Bearer",
395 info_last->entry = info->entry;
399 if (session->append_all == TRUE ||
400 info->priority != info_last->priority) {
401 connman_dbus_dict_append_basic(dict, "Priority",
404 info_last->priority = info->priority;
407 if (session->append_all == TRUE ||
408 info->allowed_bearers != info_last->allowed_bearers) {
409 connman_dbus_dict_append_array(dict, "AllowedBearers",
411 append_allowed_bearers,
413 info_last->allowed_bearers = info->allowed_bearers;
416 if (session->append_all == TRUE ||
417 info->avoid_handover != info_last->avoid_handover) {
418 connman_dbus_dict_append_basic(dict, "AvoidHandover",
420 &info->avoid_handover);
421 info_last->avoid_handover = info->avoid_handover;
424 if (session->append_all == TRUE ||
425 info->stay_connected != info_last->stay_connected) {
426 connman_dbus_dict_append_basic(dict, "StayConnected",
428 &info->stay_connected);
429 info_last->stay_connected = info->stay_connected;
432 if (session->append_all == TRUE ||
433 info->periodic_connect != info_last->periodic_connect) {
434 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
436 &info->periodic_connect);
437 info_last->periodic_connect = info->periodic_connect;
440 if (session->append_all == TRUE ||
441 info->idle_timeout != info_last->idle_timeout) {
442 connman_dbus_dict_append_basic(dict, "IdleTimeout",
444 &info->idle_timeout);
445 info_last->idle_timeout = info->idle_timeout;
448 if (session->append_all == TRUE ||
449 info->ecall != info_last->ecall) {
450 connman_dbus_dict_append_basic(dict, "EmergencyCall",
453 info_last->ecall = info->ecall;
456 if (session->append_all == TRUE ||
457 info->roaming_policy != info_last->roaming_policy) {
458 policy = roamingpolicy2string(info->roaming_policy);
459 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
462 info_last->roaming_policy = info->roaming_policy;
465 if (session->append_all == TRUE ||
466 info->marker != info_last->marker) {
467 connman_dbus_dict_append_basic(dict, "SessionMarker",
470 info_last->marker = info->marker;
473 session->append_all = FALSE;
474 session->info_dirty = FALSE;
477 static gboolean session_notify(gpointer user_data)
479 struct connman_session *session = user_data;
481 DBusMessageIter array, dict;
483 if (session->info_dirty == FALSE)
486 DBG("session %p owner %s notify_path %s", session,
487 session->owner, session->notify_path);
489 msg = dbus_message_new_method_call(session->owner, session->notify_path,
490 CONNMAN_NOTIFICATION_INTERFACE,
495 dbus_message_iter_init_append(msg, &array);
496 connman_dbus_dict_open(&array, &dict);
498 append_notify(&dict, session);
500 connman_dbus_dict_close(&array, &dict);
502 g_dbus_send_message(connection, msg);
504 session->info_dirty = FALSE;
509 static void ipconfig_ipv4_changed(struct connman_session *session)
511 struct session_info *info = session->info;
513 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
514 "IPv4", append_ipconfig_ipv4,
515 info->entry->service);
518 static void ipconfig_ipv6_changed(struct connman_session *session)
520 struct session_info *info = session->info;
522 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
523 "IPv6", append_ipconfig_ipv6,
524 info->entry->service);
527 static connman_bool_t service_type_match(struct connman_session *session,
528 struct connman_service *service)
530 struct session_info *info = session->info;
533 for (list = info->allowed_bearers;
534 list != NULL; list = list->next) {
535 struct bearer_info *info = list->data;
536 enum connman_service_type service_type;
538 if (info->match_all == TRUE)
541 service_type = connman_service_get_type(service);
542 if (info->service_type == service_type)
549 static connman_bool_t service_match(struct connman_session *session,
550 struct connman_service *service)
552 if (service_type_match(session, service) == FALSE)
558 static int service_type_weight(enum connman_service_type type)
561 * The session doesn't care which service
562 * to use. Nevertheless we have to sort them
563 * according their type. The ordering is
572 case CONNMAN_SERVICE_TYPE_ETHERNET:
574 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
576 case CONNMAN_SERVICE_TYPE_WIFI:
577 case CONNMAN_SERVICE_TYPE_WIMAX:
579 case CONNMAN_SERVICE_TYPE_CELLULAR:
581 case CONNMAN_SERVICE_TYPE_UNKNOWN:
582 case CONNMAN_SERVICE_TYPE_SYSTEM:
583 case CONNMAN_SERVICE_TYPE_GPS:
584 case CONNMAN_SERVICE_TYPE_VPN:
585 case CONNMAN_SERVICE_TYPE_GADGET:
592 static gint sort_allowed_bearers(struct connman_service *service_a,
593 struct connman_service *service_b,
594 struct connman_session *session)
596 struct session_info *info = session->info;
598 enum connman_service_type type_a, type_b;
599 int weight_a, weight_b;
601 type_a = connman_service_get_type(service_a);
602 type_b = connman_service_get_type(service_b);
604 for (list = info->allowed_bearers;
605 list != NULL; list = list->next) {
606 struct bearer_info *info = list->data;
608 if (info->match_all == TRUE) {
609 if (type_a != type_b) {
610 weight_a = service_type_weight(type_a);
611 weight_b = service_type_weight(type_b);
613 if (weight_a > weight_b)
616 if (weight_a < weight_b)
623 if (type_a == info->service_type &&
624 type_b == info->service_type) {
628 if (type_a == info->service_type &&
629 type_b != info->service_type) {
633 if (type_a != info->service_type &&
634 type_b == info->service_type) {
642 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
644 struct service_entry *entry_a = (void *)a;
645 struct service_entry *entry_b = (void *)b;
646 struct connman_session *session = user_data;
648 return sort_allowed_bearers(entry_a->service, entry_b->service,
652 static void cleanup_session(gpointer user_data)
654 struct connman_session *session = user_data;
655 struct session_info *info = session->info;
657 DBG("remove %s", session->session_path);
659 g_hash_table_destroy(session->service_hash);
660 g_sequence_free(session->service_list);
662 if (info->entry != NULL &&
663 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
664 __connman_service_disconnect(info->entry->service);
667 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
668 g_slist_free(info->allowed_bearers);
670 g_free(session->owner);
671 g_free(session->session_path);
672 g_free(session->notify_path);
673 g_free(session->info);
674 g_free(session->info_last);
679 static connman_bool_t is_online(enum connman_service_state state)
682 case CONNMAN_SERVICE_STATE_UNKNOWN:
683 case CONNMAN_SERVICE_STATE_IDLE:
684 case CONNMAN_SERVICE_STATE_ASSOCIATION:
685 case CONNMAN_SERVICE_STATE_CONFIGURATION:
686 case CONNMAN_SERVICE_STATE_DISCONNECT:
687 case CONNMAN_SERVICE_STATE_FAILURE:
688 case CONNMAN_SERVICE_STATE_READY:
690 case CONNMAN_SERVICE_STATE_ONLINE:
697 static connman_bool_t is_connecting(enum connman_service_state state)
700 case CONNMAN_SERVICE_STATE_UNKNOWN:
701 case CONNMAN_SERVICE_STATE_IDLE:
703 case CONNMAN_SERVICE_STATE_ASSOCIATION:
704 case CONNMAN_SERVICE_STATE_CONFIGURATION:
705 case CONNMAN_SERVICE_STATE_READY:
707 case CONNMAN_SERVICE_STATE_DISCONNECT:
708 case CONNMAN_SERVICE_STATE_FAILURE:
709 case CONNMAN_SERVICE_STATE_ONLINE:
716 static connman_bool_t explicit_connect(enum connman_session_reason reason)
719 case CONNMAN_SESSION_REASON_UNKNOWN:
720 case CONNMAN_SESSION_REASON_FREE_RIDE:
722 case CONNMAN_SESSION_REASON_CONNECT:
723 case CONNMAN_SESSION_REASON_PERIODIC:
730 static connman_bool_t explicit_disconnect(struct connman_session *session)
732 struct session_info *info = session->info;
734 if (info->entry == NULL)
737 DBG("session %p, reason %s service %p state %d",
738 session, reason2string(info->entry->reason),
739 info->entry->service, info->entry->state);
741 if (info->entry->reason == CONNMAN_SESSION_REASON_UNKNOWN)
744 if (explicit_connect(info->entry->reason) == FALSE)
747 if (__connman_service_session_dec(info->entry->service) == FALSE)
750 if (ecall_session != NULL && ecall_session != session)
756 static gboolean call_disconnect(gpointer user_data)
758 struct connman_service *service = user_data;
761 * TODO: We should mark this entry as pending work. In case
762 * disconnect fails we just unassign this session from the
763 * service and can't do anything later on it
765 DBG("disconnect service %p", service);
766 __connman_service_disconnect(service);
771 static gboolean call_connect(gpointer user_data)
773 struct connman_service *service = user_data;
775 DBG("connect service %p", service);
776 __connman_service_connect(service);
781 static void test_and_disconnect(struct connman_session *session)
783 struct session_info *info = session->info;
784 struct connman_service *service;
785 connman_bool_t disconnect, online;
787 if (info->entry == NULL)
790 disconnect = explicit_disconnect(session);
792 online = is_connecting(info->entry->state) == TRUE ||
793 is_online(info->entry->state) == TRUE;
795 info->online = FALSE;
796 info->reason = CONNMAN_SESSION_REASON_UNKNOWN;
797 info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
799 service = info->entry->service;
802 if (disconnect == TRUE && online == TRUE)
803 g_timeout_add_seconds(0, call_disconnect, service);
806 static void deselect_previous_service(struct connman_session *session,
807 struct service_entry *entry)
809 struct session_info *info = session->info;
811 if (info->entry != NULL && info->entry != entry)
812 test_and_disconnect(session);
815 static void select_online_service(struct connman_session *session,
816 struct service_entry *entry)
818 struct session_info *info = session->info;
823 info->entry->reason = info->reason;
825 if (explicit_connect(info->reason) == FALSE)
828 __connman_service_session_inc(info->entry->service);
831 static void select_offline_service(struct connman_session *session,
832 struct service_entry *entry)
834 struct session_info *info = session->info;
836 if (explicit_connect(info->reason) == FALSE) {
837 /* Don't select this service. It is not online and we
838 * don't call connect on it. This happends for example
839 * when the system is idle and we create a new session
840 * which is in free ride mode.
845 info->online = FALSE;
848 info->entry->reason = info->reason;
850 __connman_service_session_inc(info->entry->service);
851 g_timeout_add_seconds(0, call_connect, info->entry->service);
854 static void select_service(struct connman_session *session,
855 struct service_entry *entry)
857 DBG("session %p service %p", session, entry->service);
859 deselect_previous_service(session, entry);
861 if (is_online(entry->state) == TRUE)
862 select_online_service(session, entry);
864 select_offline_service(session, entry);
867 static void select_and_connect(struct connman_session *session,
868 enum connman_session_reason reason)
870 struct session_info *info = session->info;
871 struct service_entry *entry = NULL;
874 DBG("session %p reason %s", session, reason2string(reason));
876 info->reason = reason;
878 iter = g_sequence_get_begin_iter(session->service_list);
880 while (g_sequence_iter_is_end(iter) == FALSE) {
881 entry = g_sequence_get(iter);
883 switch (entry->state) {
884 case CONNMAN_SERVICE_STATE_ASSOCIATION:
885 case CONNMAN_SERVICE_STATE_CONFIGURATION:
886 case CONNMAN_SERVICE_STATE_READY:
887 case CONNMAN_SERVICE_STATE_ONLINE:
888 case CONNMAN_SERVICE_STATE_IDLE:
889 case CONNMAN_SERVICE_STATE_DISCONNECT:
890 select_service(session, entry);
892 case CONNMAN_SERVICE_STATE_UNKNOWN:
893 case CONNMAN_SERVICE_STATE_FAILURE:
897 iter = g_sequence_iter_next(iter);
901 static void session_changed(struct connman_session *session,
902 enum connman_session_trigger trigger)
904 struct session_info *info = session->info;
905 struct session_info *info_last = session->info_last;
909 * TODO: This only a placeholder for the 'real' algorithm to
910 * play a bit around. So we are going to improve it step by step.
913 DBG("session %p trigger %s", session, trigger2string(trigger));
916 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
917 DBG("ignore session changed event");
919 case CONNMAN_SESSION_TRIGGER_SETTING:
920 if (info->entry != NULL) {
921 iter = g_hash_table_lookup(session->service_hash,
922 info->entry->service);
925 * This service is not part of this
928 test_and_disconnect(session);
932 if (info->online == FALSE) {
933 select_and_connect(session,
934 CONNMAN_SESSION_REASON_FREE_RIDE);
938 case CONNMAN_SESSION_TRIGGER_CONNECT:
939 if (info->online == TRUE) {
940 if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
942 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
943 __connman_service_session_inc(info->entry->service);
947 select_and_connect(session,
948 CONNMAN_SESSION_REASON_CONNECT);
951 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
952 test_and_disconnect(session);
955 case CONNMAN_SESSION_TRIGGER_PERIODIC:
956 if (info->online == TRUE) {
957 info->entry->reason = CONNMAN_SESSION_REASON_PERIODIC;
958 __connman_service_session_inc(info->entry->service);
962 select_and_connect(session,
963 CONNMAN_SESSION_REASON_PERIODIC);
966 case CONNMAN_SESSION_TRIGGER_SERVICE:
967 if (info->entry != NULL &&
968 (is_connecting(info->entry->state) == TRUE ||
969 is_online(info->entry->state) == TRUE)) {
973 switch (info->reason) {
974 case CONNMAN_SESSION_REASON_CONNECT:
976 * We are not online, we are not connecting, that
977 * means we could still have a valid info->entry.
978 * Though something has changed from the service layer.
979 * Therefore we want to restart the algorithm. Before we
980 * can do that we have to cleanup a potientional old entry.
982 test_and_disconnect(session);
983 info->reason = CONNMAN_SESSION_REASON_CONNECT; /* restore value */
985 DBG("Retry to find a matching session");
987 * The user called Connect() but there was no
988 * matching session available at this point.
989 * Now there might be a new one. Let's retry
990 * to select and connect
992 select_and_connect(session,
993 CONNMAN_SESSION_REASON_CONNECT);
995 case CONNMAN_SESSION_REASON_PERIODIC:
996 case CONNMAN_SESSION_REASON_FREE_RIDE:
997 if (info->stay_connected == TRUE) {
998 DBG("StayConnected");
999 select_and_connect(session,
1000 CONNMAN_SESSION_REASON_CONNECT);
1002 select_and_connect(session,
1003 CONNMAN_SESSION_REASON_FREE_RIDE);
1006 case CONNMAN_SESSION_REASON_UNKNOWN:
1010 case CONNMAN_SESSION_TRIGGER_ECALL:
1011 if (info->online == FALSE && info->entry != NULL &&
1012 info->entry->service != NULL) {
1013 test_and_disconnect(session);
1019 if (info->entry != info_last->entry)
1020 session->info_dirty = TRUE;
1022 session_notify(session);
1025 static DBusMessage *connect_session(DBusConnection *conn,
1026 DBusMessage *msg, void *user_data)
1028 struct connman_session *session = user_data;
1030 DBG("session %p", session);
1032 if (ecall_session != NULL && ecall_session != session)
1033 return __connman_error_failed(msg, EBUSY);
1035 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1037 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1040 static DBusMessage *disconnect_session(DBusConnection *conn,
1041 DBusMessage *msg, void *user_data)
1043 struct connman_session *session = user_data;
1045 DBG("session %p", session);
1047 if (ecall_session != NULL && ecall_session != session)
1048 return __connman_error_failed(msg, EBUSY);
1050 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1052 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1055 static struct service_entry *create_service_entry(struct connman_service *service,
1057 enum connman_service_state state)
1059 struct service_entry *entry;
1060 enum connman_service_type type;
1063 entry = g_try_new0(struct service_entry, 1);
1067 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1068 entry->state = state;
1073 entry->service = service;
1075 idx = __connman_service_get_index(entry->service);
1076 entry->ifname = connman_inet_ifname(idx);
1077 if (entry->ifname == NULL)
1078 entry->ifname = g_strdup("");
1080 type = connman_service_get_type(entry->service);
1081 entry->bearer = service2bearer(type);
1086 static void destroy_service_entry(gpointer data)
1088 struct service_entry *entry = data;
1090 g_free(entry->ifname);
1095 static void update_allowed_bearers(struct connman_session *session)
1097 struct service_entry *entry;
1098 GSequenceIter *iter;
1100 if (session->service_list != NULL) {
1101 g_hash_table_remove_all(session->service_hash);
1102 g_sequence_free(session->service_list);
1105 session->service_list = __connman_service_get_list(session,
1107 create_service_entry,
1108 destroy_service_entry);
1110 g_sequence_sort(session->service_list, sort_services, session);
1112 iter = g_sequence_get_begin_iter(session->service_list);
1114 while (g_sequence_iter_is_end(iter) == FALSE) {
1115 entry = g_sequence_get(iter);
1117 DBG("service %p type %s name %s", entry->service,
1118 service2bearer(connman_service_get_type(entry->service)),
1121 g_hash_table_replace(session->service_hash,
1122 entry->service, iter);
1124 iter = g_sequence_iter_next(iter);
1127 session->info_dirty = TRUE;
1130 static void update_ecall_sessions(struct connman_session *session)
1132 struct session_info *info = session->info;
1133 struct connman_session *session_iter;
1134 GHashTableIter iter;
1135 gpointer key, value;
1137 g_hash_table_iter_init(&iter, session_hash);
1139 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1140 session_iter = value;
1142 if (session_iter == session)
1145 session_iter->info->ecall = info->ecall;
1146 session_iter->info_dirty = TRUE;
1148 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
1152 static void update_ecall(struct connman_session *session)
1154 struct session_info *info = session->info;
1155 struct session_info *info_last = session->info_last;
1157 DBG("session %p ecall_session %p ecall %d -> %d", session,
1158 ecall_session, info_last->ecall, info->ecall);
1160 if (ecall_session == NULL) {
1161 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
1164 ecall_session = session;
1165 } else if (ecall_session == session) {
1166 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
1169 ecall_session = NULL;
1174 update_ecall_sessions(session);
1176 session->info_dirty = TRUE;
1180 /* not a valid transition */
1181 info->ecall = info_last->ecall;
1184 static DBusMessage *change_session(DBusConnection *conn,
1185 DBusMessage *msg, void *user_data)
1187 struct connman_session *session = user_data;
1188 struct session_info *info = session->info;
1189 struct session_info *info_last = session->info_last;
1190 DBusMessageIter iter, value;
1192 GSList *allowed_bearers;
1194 DBG("session %p", session);
1195 if (dbus_message_iter_init(msg, &iter) == FALSE)
1196 return __connman_error_invalid_arguments(msg);
1198 dbus_message_iter_get_basic(&iter, &name);
1199 dbus_message_iter_next(&iter);
1200 dbus_message_iter_recurse(&iter, &value);
1202 switch (dbus_message_iter_get_arg_type(&value)) {
1203 case DBUS_TYPE_ARRAY:
1204 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1205 allowed_bearers = session_parse_allowed_bearers(&value);
1207 g_slist_foreach(info->allowed_bearers,
1208 cleanup_bearer_info, NULL);
1209 g_slist_free(info->allowed_bearers);
1211 if (allowed_bearers == NULL) {
1212 allowed_bearers = session_allowed_bearers_any();
1214 if (allowed_bearers == NULL)
1215 return __connman_error_failed(msg, ENOMEM);
1218 info->allowed_bearers = allowed_bearers;
1220 update_allowed_bearers(session);
1225 case DBUS_TYPE_BOOLEAN:
1226 if (g_str_equal(name, "Priority") == TRUE) {
1227 dbus_message_iter_get_basic(&value,
1230 if (info_last->priority != info->priority)
1231 session->info_dirty = TRUE;
1232 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1233 dbus_message_iter_get_basic(&value,
1234 &info->avoid_handover);
1236 if (info_last->avoid_handover != info->avoid_handover)
1237 session->info_dirty = TRUE;
1238 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1239 dbus_message_iter_get_basic(&value,
1240 &info->stay_connected);
1242 if (info_last->stay_connected != info->stay_connected)
1243 session->info_dirty = TRUE;
1244 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1245 dbus_message_iter_get_basic(&value,
1248 update_ecall(session);
1253 case DBUS_TYPE_UINT32:
1254 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1255 dbus_message_iter_get_basic(&value,
1256 &info->periodic_connect);
1258 if (info_last->periodic_connect != info->periodic_connect)
1259 session->info_dirty = TRUE;
1260 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1261 dbus_message_iter_get_basic(&value,
1262 &info->idle_timeout);
1264 if (info_last->idle_timeout != info->idle_timeout)
1265 session->info_dirty = TRUE;
1270 case DBUS_TYPE_STRING:
1271 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1273 dbus_message_iter_get_basic(&value, &val);
1274 info->roaming_policy =
1275 string2roamingpolicy(val);
1277 if (info_last->roaming_policy != info->roaming_policy)
1278 session->info_dirty = TRUE;
1287 if (session->info_dirty == TRUE)
1288 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1290 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1293 return __connman_error_invalid_arguments(msg);
1296 static void release_session(gpointer key, gpointer value, gpointer user_data)
1298 struct connman_session *session = value;
1299 DBusMessage *message;
1301 DBG("owner %s path %s", session->owner, session->notify_path);
1303 if (session->notify_watch > 0)
1304 g_dbus_remove_watch(connection, session->notify_watch);
1306 g_dbus_unregister_interface(connection, session->session_path,
1307 CONNMAN_SESSION_INTERFACE);
1309 message = dbus_message_new_method_call(session->owner,
1310 session->notify_path,
1311 CONNMAN_NOTIFICATION_INTERFACE,
1313 if (message == NULL)
1316 dbus_message_set_no_reply(message, TRUE);
1318 g_dbus_send_message(connection, message);
1321 static int session_disconnect(struct connman_session *session)
1323 DBG("session %p, %s", session, session->owner);
1325 if (session->notify_watch > 0)
1326 g_dbus_remove_watch(connection, session->notify_watch);
1328 g_dbus_unregister_interface(connection, session->session_path,
1329 CONNMAN_SESSION_INTERFACE);
1331 test_and_disconnect(session);
1333 g_hash_table_remove(session_hash, session->session_path);
1338 static void owner_disconnect(DBusConnection *conn, void *user_data)
1340 struct connman_session *session = user_data;
1342 DBG("session %p, %s died", session, session->owner);
1344 session_disconnect(session);
1347 static DBusMessage *destroy_session(DBusConnection *conn,
1348 DBusMessage *msg, void *user_data)
1350 struct connman_session *session = user_data;
1352 DBG("session %p", session);
1354 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1357 static GDBusMethodTable session_methods[] = {
1358 { "Destroy", "", "", destroy_session },
1359 { "Connect", "", "", connect_session },
1360 { "Disconnect", "", "", disconnect_session },
1361 { "Change", "sv", "", change_session },
1365 int __connman_session_create(DBusMessage *msg)
1367 const char *owner, *notify_path;
1368 char *session_path = NULL;
1369 DBusMessageIter iter, array;
1370 struct connman_session *session = NULL;
1371 struct session_info *info, *info_last;
1373 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1374 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1375 enum connman_session_roaming_policy roaming_policy =
1376 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1377 GSList *allowed_bearers = NULL;
1378 unsigned int periodic_connect = 0;
1379 unsigned int idle_timeout = 0;
1383 owner = dbus_message_get_sender(msg);
1385 DBG("owner %s", owner);
1387 if (ecall_session != NULL) {
1389 * If there is an emergency call already going on,
1390 * ignore session creation attempt
1396 dbus_message_iter_init(msg, &iter);
1397 dbus_message_iter_recurse(&iter, &array);
1399 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1400 DBusMessageIter entry, value;
1401 const char *key, *val;
1403 dbus_message_iter_recurse(&array, &entry);
1404 dbus_message_iter_get_basic(&entry, &key);
1406 dbus_message_iter_next(&entry);
1407 dbus_message_iter_recurse(&entry, &value);
1409 switch (dbus_message_iter_get_arg_type(&value)) {
1410 case DBUS_TYPE_ARRAY:
1411 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1413 session_parse_allowed_bearers(&value);
1418 case DBUS_TYPE_BOOLEAN:
1419 if (g_str_equal(key, "Priority") == TRUE) {
1420 dbus_message_iter_get_basic(&value,
1422 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1423 dbus_message_iter_get_basic(&value,
1425 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1426 dbus_message_iter_get_basic(&value,
1428 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1429 dbus_message_iter_get_basic(&value,
1435 case DBUS_TYPE_UINT32:
1436 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1437 dbus_message_iter_get_basic(&value,
1439 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1440 dbus_message_iter_get_basic(&value,
1446 case DBUS_TYPE_STRING:
1447 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1448 dbus_message_iter_get_basic(&value, &val);
1449 roaming_policy = string2roamingpolicy(val);
1454 dbus_message_iter_next(&array);
1457 dbus_message_iter_next(&iter);
1458 dbus_message_iter_get_basic(&iter, ¬ify_path);
1460 if (notify_path == NULL) {
1465 session_path = g_strdup_printf("/sessions%s", notify_path);
1466 if (session_path == NULL) {
1471 session = g_hash_table_lookup(session_hash, session_path);
1472 if (session != NULL) {
1478 session = g_try_new0(struct connman_session, 1);
1479 if (session == NULL) {
1484 session->info = g_try_new0(struct session_info, 1);
1485 if (session->info == NULL) {
1490 session->info_last = g_try_new0(struct session_info, 1);
1491 if (session->info_last == NULL) {
1496 info = session->info;
1497 info_last = session->info_last;
1499 session->owner = g_strdup(owner);
1500 session->session_path = session_path;
1501 session->notify_path = g_strdup(notify_path);
1502 session->notify_watch =
1503 g_dbus_add_disconnect_watch(connection, session->owner,
1504 owner_disconnect, session, NULL);
1506 session->service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1509 info->online = FALSE;
1510 info->priority = priority;
1511 info->avoid_handover = avoid_handover;
1512 info->stay_connected = stay_connected;
1513 info->periodic_connect = periodic_connect;
1514 info->idle_timeout = idle_timeout;
1515 info->ecall = ecall;
1516 info->roaming_policy = roaming_policy;
1520 if (allowed_bearers == NULL) {
1521 info->allowed_bearers =
1522 session_allowed_bearers_any();
1524 if (info->allowed_bearers == NULL) {
1529 info->allowed_bearers = allowed_bearers;
1532 g_hash_table_replace(session_hash, session->session_path, session);
1534 DBG("add %s", session->session_path);
1536 if (g_dbus_register_interface(connection, session->session_path,
1537 CONNMAN_SESSION_INTERFACE,
1538 session_methods, NULL,
1539 NULL, session, NULL) == FALSE) {
1540 connman_error("Failed to register %s", session->session_path);
1541 g_hash_table_remove(session_hash, session->session_path);
1548 g_dbus_send_reply(connection, msg,
1549 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1553 update_allowed_bearers(session);
1554 if (info->ecall == TRUE) {
1555 ecall_session = session;
1556 update_ecall_sessions(session);
1559 info_last->online = info->online;
1560 info_last->priority = info->priority;
1561 info_last->avoid_handover = info->avoid_handover;
1562 info_last->stay_connected = info->stay_connected;
1563 info_last->periodic_connect = info->periodic_connect;
1564 info_last->idle_timeout = info->idle_timeout;
1565 info_last->ecall = info->ecall;
1566 info_last->roaming_policy = info->roaming_policy;
1567 info_last->entry = info->entry;
1568 info_last->marker = info->marker;
1569 info_last->allowed_bearers = info->allowed_bearers;
1571 session->info_dirty = TRUE;
1572 session->append_all = TRUE;
1574 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1579 connman_error("Failed to create session");
1581 if (session != NULL) {
1582 if (session->info_last != NULL)
1583 g_free(session->info_last);
1584 if (session->info != NULL)
1585 g_free(session->info);
1589 g_free(session_path);
1591 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1592 g_slist_free(allowed_bearers);
1597 int __connman_session_destroy(DBusMessage *msg)
1599 const char *owner, *session_path;
1600 struct connman_session *session;
1602 owner = dbus_message_get_sender(msg);
1604 DBG("owner %s", owner);
1606 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1608 if (session_path == NULL)
1611 session = g_hash_table_lookup(session_hash, session_path);
1612 if (session == NULL)
1615 if (g_strcmp0(owner, session->owner) != 0)
1618 session_disconnect(session);
1623 connman_bool_t __connman_session_mode()
1628 void __connman_session_set_mode(connman_bool_t enable)
1630 DBG("enable %d", enable);
1632 sessionmode = enable;
1634 if (sessionmode == TRUE)
1635 __connman_service_disconnect_all();
1638 static void service_add(struct connman_service *service,
1641 GHashTableIter iter;
1642 GSequenceIter *iter_service_list;
1643 gpointer key, value;
1644 struct connman_session *session;
1645 struct service_entry *entry;
1647 DBG("service %p", service);
1649 g_hash_table_iter_init(&iter, session_hash);
1651 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1654 if (service_match(session, service) == FALSE)
1657 entry = create_service_entry(service, name,
1658 CONNMAN_SERVICE_STATE_IDLE);
1663 g_sequence_insert_sorted(session->service_list,
1664 entry, sort_services,
1667 g_hash_table_replace(session->service_hash, service,
1670 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1674 static void service_remove(struct connman_service *service)
1677 GHashTableIter iter;
1678 gpointer key, value;
1679 struct connman_session *session;
1680 struct session_info *info;
1682 DBG("service %p", service);
1684 g_hash_table_iter_init(&iter, session_hash);
1686 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1687 GSequenceIter *iter;
1689 info = session->info;
1691 iter = g_hash_table_lookup(session->service_hash, service);
1695 g_sequence_remove(iter);
1697 if (info->entry != NULL && info->entry->service == service)
1699 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1703 static void service_state_changed(struct connman_service *service,
1704 enum connman_service_state state)
1706 GHashTableIter iter;
1707 gpointer key, value;
1708 struct connman_session *session;
1709 struct session_info *info, *info_last;
1711 DBG("service %p state %d", service, state);
1713 g_hash_table_iter_init(&iter, session_hash);
1715 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1716 GSequenceIter *service_iter;
1719 info = session->info;
1720 info_last = session->info_last;
1722 service_iter = g_hash_table_lookup(session->service_hash, service);
1723 if (service_iter != NULL) {
1724 struct service_entry *entry;
1726 entry = g_sequence_get(service_iter);
1727 entry->state = state;
1729 if (info->entry == entry) {
1730 info->online = is_online(entry->state);
1731 if (info_last->online != info->online)
1732 session->info_dirty = TRUE;
1736 session_changed(session,
1737 CONNMAN_SESSION_TRIGGER_SERVICE);
1741 static void ipconfig_changed(struct connman_service *service,
1742 struct connman_ipconfig *ipconfig)
1744 GHashTableIter iter;
1745 gpointer key, value;
1746 struct connman_session *session;
1747 struct session_info *info;
1748 enum connman_ipconfig_type type;
1750 DBG("service %p ipconfig %p", service, ipconfig);
1752 type = __connman_ipconfig_get_config_type(ipconfig);
1754 g_hash_table_iter_init(&iter, session_hash);
1756 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1758 info = session->info;
1760 if (info->entry != NULL && info->entry->service == service) {
1761 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1762 ipconfig_ipv4_changed(session);
1763 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1764 ipconfig_ipv6_changed(session);
1769 static struct connman_notifier session_notifier = {
1771 .service_add = service_add,
1772 .service_remove = service_remove,
1773 .service_state_changed = service_state_changed,
1774 .ipconfig_changed = ipconfig_changed,
1777 int __connman_session_init(void)
1783 connection = connman_dbus_get_connection();
1784 if (connection == NULL)
1787 err = connman_notifier_register(&session_notifier);
1789 dbus_connection_unref(connection);
1793 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1794 NULL, cleanup_session);
1796 sessionmode = FALSE;
1800 void __connman_session_cleanup(void)
1804 if (connection == NULL)
1807 connman_notifier_unregister(&session_notifier);
1809 g_hash_table_foreach(session_hash, release_session, NULL);
1810 g_hash_table_destroy(session_hash);
1811 session_hash = NULL;
1813 dbus_connection_unref(connection);