Update gupnp to 0.20.5 (fdeb6f9f)
[profile/ivi/GUPnP.git] / tests / test-white-list.c
1 /*
2  * Copyright (C) 2013 Intel Corporation.
3  *
4  * Author: Ludovic Ferrandis <ludovic.ferrandis@intel.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include <libgupnp/gupnp-control-point.h>
23 #include <libgupnp/gupnp-context-manager.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <locale.h>
27 #include <signal.h>
28 #include <glib.h>
29
30 GMainLoop *main_loop;
31
32 static void
33 interrupt_signal_handler (G_GNUC_UNUSED int signum)
34 {
35         g_main_loop_quit (main_loop);
36 }
37
38 static void
39 device_proxy_available_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
40                            GUPnPDeviceProxy                *proxy)
41 {
42         const char *type, *location;
43
44         type = gupnp_device_info_get_device_type (GUPNP_DEVICE_INFO (proxy));
45         location = gupnp_device_info_get_location (GUPNP_DEVICE_INFO (proxy));
46
47         g_print ("Device available:\n");
48         g_print ("\ttype:     %s\n", type);
49         g_print ("\tlocation: %s\n", location);
50 }
51
52 static void
53 device_proxy_unavailable_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
54                              GUPnPDeviceProxy                *proxy)
55 {
56         const char *type, *location;
57
58         type = gupnp_device_info_get_device_type (GUPNP_DEVICE_INFO (proxy));
59         location = gupnp_device_info_get_location (GUPNP_DEVICE_INFO (proxy));
60
61         g_print ("Device unavailable:\n");
62         g_print ("\ttype:     %s\n", type);
63         g_print ("\tlocation: %s\n", location);
64 }
65
66 static void
67 service_proxy_available_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
68                             GUPnPServiceProxy               *proxy)
69 {
70         const char *type, *location;
71
72         type = gupnp_service_info_get_service_type (GUPNP_SERVICE_INFO (proxy));
73         location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));
74
75         g_print ("Service available:\n");
76         g_print ("\ttype:     %s\n", type);
77         g_print ("\tlocation: %s\n", location);
78 }
79
80 static void
81 service_proxy_unavailable_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
82                               GUPnPServiceProxy               *proxy)
83 {
84         const char *type, *location;
85
86         type = gupnp_service_info_get_service_type (GUPNP_SERVICE_INFO (proxy));
87         location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));
88
89         g_print ("Service unavailable:\n");
90         g_print ("\ttype:     %s\n", type);
91         g_print ("\tlocation: %s\n", location);
92 }
93
94 static void
95 context_available_cb(GUPnPContextManager *context_manager,
96                      GUPnPContext *context,
97                      gpointer user_data)
98 {
99         GUPnPControlPoint *cp;
100         GSSDPClient *client = GSSDP_CLIENT(context);
101
102         g_print ("Context Available:\n");
103         g_print ("\tServer ID:     %s\n", gssdp_client_get_server_id (client));
104         g_print ("\tInterface:     %s\n", gssdp_client_get_interface (client));
105         g_print ("\tHost IP  :     %s\n", gssdp_client_get_host_ip (client));
106         g_print ("\tNetwork  :     %s\n", gssdp_client_get_network (client));
107         g_print ("\tActive   :     %s\n", gssdp_client_get_active (client)? "TRUE" : "FALSE");
108
109
110         /* We're interested in everything */
111         cp = gupnp_control_point_new (context, "ssdp:all");
112
113         g_signal_connect (cp,
114                           "device-proxy-available",
115                           G_CALLBACK (device_proxy_available_cb),
116                           NULL);
117         g_signal_connect (cp,
118                           "device-proxy-unavailable",
119                           G_CALLBACK (device_proxy_unavailable_cb),
120                           NULL);
121         g_signal_connect (cp,
122                           "service-proxy-available",
123                           G_CALLBACK (service_proxy_available_cb),
124                           NULL);
125         g_signal_connect (cp,
126                           "service-proxy-unavailable",
127                           G_CALLBACK (service_proxy_unavailable_cb),
128                           NULL);
129
130         gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
131         gupnp_context_manager_manage_control_point(context_manager, cp);
132         g_object_unref(cp);
133 }
134
135 static void
136 context_unavailable_cb(GUPnPContextManager *context_manager,
137                        GUPnPContext *context,
138                        gpointer user_data)
139 {
140         GSSDPClient *client = GSSDP_CLIENT(context);
141
142         g_print ("Context Unavailable:\n");
143         g_print ("\tServer ID:     %s\n", gssdp_client_get_server_id (client));
144         g_print ("\tInterface:     %s\n", gssdp_client_get_interface (client));
145         g_print ("\tHost IP  :     %s\n", gssdp_client_get_host_ip (client));
146         g_print ("\tNetwork  :     %s\n", gssdp_client_get_network (client));
147         g_print ("\tActive   :     %s\n", gssdp_client_get_active (client)? "TRUE" : "FALSE");
148 }
149
150 static void
151 print_wl_entry(gpointer data, gpointer user_data)
152 {
153         g_print ("\t\t\tEntry: %s\n", (char *)data);
154 }
155
156 static void
157 print_white_list_entries(GUPnPWhiteList *wl)
158 {
159         GList *list;
160
161         g_print ("\t\tWhite List Entries:\n");
162         list = gupnp_white_list_get_entries(wl);
163         g_list_foreach (list, print_wl_entry, NULL);
164         g_print ("\n");
165 }
166
167 static gboolean
168 change_white_list(gpointer user_data)
169 {
170         GUPnPContextManager *context_manager = user_data;
171         GUPnPWhiteList *white_list;
172         static int tomato = 0;
173
174         g_print ("\nChange White List:\n");
175         g_print ("\t Action number %d:\n", tomato);
176
177         white_list = gupnp_context_manager_get_white_list(context_manager);
178
179         switch (tomato) {
180         case 0:
181                 g_print ("\t Add Entry eth0\n\n");
182                 gupnp_white_list_add_entry(white_list, "eth0");
183                 print_white_list_entries (white_list);
184                 break;
185         case 1:
186                 g_print ("\t Enable WL\n\n");
187                 gupnp_white_list_set_enabled (white_list, TRUE);
188                 break;
189         case 2:
190                 g_print ("\t Add Entry 127.0.0.1\n\n");
191                 gupnp_white_list_add_entry(white_list, "127.0.0.1");
192                 print_white_list_entries (white_list);
193                 break;
194         case 3:
195                 g_print ("\t Add Entry eth5\n\n");
196                 gupnp_white_list_add_entry(white_list, "eth5");
197                 print_white_list_entries (white_list);
198                 break;
199         case 4:
200                 g_print ("\t Remove Entry eth5\n\n");
201                 gupnp_white_list_remove_entry(white_list, "eth5");
202                 print_white_list_entries (white_list);
203                 break;
204         case 5:
205                 g_print ("\t Clear all entries\n\n");
206                 gupnp_white_list_clear(white_list);
207                 print_white_list_entries (white_list);
208                 break;
209         case 6:
210                 g_print ("\t Add Entry wlan2\n\n");
211                 gupnp_white_list_add_entry(white_list, "wlan2");
212                 print_white_list_entries(white_list);
213                 break;
214         case 7:
215                 g_print ("\t Disable WL\n\n");
216                 gupnp_white_list_set_enabled (white_list, FALSE);
217                 break;
218         case 8:
219                 g_print ("\t Enable WL\n\n");
220                 gupnp_white_list_set_enabled (white_list, TRUE);
221                 break;
222         case 9:
223                 g_print ("\t Connect to wlan0\n\n");
224                 g_timeout_add_seconds (35, change_white_list, context_manager);
225                 break;
226         case 10:
227                 g_print ("\t Add Entry wlan0\n\n");
228                 gupnp_white_list_add_entry(white_list, "wlan0");
229                 print_white_list_entries (white_list);
230                 break;
231         //~ case 11:
232                 //~ g_print ("\t Enable WL\n");
233                 //~ gupnp_white_list_enable(white_list, FALSE);
234                 //~ break;
235         default:
236                 break;
237         }
238
239         tomato++;
240
241         return (tomato < 11) && (tomato != 10);
242 }
243
244 int
245 main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
246 {
247         GUPnPContextManager *cm;
248         guint id;
249 #ifndef G_OS_WIN32
250         struct sigaction sig_action;
251 #endif /* G_OS_WIN32 */
252
253 #if !GLIB_CHECK_VERSION(2,35,0)
254         g_type_init ();
255 #endif
256         setlocale (LC_ALL, "");
257
258         cm = gupnp_context_manager_create(0);
259
260         g_signal_connect(cm,
261                          "context-available",
262                          G_CALLBACK(context_available_cb),
263                          NULL);
264
265         g_signal_connect(cm,
266                          "context-unavailable",
267                          G_CALLBACK(context_unavailable_cb),
268                          NULL);
269
270         main_loop = g_main_loop_new (NULL, FALSE);
271
272         id = g_timeout_add_seconds (5, change_white_list, cm);
273
274 #ifndef G_OS_WIN32
275         /* Hook the handler for SIGTERM */
276         memset (&sig_action, 0, sizeof (sig_action));
277         sig_action.sa_handler = interrupt_signal_handler;
278         sigaction (SIGINT, &sig_action, NULL);
279 #else
280         signal(SIGINT, interrupt_signal_handler);
281 #endif /* G_OS_WIN32 */
282
283         g_main_loop_run (main_loop);
284         g_main_loop_unref (main_loop);
285
286         g_source_remove (id);
287
288         g_object_unref (cm);
289
290         return EXIT_SUCCESS;
291 }