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