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_reason {
47 CONNMAN_SESSION_REASON_UNKNOWN = 0,
48 CONNMAN_SESSION_REASON_CONNECT = 1,
49 CONNMAN_SESSION_REASON_FREE_RIDE = 2,
50 CONNMAN_SESSION_REASON_PERIODIC = 3,
53 enum connman_session_roaming_policy {
54 CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN = 0,
55 CONNMAN_SESSION_ROAMING_POLICY_DEFAULT = 1,
56 CONNMAN_SESSION_ROAMING_POLICY_ALWAYS = 2,
57 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN = 3,
58 CONNMAN_SESSION_ROAMING_POLICY_NATIONAL = 4,
59 CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL = 5,
62 struct service_entry {
63 /* track why this service was selected */
64 enum connman_session_reason reason;
65 enum connman_service_state state;
67 struct connman_service *service;
73 connman_bool_t online;
74 connman_bool_t priority;
75 GSList *allowed_bearers;
76 connman_bool_t avoid_handover;
77 connman_bool_t stay_connected;
78 unsigned int periodic_connect;
79 unsigned int idle_timeout;
81 enum connman_session_roaming_policy roaming_policy;
84 struct service_entry *entry;
87 struct connman_session {
93 connman_bool_t append_all;
94 connman_bool_t info_dirty;
95 struct session_info *info;
96 struct session_info *info_last;
98 GSequence *service_list;
99 GHashTable *service_hash;
104 connman_bool_t match_all;
105 enum connman_service_type service_type;
108 static const char *trigger2string(enum connman_session_trigger trigger)
111 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
113 case CONNMAN_SESSION_TRIGGER_SETTING:
115 case CONNMAN_SESSION_TRIGGER_CONNECT:
117 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
119 case CONNMAN_SESSION_TRIGGER_PERIODIC:
121 case CONNMAN_SESSION_TRIGGER_SERVICE:
123 case CONNMAN_SESSION_TRIGGER_ECALL:
130 static const char *reason2string(enum connman_session_reason reason)
133 case CONNMAN_SESSION_REASON_UNKNOWN:
135 case CONNMAN_SESSION_REASON_CONNECT:
137 case CONNMAN_SESSION_REASON_FREE_RIDE:
139 case CONNMAN_SESSION_REASON_PERIODIC:
146 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
149 case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
151 case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
153 case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
155 case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
157 case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
159 case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
160 return "international";
166 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
168 if (g_strcmp0(policy, "default") == 0)
169 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
170 else if (g_strcmp0(policy, "always") == 0)
171 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
172 else if (g_strcmp0(policy, "forbidden") == 0)
173 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
174 else if (g_strcmp0(policy, "national") == 0)
175 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
176 else if (g_strcmp0(policy, "international") == 0)
177 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
179 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
182 static enum connman_service_type bearer2service(const char *bearer)
185 return CONNMAN_SERVICE_TYPE_UNKNOWN;
187 if (g_strcmp0(bearer, "ethernet") == 0)
188 return CONNMAN_SERVICE_TYPE_ETHERNET;
189 else if (g_strcmp0(bearer, "wifi") == 0)
190 return CONNMAN_SERVICE_TYPE_WIFI;
191 else if (g_strcmp0(bearer, "wimax") == 0)
192 return CONNMAN_SERVICE_TYPE_WIMAX;
193 else if (g_strcmp0(bearer, "bluetooth") == 0)
194 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
195 else if (g_strcmp0(bearer, "3g") == 0)
196 return CONNMAN_SERVICE_TYPE_CELLULAR;
198 return CONNMAN_SERVICE_TYPE_UNKNOWN;
201 static char *service2bearer(enum connman_service_type type)
204 case CONNMAN_SERVICE_TYPE_ETHERNET:
206 case CONNMAN_SERVICE_TYPE_WIFI:
208 case CONNMAN_SERVICE_TYPE_WIMAX:
210 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
212 case CONNMAN_SERVICE_TYPE_CELLULAR:
214 case CONNMAN_SERVICE_TYPE_UNKNOWN:
215 case CONNMAN_SERVICE_TYPE_SYSTEM:
216 case CONNMAN_SERVICE_TYPE_GPS:
217 case CONNMAN_SERVICE_TYPE_VPN:
218 case CONNMAN_SERVICE_TYPE_GADGET:
225 static void cleanup_bearer_info(gpointer data, gpointer user_data)
227 struct bearer_info *info = data;
233 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
235 struct bearer_info *info;
236 DBusMessageIter array;
239 dbus_message_iter_recurse(iter, &array);
241 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
244 dbus_message_iter_get_basic(&array, &bearer);
246 info = g_try_new0(struct bearer_info, 1);
248 g_slist_foreach(list, cleanup_bearer_info, NULL);
254 info->name = g_strdup(bearer);
255 info->service_type = bearer2service(info->name);
257 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
258 g_strcmp0(info->name, "*") == 0) {
259 info->match_all = TRUE;
261 info->match_all = FALSE;
264 list = g_slist_append(list, info);
266 dbus_message_iter_next(&array);
272 static GSList *session_allowed_bearers_any(void)
274 struct bearer_info *info;
277 info = g_try_new0(struct bearer_info, 1);
284 info->name = g_strdup("");
285 info->match_all = TRUE;
286 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
288 list = g_slist_append(list, info);
293 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
295 struct session_info *info = user_data;
298 for (list = info->allowed_bearers;
299 list != NULL; list = list->next) {
300 struct bearer_info *info = list->data;
302 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
307 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
309 struct connman_service *service = user_data;
310 struct connman_ipconfig *ipconfig_ipv4;
315 ipconfig_ipv4 = __connman_service_get_ip4config(service);
316 if (ipconfig_ipv4 == NULL)
319 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
322 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
324 struct connman_service *service = user_data;
325 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
330 ipconfig_ipv4 = __connman_service_get_ip4config(service);
331 ipconfig_ipv6 = __connman_service_get_ip6config(service);
332 if (ipconfig_ipv6 == NULL)
335 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
338 static void append_notify(DBusMessageIter *dict,
339 struct connman_session *session)
341 struct session_info *info = session->info;
342 struct session_info *info_last = session->info_last;
344 struct connman_service *service;
345 const char *name, *ifname, *bearer;
347 if (session->append_all == TRUE ||
348 info->online != info_last->online) {
349 connman_dbus_dict_append_basic(dict, "Online",
352 info_last->online = info->online;
355 if (session->append_all == TRUE ||
356 info->entry != info_last->entry) {
357 if (info->entry == NULL) {
363 name = info->entry->name;
364 ifname = info->entry->ifname;
365 service = info->entry->service;
366 bearer = info->entry->bearer;
369 connman_dbus_dict_append_basic(dict, "Name",
373 connman_dbus_dict_append_dict(dict, "IPv4",
374 append_ipconfig_ipv4,
377 connman_dbus_dict_append_dict(dict, "IPv6",
378 append_ipconfig_ipv6,
381 connman_dbus_dict_append_basic(dict, "Interface",
385 connman_dbus_dict_append_basic(dict, "Bearer",
389 info_last->entry = info->entry;
393 if (session->append_all == TRUE ||
394 info->priority != info_last->priority) {
395 connman_dbus_dict_append_basic(dict, "Priority",
398 info_last->priority = info->priority;
401 if (session->append_all == TRUE ||
402 info->allowed_bearers != info_last->allowed_bearers) {
403 connman_dbus_dict_append_array(dict, "AllowedBearers",
405 append_allowed_bearers,
407 info_last->allowed_bearers = info->allowed_bearers;
410 if (session->append_all == TRUE ||
411 info->avoid_handover != info_last->avoid_handover) {
412 connman_dbus_dict_append_basic(dict, "AvoidHandover",
414 &info->avoid_handover);
415 info_last->avoid_handover = info->avoid_handover;
418 if (session->append_all == TRUE ||
419 info->stay_connected != info_last->stay_connected) {
420 connman_dbus_dict_append_basic(dict, "StayConnected",
422 &info->stay_connected);
423 info_last->stay_connected = info->stay_connected;
426 if (session->append_all == TRUE ||
427 info->periodic_connect != info_last->periodic_connect) {
428 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
430 &info->periodic_connect);
431 info_last->periodic_connect = info->periodic_connect;
434 if (session->append_all == TRUE ||
435 info->idle_timeout != info_last->idle_timeout) {
436 connman_dbus_dict_append_basic(dict, "IdleTimeout",
438 &info->idle_timeout);
439 info_last->idle_timeout = info->idle_timeout;
442 if (session->append_all == TRUE ||
443 info->ecall != info_last->ecall) {
444 connman_dbus_dict_append_basic(dict, "EmergencyCall",
447 info_last->ecall = info->ecall;
450 if (session->append_all == TRUE ||
451 info->roaming_policy != info_last->roaming_policy) {
452 policy = roamingpolicy2string(info->roaming_policy);
453 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
456 info_last->roaming_policy = info->roaming_policy;
459 if (session->append_all == TRUE ||
460 info->marker != info_last->marker) {
461 connman_dbus_dict_append_basic(dict, "SessionMarker",
464 info_last->marker = info->marker;
467 session->append_all = FALSE;
468 session->info_dirty = FALSE;
471 static gboolean session_notify(gpointer user_data)
473 struct connman_session *session = user_data;
475 DBusMessageIter array, dict;
477 if (session->info_dirty == FALSE)
480 DBG("session %p owner %s notify_path %s", session,
481 session->owner, session->notify_path);
483 msg = dbus_message_new_method_call(session->owner, session->notify_path,
484 CONNMAN_NOTIFICATION_INTERFACE,
489 dbus_message_iter_init_append(msg, &array);
490 connman_dbus_dict_open(&array, &dict);
492 append_notify(&dict, session);
494 connman_dbus_dict_close(&array, &dict);
496 g_dbus_send_message(connection, msg);
498 session->info_dirty = FALSE;
503 static void ipconfig_ipv4_changed(struct connman_session *session)
505 struct session_info *info = session->info;
507 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
508 "IPv4", append_ipconfig_ipv4,
509 info->entry->service);
512 static void ipconfig_ipv6_changed(struct connman_session *session)
514 struct session_info *info = session->info;
516 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
517 "IPv6", append_ipconfig_ipv6,
518 info->entry->service);
521 static connman_bool_t service_type_match(struct connman_session *session,
522 struct connman_service *service)
524 struct session_info *info = session->info;
527 for (list = info->allowed_bearers;
528 list != NULL; list = list->next) {
529 struct bearer_info *info = list->data;
530 enum connman_service_type service_type;
532 if (info->match_all == TRUE)
535 service_type = connman_service_get_type(service);
536 if (info->service_type == service_type)
543 static connman_bool_t service_match(struct connman_session *session,
544 struct connman_service *service)
546 if (service_type_match(session, service) == FALSE)
552 static int service_type_weight(enum connman_service_type type)
555 * The session doesn't care which service
556 * to use. Nevertheless we have to sort them
557 * according their type. The ordering is
566 case CONNMAN_SERVICE_TYPE_ETHERNET:
568 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
570 case CONNMAN_SERVICE_TYPE_WIFI:
571 case CONNMAN_SERVICE_TYPE_WIMAX:
573 case CONNMAN_SERVICE_TYPE_CELLULAR:
575 case CONNMAN_SERVICE_TYPE_UNKNOWN:
576 case CONNMAN_SERVICE_TYPE_SYSTEM:
577 case CONNMAN_SERVICE_TYPE_GPS:
578 case CONNMAN_SERVICE_TYPE_VPN:
579 case CONNMAN_SERVICE_TYPE_GADGET:
586 static gint sort_allowed_bearers(struct connman_service *service_a,
587 struct connman_service *service_b,
588 struct connman_session *session)
590 struct session_info *info = session->info;
592 enum connman_service_type type_a, type_b;
593 int weight_a, weight_b;
595 type_a = connman_service_get_type(service_a);
596 type_b = connman_service_get_type(service_b);
598 for (list = info->allowed_bearers;
599 list != NULL; list = list->next) {
600 struct bearer_info *info = list->data;
602 if (info->match_all == TRUE) {
603 if (type_a != type_b) {
604 weight_a = service_type_weight(type_a);
605 weight_b = service_type_weight(type_b);
607 if (weight_a > weight_b)
610 if (weight_a < weight_b)
617 if (type_a == info->service_type &&
618 type_b == info->service_type) {
622 if (type_a == info->service_type &&
623 type_b != info->service_type) {
627 if (type_a != info->service_type &&
628 type_b == info->service_type) {
636 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
638 struct connman_service *service_a = (void *)a;
639 struct connman_service *service_b = (void *)b;
640 struct connman_session *session = user_data;
642 return sort_allowed_bearers(service_a, service_b, session);
645 static void cleanup_session(gpointer user_data)
647 struct connman_session *session = user_data;
648 struct session_info *info = session->info;
650 DBG("remove %s", session->session_path);
652 g_hash_table_destroy(session->service_hash);
653 g_sequence_free(session->service_list);
655 if (info->entry != NULL &&
656 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
657 __connman_service_disconnect(info->entry->service);
660 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
661 g_slist_free(info->allowed_bearers);
663 g_free(session->owner);
664 g_free(session->session_path);
665 g_free(session->notify_path);
666 g_free(session->info);
667 g_free(session->info_last);
672 static void release_session(gpointer key, gpointer value, gpointer user_data)
674 struct connman_session *session = value;
675 DBusMessage *message;
677 DBG("owner %s path %s", session->owner, session->notify_path);
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 message = dbus_message_new_method_call(session->owner,
686 session->notify_path,
687 CONNMAN_NOTIFICATION_INTERFACE,
692 dbus_message_set_no_reply(message, TRUE);
694 g_dbus_send_message(connection, message);
697 static int session_disconnect(struct connman_session *session)
699 DBG("session %p, %s", session, session->owner);
701 if (session->notify_watch > 0)
702 g_dbus_remove_watch(connection, session->notify_watch);
704 g_dbus_unregister_interface(connection, session->session_path,
705 CONNMAN_SESSION_INTERFACE);
707 g_hash_table_remove(session_hash, session->session_path);
712 static void owner_disconnect(DBusConnection *conn, void *user_data)
714 struct connman_session *session = user_data;
716 DBG("session %p, %s died", session, session->owner);
718 session_disconnect(session);
721 static DBusMessage *destroy_session(DBusConnection *conn,
722 DBusMessage *msg, void *user_data)
724 struct connman_session *session = user_data;
726 DBG("session %p", session);
728 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
731 static connman_bool_t is_connected(enum connman_service_state state)
734 case CONNMAN_SERVICE_STATE_UNKNOWN:
735 case CONNMAN_SERVICE_STATE_IDLE:
736 case CONNMAN_SERVICE_STATE_ASSOCIATION:
737 case CONNMAN_SERVICE_STATE_CONFIGURATION:
738 case CONNMAN_SERVICE_STATE_DISCONNECT:
739 case CONNMAN_SERVICE_STATE_FAILURE:
741 case CONNMAN_SERVICE_STATE_READY:
742 case CONNMAN_SERVICE_STATE_ONLINE:
749 static connman_bool_t explicit_connect(enum connman_session_reason reason)
752 case CONNMAN_SESSION_REASON_UNKNOWN:
753 case CONNMAN_SESSION_REASON_FREE_RIDE:
755 case CONNMAN_SESSION_REASON_CONNECT:
756 case CONNMAN_SESSION_REASON_PERIODIC:
763 static void test_and_disconnect(struct connman_session *session)
765 struct session_info *info = session->info;
767 if (info->entry == NULL)
770 DBG("session %p reason %s service %p state %d",
771 session, reason2string(info->entry->reason),
772 info->entry->service, info->entry->state);
774 if (explicit_connect(info->entry->reason) == FALSE)
777 if (__connman_service_session_dec(info->entry->service) == TRUE)
780 if (ecall_session != NULL && ecall_session != session)
783 __connman_service_disconnect(info->entry->service);
786 * TODO: We should mark this entry as pending work. In case
787 * disconnect fails we just unassign this session from the
788 * service and can't do anything later on it
792 info->online = FALSE;
796 static void select_and_connect(struct connman_session *session,
797 enum connman_session_reason reason)
799 struct session_info *info = session->info;
800 struct service_entry *entry = NULL;
802 connman_bool_t do_connect = FALSE;
804 DBG("session %p reason %s", session, reason2string(reason));
806 iter = g_sequence_get_begin_iter(session->service_list);
808 while (g_sequence_iter_is_end(iter) == FALSE) {
809 entry = g_sequence_get(iter);
811 switch (entry->state) {
812 case CONNMAN_SERVICE_STATE_ASSOCIATION:
813 case CONNMAN_SERVICE_STATE_CONFIGURATION:
814 case CONNMAN_SERVICE_STATE_READY:
815 case CONNMAN_SERVICE_STATE_ONLINE:
816 /* connecting or connected */
818 case CONNMAN_SERVICE_STATE_IDLE:
819 case CONNMAN_SERVICE_STATE_DISCONNECT:
820 if (explicit_connect(reason) == TRUE)
823 case CONNMAN_SERVICE_STATE_UNKNOWN:
824 case CONNMAN_SERVICE_STATE_FAILURE:
832 iter = g_sequence_iter_next(iter);
835 if (info->entry != NULL && info->entry != entry)
836 test_and_disconnect(session);
840 info->entry->reason = reason;
842 if (explicit_connect(reason) == TRUE)
843 __connman_service_session_inc(info->entry->service);
845 if (do_connect == TRUE)
846 __connman_service_connect(info->entry->service);
848 info->online = is_connected(entry->state);
852 static void session_changed(struct connman_session *session,
853 enum connman_session_trigger trigger)
855 struct session_info *info = session->info;
856 struct session_info *info_last = session->info_last;
860 * TODO: This only a placeholder for the 'real' algorithm to
861 * play a bit around. So we are going to improve it step by step.
864 DBG("session %p trigger %s", session, trigger2string(trigger));
867 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
868 DBG("ignore session changed event");
870 case CONNMAN_SESSION_TRIGGER_SETTING:
871 if (info->entry != NULL) {
872 iter = g_hash_table_lookup(session->service_hash,
873 info->entry->service);
876 * This service is not part of this
879 test_and_disconnect(session);
883 if (info->online == FALSE) {
884 select_and_connect(session,
885 CONNMAN_SESSION_REASON_FREE_RIDE);
889 case CONNMAN_SESSION_TRIGGER_CONNECT:
890 if (info->online == TRUE) {
891 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
892 __connman_service_session_inc(info->entry->service);
896 select_and_connect(session,
897 CONNMAN_SESSION_REASON_CONNECT);
900 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
901 test_and_disconnect(session);
904 case CONNMAN_SESSION_TRIGGER_PERIODIC:
905 if (info->online == TRUE) {
906 info->entry->reason = CONNMAN_SESSION_REASON_PERIODIC;
907 __connman_service_session_inc(info->entry->service);
911 select_and_connect(session,
912 CONNMAN_SESSION_REASON_PERIODIC);
915 case CONNMAN_SESSION_TRIGGER_SERVICE:
916 if (info->online == TRUE)
919 if (info->stay_connected == TRUE) {
920 DBG("StayConnected");
921 select_and_connect(session,
922 CONNMAN_SESSION_REASON_CONNECT);
927 select_and_connect(session,
928 CONNMAN_SESSION_REASON_FREE_RIDE);
931 case CONNMAN_SESSION_TRIGGER_ECALL:
932 if (info->online == FALSE && info->entry->service != NULL)
933 test_and_disconnect(session);
938 if (info->entry != info_last->entry)
939 session->info_dirty = TRUE;
941 session_notify(session);
944 static DBusMessage *connect_session(DBusConnection *conn,
945 DBusMessage *msg, void *user_data)
947 struct connman_session *session = user_data;
949 DBG("session %p", session);
951 if (ecall_session != NULL && ecall_session != session)
952 return __connman_error_failed(msg, EBUSY);
954 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
956 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
959 static DBusMessage *disconnect_session(DBusConnection *conn,
960 DBusMessage *msg, void *user_data)
962 struct connman_session *session = user_data;
964 DBG("session %p", session);
966 if (ecall_session != NULL && ecall_session != session)
967 return __connman_error_failed(msg, EBUSY);
969 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
971 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
974 static struct service_entry *create_service_entry(struct connman_service *service,
976 enum connman_service_state state)
978 struct service_entry *entry;
979 enum connman_service_type type;
982 entry = g_try_new0(struct service_entry, 1);
986 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
987 entry->state = state;
989 entry->service = service;
991 idx = __connman_service_get_index(entry->service);
992 entry->ifname = connman_inet_ifname(idx);
993 if (entry->ifname == NULL)
996 type = connman_service_get_type(entry->service);
997 entry->bearer = service2bearer(type);
1002 static void destroy_service_entry(gpointer data)
1004 struct service_entry *entry = data;
1009 static void update_allowed_bearers(struct connman_session *session)
1011 struct service_entry *entry;
1012 GSequenceIter *iter;
1014 if (session->service_list != NULL) {
1015 g_hash_table_remove_all(session->service_hash);
1016 g_sequence_free(session->service_list);
1019 session->service_list = __connman_service_get_list(session,
1021 create_service_entry,
1022 destroy_service_entry);
1024 g_sequence_sort(session->service_list, sort_services, session);
1026 iter = g_sequence_get_begin_iter(session->service_list);
1028 while (g_sequence_iter_is_end(iter) == FALSE) {
1029 entry = g_sequence_get(iter);
1031 DBG("service %p type %s name %s", entry->service,
1032 service2bearer(connman_service_get_type(entry->service)),
1035 g_hash_table_replace(session->service_hash,
1036 entry->service, iter);
1038 iter = g_sequence_iter_next(iter);
1041 session->info_dirty = TRUE;
1044 static void update_ecall_sessions(struct connman_session *session)
1046 struct session_info *info = session->info;
1047 struct connman_session *session_iter;
1048 GHashTableIter iter;
1049 gpointer key, value;
1051 g_hash_table_iter_init(&iter, session_hash);
1053 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1054 session_iter = value;
1056 if (session_iter == session)
1059 session_iter->info->ecall = info->ecall;
1060 session_iter->info_dirty = TRUE;
1062 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
1066 static void update_ecall(struct connman_session *session)
1068 struct session_info *info = session->info;
1069 struct session_info *info_last = session->info_last;
1071 DBG("session %p ecall_session %p ecall %d -> %d", session,
1072 ecall_session, info_last->ecall, info->ecall);
1074 if (ecall_session == NULL) {
1075 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
1078 ecall_session = session;
1079 } else if (ecall_session == session) {
1080 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
1083 ecall_session = NULL;
1088 update_ecall_sessions(session);
1090 session->info_dirty = TRUE;
1094 /* not a valid transition */
1095 info->ecall = info_last->ecall;
1098 static DBusMessage *change_session(DBusConnection *conn,
1099 DBusMessage *msg, void *user_data)
1101 struct connman_session *session = user_data;
1102 struct session_info *info = session->info;
1103 struct session_info *info_last = session->info_last;
1104 DBusMessageIter iter, value;
1106 GSList *allowed_bearers;
1108 DBG("session %p", session);
1109 if (dbus_message_iter_init(msg, &iter) == FALSE)
1110 return __connman_error_invalid_arguments(msg);
1112 dbus_message_iter_get_basic(&iter, &name);
1113 dbus_message_iter_next(&iter);
1114 dbus_message_iter_recurse(&iter, &value);
1116 switch (dbus_message_iter_get_arg_type(&value)) {
1117 case DBUS_TYPE_ARRAY:
1118 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1119 allowed_bearers = session_parse_allowed_bearers(&value);
1121 g_slist_foreach(info->allowed_bearers,
1122 cleanup_bearer_info, NULL);
1123 g_slist_free(info->allowed_bearers);
1125 if (allowed_bearers == NULL) {
1126 allowed_bearers = session_allowed_bearers_any();
1128 if (allowed_bearers == NULL)
1129 return __connman_error_failed(msg, ENOMEM);
1132 info->allowed_bearers = allowed_bearers;
1134 update_allowed_bearers(session);
1139 case DBUS_TYPE_BOOLEAN:
1140 if (g_str_equal(name, "Priority") == TRUE) {
1141 dbus_message_iter_get_basic(&value,
1144 if (info_last->priority != info->priority)
1145 session->info_dirty = TRUE;
1146 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1147 dbus_message_iter_get_basic(&value,
1148 &info->avoid_handover);
1150 if (info_last->avoid_handover != info->avoid_handover)
1151 session->info_dirty = TRUE;
1152 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1153 dbus_message_iter_get_basic(&value,
1154 &info->stay_connected);
1156 if (info_last->stay_connected != info->stay_connected)
1157 session->info_dirty = TRUE;
1158 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1159 dbus_message_iter_get_basic(&value,
1162 update_ecall(session);
1167 case DBUS_TYPE_UINT32:
1168 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1169 dbus_message_iter_get_basic(&value,
1170 &info->periodic_connect);
1172 if (info_last->periodic_connect != info->periodic_connect)
1173 session->info_dirty = TRUE;
1174 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1175 dbus_message_iter_get_basic(&value,
1176 &info->idle_timeout);
1178 if (info_last->idle_timeout != info->idle_timeout)
1179 session->info_dirty = TRUE;
1184 case DBUS_TYPE_STRING:
1185 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1187 dbus_message_iter_get_basic(&value, &val);
1188 info->roaming_policy =
1189 string2roamingpolicy(val);
1191 if (info_last->roaming_policy != info->roaming_policy)
1192 session->info_dirty = TRUE;
1201 if (session->info_dirty == TRUE)
1202 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1204 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1207 return __connman_error_invalid_arguments(msg);
1210 static GDBusMethodTable session_methods[] = {
1211 { "Destroy", "", "", destroy_session },
1212 { "Connect", "", "", connect_session },
1213 { "Disconnect", "", "", disconnect_session },
1214 { "Change", "sv", "", change_session },
1218 int __connman_session_create(DBusMessage *msg)
1220 const char *owner, *notify_path;
1221 char *session_path = NULL;
1222 DBusMessageIter iter, array;
1223 struct connman_session *session = NULL;
1224 struct session_info *info, *info_last;
1226 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1227 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1228 enum connman_session_roaming_policy roaming_policy =
1229 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1230 GSList *allowed_bearers = NULL;
1231 unsigned int periodic_connect = 0;
1232 unsigned int idle_timeout = 0;
1236 owner = dbus_message_get_sender(msg);
1238 DBG("owner %s", owner);
1240 if (ecall_session != NULL) {
1242 * If there is an emergency call already going on,
1243 * ignore session creation attempt
1249 dbus_message_iter_init(msg, &iter);
1250 dbus_message_iter_recurse(&iter, &array);
1252 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1253 DBusMessageIter entry, value;
1254 const char *key, *val;
1256 dbus_message_iter_recurse(&array, &entry);
1257 dbus_message_iter_get_basic(&entry, &key);
1259 dbus_message_iter_next(&entry);
1260 dbus_message_iter_recurse(&entry, &value);
1262 switch (dbus_message_iter_get_arg_type(&value)) {
1263 case DBUS_TYPE_ARRAY:
1264 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1266 session_parse_allowed_bearers(&value);
1271 case DBUS_TYPE_BOOLEAN:
1272 if (g_str_equal(key, "Priority") == TRUE) {
1273 dbus_message_iter_get_basic(&value,
1275 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1276 dbus_message_iter_get_basic(&value,
1278 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1279 dbus_message_iter_get_basic(&value,
1281 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1282 dbus_message_iter_get_basic(&value,
1288 case DBUS_TYPE_UINT32:
1289 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1290 dbus_message_iter_get_basic(&value,
1292 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1293 dbus_message_iter_get_basic(&value,
1299 case DBUS_TYPE_STRING:
1300 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1301 dbus_message_iter_get_basic(&value, &val);
1302 roaming_policy = string2roamingpolicy(val);
1307 dbus_message_iter_next(&array);
1310 dbus_message_iter_next(&iter);
1311 dbus_message_iter_get_basic(&iter, ¬ify_path);
1313 if (notify_path == NULL) {
1318 session_path = g_strdup_printf("/sessions%s", notify_path);
1319 if (session_path == NULL) {
1324 session = g_hash_table_lookup(session_hash, session_path);
1325 if (session != NULL) {
1331 session = g_try_new0(struct connman_session, 1);
1332 if (session == NULL) {
1337 session->info = g_try_new0(struct session_info, 1);
1338 if (session->info == NULL) {
1343 session->info_last = g_try_new0(struct session_info, 1);
1344 if (session->info_last == NULL) {
1349 info = session->info;
1350 info_last = session->info_last;
1352 session->owner = g_strdup(owner);
1353 session->session_path = session_path;
1354 session->notify_path = g_strdup(notify_path);
1355 session->notify_watch =
1356 g_dbus_add_disconnect_watch(connection, session->owner,
1357 owner_disconnect, session, NULL);
1359 session->service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1362 info->online = FALSE;
1363 info->priority = priority;
1364 info->avoid_handover = avoid_handover;
1365 info->stay_connected = stay_connected;
1366 info->periodic_connect = periodic_connect;
1367 info->idle_timeout = idle_timeout;
1368 info->ecall = ecall;
1369 info->roaming_policy = roaming_policy;
1373 if (allowed_bearers == NULL) {
1374 info->allowed_bearers =
1375 session_allowed_bearers_any();
1377 if (info->allowed_bearers == NULL) {
1382 info->allowed_bearers = allowed_bearers;
1385 g_hash_table_replace(session_hash, session->session_path, session);
1387 DBG("add %s", session->session_path);
1389 if (g_dbus_register_interface(connection, session->session_path,
1390 CONNMAN_SESSION_INTERFACE,
1391 session_methods, NULL,
1392 NULL, session, NULL) == FALSE) {
1393 connman_error("Failed to register %s", session->session_path);
1394 g_hash_table_remove(session_hash, session->session_path);
1401 g_dbus_send_reply(connection, msg,
1402 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1406 update_allowed_bearers(session);
1407 if (info->ecall == TRUE) {
1408 ecall_session = session;
1409 update_ecall_sessions(session);
1412 info_last->online = info->online;
1413 info_last->priority = info->priority;
1414 info_last->avoid_handover = info->avoid_handover;
1415 info_last->stay_connected = info->stay_connected;
1416 info_last->periodic_connect = info->periodic_connect;
1417 info_last->idle_timeout = info->idle_timeout;
1418 info_last->ecall = info->ecall;
1419 info_last->roaming_policy = info->roaming_policy;
1420 info_last->entry = info->entry;
1421 info_last->marker = info->marker;
1422 info_last->allowed_bearers = info->allowed_bearers;
1424 session->info_dirty = TRUE;
1425 session->append_all = TRUE;
1427 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1432 connman_error("Failed to create session");
1434 if (session != NULL) {
1435 if (session->info_last != NULL)
1436 g_free(session->info_last);
1437 if (session->info != NULL)
1438 g_free(session->info);
1442 g_free(session_path);
1444 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1445 g_slist_free(allowed_bearers);
1450 int __connman_session_destroy(DBusMessage *msg)
1452 const char *owner, *session_path;
1453 struct connman_session *session;
1455 owner = dbus_message_get_sender(msg);
1457 DBG("owner %s", owner);
1459 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1461 if (session_path == NULL)
1464 session = g_hash_table_lookup(session_hash, session_path);
1465 if (session == NULL)
1468 if (g_strcmp0(owner, session->owner) != 0)
1471 session_disconnect(session);
1476 connman_bool_t __connman_session_mode()
1481 void __connman_session_set_mode(connman_bool_t enable)
1483 DBG("enable %d", enable);
1485 if (sessionmode == enable)
1488 sessionmode = enable;
1490 if (sessionmode == TRUE)
1491 __connman_service_disconnect_all();
1494 static void service_add(struct connman_service *service)
1496 GHashTableIter iter;
1497 GSequenceIter *iter_service_list;
1498 gpointer key, value;
1499 struct connman_session *session;
1501 DBG("service %p", service);
1503 g_hash_table_iter_init(&iter, session_hash);
1505 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1508 if (service_match(session, service) == FALSE)
1512 g_sequence_insert_sorted(session->service_list,
1513 service, sort_services,
1516 g_hash_table_replace(session->service_hash, service,
1519 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1523 static void service_remove(struct connman_service *service)
1526 GHashTableIter iter;
1527 gpointer key, value;
1528 struct connman_session *session;
1529 struct session_info *info;
1531 DBG("service %p", service);
1533 g_hash_table_iter_init(&iter, session_hash);
1535 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1536 GSequenceIter *iter;
1538 info = session->info;
1540 iter = g_hash_table_lookup(session->service_hash, service);
1544 g_sequence_remove(iter);
1547 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1551 static void service_state_changed(struct connman_service *service,
1552 enum connman_service_state state)
1554 GHashTableIter iter;
1555 gpointer key, value;
1556 struct connman_session *session;
1557 struct session_info *info, *info_last;
1559 DBG("service %p state %d", service, state);
1561 g_hash_table_iter_init(&iter, session_hash);
1563 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1565 info = session->info;
1566 info_last = session->info_last;
1568 if (info->entry != NULL && info->entry->service == service) {
1569 info->entry->state = state;
1570 info->online = is_connected(info->entry->state);
1571 if (info_last->online != info->online)
1572 session->info_dirty = TRUE;
1575 session_changed(session,
1576 CONNMAN_SESSION_TRIGGER_SERVICE);
1580 static void ipconfig_changed(struct connman_service *service,
1581 struct connman_ipconfig *ipconfig)
1583 GHashTableIter iter;
1584 gpointer key, value;
1585 struct connman_session *session;
1586 struct session_info *info;
1587 enum connman_ipconfig_type type;
1589 DBG("service %p ipconfig %p", service, ipconfig);
1591 type = __connman_ipconfig_get_config_type(ipconfig);
1593 g_hash_table_iter_init(&iter, session_hash);
1595 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1597 info = session->info;
1599 if (info->entry != NULL && info->entry->service == service) {
1600 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1601 ipconfig_ipv4_changed(session);
1602 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1603 ipconfig_ipv6_changed(session);
1608 static struct connman_notifier session_notifier = {
1610 .service_add = service_add,
1611 .service_remove = service_remove,
1612 .service_state_changed = service_state_changed,
1613 .ipconfig_changed = ipconfig_changed,
1616 int __connman_session_init(void)
1622 connection = connman_dbus_get_connection();
1623 if (connection == NULL)
1626 err = connman_notifier_register(&session_notifier);
1628 dbus_connection_unref(connection);
1632 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1633 NULL, cleanup_session);
1635 sessionmode = FALSE;
1639 void __connman_session_cleanup(void)
1643 if (connection == NULL)
1646 connman_notifier_unregister(&session_notifier);
1648 g_hash_table_foreach(session_hash, release_session, NULL);
1649 g_hash_table_destroy(session_hash);
1651 dbus_connection_unref(connection);