85929f96f229a7a68c6894562ec9e725822a5865
[platform/upstream/connman.git] / plugins / session_policy.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  BMW Car IT GbmH. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <string.h>
28
29 #include <glib.h>
30
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/log.h>
34 #include <connman/session.h>
35
36 static GHashTable *config_hash;
37
38 static int policy_create(struct connman_session *session,
39                                 connman_session_config_cb callback,
40                                 void *user_data)
41 {
42         struct connman_session_config *config;
43
44         DBG("session %p", session);
45
46         if (callback == NULL)
47                 return -EINVAL;
48
49         config = connman_session_create_default_config();
50         if (config == NULL)
51                 return -ENOMEM;
52
53         g_hash_table_replace(config_hash, session, config);
54
55         (*callback)(session, config, user_data, 0);
56
57         return 0;
58 }
59
60 static void policy_destroy(struct connman_session *session)
61 {
62         DBG("session %p", session);
63
64         g_hash_table_remove(config_hash, session);
65 }
66
67 static struct connman_session_policy session_policy = {
68         .name = "session policy configuration",
69         .priority = CONNMAN_SESSION_POLICY_PRIORITY_LOW,
70         .create = policy_create,
71         .destroy = policy_destroy,
72 };
73
74 static void cleanup_config(gpointer user_data)
75 {
76         struct connman_session_config *config = user_data;
77
78         connman_session_free_bearers(config->allowed_bearers);
79         g_free(config);
80 }
81
82 static int session_policy_init(void)
83 {
84         int err;
85
86         err = connman_session_policy_register(&session_policy);
87         if (err < 0)
88                 return err;
89
90         config_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
91                                                 cleanup_config);
92         if (config_hash == NULL) {
93                 connman_session_policy_unregister(&session_policy);
94                 return -ENOMEM;
95         }
96
97         return 0;
98 }
99
100 static void session_policy_exit(void)
101 {
102         g_hash_table_destroy(config_hash);
103
104         connman_session_policy_unregister(&session_policy);
105 }
106
107 CONNMAN_PLUGIN_DEFINE(session_policy, "Session policy configuration plugin",
108                 VERSION, CONNMAN_PLUGIN_PRIORITY_DEFAULT,
109                 session_policy_init, session_policy_exit)