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_SERVER_LITE_RESOURCE_H__
17 #define __IOT_CONNECTIVITY_MANAGER_SERVER_LITE_RESOURCE_H__
19 #include <iotcon-types.h>
22 * @file iotcon-lite-resource.h
26 * @ingroup CAPI_IOT_CONNECTIVITY_SERVER_MODULE
27 * @defgroup CAPI_IOT_CONNECTIVITY_SERVER_LITE_RESOURCE_MODULE Lite Resource
29 * @brief Iotcon Lite Resource provides API to encapsulate resources.
31 * @section CAPI_IOT_CONNECTIVITY_SERVER_LITE_RESOURCE_MODULE_HEADER Required Header
32 * \#include <iotcon.h>
34 * @section CAPI_IOT_CONNECTIVITY_SERVER_LITE_RESOURCE_MODULE_OVERVIEW Overview
35 * This API provides that the users manages resources without request handler.
36 * When client request by CRUD functions, internal default request handler will be invoked.
37 * The default request handler will create response and send to client automatically.
38 * When updated state by iotcon_lite_update_state(), changes will notify to observers.
44 static iotcon_lite_resource_h _resource;
46 static void _create_light_resource()
49 iotcon_lite_resource_h resource = NULL;
50 iotcon_resource_types_h resource_types = NULL;
51 iotcon_state_h state = NULL;
53 ret = iotcon_resource_types_create(&resource_types);
54 if (IOTCON_ERROR_NONE != ret)
57 ret = iotcon_resource_types_add(resource_types, "org.tizen.light");
58 if (IOTCON_ERROR_NONE != ret) {
59 iotcon_resource_types_destroy(resource_types);
63 ret = iotcon_state_create(&state);
64 if (IOTCON_ERROR_NONE != ret) {
65 iotcon_resource_types_destroy(resource_types);
69 ret = iotcon_state_set_bool(state, "power", true);
70 if (IOTCON_ERROR_NONE != ret) {
71 iotcon_state_destroy(state);
72 iotcon_resource_types_destroy(resource_types);
76 ret = iotcon_state_set_int(state, "brightness", 75);
77 if (IOTCON_ERROR_NONE != ret) {
78 iotcon_state_destroy(state);
79 iotcon_resource_types_destroy(resource_types);
83 ret = iotcon_lite_resource_create("/light/1", resource_types,
84 IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE, state, &resource);
85 if (IOTCON_ERROR_NONE != ret) {
86 iotcon_state_destroy(state);
87 iotcon_resource_types_destroy(resource_types);
91 iotcon_state_destroy(state);
92 iotcon_resource_types_destroy(resource_types);
97 static void _update_brightness(int brightness)
100 iotcon_state_h state = NULL;
101 iotcon_state_h state_clone = NULL;
103 ret = iotcon_lite_resource_get_state(_resource, &state);
104 if (IOTCON_ERROR_NONE != ret)
107 ret = iotcon_state_clone(state, &state_clone);
108 if (IOTCON_ERROR_NONE != ret)
111 ret = iotcon_state_set_int(state_clone, "brightness", brightness);
112 if (IOTCON_ERROR_NONE != ret) {
113 iotcon_state_destroy(state_clone);
117 ret = iotcon_lite_resource_update_state(_resource, state_clone);
118 if (IOTCON_ERROR_NONE != ret) {
119 iotcon_state_destroy(state_clone);
123 iotcon_state_destroy(state_clone);
132 * @brief Creates a lite resource handle and registers the resource in server
133 * @details Registers a resource specified by @a uri_path, @a res_types, @a state which have
134 * @a properties in Iotcon server.\n
135 * When client requests some operations, it send a response to client, automatically.\n
139 * @privilege %http://tizen.org/privilege/internet
141 * @remarks @a uri_path length must be less than or equal 36.\n
142 * You must destroy @a resource_handle by calling iotcon_lite_resource_destroy()
143 * if @a remote_handle is no longer needed.
145 * @param[in] uri_path The URI path of the resource.
146 * @param[in] res_types The list of type of the resource.
147 * @param[in] properties The property of the resource.
148 * @param[in] state The state handle to set.
149 * @param[out] resource_handle The handle of the resource
151 * @return 0 on success, otherwise a negative error value.
152 * @retval #IOTCON_ERROR_NONE Successful
153 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
154 * @retval #IOTCON_ERROR_IOTIVITY Iotivity errors
155 * @retval #IOTCON_ERROR_DBUS Dbus errors
156 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
157 * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied
159 * @post When the resource receive CRUD request, iotcon_request_handler_cb() will be called.
161 * @see iotcon_lite_resource_destroy()
163 int iotcon_lite_resource_create(const char *uri_path,
164 iotcon_resource_types_h res_types,
166 iotcon_state_h state,
167 iotcon_lite_resource_h *resource_handle);
170 * @brief Destroys the resource and releases its data.
174 * @privilege %http://tizen.org/privilege/internet
176 * @remarks When a normal variable is used, there are only dbus error and permission\n
177 * denied error. If the errors of this API are not handled, then you must check\n
178 * whether dbus is running and an application have the privileges for the API.
180 * @param[in] resource The handle of the lite resource to be unregistered
182 * @return 0 on success, otherwise a negative error value.
183 * @retval #IOTCON_ERROR_NONE Successful
184 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
185 * @retval #IOTCON_ERROR_DBUS Dbus error
186 * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied
188 * @see iotcon_lite_resource_create()
190 int iotcon_lite_resource_destroy(iotcon_lite_resource_h resource);
193 * @brief Updates state into the lite resource handle
197 * @param[in] resource The handle of the lite resource
198 * @param[in] state The state handle to update
200 * @return 0 on success, otherwise a negative error value.
201 * @retval #IOTCON_ERROR_NONE Successful
202 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
203 * @retval #IOTCON_ERROR_OUT_OF_MEMORY Out of memory
205 * @see iotcon_lite_resource_get_state()
207 int iotcon_lite_resource_update_state(iotcon_lite_resource_h resource,
208 iotcon_state_h state);
211 * @brief Gets state from the lite resource handle
215 * @remarks @a state must not be released using iotcon_state_destroy().
217 * @param[in] resource The handle of the lite resource
218 * @param[out] state The state handle of the lite resource
220 * @return 0 on success, otherwise a negative error value.
221 * @retval #IOTCON_ERROR_NONE Successful
222 * @retval #IOTCON_ERROR_INVALID_PARAMETER Invalid parameter
224 * @see iotcon_lite_resource_update_state()
226 int iotcon_lite_resource_get_state(iotcon_lite_resource_h resource,
227 iotcon_state_h *state);
233 #endif /* __IOT_CONNECTIVITY_MANAGER_SERVER_LITE_RESOURCE_H__ */