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;
89 struct connman_session {
95 connman_bool_t append_all;
96 connman_bool_t info_dirty;
97 struct session_info *info;
98 struct session_info *info_last;
100 GSequence *service_list;
101 GHashTable *service_hash;
106 connman_bool_t match_all;
107 enum connman_service_type service_type;
110 static const char *trigger2string(enum connman_session_trigger trigger)
113 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
115 case CONNMAN_SESSION_TRIGGER_SETTING:
117 case CONNMAN_SESSION_TRIGGER_CONNECT:
119 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
121 case CONNMAN_SESSION_TRIGGER_PERIODIC:
123 case CONNMAN_SESSION_TRIGGER_SERVICE:
125 case CONNMAN_SESSION_TRIGGER_ECALL:
132 static const char *reason2string(enum connman_session_reason reason)
135 case CONNMAN_SESSION_REASON_UNKNOWN:
137 case CONNMAN_SESSION_REASON_CONNECT:
139 case CONNMAN_SESSION_REASON_FREE_RIDE:
141 case CONNMAN_SESSION_REASON_PERIODIC:
148 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
151 case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
153 case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
155 case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
157 case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
159 case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
161 case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
162 return "international";
168 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
170 if (g_strcmp0(policy, "default") == 0)
171 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
172 else if (g_strcmp0(policy, "always") == 0)
173 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
174 else if (g_strcmp0(policy, "forbidden") == 0)
175 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
176 else if (g_strcmp0(policy, "national") == 0)
177 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
178 else if (g_strcmp0(policy, "international") == 0)
179 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
181 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
184 static enum connman_service_type bearer2service(const char *bearer)
187 return CONNMAN_SERVICE_TYPE_UNKNOWN;
189 if (g_strcmp0(bearer, "ethernet") == 0)
190 return CONNMAN_SERVICE_TYPE_ETHERNET;
191 else if (g_strcmp0(bearer, "wifi") == 0)
192 return CONNMAN_SERVICE_TYPE_WIFI;
193 else if (g_strcmp0(bearer, "wimax") == 0)
194 return CONNMAN_SERVICE_TYPE_WIMAX;
195 else if (g_strcmp0(bearer, "bluetooth") == 0)
196 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
197 else if (g_strcmp0(bearer, "3g") == 0)
198 return CONNMAN_SERVICE_TYPE_CELLULAR;
199 else if (g_strcmp0(bearer, "vpn") == 0)
200 return CONNMAN_SERVICE_TYPE_VPN;
202 return CONNMAN_SERVICE_TYPE_UNKNOWN;
205 static char *service2bearer(enum connman_service_type type)
208 case CONNMAN_SERVICE_TYPE_ETHERNET:
210 case CONNMAN_SERVICE_TYPE_WIFI:
212 case CONNMAN_SERVICE_TYPE_WIMAX:
214 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
216 case CONNMAN_SERVICE_TYPE_CELLULAR:
218 case CONNMAN_SERVICE_TYPE_VPN:
220 case CONNMAN_SERVICE_TYPE_UNKNOWN:
221 case CONNMAN_SERVICE_TYPE_SYSTEM:
222 case CONNMAN_SERVICE_TYPE_GPS:
223 case CONNMAN_SERVICE_TYPE_GADGET:
230 static void cleanup_bearer_info(gpointer data, gpointer user_data)
232 struct bearer_info *info = data;
238 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
240 struct bearer_info *info;
241 DBusMessageIter array;
244 dbus_message_iter_recurse(iter, &array);
246 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
249 dbus_message_iter_get_basic(&array, &bearer);
251 info = g_try_new0(struct bearer_info, 1);
253 g_slist_foreach(list, cleanup_bearer_info, NULL);
259 info->name = g_strdup(bearer);
260 info->service_type = bearer2service(info->name);
262 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
263 g_strcmp0(info->name, "*") == 0) {
264 info->match_all = TRUE;
266 info->match_all = FALSE;
269 list = g_slist_append(list, info);
271 dbus_message_iter_next(&array);
277 static GSList *session_allowed_bearers_any(void)
279 struct bearer_info *info;
282 info = g_try_new0(struct bearer_info, 1);
289 info->name = g_strdup("");
290 info->match_all = TRUE;
291 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
293 list = g_slist_append(list, info);
298 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
300 struct session_info *info = user_data;
303 for (list = info->allowed_bearers;
304 list != NULL; list = list->next) {
305 struct bearer_info *info = list->data;
307 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
312 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
314 struct connman_service *service = user_data;
315 struct connman_ipconfig *ipconfig_ipv4;
320 ipconfig_ipv4 = __connman_service_get_ip4config(service);
321 if (ipconfig_ipv4 == NULL)
324 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
327 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
329 struct connman_service *service = user_data;
330 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
335 ipconfig_ipv4 = __connman_service_get_ip4config(service);
336 ipconfig_ipv6 = __connman_service_get_ip6config(service);
337 if (ipconfig_ipv6 == NULL)
340 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
343 static void append_notify(DBusMessageIter *dict,
344 struct connman_session *session)
346 struct session_info *info = session->info;
347 struct session_info *info_last = session->info_last;
349 struct connman_service *service;
350 const char *name, *ifname, *bearer;
352 if (session->append_all == TRUE ||
353 info->online != info_last->online) {
354 connman_dbus_dict_append_basic(dict, "Online",
357 info_last->online = info->online;
360 if (session->append_all == TRUE ||
361 info->entry != info_last->entry) {
362 if (info->entry == NULL) {
368 name = info->entry->name;
369 ifname = info->entry->ifname;
370 service = info->entry->service;
371 bearer = info->entry->bearer;
374 connman_dbus_dict_append_basic(dict, "Name",
378 connman_dbus_dict_append_dict(dict, "IPv4",
379 append_ipconfig_ipv4,
382 connman_dbus_dict_append_dict(dict, "IPv6",
383 append_ipconfig_ipv6,
386 connman_dbus_dict_append_basic(dict, "Interface",
390 connman_dbus_dict_append_basic(dict, "Bearer",
394 info_last->entry = info->entry;
398 if (session->append_all == TRUE ||
399 info->priority != info_last->priority) {
400 connman_dbus_dict_append_basic(dict, "Priority",
403 info_last->priority = info->priority;
406 if (session->append_all == TRUE ||
407 info->allowed_bearers != info_last->allowed_bearers) {
408 connman_dbus_dict_append_array(dict, "AllowedBearers",
410 append_allowed_bearers,
412 info_last->allowed_bearers = info->allowed_bearers;
415 if (session->append_all == TRUE ||
416 info->avoid_handover != info_last->avoid_handover) {
417 connman_dbus_dict_append_basic(dict, "AvoidHandover",
419 &info->avoid_handover);
420 info_last->avoid_handover = info->avoid_handover;
423 if (session->append_all == TRUE ||
424 info->stay_connected != info_last->stay_connected) {
425 connman_dbus_dict_append_basic(dict, "StayConnected",
427 &info->stay_connected);
428 info_last->stay_connected = info->stay_connected;
431 if (session->append_all == TRUE ||
432 info->periodic_connect != info_last->periodic_connect) {
433 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
435 &info->periodic_connect);
436 info_last->periodic_connect = info->periodic_connect;
439 if (session->append_all == TRUE ||
440 info->idle_timeout != info_last->idle_timeout) {
441 connman_dbus_dict_append_basic(dict, "IdleTimeout",
443 &info->idle_timeout);
444 info_last->idle_timeout = info->idle_timeout;
447 if (session->append_all == TRUE ||
448 info->ecall != info_last->ecall) {
449 connman_dbus_dict_append_basic(dict, "EmergencyCall",
452 info_last->ecall = info->ecall;
455 if (session->append_all == TRUE ||
456 info->roaming_policy != info_last->roaming_policy) {
457 policy = roamingpolicy2string(info->roaming_policy);
458 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
461 info_last->roaming_policy = info->roaming_policy;
464 if (session->append_all == TRUE ||
465 info->marker != info_last->marker) {
466 connman_dbus_dict_append_basic(dict, "SessionMarker",
469 info_last->marker = info->marker;
472 session->append_all = FALSE;
473 session->info_dirty = FALSE;
476 static gboolean session_notify(gpointer user_data)
478 struct connman_session *session = user_data;
480 DBusMessageIter array, dict;
482 if (session->info_dirty == FALSE)
485 DBG("session %p owner %s notify_path %s", session,
486 session->owner, session->notify_path);
488 msg = dbus_message_new_method_call(session->owner, session->notify_path,
489 CONNMAN_NOTIFICATION_INTERFACE,
494 dbus_message_iter_init_append(msg, &array);
495 connman_dbus_dict_open(&array, &dict);
497 append_notify(&dict, session);
499 connman_dbus_dict_close(&array, &dict);
501 g_dbus_send_message(connection, msg);
503 session->info_dirty = FALSE;
508 static void ipconfig_ipv4_changed(struct connman_session *session)
510 struct session_info *info = session->info;
512 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
513 "IPv4", append_ipconfig_ipv4,
514 info->entry->service);
517 static void ipconfig_ipv6_changed(struct connman_session *session)
519 struct session_info *info = session->info;
521 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
522 "IPv6", append_ipconfig_ipv6,
523 info->entry->service);
526 static connman_bool_t service_type_match(struct connman_session *session,
527 struct connman_service *service)
529 struct session_info *info = session->info;
532 for (list = info->allowed_bearers;
533 list != NULL; list = list->next) {
534 struct bearer_info *info = list->data;
535 enum connman_service_type service_type;
537 if (info->match_all == TRUE)
540 service_type = connman_service_get_type(service);
541 if (info->service_type == service_type)
548 static connman_bool_t service_match(struct connman_session *session,
549 struct connman_service *service)
551 if (service_type_match(session, service) == FALSE)
557 static int service_type_weight(enum connman_service_type type)
560 * The session doesn't care which service
561 * to use. Nevertheless we have to sort them
562 * according their type. The ordering is
571 case CONNMAN_SERVICE_TYPE_ETHERNET:
573 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
575 case CONNMAN_SERVICE_TYPE_WIFI:
576 case CONNMAN_SERVICE_TYPE_WIMAX:
578 case CONNMAN_SERVICE_TYPE_CELLULAR:
580 case CONNMAN_SERVICE_TYPE_UNKNOWN:
581 case CONNMAN_SERVICE_TYPE_SYSTEM:
582 case CONNMAN_SERVICE_TYPE_GPS:
583 case CONNMAN_SERVICE_TYPE_VPN:
584 case CONNMAN_SERVICE_TYPE_GADGET:
591 static gint sort_allowed_bearers(struct connman_service *service_a,
592 struct connman_service *service_b,
593 struct connman_session *session)
595 struct session_info *info = session->info;
597 enum connman_service_type type_a, type_b;
598 int weight_a, weight_b;
600 type_a = connman_service_get_type(service_a);
601 type_b = connman_service_get_type(service_b);
603 for (list = info->allowed_bearers;
604 list != NULL; list = list->next) {
605 struct bearer_info *info = list->data;
607 if (info->match_all == TRUE) {
608 if (type_a != type_b) {
609 weight_a = service_type_weight(type_a);
610 weight_b = service_type_weight(type_b);
612 if (weight_a > weight_b)
615 if (weight_a < weight_b)
622 if (type_a == info->service_type &&
623 type_b == info->service_type) {
627 if (type_a == info->service_type &&
628 type_b != info->service_type) {
632 if (type_a != info->service_type &&
633 type_b == info->service_type) {
641 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
643 struct service_entry *entry_a = (void *)a;
644 struct service_entry *entry_b = (void *)b;
645 struct connman_session *session = user_data;
647 return sort_allowed_bearers(entry_a->service, entry_b->service,
651 static void cleanup_session(gpointer user_data)
653 struct connman_session *session = user_data;
654 struct session_info *info = session->info;
656 DBG("remove %s", session->session_path);
658 g_hash_table_destroy(session->service_hash);
659 g_sequence_free(session->service_list);
661 if (info->entry != NULL &&
662 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
663 __connman_service_disconnect(info->entry->service);
666 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
667 g_slist_free(info->allowed_bearers);
669 g_free(session->owner);
670 g_free(session->session_path);
671 g_free(session->notify_path);
672 g_free(session->info);
673 g_free(session->info_last);
678 static void release_session(gpointer key, gpointer value, gpointer user_data)
680 struct connman_session *session = value;
681 DBusMessage *message;
683 DBG("owner %s path %s", session->owner, session->notify_path);
685 if (session->notify_watch > 0)
686 g_dbus_remove_watch(connection, session->notify_watch);
688 g_dbus_unregister_interface(connection, session->session_path,
689 CONNMAN_SESSION_INTERFACE);
691 message = dbus_message_new_method_call(session->owner,
692 session->notify_path,
693 CONNMAN_NOTIFICATION_INTERFACE,
698 dbus_message_set_no_reply(message, TRUE);
700 g_dbus_send_message(connection, message);
703 static int session_disconnect(struct connman_session *session)
705 DBG("session %p, %s", session, session->owner);
707 if (session->notify_watch > 0)
708 g_dbus_remove_watch(connection, session->notify_watch);
710 g_dbus_unregister_interface(connection, session->session_path,
711 CONNMAN_SESSION_INTERFACE);
713 g_hash_table_remove(session_hash, session->session_path);
718 static void owner_disconnect(DBusConnection *conn, void *user_data)
720 struct connman_session *session = user_data;
722 DBG("session %p, %s died", session, session->owner);
724 session_disconnect(session);
727 static DBusMessage *destroy_session(DBusConnection *conn,
728 DBusMessage *msg, void *user_data)
730 struct connman_session *session = user_data;
732 DBG("session %p", session);
734 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
737 static connman_bool_t is_online(enum connman_service_state state)
740 case CONNMAN_SERVICE_STATE_UNKNOWN:
741 case CONNMAN_SERVICE_STATE_IDLE:
742 case CONNMAN_SERVICE_STATE_ASSOCIATION:
743 case CONNMAN_SERVICE_STATE_CONFIGURATION:
744 case CONNMAN_SERVICE_STATE_DISCONNECT:
745 case CONNMAN_SERVICE_STATE_FAILURE:
746 case CONNMAN_SERVICE_STATE_READY:
748 case CONNMAN_SERVICE_STATE_ONLINE:
755 static connman_bool_t is_connecting(enum connman_service_state state)
758 case CONNMAN_SERVICE_STATE_UNKNOWN:
759 case CONNMAN_SERVICE_STATE_IDLE:
761 case CONNMAN_SERVICE_STATE_ASSOCIATION:
762 case CONNMAN_SERVICE_STATE_CONFIGURATION:
763 case CONNMAN_SERVICE_STATE_READY:
765 case CONNMAN_SERVICE_STATE_DISCONNECT:
766 case CONNMAN_SERVICE_STATE_FAILURE:
767 case CONNMAN_SERVICE_STATE_ONLINE:
774 static connman_bool_t explicit_connect(enum connman_session_reason reason)
777 case CONNMAN_SESSION_REASON_UNKNOWN:
778 case CONNMAN_SESSION_REASON_FREE_RIDE:
780 case CONNMAN_SESSION_REASON_CONNECT:
781 case CONNMAN_SESSION_REASON_PERIODIC:
788 static connman_bool_t explicit_disconnect(struct connman_session *session)
790 struct session_info *info = session->info;
792 if (info->entry == NULL)
795 DBG("session %p, reason %s service %p state %d",
796 session, reason2string(info->entry->reason),
797 info->entry->service, info->entry->state);
799 if (explicit_connect(info->entry->reason) == FALSE)
802 if (__connman_service_session_dec(info->entry->service) == FALSE)
805 if (ecall_session != NULL && ecall_session != session)
811 static void test_and_disconnect(struct connman_session *session)
813 struct session_info *info = session->info;
814 struct service_entry *entry;
815 connman_bool_t disconnect;
817 disconnect = explicit_disconnect(session);
821 info->online = FALSE;
824 if (disconnect == TRUE) {
825 DBG("disconnect service %p", entry->service);
828 * TODO: We should mark this entry as pending work. In case
829 * disconnect fails we just unassign this session from the
830 * service and can't do anything later on it
832 __connman_service_disconnect(entry->service);
836 static void select_and_connect(struct connman_session *session,
837 enum connman_session_reason reason)
839 struct session_info *info = session->info;
840 struct service_entry *entry = NULL;
842 connman_bool_t do_connect = FALSE;
844 DBG("session %p reason %s", session, reason2string(reason));
846 iter = g_sequence_get_begin_iter(session->service_list);
848 while (g_sequence_iter_is_end(iter) == FALSE) {
849 entry = g_sequence_get(iter);
851 switch (entry->state) {
852 case CONNMAN_SERVICE_STATE_ASSOCIATION:
853 case CONNMAN_SERVICE_STATE_CONFIGURATION:
854 case CONNMAN_SERVICE_STATE_READY:
855 case CONNMAN_SERVICE_STATE_ONLINE:
856 /* connecting or connected */
858 case CONNMAN_SERVICE_STATE_IDLE:
859 case CONNMAN_SERVICE_STATE_DISCONNECT:
860 if (explicit_connect(reason) == TRUE)
863 case CONNMAN_SERVICE_STATE_UNKNOWN:
864 case CONNMAN_SERVICE_STATE_FAILURE:
872 iter = g_sequence_iter_next(iter);
875 if (info->entry != NULL && info->entry != entry)
876 test_and_disconnect(session);
880 info->entry->reason = reason;
882 if (explicit_connect(reason) == TRUE)
883 __connman_service_session_inc(info->entry->service);
885 if (do_connect == TRUE)
886 __connman_service_connect(info->entry->service);
888 info->online = is_online(entry->state);
892 static void session_changed(struct connman_session *session,
893 enum connman_session_trigger trigger)
895 struct session_info *info = session->info;
896 struct session_info *info_last = session->info_last;
900 * TODO: This only a placeholder for the 'real' algorithm to
901 * play a bit around. So we are going to improve it step by step.
904 DBG("session %p trigger %s", session, trigger2string(trigger));
907 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
908 DBG("ignore session changed event");
910 case CONNMAN_SESSION_TRIGGER_SETTING:
911 if (info->entry != NULL) {
912 iter = g_hash_table_lookup(session->service_hash,
913 info->entry->service);
916 * This service is not part of this
919 test_and_disconnect(session);
923 if (info->online == FALSE) {
924 select_and_connect(session,
925 CONNMAN_SESSION_REASON_FREE_RIDE);
929 case CONNMAN_SESSION_TRIGGER_CONNECT:
930 if (info->online == TRUE) {
931 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
932 __connman_service_session_inc(info->entry->service);
936 select_and_connect(session,
937 CONNMAN_SESSION_REASON_CONNECT);
940 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
941 test_and_disconnect(session);
944 case CONNMAN_SESSION_TRIGGER_PERIODIC:
945 if (info->online == TRUE) {
946 info->entry->reason = CONNMAN_SESSION_REASON_PERIODIC;
947 __connman_service_session_inc(info->entry->service);
951 select_and_connect(session,
952 CONNMAN_SESSION_REASON_PERIODIC);
955 case CONNMAN_SESSION_TRIGGER_SERVICE:
956 if (info->online == TRUE)
959 if (info->entry != NULL &&
960 is_connecting(info->entry->state) == TRUE) {
964 if (info->stay_connected == TRUE) {
965 DBG("StayConnected");
966 select_and_connect(session,
967 CONNMAN_SESSION_REASON_CONNECT);
972 select_and_connect(session,
973 CONNMAN_SESSION_REASON_FREE_RIDE);
976 case CONNMAN_SESSION_TRIGGER_ECALL:
977 if (info->online == FALSE && info->entry->service != NULL)
978 test_and_disconnect(session);
983 if (info->entry != info_last->entry)
984 session->info_dirty = TRUE;
986 session_notify(session);
989 static DBusMessage *connect_session(DBusConnection *conn,
990 DBusMessage *msg, void *user_data)
992 struct connman_session *session = user_data;
994 DBG("session %p", session);
996 if (ecall_session != NULL && ecall_session != session)
997 return __connman_error_failed(msg, EBUSY);
999 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1001 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1004 static DBusMessage *disconnect_session(DBusConnection *conn,
1005 DBusMessage *msg, void *user_data)
1007 struct connman_session *session = user_data;
1009 DBG("session %p", session);
1011 if (ecall_session != NULL && ecall_session != session)
1012 return __connman_error_failed(msg, EBUSY);
1014 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1016 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1019 static struct service_entry *create_service_entry(struct connman_service *service,
1021 enum connman_service_state state)
1023 struct service_entry *entry;
1024 enum connman_service_type type;
1027 entry = g_try_new0(struct service_entry, 1);
1031 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1032 entry->state = state;
1034 entry->service = service;
1036 idx = __connman_service_get_index(entry->service);
1037 entry->ifname = connman_inet_ifname(idx);
1038 if (entry->ifname == NULL)
1041 type = connman_service_get_type(entry->service);
1042 entry->bearer = service2bearer(type);
1047 static void destroy_service_entry(gpointer data)
1049 struct service_entry *entry = data;
1054 static void update_allowed_bearers(struct connman_session *session)
1056 struct service_entry *entry;
1057 GSequenceIter *iter;
1059 if (session->service_list != NULL) {
1060 g_hash_table_remove_all(session->service_hash);
1061 g_sequence_free(session->service_list);
1064 session->service_list = __connman_service_get_list(session,
1066 create_service_entry,
1067 destroy_service_entry);
1069 g_sequence_sort(session->service_list, sort_services, session);
1071 iter = g_sequence_get_begin_iter(session->service_list);
1073 while (g_sequence_iter_is_end(iter) == FALSE) {
1074 entry = g_sequence_get(iter);
1076 DBG("service %p type %s name %s", entry->service,
1077 service2bearer(connman_service_get_type(entry->service)),
1080 g_hash_table_replace(session->service_hash,
1081 entry->service, iter);
1083 iter = g_sequence_iter_next(iter);
1086 session->info_dirty = TRUE;
1089 static void update_ecall_sessions(struct connman_session *session)
1091 struct session_info *info = session->info;
1092 struct connman_session *session_iter;
1093 GHashTableIter iter;
1094 gpointer key, value;
1096 g_hash_table_iter_init(&iter, session_hash);
1098 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1099 session_iter = value;
1101 if (session_iter == session)
1104 session_iter->info->ecall = info->ecall;
1105 session_iter->info_dirty = TRUE;
1107 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
1111 static void update_ecall(struct connman_session *session)
1113 struct session_info *info = session->info;
1114 struct session_info *info_last = session->info_last;
1116 DBG("session %p ecall_session %p ecall %d -> %d", session,
1117 ecall_session, info_last->ecall, info->ecall);
1119 if (ecall_session == NULL) {
1120 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
1123 ecall_session = session;
1124 } else if (ecall_session == session) {
1125 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
1128 ecall_session = NULL;
1133 update_ecall_sessions(session);
1135 session->info_dirty = TRUE;
1139 /* not a valid transition */
1140 info->ecall = info_last->ecall;
1143 static DBusMessage *change_session(DBusConnection *conn,
1144 DBusMessage *msg, void *user_data)
1146 struct connman_session *session = user_data;
1147 struct session_info *info = session->info;
1148 struct session_info *info_last = session->info_last;
1149 DBusMessageIter iter, value;
1151 GSList *allowed_bearers;
1153 DBG("session %p", session);
1154 if (dbus_message_iter_init(msg, &iter) == FALSE)
1155 return __connman_error_invalid_arguments(msg);
1157 dbus_message_iter_get_basic(&iter, &name);
1158 dbus_message_iter_next(&iter);
1159 dbus_message_iter_recurse(&iter, &value);
1161 switch (dbus_message_iter_get_arg_type(&value)) {
1162 case DBUS_TYPE_ARRAY:
1163 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1164 allowed_bearers = session_parse_allowed_bearers(&value);
1166 g_slist_foreach(info->allowed_bearers,
1167 cleanup_bearer_info, NULL);
1168 g_slist_free(info->allowed_bearers);
1170 if (allowed_bearers == NULL) {
1171 allowed_bearers = session_allowed_bearers_any();
1173 if (allowed_bearers == NULL)
1174 return __connman_error_failed(msg, ENOMEM);
1177 info->allowed_bearers = allowed_bearers;
1179 update_allowed_bearers(session);
1184 case DBUS_TYPE_BOOLEAN:
1185 if (g_str_equal(name, "Priority") == TRUE) {
1186 dbus_message_iter_get_basic(&value,
1189 if (info_last->priority != info->priority)
1190 session->info_dirty = TRUE;
1191 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1192 dbus_message_iter_get_basic(&value,
1193 &info->avoid_handover);
1195 if (info_last->avoid_handover != info->avoid_handover)
1196 session->info_dirty = TRUE;
1197 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1198 dbus_message_iter_get_basic(&value,
1199 &info->stay_connected);
1201 if (info_last->stay_connected != info->stay_connected)
1202 session->info_dirty = TRUE;
1203 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1204 dbus_message_iter_get_basic(&value,
1207 update_ecall(session);
1212 case DBUS_TYPE_UINT32:
1213 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1214 dbus_message_iter_get_basic(&value,
1215 &info->periodic_connect);
1217 if (info_last->periodic_connect != info->periodic_connect)
1218 session->info_dirty = TRUE;
1219 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1220 dbus_message_iter_get_basic(&value,
1221 &info->idle_timeout);
1223 if (info_last->idle_timeout != info->idle_timeout)
1224 session->info_dirty = TRUE;
1229 case DBUS_TYPE_STRING:
1230 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1232 dbus_message_iter_get_basic(&value, &val);
1233 info->roaming_policy =
1234 string2roamingpolicy(val);
1236 if (info_last->roaming_policy != info->roaming_policy)
1237 session->info_dirty = TRUE;
1246 if (session->info_dirty == TRUE)
1247 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1249 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1252 return __connman_error_invalid_arguments(msg);
1255 static GDBusMethodTable session_methods[] = {
1256 { "Destroy", "", "", destroy_session },
1257 { "Connect", "", "", connect_session },
1258 { "Disconnect", "", "", disconnect_session },
1259 { "Change", "sv", "", change_session },
1263 int __connman_session_create(DBusMessage *msg)
1265 const char *owner, *notify_path;
1266 char *session_path = NULL;
1267 DBusMessageIter iter, array;
1268 struct connman_session *session = NULL;
1269 struct session_info *info, *info_last;
1271 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1272 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1273 enum connman_session_roaming_policy roaming_policy =
1274 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1275 GSList *allowed_bearers = NULL;
1276 unsigned int periodic_connect = 0;
1277 unsigned int idle_timeout = 0;
1281 owner = dbus_message_get_sender(msg);
1283 DBG("owner %s", owner);
1285 if (ecall_session != NULL) {
1287 * If there is an emergency call already going on,
1288 * ignore session creation attempt
1294 dbus_message_iter_init(msg, &iter);
1295 dbus_message_iter_recurse(&iter, &array);
1297 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1298 DBusMessageIter entry, value;
1299 const char *key, *val;
1301 dbus_message_iter_recurse(&array, &entry);
1302 dbus_message_iter_get_basic(&entry, &key);
1304 dbus_message_iter_next(&entry);
1305 dbus_message_iter_recurse(&entry, &value);
1307 switch (dbus_message_iter_get_arg_type(&value)) {
1308 case DBUS_TYPE_ARRAY:
1309 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1311 session_parse_allowed_bearers(&value);
1316 case DBUS_TYPE_BOOLEAN:
1317 if (g_str_equal(key, "Priority") == TRUE) {
1318 dbus_message_iter_get_basic(&value,
1320 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1321 dbus_message_iter_get_basic(&value,
1323 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1324 dbus_message_iter_get_basic(&value,
1326 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1327 dbus_message_iter_get_basic(&value,
1333 case DBUS_TYPE_UINT32:
1334 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1335 dbus_message_iter_get_basic(&value,
1337 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1338 dbus_message_iter_get_basic(&value,
1344 case DBUS_TYPE_STRING:
1345 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1346 dbus_message_iter_get_basic(&value, &val);
1347 roaming_policy = string2roamingpolicy(val);
1352 dbus_message_iter_next(&array);
1355 dbus_message_iter_next(&iter);
1356 dbus_message_iter_get_basic(&iter, ¬ify_path);
1358 if (notify_path == NULL) {
1363 session_path = g_strdup_printf("/sessions%s", notify_path);
1364 if (session_path == NULL) {
1369 session = g_hash_table_lookup(session_hash, session_path);
1370 if (session != NULL) {
1376 session = g_try_new0(struct connman_session, 1);
1377 if (session == NULL) {
1382 session->info = g_try_new0(struct session_info, 1);
1383 if (session->info == NULL) {
1388 session->info_last = g_try_new0(struct session_info, 1);
1389 if (session->info_last == NULL) {
1394 info = session->info;
1395 info_last = session->info_last;
1397 session->owner = g_strdup(owner);
1398 session->session_path = session_path;
1399 session->notify_path = g_strdup(notify_path);
1400 session->notify_watch =
1401 g_dbus_add_disconnect_watch(connection, session->owner,
1402 owner_disconnect, session, NULL);
1404 session->service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1407 info->online = FALSE;
1408 info->priority = priority;
1409 info->avoid_handover = avoid_handover;
1410 info->stay_connected = stay_connected;
1411 info->periodic_connect = periodic_connect;
1412 info->idle_timeout = idle_timeout;
1413 info->ecall = ecall;
1414 info->roaming_policy = roaming_policy;
1418 if (allowed_bearers == NULL) {
1419 info->allowed_bearers =
1420 session_allowed_bearers_any();
1422 if (info->allowed_bearers == NULL) {
1427 info->allowed_bearers = allowed_bearers;
1430 g_hash_table_replace(session_hash, session->session_path, session);
1432 DBG("add %s", session->session_path);
1434 if (g_dbus_register_interface(connection, session->session_path,
1435 CONNMAN_SESSION_INTERFACE,
1436 session_methods, NULL,
1437 NULL, session, NULL) == FALSE) {
1438 connman_error("Failed to register %s", session->session_path);
1439 g_hash_table_remove(session_hash, session->session_path);
1446 g_dbus_send_reply(connection, msg,
1447 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1451 update_allowed_bearers(session);
1452 if (info->ecall == TRUE) {
1453 ecall_session = session;
1454 update_ecall_sessions(session);
1457 info_last->online = info->online;
1458 info_last->priority = info->priority;
1459 info_last->avoid_handover = info->avoid_handover;
1460 info_last->stay_connected = info->stay_connected;
1461 info_last->periodic_connect = info->periodic_connect;
1462 info_last->idle_timeout = info->idle_timeout;
1463 info_last->ecall = info->ecall;
1464 info_last->roaming_policy = info->roaming_policy;
1465 info_last->entry = info->entry;
1466 info_last->marker = info->marker;
1467 info_last->allowed_bearers = info->allowed_bearers;
1469 session->info_dirty = TRUE;
1470 session->append_all = TRUE;
1472 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1477 connman_error("Failed to create session");
1479 if (session != NULL) {
1480 if (session->info_last != NULL)
1481 g_free(session->info_last);
1482 if (session->info != NULL)
1483 g_free(session->info);
1487 g_free(session_path);
1489 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1490 g_slist_free(allowed_bearers);
1495 int __connman_session_destroy(DBusMessage *msg)
1497 const char *owner, *session_path;
1498 struct connman_session *session;
1500 owner = dbus_message_get_sender(msg);
1502 DBG("owner %s", owner);
1504 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1506 if (session_path == NULL)
1509 session = g_hash_table_lookup(session_hash, session_path);
1510 if (session == NULL)
1513 if (g_strcmp0(owner, session->owner) != 0)
1516 session_disconnect(session);
1521 connman_bool_t __connman_session_mode()
1526 void __connman_session_set_mode(connman_bool_t enable)
1528 DBG("enable %d", enable);
1530 if (sessionmode == enable)
1533 sessionmode = enable;
1535 if (sessionmode == TRUE)
1536 __connman_service_disconnect_all();
1539 static void service_add(struct connman_service *service)
1541 GHashTableIter iter;
1542 GSequenceIter *iter_service_list;
1543 gpointer key, value;
1544 struct connman_session *session;
1546 DBG("service %p", service);
1548 g_hash_table_iter_init(&iter, session_hash);
1550 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1553 if (service_match(session, service) == FALSE)
1557 g_sequence_insert_sorted(session->service_list,
1558 service, sort_services,
1561 g_hash_table_replace(session->service_hash, service,
1564 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1568 static void service_remove(struct connman_service *service)
1571 GHashTableIter iter;
1572 gpointer key, value;
1573 struct connman_session *session;
1574 struct session_info *info;
1576 DBG("service %p", service);
1578 g_hash_table_iter_init(&iter, session_hash);
1580 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1581 GSequenceIter *iter;
1583 info = session->info;
1585 iter = g_hash_table_lookup(session->service_hash, service);
1589 g_sequence_remove(iter);
1592 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1596 static void service_state_changed(struct connman_service *service,
1597 enum connman_service_state state)
1599 GHashTableIter iter;
1600 gpointer key, value;
1601 struct connman_session *session;
1602 struct session_info *info, *info_last;
1604 DBG("service %p state %d", service, state);
1606 g_hash_table_iter_init(&iter, session_hash);
1608 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1610 info = session->info;
1611 info_last = session->info_last;
1613 if (info->entry != NULL && info->entry->service == service) {
1614 info->entry->state = state;
1615 info->online = is_online(info->entry->state);
1616 if (info_last->online != info->online)
1617 session->info_dirty = TRUE;
1620 session_changed(session,
1621 CONNMAN_SESSION_TRIGGER_SERVICE);
1625 static void ipconfig_changed(struct connman_service *service,
1626 struct connman_ipconfig *ipconfig)
1628 GHashTableIter iter;
1629 gpointer key, value;
1630 struct connman_session *session;
1631 struct session_info *info;
1632 enum connman_ipconfig_type type;
1634 DBG("service %p ipconfig %p", service, ipconfig);
1636 type = __connman_ipconfig_get_config_type(ipconfig);
1638 g_hash_table_iter_init(&iter, session_hash);
1640 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1642 info = session->info;
1644 if (info->entry != NULL && info->entry->service == service) {
1645 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1646 ipconfig_ipv4_changed(session);
1647 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1648 ipconfig_ipv6_changed(session);
1653 static struct connman_notifier session_notifier = {
1655 .service_add = service_add,
1656 .service_remove = service_remove,
1657 .service_state_changed = service_state_changed,
1658 .ipconfig_changed = ipconfig_changed,
1661 int __connman_session_init(void)
1667 connection = connman_dbus_get_connection();
1668 if (connection == NULL)
1671 err = connman_notifier_register(&session_notifier);
1673 dbus_connection_unref(connection);
1677 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1678 NULL, cleanup_session);
1680 sessionmode = FALSE;
1684 void __connman_session_cleanup(void)
1688 if (connection == NULL)
1691 connman_notifier_unregister(&session_notifier);
1693 g_hash_table_foreach(session_hash, release_session, NULL);
1694 g_hash_table_destroy(session_hash);
1696 dbus_connection_unref(connection);