--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *******************************************************************************/
+/**
+ * @file tbt-iotcon-view.c
+ * @brief covers IoTvitiy connection reiotconed apis
+ * @since_tizen 3.0
+ * @author A. B. M. Nazibullah(nazib.ullah@samsung.com)
+ * @date October, 2016
+ * @bug
+ * @credit
+ *
+ */
+
+#include "utils/app_module_config.h"
+#ifdef TBT_MODULE_IOTCON
+
+#include <Elementary.h>
+#include <iotcon.h>
+#include <glib.h>
+
+#include "utils/logger.h"
+#include "utils/config.h"
+#include "utils/ui-utils.h"
+#include "view/tbt-iotcon-view.h"
+#include "view/tbt-common-view.h"
+
+#define DOOR_RESOURCE_URI "/door/1"
+#define DOOR_RESOURCE_URI2 "/door/2"
+#define DOOR_RESOURCE_TYPE "org.tizen.door"
+
+#define DOOR_RESOURCE_URI_PREFIX "/door"
+
+static bool _resource_created;
+static char *door_resource_device_id;
+
+
+/* Door Resource */
+typedef struct _door_resource_s {
+ bool attributes;
+ char *uri_path;
+ char *type;
+ iotcon_resource_interfaces_h ifaces;
+ uint8_t policies;
+ iotcon_resource_h handle;
+ iotcon_observers_h observers;
+ iotcon_representation_h repr;
+} door_resource_s;
+
+struct _iotcon_view
+{
+ common_view* view;
+
+ Evas_Object *iotcon_label;
+ Evas_Object *iotcon_btn;
+
+ Evas_Object* get_btn;
+ Evas_Object* post_btn;
+ Evas_Object* put_btn;
+ Evas_Object* del_btn;
+
+ Evas_Object* info_list;
+
+ GList *device_id_list;
+
+
+ door_resource_s my_door;
+ iotcon_query_h query;
+
+ iotcon_remote_resource_h resource_clone;
+ bool opened;
+
+ char *host;
+ char *created_uri_path;
+ iotcon_connectivity_type_e connectivity_type;
+ iotcon_resource_types_h types;
+ iotcon_resource_interfaces_h ifaces;
+};
+
+static void _app_destroy_cb(void* this);
+static char* get_iotcon_error(iotcon_error_e err);
+
+static int _set_door_resource(door_resource_s *door);
+static iotcon_resource_h _create_door_resource(char *uri_path, char *type, iotcon_resource_interfaces_h ifaces, uint8_t policies, void *user_data);
+static void _iotcon_server_change_state_btn_pressed_cb(void *data, Evas_Object *obj, void *event_info);
+static iotcon_representation_h _get_door_representation(door_resource_s *door);
+static int _request_handler_get(door_resource_s *door, iotcon_request_h request);
+static int _set_door_representation(door_resource_s *door, iotcon_representation_h repr);
+static int _send_response(iotcon_request_h request, iotcon_representation_h repr, iotcon_response_result_e result);
+static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data);
+static int _request_handler_delete(iotcon_resource_h resource, iotcon_request_h request);
+static int _request_handler_post(door_resource_s *door, iotcon_request_h request);
+static int _request_handler_put(door_resource_s *door, iotcon_request_h request);
+
+static bool _found_resource(iotcon_remote_resource_h resource, iotcon_error_e result, void *user_data);
+static void _on_response(iotcon_remote_resource_h resource, iotcon_error_e err, iotcon_request_type_e request_type, iotcon_response_h response, void *user_data);
+static void disable_control_button(iotcon_view *this, bool get, bool post, bool put, bool del);
+static Evas_Object *add_control_layout(iotcon_view *this, Evas_Object *parent);
+static void _get_button_pressed_cb(void *data, Evas_Object *obj, void *event_info);
+static void _post_button_pressed_cb(void *data, Evas_Object *obj, void *event_info);
+static void _put_button_pressed_cb(void *data, Evas_Object *obj, void *event_info);
+static void _del_button_pressed_cb(void *data, Evas_Object *obj, void *event_info);
+static void _free_door_resource(door_resource_s *door);
+static void _on_observe(iotcon_remote_resource_h resource, iotcon_error_e err, int sequence_number, iotcon_response_h response, void *user_data);
+static void discovered_resource_list_show(iotcon_view *this);
+static void _resource_selected_cb(void *data, Evas_Object *obj, void *event_info);
+
+
+/**
+ * @function iotcon_view_add
+ * @since_tizen 3.0
+ * @description IoTcon View Add
+ * @parameter Evas_Object*: Evas Object Pointer, tbt_info*: Tbt Info Pointer, Elm_Object_Item*: Elm Object Item Pointer
+ * @return iotcon_view*
+ */
+iotcon_view *iotcon_view_add(Evas_Object *navi, tbt_info *tbt_info, Elm_Object_Item *item)
+{
+ RETVM_IF(NULL == navi, NULL, "navi is null");
+
+ iotcon_view *this = NULL;
+ this = calloc(1, sizeof(iotcon_view));
+ RETVM_IF(!this, NULL, "calloc failed");
+ this->view = calloc(1, sizeof(common_view));
+ RETVM_IF(!this->view, NULL, "calloc failed");
+
+ tbt_info->layout_group = "iotcon_viewer";
+ tbt_info->layout_file = get_edje_path("iotcon_viewer.edj");
+
+ common_view_add(navi, tbt_info, item, this->view, _app_destroy_cb, this);
+
+ RETVM_IF(NULL == this->view, NULL, "navi is null");
+
+ this->iotcon_label = ui_utils_label_add(this->view->layout, "");
+ ui_utils_label_set_text(this->iotcon_label, "Info: ", "left");
+ elm_object_part_content_set(this->view->layout, "iotcon_text", this->iotcon_label);
+
+ this->info_list = elm_list_add(this->view->layout);
+ RETVM_IF(!this->info_list, NULL, "elm_list_add failed");
+ evas_object_data_set(this->info_list, "view_data", this);
+ elm_object_part_content_set(this->view->layout, "iotcon_info_view", this->info_list);
+
+ elm_list_go(this->info_list);
+ evas_object_show(this->info_list);
+
+ char db_file_path[256]={'\0',};
+
+
+ if(this->view->tbt_info->apptype == TBT_APP_IOTCON_BASIC_SERVER)
+ {
+ this->iotcon_btn = ui_utils_push_button_add(this, this->view->layout, "Open/Close", _iotcon_server_change_state_btn_pressed_cb);
+ elm_object_part_content_set(this->view->layout, "iotcon_btn", this->iotcon_btn);
+
+ elm_object_disabled_set(this->iotcon_btn, true);
+
+ int ret;
+
+ snprintf(db_file_path, sizeof(db_file_path), "%s/%s",TBT_LOGGING_DIR,"iotcon-test-svr-db-server.dat");
+ DBG("db_file_path file set = %s", db_file_path);
+
+ ret = iotcon_initialize(db_file_path);
+ RETVM_IF(ret != IOTCON_ERROR_NONE, NULL, "iotcon_initialize error %s", get_iotcon_error(ret));
+
+ ret = iotcon_set_device_name("iotcon-test-basic-server");
+ RETVM_IF(ret != IOTCON_ERROR_NONE, NULL, "iotcon_set_device_name error %s", get_iotcon_error(ret));
+
+ ret = _set_door_resource(&this->my_door);
+ RETVM_IF(ret != IOTCON_ERROR_NONE, NULL, "_set_door_resource error %s", get_iotcon_error(ret));
+
+ ret = iotcon_resource_interfaces_add(this->my_door.ifaces, IOTCON_INTERFACE_BATCH);
+ RETVM_IF(ret != IOTCON_ERROR_NONE, NULL, "iotcon_resource_interfaces_add error %s", get_iotcon_error(ret));
+
+ /* add presence */
+ ret = iotcon_start_presence(10);
+ RETVM_IF(ret != IOTCON_ERROR_NONE, NULL, "iotcon_start_presence error %s", get_iotcon_error(ret));
+
+ this->my_door.handle = _create_door_resource(this->my_door.uri_path,this->my_door.type, this->my_door.ifaces, this->my_door.policies, this);
+ RETVM_IF(NULL == this->my_door.handle, NULL, "this->my_door.handle is NULL");
+ }
+ if(this->view->tbt_info->apptype == TBT_APP_IOTCON_BASIC_CLIENT)
+ {
+
+ Evas_Object *control = add_control_layout(this, this->view->layout);
+ elm_object_part_content_set(this->view->layout, "iotcon_btn", control);
+
+ snprintf(db_file_path, sizeof(db_file_path), "%s/%s",TBT_LOGGING_DIR,"iotcon-test-svr-db-client.dat");
+ DBG("db_file_path file set = %s", db_file_path);
+
+ int ret;
+ ret = iotcon_initialize(db_file_path);
+ RETVM_IF(ret != IOTCON_ERROR_NONE, NULL, "iotcon_initialize error %s", get_iotcon_error(ret));
+
+ ret = iotcon_query_create(&this->query);
+ RETVM_IF(ret != IOTCON_ERROR_NONE, NULL, "iotcon_query_create error %s", get_iotcon_error(ret));
+
+ ret = iotcon_query_set_resource_type(this->query, DOOR_RESOURCE_TYPE);
+ RETVM_IF(ret != IOTCON_ERROR_NONE, NULL, "iotcon_query_set_resource_type error %s", get_iotcon_error(ret));
+
+ ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, IOTCON_CONNECTIVITY_ALL, this->query, _found_resource, this);
+ RETVM_IF(ret != IOTCON_ERROR_NONE, NULL, "iotcon_find_resource error %s", get_iotcon_error(ret));
+
+ }
+
+ return this;
+}
+
+
+
+/**
+ * @function _app_destroy_cb
+ * @since_tizen 3.0
+ * @description App Destroy Cb
+ * @parameter void*: Void Pointer
+ * @return static void
+ */
+static void _app_destroy_cb(void* this)
+{
+ RETM_IF(NULL == this, "data is NULL");
+ iotcon_view *view = NULL;
+ view = (iotcon_view*)this;
+ RETM_IF(NULL == view, "view is NULL");
+
+ if(view->view->tbt_info->apptype == TBT_APP_IOTCON_BASIC_SERVER)
+ {
+ iotcon_stop_presence();
+ iotcon_resource_destroy(view->my_door.handle);
+ _free_door_resource(&view->my_door);
+ _resource_created = false;
+ }
+ if(view->view->tbt_info->apptype == TBT_APP_IOTCON_BASIC_CLIENT)
+ {
+ iotcon_query_destroy(view->query);
+ }
+ /* deinitialize iotcon */
+ iotcon_deinitialize();
+ SAFE_DELETE(view->view);
+ SAFE_DELETE(view);
+}
+
+
+static void _free_door_resource(door_resource_s *door)
+{
+ iotcon_observers_destroy(door->observers);
+ iotcon_resource_interfaces_destroy(door->ifaces);
+ free(door->type);
+ free(door->uri_path);
+}
+
+static void _get_button_pressed_cb(void *data, Evas_Object *obj, void *event_info)
+{
+ DBG("_get_button_pressed_cb");
+
+ RETM_IF(NULL == data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ iotcon_query_h query;
+
+ int result;
+
+ result = iotcon_remote_resource_observe_register(this->resource_clone, IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER, NULL, _on_observe, this);
+ RETM_IF(IOTCON_ERROR_NONE != result, "iotcon_remote_resource_observe_register result(%s)", get_iotcon_error(result));
+
+ result = iotcon_query_create(&query);
+ RETM_IF(IOTCON_ERROR_NONE != result, "iotcon_query_create result(%s)", get_iotcon_error(result));
+
+ result = iotcon_query_add(query, "query_key", "query_value");
+ RETM_IF(IOTCON_ERROR_NONE != result, "iotcon_query_add result(%s)", get_iotcon_error(result));
+
+ /* send GET Request */
+ result = iotcon_remote_resource_get(this->resource_clone, query, _on_response, this);
+ RETM_IF(IOTCON_ERROR_NONE != result, "iotcon_remote_resource_get result(%s)", get_iotcon_error(result));
+
+ iotcon_query_destroy(query);
+
+}
+static void _post_button_pressed_cb(void *data, Evas_Object *obj, void *event_info)
+{
+ DBG("_post_button_pressed_cb");
+
+ RETM_IF(NULL == data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ int ret;
+ iotcon_representation_h send_repr;
+
+ ret = iotcon_representation_create(&send_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_create() Fail(%d)", ret);
+ return;
+ }
+
+ /* send POST request */
+ ret = iotcon_remote_resource_post(this->resource_clone, send_repr, NULL, _on_response, this);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("iotcon_remote_resource_post() Fail(%d)", ret);
+
+ iotcon_representation_destroy(send_repr);
+}
+static void _put_button_pressed_cb(void *data, Evas_Object *obj, void *event_info)
+{
+ DBG("_put_button_pressed_cb");
+
+ RETM_IF(NULL == data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ iotcon_representation_h send_repr;
+ iotcon_attributes_h send_attributes;
+
+ int ret;
+
+ ret = iotcon_representation_create(&send_repr);
+ RETM_IF(IOTCON_ERROR_NONE != ret, "iotcon_representation_create result(%s)", get_iotcon_error(ret));
+
+ ret = iotcon_attributes_create(&send_attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_attributes_create() Fail(%d)", ret);
+ iotcon_representation_destroy(send_repr);
+ return;
+ }
+
+ ret = iotcon_attributes_add_bool(send_attributes, "opened", !this->opened);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_attributes_add_bool() Fail(%d)", ret);
+ iotcon_attributes_destroy(send_attributes);
+ iotcon_representation_destroy(send_repr);
+ return;
+ }
+
+ ret = iotcon_representation_set_attributes(send_repr, send_attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_set_attributes() Fail(%d)", ret);
+ iotcon_attributes_destroy(send_attributes);
+ iotcon_representation_destroy(send_repr);
+ return;
+ }
+
+ iotcon_attributes_destroy(send_attributes);
+
+ /* send PUT request */
+ ret = iotcon_remote_resource_put(this->resource_clone, send_repr, NULL, _on_response, this);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("iotcon_remote_resource_put() Fail(%d)", ret);
+
+ iotcon_representation_destroy(send_repr);
+}
+static void _del_button_pressed_cb(void *data, Evas_Object *obj, void *event_info)
+{
+ DBG("_del_button_pressed_cb");
+ RETM_IF(NULL == data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ iotcon_remote_resource_h new_door_resource;
+
+ int ret;
+
+ DBG("HOST: %s, URI: %s", this->host, this->created_uri_path);
+
+ ret = iotcon_remote_resource_create(this->host, this->connectivity_type, this->created_uri_path,
+ IOTCON_RESOURCE_SECURE, this->types, this->ifaces, &new_door_resource);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_create() Fail(%s)", get_iotcon_error(ret));
+ return;
+ }
+
+ ret = iotcon_remote_resource_delete(new_door_resource, _on_response, this);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_delete() Fail(%d)", ret);
+ iotcon_remote_resource_destroy(new_door_resource);
+ return;
+ }
+}
+
+static void disable_control_button(iotcon_view *this, bool get, bool put, bool post, bool del)
+{
+ RETM_IF(NULL == this, "view is NULL");
+
+ elm_object_disabled_set(this->get_btn, get);
+ elm_object_disabled_set(this->post_btn, post);
+ elm_object_disabled_set(this->put_btn, put);
+ elm_object_disabled_set(this->del_btn, del);
+}
+
+static Evas_Object *add_control_layout(iotcon_view *this, Evas_Object *parent)
+{
+ RETVM_IF(NULL == this, NULL, "view is NULL");
+
+ Evas_Object *table = elm_table_add(parent);
+
+ evas_object_size_hint_weight_set(table, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+ evas_object_size_hint_align_set(table, EVAS_HINT_FILL, EVAS_HINT_FILL);
+ evas_object_show(table);
+
+ this->get_btn = ui_utils_push_button_add(this, table, "GET", _get_button_pressed_cb);
+ elm_table_pack(table, this->get_btn, 0, 0, 1, 1);
+
+ this->put_btn = ui_utils_push_button_add(this, table, "PUT", _put_button_pressed_cb);
+ elm_table_pack(table, this->put_btn, 1, 0, 1, 1);
+
+ this->post_btn = ui_utils_push_button_add(this, table, "POST", _post_button_pressed_cb);
+ elm_table_pack(table, this->post_btn, 2, 0, 1, 1);
+
+ this->del_btn = ui_utils_push_button_add(this, table, "DEL", _del_button_pressed_cb);
+ elm_table_pack(table, this->del_btn, 3, 0, 1, 1);
+
+ disable_control_button(this, true, true, true, true);
+
+ return table;
+}
+
+
+///////////////////////////////////////////client///////////////////////////////////////////////////
+static void _on_observe(iotcon_remote_resource_h resource, iotcon_error_e err,
+ int sequence_number, iotcon_response_h response, void *user_data)
+{
+ RETM_IF(NULL == user_data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)user_data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ int ret;
+ bool opened;
+ static int i = 0;
+ iotcon_attributes_h attributes;
+ iotcon_representation_h repr;
+ iotcon_response_result_e response_result;
+
+ RETM_IF(IOTCON_ERROR_NONE != err, "_on_observe error(%d)", err);
+
+ ret = iotcon_response_get_result(response, &response_result);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_get_result() Fail(%d)", ret);
+ return;
+ }
+
+ if (IOTCON_RESPONSE_OK != response_result) {
+ ERR("_on_response_observe Response error(%d)", response_result);
+ return;
+ }
+
+ ret = iotcon_response_get_representation(response, &repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_get_representation() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_representation_get_attributes(repr, &attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_get_attributes() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_attributes_get_bool(attributes, "opened", &opened);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_attributes_get_bool() Fail(%d)", ret);
+ return;
+ }
+
+ DBG("notify_cb information");
+ switch ((int)opened) {
+ case true:
+ DBG("[Door] opened.");
+ elm_list_item_append(this->info_list,"[Door] opened.", NULL, NULL, NULL, NULL);
+ break;
+ case false:
+ DBG("[Door] closed.");
+ elm_list_item_append(this->info_list,"[Door] closed.", NULL, NULL, NULL, NULL);
+ break;
+ default:
+ break;
+ }
+
+// if (5 == i++) {
+// iotcon_remote_resource_observe_deregister(resource);
+// iotcon_remote_resource_destroy(resource);
+// }
+
+ elm_list_go(this->info_list);
+}
+
+static void _on_response_delete(iotcon_remote_resource_h resource,
+ iotcon_response_h response, void *user_data)
+{
+ RETM_IF(NULL == user_data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)user_data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ int ret;
+ iotcon_response_result_e response_result;
+
+ ret = iotcon_response_get_result(response, &response_result);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_get_result() Fail(%d)", ret);
+ return;
+ }
+
+ if (IOTCON_RESPONSE_OK != response_result
+ && IOTCON_RESPONSE_RESOURCE_DELETED != response_result) {
+ ERR("_on_response_delete Response error(%d)", response_result);
+ return;
+ }
+ iotcon_remote_resource_observe_deregister(resource);
+ DBG("DELETE request was successful");
+ disable_control_button(this, true, true, true, true);
+
+ /* delete callback operations */
+
+ iotcon_remote_resource_destroy(resource);
+}
+
+static void _on_response_post(iotcon_remote_resource_h resource,
+ iotcon_response_h response, void *user_data)
+{
+
+ RETM_IF(NULL == user_data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)user_data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ int ret;
+ iotcon_attributes_h recv_attributes;
+ char *created_uri_path;
+ iotcon_response_result_e response_result;
+ iotcon_representation_h recv_repr = NULL;
+
+ this->types = NULL;
+ this->ifaces = NULL;
+
+ ret = iotcon_response_get_result(response, &response_result);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_get_result() Fail(%d)", ret);
+ return;
+ }
+
+ if (IOTCON_RESPONSE_RESOURCE_CREATED != response_result) {
+ ERR("_on_response_post Response error(%d)", response_result);
+ return;
+ }
+ DBG("POST request was successful");
+
+ ret = iotcon_response_get_representation(response, &recv_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_get_representation() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_representation_get_attributes(recv_repr, &recv_attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_get_attributes() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_attributes_get_str(recv_attributes, "createduripath", &created_uri_path);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_attributes_get_str() Fail(%d)", ret);
+ return;
+ }
+ this->created_uri_path = strdup(created_uri_path);
+ DBG("New resource created : %s", this->created_uri_path);
+
+ ret = iotcon_remote_resource_get_host_address(resource, &this->host);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_remote_resource_get_connectivity_type(resource, &this->connectivity_type);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_get_connectivity_type() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_remote_resource_get_types(resource, &this->types);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_remote_resource_get_interfaces(resource, &this->ifaces);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
+ return;
+ }
+
+ DBG("POST request was successful");
+ disable_control_button(this, true, true, true, false);
+}
+
+static void _on_response_put(iotcon_remote_resource_h resource,
+ iotcon_response_h response, void *user_data)
+{
+ RETM_IF(NULL == user_data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)user_data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ int ret;
+ iotcon_response_result_e response_result;
+
+ ret = iotcon_response_get_result(response, &response_result);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_get_result() Fail(%d)", ret);
+ return;
+ }
+
+ if (IOTCON_RESPONSE_RESOURCE_CHANGED != response_result) {
+ ERR("_on_response_put Response error(%d)", response_result);
+ return;
+ }
+ DBG("PUT request was successful");
+
+ disable_control_button(this, true, true, false, true);
+
+}
+
+static void _on_response_get(iotcon_remote_resource_h resource,
+ iotcon_response_h response, void *user_data)
+{
+
+ RETM_IF(NULL == user_data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)user_data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ int ret;
+ iotcon_response_result_e response_result;
+ iotcon_representation_h recv_repr;
+ iotcon_attributes_h recv_attributes = NULL;
+
+ this->opened = true;
+
+ ret = iotcon_response_get_result(response, &response_result);
+ RETM_IF(IOTCON_ERROR_NONE != ret, "iotcon_response_get_result result(%s)", get_iotcon_error(ret));
+ RETM_IF(IOTCON_RESPONSE_OK != response_result, "_on_response_get Response error(%d)", response_result);
+
+ /* get the resource host address */
+ char *resource_host = NULL;
+ iotcon_remote_resource_get_host_address(resource, &resource_host);
+ DBG("resource host : %s", resource_host);
+
+ ret = iotcon_response_get_representation(response, &recv_repr);
+ RETM_IF(IOTCON_ERROR_NONE != ret, "iotcon_response_get_representation result(%s)", get_iotcon_error(ret));
+
+
+ ret = iotcon_representation_get_attributes(recv_repr, &recv_attributes);
+ RETM_IF(IOTCON_ERROR_NONE != ret, "iotcon_representation_get_attributes result(%s)", get_iotcon_error(ret));
+
+ ret = iotcon_attributes_get_bool(recv_attributes, "opened", &this->opened);
+ RETM_IF(IOTCON_ERROR_NONE != ret, "iotcon_attributes_get_bool result(%s)", get_iotcon_error(ret));
+
+ DBG("GET request was successuful");
+ disable_control_button(this, true, false, true, true);
+}
+
+static bool _get_res_iface_cb(const char *string, void *user_data)
+{
+ char *resource_uri_path = user_data;
+
+ DBG("[%s] resource interface : %s", resource_uri_path, string);
+
+ return IOTCON_FUNC_CONTINUE;
+}
+
+static bool _get_res_type_cb(const char *string, void *user_data)
+{
+ char *resource_uri_path = user_data;
+
+ DBG("[%s] resource type : %s", resource_uri_path, string);
+
+ return IOTCON_FUNC_CONTINUE;
+}
+
+static void _presence_handler(iotcon_presence_h presence, iotcon_error_e err,
+ iotcon_presence_response_h response, void *user_data)
+{
+ char *host_address;
+ char *resource_type;
+ int ret;
+ iotcon_presence_result_e result;
+ iotcon_presence_trigger_e trigger;
+ iotcon_connectivity_type_e connectivity_type;
+
+ RETM_IF(IOTCON_ERROR_NONE != err, "_presence_handler error(%d)", err);
+
+ ret = iotcon_presence_response_get_result(response, &result);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_presence_response_get_result() Fail(%d)", ret);
+ return;
+ }
+
+ if (IOTCON_PRESENCE_OK == result) {
+ ret = iotcon_presence_response_get_trigger(response, &trigger);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_presence_response_get_trigger() Fail(%d)", ret);
+ return;
+ }
+ }
+
+ ret = iotcon_presence_response_get_host_address(response, &host_address);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_presence_response_get_host_address() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_presence_response_get_connectivity_type(response, &connectivity_type);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_presence_response_get_connectivity_type() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_presence_response_get_resource_type(response, &resource_type);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_presence_response_get_resource_type() Fail(%d)", ret);
+ return;
+ }
+
+ DBG("_presence_handler");
+ DBG("result : %d", result);
+ if (IOTCON_PRESENCE_OK == result)
+ DBG("trigger : %d", trigger);
+ DBG("host_address : %s", host_address);
+ DBG("resource_type : %s", resource_type);
+}
+
+static int _device_id_compare(const void *a, const void *b)
+{
+ return strcmp(a, b);
+}
+
+static void _on_response(iotcon_remote_resource_h resource, iotcon_error_e err,
+ iotcon_request_type_e request_type, iotcon_response_h response, void *user_data)
+{
+
+ RETM_IF(NULL == user_data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)user_data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ RETM_IF(IOTCON_ERROR_NONE != err, "_on_response error(%d)", err);
+ DBG("request(%d) was successful", request_type);
+
+ switch (request_type) {
+ case IOTCON_REQUEST_GET:
+ _on_response_get(resource, response, this);
+ break;
+ case IOTCON_REQUEST_PUT:
+ _on_response_put(resource, response, this);
+ break;
+ case IOTCON_REQUEST_POST:
+ _on_response_post(resource, response, this);
+ break;
+ case IOTCON_REQUEST_DELETE:
+ _on_response_delete(resource, response, this);
+ break;
+ default:
+ ERR("Invalid request type (%d)", request_type);
+ return;
+ }
+}
+
+static bool _found_resource(iotcon_remote_resource_h resource, iotcon_error_e result, void *user_data)
+{
+
+ RETVM_IF(NULL == user_data, false, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)user_data;
+ RETVM_IF(NULL == this, false, "view is NULL");
+
+ int ret;
+ GList *node;
+ char *resource_host;
+ char *resource_uri_path;
+ char *resource_device_id;
+ char *resource_device_name;
+ iotcon_presence_h presence_handle;
+ iotcon_resource_types_h resource_types;
+ iotcon_resource_interfaces_h resource_interfaces;
+ iotcon_connectivity_type_e connectivity_type;
+ iotcon_remote_resource_h resource_clone = NULL;
+
+ RETVM_IF(IOTCON_ERROR_NONE != result, IOTCON_FUNC_STOP, "Invalid result(%s)", get_iotcon_error(result));
+ RETVM_IF(NULL == resource, IOTCON_FUNC_CONTINUE, "NULL == resource");
+
+ DBG("===== resource found =====");
+
+ /* get the resource URI */
+ ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
+ RETVM_IF(IOTCON_ERROR_NONE != ret, IOTCON_FUNC_CONTINUE, "iotcon_remote_resource_get_uri_path result(%s)", get_iotcon_error(ret));
+
+ /* get the device unique id.
+ * this is unique per-server independent on how it was discovered. */
+ ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
+ RETVM_IF(IOTCON_ERROR_NONE != ret, IOTCON_FUNC_CONTINUE, "iotcon_remote_resource_get_device_id result(%s)", get_iotcon_error(ret));
+
+ DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
+
+ ret = iotcon_remote_resource_get_device_name(resource, &resource_device_name);
+ RETVM_IF(IOTCON_ERROR_NONE != ret, IOTCON_FUNC_CONTINUE, "iotcon_remote_resource_get_device_name result(%s)", get_iotcon_error(ret));
+
+ DBG("[%s] resource device name : %s", resource_uri_path, resource_device_name);
+
+ node = g_list_find_custom(this->device_id_list, resource_device_id, _device_id_compare);
+ RETVM_IF(node && !strncmp(DOOR_RESOURCE_URI_PREFIX, resource_uri_path, strlen(DOOR_RESOURCE_URI_PREFIX)), IOTCON_FUNC_CONTINUE, "uri_path \"%s\" already found. skip !", resource_uri_path);
+
+ door_resource_device_id = strdup(resource_device_id);
+ RETVM_IF(NULL == door_resource_device_id, IOTCON_FUNC_CONTINUE, "strdup(door_resource_device_id) Fail");
+
+ ret = iotcon_remote_resource_clone(resource, &resource_clone);
+ RETVM_IF(IOTCON_ERROR_NONE != ret, IOTCON_FUNC_CONTINUE, "iotcon_remote_resource_clone result(%s)", get_iotcon_error(ret));
+ this->device_id_list = g_list_append(this->device_id_list, resource_clone);
+
+ /* get the resource host address */
+ ret = iotcon_remote_resource_get_host_address(resource, &resource_host);
+ RETVM_IF(IOTCON_ERROR_NONE != ret, IOTCON_FUNC_CONTINUE, "iotcon_remote_resource_get_host_address result(%s)", get_iotcon_error(ret));
+
+ DBG("[%s] resource host : %s", resource_uri_path, resource_host);
+
+ ret = iotcon_remote_resource_get_connectivity_type(resource, &connectivity_type);
+ RETVM_IF(IOTCON_ERROR_NONE != ret, IOTCON_FUNC_CONTINUE, "iotcon_remote_resource_get_connectivity_type result(%s)", get_iotcon_error(ret));
+
+ /* get the resource interfaces */
+ ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
+ RETVM_IF(IOTCON_ERROR_NONE != ret, IOTCON_FUNC_CONTINUE, "iotcon_remote_resource_get_interfaces result(%s)", get_iotcon_error(ret));
+
+ ret = iotcon_resource_interfaces_foreach(resource_interfaces, _get_res_iface_cb, resource_uri_path);
+ RETVM_IF(IOTCON_ERROR_NONE != ret, IOTCON_FUNC_CONTINUE, "iotcon_resource_interfaces_foreach result(%s)", get_iotcon_error(ret));
+
+ /* get the resource types */
+ ret = iotcon_remote_resource_get_types(resource, &resource_types);
+ RETVM_IF(IOTCON_ERROR_NONE != ret, IOTCON_FUNC_CONTINUE, "iotcon_remote_resource_get_types result(%s)", get_iotcon_error(ret));
+
+ ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb, resource_uri_path);
+ RETVM_IF(IOTCON_ERROR_NONE != ret, IOTCON_FUNC_CONTINUE, "iotcon_resource_types_foreach result(%s)", get_iotcon_error(ret));
+
+ ret = iotcon_add_presence_cb(resource_host, connectivity_type, DOOR_RESOURCE_TYPE, _presence_handler, NULL, &presence_handle);
+ RETVM_IF(IOTCON_ERROR_NONE != ret, IOTCON_FUNC_CONTINUE, "iotcon_add_presence_cb result(%s)", get_iotcon_error(ret));
+
+ discovered_resource_list_show(this);
+
+ return IOTCON_FUNC_CONTINUE;
+}
+
+
+static void discovered_resource_list_show(iotcon_view *this)
+{
+ DBG(" discovered_resource_list_show ");
+ RETM_IF(NULL == this, "view is NULL");
+
+ elm_list_clear(this->info_list);
+
+ GList *l;
+ iotcon_remote_resource_h resource;
+
+ for(l = this->device_id_list; l != NULL; l = l->next)
+ {
+
+ char* resource_uri_path = NULL;
+
+ resource = (iotcon_remote_resource_h)l->data;
+
+ int result;
+
+ result = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
+ RETM_IF(IOTCON_ERROR_NONE != result, "iotcon_remote_resource_get_uri_path result(%s)", get_iotcon_error(result));
+
+
+ if (!strncmp(DOOR_RESOURCE_URI_PREFIX, resource_uri_path,strlen(DOOR_RESOURCE_URI_PREFIX)))
+ {
+ char* resource_device_id;
+ result = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
+ RETM_IF(IOTCON_ERROR_NONE != result, "iotcon_remote_resource_get_device_id result(%s)", get_iotcon_error(result));
+
+ elm_list_item_append(this->info_list,resource_device_id, NULL, NULL, _resource_selected_cb, resource);
+ }
+
+ }
+
+ elm_list_go(this->info_list);
+ evas_object_show(this->info_list);
+}
+
+static void _resource_selected_cb(void *data, Evas_Object *obj, void *event_info)
+{
+ DBG("_resource_selected_cb");
+
+ iotcon_remote_resource_h resource = (iotcon_remote_resource_h)data;
+ iotcon_view *this = evas_object_data_get(obj, "view_data");
+
+ if(!resource)
+ {
+ DBG("resource NULL");
+ }
+
+ disable_control_button(this, false, true, true, true);
+
+ int result;
+ this->resource_clone = NULL;
+ result = iotcon_remote_resource_clone(resource, &this->resource_clone);
+ RETM_IF(IOTCON_ERROR_NONE != result, "iotcon_remote_resource_clone result(%s)", get_iotcon_error(result));
+
+ char* resource_device_id;
+ int ret = iotcon_remote_resource_get_device_id(this->resource_clone, &resource_device_id);
+ RETM_IF(IOTCON_ERROR_NONE != ret, "iotcon_remote_resource_get_device_id result(%s)", get_iotcon_error(ret));
+
+ DBG("resource_device_id: %s", resource_device_id);
+
+}
+/////////////////////////////////////////////////////////////////////////////////////////////////////
+
+
+
+static iotcon_resource_h _create_door_resource(char *uri_path, char *type,
+ iotcon_resource_interfaces_h ifaces, uint8_t policies, void *user_data)
+{
+
+ RETVM_IF(NULL == user_data, false, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)user_data;
+ RETVM_IF(NULL == this, false, "view is NULL");
+
+
+ int ret;
+ iotcon_resource_h handle;
+ iotcon_resource_types_h resource_types;
+
+ ret = iotcon_resource_types_create(&resource_types);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_resource_types_create() Fail(%d)", ret);
+ return NULL;
+ }
+
+ ret = iotcon_resource_types_add(resource_types, type);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_resource_types_add() Fail(%d)", ret);
+ iotcon_resource_types_destroy(resource_types);
+ return NULL;
+ }
+
+ /* register door resource */
+ ret = iotcon_resource_create(uri_path, resource_types, ifaces, policies,
+ _request_handler, this, &handle);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_resource_create() Fail");
+ iotcon_resource_types_destroy(resource_types);
+ return NULL;
+ }
+
+ iotcon_resource_types_destroy(resource_types);
+
+ return handle;
+}
+
+static void _iotcon_server_change_state_btn_pressed_cb(void *user_data, Evas_Object *obj, void *event_info)
+{
+ DBG("_iotcon_server_change_state_btn_pressed_cb");
+
+ RETM_IF(NULL == user_data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)user_data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ int ret;
+ iotcon_representation_h repr;
+ door_resource_s *door = &this->my_door;
+
+ if (NULL == door->observers)
+ return;
+
+ if (false == door->attributes) {
+ door->attributes = true;
+ DBG("[Door] closed -> opened");
+ } else {
+ door->attributes = false;
+ DBG("[Door] opened -> closed");
+ }
+
+ DBG("NOTIFY!");
+
+ repr = _get_door_representation(door);
+ if (NULL == repr) {
+ ERR("_get_door_representation() Fail");
+ return;
+ }
+
+ ret = iotcon_resource_notify(door->handle, repr, door->observers, IOTCON_QOS_HIGH);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_resource_notify() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return;
+ }
+
+ iotcon_representation_destroy(repr);
+ return;
+}
+
+static iotcon_representation_h _get_door_representation(door_resource_s *door)
+{
+ int ret;
+ iotcon_attributes_h attributes;
+ iotcon_representation_h repr;
+
+ /* create a door Representation */
+ ret = iotcon_representation_create(&repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_create() Fail(%d)", ret);
+ return NULL;
+ }
+
+ /* create a door attributes */
+ ret = iotcon_attributes_create(&attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_attributes_create() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_representation_set_uri_path(repr, door->uri_path);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
+ iotcon_attributes_destroy(attributes);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_attributes_add_bool(attributes, "opened", door->attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_attributes_add_bool() Fail(%d)", ret);
+ iotcon_attributes_destroy(attributes);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_representation_set_attributes(repr, attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_set_attributes() Fail(%d)", ret);
+ iotcon_attributes_destroy(attributes);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ iotcon_attributes_destroy(attributes);
+
+ return repr;
+}
+
+static void _check_door_attributes(door_resource_s door)
+{
+ if (false == door.attributes)
+ DBG("[Door] closed.");
+ else
+ DBG("[Door] opened.");
+}
+
+static int _set_door_representation(door_resource_s *door,
+ iotcon_representation_h repr)
+{
+ int ret;
+ bool bval;
+ iotcon_attributes_h attributes;
+
+ ret = iotcon_representation_get_attributes(repr, &attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_get_attributes() Fail(%d)", ret);
+ return -1;
+ }
+
+ ret = iotcon_attributes_get_bool(attributes, "opened", &bval);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_attributes_get_bool() Fail(%d)", ret);
+ return -1;
+ }
+
+ door->attributes = bval;
+
+ return 0;
+}
+
+static bool _query_cb(const char *key, const char *value, void *user_data)
+{
+ DBG("key : %s, value : %s", key, value);
+
+ return IOTCON_FUNC_CONTINUE;
+}
+
+static void _request_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
+{
+ DBG("_request_handler");
+ RETM_IF(NULL == user_data, "data is NULL");
+ iotcon_view *this = NULL;
+ this = (iotcon_view*)user_data;
+ RETM_IF(NULL == this, "view is NULL");
+
+ door_resource_s *door;
+ iotcon_query_h query;
+ int ret, observe_id;
+ iotcon_request_type_e type;
+ iotcon_observe_type_e observe_type;
+ char *host_address;
+
+ char* str;
+
+ RETM_IF(NULL == request, "request is NULL");
+
+ ret = iotcon_request_get_host_address(request, &host_address);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_host_address() Fail(%d)", ret);
+ _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
+ return;
+ }
+ DBG("host_address : %s", host_address);
+
+ ret = iotcon_request_get_query(request, &query);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_query() Fail(%d)", ret);
+ _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
+ return;
+ }
+ if (query)
+ iotcon_query_foreach(query, _query_cb, NULL);
+
+ ret = iotcon_request_get_request_type(request, &type);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_request_type() Fail(%d)", ret);
+ _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
+ return;
+ }
+
+ door = &this->my_door;
+
+ if (IOTCON_REQUEST_GET == type)
+ {
+ ret = _request_handler_get(door, request);
+ str = format_string("%s", "GET");
+ elm_list_item_append(this->info_list, str, NULL, NULL, NULL, NULL);
+ SAFE_DELETE(str);
+ }
+
+ else if (IOTCON_REQUEST_PUT == type)
+ {
+ ret = _request_handler_put(door, request);
+ str = format_string("%s", "PUT");
+ elm_list_item_append(this->info_list, str, NULL, NULL, NULL, NULL);
+ SAFE_DELETE(str);
+ }
+
+ else if (IOTCON_REQUEST_POST == type)
+ {
+ ret = _request_handler_post(door, request);
+ str = format_string("%s", "POST");
+ elm_list_item_append(this->info_list, str, NULL, NULL, NULL, NULL);
+ SAFE_DELETE(str);
+ }
+
+ else if (IOTCON_REQUEST_DELETE == type)
+ {
+ ret = _request_handler_delete(resource, request);
+ str = format_string("%s", "DELETE");
+ elm_list_item_append(this->info_list, str, NULL, NULL, NULL, NULL);
+ SAFE_DELETE(str);
+ elm_object_disabled_set(this->iotcon_btn, false);
+ DBG("Delete Request get");
+ }
+
+ elm_list_go(this->info_list);
+// evas_object_show(this->info_list);
+
+ if (0 != ret) {
+ _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
+ return;
+ }
+
+ ret = iotcon_request_get_observe_type(request, &observe_type);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_observe_type() Fail(%d)", ret);
+ return;
+ }
+
+ if (IOTCON_OBSERVE_REGISTER == observe_type) {
+ ret = iotcon_request_get_observe_id(request, &observe_id);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_observe_id() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_observers_add(door->observers, observe_id);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_observers_add() Fail(%d)", ret);
+ return;
+ }
+ } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
+ ret = iotcon_request_get_observe_id(request, &observe_id);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_observe_id() Fail(%d)", ret);
+ return;
+ }
+ ret = iotcon_observers_remove(door->observers, observe_id);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_observers_remove() Fail(%d)", ret);
+ return;
+ }
+ }
+
+}
+
+static int _request_handler_delete(iotcon_resource_h resource,
+ iotcon_request_h request)
+{
+ int ret;
+ DBG("DELETE request");
+
+ ret = iotcon_resource_destroy(resource);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_resource_destroy() Fail(%d)", ret);
+ return -1;
+ }
+
+ ret = _send_response(request, NULL, IOTCON_RESPONSE_RESOURCE_DELETED);
+ if (0 != ret) {
+ ERR("_send_response() Fail(%d)", ret);
+ return -1;
+ }
+
+ return 0;
+}
+
+
+static int _request_handler_post(door_resource_s *door, iotcon_request_h request)
+{
+ int ret;
+ iotcon_attributes_h resp_attributes;
+ iotcon_representation_h resp_repr = NULL;
+ iotcon_resource_h new_door_handle;
+ DBG("POST request");
+
+ if (_resource_created) {
+ ERR("Resource(%s) is already created", DOOR_RESOURCE_URI2);
+ return -1;
+ }
+
+ new_door_handle = _create_door_resource(DOOR_RESOURCE_URI2, door->type,
+ door->ifaces, IOTCON_RESOURCE_SECURE, door);
+ if (NULL == new_door_handle) {
+ ERR("_create_door_resource() Fail");
+ return -1;
+ }
+ _resource_created = true;
+
+ /* send information that new resource was created */
+ ret = iotcon_representation_create(&resp_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_create() Fail(%d)", ret);
+ return -1;
+ }
+
+ ret = iotcon_attributes_create(&resp_attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_attributes_create() Fail(%d)", ret);
+ iotcon_representation_destroy(resp_repr);
+ return -1;
+ }
+
+ ret = iotcon_attributes_add_str(resp_attributes, "createduripath", DOOR_RESOURCE_URI2);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_attributes_add_str() Fail(%d)", ret);
+ iotcon_attributes_destroy(resp_attributes);
+ iotcon_representation_destroy(resp_repr);
+ return -1;
+ }
+
+ ret = iotcon_representation_set_attributes(resp_repr, resp_attributes);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_set_attributes() Fail(%d)", ret);
+ iotcon_attributes_destroy(resp_attributes);
+ iotcon_representation_destroy(resp_repr);
+ return -1;
+ }
+
+ iotcon_attributes_destroy(resp_attributes);
+
+ ret = _send_response(request, resp_repr, IOTCON_RESPONSE_RESOURCE_CREATED);
+ if (0 != ret) {
+ ERR("_send_response() Fail(%d)", ret);
+ iotcon_representation_destroy(resp_repr);
+ return -1;
+ }
+
+ iotcon_representation_destroy(resp_repr);
+
+ return 0;
+}
+
+static int _request_handler_put(door_resource_s *door, iotcon_request_h request)
+{
+ int ret;
+ iotcon_representation_h req_repr, resp_repr;
+ DBG("PUT request");
+
+ ret = iotcon_request_get_representation(request, &req_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_representation() Fail(%d)", ret);
+ return -1;
+ }
+
+ ret = _set_door_representation(door, req_repr);
+ if (0 != ret) {
+ ERR("_set_door_representation() Fail(%d)", ret);
+ return -1;
+ }
+
+ _check_door_attributes(*door);
+
+ resp_repr = _get_door_representation(door);
+ if (NULL == resp_repr) {
+ ERR("_get_door_representation() Fail");
+ return -1;
+ }
+
+ ret = _send_response(request, resp_repr, IOTCON_RESPONSE_OK);
+ if (0 != ret) {
+ ERR("_send_response() Fail(%d)", ret);
+ iotcon_representation_destroy(resp_repr);
+ return -1;
+ }
+
+ /* notify */
+ ret = iotcon_resource_notify(door->handle, resp_repr, door->observers, IOTCON_QOS_HIGH);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("iotcon_resource_notify() Fail(%d)", ret);
+
+ iotcon_representation_destroy(resp_repr);
+
+ return 0;
+}
+
+
+static int _request_handler_get(door_resource_s *door, iotcon_request_h request)
+{
+ int ret;
+ iotcon_representation_h resp_repr;
+ DBG("GET request");
+
+ resp_repr = _get_door_representation(door);
+ if (NULL == resp_repr) {
+ ERR("_get_door_representation() Fail");
+ return -1;
+ }
+
+ ret = _send_response(request, resp_repr, IOTCON_RESPONSE_OK);
+ if (0 != ret) {
+ ERR("_send_response() Fail(%d)", ret);
+ iotcon_representation_destroy(resp_repr);
+ return -1;
+ }
+
+ iotcon_representation_destroy(resp_repr);
+
+ return 0;
+}
+
+static int _send_response(iotcon_request_h request, iotcon_representation_h repr,
+ iotcon_response_result_e result)
+{
+ int ret;
+ iotcon_response_h response;
+
+ ret = iotcon_response_create(request, &response);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_create() Fail(%d)", ret);
+ return -1;
+ }
+
+ ret = iotcon_response_set_result(response, result);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_set_result() Fail(%d)", ret);
+ iotcon_response_destroy(response);
+ return -1;
+ }
+
+ ret = iotcon_response_set_representation(response, repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_set_representation() Fail(%d)", ret);
+ iotcon_response_destroy(response);
+ return -1;
+ }
+
+ /* send Representation to the client */
+ ret = iotcon_response_send(response);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_send() Fail(%d)", ret);
+ iotcon_response_destroy(response);
+ return -1;
+ }
+
+ iotcon_response_destroy(response);
+
+ return 0;
+}
+
+static int _set_door_resource(door_resource_s *door)
+{
+ int ret;
+
+ door->attributes = false;
+
+ door->uri_path = strdup(DOOR_RESOURCE_URI);
+ if (NULL == door->uri_path) {
+ ERR("strdup(%s) Fail", DOOR_RESOURCE_URI);
+ return -1;
+ }
+
+ door->type = strdup(DOOR_RESOURCE_TYPE);
+ if (NULL == door->type) {
+ ERR("strdup(%s) Fail", DOOR_RESOURCE_TYPE);
+ SAFE_DELETE(door->uri_path);
+ return -1;
+ }
+
+ ret = iotcon_resource_interfaces_create(&door->ifaces);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_resource_interfaces_create() Fail(%d)", ret);
+ SAFE_DELETE(door->type);
+ SAFE_DELETE(door->uri_path);
+ return ret;
+ }
+
+ ret = iotcon_resource_interfaces_add(door->ifaces, IOTCON_INTERFACE_DEFAULT);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_resource_interfaces_add() Fail(%d)", ret);
+ iotcon_resource_interfaces_destroy(door->ifaces);
+ SAFE_DELETE(door->type);
+ SAFE_DELETE(door->uri_path);
+ return ret;
+ }
+
+ door->policies = IOTCON_RESOURCE_DISCOVERABLE | IOTCON_RESOURCE_OBSERVABLE
+ | IOTCON_RESOURCE_SECURE;
+
+ ret = iotcon_observers_create(&door->observers);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_observers_create() Fail");
+ iotcon_resource_interfaces_destroy(door->ifaces);
+ SAFE_DELETE(door->type);
+ SAFE_DELETE(door->uri_path);
+ return ret;
+ }
+
+ return IOTCON_ERROR_NONE;
+}
+
+
+static char* get_iotcon_error(iotcon_error_e err)
+{
+ char* err_msg;
+
+ switch (err) {
+ case IOTCON_ERROR_NONE:
+ err_msg = "IOTCON_ERROR_NONE";
+ break;
+ case IOTCON_ERROR_IO_ERROR:
+ err_msg = "IOTCON_ERROR_IO_ERROR";
+ break;
+ case IOTCON_ERROR_OUT_OF_MEMORY:
+ err_msg = "IOTCON_ERROR_OUT_OF_MEMORY";
+ break;
+ case IOTCON_ERROR_PERMISSION_DENIED:
+ err_msg = "IOTCON_ERROR_PERMISSION_DENIED";
+ break;
+ case IOTCON_ERROR_NOT_SUPPORTED:
+ err_msg = "IOTCON_ERROR_NOT_SUPPORTED";
+ break;
+ case IOTCON_ERROR_INVALID_PARAMETER:
+ err_msg = "IOTCON_ERROR_INVALID_PARAMETER";
+ break;
+ case IOTCON_ERROR_NO_DATA:
+ err_msg = "IOTCON_ERROR_NO_DATA";
+ break;
+ case IOTCON_ERROR_TIMEOUT:
+ err_msg = "IOTCON_ERROR_TIMEOUT";
+ break;
+ case IOTCON_ERROR_IOTIVITY:
+ err_msg = "IOTCON_ERROR_IOTIVITY";
+ break;
+ case IOTCON_ERROR_REPRESENTATION:
+ err_msg = "IOTCON_ERROR_REPRESENTATION";
+ break;
+ case IOTCON_ERROR_INVALID_TYPE:
+ err_msg = "IOTCON_ERROR_INVALID_TYPE";
+ break;
+ case IOTCON_ERROR_ALREADY:
+ err_msg = "IOTCON_ERROR_ALREADY";
+ break;
+ case IOTCON_ERROR_SYSTEM:
+ err_msg = "IOTCON_ERROR_SYSTEM";
+ break;
+ default:
+ err_msg = "UNKNOWN_ERROR";
+ break;
+ }
+ return err_msg;
+}
+
+#endif