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 session_info *ecall_info;
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_DISCONNECT = 2,
52 CONNMAN_SESSION_REASON_FREE_RIDE = 3,
53 CONNMAN_SESSION_REASON_PERIODIC = 4,
56 enum connman_session_roaming_policy {
57 CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN = 0,
58 CONNMAN_SESSION_ROAMING_POLICY_DEFAULT = 1,
59 CONNMAN_SESSION_ROAMING_POLICY_ALWAYS = 2,
60 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN = 3,
61 CONNMAN_SESSION_ROAMING_POLICY_NATIONAL = 4,
62 CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL = 5,
65 struct service_entry {
66 /* track why this service was selected */
67 enum connman_session_reason reason;
68 enum connman_service_state state;
70 struct connman_service *service;
76 connman_bool_t online;
77 connman_bool_t priority;
78 GSList *allowed_bearers;
79 connman_bool_t avoid_handover;
80 connman_bool_t stay_connected;
81 unsigned int periodic_connect;
82 unsigned int idle_timeout;
84 enum connman_session_roaming_policy roaming_policy;
87 struct service_entry *entry;
88 enum connman_session_reason reason;
91 struct connman_session {
97 connman_bool_t append_all;
98 connman_bool_t info_dirty;
99 struct session_info *info;
100 struct session_info *info_last;
102 GSequence *service_list;
103 GHashTable *service_hash;
108 connman_bool_t match_all;
109 enum connman_service_type service_type;
112 static const char *trigger2string(enum connman_session_trigger trigger)
115 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
117 case CONNMAN_SESSION_TRIGGER_SETTING:
119 case CONNMAN_SESSION_TRIGGER_CONNECT:
121 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
123 case CONNMAN_SESSION_TRIGGER_PERIODIC:
125 case CONNMAN_SESSION_TRIGGER_SERVICE:
127 case CONNMAN_SESSION_TRIGGER_ECALL:
134 static const char *reason2string(enum connman_session_reason reason)
137 case CONNMAN_SESSION_REASON_UNKNOWN:
139 case CONNMAN_SESSION_REASON_CONNECT:
141 case CONNMAN_SESSION_REASON_DISCONNECT:
143 case CONNMAN_SESSION_REASON_FREE_RIDE:
145 case CONNMAN_SESSION_REASON_PERIODIC:
152 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
155 case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
157 case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
159 case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
161 case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
163 case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
165 case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
166 return "international";
172 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
174 if (g_strcmp0(policy, "default") == 0)
175 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
176 else if (g_strcmp0(policy, "always") == 0)
177 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
178 else if (g_strcmp0(policy, "forbidden") == 0)
179 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
180 else if (g_strcmp0(policy, "national") == 0)
181 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
182 else if (g_strcmp0(policy, "international") == 0)
183 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
185 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
188 static enum connman_service_type bearer2service(const char *bearer)
191 return CONNMAN_SERVICE_TYPE_UNKNOWN;
193 if (g_strcmp0(bearer, "ethernet") == 0)
194 return CONNMAN_SERVICE_TYPE_ETHERNET;
195 else if (g_strcmp0(bearer, "wifi") == 0)
196 return CONNMAN_SERVICE_TYPE_WIFI;
197 else if (g_strcmp0(bearer, "wimax") == 0)
198 return CONNMAN_SERVICE_TYPE_WIMAX;
199 else if (g_strcmp0(bearer, "bluetooth") == 0)
200 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
201 else if (g_strcmp0(bearer, "3g") == 0)
202 return CONNMAN_SERVICE_TYPE_CELLULAR;
203 else if (g_strcmp0(bearer, "vpn") == 0)
204 return CONNMAN_SERVICE_TYPE_VPN;
206 return CONNMAN_SERVICE_TYPE_UNKNOWN;
209 static char *service2bearer(enum connman_service_type type)
212 case CONNMAN_SERVICE_TYPE_ETHERNET:
214 case CONNMAN_SERVICE_TYPE_WIFI:
216 case CONNMAN_SERVICE_TYPE_WIMAX:
218 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
220 case CONNMAN_SERVICE_TYPE_CELLULAR:
222 case CONNMAN_SERVICE_TYPE_VPN:
224 case CONNMAN_SERVICE_TYPE_UNKNOWN:
225 case CONNMAN_SERVICE_TYPE_SYSTEM:
226 case CONNMAN_SERVICE_TYPE_GPS:
227 case CONNMAN_SERVICE_TYPE_GADGET:
234 static void cleanup_bearer_info(gpointer data, gpointer user_data)
236 struct bearer_info *info = data;
242 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
244 struct bearer_info *info;
245 DBusMessageIter array;
248 dbus_message_iter_recurse(iter, &array);
250 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
253 dbus_message_iter_get_basic(&array, &bearer);
255 info = g_try_new0(struct bearer_info, 1);
257 g_slist_foreach(list, cleanup_bearer_info, NULL);
263 info->name = g_strdup(bearer);
264 info->service_type = bearer2service(info->name);
266 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
267 g_strcmp0(info->name, "*") == 0) {
268 info->match_all = TRUE;
270 info->match_all = FALSE;
273 list = g_slist_append(list, info);
275 dbus_message_iter_next(&array);
281 static GSList *session_allowed_bearers_any(void)
283 struct bearer_info *info;
286 info = g_try_new0(struct bearer_info, 1);
293 info->name = g_strdup("");
294 info->match_all = TRUE;
295 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
297 list = g_slist_append(list, info);
302 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
304 struct session_info *info = user_data;
307 for (list = info->allowed_bearers;
308 list != NULL; list = list->next) {
309 struct bearer_info *info = list->data;
311 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
316 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
318 struct connman_service *service = user_data;
319 struct connman_ipconfig *ipconfig_ipv4;
324 ipconfig_ipv4 = __connman_service_get_ip4config(service);
325 if (ipconfig_ipv4 == NULL)
328 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
331 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
333 struct connman_service *service = user_data;
334 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
339 ipconfig_ipv4 = __connman_service_get_ip4config(service);
340 ipconfig_ipv6 = __connman_service_get_ip6config(service);
341 if (ipconfig_ipv6 == NULL)
344 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
347 static void append_notify(DBusMessageIter *dict,
348 struct connman_session *session)
350 struct session_info *info = session->info;
351 struct session_info *info_last = session->info_last;
353 struct connman_service *service;
354 const char *name, *ifname, *bearer;
356 if (session->append_all == TRUE ||
357 info->online != info_last->online) {
358 connman_dbus_dict_append_basic(dict, "Online",
361 info_last->online = info->online;
364 if (session->append_all == TRUE ||
365 info->entry != info_last->entry) {
366 if (info->entry == NULL) {
372 name = info->entry->name;
373 ifname = info->entry->ifname;
374 service = info->entry->service;
375 bearer = info->entry->bearer;
378 connman_dbus_dict_append_basic(dict, "Name",
382 connman_dbus_dict_append_dict(dict, "IPv4",
383 append_ipconfig_ipv4,
386 connman_dbus_dict_append_dict(dict, "IPv6",
387 append_ipconfig_ipv6,
390 connman_dbus_dict_append_basic(dict, "Interface",
394 connman_dbus_dict_append_basic(dict, "Bearer",
398 info_last->entry = info->entry;
402 if (session->append_all == TRUE ||
403 info->priority != info_last->priority) {
404 connman_dbus_dict_append_basic(dict, "Priority",
407 info_last->priority = info->priority;
410 if (session->append_all == TRUE ||
411 info->allowed_bearers != info_last->allowed_bearers) {
412 connman_dbus_dict_append_array(dict, "AllowedBearers",
414 append_allowed_bearers,
416 info_last->allowed_bearers = info->allowed_bearers;
419 if (session->append_all == TRUE ||
420 info->avoid_handover != info_last->avoid_handover) {
421 connman_dbus_dict_append_basic(dict, "AvoidHandover",
423 &info->avoid_handover);
424 info_last->avoid_handover = info->avoid_handover;
427 if (session->append_all == TRUE ||
428 info->stay_connected != info_last->stay_connected) {
429 connman_dbus_dict_append_basic(dict, "StayConnected",
431 &info->stay_connected);
432 info_last->stay_connected = info->stay_connected;
435 if (session->append_all == TRUE ||
436 info->periodic_connect != info_last->periodic_connect) {
437 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
439 &info->periodic_connect);
440 info_last->periodic_connect = info->periodic_connect;
443 if (session->append_all == TRUE ||
444 info->idle_timeout != info_last->idle_timeout) {
445 connman_dbus_dict_append_basic(dict, "IdleTimeout",
447 &info->idle_timeout);
448 info_last->idle_timeout = info->idle_timeout;
451 if (session->append_all == TRUE ||
452 info->ecall != info_last->ecall) {
453 connman_dbus_dict_append_basic(dict, "EmergencyCall",
456 info_last->ecall = info->ecall;
459 if (session->append_all == TRUE ||
460 info->roaming_policy != info_last->roaming_policy) {
461 policy = roamingpolicy2string(info->roaming_policy);
462 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
465 info_last->roaming_policy = info->roaming_policy;
468 if (session->append_all == TRUE ||
469 info->marker != info_last->marker) {
470 connman_dbus_dict_append_basic(dict, "SessionMarker",
473 info_last->marker = info->marker;
476 session->append_all = FALSE;
477 session->info_dirty = FALSE;
480 static gboolean session_notify(gpointer user_data)
482 struct connman_session *session = user_data;
484 DBusMessageIter array, dict;
486 if (session->info_dirty == FALSE)
489 DBG("session %p owner %s notify_path %s", session,
490 session->owner, session->notify_path);
492 msg = dbus_message_new_method_call(session->owner, session->notify_path,
493 CONNMAN_NOTIFICATION_INTERFACE,
498 dbus_message_iter_init_append(msg, &array);
499 connman_dbus_dict_open(&array, &dict);
501 append_notify(&dict, session);
503 connman_dbus_dict_close(&array, &dict);
505 g_dbus_send_message(connection, msg);
507 session->info_dirty = FALSE;
512 static void ipconfig_ipv4_changed(struct connman_session *session)
514 struct session_info *info = session->info;
516 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
517 "IPv4", append_ipconfig_ipv4,
518 info->entry->service);
521 static void ipconfig_ipv6_changed(struct connman_session *session)
523 struct session_info *info = session->info;
525 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
526 "IPv6", append_ipconfig_ipv6,
527 info->entry->service);
530 static connman_bool_t service_type_match(struct connman_session *session,
531 struct connman_service *service)
533 struct session_info *info = session->info;
536 for (list = info->allowed_bearers;
537 list != NULL; list = list->next) {
538 struct bearer_info *info = list->data;
539 enum connman_service_type service_type;
541 if (info->match_all == TRUE)
544 service_type = connman_service_get_type(service);
545 if (info->service_type == service_type)
552 static connman_bool_t service_match(struct connman_session *session,
553 struct connman_service *service)
555 if (service_type_match(session, service) == FALSE)
561 static int service_type_weight(enum connman_service_type type)
564 * The session doesn't care which service
565 * to use. Nevertheless we have to sort them
566 * according their type. The ordering is
575 case CONNMAN_SERVICE_TYPE_ETHERNET:
577 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
579 case CONNMAN_SERVICE_TYPE_WIFI:
580 case CONNMAN_SERVICE_TYPE_WIMAX:
582 case CONNMAN_SERVICE_TYPE_CELLULAR:
584 case CONNMAN_SERVICE_TYPE_UNKNOWN:
585 case CONNMAN_SERVICE_TYPE_SYSTEM:
586 case CONNMAN_SERVICE_TYPE_GPS:
587 case CONNMAN_SERVICE_TYPE_VPN:
588 case CONNMAN_SERVICE_TYPE_GADGET:
595 static gint sort_allowed_bearers(struct connman_service *service_a,
596 struct connman_service *service_b,
597 struct connman_session *session)
599 struct session_info *info = session->info;
601 enum connman_service_type type_a, type_b;
602 int weight_a, weight_b;
604 type_a = connman_service_get_type(service_a);
605 type_b = connman_service_get_type(service_b);
607 for (list = info->allowed_bearers;
608 list != NULL; list = list->next) {
609 struct bearer_info *info = list->data;
611 if (info->match_all == TRUE) {
612 if (type_a != type_b) {
613 weight_a = service_type_weight(type_a);
614 weight_b = service_type_weight(type_b);
616 if (weight_a > weight_b)
619 if (weight_a < weight_b)
626 if (type_a == info->service_type &&
627 type_b == info->service_type) {
631 if (type_a == info->service_type &&
632 type_b != info->service_type) {
636 if (type_a != info->service_type &&
637 type_b == info->service_type) {
645 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
647 struct service_entry *entry_a = (void *)a;
648 struct service_entry *entry_b = (void *)b;
649 struct connman_session *session = user_data;
651 return sort_allowed_bearers(entry_a->service, entry_b->service,
655 static void cleanup_session(gpointer user_data)
657 struct connman_session *session = user_data;
658 struct session_info *info = session->info;
660 DBG("remove %s", session->session_path);
662 g_hash_table_destroy(session->service_hash);
663 g_sequence_free(session->service_list);
665 if (info->entry != NULL &&
666 info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
667 __connman_service_disconnect(info->entry->service);
670 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
671 g_slist_free(info->allowed_bearers);
673 g_free(session->owner);
674 g_free(session->session_path);
675 g_free(session->notify_path);
676 g_free(session->info);
677 g_free(session->info_last);
682 static connman_bool_t is_online(enum connman_service_state state)
685 case CONNMAN_SERVICE_STATE_UNKNOWN:
686 case CONNMAN_SERVICE_STATE_IDLE:
687 case CONNMAN_SERVICE_STATE_ASSOCIATION:
688 case CONNMAN_SERVICE_STATE_CONFIGURATION:
689 case CONNMAN_SERVICE_STATE_DISCONNECT:
690 case CONNMAN_SERVICE_STATE_FAILURE:
691 case CONNMAN_SERVICE_STATE_READY:
693 case CONNMAN_SERVICE_STATE_ONLINE:
700 static connman_bool_t is_connecting(enum connman_service_state state)
703 case CONNMAN_SERVICE_STATE_UNKNOWN:
704 case CONNMAN_SERVICE_STATE_IDLE:
706 case CONNMAN_SERVICE_STATE_ASSOCIATION:
707 case CONNMAN_SERVICE_STATE_CONFIGURATION:
708 case CONNMAN_SERVICE_STATE_READY:
710 case CONNMAN_SERVICE_STATE_DISCONNECT:
711 case CONNMAN_SERVICE_STATE_FAILURE:
712 case CONNMAN_SERVICE_STATE_ONLINE:
719 static connman_bool_t explicit_connect(enum connman_session_reason reason)
722 case CONNMAN_SESSION_REASON_UNKNOWN:
723 case CONNMAN_SESSION_REASON_FREE_RIDE:
724 case CONNMAN_SESSION_REASON_DISCONNECT:
726 case CONNMAN_SESSION_REASON_CONNECT:
727 case CONNMAN_SESSION_REASON_PERIODIC:
734 static connman_bool_t explicit_disconnect(struct session_info *info)
736 if (info->entry == NULL)
739 DBG("reason %s service %p state %d",
740 reason2string(info->entry->reason),
741 info->entry->service, info->entry->state);
743 if (info->entry->reason == CONNMAN_SESSION_REASON_UNKNOWN)
746 if (explicit_connect(info->entry->reason) == FALSE)
749 if (__connman_service_session_dec(info->entry->service) == FALSE)
752 if (ecall_info != NULL && ecall_info != info)
758 static gboolean call_disconnect(gpointer user_data)
760 struct connman_service *service = user_data;
763 * TODO: We should mark this entry as pending work. In case
764 * disconnect fails we just unassign this session from the
765 * service and can't do anything later on it
767 DBG("disconnect service %p", service);
768 __connman_service_disconnect(service);
773 static gboolean call_connect(gpointer user_data)
775 struct connman_service *service = user_data;
777 DBG("connect service %p", service);
778 __connman_service_connect(service);
783 static void deselect_service(struct session_info *info)
785 struct connman_service *service;
786 connman_bool_t disconnect, online;
788 if (info->entry == NULL)
791 disconnect = explicit_disconnect(info);
793 online = is_connecting(info->entry->state) == TRUE ||
794 is_online(info->entry->state) == TRUE;
796 info->online = FALSE;
797 info->reason = CONNMAN_SESSION_REASON_UNKNOWN;
798 info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
800 service = info->entry->service;
803 if (disconnect == TRUE && online == TRUE)
804 g_timeout_add_seconds(0, call_disconnect, service);
807 static void deselect_and_disconnect(struct connman_session *session)
809 struct session_info *info = session->info;
811 deselect_service(info);
814 static void select_online_service(struct session_info *info,
815 struct service_entry *entry)
820 info->entry->reason = info->reason;
822 if (explicit_connect(info->reason) == FALSE)
825 __connman_service_session_inc(info->entry->service);
828 static void select_offline_service(struct session_info *info,
829 struct service_entry *entry)
831 if (explicit_connect(info->reason) == FALSE) {
832 /* Don't select this service. It is not online and we
833 * don't call connect on it. This happends for example
834 * when the system is idle and we create a new session
835 * which is in free ride mode.
837 deselect_service(info);
841 info->online = FALSE;
844 info->entry->reason = info->reason;
846 __connman_service_session_inc(info->entry->service);
847 g_timeout_add_seconds(0, call_connect, info->entry->service);
850 static void select_service(struct session_info *info,
851 struct service_entry *entry)
853 DBG("service %p", entry->service);
855 if (is_online(entry->state) == TRUE)
856 select_online_service(info, entry);
858 select_offline_service(info, entry);
861 static void select_and_connect(struct connman_session *session,
862 enum connman_session_reason reason)
864 struct session_info *info = session->info;
865 struct service_entry *entry = NULL;
868 DBG("session %p reason %s", session, reason2string(reason));
870 info->reason = reason;
872 iter = g_sequence_get_begin_iter(session->service_list);
874 while (g_sequence_iter_is_end(iter) == FALSE) {
875 entry = g_sequence_get(iter);
877 switch (entry->state) {
878 case CONNMAN_SERVICE_STATE_ASSOCIATION:
879 case CONNMAN_SERVICE_STATE_CONFIGURATION:
880 case CONNMAN_SERVICE_STATE_READY:
881 case CONNMAN_SERVICE_STATE_ONLINE:
882 case CONNMAN_SERVICE_STATE_IDLE:
883 case CONNMAN_SERVICE_STATE_DISCONNECT:
884 select_service(info, entry);
886 case CONNMAN_SERVICE_STATE_UNKNOWN:
887 case CONNMAN_SERVICE_STATE_FAILURE:
891 iter = g_sequence_iter_next(iter);
895 static void session_changed(struct connman_session *session,
896 enum connman_session_trigger trigger)
898 struct session_info *info = session->info;
899 struct session_info *info_last = session->info_last;
903 * TODO: This only a placeholder for the 'real' algorithm to
904 * play a bit around. So we are going to improve it step by step.
907 DBG("session %p trigger %s", session, trigger2string(trigger));
910 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
911 DBG("ignore session changed event");
913 case CONNMAN_SESSION_TRIGGER_SETTING:
914 if (info->entry != NULL) {
915 iter = g_hash_table_lookup(session->service_hash,
916 info->entry->service);
919 * This service is not part of this
922 deselect_and_disconnect(session);
926 if (info->online == FALSE) {
927 select_and_connect(session,
928 CONNMAN_SESSION_REASON_FREE_RIDE);
932 case CONNMAN_SESSION_TRIGGER_CONNECT:
933 if (info->online == TRUE) {
934 if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
936 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
937 __connman_service_session_inc(info->entry->service);
941 select_and_connect(session,
942 CONNMAN_SESSION_REASON_CONNECT);
945 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
946 deselect_and_disconnect(session);
949 case CONNMAN_SESSION_TRIGGER_PERIODIC:
950 if (info->online == TRUE) {
951 info->entry->reason = CONNMAN_SESSION_REASON_PERIODIC;
952 __connman_service_session_inc(info->entry->service);
956 select_and_connect(session,
957 CONNMAN_SESSION_REASON_PERIODIC);
960 case CONNMAN_SESSION_TRIGGER_SERVICE:
961 if (info->entry != NULL &&
962 (is_connecting(info->entry->state) == TRUE ||
963 is_online(info->entry->state) == TRUE)) {
967 switch (info->reason) {
968 case CONNMAN_SESSION_REASON_CONNECT:
970 * We are not online, we are not connecting, that
971 * means we could still have a valid info->entry.
972 * Though something has changed from the service layer.
973 * Therefore we want to restart the algorithm. Before we
974 * can do that we have to cleanup a potientional old entry.
976 deselect_and_disconnect(session);
977 info->reason = CONNMAN_SESSION_REASON_CONNECT; /* restore value */
979 DBG("Retry to find a matching session");
981 * The user called Connect() but there was no
982 * matching session available at this point.
983 * Now there might be a new one. Let's retry
984 * to select and connect
986 select_and_connect(session,
987 CONNMAN_SESSION_REASON_CONNECT);
989 case CONNMAN_SESSION_REASON_PERIODIC:
990 case CONNMAN_SESSION_REASON_FREE_RIDE:
991 if (info->stay_connected == TRUE) {
992 DBG("StayConnected");
993 select_and_connect(session,
994 CONNMAN_SESSION_REASON_CONNECT);
996 select_and_connect(session,
997 CONNMAN_SESSION_REASON_FREE_RIDE);
1000 case CONNMAN_SESSION_REASON_DISCONNECT:
1001 case CONNMAN_SESSION_REASON_UNKNOWN:
1005 case CONNMAN_SESSION_TRIGGER_ECALL:
1006 if (info->online == FALSE && info->entry != NULL &&
1007 info->entry->service != NULL) {
1008 deselect_and_disconnect(session);
1014 if (info->entry != info_last->entry)
1015 session->info_dirty = TRUE;
1017 session_notify(session);
1020 static DBusMessage *connect_session(DBusConnection *conn,
1021 DBusMessage *msg, void *user_data)
1023 struct connman_session *session = user_data;
1024 struct session_info *info = session->info;
1026 DBG("session %p", session);
1028 if (ecall_info != NULL && ecall_info != info)
1029 return __connman_error_failed(msg, EBUSY);
1031 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1033 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1036 static DBusMessage *disconnect_session(DBusConnection *conn,
1037 DBusMessage *msg, void *user_data)
1039 struct connman_session *session = user_data;
1040 struct session_info *info = session->info;
1042 DBG("session %p", session);
1044 if (ecall_info != NULL && ecall_info != info)
1045 return __connman_error_failed(msg, EBUSY);
1047 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1049 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1052 static struct service_entry *create_service_entry(struct connman_service *service,
1054 enum connman_service_state state)
1056 struct service_entry *entry;
1057 enum connman_service_type type;
1060 entry = g_try_new0(struct service_entry, 1);
1064 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1065 entry->state = state;
1070 entry->service = service;
1072 idx = __connman_service_get_index(entry->service);
1073 entry->ifname = connman_inet_ifname(idx);
1074 if (entry->ifname == NULL)
1075 entry->ifname = g_strdup("");
1077 type = connman_service_get_type(entry->service);
1078 entry->bearer = service2bearer(type);
1083 static void destroy_service_entry(gpointer data)
1085 struct service_entry *entry = data;
1087 g_free(entry->ifname);
1092 static void update_allowed_bearers(struct connman_session *session)
1094 struct service_entry *entry;
1095 GSequenceIter *iter;
1097 if (session->service_list != NULL) {
1098 g_hash_table_remove_all(session->service_hash);
1099 g_sequence_free(session->service_list);
1102 session->service_list = __connman_service_get_list(session,
1104 create_service_entry,
1105 destroy_service_entry);
1107 g_sequence_sort(session->service_list, sort_services, session);
1109 iter = g_sequence_get_begin_iter(session->service_list);
1111 while (g_sequence_iter_is_end(iter) == FALSE) {
1112 entry = g_sequence_get(iter);
1114 DBG("service %p type %s name %s", entry->service,
1115 service2bearer(connman_service_get_type(entry->service)),
1118 g_hash_table_replace(session->service_hash,
1119 entry->service, iter);
1121 iter = g_sequence_iter_next(iter);
1124 session->info_dirty = TRUE;
1127 static void update_ecall_sessions(struct connman_session *session)
1129 struct session_info *info = session->info;
1130 struct connman_session *session_iter;
1131 GHashTableIter iter;
1132 gpointer key, value;
1134 g_hash_table_iter_init(&iter, session_hash);
1136 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1137 session_iter = value;
1139 if (session_iter == session)
1142 session_iter->info->ecall = info->ecall;
1143 session_iter->info_dirty = TRUE;
1145 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
1149 static void update_ecall(struct connman_session *session)
1151 struct session_info *info = session->info;
1152 struct session_info *info_last = session->info_last;
1154 DBG("session %p ecall_info %p ecall %d -> %d", session,
1155 ecall_info, info_last->ecall, info->ecall);
1157 if (ecall_info == NULL) {
1158 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
1162 } else if (ecall_info == info) {
1163 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
1171 update_ecall_sessions(session);
1173 session->info_dirty = TRUE;
1177 /* not a valid transition */
1178 info->ecall = info_last->ecall;
1181 static DBusMessage *change_session(DBusConnection *conn,
1182 DBusMessage *msg, void *user_data)
1184 struct connman_session *session = user_data;
1185 struct session_info *info = session->info;
1186 struct session_info *info_last = session->info_last;
1187 DBusMessageIter iter, value;
1189 GSList *allowed_bearers;
1191 DBG("session %p", session);
1192 if (dbus_message_iter_init(msg, &iter) == FALSE)
1193 return __connman_error_invalid_arguments(msg);
1195 dbus_message_iter_get_basic(&iter, &name);
1196 dbus_message_iter_next(&iter);
1197 dbus_message_iter_recurse(&iter, &value);
1199 switch (dbus_message_iter_get_arg_type(&value)) {
1200 case DBUS_TYPE_ARRAY:
1201 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1202 allowed_bearers = session_parse_allowed_bearers(&value);
1204 g_slist_foreach(info->allowed_bearers,
1205 cleanup_bearer_info, NULL);
1206 g_slist_free(info->allowed_bearers);
1208 if (allowed_bearers == NULL) {
1209 allowed_bearers = session_allowed_bearers_any();
1211 if (allowed_bearers == NULL)
1212 return __connman_error_failed(msg, ENOMEM);
1215 info->allowed_bearers = allowed_bearers;
1217 update_allowed_bearers(session);
1222 case DBUS_TYPE_BOOLEAN:
1223 if (g_str_equal(name, "Priority") == TRUE) {
1224 dbus_message_iter_get_basic(&value,
1227 if (info_last->priority != info->priority)
1228 session->info_dirty = TRUE;
1229 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1230 dbus_message_iter_get_basic(&value,
1231 &info->avoid_handover);
1233 if (info_last->avoid_handover != info->avoid_handover)
1234 session->info_dirty = TRUE;
1235 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1236 dbus_message_iter_get_basic(&value,
1237 &info->stay_connected);
1239 if (info_last->stay_connected != info->stay_connected)
1240 session->info_dirty = TRUE;
1241 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1242 dbus_message_iter_get_basic(&value,
1245 update_ecall(session);
1250 case DBUS_TYPE_UINT32:
1251 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1252 dbus_message_iter_get_basic(&value,
1253 &info->periodic_connect);
1255 if (info_last->periodic_connect != info->periodic_connect)
1256 session->info_dirty = TRUE;
1257 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1258 dbus_message_iter_get_basic(&value,
1259 &info->idle_timeout);
1261 if (info_last->idle_timeout != info->idle_timeout)
1262 session->info_dirty = TRUE;
1267 case DBUS_TYPE_STRING:
1268 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1270 dbus_message_iter_get_basic(&value, &val);
1271 info->roaming_policy =
1272 string2roamingpolicy(val);
1274 if (info_last->roaming_policy != info->roaming_policy)
1275 session->info_dirty = TRUE;
1284 if (session->info_dirty == TRUE)
1285 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1287 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1290 return __connman_error_invalid_arguments(msg);
1293 static void release_session(gpointer key, gpointer value, gpointer user_data)
1295 struct connman_session *session = value;
1296 DBusMessage *message;
1298 DBG("owner %s path %s", session->owner, session->notify_path);
1300 if (session->notify_watch > 0)
1301 g_dbus_remove_watch(connection, session->notify_watch);
1303 g_dbus_unregister_interface(connection, session->session_path,
1304 CONNMAN_SESSION_INTERFACE);
1306 message = dbus_message_new_method_call(session->owner,
1307 session->notify_path,
1308 CONNMAN_NOTIFICATION_INTERFACE,
1310 if (message == NULL)
1313 dbus_message_set_no_reply(message, TRUE);
1315 g_dbus_send_message(connection, message);
1318 static int session_disconnect(struct connman_session *session)
1320 DBG("session %p, %s", session, session->owner);
1322 if (session->notify_watch > 0)
1323 g_dbus_remove_watch(connection, session->notify_watch);
1325 g_dbus_unregister_interface(connection, session->session_path,
1326 CONNMAN_SESSION_INTERFACE);
1328 deselect_and_disconnect(session);
1330 g_hash_table_remove(session_hash, session->session_path);
1335 static void owner_disconnect(DBusConnection *conn, void *user_data)
1337 struct connman_session *session = user_data;
1339 DBG("session %p, %s died", session, session->owner);
1341 session_disconnect(session);
1344 static DBusMessage *destroy_session(DBusConnection *conn,
1345 DBusMessage *msg, void *user_data)
1347 struct connman_session *session = user_data;
1349 DBG("session %p", session);
1351 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1354 static GDBusMethodTable session_methods[] = {
1355 { "Destroy", "", "", destroy_session },
1356 { "Connect", "", "", connect_session },
1357 { "Disconnect", "", "", disconnect_session },
1358 { "Change", "sv", "", change_session },
1362 int __connman_session_create(DBusMessage *msg)
1364 const char *owner, *notify_path;
1365 char *session_path = NULL;
1366 DBusMessageIter iter, array;
1367 struct connman_session *session = NULL;
1368 struct session_info *info, *info_last;
1370 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1371 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1372 enum connman_session_roaming_policy roaming_policy =
1373 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1374 GSList *allowed_bearers = NULL;
1375 unsigned int periodic_connect = 0;
1376 unsigned int idle_timeout = 0;
1380 owner = dbus_message_get_sender(msg);
1382 DBG("owner %s", owner);
1384 if (ecall_info != NULL) {
1386 * If there is an emergency call already going on,
1387 * ignore session creation attempt
1393 dbus_message_iter_init(msg, &iter);
1394 dbus_message_iter_recurse(&iter, &array);
1396 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1397 DBusMessageIter entry, value;
1398 const char *key, *val;
1400 dbus_message_iter_recurse(&array, &entry);
1401 dbus_message_iter_get_basic(&entry, &key);
1403 dbus_message_iter_next(&entry);
1404 dbus_message_iter_recurse(&entry, &value);
1406 switch (dbus_message_iter_get_arg_type(&value)) {
1407 case DBUS_TYPE_ARRAY:
1408 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1410 session_parse_allowed_bearers(&value);
1415 case DBUS_TYPE_BOOLEAN:
1416 if (g_str_equal(key, "Priority") == TRUE) {
1417 dbus_message_iter_get_basic(&value,
1419 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1420 dbus_message_iter_get_basic(&value,
1422 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1423 dbus_message_iter_get_basic(&value,
1425 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1426 dbus_message_iter_get_basic(&value,
1432 case DBUS_TYPE_UINT32:
1433 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1434 dbus_message_iter_get_basic(&value,
1436 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1437 dbus_message_iter_get_basic(&value,
1443 case DBUS_TYPE_STRING:
1444 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1445 dbus_message_iter_get_basic(&value, &val);
1446 roaming_policy = string2roamingpolicy(val);
1451 dbus_message_iter_next(&array);
1454 dbus_message_iter_next(&iter);
1455 dbus_message_iter_get_basic(&iter, ¬ify_path);
1457 if (notify_path == NULL) {
1462 session_path = g_strdup_printf("/sessions%s", notify_path);
1463 if (session_path == NULL) {
1468 session = g_hash_table_lookup(session_hash, session_path);
1469 if (session != NULL) {
1475 session = g_try_new0(struct connman_session, 1);
1476 if (session == NULL) {
1481 session->info = g_try_new0(struct session_info, 1);
1482 if (session->info == NULL) {
1487 session->info_last = g_try_new0(struct session_info, 1);
1488 if (session->info_last == NULL) {
1493 info = session->info;
1494 info_last = session->info_last;
1496 session->owner = g_strdup(owner);
1497 session->session_path = session_path;
1498 session->notify_path = g_strdup(notify_path);
1499 session->notify_watch =
1500 g_dbus_add_disconnect_watch(connection, session->owner,
1501 owner_disconnect, session, NULL);
1503 session->service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1506 info->online = FALSE;
1507 info->priority = priority;
1508 info->avoid_handover = avoid_handover;
1509 info->stay_connected = stay_connected;
1510 info->periodic_connect = periodic_connect;
1511 info->idle_timeout = idle_timeout;
1512 info->ecall = ecall;
1513 info->roaming_policy = roaming_policy;
1517 if (allowed_bearers == NULL) {
1518 info->allowed_bearers =
1519 session_allowed_bearers_any();
1521 if (info->allowed_bearers == NULL) {
1526 info->allowed_bearers = allowed_bearers;
1529 g_hash_table_replace(session_hash, session->session_path, session);
1531 DBG("add %s", session->session_path);
1533 if (g_dbus_register_interface(connection, session->session_path,
1534 CONNMAN_SESSION_INTERFACE,
1535 session_methods, NULL,
1536 NULL, session, NULL) == FALSE) {
1537 connman_error("Failed to register %s", session->session_path);
1538 g_hash_table_remove(session_hash, session->session_path);
1545 g_dbus_send_reply(connection, msg,
1546 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1550 update_allowed_bearers(session);
1551 if (info->ecall == TRUE) {
1553 update_ecall_sessions(session);
1556 info_last->online = info->online;
1557 info_last->priority = info->priority;
1558 info_last->avoid_handover = info->avoid_handover;
1559 info_last->stay_connected = info->stay_connected;
1560 info_last->periodic_connect = info->periodic_connect;
1561 info_last->idle_timeout = info->idle_timeout;
1562 info_last->ecall = info->ecall;
1563 info_last->roaming_policy = info->roaming_policy;
1564 info_last->entry = info->entry;
1565 info_last->marker = info->marker;
1566 info_last->allowed_bearers = info->allowed_bearers;
1568 session->info_dirty = TRUE;
1569 session->append_all = TRUE;
1571 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1576 connman_error("Failed to create session");
1578 if (session != NULL) {
1579 if (session->info_last != NULL)
1580 g_free(session->info_last);
1581 if (session->info != NULL)
1582 g_free(session->info);
1586 g_free(session_path);
1588 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1589 g_slist_free(allowed_bearers);
1594 int __connman_session_destroy(DBusMessage *msg)
1596 const char *owner, *session_path;
1597 struct connman_session *session;
1599 owner = dbus_message_get_sender(msg);
1601 DBG("owner %s", owner);
1603 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1605 if (session_path == NULL)
1608 session = g_hash_table_lookup(session_hash, session_path);
1609 if (session == NULL)
1612 if (g_strcmp0(owner, session->owner) != 0)
1615 session_disconnect(session);
1620 connman_bool_t __connman_session_mode()
1625 void __connman_session_set_mode(connman_bool_t enable)
1627 DBG("enable %d", enable);
1629 sessionmode = enable;
1631 if (sessionmode == TRUE)
1632 __connman_service_disconnect_all();
1635 static void service_add(struct connman_service *service,
1638 GHashTableIter iter;
1639 GSequenceIter *iter_service_list;
1640 gpointer key, value;
1641 struct connman_session *session;
1642 struct service_entry *entry;
1644 DBG("service %p", service);
1646 g_hash_table_iter_init(&iter, session_hash);
1648 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1651 if (service_match(session, service) == FALSE)
1654 entry = create_service_entry(service, name,
1655 CONNMAN_SERVICE_STATE_IDLE);
1660 g_sequence_insert_sorted(session->service_list,
1661 entry, sort_services,
1664 g_hash_table_replace(session->service_hash, service,
1667 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1671 static void service_remove(struct connman_service *service)
1674 GHashTableIter iter;
1675 gpointer key, value;
1676 struct connman_session *session;
1677 struct session_info *info;
1679 DBG("service %p", service);
1681 g_hash_table_iter_init(&iter, session_hash);
1683 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1684 GSequenceIter *iter;
1686 info = session->info;
1688 iter = g_hash_table_lookup(session->service_hash, service);
1692 g_sequence_remove(iter);
1694 if (info->entry != NULL && info->entry->service == service)
1696 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1700 static void service_state_changed(struct connman_service *service,
1701 enum connman_service_state state)
1703 GHashTableIter iter;
1704 gpointer key, value;
1705 struct connman_session *session;
1706 struct session_info *info, *info_last;
1708 DBG("service %p state %d", service, state);
1710 g_hash_table_iter_init(&iter, session_hash);
1712 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1713 GSequenceIter *service_iter;
1716 info = session->info;
1717 info_last = session->info_last;
1719 service_iter = g_hash_table_lookup(session->service_hash, service);
1720 if (service_iter != NULL) {
1721 struct service_entry *entry;
1723 entry = g_sequence_get(service_iter);
1724 entry->state = state;
1726 if (info->entry == entry) {
1727 info->online = is_online(entry->state);
1728 if (info_last->online != info->online)
1729 session->info_dirty = TRUE;
1733 session_changed(session,
1734 CONNMAN_SESSION_TRIGGER_SERVICE);
1738 static void ipconfig_changed(struct connman_service *service,
1739 struct connman_ipconfig *ipconfig)
1741 GHashTableIter iter;
1742 gpointer key, value;
1743 struct connman_session *session;
1744 struct session_info *info;
1745 enum connman_ipconfig_type type;
1747 DBG("service %p ipconfig %p", service, ipconfig);
1749 type = __connman_ipconfig_get_config_type(ipconfig);
1751 g_hash_table_iter_init(&iter, session_hash);
1753 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1755 info = session->info;
1757 if (info->entry != NULL && info->entry->service == service) {
1758 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1759 ipconfig_ipv4_changed(session);
1760 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1761 ipconfig_ipv6_changed(session);
1766 static struct connman_notifier session_notifier = {
1768 .service_add = service_add,
1769 .service_remove = service_remove,
1770 .service_state_changed = service_state_changed,
1771 .ipconfig_changed = ipconfig_changed,
1774 int __connman_session_init(void)
1780 connection = connman_dbus_get_connection();
1781 if (connection == NULL)
1784 err = connman_notifier_register(&session_notifier);
1786 dbus_connection_unref(connection);
1790 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1791 NULL, cleanup_session);
1793 sessionmode = FALSE;
1797 void __connman_session_cleanup(void)
1801 if (connection == NULL)
1804 connman_notifier_unregister(&session_notifier);
1806 g_hash_table_foreach(session_hash, release_session, NULL);
1807 g_hash_table_destroy(session_hash);
1808 session_hash = NULL;
1810 dbus_connection_unref(connection);