Tizen Native API  7.0
Response

IoTCon Response provides API to manage response.

Required Header

#include <iotcon.h>

Overview

The IoTCon Response API provides methods for managing handle and get response information. Example (Client side) :

#include <iotcon.h>

static bool _attributes_foreach(iotcon_attributes_h attributes, const char *key, void *user_data)
{
    // handle attributes
    ...
}

static void _on_get(iotcon_remote_resource_h resource, iotcon_error_e err,
        iotcon_request_type_e request_type, iotcon_response_h response, void *user_data)
{
    int ret;
    iotcon_response_result_e response_result;
    iotcon_representation_h repr = NULL;
    iotcon_attributes_h attributes = NULL;

    if (IOTCON_ERROR_NONE != err)
        return;

    ret = iotcon_response_get_result(response, &response_result);
    if (IOTCON_ERROR_NONE != ret)
        return;

    if (IOTCON_RESPONSE_OK != response_result)
        return;

    ret = iotcon_response_get_representation(response, &repr);
    if (IOTCON_ERROR_NONE != ret)
        return;

    ret = iotcon_representation_get_attributes(repr, &attributes);
    if (IOTCON_ERROR_NONE != ret)
        return;

    ret = iotcon_attributes_foreach(attributes, _attributes_foreach, NULL);
    if (IOTCON_ERROR_NONE != ret)
        return;

    ...
}

static void _request_get(iotcon_remote_resource_h resource)
{
    int ret;
    ret = iotcon_remote_resource_get(resource, NULL, _on_get, NULL);
    if (IOTCON_ERROR_NONE != ret)
        return;
}

Example (Server side) :

#include <iotcon.h>

static iotcon_attributes_h _create_attributes()
{
    int ret;
    iotcon_attributes_h attributes = NULL;

    // create & set attributes
    ...

    return attributes;
}

static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
{
    int ret;
    iotcon_request_type_e type;
    iotcon_query_h query = NULL;

    ret = iotcon_request_get_request_type(request, &type);
    if (IOTCON_ERROR_NONE != ret)
        return;

    ret = iotcon_request_get_query(request, &query);
    if (IOTCON_ERROR_NONE == ret && query) {
        ret = iotcon_query_get_interface(request, &iface);
        if (IOTCON_ERROR_NONE != ret)
            return;
    }

    if (IOTCON_REQUEST_GET == type) {
        iotcon_response_h response = NULL;
        iotcon_representation_h repr = NULL;
        iotcon_attributes_h attributes = NULL;

        ret = iotcon_response_create(request, &response);
        if (IOTCON_ERROR_NONE != ret)
            return;

        ret = iotcon_response_set_result(response, IOTCON_RESPONSE_OK);
        if (IOTCON_ERROR_NONE != ret) {
            iotcon_response_destroy(response);
            return;
        }

        ret = iotcon_representation_create(&repr);
        if (IOTCON_ERROR_NONE != ret) {
            iotcon_response_destroy(response);
            return;
        }

        ret = iotcon_representation_set_uri_path(repr, "/light/1");
        if (IOTCON_ERROR_NONE != ret) {
            iotcon_representation_destroy(repr);
            iotcon_response_destroy(response);
            return;
        }

        ret = iotcon_representation_set_attributes(response, _create_attributes());
        if (IOTCON_ERROR_NONE != ret) {
            iotcon_representation_destroy(repr);
            iotcon_response_destroy(response);
            return;
        }

        ret = iotcon_response_set_representation(response, repr);
        if (IOTCON_ERROR_NONE != ret) {
            iotcon_representation_destroy(repr);
            iotcon_response_destroy(response);
            return;
        }

        ret = iotcon_response_send(response);
        if (IOTCON_ERROR_NONE != ret) {
            iotcon_representation_destroy(repr);
            iotcon_response_destroy(response);
            return;
        }

        iotcon_representation_destroy(repr);
        iotcon_response_destroy(response);
    }
    ...
}

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_response_create (iotcon_request_h request, iotcon_response_h *response)
 Creates a response handle.
int iotcon_response_destroy (iotcon_response_h resp)
 Destroys a response handle.
int iotcon_response_get_options (iotcon_response_h resp, iotcon_options_h *options)
 Gets header options of the response.
int iotcon_response_get_representation (iotcon_response_h resp, iotcon_representation_h *repr)
 Gets representation of the response.
int iotcon_response_get_result (iotcon_response_h resp, iotcon_response_result_e *result)
 Gets result of the response.
int iotcon_response_set_result (iotcon_response_h resp, iotcon_response_result_e result)
 Sets result into the response.
int iotcon_response_set_representation (iotcon_response_h resp, iotcon_representation_h repr)
 Sets representation into the response.
int iotcon_response_set_options (iotcon_response_h resp, iotcon_options_h options)
 Sets header options into the response.
int iotcon_response_send (iotcon_response_h resp)
 Sends response for incoming request.

Function Documentation

int iotcon_response_create ( iotcon_request_h  request,
iotcon_response_h response 
)

Creates a response handle.

Since :
3.0
Remarks:
You must destroy response by calling iotcon_response_destroy() if response is no longer needed.
Parameters:
[in]requestThe handle of received request handle
[out]responseGenerated response handle
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
See also:
iotcon_response_destroy()

Destroys a response handle.

Since :
3.0
Parameters:
[in]respThe handle of the response
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_response_create()

Gets header options of the response.

Since :
3.0
Remarks:
options must not be released using iotcon_options_destroy().
Parameters:
[in]respThe handle of the response
[out]optionsThe handle of the header options
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_response_get_representation()
iotcon_response_get_result()

Gets representation of the response.

Since :
3.0
Remarks:
repr must not be released using iotcon_representation_destroy().
Parameters:
[in]respThe handle of the response
[out]reprThe handle of the representation
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_NO_DATANo data
See also:
iotcon_response_get_options()
iotcon_response_get_result()

Gets result of the response.

Since :
3.0
Parameters:
[in]respThe handle of the response
[out]resultThe result of the response
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_response_get_options()
iotcon_response_get_representation()

Sends response for incoming request.

Since :
3.0
Privilege Level:
public
Privilege:
http://tizen.org/privilege/internet
Parameters:
[in]respThe handle of the response to send
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_SYSTEMSystem error
IOTCON_ERROR_PERMISSION_DENIEDPermission denied
Precondition:
iotcon_initialize() should be called to initialize.

Sets header options into the response.

Since :
3.0
Parameters:
[in]respThe handle of the response
[in]optionsThe header options of the response
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
See also:
iotcon_response_create()
iotcon_response_destroy()

Sets representation into the response.

Since :
3.0
Parameters:
[in]respThe handle of the response
[in]reprThe representation of the response
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
See also:
iotcon_response_create()
iotcon_response_destroy()

Sets result into the response.

The result could be one of iotcon_response_result_e.

Since :
3.0
Parameters:
[in]respThe handle of the response
[in]resultThe result to set
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
See also:
iotcon_response_create()
iotcon_response_destroy()