IoTCon Lite Resource provides API to encapsulate resources.
Required Header
#include <iotcon.h>
Overview
This API provides that the users manage resources without request handler. When client request by CRUD functions, internal default request handler will be invoked. The default request handler will create response and send to client automatically. When updated attributes by iotcon_lite_resource_update_attributes(), changes will be notified to observers. Example :
#include <iotcon.h>
...
static iotcon_lite_resource_h _resource;
static bool _attributes_changed_cb(iotcon_lite_resource_h resource, iotcon_attributes_h attributes, void *user_data)
{
return true;
}
static void _create_light_resource()
{
int ret;
iotcon_lite_resource_h resource = NULL;
iotcon_resource_types_h resource_types = NULL;
iotcon_attributes_h attributes = NULL;
ret = iotcon_resource_types_create(&resource_types);
if (IOTCON_ERROR_NONE != ret)
return;
ret = iotcon_resource_types_add(resource_types, "org.tizen.light");
if (IOTCON_ERROR_NONE != ret) {
iotcon_resource_types_destroy(resource_types);
return;
}
ret = iotcon_attributes_create(&attributes);
if (IOTCON_ERROR_NONE != ret) {
iotcon_resource_types_destroy(resource_types);
return;
}
ret = iotcon_attributes_add_bool(attributes, "power", true);
if (IOTCON_ERROR_NONE != ret) {
iotcon_attributes_destroy(attributes);
iotcon_resource_types_destroy(resource_types);
return;
}
ret = iotcon_attributes_add_int(attributes, "brightness", 75);
if (IOTCON_ERROR_NONE != ret) {
iotcon_attributes_destroy(attributes);
iotcon_resource_types_destroy(resource_types);
return;
}
ret = iotcon_lite_resource_create("/light/1", resource_types,
IOTCON_RESOURCE_DISCOVERABLE | IOTCON_RESOURCE_OBSERVABLE, attributes,
_attributes_changed_cb, NULL, &resource);
if (IOTCON_ERROR_NONE != ret) {
iotcon_attributes_destroy(attributes);
iotcon_resource_types_destroy(resource_types);
return;
}
iotcon_attributes_destroy(attributes);
iotcon_resource_types_destroy(resource_types);
_resource = resource;
}
static void _update_brightness(int brightness)
{
int ret;
iotcon_attributes_h attributes = NULL;
iotcon_attributes_h attributes_clone = NULL;
ret = iotcon_lite_resource_get_attributes(_resource, &attributes);
if (IOTCON_ERROR_NONE != ret)
return;
ret = iotcon_attributes_clone(attributes, &attributes_clone);
if (IOTCON_ERROR_NONE != ret)
return;
ret = iotcon_attributes_add_int(attributes_clone, "brightness", brightness);
if (IOTCON_ERROR_NONE != ret) {
iotcon_attributes_destroy(attributes_clone);
return;
}
ret = iotcon_lite_resource_update_attributes(_resource, attributes_clone);
if (IOTCON_ERROR_NONE != ret) {
iotcon_attributes_destroy(attributes_clone);
return;
}
iotcon_attributes_destroy(attributes_clone);
}
Related Features
This API is related with the following features:
- http://tizen.org/feature/iot.ocf
It is recommended to design feature related codes in your application for reliability.
You can check if a device supports the related features for this API by using System Information, thereby controlling the procedure of your application.
To ensure your application is only running on the device with specific features, please define the features in your manifest file using the manifest editor in the SDK.
More details on featuring your application can be found from Feature Element.
Functions |
int | iotcon_lite_resource_create (const char *uri_path, iotcon_resource_types_h res_types, uint8_t policies, iotcon_attributes_h attributes, iotcon_lite_resource_post_request_cb cb, void *user_data, iotcon_lite_resource_h *resource_handle) |
| Creates a lite resource handle and registers the resource in server.
|
int | iotcon_lite_resource_destroy (iotcon_lite_resource_h resource) |
| Destroys the resource and releases its data.
|
int | iotcon_lite_resource_update_attributes (iotcon_lite_resource_h resource, iotcon_attributes_h attributes) |
| Updates attributes into the lite resource handle.
|
int | iotcon_lite_resource_get_attributes (iotcon_lite_resource_h resource, iotcon_attributes_h *attributes) |
| Gets attributes from the lite resource handle.
|
Typedefs |
typedef bool(* | iotcon_lite_resource_post_request_cb )(iotcon_lite_resource_h resource, iotcon_attributes_h attributes, void *user_data) |
| Specifies the type of function passed to iotcon_lite_resource_create().
|
Typedef Documentation
Function Documentation
Creates a lite resource handle and registers the resource in server.
Registers a resource specified by uri_path, res_types, attributes which have properties in IoTCon server.
When client requests some operations, it send a response to client, automatically.
The policies can contain multiple policies like IOTCON_RESOURCE_DISCOVERABLE | IOTCON_RESOURCE_OBSERVABLE.
- Since :
- 3.0
- Privilege Level:
- public
- Privilege:
- http://tizen.org/privilege/internet
- Parameters:
-
[in] | uri_path | The URI path of the resource |
[in] | res_types | The list of type of the resource |
[in] | policies | The policies of the resource
Set of iotcon_resource_policy_e |
[in] | attributes | The attributes handle to set |
[in] | cb | The callback function to add into callback list |
[in] | user_data | The user data to pass to the callback function |
[out] | resource_handle | The handle of the resource |
- Returns:
0
on success, otherwise a negative error value
- Return values:
-
- Precondition:
- iotcon_initialize() should be called to initialize.
- See also:
- iotcon_lite_resource_destroy()
Destroys the resource and releases its data.
- Since :
- 3.0
- Privilege Level:
- public
- Privilege:
- http://tizen.org/privilege/internet
- Parameters:
-
[in] | resource | The handle of the lite resource to be unregistered |
- Returns:
0
on success, otherwise a negative error value
- Return values:
-
- Precondition:
- iotcon_initialize() should be called to initialize.
- See also:
- iotcon_lite_resource_create()
Gets attributes from the lite resource handle.
- Since :
- 3.0
- Parameters:
-
[in] | resource | The handle of the lite resource |
[out] | attributes | The attributes handle of the lite resource |
- Returns:
0
on success, otherwise a negative error value
- Return values:
-
- See also:
- iotcon_lite_resource_update_attributes()
Updates attributes into the lite resource handle.
- Since :
- 3.0
- Privilege Level:
- public
- Privilege:
- http://tizen.org/privilege/internet
- Parameters:
-
[in] | resource | The handle of the lite resource |
[in] | attributes | The attributes handle to update |
- Returns:
0
on success, otherwise a negative error value
- Return values:
-
- Precondition:
- iotcon_initialize() should be called to initialize.
- See also:
- iotcon_lite_resource_get_attributes()