2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "iotcon-types.h"
27 #include "icl-resource.h"
28 #include "icl-resource-types.h"
29 #include "icl-response.h"
31 #include "icl-value.h"
32 #include "icl-state.h"
33 #include "icl-representation.h"
35 void icl_representation_inc_ref_count(iotcon_representation_h val)
38 RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
44 static bool _icl_representation_dec_ref_count(iotcon_representation_h val)
46 RETV_IF(NULL == val, false);
47 RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
50 if (0 == val->ref_count)
57 API int iotcon_representation_create(iotcon_representation_h *ret_repr)
60 iotcon_representation_h repr;
62 RETV_IF(NULL == ret_repr, IOTCON_ERROR_INVALID_PARAMETER);
64 repr = calloc(1, sizeof(struct icl_representation_s));
66 ERR("calloc() Fail(%d)", errno);
67 return IOTCON_ERROR_OUT_OF_MEMORY;
70 repr->visibility = (ICL_VISIBILITY_REPR | ICL_VISIBILITY_PROP);
71 icl_representation_inc_ref_count(repr);
75 return IOTCON_ERROR_NONE;
79 API void iotcon_representation_destroy(iotcon_representation_h repr)
83 if (false == _icl_representation_dec_ref_count(repr))
88 /* (GDestroyNotify) : iotcon_representation_h is proper type than gpointer */
89 g_list_free_full(repr->children, (GDestroyNotify)iotcon_representation_destroy);
91 /* null COULD be allowed */
93 iotcon_resource_types_destroy(repr->res_types);
95 /* null COULD be allowed */
97 iotcon_state_destroy(repr->state);
103 API int iotcon_representation_get_uri_path(iotcon_representation_h repr, char **uri_path)
105 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
106 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
108 *uri_path = repr->uri_path;
110 return IOTCON_ERROR_NONE;
113 API int iotcon_representation_set_uri_path(iotcon_representation_h repr,
114 const char *uri_path)
116 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
117 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
119 free(repr->uri_path);
120 repr->uri_path = strdup(uri_path);
121 if (NULL == repr->uri_path) {
122 ERR("strdup() Fail");
123 return IOTCON_ERROR_OUT_OF_MEMORY;
126 return IOTCON_ERROR_NONE;
129 API int iotcon_representation_get_resource_types(iotcon_representation_h repr,
130 iotcon_resource_types_h *types)
132 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
133 RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
135 *types = repr->res_types;
137 return IOTCON_ERROR_NONE;
140 API int iotcon_representation_set_resource_types(iotcon_representation_h repr,
141 iotcon_resource_types_h types)
143 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
145 iotcon_resource_types_destroy(repr->res_types);
146 repr->res_types = NULL;
149 repr->res_types = icl_resource_types_ref(types);
151 return IOTCON_ERROR_NONE;
154 API int iotcon_representation_get_resource_interfaces(iotcon_representation_h repr,
157 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
158 RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER);
160 *ifaces = repr->interfaces;
162 return IOTCON_ERROR_NONE;
165 API int iotcon_representation_set_resource_interfaces(iotcon_representation_h repr,
168 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
170 RETV_IF(ifaces <= IOTCON_INTERFACE_NONE || IC_INTERFACE_MAX < ifaces,
171 IOTCON_ERROR_INVALID_PARAMETER);
173 repr->interfaces = ifaces;
175 return IOTCON_ERROR_NONE;
178 API int iotcon_representation_set_state(iotcon_representation_h repr,
179 iotcon_state_h state)
181 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
183 if (state == repr->state)
184 return IOTCON_ERROR_NONE;
187 iotcon_state_destroy(repr->state);
192 icl_state_inc_ref_count(state);
196 return IOTCON_ERROR_NONE;
200 API int iotcon_representation_get_state(iotcon_representation_h repr,
201 iotcon_state_h *state)
203 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
204 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
206 *state = repr->state;
208 return IOTCON_ERROR_NONE;
211 API int iotcon_representation_add_child(iotcon_representation_h parent,
212 iotcon_representation_h child)
214 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
215 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
217 icl_representation_inc_ref_count(child);
218 parent->children = g_list_append(parent->children, child);
220 return IOTCON_ERROR_NONE;
224 API int iotcon_representation_remove_child(iotcon_representation_h parent,
225 iotcon_representation_h child)
227 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
228 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
230 parent->children = g_list_remove(parent->children, child);
232 return IOTCON_ERROR_NONE;
236 API int iotcon_representation_foreach_children(iotcon_representation_h parent,
237 iotcon_children_cb cb, void *user_data)
241 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
242 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
244 list = parent->children;
247 if (IOTCON_FUNC_STOP == cb(list->data, user_data))
252 return IOTCON_ERROR_NONE;
255 API int iotcon_representation_get_children_count(iotcon_representation_h parent,
258 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
259 RETV_IF(NULL == count, IOTCON_ERROR_INVALID_PARAMETER);
260 RETV_IF(NULL == parent->children, IOTCON_ERROR_INVALID_PARAMETER);
262 *count = g_list_length(parent->children);
264 return IOTCON_ERROR_NONE;
267 API int iotcon_representation_get_nth_child(iotcon_representation_h parent, int pos,
268 iotcon_representation_h *child)
270 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
271 RETV_IF(NULL == parent->children, IOTCON_ERROR_INVALID_PARAMETER);
272 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
274 *child = g_list_nth_data(parent->children, pos);
275 if (NULL == *child) {
276 ERR("g_list_nth_data() Fail");
277 return IOTCON_ERROR_NO_DATA;
280 return IOTCON_ERROR_NONE;
283 API int iotcon_state_foreach(iotcon_state_h state, iotcon_state_cb cb, void *user_data)
288 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
289 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
291 g_hash_table_iter_init(&iter, state->hash_table);
292 while (g_hash_table_iter_next(&iter, &key, NULL)) {
293 if (IOTCON_FUNC_STOP == cb(state, key, user_data))
297 return IOTCON_ERROR_NONE;
301 API int iotcon_state_get_keys_count(iotcon_state_h state, unsigned int *count)
303 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
304 RETV_IF(NULL == count, IOTCON_ERROR_INVALID_PARAMETER);
305 RETV_IF(NULL == state->hash_table, IOTCON_ERROR_INVALID_PARAMETER);
307 *count = g_hash_table_size(state->hash_table);
309 return IOTCON_ERROR_NONE;
313 void icl_state_clone_foreach(char *key, iotcon_value_h src_val, iotcon_state_h dest_state)
317 iotcon_value_h value, copied_val;
318 iotcon_list_h child_list, copied_list;
319 iotcon_state_h child_state;
320 iotcon_state_h copied_state = NULL;
322 type = src_val->type;
324 case IOTCON_TYPE_INT:
325 case IOTCON_TYPE_BOOL:
326 case IOTCON_TYPE_DOUBLE:
327 case IOTCON_TYPE_STR:
328 case IOTCON_TYPE_NULL:
329 copied_val = icl_value_clone(src_val);
330 if (NULL == copied_val) {
331 ERR("icl_value_clone() Fail");
335 icl_state_set_value(dest_state, key, copied_val);
337 case IOTCON_TYPE_LIST:
338 ret = icl_value_get_list(src_val, &child_list);
339 if (IOTCON_ERROR_NONE != ret) {
340 ERR("icl_value_get_list() Fail(%d)", ret);
344 copied_list = icl_list_clone(child_list);
345 if (NULL == copied_list) {
346 ERR("icl_list_clone() Fail");
350 value = icl_value_create_list(copied_list);
352 ERR("icl_value_create_list() Fail");
353 iotcon_list_destroy(copied_list);
357 icl_state_set_value(dest_state, key, value);
359 case IOTCON_TYPE_STATE:
360 ret = icl_value_get_state(src_val, &child_state);
361 if (IOTCON_ERROR_NONE != ret) {
362 ERR("icl_value_get_state() Fail(%d)", ret);
366 g_hash_table_foreach(child_state->hash_table, (GHFunc)icl_state_clone_foreach,
369 value = icl_value_create_state(copied_state);
371 ERR("icl_value_create_state(%p) Fail", copied_state);
375 icl_state_set_value(dest_state, key, value);
378 ERR("Invalid type(%d)", type);
383 API int iotcon_representation_clone(const iotcon_representation_h src,
384 iotcon_representation_h *dest)
389 iotcon_resource_types_h list;
390 iotcon_representation_h cloned_repr, copied_repr;
392 RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
393 RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
395 ret = iotcon_representation_create(&cloned_repr);
396 if (IOTCON_ERROR_NONE != ret) {
397 ERR("iotcon_representation_create() Fail(%d)", ret);
402 cloned_repr->uri_path = strdup(src->uri_path);
403 if (NULL == cloned_repr->uri_path) {
404 ERR("strdup() Fail");
405 iotcon_representation_destroy(cloned_repr);
406 return IOTCON_ERROR_OUT_OF_MEMORY;
411 cloned_repr->interfaces = src->interfaces;
413 if (src->res_types) {
414 ret = iotcon_resource_types_clone(src->res_types, &list);
415 if (IOTCON_ERROR_NONE != ret) {
416 ERR("iotcon_resource_types_clone() Fail");
417 iotcon_representation_destroy(cloned_repr);
420 cloned_repr->res_types = list;
424 for (node = g_list_first(src->children); node; node = node->next) {
425 ret = iotcon_representation_clone((iotcon_representation_h)node->data,
427 if (IOTCON_ERROR_NONE != ret) {
428 ERR("iotcon_representation_clone(child) Fail(%d)", ret);
429 iotcon_representation_destroy(cloned_repr);
432 cloned_repr->children = g_list_append(cloned_repr->children, copied_repr);
437 ret = iotcon_state_clone(src->state, &cloned_repr->state);
438 if (IOTCON_ERROR_NONE != ret) {
439 ERR("iotcon_state_clone() Fail(%d)", ret);
440 iotcon_representation_destroy(cloned_repr);
447 return IOTCON_ERROR_NONE;