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;
792 g_timeout_add_seconds(0, session_cb, session);
794 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
797 static DBusMessage *disconnect_session(DBusConnection *conn,
798 DBusMessage *msg, void *user_data)
800 struct connman_session *session = user_data;
801 struct session_info *info = &session->info;
803 DBG("session %p", session);
805 info->connect = FALSE;
807 g_timeout_add_seconds(0, session_cb, session);
809 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
812 static void print_name(gpointer data, gpointer user_data)
814 struct connman_service *service = data;
816 DBG("service %p type %s name %s", service,
817 service2bearer(connman_service_get_type(service)),
818 __connman_service_get_name(service));
821 static void update_allowed_bearers(struct connman_session *session)
823 if (session->service_list != NULL)
824 g_sequence_free(session->service_list);
826 session->service_list = __connman_service_get_list(session,
828 g_sequence_sort(session->service_list, sort_services, session);
829 g_sequence_foreach(session->service_list, print_name, NULL);
832 static void update_ecall(struct connman_session *session)
834 struct session_info *info = &session->info;
835 struct session_info *info_last = &session->info_last;
838 struct connman_session *session_iter;
840 DBG("ecall_session %p ecall %d -> %d", ecall_session,
841 info_last->ecall, info->ecall);
843 if (ecall_session == NULL) {
844 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
847 ecall_session = session;
848 } else if (ecall_session == session) {
849 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
852 ecall_session = NULL;
857 g_hash_table_iter_init(&iter, session_hash);
859 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
860 session_iter = value;
862 if (session_iter == session)
865 session_iter->info.ecall = info->ecall;
867 g_timeout_add_seconds(0, session_cb, session_iter);
870 session->info_dirty = TRUE;
874 /* not a valid transition */
875 info->ecall = info_last->ecall;
878 static DBusMessage *change_session(DBusConnection *conn,
879 DBusMessage *msg, void *user_data)
881 struct connman_session *session = user_data;
882 struct session_info *info = &session->info;
883 struct session_info *info_last = &session->info_last;
884 DBusMessageIter iter, value;
886 GSList *allowed_bearers;
889 DBG("session %p", session);
890 if (dbus_message_iter_init(msg, &iter) == FALSE)
891 return __connman_error_invalid_arguments(msg);
893 dbus_message_iter_get_basic(&iter, &name);
894 dbus_message_iter_next(&iter);
895 dbus_message_iter_recurse(&iter, &value);
897 switch (dbus_message_iter_get_arg_type(&value)) {
898 case DBUS_TYPE_ARRAY:
899 if (g_str_equal(name, "AllowedBearers") == TRUE) {
900 allowed_bearers = session_parse_allowed_bearers(&value);
902 g_slist_foreach(info->allowed_bearers,
903 cleanup_bearer_info, NULL);
904 g_slist_free(info->allowed_bearers);
906 if (allowed_bearers == NULL) {
907 allowed_bearers = session_allowed_bearers_any();
909 if (allowed_bearers == NULL)
910 return __connman_error_failed(msg, ENOMEM);
913 info->allowed_bearers = allowed_bearers;
915 update_allowed_bearers(session);
916 session->info_dirty = TRUE;
921 case DBUS_TYPE_BOOLEAN:
922 if (g_str_equal(name, "Priority") == TRUE) {
923 dbus_message_iter_get_basic(&value,
926 if (info_last->priority != info->priority)
927 session->info_dirty = TRUE;
928 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
929 dbus_message_iter_get_basic(&value,
930 &info->avoid_handover);
932 if (info_last->avoid_handover != info->avoid_handover)
933 session->info_dirty = TRUE;
934 } else if (g_str_equal(name, "StayConnected") == TRUE) {
935 dbus_message_iter_get_basic(&value,
936 &info->stay_connected);
938 if (info_last->stay_connected != info->stay_connected)
939 session->info_dirty = TRUE;
940 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
941 dbus_message_iter_get_basic(&value,
944 update_ecall(session);
949 case DBUS_TYPE_UINT32:
950 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
951 dbus_message_iter_get_basic(&value,
952 &info->periodic_connect);
954 if (info_last->periodic_connect != info->periodic_connect)
955 session->info_dirty = TRUE;
956 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
957 dbus_message_iter_get_basic(&value,
958 &info->idle_timeout);
960 if (info_last->idle_timeout != info->idle_timeout)
961 session->info_dirty = TRUE;
966 case DBUS_TYPE_STRING:
967 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
969 dbus_message_iter_get_basic(&value, &val);
970 info->roaming_policy =
971 string2roamingpolicy(val);
973 if (info_last->roaming_policy != info->roaming_policy)
974 session->info_dirty = TRUE;
983 err = session_notify(session);
985 __connman_error_failed(msg, -err);
987 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
990 return __connman_error_invalid_arguments(msg);
993 static GDBusMethodTable session_methods[] = {
994 { "Destroy", "", "", destroy_session },
995 { "Connect", "", "", connect_session },
996 { "Disconnect", "", "", disconnect_session },
997 { "Change", "sv", "", change_session },
1001 int __connman_session_create(DBusMessage *msg)
1003 const char *owner, *notify_path;
1005 DBusMessageIter iter, array;
1006 struct connman_session *session;
1007 struct session_info *info, *info_last;
1009 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1010 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1011 enum connman_session_roaming_policy roaming_policy =
1012 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1013 GSList *allowed_bearers = NULL;
1014 unsigned int periodic_connect = 0;
1015 unsigned int idle_timeout = 0;
1019 owner = dbus_message_get_sender(msg);
1021 DBG("owner %s", owner);
1023 dbus_message_iter_init(msg, &iter);
1024 dbus_message_iter_recurse(&iter, &array);
1026 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1027 DBusMessageIter entry, value;
1028 const char *key, *val;
1030 dbus_message_iter_recurse(&array, &entry);
1031 dbus_message_iter_get_basic(&entry, &key);
1033 dbus_message_iter_next(&entry);
1034 dbus_message_iter_recurse(&entry, &value);
1036 switch (dbus_message_iter_get_arg_type(&value)) {
1037 case DBUS_TYPE_ARRAY:
1038 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1040 session_parse_allowed_bearers(&value);
1045 case DBUS_TYPE_BOOLEAN:
1046 if (g_str_equal(key, "Priority") == TRUE) {
1047 dbus_message_iter_get_basic(&value,
1049 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1050 dbus_message_iter_get_basic(&value,
1052 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1053 dbus_message_iter_get_basic(&value,
1055 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1056 dbus_message_iter_get_basic(&value,
1062 case DBUS_TYPE_UINT32:
1063 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1064 dbus_message_iter_get_basic(&value,
1066 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1067 dbus_message_iter_get_basic(&value,
1073 case DBUS_TYPE_STRING:
1074 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1075 dbus_message_iter_get_basic(&value, &val);
1076 roaming_policy = string2roamingpolicy(val);
1081 dbus_message_iter_next(&array);
1084 dbus_message_iter_next(&iter);
1085 dbus_message_iter_get_basic(&iter, ¬ify_path);
1087 if (notify_path == NULL) {
1088 session_path = NULL;
1093 session_path = g_strdup_printf("/sessions%s", notify_path);
1094 if (session_path == NULL) {
1099 session = g_hash_table_lookup(session_hash, session_path);
1100 if (session != NULL) {
1105 session = g_try_new0(struct connman_session, 1);
1106 if (session == NULL) {
1111 info = &session->info;
1112 info_last = &session->info_last;
1114 session->owner = g_strdup(owner);
1115 session->session_path = session_path;
1116 session->notify_path = g_strdup(notify_path);
1117 session->notify_watch =
1118 g_dbus_add_disconnect_watch(connection, session->owner,
1119 owner_disconnect, session, NULL);
1122 info->online = FALSE;
1123 info->priority = priority;
1124 info->avoid_handover = avoid_handover;
1125 info->stay_connected = stay_connected;
1126 info->periodic_connect = periodic_connect;
1127 info->idle_timeout = idle_timeout;
1128 info->ecall = ecall;
1129 info->roaming_policy = roaming_policy;
1130 info->service = NULL;
1133 if (allowed_bearers == NULL) {
1134 info->allowed_bearers =
1135 session_allowed_bearers_any();
1137 if (info->allowed_bearers == NULL) {
1142 info->allowed_bearers = allowed_bearers;
1145 update_allowed_bearers(session);
1146 update_service(session);
1148 info_last->bearer = info->bearer;
1149 info_last->online = info->priority;
1150 info_last->avoid_handover = info->avoid_handover;
1151 info_last->stay_connected = info->stay_connected;
1152 info_last->periodic_connect = info->periodic_connect;
1153 info_last->idle_timeout = info->idle_timeout;
1154 info_last->ecall = info->ecall;
1155 info_last->roaming_policy = info->roaming_policy;
1156 info_last->service = info->service;
1157 info_last->marker = info->marker;
1158 info_last->allowed_bearers = info->allowed_bearers;
1160 session->info_dirty = TRUE;
1161 session->append_all = TRUE;
1163 g_hash_table_replace(session_hash, session->session_path, session);
1165 DBG("add %s", session->session_path);
1167 if (g_dbus_register_interface(connection, session->session_path,
1168 CONNMAN_SESSION_INTERFACE,
1169 session_methods, NULL,
1170 NULL, session, NULL) == FALSE) {
1171 connman_error("Failed to register %s", session->session_path);
1172 g_hash_table_remove(session_hash, session->session_path);
1179 g_dbus_send_reply(connection, msg,
1180 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1183 g_timeout_add_seconds(0, session_cb, session);
1188 connman_error("Failed to create session");
1189 g_free(session_path);
1191 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1192 g_slist_free(allowed_bearers);
1197 int __connman_session_destroy(DBusMessage *msg)
1199 const char *owner, *session_path;
1200 struct connman_session *session;
1202 owner = dbus_message_get_sender(msg);
1204 DBG("owner %s", owner);
1206 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1208 if (session_path == NULL)
1211 session = g_hash_table_lookup(session_hash, session_path);
1212 if (session == NULL)
1215 if (g_strcmp0(owner, session->owner) != 0)
1218 session_disconnect(session);
1223 connman_bool_t __connman_session_mode()
1228 void __connman_session_set_mode(connman_bool_t enable)
1230 DBG("enable %d", enable);
1232 if (sessionmode == enable)
1235 sessionmode = enable;
1237 if (sessionmode == TRUE)
1238 __connman_service_disconnect_all();
1241 static void service_add(struct connman_service *service)
1243 GHashTableIter iter;
1244 gpointer key, value;
1245 struct connman_session *session;
1247 DBG("service %p", service);
1249 g_hash_table_iter_init(&iter, session_hash);
1251 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1254 if (service_match(session, service) == FALSE)
1257 g_sequence_insert_sorted(session->service_list, service,
1258 sort_services, session);
1260 session_cb(session);
1264 static gint service_in_session(gconstpointer a, gconstpointer b,
1273 static void service_remove(struct connman_service *service)
1276 GHashTableIter iter;
1277 gpointer key, value;
1278 GSequenceIter *seq_iter;
1279 struct connman_session *session;
1280 struct session_info *info;
1281 struct connman_service *found_service;
1283 DBG("service %p", service);
1285 g_hash_table_iter_init(&iter, session_hash);
1287 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1289 info = &session->info;
1291 if (session->service_list == NULL)
1294 seq_iter = g_sequence_search(session->service_list, service,
1295 service_in_session, NULL);
1296 if (g_sequence_iter_is_end(seq_iter) == TRUE)
1299 g_sequence_remove(seq_iter);
1301 found_service = g_sequence_get(seq_iter);
1302 if (found_service != info->service)
1305 info->service = NULL;
1306 session_cb(session);
1310 static void service_state_changed(struct connman_service *service,
1311 enum connman_service_state state)
1313 GHashTableIter iter;
1314 gpointer key, value;
1315 struct connman_session *session;
1316 struct session_info *info;
1317 connman_bool_t online;
1319 DBG("service %p state %d", service, state);
1321 g_hash_table_iter_init(&iter, session_hash);
1323 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1325 info = &session->info;
1327 if (info->service == service) {
1328 online = __connman_service_is_connected(service);
1329 if (info->online == online)
1332 info->online = online;
1333 session_cb(session);
1338 static void ipconfig_changed(struct connman_service *service,
1339 struct connman_ipconfig *ipconfig)
1341 GHashTableIter iter;
1342 gpointer key, value;
1343 struct connman_session *session;
1344 struct session_info *info;
1345 enum connman_ipconfig_type type;
1347 DBG("service %p ipconfig %p", service, ipconfig);
1349 type = __connman_ipconfig_get_config_type(ipconfig);
1351 g_hash_table_iter_init(&iter, session_hash);
1353 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1355 info = &session->info;
1357 if (info->service == service) {
1358 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1359 ipconfig_ipv4_changed(session);
1360 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1361 ipconfig_ipv6_changed(session);
1366 static struct connman_notifier session_notifier = {
1368 .service_add = service_add,
1369 .service_remove = service_remove,
1370 .service_state_changed = service_state_changed,
1371 .ipconfig_changed = ipconfig_changed,
1374 int __connman_session_init(void)
1380 connection = connman_dbus_get_connection();
1381 if (connection == NULL)
1384 err = connman_notifier_register(&session_notifier);
1386 dbus_connection_unref(connection);
1390 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1391 NULL, cleanup_session);
1393 sessionmode = FALSE;
1397 void __connman_session_cleanup(void)
1401 if (connection == NULL)
1404 connman_notifier_unregister(&session_notifier);
1406 g_hash_table_foreach(session_hash, release_session, NULL);
1407 g_hash_table_destroy(session_hash);
1409 dbus_connection_unref(connection);