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)
{
...
}
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;
...
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.
Function Documentation
Creates a response handle.
- Since :
- 3.0
- Parameters:
-
[in] | request | The handle of received request handle |
[out] | response | Generated response handle |
- Returns:
0
on success, otherwise a negative error value
- Return values:
-
- See also:
- iotcon_response_destroy()
Destroys a response handle.
- Since :
- 3.0
- Parameters:
-
[in] | resp | The handle of the response |
- Returns:
0
on success, otherwise a negative error value
- Return values:
-
- See also:
- iotcon_response_create()
Sends response for incoming request.
- Since :
- 3.0
- Privilege Level:
- public
- Privilege:
- http://tizen.org/privilege/internet
- Parameters:
-
[in] | resp | The handle of the response to send |
- Returns:
0
on success, otherwise a negative error value
- Return values:
-
- Precondition:
- iotcon_initialize() should be called to initialize.
Sets header options into the response.
- Since :
- 3.0
- Parameters:
-
[in] | resp | The handle of the response |
[in] | options | The header options of the response |
- Returns:
0
on success, otherwise a negative error value
- Return values:
-
- See also:
- iotcon_response_create()
-
iotcon_response_destroy()
Sets representation into the response.
- Since :
- 3.0
- Parameters:
-
[in] | resp | The handle of the response |
[in] | repr | The representation of the response |
- Returns:
0
on success, otherwise a negative error value
- Return values:
-
- See also:
- iotcon_response_create()
-
iotcon_response_destroy()