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