session: Free ifname
[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         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                 /*
910                  * We are not online, we are not connecting, that
911                  * means we could still have a valid info->entry.
912                  * Though something has changed from the service layer.
913                  * Therefore we want to restart the algorithm. Before we
914                  * can do that we have to cleanup a potientional old entry.
915                  */
916                 explicit_disconnect(session);
917
918                 if (info->reason == CONNMAN_SESSION_REASON_CONNECT) {
919                         DBG("Retry to find a matching session");
920                         /*
921                          * The user called Connect() but there was no
922                          * matching session available at this point.
923                          * Now there might be a new one. Let's retry
924                          * to select and connect
925                          */
926                         select_and_connect(session,
927                                         CONNMAN_SESSION_REASON_CONNECT);
928
929                         break;
930                 }
931
932                 if (info->stay_connected == TRUE) {
933                         DBG("StayConnected");
934                         select_and_connect(session,
935                                         CONNMAN_SESSION_REASON_CONNECT);
936
937                         break;
938                 }
939
940                 select_and_connect(session,
941                                 CONNMAN_SESSION_REASON_FREE_RIDE);
942
943                 break;
944         case CONNMAN_SESSION_TRIGGER_ECALL:
945                 if (info->online == FALSE && info->entry->service != NULL)
946                         test_and_disconnect(session);
947
948                 break;
949         }
950
951         if (info->entry != info_last->entry)
952                 session->info_dirty = TRUE;
953
954         session_notify(session);
955 }
956
957 static DBusMessage *connect_session(DBusConnection *conn,
958                                         DBusMessage *msg, void *user_data)
959 {
960         struct connman_session *session = user_data;
961
962         DBG("session %p", session);
963
964         if (ecall_session != NULL && ecall_session != session)
965                 return __connman_error_failed(msg, EBUSY);
966
967         session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
968
969         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
970 }
971
972 static DBusMessage *disconnect_session(DBusConnection *conn,
973                                         DBusMessage *msg, void *user_data)
974 {
975         struct connman_session *session = user_data;
976
977         DBG("session %p", session);
978
979         if (ecall_session != NULL && ecall_session != session)
980                 return __connman_error_failed(msg, EBUSY);
981
982         session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
983
984         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
985 }
986
987 static struct service_entry *create_service_entry(struct connman_service *service,
988                                         const char *name,
989                                         enum connman_service_state state)
990 {
991         struct service_entry *entry;
992         enum connman_service_type type;
993         int idx;
994
995         entry = g_try_new0(struct service_entry, 1);
996         if (entry == NULL)
997                 return entry;
998
999         entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1000         entry->state = state;
1001         entry->name = name;
1002         entry->service = service;
1003
1004         idx = __connman_service_get_index(entry->service);
1005         entry->ifname = connman_inet_ifname(idx);
1006         if (entry->ifname == NULL)
1007                 entry->ifname = g_strdup("");
1008
1009         type = connman_service_get_type(entry->service);
1010         entry->bearer = service2bearer(type);
1011
1012         return entry;
1013 }
1014
1015 static void destroy_service_entry(gpointer data)
1016 {
1017         struct service_entry *entry = data;
1018
1019         g_free(entry->ifname);
1020
1021         g_free(entry);
1022 }
1023
1024 static void update_allowed_bearers(struct connman_session *session)
1025 {
1026         struct service_entry *entry;
1027         GSequenceIter *iter;
1028
1029         if (session->service_list != NULL) {
1030                 g_hash_table_remove_all(session->service_hash);
1031                 g_sequence_free(session->service_list);
1032         }
1033
1034         session->service_list = __connman_service_get_list(session,
1035                                                         service_match,
1036                                                         create_service_entry,
1037                                                         destroy_service_entry);
1038
1039         g_sequence_sort(session->service_list, sort_services, session);
1040
1041         iter = g_sequence_get_begin_iter(session->service_list);
1042
1043         while (g_sequence_iter_is_end(iter) == FALSE) {
1044                 entry = g_sequence_get(iter);
1045
1046                 DBG("service %p type %s name %s", entry->service,
1047                         service2bearer(connman_service_get_type(entry->service)),
1048                         entry->name);
1049
1050                 g_hash_table_replace(session->service_hash,
1051                                         entry->service, iter);
1052
1053                 iter = g_sequence_iter_next(iter);
1054         }
1055
1056         session->info_dirty = TRUE;
1057 }
1058
1059 static void update_ecall_sessions(struct connman_session *session)
1060 {
1061         struct session_info *info = session->info;
1062         struct connman_session *session_iter;
1063         GHashTableIter iter;
1064         gpointer key, value;
1065
1066         g_hash_table_iter_init(&iter, session_hash);
1067
1068         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1069                 session_iter = value;
1070
1071                 if (session_iter == session)
1072                         continue;
1073
1074                 session_iter->info->ecall = info->ecall;
1075                 session_iter->info_dirty = TRUE;
1076
1077                 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
1078         }
1079 }
1080
1081 static void update_ecall(struct connman_session *session)
1082 {
1083         struct session_info *info = session->info;
1084         struct session_info *info_last = session->info_last;
1085
1086         DBG("session %p ecall_session %p ecall %d -> %d", session,
1087                 ecall_session, info_last->ecall, info->ecall);
1088
1089         if (ecall_session == NULL) {
1090                 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
1091                         goto err;
1092
1093                 ecall_session = session;
1094         } else if (ecall_session == session) {
1095                 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
1096                         goto err;
1097
1098                 ecall_session = NULL;
1099         } else {
1100                 goto err;
1101         }
1102
1103         update_ecall_sessions(session);
1104
1105         session->info_dirty = TRUE;
1106         return;
1107
1108 err:
1109         /* not a valid transition */
1110         info->ecall = info_last->ecall;
1111 }
1112
1113 static DBusMessage *change_session(DBusConnection *conn,
1114                                         DBusMessage *msg, void *user_data)
1115 {
1116         struct connman_session *session = user_data;
1117         struct session_info *info = session->info;
1118         struct session_info *info_last = session->info_last;
1119         DBusMessageIter iter, value;
1120         const char *name;
1121         GSList *allowed_bearers;
1122
1123         DBG("session %p", session);
1124         if (dbus_message_iter_init(msg, &iter) == FALSE)
1125                 return __connman_error_invalid_arguments(msg);
1126
1127         dbus_message_iter_get_basic(&iter, &name);
1128         dbus_message_iter_next(&iter);
1129         dbus_message_iter_recurse(&iter, &value);
1130
1131         switch (dbus_message_iter_get_arg_type(&value)) {
1132         case DBUS_TYPE_ARRAY:
1133                 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1134                         allowed_bearers = session_parse_allowed_bearers(&value);
1135
1136                         g_slist_foreach(info->allowed_bearers,
1137                                         cleanup_bearer_info, NULL);
1138                         g_slist_free(info->allowed_bearers);
1139
1140                         if (allowed_bearers == NULL) {
1141                                 allowed_bearers = session_allowed_bearers_any();
1142
1143                                 if (allowed_bearers == NULL)
1144                                         return __connman_error_failed(msg, ENOMEM);
1145                         }
1146
1147                         info->allowed_bearers = allowed_bearers;
1148
1149                         update_allowed_bearers(session);
1150                 } else {
1151                         goto err;
1152                 }
1153                 break;
1154         case DBUS_TYPE_BOOLEAN:
1155                 if (g_str_equal(name, "Priority") == TRUE) {
1156                         dbus_message_iter_get_basic(&value,
1157                                         &info->priority);
1158
1159                         if (info_last->priority != info->priority)
1160                                 session->info_dirty = TRUE;
1161                 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1162                         dbus_message_iter_get_basic(&value,
1163                                         &info->avoid_handover);
1164
1165                         if (info_last->avoid_handover != info->avoid_handover)
1166                                 session->info_dirty = TRUE;
1167                 } else if (g_str_equal(name, "StayConnected") == TRUE) {
1168                         dbus_message_iter_get_basic(&value,
1169                                         &info->stay_connected);
1170
1171                         if (info_last->stay_connected != info->stay_connected)
1172                                 session->info_dirty = TRUE;
1173                 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1174                         dbus_message_iter_get_basic(&value,
1175                                         &info->ecall);
1176
1177                         update_ecall(session);
1178                 } else {
1179                         goto err;
1180                 }
1181                 break;
1182         case DBUS_TYPE_UINT32:
1183                 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1184                         dbus_message_iter_get_basic(&value,
1185                                         &info->periodic_connect);
1186
1187                         if (info_last->periodic_connect != info->periodic_connect)
1188                                 session->info_dirty = TRUE;
1189                 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1190                         dbus_message_iter_get_basic(&value,
1191                                         &info->idle_timeout);
1192
1193                         if (info_last->idle_timeout != info->idle_timeout)
1194                                 session->info_dirty = TRUE;
1195                 } else {
1196                         goto err;
1197                 }
1198                 break;
1199         case DBUS_TYPE_STRING:
1200                 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1201                         const char *val;
1202                         dbus_message_iter_get_basic(&value, &val);
1203                         info->roaming_policy =
1204                                         string2roamingpolicy(val);
1205
1206                         if (info_last->roaming_policy != info->roaming_policy)
1207                                 session->info_dirty = TRUE;
1208                 } else {
1209                         goto err;
1210                 }
1211                 break;
1212         default:
1213                 goto err;
1214         }
1215
1216         if (session->info_dirty == TRUE)
1217                 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1218
1219         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1220
1221 err:
1222         return __connman_error_invalid_arguments(msg);
1223 }
1224
1225 static void release_session(gpointer key, gpointer value, gpointer user_data)
1226 {
1227         struct connman_session *session = value;
1228         DBusMessage *message;
1229
1230         DBG("owner %s path %s", session->owner, session->notify_path);
1231
1232         if (session->notify_watch > 0)
1233                 g_dbus_remove_watch(connection, session->notify_watch);
1234
1235         g_dbus_unregister_interface(connection, session->session_path,
1236                                                 CONNMAN_SESSION_INTERFACE);
1237
1238         message = dbus_message_new_method_call(session->owner,
1239                                                 session->notify_path,
1240                                                 CONNMAN_NOTIFICATION_INTERFACE,
1241                                                 "Release");
1242         if (message == NULL)
1243                 return;
1244
1245         dbus_message_set_no_reply(message, TRUE);
1246
1247         g_dbus_send_message(connection, message);
1248 }
1249
1250 static int session_disconnect(struct connman_session *session)
1251 {
1252         DBG("session %p, %s", session, session->owner);
1253
1254         if (session->notify_watch > 0)
1255                 g_dbus_remove_watch(connection, session->notify_watch);
1256
1257         g_dbus_unregister_interface(connection, session->session_path,
1258                                                 CONNMAN_SESSION_INTERFACE);
1259
1260         test_and_disconnect(session);
1261
1262         g_hash_table_remove(session_hash, session->session_path);
1263
1264         return 0;
1265 }
1266
1267 static void owner_disconnect(DBusConnection *conn, void *user_data)
1268 {
1269         struct connman_session *session = user_data;
1270
1271         DBG("session %p, %s died", session, session->owner);
1272
1273         session_disconnect(session);
1274 }
1275
1276 static DBusMessage *destroy_session(DBusConnection *conn,
1277                                         DBusMessage *msg, void *user_data)
1278 {
1279         struct connman_session *session = user_data;
1280
1281         DBG("session %p", session);
1282
1283         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1284 }
1285
1286 static GDBusMethodTable session_methods[] = {
1287         { "Destroy",    "",   "", destroy_session    },
1288         { "Connect",    "",   "", connect_session    },
1289         { "Disconnect", "",   "", disconnect_session },
1290         { "Change",     "sv", "", change_session     },
1291         { },
1292 };
1293
1294 int __connman_session_create(DBusMessage *msg)
1295 {
1296         const char *owner, *notify_path;
1297         char *session_path = NULL;
1298         DBusMessageIter iter, array;
1299         struct connman_session *session = NULL;
1300         struct session_info *info, *info_last;
1301
1302         connman_bool_t priority = FALSE, avoid_handover = FALSE;
1303         connman_bool_t stay_connected = FALSE, ecall = FALSE;
1304         enum connman_session_roaming_policy roaming_policy =
1305                                 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1306         GSList *allowed_bearers = NULL;
1307         unsigned int periodic_connect = 0;
1308         unsigned int idle_timeout = 0;
1309
1310         int err;
1311
1312         owner = dbus_message_get_sender(msg);
1313
1314         DBG("owner %s", owner);
1315
1316         if (ecall_session != NULL) {
1317                 /*
1318                  * If there is an emergency call already going on,
1319                  * ignore session creation attempt
1320                  */
1321                 err = -EBUSY;
1322                 goto err;
1323         }
1324
1325         dbus_message_iter_init(msg, &iter);
1326         dbus_message_iter_recurse(&iter, &array);
1327
1328         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1329                 DBusMessageIter entry, value;
1330                 const char *key, *val;
1331
1332                 dbus_message_iter_recurse(&array, &entry);
1333                 dbus_message_iter_get_basic(&entry, &key);
1334
1335                 dbus_message_iter_next(&entry);
1336                 dbus_message_iter_recurse(&entry, &value);
1337
1338                 switch (dbus_message_iter_get_arg_type(&value)) {
1339                 case DBUS_TYPE_ARRAY:
1340                         if (g_str_equal(key, "AllowedBearers") == TRUE) {
1341                                 allowed_bearers =
1342                                         session_parse_allowed_bearers(&value);
1343                         } else {
1344                                 return -EINVAL;
1345                         }
1346                         break;
1347                 case DBUS_TYPE_BOOLEAN:
1348                         if (g_str_equal(key, "Priority") == TRUE) {
1349                                 dbus_message_iter_get_basic(&value,
1350                                                         &priority);
1351                         } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1352                                 dbus_message_iter_get_basic(&value,
1353                                                         &avoid_handover);
1354                         } else if (g_str_equal(key, "StayConnected") == TRUE) {
1355                                 dbus_message_iter_get_basic(&value,
1356                                                         &stay_connected);
1357                         } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1358                                 dbus_message_iter_get_basic(&value,
1359                                                         &ecall);
1360                         } else {
1361                                 return -EINVAL;
1362                         }
1363                         break;
1364                 case DBUS_TYPE_UINT32:
1365                         if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1366                                 dbus_message_iter_get_basic(&value,
1367                                                         &periodic_connect);
1368                         } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1369                                 dbus_message_iter_get_basic(&value,
1370                                                         &idle_timeout);
1371                         } else {
1372                                 return -EINVAL;
1373                         }
1374                         break;
1375                 case DBUS_TYPE_STRING:
1376                         if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1377                                 dbus_message_iter_get_basic(&value, &val);
1378                                 roaming_policy = string2roamingpolicy(val);
1379                         } else {
1380                                 return -EINVAL;
1381                         }
1382                 }
1383                 dbus_message_iter_next(&array);
1384         }
1385
1386         dbus_message_iter_next(&iter);
1387         dbus_message_iter_get_basic(&iter, &notify_path);
1388
1389         if (notify_path == NULL) {
1390                 err = -EINVAL;
1391                 goto err;
1392         }
1393
1394         session_path = g_strdup_printf("/sessions%s", notify_path);
1395         if (session_path == NULL) {
1396                 err = -ENOMEM;
1397                 goto err;
1398         }
1399
1400         session = g_hash_table_lookup(session_hash, session_path);
1401         if (session != NULL) {
1402                 session = NULL;
1403                 err = -EEXIST;
1404                 goto err;
1405         }
1406
1407         session = g_try_new0(struct connman_session, 1);
1408         if (session == NULL) {
1409                 err = -ENOMEM;
1410                 goto err;
1411         }
1412
1413         session->info = g_try_new0(struct session_info, 1);
1414         if (session->info == NULL) {
1415                 err = -ENOMEM;
1416                 goto err;
1417         }
1418
1419         session->info_last = g_try_new0(struct session_info, 1);
1420         if (session->info_last == NULL) {
1421                 err = -ENOMEM;
1422                 goto err;
1423         }
1424
1425         info = session->info;
1426         info_last = session->info_last;
1427
1428         session->owner = g_strdup(owner);
1429         session->session_path = session_path;
1430         session->notify_path = g_strdup(notify_path);
1431         session->notify_watch =
1432                 g_dbus_add_disconnect_watch(connection, session->owner,
1433                                         owner_disconnect, session, NULL);
1434
1435         session->service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1436                                                 NULL, NULL);
1437
1438         info->online = FALSE;
1439         info->priority = priority;
1440         info->avoid_handover = avoid_handover;
1441         info->stay_connected = stay_connected;
1442         info->periodic_connect = periodic_connect;
1443         info->idle_timeout = idle_timeout;
1444         info->ecall = ecall;
1445         info->roaming_policy = roaming_policy;
1446         info->entry = NULL;
1447         info->marker = 0;
1448
1449         if (allowed_bearers == NULL) {
1450                 info->allowed_bearers =
1451                                 session_allowed_bearers_any();
1452
1453                 if (info->allowed_bearers == NULL) {
1454                         err = -ENOMEM;
1455                         goto err;
1456                 }
1457         } else {
1458                 info->allowed_bearers = allowed_bearers;
1459         }
1460
1461         g_hash_table_replace(session_hash, session->session_path, session);
1462
1463         DBG("add %s", session->session_path);
1464
1465         if (g_dbus_register_interface(connection, session->session_path,
1466                                         CONNMAN_SESSION_INTERFACE,
1467                                         session_methods, NULL,
1468                                         NULL, session, NULL) == FALSE) {
1469                 connman_error("Failed to register %s", session->session_path);
1470                 g_hash_table_remove(session_hash, session->session_path);
1471                 session = NULL;
1472
1473                 err = -EINVAL;
1474                 goto err;
1475         }
1476
1477         g_dbus_send_reply(connection, msg,
1478                                 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1479                                 DBUS_TYPE_INVALID);
1480
1481
1482         update_allowed_bearers(session);
1483         if (info->ecall == TRUE) {
1484                 ecall_session = session;
1485                 update_ecall_sessions(session);
1486         }
1487
1488         info_last->online = info->online;
1489         info_last->priority = info->priority;
1490         info_last->avoid_handover = info->avoid_handover;
1491         info_last->stay_connected = info->stay_connected;
1492         info_last->periodic_connect = info->periodic_connect;
1493         info_last->idle_timeout = info->idle_timeout;
1494         info_last->ecall = info->ecall;
1495         info_last->roaming_policy = info->roaming_policy;
1496         info_last->entry = info->entry;
1497         info_last->marker = info->marker;
1498         info_last->allowed_bearers = info->allowed_bearers;
1499
1500         session->info_dirty = TRUE;
1501         session->append_all = TRUE;
1502
1503         session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1504
1505         return 0;
1506
1507 err:
1508         connman_error("Failed to create session");
1509
1510         if (session != NULL) {
1511                 if (session->info_last != NULL)
1512                         g_free(session->info_last);
1513                 if (session->info != NULL)
1514                         g_free(session->info);
1515                 g_free(session);
1516         }
1517
1518         g_free(session_path);
1519
1520         g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1521         g_slist_free(allowed_bearers);
1522
1523         return err;
1524 }
1525
1526 int __connman_session_destroy(DBusMessage *msg)
1527 {
1528         const char *owner, *session_path;
1529         struct connman_session *session;
1530
1531         owner = dbus_message_get_sender(msg);
1532
1533         DBG("owner %s", owner);
1534
1535         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1536                                                         DBUS_TYPE_INVALID);
1537         if (session_path == NULL)
1538                 return -EINVAL;
1539
1540         session = g_hash_table_lookup(session_hash, session_path);
1541         if (session == NULL)
1542                 return -EINVAL;
1543
1544         if (g_strcmp0(owner, session->owner) != 0)
1545                 return -EACCES;
1546
1547         session_disconnect(session);
1548
1549         return 0;
1550 }
1551
1552 connman_bool_t __connman_session_mode()
1553 {
1554         return sessionmode;
1555 }
1556
1557 void __connman_session_set_mode(connman_bool_t enable)
1558 {
1559         DBG("enable %d", enable);
1560
1561         if (sessionmode == enable)
1562                 return;
1563
1564         sessionmode = enable;
1565
1566         if (sessionmode == TRUE)
1567                 __connman_service_disconnect_all();
1568 }
1569
1570 static void service_add(struct connman_service *service,
1571                         const char *name)
1572 {
1573         GHashTableIter iter;
1574         GSequenceIter *iter_service_list;
1575         gpointer key, value;
1576         struct connman_session *session;
1577         struct service_entry *entry;
1578
1579         DBG("service %p", service);
1580
1581         g_hash_table_iter_init(&iter, session_hash);
1582
1583         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1584                 session = value;
1585
1586                 if (service_match(session, service) == FALSE)
1587                         continue;
1588
1589                 entry = create_service_entry(service, name,
1590                                                 CONNMAN_SERVICE_STATE_IDLE);
1591                 if (entry == NULL)
1592                         continue;
1593
1594                 iter_service_list =
1595                         g_sequence_insert_sorted(session->service_list,
1596                                                         entry, sort_services,
1597                                                         session);
1598
1599                 g_hash_table_replace(session->service_hash, service,
1600                                         iter_service_list);
1601
1602                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1603         }
1604 }
1605
1606 static void service_remove(struct connman_service *service)
1607 {
1608
1609         GHashTableIter iter;
1610         gpointer key, value;
1611         struct connman_session *session;
1612         struct session_info *info;
1613
1614         DBG("service %p", service);
1615
1616         g_hash_table_iter_init(&iter, session_hash);
1617
1618         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1619                 GSequenceIter *iter;
1620                 session = value;
1621                 info = session->info;
1622
1623                 iter = g_hash_table_lookup(session->service_hash, service);
1624                 if (iter == NULL)
1625                         continue;
1626
1627                 g_sequence_remove(iter);
1628
1629                 info->entry = NULL;
1630                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1631         }
1632 }
1633
1634 static void service_state_changed(struct connman_service *service,
1635                                         enum connman_service_state state)
1636 {
1637         GHashTableIter iter;
1638         gpointer key, value;
1639         struct connman_session *session;
1640         struct session_info *info, *info_last;
1641
1642         DBG("service %p state %d", service, state);
1643
1644         g_hash_table_iter_init(&iter, session_hash);
1645
1646         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1647                 session = value;
1648                 info = session->info;
1649                 info_last = session->info_last;
1650
1651                 if (info->entry != NULL && info->entry->service == service) {
1652                         info->entry->state = state;
1653                         info->online = is_online(info->entry->state);
1654                         if (info_last->online != info->online)
1655                                 session->info_dirty = TRUE;
1656                 }
1657
1658                 session_changed(session,
1659                                 CONNMAN_SESSION_TRIGGER_SERVICE);
1660         }
1661 }
1662
1663 static void ipconfig_changed(struct connman_service *service,
1664                                 struct connman_ipconfig *ipconfig)
1665 {
1666         GHashTableIter iter;
1667         gpointer key, value;
1668         struct connman_session *session;
1669         struct session_info *info;
1670         enum connman_ipconfig_type type;
1671
1672         DBG("service %p ipconfig %p", service, ipconfig);
1673
1674         type = __connman_ipconfig_get_config_type(ipconfig);
1675
1676         g_hash_table_iter_init(&iter, session_hash);
1677
1678         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1679                 session = value;
1680                 info = session->info;
1681
1682                 if (info->entry != NULL && info->entry->service == service) {
1683                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1684                                 ipconfig_ipv4_changed(session);
1685                         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1686                                 ipconfig_ipv6_changed(session);
1687                 }
1688         }
1689 }
1690
1691 static struct connman_notifier session_notifier = {
1692         .name                   = "session",
1693         .service_add            = service_add,
1694         .service_remove         = service_remove,
1695         .service_state_changed  = service_state_changed,
1696         .ipconfig_changed       = ipconfig_changed,
1697 };
1698
1699 int __connman_session_init(void)
1700 {
1701         int err;
1702
1703         DBG("");
1704
1705         connection = connman_dbus_get_connection();
1706         if (connection == NULL)
1707                 return -1;
1708
1709         err = connman_notifier_register(&session_notifier);
1710         if (err < 0) {
1711                 dbus_connection_unref(connection);
1712                 return err;
1713         }
1714
1715         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1716                                                 NULL, cleanup_session);
1717
1718         sessionmode = FALSE;
1719         return 0;
1720 }
1721
1722 void __connman_session_cleanup(void)
1723 {
1724         DBG("");
1725
1726         if (connection == NULL)
1727                 return;
1728
1729         connman_notifier_unregister(&session_notifier);
1730
1731         g_hash_table_foreach(session_hash, release_session, NULL);
1732         g_hash_table_destroy(session_hash);
1733         session_hash = NULL;
1734
1735         dbus_connection_unref(connection);
1736 }