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