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