summaryrefslogtreecommitdiff
path: root/examples/test-white-list.c
blob: 31fea5faf89f4e6a588afc86dca3bb8599e9a034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (C) 2013 Intel Corporation.
 *
 * Author: Ludovic Ferrandis <ludovic.ferrandis@intel.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <libgupnp/gupnp-control-point.h>
#include <libgupnp/gupnp-context-manager.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <signal.h>
#include <glib.h>

GMainLoop *main_loop;

#ifndef G_OS_WIN32
#include <glib-unix.h>
#endif

static gboolean
unix_signal_handler (gpointer user_data)
{
        g_main_loop_quit (main_loop);

        return G_SOURCE_REMOVE;
}


#ifdef G_OS_WIN32
/*
 *  Since in Windows this is basically called from
 *  the ConsoleCtrlHandler which does not share the restrictions of Unix signals
 */
static void
interrupt_signal_handler (G_GNUC_UNUSED int signum)
{
        unix_signal_handler (NULL);
}

#endif
static void
device_proxy_available_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
                           GUPnPDeviceProxy                *proxy)
{
        const char *type, *location;

        type = gupnp_device_info_get_device_type (GUPNP_DEVICE_INFO (proxy));
        location = gupnp_device_info_get_location (GUPNP_DEVICE_INFO (proxy));

        g_print ("Device available:\n");
        g_print ("\ttype:     %s\n", type);
        g_print ("\tlocation: %s\n", location);
}

static void
device_proxy_unavailable_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
                             GUPnPDeviceProxy                *proxy)
{
        const char *type, *location;

        type = gupnp_device_info_get_device_type (GUPNP_DEVICE_INFO (proxy));
        location = gupnp_device_info_get_location (GUPNP_DEVICE_INFO (proxy));

        g_print ("Device unavailable:\n");
        g_print ("\ttype:     %s\n", type);
        g_print ("\tlocation: %s\n", location);
}

static void
service_proxy_available_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
                            GUPnPServiceProxy               *proxy)
{
        const char *type, *location;

        type = gupnp_service_info_get_service_type (GUPNP_SERVICE_INFO (proxy));
        location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));

        g_print ("Service available:\n");
        g_print ("\ttype:     %s\n", type);
        g_print ("\tlocation: %s\n", location);
}

static void
service_proxy_unavailable_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
                              GUPnPServiceProxy               *proxy)
{
        const char *type, *location;

        type = gupnp_service_info_get_service_type (GUPNP_SERVICE_INFO (proxy));
        location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));

        g_print ("Service unavailable:\n");
        g_print ("\ttype:     %s\n", type);
        g_print ("\tlocation: %s\n", location);
}

static void
context_available_cb(GUPnPContextManager *context_manager,
                     GUPnPContext *context,
                     gpointer user_data)
{
        GUPnPControlPoint *cp;
        GSSDPClient *client = GSSDP_CLIENT(context);

        g_print ("Context Available:\n");
        g_print ("\tServer ID:     %s\n", gssdp_client_get_server_id (client));
        g_print ("\tInterface:     %s\n", gssdp_client_get_interface (client));
        g_print ("\tHost IP  :     %s\n", gssdp_client_get_host_ip (client));
        g_print ("\tNetwork  :     %s\n", gssdp_client_get_network (client));
        g_print ("\tActive   :     %s\n", gssdp_client_get_active (client)? "TRUE" : "FALSE");


        /* We're interested in everything */
        cp = gupnp_control_point_new (context, "ssdp:all");

        g_signal_connect (cp,
                          "device-proxy-available",
                          G_CALLBACK (device_proxy_available_cb),
                          NULL);
        g_signal_connect (cp,
                          "device-proxy-unavailable",
                          G_CALLBACK (device_proxy_unavailable_cb),
                          NULL);
        g_signal_connect (cp,
                          "service-proxy-available",
                          G_CALLBACK (service_proxy_available_cb),
                          NULL);
        g_signal_connect (cp,
                          "service-proxy-unavailable",
                          G_CALLBACK (service_proxy_unavailable_cb),
                          NULL);

        gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
        gupnp_context_manager_manage_control_point(context_manager, cp);
        g_object_unref(cp);
}

static void
context_unavailable_cb(GUPnPContextManager *context_manager,
                       GUPnPContext *context,
                       gpointer user_data)
{
        GSSDPClient *client = GSSDP_CLIENT(context);

        g_print ("Context Unavailable:\n");
        g_print ("\tServer ID:     %s\n", gssdp_client_get_server_id (client));
        g_print ("\tInterface:     %s\n", gssdp_client_get_interface (client));
        g_print ("\tHost IP  :     %s\n", gssdp_client_get_host_ip (client));
        g_print ("\tNetwork  :     %s\n", gssdp_client_get_network (client));
        g_print ("\tActive   :     %s\n", gssdp_client_get_active (client)? "TRUE" : "FALSE");
}

static void
print_wl_entry(gpointer data, gpointer user_data)
{
        g_print ("\t\t\tEntry: %s\n", (char *)data);
}

static void
print_context_filter_entries (GUPnPContextFilter *wl)
{
        GList *list;

        g_print ("\t\tContext filter Entries:\n");
        list = gupnp_context_filter_get_entries (wl);
        g_list_foreach (list, print_wl_entry, NULL);
        g_print ("\n");
}


static gboolean
change_context_filter (gpointer user_data)
{
        GUPnPContextManager *context_manager = user_data;
        GUPnPContextFilter *context_filter;
        static int tomato = 0;

        g_print ("\nChange Context filter:\n");
        g_print ("\t Action number %d:\n", tomato);

        context_filter =
                gupnp_context_manager_get_context_filter (context_manager);

        switch (tomato) {
        case 0:
                g_print ("\t Add Entry eth0\n\n");
                gupnp_context_filter_add_entry (context_filter, "eth0");
                print_context_filter_entries (context_filter);
                break;
        case 1:
                g_print ("\t Enable WL\n\n");
                gupnp_context_filter_set_enabled (context_filter, TRUE);
                break;
        case 2:
                g_print ("\t Add Entry 127.0.0.1\n\n");
                gupnp_context_filter_add_entry (context_filter, "127.0.0.1");
                print_context_filter_entries (context_filter);
                break;
        case 3:
                g_print ("\t Add Entry eth5\n\n");
                gupnp_context_filter_add_entry (context_filter, "eth5");
                print_context_filter_entries (context_filter);
                break;
        case 4:
                g_print ("\t Remove Entry eth5\n\n");
                gupnp_context_filter_remove_entry (context_filter, "eth5");
                print_context_filter_entries (context_filter);
                break;
        case 5:
                g_print ("\t Clear all entries\n\n");
                gupnp_context_filter_clear (context_filter);
                print_context_filter_entries (context_filter);
                break;
        case 6:
                g_print ("\t Add Entry wlan2\n\n");
                gupnp_context_filter_add_entry (context_filter, "wlan2");
                print_context_filter_entries (context_filter);
                break;
        case 7:
                g_print ("\t Disable WL\n\n");
                gupnp_context_filter_set_enabled (context_filter, FALSE);
                break;
        case 8:
                g_print ("\t Enable WL\n\n");
                gupnp_context_filter_set_enabled (context_filter, TRUE);
                break;
        case 9:
                g_print ("\t Connect to wlan0\n\n");
                g_timeout_add_seconds (35,
                                       change_context_filter,
                                       context_manager);
                break;
        case 10:
                g_print ("\t Add Entry wlan0\n\n");
                gupnp_context_filter_add_entry (context_filter, "wlan0");
                print_context_filter_entries (context_filter);
                break;
        //~ case 11:
        //~ g_print ("\t Enable WL\n");
        //~ gupnp_context_filter_enable(context_filter, FALSE);
        //~ break;
        default:
                break;
        }

        tomato++;

        return (tomato < 11) && (tomato != 10);
}

int
main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
{
        GUPnPContextManager *cm;
        guint id;

        setlocale (LC_ALL, "");

        cm = gupnp_context_manager_create(0);

        g_signal_connect(cm,
                         "context-available",
                         G_CALLBACK(context_available_cb),
                         NULL);

        g_signal_connect(cm,
                         "context-unavailable",
                         G_CALLBACK(context_unavailable_cb),
                         NULL);

        main_loop = g_main_loop_new (NULL, FALSE);

        id = g_timeout_add_seconds (5, change_context_filter, cm);

#ifndef G_OS_WIN32
        g_unix_signal_add (SIGINT, unix_signal_handler, NULL);
#else
        signal(SIGINT, interrupt_signal_handler);
#endif /* G_OS_WIN32 */

        g_main_loop_run (main_loop);
        g_main_loop_unref (main_loop);

        g_source_remove (id);

        g_object_unref (cm);

        return EXIT_SUCCESS;
}