tizen 2.3 release
[external/buxton.git] / demo / notifytest.c
1 /*
2  * This file is part of buxton.
3  *
4  * Copyright (C) 2013 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26
27 #define _GNU_SOURCE
28 #include <poll.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "buxton.h"
33
34 void set_cb(BuxtonResponse response, void *data)
35 {
36         bool *status = (bool *)data;
37         if (buxton_response_status(response) != 0) {
38                 *status = false;
39         } else {
40                 *status = true;
41         }
42 }
43
44 void notify_cb(BuxtonResponse response, void *data)
45 {
46         bool *status = (bool *)data;
47         BuxtonKey key;
48         int32_t *value;
49         char *name;
50
51         if (buxton_response_status(response) != 0) {
52                 *status = false;
53                 return;
54         }
55
56         key = buxton_response_key(response);
57         name = buxton_key_get_name(key);
58
59         value = (int32_t*)buxton_response_value(response);
60         if (value) {
61                 printf("key %s updated with new value %d\n", name, *value);
62         } else {
63                 printf("key %s was removed\n", name);
64         }
65
66         buxton_key_free(key);
67         free(value);
68         free(name);
69 }
70
71 int main(void)
72 {
73         BuxtonClient client;
74         BuxtonKey key1, key2, key3, key4;
75         bool status = true;
76         struct pollfd pfd[1];
77         int r;
78         int fd;
79         int do_update = 0;
80         int32_t val = 10;
81
82         if ((fd = buxton_open(&client)) < 0) {
83                 printf("couldn't connect\n");
84                 return -1;
85         }
86
87         key1 = buxton_key_create("hello", "test1", NULL, INT32);
88         if (!key1) {
89                 return -1;
90         }
91
92         key2 = buxton_key_create("hello", "test2", NULL, INT32);
93         if (!key2) {
94                 return -1;
95         }
96
97         key3 = buxton_key_create("hello", "test3", NULL, INT32);
98         if (!key3) {
99                 return -1;
100         }
101
102         key4 = buxton_key_create("hello", "test1", "user", INT32);
103         if (!key4) {
104                 return -1;
105         }
106
107         if (buxton_register_notification(client, key1, notify_cb, &status, false)) {
108                 printf("set call failed to run\n");
109                 return -1;
110         }
111
112         if (buxton_register_notification(client, key2, notify_cb, &status, false)) {
113                 printf("set call failed to run\n");
114                 return -1;
115         }
116
117         if (buxton_register_notification(client, key3, notify_cb, &status, false)) {
118                 printf("set call failed to run\n");
119                 return -1;
120         }
121
122 repoll:
123         pfd[0].fd = fd;
124         pfd[0].events = POLLIN;
125         pfd[0].revents = 0;
126         r = poll(pfd, 1, 5000);
127
128         if (r < 0) {
129                 printf("poll error\n");
130                 return -1;
131         } else if (r == 0) {
132                 if (do_update >> 1 == 0) {
133                         val++;
134                         if (buxton_set_value(client, key4, &val, set_cb, &status, false)) {
135                                 printf("set value failed\n");
136                                 return -1;
137                         }
138                 } else {
139                         do_update++;
140                 }
141                 goto repoll;
142         }
143
144         if (!buxton_client_handle_response(client)) {
145                 printf("bad response from daemon\n");
146                 return -1;
147         }
148
149         if (!status) {
150                 printf("Failed to register for notification\n");
151                 return -1;
152         }
153
154         goto repoll;
155
156         buxton_key_free(key1);
157         buxton_key_free(key2);
158         buxton_key_free(key3);
159         buxton_close(client);
160
161         return 0;
162 }
163
164 /*
165  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
166  *
167  * Local variables:
168  * c-basic-offset: 8
169  * tab-width: 8
170  * indent-tabs-mode: t
171  * End:
172  *
173  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
174  * :indentSize=8:tabSize=8:noTabs=false:
175  */