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.
16 #ifndef __IOT_CONNECTIVITY_MANAGER_STRUCT_REPRESENTATION_H__
17 #define __IOT_CONNECTIVITY_MANAGER_STRUCT_REPRESENTATION_H__
19 #include <iotcon-types.h>
22 * @file iotcon-representation.h
26 * @ingroup CAPI_IOT_CONNECTIVITY_COMMON_MODULE
27 * @defgroup CAPI_IOT_CONNECTIVITY_COMMON_REPRESENTATION_MODULE Representation
29 * @brief Iotcon Representation provides API to manage representation
31 * @section CAPI_IOT_CONNECTIVITY_COMMON_REPRESENTATION_MODULE_HEADER Required Header
32 * \#include <iotcon.h>
34 * @section CAPI_IOT_CONNECTIVITY_COMMON_REPRESENTATION_MODULE_OVERVIEW Overview
35 * The Iotcon Representation API provides data type of resp_repr handling.\n
36 * A resp_repr is a payload of a request or a response.\n
37 * It has uri_path, interface, list of resource types and its attributes.\n
38 * Attributes have capabilties to store and retrieve integer, boolean, double, string, list, null,
40 * A list is a container that includes number of datas of same type.\n
41 * It has capabilties to store and retrieve integer, boolean, double, string, list, null, resp_repr.
49 iotcon_representation_h repr;
50 iotcon_resource_types_h types;
51 iotcon_list_h bright_step_list;
53 ret = iotcon_representation_create(&resp_repr);
54 if (IOTCON_ERROR_NONE != ret) {
58 ret = iotcon_representation_set_uri_path(resp_repr, "/a/light");
59 if (IOTCON_ERROR_NONE != ret) {
60 iotcon_representation_destroy(resp_repr);
64 ret = iotcon_resource_types_create(&types);
65 if (IOTCON_ERROR_NONE != ret) {
66 iotcon_representation_destroy(resp_repr);
70 ret = iotcon_resource_types_add(types, "core.light");
71 if (IOTCON_ERROR_NONE != ret) {
72 iotcon_resource_types_destroy(types);
73 iotcon_representation_destroy(resp_repr);
77 ret = iotcon_representation_set_resource_types(resp_repr, types);
78 if (IOTCON_ERROR_NONE != ret) {
79 iotcon_resource_types_destroy(types);
80 iotcon_representation_destroy(resp_repr);
84 ret = iotcon_representation_set_resource_interfaces(resp_repr, IOTCON_INTERFACE_LINK);
85 if (IOTCON_ERROR_NONE != ret) {
86 iotcon_resource_types_destroy(types);
87 iotcon_representation_destroy(resp_repr);
91 ret = iotcon_state_add_str(resp_repr, "type", "lamp");
92 if (IOTCON_ERROR_NONE != ret) {
93 iotcon_resource_types_destroy(types);
94 iotcon_representation_destroy(resp_repr);
98 ret = iotcon_state_add_str(resp_repr, "where", "desk");
99 if (IOTCON_ERROR_NONE != ret) {
100 iotcon_resource_types_destroy(types);
101 iotcon_representation_destroy(resp_repr);
105 ret = iotcon_state_add_double(resp_repr, "default_bright", 200.0);
106 if (IOTCON_ERROR_NONE != ret) {
107 iotcon_resource_types_destroy(types);
108 iotcon_representation_destroy(resp_repr);
112 ret = iotcon_state_add_str(resp_repr, "unit", "lux");
113 if (IOTCON_ERROR_NONE != ret) {
114 iotcon_resource_types_destroy(types);
115 iotcon_representation_destroy(resp_repr);
119 ret = iotcon_state_add_bool(resp_repr, "bright_step", true);
120 if (IOTCON_ERROR_NONE != ret) {
121 iotcon_resource_types_destroy(types);
122 iotcon_representation_destroy(resp_repr);
126 ret = iotcon_list_create(IOTCON_TYPE_DOUBLE, &bright_step_list);
127 if (IOTCON_ERROR_NONE != ret) {
128 iotcon_resource_types_destroy(types);
129 iotcon_representation_destroy(resp_repr);
133 ret = iotcon_list_add_double(bright_step_list, 100.0, -1);
134 if (IOTCON_ERROR_NONE != ret) {
135 iotcon_list_destroy(bright_step_list);
136 iotcon_resource_types_destroy(types);
137 iotcon_representation_destroy(resp_repr);
141 ret = iotcon_list_add_double(bright_step_list, 200.0, -1);
142 if (IOTCON_ERROR_NONE != ret) {
143 iotcon_list_destroy(bright_step_list);
144 iotcon_resource_types_destroy(types);
145 iotcon_representation_destroy(resp_repr);
149 ret = iotcon_list_add_double(bright_step_list, 300.0, -1);
150 if (IOTCON_ERROR_NONE != ret) {
151 iotcon_list_destroy(bright_step_list);
152 iotcon_resource_types_destroy(types);
153 iotcon_representation_destroy(resp_repr);
157 ret = iotcon_list_add_double(bright_step_list, 400.0, -1);
158 if (IOTCON_ERROR_NONE != ret) {
159 iotcon_list_destroy(bright_step_list);
160 iotcon_resource_types_destroy(types);
161 iotcon_representation_destroy(resp_repr);
165 ret = iotcon_list_add_double(bright_step_list, 500.0, -1);
166 if (IOTCON_ERROR_NONE != ret) {
167 iotcon_list_destroy(bright_step_list);
168 iotcon_resource_types_destroy(types);
169 iotcon_representation_destroy(resp_repr);
173 ret = iotcon_state_add_list(resp_repr, "bright_step_list", bright_step_list);
174 if (IOTCON_ERROR_NONE != ret) {
175 iotcon_list_destroy(bright_step_list);
176 iotcon_resource_types_destroy(types);
177 iotcon_representation_destroy(resp_repr);
181 iotcon_list_destroy(bright_step_list);
182 iotcon_resource_types_destroy(types);
183 iotcon_representation_destroy(resp_repr);
190 * @brief Creates a new representation handle.
194 * @remarks You must destroy @a repr by calling iotcon_representation_destroy()
195 * if @a repr is no longer needed.
197 * @param[out] repr A newly allocated representation handle
199 * @return 0 on success, otherwise a negative error value.
200 * @retval #IOTCON_ERROR_NONE Successful
201 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
202 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
204 * @see iotcon_representation_destroy()
206 int iotcon_representation_create(iotcon_representation_h *repr);
209 * @brief Destroys a representation.
210 * @details Releases a @a representation and its internal data.
214 * @param[in] repr The representation handle to free
218 * @see iotcon_representation_create()
220 void iotcon_representation_destroy(iotcon_representation_h repr);
223 * @brief Clones from the source representation.
224 * @details Makes a deep copy of a source representation.
228 * @remarks You must destroy @a dest by calling iotcon_representation_destroy()
229 * if @a dest is no longer needed.
231 * @param[in] src Source of representation to be copied
232 * @param[out] dest Clone of a source representation
234 * @return Clone of a source representation, otherwise NULL on failure
235 * @retval iotcon_representation_h Success
236 * @retval NULL Failure
238 int iotcon_representation_clone(const iotcon_representation_h src,
239 iotcon_representation_h *dest);
242 * @brief Appends resource type name.
245 * @remarks Stored string is replaced with @a uri_path. If @a uri_path is NULL, stored string
248 * @param[in] repr The handle to the Representation
249 * @param[in] uri_path The URI of resource
251 * @return 0 on success, otherwise a negative error value.
252 * @retval #IOTCON_ERROR_NONE Successful
253 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
254 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
256 int iotcon_representation_set_uri_path(iotcon_representation_h repr,
257 const char *uri_path);
260 * @brief Gets an URI path from the representation.
264 * @remarks @a uri_path must not be released using free().
266 * @param[in] repr The representation handle
267 * @param[out] uri_path The URI path to get
269 * @return 0 on success, otherwise a negative error value.
270 * @retval #IOTCON_ERROR_NONE Successful
271 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
273 int iotcon_representation_get_uri_path(iotcon_representation_h repr, char **uri_path);
276 * @brief Sets resource type list to the Representation.
279 * @remarks Stored list is replaced with @a types. If @a types is NULL, stored list is set
281 * @param[in] repr The handle to the Representation
282 * @param[in] types The resource type list
284 * @return 0 on success, otherwise a negative error value.
285 * @retval #IOTCON_ERROR_NONE Successful
286 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
288 int iotcon_representation_set_resource_types(iotcon_representation_h repr,
289 iotcon_resource_types_h types);
292 * @brief Gets list of resource type from the representation.
296 * @remarks @a types must not be released using iotcon_resource_types_destroy().
298 * @param[in] repr The representation handle
299 * @param[out] types The list of resource types to get
301 * @return 0 on success, otherwise a negative error value.
302 * @retval #IOTCON_ERROR_NONE Successful
303 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
305 int iotcon_representation_get_resource_types(iotcon_representation_h repr,
306 iotcon_resource_types_h *types);
309 * @brief Sets interfaces to the representation.
310 * @details If you set new interfaces, current interfaces value will be replaced with @a ifaces.\n
311 * @a ifaces can be consist of multiple interface like
312 * IOTCON_INTERFACE_LINK | IOTCON_INTERFACE_BATCH.
316 * @param[in] repr The representation handle
317 * @param[in] ifaces The interfaces to set
319 * @return 0 on success, otherwise a negative error value.
320 * @retval #IOTCON_ERROR_NONE Successful
321 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
323 int iotcon_representation_set_resource_interfaces(iotcon_representation_h repr,
327 * @brief Gets resource interfaces from the representation.
331 * @param[in] repr The representation handle
332 * @param[out] ifaces The interfaces to get
334 * @return Interfaces to get. Interfaces may contain multiple interfaces.
335 * @retval #IOTCON_INTERFACE_NONE Not set
336 * @retval Bitwise OR value which consists of iotcon_interface_e items
338 int iotcon_representation_get_resource_interfaces(iotcon_representation_h repr,
342 * @brief Sets a new state handle into the representation.
346 * @param[in] repr The representation handle
347 * @param[in] state The state handle to set newly
349 * @return 0 on success, otherwise a negative error value.
350 * @retval #IOTCON_ERROR_NONE Successful
351 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
352 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
354 int iotcon_representation_set_state(iotcon_representation_h repr, iotcon_state_h state);
357 * @brief Gets a state handle in the representation.
361 * @param[in] repr The representation handle
362 * @param[in] state The state handle to get
364 * @return 0 on success, otherwise a negative error value.
365 * @retval #IOTCON_ERROR_NONE Successful
366 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
368 int iotcon_representation_get_state(iotcon_representation_h repr, iotcon_state_h *state);
371 * @brief Deletes state handle in the representation.
375 * @param[in] repr The representation handle
377 * @return 0 on success, otherwise a negative error value.
378 * @retval #IOTCON_ERROR_NONE Successful
379 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
381 int iotcon_representation_del_state(iotcon_representation_h repr);
384 * @brief Appends a new child representation on to the end of the parent representation
385 * @details Duplicated child representation is allowed to append.
389 * @param[in] parent The parent representation handle
390 * @param[in] child The child representation handle
392 * @return 0 on success, otherwise a negative error value.
393 * @retval #IOTCON_ERROR_NONE Successful
394 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
396 int iotcon_representation_append_child(iotcon_representation_h parent,
397 iotcon_representation_h child);
400 * @brief Removes a child representation from parent representation without freeing.
404 * @param[in] parent The parent representation handle
405 * @param[in] child The child representation handle
407 * @return 0 on success, otherwise a negative error value.
408 * @retval #IOTCON_ERROR_NONE Successful
409 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
411 int iotcon_representation_remove_child(iotcon_representation_h parent,
412 iotcon_representation_h child);
415 * @brief Specifies the type of function passed to iotcon_representation_foreach_children()
419 * @param[in] child The child representation handle
420 * @param[in] user_data The user data to pass to the function
422 * @return true to continue with the next iteration of the loop,
423 * otherwise false to break out of the loop. #IOTCON_FUNC_CONTINUE and #IOTCON_FUNC_STOP
424 * are more friendly values for the return.
426 * @pre iotcon_representation_foreach_children() will invoke this callback function.
428 * @see iotcon_representation_foreach_children()
431 typedef bool (*iotcon_children_cb)(iotcon_representation_h child, void *user_data);
434 * @brief Calls a function for each children representation of parent.
435 * @details iotcon_children_cb() will be called for each child.
439 * @param[in] parent The parent representation handle
440 * @param[in] cb The callback function to invoke
441 * @param[in] user_data The user data to pass to the function
443 * @return 0 on success, otherwise a negative error value.
444 * @retval #IOTCON_ERROR_NONE Successful
445 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
447 * @post iotcon_children_cb() will be called for each child.
449 * @see iotcon_children_cb()
451 int iotcon_representation_foreach_children(iotcon_representation_h parent,
452 iotcon_children_cb cb, void *user_data);
455 * @brief Gets the number of children representation in the parent representation
459 * @param[in] parent The parent representation handle
460 * @param[out] count The number of children representation
462 * @return 0 on success, otherwise a negative error value.
463 * @retval #IOTCON_ERROR_NONE Successful
464 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
466 int iotcon_representation_get_children_count(iotcon_representation_h parent,
467 unsigned int *count);
470 * @brief Gets the child representation at the given position.
471 * @details Iterates over the parent until it reaches the @a pos-1 position.
475 * @remarks @a child must not be released using iotcon_representation_destroy().
477 * @param[in] parent The parent representation handle
478 * @param[in] pos The position of the child representation
479 * @param[out] child The handle to the child representation
481 * @return 0 on success, otherwise a negative error value.
482 * @retval #IOTCON_ERROR_NONE Successful
483 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
484 * @retval #IOTCON_ERROR_NO_DATA No data available
486 int iotcon_representation_get_nth_child(iotcon_representation_h parent, int pos,
487 iotcon_representation_h *child);
493 #endif /* __IOT_CONNECTIVITY_MANAGER_STRUCT_REPRESENTATION_H__ */