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);
159 *ifaces = repr->interfaces;
161 return IOTCON_ERROR_NONE;
164 API int iotcon_representation_set_resource_interfaces(iotcon_representation_h repr,
167 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
169 RETV_IF(ifaces <= IOTCON_INTERFACE_NONE || IC_INTERFACE_MAX < ifaces,
170 IOTCON_ERROR_INVALID_PARAMETER);
172 repr->interfaces = ifaces;
174 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);
182 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
185 ERR("state already set. Remove first !");
186 return IOTCON_ERROR_ALREADY;
189 icl_state_inc_ref_count(state);
192 return IOTCON_ERROR_NONE;
196 API int iotcon_representation_get_state(iotcon_representation_h repr,
197 iotcon_state_h *state)
199 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
200 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
202 *state = repr->state;
204 return IOTCON_ERROR_NONE;
208 API int iotcon_representation_del_state(iotcon_representation_h repr)
210 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
213 iotcon_state_destroy(repr->state);
215 return IOTCON_ERROR_NONE;
219 API int iotcon_representation_append_child(iotcon_representation_h parent,
220 iotcon_representation_h child)
222 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
223 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
225 icl_representation_inc_ref_count(child);
226 parent->children = g_list_append(parent->children, child);
228 return IOTCON_ERROR_NONE;
232 API int iotcon_representation_remove_child(iotcon_representation_h parent,
233 iotcon_representation_h child)
235 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
236 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
238 parent->children = g_list_remove(parent->children, child);
240 return IOTCON_ERROR_NONE;
244 API int iotcon_representation_foreach_children(iotcon_representation_h parent,
245 iotcon_children_cb cb, void *user_data)
249 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
250 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
252 list = parent->children;
255 if (IOTCON_FUNC_STOP == cb(list->data, user_data))
260 return IOTCON_ERROR_NONE;
263 API int iotcon_representation_get_children_count(iotcon_representation_h parent,
266 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
267 RETV_IF(NULL == parent->children, IOTCON_ERROR_INVALID_PARAMETER);
269 *count = g_list_length(parent->children);
271 return IOTCON_ERROR_NONE;
274 API int iotcon_representation_get_nth_child(iotcon_representation_h parent, int pos,
275 iotcon_representation_h *child)
277 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
278 RETV_IF(NULL == parent->children, IOTCON_ERROR_INVALID_PARAMETER);
279 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
281 *child = g_list_nth_data(parent->children, pos);
282 if (NULL == *child) {
283 ERR("g_list_nth_data() Fail");
284 return IOTCON_ERROR_NO_DATA;
287 return IOTCON_ERROR_NONE;
290 API int iotcon_state_foreach(iotcon_state_h state, iotcon_state_cb cb, void *user_data)
295 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
296 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
298 g_hash_table_iter_init(&iter, state->hash_table);
299 while (g_hash_table_iter_next(&iter, &key, NULL)) {
300 if (IOTCON_FUNC_STOP == cb(state, key, user_data))
304 return IOTCON_ERROR_NONE;
308 API int iotcon_state_get_keys_count(iotcon_state_h state, unsigned int *count)
310 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
311 RETV_IF(NULL == state->hash_table, IOTCON_ERROR_INVALID_PARAMETER);
313 *count = g_hash_table_size(state->hash_table);
315 return IOTCON_ERROR_NONE;
319 void icl_state_clone_foreach(char *key, iotcon_value_h src_val, iotcon_state_h dest_state)
323 iotcon_value_h value, copied_val;
324 iotcon_list_h child_list, copied_list;
325 iotcon_state_h child_state;
326 iotcon_state_h copied_state = NULL;
328 type = src_val->type;
330 case IOTCON_TYPE_INT:
331 case IOTCON_TYPE_BOOL:
332 case IOTCON_TYPE_DOUBLE:
333 case IOTCON_TYPE_STR:
334 case IOTCON_TYPE_NULL:
335 copied_val = icl_value_clone(src_val);
336 if (NULL == copied_val) {
337 ERR("icl_value_clone() Fail");
341 icl_state_set_value(dest_state, key, copied_val);
343 case IOTCON_TYPE_LIST:
344 ret = icl_value_get_list(src_val, &child_list);
345 if (IOTCON_ERROR_NONE != ret) {
346 ERR("icl_value_get_list() Fail(%d)", ret);
350 copied_list = icl_list_clone(child_list);
351 if (NULL == copied_list) {
352 ERR("icl_list_clone() Fail");
356 value = icl_value_create_list(copied_list);
358 ERR("icl_value_create_list() Fail");
359 iotcon_list_destroy(copied_list);
363 icl_state_set_value(dest_state, key, value);
365 case IOTCON_TYPE_STATE:
366 ret = icl_value_get_state(src_val, &child_state);
367 if (IOTCON_ERROR_NONE != ret) {
368 ERR("icl_value_get_state() Fail(%d)", ret);
372 g_hash_table_foreach(child_state->hash_table, (GHFunc)icl_state_clone_foreach,
375 value = icl_value_create_state(copied_state);
377 ERR("icl_value_create_state(%p) Fail", copied_state);
381 icl_state_set_value(dest_state, key, value);
384 ERR("Invalid type(%d)", type);
389 API int iotcon_representation_clone(const iotcon_representation_h src,
390 iotcon_representation_h *dest)
395 iotcon_resource_types_h list;
396 iotcon_state_h ori_state;
397 iotcon_state_h cloned_state = NULL;
398 iotcon_representation_h cloned_repr, copied_repr;
400 RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
402 ret = iotcon_representation_create(&cloned_repr);
403 if (IOTCON_ERROR_NONE != ret) {
404 ERR("iotcon_representation_create() Fail(%d)", ret);
409 cloned_repr->uri_path = strdup(src->uri_path);
410 if (NULL == cloned_repr->uri_path) {
411 ERR("strdup() Fail");
412 iotcon_representation_destroy(cloned_repr);
413 return IOTCON_ERROR_OUT_OF_MEMORY;
418 cloned_repr->interfaces = src->interfaces;
420 if (src->res_types) {
421 ret = iotcon_resource_types_clone(src->res_types, &list);
422 if (IOTCON_ERROR_NONE != ret) {
423 ERR("iotcon_resource_types_clone() Fail");
424 iotcon_representation_destroy(cloned_repr);
427 cloned_repr->res_types = list;
430 for (node = g_list_first(src->children); node; node = node->next) {
431 ret = iotcon_representation_clone((iotcon_representation_h)node->data,
433 if (IOTCON_ERROR_NONE != ret) {
434 ERR("iotcon_representation_clone(child) Fail(%d)", ret);
435 iotcon_representation_destroy(cloned_repr);
438 cloned_repr->children = g_list_append(cloned_repr->children, copied_repr);
441 ori_state = src->state;
442 if (ori_state->hash_table) {
443 ret = iotcon_state_create(&cloned_state);
444 if (IOTCON_ERROR_NONE != ret) {
445 ERR("iotcon_state_create() Fail");
446 iotcon_representation_destroy(cloned_repr);
449 g_hash_table_foreach(ori_state->hash_table, (GHFunc)icl_state_clone_foreach,
451 ret = iotcon_representation_set_state(cloned_repr, cloned_state);
452 if (IOTCON_ERROR_NONE != ret) {
453 ERR("iotcon_representation_set_state() Fail");
454 iotcon_state_destroy(cloned_state);
455 iotcon_representation_destroy(cloned_repr);
458 iotcon_state_destroy(cloned_state);
463 return IOTCON_ERROR_NONE;