session: Refactor session_changed
[platform/upstream/connman.git] / src / session.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2011  BWM CarIT GmbH. All rights reserved.
7  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <gdbus.h>
28
29 #include "connman.h"
30
31 static DBusConnection *connection;
32 static GHashTable *session_hash;
33 static connman_bool_t sessionmode;
34 static struct connman_session *ecall_session;
35
36 enum connman_session_trigger {
37         CONNMAN_SESSION_TRIGGER_UNKNOWN         = 0,
38         CONNMAN_SESSION_TRIGGER_SETTING         = 1,
39         CONNMAN_SESSION_TRIGGER_CONNECT         = 2,
40         CONNMAN_SESSION_TRIGGER_DISCONNECT      = 3,
41         CONNMAN_SESSION_TRIGGER_PERIODIC        = 4,
42         CONNMAN_SESSION_TRIGGER_SERVICE         = 5,
43         CONNMAN_SESSION_TRIGGER_ECALL           = 6,
44 };
45
46 enum connman_session_roaming_policy {
47         CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN          = 0,
48         CONNMAN_SESSION_ROAMING_POLICY_DEFAULT          = 1,
49         CONNMAN_SESSION_ROAMING_POLICY_ALWAYS           = 2,
50         CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN        = 3,
51         CONNMAN_SESSION_ROAMING_POLICY_NATIONAL         = 4,
52         CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL    = 5,
53 };
54
55 struct session_info {
56         char *bearer;
57         const char *name;
58         char *ifname;
59         connman_bool_t online;
60         connman_bool_t priority;
61         GSList *allowed_bearers;
62         connman_bool_t avoid_handover;
63         connman_bool_t stay_connected;
64         unsigned int periodic_connect;
65         unsigned int idle_timeout;
66         connman_bool_t ecall;
67         enum connman_session_roaming_policy roaming_policy;
68         unsigned int marker;
69
70         struct connman_service *service;
71 };
72
73 struct connman_session {
74         char *owner;
75         char *session_path;
76         char *notify_path;
77         guint notify_watch;
78
79         connman_bool_t append_all;
80         connman_bool_t info_dirty;
81         struct session_info info;
82         struct session_info info_last;
83
84         GSequence *service_list;
85 };
86
87 struct bearer_info {
88         char *name;
89         connman_bool_t match_all;
90         enum connman_service_type service_type;
91 };
92
93 static const char *trigger2string(enum connman_session_trigger trigger)
94 {
95         switch (trigger) {
96         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
97                 break;
98         case CONNMAN_SESSION_TRIGGER_SETTING:
99                 return "setting";
100         case CONNMAN_SESSION_TRIGGER_CONNECT:
101                 return "connect";
102         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
103                 return "disconnect";
104         case CONNMAN_SESSION_TRIGGER_PERIODIC:
105                 return "periodic";
106         case CONNMAN_SESSION_TRIGGER_SERVICE:
107                 return "service";
108         case CONNMAN_SESSION_TRIGGER_ECALL:
109                 return "ecall";
110         }
111
112         return NULL;
113 }
114
115 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
116 {
117         switch (policy) {
118         case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
119                 break;
120         case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
121                 return "default";
122         case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
123                 return "always";
124         case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
125                 return "forbidden";
126         case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
127                 return "national";
128         case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
129                 return "international";
130         }
131
132         return NULL;
133 }
134
135 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
136 {
137         if (g_strcmp0(policy, "default") == 0)
138                 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
139         else if (g_strcmp0(policy, "always") == 0)
140                 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
141         else if (g_strcmp0(policy, "forbidden") == 0)
142                 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
143         else if (g_strcmp0(policy, "national") == 0)
144                 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
145         else if (g_strcmp0(policy, "international") == 0)
146                 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
147         else
148                 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
149 }
150
151 static enum connman_service_type bearer2service(const char *bearer)
152 {
153         if (bearer == NULL)
154                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
155
156         if (g_strcmp0(bearer, "ethernet") == 0)
157                 return CONNMAN_SERVICE_TYPE_ETHERNET;
158         else if (g_strcmp0(bearer, "wifi") == 0)
159                 return CONNMAN_SERVICE_TYPE_WIFI;
160         else if (g_strcmp0(bearer, "wimax") == 0)
161                 return CONNMAN_SERVICE_TYPE_WIMAX;
162         else if (g_strcmp0(bearer, "bluetooth") == 0)
163                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
164         else if (g_strcmp0(bearer, "3g") == 0)
165                 return CONNMAN_SERVICE_TYPE_CELLULAR;
166         else
167                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
168 }
169
170 static char *service2bearer(enum connman_service_type type)
171 {
172         switch (type) {
173         case CONNMAN_SERVICE_TYPE_ETHERNET:
174                 return "ethernet";
175         case CONNMAN_SERVICE_TYPE_WIFI:
176                 return "wifi";
177         case CONNMAN_SERVICE_TYPE_WIMAX:
178                 return "wimax";
179         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
180                 return "bluetooth";
181         case CONNMAN_SERVICE_TYPE_CELLULAR:
182                 return "3g";
183         case CONNMAN_SERVICE_TYPE_UNKNOWN:
184         case CONNMAN_SERVICE_TYPE_SYSTEM:
185         case CONNMAN_SERVICE_TYPE_GPS:
186         case CONNMAN_SERVICE_TYPE_VPN:
187         case CONNMAN_SERVICE_TYPE_GADGET:
188                 return "";
189         }
190
191         return "";
192 }
193
194 static void cleanup_bearer_info(gpointer data, gpointer user_data)
195 {
196         struct bearer_info *info = data;
197
198         g_free(info->name);
199         g_free(info);
200 }
201
202 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
203 {
204         struct bearer_info *info;
205         DBusMessageIter array;
206         GSList *list = NULL;
207
208         dbus_message_iter_recurse(iter, &array);
209
210         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
211                 char *bearer = NULL;
212
213                 dbus_message_iter_get_basic(&array, &bearer);
214
215                 info = g_try_new0(struct bearer_info, 1);
216                 if (info == NULL) {
217                         g_slist_foreach(list, cleanup_bearer_info, NULL);
218                         g_slist_free(list);
219
220                         return NULL;
221                 }
222
223                 info->name = g_strdup(bearer);
224                 info->service_type = bearer2service(info->name);
225
226                 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
227                                 g_strcmp0(info->name, "*") == 0) {
228                         info->match_all = TRUE;
229                 } else {
230                         info->match_all = FALSE;
231                 }
232
233                 list = g_slist_append(list, info);
234
235                 dbus_message_iter_next(&array);
236         }
237
238         return list;
239 }
240
241 static GSList *session_allowed_bearers_any(void)
242 {
243         struct bearer_info *info;
244         GSList *list = NULL;
245
246         info = g_try_new0(struct bearer_info, 1);
247         if (info == NULL) {
248                 g_slist_free(list);
249
250                 return NULL;
251         }
252
253         info->name = g_strdup("");
254         info->match_all = TRUE;
255         info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
256
257         list = g_slist_append(list, info);
258
259         return list;
260 }
261
262 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
263 {
264         struct session_info *info = user_data;
265         GSList *list;
266
267         for (list = info->allowed_bearers;
268                         list != NULL; list = list->next) {
269                 struct bearer_info *info = list->data;
270
271                 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
272                                                 &info->name);
273         }
274 }
275
276 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
277 {
278         struct connman_service *service = user_data;
279         struct connman_ipconfig *ipconfig_ipv4;
280
281         if (service == NULL)
282                 return;
283
284         ipconfig_ipv4 = __connman_service_get_ip4config(service);
285         if (ipconfig_ipv4 == NULL)
286                 return;
287
288         __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
289 }
290
291 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
292 {
293         struct connman_service *service = user_data;
294         struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
295
296         if (service == NULL)
297                 return;
298
299         ipconfig_ipv4 = __connman_service_get_ip4config(service);
300         ipconfig_ipv6 = __connman_service_get_ip6config(service);
301         if (ipconfig_ipv6 == NULL)
302                 return;
303
304         __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
305 }
306
307 static void append_notify(DBusMessageIter *dict,
308                                         struct connman_session *session)
309 {
310         struct session_info *info = &session->info;
311         struct session_info *info_last = &session->info_last;
312         const char *policy;
313
314         if (session->append_all == TRUE ||
315                         info->bearer != info_last->bearer) {
316                 connman_dbus_dict_append_basic(dict, "Bearer",
317                                                 DBUS_TYPE_STRING,
318                                                 &info->bearer);
319                 info_last->bearer = info->bearer;
320         }
321
322         if (session->append_all == TRUE ||
323                         info->online != info_last->online) {
324                 connman_dbus_dict_append_basic(dict, "Online",
325                                                 DBUS_TYPE_BOOLEAN,
326                                                 &info->online);
327                 info_last->online = info->online;
328         }
329
330         if (session->append_all == TRUE ||
331                         info->name != info_last->name) {
332                 connman_dbus_dict_append_basic(dict, "Name",
333                                                 DBUS_TYPE_STRING,
334                                                 &info->name);
335                 info_last->name = info->name;
336         }
337
338         if (session->append_all == TRUE ||
339                         info->service != info_last->service) {
340                 connman_dbus_dict_append_dict(dict, "IPv4",
341                                                 append_ipconfig_ipv4,
342                                                 info->service);
343
344                 connman_dbus_dict_append_dict(dict, "IPv6",
345                                                 append_ipconfig_ipv6,
346                                                 info->service);
347
348                 connman_dbus_dict_append_basic(dict, "Interface",
349                                                 DBUS_TYPE_STRING,
350                                                 &info->ifname);
351
352                 info_last->ifname = info->ifname;
353                 info_last->service = info->service;
354         }
355
356
357         if (session->append_all == TRUE ||
358                         info->priority != info_last->priority) {
359                 connman_dbus_dict_append_basic(dict, "Priority",
360                                                 DBUS_TYPE_BOOLEAN,
361                                                 &info->priority);
362                 info_last->priority = info->priority;
363         }
364
365         if (session->append_all == TRUE ||
366                         info->allowed_bearers != info_last->allowed_bearers) {
367                 connman_dbus_dict_append_array(dict, "AllowedBearers",
368                                                 DBUS_TYPE_STRING,
369                                                 append_allowed_bearers,
370                                                 info);
371                 info_last->allowed_bearers = info->allowed_bearers;
372         }
373
374         if (session->append_all == TRUE ||
375                         info->avoid_handover != info_last->avoid_handover) {
376                 connman_dbus_dict_append_basic(dict, "AvoidHandover",
377                                                 DBUS_TYPE_BOOLEAN,
378                                                 &info->avoid_handover);
379                 info_last->avoid_handover = info->avoid_handover;
380         }
381
382         if (session->append_all == TRUE ||
383                         info->stay_connected != info_last->stay_connected) {
384                 connman_dbus_dict_append_basic(dict, "StayConnected",
385                                                 DBUS_TYPE_BOOLEAN,
386                                                 &info->stay_connected);
387                 info_last->stay_connected = info->stay_connected;
388         }
389
390         if (session->append_all == TRUE ||
391                         info->periodic_connect != info_last->periodic_connect) {
392                 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
393                                                 DBUS_TYPE_UINT32,
394                                                 &info->periodic_connect);
395                 info_last->periodic_connect = info->periodic_connect;
396         }
397
398         if (session->append_all == TRUE ||
399                         info->idle_timeout != info_last->idle_timeout) {
400                 connman_dbus_dict_append_basic(dict, "IdleTimeout",
401                                                 DBUS_TYPE_UINT32,
402                                                 &info->idle_timeout);
403                 info_last->idle_timeout = info->idle_timeout;
404         }
405
406         if (session->append_all == TRUE ||
407                         info->ecall != info_last->ecall) {
408                 connman_dbus_dict_append_basic(dict, "EmergencyCall",
409                                                 DBUS_TYPE_BOOLEAN,
410                                                 &info->ecall);
411                 info_last->ecall = info->ecall;
412         }
413
414         if (session->append_all == TRUE ||
415                         info->roaming_policy != info_last->roaming_policy) {
416                 policy = roamingpolicy2string(info->roaming_policy);
417                 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
418                                                 DBUS_TYPE_STRING,
419                                                 &policy);
420                 info_last->roaming_policy = info->roaming_policy;
421         }
422
423         if (session->append_all == TRUE ||
424                         info->marker != info_last->marker) {
425                 connman_dbus_dict_append_basic(dict, "SessionMarker",
426                                                 DBUS_TYPE_UINT32,
427                                                 &info->marker);
428                 info_last->marker = info->marker;
429         }
430
431         session->append_all = FALSE;
432         session->info_dirty = FALSE;
433 }
434
435 static gboolean session_notify(gpointer user_data)
436 {
437         struct connman_session *session = user_data;
438         DBusMessage *msg;
439         DBusMessageIter array, dict;
440
441         if (session->info_dirty == FALSE)
442                 return 0;
443
444         DBG("session %p owner %s notify_path %s", session,
445                 session->owner, session->notify_path);
446
447         msg = dbus_message_new_method_call(session->owner, session->notify_path,
448                                                 CONNMAN_NOTIFICATION_INTERFACE,
449                                                 "Update");
450         if (msg == NULL)
451                 return FALSE;
452
453         dbus_message_iter_init_append(msg, &array);
454         connman_dbus_dict_open(&array, &dict);
455
456         append_notify(&dict, session);
457
458         connman_dbus_dict_close(&array, &dict);
459
460         g_dbus_send_message(connection, msg);
461
462         session->info_dirty = FALSE;
463
464         return FALSE;
465 }
466
467 static void ipconfig_ipv4_changed(struct connman_session *session)
468 {
469         struct session_info *info = &session->info;
470
471         connman_dbus_setting_changed_dict(session->owner, session->notify_path,
472                                                 "IPv4", append_ipconfig_ipv4,
473                                                 info->service);
474 }
475
476 static void ipconfig_ipv6_changed(struct connman_session *session)
477 {
478         struct session_info *info = &session->info;
479
480         connman_dbus_setting_changed_dict(session->owner, session->notify_path,
481                                                 "IPv6", append_ipconfig_ipv6,
482                                                 info->service);
483 }
484
485 static GSequenceIter *lookup_service(struct connman_session *session,
486                                         struct connman_service *service)
487 {
488         GSequenceIter *iter;
489
490         if (service == NULL)
491                 return NULL;
492
493         iter = g_sequence_get_begin_iter(session->service_list);
494
495         while (g_sequence_iter_is_end(iter) == FALSE) {
496                 struct connman_service *service_iter = g_sequence_get(iter);
497
498                 if (service_iter == service)
499                         return iter;
500
501                 iter = g_sequence_iter_next(iter);
502         }
503
504         return NULL;
505 }
506
507 static connman_bool_t service_type_match(struct connman_session *session,
508                                         struct connman_service *service)
509 {
510         struct session_info *info = &session->info;
511         GSList *list;
512
513         for (list = info->allowed_bearers;
514                         list != NULL; list = list->next) {
515                 struct bearer_info *info = list->data;
516                 enum connman_service_type service_type;
517
518                 if (info->match_all == TRUE)
519                         return TRUE;
520
521                 service_type = connman_service_get_type(service);
522                 if (info->service_type == service_type)
523                         return TRUE;
524         }
525
526         return FALSE;
527 }
528
529 static connman_bool_t service_match(struct connman_session *session,
530                                         struct connman_service *service)
531 {
532         if (service_type_match(session, service) == FALSE)
533                 return FALSE;
534
535         return TRUE;
536 }
537
538 static int service_type_weight(enum connman_service_type type)
539 {
540         /*
541          * The session doesn't care which service
542          * to use. Nevertheless we have to sort them
543          * according their type. The ordering is
544          *
545          * 1. Ethernet
546          * 2. Bluetooth
547          * 3. WiFi/WiMAX
548          * 4. GSM/UTMS/3G
549          */
550
551         switch (type) {
552         case CONNMAN_SERVICE_TYPE_ETHERNET:
553                 return 4;
554         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
555                 return 3;
556         case CONNMAN_SERVICE_TYPE_WIFI:
557         case CONNMAN_SERVICE_TYPE_WIMAX:
558                 return 2;
559         case CONNMAN_SERVICE_TYPE_CELLULAR:
560                 return 1;
561         case CONNMAN_SERVICE_TYPE_UNKNOWN:
562         case CONNMAN_SERVICE_TYPE_SYSTEM:
563         case CONNMAN_SERVICE_TYPE_GPS:
564         case CONNMAN_SERVICE_TYPE_VPN:
565         case CONNMAN_SERVICE_TYPE_GADGET:
566                 break;
567         }
568
569         return 0;
570 }
571
572 static gint sort_allowed_bearers(struct connman_service *service_a,
573                                         struct connman_service *service_b,
574                                         struct connman_session *session)
575 {
576         struct session_info *info = &session->info;
577         GSList *list;
578         enum connman_service_type type_a, type_b;
579         int weight_a, weight_b;
580
581         type_a = connman_service_get_type(service_a);
582         type_b = connman_service_get_type(service_b);
583
584         for (list = info->allowed_bearers;
585                         list != NULL; list = list->next) {
586                 struct bearer_info *info = list->data;
587
588                 if (info->match_all == TRUE) {
589                         if (type_a != type_b) {
590                                 weight_a = service_type_weight(type_a);
591                                 weight_b = service_type_weight(type_b);
592
593                                 if (weight_a > weight_b)
594                                         return -1;
595
596                                 if (weight_a < weight_b)
597                                         return 1;
598
599                                 return 0;
600                         }
601                 }
602
603                 if (type_a == info->service_type &&
604                                 type_b == info->service_type) {
605                         return 0;
606                 }
607
608                 if (type_a == info->service_type &&
609                                 type_b != info->service_type) {
610                         return -1;
611                 }
612
613                 if (type_a != info->service_type &&
614                                 type_b == info->service_type) {
615                         return 1;
616                 }
617         }
618
619         return 0;
620 }
621
622 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
623 {
624         struct connman_service *service_a = (void *)a;
625         struct connman_service *service_b = (void *)b;
626         struct connman_session *session = user_data;
627
628         return sort_allowed_bearers(service_a, service_b, session);
629 }
630
631 static void cleanup_session(gpointer user_data)
632 {
633         struct connman_session *session = user_data;
634         struct session_info *info = &session->info;
635
636         DBG("remove %s", session->session_path);
637
638         g_sequence_free(session->service_list);
639
640         g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
641         g_slist_free(info->allowed_bearers);
642
643         g_free(session->owner);
644         g_free(session->session_path);
645         g_free(session->notify_path);
646
647         g_free(session);
648 }
649
650 static void release_session(gpointer key, gpointer value, gpointer user_data)
651 {
652         struct connman_session *session = value;
653         DBusMessage *message;
654
655         DBG("owner %s path %s", session->owner, session->notify_path);
656
657         if (session->notify_watch > 0)
658                 g_dbus_remove_watch(connection, session->notify_watch);
659
660         g_dbus_unregister_interface(connection, session->session_path,
661                                                 CONNMAN_SESSION_INTERFACE);
662
663         message = dbus_message_new_method_call(session->owner,
664                                                 session->notify_path,
665                                                 CONNMAN_NOTIFICATION_INTERFACE,
666                                                 "Release");
667         if (message == NULL)
668                 return;
669
670         dbus_message_set_no_reply(message, TRUE);
671
672         g_dbus_send_message(connection, message);
673 }
674
675 static int session_disconnect(struct connman_session *session)
676 {
677         DBG("session %p, %s", session, session->owner);
678
679         if (session->notify_watch > 0)
680                 g_dbus_remove_watch(connection, session->notify_watch);
681
682         g_dbus_unregister_interface(connection, session->session_path,
683                                                 CONNMAN_SESSION_INTERFACE);
684
685         g_hash_table_remove(session_hash, session->session_path);
686
687         return 0;
688 }
689
690 static void owner_disconnect(DBusConnection *conn, void *user_data)
691 {
692         struct connman_session *session = user_data;
693
694         DBG("session %p, %s died", session, session->owner);
695
696         session_disconnect(session);
697 }
698
699 static DBusMessage *destroy_session(DBusConnection *conn,
700                                         DBusMessage *msg, void *user_data)
701 {
702         struct connman_session *session = user_data;
703
704         DBG("session %p", session);
705
706         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
707 }
708
709 static void update_info(struct session_info *info)
710 {
711         enum connman_service_type type;
712         int idx;
713
714         if (info->service != NULL) {
715                 type = connman_service_get_type(info->service);
716                 info->bearer = service2bearer(type);
717
718                 info->online = __connman_service_is_connected(info->service);
719                 info->name = __connman_service_get_name(info->service);
720
721                 idx = __connman_service_get_index(info->service);
722                 info->ifname = connman_inet_ifname(idx);
723                 if (info->ifname == NULL)
724                         info->ifname = "";
725         } else {
726                 info->bearer = "";
727                 info->online = FALSE;
728                 info->name = "";
729                 info->ifname = "";
730         }
731 }
732
733 static void notify_service_changes(struct connman_session *session)
734 {
735         struct session_info *info = &session->info;
736         struct session_info *info_last = &session->info_last;
737
738         if (info->service == info_last->service)
739                 return;
740
741         update_info(info);
742         session->info_dirty = TRUE;
743 }
744
745 static void select_and_connect(struct connman_session *session,
746                                 connman_bool_t do_connect)
747 {
748         struct session_info *info = &session->info;
749         struct connman_service *service = NULL;
750         GSequenceIter *iter;
751
752         DBG("session %p connect %d", session, do_connect);
753
754         iter = g_sequence_get_begin_iter(session->service_list);
755
756         while (g_sequence_iter_is_end(iter) == FALSE) {
757                 service = g_sequence_get(iter);
758
759                 if (__connman_service_is_connecting(service) == TRUE ||
760                                 __connman_service_is_connected(service) == TRUE) {
761                         break;
762                 }
763
764                 if (__connman_service_is_idle(service) == TRUE &&
765                                 do_connect == TRUE) {
766                         break;
767                 }
768
769                 service = NULL;
770
771                 iter = g_sequence_iter_next(iter);
772         }
773
774         if (info->service != NULL && info->service != service) {
775                 __connman_service_disconnect(info->service);
776                 info->service = NULL;
777         }
778
779         if (service != NULL) {
780                 info->service = service;
781
782                 if (do_connect == TRUE)
783                         __connman_service_connect(info->service);
784         }
785
786         notify_service_changes(session);
787 }
788
789 static void session_changed(struct connman_session *session,
790                                 enum connman_session_trigger trigger)
791 {
792         struct session_info *info = &session->info;
793         GSequenceIter *iter;
794
795         /*
796          * TODO: This only a placeholder for the 'real' algorithm to
797          * play a bit around. So we are going to improve it step by step.
798          */
799
800         DBG("session %p trigger %s", session, trigger2string(trigger));
801
802         switch (trigger) {
803         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
804                 DBG("ignore session changed event");
805                 return;
806         case CONNMAN_SESSION_TRIGGER_SETTING:
807                 if (info->service != NULL) {
808                         iter = lookup_service(session, info->service);
809                         if (iter == NULL) {
810                                 /*
811                                  * This service is not part of this
812                                  * session anymore.
813                                  */
814
815                                 __connman_service_disconnect(info->service);
816                                 info->service = NULL;
817                         }
818
819                         notify_service_changes(session);
820                 }
821
822                 /* Try to free ride */
823                 if (info->online == FALSE)
824                         select_and_connect(session, FALSE);
825
826                 break;
827         case CONNMAN_SESSION_TRIGGER_CONNECT:
828                 if (info->online == TRUE)
829                         break;
830
831                 select_and_connect(session, TRUE);
832
833                 break;
834         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
835                 if (info->online == FALSE)
836                         break;
837
838                 if (info->service != NULL)
839                         __connman_service_disconnect(info->service);
840
841                 info->service = NULL;
842                 notify_service_changes(session);
843
844                 break;
845         case CONNMAN_SESSION_TRIGGER_PERIODIC:
846                 select_and_connect(session, TRUE);
847
848                 break;
849         case CONNMAN_SESSION_TRIGGER_SERVICE:
850                 if (info->online == TRUE)
851                         break;
852
853                 if (info->stay_connected == TRUE) {
854                         DBG("StayConnected");
855                         select_and_connect(session, TRUE);
856
857                         break;
858                 }
859
860                 /* Try to free ride */
861                 select_and_connect(session, FALSE);
862
863                 break;
864         case CONNMAN_SESSION_TRIGGER_ECALL:
865                 if (info->online == FALSE && info->service != NULL)
866                         info->service = NULL;
867
868                 notify_service_changes(session);
869
870                 break;
871         }
872
873         switch (trigger) {
874         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
875                 break;
876         case CONNMAN_SESSION_TRIGGER_CONNECT:
877         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
878         case CONNMAN_SESSION_TRIGGER_ECALL:
879                 g_timeout_add_seconds(0, session_notify, session);
880                 break;
881         case CONNMAN_SESSION_TRIGGER_SETTING:
882         case CONNMAN_SESSION_TRIGGER_PERIODIC:
883         case CONNMAN_SESSION_TRIGGER_SERVICE:
884                 session_notify(session);
885                 break;
886         }
887 }
888
889 static DBusMessage *connect_session(DBusConnection *conn,
890                                         DBusMessage *msg, void *user_data)
891 {
892         struct connman_session *session = user_data;
893
894         DBG("session %p", session);
895
896         if (ecall_session != NULL && ecall_session != session)
897                 return __connman_error_failed(msg, EBUSY);
898
899         session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
900
901         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
902 }
903
904 static DBusMessage *disconnect_session(DBusConnection *conn,
905                                         DBusMessage *msg, void *user_data)
906 {
907         struct connman_session *session = user_data;
908
909         DBG("session %p", session);
910
911         if (ecall_session != NULL && ecall_session != session)
912                 return __connman_error_failed(msg, EBUSY);
913
914         session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
915
916         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
917 }
918
919 static void print_name(gpointer data, gpointer user_data)
920 {
921         struct connman_service *service = data;
922
923         DBG("service %p type %s name %s", service,
924                 service2bearer(connman_service_get_type(service)),
925                 __connman_service_get_name(service));
926 }
927
928 static void update_allowed_bearers(struct connman_session *session)
929 {
930         if (session->service_list != NULL)
931                 g_sequence_free(session->service_list);
932
933         session->service_list = __connman_service_get_list(session,
934                                                                 service_match);
935         g_sequence_sort(session->service_list, sort_services, session);
936         g_sequence_foreach(session->service_list, print_name, NULL);
937
938         session->info_dirty = TRUE;
939 }
940
941 static void update_ecall_sessions(struct connman_session *session)
942 {
943         struct session_info *info = &session->info;
944         struct connman_session *session_iter;
945         GHashTableIter iter;
946         gpointer key, value;
947
948         g_hash_table_iter_init(&iter, session_hash);
949
950         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
951                 session_iter = value;
952
953                 if (session_iter == session)
954                         continue;
955
956                 session_iter->info.ecall = info->ecall;
957                 session_iter->info_dirty = TRUE;
958
959                 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
960         }
961 }
962
963 static void update_ecall(struct connman_session *session)
964 {
965         struct session_info *info = &session->info;
966         struct session_info *info_last = &session->info_last;
967
968         DBG("session %p ecall_session %p ecall %d -> %d", session,
969                 ecall_session, info_last->ecall, info->ecall);
970
971         if (ecall_session == NULL) {
972                 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
973                         goto err;
974
975                 ecall_session = session;
976         } else if (ecall_session == session) {
977                 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
978                         goto err;
979
980                 ecall_session = NULL;
981         } else {
982                 goto err;
983         }
984
985         update_ecall_sessions(session);
986
987         session->info_dirty = TRUE;
988         return;
989
990 err:
991         /* not a valid transition */
992         info->ecall = info_last->ecall;
993 }
994
995 static DBusMessage *change_session(DBusConnection *conn,
996                                         DBusMessage *msg, void *user_data)
997 {
998         struct connman_session *session = user_data;
999         struct session_info *info = &session->info;
1000         struct session_info *info_last = &session->info_last;
1001         DBusMessageIter iter, value;
1002         const char *name;
1003         GSList *allowed_bearers;
1004
1005         DBG("session %p", session);
1006         if (dbus_message_iter_init(msg, &iter) == FALSE)
1007                 return __connman_error_invalid_arguments(msg);
1008
1009         dbus_message_iter_get_basic(&iter, &name);
1010         dbus_message_iter_next(&iter);
1011         dbus_message_iter_recurse(&iter, &value);
1012
1013         switch (dbus_message_iter_get_arg_type(&value)) {
1014         case DBUS_TYPE_ARRAY:
1015                 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1016                         allowed_bearers = session_parse_allowed_bearers(&value);
1017
1018                         g_slist_foreach(info->allowed_bearers,
1019                                         cleanup_bearer_info, NULL);
1020                         g_slist_free(info->allowed_bearers);
1021
1022                         if (allowed_bearers == NULL) {
1023                                 allowed_bearers = session_allowed_bearers_any();
1024
1025                                 if (allowed_bearers == NULL)
1026                                         return __connman_error_failed(msg, ENOMEM);
1027                         }
1028
1029                         info->allowed_bearers = allowed_bearers;
1030
1031                         update_allowed_bearers(session);
1032                 } else {
1033                         goto err;
1034                 }
1035                 break;
1036         case DBUS_TYPE_BOOLEAN:
1037                 if (g_str_equal(name, "Priority") == TRUE) {
1038                         dbus_message_iter_get_basic(&value,
1039                                         &info->priority);
1040
1041                         if (info_last->priority != info->priority)
1042                                 session->info_dirty = TRUE;
1043                 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1044                         dbus_message_iter_get_basic(&value,
1045                                         &info->avoid_handover);
1046
1047                         if (info_last->avoid_handover != info->avoid_handover)
1048                                 session->info_dirty = TRUE;
1049                         } else if (g_str_equal(name, "StayConnected") == TRUE) {
1050                         dbus_message_iter_get_basic(&value,
1051                                         &info->stay_connected);
1052
1053                         if (info_last->stay_connected != info->stay_connected)
1054                                 session->info_dirty = TRUE;
1055                 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1056                         dbus_message_iter_get_basic(&value,
1057                                         &info->ecall);
1058
1059                         update_ecall(session);
1060                 } else {
1061                         goto err;
1062                 }
1063                 break;
1064         case DBUS_TYPE_UINT32:
1065                 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1066                         dbus_message_iter_get_basic(&value,
1067                                         &info->periodic_connect);
1068
1069                         if (info_last->periodic_connect != info->periodic_connect)
1070                                 session->info_dirty = TRUE;
1071                 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1072                         dbus_message_iter_get_basic(&value,
1073                                         &info->idle_timeout);
1074
1075                         if (info_last->idle_timeout != info->idle_timeout)
1076                                 session->info_dirty = TRUE;
1077                 } else {
1078                         goto err;
1079                 }
1080                 break;
1081         case DBUS_TYPE_STRING:
1082                 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1083                         const char *val;
1084                         dbus_message_iter_get_basic(&value, &val);
1085                         info->roaming_policy =
1086                                         string2roamingpolicy(val);
1087
1088                         if (info_last->roaming_policy != info->roaming_policy)
1089                                 session->info_dirty = TRUE;
1090                 } else {
1091                         goto err;
1092                 }
1093                 break;
1094         default:
1095                 goto err;
1096         }
1097
1098         if (session->info_dirty == TRUE)
1099                 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1100
1101         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1102
1103 err:
1104         return __connman_error_invalid_arguments(msg);
1105 }
1106
1107 static GDBusMethodTable session_methods[] = {
1108         { "Destroy",    "",   "", destroy_session    },
1109         { "Connect",    "",   "", connect_session    },
1110         { "Disconnect", "",   "", disconnect_session },
1111         { "Change",     "sv", "", change_session     },
1112         { },
1113 };
1114
1115 int __connman_session_create(DBusMessage *msg)
1116 {
1117         const char *owner, *notify_path;
1118         char *session_path = NULL;
1119         DBusMessageIter iter, array;
1120         struct connman_session *session;
1121         struct session_info *info, *info_last;
1122
1123         connman_bool_t priority = FALSE, avoid_handover = FALSE;
1124         connman_bool_t stay_connected = FALSE, ecall = FALSE;
1125         enum connman_session_roaming_policy roaming_policy =
1126                                 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1127         GSList *allowed_bearers = NULL;
1128         unsigned int periodic_connect = 0;
1129         unsigned int idle_timeout = 0;
1130
1131         int err;
1132
1133         owner = dbus_message_get_sender(msg);
1134
1135         DBG("owner %s", owner);
1136
1137         if (ecall_session != NULL) {
1138                 /*
1139                  * If there is an emergency call already going on,
1140                  * ignore session creation attempt
1141                  */
1142                 err = -EBUSY;
1143                 goto err;
1144         }
1145
1146         dbus_message_iter_init(msg, &iter);
1147         dbus_message_iter_recurse(&iter, &array);
1148
1149         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1150                 DBusMessageIter entry, value;
1151                 const char *key, *val;
1152
1153                 dbus_message_iter_recurse(&array, &entry);
1154                 dbus_message_iter_get_basic(&entry, &key);
1155
1156                 dbus_message_iter_next(&entry);
1157                 dbus_message_iter_recurse(&entry, &value);
1158
1159                 switch (dbus_message_iter_get_arg_type(&value)) {
1160                 case DBUS_TYPE_ARRAY:
1161                         if (g_str_equal(key, "AllowedBearers") == TRUE) {
1162                                 allowed_bearers =
1163                                         session_parse_allowed_bearers(&value);
1164                         } else {
1165                                 return -EINVAL;
1166                         }
1167                         break;
1168                 case DBUS_TYPE_BOOLEAN:
1169                         if (g_str_equal(key, "Priority") == TRUE) {
1170                                 dbus_message_iter_get_basic(&value,
1171                                                         &priority);
1172                         } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1173                                 dbus_message_iter_get_basic(&value,
1174                                                         &avoid_handover);
1175                         } else if (g_str_equal(key, "StayConnected") == TRUE) {
1176                                 dbus_message_iter_get_basic(&value,
1177                                                         &stay_connected);
1178                         } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1179                                 dbus_message_iter_get_basic(&value,
1180                                                         &ecall);
1181                         } else {
1182                                 return -EINVAL;
1183                         }
1184                         break;
1185                 case DBUS_TYPE_UINT32:
1186                         if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1187                                 dbus_message_iter_get_basic(&value,
1188                                                         &periodic_connect);
1189                         } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1190                                 dbus_message_iter_get_basic(&value,
1191                                                         &idle_timeout);
1192                         } else {
1193                                 return -EINVAL;
1194                         }
1195                         break;
1196                 case DBUS_TYPE_STRING:
1197                         if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1198                                 dbus_message_iter_get_basic(&value, &val);
1199                                 roaming_policy = string2roamingpolicy(val);
1200                         } else {
1201                                 return -EINVAL;
1202                         }
1203                 }
1204                 dbus_message_iter_next(&array);
1205         }
1206
1207         dbus_message_iter_next(&iter);
1208         dbus_message_iter_get_basic(&iter, &notify_path);
1209
1210         if (notify_path == NULL) {
1211                 err = -EINVAL;
1212                 goto err;
1213         }
1214
1215         session_path = g_strdup_printf("/sessions%s", notify_path);
1216         if (session_path == NULL) {
1217                 err = -ENOMEM;
1218                 goto err;
1219         }
1220
1221         session = g_hash_table_lookup(session_hash, session_path);
1222         if (session != NULL) {
1223                 err = -EEXIST;
1224                 goto err;
1225         }
1226
1227         session = g_try_new0(struct connman_session, 1);
1228         if (session == NULL) {
1229                 err = -ENOMEM;
1230                 goto err;
1231         }
1232
1233         info = &session->info;
1234         info_last = &session->info_last;
1235
1236         session->owner = g_strdup(owner);
1237         session->session_path = session_path;
1238         session->notify_path = g_strdup(notify_path);
1239         session->notify_watch =
1240                 g_dbus_add_disconnect_watch(connection, session->owner,
1241                                         owner_disconnect, session, NULL);
1242
1243         info->bearer = "";
1244         info->online = FALSE;
1245         info->priority = priority;
1246         info->avoid_handover = avoid_handover;
1247         info->stay_connected = stay_connected;
1248         info->periodic_connect = periodic_connect;
1249         info->idle_timeout = idle_timeout;
1250         info->ecall = ecall;
1251         info->roaming_policy = roaming_policy;
1252         info->service = NULL;
1253         info->marker = 0;
1254
1255         if (allowed_bearers == NULL) {
1256                 info->allowed_bearers =
1257                                 session_allowed_bearers_any();
1258
1259                 if (info->allowed_bearers == NULL) {
1260                         err = -ENOMEM;
1261                         goto err;
1262                 }
1263         } else {
1264                 info->allowed_bearers = allowed_bearers;
1265         }
1266
1267         g_hash_table_replace(session_hash, session->session_path, session);
1268
1269         DBG("add %s", session->session_path);
1270
1271         if (g_dbus_register_interface(connection, session->session_path,
1272                                         CONNMAN_SESSION_INTERFACE,
1273                                         session_methods, NULL,
1274                                         NULL, session, NULL) == FALSE) {
1275                 connman_error("Failed to register %s", session->session_path);
1276                 g_hash_table_remove(session_hash, session->session_path);
1277                 session = NULL;
1278
1279                 err = -EINVAL;
1280                 goto err;
1281         }
1282
1283         g_dbus_send_reply(connection, msg,
1284                                 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1285                                 DBUS_TYPE_INVALID);
1286
1287
1288         update_allowed_bearers(session);
1289         update_info(info);
1290         if (info->ecall == TRUE) {
1291                 ecall_session = session;
1292                 update_ecall_sessions(session);
1293         }
1294
1295         info_last->bearer = info->bearer;
1296         info_last->online = info->online;
1297         info_last->priority = info->priority;
1298         info_last->avoid_handover = info->avoid_handover;
1299         info_last->stay_connected = info->stay_connected;
1300         info_last->periodic_connect = info->periodic_connect;
1301         info_last->idle_timeout = info->idle_timeout;
1302         info_last->ecall = info->ecall;
1303         info_last->roaming_policy = info->roaming_policy;
1304         info_last->service = info->service;
1305         info_last->marker = info->marker;
1306         info_last->allowed_bearers = info->allowed_bearers;
1307         update_info(info_last);
1308
1309         session->info_dirty = TRUE;
1310         session->append_all = TRUE;
1311
1312         session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1313
1314         return 0;
1315
1316 err:
1317         connman_error("Failed to create session");
1318         g_free(session_path);
1319
1320         g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1321         g_slist_free(allowed_bearers);
1322
1323         return err;
1324 }
1325
1326 int __connman_session_destroy(DBusMessage *msg)
1327 {
1328         const char *owner, *session_path;
1329         struct connman_session *session;
1330
1331         owner = dbus_message_get_sender(msg);
1332
1333         DBG("owner %s", owner);
1334
1335         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1336                                                         DBUS_TYPE_INVALID);
1337         if (session_path == NULL)
1338                 return -EINVAL;
1339
1340         session = g_hash_table_lookup(session_hash, session_path);
1341         if (session == NULL)
1342                 return -EINVAL;
1343
1344         if (g_strcmp0(owner, session->owner) != 0)
1345                 return -EACCES;
1346
1347         session_disconnect(session);
1348
1349         return 0;
1350 }
1351
1352 connman_bool_t __connman_session_mode()
1353 {
1354         return sessionmode;
1355 }
1356
1357 void __connman_session_set_mode(connman_bool_t enable)
1358 {
1359         DBG("enable %d", enable);
1360
1361         if (sessionmode == enable)
1362                 return;
1363
1364         sessionmode = enable;
1365
1366         if (sessionmode == TRUE)
1367                 __connman_service_disconnect_all();
1368 }
1369
1370 static void service_add(struct connman_service *service)
1371 {
1372         GHashTableIter iter;
1373         gpointer key, value;
1374         struct connman_session *session;
1375
1376         DBG("service %p", service);
1377
1378         g_hash_table_iter_init(&iter, session_hash);
1379
1380         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1381                 session = value;
1382
1383                 if (service_match(session, service) == FALSE)
1384                         continue;
1385
1386                 g_sequence_insert_sorted(session->service_list, service,
1387                                                 sort_services, session);
1388
1389                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1390         }
1391 }
1392
1393 static int service_remove_from_session(struct connman_session *session,
1394                                         struct connman_service *service)
1395 {
1396         GSequenceIter *iter;
1397
1398         iter = lookup_service(session, service);
1399         if (iter == NULL)
1400                 return -ENOENT;
1401
1402         session->info.online = FALSE;
1403         g_sequence_remove(iter);
1404
1405         return 0;
1406 }
1407
1408 static void service_remove(struct connman_service *service)
1409 {
1410
1411         GHashTableIter iter;
1412         gpointer key, value;
1413         struct connman_session *session;
1414         struct session_info *info;
1415
1416         DBG("service %p", service);
1417
1418         g_hash_table_iter_init(&iter, session_hash);
1419
1420         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1421                 session = value;
1422                 info = &session->info;
1423
1424                 if (service_remove_from_session(session, service) != 0)
1425                         continue;
1426
1427                 info->service = NULL;
1428                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1429         }
1430 }
1431
1432 static void service_state_changed(struct connman_service *service,
1433                                         enum connman_service_state state)
1434 {
1435         GHashTableIter iter;
1436         gpointer key, value;
1437         struct connman_session *session;
1438         struct session_info *info;
1439         connman_bool_t online;
1440
1441         DBG("service %p state %d", service, state);
1442
1443         g_hash_table_iter_init(&iter, session_hash);
1444
1445         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1446                 session = value;
1447                 info = &session->info;
1448
1449                 if (info->service == service) {
1450                         online = __connman_service_is_connected(service);
1451                         if (info->online == online)
1452                                 continue;
1453
1454                         info->online = online;
1455                         session->info_dirty = TRUE;
1456                         session_changed(session,
1457                                         CONNMAN_SESSION_TRIGGER_SERVICE);
1458                 }
1459         }
1460 }
1461
1462 static void ipconfig_changed(struct connman_service *service,
1463                                 struct connman_ipconfig *ipconfig)
1464 {
1465         GHashTableIter iter;
1466         gpointer key, value;
1467         struct connman_session *session;
1468         struct session_info *info;
1469         enum connman_ipconfig_type type;
1470
1471         DBG("service %p ipconfig %p", service, ipconfig);
1472
1473         type = __connman_ipconfig_get_config_type(ipconfig);
1474
1475         g_hash_table_iter_init(&iter, session_hash);
1476
1477         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1478                 session = value;
1479                 info = &session->info;
1480
1481                 if (info->service == service) {
1482                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1483                                 ipconfig_ipv4_changed(session);
1484                         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1485                                 ipconfig_ipv6_changed(session);
1486                 }
1487         }
1488 }
1489
1490 static struct connman_notifier session_notifier = {
1491         .name                   = "session",
1492         .service_add            = service_add,
1493         .service_remove         = service_remove,
1494         .service_state_changed  = service_state_changed,
1495         .ipconfig_changed       = ipconfig_changed,
1496 };
1497
1498 int __connman_session_init(void)
1499 {
1500         int err;
1501
1502         DBG("");
1503
1504         connection = connman_dbus_get_connection();
1505         if (connection == NULL)
1506                 return -1;
1507
1508         err = connman_notifier_register(&session_notifier);
1509         if (err < 0) {
1510                 dbus_connection_unref(connection);
1511                 return err;
1512         }
1513
1514         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1515                                                 NULL, cleanup_session);
1516
1517         sessionmode = FALSE;
1518         return 0;
1519 }
1520
1521 void __connman_session_cleanup(void)
1522 {
1523         DBG("");
1524
1525         if (connection == NULL)
1526                 return;
1527
1528         connman_notifier_unregister(&session_notifier);
1529
1530         g_hash_table_foreach(session_hash, release_session, NULL);
1531         g_hash_table_destroy(session_hash);
1532
1533         dbus_connection_unref(connection);
1534 }