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