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