Debian Codegraph @

src/network-light/upnp.c

raw
1 /*
2 * Copyright (C) 2007 Zeeshan Ali.
3 * Copyright (C) 2007 OpenedHand Ltd.
4 *
5 * Author: Zeeshan Ali <[email protected]>
6 * Author: Jorn Baayen <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program 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
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23 #include <config.h>
24
25 #include <libgupnp/gupnp-root-device.h>
26 #include <libgupnp/gupnp-service.h>
27 #include <stdio.h>
28 #include <locale.h>
29 #include <string.h>
30 #include <gmodule.h>
31 #include <glib/gstdio.h>
32
33 #include "gui.h"
34 #include "upnp.h"
35 #include "main.h"
36
37 #define DESCRIPTION_DOC "xml/network-light-desc.xml"
38 #define DIMMING_SERVICE "urn:schemas-upnp-org:service:Dimming:1"
39 #define SWITCH_SERVICE "urn:schemas-upnp-org:service:SwitchPower:1"
40 #define NETWORK_LIGHT "urn:schemas-upnp-org:device:DimmableLight:1"
41
42 typedef struct
43 {
44 GUPnPRootDevice *dev;
45 GUPnPServiceInfo *switch_power;
46 GUPnPServiceInfo *dimming;
47 } NetworkLight;
48
49 static GUPnPContextManager *context_manager;
50 static GHashTable *nl_hash;
51
52 /* Other network light services on the network */
53 static GList *switch_proxies;
54 static GList *dimming_proxies;
55
56 static GUPnPXMLDoc *doc;
57 static char *desc_location;
58 static char *uuid;
59
60 void
61 on_get_status (GUPnPService *service,
62 GUPnPServiceAction *action,
63 gpointer user_data);
64
65 void
66 on_get_target (GUPnPService *service,
67 GUPnPServiceAction *action,
68 gpointer user_data);
69
70 void
71 on_set_target (GUPnPService *service,
72 GUPnPServiceAction *action,
73 gpointer user_data);
74
75 void
76 on_query_status (GUPnPService *service,
77 const char *variable_name,
78 GValue *value,
79 gpointer user_data);
80
81 void
82 on_query_target (GUPnPService *service,
83 const char *variable_name,
84 GValue *value,
85 gpointer user_data);
86
87 void
88 on_get_load_level_status (GUPnPService *service,
89 GUPnPServiceAction *action,
90 gpointer user_data);
91
92 void
93 on_get_load_level_target (GUPnPService *service,
94 GUPnPServiceAction *action,
95 gpointer user_data);
96
97 void
98 on_set_load_level_target (GUPnPService *service,
99 GUPnPServiceAction *action,
100 gpointer user_data);
101
102 void
103 on_query_load_level_status (GUPnPService *service,
104 const char *variable_name,
105 GValue *value,
106 gpointer user_data);
107
108 void
109 on_query_load_level_target (GUPnPService *service,
110 const char *variable_name,
111 GValue *value,
112 gpointer user_data);
113
114 static NetworkLight *
115 network_light_new (GUPnPRootDevice *dev,
116 GUPnPServiceInfo *switch_power,
117 GUPnPServiceInfo *dimming)
118 {
119 NetworkLight *network_light;
120
121 network_light = g_slice_new (NetworkLight);
122
123 network_light->dev = dev;
124 network_light->switch_power = switch_power;
125 network_light->dimming = dimming;
126
127 return network_light;
128 }
129
130 static void
131 network_light_free (NetworkLight *network_light)
132 {
133 g_object_unref (network_light->dev);
134 g_object_unref (network_light->switch_power);
135 g_object_unref (network_light->dimming);
136
137 g_slice_free (NetworkLight, network_light);
138 }
139
140 void
141 notify_status_change (gboolean status)
142 {
143 GList *network_lights;
144 GList *nl_node;
145
146 network_lights = g_hash_table_get_values (nl_hash);
147
148 for (nl_node = network_lights;
149 nl_node != NULL;
150 nl_node = nl_node->next) {
151 NetworkLight *nl = (NetworkLight *) nl_node->data;
152
153 gupnp_service_notify (GUPNP_SERVICE (nl->switch_power),
154 "Status",
155 G_TYPE_BOOLEAN,
156 status,
157 NULL);
158 }
159
160 g_list_free (network_lights);
161 }
162
163 void
164 notify_load_level_change (gint load_level)
165 {
166 GList *network_lights;
167 GList *nl_node;
168
169 network_lights = g_hash_table_get_values (nl_hash);
170
171 for (nl_node = network_lights;
172 nl_node != NULL;
173 nl_node = nl_node->next) {
174 NetworkLight *nl = (NetworkLight *) nl_node->data;
175
176 gupnp_service_notify (GUPNP_SERVICE (nl->dimming),
177 "LoadLevelStatus",
178 G_TYPE_UINT,
179 load_level,
180 NULL);
181 }
182
183 g_list_free (network_lights);
184 }
185
186 G_MODULE_EXPORT
187 void
188 on_get_status (GUPnPService *service,
189 GUPnPServiceAction *action,
190 gpointer user_data)
191 {
192 gupnp_service_action_set (action,
193 "ResultStatus",
194 G_TYPE_BOOLEAN,
195 get_status (),
196 NULL);
197
198 gupnp_service_action_return_success (action);
199 }
200
201 G_MODULE_EXPORT
202 void
203 on_get_target (GUPnPService *service,
204 GUPnPServiceAction *action,
205 gpointer user_data)
206 {
207 gupnp_service_action_set (action,
208 "RetTargetValue",
209 G_TYPE_BOOLEAN,
210 get_status (),
211 NULL);
212
213 gupnp_service_action_return_success (action);
214 }
215
216 G_MODULE_EXPORT
217 void
218 on_set_target (GUPnPService *service,
219 GUPnPServiceAction *action,
220 gpointer user_data)
221 {
222 gboolean status;
223
224 gupnp_service_action_get (action,
225 "newTargetValue",
226 G_TYPE_BOOLEAN,
227 &status,
228 NULL);
229 gupnp_service_action_return_success (action);
230
231 set_status (status);
232 }
233
234 G_MODULE_EXPORT
235 void
236 on_query_status (GUPnPService *service,
237 const char *variable_name,
238 GValue *value,
239 gpointer user_data)
240 {
241 g_value_init (value, G_TYPE_BOOLEAN);
242 g_value_set_boolean (value, get_status ());
243 }
244
245 G_MODULE_EXPORT
246 void
247 on_query_target (GUPnPService *service,
248 const char *variable_name,
249 GValue *value,
250 gpointer user_data)
251 {
252 g_value_init (value, G_TYPE_BOOLEAN);
253 g_value_set_boolean (value, get_status ());
254 }
255
256 G_MODULE_EXPORT
257 void
258 on_get_load_level_status (GUPnPService *service,
259 GUPnPServiceAction *action,
260 gpointer user_data)
261 {
262 gupnp_service_action_set (action,
263 "retLoadlevelStatus",
264 G_TYPE_UINT,
265 get_load_level (),
266 NULL);
267
268 gupnp_service_action_return_success (action);
269 }
270
271 G_MODULE_EXPORT
272 void
273 on_get_load_level_target (GUPnPService *service,
274 GUPnPServiceAction *action,
275 gpointer user_data)
276 {
277 gupnp_service_action_set (action,
278 "retLoadlevelTarget",
279 G_TYPE_UINT,
280 get_load_level (),
281 NULL);
282
283 gupnp_service_action_return_success (action);
284 }
285
286 G_MODULE_EXPORT
287 void
288 on_set_load_level_target (GUPnPService *service,
289 GUPnPServiceAction *action,
290 gpointer user_data)
291 {
292 guint load_level;
293
294 gupnp_service_action_get (action,
295 "newLoadlevelTarget",
296 G_TYPE_UINT,
297 &load_level,
298 NULL);
299 gupnp_service_action_return_success (action);
300
301 if (load_level > 100)
302 load_level = 100;
303
304 set_load_level (load_level);
305 }
306
307 G_MODULE_EXPORT
308 void
309 on_query_load_level_status (GUPnPService *service,
310 const char *variable_name,
311 GValue *value,
312 gpointer user_data)
313 {
314 g_value_init (value, G_TYPE_UINT);
315 g_value_set_uint (value, get_load_level ());
316 }
317
318 G_MODULE_EXPORT
319 void
320 on_query_load_level_target (GUPnPService *service,
321 const char *variable_name,
322 GValue *value,
323 gpointer user_data)
324 {
325 g_value_init (value, G_TYPE_UINT);
326 g_value_set_uint (value, get_load_level ());
327 }
328
329 static void
330 on_notify_failed (GUPnPService *service,
331 const GList *callback_urls,
332 const GError *reason,
333 gpointer user_data)
334 {
335 GList *url_node;
336 GString *warning;
337
338 warning = g_string_new (NULL);
339 g_string_printf (warning,
340 "NOTIFY failed for the following client URLs:\n");
341 for (url_node = (GList *) callback_urls;
342 url_node;
343 url_node = url_node->next) {
344 g_string_append_printf (warning,
345 "%s\n",
346 (char *) url_node->data);
347 }
348 g_string_append_printf (warning, "Reason: %s", reason->message);
349
350 g_warning ("%s", warning->str);
351 g_string_free (warning, TRUE);
352 }
353
354 /* Copied from gupnp/libgupnp/xml-utils.c */
355 static xmlNode *
356 xml_util_get_element (xmlNode *node,
357 ...)
358 {
359 va_list var_args;
360
361 va_start (var_args, node);
362
363 while (TRUE) {
364 const char *arg;
365
366 arg = va_arg (var_args, const char *);
367 if (!arg)
368 break;
369
370 for (node = node->children; node; node = node->next) {
371 if (node->name == NULL)
372 continue;
373
374 if (!strcmp (arg, (char *) node->name))
375 break;
376 }
377
378 if (!node)
379 break;
380 }
381
382 va_end (var_args);
383
384 return node;
385 }
386
387 static void init_friendly_name (gchar *name)
388 {
389 xmlNode *fdn_node;
390
391 fdn_node = xml_util_get_element ((xmlNode *)
392 gupnp_xml_doc_get_doc (doc),
393 "root",
394 "device",
395 "friendlyName",
396 NULL);
397 if (fdn_node == NULL) {
398 g_warning ("Failed to find friendly name element"
399 "in device description, "
400 "using default value");
401
402 return;
403 }
404
405 xmlNodeSetContent (fdn_node, (unsigned char *) name);
406 }
407
408 static void init_uuid (void)
409 {
410 xmlNode *uuid_node;
411 char *udn;
412
413 uuid = g_uuid_string_random ();
414 const xmlDoc *xml_doc = gupnp_xml_doc_get_doc (doc);
415
416 uuid_node = xml_util_get_element ((xmlNode *) xml_doc,
417 "root",
418 "device",
419 "UDN",
420 NULL);
421 if (uuid_node == NULL) {
422 g_critical ("Failed to find UDN element"
423 "in device description");
424
425 return;
426 }
427
428 udn = g_strdup_printf ("uuid:%s", uuid);
429
430 xmlNodeSetContent (uuid_node, (unsigned char *) udn);
431
432 g_free (udn);
433 }
434
435 static void
436 on_service_proxy_action_ret (GObject *object,
437 GAsyncResult *result,
438 gpointer user_data)
439 {
440 GError *error = NULL;
441 GUPnPServiceProxyAction *action;
442
443 action = gupnp_service_proxy_call_action_finish (
444 GUPNP_SERVICE_PROXY (object),
445 result,
446 &error);
447
448 if (error == NULL) {
449 // Check for SOAP transport error
450 gupnp_service_proxy_action_get_result (action, &error, NULL);
451 }
452
453 if (error != NULL) {
454 GUPnPServiceInfo *info = GUPNP_SERVICE_INFO (object);
455
456 g_warning ("Failed to call action \"%s\" on \"%s\": %s",
457 (char *) user_data,
458 gupnp_service_info_get_location (info),
459 error->message);
460
461 g_error_free (error);
462 }
463
464 g_free (user_data);
465 }
466
467 void
468 set_all_status (gboolean status)
469 {
470 GList *proxy_node;
471
472 for (proxy_node = switch_proxies;
473 proxy_node;
474 proxy_node = g_list_next (proxy_node)) {
475 GUPnPServiceProxy *proxy;
476 char *action_name;
477 GUPnPServiceProxyAction *action;
478
479 proxy = GUPNP_SERVICE_PROXY (proxy_node->data);
480 action_name = g_strdup ("SetTarget");
481
482 action = gupnp_service_proxy_action_new (action_name,
483 "newTargetValue",
484 G_TYPE_BOOLEAN,
485 status,
486 NULL);
487
488 gupnp_service_proxy_call_action_async (
489 proxy,
490 action,
491 NULL,
492 on_service_proxy_action_ret,
493 action_name);
494 gupnp_service_proxy_action_unref (action);
495 }
496 }
497
498 void
499 set_all_load_level (gint load_level)
500 {
501 GList *proxy_node;
502
503 for (proxy_node = dimming_proxies;
504 proxy_node;
505 proxy_node = g_list_next (proxy_node)) {
506 GUPnPServiceProxy *proxy;
507 char *action_name;
508 GUPnPServiceProxyAction *action;
509
510 proxy = GUPNP_SERVICE_PROXY (proxy_node->data);
511 action_name = g_strdup ("SetLoadLevelTarget");
512
513 action = gupnp_service_proxy_action_new (action_name,
514 "newLoadlevelTarget",
515 G_TYPE_UINT,
516 load_level,
517 NULL);
518
519 gupnp_service_proxy_call_action_async (
520 proxy,
521 action,
522 NULL,
523 on_service_proxy_action_ret,
524 action_name);
525
526 gupnp_service_proxy_action_unref (action);
527 }
528 }
529
530 static void
531 on_network_light_available (GUPnPControlPoint *cp,
532 GUPnPDeviceProxy *light_proxy,
533 gpointer user_data)
534 {
535 GUPnPServiceProxy *switch_proxy;
536 GUPnPServiceProxy *dimming_proxy;
537 GUPnPServiceInfo *info;
538
539 info = gupnp_device_info_get_service (GUPNP_DEVICE_INFO (light_proxy),
540 SWITCH_SERVICE);
541 switch_proxy = GUPNP_SERVICE_PROXY (info);
542
543 if (switch_proxy) {
544 switch_proxies = g_list_append (switch_proxies, switch_proxy);
545 }
546
547 info = gupnp_device_info_get_service (GUPNP_DEVICE_INFO (light_proxy),
548 DIMMING_SERVICE);
549 dimming_proxy = GUPNP_SERVICE_PROXY (info);
550
551 if (dimming_proxy) {
552 dimming_proxies = g_list_append (dimming_proxies,
553 dimming_proxy);
554 }
555 }
556
557 static gint compare_service (GUPnPServiceInfo *info1,
558 GUPnPServiceInfo *info2)
559 {
560 const char *udn1;
561 const char *udn2;
562
563 udn1 = gupnp_service_info_get_udn (info1);
564 udn2 = gupnp_service_info_get_udn (info2);
565
566 return strcmp (udn1, udn2);
567 }
568
569 static void remove_service_from_list (GUPnPServiceInfo *info,
570 GList **list)
571 {
572 GList *proxy_node;
573
574 proxy_node = g_list_find_custom (*list,
575 info,
576 (GCompareFunc) compare_service);
577 if (proxy_node) {
578 g_object_unref (proxy_node->data);
579 *list = g_list_remove (*list, proxy_node->data);
580 }
581 }
582
583 static void
584 on_network_light_unavailable (GUPnPControlPoint *control_point,
585 GUPnPDeviceProxy *light_proxy,
586 gpointer user_data)
587 {
588 GUPnPServiceInfo *info;
589
590 info = gupnp_device_info_get_service (GUPNP_DEVICE_INFO (light_proxy),
591 SWITCH_SERVICE);
592 if (info) {
593 remove_service_from_list (info, &switch_proxies);
594 }
595
596 g_clear_object (&info);
597
598 info = gupnp_device_info_get_service (GUPNP_DEVICE_INFO (light_proxy),
599 DIMMING_SERVICE);
600 if (info) {
601 remove_service_from_list (info, &dimming_proxies);
602 }
603
604 g_clear_object (&info);
605 }
606
607 static gboolean
608 init_server (GUPnPContext *context)
609 {
610 NetworkLight *network_light;
611 GUPnPRootDevice *dev;
612 GUPnPServiceInfo *switch_power;
613 GUPnPServiceInfo *dimming;
614 GError *error = NULL;
615
616 /* Create root device */
617 dev = gupnp_root_device_new_full (context,
618 gupnp_resource_factory_get_default (),
619 doc,
620 desc_location,
621 DATA_DIR,
622 &error);
623 if (error != NULL) {
624 g_warning ("Failed to create root device: %s",
625 error->message);
626 g_error_free (error);
627
628 return FALSE;
629 }
630
631 switch_power = gupnp_device_info_get_service (GUPNP_DEVICE_INFO (dev),
632 SWITCH_SERVICE);
633
634 if (switch_power) {
635 gupnp_service_signals_autoconnect
636 (GUPNP_SERVICE (switch_power),
637 NULL,
638 &error);
639
640 g_signal_connect (switch_power,
641 "notify-failed",
642 G_CALLBACK (on_notify_failed),
643 NULL);
644 }
645
646 dimming = gupnp_device_info_get_service (GUPNP_DEVICE_INFO (dev),
647 DIMMING_SERVICE);
648
649 if (dimming) {
650 gupnp_service_signals_autoconnect (GUPNP_SERVICE (dimming),
651 NULL,
652 &error);
653
654 g_signal_connect (dimming,
655 "notify-failed",
656 G_CALLBACK (on_notify_failed),
657 NULL);
658 }
659
660 network_light = network_light_new (dev, switch_power, dimming);
661 g_hash_table_insert (nl_hash, g_object_ref (context), network_light);
662
663 /* Run */
664 gupnp_root_device_set_available (dev, TRUE);
665
666 g_print ("Attaching to IP/Host %s on port %d\n",
667 gssdp_client_get_host_ip (GSSDP_CLIENT (context)),
668 gupnp_context_get_port (context));
669
670 return TRUE;
671 }
672
673 static gboolean
674 prepare_desc (gchar *name)
675 {
676 GError *error = NULL;
677
678 doc = gupnp_xml_doc_new_from_path (DATA_DIR "/" DESCRIPTION_DOC,
679 &error);
680 if (doc == NULL) {
681 g_critical ("Unable to load the XML description file: %s",
682 error->message);
683
684 g_error_free (error);
685
686 return FALSE;
687 }
688
689 if (name && (strlen(name) > 0)) {
690 /* set the friendlyName in the xmlDoc */
691 init_friendly_name (name);
692 }
693
694 /* create and set the UUID in the xmlDoc */
695 init_uuid ();
696
697 /* saving the xml file to the temporal location with the uuid name */
698 desc_location = g_strdup_printf ("%s/gupnp-network-light-%s.xml",
699 g_get_tmp_dir (),
700 uuid);
701 g_assert (desc_location != NULL);
702
703 if (xmlSaveFile (desc_location, (xmlDoc *) gupnp_xml_doc_get_doc (doc)) < 0) {
704 g_print ("Error saving description file to %s.\n",
705 desc_location);
706
707 g_free (desc_location);
708 g_object_unref (doc);
709
710 return FALSE;
711 }
712
713 return TRUE;
714 }
715
716 static gboolean
717 init_client (GUPnPContext *context)
718 {
719 GUPnPControlPoint *cp;
720
721 cp = gupnp_control_point_new (context, NETWORK_LIGHT);
722
723 g_signal_connect (cp,
724 "device-proxy-available",
725 G_CALLBACK (on_network_light_available),
726 NULL);
727 g_signal_connect (cp,
728 "device-proxy-unavailable",
729 G_CALLBACK (on_network_light_unavailable),
730 NULL);
731
732 gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
733
734 /* Let context manager take care of the control point life cycle */
735 gupnp_context_manager_manage_control_point (context_manager, cp);
736
737 /* We don't need to keep our own references to the control points */
738 g_object_unref (cp);
739
740 return TRUE;
741 }
742
743 static void
744 on_context_available (GUPnPContextManager *manager,
745 GUPnPContext *context,
746 gpointer user_data)
747 {
748 /* Initialize client-side stuff */
749 init_client (context);
750
751 /* Then the server-side stuff */
752 init_server (context);
753 }
754
755 static void
756 on_context_unavailable (GUPnPContextManager *manager,
757 GUPnPContext *context,
758 gpointer user_data)
759 {
760 g_print ("Detaching from IP/Host %s and port %d\n",
761 gssdp_client_get_host_ip (GSSDP_CLIENT (context)),
762 gupnp_context_get_port (context));
763
764 g_hash_table_remove (nl_hash, context);
765 }
766
767 static gboolean
768 context_equal (GUPnPContext *context1, GUPnPContext *context2)
769 {
770 return g_ascii_strcasecmp (gssdp_client_get_host_ip (GSSDP_CLIENT (context1)),
771 gssdp_client_get_host_ip (GSSDP_CLIENT (context2))) == 0;
772 }
773
774 gboolean
775 init_upnp (gchar **interfaces, guint port, gchar *name, gboolean ipv4, gboolean ipv6)
776 {
777 GUPnPContextFilter *context_filter;
778
779 switch_proxies = NULL;
780 dimming_proxies = NULL;
781
782 nl_hash = g_hash_table_new_full (g_direct_hash,
783 (GEqualFunc) context_equal,
784 g_object_unref,
785 (GDestroyNotify) network_light_free);
786
787 if (!prepare_desc (name)) {
788 return FALSE;
789 }
790
791 // Default: Both
792 GSocketFamily family = G_SOCKET_FAMILY_INVALID;
793 if (!ipv4 && ipv6) {
794 g_debug ("Option a");
795 family = G_SOCKET_FAMILY_IPV6;
796 } else if (ipv4 && !ipv6) {
797 g_debug ("Option b");
798 family = G_SOCKET_FAMILY_IPV4;
799 } else {
800 g_debug ("Option c");
801 // Neither? Just do nothing and enable both
802 }
803
804 context_manager = gupnp_context_manager_create_full (GSSDP_UDA_VERSION_1_0,
805 family,
806 port);
807 g_assert (context_manager != NULL);
808
809 if (interfaces != NULL) {
810 context_filter = gupnp_context_manager_get_context_filter (
811 context_manager);
812 gupnp_context_filter_add_entryv (context_filter, interfaces);
813 gupnp_context_filter_set_enabled (context_filter, TRUE);
814 }
815
816 g_signal_connect (context_manager,
817 "context-available",
818 G_CALLBACK (on_context_available),
819 NULL);
820 g_signal_connect (context_manager,
821 "context-unavailable",
822 G_CALLBACK (on_context_unavailable),
823 NULL);
824
825 return TRUE;
826 }
827
828 void
829 deinit_upnp (void)
830 {
831 g_object_unref (context_manager);
832
833 g_hash_table_unref (nl_hash);
834
835 g_list_foreach (switch_proxies, (GFunc) g_object_unref, NULL);
836 g_list_foreach (dimming_proxies, (GFunc) g_object_unref, NULL);
837
838 /* Unref the descriptiont doc */
839 g_object_unref (doc);
840
841 if (g_remove (desc_location) != 0)
842 g_warning ("error removing %s\n", desc_location);
843 g_free (desc_location);
844 g_free (uuid);
845 uuid = NULL;
846 }
847