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)
48 RETV_IF(NULL == val, -1);
49 RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
52 if (0 == val->ref_count)
61 API int iotcon_representation_create(iotcon_representation_h *ret_repr)
64 iotcon_representation_h repr;
66 RETV_IF(NULL == ret_repr, IOTCON_ERROR_INVALID_PARAMETER);
68 repr = calloc(1, sizeof(struct icl_representation_s));
70 ERR("calloc() Fail(%d)", errno);
71 return IOTCON_ERROR_OUT_OF_MEMORY;
74 repr->visibility = (ICL_VISIBILITY_REPR | ICL_VISIBILITY_PROP);
75 icl_representation_inc_ref_count(repr);
79 return IOTCON_ERROR_NONE;
83 API void iotcon_representation_destroy(iotcon_representation_h repr)
87 if (false == _icl_representation_dec_ref_count(repr))
92 /* (GDestroyNotify) : iotcon_representation_h is proper type than gpointer */
93 g_list_free_full(repr->children, (GDestroyNotify)iotcon_representation_destroy);
95 /* null COULD be allowed */
97 iotcon_resource_types_destroy(repr->res_types);
99 /* null COULD be allowed */
101 iotcon_state_destroy(repr->state);
107 API int iotcon_representation_get_uri_path(iotcon_representation_h repr, char **uri_path)
109 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
110 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
112 *uri_path = repr->uri_path;
114 return IOTCON_ERROR_NONE;
117 API int iotcon_representation_set_uri_path(iotcon_representation_h repr,
118 const char *uri_path)
120 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
122 free(repr->uri_path);
123 repr->uri_path = NULL;
125 if (NULL == uri_path)
126 return IOTCON_ERROR_INVALID_PARAMETER;
128 repr->uri_path = strdup(uri_path);
129 if (NULL == repr->uri_path) {
130 ERR("strdup() Fail");
131 return IOTCON_ERROR_OUT_OF_MEMORY;
134 return IOTCON_ERROR_NONE;
137 API int iotcon_representation_get_resource_types(iotcon_representation_h repr,
138 iotcon_resource_types_h *types)
140 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
141 RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
143 *types = repr->res_types;
145 return IOTCON_ERROR_NONE;
148 API int iotcon_representation_set_resource_types(iotcon_representation_h repr,
149 iotcon_resource_types_h types)
151 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
153 iotcon_resource_types_destroy(repr->res_types);
154 repr->res_types = NULL;
157 repr->res_types = icl_resource_types_ref(types);
159 return IOTCON_ERROR_NONE;
162 API int iotcon_representation_get_resource_interfaces(iotcon_representation_h repr,
165 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
167 *ifaces = repr->interfaces;
169 return IOTCON_ERROR_NONE;
172 API int iotcon_representation_set_resource_interfaces(iotcon_representation_h repr,
175 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
177 RETV_IF(ifaces <= IOTCON_INTERFACE_NONE || IC_INTERFACE_MAX < ifaces,
178 IOTCON_ERROR_INVALID_PARAMETER);
180 repr->interfaces = ifaces;
182 return IOTCON_ERROR_NONE;
186 API int iotcon_representation_set_state(iotcon_representation_h repr,
187 iotcon_state_h state)
189 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
190 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
193 ERR("state already set. Remove first !");
194 return IOTCON_ERROR_ALREADY;
197 icl_state_inc_ref_count(state);
200 return IOTCON_ERROR_NONE;
204 API int iotcon_representation_get_state(iotcon_representation_h repr,
205 iotcon_state_h *state)
207 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
208 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
210 *state = repr->state;
212 return IOTCON_ERROR_NONE;
216 API int iotcon_representation_del_state(iotcon_representation_h repr)
218 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
221 iotcon_state_destroy(repr->state);
223 return IOTCON_ERROR_NONE;
227 API int iotcon_representation_append_child(iotcon_representation_h parent,
228 iotcon_representation_h child)
230 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
231 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
233 icl_representation_inc_ref_count(child);
234 parent->children = g_list_append(parent->children, child);
236 return IOTCON_ERROR_NONE;
240 API int iotcon_representation_remove_child(iotcon_representation_h parent,
241 iotcon_representation_h child)
243 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
244 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
246 parent->children = g_list_remove(parent->children, child);
248 return IOTCON_ERROR_NONE;
252 API int iotcon_representation_foreach_children(iotcon_representation_h parent,
253 iotcon_children_cb cb, void *user_data)
257 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
258 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
260 list = parent->children;
263 if (IOTCON_FUNC_STOP == cb(list->data, user_data))
268 return IOTCON_ERROR_NONE;
271 API int iotcon_representation_get_children_count(iotcon_representation_h parent,
274 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
275 RETV_IF(NULL == parent->children, IOTCON_ERROR_INVALID_PARAMETER);
277 *count = g_list_length(parent->children);
279 return IOTCON_ERROR_NONE;
282 API int iotcon_representation_get_nth_child(iotcon_representation_h parent, int pos,
283 iotcon_representation_h *child)
285 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
286 RETV_IF(NULL == parent->children, IOTCON_ERROR_INVALID_PARAMETER);
287 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
289 *child = g_list_nth_data(parent->children, pos);
290 if (NULL == *child) {
291 ERR("g_list_nth_data() Fail");
292 return IOTCON_ERROR_NO_DATA;
295 return IOTCON_ERROR_NONE;
298 API int iotcon_state_foreach(iotcon_state_h state, iotcon_state_cb cb, void *user_data)
303 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
304 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
306 g_hash_table_iter_init(&iter, state->hash_table);
307 while (g_hash_table_iter_next(&iter, &key, NULL)) {
308 if (IOTCON_FUNC_STOP == cb(state, key, user_data))
312 return IOTCON_ERROR_NONE;
316 API int iotcon_state_get_keys_count(iotcon_state_h state, unsigned int *count)
318 RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
319 RETV_IF(NULL == state->hash_table, IOTCON_ERROR_INVALID_PARAMETER);
321 *count = g_hash_table_size(state->hash_table);
323 return IOTCON_ERROR_NONE;
327 void icl_state_clone_foreach(char *key, iotcon_value_h src_val, iotcon_state_h dest_state)
331 iotcon_value_h value, copied_val;
332 iotcon_list_h child_list, copied_list;
333 iotcon_state_h child_state;
334 iotcon_state_h copied_state = NULL;
336 type = src_val->type;
338 case IOTCON_TYPE_INT:
339 case IOTCON_TYPE_BOOL:
340 case IOTCON_TYPE_DOUBLE:
341 case IOTCON_TYPE_STR:
342 case IOTCON_TYPE_NULL:
343 copied_val = icl_value_clone(src_val);
344 if (NULL == copied_val) {
345 ERR("icl_value_clone() Fail");
349 icl_state_set_value(dest_state, key, copied_val);
351 case IOTCON_TYPE_LIST:
352 ret = icl_value_get_list(src_val, &child_list);
353 if (IOTCON_ERROR_NONE != ret) {
354 ERR("icl_value_get_list() Fail(%d)", ret);
358 copied_list = icl_list_clone(child_list);
359 if (NULL == copied_list) {
360 ERR("icl_list_clone() Fail");
364 value = icl_value_create_list(copied_list);
366 ERR("icl_value_create_list() Fail");
367 iotcon_list_destroy(copied_list);
371 icl_state_set_value(dest_state, key, value);
373 case IOTCON_TYPE_STATE:
374 ret = icl_value_get_state(src_val, &child_state);
375 if (IOTCON_ERROR_NONE != ret) {
376 ERR("icl_value_get_state() Fail(%d)", ret);
380 g_hash_table_foreach(child_state->hash_table, (GHFunc)icl_state_clone_foreach,
383 value = icl_value_create_state(copied_state);
385 ERR("icl_value_create_state(%p) Fail", copied_state);
389 icl_state_set_value(dest_state, key, value);
392 ERR("Invalid type(%d)", type);
397 API int iotcon_representation_clone(const iotcon_representation_h src,
398 iotcon_representation_h *dest)
403 iotcon_resource_types_h list;
404 iotcon_state_h ori_state;
405 iotcon_state_h cloned_state = NULL;
406 iotcon_representation_h cloned_repr, copied_repr;
408 RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
410 ret = iotcon_representation_create(&cloned_repr);
411 if (IOTCON_ERROR_NONE != ret) {
412 ERR("iotcon_representation_create() Fail(%d)", ret);
417 cloned_repr->uri_path = strdup(src->uri_path);
418 if (NULL == cloned_repr->uri_path) {
419 ERR("strdup() Fail");
420 iotcon_representation_destroy(cloned_repr);
421 return IOTCON_ERROR_OUT_OF_MEMORY;
426 cloned_repr->interfaces = src->interfaces;
428 if (src->res_types) {
429 ret = iotcon_resource_types_clone(src->res_types, &list);
430 if (IOTCON_ERROR_NONE != ret) {
431 ERR("iotcon_resource_types_clone() Fail");
432 iotcon_representation_destroy(cloned_repr);
435 cloned_repr->res_types = list;
438 for (node = g_list_first(src->children); node; node = node->next) {
439 ret = iotcon_representation_clone((iotcon_representation_h)node->data,
441 if (IOTCON_ERROR_NONE != ret) {
442 ERR("iotcon_representation_clone(child) Fail(%d)", ret);
443 iotcon_representation_destroy(cloned_repr);
446 cloned_repr->children = g_list_append(cloned_repr->children, copied_repr);
449 ori_state = src->state;
450 if (ori_state->hash_table) {
451 ret = iotcon_state_create(&cloned_state);
452 if (IOTCON_ERROR_NONE != ret) {
453 ERR("iotcon_state_create() Fail");
454 iotcon_representation_destroy(cloned_repr);
457 g_hash_table_foreach(ori_state->hash_table, (GHFunc)icl_state_clone_foreach,
459 ret = iotcon_representation_set_state(cloned_repr, cloned_state);
460 if (IOTCON_ERROR_NONE != ret) {
461 ERR("iotcon_representation_set_state() Fail");
462 iotcon_state_destroy(cloned_state);
463 iotcon_representation_destroy(cloned_repr);
466 iotcon_state_destroy(cloned_state);
471 return IOTCON_ERROR_NONE;