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