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