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;
790 if (info->entry == NULL)
793 disconnect = explicit_disconnect(info);
795 online = is_connecting(info->entry->state) == TRUE ||
796 is_online(info->entry->state) == TRUE;
798 info->online = FALSE;
799 info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
801 service = info->entry->service;
804 DBG("disconnect %d online %d", disconnect, online);
806 if (disconnect == TRUE && online == TRUE)
807 g_timeout_add_seconds(0, call_disconnect, service);
810 static void deselect_and_disconnect(struct connman_session *session,
811 enum connman_session_reason reason)
813 struct session_info *info = session->info;
815 deselect_service(info);
817 info->reason = reason;
820 static void select_online_service(struct session_info *info,
821 struct service_entry *entry)
826 info->entry->reason = info->reason;
828 if (explicit_connect(info->reason) == FALSE)
831 __connman_service_session_inc(info->entry->service);
834 static void select_offline_service(struct session_info *info,
835 struct service_entry *entry)
837 if (explicit_connect(info->reason) == FALSE) {
838 /* Don't select this service. It is not online and we
839 * don't call connect on it. This happends for example
840 * when the system is idle and we create a new session
841 * which is in free ride mode.
843 deselect_service(info);
847 info->online = FALSE;
850 info->entry->reason = info->reason;
852 __connman_service_session_inc(info->entry->service);
853 g_timeout_add_seconds(0, call_connect, info->entry->service);
856 static void select_service(struct session_info *info,
857 struct service_entry *entry)
859 DBG("service %p", entry->service);
861 if (is_online(entry->state) == TRUE)
862 select_online_service(info, entry);
864 select_offline_service(info, entry);
867 static void select_and_connect(struct connman_session *session,
868 enum connman_session_reason reason)
870 struct session_info *info = session->info;
871 struct service_entry *entry = NULL;
874 DBG("session %p reason %s", session, reason2string(reason));
876 info->reason = reason;
878 iter = g_sequence_get_begin_iter(session->service_list);
880 while (g_sequence_iter_is_end(iter) == FALSE) {
881 entry = g_sequence_get(iter);
883 switch (entry->state) {
884 case CONNMAN_SERVICE_STATE_ASSOCIATION:
885 case CONNMAN_SERVICE_STATE_CONFIGURATION:
886 case CONNMAN_SERVICE_STATE_READY:
887 case CONNMAN_SERVICE_STATE_ONLINE:
888 case CONNMAN_SERVICE_STATE_IDLE:
889 case CONNMAN_SERVICE_STATE_DISCONNECT:
890 select_service(info, entry);
892 case CONNMAN_SERVICE_STATE_UNKNOWN:
893 case CONNMAN_SERVICE_STATE_FAILURE:
897 iter = g_sequence_iter_next(iter);
901 static void session_changed(struct connman_session *session,
902 enum connman_session_trigger trigger)
904 struct session_info *info = session->info;
905 struct session_info *info_last = session->info_last;
909 * TODO: This only a placeholder for the 'real' algorithm to
910 * play a bit around. So we are going to improve it step by step.
913 DBG("session %p trigger %s reason %s", session, trigger2string(trigger),
914 reason2string(info->reason));
917 case CONNMAN_SESSION_TRIGGER_UNKNOWN:
918 DBG("ignore session changed event");
920 case CONNMAN_SESSION_TRIGGER_SETTING:
921 if (info->entry != NULL) {
922 iter = g_hash_table_lookup(session->service_hash,
923 info->entry->service);
926 * This service is not part of this
929 deselect_and_disconnect(session, info->reason);
933 if (info->online == FALSE) {
934 select_and_connect(session,
935 CONNMAN_SESSION_REASON_FREE_RIDE);
939 case CONNMAN_SESSION_TRIGGER_CONNECT:
940 if (info->online == TRUE) {
941 if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
943 info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
944 __connman_service_session_inc(info->entry->service);
948 select_and_connect(session,
949 CONNMAN_SESSION_REASON_CONNECT);
952 case CONNMAN_SESSION_TRIGGER_DISCONNECT:
953 deselect_and_disconnect(session,
954 CONNMAN_SESSION_REASON_DISCONNECT);
957 case CONNMAN_SESSION_TRIGGER_PERIODIC:
958 if (info->online == TRUE) {
959 info->entry->reason = CONNMAN_SESSION_REASON_PERIODIC;
960 __connman_service_session_inc(info->entry->service);
964 select_and_connect(session,
965 CONNMAN_SESSION_REASON_PERIODIC);
968 case CONNMAN_SESSION_TRIGGER_SERVICE:
969 if (info->entry != NULL &&
970 (is_connecting(info->entry->state) == TRUE ||
971 is_online(info->entry->state) == TRUE)) {
975 switch (info->reason) {
976 case CONNMAN_SESSION_REASON_CONNECT:
978 * We are not online, we are not connecting, that
979 * means we could still have a valid info->entry.
980 * Though something has changed from the service layer.
981 * Therefore we want to restart the algorithm. Before we
982 * can do that we have to cleanup a potientional old entry.
984 deselect_and_disconnect(session,
985 CONNMAN_SESSION_REASON_CONNECT);
987 DBG("Retry to find a matching session");
989 * The user called Connect() but there was no
990 * matching session available at this point.
991 * Now there might be a new one. Let's retry
992 * to select and connect
994 select_and_connect(session,
995 CONNMAN_SESSION_REASON_CONNECT);
997 case CONNMAN_SESSION_REASON_PERIODIC:
998 case CONNMAN_SESSION_REASON_FREE_RIDE:
999 if (info->stay_connected == TRUE) {
1000 DBG("StayConnected");
1001 select_and_connect(session,
1002 CONNMAN_SESSION_REASON_CONNECT);
1004 select_and_connect(session,
1005 CONNMAN_SESSION_REASON_FREE_RIDE);
1008 case CONNMAN_SESSION_REASON_DISCONNECT:
1009 case CONNMAN_SESSION_REASON_UNKNOWN:
1013 case CONNMAN_SESSION_TRIGGER_ECALL:
1014 if (info->online == FALSE && info->entry != NULL &&
1015 info->entry->service != NULL) {
1016 deselect_and_disconnect(session, info->reason);
1022 if (info->entry != info_last->entry)
1023 session->info_dirty = TRUE;
1025 session_notify(session);
1028 static DBusMessage *connect_session(DBusConnection *conn,
1029 DBusMessage *msg, void *user_data)
1031 struct connman_session *session = user_data;
1032 struct session_info *info = session->info;
1034 DBG("session %p", session);
1036 if (ecall_info != NULL && ecall_info != info)
1037 return __connman_error_failed(msg, EBUSY);
1039 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1041 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1044 static DBusMessage *disconnect_session(DBusConnection *conn,
1045 DBusMessage *msg, void *user_data)
1047 struct connman_session *session = user_data;
1048 struct session_info *info = session->info;
1050 DBG("session %p", session);
1052 if (ecall_info != NULL && ecall_info != info)
1053 return __connman_error_failed(msg, EBUSY);
1055 session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1057 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1060 static struct service_entry *create_service_entry(struct connman_service *service,
1062 enum connman_service_state state)
1064 struct service_entry *entry;
1065 enum connman_service_type type;
1068 entry = g_try_new0(struct service_entry, 1);
1072 entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1073 entry->state = state;
1078 entry->service = service;
1080 idx = __connman_service_get_index(entry->service);
1081 entry->ifname = connman_inet_ifname(idx);
1082 if (entry->ifname == NULL)
1083 entry->ifname = g_strdup("");
1085 type = connman_service_get_type(entry->service);
1086 entry->bearer = service2bearer(type);
1091 static void destroy_service_entry(gpointer data)
1093 struct service_entry *entry = data;
1095 g_free(entry->ifname);
1100 static void update_allowed_bearers(struct connman_session *session)
1102 struct service_entry *entry;
1103 GSequenceIter *iter;
1105 if (session->service_list != NULL) {
1106 g_hash_table_remove_all(session->service_hash);
1107 g_sequence_free(session->service_list);
1110 session->service_list = __connman_service_get_list(session,
1112 create_service_entry,
1113 destroy_service_entry);
1115 g_sequence_sort(session->service_list, sort_services, session);
1117 iter = g_sequence_get_begin_iter(session->service_list);
1119 while (g_sequence_iter_is_end(iter) == FALSE) {
1120 entry = g_sequence_get(iter);
1122 DBG("service %p type %s name %s", entry->service,
1123 service2bearer(connman_service_get_type(entry->service)),
1126 g_hash_table_replace(session->service_hash,
1127 entry->service, iter);
1129 iter = g_sequence_iter_next(iter);
1132 session->info_dirty = TRUE;
1135 static void update_ecall_sessions(struct connman_session *session)
1137 struct session_info *info = session->info;
1138 struct connman_session *session_iter;
1139 GHashTableIter iter;
1140 gpointer key, value;
1142 g_hash_table_iter_init(&iter, session_hash);
1144 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1145 session_iter = value;
1147 if (session_iter == session)
1150 session_iter->info->ecall = info->ecall;
1151 session_iter->info_dirty = TRUE;
1153 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
1157 static void update_ecall(struct connman_session *session)
1159 struct session_info *info = session->info;
1160 struct session_info *info_last = session->info_last;
1162 DBG("session %p ecall_info %p ecall %d -> %d", session,
1163 ecall_info, info_last->ecall, info->ecall);
1165 if (ecall_info == NULL) {
1166 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
1170 } else if (ecall_info == info) {
1171 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
1179 update_ecall_sessions(session);
1181 session->info_dirty = TRUE;
1185 /* not a valid transition */
1186 info->ecall = info_last->ecall;
1189 static DBusMessage *change_session(DBusConnection *conn,
1190 DBusMessage *msg, void *user_data)
1192 struct connman_session *session = user_data;
1193 struct session_info *info = session->info;
1194 struct session_info *info_last = session->info_last;
1195 DBusMessageIter iter, value;
1197 GSList *allowed_bearers;
1199 DBG("session %p", session);
1200 if (dbus_message_iter_init(msg, &iter) == FALSE)
1201 return __connman_error_invalid_arguments(msg);
1203 dbus_message_iter_get_basic(&iter, &name);
1204 dbus_message_iter_next(&iter);
1205 dbus_message_iter_recurse(&iter, &value);
1207 switch (dbus_message_iter_get_arg_type(&value)) {
1208 case DBUS_TYPE_ARRAY:
1209 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1210 allowed_bearers = session_parse_allowed_bearers(&value);
1212 g_slist_foreach(info->allowed_bearers,
1213 cleanup_bearer_info, NULL);
1214 g_slist_free(info->allowed_bearers);
1216 if (allowed_bearers == NULL) {
1217 allowed_bearers = session_allowed_bearers_any();
1219 if (allowed_bearers == NULL)
1220 return __connman_error_failed(msg, ENOMEM);
1223 info->allowed_bearers = allowed_bearers;
1225 update_allowed_bearers(session);
1230 case DBUS_TYPE_BOOLEAN:
1231 if (g_str_equal(name, "Priority") == TRUE) {
1232 dbus_message_iter_get_basic(&value,
1235 if (info_last->priority != info->priority)
1236 session->info_dirty = TRUE;
1237 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1238 dbus_message_iter_get_basic(&value,
1239 &info->avoid_handover);
1241 if (info_last->avoid_handover != info->avoid_handover)
1242 session->info_dirty = TRUE;
1243 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1244 dbus_message_iter_get_basic(&value,
1245 &info->stay_connected);
1247 if (info_last->stay_connected != info->stay_connected)
1248 session->info_dirty = TRUE;
1249 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1250 dbus_message_iter_get_basic(&value,
1253 update_ecall(session);
1258 case DBUS_TYPE_UINT32:
1259 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1260 dbus_message_iter_get_basic(&value,
1261 &info->periodic_connect);
1263 if (info_last->periodic_connect != info->periodic_connect)
1264 session->info_dirty = TRUE;
1265 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1266 dbus_message_iter_get_basic(&value,
1267 &info->idle_timeout);
1269 if (info_last->idle_timeout != info->idle_timeout)
1270 session->info_dirty = TRUE;
1275 case DBUS_TYPE_STRING:
1276 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1278 dbus_message_iter_get_basic(&value, &val);
1279 info->roaming_policy =
1280 string2roamingpolicy(val);
1282 if (info_last->roaming_policy != info->roaming_policy)
1283 session->info_dirty = TRUE;
1292 if (session->info_dirty == TRUE)
1293 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1295 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1298 return __connman_error_invalid_arguments(msg);
1301 static void release_session(gpointer key, gpointer value, gpointer user_data)
1303 struct connman_session *session = value;
1304 DBusMessage *message;
1306 DBG("owner %s path %s", session->owner, session->notify_path);
1308 if (session->notify_watch > 0)
1309 g_dbus_remove_watch(connection, session->notify_watch);
1311 g_dbus_unregister_interface(connection, session->session_path,
1312 CONNMAN_SESSION_INTERFACE);
1314 message = dbus_message_new_method_call(session->owner,
1315 session->notify_path,
1316 CONNMAN_NOTIFICATION_INTERFACE,
1318 if (message == NULL)
1321 dbus_message_set_no_reply(message, TRUE);
1323 g_dbus_send_message(connection, message);
1326 static int session_disconnect(struct connman_session *session)
1328 DBG("session %p, %s", session, session->owner);
1330 if (session->notify_watch > 0)
1331 g_dbus_remove_watch(connection, session->notify_watch);
1333 g_dbus_unregister_interface(connection, session->session_path,
1334 CONNMAN_SESSION_INTERFACE);
1336 deselect_and_disconnect(session,
1337 CONNMAN_SESSION_REASON_DISCONNECT);
1339 g_hash_table_remove(session_hash, session->session_path);
1344 static void owner_disconnect(DBusConnection *conn, void *user_data)
1346 struct connman_session *session = user_data;
1348 DBG("session %p, %s died", session, session->owner);
1350 session_disconnect(session);
1353 static DBusMessage *destroy_session(DBusConnection *conn,
1354 DBusMessage *msg, void *user_data)
1356 struct connman_session *session = user_data;
1358 DBG("session %p", session);
1360 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1363 static GDBusMethodTable session_methods[] = {
1364 { "Destroy", "", "", destroy_session },
1365 { "Connect", "", "", connect_session },
1366 { "Disconnect", "", "", disconnect_session },
1367 { "Change", "sv", "", change_session },
1371 int __connman_session_create(DBusMessage *msg)
1373 const char *owner, *notify_path;
1374 char *session_path = NULL;
1375 DBusMessageIter iter, array;
1376 struct connman_session *session = NULL;
1377 struct session_info *info, *info_last;
1379 connman_bool_t priority = FALSE, avoid_handover = FALSE;
1380 connman_bool_t stay_connected = FALSE, ecall = FALSE;
1381 enum connman_session_roaming_policy roaming_policy =
1382 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1383 GSList *allowed_bearers = NULL;
1384 unsigned int periodic_connect = 0;
1385 unsigned int idle_timeout = 0;
1389 owner = dbus_message_get_sender(msg);
1391 DBG("owner %s", owner);
1393 if (ecall_info != NULL) {
1395 * If there is an emergency call already going on,
1396 * ignore session creation attempt
1402 dbus_message_iter_init(msg, &iter);
1403 dbus_message_iter_recurse(&iter, &array);
1405 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1406 DBusMessageIter entry, value;
1407 const char *key, *val;
1409 dbus_message_iter_recurse(&array, &entry);
1410 dbus_message_iter_get_basic(&entry, &key);
1412 dbus_message_iter_next(&entry);
1413 dbus_message_iter_recurse(&entry, &value);
1415 switch (dbus_message_iter_get_arg_type(&value)) {
1416 case DBUS_TYPE_ARRAY:
1417 if (g_str_equal(key, "AllowedBearers") == TRUE) {
1419 session_parse_allowed_bearers(&value);
1424 case DBUS_TYPE_BOOLEAN:
1425 if (g_str_equal(key, "Priority") == TRUE) {
1426 dbus_message_iter_get_basic(&value,
1428 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1429 dbus_message_iter_get_basic(&value,
1431 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1432 dbus_message_iter_get_basic(&value,
1434 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1435 dbus_message_iter_get_basic(&value,
1441 case DBUS_TYPE_UINT32:
1442 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1443 dbus_message_iter_get_basic(&value,
1445 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1446 dbus_message_iter_get_basic(&value,
1452 case DBUS_TYPE_STRING:
1453 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1454 dbus_message_iter_get_basic(&value, &val);
1455 roaming_policy = string2roamingpolicy(val);
1460 dbus_message_iter_next(&array);
1463 dbus_message_iter_next(&iter);
1464 dbus_message_iter_get_basic(&iter, ¬ify_path);
1466 if (notify_path == NULL) {
1471 session_path = g_strdup_printf("/sessions%s", notify_path);
1472 if (session_path == NULL) {
1477 session = g_hash_table_lookup(session_hash, session_path);
1478 if (session != NULL) {
1484 session = g_try_new0(struct connman_session, 1);
1485 if (session == NULL) {
1490 session->info = g_try_new0(struct session_info, 1);
1491 if (session->info == NULL) {
1496 session->info_last = g_try_new0(struct session_info, 1);
1497 if (session->info_last == NULL) {
1502 info = session->info;
1503 info_last = session->info_last;
1505 session->owner = g_strdup(owner);
1506 session->session_path = session_path;
1507 session->notify_path = g_strdup(notify_path);
1508 session->notify_watch =
1509 g_dbus_add_disconnect_watch(connection, session->owner,
1510 owner_disconnect, session, NULL);
1512 session->service_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1515 info->online = FALSE;
1516 info->priority = priority;
1517 info->avoid_handover = avoid_handover;
1518 info->stay_connected = stay_connected;
1519 info->periodic_connect = periodic_connect;
1520 info->idle_timeout = idle_timeout;
1521 info->ecall = ecall;
1522 info->roaming_policy = roaming_policy;
1526 if (allowed_bearers == NULL) {
1527 info->allowed_bearers =
1528 session_allowed_bearers_any();
1530 if (info->allowed_bearers == NULL) {
1535 info->allowed_bearers = allowed_bearers;
1538 g_hash_table_replace(session_hash, session->session_path, session);
1540 DBG("add %s", session->session_path);
1542 if (g_dbus_register_interface(connection, session->session_path,
1543 CONNMAN_SESSION_INTERFACE,
1544 session_methods, NULL,
1545 NULL, session, NULL) == FALSE) {
1546 connman_error("Failed to register %s", session->session_path);
1547 g_hash_table_remove(session_hash, session->session_path);
1554 g_dbus_send_reply(connection, msg,
1555 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1559 update_allowed_bearers(session);
1560 if (info->ecall == TRUE) {
1562 update_ecall_sessions(session);
1565 info_last->online = info->online;
1566 info_last->priority = info->priority;
1567 info_last->avoid_handover = info->avoid_handover;
1568 info_last->stay_connected = info->stay_connected;
1569 info_last->periodic_connect = info->periodic_connect;
1570 info_last->idle_timeout = info->idle_timeout;
1571 info_last->ecall = info->ecall;
1572 info_last->roaming_policy = info->roaming_policy;
1573 info_last->entry = info->entry;
1574 info_last->marker = info->marker;
1575 info_last->allowed_bearers = info->allowed_bearers;
1577 session->info_dirty = TRUE;
1578 session->append_all = TRUE;
1580 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1585 connman_error("Failed to create session");
1587 if (session != NULL) {
1588 if (session->info_last != NULL)
1589 g_free(session->info_last);
1590 if (session->info != NULL)
1591 g_free(session->info);
1595 g_free(session_path);
1597 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1598 g_slist_free(allowed_bearers);
1603 int __connman_session_destroy(DBusMessage *msg)
1605 const char *owner, *session_path;
1606 struct connman_session *session;
1608 owner = dbus_message_get_sender(msg);
1610 DBG("owner %s", owner);
1612 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1614 if (session_path == NULL)
1617 session = g_hash_table_lookup(session_hash, session_path);
1618 if (session == NULL)
1621 if (g_strcmp0(owner, session->owner) != 0)
1624 session_disconnect(session);
1629 connman_bool_t __connman_session_mode()
1634 void __connman_session_set_mode(connman_bool_t enable)
1636 DBG("enable %d", enable);
1638 sessionmode = enable;
1640 if (sessionmode == TRUE)
1641 __connman_service_disconnect_all();
1644 static void service_add(struct connman_service *service,
1647 GHashTableIter iter;
1648 GSequenceIter *iter_service_list;
1649 gpointer key, value;
1650 struct connman_session *session;
1651 struct service_entry *entry;
1653 DBG("service %p", service);
1655 g_hash_table_iter_init(&iter, session_hash);
1657 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1660 if (service_match(session, service) == FALSE)
1663 entry = create_service_entry(service, name,
1664 CONNMAN_SERVICE_STATE_IDLE);
1669 g_sequence_insert_sorted(session->service_list,
1670 entry, sort_services,
1673 g_hash_table_replace(session->service_hash, service,
1676 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1680 static void service_remove(struct connman_service *service)
1683 GHashTableIter iter;
1684 gpointer key, value;
1685 struct connman_session *session;
1686 struct session_info *info;
1688 DBG("service %p", service);
1690 g_hash_table_iter_init(&iter, session_hash);
1692 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1693 GSequenceIter *iter;
1695 info = session->info;
1697 iter = g_hash_table_lookup(session->service_hash, service);
1701 g_sequence_remove(iter);
1703 if (info->entry != NULL && info->entry->service == service)
1705 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1709 static void service_state_changed(struct connman_service *service,
1710 enum connman_service_state state)
1712 GHashTableIter iter;
1713 gpointer key, value;
1714 struct connman_session *session;
1715 struct session_info *info, *info_last;
1717 DBG("service %p state %d", service, state);
1719 g_hash_table_iter_init(&iter, session_hash);
1721 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1722 GSequenceIter *service_iter;
1725 info = session->info;
1726 info_last = session->info_last;
1728 service_iter = g_hash_table_lookup(session->service_hash, service);
1729 if (service_iter != NULL) {
1730 struct service_entry *entry;
1732 entry = g_sequence_get(service_iter);
1733 entry->state = state;
1735 if (info->entry == entry) {
1736 info->online = is_online(entry->state);
1737 if (info_last->online != info->online)
1738 session->info_dirty = TRUE;
1742 session_changed(session,
1743 CONNMAN_SESSION_TRIGGER_SERVICE);
1747 static void ipconfig_changed(struct connman_service *service,
1748 struct connman_ipconfig *ipconfig)
1750 GHashTableIter iter;
1751 gpointer key, value;
1752 struct connman_session *session;
1753 struct session_info *info;
1754 enum connman_ipconfig_type type;
1756 DBG("service %p ipconfig %p", service, ipconfig);
1758 type = __connman_ipconfig_get_config_type(ipconfig);
1760 g_hash_table_iter_init(&iter, session_hash);
1762 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1764 info = session->info;
1766 if (info->entry != NULL && info->entry->service == service) {
1767 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1768 ipconfig_ipv4_changed(session);
1769 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1770 ipconfig_ipv6_changed(session);
1775 static struct connman_notifier session_notifier = {
1777 .service_add = service_add,
1778 .service_remove = service_remove,
1779 .service_state_changed = service_state_changed,
1780 .ipconfig_changed = ipconfig_changed,
1783 int __connman_session_init(void)
1789 connection = connman_dbus_get_connection();
1790 if (connection == NULL)
1793 err = connman_notifier_register(&session_notifier);
1795 dbus_connection_unref(connection);
1799 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1800 NULL, cleanup_session);
1802 sessionmode = FALSE;
1806 void __connman_session_cleanup(void)
1810 if (connection == NULL)
1813 connman_notifier_unregister(&session_notifier);
1815 g_hash_table_foreach(session_hash, release_session, NULL);
1816 g_hash_table_destroy(session_hash);
1817 session_hash = NULL;
1819 dbus_connection_unref(connection);