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
33 static DBusConnection *connection;
34 static GHashTable *session_hash;
35 static connman_bool_t sessionmode;
36 static struct connman_session *ecall_session;
38 enum connman_session_trigger {
39 CONNMAN_SESSION_TRIGGER_UNKNOWN = 0,
40 CONNMAN_SESSION_TRIGGER_SETTING = 1,
41 CONNMAN_SESSION_TRIGGER_CONNECT = 2,
42 CONNMAN_SESSION_TRIGGER_DISCONNECT = 3,
43 CONNMAN_SESSION_TRIGGER_PERIODIC = 4,
44 CONNMAN_SESSION_TRIGGER_SERVICE = 5,
45 CONNMAN_SESSION_TRIGGER_ECALL = 6,
48 enum connman_session_reason {
49 CONNMAN_SESSION_REASON_UNKNOWN = 0,
50 CONNMAN_SESSION_REASON_CONNECT = 1,
51 CONNMAN_SESSION_REASON_FREE_RIDE = 2,
52 CONNMAN_SESSION_REASON_PERIODIC = 3,
55 enum connman_session_roaming_policy {
56 CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN = 0,
57 CONNMAN_SESSION_ROAMING_POLICY_DEFAULT = 1,
58 CONNMAN_SESSION_ROAMING_POLICY_ALWAYS = 2,
59 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN = 3,
60 CONNMAN_SESSION_ROAMING_POLICY_NATIONAL = 4,
61 CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL = 5,
64 struct service_entry {
65 /* track why this service was selected */
66 enum connman_session_reason reason;
67 enum connman_service_state state;
69 struct connman_service *service;
75 connman_bool_t online;
76 connman_bool_t priority;
77 GSList *allowed_bearers;
78 connman_bool_t avoid_handover;
79 connman_bool_t stay_connected;
80 unsigned int periodic_connect;
81 unsigned int idle_timeout;
83 enum connman_session_roaming_policy roaming_policy;
86 struct service_entry *entry;
89 struct connman_session {
95 connman_bool_t append_all;
96 connman_bool_t info_dirty;
97 struct session_info *info;
98 struct session_info *info_last;
100 GSequence *service_list;
101 GHashTable *service_hash;
106 connman_bool_t match_all;
107 enum connman_service_type service_type;
110 static const char *trigger2string(enum connman_session_trigger trigger)
113 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
115 case CONNMAN_SESSION_TRIGGER_SETTING:
117 case CONNMAN_SESSION_TRIGGER_CONNECT:
119 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
121 case CONNMAN_SESSION_TRIGGER_PERIODIC:
123 case CONNMAN_SESSION_TRIGGER_SERVICE:
125 case CONNMAN_SESSION_TRIGGER_ECALL:
132 static const char *reason2string(enum connman_session_reason reason)
135 case CONNMAN_SESSION_REASON_UNKNOWN:
137 case CONNMAN_SESSION_REASON_CONNECT:
139 case CONNMAN_SESSION_REASON_FREE_RIDE:
141 case CONNMAN_SESSION_REASON_PERIODIC:
148 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
151 case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
153 case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
155 case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
157 case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
159 case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
161 case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
162 return "international";
168 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
170 if (g_strcmp0(policy, "default") == 0)
171 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
172 else if (g_strcmp0(policy, "always") == 0)
173 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
174 else if (g_strcmp0(policy, "forbidden") == 0)
175 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
176 else if (g_strcmp0(policy, "national") == 0)
177 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
178 else if (g_strcmp0(policy, "international") == 0)
179 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
181 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
184 static enum connman_service_type bearer2service(const char *bearer)
187 return CONNMAN_SERVICE_TYPE_UNKNOWN;
189 if (g_strcmp0(bearer, "ethernet") == 0)
190 return CONNMAN_SERVICE_TYPE_ETHERNET;
191 else if (g_strcmp0(bearer, "wifi") == 0)
192 return CONNMAN_SERVICE_TYPE_WIFI;
193 else if (g_strcmp0(bearer, "wimax") == 0)
194 return CONNMAN_SERVICE_TYPE_WIMAX;
195 else if (g_strcmp0(bearer, "bluetooth") == 0)
196 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
197 else if (g_strcmp0(bearer, "3g") == 0)
198 return CONNMAN_SERVICE_TYPE_CELLULAR;
199 else if (g_strcmp0(bearer, "vpn") == 0)
200 return CONNMAN_SERVICE_TYPE_VPN;
202 return CONNMAN_SERVICE_TYPE_UNKNOWN;
205 static char *service2bearer(enum connman_service_type type)
208 case CONNMAN_SERVICE_TYPE_ETHERNET:
210 case CONNMAN_SERVICE_TYPE_WIFI:
212 case CONNMAN_SERVICE_TYPE_WIMAX:
214 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
216 case CONNMAN_SERVICE_TYPE_CELLULAR:
218 case CONNMAN_SERVICE_TYPE_VPN:
220 case CONNMAN_SERVICE_TYPE_UNKNOWN:
221 case CONNMAN_SERVICE_TYPE_SYSTEM:
222 case CONNMAN_SERVICE_TYPE_GPS:
223 case CONNMAN_SERVICE_TYPE_GADGET:
230 static void cleanup_bearer_info(gpointer data, gpointer user_data)
232 struct bearer_info *info = data;
238 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
240 struct bearer_info *info;
241 DBusMessageIter array;
244 dbus_message_iter_recurse(iter, &array);
246 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
249 dbus_message_iter_get_basic(&array, &bearer);
251 info = g_try_new0(struct bearer_info, 1);
253 g_slist_foreach(list, cleanup_bearer_info, NULL);
259 info->name = g_strdup(bearer);
260 info->service_type = bearer2service(info->name);
262 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
263 g_strcmp0(info->name, "*") == 0) {
264 info->match_all = TRUE;
266 info->match_all = FALSE;
269 list = g_slist_append(list, info);
271 dbus_message_iter_next(&array);
277 static GSList *session_allowed_bearers_any(void)
279 struct bearer_info *info;
282 info = g_try_new0(struct bearer_info, 1);
289 info->name = g_strdup("");
290 info->match_all = TRUE;
291 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
293 list = g_slist_append(list, info);
298 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
300 struct session_info *info = user_data;
303 for (list = info->allowed_bearers;
304 list != NULL; list = list->next) {
305 struct bearer_info *info = list->data;
307 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
312 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
314 struct connman_service *service = user_data;
315 struct connman_ipconfig *ipconfig_ipv4;
320 ipconfig_ipv4 = __connman_service_get_ip4config(service);
321 if (ipconfig_ipv4 == NULL)
324 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
327 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
329 struct connman_service *service = user_data;
330 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
335 ipconfig_ipv4 = __connman_service_get_ip4config(service);
336 ipconfig_ipv6 = __connman_service_get_ip6config(service);
337 if (ipconfig_ipv6 == NULL)
340 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
343 static void append_notify(DBusMessageIter *dict,
344 struct connman_session *session)
346 struct session_info *info = session->info;
347 struct session_info *info_last = session->info_last;
349 struct connman_service *service;
350 const char *name, *ifname, *bearer;
352 if (session->append_all == TRUE ||
353 info->online != info_last->online) {
354 connman_dbus_dict_append_basic(dict, "Online",
357 info_last->online = info->online;
360 if (session->append_all == TRUE ||
361 info->entry != info_last->entry) {
362 if (info->entry == NULL) {
368 name = info->entry->name;
369 ifname = info->entry->ifname;
370 service = info->entry->service;
371 bearer = info->entry->bearer;
374 connman_dbus_dict_append_basic(dict, "Name",
378 connman_dbus_dict_append_dict(dict, "IPv4",
379 append_ipconfig_ipv4,
382 connman_dbus_dict_append_dict(dict, "IPv6",
383 append_ipconfig_ipv6,
386 connman_dbus_dict_append_basic(dict, "Interface",
390 connman_dbus_dict_append_basic(dict, "Bearer",
394 info_last->entry = info->entry;
398 if (session->append_all == TRUE ||
399 info->priority != info_last->priority) {
400 connman_dbus_dict_append_basic(dict, "Priority",
403 info_last->priority = info->priority;
406 if (session->append_all == TRUE ||
407 info->allowed_bearers != info_last->allowed_bearers) {
408 connman_dbus_dict_append_array(dict, "AllowedBearers",
410 append_allowed_bearers,
412 info_last->allowed_bearers = info->allowed_bearers;
415 if (session->append_all == TRUE ||
416 info->avoid_handover != info_last->avoid_handover) {
417 connman_dbus_dict_append_basic(dict, "AvoidHandover",
419 &info->avoid_handover);
420 info_last->avoid_handover = info->avoid_handover;
423 if (session->append_all == TRUE ||
424 info->stay_connected != info_last->stay_connected) {
425 connman_dbus_dict_append_basic(dict, "StayConnected",
427 &info->stay_connected);
428 info_last->stay_connected = info->stay_connected;
431 if (session->append_all == TRUE ||
432 info->periodic_connect != info_last->periodic_connect) {
433 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
435 &info->periodic_connect);
436 info_last->periodic_connect = info->periodic_connect;
439 if (session->append_all == TRUE ||
440 info->idle_timeout != info_last->idle_timeout) {
441 connman_dbus_dict_append_basic(dict, "IdleTimeout",
443 &info->idle_timeout);
444 info_last->idle_timeout = info->idle_timeout;
447 if (session->append_all == TRUE ||
448 info->ecall != info_last->ecall) {
449 connman_dbus_dict_append_basic(dict, "EmergencyCall",
452 info_last->ecall = info->ecall;
455 if (session->append_all == TRUE ||
456 info->roaming_policy != info_last->roaming_policy) {
457 policy = roamingpolicy2string(info->roaming_policy);
458 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
461 info_last->roaming_policy = info->roaming_policy;
464 if (session->append_all == TRUE ||
465 info->marker != info_last->marker) {
466 connman_dbus_dict_append_basic(dict, "SessionMarker",
469 info_last->marker = info->marker;
472 session->append_all = FALSE;
473 session->info_dirty = FALSE;
476 static gboolean session_notify(gpointer user_data)
478 struct connman_session *session = user_data;
480 DBusMessageIter array, dict;
482 if (session->info_dirty == FALSE)
485 DBG("session %p owner %s notify_path %s", session,
486 session->owner, session->notify_path);
488 msg = dbus_message_new_method_call(session->owner, session->notify_path,
489 CONNMAN_NOTIFICATION_INTERFACE,
494 dbus_message_iter_init_append(msg, &array);
495 connman_dbus_dict_open(&array, &dict);
497 append_notify(&dict, session);
499 connman_dbus_dict_close(&array, &dict);
501 g_dbus_send_message(connection, msg);
503 session->info_dirty = FALSE;
508 static void ipconfig_ipv4_changed(struct connman_session *session)
510 struct session_info *info = session->info;
512 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
513 "IPv4", append_ipconfig_ipv4,
514 info->entry->service);
517 static void ipconfig_ipv6_changed(struct connman_session *session)
519 struct session_info *info = session->info;
521 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
522 "IPv6", append_ipconfig_ipv6,
523 info->entry->service);
526 static connman_bool_t service_type_match(struct connman_session *session,
527 struct connman_service *service)
529 struct session_info *info = session->info;
532 for (list = info->allowed_bearers;
533 list != NULL; list = list->next) {
534 struct bearer_info *info = list->data;
535 enum connman_service_type service_type;
537 if (info->match_all == TRUE)
540 service_type = connman_service_get_type(service);
541 if (info->service_type == service_type)
548 static connman_bool_t service_match(struct connman_session *session,
549 struct connman_service *service)
551 if (service_type_match(session, service) == FALSE)
557 static int service_type_weight(enum connman_service_type type)
560 * The session doesn't care which service
561 * to use. Nevertheless we have to sort them
562 * according their type. The ordering is
571 case CONNMAN_SERVICE_TYPE_ETHERNET:
573 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
575 case CONNMAN_SERVICE_TYPE_WIFI:
576 case CONNMAN_SERVICE_TYPE_WIMAX:
578 case CONNMAN_SERVICE_TYPE_CELLULAR:
580 case CONNMAN_SERVICE_TYPE_UNKNOWN:
581 case CONNMAN_SERVICE_TYPE_SYSTEM:
582 case CONNMAN_SERVICE_TYPE_GPS:
583 case CONNMAN_SERVICE_TYPE_VPN:
584 case CONNMAN_SERVICE_TYPE_GADGET:
591 static gint sort_allowed_bearers(struct connman_service *service_a,
592 struct connman_service *service_b,
593 struct connman_session *session)
595 struct session_info *info = session->info;
597 enum connman_service_type type_a, type_b;
598 int weight_a, weight_b;
600 type_a = connman_service_get_type(service_a);
601 type_b = connman_service_get_type(service_b);
603 for (list = info->allowed_bearers;
604 list != NULL; list = list->next) {
605 struct bearer_info *info = list->data;
607 if (info->match_all == TRUE) {
608 if (type_a != type_b) {
609 weight_a = service_type_weight(type_a);
610 weight_b = service_type_weight(type_b);
612 if (weight_a > weight_b)
615 if (weight_a < weight_b)
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) {
632 if (type_a != info->service_type &&
633 type_b == info->service_type) {
641 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
643 struct service_entry *entry_a = (void *)a;
644 struct service_entry *entry_b = (void *)b;
645 struct connman_session *session = user_data;
647 return sort_allowed_bearers(entry_a->service, entry_b->service,
651 static void cleanup_session(gpointer user_data)
653 struct connman_session *session = user_data;
654 struct session_info *info = session->info;
656 DBG("remove %s", session->session_path);
658 g_hash_table_destroy(session->service_hash);
659 g_sequence_free(session->service_list);
661 if (info->entry != NULL &&
662 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
663 __connman_service_disconnect(info->entry->service);
666 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
667 g_slist_free(info->allowed_bearers);
669 g_free(session->owner);
670 g_free(session->session_path);
671 g_free(session->notify_path);
672 g_free(session->info);
673 g_free(session->info_last);
678 static void release_session(gpointer key, gpointer value, gpointer user_data)
680 struct connman_session *session = value;
681 DBusMessage *message;
683 DBG("owner %s path %s", session->owner, session->notify_path);
685 if (session->notify_watch > 0)
686 g_dbus_remove_watch(connection, session->notify_watch);
688 g_dbus_unregister_interface(connection, session->session_path,
689 CONNMAN_SESSION_INTERFACE);
691 message = dbus_message_new_method_call(session->owner,
692 session->notify_path,
693 CONNMAN_NOTIFICATION_INTERFACE,
698 dbus_message_set_no_reply(message, TRUE);
700 g_dbus_send_message(connection, message);
703 static int session_disconnect(struct connman_session *session)
705 DBG("session %p, %s", session, session->owner);
707 if (session->notify_watch > 0)
708 g_dbus_remove_watch(connection, session->notify_watch);
710 g_dbus_unregister_interface(connection, session->session_path,
711 CONNMAN_SESSION_INTERFACE);
713 g_hash_table_remove(session_hash, session->session_path);
718 static void owner_disconnect(DBusConnection *conn, void *user_data)
720 struct connman_session *session = user_data;
722 DBG("session %p, %s died", session, session->owner);
724 session_disconnect(session);
727 static DBusMessage *destroy_session(DBusConnection *conn,
728 DBusMessage *msg, void *user_data)
730 struct connman_session *session = user_data;
732 DBG("session %p", session);
734 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
737 static connman_bool_t is_online(enum connman_service_state state)
740 case CONNMAN_SERVICE_STATE_UNKNOWN:
741 case CONNMAN_SERVICE_STATE_IDLE:
742 case CONNMAN_SERVICE_STATE_ASSOCIATION:
743 case CONNMAN_SERVICE_STATE_CONFIGURATION:
744 case CONNMAN_SERVICE_STATE_DISCONNECT:
745 case CONNMAN_SERVICE_STATE_FAILURE:
746 case CONNMAN_SERVICE_STATE_READY:
748 case CONNMAN_SERVICE_STATE_ONLINE:
755 static connman_bool_t is_connecting(enum connman_service_state state)
758 case CONNMAN_SERVICE_STATE_UNKNOWN:
759 case CONNMAN_SERVICE_STATE_IDLE:
761 case CONNMAN_SERVICE_STATE_ASSOCIATION:
762 case CONNMAN_SERVICE_STATE_CONFIGURATION:
763 case CONNMAN_SERVICE_STATE_READY:
765 case CONNMAN_SERVICE_STATE_DISCONNECT:
766 case CONNMAN_SERVICE_STATE_FAILURE:
767 case CONNMAN_SERVICE_STATE_ONLINE:
774 static connman_bool_t explicit_connect(enum connman_session_reason reason)
777 case CONNMAN_SESSION_REASON_UNKNOWN:
778 case CONNMAN_SESSION_REASON_FREE_RIDE:
780 case CONNMAN_SESSION_REASON_CONNECT:
781 case CONNMAN_SESSION_REASON_PERIODIC:
788 static void test_and_disconnect(struct connman_session *session)
790 struct session_info *info = session->info;
792 if (info->entry == NULL)
795 DBG("session %p reason %s service %p state %d",
796 session, reason2string(info->entry->reason),
797 info->entry->service, info->entry->state);
799 if (explicit_connect(info->entry->reason) == FALSE)
802 if (__connman_service_session_dec(info->entry->service) == TRUE)
805 if (ecall_session != NULL && ecall_session != session)
808 __connman_service_disconnect(info->entry->service);
811 * TODO: We should mark this entry as pending work. In case
812 * disconnect fails we just unassign this session from the
813 * service and can't do anything later on it
817 info->online = FALSE;
821 static void select_and_connect(struct connman_session *session,
822 enum connman_session_reason reason)
824 struct session_info *info = session->info;
825 struct service_entry *entry = NULL;
827 connman_bool_t do_connect = FALSE;
829 DBG("session %p reason %s", session, reason2string(reason));
831 iter = g_sequence_get_begin_iter(session->service_list);
833 while (g_sequence_iter_is_end(iter) == FALSE) {
834 entry = g_sequence_get(iter);
836 switch (entry->state) {
837 case CONNMAN_SERVICE_STATE_ASSOCIATION:
838 case CONNMAN_SERVICE_STATE_CONFIGURATION:
839 case CONNMAN_SERVICE_STATE_READY:
840 case CONNMAN_SERVICE_STATE_ONLINE:
841 /* connecting or connected */
843 case CONNMAN_SERVICE_STATE_IDLE:
844 case CONNMAN_SERVICE_STATE_DISCONNECT:
845 if (explicit_connect(reason) == TRUE)
848 case CONNMAN_SERVICE_STATE_UNKNOWN:
849 case CONNMAN_SERVICE_STATE_FAILURE:
857 iter = g_sequence_iter_next(iter);
860 if (info->entry != NULL && info->entry != entry)
861 test_and_disconnect(session);
865 info->entry->reason = reason;
867 if (explicit_connect(reason) == TRUE)
868 __connman_service_session_inc(info->entry->service);
870 if (do_connect == TRUE)
871 __connman_service_connect(info->entry->service);
873 info->online = is_online(entry->state);
877 static void session_changed(struct connman_session *session,
878 enum connman_session_trigger trigger)
880 struct session_info *info = session->info;
881 struct session_info *info_last = session->info_last;
885 * TODO: This only a placeholder for the 'real' algorithm to
886 * play a bit around. So we are going to improve it step by step.
889 DBG("session %p trigger %s", session, trigger2string(trigger));
892 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
893 DBG("ignore session changed event");
895 case CONNMAN_SESSION_TRIGGER_SETTING:
896 if (info->entry != NULL) {
897 iter = g_hash_table_lookup(session->service_hash,
898 info->entry->service);
901 * This service is not part of this
904 test_and_disconnect(session);
908 if (info->online == FALSE) {
909 select_and_connect(session,
910 CONNMAN_SESSION_REASON_FREE_RIDE);
914 case CONNMAN_SESSION_TRIGGER_CONNECT:
915 if (info->online == TRUE) {
916 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
917 __connman_service_session_inc(info->entry->service);
921 select_and_connect(session,
922 CONNMAN_SESSION_REASON_CONNECT);
925 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
926 test_and_disconnect(session);
929 case CONNMAN_SESSION_TRIGGER_PERIODIC:
930 if (info->online == TRUE) {
931 info->entry->reason = CONNMAN_SESSION_REASON_PERIODIC;
932 __connman_service_session_inc(info->entry->service);
936 select_and_connect(session,
937 CONNMAN_SESSION_REASON_PERIODIC);
940 case CONNMAN_SESSION_TRIGGER_SERVICE:
941 if (info->online == TRUE)
944 if (info->entry != NULL &&
945 is_connecting(info->entry->state) == TRUE) {
949 if (info->stay_connected == TRUE) {
950 DBG("StayConnected");
951 select_and_connect(session,
952 CONNMAN_SESSION_REASON_CONNECT);
957 select_and_connect(session,
958 CONNMAN_SESSION_REASON_FREE_RIDE);
961 case CONNMAN_SESSION_TRIGGER_ECALL:
962 if (info->online == FALSE && info->entry->service != NULL)
963 test_and_disconnect(session);
968 if (info->entry != info_last->entry)
969 session->info_dirty = TRUE;
971 session_notify(session);
974 static DBusMessage *connect_session(DBusConnection *conn,
975 DBusMessage *msg, void *user_data)
977 struct connman_session *session = user_data;
979 DBG("session %p", session);
981 if (ecall_session != NULL && ecall_session != session)
982 return __connman_error_failed(msg, EBUSY);
984 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
986 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
989 static DBusMessage *disconnect_session(DBusConnection *conn,
990 DBusMessage *msg, void *user_data)
992 struct connman_session *session = user_data;
994 DBG("session %p", session);
996 if (ecall_session != NULL && ecall_session != session)
997 return __connman_error_failed(msg, EBUSY);
999 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1001 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1004 static struct service_entry *create_service_entry(struct connman_service *service,
1006 enum connman_service_state state)
1008 struct service_entry *entry;
1009 enum connman_service_type type;
1012 entry = g_try_new0(struct service_entry, 1);
1016 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1017 entry->state = state;
1019 entry->service = service;
1021 idx = __connman_service_get_index(entry->service);
1022 entry->ifname = connman_inet_ifname(idx);
1023 if (entry->ifname == NULL)
1026 type = connman_service_get_type(entry->service);
1027 entry->bearer = service2bearer(type);
1032 static void destroy_service_entry(gpointer data)
1034 struct service_entry *entry = data;
1039 static void update_allowed_bearers(struct connman_session *session)
1041 struct service_entry *entry;
1042 GSequenceIter *iter;
1044 if (session->service_list != NULL) {
1045 g_hash_table_remove_all(session->service_hash);
1046 g_sequence_free(session->service_list);
1049 session->service_list = __connman_service_get_list(session,
1051 create_service_entry,
1052 destroy_service_entry);
1054 g_sequence_sort(session->service_list, sort_services, session);
1056 iter = g_sequence_get_begin_iter(session->service_list);
1058 while (g_sequence_iter_is_end(iter) == FALSE) {
1059 entry = g_sequence_get(iter);
1061 DBG("service %p type %s name %s", entry->service,
1062 service2bearer(connman_service_get_type(entry->service)),
1065 g_hash_table_replace(session->service_hash,
1066 entry->service, iter);
1068 iter = g_sequence_iter_next(iter);
1071 session->info_dirty = TRUE;
1074 static void update_ecall_sessions(struct connman_session *session)
1076 struct session_info *info = session->info;
1077 struct connman_session *session_iter;
1078 GHashTableIter iter;
1079 gpointer key, value;
1081 g_hash_table_iter_init(&iter, session_hash);
1083 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1084 session_iter = value;
1086 if (session_iter == session)
1089 session_iter->info->ecall = info->ecall;
1090 session_iter->info_dirty = TRUE;
1092 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
1096 static void update_ecall(struct connman_session *session)
1098 struct session_info *info = session->info;
1099 struct session_info *info_last = session->info_last;
1101 DBG("session %p ecall_session %p ecall %d -> %d", session,
1102 ecall_session, info_last->ecall, info->ecall);
1104 if (ecall_session == NULL) {
1105 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
1108 ecall_session = session;
1109 } else if (ecall_session == session) {
1110 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
1113 ecall_session = NULL;
1118 update_ecall_sessions(session);
1120 session->info_dirty = TRUE;
1124 /* not a valid transition */
1125 info->ecall = info_last->ecall;
1128 static DBusMessage *change_session(DBusConnection *conn,
1129 DBusMessage *msg, void *user_data)
1131 struct connman_session *session = user_data;
1132 struct session_info *info = session->info;
1133 struct session_info *info_last = session->info_last;
1134 DBusMessageIter iter, value;
1136 GSList *allowed_bearers;
1138 DBG("session %p", session);
1139 if (dbus_message_iter_init(msg, &iter) == FALSE)
1140 return __connman_error_invalid_arguments(msg);
1142 dbus_message_iter_get_basic(&iter, &name);
1143 dbus_message_iter_next(&iter);
1144 dbus_message_iter_recurse(&iter, &value);
1146 switch (dbus_message_iter_get_arg_type(&value)) {
1147 case DBUS_TYPE_ARRAY:
1148 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1149 allowed_bearers = session_parse_allowed_bearers(&value);
1151 g_slist_foreach(info->allowed_bearers,
1152 cleanup_bearer_info, NULL);
1153 g_slist_free(info->allowed_bearers);
1155 if (allowed_bearers == NULL) {
1156 allowed_bearers = session_allowed_bearers_any();
1158 if (allowed_bearers == NULL)
1159 return __connman_error_failed(msg, ENOMEM);
1162 info->allowed_bearers = allowed_bearers;
1164 update_allowed_bearers(session);
1169 case DBUS_TYPE_BOOLEAN:
1170 if (g_str_equal(name, "Priority") == TRUE) {
1171 dbus_message_iter_get_basic(&value,
1174 if (info_last->priority != info->priority)
1175 session->info_dirty = TRUE;
1176 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1177 dbus_message_iter_get_basic(&value,
1178 &info->avoid_handover);
1180 if (info_last->avoid_handover != info->avoid_handover)
1181 session->info_dirty = TRUE;
1182 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1183 dbus_message_iter_get_basic(&value,
1184 &info->stay_connected);
1186 if (info_last->stay_connected != info->stay_connected)
1187 session->info_dirty = TRUE;
1188 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1189 dbus_message_iter_get_basic(&value,
1192 update_ecall(session);
1197 case DBUS_TYPE_UINT32:
1198 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1199 dbus_message_iter_get_basic(&value,
1200 &info->periodic_connect);
1202 if (info_last->periodic_connect != info->periodic_connect)
1203 session->info_dirty = TRUE;
1204 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1205 dbus_message_iter_get_basic(&value,
1206 &info->idle_timeout);
1208 if (info_last->idle_timeout != info->idle_timeout)
1209 session->info_dirty = TRUE;
1214 case DBUS_TYPE_STRING:
1215 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1217 dbus_message_iter_get_basic(&value, &val);
1218 info->roaming_policy =
1219 string2roamingpolicy(val);
1221 if (info_last->roaming_policy != info->roaming_policy)
1222 session->info_dirty = TRUE;
1231 if (session->info_dirty == TRUE)
1232 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1234 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1237 return __connman_error_invalid_arguments(msg);
1240 static GDBusMethodTable session_methods[] = {
1241 { "Destroy", "", "", destroy_session },
1242 { "Connect", "", "", connect_session },
1243 { "Disconnect", "", "", disconnect_session },
1244 { "Change", "sv", "", change_session },
1248 int __connman_session_create(DBusMessage *msg)
1250 const char *owner, *notify_path;
1251 char *session_path = NULL;
1252 DBusMessageIter iter, array;
1253 struct connman_session *session = NULL;
1254 struct session_info *info, *info_last;
1256 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1257 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1258 enum connman_session_roaming_policy roaming_policy =
1259 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1260 GSList *allowed_bearers = NULL;
1261 unsigned int periodic_connect = 0;
1262 unsigned int idle_timeout = 0;
1266 owner = dbus_message_get_sender(msg);
1268 DBG("owner %s", owner);
1270 if (ecall_session != NULL) {
1272 * If there is an emergency call already going on,
1273 * ignore session creation attempt
1279 dbus_message_iter_init(msg, &iter);
1280 dbus_message_iter_recurse(&iter, &array);
1282 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1283 DBusMessageIter entry, value;
1284 const char *key, *val;
1286 dbus_message_iter_recurse(&array, &entry);
1287 dbus_message_iter_get_basic(&entry, &key);
1289 dbus_message_iter_next(&entry);
1290 dbus_message_iter_recurse(&entry, &value);
1292 switch (dbus_message_iter_get_arg_type(&value)) {
1293 case DBUS_TYPE_ARRAY:
1294 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1296 session_parse_allowed_bearers(&value);
1301 case DBUS_TYPE_BOOLEAN:
1302 if (g_str_equal(key, "Priority") == TRUE) {
1303 dbus_message_iter_get_basic(&value,
1305 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1306 dbus_message_iter_get_basic(&value,
1308 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1309 dbus_message_iter_get_basic(&value,
1311 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1312 dbus_message_iter_get_basic(&value,
1318 case DBUS_TYPE_UINT32:
1319 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1320 dbus_message_iter_get_basic(&value,
1322 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1323 dbus_message_iter_get_basic(&value,
1329 case DBUS_TYPE_STRING:
1330 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1331 dbus_message_iter_get_basic(&value, &val);
1332 roaming_policy = string2roamingpolicy(val);
1337 dbus_message_iter_next(&array);
1340 dbus_message_iter_next(&iter);
1341 dbus_message_iter_get_basic(&iter, ¬ify_path);
1343 if (notify_path == NULL) {
1348 session_path = g_strdup_printf("/sessions%s", notify_path);
1349 if (session_path == NULL) {
1354 session = g_hash_table_lookup(session_hash, session_path);
1355 if (session != NULL) {
1361 session = g_try_new0(struct connman_session, 1);
1362 if (session == NULL) {
1367 session->info = g_try_new0(struct session_info, 1);
1368 if (session->info == NULL) {
1373 session->info_last = g_try_new0(struct session_info, 1);
1374 if (session->info_last == NULL) {
1379 info = session->info;
1380 info_last = session->info_last;
1382 session->owner = g_strdup(owner);
1383 session->session_path = session_path;
1384 session->notify_path = g_strdup(notify_path);
1385 session->notify_watch =
1386 g_dbus_add_disconnect_watch(connection, session->owner,
1387 owner_disconnect, session, NULL);
1389 session->service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1392 info->online = FALSE;
1393 info->priority = priority;
1394 info->avoid_handover = avoid_handover;
1395 info->stay_connected = stay_connected;
1396 info->periodic_connect = periodic_connect;
1397 info->idle_timeout = idle_timeout;
1398 info->ecall = ecall;
1399 info->roaming_policy = roaming_policy;
1403 if (allowed_bearers == NULL) {
1404 info->allowed_bearers =
1405 session_allowed_bearers_any();
1407 if (info->allowed_bearers == NULL) {
1412 info->allowed_bearers = allowed_bearers;
1415 g_hash_table_replace(session_hash, session->session_path, session);
1417 DBG("add %s", session->session_path);
1419 if (g_dbus_register_interface(connection, session->session_path,
1420 CONNMAN_SESSION_INTERFACE,
1421 session_methods, NULL,
1422 NULL, session, NULL) == FALSE) {
1423 connman_error("Failed to register %s", session->session_path);
1424 g_hash_table_remove(session_hash, session->session_path);
1431 g_dbus_send_reply(connection, msg,
1432 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1436 update_allowed_bearers(session);
1437 if (info->ecall == TRUE) {
1438 ecall_session = session;
1439 update_ecall_sessions(session);
1442 info_last->online = info->online;
1443 info_last->priority = info->priority;
1444 info_last->avoid_handover = info->avoid_handover;
1445 info_last->stay_connected = info->stay_connected;
1446 info_last->periodic_connect = info->periodic_connect;
1447 info_last->idle_timeout = info->idle_timeout;
1448 info_last->ecall = info->ecall;
1449 info_last->roaming_policy = info->roaming_policy;
1450 info_last->entry = info->entry;
1451 info_last->marker = info->marker;
1452 info_last->allowed_bearers = info->allowed_bearers;
1454 session->info_dirty = TRUE;
1455 session->append_all = TRUE;
1457 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1462 connman_error("Failed to create session");
1464 if (session != NULL) {
1465 if (session->info_last != NULL)
1466 g_free(session->info_last);
1467 if (session->info != NULL)
1468 g_free(session->info);
1472 g_free(session_path);
1474 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1475 g_slist_free(allowed_bearers);
1480 int __connman_session_destroy(DBusMessage *msg)
1482 const char *owner, *session_path;
1483 struct connman_session *session;
1485 owner = dbus_message_get_sender(msg);
1487 DBG("owner %s", owner);
1489 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1491 if (session_path == NULL)
1494 session = g_hash_table_lookup(session_hash, session_path);
1495 if (session == NULL)
1498 if (g_strcmp0(owner, session->owner) != 0)
1501 session_disconnect(session);
1506 connman_bool_t __connman_session_mode()
1511 void __connman_session_set_mode(connman_bool_t enable)
1513 DBG("enable %d", enable);
1515 if (sessionmode == enable)
1518 sessionmode = enable;
1520 if (sessionmode == TRUE)
1521 __connman_service_disconnect_all();
1524 static void service_add(struct connman_service *service)
1526 GHashTableIter iter;
1527 GSequenceIter *iter_service_list;
1528 gpointer key, value;
1529 struct connman_session *session;
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) {
1538 if (service_match(session, service) == FALSE)
1542 g_sequence_insert_sorted(session->service_list,
1543 service, sort_services,
1546 g_hash_table_replace(session->service_hash, service,
1549 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1553 static void service_remove(struct connman_service *service)
1556 GHashTableIter iter;
1557 gpointer key, value;
1558 struct connman_session *session;
1559 struct session_info *info;
1561 DBG("service %p", service);
1563 g_hash_table_iter_init(&iter, session_hash);
1565 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1566 GSequenceIter *iter;
1568 info = session->info;
1570 iter = g_hash_table_lookup(session->service_hash, service);
1574 g_sequence_remove(iter);
1577 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1581 static void service_state_changed(struct connman_service *service,
1582 enum connman_service_state state)
1584 GHashTableIter iter;
1585 gpointer key, value;
1586 struct connman_session *session;
1587 struct session_info *info, *info_last;
1589 DBG("service %p state %d", service, state);
1591 g_hash_table_iter_init(&iter, session_hash);
1593 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1595 info = session->info;
1596 info_last = session->info_last;
1598 if (info->entry != NULL && info->entry->service == service) {
1599 info->entry->state = state;
1600 info->online = is_online(info->entry->state);
1601 if (info_last->online != info->online)
1602 session->info_dirty = TRUE;
1605 session_changed(session,
1606 CONNMAN_SESSION_TRIGGER_SERVICE);
1610 static void ipconfig_changed(struct connman_service *service,
1611 struct connman_ipconfig *ipconfig)
1613 GHashTableIter iter;
1614 gpointer key, value;
1615 struct connman_session *session;
1616 struct session_info *info;
1617 enum connman_ipconfig_type type;
1619 DBG("service %p ipconfig %p", service, ipconfig);
1621 type = __connman_ipconfig_get_config_type(ipconfig);
1623 g_hash_table_iter_init(&iter, session_hash);
1625 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1627 info = session->info;
1629 if (info->entry != NULL && info->entry->service == service) {
1630 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1631 ipconfig_ipv4_changed(session);
1632 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1633 ipconfig_ipv6_changed(session);
1638 static struct connman_notifier session_notifier = {
1640 .service_add = service_add,
1641 .service_remove = service_remove,
1642 .service_state_changed = service_state_changed,
1643 .ipconfig_changed = ipconfig_changed,
1646 int __connman_session_init(void)
1652 connection = connman_dbus_get_connection();
1653 if (connection == NULL)
1656 err = connman_notifier_register(&session_notifier);
1658 dbus_connection_unref(connection);
1662 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1663 NULL, cleanup_session);
1665 sessionmode = FALSE;
1669 void __connman_session_cleanup(void)
1673 if (connection == NULL)
1676 connman_notifier_unregister(&session_notifier);
1678 g_hash_table_foreach(session_hash, release_session, NULL);
1679 g_hash_table_destroy(session_hash);
1681 dbus_connection_unref(connection);