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