HomePort
demo_application.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Aalborg University. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Aalborg University ''AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Aalborg University OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  */
27 
28 #include "demo_application.h"
30 #include <hpd/common/hpd_common.h>
31 
32 typedef struct demo_app demo_app_t;
33 
34 struct demo_app {
37 };
38 
39 static hpd_error_t demo_app_on_create(void **data, const hpd_module_t *context);
42 static hpd_error_t demo_app_on_stop(void *data, hpd_t *hpd);
43 static hpd_error_t demo_app_on_parse_opt(void *data, const char *name, const char *arg);
44 
51 };
52 
53 static void demo_app_on_device(demo_app_t *demo_app, const hpd_device_id_t *device, const char *msg)
54 {
55  hpd_error_t rc, rc2;
56 
57  hpd_adapter_id_t *adapter;
58  if ((rc = hpd_device_get_adapter(device, &adapter))) goto error_return;
59 
60  const char *aid;
61  if ((rc = hpd_adapter_get_id(adapter, &aid))) goto error_free_id;
62 
63  const char *did;
64  if ((rc = hpd_device_get_id(device, &did))) goto error_free_id;
65 
66  HPD_LOG_INFO(demo_app->context, msg, aid, did);
67 
68  if ((rc = hpd_adapter_id_free(adapter))) goto error_return;
69  return;
70 
71  error_free_id:
72  if ((rc2 = hpd_adapter_id_free(adapter)))
73  HPD_LOG_ERROR(demo_app->context, "Free function failed [code: %i].", rc2);
74 
75  error_return:
76  HPD_LOG_ERROR(demo_app->context, "%s() failed [code: %i].", __FUNCTION__, rc);
77 }
78 
79 void demo_app_on_attach(void *data, const hpd_device_id_t *device)
80 {
81  demo_app_on_device(data, device, "%s/%s attached");
82 }
83 
84 void demo_app_on_detach(void *data, const hpd_device_id_t *device)
85 {
86  demo_app_on_device(data, device, "%s/%s detached");
87 }
88 
89 void demo_app_on_change(void *data, const hpd_service_id_t *service, const hpd_value_t *val)
90 {
91  hpd_error_t rc, rc2;
93 
94  hpd_adapter_id_t *adapter;
95  if ((rc = hpd_service_get_adapter(service, &adapter))) goto error_return;
96 
97  hpd_device_id_t *device;
98  if ((rc = hpd_service_get_device(service, &device))) goto error_free_adapter;
99 
100  const char *aid;
101  if ((rc = hpd_adapter_get_id(adapter, &aid))) goto error_free_device;
102 
103  const char *did;
104  if ((rc = hpd_device_get_id(device, &did))) goto error_free_device;
105 
106  const char *sid;
107  if ((rc = hpd_service_get_id(service, &sid))) goto error_free_device;
108 
109  const char *v;
110  size_t len;
111  if ((rc = hpd_value_get_body(val, &v, &len))) goto error_free_device;
112 
113  HPD_LOG_INFO(demo_app->context, "%s/%s/%s changed to %.*s", aid, did, sid, len, v);
114 
115  if ((rc = hpd_device_id_free(device))) goto error_free_adapter;
116  if ((rc = hpd_adapter_id_free(adapter))) goto error_return;
117  return;
118 
119  error_free_device:
120  if ((rc2 = hpd_device_id_free(device)))
121  HPD_LOG_ERROR(demo_app->context, "Free function failed [code: %i].", rc2);
122 
123  error_free_adapter:
124  if ((rc2 = hpd_adapter_id_free(adapter)))
125  HPD_LOG_ERROR(demo_app->context, "Free function failed [code: %i].", rc2);
126 
127  error_return:
128  HPD_LOG_ERROR(demo_app->context, "%s() failed [code: %i].", __FUNCTION__, rc);
129 }
130 
131 static hpd_error_t demo_app_on_create(void **data, const hpd_module_t *context)
132 {
134  HPD_CALLOC(demo_app, 1, demo_app_t);
135  demo_app->context = context;
136  (*data) = demo_app;
137  return HPD_E_SUCCESS;
138 
139  alloc_error:
140  HPD_LOG_RETURN_E_ALLOC(context);
141 }
142 
144 {
146  free(demo_app);
147  return HPD_E_SUCCESS;
148 }
149 
151 {
152  hpd_error_t rc, rc2;
153 
155 
156  if ((rc = hpd_listener_alloc(&demo_app->listener, hpd)))
157  goto error_return;
158  if ((rc = hpd_listener_set_data(demo_app->listener, demo_app, NULL)))
159  goto error_free;
161  goto error_free;
163  goto error_free;
164  if ((rc = hpd_foreach_attached(demo_app->listener)))
165  goto error_free;
166  if ((rc = hpd_subscribe(demo_app->listener)))
167  goto error_free;
168 
169  return HPD_E_SUCCESS;
170 
171  error_free:
172  if ((rc2 = hpd_listener_free(demo_app->listener)))
173  HPD_LOG_ERROR(demo_app->context, "Free function failed [code: %i].", rc2);
174  else
175  demo_app->listener = NULL;
176 
177  error_return:
178  return rc;
179 }
180 
182 {
184 
185  return hpd_listener_free(demo_app->listener);
186 }
187 
188 static hpd_error_t demo_app_on_parse_opt(void *data, const char *name, const char *arg)
189 {
190  return HPD_E_ARGUMENT;
191 }
struct hpd_module_def hpd_demo_app_def
hpd_error_t hpd_service_get_adapter(const hpd_service_id_t *sid, hpd_adapter_id_t **aid)
hpd_error_t hpd_listener_alloc(hpd_listener_t **listener, hpd_t *hpd)
[hpd_response_t functions]
Definition: event_api.c:42
static hpd_error_t demo_app_on_destroy(void *data)
static void demo_app_on_device(demo_app_t *demo_app, const hpd_device_id_t *device, const char *msg)
static hpd_error_t demo_app_on_create(void **data, const hpd_module_t *context)
hpd_error_t hpd_value_get_body(const hpd_value_t *value, const char **body, size_t *len)
Definition: value_api.c:87
hpd_error_t hpd_listener_set_value_callback(hpd_listener_t *listener, hpd_value_f on_change)
Definition: event_api.c:54
free(data.url)
void demo_app_on_change(void *data, const hpd_service_id_t *service, const hpd_value_t *val)
#define HPD_LOG_INFO(CONTEXT, FMT,...)
#define HPD_CALLOC(PTR, NUM, CAST)
Allocates and zeros a structure.
Definition: hpd_common.h:44
hpd_error_t hpd_adapter_get_id(const hpd_adapter_id_t *aid, const char **id)
[id_t functions]
hpd_error_t hpd_listener_set_device_callback(hpd_listener_t *listener, hpd_device_f on_attach, hpd_device_f on_detach)
Definition: event_api.c:59
hpd_error_t hpd_device_get_id(const hpd_device_id_t *did, const char **id)
[hpd_adapter_t functions]
hpd_error_t hpd_listener_set_data(hpd_listener_t *listener, void *data, hpd_free_f on_free)
Definition: event_api.c:48
Definition: comm.h:70
const hpd_module_t * context
hpd_error_t hpd_service_get_id(const hpd_service_id_t *sid, const char **id)
[hpd_device_t functions]
enum hpd_error hpd_error_t
Definition: hpd_types.h:167
#define HPD_LOG_RETURN_E_ALLOC(CONTEXT)
[Application API Callbacks]
Definition: hpd_types.h:200
hpd_error_t hpd_device_id_free(hpd_device_id_t *id)
Definition: discovery_api.c:64
void demo_app_on_attach(void *data, const hpd_device_id_t *device)
hpd_error_t hpd_foreach_attached(const hpd_listener_t *listener)
Definition: event_api.c:86
static hpd_error_t demo_app_on_stop(void *data, hpd_t *hpd)
struct data data
Definition: daemon.h:50
static hpd_error_t demo_app_on_parse_opt(void *data, const char *name, const char *arg)
hpd_error_t hpd_service_get_device(const hpd_service_id_t *sid, hpd_device_id_t **did)
hpd_listener_t * listener
hpd_error_t hpd_adapter_id_free(hpd_adapter_id_t *id)
Definition: discovery_api.c:46
static hpd_error_t demo_app_on_start(void *data, hpd_t *hpd)
void demo_app_on_detach(void *data, const hpd_device_id_t *device)
#define HPD_LOG_ERROR(CONTEXT, FMT,...)
hpd_error_t hpd_device_get_adapter(const hpd_device_id_t *did, hpd_adapter_id_t **aid)
hpd_error_t hpd_listener_free(hpd_listener_t *listener)
Definition: event_api.c:74
hpd_error_t hpd_subscribe(hpd_listener_t *listener)
Definition: event_api.c:65