tizen 2.3 release
[external/buxton.git] / demo / helloget.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 #include <string.h>
32
33 #include "buxton.h"
34
35 void get_cb(BuxtonResponse response, void *data)
36 {
37         int32_t* ret = (int32_t*)data;
38
39         if (buxton_response_status(response) != 0) {
40                 printf("Failed to get value\n");
41                 return;
42         }
43
44         *ret = *(int32_t*)buxton_response_value(response);
45 }
46
47 int main(void)
48 {
49         BuxtonClient client;
50         BuxtonKey key;
51         struct pollfd pfd[1];
52         int r;
53         int32_t gvalue = -1;
54         int fd;
55
56         if ((fd = buxton_open(&client)) < 0) {
57                 printf("couldn't connect\n");
58                 return -1;
59         }
60
61         key = buxton_key_create("hello", "test", "user", INT32);
62         if (!key) {
63                 return -1;
64         }
65
66         if (buxton_get_value(client, key, get_cb,
67                              &gvalue, false)) {
68                 printf("get call failed to run\n");
69                 return -1;
70         }
71
72         pfd[0].fd = fd;
73         pfd[0].events = POLLIN;
74         pfd[0].revents = 0;
75         r = poll(pfd, 1, 5000);
76
77         if (r <= 0) {
78                 printf("poll error\n");
79                 return -1;
80         }
81
82         if (!buxton_client_handle_response(client)) {
83                 printf("bad response from daemon\n");
84                 return -1;
85         }
86
87         if (gvalue >= 0) {
88                 printf("got value: %d\n", gvalue);
89         }
90
91         buxton_key_free(key);
92         buxton_close(client);
93         return 0;
94 }
95
96 /*
97  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
98  *
99  * Local variables:
100  * c-basic-offset: 8
101  * tab-width: 8
102  * indent-tabs-mode: t
103  * End:
104  *
105  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
106  * :indentSize=8:tabSize=8:noTabs=false:
107  */