summaryrefslogtreecommitdiff
path: root/examples/light-client.c
blob: b3eae9563b6b289104753c8385406a6123fe847c (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
/*
 * Example UPnP device/service, implementing the BinaryLight device and
 * SwitchPower services to emulate a light switch.
 *
 * The user interface is as minimal as possible so that the GUPnP concepts and
 * best practises are more apparent.  For a better implementation of
 * BinaryLight, see gupnp-tools.
 *
 * This example code is in the public domain.
 */

#include <libgupnp/gupnp.h>
#include <stdlib.h>

static GMainLoop *main_loop;

static enum {
  OFF = 0,
  ON = 1,
  TOGGLE
} mode;

static gboolean quiet;
static gint repeat_counter = 1;
static gint repeat_delay;

static GOptionEntry entries[] =
{
  { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, "Turn off output", NULL },
  { "repeat-counter", 'c', 0, G_OPTION_ARG_INT, &repeat_counter, "Repeat counter", "value" },
  { "repeat-delay", 'd', 0, G_OPTION_ARG_INT, &repeat_delay, "Delay in [ms] between each iteration", "value" },
  { NULL }
};


static void
send_cmd (GUPnPServiceProxy *proxy)
{
  GError *error = NULL;
  gboolean target;

  GUPnPServiceProxyAction *action;

  if (mode == TOGGLE) {
    /* We're toggling, so first fetch the current status */

    action = gupnp_service_proxy_action_new ("GetStatus", NULL);

    gupnp_service_proxy_call_action (proxy, action, NULL, &error);
    if (error != NULL)
      goto error;

    gupnp_service_proxy_action_get_result (action,
                                           &error,
                                           "ResultStatus", G_TYPE_BOOLEAN, &target, NULL);
    g_clear_pointer (&action, gupnp_service_proxy_action_unref);

    if (error != NULL)
      goto error;

    /* And then toggle it */
    target = ! target;

  } else {
    /* Mode is a boolean, so the target is the mode thanks to our well chosen
       enumeration values. */
    target = mode;
  }

  /* Set the target */

  action = gupnp_service_proxy_action_new ("SetTarget",
                                           "newTargetValue", G_TYPE_BOOLEAN, target, NULL);
  gupnp_service_proxy_call_action (proxy, action, NULL, &error);
  g_clear_pointer (&action, gupnp_service_proxy_action_unref);
  if (error != NULL) {
          goto error;
  } else {
    if (!quiet) {
      g_print ("Set switch to %s.\n", target ? "on" : "off");
    }
  }
  
 done:
  /* Only manipulate the first light switch that is found */
  if (--repeat_counter <= 0) {
    g_main_loop_quit (main_loop);
  }
  return;

 error:
  g_clear_pointer (&action, gupnp_service_proxy_action_unref);
  g_printerr ("Cannot set switch: %s\n", error->message);
  g_error_free (error);
  goto done;
}

static gboolean timeout_func (gpointer user_data)
{
  GUPnPServiceProxy *proxy = (GUPnPServiceProxy *) user_data;

  send_cmd (proxy);
  return TRUE;
}

static void
service_proxy_available_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
                            GUPnPServiceProxy               *proxy)
{
  if (repeat_counter>0)
  {
    send_cmd (proxy);
    g_timeout_add (repeat_delay, timeout_func, proxy);
  } else {
    g_main_loop_quit (main_loop);
  }
}

static void
usage (GOptionContext *optionContext)
{
  gchar *help_text;

  help_text = g_option_context_get_help (optionContext, TRUE, NULL);
  g_printerr ("%s", help_text);
  g_free (help_text);
}

int
main (int argc, char **argv)
{
  GOptionContext *optionContext;
  GError *error = NULL;
  GUPnPContext *context;
  GUPnPControlPoint *cp;

  optionContext = g_option_context_new ("[on|off|toggle]");
  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);
  }

  /* Check and parse command line arguments */
  if (argc != 2) {
    usage (optionContext);
    return EXIT_FAILURE;
  }
  
  if (g_str_equal (argv[1], "on")) {
    mode = ON;
  } else if (g_str_equal (argv[1], "off")) {
    mode = OFF;
  } else if (g_str_equal (argv[1], "toggle")) {
    mode = TOGGLE;
  } else {
    usage (optionContext);
    return EXIT_FAILURE;
  }

  /* Create the UPnP context */
  context = gupnp_context_new_for_address (NULL,
                                           0,
                                           GSSDP_UDA_VERSION_1_0,
                                           &error);
  if (error) {
    g_printerr ("Error creating the GUPnP context: %s\n",
		error->message);
    g_error_free (error);

    return EXIT_FAILURE;
  }

  /* Create the control point, searching for SwitchPower services */
  cp = gupnp_control_point_new (context,
				"urn:schemas-upnp-org:service:SwitchPower:1");
  
  /* Connect to the service-found callback */
  g_signal_connect (cp,
                    "service-proxy-available",
                    G_CALLBACK (service_proxy_available_cb),
                    NULL);
  
  /* Start searching when the main loop runs */
  gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);

  /* 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 (cp);
  g_object_unref (context);
  
  return EXIT_SUCCESS;
}