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->connect == FALSE) {
728 if (info->service != NULL)
729 __connman_service_disconnect(info->service);
730 info->service = NULL;
734 iter = g_sequence_get_begin_iter(session->service_list);
736 while (g_sequence_iter_is_end(iter) == FALSE) {
737 service = g_sequence_get(iter);
739 if (__connman_service_is_connecting(service) == TRUE ||
740 __connman_service_is_connected(service) == TRUE) {
744 if (__connman_service_is_idle(service) == TRUE &&
745 info->connect == TRUE) {
746 callback = session_changed_connect;
752 iter = g_sequence_iter_next(iter);
755 if (info->service != NULL && info->service != service) {
756 __connman_service_disconnect(info->service);
757 info->service = NULL;
760 if (service != NULL) {
761 info->service = service;
763 if (callback != NULL)
768 if (info->service != info_last->service)
769 update_service(session);
772 static gboolean session_cb(gpointer user_data)
774 struct connman_session *session = user_data;
776 session_changed(session);
777 session_notify(session);
782 static DBusMessage *connect_session(DBusConnection *conn,
783 DBusMessage *msg, void *user_data)
785 struct connman_session *session = user_data;
786 struct session_info *info = &session->info;
788 DBG("session %p", session);
790 info->connect = TRUE;
791 session->info_dirty = TRUE;
793 g_timeout_add_seconds(0, session_cb, session);
795 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
798 static DBusMessage *disconnect_session(DBusConnection *conn,
799 DBusMessage *msg, void *user_data)
801 struct connman_session *session = user_data;
802 struct session_info *info = &session->info;
804 DBG("session %p", session);
806 info->connect = FALSE;
807 session->info_dirty = TRUE;
809 g_timeout_add_seconds(0, session_cb, session);
811 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
814 static void print_name(gpointer data, gpointer user_data)
816 struct connman_service *service = data;
818 DBG("service %p type %s name %s", service,
819 service2bearer(connman_service_get_type(service)),
820 __connman_service_get_name(service));
823 static void update_allowed_bearers(struct connman_session *session)
825 if (session->service_list != NULL)
826 g_sequence_free(session->service_list);
828 session->service_list = __connman_service_get_list(session,
830 g_sequence_sort(session->service_list, sort_services, session);
831 g_sequence_foreach(session->service_list, print_name, NULL);
833 session->info_dirty = TRUE;
836 static void update_ecall_sessions(struct connman_session *session)
838 struct session_info *info = &session->info;
839 struct connman_session *session_iter;
843 g_hash_table_iter_init(&iter, session_hash);
845 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
846 session_iter = value;
848 if (session_iter == session)
851 session_iter->info.ecall = info->ecall;
852 session_iter->info_dirty = TRUE;
854 g_timeout_add_seconds(0, session_cb, session_iter);
858 static void update_ecall(struct connman_session *session)
860 struct session_info *info = &session->info;
861 struct session_info *info_last = &session->info_last;
863 DBG("ecall_session %p ecall %d -> %d", ecall_session,
864 info_last->ecall, info->ecall);
866 if (ecall_session == NULL) {
867 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
870 ecall_session = session;
871 } else if (ecall_session == session) {
872 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
875 ecall_session = NULL;
880 update_ecall_sessions(session);
882 session->info_dirty = TRUE;
886 /* not a valid transition */
887 info->ecall = info_last->ecall;
890 static DBusMessage *change_session(DBusConnection *conn,
891 DBusMessage *msg, void *user_data)
893 struct connman_session *session = user_data;
894 struct session_info *info = &session->info;
895 struct session_info *info_last = &session->info_last;
896 DBusMessageIter iter, value;
898 GSList *allowed_bearers;
900 DBG("session %p", session);
901 if (dbus_message_iter_init(msg, &iter) == FALSE)
902 return __connman_error_invalid_arguments(msg);
904 dbus_message_iter_get_basic(&iter, &name);
905 dbus_message_iter_next(&iter);
906 dbus_message_iter_recurse(&iter, &value);
908 switch (dbus_message_iter_get_arg_type(&value)) {
909 case DBUS_TYPE_ARRAY:
910 if (g_str_equal(name, "AllowedBearers") == TRUE) {
911 allowed_bearers = session_parse_allowed_bearers(&value);
913 g_slist_foreach(info->allowed_bearers,
914 cleanup_bearer_info, NULL);
915 g_slist_free(info->allowed_bearers);
917 if (allowed_bearers == NULL) {
918 allowed_bearers = session_allowed_bearers_any();
920 if (allowed_bearers == NULL)
921 return __connman_error_failed(msg, ENOMEM);
924 info->allowed_bearers = allowed_bearers;
926 update_allowed_bearers(session);
931 case DBUS_TYPE_BOOLEAN:
932 if (g_str_equal(name, "Priority") == TRUE) {
933 dbus_message_iter_get_basic(&value,
936 if (info_last->priority != info->priority)
937 session->info_dirty = TRUE;
938 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
939 dbus_message_iter_get_basic(&value,
940 &info->avoid_handover);
942 if (info_last->avoid_handover != info->avoid_handover)
943 session->info_dirty = TRUE;
944 } else if (g_str_equal(name, "StayConnected") == TRUE) {
945 dbus_message_iter_get_basic(&value,
946 &info->stay_connected);
948 if (info_last->stay_connected != info->stay_connected)
949 session->info_dirty = TRUE;
950 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
951 dbus_message_iter_get_basic(&value,
954 update_ecall(session);
959 case DBUS_TYPE_UINT32:
960 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
961 dbus_message_iter_get_basic(&value,
962 &info->periodic_connect);
964 if (info_last->periodic_connect != info->periodic_connect)
965 session->info_dirty = TRUE;
966 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
967 dbus_message_iter_get_basic(&value,
968 &info->idle_timeout);
970 if (info_last->idle_timeout != info->idle_timeout)
971 session->info_dirty = TRUE;
976 case DBUS_TYPE_STRING:
977 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
979 dbus_message_iter_get_basic(&value, &val);
980 info->roaming_policy =
981 string2roamingpolicy(val);
983 if (info_last->roaming_policy != info->roaming_policy)
984 session->info_dirty = TRUE;
993 if (session->info_dirty == TRUE)
996 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
999 return __connman_error_invalid_arguments(msg);
1002 static GDBusMethodTable session_methods[] = {
1003 { "Destroy", "", "", destroy_session },
1004 { "Connect", "", "", connect_session },
1005 { "Disconnect", "", "", disconnect_session },
1006 { "Change", "sv", "", change_session },
1010 int __connman_session_create(DBusMessage *msg)
1012 const char *owner, *notify_path;
1013 char *session_path = NULL;
1014 DBusMessageIter iter, array;
1015 struct connman_session *session;
1016 struct session_info *info, *info_last;
1018 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1019 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1020 enum connman_session_roaming_policy roaming_policy =
1021 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1022 GSList *allowed_bearers = NULL;
1023 unsigned int periodic_connect = 0;
1024 unsigned int idle_timeout = 0;
1028 owner = dbus_message_get_sender(msg);
1030 DBG("owner %s", owner);
1032 if (ecall_session != NULL) {
1034 * If there is an emergency call already going on,
1035 * ignore session creation attempt
1041 dbus_message_iter_init(msg, &iter);
1042 dbus_message_iter_recurse(&iter, &array);
1044 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1045 DBusMessageIter entry, value;
1046 const char *key, *val;
1048 dbus_message_iter_recurse(&array, &entry);
1049 dbus_message_iter_get_basic(&entry, &key);
1051 dbus_message_iter_next(&entry);
1052 dbus_message_iter_recurse(&entry, &value);
1054 switch (dbus_message_iter_get_arg_type(&value)) {
1055 case DBUS_TYPE_ARRAY:
1056 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1058 session_parse_allowed_bearers(&value);
1063 case DBUS_TYPE_BOOLEAN:
1064 if (g_str_equal(key, "Priority") == TRUE) {
1065 dbus_message_iter_get_basic(&value,
1067 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1068 dbus_message_iter_get_basic(&value,
1070 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1071 dbus_message_iter_get_basic(&value,
1073 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1074 dbus_message_iter_get_basic(&value,
1080 case DBUS_TYPE_UINT32:
1081 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1082 dbus_message_iter_get_basic(&value,
1084 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1085 dbus_message_iter_get_basic(&value,
1091 case DBUS_TYPE_STRING:
1092 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1093 dbus_message_iter_get_basic(&value, &val);
1094 roaming_policy = string2roamingpolicy(val);
1099 dbus_message_iter_next(&array);
1102 dbus_message_iter_next(&iter);
1103 dbus_message_iter_get_basic(&iter, ¬ify_path);
1105 if (notify_path == NULL) {
1110 session_path = g_strdup_printf("/sessions%s", notify_path);
1111 if (session_path == NULL) {
1116 session = g_hash_table_lookup(session_hash, session_path);
1117 if (session != NULL) {
1122 session = g_try_new0(struct connman_session, 1);
1123 if (session == NULL) {
1128 info = &session->info;
1129 info_last = &session->info_last;
1131 session->owner = g_strdup(owner);
1132 session->session_path = session_path;
1133 session->notify_path = g_strdup(notify_path);
1134 session->notify_watch =
1135 g_dbus_add_disconnect_watch(connection, session->owner,
1136 owner_disconnect, session, NULL);
1139 info->online = FALSE;
1140 info->priority = priority;
1141 info->avoid_handover = avoid_handover;
1142 info->stay_connected = stay_connected;
1143 info->periodic_connect = periodic_connect;
1144 info->idle_timeout = idle_timeout;
1145 info->ecall = ecall;
1146 info->roaming_policy = roaming_policy;
1147 info->service = NULL;
1150 if (allowed_bearers == NULL) {
1151 info->allowed_bearers =
1152 session_allowed_bearers_any();
1154 if (info->allowed_bearers == NULL) {
1159 info->allowed_bearers = allowed_bearers;
1162 g_hash_table_replace(session_hash, session->session_path, session);
1164 DBG("add %s", session->session_path);
1166 if (g_dbus_register_interface(connection, session->session_path,
1167 CONNMAN_SESSION_INTERFACE,
1168 session_methods, NULL,
1169 NULL, session, NULL) == FALSE) {
1170 connman_error("Failed to register %s", session->session_path);
1171 g_hash_table_remove(session_hash, session->session_path);
1178 g_dbus_send_reply(connection, msg,
1179 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1183 update_allowed_bearers(session);
1184 update_service(session);
1185 if (info->ecall == TRUE) {
1186 ecall_session = session;
1187 update_ecall_sessions(session);
1190 info_last->bearer = info->bearer;
1191 info_last->online = info->priority;
1192 info_last->avoid_handover = info->avoid_handover;
1193 info_last->stay_connected = info->stay_connected;
1194 info_last->periodic_connect = info->periodic_connect;
1195 info_last->idle_timeout = info->idle_timeout;
1196 info_last->ecall = info->ecall;
1197 info_last->roaming_policy = info->roaming_policy;
1198 info_last->service = info->service;
1199 info_last->marker = info->marker;
1200 info_last->allowed_bearers = info->allowed_bearers;
1202 session->info_dirty = TRUE;
1203 session->append_all = TRUE;
1205 g_timeout_add_seconds(0, session_cb, session);
1210 connman_error("Failed to create session");
1211 g_free(session_path);
1213 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1214 g_slist_free(allowed_bearers);
1219 int __connman_session_destroy(DBusMessage *msg)
1221 const char *owner, *session_path;
1222 struct connman_session *session;
1224 owner = dbus_message_get_sender(msg);
1226 DBG("owner %s", owner);
1228 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1230 if (session_path == NULL)
1233 session = g_hash_table_lookup(session_hash, session_path);
1234 if (session == NULL)
1237 if (g_strcmp0(owner, session->owner) != 0)
1240 session_disconnect(session);
1245 connman_bool_t __connman_session_mode()
1250 void __connman_session_set_mode(connman_bool_t enable)
1252 DBG("enable %d", enable);
1254 if (sessionmode == enable)
1257 sessionmode = enable;
1259 if (sessionmode == TRUE)
1260 __connman_service_disconnect_all();
1263 static void service_add(struct connman_service *service)
1265 GHashTableIter iter;
1266 gpointer key, value;
1267 struct connman_session *session;
1269 DBG("service %p", service);
1271 g_hash_table_iter_init(&iter, session_hash);
1273 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1276 if (service_match(session, service) == FALSE)
1279 g_sequence_insert_sorted(session->service_list, service,
1280 sort_services, session);
1282 session_cb(session);
1286 static gint service_in_session(gconstpointer a, gconstpointer b,
1295 static void service_remove(struct connman_service *service)
1298 GHashTableIter iter;
1299 gpointer key, value;
1300 GSequenceIter *seq_iter;
1301 struct connman_session *session;
1302 struct session_info *info;
1303 struct connman_service *found_service;
1305 DBG("service %p", service);
1307 g_hash_table_iter_init(&iter, session_hash);
1309 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1311 info = &session->info;
1313 if (session->service_list == NULL)
1316 seq_iter = g_sequence_search(session->service_list, service,
1317 service_in_session, NULL);
1318 if (g_sequence_iter_is_end(seq_iter) == TRUE)
1321 g_sequence_remove(seq_iter);
1323 found_service = g_sequence_get(seq_iter);
1324 if (found_service != info->service)
1327 info->service = NULL;
1328 session_cb(session);
1332 static void service_state_changed(struct connman_service *service,
1333 enum connman_service_state state)
1335 GHashTableIter iter;
1336 gpointer key, value;
1337 struct connman_session *session;
1338 struct session_info *info;
1339 connman_bool_t online;
1341 DBG("service %p state %d", service, state);
1343 g_hash_table_iter_init(&iter, session_hash);
1345 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1347 info = &session->info;
1349 if (info->service == service) {
1350 online = __connman_service_is_connected(service);
1351 if (info->online == online)
1354 info->online = online;
1355 session_cb(session);
1360 static void ipconfig_changed(struct connman_service *service,
1361 struct connman_ipconfig *ipconfig)
1363 GHashTableIter iter;
1364 gpointer key, value;
1365 struct connman_session *session;
1366 struct session_info *info;
1367 enum connman_ipconfig_type type;
1369 DBG("service %p ipconfig %p", service, ipconfig);
1371 type = __connman_ipconfig_get_config_type(ipconfig);
1373 g_hash_table_iter_init(&iter, session_hash);
1375 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1377 info = &session->info;
1379 if (info->service == service) {
1380 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1381 ipconfig_ipv4_changed(session);
1382 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1383 ipconfig_ipv6_changed(session);
1388 static struct connman_notifier session_notifier = {
1390 .service_add = service_add,
1391 .service_remove = service_remove,
1392 .service_state_changed = service_state_changed,
1393 .ipconfig_changed = ipconfig_changed,
1396 int __connman_session_init(void)
1402 connection = connman_dbus_get_connection();
1403 if (connection == NULL)
1406 err = connman_notifier_register(&session_notifier);
1408 dbus_connection_unref(connection);
1412 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1413 NULL, cleanup_session);
1415 sessionmode = FALSE;
1419 void __connman_session_cleanup(void)
1423 if (connection == NULL)
1426 connman_notifier_unregister(&session_notifier);
1428 g_hash_table_foreach(session_hash, release_session, NULL);
1429 g_hash_table_destroy(session_hash);
1431 dbus_connection_unref(connection);