session: Handle bearers update correctly
[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, "cellular") == 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 "cellular";
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. Cellular
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         DBG("");
789
790         if (info->entry == NULL)
791                 return;
792
793         disconnect = explicit_disconnect(info);
794
795         online = is_connecting(info->entry->state) == TRUE ||
796                         is_online(info->entry->state) == TRUE;
797
798         info->online = FALSE;
799         info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
800
801         service = info->entry->service;
802         info->entry = NULL;
803
804         DBG("disconnect %d online %d", disconnect, online);
805
806         if (disconnect == TRUE && online == TRUE)
807                 g_timeout_add_seconds(0, call_disconnect, service);
808 }
809
810 static void deselect_and_disconnect(struct connman_session *session,
811                                         enum connman_session_reason reason)
812 {
813         struct session_info *info = session->info;
814
815         deselect_service(info);
816
817         info->reason = reason;
818 }
819
820 static void select_online_service(struct session_info *info,
821                                         struct service_entry *entry)
822 {
823         info->online = TRUE;
824
825         info->entry = entry;
826         info->entry->reason = info->reason;
827
828         if (explicit_connect(info->reason) == FALSE)
829                 return;
830
831         __connman_service_session_inc(info->entry->service);
832 }
833
834 static void select_offline_service(struct session_info *info,
835                                         struct service_entry *entry)
836 {
837         if (explicit_connect(info->reason) == FALSE)
838                 return;
839
840         info->online = FALSE;
841
842         info->entry = entry;
843         info->entry->reason = info->reason;
844
845         __connman_service_session_inc(info->entry->service);
846         g_timeout_add_seconds(0, call_connect, info->entry->service);
847 }
848
849 static void select_service(struct session_info *info,
850                                 struct service_entry *entry)
851 {
852         DBG("service %p", entry->service);
853
854         if (is_online(entry->state) == TRUE)
855                 select_online_service(info, entry);
856         else
857                 select_offline_service(info, entry);
858 }
859
860 static void select_and_connect(struct connman_session *session,
861                                 enum connman_session_reason reason)
862 {
863         struct session_info *info = session->info;
864         struct service_entry *entry = NULL;
865         GSequenceIter *iter;
866
867         DBG("session %p reason %s", session, reason2string(reason));
868
869         info->reason = reason;
870
871         iter = g_sequence_get_begin_iter(session->service_list);
872
873         while (g_sequence_iter_is_end(iter) == FALSE) {
874                 entry = g_sequence_get(iter);
875
876                 switch (entry->state) {
877                 case CONNMAN_SERVICE_STATE_ASSOCIATION:
878                 case CONNMAN_SERVICE_STATE_CONFIGURATION:
879                 case CONNMAN_SERVICE_STATE_READY:
880                 case CONNMAN_SERVICE_STATE_ONLINE:
881                 case CONNMAN_SERVICE_STATE_IDLE:
882                 case CONNMAN_SERVICE_STATE_DISCONNECT:
883                         select_service(info, entry);
884                         return;
885                 case CONNMAN_SERVICE_STATE_UNKNOWN:
886                 case CONNMAN_SERVICE_STATE_FAILURE:
887                         break;
888                 }
889
890                 iter = g_sequence_iter_next(iter);
891         }
892 }
893
894 static struct service_entry *create_service_entry(struct connman_service *service,
895                                         const char *name,
896                                         enum connman_service_state state)
897 {
898         struct service_entry *entry;
899         enum connman_service_type type;
900         int idx;
901
902         entry = g_try_new0(struct service_entry, 1);
903         if (entry == NULL)
904                 return entry;
905
906         entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
907         entry->state = state;
908         if (name != NULL)
909                 entry->name = name;
910         else
911                 entry->name = "";
912         entry->service = service;
913
914         idx = __connman_service_get_index(entry->service);
915         entry->ifname = connman_inet_ifname(idx);
916         if (entry->ifname == NULL)
917                 entry->ifname = g_strdup("");
918
919         type = connman_service_get_type(entry->service);
920         entry->bearer = service2bearer(type);
921
922         return entry;
923 }
924
925 static void destroy_service_entry(gpointer data)
926 {
927         struct service_entry *entry = data;
928
929         g_free(entry->ifname);
930
931         g_free(entry);
932 }
933
934 static void populate_service_list(struct connman_session *session)
935 {
936         struct service_entry *entry;
937         GSequenceIter *iter;
938
939         session->service_hash =
940                 g_hash_table_new_full(g_direct_hash, g_direct_equal,
941                                         NULL, NULL);
942         session->service_list = __connman_service_get_list(session,
943                                                         service_match,
944                                                         create_service_entry,
945                                                         destroy_service_entry);
946
947         g_sequence_sort(session->service_list, sort_services, session);
948
949         iter = g_sequence_get_begin_iter(session->service_list);
950
951         while (g_sequence_iter_is_end(iter) == FALSE) {
952                 entry = g_sequence_get(iter);
953
954                 DBG("service %p type %s name %s", entry->service,
955                         service2bearer(connman_service_get_type(entry->service)),
956                         entry->name);
957
958                 g_hash_table_replace(session->service_hash,
959                                         entry->service, iter);
960
961                 iter = g_sequence_iter_next(iter);
962         }
963 }
964
965 static void session_changed(struct connman_session *session,
966                                 enum connman_session_trigger trigger)
967 {
968         struct session_info *info = session->info;
969         struct session_info *info_last = session->info_last;
970         GSequenceIter *service_iter = NULL, *service_iter_last = NULL;
971         GSequence *service_list_last;
972         GHashTable *service_hash_last;
973
974         /*
975          * TODO: This only a placeholder for the 'real' algorithm to
976          * play a bit around. So we are going to improve it step by step.
977          */
978
979         DBG("session %p trigger %s reason %s", session, trigger2string(trigger),
980                                                 reason2string(info->reason));
981
982         if (info->entry != NULL) {
983                 info->online = is_online(info->entry->state);
984                 if (info_last->online != info->online)
985                         session->info_dirty = TRUE;
986         }
987
988         switch (trigger) {
989         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
990                 DBG("ignore session changed event");
991                 return;
992         case CONNMAN_SESSION_TRIGGER_SETTING:
993                 if (info->allowed_bearers != info_last->allowed_bearers) {
994
995                         service_hash_last = session->service_hash;
996                         service_list_last = session->service_list;
997
998                         populate_service_list(session);
999
1000                         if (info->entry != NULL) {
1001                                 service_iter_last = g_hash_table_lookup(
1002                                                         service_hash_last,
1003                                                         info->entry->service);
1004                                 service_iter = g_hash_table_lookup(
1005                                                         session->service_hash,
1006                                                         info->entry->service);
1007                         }
1008
1009                         if (service_iter == NULL && service_iter_last != NULL) {
1010                                 /*
1011                                  * The currently selected service is
1012                                  * not part of this session anymore.
1013                                  */
1014                                 deselect_and_disconnect(session, info->reason);
1015                         }
1016
1017                         g_hash_table_remove_all(service_hash_last);
1018                         g_sequence_free(service_list_last);
1019                 }
1020
1021                 if (info->online == FALSE) {
1022                         select_and_connect(session,
1023                                         CONNMAN_SESSION_REASON_FREE_RIDE);
1024                 }
1025
1026                 break;
1027         case CONNMAN_SESSION_TRIGGER_CONNECT:
1028                 if (info->online == TRUE) {
1029                         if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
1030                                 break;
1031                         info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
1032                         __connman_service_session_inc(info->entry->service);
1033                         break;
1034                 }
1035
1036                 select_and_connect(session,
1037                                 CONNMAN_SESSION_REASON_CONNECT);
1038
1039                 break;
1040         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
1041                 deselect_and_disconnect(session,
1042                                         CONNMAN_SESSION_REASON_DISCONNECT);
1043
1044                 break;
1045         case CONNMAN_SESSION_TRIGGER_PERIODIC:
1046                 if (info->online == TRUE) {
1047                         info->entry->reason = CONNMAN_SESSION_REASON_PERIODIC;
1048                         __connman_service_session_inc(info->entry->service);
1049                         break;
1050                 }
1051
1052                 select_and_connect(session,
1053                                 CONNMAN_SESSION_REASON_PERIODIC);
1054
1055                 break;
1056         case CONNMAN_SESSION_TRIGGER_SERVICE:
1057                 if (info->entry != NULL &&
1058                                 (is_connecting(info->entry->state) == TRUE ||
1059                                         is_online(info->entry->state) == TRUE)) {
1060                         break;
1061                 }
1062
1063                 deselect_and_disconnect(session, info->reason);
1064
1065                 if (info->reason == CONNMAN_SESSION_REASON_FREE_RIDE ||
1066                                 info->stay_connected == TRUE) {
1067                         select_and_connect(session, info->reason);
1068                 }
1069
1070                 break;
1071         case CONNMAN_SESSION_TRIGGER_ECALL:
1072                 if (info->online == FALSE && info->entry != NULL &&
1073                                 info->entry->service != NULL) {
1074                         deselect_and_disconnect(session, info->reason);
1075                 }
1076
1077                 break;
1078         }
1079
1080         if (info->entry != info_last->entry)
1081                 session->info_dirty = TRUE;
1082
1083         session_notify(session);
1084 }
1085
1086 static DBusMessage *connect_session(DBusConnection *conn,
1087                                         DBusMessage *msg, void *user_data)
1088 {
1089         struct connman_session *session = user_data;
1090         struct session_info *info = session->info;
1091
1092         DBG("session %p", session);
1093
1094         if (ecall_info != NULL && ecall_info != info)
1095                 return __connman_error_failed(msg, EBUSY);
1096
1097         session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1098
1099         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1100 }
1101
1102 static DBusMessage *disconnect_session(DBusConnection *conn,
1103                                         DBusMessage *msg, void *user_data)
1104 {
1105         struct connman_session *session = user_data;
1106         struct session_info *info = session->info;
1107
1108         DBG("session %p", session);
1109
1110         if (ecall_info != NULL && ecall_info != info)
1111                 return __connman_error_failed(msg, EBUSY);
1112
1113         session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1114
1115         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1116 }
1117
1118 static void update_ecall_sessions(struct connman_session *session)
1119 {
1120         struct session_info *info = session->info;
1121         struct connman_session *session_iter;
1122         GHashTableIter iter;
1123         gpointer key, value;
1124
1125         g_hash_table_iter_init(&iter, session_hash);
1126
1127         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1128                 session_iter = value;
1129
1130                 if (session_iter == session)
1131                         continue;
1132
1133                 session_iter->info->ecall = info->ecall;
1134                 session_iter->info_dirty = TRUE;
1135
1136                 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
1137         }
1138 }
1139
1140 static void update_ecall(struct connman_session *session)
1141 {
1142         struct session_info *info = session->info;
1143         struct session_info *info_last = session->info_last;
1144
1145         DBG("session %p ecall_info %p ecall %d -> %d", session,
1146                 ecall_info, info_last->ecall, info->ecall);
1147
1148         if (ecall_info == NULL) {
1149                 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
1150                         goto err;
1151
1152                 ecall_info = info;
1153         } else if (ecall_info == info) {
1154                 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
1155                         goto err;
1156
1157                 ecall_info = NULL;
1158         } else {
1159                 goto err;
1160         }
1161
1162         update_ecall_sessions(session);
1163
1164         session->info_dirty = TRUE;
1165         return;
1166
1167 err:
1168         /* not a valid transition */
1169         info->ecall = info_last->ecall;
1170 }
1171
1172 static DBusMessage *change_session(DBusConnection *conn,
1173                                         DBusMessage *msg, void *user_data)
1174 {
1175         struct connman_session *session = user_data;
1176         struct session_info *info = session->info;
1177         struct session_info *info_last = session->info_last;
1178         DBusMessageIter iter, value;
1179         const char *name;
1180         GSList *allowed_bearers;
1181
1182         DBG("session %p", session);
1183         if (dbus_message_iter_init(msg, &iter) == FALSE)
1184                 return __connman_error_invalid_arguments(msg);
1185
1186         dbus_message_iter_get_basic(&iter, &name);
1187         dbus_message_iter_next(&iter);
1188         dbus_message_iter_recurse(&iter, &value);
1189
1190         switch (dbus_message_iter_get_arg_type(&value)) {
1191         case DBUS_TYPE_ARRAY:
1192                 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1193                         allowed_bearers = session_parse_allowed_bearers(&value);
1194
1195                         g_slist_foreach(info->allowed_bearers,
1196                                         cleanup_bearer_info, NULL);
1197                         g_slist_free(info->allowed_bearers);
1198
1199                         if (allowed_bearers == NULL) {
1200                                 allowed_bearers = session_allowed_bearers_any();
1201
1202                                 if (allowed_bearers == NULL)
1203                                         return __connman_error_failed(msg, ENOMEM);
1204                         }
1205
1206                         info->allowed_bearers = allowed_bearers;
1207
1208                         session->info_dirty = TRUE;
1209                 } else {
1210                         goto err;
1211                 }
1212                 break;
1213         case DBUS_TYPE_BOOLEAN:
1214                 if (g_str_equal(name, "Priority") == TRUE) {
1215                         dbus_message_iter_get_basic(&value,
1216                                         &info->priority);
1217
1218                         if (info_last->priority != info->priority)
1219                                 session->info_dirty = TRUE;
1220                 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1221                         dbus_message_iter_get_basic(&value,
1222                                         &info->avoid_handover);
1223
1224                         if (info_last->avoid_handover != info->avoid_handover)
1225                                 session->info_dirty = TRUE;
1226                 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1227                         dbus_message_iter_get_basic(&value,
1228                                         &info->stay_connected);
1229
1230                         if (info_last->stay_connected != info->stay_connected)
1231                                 session->info_dirty = TRUE;
1232                 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1233                         dbus_message_iter_get_basic(&value,
1234                                         &info->ecall);
1235
1236                         update_ecall(session);
1237                 } else {
1238                         goto err;
1239                 }
1240                 break;
1241         case DBUS_TYPE_UINT32:
1242                 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1243                         dbus_message_iter_get_basic(&value,
1244                                         &info->periodic_connect);
1245
1246                         if (info_last->periodic_connect != info->periodic_connect)
1247                                 session->info_dirty = TRUE;
1248                 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1249                         dbus_message_iter_get_basic(&value,
1250                                         &info->idle_timeout);
1251
1252                         if (info_last->idle_timeout != info->idle_timeout)
1253                                 session->info_dirty = TRUE;
1254                 } else {
1255                         goto err;
1256                 }
1257                 break;
1258         case DBUS_TYPE_STRING:
1259                 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1260                         const char *val;
1261                         dbus_message_iter_get_basic(&value, &val);
1262                         info->roaming_policy =
1263                                         string2roamingpolicy(val);
1264
1265                         if (info_last->roaming_policy != info->roaming_policy)
1266                                 session->info_dirty = TRUE;
1267                 } else {
1268                         goto err;
1269                 }
1270                 break;
1271         default:
1272                 goto err;
1273         }
1274
1275         if (session->info_dirty == TRUE)
1276                 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1277
1278         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1279
1280 err:
1281         return __connman_error_invalid_arguments(msg);
1282 }
1283
1284 static void release_session(gpointer key, gpointer value, gpointer user_data)
1285 {
1286         struct connman_session *session = value;
1287         DBusMessage *message;
1288
1289         DBG("owner %s path %s", session->owner, session->notify_path);
1290
1291         if (session->notify_watch > 0)
1292                 g_dbus_remove_watch(connection, session->notify_watch);
1293
1294         g_dbus_unregister_interface(connection, session->session_path,
1295                                                 CONNMAN_SESSION_INTERFACE);
1296
1297         message = dbus_message_new_method_call(session->owner,
1298                                                 session->notify_path,
1299                                                 CONNMAN_NOTIFICATION_INTERFACE,
1300                                                 "Release");
1301         if (message == NULL)
1302                 return;
1303
1304         dbus_message_set_no_reply(message, TRUE);
1305
1306         g_dbus_send_message(connection, message);
1307 }
1308
1309 static int session_disconnect(struct connman_session *session)
1310 {
1311         DBG("session %p, %s", session, session->owner);
1312
1313         if (session->notify_watch > 0)
1314                 g_dbus_remove_watch(connection, session->notify_watch);
1315
1316         g_dbus_unregister_interface(connection, session->session_path,
1317                                                 CONNMAN_SESSION_INTERFACE);
1318
1319         deselect_and_disconnect(session,
1320                                 CONNMAN_SESSION_REASON_DISCONNECT);
1321
1322         g_hash_table_remove(session_hash, session->session_path);
1323
1324         return 0;
1325 }
1326
1327 static void owner_disconnect(DBusConnection *conn, void *user_data)
1328 {
1329         struct connman_session *session = user_data;
1330
1331         DBG("session %p, %s died", session, session->owner);
1332
1333         session_disconnect(session);
1334 }
1335
1336 static DBusMessage *destroy_session(DBusConnection *conn,
1337                                         DBusMessage *msg, void *user_data)
1338 {
1339         struct connman_session *session = user_data;
1340
1341         DBG("session %p", session);
1342
1343         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1344 }
1345
1346 static GDBusMethodTable session_methods[] = {
1347         { "Destroy",    "",   "", destroy_session    },
1348         { "Connect",    "",   "", connect_session    },
1349         { "Disconnect", "",   "", disconnect_session },
1350         { "Change",     "sv", "", change_session     },
1351         { },
1352 };
1353
1354 int __connman_session_create(DBusMessage *msg)
1355 {
1356         const char *owner, *notify_path;
1357         char *session_path = NULL;
1358         DBusMessageIter iter, array;
1359         struct connman_session *session = NULL;
1360         struct session_info *info, *info_last;
1361
1362         connman_bool_t priority = FALSE, avoid_handover = FALSE;
1363         connman_bool_t stay_connected = FALSE, ecall = FALSE;
1364         enum connman_session_roaming_policy roaming_policy =
1365                                 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1366         GSList *allowed_bearers = NULL;
1367         unsigned int periodic_connect = 0;
1368         unsigned int idle_timeout = 0;
1369
1370         int err;
1371
1372         owner = dbus_message_get_sender(msg);
1373
1374         DBG("owner %s", owner);
1375
1376         if (ecall_info != NULL) {
1377                 /*
1378                  * If there is an emergency call already going on,
1379                  * ignore session creation attempt
1380                  */
1381                 err = -EBUSY;
1382                 goto err;
1383         }
1384
1385         dbus_message_iter_init(msg, &iter);
1386         dbus_message_iter_recurse(&iter, &array);
1387
1388         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1389                 DBusMessageIter entry, value;
1390                 const char *key, *val;
1391
1392                 dbus_message_iter_recurse(&array, &entry);
1393                 dbus_message_iter_get_basic(&entry, &key);
1394
1395                 dbus_message_iter_next(&entry);
1396                 dbus_message_iter_recurse(&entry, &value);
1397
1398                 switch (dbus_message_iter_get_arg_type(&value)) {
1399                 case DBUS_TYPE_ARRAY:
1400                         if (g_str_equal(key, "AllowedBearers") == TRUE) {
1401                                 allowed_bearers =
1402                                         session_parse_allowed_bearers(&value);
1403                         } else {
1404                                 return -EINVAL;
1405                         }
1406                         break;
1407                 case DBUS_TYPE_BOOLEAN:
1408                         if (g_str_equal(key, "Priority") == TRUE) {
1409                                 dbus_message_iter_get_basic(&value,
1410                                                         &priority);
1411                         } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1412                                 dbus_message_iter_get_basic(&value,
1413                                                         &avoid_handover);
1414                         } else if (g_str_equal(key, "StayConnected") == TRUE) {
1415                                 dbus_message_iter_get_basic(&value,
1416                                                         &stay_connected);
1417                         } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1418                                 dbus_message_iter_get_basic(&value,
1419                                                         &ecall);
1420                         } else {
1421                                 return -EINVAL;
1422                         }
1423                         break;
1424                 case DBUS_TYPE_UINT32:
1425                         if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1426                                 dbus_message_iter_get_basic(&value,
1427                                                         &periodic_connect);
1428                         } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1429                                 dbus_message_iter_get_basic(&value,
1430                                                         &idle_timeout);
1431                         } else {
1432                                 return -EINVAL;
1433                         }
1434                         break;
1435                 case DBUS_TYPE_STRING:
1436                         if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1437                                 dbus_message_iter_get_basic(&value, &val);
1438                                 roaming_policy = string2roamingpolicy(val);
1439                         } else {
1440                                 return -EINVAL;
1441                         }
1442                 }
1443                 dbus_message_iter_next(&array);
1444         }
1445
1446         dbus_message_iter_next(&iter);
1447         dbus_message_iter_get_basic(&iter, &notify_path);
1448
1449         if (notify_path == NULL) {
1450                 err = -EINVAL;
1451                 goto err;
1452         }
1453
1454         session_path = g_strdup_printf("/sessions%s", notify_path);
1455         if (session_path == NULL) {
1456                 err = -ENOMEM;
1457                 goto err;
1458         }
1459
1460         session = g_hash_table_lookup(session_hash, session_path);
1461         if (session != NULL) {
1462                 session = NULL;
1463                 err = -EEXIST;
1464                 goto err;
1465         }
1466
1467         session = g_try_new0(struct connman_session, 1);
1468         if (session == NULL) {
1469                 err = -ENOMEM;
1470                 goto err;
1471         }
1472
1473         session->info = g_try_new0(struct session_info, 1);
1474         if (session->info == NULL) {
1475                 err = -ENOMEM;
1476                 goto err;
1477         }
1478
1479         session->info_last = g_try_new0(struct session_info, 1);
1480         if (session->info_last == NULL) {
1481                 err = -ENOMEM;
1482                 goto err;
1483         }
1484
1485         info = session->info;
1486         info_last = session->info_last;
1487
1488         session->owner = g_strdup(owner);
1489         session->session_path = session_path;
1490         session->notify_path = g_strdup(notify_path);
1491         session->notify_watch =
1492                 g_dbus_add_disconnect_watch(connection, session->owner,
1493                                         owner_disconnect, session, NULL);
1494
1495         info->online = FALSE;
1496         info->priority = priority;
1497         info->avoid_handover = avoid_handover;
1498         info->stay_connected = stay_connected;
1499         info->periodic_connect = periodic_connect;
1500         info->idle_timeout = idle_timeout;
1501         info->ecall = ecall;
1502         info->roaming_policy = roaming_policy;
1503         info->entry = NULL;
1504         info->marker = 0;
1505
1506         if (allowed_bearers == NULL) {
1507                 info->allowed_bearers =
1508                                 session_allowed_bearers_any();
1509
1510                 if (info->allowed_bearers == NULL) {
1511                         err = -ENOMEM;
1512                         goto err;
1513                 }
1514         } else {
1515                 info->allowed_bearers = allowed_bearers;
1516         }
1517
1518         g_hash_table_replace(session_hash, session->session_path, session);
1519
1520         DBG("add %s", session->session_path);
1521
1522         if (g_dbus_register_interface(connection, session->session_path,
1523                                         CONNMAN_SESSION_INTERFACE,
1524                                         session_methods, NULL,
1525                                         NULL, session, NULL) == FALSE) {
1526                 connman_error("Failed to register %s", session->session_path);
1527                 g_hash_table_remove(session_hash, session->session_path);
1528                 session = NULL;
1529
1530                 err = -EINVAL;
1531                 goto err;
1532         }
1533
1534         g_dbus_send_reply(connection, msg,
1535                                 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1536                                 DBUS_TYPE_INVALID);
1537
1538
1539         populate_service_list(session);
1540         if (info->ecall == TRUE) {
1541                 ecall_info = info;
1542                 update_ecall_sessions(session);
1543         }
1544
1545         info_last->online = info->online;
1546         info_last->priority = info->priority;
1547         info_last->avoid_handover = info->avoid_handover;
1548         info_last->stay_connected = info->stay_connected;
1549         info_last->periodic_connect = info->periodic_connect;
1550         info_last->idle_timeout = info->idle_timeout;
1551         info_last->ecall = info->ecall;
1552         info_last->roaming_policy = info->roaming_policy;
1553         info_last->entry = info->entry;
1554         info_last->marker = info->marker;
1555         info_last->allowed_bearers = info->allowed_bearers;
1556
1557         session->info_dirty = TRUE;
1558         session->append_all = TRUE;
1559
1560         session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1561
1562         return 0;
1563
1564 err:
1565         connman_error("Failed to create session");
1566
1567         if (session != NULL) {
1568                 if (session->info_last != NULL)
1569                         g_free(session->info_last);
1570                 if (session->info != NULL)
1571                         g_free(session->info);
1572                 g_free(session);
1573         }
1574
1575         g_free(session_path);
1576
1577         g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1578         g_slist_free(allowed_bearers);
1579
1580         return err;
1581 }
1582
1583 int __connman_session_destroy(DBusMessage *msg)
1584 {
1585         const char *owner, *session_path;
1586         struct connman_session *session;
1587
1588         owner = dbus_message_get_sender(msg);
1589
1590         DBG("owner %s", owner);
1591
1592         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1593                                                         DBUS_TYPE_INVALID);
1594         if (session_path == NULL)
1595                 return -EINVAL;
1596
1597         session = g_hash_table_lookup(session_hash, session_path);
1598         if (session == NULL)
1599                 return -EINVAL;
1600
1601         if (g_strcmp0(owner, session->owner) != 0)
1602                 return -EACCES;
1603
1604         session_disconnect(session);
1605
1606         return 0;
1607 }
1608
1609 connman_bool_t __connman_session_mode()
1610 {
1611         return sessionmode;
1612 }
1613
1614 void __connman_session_set_mode(connman_bool_t enable)
1615 {
1616         DBG("enable %d", enable);
1617
1618         sessionmode = enable;
1619
1620         if (sessionmode == TRUE)
1621                 __connman_service_disconnect_all();
1622 }
1623
1624 static void service_add(struct connman_service *service,
1625                         const char *name)
1626 {
1627         GHashTableIter iter;
1628         GSequenceIter *iter_service_list;
1629         gpointer key, value;
1630         struct connman_session *session;
1631         struct service_entry *entry;
1632
1633         DBG("service %p", service);
1634
1635         g_hash_table_iter_init(&iter, session_hash);
1636
1637         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1638                 session = value;
1639
1640                 if (service_match(session, service) == FALSE)
1641                         continue;
1642
1643                 entry = create_service_entry(service, name,
1644                                                 CONNMAN_SERVICE_STATE_IDLE);
1645                 if (entry == NULL)
1646                         continue;
1647
1648                 iter_service_list =
1649                         g_sequence_insert_sorted(session->service_list,
1650                                                         entry, sort_services,
1651                                                         session);
1652
1653                 g_hash_table_replace(session->service_hash, service,
1654                                         iter_service_list);
1655
1656                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1657         }
1658 }
1659
1660 static void service_remove(struct connman_service *service)
1661 {
1662
1663         GHashTableIter iter;
1664         gpointer key, value;
1665         struct connman_session *session;
1666         struct session_info *info;
1667
1668         DBG("service %p", service);
1669
1670         g_hash_table_iter_init(&iter, session_hash);
1671
1672         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1673                 GSequenceIter *iter;
1674                 session = value;
1675                 info = session->info;
1676
1677                 iter = g_hash_table_lookup(session->service_hash, service);
1678                 if (iter == NULL)
1679                         continue;
1680
1681                 g_sequence_remove(iter);
1682
1683                 if (info->entry != NULL && info->entry->service == service)
1684                         info->entry = NULL;
1685                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1686         }
1687 }
1688
1689 static void service_state_changed(struct connman_service *service,
1690                                         enum connman_service_state state)
1691 {
1692         GHashTableIter iter;
1693         gpointer key, value;
1694
1695         DBG("service %p state %d", service, state);
1696
1697         g_hash_table_iter_init(&iter, session_hash);
1698
1699         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1700                 struct connman_session *session = value;
1701                 GSequenceIter *service_iter;
1702
1703                 service_iter = g_hash_table_lookup(session->service_hash, service);
1704                 if (service_iter != NULL) {
1705                         struct service_entry *entry;
1706
1707                         entry = g_sequence_get(service_iter);
1708                         entry->state = state;
1709                 }
1710
1711                 session_changed(session,
1712                                 CONNMAN_SESSION_TRIGGER_SERVICE);
1713         }
1714 }
1715
1716 static void ipconfig_changed(struct connman_service *service,
1717                                 struct connman_ipconfig *ipconfig)
1718 {
1719         GHashTableIter iter;
1720         gpointer key, value;
1721         struct connman_session *session;
1722         struct session_info *info;
1723         enum connman_ipconfig_type type;
1724
1725         DBG("service %p ipconfig %p", service, ipconfig);
1726
1727         type = __connman_ipconfig_get_config_type(ipconfig);
1728
1729         g_hash_table_iter_init(&iter, session_hash);
1730
1731         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1732                 session = value;
1733                 info = session->info;
1734
1735                 if (info->entry != NULL && info->entry->service == service) {
1736                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1737                                 ipconfig_ipv4_changed(session);
1738                         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1739                                 ipconfig_ipv6_changed(session);
1740                 }
1741         }
1742 }
1743
1744 static struct connman_notifier session_notifier = {
1745         .name                   = "session",
1746         .service_add            = service_add,
1747         .service_remove         = service_remove,
1748         .service_state_changed  = service_state_changed,
1749         .ipconfig_changed       = ipconfig_changed,
1750 };
1751
1752 int __connman_session_init(void)
1753 {
1754         int err;
1755
1756         DBG("");
1757
1758         connection = connman_dbus_get_connection();
1759         if (connection == NULL)
1760                 return -1;
1761
1762         err = connman_notifier_register(&session_notifier);
1763         if (err < 0) {
1764                 dbus_connection_unref(connection);
1765                 return err;
1766         }
1767
1768         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1769                                                 NULL, cleanup_session);
1770
1771         sessionmode = FALSE;
1772         return 0;
1773 }
1774
1775 void __connman_session_cleanup(void)
1776 {
1777         DBG("");
1778
1779         if (connection == NULL)
1780                 return;
1781
1782         connman_notifier_unregister(&session_notifier);
1783
1784         g_hash_table_foreach(session_hash, release_session, NULL);
1785         g_hash_table_destroy(session_hash);
1786         session_hash = NULL;
1787
1788         dbus_connection_unref(connection);
1789 }