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 void cleanup_bearer_info(gpointer data, gpointer user_data)
165 struct bearer_info *info = data;
171 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
173 struct bearer_info *info;
174 DBusMessageIter array;
177 dbus_message_iter_recurse(iter, &array);
179 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
182 dbus_message_iter_get_basic(&array, &bearer);
184 info = g_try_new0(struct bearer_info, 1);
186 g_slist_foreach(list, cleanup_bearer_info, NULL);
192 info->name = g_strdup(bearer);
193 info->service_type = bearer2service(info->name);
195 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
196 g_strcmp0(info->name, "*") == 0) {
197 info->match_all = TRUE;
199 info->match_all = FALSE;
202 list = g_slist_append(list, info);
204 dbus_message_iter_next(&array);
210 static GSList *session_allowed_bearers_any(void)
212 struct bearer_info *info;
215 info = g_try_new0(struct bearer_info, 1);
222 info->name = g_strdup("");
223 info->match_all = TRUE;
224 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
226 list = g_slist_append(list, info);
231 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
233 struct session_info *info = user_data;
236 for (list = info->allowed_bearers;
237 list != NULL; list = list->next) {
238 struct bearer_info *info = list->data;
240 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
245 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
247 struct connman_service *service = user_data;
248 struct connman_ipconfig *ipconfig_ipv4;
253 ipconfig_ipv4 = __connman_service_get_ip4config(service);
254 if (ipconfig_ipv4 == NULL)
257 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
260 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
262 struct connman_service *service = user_data;
263 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
268 ipconfig_ipv4 = __connman_service_get_ip4config(service);
269 ipconfig_ipv6 = __connman_service_get_ip6config(service);
270 if (ipconfig_ipv6 == NULL)
273 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
276 static void append_notify(DBusMessageIter *dict,
277 struct connman_session *session)
279 struct session_info *info = &session->info;
280 struct session_info *info_last = &session->info_last;
283 if (session->append_all == TRUE ||
284 info->bearer != info_last->bearer) {
285 connman_dbus_dict_append_basic(dict, "Bearer",
288 info_last->bearer = info->bearer;
291 if (session->append_all == TRUE ||
292 info->online != info_last->online) {
293 connman_dbus_dict_append_basic(dict, "Online",
296 info_last->online = info->online;
299 if (session->append_all == TRUE ||
300 info->name != info_last->name) {
301 connman_dbus_dict_append_basic(dict, "Name",
304 info_last->name = info->name;
307 if (session->append_all == TRUE ||
308 info->service != info_last->service) {
309 connman_dbus_dict_append_dict(dict, "IPv4",
310 append_ipconfig_ipv4,
313 connman_dbus_dict_append_dict(dict, "IPv6",
314 append_ipconfig_ipv6,
317 connman_dbus_dict_append_basic(dict, "Interface",
321 info_last->ifname = info->ifname;
322 info_last->service = info->service;
326 if (session->append_all == TRUE ||
327 info->priority != info_last->priority) {
328 connman_dbus_dict_append_basic(dict, "Priority",
331 info_last->priority = info->priority;
334 if (session->append_all == TRUE ||
335 info->allowed_bearers != info_last->allowed_bearers) {
336 connman_dbus_dict_append_array(dict, "AllowedBearers",
338 append_allowed_bearers,
340 info_last->allowed_bearers = info->allowed_bearers;
343 if (session->append_all == TRUE ||
344 info->avoid_handover != info_last->avoid_handover) {
345 connman_dbus_dict_append_basic(dict, "AvoidHandover",
347 &info->avoid_handover);
348 info_last->avoid_handover = info->avoid_handover;
351 if (session->append_all == TRUE ||
352 info->stay_connected != info_last->stay_connected) {
353 connman_dbus_dict_append_basic(dict, "StayConnected",
355 &info->stay_connected);
356 info_last->stay_connected = info->stay_connected;
359 if (session->append_all == TRUE ||
360 info->periodic_connect != info_last->periodic_connect) {
361 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
363 &info->periodic_connect);
364 info_last->periodic_connect = info->periodic_connect;
367 if (session->append_all == TRUE ||
368 info->idle_timeout != info_last->idle_timeout) {
369 connman_dbus_dict_append_basic(dict, "IdleTimeout",
371 &info->idle_timeout);
372 info_last->idle_timeout = info->idle_timeout;
375 if (session->append_all == TRUE ||
376 info->ecall != info_last->ecall) {
377 connman_dbus_dict_append_basic(dict, "EmergencyCall",
380 info_last->ecall = info->ecall;
383 if (session->append_all == TRUE ||
384 info->roaming_policy != info_last->roaming_policy) {
385 policy = roamingpolicy2string(info->roaming_policy);
386 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
389 info_last->roaming_policy = info->roaming_policy;
392 if (session->append_all == TRUE ||
393 info->marker != info_last->marker) {
394 connman_dbus_dict_append_basic(dict, "SessionMarker",
397 info_last->marker = info->marker;
400 session->append_all = FALSE;
401 session->info_dirty = FALSE;
404 static int session_notify(struct connman_session *session)
407 DBusMessageIter array, dict;
409 if (session->info_dirty == FALSE)
412 DBG("session %p owner %s notify_path %s", session,
413 session->owner, session->notify_path);
415 msg = dbus_message_new_method_call(session->owner, session->notify_path,
416 CONNMAN_NOTIFICATION_INTERFACE,
421 dbus_message_iter_init_append(msg, &array);
422 connman_dbus_dict_open(&array, &dict);
424 append_notify(&dict, session);
426 connman_dbus_dict_close(&array, &dict);
428 g_dbus_send_message(connection, msg);
430 session->info_dirty = FALSE;
435 static void ipconfig_ipv4_changed(struct connman_session *session)
437 struct session_info *info = &session->info;
439 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
440 "IPv4", append_ipconfig_ipv4,
444 static void ipconfig_ipv6_changed(struct connman_session *session)
446 struct session_info *info = &session->info;
448 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
449 "IPv6", append_ipconfig_ipv6,
453 static GSequenceIter *lookup_service(struct connman_session *session,
454 struct connman_service *service)
461 iter = g_sequence_get_begin_iter(session->service_list);
463 while (g_sequence_iter_is_end(iter) == FALSE) {
464 struct connman_service *service_iter = g_sequence_get(iter);
466 if (service_iter == service)
469 iter = g_sequence_iter_next(iter);
475 static connman_bool_t service_type_match(struct connman_session *session,
476 struct connman_service *service)
478 struct session_info *info = &session->info;
481 for (list = info->allowed_bearers;
482 list != NULL; list = list->next) {
483 struct bearer_info *info = list->data;
484 enum connman_service_type service_type;
486 if (info->match_all == TRUE)
489 service_type = connman_service_get_type(service);
490 if (info->service_type == service_type)
497 static connman_bool_t service_match(struct connman_session *session,
498 struct connman_service *service)
500 if (service_type_match(session, service) == FALSE)
506 static int service_type_weight(enum connman_service_type type)
509 * The session doesn't care which service
510 * to use. Nevertheless we have to sort them
511 * according their type. The ordering is
520 case CONNMAN_SERVICE_TYPE_ETHERNET:
522 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
524 case CONNMAN_SERVICE_TYPE_WIFI:
525 case CONNMAN_SERVICE_TYPE_WIMAX:
527 case CONNMAN_SERVICE_TYPE_CELLULAR:
529 case CONNMAN_SERVICE_TYPE_UNKNOWN:
530 case CONNMAN_SERVICE_TYPE_SYSTEM:
531 case CONNMAN_SERVICE_TYPE_GPS:
532 case CONNMAN_SERVICE_TYPE_VPN:
533 case CONNMAN_SERVICE_TYPE_GADGET:
540 static gint sort_allowed_bearers(struct connman_service *service_a,
541 struct connman_service *service_b,
542 struct connman_session *session)
544 struct session_info *info = &session->info;
546 enum connman_service_type type_a, type_b;
547 int weight_a, weight_b;
549 type_a = connman_service_get_type(service_a);
550 type_b = connman_service_get_type(service_b);
552 for (list = info->allowed_bearers;
553 list != NULL; list = list->next) {
554 struct bearer_info *info = list->data;
556 if (info->match_all == TRUE) {
557 if (type_a != type_b) {
558 weight_a = service_type_weight(type_a);
559 weight_b = service_type_weight(type_b);
561 if (weight_a > weight_b)
564 if (weight_a < weight_b)
571 if (type_a == info->service_type &&
572 type_b == info->service_type) {
576 if (type_a == info->service_type &&
577 type_b != info->service_type) {
581 if (type_a != info->service_type &&
582 type_b == info->service_type) {
590 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
592 struct connman_service *service_a = (void *)a;
593 struct connman_service *service_b = (void *)b;
594 struct connman_session *session = user_data;
596 return sort_allowed_bearers(service_a, service_b, session);
599 static void cleanup_session(gpointer user_data)
601 struct connman_session *session = user_data;
602 struct session_info *info = &session->info;
604 DBG("remove %s", session->session_path);
606 g_sequence_free(session->service_list);
608 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
609 g_slist_free(info->allowed_bearers);
611 g_free(session->owner);
612 g_free(session->session_path);
613 g_free(session->notify_path);
618 static void release_session(gpointer key, gpointer value, gpointer user_data)
620 struct connman_session *session = value;
621 DBusMessage *message;
623 DBG("owner %s path %s", session->owner, session->notify_path);
625 if (session->notify_watch > 0)
626 g_dbus_remove_watch(connection, session->notify_watch);
628 g_dbus_unregister_interface(connection, session->session_path,
629 CONNMAN_SESSION_INTERFACE);
631 message = dbus_message_new_method_call(session->owner,
632 session->notify_path,
633 CONNMAN_NOTIFICATION_INTERFACE,
638 dbus_message_set_no_reply(message, TRUE);
640 g_dbus_send_message(connection, message);
643 static int session_disconnect(struct connman_session *session)
645 DBG("session %p, %s", session, session->owner);
647 if (session->notify_watch > 0)
648 g_dbus_remove_watch(connection, session->notify_watch);
650 g_dbus_unregister_interface(connection, session->session_path,
651 CONNMAN_SESSION_INTERFACE);
653 g_hash_table_remove(session_hash, session->session_path);
658 static void owner_disconnect(DBusConnection *conn, void *user_data)
660 struct connman_session *session = user_data;
662 DBG("session %p, %s died", session, session->owner);
664 session_disconnect(session);
667 static DBusMessage *destroy_session(DBusConnection *conn,
668 DBusMessage *msg, void *user_data)
670 struct connman_session *session = user_data;
672 DBG("session %p", session);
674 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
677 static gboolean session_changed_connect(gpointer user_data)
679 struct connman_session *session = user_data;
680 struct session_info *info = &session->info;
682 __connman_service_connect(info->service);
687 static void update_info(struct session_info *info)
689 enum connman_service_type type;
692 if (info->service != NULL) {
693 type = connman_service_get_type(info->service);
694 info->bearer = service2bearer(type);
696 info->online = __connman_service_is_connected(info->service);
697 info->name = __connman_service_get_name(info->service);
699 idx = __connman_service_get_index(info->service);
700 info->ifname = connman_inet_ifname(idx);
701 if (info->ifname == NULL)
705 info->online = FALSE;
711 static void session_changed(struct connman_session *session)
713 struct session_info *info = &session->info;
714 struct session_info *info_last = &session->info_last;
715 struct connman_service *service = NULL;
716 GSourceFunc callback = NULL;
720 * TODO: This only a placeholder for the 'real' algorithm to
721 * play a bit around. So we are going to improve it step by step.
724 if (info->ecall == TRUE && session != ecall_session)
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) {
770 session->info_dirty = TRUE;
774 static gboolean session_cb(gpointer user_data)
776 struct connman_session *session = user_data;
778 session_changed(session);
779 session_notify(session);
784 static DBusMessage *connect_session(DBusConnection *conn,
785 DBusMessage *msg, void *user_data)
787 struct connman_session *session = user_data;
788 struct session_info *info = &session->info;
790 DBG("session %p", session);
792 info->connect = TRUE;
794 if (ecall_session != NULL && ecall_session != session)
795 return __connman_error_failed(msg, EBUSY);
797 session->info_dirty = TRUE;
799 g_timeout_add_seconds(0, session_cb, session);
801 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
804 static DBusMessage *disconnect_session(DBusConnection *conn,
805 DBusMessage *msg, void *user_data)
807 struct connman_session *session = user_data;
808 struct session_info *info = &session->info;
810 DBG("session %p", session);
812 info->connect = FALSE;
814 if (ecall_session != NULL && ecall_session != session)
815 return __connman_error_failed(msg, EBUSY);
817 session->info_dirty = TRUE;
819 g_timeout_add_seconds(0, session_cb, session);
821 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
824 static void print_name(gpointer data, gpointer user_data)
826 struct connman_service *service = data;
828 DBG("service %p type %s name %s", service,
829 service2bearer(connman_service_get_type(service)),
830 __connman_service_get_name(service));
833 static void update_allowed_bearers(struct connman_session *session)
835 if (session->service_list != NULL)
836 g_sequence_free(session->service_list);
838 session->service_list = __connman_service_get_list(session,
840 g_sequence_sort(session->service_list, sort_services, session);
841 g_sequence_foreach(session->service_list, print_name, NULL);
843 session->info_dirty = TRUE;
846 static void update_ecall_sessions(struct connman_session *session)
848 struct session_info *info = &session->info;
849 struct connman_session *session_iter;
853 g_hash_table_iter_init(&iter, session_hash);
855 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
856 session_iter = value;
858 if (session_iter == session)
861 session_iter->info.ecall = info->ecall;
862 session_iter->info_dirty = TRUE;
864 g_timeout_add_seconds(0, session_cb, session_iter);
868 static void update_ecall(struct connman_session *session)
870 struct session_info *info = &session->info;
871 struct session_info *info_last = &session->info_last;
873 DBG("ecall_session %p ecall %d -> %d", ecall_session,
874 info_last->ecall, info->ecall);
876 if (ecall_session == NULL) {
877 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
880 ecall_session = session;
881 } else if (ecall_session == session) {
882 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
885 ecall_session = NULL;
890 update_ecall_sessions(session);
892 session->info_dirty = TRUE;
896 /* not a valid transition */
897 info->ecall = info_last->ecall;
900 static DBusMessage *change_session(DBusConnection *conn,
901 DBusMessage *msg, void *user_data)
903 struct connman_session *session = user_data;
904 struct session_info *info = &session->info;
905 struct session_info *info_last = &session->info_last;
906 DBusMessageIter iter, value;
908 GSList *allowed_bearers;
910 DBG("session %p", session);
911 if (dbus_message_iter_init(msg, &iter) == FALSE)
912 return __connman_error_invalid_arguments(msg);
914 dbus_message_iter_get_basic(&iter, &name);
915 dbus_message_iter_next(&iter);
916 dbus_message_iter_recurse(&iter, &value);
918 switch (dbus_message_iter_get_arg_type(&value)) {
919 case DBUS_TYPE_ARRAY:
920 if (g_str_equal(name, "AllowedBearers") == TRUE) {
921 allowed_bearers = session_parse_allowed_bearers(&value);
923 g_slist_foreach(info->allowed_bearers,
924 cleanup_bearer_info, NULL);
925 g_slist_free(info->allowed_bearers);
927 if (allowed_bearers == NULL) {
928 allowed_bearers = session_allowed_bearers_any();
930 if (allowed_bearers == NULL)
931 return __connman_error_failed(msg, ENOMEM);
934 info->allowed_bearers = allowed_bearers;
936 update_allowed_bearers(session);
941 case DBUS_TYPE_BOOLEAN:
942 if (g_str_equal(name, "Priority") == TRUE) {
943 dbus_message_iter_get_basic(&value,
946 if (info_last->priority != info->priority)
947 session->info_dirty = TRUE;
948 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
949 dbus_message_iter_get_basic(&value,
950 &info->avoid_handover);
952 if (info_last->avoid_handover != info->avoid_handover)
953 session->info_dirty = TRUE;
954 } else if (g_str_equal(name, "StayConnected") == TRUE) {
955 dbus_message_iter_get_basic(&value,
956 &info->stay_connected);
958 if (info_last->stay_connected != info->stay_connected)
959 session->info_dirty = TRUE;
960 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
961 dbus_message_iter_get_basic(&value,
964 update_ecall(session);
969 case DBUS_TYPE_UINT32:
970 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
971 dbus_message_iter_get_basic(&value,
972 &info->periodic_connect);
974 if (info_last->periodic_connect != info->periodic_connect)
975 session->info_dirty = TRUE;
976 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
977 dbus_message_iter_get_basic(&value,
978 &info->idle_timeout);
980 if (info_last->idle_timeout != info->idle_timeout)
981 session->info_dirty = TRUE;
986 case DBUS_TYPE_STRING:
987 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
989 dbus_message_iter_get_basic(&value, &val);
990 info->roaming_policy =
991 string2roamingpolicy(val);
993 if (info_last->roaming_policy != info->roaming_policy)
994 session->info_dirty = TRUE;
1003 if (session->info_dirty == TRUE)
1004 session_cb(session);
1006 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1009 return __connman_error_invalid_arguments(msg);
1012 static GDBusMethodTable session_methods[] = {
1013 { "Destroy", "", "", destroy_session },
1014 { "Connect", "", "", connect_session },
1015 { "Disconnect", "", "", disconnect_session },
1016 { "Change", "sv", "", change_session },
1020 int __connman_session_create(DBusMessage *msg)
1022 const char *owner, *notify_path;
1023 char *session_path = NULL;
1024 DBusMessageIter iter, array;
1025 struct connman_session *session;
1026 struct session_info *info, *info_last;
1028 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1029 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1030 enum connman_session_roaming_policy roaming_policy =
1031 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1032 GSList *allowed_bearers = NULL;
1033 unsigned int periodic_connect = 0;
1034 unsigned int idle_timeout = 0;
1038 owner = dbus_message_get_sender(msg);
1040 DBG("owner %s", owner);
1042 if (ecall_session != NULL) {
1044 * If there is an emergency call already going on,
1045 * ignore session creation attempt
1051 dbus_message_iter_init(msg, &iter);
1052 dbus_message_iter_recurse(&iter, &array);
1054 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1055 DBusMessageIter entry, value;
1056 const char *key, *val;
1058 dbus_message_iter_recurse(&array, &entry);
1059 dbus_message_iter_get_basic(&entry, &key);
1061 dbus_message_iter_next(&entry);
1062 dbus_message_iter_recurse(&entry, &value);
1064 switch (dbus_message_iter_get_arg_type(&value)) {
1065 case DBUS_TYPE_ARRAY:
1066 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1068 session_parse_allowed_bearers(&value);
1073 case DBUS_TYPE_BOOLEAN:
1074 if (g_str_equal(key, "Priority") == TRUE) {
1075 dbus_message_iter_get_basic(&value,
1077 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1078 dbus_message_iter_get_basic(&value,
1080 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1081 dbus_message_iter_get_basic(&value,
1083 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1084 dbus_message_iter_get_basic(&value,
1090 case DBUS_TYPE_UINT32:
1091 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1092 dbus_message_iter_get_basic(&value,
1094 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1095 dbus_message_iter_get_basic(&value,
1101 case DBUS_TYPE_STRING:
1102 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1103 dbus_message_iter_get_basic(&value, &val);
1104 roaming_policy = string2roamingpolicy(val);
1109 dbus_message_iter_next(&array);
1112 dbus_message_iter_next(&iter);
1113 dbus_message_iter_get_basic(&iter, ¬ify_path);
1115 if (notify_path == NULL) {
1120 session_path = g_strdup_printf("/sessions%s", notify_path);
1121 if (session_path == NULL) {
1126 session = g_hash_table_lookup(session_hash, session_path);
1127 if (session != NULL) {
1132 session = g_try_new0(struct connman_session, 1);
1133 if (session == NULL) {
1138 info = &session->info;
1139 info_last = &session->info_last;
1141 session->owner = g_strdup(owner);
1142 session->session_path = session_path;
1143 session->notify_path = g_strdup(notify_path);
1144 session->notify_watch =
1145 g_dbus_add_disconnect_watch(connection, session->owner,
1146 owner_disconnect, session, NULL);
1149 info->online = FALSE;
1150 info->priority = priority;
1151 info->avoid_handover = avoid_handover;
1152 info->stay_connected = stay_connected;
1153 info->periodic_connect = periodic_connect;
1154 info->idle_timeout = idle_timeout;
1155 info->ecall = ecall;
1156 info->roaming_policy = roaming_policy;
1157 info->service = NULL;
1160 if (allowed_bearers == NULL) {
1161 info->allowed_bearers =
1162 session_allowed_bearers_any();
1164 if (info->allowed_bearers == NULL) {
1169 info->allowed_bearers = allowed_bearers;
1172 g_hash_table_replace(session_hash, session->session_path, session);
1174 DBG("add %s", session->session_path);
1176 if (g_dbus_register_interface(connection, session->session_path,
1177 CONNMAN_SESSION_INTERFACE,
1178 session_methods, NULL,
1179 NULL, session, NULL) == FALSE) {
1180 connman_error("Failed to register %s", session->session_path);
1181 g_hash_table_remove(session_hash, session->session_path);
1188 g_dbus_send_reply(connection, msg,
1189 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1193 update_allowed_bearers(session);
1195 if (info->ecall == TRUE) {
1196 ecall_session = session;
1197 update_ecall_sessions(session);
1200 info_last->bearer = info->bearer;
1201 info_last->online = info->online;
1202 info_last->priority = 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;
1212 update_info(info_last);
1214 session->info_dirty = TRUE;
1215 session->append_all = TRUE;
1217 g_timeout_add_seconds(0, session_cb, session);
1222 connman_error("Failed to create session");
1223 g_free(session_path);
1225 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1226 g_slist_free(allowed_bearers);
1231 int __connman_session_destroy(DBusMessage *msg)
1233 const char *owner, *session_path;
1234 struct connman_session *session;
1236 owner = dbus_message_get_sender(msg);
1238 DBG("owner %s", owner);
1240 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1242 if (session_path == NULL)
1245 session = g_hash_table_lookup(session_hash, session_path);
1246 if (session == NULL)
1249 if (g_strcmp0(owner, session->owner) != 0)
1252 session_disconnect(session);
1257 connman_bool_t __connman_session_mode()
1262 void __connman_session_set_mode(connman_bool_t enable)
1264 DBG("enable %d", enable);
1266 if (sessionmode == enable)
1269 sessionmode = enable;
1271 if (sessionmode == TRUE)
1272 __connman_service_disconnect_all();
1275 static void service_add(struct connman_service *service)
1277 GHashTableIter iter;
1278 gpointer key, value;
1279 struct connman_session *session;
1281 DBG("service %p", service);
1283 g_hash_table_iter_init(&iter, session_hash);
1285 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1288 if (service_match(session, service) == FALSE)
1291 g_sequence_insert_sorted(session->service_list, service,
1292 sort_services, session);
1294 session_cb(session);
1298 static int service_remove_from_session(struct connman_session *session,
1299 struct connman_service *service)
1302 GSequenceIter *iter;
1304 iter = lookup_service(session, service);
1308 session->info.online = FALSE;
1309 g_sequence_remove(iter);
1314 static void service_remove(struct connman_service *service)
1317 GHashTableIter iter;
1318 gpointer key, value;
1319 struct connman_session *session;
1320 struct session_info *info;
1322 DBG("service %p", service);
1324 g_hash_table_iter_init(&iter, session_hash);
1326 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1328 info = &session->info;
1330 if (service_remove_from_session(session, service) != 0)
1333 info->service = NULL;
1334 session_cb(session);
1338 static void service_state_changed(struct connman_service *service,
1339 enum connman_service_state state)
1341 GHashTableIter iter;
1342 gpointer key, value;
1343 struct connman_session *session;
1344 struct session_info *info;
1345 connman_bool_t online;
1347 DBG("service %p state %d", service, state);
1349 g_hash_table_iter_init(&iter, session_hash);
1351 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1353 info = &session->info;
1355 if (info->service == service) {
1356 online = __connman_service_is_connected(service);
1357 if (info->online == online)
1360 info->online = online;
1361 session->info_dirty = TRUE;
1362 session_cb(session);
1367 static void ipconfig_changed(struct connman_service *service,
1368 struct connman_ipconfig *ipconfig)
1370 GHashTableIter iter;
1371 gpointer key, value;
1372 struct connman_session *session;
1373 struct session_info *info;
1374 enum connman_ipconfig_type type;
1376 DBG("service %p ipconfig %p", service, ipconfig);
1378 type = __connman_ipconfig_get_config_type(ipconfig);
1380 g_hash_table_iter_init(&iter, session_hash);
1382 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1384 info = &session->info;
1386 if (info->service == service) {
1387 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1388 ipconfig_ipv4_changed(session);
1389 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1390 ipconfig_ipv6_changed(session);
1395 static struct connman_notifier session_notifier = {
1397 .service_add = service_add,
1398 .service_remove = service_remove,
1399 .service_state_changed = service_state_changed,
1400 .ipconfig_changed = ipconfig_changed,
1403 int __connman_session_init(void)
1409 connection = connman_dbus_get_connection();
1410 if (connection == NULL)
1413 err = connman_notifier_register(&session_notifier);
1415 dbus_connection_unref(connection);
1419 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1420 NULL, cleanup_session);
1422 sessionmode = FALSE;
1426 void __connman_session_cleanup(void)
1430 if (connection == NULL)
1433 connman_notifier_unregister(&session_notifier);
1435 g_hash_table_foreach(session_hash, release_session, NULL);
1436 g_hash_table_destroy(session_hash);
1438 dbus_connection_unref(connection);