session: Factor out select code from session_changed
[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 void update_info(struct session_info *info)
678 {
679         enum connman_service_type type;
680         int idx;
681
682         if (info->service != NULL) {
683                 type = connman_service_get_type(info->service);
684                 info->bearer = service2bearer(type);
685
686                 info->online = __connman_service_is_connected(info->service);
687                 info->name = __connman_service_get_name(info->service);
688
689                 idx = __connman_service_get_index(info->service);
690                 info->ifname = connman_inet_ifname(idx);
691                 if (info->ifname == NULL)
692                         info->ifname = "";
693         } else {
694                 info->bearer = "";
695                 info->online = FALSE;
696                 info->name = "";
697                 info->ifname = "";
698         }
699 }
700
701 static void select_and_connect(struct connman_session *session,
702                                 connman_bool_t do_connect)
703 {
704         struct session_info *info = &session->info;
705         struct connman_service *service = NULL;
706         GSequenceIter *iter;
707
708         DBG("session %p connect %d", session, do_connect);
709
710         iter = g_sequence_get_begin_iter(session->service_list);
711
712         while (g_sequence_iter_is_end(iter) == FALSE) {
713                 service = g_sequence_get(iter);
714
715                 if (__connman_service_is_connecting(service) == TRUE ||
716                                 __connman_service_is_connected(service) == TRUE) {
717                         break;
718                 }
719
720                 if (__connman_service_is_idle(service) == TRUE &&
721                                 do_connect == TRUE) {
722                         break;
723                 }
724
725                 service = NULL;
726
727                 iter = g_sequence_iter_next(iter);
728         }
729
730         if (info->service != NULL && info->service != service) {
731                 __connman_service_disconnect(info->service);
732                 info->service = NULL;
733         }
734
735         if (service != NULL) {
736                 info->service = service;
737
738                 if (do_connect == TRUE)
739                         __connman_service_connect(info->service);
740         }
741 }
742
743 static void session_changed(struct connman_session *session)
744 {
745         struct session_info *info = &session->info;
746         struct session_info *info_last = &session->info_last;
747
748         /*
749          * TODO: This only a placeholder for the 'real' algorithm to
750          * play a bit around. So we are going to improve it step by step.
751          */
752
753         if (info->ecall == TRUE && session != ecall_session)
754                 goto out;
755
756         if (info->connect == FALSE) {
757                 if (info->service != NULL)
758                         __connman_service_disconnect(info->service);
759                 info->service = NULL;
760         } else {
761                 select_and_connect(session, TRUE);
762         }
763
764 out:
765         if (info->service != info_last->service) {
766                 update_info(info);
767                 session->info_dirty = TRUE;
768         }
769 }
770
771 static gboolean session_cb(gpointer user_data)
772 {
773         struct connman_session *session = user_data;
774
775         session_changed(session);
776         session_notify(session);
777
778         return FALSE;
779 }
780
781 static DBusMessage *connect_session(DBusConnection *conn,
782                                         DBusMessage *msg, void *user_data)
783 {
784         struct connman_session *session = user_data;
785         struct session_info *info = &session->info;
786
787         DBG("session %p", session);
788
789         info->connect = TRUE;
790
791         if (ecall_session != NULL && ecall_session != session)
792                 return __connman_error_failed(msg, EBUSY);
793
794         session->info_dirty = TRUE;
795
796         g_timeout_add_seconds(0, session_cb, session);
797
798         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
799 }
800
801 static DBusMessage *disconnect_session(DBusConnection *conn,
802                                         DBusMessage *msg, void *user_data)
803 {
804         struct connman_session *session = user_data;
805         struct session_info *info = &session->info;
806
807         DBG("session %p", session);
808
809         info->connect = FALSE;
810
811         if (ecall_session != NULL && ecall_session != session)
812                 return __connman_error_failed(msg, EBUSY);
813
814         session->info_dirty = TRUE;
815
816         g_timeout_add_seconds(0, session_cb, session);
817
818         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
819 }
820
821 static void print_name(gpointer data, gpointer user_data)
822 {
823         struct connman_service *service = data;
824
825         DBG("service %p type %s name %s", service,
826                 service2bearer(connman_service_get_type(service)),
827                 __connman_service_get_name(service));
828 }
829
830 static void update_allowed_bearers(struct connman_session *session)
831 {
832         if (session->service_list != NULL)
833                 g_sequence_free(session->service_list);
834
835         session->service_list = __connman_service_get_list(session,
836                                                                 service_match);
837         g_sequence_sort(session->service_list, sort_services, session);
838         g_sequence_foreach(session->service_list, print_name, NULL);
839
840         session->info_dirty = TRUE;
841 }
842
843 static void update_ecall_sessions(struct connman_session *session)
844 {
845         struct session_info *info = &session->info;
846         struct connman_session *session_iter;
847         GHashTableIter iter;
848         gpointer key, value;
849
850         g_hash_table_iter_init(&iter, session_hash);
851
852         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
853                 session_iter = value;
854
855                 if (session_iter == session)
856                         continue;
857
858                 session_iter->info.ecall = info->ecall;
859                 session_iter->info_dirty = TRUE;
860
861                 g_timeout_add_seconds(0, session_cb, session_iter);
862         }
863 }
864
865 static void update_ecall(struct connman_session *session)
866 {
867         struct session_info *info = &session->info;
868         struct session_info *info_last = &session->info_last;
869
870         DBG("ecall_session %p ecall %d -> %d", ecall_session,
871                 info_last->ecall, info->ecall);
872
873         if (ecall_session == NULL) {
874                 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
875                         goto err;
876
877                 ecall_session = session;
878         } else if (ecall_session == session) {
879                 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
880                         goto err;
881
882                 ecall_session = NULL;
883         } else {
884                 goto err;
885         }
886
887         update_ecall_sessions(session);
888
889         session->info_dirty = TRUE;
890         return;
891
892 err:
893         /* not a valid transition */
894         info->ecall = info_last->ecall;
895 }
896
897 static DBusMessage *change_session(DBusConnection *conn,
898                                         DBusMessage *msg, void *user_data)
899 {
900         struct connman_session *session = user_data;
901         struct session_info *info = &session->info;
902         struct session_info *info_last = &session->info_last;
903         DBusMessageIter iter, value;
904         const char *name;
905         GSList *allowed_bearers;
906
907         DBG("session %p", session);
908         if (dbus_message_iter_init(msg, &iter) == FALSE)
909                 return __connman_error_invalid_arguments(msg);
910
911         dbus_message_iter_get_basic(&iter, &name);
912         dbus_message_iter_next(&iter);
913         dbus_message_iter_recurse(&iter, &value);
914
915         switch (dbus_message_iter_get_arg_type(&value)) {
916         case DBUS_TYPE_ARRAY:
917                 if (g_str_equal(name, "AllowedBearers") == TRUE) {
918                         allowed_bearers = session_parse_allowed_bearers(&value);
919
920                         g_slist_foreach(info->allowed_bearers,
921                                         cleanup_bearer_info, NULL);
922                         g_slist_free(info->allowed_bearers);
923
924                         if (allowed_bearers == NULL) {
925                                 allowed_bearers = session_allowed_bearers_any();
926
927                                 if (allowed_bearers == NULL)
928                                         return __connman_error_failed(msg, ENOMEM);
929                         }
930
931                         info->allowed_bearers = allowed_bearers;
932
933                         update_allowed_bearers(session);
934                 } else {
935                         goto err;
936                 }
937                 break;
938         case DBUS_TYPE_BOOLEAN:
939                 if (g_str_equal(name, "Priority") == TRUE) {
940                         dbus_message_iter_get_basic(&value,
941                                         &info->priority);
942
943                         if (info_last->priority != info->priority)
944                                 session->info_dirty = TRUE;
945                 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
946                         dbus_message_iter_get_basic(&value,
947                                         &info->avoid_handover);
948
949                         if (info_last->avoid_handover != info->avoid_handover)
950                                 session->info_dirty = TRUE;
951                         } else if (g_str_equal(name, "StayConnected") == TRUE) {
952                         dbus_message_iter_get_basic(&value,
953                                         &info->stay_connected);
954
955                         if (info_last->stay_connected != info->stay_connected)
956                                 session->info_dirty = TRUE;
957                 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
958                         dbus_message_iter_get_basic(&value,
959                                         &info->ecall);
960
961                         update_ecall(session);
962                 } else {
963                         goto err;
964                 }
965                 break;
966         case DBUS_TYPE_UINT32:
967                 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
968                         dbus_message_iter_get_basic(&value,
969                                         &info->periodic_connect);
970
971                         if (info_last->periodic_connect != info->periodic_connect)
972                                 session->info_dirty = TRUE;
973                 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
974                         dbus_message_iter_get_basic(&value,
975                                         &info->idle_timeout);
976
977                         if (info_last->idle_timeout != info->idle_timeout)
978                                 session->info_dirty = TRUE;
979                 } else {
980                         goto err;
981                 }
982                 break;
983         case DBUS_TYPE_STRING:
984                 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
985                         const char *val;
986                         dbus_message_iter_get_basic(&value, &val);
987                         info->roaming_policy =
988                                         string2roamingpolicy(val);
989
990                         if (info_last->roaming_policy != info->roaming_policy)
991                                 session->info_dirty = TRUE;
992                 } else {
993                         goto err;
994                 }
995                 break;
996         default:
997                 goto err;
998         }
999
1000         if (session->info_dirty == TRUE)
1001                 session_cb(session);
1002
1003         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1004
1005 err:
1006         return __connman_error_invalid_arguments(msg);
1007 }
1008
1009 static GDBusMethodTable session_methods[] = {
1010         { "Destroy",    "",   "", destroy_session    },
1011         { "Connect",    "",   "", connect_session    },
1012         { "Disconnect", "",   "", disconnect_session },
1013         { "Change",     "sv", "", change_session     },
1014         { },
1015 };
1016
1017 int __connman_session_create(DBusMessage *msg)
1018 {
1019         const char *owner, *notify_path;
1020         char *session_path = NULL;
1021         DBusMessageIter iter, array;
1022         struct connman_session *session;
1023         struct session_info *info, *info_last;
1024
1025         connman_bool_t priority = FALSE, avoid_handover = FALSE;
1026         connman_bool_t stay_connected = FALSE, ecall = FALSE;
1027         enum connman_session_roaming_policy roaming_policy =
1028                                 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1029         GSList *allowed_bearers = NULL;
1030         unsigned int periodic_connect = 0;
1031         unsigned int idle_timeout = 0;
1032
1033         int err;
1034
1035         owner = dbus_message_get_sender(msg);
1036
1037         DBG("owner %s", owner);
1038
1039         if (ecall_session != NULL) {
1040                 /*
1041                  * If there is an emergency call already going on,
1042                  * ignore session creation attempt
1043                  */
1044                 err = -EBUSY;
1045                 goto err;
1046         }
1047
1048         dbus_message_iter_init(msg, &iter);
1049         dbus_message_iter_recurse(&iter, &array);
1050
1051         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1052                 DBusMessageIter entry, value;
1053                 const char *key, *val;
1054
1055                 dbus_message_iter_recurse(&array, &entry);
1056                 dbus_message_iter_get_basic(&entry, &key);
1057
1058                 dbus_message_iter_next(&entry);
1059                 dbus_message_iter_recurse(&entry, &value);
1060
1061                 switch (dbus_message_iter_get_arg_type(&value)) {
1062                 case DBUS_TYPE_ARRAY:
1063                         if (g_str_equal(key, "AllowedBearers") == TRUE) {
1064                                 allowed_bearers =
1065                                         session_parse_allowed_bearers(&value);
1066                         } else {
1067                                 return -EINVAL;
1068                         }
1069                         break;
1070                 case DBUS_TYPE_BOOLEAN:
1071                         if (g_str_equal(key, "Priority") == TRUE) {
1072                                 dbus_message_iter_get_basic(&value,
1073                                                         &priority);
1074                         } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1075                                 dbus_message_iter_get_basic(&value,
1076                                                         &avoid_handover);
1077                         } else if (g_str_equal(key, "StayConnected") == TRUE) {
1078                                 dbus_message_iter_get_basic(&value,
1079                                                         &stay_connected);
1080                         } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1081                                 dbus_message_iter_get_basic(&value,
1082                                                         &ecall);
1083                         } else {
1084                                 return -EINVAL;
1085                         }
1086                         break;
1087                 case DBUS_TYPE_UINT32:
1088                         if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1089                                 dbus_message_iter_get_basic(&value,
1090                                                         &periodic_connect);
1091                         } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1092                                 dbus_message_iter_get_basic(&value,
1093                                                         &idle_timeout);
1094                         } else {
1095                                 return -EINVAL;
1096                         }
1097                         break;
1098                 case DBUS_TYPE_STRING:
1099                         if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1100                                 dbus_message_iter_get_basic(&value, &val);
1101                                 roaming_policy = string2roamingpolicy(val);
1102                         } else {
1103                                 return -EINVAL;
1104                         }
1105                 }
1106                 dbus_message_iter_next(&array);
1107         }
1108
1109         dbus_message_iter_next(&iter);
1110         dbus_message_iter_get_basic(&iter, &notify_path);
1111
1112         if (notify_path == NULL) {
1113                 err = -EINVAL;
1114                 goto err;
1115         }
1116
1117         session_path = g_strdup_printf("/sessions%s", notify_path);
1118         if (session_path == NULL) {
1119                 err = -ENOMEM;
1120                 goto err;
1121         }
1122
1123         session = g_hash_table_lookup(session_hash, session_path);
1124         if (session != NULL) {
1125                 err = -EEXIST;
1126                 goto err;
1127         }
1128
1129         session = g_try_new0(struct connman_session, 1);
1130         if (session == NULL) {
1131                 err = -ENOMEM;
1132                 goto err;
1133         }
1134
1135         info = &session->info;
1136         info_last = &session->info_last;
1137
1138         session->owner = g_strdup(owner);
1139         session->session_path = session_path;
1140         session->notify_path = g_strdup(notify_path);
1141         session->notify_watch =
1142                 g_dbus_add_disconnect_watch(connection, session->owner,
1143                                         owner_disconnect, session, NULL);
1144
1145         info->bearer = "";
1146         info->online = FALSE;
1147         info->priority = priority;
1148         info->avoid_handover = avoid_handover;
1149         info->stay_connected = stay_connected;
1150         info->periodic_connect = periodic_connect;
1151         info->idle_timeout = idle_timeout;
1152         info->ecall = ecall;
1153         info->roaming_policy = roaming_policy;
1154         info->service = NULL;
1155         info->marker = 0;
1156
1157         if (allowed_bearers == NULL) {
1158                 info->allowed_bearers =
1159                                 session_allowed_bearers_any();
1160
1161                 if (info->allowed_bearers == NULL) {
1162                         err = -ENOMEM;
1163                         goto err;
1164                 }
1165         } else {
1166                 info->allowed_bearers = allowed_bearers;
1167         }
1168
1169         g_hash_table_replace(session_hash, session->session_path, session);
1170
1171         DBG("add %s", session->session_path);
1172
1173         if (g_dbus_register_interface(connection, session->session_path,
1174                                         CONNMAN_SESSION_INTERFACE,
1175                                         session_methods, NULL,
1176                                         NULL, session, NULL) == FALSE) {
1177                 connman_error("Failed to register %s", session->session_path);
1178                 g_hash_table_remove(session_hash, session->session_path);
1179                 session = NULL;
1180
1181                 err = -EINVAL;
1182                 goto err;
1183         }
1184
1185         g_dbus_send_reply(connection, msg,
1186                                 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1187                                 DBUS_TYPE_INVALID);
1188
1189
1190         update_allowed_bearers(session);
1191         update_info(info);
1192         if (info->ecall == TRUE) {
1193                 ecall_session = session;
1194                 update_ecall_sessions(session);
1195         }
1196
1197         info_last->bearer = info->bearer;
1198         info_last->online = info->online;
1199         info_last->priority = info->priority;
1200         info_last->avoid_handover = info->avoid_handover;
1201         info_last->stay_connected = info->stay_connected;
1202         info_last->periodic_connect = info->periodic_connect;
1203         info_last->idle_timeout = info->idle_timeout;
1204         info_last->ecall = info->ecall;
1205         info_last->roaming_policy = info->roaming_policy;
1206         info_last->service = info->service;
1207         info_last->marker = info->marker;
1208         info_last->allowed_bearers = info->allowed_bearers;
1209         update_info(info_last);
1210
1211         session->info_dirty = TRUE;
1212         session->append_all = TRUE;
1213
1214         g_timeout_add_seconds(0, session_cb, session);
1215
1216         return 0;
1217
1218 err:
1219         connman_error("Failed to create session");
1220         g_free(session_path);
1221
1222         g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1223         g_slist_free(allowed_bearers);
1224
1225         return err;
1226 }
1227
1228 int __connman_session_destroy(DBusMessage *msg)
1229 {
1230         const char *owner, *session_path;
1231         struct connman_session *session;
1232
1233         owner = dbus_message_get_sender(msg);
1234
1235         DBG("owner %s", owner);
1236
1237         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1238                                                         DBUS_TYPE_INVALID);
1239         if (session_path == NULL)
1240                 return -EINVAL;
1241
1242         session = g_hash_table_lookup(session_hash, session_path);
1243         if (session == NULL)
1244                 return -EINVAL;
1245
1246         if (g_strcmp0(owner, session->owner) != 0)
1247                 return -EACCES;
1248
1249         session_disconnect(session);
1250
1251         return 0;
1252 }
1253
1254 connman_bool_t __connman_session_mode()
1255 {
1256         return sessionmode;
1257 }
1258
1259 void __connman_session_set_mode(connman_bool_t enable)
1260 {
1261         DBG("enable %d", enable);
1262
1263         if (sessionmode == enable)
1264                 return;
1265
1266         sessionmode = enable;
1267
1268         if (sessionmode == TRUE)
1269                 __connman_service_disconnect_all();
1270 }
1271
1272 static void service_add(struct connman_service *service)
1273 {
1274         GHashTableIter iter;
1275         gpointer key, value;
1276         struct connman_session *session;
1277
1278         DBG("service %p", service);
1279
1280         g_hash_table_iter_init(&iter, session_hash);
1281
1282         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1283                 session = value;
1284
1285                 if (service_match(session, service) == FALSE)
1286                         continue;
1287
1288                 g_sequence_insert_sorted(session->service_list, service,
1289                                                 sort_services, session);
1290
1291                 session_cb(session);
1292         }
1293 }
1294
1295 static int service_remove_from_session(struct connman_session *session,
1296                                         struct connman_service *service)
1297 {
1298
1299         GSequenceIter *iter;
1300
1301         iter = lookup_service(session, service);
1302         if (iter == NULL)
1303                 return -ENOENT;
1304
1305         session->info.online = FALSE;
1306         g_sequence_remove(iter);
1307
1308         return 0;
1309 }
1310
1311 static void service_remove(struct connman_service *service)
1312 {
1313
1314         GHashTableIter iter;
1315         gpointer key, value;
1316         struct connman_session *session;
1317         struct session_info *info;
1318
1319         DBG("service %p", service);
1320
1321         g_hash_table_iter_init(&iter, session_hash);
1322
1323         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1324                 session = value;
1325                 info = &session->info;
1326
1327                 if (service_remove_from_session(session, service) != 0)
1328                         continue;
1329
1330                 info->service = NULL;
1331                 session_cb(session);
1332         }
1333 }
1334
1335 static void service_state_changed(struct connman_service *service,
1336                                         enum connman_service_state state)
1337 {
1338         GHashTableIter iter;
1339         gpointer key, value;
1340         struct connman_session *session;
1341         struct session_info *info;
1342         connman_bool_t online;
1343
1344         DBG("service %p state %d", service, state);
1345
1346         g_hash_table_iter_init(&iter, session_hash);
1347
1348         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1349                 session = value;
1350                 info = &session->info;
1351
1352                 if (info->service == service) {
1353                         online = __connman_service_is_connected(service);
1354                         if (info->online == online)
1355                                 continue;
1356
1357                         info->online = online;
1358                         session->info_dirty = TRUE;
1359                         session_cb(session);
1360                 }
1361         }
1362 }
1363
1364 static void ipconfig_changed(struct connman_service *service,
1365                                 struct connman_ipconfig *ipconfig)
1366 {
1367         GHashTableIter iter;
1368         gpointer key, value;
1369         struct connman_session *session;
1370         struct session_info *info;
1371         enum connman_ipconfig_type type;
1372
1373         DBG("service %p ipconfig %p", service, ipconfig);
1374
1375         type = __connman_ipconfig_get_config_type(ipconfig);
1376
1377         g_hash_table_iter_init(&iter, session_hash);
1378
1379         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1380                 session = value;
1381                 info = &session->info;
1382
1383                 if (info->service == service) {
1384                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1385                                 ipconfig_ipv4_changed(session);
1386                         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1387                                 ipconfig_ipv6_changed(session);
1388                 }
1389         }
1390 }
1391
1392 static struct connman_notifier session_notifier = {
1393         .name                   = "session",
1394         .service_add            = service_add,
1395         .service_remove         = service_remove,
1396         .service_state_changed  = service_state_changed,
1397         .ipconfig_changed       = ipconfig_changed,
1398 };
1399
1400 int __connman_session_init(void)
1401 {
1402         int err;
1403
1404         DBG("");
1405
1406         connection = connman_dbus_get_connection();
1407         if (connection == NULL)
1408                 return -1;
1409
1410         err = connman_notifier_register(&session_notifier);
1411         if (err < 0) {
1412                 dbus_connection_unref(connection);
1413                 return err;
1414         }
1415
1416         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1417                                                 NULL, cleanup_session);
1418
1419         sessionmode = FALSE;
1420         return 0;
1421 }
1422
1423 void __connman_session_cleanup(void)
1424 {
1425         DBG("");
1426
1427         if (connection == NULL)
1428                 return;
1429
1430         connman_notifier_unregister(&session_notifier);
1431
1432         g_hash_table_foreach(session_hash, release_session, NULL);
1433         g_hash_table_destroy(session_hash);
1434
1435         dbus_connection_unref(connection);
1436 }