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_trigger {
37 CONNMAN_SESSION_TRIGGER_UNKNOWN = 0,
38 CONNMAN_SESSION_TRIGGER_SETTING = 1,
39 CONNMAN_SESSION_TRIGGER_CONNECT = 2,
40 CONNMAN_SESSION_TRIGGER_DISCONNECT = 3,
41 CONNMAN_SESSION_TRIGGER_PERIODIC = 4,
42 CONNMAN_SESSION_TRIGGER_SERVICE = 5,
43 CONNMAN_SESSION_TRIGGER_ECALL = 6,
46 enum connman_session_roaming_policy {
47 CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN = 0,
48 CONNMAN_SESSION_ROAMING_POLICY_DEFAULT = 1,
49 CONNMAN_SESSION_ROAMING_POLICY_ALWAYS = 2,
50 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN = 3,
51 CONNMAN_SESSION_ROAMING_POLICY_NATIONAL = 4,
52 CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL = 5,
59 connman_bool_t online;
60 connman_bool_t priority;
61 GSList *allowed_bearers;
62 connman_bool_t avoid_handover;
63 connman_bool_t stay_connected;
64 unsigned int periodic_connect;
65 unsigned int idle_timeout;
67 enum connman_session_roaming_policy roaming_policy;
70 struct connman_service *service;
73 struct connman_session {
79 connman_bool_t append_all;
80 connman_bool_t info_dirty;
81 struct session_info info;
82 struct session_info info_last;
84 GSequence *service_list;
89 connman_bool_t match_all;
90 enum connman_service_type service_type;
93 static const char *trigger2string(enum connman_session_trigger trigger)
96 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
98 case CONNMAN_SESSION_TRIGGER_SETTING:
100 case CONNMAN_SESSION_TRIGGER_CONNECT:
102 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
104 case CONNMAN_SESSION_TRIGGER_PERIODIC:
106 case CONNMAN_SESSION_TRIGGER_SERVICE:
108 case CONNMAN_SESSION_TRIGGER_ECALL:
115 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
118 case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
120 case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
122 case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
124 case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
126 case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
128 case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
129 return "international";
135 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
137 if (g_strcmp0(policy, "default") == 0)
138 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
139 else if (g_strcmp0(policy, "always") == 0)
140 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
141 else if (g_strcmp0(policy, "forbidden") == 0)
142 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
143 else if (g_strcmp0(policy, "national") == 0)
144 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
145 else if (g_strcmp0(policy, "international") == 0)
146 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
148 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
151 static enum connman_service_type bearer2service(const char *bearer)
154 return CONNMAN_SERVICE_TYPE_UNKNOWN;
156 if (g_strcmp0(bearer, "ethernet") == 0)
157 return CONNMAN_SERVICE_TYPE_ETHERNET;
158 else if (g_strcmp0(bearer, "wifi") == 0)
159 return CONNMAN_SERVICE_TYPE_WIFI;
160 else if (g_strcmp0(bearer, "wimax") == 0)
161 return CONNMAN_SERVICE_TYPE_WIMAX;
162 else if (g_strcmp0(bearer, "bluetooth") == 0)
163 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
164 else if (g_strcmp0(bearer, "3g") == 0)
165 return CONNMAN_SERVICE_TYPE_CELLULAR;
167 return CONNMAN_SERVICE_TYPE_UNKNOWN;
170 static char *service2bearer(enum connman_service_type type)
173 case CONNMAN_SERVICE_TYPE_ETHERNET:
175 case CONNMAN_SERVICE_TYPE_WIFI:
177 case CONNMAN_SERVICE_TYPE_WIMAX:
179 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
181 case CONNMAN_SERVICE_TYPE_CELLULAR:
183 case CONNMAN_SERVICE_TYPE_UNKNOWN:
184 case CONNMAN_SERVICE_TYPE_SYSTEM:
185 case CONNMAN_SERVICE_TYPE_GPS:
186 case CONNMAN_SERVICE_TYPE_VPN:
187 case CONNMAN_SERVICE_TYPE_GADGET:
194 static void cleanup_bearer_info(gpointer data, gpointer user_data)
196 struct bearer_info *info = data;
202 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
204 struct bearer_info *info;
205 DBusMessageIter array;
208 dbus_message_iter_recurse(iter, &array);
210 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
213 dbus_message_iter_get_basic(&array, &bearer);
215 info = g_try_new0(struct bearer_info, 1);
217 g_slist_foreach(list, cleanup_bearer_info, NULL);
223 info->name = g_strdup(bearer);
224 info->service_type = bearer2service(info->name);
226 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
227 g_strcmp0(info->name, "*") == 0) {
228 info->match_all = TRUE;
230 info->match_all = FALSE;
233 list = g_slist_append(list, info);
235 dbus_message_iter_next(&array);
241 static GSList *session_allowed_bearers_any(void)
243 struct bearer_info *info;
246 info = g_try_new0(struct bearer_info, 1);
253 info->name = g_strdup("");
254 info->match_all = TRUE;
255 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
257 list = g_slist_append(list, info);
262 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
264 struct session_info *info = user_data;
267 for (list = info->allowed_bearers;
268 list != NULL; list = list->next) {
269 struct bearer_info *info = list->data;
271 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
276 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
278 struct connman_service *service = user_data;
279 struct connman_ipconfig *ipconfig_ipv4;
284 ipconfig_ipv4 = __connman_service_get_ip4config(service);
285 if (ipconfig_ipv4 == NULL)
288 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
291 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
293 struct connman_service *service = user_data;
294 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
299 ipconfig_ipv4 = __connman_service_get_ip4config(service);
300 ipconfig_ipv6 = __connman_service_get_ip6config(service);
301 if (ipconfig_ipv6 == NULL)
304 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
307 static void append_notify(DBusMessageIter *dict,
308 struct connman_session *session)
310 struct session_info *info = &session->info;
311 struct session_info *info_last = &session->info_last;
314 if (session->append_all == TRUE ||
315 info->bearer != info_last->bearer) {
316 connman_dbus_dict_append_basic(dict, "Bearer",
319 info_last->bearer = info->bearer;
322 if (session->append_all == TRUE ||
323 info->online != info_last->online) {
324 connman_dbus_dict_append_basic(dict, "Online",
327 info_last->online = info->online;
330 if (session->append_all == TRUE ||
331 info->name != info_last->name) {
332 connman_dbus_dict_append_basic(dict, "Name",
335 info_last->name = info->name;
338 if (session->append_all == TRUE ||
339 info->service != info_last->service) {
340 connman_dbus_dict_append_dict(dict, "IPv4",
341 append_ipconfig_ipv4,
344 connman_dbus_dict_append_dict(dict, "IPv6",
345 append_ipconfig_ipv6,
348 connman_dbus_dict_append_basic(dict, "Interface",
352 info_last->ifname = info->ifname;
353 info_last->service = info->service;
357 if (session->append_all == TRUE ||
358 info->priority != info_last->priority) {
359 connman_dbus_dict_append_basic(dict, "Priority",
362 info_last->priority = info->priority;
365 if (session->append_all == TRUE ||
366 info->allowed_bearers != info_last->allowed_bearers) {
367 connman_dbus_dict_append_array(dict, "AllowedBearers",
369 append_allowed_bearers,
371 info_last->allowed_bearers = info->allowed_bearers;
374 if (session->append_all == TRUE ||
375 info->avoid_handover != info_last->avoid_handover) {
376 connman_dbus_dict_append_basic(dict, "AvoidHandover",
378 &info->avoid_handover);
379 info_last->avoid_handover = info->avoid_handover;
382 if (session->append_all == TRUE ||
383 info->stay_connected != info_last->stay_connected) {
384 connman_dbus_dict_append_basic(dict, "StayConnected",
386 &info->stay_connected);
387 info_last->stay_connected = info->stay_connected;
390 if (session->append_all == TRUE ||
391 info->periodic_connect != info_last->periodic_connect) {
392 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
394 &info->periodic_connect);
395 info_last->periodic_connect = info->periodic_connect;
398 if (session->append_all == TRUE ||
399 info->idle_timeout != info_last->idle_timeout) {
400 connman_dbus_dict_append_basic(dict, "IdleTimeout",
402 &info->idle_timeout);
403 info_last->idle_timeout = info->idle_timeout;
406 if (session->append_all == TRUE ||
407 info->ecall != info_last->ecall) {
408 connman_dbus_dict_append_basic(dict, "EmergencyCall",
411 info_last->ecall = info->ecall;
414 if (session->append_all == TRUE ||
415 info->roaming_policy != info_last->roaming_policy) {
416 policy = roamingpolicy2string(info->roaming_policy);
417 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
420 info_last->roaming_policy = info->roaming_policy;
423 if (session->append_all == TRUE ||
424 info->marker != info_last->marker) {
425 connman_dbus_dict_append_basic(dict, "SessionMarker",
428 info_last->marker = info->marker;
431 session->append_all = FALSE;
432 session->info_dirty = FALSE;
435 static gboolean session_notify(gpointer user_data)
437 struct connman_session *session = user_data;
439 DBusMessageIter array, dict;
441 if (session->info_dirty == FALSE)
444 DBG("session %p owner %s notify_path %s", session,
445 session->owner, session->notify_path);
447 msg = dbus_message_new_method_call(session->owner, session->notify_path,
448 CONNMAN_NOTIFICATION_INTERFACE,
453 dbus_message_iter_init_append(msg, &array);
454 connman_dbus_dict_open(&array, &dict);
456 append_notify(&dict, session);
458 connman_dbus_dict_close(&array, &dict);
460 g_dbus_send_message(connection, msg);
462 session->info_dirty = FALSE;
467 static void ipconfig_ipv4_changed(struct connman_session *session)
469 struct session_info *info = &session->info;
471 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
472 "IPv4", append_ipconfig_ipv4,
476 static void ipconfig_ipv6_changed(struct connman_session *session)
478 struct session_info *info = &session->info;
480 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
481 "IPv6", append_ipconfig_ipv6,
485 static GSequenceIter *lookup_service(struct connman_session *session,
486 struct connman_service *service)
493 iter = g_sequence_get_begin_iter(session->service_list);
495 while (g_sequence_iter_is_end(iter) == FALSE) {
496 struct connman_service *service_iter = g_sequence_get(iter);
498 if (service_iter == service)
501 iter = g_sequence_iter_next(iter);
507 static connman_bool_t service_type_match(struct connman_session *session,
508 struct connman_service *service)
510 struct session_info *info = &session->info;
513 for (list = info->allowed_bearers;
514 list != NULL; list = list->next) {
515 struct bearer_info *info = list->data;
516 enum connman_service_type service_type;
518 if (info->match_all == TRUE)
521 service_type = connman_service_get_type(service);
522 if (info->service_type == service_type)
529 static connman_bool_t service_match(struct connman_session *session,
530 struct connman_service *service)
532 if (service_type_match(session, service) == FALSE)
538 static int service_type_weight(enum connman_service_type type)
541 * The session doesn't care which service
542 * to use. Nevertheless we have to sort them
543 * according their type. The ordering is
552 case CONNMAN_SERVICE_TYPE_ETHERNET:
554 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
556 case CONNMAN_SERVICE_TYPE_WIFI:
557 case CONNMAN_SERVICE_TYPE_WIMAX:
559 case CONNMAN_SERVICE_TYPE_CELLULAR:
561 case CONNMAN_SERVICE_TYPE_UNKNOWN:
562 case CONNMAN_SERVICE_TYPE_SYSTEM:
563 case CONNMAN_SERVICE_TYPE_GPS:
564 case CONNMAN_SERVICE_TYPE_VPN:
565 case CONNMAN_SERVICE_TYPE_GADGET:
572 static gint sort_allowed_bearers(struct connman_service *service_a,
573 struct connman_service *service_b,
574 struct connman_session *session)
576 struct session_info *info = &session->info;
578 enum connman_service_type type_a, type_b;
579 int weight_a, weight_b;
581 type_a = connman_service_get_type(service_a);
582 type_b = connman_service_get_type(service_b);
584 for (list = info->allowed_bearers;
585 list != NULL; list = list->next) {
586 struct bearer_info *info = list->data;
588 if (info->match_all == TRUE) {
589 if (type_a != type_b) {
590 weight_a = service_type_weight(type_a);
591 weight_b = service_type_weight(type_b);
593 if (weight_a > weight_b)
596 if (weight_a < weight_b)
603 if (type_a == info->service_type &&
604 type_b == info->service_type) {
608 if (type_a == info->service_type &&
609 type_b != info->service_type) {
613 if (type_a != info->service_type &&
614 type_b == info->service_type) {
622 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
624 struct connman_service *service_a = (void *)a;
625 struct connman_service *service_b = (void *)b;
626 struct connman_session *session = user_data;
628 return sort_allowed_bearers(service_a, service_b, session);
631 static void cleanup_session(gpointer user_data)
633 struct connman_session *session = user_data;
634 struct session_info *info = &session->info;
636 DBG("remove %s", session->session_path);
638 g_sequence_free(session->service_list);
640 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
641 g_slist_free(info->allowed_bearers);
643 g_free(session->owner);
644 g_free(session->session_path);
645 g_free(session->notify_path);
650 static void release_session(gpointer key, gpointer value, gpointer user_data)
652 struct connman_session *session = value;
653 DBusMessage *message;
655 DBG("owner %s path %s", session->owner, session->notify_path);
657 if (session->notify_watch > 0)
658 g_dbus_remove_watch(connection, session->notify_watch);
660 g_dbus_unregister_interface(connection, session->session_path,
661 CONNMAN_SESSION_INTERFACE);
663 message = dbus_message_new_method_call(session->owner,
664 session->notify_path,
665 CONNMAN_NOTIFICATION_INTERFACE,
670 dbus_message_set_no_reply(message, TRUE);
672 g_dbus_send_message(connection, message);
675 static int session_disconnect(struct connman_session *session)
677 DBG("session %p, %s", session, session->owner);
679 if (session->notify_watch > 0)
680 g_dbus_remove_watch(connection, session->notify_watch);
682 g_dbus_unregister_interface(connection, session->session_path,
683 CONNMAN_SESSION_INTERFACE);
685 g_hash_table_remove(session_hash, session->session_path);
690 static void owner_disconnect(DBusConnection *conn, void *user_data)
692 struct connman_session *session = user_data;
694 DBG("session %p, %s died", session, session->owner);
696 session_disconnect(session);
699 static DBusMessage *destroy_session(DBusConnection *conn,
700 DBusMessage *msg, void *user_data)
702 struct connman_session *session = user_data;
704 DBG("session %p", session);
706 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
709 static void update_info(struct session_info *info)
711 enum connman_service_type type;
714 if (info->service != NULL) {
715 type = connman_service_get_type(info->service);
716 info->bearer = service2bearer(type);
718 info->online = __connman_service_is_connected(info->service);
719 info->name = __connman_service_get_name(info->service);
720 if (info->name == NULL)
723 idx = __connman_service_get_index(info->service);
724 info->ifname = connman_inet_ifname(idx);
725 if (info->ifname == NULL)
729 info->online = FALSE;
735 static void notify_service_changes(struct connman_session *session)
737 struct session_info *info = &session->info;
738 struct session_info *info_last = &session->info_last;
740 if (info->service == info_last->service)
744 session->info_dirty = TRUE;
747 static void select_and_connect(struct connman_session *session,
748 connman_bool_t do_connect)
750 struct session_info *info = &session->info;
751 struct connman_service *service = NULL;
754 DBG("session %p connect %d", session, do_connect);
756 iter = g_sequence_get_begin_iter(session->service_list);
758 while (g_sequence_iter_is_end(iter) == FALSE) {
759 service = g_sequence_get(iter);
761 if (__connman_service_is_connecting(service) == TRUE ||
762 __connman_service_is_connected(service) == TRUE) {
766 if (__connman_service_is_idle(service) == TRUE &&
767 do_connect == TRUE) {
773 iter = g_sequence_iter_next(iter);
776 if (info->service != NULL && info->service != service) {
777 __connman_service_disconnect(info->service);
778 info->service = NULL;
781 if (service != NULL) {
782 info->service = service;
784 if (do_connect == TRUE)
785 __connman_service_connect(info->service);
788 notify_service_changes(session);
791 static void session_changed(struct connman_session *session,
792 enum connman_session_trigger trigger)
794 struct session_info *info = &session->info;
798 * TODO: This only a placeholder for the 'real' algorithm to
799 * play a bit around. So we are going to improve it step by step.
802 DBG("session %p trigger %s", session, trigger2string(trigger));
805 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
806 DBG("ignore session changed event");
808 case CONNMAN_SESSION_TRIGGER_SETTING:
809 if (info->service != NULL) {
810 iter = lookup_service(session, info->service);
813 * This service is not part of this
817 __connman_service_disconnect(info->service);
818 info->service = NULL;
821 notify_service_changes(session);
824 /* Try to free ride */
825 if (info->online == FALSE)
826 select_and_connect(session, FALSE);
829 case CONNMAN_SESSION_TRIGGER_CONNECT:
830 if (info->online == TRUE)
833 select_and_connect(session, TRUE);
836 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
837 if (info->online == FALSE)
840 if (info->service != NULL)
841 __connman_service_disconnect(info->service);
843 info->service = NULL;
844 notify_service_changes(session);
847 case CONNMAN_SESSION_TRIGGER_PERIODIC:
848 select_and_connect(session, TRUE);
851 case CONNMAN_SESSION_TRIGGER_SERVICE:
852 if (info->online == TRUE)
855 if (info->stay_connected == TRUE) {
856 DBG("StayConnected");
857 select_and_connect(session, TRUE);
862 /* Try to free ride */
863 select_and_connect(session, FALSE);
866 case CONNMAN_SESSION_TRIGGER_ECALL:
867 if (info->online == FALSE && info->service != NULL)
868 info->service = NULL;
870 notify_service_changes(session);
876 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
878 case CONNMAN_SESSION_TRIGGER_CONNECT:
879 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
880 case CONNMAN_SESSION_TRIGGER_ECALL:
881 g_timeout_add_seconds(0, session_notify, session);
883 case CONNMAN_SESSION_TRIGGER_SETTING:
884 case CONNMAN_SESSION_TRIGGER_PERIODIC:
885 case CONNMAN_SESSION_TRIGGER_SERVICE:
886 session_notify(session);
891 static DBusMessage *connect_session(DBusConnection *conn,
892 DBusMessage *msg, void *user_data)
894 struct connman_session *session = user_data;
896 DBG("session %p", session);
898 if (ecall_session != NULL && ecall_session != session)
899 return __connman_error_failed(msg, EBUSY);
901 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
903 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
906 static DBusMessage *disconnect_session(DBusConnection *conn,
907 DBusMessage *msg, void *user_data)
909 struct connman_session *session = user_data;
911 DBG("session %p", session);
913 if (ecall_session != NULL && ecall_session != session)
914 return __connman_error_failed(msg, EBUSY);
916 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
918 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
921 static void print_name(gpointer data, gpointer user_data)
923 struct connman_service *service = data;
925 DBG("service %p type %s name %s", service,
926 service2bearer(connman_service_get_type(service)),
927 __connman_service_get_name(service));
930 static void update_allowed_bearers(struct connman_session *session)
932 if (session->service_list != NULL)
933 g_sequence_free(session->service_list);
935 session->service_list = __connman_service_get_list(session,
937 g_sequence_sort(session->service_list, sort_services, session);
938 g_sequence_foreach(session->service_list, print_name, NULL);
940 session->info_dirty = TRUE;
943 static void update_ecall_sessions(struct connman_session *session)
945 struct session_info *info = &session->info;
946 struct connman_session *session_iter;
950 g_hash_table_iter_init(&iter, session_hash);
952 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
953 session_iter = value;
955 if (session_iter == session)
958 session_iter->info.ecall = info->ecall;
959 session_iter->info_dirty = TRUE;
961 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
965 static void update_ecall(struct connman_session *session)
967 struct session_info *info = &session->info;
968 struct session_info *info_last = &session->info_last;
970 DBG("session %p ecall_session %p ecall %d -> %d", session,
971 ecall_session, info_last->ecall, info->ecall);
973 if (ecall_session == NULL) {
974 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
977 ecall_session = session;
978 } else if (ecall_session == session) {
979 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
982 ecall_session = NULL;
987 update_ecall_sessions(session);
989 session->info_dirty = TRUE;
993 /* not a valid transition */
994 info->ecall = info_last->ecall;
997 static DBusMessage *change_session(DBusConnection *conn,
998 DBusMessage *msg, void *user_data)
1000 struct connman_session *session = user_data;
1001 struct session_info *info = &session->info;
1002 struct session_info *info_last = &session->info_last;
1003 DBusMessageIter iter, value;
1005 GSList *allowed_bearers;
1007 DBG("session %p", session);
1008 if (dbus_message_iter_init(msg, &iter) == FALSE)
1009 return __connman_error_invalid_arguments(msg);
1011 dbus_message_iter_get_basic(&iter, &name);
1012 dbus_message_iter_next(&iter);
1013 dbus_message_iter_recurse(&iter, &value);
1015 switch (dbus_message_iter_get_arg_type(&value)) {
1016 case DBUS_TYPE_ARRAY:
1017 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1018 allowed_bearers = session_parse_allowed_bearers(&value);
1020 g_slist_foreach(info->allowed_bearers,
1021 cleanup_bearer_info, NULL);
1022 g_slist_free(info->allowed_bearers);
1024 if (allowed_bearers == NULL) {
1025 allowed_bearers = session_allowed_bearers_any();
1027 if (allowed_bearers == NULL)
1028 return __connman_error_failed(msg, ENOMEM);
1031 info->allowed_bearers = allowed_bearers;
1033 update_allowed_bearers(session);
1038 case DBUS_TYPE_BOOLEAN:
1039 if (g_str_equal(name, "Priority") == TRUE) {
1040 dbus_message_iter_get_basic(&value,
1043 if (info_last->priority != info->priority)
1044 session->info_dirty = TRUE;
1045 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1046 dbus_message_iter_get_basic(&value,
1047 &info->avoid_handover);
1049 if (info_last->avoid_handover != info->avoid_handover)
1050 session->info_dirty = TRUE;
1051 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1052 dbus_message_iter_get_basic(&value,
1053 &info->stay_connected);
1055 if (info_last->stay_connected != info->stay_connected)
1056 session->info_dirty = TRUE;
1057 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1058 dbus_message_iter_get_basic(&value,
1061 update_ecall(session);
1066 case DBUS_TYPE_UINT32:
1067 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1068 dbus_message_iter_get_basic(&value,
1069 &info->periodic_connect);
1071 if (info_last->periodic_connect != info->periodic_connect)
1072 session->info_dirty = TRUE;
1073 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1074 dbus_message_iter_get_basic(&value,
1075 &info->idle_timeout);
1077 if (info_last->idle_timeout != info->idle_timeout)
1078 session->info_dirty = TRUE;
1083 case DBUS_TYPE_STRING:
1084 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1086 dbus_message_iter_get_basic(&value, &val);
1087 info->roaming_policy =
1088 string2roamingpolicy(val);
1090 if (info_last->roaming_policy != info->roaming_policy)
1091 session->info_dirty = TRUE;
1100 if (session->info_dirty == TRUE)
1101 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1103 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1106 return __connman_error_invalid_arguments(msg);
1109 static GDBusMethodTable session_methods[] = {
1110 { "Destroy", "", "", destroy_session },
1111 { "Connect", "", "", connect_session },
1112 { "Disconnect", "", "", disconnect_session },
1113 { "Change", "sv", "", change_session },
1117 int __connman_session_create(DBusMessage *msg)
1119 const char *owner, *notify_path;
1120 char *session_path = NULL;
1121 DBusMessageIter iter, array;
1122 struct connman_session *session;
1123 struct session_info *info, *info_last;
1125 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1126 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1127 enum connman_session_roaming_policy roaming_policy =
1128 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1129 GSList *allowed_bearers = NULL;
1130 unsigned int periodic_connect = 0;
1131 unsigned int idle_timeout = 0;
1135 owner = dbus_message_get_sender(msg);
1137 DBG("owner %s", owner);
1139 if (ecall_session != NULL) {
1141 * If there is an emergency call already going on,
1142 * ignore session creation attempt
1148 dbus_message_iter_init(msg, &iter);
1149 dbus_message_iter_recurse(&iter, &array);
1151 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1152 DBusMessageIter entry, value;
1153 const char *key, *val;
1155 dbus_message_iter_recurse(&array, &entry);
1156 dbus_message_iter_get_basic(&entry, &key);
1158 dbus_message_iter_next(&entry);
1159 dbus_message_iter_recurse(&entry, &value);
1161 switch (dbus_message_iter_get_arg_type(&value)) {
1162 case DBUS_TYPE_ARRAY:
1163 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1165 session_parse_allowed_bearers(&value);
1170 case DBUS_TYPE_BOOLEAN:
1171 if (g_str_equal(key, "Priority") == TRUE) {
1172 dbus_message_iter_get_basic(&value,
1174 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1175 dbus_message_iter_get_basic(&value,
1177 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1178 dbus_message_iter_get_basic(&value,
1180 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1181 dbus_message_iter_get_basic(&value,
1187 case DBUS_TYPE_UINT32:
1188 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1189 dbus_message_iter_get_basic(&value,
1191 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1192 dbus_message_iter_get_basic(&value,
1198 case DBUS_TYPE_STRING:
1199 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1200 dbus_message_iter_get_basic(&value, &val);
1201 roaming_policy = string2roamingpolicy(val);
1206 dbus_message_iter_next(&array);
1209 dbus_message_iter_next(&iter);
1210 dbus_message_iter_get_basic(&iter, ¬ify_path);
1212 if (notify_path == NULL) {
1217 session_path = g_strdup_printf("/sessions%s", notify_path);
1218 if (session_path == NULL) {
1223 session = g_hash_table_lookup(session_hash, session_path);
1224 if (session != NULL) {
1229 session = g_try_new0(struct connman_session, 1);
1230 if (session == NULL) {
1235 info = &session->info;
1236 info_last = &session->info_last;
1238 session->owner = g_strdup(owner);
1239 session->session_path = session_path;
1240 session->notify_path = g_strdup(notify_path);
1241 session->notify_watch =
1242 g_dbus_add_disconnect_watch(connection, session->owner,
1243 owner_disconnect, session, NULL);
1246 info->online = FALSE;
1247 info->priority = priority;
1248 info->avoid_handover = avoid_handover;
1249 info->stay_connected = stay_connected;
1250 info->periodic_connect = periodic_connect;
1251 info->idle_timeout = idle_timeout;
1252 info->ecall = ecall;
1253 info->roaming_policy = roaming_policy;
1254 info->service = NULL;
1257 if (allowed_bearers == NULL) {
1258 info->allowed_bearers =
1259 session_allowed_bearers_any();
1261 if (info->allowed_bearers == NULL) {
1266 info->allowed_bearers = allowed_bearers;
1269 g_hash_table_replace(session_hash, session->session_path, session);
1271 DBG("add %s", session->session_path);
1273 if (g_dbus_register_interface(connection, session->session_path,
1274 CONNMAN_SESSION_INTERFACE,
1275 session_methods, NULL,
1276 NULL, session, NULL) == FALSE) {
1277 connman_error("Failed to register %s", session->session_path);
1278 g_hash_table_remove(session_hash, session->session_path);
1285 g_dbus_send_reply(connection, msg,
1286 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1290 update_allowed_bearers(session);
1292 if (info->ecall == TRUE) {
1293 ecall_session = session;
1294 update_ecall_sessions(session);
1297 info_last->bearer = info->bearer;
1298 info_last->online = info->online;
1299 info_last->priority = info->priority;
1300 info_last->avoid_handover = info->avoid_handover;
1301 info_last->stay_connected = info->stay_connected;
1302 info_last->periodic_connect = info->periodic_connect;
1303 info_last->idle_timeout = info->idle_timeout;
1304 info_last->ecall = info->ecall;
1305 info_last->roaming_policy = info->roaming_policy;
1306 info_last->service = info->service;
1307 info_last->marker = info->marker;
1308 info_last->allowed_bearers = info->allowed_bearers;
1309 update_info(info_last);
1311 session->info_dirty = TRUE;
1312 session->append_all = TRUE;
1314 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1319 connman_error("Failed to create session");
1320 g_free(session_path);
1322 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1323 g_slist_free(allowed_bearers);
1328 int __connman_session_destroy(DBusMessage *msg)
1330 const char *owner, *session_path;
1331 struct connman_session *session;
1333 owner = dbus_message_get_sender(msg);
1335 DBG("owner %s", owner);
1337 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1339 if (session_path == NULL)
1342 session = g_hash_table_lookup(session_hash, session_path);
1343 if (session == NULL)
1346 if (g_strcmp0(owner, session->owner) != 0)
1349 session_disconnect(session);
1354 connman_bool_t __connman_session_mode()
1359 void __connman_session_set_mode(connman_bool_t enable)
1361 DBG("enable %d", enable);
1363 if (sessionmode == enable)
1366 sessionmode = enable;
1368 if (sessionmode == TRUE)
1369 __connman_service_disconnect_all();
1372 static void service_add(struct connman_service *service)
1374 GHashTableIter iter;
1375 gpointer key, value;
1376 struct connman_session *session;
1378 DBG("service %p", service);
1380 g_hash_table_iter_init(&iter, session_hash);
1382 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1385 if (service_match(session, service) == FALSE)
1388 g_sequence_insert_sorted(session->service_list, service,
1389 sort_services, session);
1391 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1395 static int service_remove_from_session(struct connman_session *session,
1396 struct connman_service *service)
1398 GSequenceIter *iter;
1400 iter = lookup_service(session, service);
1404 session->info.online = FALSE;
1405 g_sequence_remove(iter);
1410 static void service_remove(struct connman_service *service)
1413 GHashTableIter iter;
1414 gpointer key, value;
1415 struct connman_session *session;
1416 struct session_info *info;
1418 DBG("service %p", service);
1420 g_hash_table_iter_init(&iter, session_hash);
1422 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1424 info = &session->info;
1426 if (service_remove_from_session(session, service) != 0)
1429 info->service = NULL;
1430 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1434 static void service_state_changed(struct connman_service *service,
1435 enum connman_service_state state)
1437 GHashTableIter iter;
1438 gpointer key, value;
1439 struct connman_session *session;
1440 struct session_info *info;
1441 connman_bool_t online;
1443 DBG("service %p state %d", service, state);
1445 g_hash_table_iter_init(&iter, session_hash);
1447 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1449 info = &session->info;
1451 if (info->service == service) {
1452 online = __connman_service_is_connected(service);
1453 if (info->online == online)
1456 info->online = online;
1457 session->info_dirty = TRUE;
1458 session_changed(session,
1459 CONNMAN_SESSION_TRIGGER_SERVICE);
1464 static void ipconfig_changed(struct connman_service *service,
1465 struct connman_ipconfig *ipconfig)
1467 GHashTableIter iter;
1468 gpointer key, value;
1469 struct connman_session *session;
1470 struct session_info *info;
1471 enum connman_ipconfig_type type;
1473 DBG("service %p ipconfig %p", service, ipconfig);
1475 type = __connman_ipconfig_get_config_type(ipconfig);
1477 g_hash_table_iter_init(&iter, session_hash);
1479 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1481 info = &session->info;
1483 if (info->service == service) {
1484 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1485 ipconfig_ipv4_changed(session);
1486 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1487 ipconfig_ipv6_changed(session);
1492 static struct connman_notifier session_notifier = {
1494 .service_add = service_add,
1495 .service_remove = service_remove,
1496 .service_state_changed = service_state_changed,
1497 .ipconfig_changed = ipconfig_changed,
1500 int __connman_session_init(void)
1506 connection = connman_dbus_get_connection();
1507 if (connection == NULL)
1510 err = connman_notifier_register(&session_notifier);
1512 dbus_connection_unref(connection);
1516 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1517 NULL, cleanup_session);
1519 sessionmode = FALSE;
1523 void __connman_session_cleanup(void)
1527 if (connection == NULL)
1530 connman_notifier_unregister(&session_notifier);
1532 g_hash_table_foreach(session_hash, release_session, NULL);
1533 g_hash_table_destroy(session_hash);
1535 dbus_connection_unref(connection);