tizen 2.3.1 release
[external/buxton.git] / demo / hellonotify.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 notify_cb(BuxtonResponse response, void *data)
35 {
36         bool *status = (bool *)data;
37         BuxtonKey key;
38         int32_t *value;
39         char *name;
40
41         if (buxton_response_status(response) != 0) {
42                 *status = false;
43                 return;
44         }
45
46         key = buxton_response_key(response);
47         name = buxton_key_get_name(key);
48
49         value = (int32_t*)buxton_response_value(response);
50         if (value) {
51                 printf("key %s updated with new value %d\n", name, *value);
52         } else {
53                 printf("key %s was removed\n", name);
54         }
55
56         buxton_key_free(key);
57         free(value);
58         free(name);
59 }
60
61 int main(void)
62 {
63         BuxtonClient client;
64         BuxtonKey key;
65         bool status = true;
66         struct pollfd pfd[1];
67         int r;
68         int fd;
69         int repoll_count = 10;
70
71         if ((fd = buxton_open(&client)) < 0) {
72                 printf("couldn't connect\n");
73                 return -1;
74         }
75
76         key = buxton_key_create("hello", "test", NULL, INT32);
77         if (!key) {
78                 return -1;
79         }
80
81         if (buxton_register_notification(client, key, notify_cb, &status, false)) {
82                 printf("set call failed to run\n");
83                 return -1;
84         }
85 repoll:
86         pfd[0].fd = fd;
87         pfd[0].events = POLLIN;
88         pfd[0].revents = 0;
89         r = poll(pfd, 1, 5000);
90
91         if (r < 0) {
92                 printf("poll error\n");
93                 return -1;
94         } else if (r == 0) {
95                 if (repoll_count-- > 0) {
96                         goto out;
97                 }
98                 goto repoll;
99         }
100
101         if (!buxton_client_handle_response(client)) {
102                 printf("bad response from daemon\n");
103                 return -1;
104         }
105
106         if (!status) {
107                 printf("Failed to register for notification\n");
108                 return -1;
109         }
110
111         goto repoll;
112
113 out:
114         if (buxton_unregister_notification(client, key, NULL, NULL, true)) {
115                 printf("Unregistration of notification failed\n");
116                 return -1;
117         }
118
119         buxton_key_free(key);
120         buxton_close(client);
121
122         return 0;
123 }
124
125 /*
126  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
127  *
128  * Local variables:
129  * c-basic-offset: 8
130  * tab-width: 8
131  * indent-tabs-mode: t
132  * End:
133  *
134  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
135  * :indentSize=8:tabSize=8:noTabs=false:
136  */