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
31 static DBusConnection *connection;
32 static GHashTable *session_hash;
33 static connman_bool_t sessionmode;
34 static struct connman_session *ecall_session;
36 enum connman_session_roaming_policy {
37 CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN = 0,
38 CONNMAN_SESSION_ROAMING_POLICY_DEFAULT = 1,
39 CONNMAN_SESSION_ROAMING_POLICY_ALWAYS = 2,
40 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN = 3,
41 CONNMAN_SESSION_ROAMING_POLICY_NATIONAL = 4,
42 CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL = 5,
49 connman_bool_t connect;
50 connman_bool_t online;
51 connman_bool_t priority;
52 GSList *allowed_bearers;
53 connman_bool_t avoid_handover;
54 connman_bool_t stay_connected;
55 unsigned int periodic_connect;
56 unsigned int idle_timeout;
58 enum connman_session_roaming_policy roaming_policy;
61 struct connman_service *service;
64 struct connman_session {
70 connman_bool_t append_all;
71 connman_bool_t info_dirty;
72 struct session_info info;
73 struct session_info info_last;
75 GSequence *service_list;
80 connman_bool_t match_all;
81 enum connman_service_type service_type;
84 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
87 case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
89 case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
91 case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
93 case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
95 case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
97 case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
98 return "international";
104 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
106 if (g_strcmp0(policy, "default") == 0)
107 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
108 else if (g_strcmp0(policy, "always") == 0)
109 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
110 else if (g_strcmp0(policy, "forbidden") == 0)
111 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
112 else if (g_strcmp0(policy, "national") == 0)
113 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
114 else if (g_strcmp0(policy, "international") == 0)
115 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
117 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
120 static enum connman_service_type bearer2service(const char *bearer)
123 return CONNMAN_SERVICE_TYPE_UNKNOWN;
125 if (g_strcmp0(bearer, "ethernet") == 0)
126 return CONNMAN_SERVICE_TYPE_ETHERNET;
127 else if (g_strcmp0(bearer, "wifi") == 0)
128 return CONNMAN_SERVICE_TYPE_WIFI;
129 else if (g_strcmp0(bearer, "wimax") == 0)
130 return CONNMAN_SERVICE_TYPE_WIMAX;
131 else if (g_strcmp0(bearer, "bluetooth") == 0)
132 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
133 else if (g_strcmp0(bearer, "3g") == 0)
134 return CONNMAN_SERVICE_TYPE_CELLULAR;
136 return CONNMAN_SERVICE_TYPE_UNKNOWN;
139 static char *service2bearer(enum connman_service_type type)
142 case CONNMAN_SERVICE_TYPE_ETHERNET:
144 case CONNMAN_SERVICE_TYPE_WIFI:
146 case CONNMAN_SERVICE_TYPE_WIMAX:
148 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
150 case CONNMAN_SERVICE_TYPE_CELLULAR:
152 case CONNMAN_SERVICE_TYPE_UNKNOWN:
153 case CONNMAN_SERVICE_TYPE_SYSTEM:
154 case CONNMAN_SERVICE_TYPE_GPS:
155 case CONNMAN_SERVICE_TYPE_VPN:
156 case CONNMAN_SERVICE_TYPE_GADGET:
163 static char *session2bearer(struct connman_session *session)
165 struct session_info *info = &session->info;
167 struct bearer_info *bearer_info;
168 enum connman_service_type type;
170 if (info->service == NULL)
173 type = connman_service_get_type(info->service);
175 for (list = info->allowed_bearers;
176 list != NULL; list = list->next) {
177 bearer_info = list->data;
179 if (bearer_info->match_all)
180 return service2bearer(type);
182 if (bearer_info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN)
183 return bearer_info->name;
185 if (bearer_info->service_type == type)
186 return service2bearer(type);
193 static void cleanup_bearer_info(gpointer data, gpointer user_data)
195 struct bearer_info *info = data;
201 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
203 struct bearer_info *info;
204 DBusMessageIter array;
207 dbus_message_iter_recurse(iter, &array);
209 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
212 dbus_message_iter_get_basic(&array, &bearer);
214 info = g_try_new0(struct bearer_info, 1);
216 g_slist_foreach(list, cleanup_bearer_info, NULL);
222 info->name = g_strdup(bearer);
223 info->service_type = bearer2service(info->name);
225 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
226 g_strcmp0(info->name, "*") == 0) {
227 info->match_all = TRUE;
229 info->match_all = FALSE;
232 list = g_slist_append(list, info);
234 dbus_message_iter_next(&array);
240 static GSList *session_allowed_bearers_any(void)
242 struct bearer_info *info;
245 info = g_try_new0(struct bearer_info, 1);
252 info->name = g_strdup("");
253 info->match_all = TRUE;
254 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
256 list = g_slist_append(list, info);
261 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
263 struct session_info *info = user_data;
266 for (list = info->allowed_bearers;
267 list != NULL; list = list->next) {
268 struct bearer_info *info = list->data;
270 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
275 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
277 struct connman_service *service = user_data;
278 struct connman_ipconfig *ipconfig_ipv4;
283 ipconfig_ipv4 = __connman_service_get_ip4config(service);
284 if (ipconfig_ipv4 == NULL)
287 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
290 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
292 struct connman_service *service = user_data;
293 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
298 ipconfig_ipv4 = __connman_service_get_ip4config(service);
299 ipconfig_ipv6 = __connman_service_get_ip6config(service);
300 if (ipconfig_ipv6 == NULL)
303 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
306 static void append_notify(DBusMessageIter *dict,
307 struct connman_session *session)
309 struct session_info *info = &session->info;
310 struct session_info *info_last = &session->info_last;
313 if (session->append_all == TRUE ||
314 info->bearer != info_last->bearer) {
315 connman_dbus_dict_append_basic(dict, "Bearer",
318 info_last->bearer = info->bearer;
321 if (session->append_all == TRUE ||
322 info->online != info_last->online) {
323 connman_dbus_dict_append_basic(dict, "Online",
326 info_last->online = info->online;
329 if (session->append_all == TRUE ||
330 info->name != info_last->name) {
331 connman_dbus_dict_append_basic(dict, "Name",
334 info_last->name = info->name;
337 if (session->append_all == TRUE ||
338 info->service != info_last->service) {
339 connman_dbus_dict_append_dict(dict, "IPv4",
340 append_ipconfig_ipv4,
343 connman_dbus_dict_append_dict(dict, "IPv6",
344 append_ipconfig_ipv6,
347 connman_dbus_dict_append_basic(dict, "Interface",
351 info_last->service = info->service;
355 if (session->append_all == TRUE ||
356 info->priority != info_last->priority) {
357 connman_dbus_dict_append_basic(dict, "Priority",
360 info_last->priority = info->priority;
363 if (session->append_all == TRUE ||
364 info->allowed_bearers != info_last->allowed_bearers) {
365 connman_dbus_dict_append_array(dict, "AllowedBearers",
367 append_allowed_bearers,
369 info_last->allowed_bearers = info->allowed_bearers;
372 if (session->append_all == TRUE ||
373 info->avoid_handover != info_last->avoid_handover) {
374 connman_dbus_dict_append_basic(dict, "AvoidHandover",
376 &info->avoid_handover);
377 info_last->avoid_handover = info->avoid_handover;
380 if (session->append_all == TRUE ||
381 info->stay_connected != info_last->stay_connected) {
382 connman_dbus_dict_append_basic(dict, "StayConnected",
384 &info->stay_connected);
385 info_last->stay_connected = info->stay_connected;
388 if (session->append_all == TRUE ||
389 info->periodic_connect != info_last->periodic_connect) {
390 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
392 &info->periodic_connect);
393 info_last->periodic_connect = info->periodic_connect;
396 if (session->append_all == TRUE ||
397 info->idle_timeout != info_last->idle_timeout) {
398 connman_dbus_dict_append_basic(dict, "IdleTimeout",
400 &info->idle_timeout);
401 info_last->idle_timeout = info->idle_timeout;
404 if (session->append_all == TRUE ||
405 info->ecall != info_last->ecall) {
406 connman_dbus_dict_append_basic(dict, "EmergencyCall",
409 info_last->ecall = info->ecall;
412 if (session->append_all == TRUE ||
413 info->roaming_policy != info_last->roaming_policy) {
414 policy = roamingpolicy2string(info->roaming_policy);
415 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
418 info_last->roaming_policy = info->roaming_policy;
421 if (session->append_all == TRUE ||
422 info->marker != info_last->marker) {
423 connman_dbus_dict_append_basic(dict, "SessionMarker",
426 info_last->marker = info->marker;
429 session->append_all = FALSE;
430 session->info_dirty = FALSE;
433 static int session_notify(struct connman_session *session)
436 DBusMessageIter array, dict;
438 if (session->info_dirty == FALSE)
441 DBG("session %p owner %s notify_path %s", session,
442 session->owner, session->notify_path);
444 msg = dbus_message_new_method_call(session->owner, session->notify_path,
445 CONNMAN_NOTIFICATION_INTERFACE,
450 dbus_message_iter_init_append(msg, &array);
451 connman_dbus_dict_open(&array, &dict);
453 append_notify(&dict, session);
455 connman_dbus_dict_close(&array, &dict);
457 g_dbus_send_message(connection, msg);
459 session->info_dirty = FALSE;
464 static void ipconfig_ipv4_changed(struct connman_session *session)
466 struct session_info *info = &session->info;
468 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
469 "IPv4", append_ipconfig_ipv4,
473 static void ipconfig_ipv6_changed(struct connman_session *session)
475 struct session_info *info = &session->info;
477 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
478 "IPv6", append_ipconfig_ipv6,
482 static connman_bool_t service_type_match(struct connman_session *session,
483 struct connman_service *service)
485 struct session_info *info = &session->info;
488 for (list = info->allowed_bearers;
489 list != NULL; list = list->next) {
490 struct bearer_info *info = list->data;
491 enum connman_service_type service_type;
493 if (info->match_all == TRUE)
496 service_type = connman_service_get_type(service);
497 if (info->service_type == service_type)
504 static connman_bool_t service_match(struct connman_session *session,
505 struct connman_service *service)
507 if (service_type_match(session, service) == FALSE)
513 static int service_type_weight(enum connman_service_type type)
516 * The session doesn't care which service
517 * to use. Nevertheless we have to sort them
518 * according their type. The ordering is
527 case CONNMAN_SERVICE_TYPE_ETHERNET:
529 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
531 case CONNMAN_SERVICE_TYPE_WIFI:
532 case CONNMAN_SERVICE_TYPE_WIMAX:
534 case CONNMAN_SERVICE_TYPE_CELLULAR:
536 case CONNMAN_SERVICE_TYPE_UNKNOWN:
537 case CONNMAN_SERVICE_TYPE_SYSTEM:
538 case CONNMAN_SERVICE_TYPE_GPS:
539 case CONNMAN_SERVICE_TYPE_VPN:
540 case CONNMAN_SERVICE_TYPE_GADGET:
547 static gint sort_allowed_bearers(struct connman_service *service_a,
548 struct connman_service *service_b,
549 struct connman_session *session)
551 struct session_info *info = &session->info;
553 enum connman_service_type type_a, type_b;
554 int weight_a, weight_b;
556 type_a = connman_service_get_type(service_a);
557 type_b = connman_service_get_type(service_b);
559 for (list = info->allowed_bearers;
560 list != NULL; list = list->next) {
561 struct bearer_info *info = list->data;
563 if (info->match_all == TRUE) {
564 if (type_a != type_b) {
565 weight_a = service_type_weight(type_a);
566 weight_b = service_type_weight(type_b);
568 if (weight_a > weight_b)
571 if (weight_a < weight_b)
578 if (type_a == info->service_type &&
579 type_b == info->service_type) {
583 if (type_a == info->service_type &&
584 type_b != info->service_type) {
588 if (type_a != info->service_type &&
589 type_b == info->service_type) {
597 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
599 struct connman_service *service_a = (void *)a;
600 struct connman_service *service_b = (void *)b;
601 struct connman_session *session = user_data;
603 return sort_allowed_bearers(service_a, service_b, session);
606 static void cleanup_session(gpointer user_data)
608 struct connman_session *session = user_data;
609 struct session_info *info = &session->info;
611 DBG("remove %s", session->session_path);
613 g_sequence_free(session->service_list);
615 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
616 g_slist_free(info->allowed_bearers);
618 g_free(session->owner);
619 g_free(session->session_path);
620 g_free(session->notify_path);
625 static void release_session(gpointer key, gpointer value, gpointer user_data)
627 struct connman_session *session = value;
628 DBusMessage *message;
630 DBG("owner %s path %s", session->owner, session->notify_path);
632 if (session->notify_watch > 0)
633 g_dbus_remove_watch(connection, session->notify_watch);
635 g_dbus_unregister_interface(connection, session->session_path,
636 CONNMAN_SESSION_INTERFACE);
638 message = dbus_message_new_method_call(session->owner,
639 session->notify_path,
640 CONNMAN_NOTIFICATION_INTERFACE,
645 dbus_message_set_no_reply(message, TRUE);
647 g_dbus_send_message(connection, message);
650 static int session_disconnect(struct connman_session *session)
652 DBG("session %p, %s", session, session->owner);
654 if (session->notify_watch > 0)
655 g_dbus_remove_watch(connection, session->notify_watch);
657 g_dbus_unregister_interface(connection, session->session_path,
658 CONNMAN_SESSION_INTERFACE);
660 g_hash_table_remove(session_hash, session->session_path);
665 static void owner_disconnect(DBusConnection *conn, void *user_data)
667 struct connman_session *session = user_data;
669 DBG("session %p, %s died", session, session->owner);
671 session_disconnect(session);
674 static DBusMessage *destroy_session(DBusConnection *conn,
675 DBusMessage *msg, void *user_data)
677 struct connman_session *session = user_data;
679 DBG("session %p", session);
681 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
684 static gboolean session_changed_connect(gpointer user_data)
686 struct connman_session *session = user_data;
687 struct session_info *info = &session->info;
689 __connman_service_connect(info->service);
694 static void update_service(struct connman_session *session)
696 struct session_info *info = &session->info;
699 if (info->service != NULL) {
700 info->bearer = session2bearer(session);
701 info->online = __connman_service_is_connected(info->service);
702 info->name = __connman_service_get_name(info->service);
703 idx = __connman_service_get_index(info->service);
704 info->ifname = connman_inet_ifname(idx);
708 info->online = FALSE;
714 static void session_changed(struct connman_session *session)
716 struct session_info *info = &session->info;
717 struct session_info *info_last = &session->info_last;
718 struct connman_service *service = NULL;
719 GSourceFunc callback = NULL;
723 * TODO: This only a placeholder for the 'real' algorithm to
724 * play a bit around. So we are going to improve it step by step.
727 if (info->ecall == TRUE && session != ecall_session)
730 if (info->connect == FALSE) {
731 if (info->service != NULL)
732 __connman_service_disconnect(info->service);
733 info->service = NULL;
737 iter = g_sequence_get_begin_iter(session->service_list);
739 while (g_sequence_iter_is_end(iter) == FALSE) {
740 service = g_sequence_get(iter);
742 if (__connman_service_is_connecting(service) == TRUE ||
743 __connman_service_is_connected(service) == TRUE) {
747 if (__connman_service_is_idle(service) == TRUE &&
748 info->connect == TRUE) {
749 callback = session_changed_connect;
755 iter = g_sequence_iter_next(iter);
758 if (info->service != NULL && info->service != service) {
759 __connman_service_disconnect(info->service);
760 info->service = NULL;
763 if (service != NULL) {
764 info->service = service;
766 if (callback != NULL)
771 if (info->service != info_last->service)
772 update_service(session);
775 static gboolean session_cb(gpointer user_data)
777 struct connman_session *session = user_data;
779 session_changed(session);
780 session_notify(session);
785 static DBusMessage *connect_session(DBusConnection *conn,
786 DBusMessage *msg, void *user_data)
788 struct connman_session *session = user_data;
789 struct session_info *info = &session->info;
791 DBG("session %p", session);
793 info->connect = TRUE;
795 if (ecall_session != NULL && ecall_session != session)
796 return __connman_error_failed(msg, EBUSY);
798 session->info_dirty = TRUE;
800 g_timeout_add_seconds(0, session_cb, session);
802 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
805 static DBusMessage *disconnect_session(DBusConnection *conn,
806 DBusMessage *msg, void *user_data)
808 struct connman_session *session = user_data;
809 struct session_info *info = &session->info;
811 DBG("session %p", session);
813 info->connect = FALSE;
815 if (ecall_session != NULL && ecall_session != session)
816 return __connman_error_failed(msg, EBUSY);
818 session->info_dirty = TRUE;
820 g_timeout_add_seconds(0, session_cb, session);
822 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
825 static void print_name(gpointer data, gpointer user_data)
827 struct connman_service *service = data;
829 DBG("service %p type %s name %s", service,
830 service2bearer(connman_service_get_type(service)),
831 __connman_service_get_name(service));
834 static void update_allowed_bearers(struct connman_session *session)
836 if (session->service_list != NULL)
837 g_sequence_free(session->service_list);
839 session->service_list = __connman_service_get_list(session,
841 g_sequence_sort(session->service_list, sort_services, session);
842 g_sequence_foreach(session->service_list, print_name, NULL);
844 session->info_dirty = TRUE;
847 static void update_ecall_sessions(struct connman_session *session)
849 struct session_info *info = &session->info;
850 struct connman_session *session_iter;
854 g_hash_table_iter_init(&iter, session_hash);
856 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
857 session_iter = value;
859 if (session_iter == session)
862 session_iter->info.ecall = info->ecall;
863 session_iter->info_dirty = TRUE;
865 g_timeout_add_seconds(0, session_cb, session_iter);
869 static void update_ecall(struct connman_session *session)
871 struct session_info *info = &session->info;
872 struct session_info *info_last = &session->info_last;
874 DBG("ecall_session %p ecall %d -> %d", ecall_session,
875 info_last->ecall, info->ecall);
877 if (ecall_session == NULL) {
878 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
881 ecall_session = session;
882 } else if (ecall_session == session) {
883 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
886 ecall_session = NULL;
891 update_ecall_sessions(session);
893 session->info_dirty = TRUE;
897 /* not a valid transition */
898 info->ecall = info_last->ecall;
901 static DBusMessage *change_session(DBusConnection *conn,
902 DBusMessage *msg, void *user_data)
904 struct connman_session *session = user_data;
905 struct session_info *info = &session->info;
906 struct session_info *info_last = &session->info_last;
907 DBusMessageIter iter, value;
909 GSList *allowed_bearers;
911 DBG("session %p", session);
912 if (dbus_message_iter_init(msg, &iter) == FALSE)
913 return __connman_error_invalid_arguments(msg);
915 dbus_message_iter_get_basic(&iter, &name);
916 dbus_message_iter_next(&iter);
917 dbus_message_iter_recurse(&iter, &value);
919 switch (dbus_message_iter_get_arg_type(&value)) {
920 case DBUS_TYPE_ARRAY:
921 if (g_str_equal(name, "AllowedBearers") == TRUE) {
922 allowed_bearers = session_parse_allowed_bearers(&value);
924 g_slist_foreach(info->allowed_bearers,
925 cleanup_bearer_info, NULL);
926 g_slist_free(info->allowed_bearers);
928 if (allowed_bearers == NULL) {
929 allowed_bearers = session_allowed_bearers_any();
931 if (allowed_bearers == NULL)
932 return __connman_error_failed(msg, ENOMEM);
935 info->allowed_bearers = allowed_bearers;
937 update_allowed_bearers(session);
942 case DBUS_TYPE_BOOLEAN:
943 if (g_str_equal(name, "Priority") == TRUE) {
944 dbus_message_iter_get_basic(&value,
947 if (info_last->priority != info->priority)
948 session->info_dirty = TRUE;
949 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
950 dbus_message_iter_get_basic(&value,
951 &info->avoid_handover);
953 if (info_last->avoid_handover != info->avoid_handover)
954 session->info_dirty = TRUE;
955 } else if (g_str_equal(name, "StayConnected") == TRUE) {
956 dbus_message_iter_get_basic(&value,
957 &info->stay_connected);
959 if (info_last->stay_connected != info->stay_connected)
960 session->info_dirty = TRUE;
961 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
962 dbus_message_iter_get_basic(&value,
965 update_ecall(session);
970 case DBUS_TYPE_UINT32:
971 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
972 dbus_message_iter_get_basic(&value,
973 &info->periodic_connect);
975 if (info_last->periodic_connect != info->periodic_connect)
976 session->info_dirty = TRUE;
977 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
978 dbus_message_iter_get_basic(&value,
979 &info->idle_timeout);
981 if (info_last->idle_timeout != info->idle_timeout)
982 session->info_dirty = TRUE;
987 case DBUS_TYPE_STRING:
988 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
990 dbus_message_iter_get_basic(&value, &val);
991 info->roaming_policy =
992 string2roamingpolicy(val);
994 if (info_last->roaming_policy != info->roaming_policy)
995 session->info_dirty = TRUE;
1004 if (session->info_dirty == TRUE)
1005 session_cb(session);
1007 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1010 return __connman_error_invalid_arguments(msg);
1013 static GDBusMethodTable session_methods[] = {
1014 { "Destroy", "", "", destroy_session },
1015 { "Connect", "", "", connect_session },
1016 { "Disconnect", "", "", disconnect_session },
1017 { "Change", "sv", "", change_session },
1021 int __connman_session_create(DBusMessage *msg)
1023 const char *owner, *notify_path;
1024 char *session_path = NULL;
1025 DBusMessageIter iter, array;
1026 struct connman_session *session;
1027 struct session_info *info, *info_last;
1029 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1030 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1031 enum connman_session_roaming_policy roaming_policy =
1032 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1033 GSList *allowed_bearers = NULL;
1034 unsigned int periodic_connect = 0;
1035 unsigned int idle_timeout = 0;
1039 owner = dbus_message_get_sender(msg);
1041 DBG("owner %s", owner);
1043 if (ecall_session != NULL) {
1045 * If there is an emergency call already going on,
1046 * ignore session creation attempt
1052 dbus_message_iter_init(msg, &iter);
1053 dbus_message_iter_recurse(&iter, &array);
1055 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1056 DBusMessageIter entry, value;
1057 const char *key, *val;
1059 dbus_message_iter_recurse(&array, &entry);
1060 dbus_message_iter_get_basic(&entry, &key);
1062 dbus_message_iter_next(&entry);
1063 dbus_message_iter_recurse(&entry, &value);
1065 switch (dbus_message_iter_get_arg_type(&value)) {
1066 case DBUS_TYPE_ARRAY:
1067 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1069 session_parse_allowed_bearers(&value);
1074 case DBUS_TYPE_BOOLEAN:
1075 if (g_str_equal(key, "Priority") == TRUE) {
1076 dbus_message_iter_get_basic(&value,
1078 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1079 dbus_message_iter_get_basic(&value,
1081 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1082 dbus_message_iter_get_basic(&value,
1084 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1085 dbus_message_iter_get_basic(&value,
1091 case DBUS_TYPE_UINT32:
1092 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1093 dbus_message_iter_get_basic(&value,
1095 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1096 dbus_message_iter_get_basic(&value,
1102 case DBUS_TYPE_STRING:
1103 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1104 dbus_message_iter_get_basic(&value, &val);
1105 roaming_policy = string2roamingpolicy(val);
1110 dbus_message_iter_next(&array);
1113 dbus_message_iter_next(&iter);
1114 dbus_message_iter_get_basic(&iter, ¬ify_path);
1116 if (notify_path == NULL) {
1121 session_path = g_strdup_printf("/sessions%s", notify_path);
1122 if (session_path == NULL) {
1127 session = g_hash_table_lookup(session_hash, session_path);
1128 if (session != NULL) {
1133 session = g_try_new0(struct connman_session, 1);
1134 if (session == NULL) {
1139 info = &session->info;
1140 info_last = &session->info_last;
1142 session->owner = g_strdup(owner);
1143 session->session_path = session_path;
1144 session->notify_path = g_strdup(notify_path);
1145 session->notify_watch =
1146 g_dbus_add_disconnect_watch(connection, session->owner,
1147 owner_disconnect, session, NULL);
1150 info->online = FALSE;
1151 info->priority = priority;
1152 info->avoid_handover = avoid_handover;
1153 info->stay_connected = stay_connected;
1154 info->periodic_connect = periodic_connect;
1155 info->idle_timeout = idle_timeout;
1156 info->ecall = ecall;
1157 info->roaming_policy = roaming_policy;
1158 info->service = NULL;
1161 if (allowed_bearers == NULL) {
1162 info->allowed_bearers =
1163 session_allowed_bearers_any();
1165 if (info->allowed_bearers == NULL) {
1170 info->allowed_bearers = allowed_bearers;
1173 g_hash_table_replace(session_hash, session->session_path, session);
1175 DBG("add %s", session->session_path);
1177 if (g_dbus_register_interface(connection, session->session_path,
1178 CONNMAN_SESSION_INTERFACE,
1179 session_methods, NULL,
1180 NULL, session, NULL) == FALSE) {
1181 connman_error("Failed to register %s", session->session_path);
1182 g_hash_table_remove(session_hash, session->session_path);
1189 g_dbus_send_reply(connection, msg,
1190 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1194 update_allowed_bearers(session);
1195 update_service(session);
1196 if (info->ecall == TRUE) {
1197 ecall_session = session;
1198 update_ecall_sessions(session);
1201 info_last->bearer = info->bearer;
1202 info_last->online = info->priority;
1203 info_last->avoid_handover = info->avoid_handover;
1204 info_last->stay_connected = info->stay_connected;
1205 info_last->periodic_connect = info->periodic_connect;
1206 info_last->idle_timeout = info->idle_timeout;
1207 info_last->ecall = info->ecall;
1208 info_last->roaming_policy = info->roaming_policy;
1209 info_last->service = info->service;
1210 info_last->marker = info->marker;
1211 info_last->allowed_bearers = info->allowed_bearers;
1213 session->info_dirty = TRUE;
1214 session->append_all = TRUE;
1216 g_timeout_add_seconds(0, session_cb, session);
1221 connman_error("Failed to create session");
1222 g_free(session_path);
1224 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1225 g_slist_free(allowed_bearers);
1230 int __connman_session_destroy(DBusMessage *msg)
1232 const char *owner, *session_path;
1233 struct connman_session *session;
1235 owner = dbus_message_get_sender(msg);
1237 DBG("owner %s", owner);
1239 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1241 if (session_path == NULL)
1244 session = g_hash_table_lookup(session_hash, session_path);
1245 if (session == NULL)
1248 if (g_strcmp0(owner, session->owner) != 0)
1251 session_disconnect(session);
1256 connman_bool_t __connman_session_mode()
1261 void __connman_session_set_mode(connman_bool_t enable)
1263 DBG("enable %d", enable);
1265 if (sessionmode == enable)
1268 sessionmode = enable;
1270 if (sessionmode == TRUE)
1271 __connman_service_disconnect_all();
1274 static void service_add(struct connman_service *service)
1276 GHashTableIter iter;
1277 gpointer key, value;
1278 struct connman_session *session;
1280 DBG("service %p", service);
1282 g_hash_table_iter_init(&iter, session_hash);
1284 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1287 if (service_match(session, service) == FALSE)
1290 g_sequence_insert_sorted(session->service_list, service,
1291 sort_services, session);
1293 session_cb(session);
1297 static gint service_in_session(gconstpointer a, gconstpointer b,
1306 static void service_remove(struct connman_service *service)
1309 GHashTableIter iter;
1310 gpointer key, value;
1311 GSequenceIter *seq_iter;
1312 struct connman_session *session;
1313 struct session_info *info;
1314 struct connman_service *found_service;
1316 DBG("service %p", service);
1318 g_hash_table_iter_init(&iter, session_hash);
1320 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1322 info = &session->info;
1324 if (session->service_list == NULL)
1327 seq_iter = g_sequence_search(session->service_list, service,
1328 service_in_session, NULL);
1329 if (g_sequence_iter_is_end(seq_iter) == TRUE)
1332 g_sequence_remove(seq_iter);
1334 found_service = g_sequence_get(seq_iter);
1335 if (found_service != info->service)
1338 info->service = NULL;
1339 session_cb(session);
1343 static void service_state_changed(struct connman_service *service,
1344 enum connman_service_state state)
1346 GHashTableIter iter;
1347 gpointer key, value;
1348 struct connman_session *session;
1349 struct session_info *info;
1350 connman_bool_t online;
1352 DBG("service %p state %d", service, state);
1354 g_hash_table_iter_init(&iter, session_hash);
1356 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1358 info = &session->info;
1360 if (info->service == service) {
1361 online = __connman_service_is_connected(service);
1362 if (info->online == online)
1365 info->online = online;
1366 session_cb(session);
1371 static void ipconfig_changed(struct connman_service *service,
1372 struct connman_ipconfig *ipconfig)
1374 GHashTableIter iter;
1375 gpointer key, value;
1376 struct connman_session *session;
1377 struct session_info *info;
1378 enum connman_ipconfig_type type;
1380 DBG("service %p ipconfig %p", service, ipconfig);
1382 type = __connman_ipconfig_get_config_type(ipconfig);
1384 g_hash_table_iter_init(&iter, session_hash);
1386 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1388 info = &session->info;
1390 if (info->service == service) {
1391 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1392 ipconfig_ipv4_changed(session);
1393 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1394 ipconfig_ipv6_changed(session);
1399 static struct connman_notifier session_notifier = {
1401 .service_add = service_add,
1402 .service_remove = service_remove,
1403 .service_state_changed = service_state_changed,
1404 .ipconfig_changed = ipconfig_changed,
1407 int __connman_session_init(void)
1413 connection = connman_dbus_get_connection();
1414 if (connection == NULL)
1417 err = connman_notifier_register(&session_notifier);
1419 dbus_connection_unref(connection);
1423 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1424 NULL, cleanup_session);
1426 sessionmode = FALSE;
1430 void __connman_session_cleanup(void)
1434 if (connection == NULL)
1437 connman_notifier_unregister(&session_notifier);
1439 g_hash_table_foreach(session_hash, release_session, NULL);
1440 g_hash_table_destroy(session_hash);
1442 dbus_connection_unref(connection);