session: Track reason on a session level
[framework/connectivity/connman.git] / src / session.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2011  BWM CarIT GmbH. All rights reserved.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <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         const 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                 break;
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 (explicit_connect(info->entry->reason) == FALSE)
742                 return FALSE;
743
744         if (__connman_service_session_dec(info->entry->service) == FALSE)
745                 return FALSE;
746
747         if (ecall_session != NULL && ecall_session != session)
748                 return FALSE;
749
750         return TRUE;
751 }
752
753 static void test_and_disconnect(struct connman_session *session)
754 {
755         struct session_info *info = session->info;
756         struct service_entry *entry;
757         connman_bool_t disconnect;
758
759         disconnect = explicit_disconnect(session);
760
761         entry = info->entry;
762
763         info->online = FALSE;
764         info->reason = CONNMAN_SESSION_REASON_UNKNOWN;
765         info->entry = NULL;
766
767         if (disconnect == TRUE) {
768                 DBG("disconnect service %p", entry->service);
769
770                 /*
771                  * TODO: We should mark this entry as pending work. In case
772                  * disconnect fails we just unassign this session from the
773                  * service and can't do anything later on it
774                  */
775                 __connman_service_disconnect(entry->service);
776         }
777 }
778
779 static void select_and_connect(struct connman_session *session,
780                                 enum connman_session_reason reason)
781 {
782         struct session_info *info = session->info;
783         struct service_entry *entry = NULL;
784         GSequenceIter *iter;
785         connman_bool_t do_connect = FALSE;
786
787         DBG("session %p reason %s", session, reason2string(reason));
788
789         info->reason = reason;
790
791         iter = g_sequence_get_begin_iter(session->service_list);
792
793         while (g_sequence_iter_is_end(iter) == FALSE) {
794                 entry = g_sequence_get(iter);
795
796                 switch (entry->state) {
797                 case CONNMAN_SERVICE_STATE_ASSOCIATION:
798                 case CONNMAN_SERVICE_STATE_CONFIGURATION:
799                 case CONNMAN_SERVICE_STATE_READY:
800                 case CONNMAN_SERVICE_STATE_ONLINE:
801                         /* connecting or connected */
802                         break;
803                 case CONNMAN_SERVICE_STATE_IDLE:
804                 case CONNMAN_SERVICE_STATE_DISCONNECT:
805                         if (explicit_connect(reason) == TRUE)
806                                 do_connect = TRUE;
807                         break;
808                 case CONNMAN_SERVICE_STATE_UNKNOWN:
809                 case CONNMAN_SERVICE_STATE_FAILURE:
810                         entry = NULL;
811                         break;
812                 }
813
814                 if (entry != NULL)
815                         break;
816
817                 iter = g_sequence_iter_next(iter);
818         }
819
820         if (info->entry != NULL && info->entry != entry)
821                 test_and_disconnect(session);
822
823         if (entry != NULL) {
824                 info->entry = entry;
825                 info->entry->reason = reason;
826
827                 if (explicit_connect(reason) == TRUE)
828                         __connman_service_session_inc(info->entry->service);
829
830                 if (do_connect == TRUE)
831                         __connman_service_connect(info->entry->service);
832                 else
833                         info->online = is_online(entry->state);
834         }
835 }
836
837 static void session_changed(struct connman_session *session,
838                                 enum connman_session_trigger trigger)
839 {
840         struct session_info *info = session->info;
841         struct session_info *info_last = session->info_last;
842         GSequenceIter *iter;
843
844         /*
845          * TODO: This only a placeholder for the 'real' algorithm to
846          * play a bit around. So we are going to improve it step by step.
847          */
848
849         DBG("session %p trigger %s", session, trigger2string(trigger));
850
851         switch (trigger) {
852         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
853                 DBG("ignore session changed event");
854                 return;
855         case CONNMAN_SESSION_TRIGGER_SETTING:
856                 if (info->entry != NULL) {
857                         iter = g_hash_table_lookup(session->service_hash,
858                                                         info->entry->service);
859                         if (iter == NULL) {
860                                 /*
861                                  * This service is not part of this
862                                  * session anymore.
863                                  */
864                                 test_and_disconnect(session);
865                         }
866                 }
867
868                 if (info->online == FALSE) {
869                         select_and_connect(session,
870                                         CONNMAN_SESSION_REASON_FREE_RIDE);
871                 }
872
873                 break;
874         case CONNMAN_SESSION_TRIGGER_CONNECT:
875                 if (info->online == TRUE) {
876                         info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
877                         __connman_service_session_inc(info->entry->service);
878                         break;
879                 }
880
881                 select_and_connect(session,
882                                 CONNMAN_SESSION_REASON_CONNECT);
883
884                 break;
885         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
886                 test_and_disconnect(session);
887
888                 break;
889         case CONNMAN_SESSION_TRIGGER_PERIODIC:
890                 if (info->online == TRUE) {
891                         info->entry->reason = CONNMAN_SESSION_REASON_PERIODIC;
892                         __connman_service_session_inc(info->entry->service);
893                         break;
894                 }
895
896                 select_and_connect(session,
897                                 CONNMAN_SESSION_REASON_PERIODIC);
898
899                 break;
900         case CONNMAN_SESSION_TRIGGER_SERVICE:
901                 if (info->online == TRUE)
902                         break;
903
904                 if (info->entry != NULL &&
905                                 is_connecting(info->entry->state) == TRUE) {
906                         break;
907                 }
908
909                 if (info->reason == CONNMAN_SESSION_REASON_CONNECT) {
910                         DBG("Retry to find a matching session");
911                         /*
912                          * The user called Connect() but there was no
913                          * matching session available at this point.
914                          * Now there might be a new one. Let's retry
915                          * to select and connect
916                          */
917                         select_and_connect(session,
918                                         CONNMAN_SESSION_REASON_CONNECT);
919
920                         break;
921                 }
922
923                 if (info->stay_connected == TRUE) {
924                         DBG("StayConnected");
925                         select_and_connect(session,
926                                         CONNMAN_SESSION_REASON_CONNECT);
927
928                         break;
929                 }
930
931                 select_and_connect(session,
932                                 CONNMAN_SESSION_REASON_FREE_RIDE);
933
934                 break;
935         case CONNMAN_SESSION_TRIGGER_ECALL:
936                 if (info->online == FALSE && info->entry->service != NULL)
937                         test_and_disconnect(session);
938
939                 break;
940         }
941
942         if (info->entry != info_last->entry)
943                 session->info_dirty = TRUE;
944
945         session_notify(session);
946 }
947
948 static DBusMessage *connect_session(DBusConnection *conn,
949                                         DBusMessage *msg, void *user_data)
950 {
951         struct connman_session *session = user_data;
952
953         DBG("session %p", session);
954
955         if (ecall_session != NULL && ecall_session != session)
956                 return __connman_error_failed(msg, EBUSY);
957
958         session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
959
960         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
961 }
962
963 static DBusMessage *disconnect_session(DBusConnection *conn,
964                                         DBusMessage *msg, void *user_data)
965 {
966         struct connman_session *session = user_data;
967
968         DBG("session %p", session);
969
970         if (ecall_session != NULL && ecall_session != session)
971                 return __connman_error_failed(msg, EBUSY);
972
973         session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
974
975         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
976 }
977
978 static struct service_entry *create_service_entry(struct connman_service *service,
979                                         const char *name,
980                                         enum connman_service_state state)
981 {
982         struct service_entry *entry;
983         enum connman_service_type type;
984         int idx;
985
986         entry = g_try_new0(struct service_entry, 1);
987         if (entry == NULL)
988                 return entry;
989
990         entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
991         entry->state = state;
992         entry->name = name;
993         entry->service = service;
994
995         idx = __connman_service_get_index(entry->service);
996         entry->ifname = connman_inet_ifname(idx);
997         if (entry->ifname == NULL)
998                 entry->ifname = "";
999
1000         type = connman_service_get_type(entry->service);
1001         entry->bearer = service2bearer(type);
1002
1003         return entry;
1004 }
1005
1006 static void destroy_service_entry(gpointer data)
1007 {
1008         struct service_entry *entry = data;
1009
1010         g_free(entry);
1011 }
1012
1013 static void update_allowed_bearers(struct connman_session *session)
1014 {
1015         struct service_entry *entry;
1016         GSequenceIter *iter;
1017
1018         if (session->service_list != NULL) {
1019                 g_hash_table_remove_all(session->service_hash);
1020                 g_sequence_free(session->service_list);
1021         }
1022
1023         session->service_list = __connman_service_get_list(session,
1024                                                         service_match,
1025                                                         create_service_entry,
1026                                                         destroy_service_entry);
1027
1028         g_sequence_sort(session->service_list, sort_services, session);
1029
1030         iter = g_sequence_get_begin_iter(session->service_list);
1031
1032         while (g_sequence_iter_is_end(iter) == FALSE) {
1033                 entry = g_sequence_get(iter);
1034
1035                 DBG("service %p type %s name %s", entry->service,
1036                         service2bearer(connman_service_get_type(entry->service)),
1037                         entry->name);
1038
1039                 g_hash_table_replace(session->service_hash,
1040                                         entry->service, iter);
1041
1042                 iter = g_sequence_iter_next(iter);
1043         }
1044
1045         session->info_dirty = TRUE;
1046 }
1047
1048 static void update_ecall_sessions(struct connman_session *session)
1049 {
1050         struct session_info *info = session->info;
1051         struct connman_session *session_iter;
1052         GHashTableIter iter;
1053         gpointer key, value;
1054
1055         g_hash_table_iter_init(&iter, session_hash);
1056
1057         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1058                 session_iter = value;
1059
1060                 if (session_iter == session)
1061                         continue;
1062
1063                 session_iter->info->ecall = info->ecall;
1064                 session_iter->info_dirty = TRUE;
1065
1066                 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
1067         }
1068 }
1069
1070 static void update_ecall(struct connman_session *session)
1071 {
1072         struct session_info *info = session->info;
1073         struct session_info *info_last = session->info_last;
1074
1075         DBG("session %p ecall_session %p ecall %d -> %d", session,
1076                 ecall_session, info_last->ecall, info->ecall);
1077
1078         if (ecall_session == NULL) {
1079                 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
1080                         goto err;
1081
1082                 ecall_session = session;
1083         } else if (ecall_session == session) {
1084                 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
1085                         goto err;
1086
1087                 ecall_session = NULL;
1088         } else {
1089                 goto err;
1090         }
1091
1092         update_ecall_sessions(session);
1093
1094         session->info_dirty = TRUE;
1095         return;
1096
1097 err:
1098         /* not a valid transition */
1099         info->ecall = info_last->ecall;
1100 }
1101
1102 static DBusMessage *change_session(DBusConnection *conn,
1103                                         DBusMessage *msg, void *user_data)
1104 {
1105         struct connman_session *session = user_data;
1106         struct session_info *info = session->info;
1107         struct session_info *info_last = session->info_last;
1108         DBusMessageIter iter, value;
1109         const char *name;
1110         GSList *allowed_bearers;
1111
1112         DBG("session %p", session);
1113         if (dbus_message_iter_init(msg, &iter) == FALSE)
1114                 return __connman_error_invalid_arguments(msg);
1115
1116         dbus_message_iter_get_basic(&iter, &name);
1117         dbus_message_iter_next(&iter);
1118         dbus_message_iter_recurse(&iter, &value);
1119
1120         switch (dbus_message_iter_get_arg_type(&value)) {
1121         case DBUS_TYPE_ARRAY:
1122                 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1123                         allowed_bearers = session_parse_allowed_bearers(&value);
1124
1125                         g_slist_foreach(info->allowed_bearers,
1126                                         cleanup_bearer_info, NULL);
1127                         g_slist_free(info->allowed_bearers);
1128
1129                         if (allowed_bearers == NULL) {
1130                                 allowed_bearers = session_allowed_bearers_any();
1131
1132                                 if (allowed_bearers == NULL)
1133                                         return __connman_error_failed(msg, ENOMEM);
1134                         }
1135
1136                         info->allowed_bearers = allowed_bearers;
1137
1138                         update_allowed_bearers(session);
1139                 } else {
1140                         goto err;
1141                 }
1142                 break;
1143         case DBUS_TYPE_BOOLEAN:
1144                 if (g_str_equal(name, "Priority") == TRUE) {
1145                         dbus_message_iter_get_basic(&value,
1146                                         &info->priority);
1147
1148                         if (info_last->priority != info->priority)
1149                                 session->info_dirty = TRUE;
1150                 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1151                         dbus_message_iter_get_basic(&value,
1152                                         &info->avoid_handover);
1153
1154                         if (info_last->avoid_handover != info->avoid_handover)
1155                                 session->info_dirty = TRUE;
1156                 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1157                         dbus_message_iter_get_basic(&value,
1158                                         &info->stay_connected);
1159
1160                         if (info_last->stay_connected != info->stay_connected)
1161                                 session->info_dirty = TRUE;
1162                 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1163                         dbus_message_iter_get_basic(&value,
1164                                         &info->ecall);
1165
1166                         update_ecall(session);
1167                 } else {
1168                         goto err;
1169                 }
1170                 break;
1171         case DBUS_TYPE_UINT32:
1172                 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1173                         dbus_message_iter_get_basic(&value,
1174                                         &info->periodic_connect);
1175
1176                         if (info_last->periodic_connect != info->periodic_connect)
1177                                 session->info_dirty = TRUE;
1178                 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1179                         dbus_message_iter_get_basic(&value,
1180                                         &info->idle_timeout);
1181
1182                         if (info_last->idle_timeout != info->idle_timeout)
1183                                 session->info_dirty = TRUE;
1184                 } else {
1185                         goto err;
1186                 }
1187                 break;
1188         case DBUS_TYPE_STRING:
1189                 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1190                         const char *val;
1191                         dbus_message_iter_get_basic(&value, &val);
1192                         info->roaming_policy =
1193                                         string2roamingpolicy(val);
1194
1195                         if (info_last->roaming_policy != info->roaming_policy)
1196                                 session->info_dirty = TRUE;
1197                 } else {
1198                         goto err;
1199                 }
1200                 break;
1201         default:
1202                 goto err;
1203         }
1204
1205         if (session->info_dirty == TRUE)
1206                 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1207
1208         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1209
1210 err:
1211         return __connman_error_invalid_arguments(msg);
1212 }
1213
1214 static void release_session(gpointer key, gpointer value, gpointer user_data)
1215 {
1216         struct connman_session *session = value;
1217         DBusMessage *message;
1218
1219         DBG("owner %s path %s", session->owner, session->notify_path);
1220
1221         if (session->notify_watch > 0)
1222                 g_dbus_remove_watch(connection, session->notify_watch);
1223
1224         g_dbus_unregister_interface(connection, session->session_path,
1225                                                 CONNMAN_SESSION_INTERFACE);
1226
1227         message = dbus_message_new_method_call(session->owner,
1228                                                 session->notify_path,
1229                                                 CONNMAN_NOTIFICATION_INTERFACE,
1230                                                 "Release");
1231         if (message == NULL)
1232                 return;
1233
1234         dbus_message_set_no_reply(message, TRUE);
1235
1236         g_dbus_send_message(connection, message);
1237 }
1238
1239 static int session_disconnect(struct connman_session *session)
1240 {
1241         DBG("session %p, %s", session, session->owner);
1242
1243         if (session->notify_watch > 0)
1244                 g_dbus_remove_watch(connection, session->notify_watch);
1245
1246         g_dbus_unregister_interface(connection, session->session_path,
1247                                                 CONNMAN_SESSION_INTERFACE);
1248
1249         test_and_disconnect(session);
1250
1251         g_hash_table_remove(session_hash, session->session_path);
1252
1253         return 0;
1254 }
1255
1256 static void owner_disconnect(DBusConnection *conn, void *user_data)
1257 {
1258         struct connman_session *session = user_data;
1259
1260         DBG("session %p, %s died", session, session->owner);
1261
1262         session_disconnect(session);
1263 }
1264
1265 static DBusMessage *destroy_session(DBusConnection *conn,
1266                                         DBusMessage *msg, void *user_data)
1267 {
1268         struct connman_session *session = user_data;
1269
1270         DBG("session %p", session);
1271
1272         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1273 }
1274
1275 static GDBusMethodTable session_methods[] = {
1276         { "Destroy",    "",   "", destroy_session    },
1277         { "Connect",    "",   "", connect_session    },
1278         { "Disconnect", "",   "", disconnect_session },
1279         { "Change",     "sv", "", change_session     },
1280         { },
1281 };
1282
1283 int __connman_session_create(DBusMessage *msg)
1284 {
1285         const char *owner, *notify_path;
1286         char *session_path = NULL;
1287         DBusMessageIter iter, array;
1288         struct connman_session *session = NULL;
1289         struct session_info *info, *info_last;
1290
1291         connman_bool_t priority = FALSE, avoid_handover = FALSE;
1292         connman_bool_t stay_connected = FALSE, ecall = FALSE;
1293         enum connman_session_roaming_policy roaming_policy =
1294                                 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1295         GSList *allowed_bearers = NULL;
1296         unsigned int periodic_connect = 0;
1297         unsigned int idle_timeout = 0;
1298
1299         int err;
1300
1301         owner = dbus_message_get_sender(msg);
1302
1303         DBG("owner %s", owner);
1304
1305         if (ecall_session != NULL) {
1306                 /*
1307                  * If there is an emergency call already going on,
1308                  * ignore session creation attempt
1309                  */
1310                 err = -EBUSY;
1311                 goto err;
1312         }
1313
1314         dbus_message_iter_init(msg, &iter);
1315         dbus_message_iter_recurse(&iter, &array);
1316
1317         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1318                 DBusMessageIter entry, value;
1319                 const char *key, *val;
1320
1321                 dbus_message_iter_recurse(&array, &entry);
1322                 dbus_message_iter_get_basic(&entry, &key);
1323
1324                 dbus_message_iter_next(&entry);
1325                 dbus_message_iter_recurse(&entry, &value);
1326
1327                 switch (dbus_message_iter_get_arg_type(&value)) {
1328                 case DBUS_TYPE_ARRAY:
1329                         if (g_str_equal(key, "AllowedBearers") == TRUE) {
1330                                 allowed_bearers =
1331                                         session_parse_allowed_bearers(&value);
1332                         } else {
1333                                 return -EINVAL;
1334                         }
1335                         break;
1336                 case DBUS_TYPE_BOOLEAN:
1337                         if (g_str_equal(key, "Priority") == TRUE) {
1338                                 dbus_message_iter_get_basic(&value,
1339                                                         &priority);
1340                         } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1341                                 dbus_message_iter_get_basic(&value,
1342                                                         &avoid_handover);
1343                         } else if (g_str_equal(key, "StayConnected") == TRUE) {
1344                                 dbus_message_iter_get_basic(&value,
1345                                                         &stay_connected);
1346                         } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1347                                 dbus_message_iter_get_basic(&value,
1348                                                         &ecall);
1349                         } else {
1350                                 return -EINVAL;
1351                         }
1352                         break;
1353                 case DBUS_TYPE_UINT32:
1354                         if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1355                                 dbus_message_iter_get_basic(&value,
1356                                                         &periodic_connect);
1357                         } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1358                                 dbus_message_iter_get_basic(&value,
1359                                                         &idle_timeout);
1360                         } else {
1361                                 return -EINVAL;
1362                         }
1363                         break;
1364                 case DBUS_TYPE_STRING:
1365                         if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1366                                 dbus_message_iter_get_basic(&value, &val);
1367                                 roaming_policy = string2roamingpolicy(val);
1368                         } else {
1369                                 return -EINVAL;
1370                         }
1371                 }
1372                 dbus_message_iter_next(&array);
1373         }
1374
1375         dbus_message_iter_next(&iter);
1376         dbus_message_iter_get_basic(&iter, &notify_path);
1377
1378         if (notify_path == NULL) {
1379                 err = -EINVAL;
1380                 goto err;
1381         }
1382
1383         session_path = g_strdup_printf("/sessions%s", notify_path);
1384         if (session_path == NULL) {
1385                 err = -ENOMEM;
1386                 goto err;
1387         }
1388
1389         session = g_hash_table_lookup(session_hash, session_path);
1390         if (session != NULL) {
1391                 session = NULL;
1392                 err = -EEXIST;
1393                 goto err;
1394         }
1395
1396         session = g_try_new0(struct connman_session, 1);
1397         if (session == NULL) {
1398                 err = -ENOMEM;
1399                 goto err;
1400         }
1401
1402         session->info = g_try_new0(struct session_info, 1);
1403         if (session->info == NULL) {
1404                 err = -ENOMEM;
1405                 goto err;
1406         }
1407
1408         session->info_last = g_try_new0(struct session_info, 1);
1409         if (session->info_last == NULL) {
1410                 err = -ENOMEM;
1411                 goto err;
1412         }
1413
1414         info = session->info;
1415         info_last = session->info_last;
1416
1417         session->owner = g_strdup(owner);
1418         session->session_path = session_path;
1419         session->notify_path = g_strdup(notify_path);
1420         session->notify_watch =
1421                 g_dbus_add_disconnect_watch(connection, session->owner,
1422                                         owner_disconnect, session, NULL);
1423
1424         session->service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1425                                                 NULL, NULL);
1426
1427         info->online = FALSE;
1428         info->priority = priority;
1429         info->avoid_handover = avoid_handover;
1430         info->stay_connected = stay_connected;
1431         info->periodic_connect = periodic_connect;
1432         info->idle_timeout = idle_timeout;
1433         info->ecall = ecall;
1434         info->roaming_policy = roaming_policy;
1435         info->entry = NULL;
1436         info->marker = 0;
1437
1438         if (allowed_bearers == NULL) {
1439                 info->allowed_bearers =
1440                                 session_allowed_bearers_any();
1441
1442                 if (info->allowed_bearers == NULL) {
1443                         err = -ENOMEM;
1444                         goto err;
1445                 }
1446         } else {
1447                 info->allowed_bearers = allowed_bearers;
1448         }
1449
1450         g_hash_table_replace(session_hash, session->session_path, session);
1451
1452         DBG("add %s", session->session_path);
1453
1454         if (g_dbus_register_interface(connection, session->session_path,
1455                                         CONNMAN_SESSION_INTERFACE,
1456                                         session_methods, NULL,
1457                                         NULL, session, NULL) == FALSE) {
1458                 connman_error("Failed to register %s", session->session_path);
1459                 g_hash_table_remove(session_hash, session->session_path);
1460                 session = NULL;
1461
1462                 err = -EINVAL;
1463                 goto err;
1464         }
1465
1466         g_dbus_send_reply(connection, msg,
1467                                 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1468                                 DBUS_TYPE_INVALID);
1469
1470
1471         update_allowed_bearers(session);
1472         if (info->ecall == TRUE) {
1473                 ecall_session = session;
1474                 update_ecall_sessions(session);
1475         }
1476
1477         info_last->online = info->online;
1478         info_last->priority = info->priority;
1479         info_last->avoid_handover = info->avoid_handover;
1480         info_last->stay_connected = info->stay_connected;
1481         info_last->periodic_connect = info->periodic_connect;
1482         info_last->idle_timeout = info->idle_timeout;
1483         info_last->ecall = info->ecall;
1484         info_last->roaming_policy = info->roaming_policy;
1485         info_last->entry = info->entry;
1486         info_last->marker = info->marker;
1487         info_last->allowed_bearers = info->allowed_bearers;
1488
1489         session->info_dirty = TRUE;
1490         session->append_all = TRUE;
1491
1492         session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1493
1494         return 0;
1495
1496 err:
1497         connman_error("Failed to create session");
1498
1499         if (session != NULL) {
1500                 if (session->info_last != NULL)
1501                         g_free(session->info_last);
1502                 if (session->info != NULL)
1503                         g_free(session->info);
1504                 g_free(session);
1505         }
1506
1507         g_free(session_path);
1508
1509         g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1510         g_slist_free(allowed_bearers);
1511
1512         return err;
1513 }
1514
1515 int __connman_session_destroy(DBusMessage *msg)
1516 {
1517         const char *owner, *session_path;
1518         struct connman_session *session;
1519
1520         owner = dbus_message_get_sender(msg);
1521
1522         DBG("owner %s", owner);
1523
1524         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1525                                                         DBUS_TYPE_INVALID);
1526         if (session_path == NULL)
1527                 return -EINVAL;
1528
1529         session = g_hash_table_lookup(session_hash, session_path);
1530         if (session == NULL)
1531                 return -EINVAL;
1532
1533         if (g_strcmp0(owner, session->owner) != 0)
1534                 return -EACCES;
1535
1536         session_disconnect(session);
1537
1538         return 0;
1539 }
1540
1541 connman_bool_t __connman_session_mode()
1542 {
1543         return sessionmode;
1544 }
1545
1546 void __connman_session_set_mode(connman_bool_t enable)
1547 {
1548         DBG("enable %d", enable);
1549
1550         if (sessionmode == enable)
1551                 return;
1552
1553         sessionmode = enable;
1554
1555         if (sessionmode == TRUE)
1556                 __connman_service_disconnect_all();
1557 }
1558
1559 static void service_add(struct connman_service *service)
1560 {
1561         GHashTableIter iter;
1562         GSequenceIter *iter_service_list;
1563         gpointer key, value;
1564         struct connman_session *session;
1565
1566         DBG("service %p", service);
1567
1568         g_hash_table_iter_init(&iter, session_hash);
1569
1570         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1571                 session = value;
1572
1573                 if (service_match(session, service) == FALSE)
1574                         continue;
1575
1576                 iter_service_list =
1577                         g_sequence_insert_sorted(session->service_list,
1578                                                         service, sort_services,
1579                                                         session);
1580
1581                 g_hash_table_replace(session->service_hash, service,
1582                                         iter_service_list);
1583
1584                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1585         }
1586 }
1587
1588 static void service_remove(struct connman_service *service)
1589 {
1590
1591         GHashTableIter iter;
1592         gpointer key, value;
1593         struct connman_session *session;
1594         struct session_info *info;
1595
1596         DBG("service %p", service);
1597
1598         g_hash_table_iter_init(&iter, session_hash);
1599
1600         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1601                 GSequenceIter *iter;
1602                 session = value;
1603                 info = session->info;
1604
1605                 iter = g_hash_table_lookup(session->service_hash, service);
1606                 if (iter == NULL)
1607                         continue;
1608
1609                 g_sequence_remove(iter);
1610
1611                 info->entry = NULL;
1612                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1613         }
1614 }
1615
1616 static void service_state_changed(struct connman_service *service,
1617                                         enum connman_service_state state)
1618 {
1619         GHashTableIter iter;
1620         gpointer key, value;
1621         struct connman_session *session;
1622         struct session_info *info, *info_last;
1623
1624         DBG("service %p state %d", service, state);
1625
1626         g_hash_table_iter_init(&iter, session_hash);
1627
1628         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1629                 session = value;
1630                 info = session->info;
1631                 info_last = session->info_last;
1632
1633                 if (info->entry != NULL && info->entry->service == service) {
1634                         info->entry->state = state;
1635                         info->online = is_online(info->entry->state);
1636                         if (info_last->online != info->online)
1637                                 session->info_dirty = TRUE;
1638                 }
1639
1640                 session_changed(session,
1641                                 CONNMAN_SESSION_TRIGGER_SERVICE);
1642         }
1643 }
1644
1645 static void ipconfig_changed(struct connman_service *service,
1646                                 struct connman_ipconfig *ipconfig)
1647 {
1648         GHashTableIter iter;
1649         gpointer key, value;
1650         struct connman_session *session;
1651         struct session_info *info;
1652         enum connman_ipconfig_type type;
1653
1654         DBG("service %p ipconfig %p", service, ipconfig);
1655
1656         type = __connman_ipconfig_get_config_type(ipconfig);
1657
1658         g_hash_table_iter_init(&iter, session_hash);
1659
1660         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1661                 session = value;
1662                 info = session->info;
1663
1664                 if (info->entry != NULL && info->entry->service == service) {
1665                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1666                                 ipconfig_ipv4_changed(session);
1667                         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1668                                 ipconfig_ipv6_changed(session);
1669                 }
1670         }
1671 }
1672
1673 static struct connman_notifier session_notifier = {
1674         .name                   = "session",
1675         .service_add            = service_add,
1676         .service_remove         = service_remove,
1677         .service_state_changed  = service_state_changed,
1678         .ipconfig_changed       = ipconfig_changed,
1679 };
1680
1681 int __connman_session_init(void)
1682 {
1683         int err;
1684
1685         DBG("");
1686
1687         connection = connman_dbus_get_connection();
1688         if (connection == NULL)
1689                 return -1;
1690
1691         err = connman_notifier_register(&session_notifier);
1692         if (err < 0) {
1693                 dbus_connection_unref(connection);
1694                 return err;
1695         }
1696
1697         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1698                                                 NULL, cleanup_session);
1699
1700         sessionmode = FALSE;
1701         return 0;
1702 }
1703
1704 void __connman_session_cleanup(void)
1705 {
1706         DBG("");
1707
1708         if (connection == NULL)
1709                 return;
1710
1711         connman_notifier_unregister(&session_notifier);
1712
1713         g_hash_table_foreach(session_hash, release_session, NULL);
1714         g_hash_table_destroy(session_hash);
1715
1716         dbus_connection_unref(connection);
1717 }