5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * Copyright (C) 2011 BWM CarIT GmbH. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 static DBusConnection *connection;
32 static GHashTable *session_hash;
33 static connman_bool_t sessionmode;
35 enum connman_session_roaming_policy {
36 CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN = 0,
37 CONNMAN_SESSION_ROAMING_POLICY_DEFAULT = 1,
38 CONNMAN_SESSION_ROAMING_POLICY_ALWAYS = 2,
39 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN = 3,
40 CONNMAN_SESSION_ROAMING_POLICY_NATIONAL = 4,
41 CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL = 5,
48 connman_bool_t connect;
49 connman_bool_t online;
50 connman_bool_t priority;
51 GSList *allowed_bearers;
52 connman_bool_t avoid_handover;
53 connman_bool_t stay_connected;
54 unsigned int periodic_connect;
55 unsigned int idle_timeout;
57 enum connman_session_roaming_policy roaming_policy;
60 struct connman_service *service;
63 struct connman_session {
69 connman_bool_t info_dirty;
70 struct session_info info;
71 struct session_info info_last;
73 GSequence *service_list;
78 connman_bool_t match_all;
79 enum connman_service_type service_type;
82 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
85 case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
87 case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
89 case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
91 case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
93 case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
95 case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
96 return "international";
102 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
104 if (g_strcmp0(policy, "default") == 0)
105 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
106 else if (g_strcmp0(policy, "always") == 0)
107 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
108 else if (g_strcmp0(policy, "forbidden") == 0)
109 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
110 else if (g_strcmp0(policy, "national") == 0)
111 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
112 else if (g_strcmp0(policy, "international") == 0)
113 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
115 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
118 static enum connman_service_type bearer2service(const char *bearer)
121 return CONNMAN_SERVICE_TYPE_UNKNOWN;
123 if (g_strcmp0(bearer, "ethernet") == 0)
124 return CONNMAN_SERVICE_TYPE_ETHERNET;
125 else if (g_strcmp0(bearer, "wifi") == 0)
126 return CONNMAN_SERVICE_TYPE_WIFI;
127 else if (g_strcmp0(bearer, "wimax") == 0)
128 return CONNMAN_SERVICE_TYPE_WIMAX;
129 else if (g_strcmp0(bearer, "bluetooth") == 0)
130 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
131 else if (g_strcmp0(bearer, "3g") == 0)
132 return CONNMAN_SERVICE_TYPE_CELLULAR;
134 return CONNMAN_SERVICE_TYPE_UNKNOWN;
137 static char *service2bearer(enum connman_service_type type)
140 case CONNMAN_SERVICE_TYPE_ETHERNET:
142 case CONNMAN_SERVICE_TYPE_WIFI:
144 case CONNMAN_SERVICE_TYPE_WIMAX:
146 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
148 case CONNMAN_SERVICE_TYPE_CELLULAR:
150 case CONNMAN_SERVICE_TYPE_UNKNOWN:
151 case CONNMAN_SERVICE_TYPE_SYSTEM:
152 case CONNMAN_SERVICE_TYPE_GPS:
153 case CONNMAN_SERVICE_TYPE_VPN:
154 case CONNMAN_SERVICE_TYPE_GADGET:
161 static char *session2bearer(struct connman_session *session)
163 struct session_info *info = &session->info;
165 struct bearer_info *bearer_info;
166 enum connman_service_type type;
168 if (info->service == NULL)
171 type = connman_service_get_type(info->service);
173 for (list = info->allowed_bearers;
174 list != NULL; list = list->next) {
175 bearer_info = list->data;
177 if (bearer_info->match_all)
178 return service2bearer(type);
180 if (bearer_info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN)
181 return bearer_info->name;
183 if (bearer_info->service_type == type)
184 return service2bearer(type);
191 static void cleanup_bearer_info(gpointer data, gpointer user_data)
193 struct bearer_info *info = data;
199 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
201 struct bearer_info *info;
202 DBusMessageIter array;
205 dbus_message_iter_recurse(iter, &array);
207 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
210 dbus_message_iter_get_basic(&array, &bearer);
212 info = g_try_new0(struct bearer_info, 1);
214 g_slist_foreach(list, cleanup_bearer_info, NULL);
220 info->name = g_strdup(bearer);
221 info->service_type = bearer2service(info->name);
223 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
224 g_strcmp0(info->name, "*") == 0) {
225 info->match_all = TRUE;
227 info->match_all = FALSE;
230 list = g_slist_append(list, info);
232 dbus_message_iter_next(&array);
238 static GSList *session_allowed_bearers_any(void)
240 struct bearer_info *info;
243 info = g_try_new0(struct bearer_info, 1);
250 info->name = g_strdup("");
251 info->match_all = TRUE;
252 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
254 list = g_slist_append(list, info);
259 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
261 struct session_info *info = user_data;
264 for (list = info->allowed_bearers;
265 list != NULL; list = list->next) {
266 struct bearer_info *info = list->data;
268 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
273 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
275 struct connman_service *service = user_data;
276 struct connman_ipconfig *ipconfig_ipv4;
281 ipconfig_ipv4 = __connman_service_get_ip4config(service);
282 if (ipconfig_ipv4 == NULL)
285 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
288 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
290 struct connman_service *service = user_data;
291 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
296 ipconfig_ipv4 = __connman_service_get_ip4config(service);
297 ipconfig_ipv6 = __connman_service_get_ip6config(service);
298 if (ipconfig_ipv6 == NULL)
301 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
304 static void append_notify(DBusMessageIter *dict,
305 struct connman_session *session)
307 struct session_info *info = &session->info;
308 struct session_info *info_last = &session->info_last;
311 if (info->bearer != info_last->bearer) {
312 connman_dbus_dict_append_basic(dict, "Bearer",
315 info_last->bearer = info->bearer;
318 if (info->online != info_last->online) {
319 connman_dbus_dict_append_basic(dict, "Online",
322 info_last->online = info->online;
325 if (info->name != info_last->name) {
326 connman_dbus_dict_append_basic(dict, "Name",
329 info_last->name = info->name;
332 if (info->service != info_last->service) {
333 connman_dbus_dict_append_dict(dict, "IPv4",
334 append_ipconfig_ipv4,
337 connman_dbus_dict_append_dict(dict, "IPv6",
338 append_ipconfig_ipv6,
341 connman_dbus_dict_append_basic(dict, "Interface",
345 info_last->service = info->service;
349 if (info->priority != info_last->priority) {
350 connman_dbus_dict_append_basic(dict, "Priority",
353 info_last->priority = info->priority;
356 if (info->allowed_bearers != info_last->allowed_bearers) {
357 connman_dbus_dict_append_array(dict, "AllowedBearers",
359 append_allowed_bearers,
361 info_last->allowed_bearers = info->allowed_bearers;
364 if (info->avoid_handover != info_last->avoid_handover) {
365 connman_dbus_dict_append_basic(dict, "AvoidHandover",
367 &info->avoid_handover);
368 info_last->avoid_handover = info->avoid_handover;
371 if (info->stay_connected != info_last->stay_connected) {
372 connman_dbus_dict_append_basic(dict, "StayConnected",
374 &info->stay_connected);
375 info_last->stay_connected = info->stay_connected;
378 if (info->periodic_connect != info_last->periodic_connect) {
379 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
381 &info->periodic_connect);
382 info_last->periodic_connect = info->periodic_connect;
385 if (info->idle_timeout != info_last->idle_timeout) {
386 connman_dbus_dict_append_basic(dict, "IdleTimeout",
388 &info->idle_timeout);
389 info_last->idle_timeout = info->idle_timeout;
392 if (info->ecall != info_last->ecall) {
393 connman_dbus_dict_append_basic(dict, "EmergencyCall",
396 info_last->ecall = info->ecall;
399 if (info->roaming_policy != info_last->roaming_policy) {
400 policy = roamingpolicy2string(info->roaming_policy);
401 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
404 info_last->roaming_policy = info->roaming_policy;
407 if (info->marker != info_last->marker) {
408 connman_dbus_dict_append_basic(dict, "SessionMarker",
411 info_last->marker = info->marker;
414 session->info_dirty = FALSE;
417 static int session_notify(struct connman_session *session)
420 DBusMessageIter array, dict;
422 if (session->info_dirty == FALSE)
425 DBG("session %p owner %s notify_path %s", session,
426 session->owner, session->notify_path);
428 msg = dbus_message_new_method_call(session->owner, session->notify_path,
429 CONNMAN_NOTIFICATION_INTERFACE,
434 dbus_message_iter_init_append(msg, &array);
435 connman_dbus_dict_open(&array, &dict);
437 append_notify(&dict, session);
439 connman_dbus_dict_close(&array, &dict);
441 g_dbus_send_message(connection, msg);
443 session->info_dirty = FALSE;
448 static void ipconfig_ipv4_changed(struct connman_session *session)
450 struct session_info *info = &session->info;
452 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
453 "IPv4", append_ipconfig_ipv4,
457 static void ipconfig_ipv6_changed(struct connman_session *session)
459 struct session_info *info = &session->info;
461 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
462 "IPv6", append_ipconfig_ipv6,
466 static connman_bool_t service_type_match(struct connman_session *session,
467 struct connman_service *service)
469 struct session_info *info = &session->info;
472 for (list = info->allowed_bearers;
473 list != NULL; list = list->next) {
474 struct bearer_info *info = list->data;
475 enum connman_service_type service_type;
477 if (info->match_all == TRUE)
480 service_type = connman_service_get_type(service);
481 if (info->service_type == service_type)
488 static connman_bool_t service_match(struct connman_session *session,
489 struct connman_service *service)
491 if (service_type_match(session, service) == FALSE)
497 static int service_type_weight(enum connman_service_type type)
500 * The session doesn't care which service
501 * to use. Nevertheless we have to sort them
502 * according their type. The ordering is
511 case CONNMAN_SERVICE_TYPE_ETHERNET:
513 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
515 case CONNMAN_SERVICE_TYPE_WIFI:
516 case CONNMAN_SERVICE_TYPE_WIMAX:
518 case CONNMAN_SERVICE_TYPE_CELLULAR:
520 case CONNMAN_SERVICE_TYPE_UNKNOWN:
521 case CONNMAN_SERVICE_TYPE_SYSTEM:
522 case CONNMAN_SERVICE_TYPE_GPS:
523 case CONNMAN_SERVICE_TYPE_VPN:
524 case CONNMAN_SERVICE_TYPE_GADGET:
531 static gint sort_allowed_bearers(struct connman_service *service_a,
532 struct connman_service *service_b,
533 struct connman_session *session)
535 struct session_info *info = &session->info;
537 enum connman_service_type type_a, type_b;
538 int weight_a, weight_b;
540 type_a = connman_service_get_type(service_a);
541 type_b = connman_service_get_type(service_b);
543 for (list = info->allowed_bearers;
544 list != NULL; list = list->next) {
545 struct bearer_info *info = list->data;
547 if (info->match_all == TRUE) {
548 if (type_a != type_b) {
549 weight_a = service_type_weight(type_a);
550 weight_b = service_type_weight(type_b);
552 if (weight_a > weight_b)
555 if (weight_a < weight_b)
562 if (type_a == info->service_type &&
563 type_b == info->service_type) {
567 if (type_a == info->service_type &&
568 type_b != info->service_type) {
572 if (type_a != info->service_type &&
573 type_b == info->service_type) {
581 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
583 struct connman_service *service_a = (void *)a;
584 struct connman_service *service_b = (void *)b;
585 struct connman_session *session = user_data;
587 return sort_allowed_bearers(service_a, service_b, session);
590 static void cleanup_session(gpointer user_data)
592 struct connman_session *session = user_data;
593 struct session_info *info = &session->info;
595 DBG("remove %s", session->session_path);
597 g_sequence_free(session->service_list);
599 g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
600 g_slist_free(info->allowed_bearers);
602 g_free(session->owner);
603 g_free(session->session_path);
604 g_free(session->notify_path);
609 static void release_session(gpointer key, gpointer value, gpointer user_data)
611 struct connman_session *session = value;
612 DBusMessage *message;
614 DBG("owner %s path %s", session->owner, session->notify_path);
616 if (session->notify_watch > 0)
617 g_dbus_remove_watch(connection, session->notify_watch);
619 g_dbus_unregister_interface(connection, session->session_path,
620 CONNMAN_SESSION_INTERFACE);
622 message = dbus_message_new_method_call(session->owner,
623 session->notify_path,
624 CONNMAN_NOTIFICATION_INTERFACE,
629 dbus_message_set_no_reply(message, TRUE);
631 g_dbus_send_message(connection, message);
634 static int session_disconnect(struct connman_session *session)
636 DBG("session %p, %s", session, session->owner);
638 if (session->notify_watch > 0)
639 g_dbus_remove_watch(connection, session->notify_watch);
641 g_dbus_unregister_interface(connection, session->session_path,
642 CONNMAN_SESSION_INTERFACE);
644 g_hash_table_remove(session_hash, session->session_path);
649 static void owner_disconnect(DBusConnection *conn, void *user_data)
651 struct connman_session *session = user_data;
653 DBG("session %p, %s died", session, session->owner);
655 session_disconnect(session);
658 static DBusMessage *destroy_session(DBusConnection *conn,
659 DBusMessage *msg, void *user_data)
661 struct connman_session *session = user_data;
663 DBG("session %p", session);
665 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
668 static gboolean session_changed_connect(gpointer user_data)
670 struct connman_session *session = user_data;
671 struct session_info *info = &session->info;
673 __connman_service_connect(info->service);
678 static void update_service(struct connman_session *session)
680 struct session_info *info = &session->info;
683 if (info->service != NULL) {
684 info->bearer = session2bearer(session);
685 info->online = __connman_service_is_connected(info->service);
686 info->name = __connman_service_get_name(info->service);
687 idx = __connman_service_get_index(info->service);
688 info->ifname = connman_inet_ifname(idx);
692 info->online = FALSE;
698 static void session_changed(struct connman_session *session)
700 struct session_info *info = &session->info;
701 struct session_info *info_last = &session->info_last;
702 struct connman_service *service = NULL;
703 GSourceFunc callback = NULL;
707 * TODO: This only a placeholder for the 'real' algorithm to
708 * play a bit around. So we are going to improve it step by step.
711 if (info->connect == FALSE) {
712 if (info->service != NULL)
713 __connman_service_disconnect(info->service);
714 info->service = NULL;
718 iter = g_sequence_get_begin_iter(session->service_list);
720 while (g_sequence_iter_is_end(iter) == FALSE) {
721 service = g_sequence_get(iter);
723 if (__connman_service_is_connecting(service) == TRUE ||
724 __connman_service_is_connected(service) == TRUE) {
728 if (__connman_service_is_idle(service) == TRUE &&
729 info->connect == TRUE) {
730 callback = session_changed_connect;
736 iter = g_sequence_iter_next(iter);
739 if (info->service != NULL && info->service != service) {
740 __connman_service_disconnect(info->service);
741 info->service = NULL;
744 if (service != NULL) {
745 info->service = service;
747 if (callback != NULL)
752 if (info->service != info_last->service)
753 update_service(session);
756 static gboolean session_cb(gpointer user_data)
758 struct connman_session *session = user_data;
760 session_changed(session);
761 session_notify(session);
766 static DBusMessage *connect_session(DBusConnection *conn,
767 DBusMessage *msg, void *user_data)
769 struct connman_session *session = user_data;
770 struct session_info *info = &session->info;
772 DBG("session %p", session);
774 info->connect = TRUE;
776 g_timeout_add_seconds(0, session_cb, session);
778 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
781 static DBusMessage *disconnect_session(DBusConnection *conn,
782 DBusMessage *msg, void *user_data)
784 struct connman_session *session = user_data;
785 struct session_info *info = &session->info;
787 DBG("session %p", session);
789 info->connect = FALSE;
791 g_timeout_add_seconds(0, session_cb, session);
793 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
796 static void print_name(gpointer data, gpointer user_data)
798 struct connman_service *service = data;
800 DBG("service %p type %s name %s", service,
801 service2bearer(connman_service_get_type(service)),
802 __connman_service_get_name(service));
805 static void update_allowed_bearers(struct connman_session *session)
807 if (session->service_list != NULL)
808 g_sequence_free(session->service_list);
810 session->service_list = __connman_service_get_list(session,
812 g_sequence_sort(session->service_list, sort_services, session);
813 g_sequence_foreach(session->service_list, print_name, NULL);
816 static DBusMessage *change_session(DBusConnection *conn,
817 DBusMessage *msg, void *user_data)
819 struct connman_session *session = user_data;
820 struct session_info *info = &session->info;
821 struct session_info *info_last = &session->info_last;
822 DBusMessageIter iter, value;
824 GSList *allowed_bearers;
827 DBG("session %p", session);
828 if (dbus_message_iter_init(msg, &iter) == FALSE)
829 return __connman_error_invalid_arguments(msg);
831 dbus_message_iter_get_basic(&iter, &name);
832 dbus_message_iter_next(&iter);
833 dbus_message_iter_recurse(&iter, &value);
835 switch (dbus_message_iter_get_arg_type(&value)) {
836 case DBUS_TYPE_ARRAY:
837 if (g_str_equal(name, "AllowedBearers") == TRUE) {
838 allowed_bearers = session_parse_allowed_bearers(&value);
840 g_slist_foreach(info->allowed_bearers,
841 cleanup_bearer_info, NULL);
842 g_slist_free(info->allowed_bearers);
844 if (allowed_bearers == NULL) {
845 allowed_bearers = session_allowed_bearers_any();
847 if (allowed_bearers == NULL)
848 return __connman_error_failed(msg, ENOMEM);
851 info->allowed_bearers = allowed_bearers;
853 update_allowed_bearers(session);
854 session->info_dirty = TRUE;
859 case DBUS_TYPE_BOOLEAN:
860 if (g_str_equal(name, "Priority") == TRUE) {
861 dbus_message_iter_get_basic(&value,
864 if (info_last->priority != info->priority)
865 session->info_dirty = TRUE;
866 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
867 dbus_message_iter_get_basic(&value,
868 &info->avoid_handover);
870 if (info_last->avoid_handover != info->avoid_handover)
871 session->info_dirty = TRUE;
872 } else if (g_str_equal(name, "StayConnected") == TRUE) {
873 dbus_message_iter_get_basic(&value,
874 &info->stay_connected);
876 if (info_last->stay_connected != info->stay_connected)
877 session->info_dirty = TRUE;
878 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
879 dbus_message_iter_get_basic(&value,
882 if (info_last->ecall != info->ecall)
883 session->info_dirty = TRUE;
888 case DBUS_TYPE_UINT32:
889 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
890 dbus_message_iter_get_basic(&value,
891 &info->periodic_connect);
893 if (info_last->periodic_connect != info->periodic_connect)
894 session->info_dirty = TRUE;
895 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
896 dbus_message_iter_get_basic(&value,
897 &info->idle_timeout);
899 if (info_last->idle_timeout != info->idle_timeout)
900 session->info_dirty = TRUE;
905 case DBUS_TYPE_STRING:
906 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
908 dbus_message_iter_get_basic(&value, &val);
909 info->roaming_policy =
910 string2roamingpolicy(val);
912 if (info_last->roaming_policy != info->roaming_policy)
913 session->info_dirty = TRUE;
922 err = session_notify(session);
924 __connman_error_failed(msg, -err);
926 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
929 return __connman_error_invalid_arguments(msg);
932 static GDBusMethodTable session_methods[] = {
933 { "Destroy", "", "", destroy_session },
934 { "Connect", "", "", connect_session },
935 { "Disconnect", "", "", disconnect_session },
936 { "Change", "sv", "", change_session },
940 int __connman_session_create(DBusMessage *msg)
942 const char *owner, *notify_path;
944 DBusMessageIter iter, array;
945 struct connman_session *session;
946 struct session_info *info, *info_last;
948 connman_bool_t priority = FALSE, avoid_handover = FALSE;
949 connman_bool_t stay_connected = FALSE, ecall = FALSE;
950 enum connman_session_roaming_policy roaming_policy =
951 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
952 GSList *allowed_bearers = NULL;
953 unsigned int periodic_connect = 0;
954 unsigned int idle_timeout = 0;
958 owner = dbus_message_get_sender(msg);
960 DBG("owner %s", owner);
962 dbus_message_iter_init(msg, &iter);
963 dbus_message_iter_recurse(&iter, &array);
965 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
966 DBusMessageIter entry, value;
967 const char *key, *val;
969 dbus_message_iter_recurse(&array, &entry);
970 dbus_message_iter_get_basic(&entry, &key);
972 dbus_message_iter_next(&entry);
973 dbus_message_iter_recurse(&entry, &value);
975 switch (dbus_message_iter_get_arg_type(&value)) {
976 case DBUS_TYPE_ARRAY:
977 if (g_str_equal(key, "AllowedBearers") == TRUE) {
979 session_parse_allowed_bearers(&value);
984 case DBUS_TYPE_BOOLEAN:
985 if (g_str_equal(key, "Priority") == TRUE) {
986 dbus_message_iter_get_basic(&value,
988 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
989 dbus_message_iter_get_basic(&value,
991 } else if (g_str_equal(key, "StayConnected") == TRUE) {
992 dbus_message_iter_get_basic(&value,
994 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
995 dbus_message_iter_get_basic(&value,
1001 case DBUS_TYPE_UINT32:
1002 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1003 dbus_message_iter_get_basic(&value,
1005 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1006 dbus_message_iter_get_basic(&value,
1012 case DBUS_TYPE_STRING:
1013 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1014 dbus_message_iter_get_basic(&value, &val);
1015 roaming_policy = string2roamingpolicy(val);
1020 dbus_message_iter_next(&array);
1023 dbus_message_iter_next(&iter);
1024 dbus_message_iter_get_basic(&iter, ¬ify_path);
1026 if (notify_path == NULL) {
1027 session_path = NULL;
1032 session_path = g_strdup_printf("/sessions%s", notify_path);
1033 if (session_path == NULL) {
1038 session = g_hash_table_lookup(session_hash, session_path);
1039 if (session != NULL) {
1044 session = g_try_new0(struct connman_session, 1);
1045 if (session == NULL) {
1050 info = &session->info;
1051 info_last = &session->info_last;
1053 session->owner = g_strdup(owner);
1054 session->session_path = session_path;
1055 session->notify_path = g_strdup(notify_path);
1056 session->notify_watch =
1057 g_dbus_add_disconnect_watch(connection, session->owner,
1058 owner_disconnect, session, NULL);
1061 info->online = FALSE;
1062 info->priority = priority;
1063 info->avoid_handover = avoid_handover;
1064 info->stay_connected = stay_connected;
1065 info->periodic_connect = periodic_connect;
1066 info->idle_timeout = idle_timeout;
1067 info->ecall = ecall;
1068 info->roaming_policy = roaming_policy;
1070 if (allowed_bearers == NULL) {
1071 info->allowed_bearers =
1072 session_allowed_bearers_any();
1074 if (info->allowed_bearers == NULL) {
1079 info->allowed_bearers = allowed_bearers;
1082 info_last->bearer = NULL;
1083 info_last->online = !priority;
1084 info_last->avoid_handover = !avoid_handover;
1085 info_last->stay_connected = !stay_connected;
1086 info_last->periodic_connect = !periodic_connect;
1087 info_last->idle_timeout = !idle_timeout;
1088 info_last->ecall = !ecall;
1089 info_last->roaming_policy =
1090 CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
1091 info_last->allowed_bearers = NULL;
1092 info_last->service = (void *) 1;
1093 info_last->marker = info->marker + 1;
1095 session->info_dirty = TRUE;
1097 update_allowed_bearers(session);
1099 g_hash_table_replace(session_hash, session->session_path, session);
1101 DBG("add %s", session->session_path);
1103 if (g_dbus_register_interface(connection, session->session_path,
1104 CONNMAN_SESSION_INTERFACE,
1105 session_methods, NULL,
1106 NULL, session, NULL) == FALSE) {
1107 connman_error("Failed to register %s", session->session_path);
1108 g_hash_table_remove(session_hash, session->session_path);
1115 g_dbus_send_reply(connection, msg,
1116 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1119 g_timeout_add_seconds(0, session_cb, session);
1124 connman_error("Failed to create session");
1125 g_free(session_path);
1127 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1128 g_slist_free(allowed_bearers);
1133 int __connman_session_destroy(DBusMessage *msg)
1135 const char *owner, *session_path;
1136 struct connman_session *session;
1138 owner = dbus_message_get_sender(msg);
1140 DBG("owner %s", owner);
1142 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1144 if (session_path == NULL)
1147 session = g_hash_table_lookup(session_hash, session_path);
1148 if (session == NULL)
1151 if (g_strcmp0(owner, session->owner) != 0)
1154 session_disconnect(session);
1159 connman_bool_t __connman_session_mode()
1164 void __connman_session_set_mode(connman_bool_t enable)
1166 DBG("enable %d", enable);
1168 if (sessionmode == enable)
1171 sessionmode = enable;
1173 if (sessionmode == TRUE)
1174 __connman_service_disconnect_all();
1177 static void service_add(struct connman_service *service)
1179 GHashTableIter iter;
1180 gpointer key, value;
1181 struct connman_session *session;
1183 DBG("service %p", service);
1185 g_hash_table_iter_init(&iter, session_hash);
1187 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1190 if (service_match(session, service) == FALSE)
1193 g_sequence_insert_sorted(session->service_list, service,
1194 sort_services, session);
1196 session_cb(session);
1200 static gint service_in_session(gconstpointer a, gconstpointer b,
1209 static void service_remove(struct connman_service *service)
1212 GHashTableIter iter;
1213 gpointer key, value;
1214 GSequenceIter *seq_iter;
1215 struct connman_session *session;
1216 struct session_info *info;
1217 struct connman_service *found_service;
1219 DBG("service %p", service);
1221 g_hash_table_iter_init(&iter, session_hash);
1223 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1225 info = &session->info;
1227 if (session->service_list == NULL)
1230 seq_iter = g_sequence_search(session->service_list, service,
1231 service_in_session, NULL);
1232 if (g_sequence_iter_is_end(seq_iter) == TRUE)
1235 g_sequence_remove(seq_iter);
1237 found_service = g_sequence_get(seq_iter);
1238 if (found_service != info->service)
1241 info->service = NULL;
1242 session_cb(session);
1246 static void service_state_changed(struct connman_service *service,
1247 enum connman_service_state state)
1249 GHashTableIter iter;
1250 gpointer key, value;
1251 struct connman_session *session;
1252 struct session_info *info;
1253 connman_bool_t online;
1255 DBG("service %p state %d", service, state);
1257 g_hash_table_iter_init(&iter, session_hash);
1259 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1261 info = &session->info;
1263 if (info->service == service) {
1264 online = __connman_service_is_connected(service);
1265 if (info->online == online)
1268 info->online = online;
1269 session_cb(session);
1274 static void ipconfig_changed(struct connman_service *service,
1275 struct connman_ipconfig *ipconfig)
1277 GHashTableIter iter;
1278 gpointer key, value;
1279 struct connman_session *session;
1280 struct session_info *info;
1281 enum connman_ipconfig_type type;
1283 DBG("service %p ipconfig %p", service, ipconfig);
1285 type = __connman_ipconfig_get_config_type(ipconfig);
1287 g_hash_table_iter_init(&iter, session_hash);
1289 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1291 info = &session->info;
1293 if (info->service == service) {
1294 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1295 ipconfig_ipv4_changed(session);
1296 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1297 ipconfig_ipv6_changed(session);
1302 static struct connman_notifier session_notifier = {
1304 .service_add = service_add,
1305 .service_remove = service_remove,
1306 .service_state_changed = service_state_changed,
1307 .ipconfig_changed = ipconfig_changed,
1310 int __connman_session_init(void)
1316 connection = connman_dbus_get_connection();
1317 if (connection == NULL)
1320 err = connman_notifier_register(&session_notifier);
1322 dbus_connection_unref(connection);
1326 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1327 NULL, cleanup_session);
1329 sessionmode = FALSE;
1333 void __connman_session_cleanup(void)
1337 if (connection == NULL)
1340 connman_notifier_unregister(&session_notifier);
1342 g_hash_table_foreach(session_hash, release_session, NULL);
1343 g_hash_table_destroy(session_hash);
1345 dbus_connection_unref(connection);