Submit version 0.20.1 of GUPnP (4186015)
[profile/ivi/GUPnP.git] / tests / test-proxy.c
1 /*
2  * Copyright (C) 2007 OpenedHand Ltd.
3  *
4  * Author: Jorn Baayen <jorn@openedhand.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library 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 <stdlib.h>
24 #include <string.h>
25 #include <locale.h>
26 #include <signal.h>
27 #include <glib.h>
28
29 GMainLoop *main_loop;
30
31 static void
32 interrupt_signal_handler (int signum)
33 {
34         g_main_loop_quit (main_loop);
35 }
36
37 static void
38 subscription_lost_cb (GUPnPServiceProxy *proxy,
39                       const GError      *reason,
40                       gpointer           user_data)
41 {
42         g_print ("Lost subscription: %s\n", reason->message);
43 }
44
45 static void
46 notify_cb (GUPnPServiceProxy *proxy,
47            const char        *variable,
48            GValue            *value,
49            gpointer           user_data)
50 {
51         g_print ("Received a notification for variable '%s':\n", variable);
52         g_print ("\tvalue:     %d\n", g_value_get_uint (value));
53         g_print ("\tuser_data: %s\n", (const char *) user_data);
54 }
55
56 static void
57 service_proxy_available_cb (GUPnPControlPoint *cp,
58                             GUPnPServiceProxy *proxy)
59 {
60         const char *location;
61         char *result = NULL;
62         guint count, total;
63         GError *error = NULL;
64
65         location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));
66
67         g_print ("ContentDirectory available:\n");
68         g_print ("\tlocation: %s\n", location);
69
70         /* We want to be notified whenever SystemUpdateID (of type uint)
71          * changes */
72         gupnp_service_proxy_add_notify (proxy,
73                                         "SystemUpdateID",
74                                         G_TYPE_UINT,
75                                         notify_cb,
76                                         (gpointer) "Test");
77
78         /* Subscribe */
79         g_signal_connect (proxy,
80                           "subscription-lost",
81                           G_CALLBACK (subscription_lost_cb),
82                           NULL);
83
84         gupnp_service_proxy_set_subscribed (proxy, TRUE);
85
86         /* And test action IO */
87         gupnp_service_proxy_send_action (proxy,
88                                          "Browse",
89                                          &error,
90                                          /* IN args */
91                                          "ObjectID",
92                                                 G_TYPE_STRING,
93                                                 "0",
94                                          "BrowseFlag",
95                                                 G_TYPE_STRING,
96                                                 "BrowseDirectChildren",
97                                          "Filter",
98                                                 G_TYPE_STRING,
99                                                 "*",
100                                          "StartingIndex",
101                                                 G_TYPE_UINT,
102                                                 0,
103                                          "RequestedCount",
104                                                 G_TYPE_UINT,
105                                                 0,
106                                          "SortCriteria",
107                                                 G_TYPE_STRING,
108                                                 "",
109                                          NULL,
110                                          /* OUT args */
111                                          "Result",
112                                                 G_TYPE_STRING,
113                                                 &result,
114                                          "NumberReturned",
115                                                 G_TYPE_UINT,
116                                                 &count,
117                                          "TotalMatches",
118                                                 G_TYPE_UINT,
119                                                 &total,
120                                          NULL);
121
122         if (error) {
123                 g_printerr ("Error: %s\n", error->message);
124                 g_error_free (error);
125
126                 return;
127         }
128
129         g_print ("Browse returned:\n");
130         g_print ("\tResult:         %s\n", result);
131         g_print ("\tNumberReturned: %u\n", count);
132         g_print ("\tTotalMatches:   %u\n", total);
133
134         g_free (result);
135 }
136
137 static void
138 service_proxy_unavailable_cb (GUPnPControlPoint *cp,
139                               GUPnPServiceProxy *proxy)
140 {
141         const char *location;
142
143         location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));
144
145         g_print ("ContentDirectory unavailable:\n");
146         g_print ("\tlocation: %s\n", location);
147 }
148
149 int
150 main (int argc, char **argv)
151 {
152         GError *error;
153         GUPnPContext *context;
154         GUPnPControlPoint *cp;
155 #ifndef G_OS_WIN32
156         struct sigaction sig_action;
157 #endif /* G_OS_WIN32 */
158
159 #if !GLIB_CHECK_VERSION(2,35,0)
160         g_type_init ();
161 #endif
162         setlocale (LC_ALL, "");
163
164         error = NULL;
165         context = g_initable_new (GUPNP_TYPE_CONTEXT, NULL, &error, NULL);
166         if (error) {
167                 g_printerr ("Error creating the GUPnP context: %s\n",
168                             error->message);
169                 g_error_free (error);
170
171                 return EXIT_FAILURE;
172         }
173
174         /* We're interested in everything */
175         cp = gupnp_control_point_new
176                 (context, "urn:schemas-upnp-org:service:ContentDirectory:1");
177
178         g_signal_connect (cp,
179                           "service-proxy-available",
180                           G_CALLBACK (service_proxy_available_cb),
181                           NULL);
182         g_signal_connect (cp,
183                           "service-proxy-unavailable",
184                           G_CALLBACK (service_proxy_unavailable_cb),
185                           NULL);
186
187         gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
188
189         main_loop = g_main_loop_new (NULL, FALSE);
190
191         /* Hook the handler for SIGTERM */
192 #ifndef G_OS_WIN32
193         memset (&sig_action, 0, sizeof (sig_action));
194         sig_action.sa_handler = interrupt_signal_handler;
195         sigaction (SIGINT, &sig_action, NULL);
196 #else
197         signal(SIGINT,interrupt_signal_handler);
198 #endif /* G_OS_WIN32 */
199
200         g_main_loop_run (main_loop);
201         g_main_loop_unref (main_loop);
202
203         g_object_unref (cp);
204         g_object_unref (context);
205
206         return EXIT_SUCCESS;
207 }