static gboolean init_server (GUPnPContext *context) { NetworkLight *network_light; GUPnPRootDevice *dev; GUPnPServiceInfo *switch_power; GUPnPServiceInfo *dimming; GError *error = NULL; /* Create root device */ dev = gupnp_root_device_new_full (context, gupnp_resource_factory_get_default (), doc, desc_location, DATA_DIR); switch_power = gupnp_device_info_get_service (GUPNP_DEVICE_INFO (dev), SWITCH_SERVICE); if (switch_power) { gupnp_service_signals_autoconnect (GUPNP_SERVICE (switch_power), NULL, &error); g_signal_connect (switch_power, "notify-failed", G_CALLBACK (on_notify_failed), NULL); } dimming = gupnp_device_info_get_service (GUPNP_DEVICE_INFO (dev), DIMMING_SERVICE); if (dimming) { gupnp_service_signals_autoconnect (GUPNP_SERVICE (dimming), NULL, &error); g_signal_connect (dimming, "notify-failed", G_CALLBACK (on_notify_failed), NULL); } network_light = network_light_new (dev, switch_power, dimming); g_hash_table_insert (nl_hash, g_object_ref (context), network_light); /* Run */ gupnp_root_device_set_available (dev, TRUE); g_print ("Attaching to IP/Host %s on port %d\n", gupnp_context_get_host_ip (context), gupnp_context_get_port (context)); return TRUE; }
int main (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv) { GOptionContext *optionContext; GMainLoop *main_loop; GError *error = NULL; GUPnPContext *context; GUPnPRootDevice *dev; GUPnPServiceInfo *service; #if !GLIB_CHECK_VERSION(2,35,0) g_type_init (); #endif optionContext = g_option_context_new (NULL); g_option_context_add_main_entries (optionContext, entries, NULL); if (!g_option_context_parse (optionContext, &argc, &argv, &error)) { g_print ("option parsing failed: %s\n", error->message); exit (1); } /* By default the light is off */ status = FALSE; if (!quiet) { g_print ("The light is now %s.\n", status ? "on" : "off"); } /* Create the UPnP context */ context = gupnp_context_new (NULL, NULL, 0, &error); if (error) { g_printerr ("Error creating the GUPnP context: %s\n", error->message); g_error_free (error); return EXIT_FAILURE; } /* Create root device */ dev = gupnp_root_device_new (context, "BinaryLight1.xml", "."); gupnp_root_device_set_available (dev, TRUE); /* Get the switch service from the root device */ service = gupnp_device_info_get_service (GUPNP_DEVICE_INFO (dev), "urn:schemas-upnp-org:service:SwitchPower:1"); if (!service) { g_printerr ("Cannot get SwitchPower1 service\n"); return EXIT_FAILURE; } /* Autoconnect the action and state variable handlers. This connects query_target_cb and query_status_cb to the Target and Status state variables query callbacks, and set_target_cb, get_target_cb and get_status_cb to SetTarget, GetTarget and GetStatus actions respectively. */ gupnp_service_signals_autoconnect (GUPNP_SERVICE (service), NULL, &error); if (error) { g_printerr ("Failed to autoconnect signals: %s\n", error->message); g_error_free (error); return EXIT_FAILURE; } /* Run the main loop */ main_loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (main_loop); /* Cleanup */ g_main_loop_unref (main_loop); g_object_unref (service); g_object_unref (dev); g_object_unref (context); return EXIT_SUCCESS; }