session: Remove match_all from connman_session_bearer
[platform/upstream/connman.git] / src / session.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 <errno.h>
28
29 #include <gdbus.h>
30
31 #include <connman/session.h>
32
33 #include "connman.h"
34
35 static DBusConnection *connection;
36 static GHashTable *session_hash;
37 static connman_bool_t sessionmode;
38 static struct connman_session *ecall_session;
39 static GSList *policy_list;
40
41 enum connman_session_trigger {
42         CONNMAN_SESSION_TRIGGER_UNKNOWN         = 0,
43         CONNMAN_SESSION_TRIGGER_SETTING         = 1,
44         CONNMAN_SESSION_TRIGGER_CONNECT         = 2,
45         CONNMAN_SESSION_TRIGGER_DISCONNECT      = 3,
46         CONNMAN_SESSION_TRIGGER_SERVICE         = 4,
47         CONNMAN_SESSION_TRIGGER_ECALL           = 5,
48 };
49
50 enum connman_session_reason {
51         CONNMAN_SESSION_REASON_UNKNOWN          = 0,
52         CONNMAN_SESSION_REASON_CONNECT          = 1,
53         CONNMAN_SESSION_REASON_FREE_RIDE        = 2,
54 };
55
56 enum connman_session_state {
57         CONNMAN_SESSION_STATE_DISCONNECTED   = 0,
58         CONNMAN_SESSION_STATE_CONNECTED      = 1,
59         CONNMAN_SESSION_STATE_ONLINE         = 2,
60 };
61
62 struct service_entry {
63         /* track why this service was selected */
64         enum connman_session_reason reason;
65         enum connman_service_state state;
66         const char *name;
67         struct connman_service *service;
68         char *ifname;
69         const char *bearer;
70         GSList *pending_timeouts;
71 };
72
73 struct session_info {
74         struct connman_session_config config;
75         enum connman_session_state state;
76         struct service_entry *entry;
77         enum connman_session_reason reason;
78 };
79
80 struct connman_session {
81         char *owner;
82         char *session_path;
83         char *notify_path;
84         guint notify_watch;
85
86         struct connman_session_policy *policy;
87
88         connman_bool_t append_all;
89         struct session_info *info;
90         struct session_info *info_last;
91         struct connman_session_config *policy_config;
92
93         connman_bool_t ecall;
94
95         GSequence *service_list;
96         GHashTable *service_hash;
97 };
98
99 static const char *trigger2string(enum connman_session_trigger trigger)
100 {
101         switch (trigger) {
102         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
103                 break;
104         case CONNMAN_SESSION_TRIGGER_SETTING:
105                 return "setting";
106         case CONNMAN_SESSION_TRIGGER_CONNECT:
107                 return "connect";
108         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
109                 return "disconnect";
110         case CONNMAN_SESSION_TRIGGER_SERVICE:
111                 return "service";
112         case CONNMAN_SESSION_TRIGGER_ECALL:
113                 return "ecall";
114         }
115
116         return NULL;
117 }
118
119 static const char *reason2string(enum connman_session_reason reason)
120 {
121         switch (reason) {
122         case CONNMAN_SESSION_REASON_UNKNOWN:
123                 return "unknown";
124         case CONNMAN_SESSION_REASON_CONNECT:
125                 return "connect";
126         case CONNMAN_SESSION_REASON_FREE_RIDE:
127                 return "free-ride";
128         }
129
130         return NULL;
131 }
132
133 static const char *state2string(enum connman_session_state state)
134 {
135         switch (state) {
136         case CONNMAN_SESSION_STATE_DISCONNECTED:
137                 return "disconnected";
138         case CONNMAN_SESSION_STATE_CONNECTED:
139                 return "connected";
140         case CONNMAN_SESSION_STATE_ONLINE:
141                 return "online";
142         }
143
144         return NULL;
145 }
146
147 static const char *type2string(enum connman_session_type type)
148 {
149         switch (type) {
150         case CONNMAN_SESSION_TYPE_UNKNOWN:
151                 return "";
152         case CONNMAN_SESSION_TYPE_ANY:
153                 return "any";
154         case CONNMAN_SESSION_TYPE_LOCAL:
155                 return "local";
156         case CONNMAN_SESSION_TYPE_INTERNET:
157                 return "internet";
158         }
159
160         return NULL;
161 }
162
163 static enum connman_session_type string2type(const char *type)
164 {
165         if (g_strcmp0(type, "any") == 0)
166                 return CONNMAN_SESSION_TYPE_ANY;
167         if (g_strcmp0(type, "local") == 0)
168                 return CONNMAN_SESSION_TYPE_LOCAL;
169         else if (g_strcmp0(type, "internet") == 0)
170                 return CONNMAN_SESSION_TYPE_INTERNET;
171
172         return CONNMAN_SESSION_TYPE_UNKNOWN;
173 }
174
175 static int bearer2service(const char *bearer, enum connman_service_type *type)
176 {
177         if (g_strcmp0(bearer, "ethernet") == 0)
178                 *type = CONNMAN_SERVICE_TYPE_ETHERNET;
179         else if (g_strcmp0(bearer, "wifi") == 0)
180                 *type = CONNMAN_SERVICE_TYPE_WIFI;
181         else if (g_strcmp0(bearer, "wimax") == 0)
182                 *type = CONNMAN_SERVICE_TYPE_WIMAX;
183         else if (g_strcmp0(bearer, "bluetooth") == 0)
184                 *type = CONNMAN_SERVICE_TYPE_BLUETOOTH;
185         else if (g_strcmp0(bearer, "cellular") == 0)
186                 *type = CONNMAN_SERVICE_TYPE_CELLULAR;
187         else if (g_strcmp0(bearer, "vpn") == 0)
188                 *type = CONNMAN_SERVICE_TYPE_VPN;
189         else if (g_strcmp0(bearer, "*") == 0)
190                 *type = CONNMAN_SERVICE_TYPE_UNKNOWN;
191         else
192                 return -EINVAL;
193
194         return 0;
195 }
196
197 static char *service2bearer(enum connman_service_type type)
198 {
199         switch (type) {
200         case CONNMAN_SERVICE_TYPE_ETHERNET:
201                 return "ethernet";
202         case CONNMAN_SERVICE_TYPE_WIFI:
203                 return "wifi";
204         case CONNMAN_SERVICE_TYPE_WIMAX:
205                 return "wimax";
206         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
207                 return "bluetooth";
208         case CONNMAN_SERVICE_TYPE_CELLULAR:
209                 return "cellular";
210         case CONNMAN_SERVICE_TYPE_VPN:
211                 return "vpn";
212         case CONNMAN_SERVICE_TYPE_UNKNOWN:
213         case CONNMAN_SERVICE_TYPE_SYSTEM:
214         case CONNMAN_SERVICE_TYPE_GPS:
215         case CONNMAN_SERVICE_TYPE_GADGET:
216                 return "";
217         }
218
219         return "";
220 }
221
222 static int assign_policy_plugin(struct connman_session *session)
223 {
224         if (session->policy != NULL)
225                 return -EALREADY;
226
227         if (policy_list == NULL)
228                 return 0;
229
230         session->policy = policy_list->data;
231
232         return 0;
233 }
234
235 static int create_policy_config(struct connman_session *session,
236                                 connman_session_config_cb callback,
237                                 void *user_data)
238 {
239         struct connman_session_config *config;
240
241         if (session->policy == NULL) {
242                 config = connman_session_create_default_config();
243                 if (config == NULL)
244                         return -ENOMEM;
245
246                 session->policy_config = config;
247
248                 return 0;
249         }
250
251         return (*session->policy->create)(session, callback, user_data);
252 }
253
254 static void destroy_policy_config(struct connman_session *session)
255 {
256         if (session->policy == NULL) {
257                 connman_session_free_bearers(
258                         session->policy_config->allowed_bearers);
259                 g_free(session->policy_config);
260         } else {
261                 (*session->policy->destroy)(session);
262         }
263 }
264
265 static void probe_policy(struct connman_session_policy *policy)
266 {
267
268         GHashTableIter iter;
269         gpointer key, value;
270         struct connman_session *session;
271
272         DBG("policy %p name %s", policy, policy->name);
273
274         g_hash_table_iter_init(&iter, session_hash);
275
276         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
277                 session = value;
278
279                 if (session->policy != NULL)
280                         continue;
281
282                 assign_policy_plugin(session);
283         }
284 }
285
286 static void remove_policy(struct connman_session_policy *policy)
287 {
288         GHashTableIter iter;
289         gpointer key, value;
290         struct connman_session *session;
291
292         DBG("policy %p name %s", policy, policy->name);
293
294         g_hash_table_iter_init(&iter, session_hash);
295
296         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
297                 session = value;
298
299                 if (session->policy != policy)
300                         continue;
301
302                 session->policy = NULL;
303                 assign_policy_plugin(session);
304         }
305 }
306
307 static gint compare_priority(gconstpointer a, gconstpointer b)
308 {
309         const struct connman_session_policy *policy1 = a;
310         const struct connman_session_policy *policy2 = b;
311
312         return policy2->priority - policy1->priority;
313 }
314
315
316 int connman_session_policy_register(struct connman_session_policy *policy)
317 {
318         DBG("name %s", policy->name);
319
320         if (policy->create == NULL || policy->destroy == NULL)
321                 return -EINVAL;
322
323         policy_list = g_slist_insert_sorted(policy_list, policy,
324                                                 compare_priority);
325
326         probe_policy(policy);
327
328         return 0;
329 }
330
331 void connman_session_policy_unregister(struct connman_session_policy *policy)
332 {
333         DBG("name %s", policy->name);
334
335         policy_list = g_slist_remove(policy_list, policy);
336
337         remove_policy(policy);
338 }
339
340 struct connman_session_config *connman_session_create_default_config(void)
341 {
342         struct connman_session_config *config;
343
344         config = g_try_new0(struct connman_session_config, 1);
345         if (config == NULL)
346                 return NULL;
347
348         config->priority = FALSE;
349         config->roaming_policy = CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
350         config->type = CONNMAN_SESSION_TYPE_ANY;
351         config->ecall = FALSE;
352         config->allowed_bearers = connman_session_allowed_bearers_any();
353         if (config->allowed_bearers == NULL) {
354                 g_free(config);
355                 return NULL;
356         }
357
358         return config;
359 }
360
361 static enum connman_session_type apply_policy_on_type(
362                         enum connman_session_type policy,
363                         enum connman_session_type type)
364 {
365         if (type == CONNMAN_SESSION_TYPE_UNKNOWN)
366                 return CONNMAN_SESSION_TYPE_UNKNOWN;
367
368         if (policy == CONNMAN_SESSION_TYPE_ANY)
369                 return type;
370
371         if (policy == CONNMAN_SESSION_TYPE_LOCAL)
372                 return CONNMAN_SESSION_TYPE_LOCAL;
373
374         return CONNMAN_SESSION_TYPE_INTERNET;
375 }
376
377 static void cleanup_bearer(gpointer data)
378 {
379         struct connman_session_bearer *bearer = data;
380
381         g_free(bearer);
382 }
383
384 void connman_session_free_bearers(GSList *bearers)
385 {
386         g_slist_free_full(bearers, cleanup_bearer);
387 }
388
389 static int session_parse_allowed_bearers(DBusMessageIter *iter, GSList **list)
390 {
391         struct connman_session_bearer *bearer;
392         DBusMessageIter array;
393         int err;
394
395         dbus_message_iter_recurse(iter, &array);
396
397         *list = NULL;
398
399         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
400                 char *bearer_name = NULL;
401
402                 dbus_message_iter_get_basic(&array, &bearer_name);
403
404                 bearer = g_try_new0(struct connman_session_bearer, 1);
405                 if (bearer == NULL) {
406                         connman_session_free_bearers(*list);
407                         *list = NULL;
408                         return -ENOMEM;
409                 }
410
411                 err = bearer2service(bearer_name, &bearer->service_type);
412                 if (err < 0) {
413                         connman_session_free_bearers(*list);
414                         *list = NULL;
415                         return err;
416                 }
417
418                 *list = g_slist_append(*list, bearer);
419
420                 dbus_message_iter_next(&array);
421         }
422
423         return 0;
424 }
425
426 static struct connman_session_bearer *clone_bearer(
427                                         struct connman_session_bearer *orig)
428 {
429         struct connman_session_bearer *bearer;
430
431         bearer = g_try_new0(struct connman_session_bearer, 1);
432         if (bearer == NULL)
433                 return NULL;
434
435         bearer->service_type = orig->service_type;
436
437         return bearer;
438 }
439
440 static int filter_bearer(GSList *policy_bearers,
441                                 struct connman_session_bearer *bearer,
442                                 GSList **list)
443 {
444         struct connman_session_bearer *policy, *tmp;
445         GSList *it;
446
447         if (policy_bearers == NULL)
448                 goto clone;
449
450         for (it = policy_bearers; it != NULL; it = it->next) {
451                 policy = it->data;
452
453                 if (policy->service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
454                                 bearer->service_type != policy->service_type)
455                         continue;
456
457                 goto clone;
458         }
459
460         *list = NULL;
461
462         return 0;
463
464 clone:
465         tmp = clone_bearer(bearer);
466         if (tmp == NULL) {
467                 connman_session_free_bearers(*list);
468                 *list = NULL;
469                 return -ENOMEM;
470         }
471         *list = g_slist_append(*list, tmp);
472
473         return 0;
474 }
475
476 static int apply_policy_on_bearers(GSList *policy_bearers, GSList *bearers,
477                                 GSList **list)
478 {
479         struct connman_session_bearer *bearer;
480         GSList *it;
481         int err;
482
483         *list = NULL;
484
485         for (it = bearers; it != NULL; it = it->next) {
486                 bearer = it->data;
487
488                 err = filter_bearer(policy_bearers, bearer, list);
489                 if (err < 0)
490                         return err;
491         }
492
493         return 0;
494 }
495
496 GSList *connman_session_allowed_bearers_any(void)
497 {
498         struct connman_session_bearer *bearer;
499         GSList *list = NULL;
500
501         bearer = g_try_new0(struct connman_session_bearer, 1);
502         if (bearer == NULL)
503                 return NULL;
504
505         bearer->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
506
507         list = g_slist_append(list, bearer);
508
509         return list;
510 }
511
512 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
513 {
514         struct session_info *info = user_data;
515         GSList *list;
516
517         for (list = info->config.allowed_bearers;
518                         list != NULL; list = list->next) {
519                 struct connman_session_bearer *bearer = list->data;
520                 const char *name =
521                         __connman_service_type2string(bearer->service_type);
522
523                 if (name == NULL)
524                         name = "*";
525
526                 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
527                                                 &name);
528         }
529 }
530
531 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
532 {
533         struct connman_service *service = user_data;
534         struct connman_ipconfig *ipconfig_ipv4;
535
536         if (service == NULL)
537                 return;
538
539         if (__connman_service_is_connected_state(service,
540                                 CONNMAN_IPCONFIG_TYPE_IPV4) == FALSE) {
541                 return;
542         }
543
544         ipconfig_ipv4 = __connman_service_get_ip4config(service);
545         if (ipconfig_ipv4 == NULL)
546                 return;
547
548         __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
549 }
550
551 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
552 {
553         struct connman_service *service = user_data;
554         struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
555
556         if (service == NULL)
557                 return;
558
559         if (__connman_service_is_connected_state(service,
560                                 CONNMAN_IPCONFIG_TYPE_IPV6) == FALSE) {
561                 return;
562         }
563
564         ipconfig_ipv4 = __connman_service_get_ip4config(service);
565         ipconfig_ipv6 = __connman_service_get_ip6config(service);
566         if (ipconfig_ipv6 == NULL)
567                 return;
568
569         __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
570 }
571
572 static void append_notify(DBusMessageIter *dict,
573                                         struct connman_session *session)
574 {
575         struct session_info *info = session->info;
576         struct session_info *info_last = session->info_last;
577         struct connman_service *service;
578         const char *name, *ifname, *bearer;
579
580         if (session->append_all == TRUE ||
581                         info->state != info_last->state) {
582                 const char *state = state2string(info->state);
583
584                 connman_dbus_dict_append_basic(dict, "State",
585                                                 DBUS_TYPE_STRING,
586                                                 &state);
587                 info_last->state = info->state;
588         }
589
590         if (session->append_all == TRUE ||
591                         info->entry != info_last->entry) {
592                 if (info->entry == NULL) {
593                         name = "";
594                         ifname = "";
595                         service = NULL;
596                         bearer = "";
597                 } else {
598                         name = info->entry->name;
599                         ifname = info->entry->ifname;
600                         service = info->entry->service;
601                         bearer = info->entry->bearer;
602                 }
603
604                 connman_dbus_dict_append_basic(dict, "Name",
605                                                 DBUS_TYPE_STRING,
606                                                 &name);
607
608                 connman_dbus_dict_append_dict(dict, "IPv4",
609                                                 append_ipconfig_ipv4,
610                                                 service);
611
612                 connman_dbus_dict_append_dict(dict, "IPv6",
613                                                 append_ipconfig_ipv6,
614                                                 service);
615
616                 connman_dbus_dict_append_basic(dict, "Interface",
617                                                 DBUS_TYPE_STRING,
618                                                 &ifname);
619
620                 connman_dbus_dict_append_basic(dict, "Bearer",
621                                                 DBUS_TYPE_STRING,
622                                                 &bearer);
623
624                 info_last->entry = info->entry;
625         }
626
627         if (session->append_all == TRUE ||
628                         info->config.type != info_last->config.type) {
629                 const char *type = type2string(info->config.type);
630
631                 connman_dbus_dict_append_basic(dict, "ConnectionType",
632                                                 DBUS_TYPE_STRING,
633                                                 &type);
634                 info_last->config.type = info->config.type;
635         }
636
637         if (session->append_all == TRUE ||
638                         info->config.allowed_bearers != info_last->config.allowed_bearers) {
639                 connman_dbus_dict_append_array(dict, "AllowedBearers",
640                                                 DBUS_TYPE_STRING,
641                                                 append_allowed_bearers,
642                                                 info);
643                 info_last->config.allowed_bearers = info->config.allowed_bearers;
644         }
645
646         session->append_all = FALSE;
647 }
648
649 static connman_bool_t is_type_matching_state(enum connman_session_state *state,
650                                                 enum connman_session_type type)
651 {
652         switch (type) {
653         case CONNMAN_SESSION_TYPE_UNKNOWN:
654                 return FALSE;
655         case CONNMAN_SESSION_TYPE_ANY:
656                 return TRUE;
657         case CONNMAN_SESSION_TYPE_LOCAL:
658                 if (*state >= CONNMAN_SESSION_STATE_CONNECTED) {
659                         *state = CONNMAN_SESSION_STATE_CONNECTED;
660                         return TRUE;
661                 }
662
663                 break;
664         case CONNMAN_SESSION_TYPE_INTERNET:
665                 if (*state == CONNMAN_SESSION_STATE_ONLINE)
666                         return TRUE;
667                 break;
668         }
669
670         return FALSE;
671 }
672
673 static connman_bool_t compute_notifiable_changes(struct connman_session *session)
674 {
675         struct session_info *info_last = session->info_last;
676         struct session_info *info = session->info;
677
678         if (session->append_all == TRUE)
679                 return TRUE;
680
681         if (info->state != info_last->state)
682                 return TRUE;
683
684         if (info->entry != info_last->entry &&
685                         info->state >= CONNMAN_SESSION_STATE_CONNECTED)
686                 return TRUE;
687
688         if (info->config.allowed_bearers != info_last->config.allowed_bearers ||
689                         info->config.type != info_last->config.type)
690                 return TRUE;
691
692         return FALSE;
693 }
694
695 static gboolean session_notify(gpointer user_data)
696 {
697         struct connman_session *session = user_data;
698         DBusMessage *msg;
699         DBusMessageIter array, dict;
700
701         if (compute_notifiable_changes(session) == FALSE)
702                 return FALSE;
703
704         DBG("session %p owner %s notify_path %s", session,
705                 session->owner, session->notify_path);
706
707         msg = dbus_message_new_method_call(session->owner, session->notify_path,
708                                                 CONNMAN_NOTIFICATION_INTERFACE,
709                                                 "Update");
710         if (msg == NULL)
711                 return FALSE;
712
713         dbus_message_iter_init_append(msg, &array);
714         connman_dbus_dict_open(&array, &dict);
715
716         append_notify(&dict, session);
717
718         connman_dbus_dict_close(&array, &dict);
719
720         g_dbus_send_message(connection, msg);
721
722         return FALSE;
723 }
724
725 static void ipconfig_ipv4_changed(struct connman_session *session)
726 {
727         struct session_info *info = session->info;
728
729         connman_dbus_setting_changed_dict(session->owner, session->notify_path,
730                                                 "IPv4", append_ipconfig_ipv4,
731                                                 info->entry->service);
732 }
733
734 static void ipconfig_ipv6_changed(struct connman_session *session)
735 {
736         struct session_info *info = session->info;
737
738         connman_dbus_setting_changed_dict(session->owner, session->notify_path,
739                                                 "IPv6", append_ipconfig_ipv6,
740                                                 info->entry->service);
741 }
742
743 static connman_bool_t service_type_match(struct connman_session *session,
744                                         struct connman_service *service)
745 {
746         struct session_info *info = session->info;
747         GSList *list;
748
749         for (list = info->config.allowed_bearers;
750                         list != NULL; list = list->next) {
751                 struct connman_session_bearer *bearer = list->data;
752                 enum connman_service_type service_type;
753
754                 if (bearer->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN)
755                         return TRUE;
756
757                 service_type = connman_service_get_type(service);
758                 if (bearer->service_type == service_type)
759                         return TRUE;
760         }
761
762         return FALSE;
763 }
764
765 static connman_bool_t service_match(struct connman_session *session,
766                                         struct connman_service *service)
767 {
768         if (service_type_match(session, service) == FALSE)
769                 return FALSE;
770
771         return TRUE;
772 }
773
774 static int service_type_weight(enum connman_service_type type)
775 {
776         /*
777          * The session doesn't care which service
778          * to use. Nevertheless we have to sort them
779          * according their type. The ordering is
780          *
781          * 1. Ethernet
782          * 2. Bluetooth
783          * 3. WiFi/WiMAX
784          * 4. Cellular
785          */
786
787         switch (type) {
788         case CONNMAN_SERVICE_TYPE_ETHERNET:
789                 return 4;
790         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
791                 return 3;
792         case CONNMAN_SERVICE_TYPE_WIFI:
793         case CONNMAN_SERVICE_TYPE_WIMAX:
794                 return 2;
795         case CONNMAN_SERVICE_TYPE_CELLULAR:
796                 return 1;
797         case CONNMAN_SERVICE_TYPE_UNKNOWN:
798         case CONNMAN_SERVICE_TYPE_SYSTEM:
799         case CONNMAN_SERVICE_TYPE_GPS:
800         case CONNMAN_SERVICE_TYPE_VPN:
801         case CONNMAN_SERVICE_TYPE_GADGET:
802                 break;
803         }
804
805         return 0;
806 }
807
808 static gint sort_allowed_bearers(struct connman_service *service_a,
809                                         struct connman_service *service_b,
810                                         struct connman_session *session)
811 {
812         struct session_info *info = session->info;
813         GSList *list;
814         enum connman_service_type type_a, type_b;
815         int weight_a, weight_b;
816
817         type_a = connman_service_get_type(service_a);
818         type_b = connman_service_get_type(service_b);
819
820         for (list = info->config.allowed_bearers;
821                         list != NULL; list = list->next) {
822                 struct connman_session_bearer *bearer = list->data;
823
824                 if (bearer->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN) {
825                         if (type_a != type_b) {
826                                 weight_a = service_type_weight(type_a);
827                                 weight_b = service_type_weight(type_b);
828
829                                 if (weight_a > weight_b)
830                                         return -1;
831
832                                 if (weight_a < weight_b)
833                                         return 1;
834
835                                 return 0;
836                         }
837                 }
838
839                 if (type_a == bearer->service_type &&
840                                 type_b == bearer->service_type) {
841                         return 0;
842                 }
843
844                 if (type_a == bearer->service_type &&
845                                 type_b != bearer->service_type) {
846                         return -1;
847                 }
848
849                 if (type_a != bearer->service_type &&
850                                 type_b == bearer->service_type) {
851                         return 1;
852                 }
853         }
854
855         return 0;
856 }
857
858 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
859 {
860         struct service_entry *entry_a = (void *)a;
861         struct service_entry *entry_b = (void *)b;
862         struct connman_session *session = user_data;
863
864         return sort_allowed_bearers(entry_a->service, entry_b->service,
865                                 session);
866 }
867
868 static void free_session(struct connman_session *session)
869 {
870         destroy_policy_config(session);
871         connman_session_free_bearers(session->info->config.allowed_bearers);
872         g_free(session->owner);
873         g_free(session->session_path);
874         g_free(session->notify_path);
875         g_free(session->info);
876         g_free(session->info_last);
877
878         g_free(session);
879 }
880
881 static void cleanup_session(gpointer user_data)
882 {
883         struct connman_session *session = user_data;
884         struct session_info *info = session->info;
885
886         DBG("remove %s", session->session_path);
887
888         g_hash_table_destroy(session->service_hash);
889         g_sequence_free(session->service_list);
890
891         if (info->entry != NULL &&
892                         info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
893                 __connman_service_disconnect(info->entry->service);
894         }
895
896         free_session(session);
897 }
898
899 static enum connman_session_state service_to_session_state(enum connman_service_state state)
900 {
901         switch (state) {
902         case CONNMAN_SERVICE_STATE_UNKNOWN:
903         case CONNMAN_SERVICE_STATE_IDLE:
904         case CONNMAN_SERVICE_STATE_ASSOCIATION:
905         case CONNMAN_SERVICE_STATE_CONFIGURATION:
906         case CONNMAN_SERVICE_STATE_DISCONNECT:
907         case CONNMAN_SERVICE_STATE_FAILURE:
908                 break;
909         case CONNMAN_SERVICE_STATE_READY:
910                 return CONNMAN_SESSION_STATE_CONNECTED;
911         case CONNMAN_SERVICE_STATE_ONLINE:
912                 return CONNMAN_SESSION_STATE_ONLINE;
913         }
914
915         return CONNMAN_SESSION_STATE_DISCONNECTED;
916 }
917
918 static connman_bool_t is_connected(enum connman_service_state state)
919 {
920         switch (state) {
921         case CONNMAN_SERVICE_STATE_UNKNOWN:
922         case CONNMAN_SERVICE_STATE_IDLE:
923         case CONNMAN_SERVICE_STATE_ASSOCIATION:
924         case CONNMAN_SERVICE_STATE_CONFIGURATION:
925         case CONNMAN_SERVICE_STATE_DISCONNECT:
926         case CONNMAN_SERVICE_STATE_FAILURE:
927                 break;
928         case CONNMAN_SERVICE_STATE_READY:
929         case CONNMAN_SERVICE_STATE_ONLINE:
930                 return TRUE;
931         }
932
933         return FALSE;
934 }
935
936 static connman_bool_t is_connecting(enum connman_service_state state)
937 {
938         switch (state) {
939         case CONNMAN_SERVICE_STATE_UNKNOWN:
940         case CONNMAN_SERVICE_STATE_IDLE:
941                 break;
942         case CONNMAN_SERVICE_STATE_ASSOCIATION:
943         case CONNMAN_SERVICE_STATE_CONFIGURATION:
944                 return TRUE;
945         case CONNMAN_SERVICE_STATE_DISCONNECT:
946         case CONNMAN_SERVICE_STATE_FAILURE:
947         case CONNMAN_SERVICE_STATE_READY:
948         case CONNMAN_SERVICE_STATE_ONLINE:
949                 break;
950         }
951
952         return FALSE;
953 }
954
955 static connman_bool_t explicit_connect(enum connman_session_reason reason)
956 {
957         switch (reason) {
958         case CONNMAN_SESSION_REASON_UNKNOWN:
959         case CONNMAN_SESSION_REASON_FREE_RIDE:
960                 break;
961         case CONNMAN_SESSION_REASON_CONNECT:
962                 return TRUE;
963         }
964
965         return FALSE;
966 }
967
968 static connman_bool_t explicit_disconnect(struct session_info *info)
969 {
970         if (info->entry == NULL)
971                 return FALSE;
972
973         DBG("reason %s service %p state %d",
974                 reason2string(info->entry->reason),
975                 info->entry->service, info->entry->state);
976
977         if (info->entry->reason == CONNMAN_SESSION_REASON_UNKNOWN)
978                 return FALSE;
979
980         if (explicit_connect(info->entry->reason) == FALSE)
981                 return FALSE;
982
983         if (__connman_service_session_dec(info->entry->service) == FALSE)
984                 return FALSE;
985
986         return TRUE;
987 }
988
989 struct pending_data {
990         unsigned int timeout;
991         struct service_entry *entry;
992         gboolean (*cb)(gpointer);
993 };
994
995 static void pending_timeout_free(gpointer data, gpointer user_data)
996 {
997         struct pending_data *pending = data;
998
999         DBG("pending %p timeout %d", pending, pending->timeout);
1000         g_source_remove(pending->timeout);
1001         g_free(pending);
1002 }
1003
1004 static void pending_timeout_remove_all(struct service_entry *entry)
1005 {
1006         DBG("");
1007
1008         g_slist_foreach(entry->pending_timeouts, pending_timeout_free, NULL);
1009         g_slist_free(entry->pending_timeouts);
1010         entry->pending_timeouts = NULL;
1011 }
1012
1013 static gboolean pending_timeout_cb(gpointer data)
1014 {
1015         struct pending_data *pending = data;
1016         struct service_entry *entry = pending->entry;
1017         gboolean ret;
1018
1019         DBG("pending %p timeout %d", pending, pending->timeout);
1020
1021         ret = pending->cb(pending->entry);
1022         if (ret == FALSE) {
1023                 entry->pending_timeouts =
1024                         g_slist_remove(entry->pending_timeouts,
1025                                         pending);
1026                 g_free(pending);
1027         }
1028         return ret;
1029 }
1030
1031 static connman_bool_t pending_timeout_add(unsigned int seconds,
1032                                         gboolean (*cb)(gpointer),
1033                                         struct service_entry *entry)
1034 {
1035         struct pending_data *pending = g_try_new0(struct pending_data, 1);
1036
1037         if (pending == NULL || cb == NULL || entry == NULL) {
1038                 g_free(pending);
1039                 return FALSE;
1040         }
1041
1042         pending->cb = cb;
1043         pending->entry = entry;
1044         pending->timeout = g_timeout_add_seconds(seconds, pending_timeout_cb,
1045                                                 pending);
1046         entry->pending_timeouts = g_slist_prepend(entry->pending_timeouts,
1047                                                 pending);
1048
1049         DBG("pending %p entry %p timeout id %d", pending, entry,
1050                 pending->timeout);
1051
1052         return TRUE;
1053 }
1054
1055 static gboolean call_disconnect(gpointer user_data)
1056 {
1057         struct service_entry *entry = user_data;
1058         struct connman_service *service = entry->service;
1059
1060         /*
1061          * TODO: We should mark this entry as pending work. In case
1062          * disconnect fails we just unassign this session from the
1063          * service and can't do anything later on it
1064          */
1065         DBG("disconnect service %p", service);
1066         __connman_service_disconnect(service);
1067
1068         return FALSE;
1069 }
1070
1071 static gboolean call_connect(gpointer user_data)
1072 {
1073         struct service_entry *entry = user_data;
1074         struct connman_service *service = entry->service;
1075
1076         DBG("connect service %p", service);
1077         __connman_service_connect(service);
1078
1079         return FALSE;
1080 }
1081
1082 static void deselect_service(struct session_info *info)
1083 {
1084         struct service_entry *entry;
1085         connman_bool_t disconnect, connected;
1086
1087         DBG("");
1088
1089         if (info->entry == NULL)
1090                 return;
1091
1092         disconnect = explicit_disconnect(info);
1093
1094         connected = is_connecting(info->entry->state) == TRUE ||
1095                         is_connected(info->entry->state) == TRUE;
1096
1097         info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1098         info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1099
1100         entry = info->entry;
1101         info->entry = NULL;
1102
1103         DBG("disconnect %d connected %d", disconnect, connected);
1104
1105         if (disconnect == TRUE && connected == TRUE)
1106                 pending_timeout_add(0, call_disconnect, entry);
1107 }
1108
1109 static void deselect_and_disconnect(struct connman_session *session)
1110 {
1111         struct session_info *info = session->info;
1112
1113         deselect_service(info);
1114
1115         info->reason = CONNMAN_SESSION_REASON_FREE_RIDE;
1116 }
1117
1118 static void select_connected_service(struct session_info *info,
1119                                         struct service_entry *entry)
1120 {
1121         enum connman_session_state state;
1122
1123         state = service_to_session_state(entry->state);
1124         if (is_type_matching_state(&state, info->config.type) == FALSE)
1125                 return;
1126
1127         info->state = state;
1128
1129         info->entry = entry;
1130         info->entry->reason = info->reason;
1131
1132         if (explicit_connect(info->reason) == FALSE)
1133                 return;
1134
1135         __connman_service_session_inc(info->entry->service);
1136 }
1137
1138 static void select_offline_service(struct session_info *info,
1139                                         struct service_entry *entry)
1140 {
1141         if (explicit_connect(info->reason) == FALSE)
1142                 return;
1143
1144         info->state = service_to_session_state(entry->state);
1145
1146         info->entry = entry;
1147         info->entry->reason = info->reason;
1148
1149         __connman_service_session_inc(info->entry->service);
1150         pending_timeout_add(0, call_connect, entry);
1151 }
1152
1153 static void select_service(struct session_info *info,
1154                                 struct service_entry *entry)
1155 {
1156         DBG("service %p", entry->service);
1157
1158         if (is_connected(entry->state) == TRUE)
1159                 select_connected_service(info, entry);
1160         else
1161                 select_offline_service(info, entry);
1162 }
1163
1164 static void select_and_connect(struct connman_session *session,
1165                                 enum connman_session_reason reason)
1166 {
1167         struct session_info *info = session->info;
1168         struct service_entry *entry = NULL;
1169         GSequenceIter *iter;
1170
1171         DBG("session %p reason %s", session, reason2string(reason));
1172
1173         info->reason = reason;
1174
1175         iter = g_sequence_get_begin_iter(session->service_list);
1176
1177         while (g_sequence_iter_is_end(iter) == FALSE) {
1178                 entry = g_sequence_get(iter);
1179
1180                 switch (entry->state) {
1181                 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1182                 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1183                 case CONNMAN_SERVICE_STATE_READY:
1184                 case CONNMAN_SERVICE_STATE_ONLINE:
1185                 case CONNMAN_SERVICE_STATE_IDLE:
1186                 case CONNMAN_SERVICE_STATE_DISCONNECT:
1187                         select_service(info, entry);
1188                         return;
1189                 case CONNMAN_SERVICE_STATE_UNKNOWN:
1190                 case CONNMAN_SERVICE_STATE_FAILURE:
1191                         break;
1192                 }
1193
1194                 iter = g_sequence_iter_next(iter);
1195         }
1196 }
1197
1198 static struct service_entry *create_service_entry(struct connman_service *service,
1199                                         const char *name,
1200                                         enum connman_service_state state)
1201 {
1202         struct service_entry *entry;
1203         enum connman_service_type type;
1204         int idx;
1205
1206         entry = g_try_new0(struct service_entry, 1);
1207         if (entry == NULL)
1208                 return entry;
1209
1210         entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1211         entry->state = state;
1212         if (name != NULL)
1213                 entry->name = name;
1214         else
1215                 entry->name = "";
1216         entry->service = service;
1217
1218         idx = __connman_service_get_index(entry->service);
1219         entry->ifname = connman_inet_ifname(idx);
1220         if (entry->ifname == NULL)
1221                 entry->ifname = g_strdup("");
1222
1223         type = connman_service_get_type(entry->service);
1224         entry->bearer = service2bearer(type);
1225
1226         return entry;
1227 }
1228
1229 static void destroy_service_entry(gpointer data)
1230 {
1231         struct service_entry *entry = data;
1232
1233         pending_timeout_remove_all(entry);
1234         g_free(entry->ifname);
1235
1236         g_free(entry);
1237 }
1238
1239 static void populate_service_list(struct connman_session *session)
1240 {
1241         struct service_entry *entry;
1242         GSequenceIter *iter;
1243
1244         session->service_hash =
1245                 g_hash_table_new_full(g_direct_hash, g_direct_equal,
1246                                         NULL, NULL);
1247         session->service_list = __connman_service_get_list(session,
1248                                                         service_match,
1249                                                         create_service_entry,
1250                                                         destroy_service_entry);
1251
1252         g_sequence_sort(session->service_list, sort_services, session);
1253
1254         iter = g_sequence_get_begin_iter(session->service_list);
1255
1256         while (g_sequence_iter_is_end(iter) == FALSE) {
1257                 entry = g_sequence_get(iter);
1258
1259                 DBG("service %p type %s name %s", entry->service,
1260                         service2bearer(connman_service_get_type(entry->service)),
1261                         entry->name);
1262
1263                 g_hash_table_replace(session->service_hash,
1264                                         entry->service, iter);
1265
1266                 iter = g_sequence_iter_next(iter);
1267         }
1268 }
1269
1270 static void session_changed(struct connman_session *session,
1271                                 enum connman_session_trigger trigger)
1272 {
1273         struct session_info *info = session->info;
1274         struct session_info *info_last = session->info_last;
1275         GSequenceIter *service_iter = NULL, *service_iter_last = NULL;
1276         GSequence *service_list_last;
1277         GHashTable *service_hash_last;
1278
1279         /*
1280          * TODO: This only a placeholder for the 'real' algorithm to
1281          * play a bit around. So we are going to improve it step by step.
1282          */
1283
1284         DBG("session %p trigger %s reason %s", session, trigger2string(trigger),
1285                                                 reason2string(info->reason));
1286
1287         if (info->entry != NULL) {
1288                 enum connman_session_state state;
1289
1290                 state = service_to_session_state(info->entry->state);
1291
1292                 if (is_type_matching_state(&state, info->config.type) == TRUE)
1293                         info->state = state;
1294         }
1295
1296         switch (trigger) {
1297         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
1298                 DBG("ignore session changed event");
1299                 return;
1300         case CONNMAN_SESSION_TRIGGER_SETTING:
1301                 if (info->config.allowed_bearers != info_last->config.allowed_bearers) {
1302
1303                         service_hash_last = session->service_hash;
1304                         service_list_last = session->service_list;
1305
1306                         populate_service_list(session);
1307
1308                         if (info->entry != NULL) {
1309                                 service_iter_last = g_hash_table_lookup(
1310                                                         service_hash_last,
1311                                                         info->entry->service);
1312                                 service_iter = g_hash_table_lookup(
1313                                                         session->service_hash,
1314                                                         info->entry->service);
1315                         }
1316
1317                         if (service_iter == NULL && service_iter_last != NULL) {
1318                                 /*
1319                                  * The currently selected service is
1320                                  * not part of this session anymore.
1321                                  */
1322                                 deselect_and_disconnect(session);
1323                         }
1324
1325                         g_hash_table_remove_all(service_hash_last);
1326                         g_sequence_free(service_list_last);
1327                 }
1328
1329                 if (info->config.type != info_last->config.type) {
1330                         if (info->state >= CONNMAN_SESSION_STATE_CONNECTED &&
1331                                         is_type_matching_state(&info->state,
1332                                                         info->config.type) == FALSE)
1333                                 deselect_and_disconnect(session);
1334                 }
1335
1336                 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED) {
1337                         select_and_connect(session,
1338                                         CONNMAN_SESSION_REASON_FREE_RIDE);
1339                 }
1340
1341                 break;
1342         case CONNMAN_SESSION_TRIGGER_ECALL:
1343                 /*
1344                  * For the time beeing we fallback to normal connect
1345                  * strategy.
1346                  */
1347         case CONNMAN_SESSION_TRIGGER_CONNECT:
1348                 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED) {
1349                         if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
1350                                 break;
1351                         info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
1352                         __connman_service_session_inc(info->entry->service);
1353                         break;
1354                 }
1355
1356                 if (info->entry != NULL &&
1357                                 is_connecting(info->entry->state) == TRUE) {
1358                         break;
1359                 }
1360
1361                 select_and_connect(session,
1362                                 CONNMAN_SESSION_REASON_CONNECT);
1363
1364                 break;
1365         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
1366                 deselect_and_disconnect(session);
1367
1368                 break;
1369         case CONNMAN_SESSION_TRIGGER_SERVICE:
1370                 if (info->entry != NULL &&
1371                         (is_connecting(info->entry->state) == TRUE ||
1372                                 is_connected(info->entry->state) == TRUE)) {
1373                         break;
1374                 }
1375
1376                 deselect_and_disconnect(session);
1377
1378                 if (info->reason == CONNMAN_SESSION_REASON_FREE_RIDE) {
1379                         select_and_connect(session, info->reason);
1380                 }
1381
1382                 break;
1383         }
1384
1385         session_notify(session);
1386 }
1387
1388 static DBusMessage *connect_session(DBusConnection *conn,
1389                                         DBusMessage *msg, void *user_data)
1390 {
1391         struct connman_session *session = user_data;
1392
1393         DBG("session %p", session);
1394
1395         if (ecall_session != NULL) {
1396                 if (ecall_session->ecall == TRUE && ecall_session != session)
1397                         return __connman_error_failed(msg, EBUSY);
1398
1399                 session->ecall = TRUE;
1400                 session_changed(session, CONNMAN_SESSION_TRIGGER_ECALL);
1401         } else
1402                 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1403
1404         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1405 }
1406
1407 static DBusMessage *disconnect_session(DBusConnection *conn,
1408                                         DBusMessage *msg, void *user_data)
1409 {
1410         struct connman_session *session = user_data;
1411
1412         DBG("session %p", session);
1413
1414         if (ecall_session != NULL) {
1415                 if (ecall_session->ecall == TRUE && ecall_session != session)
1416                         return __connman_error_failed(msg, EBUSY);
1417
1418                 session->ecall = FALSE;
1419         }
1420
1421         session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1422
1423         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1424 }
1425
1426 static DBusMessage *change_session(DBusConnection *conn,
1427                                         DBusMessage *msg, void *user_data)
1428 {
1429         struct connman_session *session = user_data;
1430         struct session_info *info = session->info;
1431         DBusMessageIter iter, value;
1432         const char *name;
1433         const char *val;
1434         GSList *allowed_bearers;
1435         int err;
1436
1437         DBG("session %p", session);
1438         if (dbus_message_iter_init(msg, &iter) == FALSE)
1439                 return __connman_error_invalid_arguments(msg);
1440
1441         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1442                 return __connman_error_invalid_arguments(msg);
1443
1444         dbus_message_iter_get_basic(&iter, &name);
1445         dbus_message_iter_next(&iter);
1446
1447         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1448                 return __connman_error_invalid_arguments(msg);
1449
1450         dbus_message_iter_recurse(&iter, &value);
1451
1452         switch (dbus_message_iter_get_arg_type(&value)) {
1453         case DBUS_TYPE_ARRAY:
1454                 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1455                         err = session_parse_allowed_bearers(&value,
1456                                                         &allowed_bearers);
1457                         if (err < 0)
1458                                 return __connman_error_failed(msg, err);
1459
1460                         connman_session_free_bearers(info->config.allowed_bearers);
1461                         err = apply_policy_on_bearers(
1462                                         session->policy_config->allowed_bearers,
1463                                         allowed_bearers,
1464                                         &info->config.allowed_bearers);
1465
1466                         connman_session_free_bearers(allowed_bearers);
1467                         if (err < 0)
1468                                 return __connman_error_failed(msg, err);
1469                 } else {
1470                         goto err;
1471                 }
1472                 break;
1473         case DBUS_TYPE_STRING:
1474                 if (g_str_equal(name, "ConnectionType") == TRUE) {
1475                         dbus_message_iter_get_basic(&value, &val);
1476                         info->config.type = apply_policy_on_type(
1477                                 session->policy_config->type,
1478                                 string2type(val));
1479                 } else {
1480                         goto err;
1481                 }
1482                 break;
1483         default:
1484                 goto err;
1485         }
1486
1487         session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1488
1489         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1490
1491 err:
1492         return __connman_error_invalid_arguments(msg);
1493 }
1494
1495 static void release_session(gpointer key, gpointer value, gpointer user_data)
1496 {
1497         struct connman_session *session = value;
1498         DBusMessage *message;
1499
1500         DBG("owner %s path %s", session->owner, session->notify_path);
1501
1502         if (session->notify_watch > 0)
1503                 g_dbus_remove_watch(connection, session->notify_watch);
1504
1505         g_dbus_unregister_interface(connection, session->session_path,
1506                                                 CONNMAN_SESSION_INTERFACE);
1507
1508         message = dbus_message_new_method_call(session->owner,
1509                                                 session->notify_path,
1510                                                 CONNMAN_NOTIFICATION_INTERFACE,
1511                                                 "Release");
1512         if (message == NULL)
1513                 return;
1514
1515         dbus_message_set_no_reply(message, TRUE);
1516
1517         g_dbus_send_message(connection, message);
1518 }
1519
1520 static int session_disconnect(struct connman_session *session)
1521 {
1522         DBG("session %p, %s", session, session->owner);
1523
1524         if (session->notify_watch > 0)
1525                 g_dbus_remove_watch(connection, session->notify_watch);
1526
1527         g_dbus_unregister_interface(connection, session->session_path,
1528                                                 CONNMAN_SESSION_INTERFACE);
1529
1530         deselect_and_disconnect(session);
1531
1532         g_hash_table_remove(session_hash, session->session_path);
1533
1534         return 0;
1535 }
1536
1537 static void owner_disconnect(DBusConnection *conn, void *user_data)
1538 {
1539         struct connman_session *session = user_data;
1540
1541         DBG("session %p, %s died", session, session->owner);
1542
1543         session_disconnect(session);
1544 }
1545
1546 static DBusMessage *destroy_session(DBusConnection *conn,
1547                                         DBusMessage *msg, void *user_data)
1548 {
1549         struct connman_session *session = user_data;
1550
1551         DBG("session %p", session);
1552
1553         if (ecall_session != NULL && ecall_session != session)
1554                 return __connman_error_failed(msg, EBUSY);
1555
1556         session_disconnect(session);
1557
1558         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1559 }
1560
1561 static const GDBusMethodTable session_methods[] = {
1562         { GDBUS_METHOD("Destroy", NULL, NULL, destroy_session) },
1563         { GDBUS_METHOD("Connect", NULL, NULL, connect_session) },
1564         { GDBUS_METHOD("Disconnect", NULL, NULL,
1565                         disconnect_session ) },
1566         { GDBUS_METHOD("Change",
1567                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1568                         NULL, change_session) },
1569         { },
1570 };
1571
1572 static void session_create_cb(struct connman_session *session,
1573                                 struct connman_session_config *config,
1574                                 void *user_data, int err)
1575 {
1576         DBG("session %p config %p", session, config);
1577
1578         if (err != 0)
1579                 return;
1580
1581         session->policy_config = config;
1582 }
1583
1584 int __connman_session_create(DBusMessage *msg)
1585 {
1586         const char *owner, *notify_path;
1587         char *session_path = NULL;
1588         DBusMessageIter iter, array;
1589         struct connman_session *session = NULL;
1590         struct session_info *info, *info_last;
1591         enum connman_session_type type = CONNMAN_SESSION_TYPE_ANY;
1592         GSList *allowed_bearers = NULL;
1593         connman_bool_t allowed_bearers_valid = FALSE;
1594         connman_bool_t type_valid = FALSE;
1595         int err;
1596
1597         owner = dbus_message_get_sender(msg);
1598
1599         DBG("owner %s", owner);
1600
1601         if (ecall_session != NULL && ecall_session->ecall == TRUE) {
1602                 /*
1603                  * If there is an emergency call already going on,
1604                  * ignore session creation attempt
1605                  */
1606                 err = -EBUSY;
1607                 goto err;
1608         }
1609
1610         dbus_message_iter_init(msg, &iter);
1611         dbus_message_iter_recurse(&iter, &array);
1612
1613         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1614                 DBusMessageIter entry, value;
1615                 const char *key, *val;
1616
1617                 dbus_message_iter_recurse(&array, &entry);
1618                 dbus_message_iter_get_basic(&entry, &key);
1619
1620                 dbus_message_iter_next(&entry);
1621                 dbus_message_iter_recurse(&entry, &value);
1622
1623                 switch (dbus_message_iter_get_arg_type(&value)) {
1624                 case DBUS_TYPE_ARRAY:
1625                         if (g_str_equal(key, "AllowedBearers") == TRUE) {
1626                                 err = session_parse_allowed_bearers(&value,
1627                                                         &allowed_bearers);
1628                                 if (err < 0)
1629                                         goto err;
1630
1631                                 allowed_bearers_valid = TRUE;
1632                         } else {
1633                                 return -EINVAL;
1634                         }
1635                         break;
1636                 case DBUS_TYPE_STRING:
1637                         if (g_str_equal(key, "ConnectionType") == TRUE) {
1638                                 dbus_message_iter_get_basic(&value, &val);
1639                                 type = string2type(val);
1640                                 type_valid = TRUE;
1641                         } else {
1642                                 return -EINVAL;
1643                         }
1644                 }
1645                 dbus_message_iter_next(&array);
1646         }
1647
1648         dbus_message_iter_next(&iter);
1649         dbus_message_iter_get_basic(&iter, &notify_path);
1650
1651         if (notify_path == NULL) {
1652                 err = -EINVAL;
1653                 goto err;
1654         }
1655
1656         session_path = g_strdup_printf("/sessions%s", notify_path);
1657         if (session_path == NULL) {
1658                 err = -ENOMEM;
1659                 goto err;
1660         }
1661
1662         session = g_hash_table_lookup(session_hash, session_path);
1663         if (session != NULL) {
1664                 session = NULL;
1665                 err = -EEXIST;
1666                 goto err;
1667         }
1668
1669         session = g_try_new0(struct connman_session, 1);
1670         if (session == NULL) {
1671                 err = -ENOMEM;
1672                 goto err;
1673         }
1674
1675         session->info = g_try_new0(struct session_info, 1);
1676         if (session->info == NULL) {
1677                 err = -ENOMEM;
1678                 goto err;
1679         }
1680
1681         session->info_last = g_try_new0(struct session_info, 1);
1682         if (session->info_last == NULL) {
1683                 err = -ENOMEM;
1684                 goto err;
1685         }
1686
1687         info = session->info;
1688         info_last = session->info_last;
1689
1690         session->owner = g_strdup(owner);
1691         session->session_path = session_path;
1692         session->notify_path = g_strdup(notify_path);
1693         session->notify_watch =
1694                 g_dbus_add_disconnect_watch(connection, session->owner,
1695                                         owner_disconnect, session, NULL);
1696
1697         err = assign_policy_plugin(session);
1698         if (err < 0)
1699                 goto err;
1700         err = create_policy_config(session, session_create_cb, NULL);
1701         if (err < 0)
1702                 goto err;
1703
1704         if (session->policy_config->ecall == TRUE)
1705                 ecall_session = session;
1706
1707         info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1708         if (type_valid == FALSE)
1709                 type = CONNMAN_SESSION_TYPE_ANY;
1710         info->config.type = apply_policy_on_type(
1711                                 session->policy_config->type,
1712                                 type);
1713         info->config.priority = session->policy_config->priority;
1714         info->config.roaming_policy = session->policy_config->roaming_policy;
1715         info->entry = NULL;
1716
1717         if (allowed_bearers_valid == FALSE) {
1718                 allowed_bearers = connman_session_allowed_bearers_any();
1719                 if (allowed_bearers == NULL) {
1720                         err = -ENOMEM;
1721                         goto err;
1722                 }
1723         }
1724
1725         err = apply_policy_on_bearers(
1726                         session->policy_config->allowed_bearers,
1727                         allowed_bearers,
1728                         &info->config.allowed_bearers);
1729         if (err < 0)
1730                 goto err;
1731
1732         g_hash_table_replace(session_hash, session->session_path, session);
1733
1734         DBG("add %s", session->session_path);
1735
1736         if (g_dbus_register_interface(connection, session->session_path,
1737                                         CONNMAN_SESSION_INTERFACE,
1738                                         session_methods, NULL,
1739                                         NULL, session, NULL) == FALSE) {
1740                 connman_error("Failed to register %s", session->session_path);
1741                 g_hash_table_remove(session_hash, session->session_path);
1742                 session = NULL;
1743
1744                 err = -EINVAL;
1745                 goto err;
1746         }
1747
1748         g_dbus_send_reply(connection, msg,
1749                                 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1750                                 DBUS_TYPE_INVALID);
1751
1752
1753         populate_service_list(session);
1754
1755         info_last->state = info->state;
1756         info_last->config.priority = info->config.priority;
1757         info_last->config.roaming_policy = info->config.roaming_policy;
1758         info_last->entry = info->entry;
1759         info_last->config.allowed_bearers = info->config.allowed_bearers;
1760
1761         session->append_all = TRUE;
1762
1763         session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1764
1765         return 0;
1766
1767 err:
1768         connman_error("Failed to create session");
1769
1770         if (session != NULL) {
1771                 if (session->info_last != NULL)
1772                         g_free(session->info_last);
1773                 if (session->info != NULL)
1774                         g_free(session->info);
1775                 g_free(session);
1776         }
1777
1778         g_free(session_path);
1779
1780         connman_session_free_bearers(allowed_bearers);
1781
1782         return err;
1783 }
1784
1785 int __connman_session_destroy(DBusMessage *msg)
1786 {
1787         const char *owner, *session_path;
1788         struct connman_session *session;
1789
1790         owner = dbus_message_get_sender(msg);
1791
1792         DBG("owner %s", owner);
1793
1794         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1795                                                         DBUS_TYPE_INVALID);
1796         if (session_path == NULL)
1797                 return -EINVAL;
1798
1799         session = g_hash_table_lookup(session_hash, session_path);
1800         if (session == NULL)
1801                 return -EINVAL;
1802
1803         if (g_strcmp0(owner, session->owner) != 0)
1804                 return -EACCES;
1805
1806         session_disconnect(session);
1807
1808         return 0;
1809 }
1810
1811 connman_bool_t __connman_session_mode()
1812 {
1813         return sessionmode;
1814 }
1815
1816 void __connman_session_set_mode(connman_bool_t enable)
1817 {
1818         DBG("enable %d", enable);
1819
1820         if (sessionmode != enable) {
1821                 sessionmode = enable;
1822
1823                 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
1824                                 CONNMAN_MANAGER_INTERFACE, "SessionMode",
1825                                 DBUS_TYPE_BOOLEAN, &sessionmode);
1826         }
1827
1828         if (sessionmode == TRUE)
1829                 __connman_service_disconnect_all();
1830 }
1831
1832 static void service_add(struct connman_service *service,
1833                         const char *name)
1834 {
1835         GHashTableIter iter;
1836         GSequenceIter *iter_service_list;
1837         gpointer key, value;
1838         struct connman_session *session;
1839         struct service_entry *entry;
1840
1841         DBG("service %p", service);
1842
1843         g_hash_table_iter_init(&iter, session_hash);
1844
1845         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1846                 session = value;
1847
1848                 if (service_match(session, service) == FALSE)
1849                         continue;
1850
1851                 entry = create_service_entry(service, name,
1852                                                 CONNMAN_SERVICE_STATE_IDLE);
1853                 if (entry == NULL)
1854                         continue;
1855
1856                 iter_service_list =
1857                         g_sequence_insert_sorted(session->service_list,
1858                                                         entry, sort_services,
1859                                                         session);
1860
1861                 g_hash_table_replace(session->service_hash, service,
1862                                         iter_service_list);
1863
1864                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1865         }
1866 }
1867
1868 static void service_remove(struct connman_service *service)
1869 {
1870
1871         GHashTableIter iter;
1872         gpointer key, value;
1873         struct connman_session *session;
1874         struct session_info *info;
1875
1876         DBG("service %p", service);
1877
1878         g_hash_table_iter_init(&iter, session_hash);
1879
1880         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1881                 GSequenceIter *seq_iter;
1882                 session = value;
1883                 info = session->info;
1884
1885                 seq_iter = g_hash_table_lookup(session->service_hash, service);
1886                 if (seq_iter == NULL)
1887                         continue;
1888
1889                 g_sequence_remove(seq_iter);
1890
1891                 if (info->entry != NULL && info->entry->service == service)
1892                         info->entry = NULL;
1893                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1894         }
1895 }
1896
1897 static void service_state_changed(struct connman_service *service,
1898                                         enum connman_service_state state)
1899 {
1900         GHashTableIter iter;
1901         gpointer key, value;
1902
1903         DBG("service %p state %d", service, state);
1904
1905         g_hash_table_iter_init(&iter, session_hash);
1906
1907         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1908                 struct connman_session *session = value;
1909                 GSequenceIter *service_iter;
1910
1911                 service_iter = g_hash_table_lookup(session->service_hash, service);
1912                 if (service_iter != NULL) {
1913                         struct service_entry *entry;
1914
1915                         entry = g_sequence_get(service_iter);
1916                         entry->state = state;
1917                 }
1918
1919                 session_changed(session,
1920                                 CONNMAN_SESSION_TRIGGER_SERVICE);
1921         }
1922 }
1923
1924 static void ipconfig_changed(struct connman_service *service,
1925                                 struct connman_ipconfig *ipconfig)
1926 {
1927         GHashTableIter iter;
1928         gpointer key, value;
1929         struct connman_session *session;
1930         struct session_info *info;
1931         enum connman_ipconfig_type type;
1932
1933         DBG("service %p ipconfig %p", service, ipconfig);
1934
1935         type = __connman_ipconfig_get_config_type(ipconfig);
1936
1937         g_hash_table_iter_init(&iter, session_hash);
1938
1939         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1940                 session = value;
1941                 info = session->info;
1942
1943                 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
1944                         continue;
1945
1946                 if (info->entry != NULL && info->entry->service == service) {
1947                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1948                                 ipconfig_ipv4_changed(session);
1949                         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1950                                 ipconfig_ipv6_changed(session);
1951                 }
1952         }
1953 }
1954
1955 static struct connman_notifier session_notifier = {
1956         .name                   = "session",
1957         .service_add            = service_add,
1958         .service_remove         = service_remove,
1959         .service_state_changed  = service_state_changed,
1960         .ipconfig_changed       = ipconfig_changed,
1961 };
1962
1963 int __connman_session_init(void)
1964 {
1965         int err;
1966
1967         DBG("");
1968
1969         connection = connman_dbus_get_connection();
1970         if (connection == NULL)
1971                 return -1;
1972
1973         err = connman_notifier_register(&session_notifier);
1974         if (err < 0) {
1975                 dbus_connection_unref(connection);
1976                 return err;
1977         }
1978
1979         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1980                                                 NULL, cleanup_session);
1981
1982         sessionmode = FALSE;
1983         return 0;
1984 }
1985
1986 void __connman_session_cleanup(void)
1987 {
1988         DBG("");
1989
1990         if (connection == NULL)
1991                 return;
1992
1993         connman_notifier_unregister(&session_notifier);
1994
1995         g_hash_table_foreach(session_hash, release_session, NULL);
1996         g_hash_table_destroy(session_hash);
1997         session_hash = NULL;
1998
1999         dbus_connection_unref(connection);
2000 }