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.
21 #include "iotcon-types.h"
24 #include "icl-resource-interfaces.h"
26 iotcon_resource_interfaces_h icl_resource_interfaces_ref(
27 iotcon_resource_interfaces_h ifaces)
29 RETV_IF(NULL == ifaces, NULL);
30 RETV_IF(ifaces->ref_count <= 0, NULL);
38 API int iotcon_resource_interfaces_create(iotcon_resource_interfaces_h *ret_ifaces)
40 iotcon_resource_interfaces_h ifaces;
42 RETV_IF(false == ic_utils_check_oic_feature_supported(), IOTCON_ERROR_NOT_SUPPORTED);
43 RETV_IF(NULL == ret_ifaces, IOTCON_ERROR_INVALID_PARAMETER);
45 ifaces = calloc(1, sizeof(struct icl_resource_ifaces));
47 ERR("calloc() Fail(%d)", errno);
48 return IOTCON_ERROR_OUT_OF_MEMORY;
51 ifaces->ref_count = 1;
55 return IOTCON_ERROR_NONE;
59 API void iotcon_resource_interfaces_destroy(iotcon_resource_interfaces_h ifaces)
61 RET_IF(NULL == ifaces);
65 if (0 == ifaces->ref_count) {
66 g_list_free_full(ifaces->iface_list, free);
72 static int _icl_resource_interfaces_strcmp(const void *a, const void *b)
78 static bool _icl_resource_interfaces_duplicate_check(iotcon_resource_interfaces_h ifaces,
83 RETV_IF(NULL == ifaces, false);
84 RETV_IF(NULL == iface, false);
86 node = g_list_find_custom(ifaces->iface_list, iface, _icl_resource_interfaces_strcmp);
94 /* Duplicate strings are not allowed. */
95 API int iotcon_resource_interfaces_add(iotcon_resource_interfaces_h ifaces,
100 RETV_IF(false == ic_utils_check_oic_feature_supported(), IOTCON_ERROR_NOT_SUPPORTED);
101 RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER);
102 RETV_IF(NULL == iface, IOTCON_ERROR_INVALID_PARAMETER);
103 RETVM_IF(1 < ifaces->ref_count, IOTCON_ERROR_INVALID_PARAMETER,
104 "Don't modify it. It is already set.");
106 if (true == _icl_resource_interfaces_duplicate_check(ifaces, iface)) {
107 ERR("%s is already contained.", iface);
108 return IOTCON_ERROR_INVALID_PARAMETER;
111 resource_iface = strdup(iface);
112 if (NULL == resource_iface) {
113 ERR("strdup() Fail");
114 return IOTCON_ERROR_INVALID_PARAMETER;
117 ifaces->iface_list = g_list_append(ifaces->iface_list, resource_iface);
119 return IOTCON_ERROR_NONE;
123 API int iotcon_resource_interfaces_remove(iotcon_resource_interfaces_h ifaces,
129 RETV_IF(false == ic_utils_check_oic_feature_supported(), IOTCON_ERROR_NOT_SUPPORTED);
130 RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER);
131 RETV_IF(NULL == iface, IOTCON_ERROR_INVALID_PARAMETER);
132 RETVM_IF(1 < ifaces->ref_count, IOTCON_ERROR_INVALID_PARAMETER,
133 "Don't modify it. It is already set.");
135 node = g_list_find_custom(ifaces->iface_list, iface, _icl_resource_interfaces_strcmp);
137 ERR("g_list_find_custom() Fail");
138 return IOTCON_ERROR_NO_DATA;
141 node_data = node->data;
142 ifaces->iface_list = g_list_delete_link(ifaces->iface_list, node);
145 return IOTCON_ERROR_NONE;
149 API int iotcon_resource_interfaces_foreach(iotcon_resource_interfaces_h ifaces,
150 iotcon_resource_interfaces_foreach_cb cb, void *user_data)
154 RETV_IF(false == ic_utils_check_oic_feature_supported(), IOTCON_ERROR_NOT_SUPPORTED);
155 RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER);
156 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
158 for (node = ifaces->iface_list; node; node = node->next) {
159 if (IOTCON_FUNC_STOP == cb((const char*)node->data, user_data))
163 return IOTCON_ERROR_NONE;
167 API int iotcon_resource_interfaces_clone(iotcon_resource_interfaces_h src,
168 iotcon_resource_interfaces_h *dest)
171 char *resource_iface;
172 iotcon_resource_interfaces_h resource_ifaces;
174 RETV_IF(false == ic_utils_check_oic_feature_supported(), IOTCON_ERROR_NOT_SUPPORTED);
175 RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
176 RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
178 resource_ifaces = calloc(1, sizeof(struct icl_resource_ifaces));
179 if (NULL == resource_ifaces) {
180 ERR("calloc() Fail(%d)", errno);
181 return IOTCON_ERROR_OUT_OF_MEMORY;
184 for (node = src->iface_list; node; node = node->next) {
185 resource_iface = ic_utils_strdup(node->data);
186 if (NULL == resource_iface) {
187 iotcon_resource_interfaces_destroy(resource_ifaces);
188 ERR("ic_utils_strdup() Fail");
189 return IOTCON_ERROR_OUT_OF_MEMORY;
191 resource_ifaces->iface_list = g_list_append(resource_ifaces->iface_list,
195 resource_ifaces->ref_count = 1;
197 *dest = resource_ifaces;
199 return IOTCON_ERROR_NONE;