1 /* Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
19 #include "iotcon-types.h"
23 #include "icl-value.h"
24 #include "icl-representation.h"
25 #include "icl-state.h"
27 void icl_state_inc_ref_count(iotcon_state_h val)
30 RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
36 static bool _icl_state_dec_ref_count(iotcon_state_h val)
38 RETV_IF(NULL == val, false);
39 RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
42 if (0 == val->ref_count)
49 API int iotcon_state_create(iotcon_state_h *ret_state)
54 RETV_IF(NULL == ret_state, IOTCON_ERROR_INVALID_PARAMETER);
56 state = calloc(1, sizeof(struct icl_state_s));
58 ERR("calloc() Fail(%d)", errno);
59 return IOTCON_ERROR_OUT_OF_MEMORY;
62 state->hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, free,
64 icl_state_inc_ref_count(state);
68 return IOTCON_ERROR_NONE;
72 API void iotcon_state_destroy(iotcon_state_h state)
74 RET_IF(NULL == state);
76 if (false == _icl_state_dec_ref_count(state))
79 g_hash_table_destroy(state->hash_table);
83 API int iotcon_state_clone(iotcon_state_h state, iotcon_state_h *state_clone)
86 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
87 RETV_IF(NULL == state_clone, IOTCON_ERROR_INVALID_PARAMETER);
89 ret = icl_state_clone(state, state_clone);
90 if (IOTCON_ERROR_NONE != ret) {
91 ERR("icl_state_clone() Fail(%d)", ret);
95 return IOTCON_ERROR_NONE;
99 int icl_state_del_value(iotcon_state_h state, const char *key)
101 gboolean ret = FALSE;
102 iotcon_value_h value = NULL;
104 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
105 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
107 value = g_hash_table_lookup(state->hash_table, key);
109 ERR("g_hash_table_lookup(%s) Fail", key);
110 return IOTCON_ERROR_NO_DATA;
113 ret = g_hash_table_remove(state->hash_table, key);
115 ERR("g_hash_table_remove(%s) Fail", key);
116 return IOTCON_ERROR_NO_DATA;
119 return IOTCON_ERROR_NONE;
122 API int iotcon_state_get_int(iotcon_state_h state, const char *key, int *val)
124 iotcon_value_h value;
126 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
127 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
128 RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
130 value = g_hash_table_lookup(state->hash_table, key);
132 ERR("g_hash_table_lookup() Fail");
133 return IOTCON_ERROR_NO_DATA;
136 icl_basic_s *real = (icl_basic_s*)value;
137 if (IOTCON_TYPE_INT != real->type) {
138 ERR("Invalid Type(%d)", real->type);
139 return IOTCON_ERROR_INVALID_TYPE;
144 return IOTCON_ERROR_NONE;
147 API int iotcon_state_set_int(iotcon_state_h state, const char *key, int val)
149 iotcon_value_h value;
151 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
152 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
154 value = icl_value_create_int(val);
156 ERR("icl_value_create_int(%d) Fail", val);
157 return IOTCON_ERROR_OUT_OF_MEMORY;
160 g_hash_table_replace(state->hash_table, ic_utils_strdup(key), value);
162 return IOTCON_ERROR_NONE;
165 API int iotcon_state_unset(iotcon_state_h state, const char *key)
169 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
170 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
172 ret = icl_state_del_value(state, key);
173 if (IOTCON_ERROR_NONE != ret)
174 ERR("icl_state_del_value() Fail(%d)", ret);
179 API int iotcon_state_get_bool(iotcon_state_h state, const char *key, bool *val)
181 icl_basic_s *real = NULL;
182 iotcon_value_h value = NULL;
184 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
185 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
186 RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
188 value = g_hash_table_lookup(state->hash_table, key);
190 ERR("g_hash_table_lookup() Fail");
191 return IOTCON_ERROR_NO_DATA;
194 real = (icl_basic_s*)value;
195 if (IOTCON_TYPE_BOOL != real->type) {
196 ERR("Invalid Type(%d)", real->type);
197 return IOTCON_ERROR_INVALID_TYPE;
202 return IOTCON_ERROR_NONE;
205 API int iotcon_state_set_bool(iotcon_state_h state, const char *key, bool val)
207 iotcon_value_h value = NULL;
209 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
210 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
212 value = icl_value_create_bool(val);
214 ERR("icl_value_create_bool(%d) Fail", val);
215 return IOTCON_ERROR_OUT_OF_MEMORY;
218 g_hash_table_replace(state->hash_table, ic_utils_strdup(key), value);
220 return IOTCON_ERROR_NONE;
223 API int iotcon_state_get_double(iotcon_state_h state, const char *key, double *val)
225 icl_basic_s *real = NULL;
226 iotcon_value_h value = NULL;
228 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
229 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
230 RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
232 value = g_hash_table_lookup(state->hash_table, key);
234 ERR("g_hash_table_lookup() Fail");
235 return IOTCON_ERROR_NO_DATA;
238 real = (icl_basic_s*)value;
239 if (IOTCON_TYPE_DOUBLE != real->type) {
240 ERR("Invalid Type(%d)", real->type);
241 return IOTCON_ERROR_INVALID_TYPE;
246 return IOTCON_ERROR_NONE;
249 API int iotcon_state_set_double(iotcon_state_h state, const char *key, double val)
251 iotcon_value_h value = NULL;
253 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
254 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
256 value = icl_value_create_double(val);
258 ERR("icl_value_create_double(%f) Fail", val);
259 return IOTCON_ERROR_OUT_OF_MEMORY;
262 g_hash_table_replace(state->hash_table, ic_utils_strdup(key), value);
264 return IOTCON_ERROR_NONE;
267 API int iotcon_state_get_str(iotcon_state_h state, const char *key, char **val)
269 icl_basic_s *real = NULL;
270 iotcon_value_h value = NULL;
272 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
273 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
274 RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
276 value = g_hash_table_lookup(state->hash_table, key);
278 ERR("g_hash_table_lookup() Fail");
279 return IOTCON_ERROR_NO_DATA;
282 real = (icl_basic_s*)value;
283 if (IOTCON_TYPE_STR != real->type) {
284 ERR("Invalid Type(%d)", real->type);
285 return IOTCON_ERROR_INVALID_TYPE;
290 return IOTCON_ERROR_NONE;
293 API int iotcon_state_set_str(iotcon_state_h state, const char *key, char *val)
295 iotcon_value_h value = NULL;
297 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
298 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
299 RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
301 value = icl_value_create_str(val);
303 ERR("icl_value_create_str(%s) Fail", val);
304 return IOTCON_ERROR_OUT_OF_MEMORY;
307 g_hash_table_replace(state->hash_table, ic_utils_strdup(key), value);
309 return IOTCON_ERROR_NONE;
312 API int iotcon_state_is_null(iotcon_state_h state, const char *key, bool *is_null)
314 icl_basic_s *real = NULL;
315 iotcon_value_h value = NULL;
317 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
318 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
320 value = (iotcon_value_h) g_hash_table_lookup(state->hash_table, key);
322 ERR("g_hash_table_lookup() Fail");
323 return IOTCON_ERROR_NO_DATA;
326 real = (icl_basic_s*)value;
327 *is_null = (IOTCON_TYPE_NULL == real->type) ? true : false;
329 return IOTCON_ERROR_NONE;
332 API int iotcon_state_set_null(iotcon_state_h state, const char *key)
334 iotcon_value_h value = NULL;
336 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
337 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
339 value = icl_value_create_null();
341 ERR("icl_value_create_null() Fail");
342 return IOTCON_ERROR_OUT_OF_MEMORY;
345 g_hash_table_replace(state->hash_table, ic_utils_strdup(key), value);
347 return IOTCON_ERROR_NONE;
350 API int iotcon_state_get_list(iotcon_state_h state, const char *key, iotcon_list_h *list)
352 iotcon_value_h value = NULL;
353 icl_val_list_s *real = NULL;
355 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
356 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
357 RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
359 value = g_hash_table_lookup(state->hash_table, key);
361 ERR("g_hash_table_lookup() Fail");
362 return IOTCON_ERROR_NO_DATA;
365 real = (icl_val_list_s*)value;
366 if (IOTCON_TYPE_LIST != real->type) {
367 ERR("Invalid Type(%d)", real->type);
368 return IOTCON_ERROR_INVALID_TYPE;
373 return IOTCON_ERROR_NONE;
376 API int iotcon_state_set_list(iotcon_state_h state, const char *key, iotcon_list_h list)
378 iotcon_value_h value = NULL;
380 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
381 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
382 RETV_IF(NULL == list, IOTCON_ERROR_INVALID_PARAMETER);
384 value = icl_value_create_list(list);
386 ERR("icl_value_create_list() Fail");
387 return IOTCON_ERROR_OUT_OF_MEMORY;
389 icl_list_inc_ref_count(list);
391 g_hash_table_replace(state->hash_table, ic_utils_strdup(key), value);
393 return IOTCON_ERROR_NONE;
396 API int iotcon_state_get_state(iotcon_state_h src, const char *key, iotcon_state_h *dest)
398 icl_val_state_s *real = NULL;
399 iotcon_value_h value = NULL;
401 RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
402 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
403 RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
405 value = g_hash_table_lookup(src->hash_table, key);
407 ERR("g_hash_table_lookup() Fail");
408 return IOTCON_ERROR_NO_DATA;
411 real = (icl_val_state_s*)value;
412 if (IOTCON_TYPE_STATE != real->type) {
413 ERR("Invalid Type(%d)", real->type);
414 return IOTCON_ERROR_INVALID_TYPE;
419 return IOTCON_ERROR_NONE;
422 API int iotcon_state_set_state(iotcon_state_h state, const char *key, iotcon_state_h val)
424 iotcon_value_h value = NULL;
426 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
427 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
428 RETV_IF(NULL == val, IOTCON_ERROR_INVALID_PARAMETER);
430 value = icl_value_create_state(val);
432 ERR("icl_value_create_state(%p) Fail", val);
433 return IOTCON_ERROR_OUT_OF_MEMORY;
435 icl_state_inc_ref_count(val);
437 g_hash_table_replace(state->hash_table, ic_utils_strdup(key), value);
439 return IOTCON_ERROR_NONE;
442 API int iotcon_state_get_type(iotcon_state_h state, const char *key,
445 iotcon_value_h value = NULL;
447 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
448 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
449 RETV_IF(NULL == type, IOTCON_ERROR_INVALID_PARAMETER);
451 value = g_hash_table_lookup(state->hash_table, key);
453 ERR("g_hash_table_lookup() Fail");
454 return IOTCON_ERROR_NO_DATA;
458 return IOTCON_ERROR_NONE;
461 int icl_state_set_value(iotcon_state_h state, const char *key, iotcon_value_h value)
463 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
464 RETV_IF(NULL == key, IOTCON_ERROR_INVALID_PARAMETER);
465 RETV_IF(NULL == value, IOTCON_ERROR_INVALID_PARAMETER);
467 g_hash_table_replace(state->hash_table, ic_utils_strdup(key), value);
469 return IOTCON_ERROR_NONE;
473 int icl_state_clone(iotcon_state_h src, iotcon_state_h *dest)
477 iotcon_state_h cloned_state = NULL;
479 RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
480 RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
482 if (src->hash_table) {
483 ret = iotcon_state_create(&cloned_state);
484 if (IOTCON_ERROR_NONE != ret) {
485 ERR("iotcon_state_create() Fail(%d)", ret);
489 g_hash_table_foreach(src->hash_table, (GHFunc)icl_state_clone_foreach,
493 *dest = cloned_state;
495 return IOTCON_ERROR_NONE;