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 (explicit_connect(info->entry->reason) == FALSE)
744 if (__connman_service_session_dec(info->entry->service) == FALSE)
747 if (ecall_session != NULL && ecall_session != session)
753 static void test_and_disconnect(struct connman_session *session)
755 struct session_info *info = session->info;
756 struct service_entry *entry;
757 connman_bool_t disconnect;
759 disconnect = explicit_disconnect(session);
763 info->online = FALSE;
764 info->reason = CONNMAN_SESSION_REASON_UNKNOWN;
767 if (disconnect == TRUE) {
768 DBG("disconnect service %p", entry->service);
771 * TODO: We should mark this entry as pending work. In case
772 * disconnect fails we just unassign this session from the
773 * service and can't do anything later on it
775 __connman_service_disconnect(entry->service);
779 static void select_and_connect(struct connman_session *session,
780 enum connman_session_reason reason)
782 struct session_info *info = session->info;
783 struct service_entry *entry = NULL;
785 connman_bool_t do_connect = FALSE;
787 DBG("session %p reason %s", session, reason2string(reason));
789 info->reason = reason;
791 iter = g_sequence_get_begin_iter(session->service_list);
793 while (g_sequence_iter_is_end(iter) == FALSE) {
794 entry = g_sequence_get(iter);
796 switch (entry->state) {
797 case CONNMAN_SERVICE_STATE_ASSOCIATION:
798 case CONNMAN_SERVICE_STATE_CONFIGURATION:
799 case CONNMAN_SERVICE_STATE_READY:
800 case CONNMAN_SERVICE_STATE_ONLINE:
801 /* connecting or connected */
803 case CONNMAN_SERVICE_STATE_IDLE:
804 case CONNMAN_SERVICE_STATE_DISCONNECT:
805 if (explicit_connect(reason) == TRUE)
808 case CONNMAN_SERVICE_STATE_UNKNOWN:
809 case CONNMAN_SERVICE_STATE_FAILURE:
817 iter = g_sequence_iter_next(iter);
820 if (info->entry != NULL && info->entry != entry)
821 test_and_disconnect(session);
825 info->entry->reason = reason;
827 if (explicit_connect(reason) == TRUE)
828 __connman_service_session_inc(info->entry->service);
830 if (do_connect == TRUE)
831 __connman_service_connect(info->entry->service);
833 info->online = is_online(entry->state);
837 static void session_changed(struct connman_session *session,
838 enum connman_session_trigger trigger)
840 struct session_info *info = session->info;
841 struct session_info *info_last = session->info_last;
845 * TODO: This only a placeholder for the 'real' algorithm to
846 * play a bit around. So we are going to improve it step by step.
849 DBG("session %p trigger %s", session, trigger2string(trigger));
852 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
853 DBG("ignore session changed event");
855 case CONNMAN_SESSION_TRIGGER_SETTING:
856 if (info->entry != NULL) {
857 iter = g_hash_table_lookup(session->service_hash,
858 info->entry->service);
861 * This service is not part of this
864 test_and_disconnect(session);
868 if (info->online == FALSE) {
869 select_and_connect(session,
870 CONNMAN_SESSION_REASON_FREE_RIDE);
874 case CONNMAN_SESSION_TRIGGER_CONNECT:
875 if (info->online == TRUE) {
876 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
877 __connman_service_session_inc(info->entry->service);
881 select_and_connect(session,
882 CONNMAN_SESSION_REASON_CONNECT);
885 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
886 test_and_disconnect(session);
889 case CONNMAN_SESSION_TRIGGER_PERIODIC:
890 if (info->online == TRUE) {
891 info->entry->reason = CONNMAN_SESSION_REASON_PERIODIC;
892 __connman_service_session_inc(info->entry->service);
896 select_and_connect(session,
897 CONNMAN_SESSION_REASON_PERIODIC);
900 case CONNMAN_SESSION_TRIGGER_SERVICE:
901 if (info->online == TRUE)
904 if (info->entry != NULL &&
905 is_connecting(info->entry->state) == TRUE) {
910 * We are not online, we are not connecting, that
911 * means we could still have a valid info->entry.
912 * Though something has changed from the service layer.
913 * Therefore we want to restart the algorithm. Before we
914 * can do that we have to cleanup a potientional old entry.
916 explicit_disconnect(session);
918 if (info->reason == CONNMAN_SESSION_REASON_CONNECT) {
919 DBG("Retry to find a matching session");
921 * The user called Connect() but there was no
922 * matching session available at this point.
923 * Now there might be a new one. Let's retry
924 * to select and connect
926 select_and_connect(session,
927 CONNMAN_SESSION_REASON_CONNECT);
932 if (info->stay_connected == TRUE) {
933 DBG("StayConnected");
934 select_and_connect(session,
935 CONNMAN_SESSION_REASON_CONNECT);
940 select_and_connect(session,
941 CONNMAN_SESSION_REASON_FREE_RIDE);
944 case CONNMAN_SESSION_TRIGGER_ECALL:
945 if (info->online == FALSE && info->entry->service != NULL)
946 test_and_disconnect(session);
951 if (info->entry != info_last->entry)
952 session->info_dirty = TRUE;
954 session_notify(session);
957 static DBusMessage *connect_session(DBusConnection *conn,
958 DBusMessage *msg, void *user_data)
960 struct connman_session *session = user_data;
962 DBG("session %p", session);
964 if (ecall_session != NULL && ecall_session != session)
965 return __connman_error_failed(msg, EBUSY);
967 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
969 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
972 static DBusMessage *disconnect_session(DBusConnection *conn,
973 DBusMessage *msg, void *user_data)
975 struct connman_session *session = user_data;
977 DBG("session %p", session);
979 if (ecall_session != NULL && ecall_session != session)
980 return __connman_error_failed(msg, EBUSY);
982 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
984 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
987 static struct service_entry *create_service_entry(struct connman_service *service,
989 enum connman_service_state state)
991 struct service_entry *entry;
992 enum connman_service_type type;
995 entry = g_try_new0(struct service_entry, 1);
999 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1000 entry->state = state;
1002 entry->service = service;
1004 idx = __connman_service_get_index(entry->service);
1005 entry->ifname = connman_inet_ifname(idx);
1006 if (entry->ifname == NULL)
1007 entry->ifname = g_strdup("");
1009 type = connman_service_get_type(entry->service);
1010 entry->bearer = service2bearer(type);
1015 static void destroy_service_entry(gpointer data)
1017 struct service_entry *entry = data;
1019 g_free(entry->ifname);
1024 static void update_allowed_bearers(struct connman_session *session)
1026 struct service_entry *entry;
1027 GSequenceIter *iter;
1029 if (session->service_list != NULL) {
1030 g_hash_table_remove_all(session->service_hash);
1031 g_sequence_free(session->service_list);
1034 session->service_list = __connman_service_get_list(session,
1036 create_service_entry,
1037 destroy_service_entry);
1039 g_sequence_sort(session->service_list, sort_services, session);
1041 iter = g_sequence_get_begin_iter(session->service_list);
1043 while (g_sequence_iter_is_end(iter) == FALSE) {
1044 entry = g_sequence_get(iter);
1046 DBG("service %p type %s name %s", entry->service,
1047 service2bearer(connman_service_get_type(entry->service)),
1050 g_hash_table_replace(session->service_hash,
1051 entry->service, iter);
1053 iter = g_sequence_iter_next(iter);
1056 session->info_dirty = TRUE;
1059 static void update_ecall_sessions(struct connman_session *session)
1061 struct session_info *info = session->info;
1062 struct connman_session *session_iter;
1063 GHashTableIter iter;
1064 gpointer key, value;
1066 g_hash_table_iter_init(&iter, session_hash);
1068 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1069 session_iter = value;
1071 if (session_iter == session)
1074 session_iter->info->ecall = info->ecall;
1075 session_iter->info_dirty = TRUE;
1077 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
1081 static void update_ecall(struct connman_session *session)
1083 struct session_info *info = session->info;
1084 struct session_info *info_last = session->info_last;
1086 DBG("session %p ecall_session %p ecall %d -> %d", session,
1087 ecall_session, info_last->ecall, info->ecall);
1089 if (ecall_session == NULL) {
1090 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
1093 ecall_session = session;
1094 } else if (ecall_session == session) {
1095 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
1098 ecall_session = NULL;
1103 update_ecall_sessions(session);
1105 session->info_dirty = TRUE;
1109 /* not a valid transition */
1110 info->ecall = info_last->ecall;
1113 static DBusMessage *change_session(DBusConnection *conn,
1114 DBusMessage *msg, void *user_data)
1116 struct connman_session *session = user_data;
1117 struct session_info *info = session->info;
1118 struct session_info *info_last = session->info_last;
1119 DBusMessageIter iter, value;
1121 GSList *allowed_bearers;
1123 DBG("session %p", session);
1124 if (dbus_message_iter_init(msg, &iter) == FALSE)
1125 return __connman_error_invalid_arguments(msg);
1127 dbus_message_iter_get_basic(&iter, &name);
1128 dbus_message_iter_next(&iter);
1129 dbus_message_iter_recurse(&iter, &value);
1131 switch (dbus_message_iter_get_arg_type(&value)) {
1132 case DBUS_TYPE_ARRAY:
1133 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1134 allowed_bearers = session_parse_allowed_bearers(&value);
1136 g_slist_foreach(info->allowed_bearers,
1137 cleanup_bearer_info, NULL);
1138 g_slist_free(info->allowed_bearers);
1140 if (allowed_bearers == NULL) {
1141 allowed_bearers = session_allowed_bearers_any();
1143 if (allowed_bearers == NULL)
1144 return __connman_error_failed(msg, ENOMEM);
1147 info->allowed_bearers = allowed_bearers;
1149 update_allowed_bearers(session);
1154 case DBUS_TYPE_BOOLEAN:
1155 if (g_str_equal(name, "Priority") == TRUE) {
1156 dbus_message_iter_get_basic(&value,
1159 if (info_last->priority != info->priority)
1160 session->info_dirty = TRUE;
1161 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1162 dbus_message_iter_get_basic(&value,
1163 &info->avoid_handover);
1165 if (info_last->avoid_handover != info->avoid_handover)
1166 session->info_dirty = TRUE;
1167 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1168 dbus_message_iter_get_basic(&value,
1169 &info->stay_connected);
1171 if (info_last->stay_connected != info->stay_connected)
1172 session->info_dirty = TRUE;
1173 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1174 dbus_message_iter_get_basic(&value,
1177 update_ecall(session);
1182 case DBUS_TYPE_UINT32:
1183 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1184 dbus_message_iter_get_basic(&value,
1185 &info->periodic_connect);
1187 if (info_last->periodic_connect != info->periodic_connect)
1188 session->info_dirty = TRUE;
1189 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1190 dbus_message_iter_get_basic(&value,
1191 &info->idle_timeout);
1193 if (info_last->idle_timeout != info->idle_timeout)
1194 session->info_dirty = TRUE;
1199 case DBUS_TYPE_STRING:
1200 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1202 dbus_message_iter_get_basic(&value, &val);
1203 info->roaming_policy =
1204 string2roamingpolicy(val);
1206 if (info_last->roaming_policy != info->roaming_policy)
1207 session->info_dirty = TRUE;
1216 if (session->info_dirty == TRUE)
1217 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1219 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1222 return __connman_error_invalid_arguments(msg);
1225 static void release_session(gpointer key, gpointer value, gpointer user_data)
1227 struct connman_session *session = value;
1228 DBusMessage *message;
1230 DBG("owner %s path %s", session->owner, session->notify_path);
1232 if (session->notify_watch > 0)
1233 g_dbus_remove_watch(connection, session->notify_watch);
1235 g_dbus_unregister_interface(connection, session->session_path,
1236 CONNMAN_SESSION_INTERFACE);
1238 message = dbus_message_new_method_call(session->owner,
1239 session->notify_path,
1240 CONNMAN_NOTIFICATION_INTERFACE,
1242 if (message == NULL)
1245 dbus_message_set_no_reply(message, TRUE);
1247 g_dbus_send_message(connection, message);
1250 static int session_disconnect(struct connman_session *session)
1252 DBG("session %p, %s", session, session->owner);
1254 if (session->notify_watch > 0)
1255 g_dbus_remove_watch(connection, session->notify_watch);
1257 g_dbus_unregister_interface(connection, session->session_path,
1258 CONNMAN_SESSION_INTERFACE);
1260 test_and_disconnect(session);
1262 g_hash_table_remove(session_hash, session->session_path);
1267 static void owner_disconnect(DBusConnection *conn, void *user_data)
1269 struct connman_session *session = user_data;
1271 DBG("session %p, %s died", session, session->owner);
1273 session_disconnect(session);
1276 static DBusMessage *destroy_session(DBusConnection *conn,
1277 DBusMessage *msg, void *user_data)
1279 struct connman_session *session = user_data;
1281 DBG("session %p", session);
1283 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1286 static GDBusMethodTable session_methods[] = {
1287 { "Destroy", "", "", destroy_session },
1288 { "Connect", "", "", connect_session },
1289 { "Disconnect", "", "", disconnect_session },
1290 { "Change", "sv", "", change_session },
1294 int __connman_session_create(DBusMessage *msg)
1296 const char *owner, *notify_path;
1297 char *session_path = NULL;
1298 DBusMessageIter iter, array;
1299 struct connman_session *session = NULL;
1300 struct session_info *info, *info_last;
1302 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1303 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1304 enum connman_session_roaming_policy roaming_policy =
1305 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1306 GSList *allowed_bearers = NULL;
1307 unsigned int periodic_connect = 0;
1308 unsigned int idle_timeout = 0;
1312 owner = dbus_message_get_sender(msg);
1314 DBG("owner %s", owner);
1316 if (ecall_session != NULL) {
1318 * If there is an emergency call already going on,
1319 * ignore session creation attempt
1325 dbus_message_iter_init(msg, &iter);
1326 dbus_message_iter_recurse(&iter, &array);
1328 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1329 DBusMessageIter entry, value;
1330 const char *key, *val;
1332 dbus_message_iter_recurse(&array, &entry);
1333 dbus_message_iter_get_basic(&entry, &key);
1335 dbus_message_iter_next(&entry);
1336 dbus_message_iter_recurse(&entry, &value);
1338 switch (dbus_message_iter_get_arg_type(&value)) {
1339 case DBUS_TYPE_ARRAY:
1340 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1342 session_parse_allowed_bearers(&value);
1347 case DBUS_TYPE_BOOLEAN:
1348 if (g_str_equal(key, "Priority") == TRUE) {
1349 dbus_message_iter_get_basic(&value,
1351 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1352 dbus_message_iter_get_basic(&value,
1354 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1355 dbus_message_iter_get_basic(&value,
1357 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1358 dbus_message_iter_get_basic(&value,
1364 case DBUS_TYPE_UINT32:
1365 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1366 dbus_message_iter_get_basic(&value,
1368 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1369 dbus_message_iter_get_basic(&value,
1375 case DBUS_TYPE_STRING:
1376 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1377 dbus_message_iter_get_basic(&value, &val);
1378 roaming_policy = string2roamingpolicy(val);
1383 dbus_message_iter_next(&array);
1386 dbus_message_iter_next(&iter);
1387 dbus_message_iter_get_basic(&iter, ¬ify_path);
1389 if (notify_path == NULL) {
1394 session_path = g_strdup_printf("/sessions%s", notify_path);
1395 if (session_path == NULL) {
1400 session = g_hash_table_lookup(session_hash, session_path);
1401 if (session != NULL) {
1407 session = g_try_new0(struct connman_session, 1);
1408 if (session == NULL) {
1413 session->info = g_try_new0(struct session_info, 1);
1414 if (session->info == NULL) {
1419 session->info_last = g_try_new0(struct session_info, 1);
1420 if (session->info_last == NULL) {
1425 info = session->info;
1426 info_last = session->info_last;
1428 session->owner = g_strdup(owner);
1429 session->session_path = session_path;
1430 session->notify_path = g_strdup(notify_path);
1431 session->notify_watch =
1432 g_dbus_add_disconnect_watch(connection, session->owner,
1433 owner_disconnect, session, NULL);
1435 session->service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1438 info->online = FALSE;
1439 info->priority = priority;
1440 info->avoid_handover = avoid_handover;
1441 info->stay_connected = stay_connected;
1442 info->periodic_connect = periodic_connect;
1443 info->idle_timeout = idle_timeout;
1444 info->ecall = ecall;
1445 info->roaming_policy = roaming_policy;
1449 if (allowed_bearers == NULL) {
1450 info->allowed_bearers =
1451 session_allowed_bearers_any();
1453 if (info->allowed_bearers == NULL) {
1458 info->allowed_bearers = allowed_bearers;
1461 g_hash_table_replace(session_hash, session->session_path, session);
1463 DBG("add %s", session->session_path);
1465 if (g_dbus_register_interface(connection, session->session_path,
1466 CONNMAN_SESSION_INTERFACE,
1467 session_methods, NULL,
1468 NULL, session, NULL) == FALSE) {
1469 connman_error("Failed to register %s", session->session_path);
1470 g_hash_table_remove(session_hash, session->session_path);
1477 g_dbus_send_reply(connection, msg,
1478 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1482 update_allowed_bearers(session);
1483 if (info->ecall == TRUE) {
1484 ecall_session = session;
1485 update_ecall_sessions(session);
1488 info_last->online = info->online;
1489 info_last->priority = info->priority;
1490 info_last->avoid_handover = info->avoid_handover;
1491 info_last->stay_connected = info->stay_connected;
1492 info_last->periodic_connect = info->periodic_connect;
1493 info_last->idle_timeout = info->idle_timeout;
1494 info_last->ecall = info->ecall;
1495 info_last->roaming_policy = info->roaming_policy;
1496 info_last->entry = info->entry;
1497 info_last->marker = info->marker;
1498 info_last->allowed_bearers = info->allowed_bearers;
1500 session->info_dirty = TRUE;
1501 session->append_all = TRUE;
1503 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1508 connman_error("Failed to create session");
1510 if (session != NULL) {
1511 if (session->info_last != NULL)
1512 g_free(session->info_last);
1513 if (session->info != NULL)
1514 g_free(session->info);
1518 g_free(session_path);
1520 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1521 g_slist_free(allowed_bearers);
1526 int __connman_session_destroy(DBusMessage *msg)
1528 const char *owner, *session_path;
1529 struct connman_session *session;
1531 owner = dbus_message_get_sender(msg);
1533 DBG("owner %s", owner);
1535 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1537 if (session_path == NULL)
1540 session = g_hash_table_lookup(session_hash, session_path);
1541 if (session == NULL)
1544 if (g_strcmp0(owner, session->owner) != 0)
1547 session_disconnect(session);
1552 connman_bool_t __connman_session_mode()
1557 void __connman_session_set_mode(connman_bool_t enable)
1559 DBG("enable %d", enable);
1561 if (sessionmode == enable)
1564 sessionmode = enable;
1566 if (sessionmode == TRUE)
1567 __connman_service_disconnect_all();
1570 static void service_add(struct connman_service *service,
1573 GHashTableIter iter;
1574 GSequenceIter *iter_service_list;
1575 gpointer key, value;
1576 struct connman_session *session;
1577 struct service_entry *entry;
1579 DBG("service %p", service);
1581 g_hash_table_iter_init(&iter, session_hash);
1583 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1586 if (service_match(session, service) == FALSE)
1589 entry = create_service_entry(service, name,
1590 CONNMAN_SERVICE_STATE_IDLE);
1595 g_sequence_insert_sorted(session->service_list,
1596 entry, sort_services,
1599 g_hash_table_replace(session->service_hash, service,
1602 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1606 static void service_remove(struct connman_service *service)
1609 GHashTableIter iter;
1610 gpointer key, value;
1611 struct connman_session *session;
1612 struct session_info *info;
1614 DBG("service %p", service);
1616 g_hash_table_iter_init(&iter, session_hash);
1618 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1619 GSequenceIter *iter;
1621 info = session->info;
1623 iter = g_hash_table_lookup(session->service_hash, service);
1627 g_sequence_remove(iter);
1630 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1634 static void service_state_changed(struct connman_service *service,
1635 enum connman_service_state state)
1637 GHashTableIter iter;
1638 gpointer key, value;
1639 struct connman_session *session;
1640 struct session_info *info, *info_last;
1642 DBG("service %p state %d", service, state);
1644 g_hash_table_iter_init(&iter, session_hash);
1646 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1648 info = session->info;
1649 info_last = session->info_last;
1651 if (info->entry != NULL && info->entry->service == service) {
1652 info->entry->state = state;
1653 info->online = is_online(info->entry->state);
1654 if (info_last->online != info->online)
1655 session->info_dirty = TRUE;
1658 session_changed(session,
1659 CONNMAN_SESSION_TRIGGER_SERVICE);
1663 static void ipconfig_changed(struct connman_service *service,
1664 struct connman_ipconfig *ipconfig)
1666 GHashTableIter iter;
1667 gpointer key, value;
1668 struct connman_session *session;
1669 struct session_info *info;
1670 enum connman_ipconfig_type type;
1672 DBG("service %p ipconfig %p", service, ipconfig);
1674 type = __connman_ipconfig_get_config_type(ipconfig);
1676 g_hash_table_iter_init(&iter, session_hash);
1678 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1680 info = session->info;
1682 if (info->entry != NULL && info->entry->service == service) {
1683 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1684 ipconfig_ipv4_changed(session);
1685 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1686 ipconfig_ipv6_changed(session);
1691 static struct connman_notifier session_notifier = {
1693 .service_add = service_add,
1694 .service_remove = service_remove,
1695 .service_state_changed = service_state_changed,
1696 .ipconfig_changed = ipconfig_changed,
1699 int __connman_session_init(void)
1705 connection = connman_dbus_get_connection();
1706 if (connection == NULL)
1709 err = connman_notifier_register(&session_notifier);
1711 dbus_connection_unref(connection);
1715 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1716 NULL, cleanup_session);
1718 sessionmode = FALSE;
1722 void __connman_session_cleanup(void)
1726 if (connection == NULL)
1729 connman_notifier_unregister(&session_notifier);
1731 g_hash_table_foreach(session_hash, release_session, NULL);
1732 g_hash_table_destroy(session_hash);
1733 session_hash = NULL;
1735 dbus_connection_unref(connection);