* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glib.h>
+#include <tizen_type.h>
#include "iotcon.h"
#include "ic-utils.h"
-#include "icl-resource-types.h"
-#include "icl-resource.h"
-#include "icl-request.h"
-#include "icl-repr.h"
+#include "icl.h"
+#include "icl-representation.h"
#include "icl-dbus.h"
+#include "icl-request.h"
#include "icl-dbus-type.h"
-#include "icl.h"
+#include "icl-resource-types.h"
+#include "icl-resource.h"
+#include "icl-payload.h"
static void _icl_request_handler(GDBusConnection *connection,
const gchar *sender_name,
gpointer user_data)
{
FN_CALL;
- GVariantIter *options;
- unsigned short option_id;
- char *option_data;
- GVariantIter *query;
+ int ret;
char *key = NULL;
+ char *option_data;
char *value = NULL;
- char *repr_json;
- int request_handle;
- int resource_handle;
+ GVariant *repr_gvar;
+ GVariantIter *query;
+ GVariantIter *options;
+ GVariantIter *repr_iter;
+ unsigned short option_id;
struct icl_resource_request request = {0};
iotcon_resource_h resource = user_data;
iotcon_request_handler_cb cb = resource->cb;
- g_variant_get(parameters, "(ia(qs)a(ss)ii&sii)",
- &request.types,
+ g_variant_get(parameters, "(siia(qs)a(ss)iiavxx)",
+ &request.host_address,
+ &request.connectivity_type,
+ &request.type,
&options,
&query,
&request.observation_info.action,
- &request.observation_info.observer_id,
- &repr_json,
- &request_handle,
- &resource_handle);
+ &request.observation_info.observe_id,
+ &repr_iter,
+ &request.oic_request_h,
+ &request.oic_resource_h);
if (g_variant_iter_n_children(options)) {
- request.header_options = iotcon_options_new();
+ ret = iotcon_options_create(&request.header_options);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_options_create() Fail(%d)", ret);
+ g_variant_iter_free(options);
+ g_variant_iter_free(query);
+ g_variant_iter_free(repr_iter);
+ return;
+ }
+
while (g_variant_iter_loop(options, "(q&s)", &option_id, &option_data))
- iotcon_options_insert(request.header_options, option_id, option_data);
+ iotcon_options_add(request.header_options, option_id, option_data);
}
g_variant_iter_free(options);
if (g_variant_iter_n_children(query)) {
- request.query = iotcon_query_new();
+ ret = iotcon_query_create(&request.query);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_query_create() Fail(%d)", ret);
+ g_variant_iter_free(query);
+ g_variant_iter_free(repr_iter);
+ if (request.header_options)
+ iotcon_options_destroy(request.header_options);
+ return;
+ }
+
while (g_variant_iter_loop(query, "(&s&s)", &key, &value))
- iotcon_query_insert(request.query, key, value);
+ iotcon_query_add(request.query, key, value);
}
g_variant_iter_free(query);
- request.request_handle = GINT_TO_POINTER(request_handle);
- request.resource_handle = GINT_TO_POINTER(resource_handle);
-
- if (ic_utils_dbus_decode_str(repr_json)) {
- request.repr = icl_repr_create_repr(repr_json);
+ if (g_variant_iter_loop(repr_iter, "v", &repr_gvar)) {
+ request.repr = icl_representation_from_gvariant(repr_gvar);
if (NULL == request.repr) {
- ERR("icl_repr_create_repr() Fail");
+ ERR("icl_representation_from_gvariant() Fail");
if (request.query)
- iotcon_query_free(request.query);
+ iotcon_query_destroy(request.query);
if (request.header_options)
- iotcon_options_free(request.header_options);
+ iotcon_options_destroy(request.header_options);
return;
}
}
+ g_variant_iter_free(repr_iter);
- /* TODO remove request.uri */
- request.uri_path = "temp_uri_path";
+ /* for iotcon_resource_notify */
+ if (IOTCON_OBSERVE_REGISTER == request.observation_info.action) {
+ if (NULL == resource->observers)
+ iotcon_observers_create(&resource->observers);
+ iotcon_observers_add(resource->observers, request.observation_info.observe_id);
+ } else if (IOTCON_OBSERVE_DEREGISTER == request.observation_info.action) {
+ iotcon_observers_remove(resource->observers, request.observation_info.observe_id);
+ }
if (cb)
cb(resource, &request, resource->user_data);
- /* To avoid unnecessary ERR log (repr could be NULL) */
+ /* To avoid unnecessary ERR log (representation could be NULL) */
if (request.repr)
- iotcon_repr_free(request.repr);
+ iotcon_representation_destroy(request.repr);
if (request.query)
- iotcon_query_free(request.query);
+ iotcon_query_destroy(request.query);
if (request.header_options)
- iotcon_options_free(request.header_options);
+ iotcon_options_destroy(request.header_options);
}
static void _icl_resource_conn_cleanup(iotcon_resource_h resource)
{
resource->sub_id = 0;
- resource->handle = 0;
+
+ if (resource->handle) {
+ resource->handle = 0;
+ return;
+ }
+
+ iotcon_resource_types_destroy(resource->types);
+ if (resource->observers)
+ iotcon_observers_destroy(resource->observers);
+ free(resource->uri_path);
+ free(resource);
}
/* The length of uri_path should be less than or equal to 36. */
-API iotcon_resource_h iotcon_register_resource(const char *uri_path,
+API int iotcon_resource_create(const char *uri_path,
iotcon_resource_types_h res_types,
int ifaces,
- uint8_t properties,
+ int properties,
iotcon_request_handler_cb cb,
- void *user_data)
+ void *user_data,
+ iotcon_resource_h *resource_handle)
{
- FN_CALL;
- int signal_number;
+ int ret;
unsigned int sub_id;
- GError *error = NULL;
const gchar **types;
- char sig_name[IC_DBUS_SIGNAL_LENGTH];
+ GError *error = NULL;
iotcon_resource_h resource;
+ int64_t signal_number;
+ char signal_name[IC_DBUS_SIGNAL_LENGTH];
- RETV_IF(NULL == icl_dbus_get_object(), NULL);
- RETV_IF(NULL == uri_path, NULL);
- RETVM_IF(IOTCON_URI_PATH_LENGTH_MAX < strlen(uri_path), NULL, "Invalid uri_path(%s)",
- uri_path);
- RETV_IF(NULL == res_types, NULL);
- RETV_IF(NULL == cb, NULL);
+ RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
+ RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
+ RETVM_IF(ICL_URI_PATH_LENGTH_MAX < strlen(uri_path),
+ IOTCON_ERROR_INVALID_PARAMETER, "Invalid uri_path(%s)", uri_path);
+ RETV_IF(NULL == res_types, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
resource = calloc(1, sizeof(struct icl_resource));
if (NULL == resource) {
ERR("calloc() Fail(%d)", errno);
- return NULL;
+ return IOTCON_ERROR_OUT_OF_MEMORY;
}
types = icl_dbus_resource_types_to_array(res_types);
if (NULL == types) {
ERR("icl_dbus_resource_types_to_array() Fail");
free(resource);
- return NULL;
+ return IOTCON_ERROR_OUT_OF_MEMORY;
}
- signal_number = icl_dbus_generate_signal_number();
-
ic_dbus_call_register_resource_sync(icl_dbus_get_object(), uri_path, types, ifaces,
- properties, signal_number, &(resource->handle), NULL, &error);
+ properties, &signal_number, &(resource->handle), NULL, &error);
if (error) {
ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message);
+ ret = icl_dbus_convert_dbus_error(error->code);
g_error_free(error);
free(types);
free(resource);
- return NULL;
+ return ret;
}
free(types);
if (0 == resource->handle) {
ERR("iotcon-daemon Fail");
free(resource);
- return NULL;
+ return IOTCON_ERROR_IOTIVITY;
}
resource->cb = cb;
resource->types = icl_resource_types_ref(res_types);
resource->uri_path = ic_utils_strdup(uri_path);
resource->ifaces = ifaces;
- resource->is_observable = properties & IOTCON_OBSERVABLE;
+ resource->properties = properties;
- snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER,
+ snprintf(signal_name, sizeof(signal_name), "%s_%llx", IC_DBUS_SIGNAL_REQUEST_HANDLER,
signal_number);
- sub_id = icl_dbus_subscribe_signal(sig_name, resource, _icl_resource_conn_cleanup,
+ sub_id = icl_dbus_subscribe_signal(signal_name, resource, _icl_resource_conn_cleanup,
_icl_request_handler);
if (0 == sub_id) {
ERR("icl_dbus_subscribe_signal() Fail");
+ iotcon_resource_types_destroy(res_types);
+ free(resource->uri_path);
free(resource);
- return NULL;
+ return IOTCON_ERROR_DBUS;
}
resource->sub_id = sub_id;
- return resource;
+ *resource_handle = resource;
+
+ return IOTCON_ERROR_NONE;
}
-API int iotcon_unregister_resource(iotcon_resource_h resource)
+API int iotcon_resource_destroy(iotcon_resource_h resource)
{
FN_CALL;
int ret;
GError *error = NULL;
- RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- if (0 == resource->sub_id) {
+ if (0 == resource->handle) { /* iotcon dbus disconnected */
WARN("Invalid Resource handle");
- iotcon_resource_types_free(resource->types);
+ iotcon_resource_types_destroy(resource->types);
+ if (resource->observers)
+ iotcon_observers_destroy(resource->observers);
free(resource->uri_path);
free(resource);
return IOTCON_ERROR_NONE;
}
- ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle,
- &ret, NULL, &error);
- if (error) {
- ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message);
- g_error_free(error);
+ if (NULL == icl_dbus_get_object()) {
+ ERR("icl_dbus_get_object() return NULL");
return IOTCON_ERROR_DBUS;
}
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon-daemon Fail(%d)", ret);
- return icl_dbus_convert_daemon_error(ret);
+ ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle, NULL,
+ &error);
+ if (error) {
+ ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message);
+ ret = icl_dbus_convert_dbus_error(error->code);
+ g_error_free(error);
+ return ret;
}
+ resource->handle = 0;
icl_dbus_unsubscribe_signal(resource->sub_id);
-
- iotcon_resource_types_free(resource->types);
- free(resource->uri_path);
- free(resource);
+ resource->sub_id = 0;
return IOTCON_ERROR_NONE;
}
-API int iotcon_bind_interface(iotcon_resource_h resource, iotcon_interface_e iface)
+API int iotcon_resource_bind_interface(iotcon_resource_h resource,
+ iotcon_interface_e iface)
{
FN_CALL;
int ret;
iface, &ret, NULL, &error);
if (error) {
ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message);
+ ret = icl_dbus_convert_dbus_error(error->code);
g_error_free(error);
- return IOTCON_ERROR_DBUS;
+ return ret;
}
if (IOTCON_ERROR_NONE != ret) {
}
-API int iotcon_bind_type(iotcon_resource_h resource, const char *resource_type)
+API int iotcon_resource_bind_type(iotcon_resource_h resource, const char *resource_type)
{
FN_CALL;
int ret;
RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(NULL == resource_type, IOTCON_ERROR_INVALID_PARAMETER);
- if (IOTCON_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type)) {
+ if (ICL_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type)) {
ERR("Invalid resource_type(%s)", resource_type);
return IOTCON_ERROR_INVALID_PARAMETER;
}
&ret, NULL, &error);
if (error) {
ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message);
+ ret = icl_dbus_convert_dbus_error(error->code);
g_error_free(error);
- return IOTCON_ERROR_DBUS;
+ return ret;
}
if (IOTCON_ERROR_NONE != ret) {
return ret;
}
-
-API int iotcon_bind_request_handler(iotcon_resource_h resource,
- iotcon_request_handler_cb cb)
+API int iotcon_resource_set_request_handler(iotcon_resource_h resource,
+ iotcon_request_handler_cb cb, void *user_data)
{
RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
- WARN("Request handler is changed");
+ DBG("Request handler is changed");
resource->cb = cb;
+ resource->user_data = user_data;
return IOTCON_ERROR_NONE;
}
-API int iotcon_bind_resource(iotcon_resource_h parent, iotcon_resource_h child)
+API int iotcon_resource_bind_child_resource(iotcon_resource_h parent,
+ iotcon_resource_h child)
{
- FN_CALL;
- int ret;
- int i;
GError *error = NULL;
+ int i, ret;
RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
return IOTCON_ERROR_INVALID_PARAMETER;
}
- for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) {
+ for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
if (child == parent->children[i]) {
ERR("Child resource was already bound to parent resource.");
return IOTCON_ERROR_ALREADY;
}
}
- for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) {
+ for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
if (NULL == parent->children[i]) {
ic_dbus_call_bind_resource_sync(icl_dbus_get_object(), parent->handle,
child->handle, &ret, NULL, &error);
if (error) {
ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message);
+ ret = icl_dbus_convert_dbus_error(error->code);
g_error_free(error);
- return IOTCON_ERROR_DBUS;
+ return ret;
}
if (IOTCON_ERROR_NONE != ret) {
}
-API int iotcon_unbind_resource(iotcon_resource_h parent, iotcon_resource_h child)
+API int iotcon_resource_unbind_child_resource(iotcon_resource_h parent,
+ iotcon_resource_h child)
{
- int ret;
- int i;
GError *error = NULL;
+ int i, ret;
RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
child->handle, &ret, NULL, &error);
if (error) {
ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message);
+ ret = icl_dbus_convert_dbus_error(error->code);
g_error_free(error);
- return IOTCON_ERROR_DBUS;
+ return ret;
}
if (IOTCON_ERROR_NONE != ret) {
return icl_dbus_convert_daemon_error(ret);
}
- for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) {
+ for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
if (child == parent->children[i])
parent->children[i] = NULL;
}
RETV_IF(NULL == number, IOTCON_ERROR_INVALID_PARAMETER);
*number = 0;
- for (i = 0; i < IOTCON_CONTAINED_RESOURCES_MAX; i++) {
+ for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
if (resource->children[i])
*number += 1;
}
{
RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
- if ((index < 0) || (IOTCON_CONTAINED_RESOURCES_MAX <= index)) {
+ if ((index < 0) || (ICL_CONTAINED_RESOURCES_MAX <= index)) {
ERR("Invalid index(%d)", index);
return IOTCON_ERROR_INVALID_PARAMETER;
}
}
-API int iotcon_resource_is_observable(iotcon_resource_h resource, bool *observable)
+API int iotcon_resource_get_properties(iotcon_resource_h resource, int *properties)
{
RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == observable, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == properties, IOTCON_ERROR_INVALID_PARAMETER);
- *observable = resource->is_observable;
+ *properties = resource->properties;
return IOTCON_ERROR_NONE;
}
-
-API iotcon_notimsg_h iotcon_notimsg_new(iotcon_repr_h repr, iotcon_interface_e iface)
-{
- iotcon_notimsg_h msg;
-
- RETV_IF(NULL == repr, NULL);
-
- msg = calloc(1, sizeof(struct icl_notify_msg));
- if (NULL == msg) {
- ERR("calloc() Fail(%d)", errno);
- return NULL;
- }
-
- msg->repr = repr;
- icl_repr_inc_ref_count(msg->repr);
- msg->iface = iface;
- msg->error_code = 200;
-
- return msg;
-}
-
-
-API void iotcon_notimsg_free(iotcon_notimsg_h msg)
-{
- RET_IF(NULL == msg);
-
- iotcon_repr_free(msg->repr);
- free(msg);
-}
-
-
-API int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notimsg_h msg,
- iotcon_observers_h observers)
+API int iotcon_resource_notify(iotcon_resource_h resource,
+ iotcon_representation_h repr, iotcon_observers_h observers)
{
int ret;
GError *error = NULL;
- GVariant *noti_msg;
GVariant *obs;
+ GVariant *repr_gvar;
RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER);
if (0 == resource->sub_id) {
ERR("Invalid Resource handle");
return IOTCON_ERROR_INVALID_PARAMETER;
}
- noti_msg = icl_dbus_notimsg_to_gvariant(msg);
- if (NULL == noti_msg) {
- ERR("icl_dbus_notimsg_to_gvariant() Fail");
- return IOTCON_ERROR_REPRESENTATION;
+ repr_gvar = icl_dbus_representation_to_gvariant(repr);
+ if (NULL == repr_gvar) {
+ ERR("icl_representation_to_gvariant() Fail");
+ return IOTCON_ERROR_SYSTEM;
}
- obs = icl_dbus_observers_to_gvariant(observers);
- ic_dbus_call_notify_list_of_observers_sync(icl_dbus_get_object(), resource->handle,
- noti_msg, obs, &ret, NULL, &error);
+ if (observers)
+ obs = icl_dbus_observers_to_gvariant(observers);
+ else
+ obs = icl_dbus_observers_to_gvariant(resource->observers);
+
+ ic_dbus_call_notify_sync(icl_dbus_get_object(), resource->handle, repr_gvar, obs,
+ &ret, NULL, &error);
if (error) {
- ERR("ic_dbus_call_notify_list_of_observers_sync() Fail(%s)", error->message);
+ ERR("ic_dbus_call_notify_sync() Fail(%s)", error->message);
+ ret = icl_dbus_convert_dbus_error(error->code);
g_error_free(error);
g_variant_unref(obs);
- g_variant_unref(noti_msg);
- return IOTCON_ERROR_DBUS;
+ g_variant_unref(repr_gvar);
+ return ret;
}
if (IOTCON_ERROR_NONE != ret) {
return IOTCON_ERROR_NONE;
}
-
-API int iotcon_notify_all(iotcon_resource_h resource)
-{
- int ret;
- GError *error = NULL;
-
- RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
- RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
- if (0 == resource->sub_id) {
- ERR("Invalid Resource handle");
- return IOTCON_ERROR_INVALID_PARAMETER;
- }
-
- ic_dbus_call_notify_all_sync(icl_dbus_get_object(), resource->handle, &ret, NULL,
- &error);
- if (error) {
- ERR("ic_dbus_call_notify_all_sync() Fail(%s)", error->message);
- g_error_free(error);
- return IOTCON_ERROR_DBUS;
- }
-
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon-daemon Fail(%d)", ret);
- return icl_dbus_convert_daemon_error(ret);
- }
-
- return IOTCON_ERROR_NONE;
-}