Debian Codegraph @

src/discover/gssdp-discover.c

raw
1 /*
2 * Copyright (C) 2011 Nokia Corporation.
3 * Copyright (C) 2014 Jens Georg <[email protected]>
4 *
5 * Author: Jens Georg <[email protected]>
6 * <[email protected]>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23
24 #include <config.h>
25
26 #include <glib.h>
27 #include <libgssdp/gssdp.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 static char *interface = NULL;
32 static char *target = NULL;
33 static char *message_type = NULL;
34 static int timeout = 0;
35 static int rescan_interval = 0;
36
37 typedef struct _GSSDPDiscover {
38 GMainLoop *main_loop;
39 GSSDPClient *client;
40 GSSDPResourceBrowser *browser;
41 } GSSDPDiscover;
42
43 static GOptionEntry entries[] =
44 {
45 { "interface", 'i', 0, G_OPTION_ARG_STRING, &interface, "Network INTERFACE to use", "INTERFACE" },
46 { "target", 't', 0, G_OPTION_ARG_STRING, &target, "SSDP TARGET to search for (default: ssdp:all)", "TARGET" },
47 { "timeout", 'n', 0, G_OPTION_ARG_INT, &timeout, "TIME in seconds to wait for replies before exiting", "TIME" },
48 { "rescan-interval", 'r', 0, G_OPTION_ARG_INT, &rescan_interval, "TIME in seconds to wait before sending another discovery request", "TIME" },
49 { "message-type", 'm', 0, G_OPTION_ARG_STRING, &message_type, "TYPE of message (available,unavailable,all)", "TYPE" },
50 { NULL }
51 };
52
53 static gboolean
54 on_force_rescan_timeout (GSSDPDiscover *discover) {
55 gssdp_resource_browser_set_active (discover->browser,
56 FALSE);
57 gssdp_resource_browser_set_active (discover->browser,
58 TRUE);
59 return TRUE;
60 }
61
62 static void
63 on_resource_available (GSSDPResourceBrowser *browser,
64 const char *usn,
65 GList *locations)
66 {
67 GList *l;
68
69 g_print ("resource available\n"
70 " USN: %s\n",
71 usn);
72
73 for (l = locations; l; l = l->next)
74 g_print (" Location: %s\n", (char *) l->data);
75 }
76
77 static void
78 on_resource_unavailable (GSSDPResourceBrowser *browser,
79 const char *usn)
80 {
81 g_print ("resource unavailable\n"
82 " USN: %s\n",
83 usn);
84 }
85
86
87 int main (int argc, char *argv[]) {
88 GError *error = NULL;
89 GOptionContext *context;
90 GSSDPDiscover discover;
91
92 context = g_option_context_new ("- discover devices using SSDP");
93 g_option_context_add_main_entries (context, entries, NULL);
94
95 if (!g_option_context_parse (context, &argc, &argv, &error)) {
96 g_warning ("Failed to parse options: %s", error->message);
97 g_error_free (error);
98 exit (1);
99 }
100
101 g_option_context_free (context);
102
103 discover.client = gssdp_client_new_full (interface,
104 NULL,
105 0,
106 GSSDP_UDA_VERSION_1_0,
107 &error);
108 if (error != NULL) {
109 g_warning ("Failed to create GSSDP client: %s", error->message);
110 g_error_free (error);
111 exit (1);
112 }
113
114 g_print ("Using network interface %s\n",
115 gssdp_client_get_interface (discover.client));
116
117 if (target) {
118 g_print ("Scanning for resources matching %s\n", target);
119 discover.browser = gssdp_resource_browser_new
120 (discover.client, target);
121 } else {
122 g_print ("Scanning for all resources\n");
123 discover.browser = gssdp_resource_browser_new
124 (discover.client, "ssdp:all");
125 }
126
127 if (message_type == NULL) {
128 message_type = g_strdup ("available");
129 } else {
130 if (strncmp (message_type, "available", 9) != 0 &&
131 strncmp (message_type, "all", 3) != 0 &&
132 strncmp (message_type, "unavailable", 11) != 0) {
133 g_print ("Invalid message type. Use \"all\", ");
134 g_print ("\"available\" or \"unavailable\"\n");
135 exit (1);
136 }
137 }
138
139 if (strncmp (message_type, "available", 9) == 0 ||
140 strncmp (message_type, "all", 3) == 0) {
141 g_signal_connect (discover.browser,
142 "resource-available",
143 G_CALLBACK (on_resource_available),
144 &discover);
145 }
146
147 if (strncmp (message_type, "unavailable", 11) == 0 ||
148 strncmp (message_type, "all", 3) == 0) {
149 g_signal_connect (discover.browser,
150 "resource-unavailable",
151 G_CALLBACK (on_resource_unavailable),
152 &discover);
153 }
154
155 g_print ("Showing \"%s\" messages\n", message_type);
156
157 discover.main_loop = g_main_loop_new (NULL, FALSE);
158
159 gssdp_resource_browser_set_active (discover.browser, TRUE);
160
161 if (timeout > 0)
162 g_timeout_add_seconds (timeout,
163 (GSourceFunc) g_main_loop_quit,
164 discover.main_loop);
165
166 if (rescan_interval > 0 &&
167 (rescan_interval < timeout || timeout == 0))
168 g_timeout_add_seconds (rescan_interval,
169 (GSourceFunc) on_force_rescan_timeout,
170 &discover);
171
172 g_main_loop_run (discover.main_loop);
173
174 g_main_loop_unref (discover.main_loop);
175 g_object_unref (discover.browser);
176 g_object_unref (discover.client);
177
178 g_free (interface);
179 g_free (target);
180 g_free (message_type);
181
182 return 0;
183 }