5cc85c4c35ddf499b5a45850decd174d754a4131
[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 <gdbus.h>
28
29 #include "connman.h"
30
31 static DBusConnection *connection;
32 static GHashTable *session_hash;
33 static connman_bool_t sessionmode;
34 static struct connman_session *ecall_session;
35
36 enum connman_session_roaming_policy {
37         CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN          = 0,
38         CONNMAN_SESSION_ROAMING_POLICY_DEFAULT          = 1,
39         CONNMAN_SESSION_ROAMING_POLICY_ALWAYS           = 2,
40         CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN        = 3,
41         CONNMAN_SESSION_ROAMING_POLICY_NATIONAL         = 4,
42         CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL    = 5,
43 };
44
45 struct session_info {
46         char *bearer;
47         const char *name;
48         char *ifname;
49         connman_bool_t connect;
50         connman_bool_t online;
51         connman_bool_t priority;
52         GSList *allowed_bearers;
53         connman_bool_t avoid_handover;
54         connman_bool_t stay_connected;
55         unsigned int periodic_connect;
56         unsigned int idle_timeout;
57         connman_bool_t ecall;
58         enum connman_session_roaming_policy roaming_policy;
59         unsigned int marker;
60
61         struct connman_service *service;
62 };
63
64 struct connman_session {
65         char *owner;
66         char *session_path;
67         char *notify_path;
68         guint notify_watch;
69
70         connman_bool_t append_all;
71         connman_bool_t info_dirty;
72         struct session_info info;
73         struct session_info info_last;
74
75         GSequence *service_list;
76 };
77
78 struct bearer_info {
79         char *name;
80         connman_bool_t match_all;
81         enum connman_service_type service_type;
82 };
83
84 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
85 {
86         switch (policy) {
87         case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
88                 break;
89         case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
90                 return "default";
91         case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
92                 return "always";
93         case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
94                 return "forbidden";
95         case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
96                 return "national";
97         case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
98                 return "international";
99         }
100
101         return NULL;
102 }
103
104 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
105 {
106         if (g_strcmp0(policy, "default") == 0)
107                 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
108         else if (g_strcmp0(policy, "always") == 0)
109                 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
110         else if (g_strcmp0(policy, "forbidden") == 0)
111                 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
112         else if (g_strcmp0(policy, "national") == 0)
113                 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
114         else if (g_strcmp0(policy, "international") == 0)
115                 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
116         else
117                 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
118 }
119
120 static enum connman_service_type bearer2service(const char *bearer)
121 {
122         if (bearer == NULL)
123                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
124
125         if (g_strcmp0(bearer, "ethernet") == 0)
126                 return CONNMAN_SERVICE_TYPE_ETHERNET;
127         else if (g_strcmp0(bearer, "wifi") == 0)
128                 return CONNMAN_SERVICE_TYPE_WIFI;
129         else if (g_strcmp0(bearer, "wimax") == 0)
130                 return CONNMAN_SERVICE_TYPE_WIMAX;
131         else if (g_strcmp0(bearer, "bluetooth") == 0)
132                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
133         else if (g_strcmp0(bearer, "3g") == 0)
134                 return CONNMAN_SERVICE_TYPE_CELLULAR;
135         else
136                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
137 }
138
139 static char *service2bearer(enum connman_service_type type)
140 {
141         switch (type) {
142         case CONNMAN_SERVICE_TYPE_ETHERNET:
143                 return "ethernet";
144         case CONNMAN_SERVICE_TYPE_WIFI:
145                 return "wifi";
146         case CONNMAN_SERVICE_TYPE_WIMAX:
147                 return "wimax";
148         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
149                 return "bluetooth";
150         case CONNMAN_SERVICE_TYPE_CELLULAR:
151                 return "3g";
152         case CONNMAN_SERVICE_TYPE_UNKNOWN:
153         case CONNMAN_SERVICE_TYPE_SYSTEM:
154         case CONNMAN_SERVICE_TYPE_GPS:
155         case CONNMAN_SERVICE_TYPE_VPN:
156         case CONNMAN_SERVICE_TYPE_GADGET:
157                 return "";
158         }
159
160         return "";
161 }
162
163 static void cleanup_bearer_info(gpointer data, gpointer user_data)
164 {
165         struct bearer_info *info = data;
166
167         g_free(info->name);
168         g_free(info);
169 }
170
171 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
172 {
173         struct bearer_info *info;
174         DBusMessageIter array;
175         GSList *list = NULL;
176
177         dbus_message_iter_recurse(iter, &array);
178
179         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
180                 char *bearer = NULL;
181
182                 dbus_message_iter_get_basic(&array, &bearer);
183
184                 info = g_try_new0(struct bearer_info, 1);
185                 if (info == NULL) {
186                         g_slist_foreach(list, cleanup_bearer_info, NULL);
187                         g_slist_free(list);
188
189                         return NULL;
190                 }
191
192                 info->name = g_strdup(bearer);
193                 info->service_type = bearer2service(info->name);
194
195                 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
196                                 g_strcmp0(info->name, "*") == 0) {
197                         info->match_all = TRUE;
198                 } else {
199                         info->match_all = FALSE;
200                 }
201
202                 list = g_slist_append(list, info);
203
204                 dbus_message_iter_next(&array);
205         }
206
207         return list;
208 }
209
210 static GSList *session_allowed_bearers_any(void)
211 {
212         struct bearer_info *info;
213         GSList *list = NULL;
214
215         info = g_try_new0(struct bearer_info, 1);
216         if (info == NULL) {
217                 g_slist_free(list);
218
219                 return NULL;
220         }
221
222         info->name = g_strdup("");
223         info->match_all = TRUE;
224         info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
225
226         list = g_slist_append(list, info);
227
228         return list;
229 }
230
231 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
232 {
233         struct session_info *info = user_data;
234         GSList *list;
235
236         for (list = info->allowed_bearers;
237                         list != NULL; list = list->next) {
238                 struct bearer_info *info = list->data;
239
240                 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
241                                                 &info->name);
242         }
243 }
244
245 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
246 {
247         struct connman_service *service = user_data;
248         struct connman_ipconfig *ipconfig_ipv4;
249
250         if (service == NULL)
251                 return;
252
253         ipconfig_ipv4 = __connman_service_get_ip4config(service);
254         if (ipconfig_ipv4 == NULL)
255                 return;
256
257         __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
258 }
259
260 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
261 {
262         struct connman_service *service = user_data;
263         struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
264
265         if (service == NULL)
266                 return;
267
268         ipconfig_ipv4 = __connman_service_get_ip4config(service);
269         ipconfig_ipv6 = __connman_service_get_ip6config(service);
270         if (ipconfig_ipv6 == NULL)
271                 return;
272
273         __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
274 }
275
276 static void append_notify(DBusMessageIter *dict,
277                                         struct connman_session *session)
278 {
279         struct session_info *info = &session->info;
280         struct session_info *info_last = &session->info_last;
281         const char *policy;
282
283         if (session->append_all == TRUE ||
284                         info->bearer != info_last->bearer) {
285                 connman_dbus_dict_append_basic(dict, "Bearer",
286                                                 DBUS_TYPE_STRING,
287                                                 &info->bearer);
288                 info_last->bearer = info->bearer;
289         }
290
291         if (session->append_all == TRUE ||
292                         info->online != info_last->online) {
293                 connman_dbus_dict_append_basic(dict, "Online",
294                                                 DBUS_TYPE_BOOLEAN,
295                                                 &info->online);
296                 info_last->online = info->online;
297         }
298
299         if (session->append_all == TRUE ||
300                         info->name != info_last->name) {
301                 connman_dbus_dict_append_basic(dict, "Name",
302                                                 DBUS_TYPE_STRING,
303                                                 &info->name);
304                 info_last->name = info->name;
305         }
306
307         if (session->append_all == TRUE ||
308                         info->service != info_last->service) {
309                 connman_dbus_dict_append_dict(dict, "IPv4",
310                                                 append_ipconfig_ipv4,
311                                                 info->service);
312
313                 connman_dbus_dict_append_dict(dict, "IPv6",
314                                                 append_ipconfig_ipv6,
315                                                 info->service);
316
317                 connman_dbus_dict_append_basic(dict, "Interface",
318                                                 DBUS_TYPE_STRING,
319                                                 &info->ifname);
320
321                 info_last->ifname = info->ifname;
322                 info_last->service = info->service;
323         }
324
325
326         if (session->append_all == TRUE ||
327                         info->priority != info_last->priority) {
328                 connman_dbus_dict_append_basic(dict, "Priority",
329                                                 DBUS_TYPE_BOOLEAN,
330                                                 &info->priority);
331                 info_last->priority = info->priority;
332         }
333
334         if (session->append_all == TRUE ||
335                         info->allowed_bearers != info_last->allowed_bearers) {
336                 connman_dbus_dict_append_array(dict, "AllowedBearers",
337                                                 DBUS_TYPE_STRING,
338                                                 append_allowed_bearers,
339                                                 info);
340                 info_last->allowed_bearers = info->allowed_bearers;
341         }
342
343         if (session->append_all == TRUE ||
344                         info->avoid_handover != info_last->avoid_handover) {
345                 connman_dbus_dict_append_basic(dict, "AvoidHandover",
346                                                 DBUS_TYPE_BOOLEAN,
347                                                 &info->avoid_handover);
348                 info_last->avoid_handover = info->avoid_handover;
349         }
350
351         if (session->append_all == TRUE ||
352                         info->stay_connected != info_last->stay_connected) {
353                 connman_dbus_dict_append_basic(dict, "StayConnected",
354                                                 DBUS_TYPE_BOOLEAN,
355                                                 &info->stay_connected);
356                 info_last->stay_connected = info->stay_connected;
357         }
358
359         if (session->append_all == TRUE ||
360                         info->periodic_connect != info_last->periodic_connect) {
361                 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
362                                                 DBUS_TYPE_UINT32,
363                                                 &info->periodic_connect);
364                 info_last->periodic_connect = info->periodic_connect;
365         }
366
367         if (session->append_all == TRUE ||
368                         info->idle_timeout != info_last->idle_timeout) {
369                 connman_dbus_dict_append_basic(dict, "IdleTimeout",
370                                                 DBUS_TYPE_UINT32,
371                                                 &info->idle_timeout);
372                 info_last->idle_timeout = info->idle_timeout;
373         }
374
375         if (session->append_all == TRUE ||
376                         info->ecall != info_last->ecall) {
377                 connman_dbus_dict_append_basic(dict, "EmergencyCall",
378                                                 DBUS_TYPE_BOOLEAN,
379                                                 &info->ecall);
380                 info_last->ecall = info->ecall;
381         }
382
383         if (session->append_all == TRUE ||
384                         info->roaming_policy != info_last->roaming_policy) {
385                 policy = roamingpolicy2string(info->roaming_policy);
386                 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
387                                                 DBUS_TYPE_STRING,
388                                                 &policy);
389                 info_last->roaming_policy = info->roaming_policy;
390         }
391
392         if (session->append_all == TRUE ||
393                         info->marker != info_last->marker) {
394                 connman_dbus_dict_append_basic(dict, "SessionMarker",
395                                                 DBUS_TYPE_UINT32,
396                                                 &info->marker);
397                 info_last->marker = info->marker;
398         }
399
400         session->append_all = FALSE;
401         session->info_dirty = FALSE;
402 }
403
404 static int session_notify(struct connman_session *session)
405 {
406         DBusMessage *msg;
407         DBusMessageIter array, dict;
408
409         if (session->info_dirty == FALSE)
410                 return 0;
411
412         DBG("session %p owner %s notify_path %s", session,
413                 session->owner, session->notify_path);
414
415         msg = dbus_message_new_method_call(session->owner, session->notify_path,
416                                                 CONNMAN_NOTIFICATION_INTERFACE,
417                                                 "Update");
418         if (msg == NULL)
419                 return -ENOMEM;
420
421         dbus_message_iter_init_append(msg, &array);
422         connman_dbus_dict_open(&array, &dict);
423
424         append_notify(&dict, session);
425
426         connman_dbus_dict_close(&array, &dict);
427
428         g_dbus_send_message(connection, msg);
429
430         session->info_dirty = FALSE;
431
432         return 0;
433 }
434
435 static void ipconfig_ipv4_changed(struct connman_session *session)
436 {
437         struct session_info *info = &session->info;
438
439         connman_dbus_setting_changed_dict(session->owner, session->notify_path,
440                                                 "IPv4", append_ipconfig_ipv4,
441                                                 info->service);
442 }
443
444 static void ipconfig_ipv6_changed(struct connman_session *session)
445 {
446         struct session_info *info = &session->info;
447
448         connman_dbus_setting_changed_dict(session->owner, session->notify_path,
449                                                 "IPv6", append_ipconfig_ipv6,
450                                                 info->service);
451 }
452
453 static connman_bool_t service_type_match(struct connman_session *session,
454                                         struct connman_service *service)
455 {
456         struct session_info *info = &session->info;
457         GSList *list;
458
459         for (list = info->allowed_bearers;
460                         list != NULL; list = list->next) {
461                 struct bearer_info *info = list->data;
462                 enum connman_service_type service_type;
463
464                 if (info->match_all == TRUE)
465                         return TRUE;
466
467                 service_type = connman_service_get_type(service);
468                 if (info->service_type == service_type)
469                         return TRUE;
470         }
471
472         return FALSE;
473 }
474
475 static connman_bool_t service_match(struct connman_session *session,
476                                         struct connman_service *service)
477 {
478         if (service_type_match(session, service) == FALSE)
479                 return FALSE;
480
481         return TRUE;
482 }
483
484 static int service_type_weight(enum connman_service_type type)
485 {
486         /*
487          * The session doesn't care which service
488          * to use. Nevertheless we have to sort them
489          * according their type. The ordering is
490          *
491          * 1. Ethernet
492          * 2. Bluetooth
493          * 3. WiFi/WiMAX
494          * 4. GSM/UTMS/3G
495          */
496
497         switch (type) {
498         case CONNMAN_SERVICE_TYPE_ETHERNET:
499                 return 4;
500         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
501                 return 3;
502         case CONNMAN_SERVICE_TYPE_WIFI:
503         case CONNMAN_SERVICE_TYPE_WIMAX:
504                 return 2;
505         case CONNMAN_SERVICE_TYPE_CELLULAR:
506                 return 1;
507         case CONNMAN_SERVICE_TYPE_UNKNOWN:
508         case CONNMAN_SERVICE_TYPE_SYSTEM:
509         case CONNMAN_SERVICE_TYPE_GPS:
510         case CONNMAN_SERVICE_TYPE_VPN:
511         case CONNMAN_SERVICE_TYPE_GADGET:
512                 break;
513         }
514
515         return 0;
516 }
517
518 static gint sort_allowed_bearers(struct connman_service *service_a,
519                                         struct connman_service *service_b,
520                                         struct connman_session *session)
521 {
522         struct session_info *info = &session->info;
523         GSList *list;
524         enum connman_service_type type_a, type_b;
525         int weight_a, weight_b;
526
527         type_a = connman_service_get_type(service_a);
528         type_b = connman_service_get_type(service_b);
529
530         for (list = info->allowed_bearers;
531                         list != NULL; list = list->next) {
532                 struct bearer_info *info = list->data;
533
534                 if (info->match_all == TRUE) {
535                         if (type_a != type_b) {
536                                 weight_a = service_type_weight(type_a);
537                                 weight_b = service_type_weight(type_b);
538
539                                 if (weight_a > weight_b)
540                                         return -1;
541
542                                 if (weight_a < weight_b)
543                                         return 1;
544
545                                 return 0;
546                         }
547                 }
548
549                 if (type_a == info->service_type &&
550                                 type_b == info->service_type) {
551                         return 0;
552                 }
553
554                 if (type_a == info->service_type &&
555                                 type_b != info->service_type) {
556                         return -1;
557                 }
558
559                 if (type_a != info->service_type &&
560                                 type_b == info->service_type) {
561                         return 1;
562                 }
563         }
564
565         return 0;
566 }
567
568 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
569 {
570         struct connman_service *service_a = (void *)a;
571         struct connman_service *service_b = (void *)b;
572         struct connman_session *session = user_data;
573
574         return sort_allowed_bearers(service_a, service_b, session);
575 }
576
577 static void cleanup_session(gpointer user_data)
578 {
579         struct connman_session *session = user_data;
580         struct session_info *info = &session->info;
581
582         DBG("remove %s", session->session_path);
583
584         g_sequence_free(session->service_list);
585
586         g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
587         g_slist_free(info->allowed_bearers);
588
589         g_free(session->owner);
590         g_free(session->session_path);
591         g_free(session->notify_path);
592
593         g_free(session);
594 }
595
596 static void release_session(gpointer key, gpointer value, gpointer user_data)
597 {
598         struct connman_session *session = value;
599         DBusMessage *message;
600
601         DBG("owner %s path %s", session->owner, session->notify_path);
602
603         if (session->notify_watch > 0)
604                 g_dbus_remove_watch(connection, session->notify_watch);
605
606         g_dbus_unregister_interface(connection, session->session_path,
607                                                 CONNMAN_SESSION_INTERFACE);
608
609         message = dbus_message_new_method_call(session->owner,
610                                                 session->notify_path,
611                                                 CONNMAN_NOTIFICATION_INTERFACE,
612                                                 "Release");
613         if (message == NULL)
614                 return;
615
616         dbus_message_set_no_reply(message, TRUE);
617
618         g_dbus_send_message(connection, message);
619 }
620
621 static int session_disconnect(struct connman_session *session)
622 {
623         DBG("session %p, %s", session, session->owner);
624
625         if (session->notify_watch > 0)
626                 g_dbus_remove_watch(connection, session->notify_watch);
627
628         g_dbus_unregister_interface(connection, session->session_path,
629                                                 CONNMAN_SESSION_INTERFACE);
630
631         g_hash_table_remove(session_hash, session->session_path);
632
633         return 0;
634 }
635
636 static void owner_disconnect(DBusConnection *conn, void *user_data)
637 {
638         struct connman_session *session = user_data;
639
640         DBG("session %p, %s died", session, session->owner);
641
642         session_disconnect(session);
643 }
644
645 static DBusMessage *destroy_session(DBusConnection *conn,
646                                         DBusMessage *msg, void *user_data)
647 {
648         struct connman_session *session = user_data;
649
650         DBG("session %p", session);
651
652         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
653 }
654
655 static gboolean session_changed_connect(gpointer user_data)
656 {
657         struct connman_session *session = user_data;
658         struct session_info *info = &session->info;
659
660         __connman_service_connect(info->service);
661
662         return FALSE;
663 }
664
665 static void update_info(struct session_info *info)
666 {
667         enum connman_service_type type;
668         int idx;
669
670         if (info->service != NULL) {
671                 type = connman_service_get_type(info->service);
672                 info->bearer = service2bearer(type);
673
674                 info->online = __connman_service_is_connected(info->service);
675                 info->name = __connman_service_get_name(info->service);
676
677                 idx = __connman_service_get_index(info->service);
678                 info->ifname = connman_inet_ifname(idx);
679                 if (info->ifname == NULL)
680                         info->ifname = "";
681         } else {
682                 info->bearer = "";
683                 info->online = FALSE;
684                 info->name = "";
685                 info->ifname = "";
686         }
687 }
688
689 static void session_changed(struct connman_session *session)
690 {
691         struct session_info *info = &session->info;
692         struct session_info *info_last = &session->info_last;
693         struct connman_service *service = NULL;
694         GSourceFunc callback = NULL;
695         GSequenceIter *iter;
696
697         /*
698          * TODO: This only a placeholder for the 'real' algorithm to
699          * play a bit around. So we are going to improve it step by step.
700          */
701
702         if (info->ecall == TRUE && session != ecall_session)
703                 goto out;
704
705         if (info->connect == FALSE) {
706                 if (info->service != NULL)
707                         __connman_service_disconnect(info->service);
708                 info->service = NULL;
709                 goto out;
710         }
711
712         iter = g_sequence_get_begin_iter(session->service_list);
713
714         while (g_sequence_iter_is_end(iter) == FALSE) {
715                 service = g_sequence_get(iter);
716
717                 if (__connman_service_is_connecting(service) == TRUE ||
718                                 __connman_service_is_connected(service) == TRUE) {
719                         break;
720                 }
721
722                 if (__connman_service_is_idle(service) == TRUE &&
723                                 info->connect == TRUE) {
724                         callback = session_changed_connect;
725                         break;
726                 }
727
728                 service = NULL;
729
730                 iter = g_sequence_iter_next(iter);
731         }
732
733         if (info->service != NULL && info->service != service) {
734                 __connman_service_disconnect(info->service);
735                 info->service = NULL;
736         }
737
738         if (service != NULL) {
739                 info->service = service;
740
741                 if (callback != NULL)
742                         callback(session);
743         }
744
745 out:
746         if (info->service != info_last->service) {
747                 update_info(info);
748                 session->info_dirty = TRUE;
749         }
750 }
751
752 static gboolean session_cb(gpointer user_data)
753 {
754         struct connman_session *session = user_data;
755
756         session_changed(session);
757         session_notify(session);
758
759         return FALSE;
760 }
761
762 static DBusMessage *connect_session(DBusConnection *conn,
763                                         DBusMessage *msg, void *user_data)
764 {
765         struct connman_session *session = user_data;
766         struct session_info *info = &session->info;
767
768         DBG("session %p", session);
769
770         info->connect = TRUE;
771
772         if (ecall_session != NULL && ecall_session != session)
773                 return __connman_error_failed(msg, EBUSY);
774
775         session->info_dirty = TRUE;
776
777         g_timeout_add_seconds(0, session_cb, session);
778
779         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
780 }
781
782 static DBusMessage *disconnect_session(DBusConnection *conn,
783                                         DBusMessage *msg, void *user_data)
784 {
785         struct connman_session *session = user_data;
786         struct session_info *info = &session->info;
787
788         DBG("session %p", session);
789
790         info->connect = FALSE;
791
792         if (ecall_session != NULL && ecall_session != session)
793                 return __connman_error_failed(msg, EBUSY);
794
795         session->info_dirty = TRUE;
796
797         g_timeout_add_seconds(0, session_cb, session);
798
799         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
800 }
801
802 static void print_name(gpointer data, gpointer user_data)
803 {
804         struct connman_service *service = data;
805
806         DBG("service %p type %s name %s", service,
807                 service2bearer(connman_service_get_type(service)),
808                 __connman_service_get_name(service));
809 }
810
811 static void update_allowed_bearers(struct connman_session *session)
812 {
813         if (session->service_list != NULL)
814                 g_sequence_free(session->service_list);
815
816         session->service_list = __connman_service_get_list(session,
817                                                                 service_match);
818         g_sequence_sort(session->service_list, sort_services, session);
819         g_sequence_foreach(session->service_list, print_name, NULL);
820
821         session->info_dirty = TRUE;
822 }
823
824 static void update_ecall_sessions(struct connman_session *session)
825 {
826         struct session_info *info = &session->info;
827         struct connman_session *session_iter;
828         GHashTableIter iter;
829         gpointer key, value;
830
831         g_hash_table_iter_init(&iter, session_hash);
832
833         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
834                 session_iter = value;
835
836                 if (session_iter == session)
837                         continue;
838
839                 session_iter->info.ecall = info->ecall;
840                 session_iter->info_dirty = TRUE;
841
842                 g_timeout_add_seconds(0, session_cb, session_iter);
843         }
844 }
845
846 static void update_ecall(struct connman_session *session)
847 {
848         struct session_info *info = &session->info;
849         struct session_info *info_last = &session->info_last;
850
851         DBG("ecall_session %p ecall %d -> %d", ecall_session,
852                 info_last->ecall, info->ecall);
853
854         if (ecall_session == NULL) {
855                 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
856                         goto err;
857
858                 ecall_session = session;
859         } else if (ecall_session == session) {
860                 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
861                         goto err;
862
863                 ecall_session = NULL;
864         } else {
865                 goto err;
866         }
867
868         update_ecall_sessions(session);
869
870         session->info_dirty = TRUE;
871         return;
872
873 err:
874         /* not a valid transition */
875         info->ecall = info_last->ecall;
876 }
877
878 static DBusMessage *change_session(DBusConnection *conn,
879                                         DBusMessage *msg, void *user_data)
880 {
881         struct connman_session *session = user_data;
882         struct session_info *info = &session->info;
883         struct session_info *info_last = &session->info_last;
884         DBusMessageIter iter, value;
885         const char *name;
886         GSList *allowed_bearers;
887
888         DBG("session %p", session);
889         if (dbus_message_iter_init(msg, &iter) == FALSE)
890                 return __connman_error_invalid_arguments(msg);
891
892         dbus_message_iter_get_basic(&iter, &name);
893         dbus_message_iter_next(&iter);
894         dbus_message_iter_recurse(&iter, &value);
895
896         switch (dbus_message_iter_get_arg_type(&value)) {
897         case DBUS_TYPE_ARRAY:
898                 if (g_str_equal(name, "AllowedBearers") == TRUE) {
899                         allowed_bearers = session_parse_allowed_bearers(&value);
900
901                         g_slist_foreach(info->allowed_bearers,
902                                         cleanup_bearer_info, NULL);
903                         g_slist_free(info->allowed_bearers);
904
905                         if (allowed_bearers == NULL) {
906                                 allowed_bearers = session_allowed_bearers_any();
907
908                                 if (allowed_bearers == NULL)
909                                         return __connman_error_failed(msg, ENOMEM);
910                         }
911
912                         info->allowed_bearers = allowed_bearers;
913
914                         update_allowed_bearers(session);
915                 } else {
916                         goto err;
917                 }
918                 break;
919         case DBUS_TYPE_BOOLEAN:
920                 if (g_str_equal(name, "Priority") == TRUE) {
921                         dbus_message_iter_get_basic(&value,
922                                         &info->priority);
923
924                         if (info_last->priority != info->priority)
925                                 session->info_dirty = TRUE;
926                 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
927                         dbus_message_iter_get_basic(&value,
928                                         &info->avoid_handover);
929
930                         if (info_last->avoid_handover != info->avoid_handover)
931                                 session->info_dirty = TRUE;
932                         } else if (g_str_equal(name, "StayConnected") == TRUE) {
933                         dbus_message_iter_get_basic(&value,
934                                         &info->stay_connected);
935
936                         if (info_last->stay_connected != info->stay_connected)
937                                 session->info_dirty = TRUE;
938                 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
939                         dbus_message_iter_get_basic(&value,
940                                         &info->ecall);
941
942                         update_ecall(session);
943                 } else {
944                         goto err;
945                 }
946                 break;
947         case DBUS_TYPE_UINT32:
948                 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
949                         dbus_message_iter_get_basic(&value,
950                                         &info->periodic_connect);
951
952                         if (info_last->periodic_connect != info->periodic_connect)
953                                 session->info_dirty = TRUE;
954                 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
955                         dbus_message_iter_get_basic(&value,
956                                         &info->idle_timeout);
957
958                         if (info_last->idle_timeout != info->idle_timeout)
959                                 session->info_dirty = TRUE;
960                 } else {
961                         goto err;
962                 }
963                 break;
964         case DBUS_TYPE_STRING:
965                 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
966                         const char *val;
967                         dbus_message_iter_get_basic(&value, &val);
968                         info->roaming_policy =
969                                         string2roamingpolicy(val);
970
971                         if (info_last->roaming_policy != info->roaming_policy)
972                                 session->info_dirty = TRUE;
973                 } else {
974                         goto err;
975                 }
976                 break;
977         default:
978                 goto err;
979         }
980
981         if (session->info_dirty == TRUE)
982                 session_cb(session);
983
984         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
985
986 err:
987         return __connman_error_invalid_arguments(msg);
988 }
989
990 static GDBusMethodTable session_methods[] = {
991         { "Destroy",    "",   "", destroy_session    },
992         { "Connect",    "",   "", connect_session    },
993         { "Disconnect", "",   "", disconnect_session },
994         { "Change",     "sv", "", change_session     },
995         { },
996 };
997
998 int __connman_session_create(DBusMessage *msg)
999 {
1000         const char *owner, *notify_path;
1001         char *session_path = NULL;
1002         DBusMessageIter iter, array;
1003         struct connman_session *session;
1004         struct session_info *info, *info_last;
1005
1006         connman_bool_t priority = FALSE, avoid_handover = FALSE;
1007         connman_bool_t stay_connected = FALSE, ecall = FALSE;
1008         enum connman_session_roaming_policy roaming_policy =
1009                                 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1010         GSList *allowed_bearers = NULL;
1011         unsigned int periodic_connect = 0;
1012         unsigned int idle_timeout = 0;
1013
1014         int err;
1015
1016         owner = dbus_message_get_sender(msg);
1017
1018         DBG("owner %s", owner);
1019
1020         if (ecall_session != NULL) {
1021                 /*
1022                  * If there is an emergency call already going on,
1023                  * ignore session creation attempt
1024                  */
1025                 err = -EBUSY;
1026                 goto err;
1027         }
1028
1029         dbus_message_iter_init(msg, &iter);
1030         dbus_message_iter_recurse(&iter, &array);
1031
1032         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1033                 DBusMessageIter entry, value;
1034                 const char *key, *val;
1035
1036                 dbus_message_iter_recurse(&array, &entry);
1037                 dbus_message_iter_get_basic(&entry, &key);
1038
1039                 dbus_message_iter_next(&entry);
1040                 dbus_message_iter_recurse(&entry, &value);
1041
1042                 switch (dbus_message_iter_get_arg_type(&value)) {
1043                 case DBUS_TYPE_ARRAY:
1044                         if (g_str_equal(key, "AllowedBearers") == TRUE) {
1045                                 allowed_bearers =
1046                                         session_parse_allowed_bearers(&value);
1047                         } else {
1048                                 return -EINVAL;
1049                         }
1050                         break;
1051                 case DBUS_TYPE_BOOLEAN:
1052                         if (g_str_equal(key, "Priority") == TRUE) {
1053                                 dbus_message_iter_get_basic(&value,
1054                                                         &priority);
1055                         } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1056                                 dbus_message_iter_get_basic(&value,
1057                                                         &avoid_handover);
1058                         } else if (g_str_equal(key, "StayConnected") == TRUE) {
1059                                 dbus_message_iter_get_basic(&value,
1060                                                         &stay_connected);
1061                         } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1062                                 dbus_message_iter_get_basic(&value,
1063                                                         &ecall);
1064                         } else {
1065                                 return -EINVAL;
1066                         }
1067                         break;
1068                 case DBUS_TYPE_UINT32:
1069                         if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1070                                 dbus_message_iter_get_basic(&value,
1071                                                         &periodic_connect);
1072                         } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1073                                 dbus_message_iter_get_basic(&value,
1074                                                         &idle_timeout);
1075                         } else {
1076                                 return -EINVAL;
1077                         }
1078                         break;
1079                 case DBUS_TYPE_STRING:
1080                         if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1081                                 dbus_message_iter_get_basic(&value, &val);
1082                                 roaming_policy = string2roamingpolicy(val);
1083                         } else {
1084                                 return -EINVAL;
1085                         }
1086                 }
1087                 dbus_message_iter_next(&array);
1088         }
1089
1090         dbus_message_iter_next(&iter);
1091         dbus_message_iter_get_basic(&iter, &notify_path);
1092
1093         if (notify_path == NULL) {
1094                 err = -EINVAL;
1095                 goto err;
1096         }
1097
1098         session_path = g_strdup_printf("/sessions%s", notify_path);
1099         if (session_path == NULL) {
1100                 err = -ENOMEM;
1101                 goto err;
1102         }
1103
1104         session = g_hash_table_lookup(session_hash, session_path);
1105         if (session != NULL) {
1106                 err = -EEXIST;
1107                 goto err;
1108         }
1109
1110         session = g_try_new0(struct connman_session, 1);
1111         if (session == NULL) {
1112                 err = -ENOMEM;
1113                 goto err;
1114         }
1115
1116         info = &session->info;
1117         info_last = &session->info_last;
1118
1119         session->owner = g_strdup(owner);
1120         session->session_path = session_path;
1121         session->notify_path = g_strdup(notify_path);
1122         session->notify_watch =
1123                 g_dbus_add_disconnect_watch(connection, session->owner,
1124                                         owner_disconnect, session, NULL);
1125
1126         info->bearer = "";
1127         info->online = FALSE;
1128         info->priority = priority;
1129         info->avoid_handover = avoid_handover;
1130         info->stay_connected = stay_connected;
1131         info->periodic_connect = periodic_connect;
1132         info->idle_timeout = idle_timeout;
1133         info->ecall = ecall;
1134         info->roaming_policy = roaming_policy;
1135         info->service = NULL;
1136         info->marker = 0;
1137
1138         if (allowed_bearers == NULL) {
1139                 info->allowed_bearers =
1140                                 session_allowed_bearers_any();
1141
1142                 if (info->allowed_bearers == NULL) {
1143                         err = -ENOMEM;
1144                         goto err;
1145                 }
1146         } else {
1147                 info->allowed_bearers = allowed_bearers;
1148         }
1149
1150         g_hash_table_replace(session_hash, session->session_path, session);
1151
1152         DBG("add %s", session->session_path);
1153
1154         if (g_dbus_register_interface(connection, session->session_path,
1155                                         CONNMAN_SESSION_INTERFACE,
1156                                         session_methods, NULL,
1157                                         NULL, session, NULL) == FALSE) {
1158                 connman_error("Failed to register %s", session->session_path);
1159                 g_hash_table_remove(session_hash, session->session_path);
1160                 session = NULL;
1161
1162                 err = -EINVAL;
1163                 goto err;
1164         }
1165
1166         g_dbus_send_reply(connection, msg,
1167                                 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1168                                 DBUS_TYPE_INVALID);
1169
1170
1171         update_allowed_bearers(session);
1172         update_info(info);
1173         if (info->ecall == TRUE) {
1174                 ecall_session = session;
1175                 update_ecall_sessions(session);
1176         }
1177
1178         info_last->bearer = info->bearer;
1179         info_last->online = info->online;
1180         info_last->priority = info->priority;
1181         info_last->avoid_handover = info->avoid_handover;
1182         info_last->stay_connected = info->stay_connected;
1183         info_last->periodic_connect = info->periodic_connect;
1184         info_last->idle_timeout = info->idle_timeout;
1185         info_last->ecall = info->ecall;
1186         info_last->roaming_policy = info->roaming_policy;
1187         info_last->service = info->service;
1188         info_last->marker = info->marker;
1189         info_last->allowed_bearers = info->allowed_bearers;
1190         update_info(info_last);
1191
1192         session->info_dirty = TRUE;
1193         session->append_all = TRUE;
1194
1195         g_timeout_add_seconds(0, session_cb, session);
1196
1197         return 0;
1198
1199 err:
1200         connman_error("Failed to create session");
1201         g_free(session_path);
1202
1203         g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1204         g_slist_free(allowed_bearers);
1205
1206         return err;
1207 }
1208
1209 int __connman_session_destroy(DBusMessage *msg)
1210 {
1211         const char *owner, *session_path;
1212         struct connman_session *session;
1213
1214         owner = dbus_message_get_sender(msg);
1215
1216         DBG("owner %s", owner);
1217
1218         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1219                                                         DBUS_TYPE_INVALID);
1220         if (session_path == NULL)
1221                 return -EINVAL;
1222
1223         session = g_hash_table_lookup(session_hash, session_path);
1224         if (session == NULL)
1225                 return -EINVAL;
1226
1227         if (g_strcmp0(owner, session->owner) != 0)
1228                 return -EACCES;
1229
1230         session_disconnect(session);
1231
1232         return 0;
1233 }
1234
1235 connman_bool_t __connman_session_mode()
1236 {
1237         return sessionmode;
1238 }
1239
1240 void __connman_session_set_mode(connman_bool_t enable)
1241 {
1242         DBG("enable %d", enable);
1243
1244         if (sessionmode == enable)
1245                 return;
1246
1247         sessionmode = enable;
1248
1249         if (sessionmode == TRUE)
1250                 __connman_service_disconnect_all();
1251 }
1252
1253 static void service_add(struct connman_service *service)
1254 {
1255         GHashTableIter iter;
1256         gpointer key, value;
1257         struct connman_session *session;
1258
1259         DBG("service %p", service);
1260
1261         g_hash_table_iter_init(&iter, session_hash);
1262
1263         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1264                 session = value;
1265
1266                 if (service_match(session, service) == FALSE)
1267                         continue;
1268
1269                 g_sequence_insert_sorted(session->service_list, service,
1270                                                 sort_services, session);
1271
1272                 session_cb(session);
1273         }
1274 }
1275
1276 static connman_bool_t service_remove_from_session(
1277                                         struct connman_session *session,
1278                                         struct connman_service *service)
1279 {
1280         GSequenceIter *iter;
1281
1282         iter = g_sequence_get_begin_iter(session->service_list);
1283
1284         while (g_sequence_iter_is_end(iter) == FALSE) {
1285                 struct connman_service *service_iter = g_sequence_get(iter);
1286
1287                 if (service_iter == service) {
1288                         g_sequence_remove(iter);
1289
1290                         return TRUE;
1291                 }
1292
1293                 iter = g_sequence_iter_next(iter);
1294         }
1295
1296         return FALSE;
1297 }
1298
1299 static void service_remove(struct connman_service *service)
1300 {
1301
1302         GHashTableIter iter;
1303         gpointer key, value;
1304         struct connman_session *session;
1305         struct session_info *info;
1306
1307         DBG("service %p", service);
1308
1309         g_hash_table_iter_init(&iter, session_hash);
1310
1311         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1312                 session = value;
1313                 info = &session->info;
1314
1315                 if (service_remove_from_session(session, service) == FALSE)
1316                         continue;
1317
1318                 info->service = NULL;
1319                 session_cb(session);
1320         }
1321 }
1322
1323 static void service_state_changed(struct connman_service *service,
1324                                         enum connman_service_state state)
1325 {
1326         GHashTableIter iter;
1327         gpointer key, value;
1328         struct connman_session *session;
1329         struct session_info *info;
1330         connman_bool_t online;
1331
1332         DBG("service %p state %d", service, state);
1333
1334         g_hash_table_iter_init(&iter, session_hash);
1335
1336         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1337                 session = value;
1338                 info = &session->info;
1339
1340                 if (info->service == service) {
1341                         online = __connman_service_is_connected(service);
1342                         if (info->online == online)
1343                                 continue;
1344
1345                         info->online = online;
1346                         session->info_dirty = TRUE;
1347                         session_cb(session);
1348                 }
1349         }
1350 }
1351
1352 static void ipconfig_changed(struct connman_service *service,
1353                                 struct connman_ipconfig *ipconfig)
1354 {
1355         GHashTableIter iter;
1356         gpointer key, value;
1357         struct connman_session *session;
1358         struct session_info *info;
1359         enum connman_ipconfig_type type;
1360
1361         DBG("service %p ipconfig %p", service, ipconfig);
1362
1363         type = __connman_ipconfig_get_config_type(ipconfig);
1364
1365         g_hash_table_iter_init(&iter, session_hash);
1366
1367         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1368                 session = value;
1369                 info = &session->info;
1370
1371                 if (info->service == service) {
1372                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1373                                 ipconfig_ipv4_changed(session);
1374                         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1375                                 ipconfig_ipv6_changed(session);
1376                 }
1377         }
1378 }
1379
1380 static struct connman_notifier session_notifier = {
1381         .name                   = "session",
1382         .service_add            = service_add,
1383         .service_remove         = service_remove,
1384         .service_state_changed  = service_state_changed,
1385         .ipconfig_changed       = ipconfig_changed,
1386 };
1387
1388 int __connman_session_init(void)
1389 {
1390         int err;
1391
1392         DBG("");
1393
1394         connection = connman_dbus_get_connection();
1395         if (connection == NULL)
1396                 return -1;
1397
1398         err = connman_notifier_register(&session_notifier);
1399         if (err < 0) {
1400                 dbus_connection_unref(connection);
1401                 return err;
1402         }
1403
1404         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1405                                                 NULL, cleanup_session);
1406
1407         sessionmode = FALSE;
1408         return 0;
1409 }
1410
1411 void __connman_session_cleanup(void)
1412 {
1413         DBG("");
1414
1415         if (connection == NULL)
1416                 return;
1417
1418         connman_notifier_unregister(&session_notifier);
1419
1420         g_hash_table_foreach(session_hash, release_session, NULL);
1421         g_hash_table_destroy(session_hash);
1422
1423         dbus_connection_unref(connection);
1424 }