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 session_info *ecall_info;
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_DISCONNECT = 2,
52 CONNMAN_SESSION_REASON_FREE_RIDE = 3,
53 CONNMAN_SESSION_REASON_PERIODIC = 4,
56 enum connman_session_roaming_policy {
57 CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN = 0,
58 CONNMAN_SESSION_ROAMING_POLICY_DEFAULT = 1,
59 CONNMAN_SESSION_ROAMING_POLICY_ALWAYS = 2,
60 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN = 3,
61 CONNMAN_SESSION_ROAMING_POLICY_NATIONAL = 4,
62 CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL = 5,
65 struct service_entry {
66 /* track why this service was selected */
67 enum connman_session_reason reason;
68 enum connman_service_state state;
70 struct connman_service *service;
76 connman_bool_t online;
77 connman_bool_t priority;
78 GSList *allowed_bearers;
79 connman_bool_t avoid_handover;
80 connman_bool_t stay_connected;
81 unsigned int periodic_connect;
82 unsigned int idle_timeout;
84 enum connman_session_roaming_policy roaming_policy;
87 struct service_entry *entry;
88 enum connman_session_reason reason;
91 struct connman_session {
97 connman_bool_t append_all;
98 connman_bool_t info_dirty;
99 struct session_info *info;
100 struct session_info *info_last;
102 GSequence *service_list;
103 GHashTable *service_hash;
108 connman_bool_t match_all;
109 enum connman_service_type service_type;
112 static const char *trigger2string(enum connman_session_trigger trigger)
115 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
117 case CONNMAN_SESSION_TRIGGER_SETTING:
119 case CONNMAN_SESSION_TRIGGER_CONNECT:
121 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
123 case CONNMAN_SESSION_TRIGGER_PERIODIC:
125 case CONNMAN_SESSION_TRIGGER_SERVICE:
127 case CONNMAN_SESSION_TRIGGER_ECALL:
134 static const char *reason2string(enum connman_session_reason reason)
137 case CONNMAN_SESSION_REASON_UNKNOWN:
139 case CONNMAN_SESSION_REASON_CONNECT:
141 case CONNMAN_SESSION_REASON_DISCONNECT:
143 case CONNMAN_SESSION_REASON_FREE_RIDE:
145 case CONNMAN_SESSION_REASON_PERIODIC:
152 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
155 case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
157 case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
159 case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
161 case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
163 case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
165 case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
166 return "international";
172 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
174 if (g_strcmp0(policy, "default") == 0)
175 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
176 else if (g_strcmp0(policy, "always") == 0)
177 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
178 else if (g_strcmp0(policy, "forbidden") == 0)
179 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
180 else if (g_strcmp0(policy, "national") == 0)
181 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
182 else if (g_strcmp0(policy, "international") == 0)
183 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
185 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
188 static enum connman_service_type bearer2service(const char *bearer)
191 return CONNMAN_SERVICE_TYPE_UNKNOWN;
193 if (g_strcmp0(bearer, "ethernet") == 0)
194 return CONNMAN_SERVICE_TYPE_ETHERNET;
195 else if (g_strcmp0(bearer, "wifi") == 0)
196 return CONNMAN_SERVICE_TYPE_WIFI;
197 else if (g_strcmp0(bearer, "wimax") == 0)
198 return CONNMAN_SERVICE_TYPE_WIMAX;
199 else if (g_strcmp0(bearer, "bluetooth") == 0)
200 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
201 else if (g_strcmp0(bearer, "cellular") == 0)
202 return CONNMAN_SERVICE_TYPE_CELLULAR;
203 else if (g_strcmp0(bearer, "vpn") == 0)
204 return CONNMAN_SERVICE_TYPE_VPN;
206 return CONNMAN_SERVICE_TYPE_UNKNOWN;
209 static char *service2bearer(enum connman_service_type type)
212 case CONNMAN_SERVICE_TYPE_ETHERNET:
214 case CONNMAN_SERVICE_TYPE_WIFI:
216 case CONNMAN_SERVICE_TYPE_WIMAX:
218 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
220 case CONNMAN_SERVICE_TYPE_CELLULAR:
222 case CONNMAN_SERVICE_TYPE_VPN:
224 case CONNMAN_SERVICE_TYPE_UNKNOWN:
225 case CONNMAN_SERVICE_TYPE_SYSTEM:
226 case CONNMAN_SERVICE_TYPE_GPS:
227 case CONNMAN_SERVICE_TYPE_GADGET:
234 static void cleanup_bearer_info(gpointer data, gpointer user_data)
236 struct bearer_info *info = data;
242 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
244 struct bearer_info *info;
245 DBusMessageIter array;
248 dbus_message_iter_recurse(iter, &array);
250 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
253 dbus_message_iter_get_basic(&array, &bearer);
255 info = g_try_new0(struct bearer_info, 1);
257 g_slist_foreach(list, cleanup_bearer_info, NULL);
263 info->name = g_strdup(bearer);
264 info->service_type = bearer2service(info->name);
266 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
267 g_strcmp0(info->name, "*") == 0) {
268 info->match_all = TRUE;
270 info->match_all = FALSE;
273 list = g_slist_append(list, info);
275 dbus_message_iter_next(&array);
281 static GSList *session_allowed_bearers_any(void)
283 struct bearer_info *info;
286 info = g_try_new0(struct bearer_info, 1);
293 info->name = g_strdup("");
294 info->match_all = TRUE;
295 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
297 list = g_slist_append(list, info);
302 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
304 struct session_info *info = user_data;
307 for (list = info->allowed_bearers;
308 list != NULL; list = list->next) {
309 struct bearer_info *info = list->data;
311 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
316 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
318 struct connman_service *service = user_data;
319 struct connman_ipconfig *ipconfig_ipv4;
324 ipconfig_ipv4 = __connman_service_get_ip4config(service);
325 if (ipconfig_ipv4 == NULL)
328 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
331 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
333 struct connman_service *service = user_data;
334 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
339 ipconfig_ipv4 = __connman_service_get_ip4config(service);
340 ipconfig_ipv6 = __connman_service_get_ip6config(service);
341 if (ipconfig_ipv6 == NULL)
344 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
347 static void append_notify(DBusMessageIter *dict,
348 struct connman_session *session)
350 struct session_info *info = session->info;
351 struct session_info *info_last = session->info_last;
353 struct connman_service *service;
354 const char *name, *ifname, *bearer;
356 if (session->append_all == TRUE ||
357 info->online != info_last->online) {
358 connman_dbus_dict_append_basic(dict, "Online",
361 info_last->online = info->online;
364 if (session->append_all == TRUE ||
365 info->entry != info_last->entry) {
366 if (info->entry == NULL) {
372 name = info->entry->name;
373 ifname = info->entry->ifname;
374 service = info->entry->service;
375 bearer = info->entry->bearer;
378 connman_dbus_dict_append_basic(dict, "Name",
382 connman_dbus_dict_append_dict(dict, "IPv4",
383 append_ipconfig_ipv4,
386 connman_dbus_dict_append_dict(dict, "IPv6",
387 append_ipconfig_ipv6,
390 connman_dbus_dict_append_basic(dict, "Interface",
394 connman_dbus_dict_append_basic(dict, "Bearer",
398 info_last->entry = info->entry;
402 if (session->append_all == TRUE ||
403 info->priority != info_last->priority) {
404 connman_dbus_dict_append_basic(dict, "Priority",
407 info_last->priority = info->priority;
410 if (session->append_all == TRUE ||
411 info->allowed_bearers != info_last->allowed_bearers) {
412 connman_dbus_dict_append_array(dict, "AllowedBearers",
414 append_allowed_bearers,
416 info_last->allowed_bearers = info->allowed_bearers;
419 if (session->append_all == TRUE ||
420 info->avoid_handover != info_last->avoid_handover) {
421 connman_dbus_dict_append_basic(dict, "AvoidHandover",
423 &info->avoid_handover);
424 info_last->avoid_handover = info->avoid_handover;
427 if (session->append_all == TRUE ||
428 info->stay_connected != info_last->stay_connected) {
429 connman_dbus_dict_append_basic(dict, "StayConnected",
431 &info->stay_connected);
432 info_last->stay_connected = info->stay_connected;
435 if (session->append_all == TRUE ||
436 info->periodic_connect != info_last->periodic_connect) {
437 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
439 &info->periodic_connect);
440 info_last->periodic_connect = info->periodic_connect;
443 if (session->append_all == TRUE ||
444 info->idle_timeout != info_last->idle_timeout) {
445 connman_dbus_dict_append_basic(dict, "IdleTimeout",
447 &info->idle_timeout);
448 info_last->idle_timeout = info->idle_timeout;
451 if (session->append_all == TRUE ||
452 info->ecall != info_last->ecall) {
453 connman_dbus_dict_append_basic(dict, "EmergencyCall",
456 info_last->ecall = info->ecall;
459 if (session->append_all == TRUE ||
460 info->roaming_policy != info_last->roaming_policy) {
461 policy = roamingpolicy2string(info->roaming_policy);
462 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
465 info_last->roaming_policy = info->roaming_policy;
468 if (session->append_all == TRUE ||
469 info->marker != info_last->marker) {
470 connman_dbus_dict_append_basic(dict, "SessionMarker",
473 info_last->marker = info->marker;
476 session->append_all = FALSE;
477 session->info_dirty = FALSE;
480 static gboolean session_notify(gpointer user_data)
482 struct connman_session *session = user_data;
484 DBusMessageIter array, dict;
486 if (session->info_dirty == FALSE)
489 DBG("session %p owner %s notify_path %s", session,
490 session->owner, session->notify_path);
492 msg = dbus_message_new_method_call(session->owner, session->notify_path,
493 CONNMAN_NOTIFICATION_INTERFACE,
498 dbus_message_iter_init_append(msg, &array);
499 connman_dbus_dict_open(&array, &dict);
501 append_notify(&dict, session);
503 connman_dbus_dict_close(&array, &dict);
505 g_dbus_send_message(connection, msg);
507 session->info_dirty = FALSE;
512 static void ipconfig_ipv4_changed(struct connman_session *session)
514 struct session_info *info = session->info;
516 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
517 "IPv4", append_ipconfig_ipv4,
518 info->entry->service);
521 static void ipconfig_ipv6_changed(struct connman_session *session)
523 struct session_info *info = session->info;
525 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
526 "IPv6", append_ipconfig_ipv6,
527 info->entry->service);
530 static connman_bool_t service_type_match(struct connman_session *session,
531 struct connman_service *service)
533 struct session_info *info = session->info;
536 for (list = info->allowed_bearers;
537 list != NULL; list = list->next) {
538 struct bearer_info *info = list->data;
539 enum connman_service_type service_type;
541 if (info->match_all == TRUE)
544 service_type = connman_service_get_type(service);
545 if (info->service_type == service_type)
552 static connman_bool_t service_match(struct connman_session *session,
553 struct connman_service *service)
555 if (service_type_match(session, service) == FALSE)
561 static int service_type_weight(enum connman_service_type type)
564 * The session doesn't care which service
565 * to use. Nevertheless we have to sort them
566 * according their type. The ordering is
575 case CONNMAN_SERVICE_TYPE_ETHERNET:
577 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
579 case CONNMAN_SERVICE_TYPE_WIFI:
580 case CONNMAN_SERVICE_TYPE_WIMAX:
582 case CONNMAN_SERVICE_TYPE_CELLULAR:
584 case CONNMAN_SERVICE_TYPE_UNKNOWN:
585 case CONNMAN_SERVICE_TYPE_SYSTEM:
586 case CONNMAN_SERVICE_TYPE_GPS:
587 case CONNMAN_SERVICE_TYPE_VPN:
588 case CONNMAN_SERVICE_TYPE_GADGET:
595 static gint sort_allowed_bearers(struct connman_service *service_a,
596 struct connman_service *service_b,
597 struct connman_session *session)
599 struct session_info *info = session->info;
601 enum connman_service_type type_a, type_b;
602 int weight_a, weight_b;
604 type_a = connman_service_get_type(service_a);
605 type_b = connman_service_get_type(service_b);
607 for (list = info->allowed_bearers;
608 list != NULL; list = list->next) {
609 struct bearer_info *info = list->data;
611 if (info->match_all == TRUE) {
612 if (type_a != type_b) {
613 weight_a = service_type_weight(type_a);
614 weight_b = service_type_weight(type_b);
616 if (weight_a > weight_b)
619 if (weight_a < weight_b)
626 if (type_a == info->service_type &&
627 type_b == info->service_type) {
631 if (type_a == info->service_type &&
632 type_b != info->service_type) {
636 if (type_a != info->service_type &&
637 type_b == info->service_type) {
645 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
647 struct service_entry *entry_a = (void *)a;
648 struct service_entry *entry_b = (void *)b;
649 struct connman_session *session = user_data;
651 return sort_allowed_bearers(entry_a->service, entry_b->service,
655 static void cleanup_session(gpointer user_data)
657 struct connman_session *session = user_data;
658 struct session_info *info = session->info;
660 DBG("remove %s", session->session_path);
662 g_hash_table_destroy(session->service_hash);
663 g_sequence_free(session->service_list);
665 if (info->entry != NULL &&
666 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
667 __connman_service_disconnect(info->entry->service);
670 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
671 g_slist_free(info->allowed_bearers);
673 g_free(session->owner);
674 g_free(session->session_path);
675 g_free(session->notify_path);
676 g_free(session->info);
677 g_free(session->info_last);
682 static connman_bool_t is_online(enum connman_service_state state)
685 case CONNMAN_SERVICE_STATE_UNKNOWN:
686 case CONNMAN_SERVICE_STATE_IDLE:
687 case CONNMAN_SERVICE_STATE_ASSOCIATION:
688 case CONNMAN_SERVICE_STATE_CONFIGURATION:
689 case CONNMAN_SERVICE_STATE_DISCONNECT:
690 case CONNMAN_SERVICE_STATE_FAILURE:
691 case CONNMAN_SERVICE_STATE_READY:
693 case CONNMAN_SERVICE_STATE_ONLINE:
700 static connman_bool_t is_connecting(enum connman_service_state state)
703 case CONNMAN_SERVICE_STATE_UNKNOWN:
704 case CONNMAN_SERVICE_STATE_IDLE:
706 case CONNMAN_SERVICE_STATE_ASSOCIATION:
707 case CONNMAN_SERVICE_STATE_CONFIGURATION:
708 case CONNMAN_SERVICE_STATE_READY:
710 case CONNMAN_SERVICE_STATE_DISCONNECT:
711 case CONNMAN_SERVICE_STATE_FAILURE:
712 case CONNMAN_SERVICE_STATE_ONLINE:
719 static connman_bool_t explicit_connect(enum connman_session_reason reason)
722 case CONNMAN_SESSION_REASON_UNKNOWN:
723 case CONNMAN_SESSION_REASON_FREE_RIDE:
724 case CONNMAN_SESSION_REASON_DISCONNECT:
726 case CONNMAN_SESSION_REASON_CONNECT:
727 case CONNMAN_SESSION_REASON_PERIODIC:
734 static connman_bool_t explicit_disconnect(struct session_info *info)
736 if (info->entry == NULL)
739 DBG("reason %s service %p state %d",
740 reason2string(info->entry->reason),
741 info->entry->service, info->entry->state);
743 if (info->entry->reason == CONNMAN_SESSION_REASON_UNKNOWN)
746 if (explicit_connect(info->entry->reason) == FALSE)
749 if (__connman_service_session_dec(info->entry->service) == FALSE)
752 if (ecall_info != NULL && ecall_info != info)
758 static gboolean call_disconnect(gpointer user_data)
760 struct connman_service *service = user_data;
763 * TODO: We should mark this entry as pending work. In case
764 * disconnect fails we just unassign this session from the
765 * service and can't do anything later on it
767 DBG("disconnect service %p", service);
768 __connman_service_disconnect(service);
773 static gboolean call_connect(gpointer user_data)
775 struct connman_service *service = user_data;
777 DBG("connect service %p", service);
778 __connman_service_connect(service);
783 static void deselect_service(struct session_info *info)
785 struct connman_service *service;
786 connman_bool_t disconnect, online;
790 if (info->entry == NULL)
793 disconnect = explicit_disconnect(info);
795 online = is_connecting(info->entry->state) == TRUE ||
796 is_online(info->entry->state) == TRUE;
798 info->online = FALSE;
799 info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
801 service = info->entry->service;
804 DBG("disconnect %d online %d", disconnect, online);
806 if (disconnect == TRUE && online == TRUE)
807 g_timeout_add_seconds(0, call_disconnect, service);
810 static void deselect_and_disconnect(struct connman_session *session,
811 enum connman_session_reason reason)
813 struct session_info *info = session->info;
815 deselect_service(info);
817 info->reason = reason;
820 static void select_online_service(struct session_info *info,
821 struct service_entry *entry)
826 info->entry->reason = info->reason;
828 if (explicit_connect(info->reason) == FALSE)
831 __connman_service_session_inc(info->entry->service);
834 static void select_offline_service(struct session_info *info,
835 struct service_entry *entry)
837 if (explicit_connect(info->reason) == FALSE)
840 info->online = FALSE;
843 info->entry->reason = info->reason;
845 __connman_service_session_inc(info->entry->service);
846 g_timeout_add_seconds(0, call_connect, info->entry->service);
849 static void select_service(struct session_info *info,
850 struct service_entry *entry)
852 DBG("service %p", entry->service);
854 if (is_online(entry->state) == TRUE)
855 select_online_service(info, entry);
857 select_offline_service(info, entry);
860 static void select_and_connect(struct connman_session *session,
861 enum connman_session_reason reason)
863 struct session_info *info = session->info;
864 struct service_entry *entry = NULL;
867 DBG("session %p reason %s", session, reason2string(reason));
869 info->reason = reason;
871 iter = g_sequence_get_begin_iter(session->service_list);
873 while (g_sequence_iter_is_end(iter) == FALSE) {
874 entry = g_sequence_get(iter);
876 switch (entry->state) {
877 case CONNMAN_SERVICE_STATE_ASSOCIATION:
878 case CONNMAN_SERVICE_STATE_CONFIGURATION:
879 case CONNMAN_SERVICE_STATE_READY:
880 case CONNMAN_SERVICE_STATE_ONLINE:
881 case CONNMAN_SERVICE_STATE_IDLE:
882 case CONNMAN_SERVICE_STATE_DISCONNECT:
883 select_service(info, entry);
885 case CONNMAN_SERVICE_STATE_UNKNOWN:
886 case CONNMAN_SERVICE_STATE_FAILURE:
890 iter = g_sequence_iter_next(iter);
894 static struct service_entry *create_service_entry(struct connman_service *service,
896 enum connman_service_state state)
898 struct service_entry *entry;
899 enum connman_service_type type;
902 entry = g_try_new0(struct service_entry, 1);
906 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
907 entry->state = state;
912 entry->service = service;
914 idx = __connman_service_get_index(entry->service);
915 entry->ifname = connman_inet_ifname(idx);
916 if (entry->ifname == NULL)
917 entry->ifname = g_strdup("");
919 type = connman_service_get_type(entry->service);
920 entry->bearer = service2bearer(type);
925 static void destroy_service_entry(gpointer data)
927 struct service_entry *entry = data;
929 g_free(entry->ifname);
934 static void populate_service_list(struct connman_session *session)
936 struct service_entry *entry;
939 session->service_hash =
940 g_hash_table_new_full(g_direct_hash, g_direct_equal,
942 session->service_list = __connman_service_get_list(session,
944 create_service_entry,
945 destroy_service_entry);
947 g_sequence_sort(session->service_list, sort_services, session);
949 iter = g_sequence_get_begin_iter(session->service_list);
951 while (g_sequence_iter_is_end(iter) == FALSE) {
952 entry = g_sequence_get(iter);
954 DBG("service %p type %s name %s", entry->service,
955 service2bearer(connman_service_get_type(entry->service)),
958 g_hash_table_replace(session->service_hash,
959 entry->service, iter);
961 iter = g_sequence_iter_next(iter);
965 static void session_changed(struct connman_session *session,
966 enum connman_session_trigger trigger)
968 struct session_info *info = session->info;
969 struct session_info *info_last = session->info_last;
970 GSequenceIter *service_iter = NULL, *service_iter_last = NULL;
971 GSequence *service_list_last;
972 GHashTable *service_hash_last;
975 * TODO: This only a placeholder for the 'real' algorithm to
976 * play a bit around. So we are going to improve it step by step.
979 DBG("session %p trigger %s reason %s", session, trigger2string(trigger),
980 reason2string(info->reason));
982 if (info->entry != NULL) {
983 info->online = is_online(info->entry->state);
984 if (info_last->online != info->online)
985 session->info_dirty = TRUE;
989 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
990 DBG("ignore session changed event");
992 case CONNMAN_SESSION_TRIGGER_SETTING:
993 if (info->allowed_bearers != info_last->allowed_bearers) {
995 service_hash_last = session->service_hash;
996 service_list_last = session->service_list;
998 populate_service_list(session);
1000 if (info->entry != NULL) {
1001 service_iter_last = g_hash_table_lookup(
1003 info->entry->service);
1004 service_iter = g_hash_table_lookup(
1005 session->service_hash,
1006 info->entry->service);
1009 if (service_iter == NULL && service_iter_last != NULL) {
1011 * The currently selected service is
1012 * not part of this session anymore.
1014 deselect_and_disconnect(session, info->reason);
1017 g_hash_table_remove_all(service_hash_last);
1018 g_sequence_free(service_list_last);
1021 if (info->online == FALSE) {
1022 select_and_connect(session,
1023 CONNMAN_SESSION_REASON_FREE_RIDE);
1027 case CONNMAN_SESSION_TRIGGER_CONNECT:
1028 if (info->online == TRUE) {
1029 if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
1031 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
1032 __connman_service_session_inc(info->entry->service);
1036 select_and_connect(session,
1037 CONNMAN_SESSION_REASON_CONNECT);
1040 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
1041 deselect_and_disconnect(session,
1042 CONNMAN_SESSION_REASON_DISCONNECT);
1045 case CONNMAN_SESSION_TRIGGER_PERIODIC:
1046 if (info->online == TRUE) {
1047 info->entry->reason = CONNMAN_SESSION_REASON_PERIODIC;
1048 __connman_service_session_inc(info->entry->service);
1052 select_and_connect(session,
1053 CONNMAN_SESSION_REASON_PERIODIC);
1056 case CONNMAN_SESSION_TRIGGER_SERVICE:
1057 if (info->entry != NULL &&
1058 (is_connecting(info->entry->state) == TRUE ||
1059 is_online(info->entry->state) == TRUE)) {
1063 deselect_and_disconnect(session, info->reason);
1065 if (info->reason == CONNMAN_SESSION_REASON_FREE_RIDE ||
1066 info->stay_connected == TRUE) {
1067 select_and_connect(session, info->reason);
1071 case CONNMAN_SESSION_TRIGGER_ECALL:
1072 if (info->online == FALSE && info->entry != NULL &&
1073 info->entry->service != NULL) {
1074 deselect_and_disconnect(session, info->reason);
1080 if (info->entry != info_last->entry)
1081 session->info_dirty = TRUE;
1083 session_notify(session);
1086 static DBusMessage *connect_session(DBusConnection *conn,
1087 DBusMessage *msg, void *user_data)
1089 struct connman_session *session = user_data;
1090 struct session_info *info = session->info;
1092 DBG("session %p", session);
1094 if (ecall_info != NULL && ecall_info != info)
1095 return __connman_error_failed(msg, EBUSY);
1097 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1099 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1102 static DBusMessage *disconnect_session(DBusConnection *conn,
1103 DBusMessage *msg, void *user_data)
1105 struct connman_session *session = user_data;
1106 struct session_info *info = session->info;
1108 DBG("session %p", session);
1110 if (ecall_info != NULL && ecall_info != info)
1111 return __connman_error_failed(msg, EBUSY);
1113 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1115 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1118 static void update_ecall_sessions(struct connman_session *session)
1120 struct session_info *info = session->info;
1121 struct connman_session *session_iter;
1122 GHashTableIter iter;
1123 gpointer key, value;
1125 g_hash_table_iter_init(&iter, session_hash);
1127 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1128 session_iter = value;
1130 if (session_iter == session)
1133 session_iter->info->ecall = info->ecall;
1134 session_iter->info_dirty = TRUE;
1136 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
1140 static void update_ecall(struct connman_session *session)
1142 struct session_info *info = session->info;
1143 struct session_info *info_last = session->info_last;
1145 DBG("session %p ecall_info %p ecall %d -> %d", session,
1146 ecall_info, info_last->ecall, info->ecall);
1148 if (ecall_info == NULL) {
1149 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
1153 } else if (ecall_info == info) {
1154 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
1162 update_ecall_sessions(session);
1164 session->info_dirty = TRUE;
1168 /* not a valid transition */
1169 info->ecall = info_last->ecall;
1172 static DBusMessage *change_session(DBusConnection *conn,
1173 DBusMessage *msg, void *user_data)
1175 struct connman_session *session = user_data;
1176 struct session_info *info = session->info;
1177 struct session_info *info_last = session->info_last;
1178 DBusMessageIter iter, value;
1180 GSList *allowed_bearers;
1182 DBG("session %p", session);
1183 if (dbus_message_iter_init(msg, &iter) == FALSE)
1184 return __connman_error_invalid_arguments(msg);
1186 dbus_message_iter_get_basic(&iter, &name);
1187 dbus_message_iter_next(&iter);
1188 dbus_message_iter_recurse(&iter, &value);
1190 switch (dbus_message_iter_get_arg_type(&value)) {
1191 case DBUS_TYPE_ARRAY:
1192 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1193 allowed_bearers = session_parse_allowed_bearers(&value);
1195 g_slist_foreach(info->allowed_bearers,
1196 cleanup_bearer_info, NULL);
1197 g_slist_free(info->allowed_bearers);
1199 if (allowed_bearers == NULL) {
1200 allowed_bearers = session_allowed_bearers_any();
1202 if (allowed_bearers == NULL)
1203 return __connman_error_failed(msg, ENOMEM);
1206 info->allowed_bearers = allowed_bearers;
1208 session->info_dirty = TRUE;
1213 case DBUS_TYPE_BOOLEAN:
1214 if (g_str_equal(name, "Priority") == TRUE) {
1215 dbus_message_iter_get_basic(&value,
1218 if (info_last->priority != info->priority)
1219 session->info_dirty = TRUE;
1220 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1221 dbus_message_iter_get_basic(&value,
1222 &info->avoid_handover);
1224 if (info_last->avoid_handover != info->avoid_handover)
1225 session->info_dirty = TRUE;
1226 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1227 dbus_message_iter_get_basic(&value,
1228 &info->stay_connected);
1230 if (info_last->stay_connected != info->stay_connected)
1231 session->info_dirty = TRUE;
1232 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1233 dbus_message_iter_get_basic(&value,
1236 update_ecall(session);
1241 case DBUS_TYPE_UINT32:
1242 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1243 dbus_message_iter_get_basic(&value,
1244 &info->periodic_connect);
1246 if (info_last->periodic_connect != info->periodic_connect)
1247 session->info_dirty = TRUE;
1248 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1249 dbus_message_iter_get_basic(&value,
1250 &info->idle_timeout);
1252 if (info_last->idle_timeout != info->idle_timeout)
1253 session->info_dirty = TRUE;
1258 case DBUS_TYPE_STRING:
1259 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1261 dbus_message_iter_get_basic(&value, &val);
1262 info->roaming_policy =
1263 string2roamingpolicy(val);
1265 if (info_last->roaming_policy != info->roaming_policy)
1266 session->info_dirty = TRUE;
1275 if (session->info_dirty == TRUE)
1276 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1278 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1281 return __connman_error_invalid_arguments(msg);
1284 static void release_session(gpointer key, gpointer value, gpointer user_data)
1286 struct connman_session *session = value;
1287 DBusMessage *message;
1289 DBG("owner %s path %s", session->owner, session->notify_path);
1291 if (session->notify_watch > 0)
1292 g_dbus_remove_watch(connection, session->notify_watch);
1294 g_dbus_unregister_interface(connection, session->session_path,
1295 CONNMAN_SESSION_INTERFACE);
1297 message = dbus_message_new_method_call(session->owner,
1298 session->notify_path,
1299 CONNMAN_NOTIFICATION_INTERFACE,
1301 if (message == NULL)
1304 dbus_message_set_no_reply(message, TRUE);
1306 g_dbus_send_message(connection, message);
1309 static int session_disconnect(struct connman_session *session)
1311 DBG("session %p, %s", session, session->owner);
1313 if (session->notify_watch > 0)
1314 g_dbus_remove_watch(connection, session->notify_watch);
1316 g_dbus_unregister_interface(connection, session->session_path,
1317 CONNMAN_SESSION_INTERFACE);
1319 deselect_and_disconnect(session,
1320 CONNMAN_SESSION_REASON_DISCONNECT);
1322 g_hash_table_remove(session_hash, session->session_path);
1327 static void owner_disconnect(DBusConnection *conn, void *user_data)
1329 struct connman_session *session = user_data;
1331 DBG("session %p, %s died", session, session->owner);
1333 session_disconnect(session);
1336 static DBusMessage *destroy_session(DBusConnection *conn,
1337 DBusMessage *msg, void *user_data)
1339 struct connman_session *session = user_data;
1341 DBG("session %p", session);
1343 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1346 static GDBusMethodTable session_methods[] = {
1347 { "Destroy", "", "", destroy_session },
1348 { "Connect", "", "", connect_session },
1349 { "Disconnect", "", "", disconnect_session },
1350 { "Change", "sv", "", change_session },
1354 int __connman_session_create(DBusMessage *msg)
1356 const char *owner, *notify_path;
1357 char *session_path = NULL;
1358 DBusMessageIter iter, array;
1359 struct connman_session *session = NULL;
1360 struct session_info *info, *info_last;
1362 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1363 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1364 enum connman_session_roaming_policy roaming_policy =
1365 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1366 GSList *allowed_bearers = NULL;
1367 unsigned int periodic_connect = 0;
1368 unsigned int idle_timeout = 0;
1372 owner = dbus_message_get_sender(msg);
1374 DBG("owner %s", owner);
1376 if (ecall_info != NULL) {
1378 * If there is an emergency call already going on,
1379 * ignore session creation attempt
1385 dbus_message_iter_init(msg, &iter);
1386 dbus_message_iter_recurse(&iter, &array);
1388 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1389 DBusMessageIter entry, value;
1390 const char *key, *val;
1392 dbus_message_iter_recurse(&array, &entry);
1393 dbus_message_iter_get_basic(&entry, &key);
1395 dbus_message_iter_next(&entry);
1396 dbus_message_iter_recurse(&entry, &value);
1398 switch (dbus_message_iter_get_arg_type(&value)) {
1399 case DBUS_TYPE_ARRAY:
1400 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1402 session_parse_allowed_bearers(&value);
1407 case DBUS_TYPE_BOOLEAN:
1408 if (g_str_equal(key, "Priority") == TRUE) {
1409 dbus_message_iter_get_basic(&value,
1411 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1412 dbus_message_iter_get_basic(&value,
1414 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1415 dbus_message_iter_get_basic(&value,
1417 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1418 dbus_message_iter_get_basic(&value,
1424 case DBUS_TYPE_UINT32:
1425 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1426 dbus_message_iter_get_basic(&value,
1428 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1429 dbus_message_iter_get_basic(&value,
1435 case DBUS_TYPE_STRING:
1436 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1437 dbus_message_iter_get_basic(&value, &val);
1438 roaming_policy = string2roamingpolicy(val);
1443 dbus_message_iter_next(&array);
1446 dbus_message_iter_next(&iter);
1447 dbus_message_iter_get_basic(&iter, ¬ify_path);
1449 if (notify_path == NULL) {
1454 session_path = g_strdup_printf("/sessions%s", notify_path);
1455 if (session_path == NULL) {
1460 session = g_hash_table_lookup(session_hash, session_path);
1461 if (session != NULL) {
1467 session = g_try_new0(struct connman_session, 1);
1468 if (session == NULL) {
1473 session->info = g_try_new0(struct session_info, 1);
1474 if (session->info == NULL) {
1479 session->info_last = g_try_new0(struct session_info, 1);
1480 if (session->info_last == NULL) {
1485 info = session->info;
1486 info_last = session->info_last;
1488 session->owner = g_strdup(owner);
1489 session->session_path = session_path;
1490 session->notify_path = g_strdup(notify_path);
1491 session->notify_watch =
1492 g_dbus_add_disconnect_watch(connection, session->owner,
1493 owner_disconnect, session, NULL);
1495 info->online = FALSE;
1496 info->priority = priority;
1497 info->avoid_handover = avoid_handover;
1498 info->stay_connected = stay_connected;
1499 info->periodic_connect = periodic_connect;
1500 info->idle_timeout = idle_timeout;
1501 info->ecall = ecall;
1502 info->roaming_policy = roaming_policy;
1506 if (allowed_bearers == NULL) {
1507 info->allowed_bearers =
1508 session_allowed_bearers_any();
1510 if (info->allowed_bearers == NULL) {
1515 info->allowed_bearers = allowed_bearers;
1518 g_hash_table_replace(session_hash, session->session_path, session);
1520 DBG("add %s", session->session_path);
1522 if (g_dbus_register_interface(connection, session->session_path,
1523 CONNMAN_SESSION_INTERFACE,
1524 session_methods, NULL,
1525 NULL, session, NULL) == FALSE) {
1526 connman_error("Failed to register %s", session->session_path);
1527 g_hash_table_remove(session_hash, session->session_path);
1534 g_dbus_send_reply(connection, msg,
1535 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1539 populate_service_list(session);
1540 if (info->ecall == TRUE) {
1542 update_ecall_sessions(session);
1545 info_last->online = info->online;
1546 info_last->priority = info->priority;
1547 info_last->avoid_handover = info->avoid_handover;
1548 info_last->stay_connected = info->stay_connected;
1549 info_last->periodic_connect = info->periodic_connect;
1550 info_last->idle_timeout = info->idle_timeout;
1551 info_last->ecall = info->ecall;
1552 info_last->roaming_policy = info->roaming_policy;
1553 info_last->entry = info->entry;
1554 info_last->marker = info->marker;
1555 info_last->allowed_bearers = info->allowed_bearers;
1557 session->info_dirty = TRUE;
1558 session->append_all = TRUE;
1560 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1565 connman_error("Failed to create session");
1567 if (session != NULL) {
1568 if (session->info_last != NULL)
1569 g_free(session->info_last);
1570 if (session->info != NULL)
1571 g_free(session->info);
1575 g_free(session_path);
1577 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1578 g_slist_free(allowed_bearers);
1583 int __connman_session_destroy(DBusMessage *msg)
1585 const char *owner, *session_path;
1586 struct connman_session *session;
1588 owner = dbus_message_get_sender(msg);
1590 DBG("owner %s", owner);
1592 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1594 if (session_path == NULL)
1597 session = g_hash_table_lookup(session_hash, session_path);
1598 if (session == NULL)
1601 if (g_strcmp0(owner, session->owner) != 0)
1604 session_disconnect(session);
1609 connman_bool_t __connman_session_mode()
1614 void __connman_session_set_mode(connman_bool_t enable)
1616 DBG("enable %d", enable);
1618 sessionmode = enable;
1620 if (sessionmode == TRUE)
1621 __connman_service_disconnect_all();
1624 static void service_add(struct connman_service *service,
1627 GHashTableIter iter;
1628 GSequenceIter *iter_service_list;
1629 gpointer key, value;
1630 struct connman_session *session;
1631 struct service_entry *entry;
1633 DBG("service %p", service);
1635 g_hash_table_iter_init(&iter, session_hash);
1637 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1640 if (service_match(session, service) == FALSE)
1643 entry = create_service_entry(service, name,
1644 CONNMAN_SERVICE_STATE_IDLE);
1649 g_sequence_insert_sorted(session->service_list,
1650 entry, sort_services,
1653 g_hash_table_replace(session->service_hash, service,
1656 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1660 static void service_remove(struct connman_service *service)
1663 GHashTableIter iter;
1664 gpointer key, value;
1665 struct connman_session *session;
1666 struct session_info *info;
1668 DBG("service %p", service);
1670 g_hash_table_iter_init(&iter, session_hash);
1672 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1673 GSequenceIter *iter;
1675 info = session->info;
1677 iter = g_hash_table_lookup(session->service_hash, service);
1681 g_sequence_remove(iter);
1683 if (info->entry != NULL && info->entry->service == service)
1685 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1689 static void service_state_changed(struct connman_service *service,
1690 enum connman_service_state state)
1692 GHashTableIter iter;
1693 gpointer key, value;
1695 DBG("service %p state %d", service, state);
1697 g_hash_table_iter_init(&iter, session_hash);
1699 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1700 struct connman_session *session = value;
1701 GSequenceIter *service_iter;
1703 service_iter = g_hash_table_lookup(session->service_hash, service);
1704 if (service_iter != NULL) {
1705 struct service_entry *entry;
1707 entry = g_sequence_get(service_iter);
1708 entry->state = state;
1711 session_changed(session,
1712 CONNMAN_SESSION_TRIGGER_SERVICE);
1716 static void ipconfig_changed(struct connman_service *service,
1717 struct connman_ipconfig *ipconfig)
1719 GHashTableIter iter;
1720 gpointer key, value;
1721 struct connman_session *session;
1722 struct session_info *info;
1723 enum connman_ipconfig_type type;
1725 DBG("service %p ipconfig %p", service, ipconfig);
1727 type = __connman_ipconfig_get_config_type(ipconfig);
1729 g_hash_table_iter_init(&iter, session_hash);
1731 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1733 info = session->info;
1735 if (info->entry != NULL && info->entry->service == service) {
1736 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1737 ipconfig_ipv4_changed(session);
1738 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1739 ipconfig_ipv6_changed(session);
1744 static struct connman_notifier session_notifier = {
1746 .service_add = service_add,
1747 .service_remove = service_remove,
1748 .service_state_changed = service_state_changed,
1749 .ipconfig_changed = ipconfig_changed,
1752 int __connman_session_init(void)
1758 connection = connman_dbus_get_connection();
1759 if (connection == NULL)
1762 err = connman_notifier_register(&session_notifier);
1764 dbus_connection_unref(connection);
1768 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1769 NULL, cleanup_session);
1771 sessionmode = FALSE;
1775 void __connman_session_cleanup(void)
1779 if (connection == NULL)
1782 connman_notifier_unregister(&session_notifier);
1784 g_hash_table_foreach(session_hash, release_session, NULL);
1785 g_hash_table_destroy(session_hash);
1786 session_hash = NULL;
1788 dbus_connection_unref(connection);