wispr: Adding structures and start/stop functions
[platform/upstream/connman.git] / src / wispr.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. 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
28 #include "connman.h"
29
30 struct connman_wispr_portal_context {
31         struct connman_service *service;
32         enum connman_ipconfig_type type;
33 };
34
35 struct connman_wispr_portal {
36         struct connman_wispr_portal_context *ipv4_context;
37         struct connman_wispr_portal_context *ipv6_context;
38 };
39
40 static GHashTable *wispr_portal_list = NULL;
41
42 static void free_connman_wispr_portal_context(struct connman_wispr_portal_context *wp_context)
43 {
44         DBG("");
45
46         if (wp_context == NULL)
47                 return;
48
49         g_free(wp_context);
50 }
51
52 static void free_connman_wispr_portal(gpointer data)
53 {
54         struct connman_wispr_portal *wispr_portal = data;
55
56         DBG("");
57
58         if (wispr_portal == NULL)
59                 return;
60
61         free_connman_wispr_portal_context(wispr_portal->ipv4_context);
62         free_connman_wispr_portal_context(wispr_portal->ipv6_context);
63
64         g_free(wispr_portal);
65 }
66
67 int __connman_wispr_start(struct connman_service *service,
68                                         enum connman_ipconfig_type type)
69 {
70         struct connman_wispr_portal_context *wp_context = NULL;
71         struct connman_wispr_portal *wispr_portal = NULL;
72         int index;
73
74         DBG("service %p", service);
75
76         if (wispr_portal_list == NULL)
77                 return -EINVAL;
78
79         index = __connman_service_get_index(service);
80         if (index < 0)
81                 return -EINVAL;
82
83         wispr_portal = g_hash_table_lookup(wispr_portal_list,
84                                         GINT_TO_POINTER(index));
85         if (wispr_portal == NULL) {
86                 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
87                 if (wispr_portal == NULL)
88                         return -ENOMEM;
89
90                 g_hash_table_replace(wispr_portal_list,
91                                         GINT_TO_POINTER(index), wispr_portal);
92         }
93
94         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
95                 wp_context = wispr_portal->ipv4_context;
96         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
97                 wp_context = wispr_portal->ipv6_context;
98         else
99                 return -EINVAL;
100
101         if (wp_context == NULL) {
102                 wp_context = g_try_new0(struct connman_wispr_portal_context, 1);
103                 if (wp_context == NULL)
104                         return -ENOMEM;
105
106                 wp_context->service = service;
107                 wp_context->type = type;
108
109                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
110                         wispr_portal->ipv4_context = wp_context;
111                 else
112                         wispr_portal->ipv6_context = wp_context;
113         }
114
115         return 0;
116 }
117
118 void __connman_wispr_stop(struct connman_service *service)
119 {
120         int index;
121
122         DBG("service %p", service);
123
124         if (wispr_portal_list == NULL)
125                 return;
126
127         index = __connman_service_get_index(service);
128         if (index < 0)
129                 return;
130
131         g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
132 }
133
134 int __connman_wispr_init(void)
135 {
136         DBG("");
137
138         wispr_portal_list = g_hash_table_new_full(g_direct_hash,
139                                                 g_direct_equal, NULL,
140                                                 free_connman_wispr_portal);
141
142         return 0;
143 }
144
145 void __connman_wispr_cleanup(void)
146 {
147         DBG("");
148
149         g_hash_table_destroy(wispr_portal_list);
150         wispr_portal_list = NULL;
151 }