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,
44 struct connman_session {
53 connman_bool_t connect;
54 connman_bool_t online;
55 connman_bool_t priority;
56 GSList *allowed_bearers;
57 connman_bool_t avoid_handover;
58 connman_bool_t stay_connected;
59 unsigned int periodic_connect;
60 unsigned int idle_timeout;
62 enum connman_session_roaming_policy roaming_policy;
65 GSequence *service_list;
66 struct connman_service *service;
71 connman_bool_t match_all;
72 enum connman_service_type service_type;
75 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
78 case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
80 case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
82 case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
84 case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
86 case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
88 case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
89 return "international";
95 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
97 if (g_strcmp0(policy, "default") == 0)
98 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
99 else if (g_strcmp0(policy, "always") == 0)
100 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
101 else if (g_strcmp0(policy, "forbidden") == 0)
102 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
103 else if (g_strcmp0(policy, "national") == 0)
104 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
105 else if (g_strcmp0(policy, "international") == 0)
106 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
108 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
111 static enum connman_service_type bearer2service(const char *bearer)
114 return CONNMAN_SERVICE_TYPE_UNKNOWN;
116 if (g_strcmp0(bearer, "ethernet") == 0)
117 return CONNMAN_SERVICE_TYPE_ETHERNET;
118 else if (g_strcmp0(bearer, "wifi") == 0)
119 return CONNMAN_SERVICE_TYPE_WIFI;
120 else if (g_strcmp0(bearer, "wimax") == 0)
121 return CONNMAN_SERVICE_TYPE_WIMAX;
122 else if (g_strcmp0(bearer, "bluetooth") == 0)
123 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
124 else if (g_strcmp0(bearer, "3g") == 0)
125 return CONNMAN_SERVICE_TYPE_CELLULAR;
127 return CONNMAN_SERVICE_TYPE_UNKNOWN;
130 static char *service2bearer(enum connman_service_type type)
133 case CONNMAN_SERVICE_TYPE_ETHERNET:
135 case CONNMAN_SERVICE_TYPE_WIFI:
137 case CONNMAN_SERVICE_TYPE_WIMAX:
139 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
141 case CONNMAN_SERVICE_TYPE_CELLULAR:
143 case CONNMAN_SERVICE_TYPE_UNKNOWN:
144 case CONNMAN_SERVICE_TYPE_SYSTEM:
145 case CONNMAN_SERVICE_TYPE_GPS:
146 case CONNMAN_SERVICE_TYPE_VPN:
147 case CONNMAN_SERVICE_TYPE_GADGET:
154 static char *session2bearer(struct connman_session *session)
157 struct bearer_info *info;
158 enum connman_service_type type;
160 if (session->service == NULL)
163 type = connman_service_get_type(session->service);
165 for (list = session->allowed_bearers; list != NULL; list = list->next) {
169 return service2bearer(type);
171 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN)
174 if (info->service_type == type)
175 return service2bearer(type);
182 static void cleanup_bearer_info(gpointer data, gpointer user_data)
184 struct bearer_info *info = data;
190 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
192 struct bearer_info *info;
193 DBusMessageIter array;
196 dbus_message_iter_recurse(iter, &array);
198 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
201 dbus_message_iter_get_basic(&array, &bearer);
203 info = g_try_new0(struct bearer_info, 1);
205 g_slist_foreach(list, cleanup_bearer_info, NULL);
211 info->name = g_strdup(bearer);
212 info->service_type = bearer2service(info->name);
214 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
215 g_strcmp0(info->name, "*") == 0) {
216 info->match_all = TRUE;
218 info->match_all = FALSE;
221 list = g_slist_append(list, info);
223 dbus_message_iter_next(&array);
229 static GSList *session_allowed_bearers_any(void)
231 struct bearer_info *info;
234 info = g_try_new0(struct bearer_info, 1);
241 info->name = g_strdup("");
242 info->match_all = TRUE;
243 info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
245 list = g_slist_append(list, info);
250 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
252 struct connman_session *session = user_data;
255 for (list = session->allowed_bearers; list != NULL; list = list->next) {
256 struct bearer_info *info = list->data;
258 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
263 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
265 struct connman_service *service = user_data;
266 struct connman_ipconfig *ipconfig_ipv4;
271 ipconfig_ipv4 = __connman_service_get_ip4config(service);
272 if (ipconfig_ipv4 == NULL)
275 __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
278 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
280 struct connman_service *service = user_data;
281 struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
286 ipconfig_ipv4 = __connman_service_get_ip4config(service);
287 ipconfig_ipv6 = __connman_service_get_ip6config(service);
288 if (ipconfig_ipv6 == NULL)
291 __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
294 static void append_service(DBusMessageIter *dict,
295 struct connman_session *session)
297 connman_dbus_dict_append_basic(dict, "Bearer",
298 DBUS_TYPE_STRING, &session->bearer);
300 connman_dbus_dict_append_basic(dict, "Online",
301 DBUS_TYPE_BOOLEAN, &session->online);
303 connman_dbus_dict_append_basic(dict, "Name",
304 DBUS_TYPE_STRING, &session->name);
306 connman_dbus_dict_append_dict(dict, "IPv4",
307 append_ipconfig_ipv4, session->service);
309 connman_dbus_dict_append_dict(dict, "IPv6",
310 append_ipconfig_ipv6, session->service);
312 connman_dbus_dict_append_basic(dict, "Interface",
313 DBUS_TYPE_STRING, &session->ifname);
317 static void append_notify_all(DBusMessageIter *dict,
318 struct connman_session *session)
322 append_service(dict, session);
324 connman_dbus_dict_append_basic(dict, "Priority",
325 DBUS_TYPE_BOOLEAN, &session->priority);
327 connman_dbus_dict_append_array(dict, "AllowedBearers",
329 append_allowed_bearers,
332 connman_dbus_dict_append_basic(dict, "AvoidHandover",
334 &session->avoid_handover);
336 connman_dbus_dict_append_basic(dict, "StayConnected",
338 &session->stay_connected);
340 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
342 &session->periodic_connect);
344 connman_dbus_dict_append_basic(dict, "IdleTimeout",
346 &session->idle_timeout);
348 connman_dbus_dict_append_basic(dict, "EmergencyCall",
349 DBUS_TYPE_BOOLEAN, &session->ecall);
351 policy = roamingpolicy2string(session->roaming_policy);
352 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
356 connman_dbus_dict_append_basic(dict, "SessionMarker",
357 DBUS_TYPE_UINT32, &session->marker);
360 static gboolean session_notify_all(gpointer user_data)
362 struct connman_session *session = user_data;
364 DBusMessageIter array, dict;
366 DBG("session %p owner %s notify_path %s", session,
367 session->owner, session->notify_path);
369 msg = dbus_message_new_method_call(session->owner, session->notify_path,
370 CONNMAN_NOTIFICATION_INTERFACE,
373 connman_error("Could not create notification message");
377 dbus_message_iter_init_append(msg, &array);
378 connman_dbus_dict_open(&array, &dict);
380 append_notify_all(&dict, session);
382 connman_dbus_dict_close(&array, &dict);
384 g_dbus_send_message(connection, msg);
389 static gboolean service_changed(gpointer user_data)
391 struct connman_session *session = user_data;
395 DBusMessageIter array, dict;
397 DBG("session %p owner %s notify_path %s", session,
398 session->owner, session->notify_path);
400 msg = dbus_message_new_method_call(session->owner, session->notify_path,
401 CONNMAN_NOTIFICATION_INTERFACE,
404 connman_error("Could not create notification message");
408 dbus_message_iter_init_append(msg, &array);
409 connman_dbus_dict_open(&array, &dict);
411 append_service(&dict, session);
413 connman_dbus_dict_close(&array, &dict);
415 g_dbus_send_message(connection, msg);
420 static void online_changed(struct connman_session *session)
422 connman_dbus_setting_changed_basic(session->owner, session->notify_path,
423 "Online", DBUS_TYPE_BOOLEAN,
427 static void ipconfig_ipv4_changed(struct connman_session *session)
429 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
430 "IPv4", append_ipconfig_ipv4, session->service);
433 static void ipconfig_ipv6_changed(struct connman_session *session)
435 connman_dbus_setting_changed_dict(session->owner, session->notify_path,
436 "IPv6", append_ipconfig_ipv6, session->service);
439 static void update_service(struct connman_session *session)
443 if (session->service != NULL) {
444 session->bearer = session2bearer(session);
446 __connman_service_is_connected(session->service);
447 session->name = __connman_service_get_name(session->service);
448 idx = __connman_service_get_index(session->service);
449 session->ifname = connman_inet_ifname(idx);
452 session->bearer = "";
453 session->online = FALSE;
455 session->ifname = "";
459 static connman_bool_t service_type_match(struct connman_session *session,
460 struct connman_service *service)
464 for (list = session->allowed_bearers; list != NULL; list = list->next) {
465 struct bearer_info *info = list->data;
466 enum connman_service_type service_type;
468 if (info->match_all == TRUE)
471 service_type = connman_service_get_type(service);
472 if (info->service_type == service_type)
479 static connman_bool_t service_match(struct connman_session *session,
480 struct connman_service *service)
482 if (service_type_match(session, service) == FALSE)
488 static int service_type_weight(enum connman_service_type type)
491 * The session doesn't care which service
492 * to use. Nevertheless we have to sort them
493 * according their type. The ordering is
502 case CONNMAN_SERVICE_TYPE_ETHERNET:
504 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
506 case CONNMAN_SERVICE_TYPE_WIFI:
507 case CONNMAN_SERVICE_TYPE_WIMAX:
509 case CONNMAN_SERVICE_TYPE_CELLULAR:
511 case CONNMAN_SERVICE_TYPE_UNKNOWN:
512 case CONNMAN_SERVICE_TYPE_SYSTEM:
513 case CONNMAN_SERVICE_TYPE_GPS:
514 case CONNMAN_SERVICE_TYPE_VPN:
515 case CONNMAN_SERVICE_TYPE_GADGET:
522 static gint sort_allowed_bearers(struct connman_service *service_a,
523 struct connman_service *service_b,
524 struct connman_session *session)
527 enum connman_service_type type_a, type_b;
528 int weight_a, weight_b;
530 type_a = connman_service_get_type(service_a);
531 type_b = connman_service_get_type(service_b);
533 for (list = session->allowed_bearers; list != NULL; list = list->next) {
534 struct bearer_info *info = list->data;
536 if (info->match_all == TRUE) {
537 if (type_a != type_b) {
538 weight_a = service_type_weight(type_a);
539 weight_b = service_type_weight(type_b);
541 if (weight_a > weight_b)
544 if (weight_a < weight_b)
551 if (type_a == info->service_type &&
552 type_b == info->service_type) {
556 if (type_a == info->service_type &&
557 type_b != info->service_type) {
561 if (type_a != info->service_type &&
562 type_b == info->service_type) {
570 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
572 struct connman_service *service_a = (void *)a;
573 struct connman_service *service_b = (void *)b;
574 struct connman_session *session = user_data;
576 return sort_allowed_bearers(service_a, service_b, session);
579 static void print_name(gpointer data, gpointer user_data)
581 struct connman_service *service = data;
583 DBG("service %p type %s name %s", service,
584 service2bearer(connman_service_get_type(service)),
585 __connman_service_get_name(service));
588 static connman_bool_t session_select_service(struct connman_session *session)
590 struct connman_service *service;
593 session->service_list =
594 __connman_service_get_list(session, service_match);
596 if (session->service_list == NULL)
599 g_sequence_sort(session->service_list, sort_services, session);
600 g_sequence_foreach(session->service_list, print_name, NULL);
602 iter = g_sequence_get_begin_iter(session->service_list);
604 while (g_sequence_iter_is_end(iter) == FALSE) {
605 service = g_sequence_get(iter);
607 if (__connman_service_is_connecting(service) == TRUE ||
608 __connman_service_is_connected(service) == TRUE) {
609 session->service = service;
613 iter = g_sequence_iter_next(iter);
619 static void cleanup_session(gpointer user_data)
621 struct connman_session *session = user_data;
623 DBG("remove %s", session->session_path);
625 g_sequence_free(session->service_list);
627 g_slist_foreach(session->allowed_bearers, cleanup_bearer_info, NULL);
628 g_slist_free(session->allowed_bearers);
630 g_free(session->owner);
631 g_free(session->session_path);
632 g_free(session->notify_path);
637 static void release_session(gpointer key, gpointer value, gpointer user_data)
639 struct connman_session *session = value;
640 DBusMessage *message;
642 DBG("owner %s path %s", session->owner, session->notify_path);
644 if (session->notify_watch > 0)
645 g_dbus_remove_watch(connection, session->notify_watch);
647 g_dbus_unregister_interface(connection, session->session_path,
648 CONNMAN_SESSION_INTERFACE);
650 message = dbus_message_new_method_call(session->owner,
651 session->notify_path,
652 CONNMAN_NOTIFICATION_INTERFACE,
657 dbus_message_set_no_reply(message, TRUE);
659 g_dbus_send_message(connection, message);
662 static int session_disconnect(struct connman_session *session)
664 DBG("session %p, %s", session, session->owner);
666 if (session->notify_watch > 0)
667 g_dbus_remove_watch(connection, session->notify_watch);
669 g_dbus_unregister_interface(connection, session->session_path,
670 CONNMAN_SESSION_INTERFACE);
672 g_hash_table_remove(session_hash, session->session_path);
677 static void owner_disconnect(DBusConnection *conn, void *user_data)
679 struct connman_session *session = user_data;
681 DBG("session %p, %s died", session, session->owner);
683 session_disconnect(session);
686 static DBusMessage *destroy_session(DBusConnection *conn,
687 DBusMessage *msg, void *user_data)
689 struct connman_session *session = user_data;
691 DBG("session %p", session);
693 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
696 static gboolean session_do_connect(gpointer user_data)
698 struct connman_session *session = user_data;
700 __connman_service_connect(session->service);
702 service_changed(session);
706 static DBusMessage *connect_session(DBusConnection *conn,
707 DBusMessage *msg, void *user_data)
709 struct connman_session *session = user_data;
710 struct connman_service *service = NULL;
711 GSourceFunc callback = NULL;
714 DBG("session %p", session);
716 session->connect = TRUE;
718 if (session->service_list != NULL)
719 g_sequence_free(session->service_list);
721 session->service_list = __connman_service_get_list(session,
724 g_sequence_sort(session->service_list, sort_services, session);
725 g_sequence_foreach(session->service_list, print_name, NULL);
727 iter = g_sequence_get_begin_iter(session->service_list);
729 while (g_sequence_iter_is_end(iter) == FALSE) {
730 service = g_sequence_get(iter);
732 if (__connman_service_is_connecting(service) == TRUE ||
733 __connman_service_is_connected(service) == TRUE) {
734 callback = service_changed;
738 if (__connman_service_is_idle(service) == TRUE) {
739 callback = session_do_connect;
745 iter = g_sequence_iter_next(iter);
748 if (service != NULL) {
749 session->service = service;
750 update_service(session);
751 g_timeout_add_seconds(0, callback, session);
754 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
757 static gboolean session_do_disconnect(gpointer user_data)
759 struct connman_session *session = user_data;
761 if (__connman_service_disconnect(session->service) < 0)
764 session->service = NULL;
765 update_service(session);
766 service_changed(session);
771 static DBusMessage *disconnect_session(DBusConnection *conn,
772 DBusMessage *msg, void *user_data)
774 struct connman_session *session = user_data;
776 DBG("session %p", session);
778 session->connect = FALSE;
780 if (session->service == NULL)
781 return __connman_error_already_disabled(msg);
783 g_timeout_add_seconds(0, session_do_disconnect, session);
785 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
788 static DBusMessage *change_session(DBusConnection *conn,
789 DBusMessage *msg, void *user_data)
791 struct connman_session *session = user_data;
792 DBusMessageIter iter, value;
794 DBusMessageIter reply_array, reply_dict;
796 GSList *allowed_bearers;
798 DBG("session %p", session);
799 if (dbus_message_iter_init(msg, &iter) == FALSE)
800 return __connman_error_invalid_arguments(msg);
802 reply = dbus_message_new_method_call(session->owner,
803 session->notify_path,
804 CONNMAN_NOTIFICATION_INTERFACE,
807 return __connman_error_failed(msg, ENOMEM);
809 dbus_message_iter_init_append(reply, &reply_array);
810 connman_dbus_dict_open(&reply_array, &reply_dict);
812 dbus_message_iter_get_basic(&iter, &name);
813 dbus_message_iter_next(&iter);
814 dbus_message_iter_recurse(&iter, &value);
816 switch (dbus_message_iter_get_arg_type(&value)) {
817 case DBUS_TYPE_ARRAY:
818 if (g_str_equal(name, "AllowedBearers") == TRUE) {
819 allowed_bearers = session_parse_allowed_bearers(&value);
821 g_slist_foreach(session->allowed_bearers,
822 cleanup_bearer_info, NULL);
823 g_slist_free(session->allowed_bearers);
825 if (allowed_bearers == NULL) {
826 allowed_bearers = session_allowed_bearers_any();
828 if (allowed_bearers == NULL) {
829 dbus_message_unref(reply);
830 return __connman_error_failed(msg, ENOMEM);
834 session->allowed_bearers = allowed_bearers;
836 /* update_allowed_bearers(); */
838 connman_dbus_dict_append_array(&reply_dict,
841 append_allowed_bearers,
847 case DBUS_TYPE_BOOLEAN:
848 if (g_str_equal(name, "Priority") == TRUE) {
849 dbus_message_iter_get_basic(&value, &session->priority);
851 /* update_priority(); */
853 connman_dbus_dict_append_basic(&reply_dict, "Priority",
857 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
858 dbus_message_iter_get_basic(&value,
859 &session->avoid_handover);
861 /* update_avoid_handover(); */
863 connman_dbus_dict_append_basic(&reply_dict,
866 &session->avoid_handover);
868 } else if (g_str_equal(name, "StayConnected") == TRUE) {
869 dbus_message_iter_get_basic(&value,
870 &session->stay_connected);
872 /* update_stay_connected(); */
874 connman_dbus_dict_append_basic(&reply_dict,
877 &session->stay_connected);
879 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
880 dbus_message_iter_get_basic(&value, &session->ecall);
882 /* update_ecall(); */
884 connman_dbus_dict_append_basic(&reply_dict,
893 case DBUS_TYPE_UINT32:
894 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
895 dbus_message_iter_get_basic(&value,
896 &session->periodic_connect);
898 /* update_periodic_update(); */
900 connman_dbus_dict_append_basic(&reply_dict,
903 &session->periodic_connect);
904 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
905 dbus_message_iter_get_basic(&value,
906 &session->idle_timeout);
908 /* update_idle_timeout(); */
910 connman_dbus_dict_append_basic(&reply_dict,
913 &session->idle_timeout);
918 case DBUS_TYPE_STRING:
919 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
921 dbus_message_iter_get_basic(&value, &val);
922 session->roaming_policy = string2roamingpolicy(val);
924 /* update_roaming_allowed(); */
926 val = roamingpolicy2string(session->roaming_policy);
927 connman_dbus_dict_append_basic(&reply_dict,
937 connman_dbus_dict_close(&reply_array, &reply_dict);
939 g_dbus_send_message(connection, reply);
941 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
944 dbus_message_unref(reply);
945 return __connman_error_invalid_arguments(msg);
948 static GDBusMethodTable session_methods[] = {
949 { "Destroy", "", "", destroy_session },
950 { "Connect", "", "", connect_session },
951 { "Disconnect", "", "", disconnect_session },
952 { "Change", "sv", "", change_session },
956 int __connman_session_create(DBusMessage *msg)
958 const char *owner, *notify_path;
960 DBusMessageIter iter, array;
961 struct connman_session *session;
963 connman_bool_t priority = FALSE, avoid_handover = FALSE;
964 connman_bool_t stay_connected = FALSE, ecall = FALSE;
965 enum connman_session_roaming_policy roaming_policy =
966 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
967 GSList *allowed_bearers = NULL;
968 unsigned int periodic_connect = 0;
969 unsigned int idle_timeout = 0;
973 owner = dbus_message_get_sender(msg);
975 DBG("owner %s", owner);
977 dbus_message_iter_init(msg, &iter);
978 dbus_message_iter_recurse(&iter, &array);
980 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
981 DBusMessageIter entry, value;
982 const char *key, *val;
984 dbus_message_iter_recurse(&array, &entry);
985 dbus_message_iter_get_basic(&entry, &key);
987 dbus_message_iter_next(&entry);
988 dbus_message_iter_recurse(&entry, &value);
990 switch (dbus_message_iter_get_arg_type(&value)) {
991 case DBUS_TYPE_ARRAY:
992 if (g_str_equal(key, "AllowedBearers") == TRUE) {
994 session_parse_allowed_bearers(&value);
999 case DBUS_TYPE_BOOLEAN:
1000 if (g_str_equal(key, "Priority") == TRUE) {
1001 dbus_message_iter_get_basic(&value,
1003 } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1004 dbus_message_iter_get_basic(&value,
1006 } else if (g_str_equal(key, "StayConnected") == TRUE) {
1007 dbus_message_iter_get_basic(&value,
1009 } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1010 dbus_message_iter_get_basic(&value,
1016 case DBUS_TYPE_UINT32:
1017 if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1018 dbus_message_iter_get_basic(&value,
1020 } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1021 dbus_message_iter_get_basic(&value,
1027 case DBUS_TYPE_STRING:
1028 if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1029 dbus_message_iter_get_basic(&value, &val);
1030 roaming_policy = string2roamingpolicy(val);
1035 dbus_message_iter_next(&array);
1038 dbus_message_iter_next(&iter);
1039 dbus_message_iter_get_basic(&iter, ¬ify_path);
1041 if (notify_path == NULL) {
1042 session_path = NULL;
1047 session_path = g_strdup_printf("/sessions%s", notify_path);
1048 if (session_path == NULL) {
1053 session = g_hash_table_lookup(session_hash, session_path);
1054 if (session != NULL) {
1059 session = g_try_new0(struct connman_session, 1);
1060 if (session == NULL) {
1065 session->owner = g_strdup(owner);
1066 session->session_path = session_path;
1067 session->notify_path = g_strdup(notify_path);
1068 session->notify_watch =
1069 g_dbus_add_disconnect_watch(connection, session->owner,
1070 owner_disconnect, session, NULL);
1072 session->bearer = "";
1073 session->online = FALSE;
1074 session->priority = priority;
1075 session->avoid_handover = avoid_handover;
1076 session->stay_connected = stay_connected;
1077 session->periodic_connect = periodic_connect;
1078 session->idle_timeout = idle_timeout;
1079 session->ecall = ecall;
1080 session->roaming_policy = roaming_policy;
1082 if (allowed_bearers == NULL) {
1083 session->allowed_bearers = session_allowed_bearers_any();
1085 if (session->allowed_bearers == NULL) {
1090 session->allowed_bearers = allowed_bearers;
1093 session->service_list = NULL;
1095 update_service(session);
1097 g_hash_table_replace(session_hash, session->session_path, session);
1099 DBG("add %s", session->session_path);
1101 if (g_dbus_register_interface(connection, session->session_path,
1102 CONNMAN_SESSION_INTERFACE,
1103 session_methods, NULL,
1104 NULL, session, NULL) == FALSE) {
1105 connman_error("Failed to register %s", session->session_path);
1106 g_hash_table_remove(session_hash, session->session_path);
1113 g_dbus_send_reply(connection, msg,
1114 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1119 * Check if the session settings matches to a service which is
1120 * a already connected
1122 if (session_select_service(session) == TRUE)
1123 update_service(session);
1125 g_timeout_add_seconds(0, session_notify_all, session);
1130 connman_error("Failed to create session");
1131 g_free(session_path);
1133 g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1134 g_slist_free(allowed_bearers);
1139 int __connman_session_destroy(DBusMessage *msg)
1141 const char *owner, *session_path;
1142 struct connman_session *session;
1144 owner = dbus_message_get_sender(msg);
1146 DBG("owner %s", owner);
1148 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1150 if (session_path == NULL)
1153 session = g_hash_table_lookup(session_hash, session_path);
1154 if (session == NULL)
1157 if (g_strcmp0(owner, session->owner) != 0)
1160 session_disconnect(session);
1165 connman_bool_t __connman_session_mode()
1170 void __connman_session_set_mode(connman_bool_t enable)
1172 DBG("enable %d", enable);
1174 if (sessionmode == enable)
1177 sessionmode = enable;
1179 if (sessionmode == TRUE)
1180 __connman_service_disconnect_all();
1183 static void service_add(struct connman_service *service)
1185 GHashTableIter iter;
1186 gpointer key, value;
1187 struct connman_session *session;
1189 DBG("service %p", service);
1191 g_hash_table_iter_init(&iter, session_hash);
1193 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1196 if (service_match(session, service) == FALSE)
1199 g_sequence_insert_sorted(session->service_list, service,
1200 sort_services, session);
1204 static gint service_in_session(gconstpointer a, gconstpointer b,
1213 static void service_remove(struct connman_service *service)
1216 GHashTableIter iter;
1217 gpointer key, value;
1218 GSequenceIter *seq_iter;
1219 struct connman_session *session;
1220 struct connman_service *found_service;
1222 DBG("service %p", service);
1224 g_hash_table_iter_init(&iter, session_hash);
1226 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1229 if (session->service_list == NULL)
1232 seq_iter = g_sequence_search(session->service_list, service,
1233 service_in_session, NULL);
1234 if (g_sequence_iter_is_end(seq_iter) == TRUE)
1237 g_sequence_remove(seq_iter);
1239 found_service = g_sequence_get(seq_iter);
1240 if (found_service != session->service)
1243 session->service = NULL;
1244 update_service(session);
1245 g_timeout_add_seconds(0, service_changed, session);
1249 static void service_state_changed(struct connman_service *service,
1250 enum connman_service_state state)
1252 GHashTableIter iter;
1253 gpointer key, value;
1254 struct connman_session *session;
1255 connman_bool_t online;
1257 DBG("service %p state %d", service, state);
1259 g_hash_table_iter_init(&iter, session_hash);
1261 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1264 if (session->service == service) {
1265 online = __connman_service_is_connected(service);
1266 if (session->online == online)
1269 session->online = online;
1270 online_changed(session);
1275 static void ipconfig_changed(struct connman_service *service,
1276 struct connman_ipconfig *ipconfig)
1278 GHashTableIter iter;
1279 gpointer key, value;
1280 struct connman_session *session;
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) {
1292 if (session->service == service) {
1293 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1294 ipconfig_ipv4_changed(session);
1295 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1296 ipconfig_ipv6_changed(session);
1301 static struct connman_notifier session_notifier = {
1303 .service_add = service_add,
1304 .service_remove = service_remove,
1305 .service_state_changed = service_state_changed,
1306 .ipconfig_changed = ipconfig_changed,
1309 int __connman_session_init(void)
1315 connection = connman_dbus_get_connection();
1316 if (connection == NULL)
1319 err = connman_notifier_register(&session_notifier);
1321 dbus_connection_unref(connection);
1325 session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1326 NULL, cleanup_session);
1328 sessionmode = FALSE;
1332 void __connman_session_cleanup(void)
1336 if (connection == NULL)
1339 connman_notifier_unregister(&session_notifier);
1341 g_hash_table_foreach(session_hash, release_session, NULL);
1342 g_hash_table_destroy(session_hash);
1344 dbus_connection_unref(connection);