session: Handle AllowedBearers changes
[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 cleanup_session(gpointer user_data)
588 {
589         struct connman_session *session = user_data;
590         struct session_info *info = &session->info;
591
592         DBG("remove %s", session->session_path);
593
594         g_sequence_free(session->service_list);
595
596         g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
597         g_slist_free(info->allowed_bearers);
598
599         g_free(session->owner);
600         g_free(session->session_path);
601         g_free(session->notify_path);
602
603         g_free(session);
604 }
605
606 static void release_session(gpointer key, gpointer value, gpointer user_data)
607 {
608         struct connman_session *session = value;
609         DBusMessage *message;
610
611         DBG("owner %s path %s", session->owner, session->notify_path);
612
613         if (session->notify_watch > 0)
614                 g_dbus_remove_watch(connection, session->notify_watch);
615
616         g_dbus_unregister_interface(connection, session->session_path,
617                                                 CONNMAN_SESSION_INTERFACE);
618
619         message = dbus_message_new_method_call(session->owner,
620                                                 session->notify_path,
621                                                 CONNMAN_NOTIFICATION_INTERFACE,
622                                                 "Release");
623         if (message == NULL)
624                 return;
625
626         dbus_message_set_no_reply(message, TRUE);
627
628         g_dbus_send_message(connection, message);
629 }
630
631 static int session_disconnect(struct connman_session *session)
632 {
633         DBG("session %p, %s", session, session->owner);
634
635         if (session->notify_watch > 0)
636                 g_dbus_remove_watch(connection, session->notify_watch);
637
638         g_dbus_unregister_interface(connection, session->session_path,
639                                                 CONNMAN_SESSION_INTERFACE);
640
641         g_hash_table_remove(session_hash, session->session_path);
642
643         return 0;
644 }
645
646 static void owner_disconnect(DBusConnection *conn, void *user_data)
647 {
648         struct connman_session *session = user_data;
649
650         DBG("session %p, %s died", session, session->owner);
651
652         session_disconnect(session);
653 }
654
655 static DBusMessage *destroy_session(DBusConnection *conn,
656                                         DBusMessage *msg, void *user_data)
657 {
658         struct connman_session *session = user_data;
659
660         DBG("session %p", session);
661
662         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
663 }
664
665 static gboolean session_changed_connect(gpointer user_data)
666 {
667         struct connman_session *session = user_data;
668         struct session_info *info = &session->info;
669
670         __connman_service_connect(info->service);
671
672         return FALSE;
673 }
674
675 static void update_service(struct connman_session *session)
676 {
677         struct session_info *info = &session->info;
678         int idx;
679
680         if (info->service != NULL) {
681                 info->bearer = session2bearer(session);
682                 info->online = __connman_service_is_connected(info->service);
683                 info->name = __connman_service_get_name(info->service);
684                 idx = __connman_service_get_index(info->service);
685                 info->ifname = connman_inet_ifname(idx);
686
687         } else {
688                 info->bearer = "";
689                 info->online = FALSE;
690                 info->name = "";
691                 info->ifname = "";
692         }
693 }
694
695 static void session_changed(struct connman_session *session)
696 {
697         struct session_info *info = &session->info;
698         struct session_info *info_last = &session->info_last;
699         struct connman_service *service = NULL;
700         GSourceFunc callback = NULL;
701         GSequenceIter *iter;
702
703         /*
704          * TODO: This only a placeholder for the 'real' algorithm to
705          * play a bit around. So we are going to improve it step by step.
706          */
707
708         if (info->connect == FALSE) {
709                 if (info->service != NULL)
710                         __connman_service_disconnect(info->service);
711                 info->service = NULL;
712                 goto out;
713         }
714
715         iter = g_sequence_get_begin_iter(session->service_list);
716
717         while (g_sequence_iter_is_end(iter) == FALSE) {
718                 service = g_sequence_get(iter);
719
720                 if (__connman_service_is_connecting(service) == TRUE ||
721                                 __connman_service_is_connected(service) == TRUE) {
722                         break;
723                 }
724
725                 if (__connman_service_is_idle(service) == TRUE &&
726                                 info->connect == TRUE) {
727                         callback = session_changed_connect;
728                         break;
729                 }
730
731                 service = NULL;
732
733                 iter = g_sequence_iter_next(iter);
734         }
735
736         if (info->service != NULL && info->service != service) {
737                 __connman_service_disconnect(info->service);
738                 info->service = NULL;
739         }
740
741         if (service != NULL) {
742                 info->service = service;
743
744                 if (callback != NULL)
745                         callback(session);
746         }
747
748 out:
749         if (info->service != info_last->service)
750                 update_service(session);
751 }
752
753 static gboolean session_cb(gpointer user_data)
754 {
755         struct connman_session *session = user_data;
756
757         session_changed(session);
758         session_notify(session);
759
760         return FALSE;
761 }
762
763 static DBusMessage *connect_session(DBusConnection *conn,
764                                         DBusMessage *msg, void *user_data)
765 {
766         struct connman_session *session = user_data;
767         struct session_info *info = &session->info;
768
769         DBG("session %p", session);
770
771         info->connect = TRUE;
772
773         g_timeout_add_seconds(0, session_cb, session);
774
775         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
776 }
777
778 static DBusMessage *disconnect_session(DBusConnection *conn,
779                                         DBusMessage *msg, void *user_data)
780 {
781         struct connman_session *session = user_data;
782         struct session_info *info = &session->info;
783
784         DBG("session %p", session);
785
786         info->connect = FALSE;
787
788         g_timeout_add_seconds(0, session_cb, session);
789
790         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
791 }
792
793 static void print_name(gpointer data, gpointer user_data)
794 {
795         struct connman_service *service = data;
796
797         DBG("service %p type %s name %s", service,
798                 service2bearer(connman_service_get_type(service)),
799                 __connman_service_get_name(service));
800 }
801
802 static void update_allowed_bearers(struct connman_session *session)
803 {
804         if (session->service_list != NULL)
805                 g_sequence_free(session->service_list);
806
807         session->service_list = __connman_service_get_list(session,
808                                                                 service_match);
809         g_sequence_sort(session->service_list, sort_services, session);
810         g_sequence_foreach(session->service_list, print_name, NULL);
811 }
812
813 static DBusMessage *change_session(DBusConnection *conn,
814                                         DBusMessage *msg, void *user_data)
815 {
816         struct connman_session *session = user_data;
817         struct session_info *info = &session->info;
818         DBusMessageIter iter, value;
819         DBusMessage *reply;
820         DBusMessageIter reply_array, reply_dict;
821         const char *name;
822         GSList *allowed_bearers;
823
824         DBG("session %p", session);
825         if (dbus_message_iter_init(msg, &iter) == FALSE)
826                 return __connman_error_invalid_arguments(msg);
827
828         reply = dbus_message_new_method_call(session->owner,
829                                                 session->notify_path,
830                                                 CONNMAN_NOTIFICATION_INTERFACE,
831                                                 "Update");
832         if (reply == NULL)
833                 return __connman_error_failed(msg, ENOMEM);
834
835         dbus_message_iter_init_append(reply, &reply_array);
836         connman_dbus_dict_open(&reply_array, &reply_dict);
837
838         dbus_message_iter_get_basic(&iter, &name);
839         dbus_message_iter_next(&iter);
840         dbus_message_iter_recurse(&iter, &value);
841
842         switch (dbus_message_iter_get_arg_type(&value)) {
843         case DBUS_TYPE_ARRAY:
844                 if (g_str_equal(name, "AllowedBearers") == TRUE) {
845                         allowed_bearers = session_parse_allowed_bearers(&value);
846
847                         g_slist_foreach(info->allowed_bearers,
848                                         cleanup_bearer_info, NULL);
849                         g_slist_free(info->allowed_bearers);
850
851                         if (allowed_bearers == NULL) {
852                                 allowed_bearers = session_allowed_bearers_any();
853
854                                 if (allowed_bearers == NULL) {
855                                         dbus_message_unref(reply);
856                                         return __connman_error_failed(msg, ENOMEM);
857                                 }
858                         }
859
860                         info->allowed_bearers = allowed_bearers;
861
862                         update_allowed_bearers(session);
863                 } else {
864                         goto err;
865                 }
866                 break;
867         case DBUS_TYPE_BOOLEAN:
868                 if (g_str_equal(name, "Priority") == TRUE) {
869                         dbus_message_iter_get_basic(&value,
870                                         &info->priority);
871
872                         /* update_priority(); */
873                 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
874                         dbus_message_iter_get_basic(&value,
875                                         &info->avoid_handover);
876
877                         /* update_avoid_handover(); */
878                 } else if (g_str_equal(name, "StayConnected") == TRUE) {
879                         dbus_message_iter_get_basic(&value,
880                                         &info->stay_connected);
881
882                         /* update_stay_connected(); */
883                 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
884                         dbus_message_iter_get_basic(&value,
885                                         &info->ecall);
886
887                         /* update_ecall(); */
888                 } else {
889                         goto err;
890                 }
891                 break;
892         case DBUS_TYPE_UINT32:
893                 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
894                         dbus_message_iter_get_basic(&value,
895                                         &info->periodic_connect);
896
897                         /* update_periodic_update(); */
898                 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
899                         dbus_message_iter_get_basic(&value,
900                                         &info->idle_timeout);
901
902                         /* update_idle_timeout(); */
903                 } else {
904                         goto err;
905                 }
906                 break;
907         case DBUS_TYPE_STRING:
908                 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
909                         const char *val;
910                         dbus_message_iter_get_basic(&value, &val);
911                         info->roaming_policy =
912                                         string2roamingpolicy(val);
913
914                         /* update_roaming_allowed(); */
915                 } else {
916                         goto err;
917                 }
918                 break;
919         }
920
921         append_notify(&reply_dict, session);
922
923         connman_dbus_dict_close(&reply_array, &reply_dict);
924
925         g_dbus_send_message(connection, reply);
926
927         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
928
929 err:
930         dbus_message_unref(reply);
931         return __connman_error_invalid_arguments(msg);
932 }
933
934 static GDBusMethodTable session_methods[] = {
935         { "Destroy",    "",   "", destroy_session    },
936         { "Connect",    "",   "", connect_session    },
937         { "Disconnect", "",   "", disconnect_session },
938         { "Change",     "sv", "", change_session     },
939         { },
940 };
941
942 int __connman_session_create(DBusMessage *msg)
943 {
944         const char *owner, *notify_path;
945         char *session_path;
946         DBusMessageIter iter, array;
947         struct connman_session *session;
948         struct session_info *info, *info_last;
949
950         connman_bool_t priority = FALSE, avoid_handover = FALSE;
951         connman_bool_t stay_connected = FALSE, ecall = FALSE;
952         enum connman_session_roaming_policy roaming_policy =
953                                 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
954         GSList *allowed_bearers = NULL;
955         unsigned int periodic_connect = 0;
956         unsigned int idle_timeout = 0;
957
958         int err;
959
960         owner = dbus_message_get_sender(msg);
961
962         DBG("owner %s", owner);
963
964         dbus_message_iter_init(msg, &iter);
965         dbus_message_iter_recurse(&iter, &array);
966
967         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
968                 DBusMessageIter entry, value;
969                 const char *key, *val;
970
971                 dbus_message_iter_recurse(&array, &entry);
972                 dbus_message_iter_get_basic(&entry, &key);
973
974                 dbus_message_iter_next(&entry);
975                 dbus_message_iter_recurse(&entry, &value);
976
977                 switch (dbus_message_iter_get_arg_type(&value)) {
978                 case DBUS_TYPE_ARRAY:
979                         if (g_str_equal(key, "AllowedBearers") == TRUE) {
980                                 allowed_bearers =
981                                         session_parse_allowed_bearers(&value);
982                         } else {
983                                 return -EINVAL;
984                         }
985                         break;
986                 case DBUS_TYPE_BOOLEAN:
987                         if (g_str_equal(key, "Priority") == TRUE) {
988                                 dbus_message_iter_get_basic(&value,
989                                                         &priority);
990                         } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
991                                 dbus_message_iter_get_basic(&value,
992                                                         &avoid_handover);
993                         } else if (g_str_equal(key, "StayConnected") == TRUE) {
994                                 dbus_message_iter_get_basic(&value,
995                                                         &stay_connected);
996                         } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
997                                 dbus_message_iter_get_basic(&value,
998                                                         &ecall);
999                         } else {
1000                                 return -EINVAL;
1001                         }
1002                         break;
1003                 case DBUS_TYPE_UINT32:
1004                         if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1005                                 dbus_message_iter_get_basic(&value,
1006                                                         &periodic_connect);
1007                         } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1008                                 dbus_message_iter_get_basic(&value,
1009                                                         &idle_timeout);
1010                         } else {
1011                                 return -EINVAL;
1012                         }
1013                         break;
1014                 case DBUS_TYPE_STRING:
1015                         if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1016                                 dbus_message_iter_get_basic(&value, &val);
1017                                 roaming_policy = string2roamingpolicy(val);
1018                         } else {
1019                                 return -EINVAL;
1020                         }
1021                 }
1022                 dbus_message_iter_next(&array);
1023         }
1024
1025         dbus_message_iter_next(&iter);
1026         dbus_message_iter_get_basic(&iter, &notify_path);
1027
1028         if (notify_path == NULL) {
1029                 session_path = NULL;
1030                 err = -EINVAL;
1031                 goto err;
1032         }
1033
1034         session_path = g_strdup_printf("/sessions%s", notify_path);
1035         if (session_path == NULL) {
1036                 err = -ENOMEM;
1037                 goto err;
1038         }
1039
1040         session = g_hash_table_lookup(session_hash, session_path);
1041         if (session != NULL) {
1042                 err = -EEXIST;
1043                 goto err;
1044         }
1045
1046         session = g_try_new0(struct connman_session, 1);
1047         if (session == NULL) {
1048                 err = -ENOMEM;
1049                 goto err;
1050         }
1051
1052         info = &session->info;
1053         info_last = &session->info_last;
1054
1055         session->owner = g_strdup(owner);
1056         session->session_path = session_path;
1057         session->notify_path = g_strdup(notify_path);
1058         session->notify_watch =
1059                 g_dbus_add_disconnect_watch(connection, session->owner,
1060                                         owner_disconnect, session, NULL);
1061
1062         info->bearer = "";
1063         info->online = FALSE;
1064         info->priority = priority;
1065         info->avoid_handover = avoid_handover;
1066         info->stay_connected = stay_connected;
1067         info->periodic_connect = periodic_connect;
1068         info->idle_timeout = idle_timeout;
1069         info->ecall = ecall;
1070         info->roaming_policy = roaming_policy;
1071
1072         if (allowed_bearers == NULL) {
1073                 info->allowed_bearers =
1074                                 session_allowed_bearers_any();
1075
1076                 if (info->allowed_bearers == NULL) {
1077                         err = -ENOMEM;
1078                         goto err;
1079                 }
1080         } else {
1081                 info->allowed_bearers = allowed_bearers;
1082         }
1083
1084         info_last->bearer = NULL;
1085         info_last->online = !priority;
1086         info_last->avoid_handover = !avoid_handover;
1087         info_last->stay_connected = !stay_connected;
1088         info_last->periodic_connect = !periodic_connect;
1089         info_last->idle_timeout = !idle_timeout;
1090         info_last->ecall = !ecall;
1091         info_last->roaming_policy =
1092                         CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
1093         info_last->allowed_bearers = NULL;
1094         info_last->service = (void *) 1;
1095         info_last->marker = info->marker + 1;
1096
1097         update_allowed_bearers(session);
1098
1099         g_hash_table_replace(session_hash, session->session_path, session);
1100
1101         DBG("add %s", session->session_path);
1102
1103         if (g_dbus_register_interface(connection, session->session_path,
1104                                         CONNMAN_SESSION_INTERFACE,
1105                                         session_methods, NULL,
1106                                         NULL, session, NULL) == FALSE) {
1107                 connman_error("Failed to register %s", session->session_path);
1108                 g_hash_table_remove(session_hash, session->session_path);
1109                 session = NULL;
1110
1111                 err = -EINVAL;
1112                 goto err;
1113         }
1114
1115         g_dbus_send_reply(connection, msg,
1116                                 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1117                                 DBUS_TYPE_INVALID);
1118
1119         g_timeout_add_seconds(0, session_cb, session);
1120
1121         return 0;
1122
1123 err:
1124         connman_error("Failed to create session");
1125         g_free(session_path);
1126
1127         g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1128         g_slist_free(allowed_bearers);
1129
1130         return err;
1131 }
1132
1133 int __connman_session_destroy(DBusMessage *msg)
1134 {
1135         const char *owner, *session_path;
1136         struct connman_session *session;
1137
1138         owner = dbus_message_get_sender(msg);
1139
1140         DBG("owner %s", owner);
1141
1142         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1143                                                         DBUS_TYPE_INVALID);
1144         if (session_path == NULL)
1145                 return -EINVAL;
1146
1147         session = g_hash_table_lookup(session_hash, session_path);
1148         if (session == NULL)
1149                 return -EINVAL;
1150
1151         if (g_strcmp0(owner, session->owner) != 0)
1152                 return -EACCES;
1153
1154         session_disconnect(session);
1155
1156         return 0;
1157 }
1158
1159 connman_bool_t __connman_session_mode()
1160 {
1161         return sessionmode;
1162 }
1163
1164 void __connman_session_set_mode(connman_bool_t enable)
1165 {
1166         DBG("enable %d", enable);
1167
1168         if (sessionmode == enable)
1169                 return;
1170
1171         sessionmode = enable;
1172
1173         if (sessionmode == TRUE)
1174                 __connman_service_disconnect_all();
1175 }
1176
1177 static void service_add(struct connman_service *service)
1178 {
1179         GHashTableIter iter;
1180         gpointer key, value;
1181         struct connman_session *session;
1182
1183         DBG("service %p", service);
1184
1185         g_hash_table_iter_init(&iter, session_hash);
1186
1187         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1188                 session = value;
1189
1190                 if (service_match(session, service) == FALSE)
1191                         continue;
1192
1193                 g_sequence_insert_sorted(session->service_list, service,
1194                                                 sort_services, session);
1195
1196                 session_cb(session);
1197         }
1198 }
1199
1200 static gint service_in_session(gconstpointer a, gconstpointer b,
1201                                 gpointer user_data)
1202 {
1203         if (a == b)
1204                 return 0;
1205
1206         return -1;
1207 }
1208
1209 static void service_remove(struct connman_service *service)
1210 {
1211
1212         GHashTableIter iter;
1213         gpointer key, value;
1214         GSequenceIter *seq_iter;
1215         struct connman_session *session;
1216         struct session_info *info;
1217         struct connman_service *found_service;
1218
1219         DBG("service %p", service);
1220
1221         g_hash_table_iter_init(&iter, session_hash);
1222
1223         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1224                 session = value;
1225                 info = &session->info;
1226
1227                 if (session->service_list == NULL)
1228                         continue;
1229
1230                 seq_iter = g_sequence_search(session->service_list, service,
1231                                                 service_in_session, NULL);
1232                 if (g_sequence_iter_is_end(seq_iter) == TRUE)
1233                         continue;
1234
1235                 g_sequence_remove(seq_iter);
1236
1237                 found_service = g_sequence_get(seq_iter);
1238                 if (found_service != info->service)
1239                         continue;
1240
1241                 info->service = NULL;
1242                 session_cb(session);
1243         }
1244 }
1245
1246 static void service_state_changed(struct connman_service *service,
1247                                         enum connman_service_state state)
1248 {
1249         GHashTableIter iter;
1250         gpointer key, value;
1251         struct connman_session *session;
1252         struct session_info *info;
1253         connman_bool_t online;
1254
1255         DBG("service %p state %d", service, state);
1256
1257         g_hash_table_iter_init(&iter, session_hash);
1258
1259         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1260                 session = value;
1261                 info = &session->info;
1262
1263                 if (info->service == service) {
1264                         online = __connman_service_is_connected(service);
1265                         if (info->online == online)
1266                                 continue;
1267
1268                         info->online = online;
1269                         session_cb(session);
1270                 }
1271         }
1272 }
1273
1274 static void ipconfig_changed(struct connman_service *service,
1275                                 struct connman_ipconfig *ipconfig)
1276 {
1277         GHashTableIter iter;
1278         gpointer key, value;
1279         struct connman_session *session;
1280         struct session_info *info;
1281         enum connman_ipconfig_type type;
1282
1283         DBG("service %p ipconfig %p", service, ipconfig);
1284
1285         type = __connman_ipconfig_get_config_type(ipconfig);
1286
1287         g_hash_table_iter_init(&iter, session_hash);
1288
1289         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1290                 session = value;
1291                 info = &session->info;
1292
1293                 if (info->service == service) {
1294                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1295                                 ipconfig_ipv4_changed(session);
1296                         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1297                                 ipconfig_ipv6_changed(session);
1298                 }
1299         }
1300 }
1301
1302 static struct connman_notifier session_notifier = {
1303         .name                   = "session",
1304         .service_add            = service_add,
1305         .service_remove         = service_remove,
1306         .service_state_changed  = service_state_changed,
1307         .ipconfig_changed       = ipconfig_changed,
1308 };
1309
1310 int __connman_session_init(void)
1311 {
1312         int err;
1313
1314         DBG("");
1315
1316         connection = connman_dbus_get_connection();
1317         if (connection == NULL)
1318                 return -1;
1319
1320         err = connman_notifier_register(&session_notifier);
1321         if (err < 0) {
1322                 dbus_connection_unref(connection);
1323                 return err;
1324         }
1325
1326         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1327                                                 NULL, cleanup_session);
1328
1329         sessionmode = FALSE;
1330         return 0;
1331 }
1332
1333 void __connman_session_cleanup(void)
1334 {
1335         DBG("");
1336
1337         if (connection == NULL)
1338                 return;
1339
1340         connman_notifier_unregister(&session_notifier);
1341
1342         g_hash_table_foreach(session_hash, release_session, NULL);
1343         g_hash_table_destroy(session_hash);
1344
1345         dbus_connection_unref(connection);
1346 }