Tizen Native API
5.0
|
IoTCon Resource provides API to manage resource.
#include <iotcon.h>
The IoTCon Resource API provides methods for managing handle and resource information. Example :
#include <iotcon.h> static iotcon_resource_h _resource_room; static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data) { // handle request ... } static void _door_request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data) { // handle request ... } static void _create_resource() { int ret; uint8_t policies; iotcon_resource_interfaces_h resource_ifaces = NULL; iotcon_resource_types_h resource_types = NULL; iotcon_resource_h resource_door = NULL; // 1. create room resource policies = IOTCON_RESOURCE_DISCOVERABLE | IOTCON_RESOURCE_OBSERVABLE; ret = iotcon_resource_types_create(&resource_types); if (IOTCON_ERROR_NONE != ret) return; ret = iotcon_resource_types_add(resource_types, "org.tizen.room"); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_types_destroy(resource_types); return; } ret = iotcon_resource_interfaces_create(&resource_ifaces); if (IOTCON_ERROR_NONE != ret) iotcon_resource_types_destroy(resource_types); return; ret = iotcon_resource_interfaces_add(resource_ifaces, IOTCON_INTERFACE_DEFAULT); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_interfaces_destroy(resource_ifaces); iotcon_resource_types_destroy(resource_types); return; } ret = iotcon_resource_interfaces_add(resource_ifaces, IOTCON_INTERFACE_LINK); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_interfaces_destroy(resource_ifaces); iotcon_resource_types_destroy(resource_types); return; } ret = iotcon_resource_interfaces_add(resource_ifaces, IOTCON_INTERFACE_BATCH); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_interfaces_destroy(resource_ifaces); iotcon_resource_types_destroy(resource_types); return; } ret = iotcon_resource_create("/room/1", resource_types, resource_ifaces, policies, _room_request_handler, NULL, &_resource_room); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_interfaces_destroy(resource_ifaces); iotcon_resource_types_destroy(resource_types); return; } iotcon_resource_interfaces_destroy(resource_ifaces); iotcon_resource_types_destroy(resource_types); // 2. create door resource policies = IOTCON_RESOURCE_OBSERVABLE; ret = iotcon_resource_types_create(&resource_types); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_destroy(_resource_room); _resource_room = NULL; return; } ret = iotcon_resource_types_add(resource_types, "org.tizen.door"); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_types_destroy(resource_types); iotcon_resource_destroy(_resource_room); _resource_room = NULL; return; } ret = iotcon_resource_interfaces_create(&resource_ifaces); if (IOTCON_ERROR_NONE != ret) iotcon_resource_types_destroy(resource_types); iotcon_resource_destroy(_resource_room); return; ret = iotcon_resource_interfaces_add(resource_ifaces, IOTCON_INTERFACE_DEFAULT); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_interfaces_destroy(resource_ifaces); iotcon_resource_types_destroy(resource_types); iotcon_resource_destroy(_resource_room); return; } ret = iotcon_resource_create("/door/1", resource_types, resource_ifaces, policies, _door_request_handler, NULL, &resource_door); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_interfaces_destroy(resource_ifaces); iotcon_resource_types_destroy(resource_types); iotcon_resource_destroy(_resource_room); _resource_room = NULL; return; } iotcon_resource_interfaces_destroy(resource_ifaces); iotcon_resource_types_destroy(resource_types); // 3. bind door resource to room resource ret = iotcon_resource_bind_child_resource(_resource_room, resource_door); if (IOTCON_ERROR_NONE != ret) { iotcon_resource_destroy(resource_door); iotcon_resource_destroy(_resource_room); _resource_room = NULL; return; } }
This API is related with the following features:
Functions | |
int | iotcon_resource_create (const char *uri_path, iotcon_resource_types_h res_types, iotcon_resource_interfaces_h ifaces, uint8_t policies, iotcon_request_handler_cb cb, void *user_data, iotcon_resource_h *resource_handle) |
Creates a resource handle and registers the resource in server. | |
int | iotcon_resource_destroy (iotcon_resource_h resource_handle) |
Destroys the resource and releases its data. | |
int | iotcon_resource_bind_interface (iotcon_resource_h resource, const char *iface) |
Binds an interface to the resource. | |
int | iotcon_resource_bind_type (iotcon_resource_h resource_handle, const char *resource_type) |
Binds a type to the resource. | |
int | iotcon_resource_set_request_handler (iotcon_resource_h resource, iotcon_request_handler_cb cb, void *user_data) |
Sets a request handler to the resource. | |
int | iotcon_resource_bind_child_resource (iotcon_resource_h parent, iotcon_resource_h child) |
Binds a child resource into the parent resource. | |
int | iotcon_resource_unbind_child_resource (iotcon_resource_h parent, iotcon_resource_h child) |
Unbinds a child resource from the parent resource. | |
int | iotcon_resource_notify (iotcon_resource_h resource, iotcon_representation_h repr, iotcon_observers_h observers, iotcon_qos_e qos) |
Notifies specific clients that resource's attributes have changed. | |
int | iotcon_resource_get_child_count (iotcon_resource_h resource, unsigned int *count) |
Gets the number of child resources of the resource. | |
int | iotcon_resource_get_nth_child (iotcon_resource_h parent, int index, iotcon_resource_h *child) |
Gets the child resource at the given index in the parent resource. | |
int | iotcon_resource_get_uri_path (iotcon_resource_h resource, char **uri_path) |
Gets an URI path of the resource. | |
int | iotcon_resource_get_types (iotcon_resource_h resource, iotcon_resource_types_h *types) |
Gets the list of types in the resource. | |
int | iotcon_resource_get_interfaces (iotcon_resource_h resource, iotcon_resource_interfaces_h *ifaces) |
Gets the interfaces of the resource. | |
int | iotcon_resource_get_policies (iotcon_resource_h resource, uint8_t *policies) |
Gets the policies in the resource. | |
Typedefs | |
typedef void(* | iotcon_request_handler_cb )(iotcon_resource_h resource, iotcon_request_h request, void *user_data) |
Specifies the type of function passed to iotcon_resource_create() and iotcon_resource_set_request_handler(). |
typedef void(* iotcon_request_handler_cb)(iotcon_resource_h resource, iotcon_request_h request, void *user_data) |
Specifies the type of function passed to iotcon_resource_create() and iotcon_resource_set_request_handler().
Called when server receives request from the client.
[in] | resource | The resource requested |
[in] | request | The request from client |
[in] | user_data | The user data to pass to the function |
int iotcon_resource_bind_child_resource | ( | iotcon_resource_h | parent, |
iotcon_resource_h | child | ||
) |
Binds a child resource into the parent resource.
[in] | parent | The handle of the parent resource |
[in] | child | The handle of the child resource to be added to the parent resource |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
IOTCON_ERROR_ALREADY | Already done |
IOTCON_ERROR_SYSTEM | System error |
IOTCON_ERROR_PERMISSION_DENIED | Permission denied |
int iotcon_resource_bind_interface | ( | iotcon_resource_h | resource, |
const char * | iface | ||
) |
Binds an interface to the resource.
[in] | resource | The handle of the resource |
[in] | iface | The interface to be bound to the resource |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
IOTCON_ERROR_SYSTEM | System error |
IOTCON_ERROR_PERMISSION_DENIED | Permission denied |
IOTCON_ERROR_ALREADY | Already done |
int iotcon_resource_bind_type | ( | iotcon_resource_h | resource_handle, |
const char * | resource_type | ||
) |
Binds a type to the resource.
[in] | resource_handle | The handle of the resource |
[in] | resource_type | The type to be bound to the resource |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
IOTCON_ERROR_SYSTEM | System error |
IOTCON_ERROR_PERMISSION_DENIED | Permission denied |
IOTCON_ERROR_ALREADY | Already done |
int iotcon_resource_create | ( | const char * | uri_path, |
iotcon_resource_types_h | res_types, | ||
iotcon_resource_interfaces_h | ifaces, | ||
uint8_t | policies, | ||
iotcon_request_handler_cb | cb, | ||
void * | user_data, | ||
iotcon_resource_h * | resource_handle | ||
) |
Creates a resource handle and registers the resource in server.
Registers a resource specified by uri_path, res_types, ifaces which has properties in IoTCon server.
When client finds the registered resource, iotcon_request_handler_cb() will be called automatically.
uri_path format would be relative URI path like '/a/light'
res_types is a list of resource types. Create a iotcon_resource_types_h handle and add types string to it.
ifaces is a list of resource interfaces. Create a iotcon_resource_interfaces_h handle and add interfaces string to it.
policies also can contain multiple policies like IOTCON_RESOURCE_DISCOVERABLE | IOTCON_RESOURCE_OBSERVABLE.
iotcon_request_handler_cb() will be called when receive CRUD request to the registered resource.
[in] | uri_path | The URI path of the resource |
[in] | res_types | The list of type of the resource |
[in] | ifaces | The list of interface of the resource |
[in] | policies | The policies of the resource Set of iotcon_resource_policy_e |
[in] | cb | The request handler callback function |
[in] | user_data | The user data to pass to the callback function |
[out] | resource_handle | The handle of the resource |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
IOTCON_ERROR_IOTIVITY | IoTivity errors |
IOTCON_ERROR_OUT_OF_MEMORY | Out of memory |
IOTCON_ERROR_PERMISSION_DENIED | Permission denied |
int iotcon_resource_destroy | ( | iotcon_resource_h | resource_handle | ) |
Destroys the resource and releases its data.
[in] | resource_handle | The handle of the resource to be unregistered |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
IOTCON_ERROR_PERMISSION_DENIED | Permission denied |
int iotcon_resource_get_child_count | ( | iotcon_resource_h | resource, |
unsigned int * | count | ||
) |
Gets the number of child resources of the resource.
[in] | resource | The handle of the resource |
[out] | count | The number of child resources |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
int iotcon_resource_get_interfaces | ( | iotcon_resource_h | resource, |
iotcon_resource_interfaces_h * | ifaces | ||
) |
Gets the interfaces of the resource.
[in] | resource | The handle of the resource |
[out] | ifaces | The interfaces of the resource |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
int iotcon_resource_get_nth_child | ( | iotcon_resource_h | parent, |
int | index, | ||
iotcon_resource_h * | child | ||
) |
Gets the child resource at the given index in the parent resource.
[in] | parent | The handle of the parent resource |
[in] | index | The index of the child resource |
[out] | child | The child resource at the index |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
IOTCON_ERROR_NO_DATA | No data available |
int iotcon_resource_get_policies | ( | iotcon_resource_h | resource, |
uint8_t * | policies | ||
) |
Gets the policies in the resource.
policies can contain multiple policies like IOTCON_RESOURCE_DISCOVERABLE | IOTCON_RESOURCE_OBSERVABLE.
[in] | resource | The handle of the resource |
[out] | policies | The policies of resource Set of iotcon_resource_policy_e |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
int iotcon_resource_get_types | ( | iotcon_resource_h | resource, |
iotcon_resource_types_h * | types | ||
) |
Gets the list of types in the resource.
[in] | resource | The handle of the resource |
[out] | types | The types of the resource |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
int iotcon_resource_get_uri_path | ( | iotcon_resource_h | resource, |
char ** | uri_path | ||
) |
Gets an URI path of the resource.
[in] | resource | The handle of the resource |
[out] | uri_path | The URI path of the resource |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
int iotcon_resource_notify | ( | iotcon_resource_h | resource, |
iotcon_representation_h | repr, | ||
iotcon_observers_h | observers, | ||
iotcon_qos_e | qos | ||
) |
Notifies specific clients that resource's attributes have changed.
If observers is NULL
, the msg will notify to all observers.
[in] | resource | The handle of the resource |
[in] | repr | The handle of the representation |
[in] | observers | The handle of the observers |
[in] | qos | The quality of service for message transfer |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
IOTCON_ERROR_REPRESENTATION | Representation error |
IOTCON_ERROR_SYSTEM | System error |
IOTCON_ERROR_PERMISSION_DENIED | Permission denied |
int iotcon_resource_set_request_handler | ( | iotcon_resource_h | resource, |
iotcon_request_handler_cb | cb, | ||
void * | user_data | ||
) |
Sets a request handler to the resource.
When the resource receive CRUD request, iotcon_request_handler_cb() will be called.
[in] | resource | The handle of the resource |
[in] | cb | The request handler to be bound to the resource |
[in] | user_data | The user data to pass to the callback function |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
int iotcon_resource_unbind_child_resource | ( | iotcon_resource_h | parent, |
iotcon_resource_h | child | ||
) |
Unbinds a child resource from the parent resource.
[in] | parent | The handle of the parent resource |
[in] | child | The handle of the child resource to be unbound from the parent resource |
0
on success, otherwise a negative error value IOTCON_ERROR_NONE | Successful |
IOTCON_ERROR_NOT_SUPPORTED | Not supported |
IOTCON_ERROR_INVALID_PARAMETER | Invalid parameter |
IOTCON_ERROR_SYSTEM | System error |
IOTCON_ERROR_PERMISSION_DENIED | Permission denied |
IOTCON_ERROR_NO_DATA | No data available |