Tizen Native API  3.0
Lite Resource

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

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().

Since :
3.0
Parameters:
[in]resourceThe handle of the lite resource
[in]attributesThe attributes of the lite resource
[in]user_dataThe user data to pass to the function
Returns:
true to accept post request, otherwise false to reject it
Precondition:
The callback must be registered using iotcon_lite_resource_create().
See also:
iotcon_lite_resource_create()

Function Documentation

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.

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
Remarks:
uri_path length must be less than 128.
You must destroy resource_handle by calling iotcon_lite_resource_destroy() if remote_handle is no longer needed.
Parameters:
[in]uri_pathThe URI path of the resource
[in]res_typesThe list of type of the resource
[in]policiesThe policies of the resource
Set of iotcon_resource_policy_e
[in]attributesThe attributes handle to set
[in]cbThe callback function to add into callback list
[in]user_dataThe user data to pass to the callback function
[out]resource_handleThe handle of the resource
Returns:
0 on success, otherwise a negative error value
Return values:
IOTCON_ERROR_NONESuccessful
IOTCON_ERROR_NOT_SUPPORTEDNot supported
IOTCON_ERROR_INVALID_PARAMETERInvalid parameter
IOTCON_ERROR_IOTIVITYIoTivity errors
IOTCON_ERROR_OUT_OF_MEMORYOut of memory
IOTCON_ERROR_PERMISSION_DENIEDPermission denied
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
Remarks:
When a normal variable is used, there is only permission denied error. If the errors of this API are not handled, then you must check an application having the privileges for the API.
Parameters:
[in]resourceThe handle of the lite resource to be unregistered
Returns:
0 on success, otherwise a negative error value
Return values:
IOTCON_ERROR_NONESuccessful
IOTCON_ERROR_NOT_SUPPORTEDNot supported
IOTCON_ERROR_INVALID_PARAMETERInvalid parameter
IOTCON_ERROR_PERMISSION_DENIEDPermission denied
Precondition:
iotcon_initialize() should be called to initialize.
See also:
iotcon_lite_resource_create()

Gets attributes from the lite resource handle.

Since :
3.0
Remarks:
attributes must not be released using iotcon_attributes_destroy().
Parameters:
[in]resourceThe handle of the lite resource
[out]attributesThe attributes handle of the lite resource
Returns:
0 on success, otherwise a negative error value
Return values:
IOTCON_ERROR_NONESuccessful
IOTCON_ERROR_NOT_SUPPORTEDNot supported
IOTCON_ERROR_INVALID_PARAMETERInvalid parameter
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]resourceThe handle of the lite resource
[in]attributesThe attributes handle to update
Returns:
0 on success, otherwise a negative error value
Return values:
IOTCON_ERROR_NONESuccessful
IOTCON_ERROR_NOT_SUPPORTEDNot supported
IOTCON_ERROR_INVALID_PARAMETERInvalid parameter
IOTCON_ERROR_OUT_OF_MEMORYOut of memory
IOTCON_ERROR_PERMISSION_DENIEDPermission denied
Precondition:
iotcon_initialize() should be called to initialize.
See also:
iotcon_lite_resource_get_attributes()