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_STATE_H__
17 #define __IOT_CONNECTIVITY_MANAGER_STRUCT_STATE_H__
19 #include <iotcon-types.h>
22 * @file iotcon-state.h
26 * @ingroup CAPI_IOT_CONNECTIVITY_COMMON_REPRESENTATION_MODULE
27 * @defgroup CAPI_IOT_CONNECTIVITY_COMMON_REPRESENTATION_STATE_MODULE State
29 * @brief Iotcon State provides API to manage state.
31 * @section CAPI_IOT_CONNECTIVITY_COMMON_REPRESENTATION_STATE_MODULE_HEADER Required Header
32 * \#include <iotcon.h>
34 * @section CAPI_IOT_CONNECTIVITY_COMMON_REPRESENTATION_STATE_MODULE_OVERVIEW Overview
35 * The iotcon state API provides string key based hash table.
41 static void _request_handler(iotcon_resource_h resource, iotcon_request_h request,
47 ret = iotcon_request_get_types(request, &types);
48 if (IOTCON_ERROR_NONE != ret)
51 if (IOTCON_REQUEST_GET & types) {
52 iotcon_response_h response = NULL;
53 iotcon_representation_h representation = NULL;
54 iotcon_state_h state = NULL;
56 ret = iotcon_response_create(request, &response);
57 if (IOTCON_ERROR_NONE != ret)
60 ret = iotcon_representation_create(&representation);
61 if (IOTCON_ERROR_NONE != ret) {
62 iotcon_response_destroy(resopnse);
68 ret = iotcon_state_create(&state);
69 if (IOTCON_ERROR_NONE != ret) {
70 iotcon_representation_destroy(representation);
71 iotcon_response_destroy(resopnse);
75 ret = iotcon_state_add_bool(state, "power", true);
76 if (IOTCON_ERROR_NONE != ret) {
77 iotcon_state_destroy(state);
78 iotcon_representation_destroy(representation);
79 iotcon_response_destroy(resopnse);
83 ret = itocon_state_set_int(state, "brightness", 75);
84 if (IOTCON_ERROR_NONE != ret) {
85 iotcon_state_destroy(state);
86 iotcon_representation_destroy(representation);
87 iotcon_response_destroy(resopnse);
91 ret = iotcon_representation_set_state(representation, state);
92 if (IOTCON_ERROR_NONE != ret) {
93 iotcon_state_destroy(state);
94 iotcon_representation_destroy(representation);
95 iotcon_response_destroy(resopnse);
101 ret = iotcon_response_set_representation(response, IOTCON_INTERFACE_DEFAULT,
103 if (IOTCON_ERROR_NONE != ret) {
104 iotcon_state_destroy(state);
105 iotcon_representation_destroy(representation);
106 iotcon_response_destroy(resopnse);
110 ret = iotcon_response_send(response);
111 if (IOTCON_ERROR_NONE != ret) {
112 iotcon_state_destroy(state);
113 iotcon_representation_destroy(representation);
114 iotcon_response_destroy(resopnse);
118 iotcon_state_destroy(state);
119 iotcon_representation_destroy(representation);
120 iotcon_response_destroy(resopnse);
130 * @brief Creates a new state handle.
134 * @remarks You must destroy @a state by calling iotcon_state_destroy()
135 * if @a state is no longer needed.
137 * @param[out] state A newly allocated state handle
139 * @return 0 on success, otherwise a negative error value.
140 * @retval #IOTCON_ERROR_NONE Successful
141 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
142 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
144 * @see iotcon_state_destroy()
146 int iotcon_state_create(iotcon_state_h *state);
149 * @brief Destroys a state.
150 * @details Releases a @a state and its internal data.
154 * @param[in] state The state handle to free
158 * @see iotcon_state_create()
160 void iotcon_state_destroy(iotcon_state_h state);
163 * @brief Clones a state handle.
167 * @remarks You must destroy @a state_clone by calling iotcon_state_destroy()
168 * if @a state_clone is no longer needed.
170 * @param[in] state The state handle
171 * @param[out] state_clone The cloned state handle
173 * @return 0 on success, otherwise a negative error value.
174 * @retval #IOTCON_ERROR_NONE Successful
175 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
176 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
178 * @see iotcon_state_create()
179 * @see iotcon_state_destroy()
181 int iotcon_state_clone(iotcon_state_h state, iotcon_state_h *state_clone);
184 * @brief Adds a new key and integer value into the representation.
185 * @details If @a key is already exists, current value will be replaced with new @a val.
189 * @param[in] state The state handle
190 * @param[in] key The key
191 * @param[in] val The value
193 * @return 0 on success, otherwise a negative error value.
194 * @retval #IOTCON_ERROR_NONE Successful
195 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
196 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
198 int iotcon_state_add_int(iotcon_state_h state, const char *key, int val);
201 * @brief Adds a new key and boolean value into the representation.
202 * @details If @a key is already exists, current value will be replaced with new @a val.
206 * @param[in] state The state handle
207 * @param[in] key The key
208 * @param[in] val The value
210 * @return 0 on success, otherwise a negative error value.
211 * @retval #IOTCON_ERROR_NONE Successful
212 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
213 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
215 int iotcon_state_add_bool(iotcon_state_h state, const char *key, bool val);
218 * @brief Adds a new key and double value into the representation.
219 * @details If @a key is already exists, current value will be replaced with new @a val.
223 * @param[in] state The state handle
224 * @param[in] key The key
225 * @param[in] val The value
227 * @return 0 on success, otherwise a negative error value.
228 * @retval #IOTCON_ERROR_NONE Successful
229 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
230 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
232 int iotcon_state_add_double(iotcon_state_h state, const char *key, double val);
235 * @brief Adds a new key and string value into the representation.
236 * @details If @a key is already exists, current value will be replaced with new @a val.
240 * @param[in] state The state handle
241 * @param[in] key The key
242 * @param[in] val The value
244 * @return 0 on success, otherwise a negative error value.
245 * @retval #IOTCON_ERROR_NONE Successful
246 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
247 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
249 int iotcon_state_add_str(iotcon_state_h state, const char *key, char *val);
252 * @brief Adds a new key and list value into the representation.
253 * @details If @a key is already exists, current list will be replaced with new @a list.
257 * @param[in] state The state handle
258 * @param[in] key The key
259 * @param[in] list The value
261 * @return 0 on success, otherwise a negative error value.
262 * @retval #IOTCON_ERROR_NONE Successful
263 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
264 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
266 int iotcon_state_add_list(iotcon_state_h state, const char *key, iotcon_list_h list);
269 * @brief Adds a new key and state value into the representation.
270 * @details If @a key is already exists, current state will be replaced with new @a src.
274 * @param[in] dest The state handle
275 * @param[in] key The key
276 * @param[in] src The state handle to set newly
278 * @return 0 on success, otherwise a negative error value.
279 * @retval #IOTCON_ERROR_NONE Successful
280 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
281 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
283 int iotcon_state_add_state(iotcon_state_h dest, const char *key, iotcon_state_h src);
286 * @brief Adds a new key with NULL value into the representation.
287 * @details If @a key is already exists, current value will be replaced with NULL
291 * @param[in] state The state handle
292 * @param[in] key The key to be set NULL
294 * @return 0 on success, otherwise a negative error value.
295 * @retval #IOTCON_ERROR_NONE Successful
296 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
297 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
299 int iotcon_state_add_null(iotcon_state_h state, const char *key);
302 * @brief Gets the integer value from the given key.
306 * @param[in] state The state handle
307 * @param[in] key The key
308 * @param[out] val The integer value
310 * @return 0 on success, otherwise a negative error value.
311 * @retval #IOTCON_ERROR_NONE Successful
312 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
313 * @retval #IOTCON_ERROR_NO_DATA No data available
314 * @retval #IOTCON_ERROR_INVALID_TYPE Invalid type
316 int iotcon_state_get_int(iotcon_state_h state, const char *key, int *val);
319 * @brief Gets the boolean value from the given key.
323 * @param[in] state The state handle
324 * @param[in] key The key
325 * @param[out] val The boolean value
327 * @return 0 on success, otherwise a negative error value.
328 * @retval #IOTCON_ERROR_NONE Successful
329 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
330 * @retval #IOTCON_ERROR_NO_DATA No data available
331 * @retval #IOTCON_ERROR_INVALID_TYPE Invalid type
333 int iotcon_state_get_bool(iotcon_state_h state, const char *key, bool *val);
336 * @brief Gets the double value from the given key.
340 * @param[in] state The state handle
341 * @param[in] key The key
342 * @param[out] val The double value
344 * @return 0 on success, otherwise a negative error value.
345 * @retval #IOTCON_ERROR_NONE Successful
346 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
347 * @retval #IOTCON_ERROR_NO_DATA No data available
348 * @retval #IOTCON_ERROR_INVALID_TYPE Invalid type
350 int iotcon_state_get_double(iotcon_state_h state, const char *key, double *val);
353 * @brief Gets the string value from the given key.
357 * @remarks @a val must not be released using free().
359 * @param[in] state The state handle
360 * @param[in] key The key
361 * @param[out] val The string value
363 * @return 0 on success, otherwise a negative error value.
364 * @retval #IOTCON_ERROR_NONE Successful
365 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
366 * @retval #IOTCON_ERROR_NO_DATA No data available
367 * @retval #IOTCON_ERROR_INVALID_TYPE Invalid type
369 int iotcon_state_get_str(iotcon_state_h state, const char *key, char **val);
372 * @brief Gets the list value from the given key.
376 * @remarks @a list must not be released using iotcon_list_destroy().
378 * @param[in] state The state handle
379 * @param[in] key The key
380 * @param[out] list The list value
382 * @return 0 on success, otherwise a negative error value.
383 * @retval #IOTCON_ERROR_NONE Successful
384 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
385 * @retval #IOTCON_ERROR_NO_DATA No data available
386 * @retval #IOTCON_ERROR_INVALID_TYPE Invalid type
388 int iotcon_state_get_list(iotcon_state_h state, const char *key, iotcon_list_h *list);
391 * @brief Gets the state value from the given key.
395 * @remarks @a state must not be released using iotcon_state_destroy().
397 * @param[in] src The state handle
398 * @param[in] key The key
399 * @param[out] dest The state value at the key
401 * @return 0 on success, otherwise a negative error value.
402 * @retval #IOTCON_ERROR_NONE Successful
403 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
404 * @retval #IOTCON_ERROR_NO_DATA No data available
405 * @retval #IOTCON_ERROR_INVALID_TYPE Invalid type
407 int iotcon_state_get_state(iotcon_state_h src, const char *key, iotcon_state_h *dest);
410 * @brief Checks whether the value of given key is NULL or not.
414 * @param[in] state The state handle
415 * @param[in] key The key
416 * @param[out] is_null true if the type of the given key is null, otherwise false
418 * @return 0 on success, otherwise a negative error value.
419 * @retval #IOTCON_ERROR_NONE Successful
420 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
422 int iotcon_state_is_null(iotcon_state_h state, const char *key, bool *is_null);
425 * @brief Removes the key and its associated value from the state.
429 * @param[in] state The state handle
430 * @param[in] key The key
432 * @return 0 on success, otherwise a negative error value.
433 * @retval #IOTCON_ERROR_NONE Successful
434 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
435 * @retval #IOTCON_ERROR_NO_DATA No data available
437 int iotcon_state_remove(iotcon_state_h state, const char *key);
440 * @brief Gets the type of a value at the given key.
441 * @details It gets the data type of value related the @a key in @a state.
442 * The data type could be one of #iotcon_type_e.
446 * @param[in] state The state handle
447 * @param[in] key The key
448 * @param[out] type The data type of value related the key in state handle.
450 * @return 0 on success, otherwise a negative error value.
451 * @retval #IOTCON_ERROR_NONE Successful
452 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
453 * @retval #IOTCON_ERROR_NO_DATA No data available
455 int iotcon_state_get_type(iotcon_state_h state, const char *key,
456 iotcon_type_e *type);
459 * @brief Specifies the type of function passed to iotcon_state_foreach()
463 * @param[in] state The state handle
464 * @param[in] key The key
465 * @param[in] user_data The user data to pass to the function
467 * @return true to continue with the next iteration of the loop,
468 * otherwise false to break out of the loop. #IOTCON_FUNC_CONTINUE and #IOTCON_FUNC_STOP
469 * are more friendly values for the return.
471 * @pre iotcon_state_foreach() will invoke this callback function.
473 * @see iotcon_state_foreach()
475 typedef bool (*iotcon_state_cb)(iotcon_state_h state, const char *key, void *user_data);
478 * @brief Calls a function for each element of state.
479 * @details iotcon_state_cb() will be called for each child.
483 * @param[in] state The state handle
484 * @param[in] cb The callback function to invoke
485 * @param[in] user_data The user data to pass to the function
487 * @return 0 on success, otherwise a negative error value.
488 * @retval #IOTCON_ERROR_NONE Successful
489 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
491 * @post iotcon_state_cb() will be called for each child.
493 * @see iotcon_state_cb()
495 int iotcon_state_foreach(iotcon_state_h state, iotcon_state_cb cb, void *user_data);
498 * @brief Gets the number of keys in the state.
502 * @param[in] state The state handle
503 * @param[out] count The number of keys
505 * @return 0 on success, otherwise a negative error value.
506 * @retval #IOTCON_ERROR_NONE Successful
507 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
509 int iotcon_state_get_keys_count(iotcon_state_h state, unsigned int *count);
515 #endif /* __IOT_CONNECTIVITY_MANAGER_STRUCT_STATE_H__ */