iotcon_resource_h child)
{
FN_CALL;
- int i, ret;
+ OCStackResult ret;
OCResourceHandle handle_parent, handle_child;
RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(parent == child, IOTCON_ERROR_INVALID_PARAMETER);
- for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
- if (child == parent->children[i]) {
- ERR("Child resource was already bound to parent resource.");
- return IOTCON_ERROR_ALREADY;
- }
- }
-
handle_parent = IC_INT64_TO_POINTER(parent->handle);
handle_child = IC_INT64_TO_POINTER(child->handle);
- for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
- if (NULL == parent->children[i]) {
- icl_ioty_csdk_lock();
- ret = OCBindResource(handle_parent, handle_child);
- icl_ioty_csdk_unlock();
-
- if (OC_STACK_OK != ret) {
- ERR("OCBindResource() Fail(%d)", ret);
- return ic_ioty_parse_oic_error(ret);
- }
+ icl_ioty_csdk_lock();
+ ret = OCBindResource(handle_parent, handle_child);
+ icl_ioty_csdk_unlock();
- parent->children[i] = child;
- return IOTCON_ERROR_NONE;
- }
+ if (OC_STACK_OK != ret) {
+ ERR("OCBindResource() Fail(%d)", ret);
+ return ic_ioty_parse_oic_error(ret);
}
- ERR("There is no slot to bind a child resource");
- return IOTCON_ERROR_OUT_OF_MEMORY;
+ return IOTCON_ERROR_NONE;
}
int icl_ioty_resource_unbind_child_resource(iotcon_resource_h parent,
iotcon_resource_h child)
{
FN_CALL;
- int i, ret;
+ OCStackResult ret;
OCResourceHandle handle_parent, handle_child;
RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
return ic_ioty_parse_oic_error(ret);
}
- for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
- if (child == parent->children[i])
- parent->children[i] = NULL;
- }
-
return IOTCON_ERROR_NONE;
}
RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(NULL == count, IOTCON_ERROR_INVALID_PARAMETER);
- if (NULL == parent->children)
- *count = 0;
- else
- *count = g_list_length(parent->children);
+ *count = g_list_length(parent->children);
return IOTCON_ERROR_NONE;
}
API int iotcon_representation_get_nth_child(iotcon_representation_h parent,
int pos, iotcon_representation_h *child)
{
+ iotcon_representation_h repr;
+
RETV_IF(false == ic_utils_check_oic_feature_supported(), IOTCON_ERROR_NOT_SUPPORTED);
RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == parent->children, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(pos < 0, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == parent->children, IOTCON_ERROR_NO_DATA);
- *child = g_list_nth_data(parent->children, pos);
- if (NULL == *child) {
+ repr = g_list_nth_data(parent->children, pos);
+ if (NULL == repr) {
ERR("g_list_nth_data() Fail");
return IOTCON_ERROR_NO_DATA;
}
+ *child = repr;
+
return IOTCON_ERROR_NONE;
}
API int iotcon_resource_bind_child_resource(iotcon_resource_h parent,
iotcon_resource_h child)
{
- int i, ret, connectivity_type;
+ int ret;
RETV_IF(false == ic_utils_check_oic_feature_supported(), IOTCON_ERROR_NOT_SUPPORTED);
RETV_IF(false == ic_utils_check_permission(), IOTCON_ERROR_PERMISSION_DENIED);
RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(parent == child, IOTCON_ERROR_INVALID_PARAMETER);
- for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
- if (child == parent->children[i]) {
- ERR("Child resource was already bound to parent resource.");
- return IOTCON_ERROR_ALREADY;
- }
+ if (g_list_find(parent->children, child)) {
+ ERR("Child resource was already bound to parent resource.");
+ return IOTCON_ERROR_ALREADY;
}
- connectivity_type = parent->connectivity_type;
-
- switch (connectivity_type) {
- case IOTCON_CONNECTIVITY_ALL:
- ret = icl_ioty_resource_bind_child_resource(parent, child);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icl_ioty_resource_bind_child_resource() Fail(%d)", ret);
- return ret;
- }
- break;
- default:
- ERR("Invalid Connectivity Type(%d)", connectivity_type);
- return IOTCON_ERROR_INVALID_PARAMETER;
+ ret = icl_ioty_resource_bind_child_resource(parent, child);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("icl_ioty_resource_bind_child_resource() Fail(%d)", ret);
+ return ret;
}
+ parent->children = g_list_append(parent->children, child);
+
return IOTCON_ERROR_NONE;
}
API int iotcon_resource_unbind_child_resource(iotcon_resource_h parent,
iotcon_resource_h child)
{
- int ret, connectivity_type;
+ int ret;
RETV_IF(false == ic_utils_check_oic_feature_supported(), IOTCON_ERROR_NOT_SUPPORTED);
RETV_IF(false == ic_utils_check_permission(), IOTCON_ERROR_PERMISSION_DENIED);
RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
- connectivity_type = parent->connectivity_type;
-
- switch (connectivity_type) {
- case IOTCON_CONNECTIVITY_ALL:
- ret = icl_ioty_resource_unbind_child_resource(parent, child);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icl_ioty_resource_unbind_child_resource() Fail(%d)", ret);
- return ret;
- }
- break;
- default:
- ERR("Invalid Connectivity Type(%d)", connectivity_type);
+ if (NULL == g_list_find(parent->children, child)) {
+ ERR("child resource is not bound to parent resource.");
return IOTCON_ERROR_INVALID_PARAMETER;
}
+ ret = icl_ioty_resource_unbind_child_resource(parent, child);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("icl_ioty_resource_unbind_child_resource() Fail(%d)", ret);
+ return ret;
+ }
+
+ parent->children = g_list_remove(parent->children, child);
+
+ if (0 == g_list_length(parent->children)) {
+ g_list_free(parent->children);
+ parent->children = NULL;
+ }
+
return IOTCON_ERROR_NONE;
}
API int iotcon_resource_get_number_of_children(iotcon_resource_h resource,
int *number)
{
- int i;
-
RETV_IF(false == ic_utils_check_oic_feature_supported(), IOTCON_ERROR_NOT_SUPPORTED);
RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(NULL == number, IOTCON_ERROR_INVALID_PARAMETER);
- *number = 0;
- for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
- if (resource->children[i])
- *number += 1;
- }
+ *number = g_list_length(resource->children);
return IOTCON_ERROR_NONE;
}
API int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index,
iotcon_resource_h *child)
{
+ iotcon_resource_h resource;
+
RETV_IF(false == ic_utils_check_oic_feature_supported(), IOTCON_ERROR_NOT_SUPPORTED);
RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
- if ((index < 0) || (ICL_CONTAINED_RESOURCES_MAX <= index)) {
- ERR("Invalid index(%d)", index);
- return IOTCON_ERROR_INVALID_PARAMETER;
+ RETV_IF(index < 0, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == parent->children, IOTCON_ERROR_NO_DATA);
+
+ resource = g_list_nth_data(parent->children, index);
+ if (NULL == resource) {
+ ERR("g_list_nth_data() Fail");
+ return IOTCON_ERROR_NO_DATA;
}
- RETV_IF(NULL == parent->children[index], IOTCON_ERROR_NO_DATA);
- *child = parent->children[index];
+ *child = resource;
return IOTCON_ERROR_NONE;
}