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