4 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * @file buxton-helper.c
23 * @desc Helper function for simplification of using and
24 * for looking like vconf interface
27 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
36 #include "buxton-helper.h"
40 #define BUXTON_ATTEMPT_NUMBER 5
42 static BuxtonClient client;
44 struct callback_context {
45 struct buxton_variant *b_variant;
49 void general_get_callback(BuxtonResponse response, void *data)
52 struct buxton_variant *b_variant;
53 struct callback_context *context;
56 BuxtonDataType key_type;
58 ret_msg_if(data == NULL,
59 "Invalid pointer argument!");
60 context = (struct callback_context *)data;
61 ret_msg_if(context->b_variant == NULL,
62 "Invalid b_variant argument!");
64 b_variant = context->b_variant;
65 context->error_code = -1;
67 ret = buxton_response_status(response);
68 ret_msg_if(ret, "Invalid buxton response");
70 p = buxton_response_value(response);
72 ret_msg_if(p == NULL, "Bad buxton response: %s", strerror(errno));
74 key = buxton_response_key(response);
80 key_type = buxton_key_get_type(key);
81 if(key_type != b_variant->type) {
82 _E("Not proper type");
88 b_variant->string_value = (char *)p;
91 b_variant->int32_value = *(int32_t *)p;
94 b_variant->uint32_value = *(uint32_t *)p;
97 b_variant->int64_value = *(int64_t *)p;
100 b_variant->uint64_value = *(uint64_t *)p;
103 b_variant->float_value = *(float *)p;
106 b_variant->double_value = *(double *)p;
109 b_variant->bool_value = *(bool *)p;
115 context->error_code = 0;
117 /* we need to free only in case of data types which was copied by
118 * _value_, string's pointer will be used by client */
119 if (key_type != STRING)
124 static int init_buxton_client(void)
131 ret = buxton_open(&client);
132 ret_value_msg_if(ret <= 0, -1, "couldn't connect to buxton server");
137 void finilize_buxton_client(void)
139 /* buxton_close is robust to null pointer */
140 buxton_close(client);
143 int buxton_get_var(char *layer_name, char *group_name,
144 char *key_name, struct buxton_variant *value)
150 struct callback_context context = {
155 ret_value_msg_if(value == NULL, -1,
156 "Please provide valid pointer!");
159 ret = init_buxton_client();
161 ret_value_if(ret, ret);
163 key = buxton_key_create(group_name, key_name,
164 layer_name, value->type);
166 ret = buxton_get_value(client, key, general_get_callback, &context,
168 buxton_key_free(key);
169 /* don't return buxton error code, need to return internal error
171 if (errno == EPIPE && attempts < BUXTON_ATTEMPT_NUMBER) {
172 buxton_close(client);
176 return ret || context.error_code ? -1 : 0;
179 void general_set_callback(BuxtonResponse response, void *data)
186 ret_msg_if(data == NULL, "Please provide *data for return value!");
188 resp_ret = buxton_response_status(response);
191 _E("Failed to set value\n");
196 key = buxton_response_key(response);
197 name = buxton_key_get_name(key);
198 _I("Set value for key %s\n", name);
199 buxton_key_free(key);
204 /* for avoid ptr unligtment */
205 static void *get_variant_align(struct buxton_variant *b_variant)
207 switch (b_variant->type) {
209 return b_variant->string_value;
211 return &b_variant->int32_value;
213 return &b_variant->uint32_value;
215 return &b_variant->int64_value;
217 return &b_variant->uint64_value;
219 return &b_variant->float_value;
221 return &b_variant->double_value;
223 return &b_variant->bool_value;
228 int buxton_set_var(char *layer_name, char *group_name,
229 char *key_name, struct buxton_variant *value)
235 ret_value_msg_if(value == NULL, -1, "Please provide valid pointer");
238 ret = init_buxton_client();
240 ret_value_if(ret, ret);
242 key = buxton_key_create(group_name, key_name,
243 layer_name, value->type);
245 ret = buxton_set_value(client, key, get_variant_align(value),
246 general_set_callback, &ret_data, true);
247 buxton_key_free(key);
249 if (errno == EPIPE && attempts < BUXTON_ATTEMPT_NUMBER) {
250 buxton_close(client);
255 return ret || ret_data ? -1 : 0;