session: Split service_remove_from_session()
[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 GSequenceIter *lookup_service(struct connman_session *session,
454                                         struct connman_service *service)
455 {
456         GSequenceIter *iter;
457
458         if (service == NULL)
459                 return NULL;
460
461         iter = g_sequence_get_begin_iter(session->service_list);
462
463         while (g_sequence_iter_is_end(iter) == FALSE) {
464                 struct connman_service *service_iter = g_sequence_get(iter);
465
466                 if (service_iter == service)
467                         return iter;
468
469                 iter = g_sequence_iter_next(iter);
470         }
471
472         return NULL;
473 }
474
475 static connman_bool_t service_type_match(struct connman_session *session,
476                                         struct connman_service *service)
477 {
478         struct session_info *info = &session->info;
479         GSList *list;
480
481         for (list = info->allowed_bearers;
482                         list != NULL; list = list->next) {
483                 struct bearer_info *info = list->data;
484                 enum connman_service_type service_type;
485
486                 if (info->match_all == TRUE)
487                         return TRUE;
488
489                 service_type = connman_service_get_type(service);
490                 if (info->service_type == service_type)
491                         return TRUE;
492         }
493
494         return FALSE;
495 }
496
497 static connman_bool_t service_match(struct connman_session *session,
498                                         struct connman_service *service)
499 {
500         if (service_type_match(session, service) == FALSE)
501                 return FALSE;
502
503         return TRUE;
504 }
505
506 static int service_type_weight(enum connman_service_type type)
507 {
508         /*
509          * The session doesn't care which service
510          * to use. Nevertheless we have to sort them
511          * according their type. The ordering is
512          *
513          * 1. Ethernet
514          * 2. Bluetooth
515          * 3. WiFi/WiMAX
516          * 4. GSM/UTMS/3G
517          */
518
519         switch (type) {
520         case CONNMAN_SERVICE_TYPE_ETHERNET:
521                 return 4;
522         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
523                 return 3;
524         case CONNMAN_SERVICE_TYPE_WIFI:
525         case CONNMAN_SERVICE_TYPE_WIMAX:
526                 return 2;
527         case CONNMAN_SERVICE_TYPE_CELLULAR:
528                 return 1;
529         case CONNMAN_SERVICE_TYPE_UNKNOWN:
530         case CONNMAN_SERVICE_TYPE_SYSTEM:
531         case CONNMAN_SERVICE_TYPE_GPS:
532         case CONNMAN_SERVICE_TYPE_VPN:
533         case CONNMAN_SERVICE_TYPE_GADGET:
534                 break;
535         }
536
537         return 0;
538 }
539
540 static gint sort_allowed_bearers(struct connman_service *service_a,
541                                         struct connman_service *service_b,
542                                         struct connman_session *session)
543 {
544         struct session_info *info = &session->info;
545         GSList *list;
546         enum connman_service_type type_a, type_b;
547         int weight_a, weight_b;
548
549         type_a = connman_service_get_type(service_a);
550         type_b = connman_service_get_type(service_b);
551
552         for (list = info->allowed_bearers;
553                         list != NULL; list = list->next) {
554                 struct bearer_info *info = list->data;
555
556                 if (info->match_all == TRUE) {
557                         if (type_a != type_b) {
558                                 weight_a = service_type_weight(type_a);
559                                 weight_b = service_type_weight(type_b);
560
561                                 if (weight_a > weight_b)
562                                         return -1;
563
564                                 if (weight_a < weight_b)
565                                         return 1;
566
567                                 return 0;
568                         }
569                 }
570
571                 if (type_a == info->service_type &&
572                                 type_b == info->service_type) {
573                         return 0;
574                 }
575
576                 if (type_a == info->service_type &&
577                                 type_b != info->service_type) {
578                         return -1;
579                 }
580
581                 if (type_a != info->service_type &&
582                                 type_b == info->service_type) {
583                         return 1;
584                 }
585         }
586
587         return 0;
588 }
589
590 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
591 {
592         struct connman_service *service_a = (void *)a;
593         struct connman_service *service_b = (void *)b;
594         struct connman_session *session = user_data;
595
596         return sort_allowed_bearers(service_a, service_b, session);
597 }
598
599 static void cleanup_session(gpointer user_data)
600 {
601         struct connman_session *session = user_data;
602         struct session_info *info = &session->info;
603
604         DBG("remove %s", session->session_path);
605
606         g_sequence_free(session->service_list);
607
608         g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
609         g_slist_free(info->allowed_bearers);
610
611         g_free(session->owner);
612         g_free(session->session_path);
613         g_free(session->notify_path);
614
615         g_free(session);
616 }
617
618 static void release_session(gpointer key, gpointer value, gpointer user_data)
619 {
620         struct connman_session *session = value;
621         DBusMessage *message;
622
623         DBG("owner %s path %s", session->owner, session->notify_path);
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         message = dbus_message_new_method_call(session->owner,
632                                                 session->notify_path,
633                                                 CONNMAN_NOTIFICATION_INTERFACE,
634                                                 "Release");
635         if (message == NULL)
636                 return;
637
638         dbus_message_set_no_reply(message, TRUE);
639
640         g_dbus_send_message(connection, message);
641 }
642
643 static int session_disconnect(struct connman_session *session)
644 {
645         DBG("session %p, %s", session, session->owner);
646
647         if (session->notify_watch > 0)
648                 g_dbus_remove_watch(connection, session->notify_watch);
649
650         g_dbus_unregister_interface(connection, session->session_path,
651                                                 CONNMAN_SESSION_INTERFACE);
652
653         g_hash_table_remove(session_hash, session->session_path);
654
655         return 0;
656 }
657
658 static void owner_disconnect(DBusConnection *conn, void *user_data)
659 {
660         struct connman_session *session = user_data;
661
662         DBG("session %p, %s died", session, session->owner);
663
664         session_disconnect(session);
665 }
666
667 static DBusMessage *destroy_session(DBusConnection *conn,
668                                         DBusMessage *msg, void *user_data)
669 {
670         struct connman_session *session = user_data;
671
672         DBG("session %p", session);
673
674         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
675 }
676
677 static gboolean session_changed_connect(gpointer user_data)
678 {
679         struct connman_session *session = user_data;
680         struct session_info *info = &session->info;
681
682         __connman_service_connect(info->service);
683
684         return FALSE;
685 }
686
687 static void update_info(struct session_info *info)
688 {
689         enum connman_service_type type;
690         int idx;
691
692         if (info->service != NULL) {
693                 type = connman_service_get_type(info->service);
694                 info->bearer = service2bearer(type);
695
696                 info->online = __connman_service_is_connected(info->service);
697                 info->name = __connman_service_get_name(info->service);
698
699                 idx = __connman_service_get_index(info->service);
700                 info->ifname = connman_inet_ifname(idx);
701                 if (info->ifname == NULL)
702                         info->ifname = "";
703         } else {
704                 info->bearer = "";
705                 info->online = FALSE;
706                 info->name = "";
707                 info->ifname = "";
708         }
709 }
710
711 static void session_changed(struct connman_session *session)
712 {
713         struct session_info *info = &session->info;
714         struct session_info *info_last = &session->info_last;
715         struct connman_service *service = NULL;
716         GSourceFunc callback = NULL;
717         GSequenceIter *iter;
718
719         /*
720          * TODO: This only a placeholder for the 'real' algorithm to
721          * play a bit around. So we are going to improve it step by step.
722          */
723
724         if (info->ecall == TRUE && session != ecall_session)
725                 goto out;
726
727         if (info->connect == FALSE) {
728                 if (info->service != NULL)
729                         __connman_service_disconnect(info->service);
730                 info->service = NULL;
731                 goto out;
732         }
733
734         iter = g_sequence_get_begin_iter(session->service_list);
735
736         while (g_sequence_iter_is_end(iter) == FALSE) {
737                 service = g_sequence_get(iter);
738
739                 if (__connman_service_is_connecting(service) == TRUE ||
740                                 __connman_service_is_connected(service) == TRUE) {
741                         break;
742                 }
743
744                 if (__connman_service_is_idle(service) == TRUE &&
745                                 info->connect == TRUE) {
746                         callback = session_changed_connect;
747                         break;
748                 }
749
750                 service = NULL;
751
752                 iter = g_sequence_iter_next(iter);
753         }
754
755         if (info->service != NULL && info->service != service) {
756                 __connman_service_disconnect(info->service);
757                 info->service = NULL;
758         }
759
760         if (service != NULL) {
761                 info->service = service;
762
763                 if (callback != NULL)
764                         callback(session);
765         }
766
767 out:
768         if (info->service != info_last->service) {
769                 update_info(info);
770                 session->info_dirty = TRUE;
771         }
772 }
773
774 static gboolean session_cb(gpointer user_data)
775 {
776         struct connman_session *session = user_data;
777
778         session_changed(session);
779         session_notify(session);
780
781         return FALSE;
782 }
783
784 static DBusMessage *connect_session(DBusConnection *conn,
785                                         DBusMessage *msg, void *user_data)
786 {
787         struct connman_session *session = user_data;
788         struct session_info *info = &session->info;
789
790         DBG("session %p", session);
791
792         info->connect = TRUE;
793
794         if (ecall_session != NULL && ecall_session != session)
795                 return __connman_error_failed(msg, EBUSY);
796
797         session->info_dirty = TRUE;
798
799         g_timeout_add_seconds(0, session_cb, session);
800
801         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
802 }
803
804 static DBusMessage *disconnect_session(DBusConnection *conn,
805                                         DBusMessage *msg, void *user_data)
806 {
807         struct connman_session *session = user_data;
808         struct session_info *info = &session->info;
809
810         DBG("session %p", session);
811
812         info->connect = FALSE;
813
814         if (ecall_session != NULL && ecall_session != session)
815                 return __connman_error_failed(msg, EBUSY);
816
817         session->info_dirty = TRUE;
818
819         g_timeout_add_seconds(0, session_cb, session);
820
821         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
822 }
823
824 static void print_name(gpointer data, gpointer user_data)
825 {
826         struct connman_service *service = data;
827
828         DBG("service %p type %s name %s", service,
829                 service2bearer(connman_service_get_type(service)),
830                 __connman_service_get_name(service));
831 }
832
833 static void update_allowed_bearers(struct connman_session *session)
834 {
835         if (session->service_list != NULL)
836                 g_sequence_free(session->service_list);
837
838         session->service_list = __connman_service_get_list(session,
839                                                                 service_match);
840         g_sequence_sort(session->service_list, sort_services, session);
841         g_sequence_foreach(session->service_list, print_name, NULL);
842
843         session->info_dirty = TRUE;
844 }
845
846 static void update_ecall_sessions(struct connman_session *session)
847 {
848         struct session_info *info = &session->info;
849         struct connman_session *session_iter;
850         GHashTableIter iter;
851         gpointer key, value;
852
853         g_hash_table_iter_init(&iter, session_hash);
854
855         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
856                 session_iter = value;
857
858                 if (session_iter == session)
859                         continue;
860
861                 session_iter->info.ecall = info->ecall;
862                 session_iter->info_dirty = TRUE;
863
864                 g_timeout_add_seconds(0, session_cb, session_iter);
865         }
866 }
867
868 static void update_ecall(struct connman_session *session)
869 {
870         struct session_info *info = &session->info;
871         struct session_info *info_last = &session->info_last;
872
873         DBG("ecall_session %p ecall %d -> %d", ecall_session,
874                 info_last->ecall, info->ecall);
875
876         if (ecall_session == NULL) {
877                 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
878                         goto err;
879
880                 ecall_session = session;
881         } else if (ecall_session == session) {
882                 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
883                         goto err;
884
885                 ecall_session = NULL;
886         } else {
887                 goto err;
888         }
889
890         update_ecall_sessions(session);
891
892         session->info_dirty = TRUE;
893         return;
894
895 err:
896         /* not a valid transition */
897         info->ecall = info_last->ecall;
898 }
899
900 static DBusMessage *change_session(DBusConnection *conn,
901                                         DBusMessage *msg, void *user_data)
902 {
903         struct connman_session *session = user_data;
904         struct session_info *info = &session->info;
905         struct session_info *info_last = &session->info_last;
906         DBusMessageIter iter, value;
907         const char *name;
908         GSList *allowed_bearers;
909
910         DBG("session %p", session);
911         if (dbus_message_iter_init(msg, &iter) == FALSE)
912                 return __connman_error_invalid_arguments(msg);
913
914         dbus_message_iter_get_basic(&iter, &name);
915         dbus_message_iter_next(&iter);
916         dbus_message_iter_recurse(&iter, &value);
917
918         switch (dbus_message_iter_get_arg_type(&value)) {
919         case DBUS_TYPE_ARRAY:
920                 if (g_str_equal(name, "AllowedBearers") == TRUE) {
921                         allowed_bearers = session_parse_allowed_bearers(&value);
922
923                         g_slist_foreach(info->allowed_bearers,
924                                         cleanup_bearer_info, NULL);
925                         g_slist_free(info->allowed_bearers);
926
927                         if (allowed_bearers == NULL) {
928                                 allowed_bearers = session_allowed_bearers_any();
929
930                                 if (allowed_bearers == NULL)
931                                         return __connman_error_failed(msg, ENOMEM);
932                         }
933
934                         info->allowed_bearers = allowed_bearers;
935
936                         update_allowed_bearers(session);
937                 } else {
938                         goto err;
939                 }
940                 break;
941         case DBUS_TYPE_BOOLEAN:
942                 if (g_str_equal(name, "Priority") == TRUE) {
943                         dbus_message_iter_get_basic(&value,
944                                         &info->priority);
945
946                         if (info_last->priority != info->priority)
947                                 session->info_dirty = TRUE;
948                 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
949                         dbus_message_iter_get_basic(&value,
950                                         &info->avoid_handover);
951
952                         if (info_last->avoid_handover != info->avoid_handover)
953                                 session->info_dirty = TRUE;
954                         } else if (g_str_equal(name, "StayConnected") == TRUE) {
955                         dbus_message_iter_get_basic(&value,
956                                         &info->stay_connected);
957
958                         if (info_last->stay_connected != info->stay_connected)
959                                 session->info_dirty = TRUE;
960                 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
961                         dbus_message_iter_get_basic(&value,
962                                         &info->ecall);
963
964                         update_ecall(session);
965                 } else {
966                         goto err;
967                 }
968                 break;
969         case DBUS_TYPE_UINT32:
970                 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
971                         dbus_message_iter_get_basic(&value,
972                                         &info->periodic_connect);
973
974                         if (info_last->periodic_connect != info->periodic_connect)
975                                 session->info_dirty = TRUE;
976                 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
977                         dbus_message_iter_get_basic(&value,
978                                         &info->idle_timeout);
979
980                         if (info_last->idle_timeout != info->idle_timeout)
981                                 session->info_dirty = TRUE;
982                 } else {
983                         goto err;
984                 }
985                 break;
986         case DBUS_TYPE_STRING:
987                 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
988                         const char *val;
989                         dbus_message_iter_get_basic(&value, &val);
990                         info->roaming_policy =
991                                         string2roamingpolicy(val);
992
993                         if (info_last->roaming_policy != info->roaming_policy)
994                                 session->info_dirty = TRUE;
995                 } else {
996                         goto err;
997                 }
998                 break;
999         default:
1000                 goto err;
1001         }
1002
1003         if (session->info_dirty == TRUE)
1004                 session_cb(session);
1005
1006         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1007
1008 err:
1009         return __connman_error_invalid_arguments(msg);
1010 }
1011
1012 static GDBusMethodTable session_methods[] = {
1013         { "Destroy",    "",   "", destroy_session    },
1014         { "Connect",    "",   "", connect_session    },
1015         { "Disconnect", "",   "", disconnect_session },
1016         { "Change",     "sv", "", change_session     },
1017         { },
1018 };
1019
1020 int __connman_session_create(DBusMessage *msg)
1021 {
1022         const char *owner, *notify_path;
1023         char *session_path = NULL;
1024         DBusMessageIter iter, array;
1025         struct connman_session *session;
1026         struct session_info *info, *info_last;
1027
1028         connman_bool_t priority = FALSE, avoid_handover = FALSE;
1029         connman_bool_t stay_connected = FALSE, ecall = FALSE;
1030         enum connman_session_roaming_policy roaming_policy =
1031                                 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1032         GSList *allowed_bearers = NULL;
1033         unsigned int periodic_connect = 0;
1034         unsigned int idle_timeout = 0;
1035
1036         int err;
1037
1038         owner = dbus_message_get_sender(msg);
1039
1040         DBG("owner %s", owner);
1041
1042         if (ecall_session != NULL) {
1043                 /*
1044                  * If there is an emergency call already going on,
1045                  * ignore session creation attempt
1046                  */
1047                 err = -EBUSY;
1048                 goto err;
1049         }
1050
1051         dbus_message_iter_init(msg, &iter);
1052         dbus_message_iter_recurse(&iter, &array);
1053
1054         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1055                 DBusMessageIter entry, value;
1056                 const char *key, *val;
1057
1058                 dbus_message_iter_recurse(&array, &entry);
1059                 dbus_message_iter_get_basic(&entry, &key);
1060
1061                 dbus_message_iter_next(&entry);
1062                 dbus_message_iter_recurse(&entry, &value);
1063
1064                 switch (dbus_message_iter_get_arg_type(&value)) {
1065                 case DBUS_TYPE_ARRAY:
1066                         if (g_str_equal(key, "AllowedBearers") == TRUE) {
1067                                 allowed_bearers =
1068                                         session_parse_allowed_bearers(&value);
1069                         } else {
1070                                 return -EINVAL;
1071                         }
1072                         break;
1073                 case DBUS_TYPE_BOOLEAN:
1074                         if (g_str_equal(key, "Priority") == TRUE) {
1075                                 dbus_message_iter_get_basic(&value,
1076                                                         &priority);
1077                         } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1078                                 dbus_message_iter_get_basic(&value,
1079                                                         &avoid_handover);
1080                         } else if (g_str_equal(key, "StayConnected") == TRUE) {
1081                                 dbus_message_iter_get_basic(&value,
1082                                                         &stay_connected);
1083                         } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1084                                 dbus_message_iter_get_basic(&value,
1085                                                         &ecall);
1086                         } else {
1087                                 return -EINVAL;
1088                         }
1089                         break;
1090                 case DBUS_TYPE_UINT32:
1091                         if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1092                                 dbus_message_iter_get_basic(&value,
1093                                                         &periodic_connect);
1094                         } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1095                                 dbus_message_iter_get_basic(&value,
1096                                                         &idle_timeout);
1097                         } else {
1098                                 return -EINVAL;
1099                         }
1100                         break;
1101                 case DBUS_TYPE_STRING:
1102                         if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1103                                 dbus_message_iter_get_basic(&value, &val);
1104                                 roaming_policy = string2roamingpolicy(val);
1105                         } else {
1106                                 return -EINVAL;
1107                         }
1108                 }
1109                 dbus_message_iter_next(&array);
1110         }
1111
1112         dbus_message_iter_next(&iter);
1113         dbus_message_iter_get_basic(&iter, &notify_path);
1114
1115         if (notify_path == NULL) {
1116                 err = -EINVAL;
1117                 goto err;
1118         }
1119
1120         session_path = g_strdup_printf("/sessions%s", notify_path);
1121         if (session_path == NULL) {
1122                 err = -ENOMEM;
1123                 goto err;
1124         }
1125
1126         session = g_hash_table_lookup(session_hash, session_path);
1127         if (session != NULL) {
1128                 err = -EEXIST;
1129                 goto err;
1130         }
1131
1132         session = g_try_new0(struct connman_session, 1);
1133         if (session == NULL) {
1134                 err = -ENOMEM;
1135                 goto err;
1136         }
1137
1138         info = &session->info;
1139         info_last = &session->info_last;
1140
1141         session->owner = g_strdup(owner);
1142         session->session_path = session_path;
1143         session->notify_path = g_strdup(notify_path);
1144         session->notify_watch =
1145                 g_dbus_add_disconnect_watch(connection, session->owner,
1146                                         owner_disconnect, session, NULL);
1147
1148         info->bearer = "";
1149         info->online = FALSE;
1150         info->priority = priority;
1151         info->avoid_handover = avoid_handover;
1152         info->stay_connected = stay_connected;
1153         info->periodic_connect = periodic_connect;
1154         info->idle_timeout = idle_timeout;
1155         info->ecall = ecall;
1156         info->roaming_policy = roaming_policy;
1157         info->service = NULL;
1158         info->marker = 0;
1159
1160         if (allowed_bearers == NULL) {
1161                 info->allowed_bearers =
1162                                 session_allowed_bearers_any();
1163
1164                 if (info->allowed_bearers == NULL) {
1165                         err = -ENOMEM;
1166                         goto err;
1167                 }
1168         } else {
1169                 info->allowed_bearers = allowed_bearers;
1170         }
1171
1172         g_hash_table_replace(session_hash, session->session_path, session);
1173
1174         DBG("add %s", session->session_path);
1175
1176         if (g_dbus_register_interface(connection, session->session_path,
1177                                         CONNMAN_SESSION_INTERFACE,
1178                                         session_methods, NULL,
1179                                         NULL, session, NULL) == FALSE) {
1180                 connman_error("Failed to register %s", session->session_path);
1181                 g_hash_table_remove(session_hash, session->session_path);
1182                 session = NULL;
1183
1184                 err = -EINVAL;
1185                 goto err;
1186         }
1187
1188         g_dbus_send_reply(connection, msg,
1189                                 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1190                                 DBUS_TYPE_INVALID);
1191
1192
1193         update_allowed_bearers(session);
1194         update_info(info);
1195         if (info->ecall == TRUE) {
1196                 ecall_session = session;
1197                 update_ecall_sessions(session);
1198         }
1199
1200         info_last->bearer = info->bearer;
1201         info_last->online = info->online;
1202         info_last->priority = 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         update_info(info_last);
1213
1214         session->info_dirty = TRUE;
1215         session->append_all = TRUE;
1216
1217         g_timeout_add_seconds(0, session_cb, session);
1218
1219         return 0;
1220
1221 err:
1222         connman_error("Failed to create session");
1223         g_free(session_path);
1224
1225         g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1226         g_slist_free(allowed_bearers);
1227
1228         return err;
1229 }
1230
1231 int __connman_session_destroy(DBusMessage *msg)
1232 {
1233         const char *owner, *session_path;
1234         struct connman_session *session;
1235
1236         owner = dbus_message_get_sender(msg);
1237
1238         DBG("owner %s", owner);
1239
1240         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1241                                                         DBUS_TYPE_INVALID);
1242         if (session_path == NULL)
1243                 return -EINVAL;
1244
1245         session = g_hash_table_lookup(session_hash, session_path);
1246         if (session == NULL)
1247                 return -EINVAL;
1248
1249         if (g_strcmp0(owner, session->owner) != 0)
1250                 return -EACCES;
1251
1252         session_disconnect(session);
1253
1254         return 0;
1255 }
1256
1257 connman_bool_t __connman_session_mode()
1258 {
1259         return sessionmode;
1260 }
1261
1262 void __connman_session_set_mode(connman_bool_t enable)
1263 {
1264         DBG("enable %d", enable);
1265
1266         if (sessionmode == enable)
1267                 return;
1268
1269         sessionmode = enable;
1270
1271         if (sessionmode == TRUE)
1272                 __connman_service_disconnect_all();
1273 }
1274
1275 static void service_add(struct connman_service *service)
1276 {
1277         GHashTableIter iter;
1278         gpointer key, value;
1279         struct connman_session *session;
1280
1281         DBG("service %p", service);
1282
1283         g_hash_table_iter_init(&iter, session_hash);
1284
1285         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1286                 session = value;
1287
1288                 if (service_match(session, service) == FALSE)
1289                         continue;
1290
1291                 g_sequence_insert_sorted(session->service_list, service,
1292                                                 sort_services, session);
1293
1294                 session_cb(session);
1295         }
1296 }
1297
1298 static int service_remove_from_session(struct connman_session *session,
1299                                         struct connman_service *service)
1300 {
1301
1302         GSequenceIter *iter;
1303
1304         iter = lookup_service(session, service);
1305         if (iter == NULL)
1306                 return -ENOENT;
1307
1308         session->info.online = FALSE;
1309         g_sequence_remove(iter);
1310
1311         return 0;
1312 }
1313
1314 static void service_remove(struct connman_service *service)
1315 {
1316
1317         GHashTableIter iter;
1318         gpointer key, value;
1319         struct connman_session *session;
1320         struct session_info *info;
1321
1322         DBG("service %p", service);
1323
1324         g_hash_table_iter_init(&iter, session_hash);
1325
1326         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1327                 session = value;
1328                 info = &session->info;
1329
1330                 if (service_remove_from_session(session, service) != 0)
1331                         continue;
1332
1333                 info->service = NULL;
1334                 session_cb(session);
1335         }
1336 }
1337
1338 static void service_state_changed(struct connman_service *service,
1339                                         enum connman_service_state state)
1340 {
1341         GHashTableIter iter;
1342         gpointer key, value;
1343         struct connman_session *session;
1344         struct session_info *info;
1345         connman_bool_t online;
1346
1347         DBG("service %p state %d", service, state);
1348
1349         g_hash_table_iter_init(&iter, session_hash);
1350
1351         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1352                 session = value;
1353                 info = &session->info;
1354
1355                 if (info->service == service) {
1356                         online = __connman_service_is_connected(service);
1357                         if (info->online == online)
1358                                 continue;
1359
1360                         info->online = online;
1361                         session->info_dirty = TRUE;
1362                         session_cb(session);
1363                 }
1364         }
1365 }
1366
1367 static void ipconfig_changed(struct connman_service *service,
1368                                 struct connman_ipconfig *ipconfig)
1369 {
1370         GHashTableIter iter;
1371         gpointer key, value;
1372         struct connman_session *session;
1373         struct session_info *info;
1374         enum connman_ipconfig_type type;
1375
1376         DBG("service %p ipconfig %p", service, ipconfig);
1377
1378         type = __connman_ipconfig_get_config_type(ipconfig);
1379
1380         g_hash_table_iter_init(&iter, session_hash);
1381
1382         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1383                 session = value;
1384                 info = &session->info;
1385
1386                 if (info->service == service) {
1387                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1388                                 ipconfig_ipv4_changed(session);
1389                         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1390                                 ipconfig_ipv6_changed(session);
1391                 }
1392         }
1393 }
1394
1395 static struct connman_notifier session_notifier = {
1396         .name                   = "session",
1397         .service_add            = service_add,
1398         .service_remove         = service_remove,
1399         .service_state_changed  = service_state_changed,
1400         .ipconfig_changed       = ipconfig_changed,
1401 };
1402
1403 int __connman_session_init(void)
1404 {
1405         int err;
1406
1407         DBG("");
1408
1409         connection = connman_dbus_get_connection();
1410         if (connection == NULL)
1411                 return -1;
1412
1413         err = connman_notifier_register(&session_notifier);
1414         if (err < 0) {
1415                 dbus_connection_unref(connection);
1416                 return err;
1417         }
1418
1419         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1420                                                 NULL, cleanup_session);
1421
1422         sessionmode = FALSE;
1423         return 0;
1424 }
1425
1426 void __connman_session_cleanup(void)
1427 {
1428         DBG("");
1429
1430         if (connection == NULL)
1431                 return;
1432
1433         connman_notifier_unregister(&session_notifier);
1434
1435         g_hash_table_foreach(session_hash, release_session, NULL);
1436         g_hash_table_destroy(session_hash);
1437
1438         dbus_connection_unref(connection);
1439 }