%{_bindir}/iotcon-test-basic-client
%{_bindir}/iotcon-test-basic-server
%{_bindir}/iotcon-test-device-client
-%{_bindir}/iotcon-test-repr-client
-%{_bindir}/iotcon-test-repr-server
+%{_bindir}/iotcon-test-iface-client
+%{_bindir}/iotcon-test-iface-server
%{_bindir}/iotcon-test-encap-client
%{_bindir}/iotcon-test-encap-server
SET(IOTCON_TEST_DEVICE_CLIENT "iotcon-test-device-client")
SET(IOTCON_TEST_DEVICE_CLIENT_SRCS "iotcon-test-device-client.c")
-SET(IOTCON_TEST_REPR_CLIENT "iotcon-test-repr-client")
-SET(IOTCON_TEST_REPR_SERVER "iotcon-test-repr-server")
-SET(IOTCON_TEST_REPR_CLIENT_SRCS "iotcon-test-repr-client.c")
-SET(IOTCON_TEST_REPR_SERVER_SRCS "iotcon-test-repr-server.c")
+SET(IOTCON_TEST_IFACE_CLIENT "iotcon-test-iface-client")
+SET(IOTCON_TEST_IFACE_SERVER "iotcon-test-iface-server")
+SET(IOTCON_TEST_IFACE_CLIENT_SRCS "iotcon-test-iface-client.c")
+SET(IOTCON_TEST_IFACE_SERVER_SRCS "iotcon-test-iface-server.c")
SET(IOTCON_TEST_ENCAP_CLIENT "iotcon-test-encap-client")
SET(IOTCON_TEST_ENCAP_SERVER "iotcon-test-encap-server")
TARGET_LINK_LIBRARIES(${IOTCON_TEST_DEVICE_CLIENT} ${test_pkgs_LIBRARIES} ${CLIENT})
INSTALL(TARGETS ${IOTCON_TEST_DEVICE_CLIENT} DESTINATION ${BIN_INSTALL_DIR})
-ADD_EXECUTABLE(${IOTCON_TEST_REPR_CLIENT} ${IOTCON_TEST_REPR_CLIENT_SRCS})
-TARGET_LINK_LIBRARIES(${IOTCON_TEST_REPR_CLIENT} ${test_pkgs_LIBRARIES} ${CLIENT})
-INSTALL(TARGETS ${IOTCON_TEST_REPR_CLIENT} DESTINATION ${BIN_INSTALL_DIR})
+ADD_EXECUTABLE(${IOTCON_TEST_IFACE_CLIENT} ${IOTCON_TEST_IFACE_CLIENT_SRCS})
+TARGET_LINK_LIBRARIES(${IOTCON_TEST_IFACE_CLIENT} ${test_pkgs_LIBRARIES} ${CLIENT})
+INSTALL(TARGETS ${IOTCON_TEST_IFACE_CLIENT} DESTINATION ${BIN_INSTALL_DIR})
-ADD_EXECUTABLE(${IOTCON_TEST_REPR_SERVER} ${IOTCON_TEST_REPR_SERVER_SRCS})
-TARGET_LINK_LIBRARIES(${IOTCON_TEST_REPR_SERVER} ${test_pkgs_LIBRARIES} ${CLIENT})
-INSTALL(TARGETS ${IOTCON_TEST_REPR_SERVER} DESTINATION ${BIN_INSTALL_DIR})
+ADD_EXECUTABLE(${IOTCON_TEST_IFACE_SERVER} ${IOTCON_TEST_IFACE_SERVER_SRCS})
+TARGET_LINK_LIBRARIES(${IOTCON_TEST_IFACE_SERVER} ${test_pkgs_LIBRARIES} ${CLIENT})
+INSTALL(TARGETS ${IOTCON_TEST_IFACE_SERVER} DESTINATION ${BIN_INSTALL_DIR})
ADD_EXECUTABLE(${IOTCON_TEST_ENCAP_CLIENT} ${IOTCON_TEST_ENCAP_CLIENT_SRCS})
TARGET_LINK_LIBRARIES(${IOTCON_TEST_ENCAP_CLIENT} ${test_pkgs_LIBRARIES} ${CLIENT})
--- /dev/null
+ IoTCon(Tizen IoT Connectivity) Test Program
+
+
+If you install the "iotcon package", you can test operations of iotcon.
+The result of test is printed out through DLOG. If you specify the LOG_TAG value
+to 'ICTEST'(&'IOTCON'), you can see the output of test programs.
+
+There are four pairs of test programs.
+
+
+1. Basic Test
+
+It is for testing CRUDN methods. A client can require CRUDN operations to server's resource
+through iotcon_remote_resource_get/put/post/delete() and iotcon_remote_resource_observe_register().
+
+ $ /usr/bin/iotcon-test-basic-server
+ $ /usr/bin/iotcon-test-basic-client
+
+
+2. Interface Test
+
+You can check the operations of resource that contains child resources. If the client
+require GET operation to server's parent resource, server sends response with representation
+including children's information. The contents of information are decided by the resource
+interface set by server. Client can send desired interface with request to server.
+
+ $ /usr/bin/iotcon-test-iface-server
+ $ /usr/bin/iotcon-test-iface-client
+
+
+3. Get Device/Platform Information Test
+
+Because device information and platform information are set by iotcon, you can check the
+information through iotcon_get_device_info() and iotcon_get_platform_info().
+
+ $ /usr/bin/iotcon-test-device-client
+
+
+4. Resource Encapsulation Test
+
+It is for testing resource encapsulation. Resource Encapsulation consists of lite resource,
+resource monitoring, and resource caching. If the server uses lite resource, there is no
+need to create a request handler. The client can check resource's state
+using iotcon_remote_resource_start_monitoring(). Also, the client can keep the resource's
+representation up to date using iotcon_remote_resource_start_caching().
+
+ $ /usr/bin/iotcon-test-encap-server
+ $ /usr/bin/iotcon-test-encap-client
--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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.
+ */
+
+#include <stdlib.h>
+#include <glib.h>
+#include <iotcon.h>
+#include "test.h"
+
+static char *room_resource_device_id;
+static GList *device_id_list;
+
+#define ROOM_RESOURCE_TYPE "org.tizen.room"
+#define ROOM_RESOURCE_URI_PREFIX "/room"
+#define LIGHT_RESOURCE_URI_PREFIX "/light"
+#define FAN_RESOURCE_URI_PREFIX "/fan"
+
+static bool _get_int_list_cb(int pos, const int value, void *user_data)
+{
+ DBG("%d", value);
+
+ return IOTCON_FUNC_CONTINUE;
+}
+
+static void _print_repr(iotcon_representation_h recv_repr)
+{
+ int i, ret, int_val;
+ bool is_null, bool_val;
+ char *uri_path, *str_val;
+ iotcon_list_h list_val;
+ iotcon_representation_h child_repr;
+ iotcon_state_h recv_state, child_state;
+ unsigned int key_count, children_count;
+
+ INFO("GET request was successful");
+
+ DBG("[ parent representation ]");
+ ret = iotcon_representation_get_uri_path(recv_repr, &uri_path);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_get_uri_path() Fail(%d)", ret);
+ return;
+ }
+ DBG("uri_path : %s", uri_path);
+
+ ret = iotcon_representation_get_state(recv_repr, &recv_state);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_get_state() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_state_get_keys_count(recv_state, &key_count);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
+ return;
+ }
+
+ if (key_count) {
+ ret = iotcon_state_get_str(recv_state, "name", &str_val);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_get_str() Fail(%d)", ret);
+ return;
+ }
+ DBG("name : %s", str_val);
+
+ ret = iotcon_state_get_list(recv_state, "today_temp", &list_val);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_get_list() Fail(%d)", ret);
+ return;
+ }
+
+ DBG("today's temperature :");
+ ret = iotcon_list_foreach_int(list_val, _get_int_list_cb, NULL);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_foreach_int() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_state_is_null(recv_state, "null value", &is_null);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_is_null() Fail(%d)", ret);
+ return;
+ }
+
+ if (is_null)
+ DBG("null value is null");
+ }
+
+ ret = iotcon_representation_get_children_count(recv_repr, &children_count);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_get_children_count() Fail(%d)", ret);
+ return;
+ }
+
+ for (i = 0; i < children_count; i++) {
+ DBG("[ child representation ]");
+
+ ret = iotcon_representation_get_nth_child(recv_repr, i, &child_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_get_nth_child(%d) Fail(%d)", i, ret);
+ continue;
+ }
+
+ ret = iotcon_representation_get_uri_path(child_repr, &uri_path);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_get_uri_path() Fail(%d)", ret);
+ continue;
+ }
+ DBG("uri_path : %s", uri_path);
+
+ ret = iotcon_representation_get_state(child_repr, &child_state);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_get_state() Fail(%d)", ret);
+ continue;
+ }
+
+ if (TEST_STR_EQUAL == strncmp(LIGHT_RESOURCE_URI_PREFIX, uri_path,
+ strlen(LIGHT_RESOURCE_URI_PREFIX))) {
+ ret = iotcon_state_get_keys_count(child_state, &key_count);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
+ continue;
+ }
+
+ if (key_count) {
+ ret = iotcon_state_get_int(child_state, "brightness", &int_val);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_get_int() Fail(%d)", ret);
+ continue;
+ }
+ DBG("brightness : %d", int_val);
+ }
+ } else if (TEST_STR_EQUAL == strncmp(FAN_RESOURCE_URI_PREFIX, uri_path,
+ strlen(FAN_RESOURCE_URI_PREFIX))) {
+ ret = iotcon_state_get_keys_count(child_state, &key_count);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
+ continue;
+ }
+ if (key_count) {
+ ret = iotcon_state_get_bool(child_state, "state", &bool_val);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_get_bool() Fail(%d)", ret);
+ continue;
+ }
+ DBG("state : %d", bool_val);
+ }
+ }
+ }
+}
+
+static void _on_get_2nd(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 recv_repr = NULL;
+
+ RETM_IF(IOTCON_ERROR_NONE != err, "Invalid err(%d)", err);
+
+ ret = iotcon_response_get_result(response, &response_result);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_get_result() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_response_get_representation(response, &recv_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_get_representation() Fail(%d)", ret);
+ return;
+ }
+
+ if (IOTCON_RESPONSE_RESULT_OK == response_result)
+ _print_repr(recv_repr);
+ else
+ ERR("Invalid result(%d)", response_result);
+
+ iotcon_remote_resource_destroy(resource);
+}
+
+static void _on_response_1st(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_query_h query_params;
+ iotcon_representation_h recv_repr = NULL;
+
+ RETM_IF(IOTCON_ERROR_NONE != err, "Invalid err(%d)", err);
+
+ ret = iotcon_response_get_result(response, &response_result);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_get_result() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_response_get_representation(response, &recv_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_get_representation() Fail(%d)", ret);
+ return;
+ }
+
+ if (IOTCON_RESPONSE_RESULT_OK == response_result)
+ _print_repr(recv_repr);
+ else
+ ERR("Invalid result(%d)", response_result);
+
+ ret = iotcon_query_create(&query_params);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_query_create() Fail(%d)", ret);
+ return;
+ }
+
+ ret = iotcon_query_set_interface(query_params, IOTCON_INTERFACE_BATCH);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_query_set_interface() Fail(%d)", ret);
+ iotcon_query_destroy(query_params);
+ return;
+ }
+
+ /* send GET request again with BATCH interface */
+ ret = iotcon_remote_resource_get(resource, query_params, _on_get_2nd, NULL);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_get() Fail(%d)", ret);
+ iotcon_query_destroy(query_params);
+ return;
+ }
+
+ iotcon_query_destroy(query_params);
+}
+
+static bool _get_res_type_fn(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 int _device_id_compare(const void *a, const void *b)
+{
+ return strcmp(a, b);
+}
+
+static void _found_resource(iotcon_remote_resource_h resource, iotcon_error_e result,
+ void *user_data)
+{
+ GList *node;
+ char *resource_host;
+ char *resource_uri_path;
+ char *resource_device_id;
+ int ret, resource_interfaces;
+ iotcon_resource_types_h resource_types;
+ iotcon_remote_resource_h cloned_resource;
+
+ RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result(%d)", result);
+
+ if (NULL == resource)
+ return;
+
+ INFO("===== resource found =====");
+
+ /* get the resource URI */
+ ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret);
+ return;
+ }
+
+ /* get the resource device id */
+ ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret);
+ return;
+ }
+ DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
+
+ node = g_list_find_custom(device_id_list, resource_device_id, _device_id_compare);
+
+ if (node && TEST_STR_EQUAL == strncmp(ROOM_RESOURCE_URI_PREFIX, resource_uri_path,
+ strlen(ROOM_RESOURCE_URI_PREFIX))) {
+ DBG("uri_path \"%s\" already found. skip !", resource_uri_path);
+ return;
+ }
+
+ room_resource_device_id = strdup(resource_device_id);
+ if (NULL == room_resource_device_id) {
+ ERR("strdup(room_resource_device_id) Fail");
+ return;
+ }
+
+ device_id_list = g_list_append(device_id_list, room_resource_device_id);
+
+ /* get the resource host address */
+ ret = iotcon_remote_resource_get_host_address(resource, &resource_host);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret);
+ device_id_list = g_list_remove(device_id_list, room_resource_device_id);
+ free(room_resource_device_id);
+ return;
+ }
+ DBG("[%s] resource host : %s", resource_uri_path, resource_host);
+
+ /* get the resource interfaces */
+ ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
+ device_id_list = g_list_remove(device_id_list, room_resource_device_id);
+ free(room_resource_device_id);
+ return;
+ }
+ if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
+ DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
+ if (IOTCON_INTERFACE_LINK & resource_interfaces)
+ DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
+ if (IOTCON_INTERFACE_BATCH & resource_interfaces)
+ DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
+ if (IOTCON_INTERFACE_GROUP & resource_interfaces)
+ DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
+
+ /* get the resource types */
+ ret = iotcon_remote_resource_get_types(resource, &resource_types);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
+ device_id_list = g_list_remove(device_id_list, room_resource_device_id);
+ free(room_resource_device_id);
+ return;
+ }
+ ret = iotcon_resource_types_foreach(resource_types, _get_res_type_fn,
+ resource_uri_path);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_resource_types_foreach() Fail(%d)", ret);
+ device_id_list = g_list_remove(device_id_list, room_resource_device_id);
+ free(room_resource_device_id);
+ return;
+ }
+
+ if (TEST_STR_EQUAL == strncmp(ROOM_RESOURCE_URI_PREFIX, resource_uri_path,
+ strlen(ROOM_RESOURCE_URI_PREFIX))) {
+ ret = iotcon_remote_resource_clone(resource, &cloned_resource);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_remote_resource_clone() Fail(%d)", ret);
+ device_id_list = g_list_remove(device_id_list, room_resource_device_id);
+ free(room_resource_device_id);
+ return;
+ }
+
+ /* send GET request */
+ ret = iotcon_remote_resource_get(cloned_resource, NULL, _on_response_1st, NULL);
+ if (IOTCON_ERROR_NONE != ret)
+ ERR("iotcon_remote_resource_get() Fail(%d)", ret);
+ }
+
+ device_id_list = g_list_remove(device_id_list, room_resource_device_id);
+ free(room_resource_device_id);
+}
+
+int main(int argc, char **argv)
+{
+ FN_CALL;
+ int ret;
+ GMainLoop *loop;
+
+ loop = g_main_loop_new(NULL, FALSE);
+
+ /* connect iotcon */
+ ret = iotcon_connect();
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_connect() Fail(%d)", ret);
+ return -1;
+ }
+
+ /* find room typed resources */
+ ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, IOTCON_CONNECTIVITY_IPV4,
+ ROOM_RESOURCE_TYPE, _found_resource, NULL);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_find_resource() Fail(%d)", ret);
+ iotcon_disconnect();
+ return -1;
+ }
+
+ g_main_loop_run(loop);
+ g_main_loop_unref(loop);
+
+ g_list_free_full(device_id_list, free);
+
+ /* disconnect iotcon */
+ iotcon_disconnect();
+
+ return 0;
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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.
+ */
+
+#include <stdlib.h>
+#include <glib.h>
+
+#include <iotcon.h>
+#include "test.h"
+
+#define ROOM_RESOURCE_URI "/room/1"
+#define ROOM_RESOURCE_TYPE "org.tizen.room"
+#define LIGHT_RESOURCE_URI "/light/1"
+#define LIGHT_RESOURCE_TYPE "org.tizen.light"
+#define FAN_RESOURCE_URI "/fan/1"
+#define FAN_RESOURCE_TYPE "org.tizen.fan"
+
+/* Light Resource */
+typedef struct _light_resource_s {
+ int brightness;
+ char *uri_path;
+ char *type;
+ int ifaces;
+ int properties;
+ iotcon_resource_h handle;
+} light_resource_s;
+
+/* Fan Resource */
+typedef struct _fan_resource_s {
+ bool state;
+ char *uri_path;
+ char *type;
+ int ifaces;
+ int properties;
+ iotcon_resource_h handle;
+} fan_resource_s;
+
+/* Room Resource */
+typedef struct _room_resource_s {
+ char *name;
+ int today_temp[5];
+ char *uri_path;
+ char *type;
+ int ifaces;
+ int properties;
+ iotcon_resource_h handle;
+ light_resource_s *child_light;
+ fan_resource_s *child_fan;
+} room_resource_s;
+
+static int _set_room_resource(room_resource_s *room)
+{
+ room->name = strdup("Michael's Room");
+ if (NULL == room->name) {
+ ERR("strdup() Fail");
+ return -1;
+ }
+
+ room->today_temp[0] = 13;
+ room->today_temp[1] = 19;
+ room->today_temp[2] = 24;
+ room->today_temp[3] = 21;
+ room->today_temp[4] = 14;
+
+ room->uri_path = strdup(ROOM_RESOURCE_URI);
+ if (NULL == room->uri_path) {
+ ERR("strdup(%s) Fail", ROOM_RESOURCE_URI);
+ free(room->name);
+ return -1;
+ }
+
+ room->type = strdup(ROOM_RESOURCE_TYPE);
+ if (NULL == room->type) {
+ ERR("strdup(%s) Fail", ROOM_RESOURCE_TYPE);
+ free(room->uri_path);
+ free(room->name);
+ return -1;
+ }
+
+ room->ifaces = IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH;
+ room->properties = IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE;
+
+ return 0;
+}
+
+static void _free_room_resource(room_resource_s *room)
+{
+ free(room->type);
+ free(room->uri_path);
+ free(room->name);
+}
+
+static int _set_light_resource(light_resource_s *light)
+{
+ light->brightness = 50;
+
+ light->uri_path = strdup(LIGHT_RESOURCE_URI);
+ if (NULL == light->uri_path) {
+ ERR("strdup(%s) Fail", LIGHT_RESOURCE_URI);
+ return -1;
+ }
+
+ light->type = strdup(LIGHT_RESOURCE_TYPE);
+ if (NULL == light->type) {
+ ERR("strdup(%s) Fail", LIGHT_RESOURCE_TYPE);
+ free(light->uri_path);
+ return -1;
+ }
+
+ light->ifaces = IOTCON_INTERFACE_DEFAULT;
+ light->properties = IOTCON_HIDDEN;
+
+ return 0;
+}
+
+static void _free_light_resource(light_resource_s *light)
+{
+ free(light->type);
+ free(light->uri_path);
+}
+
+static int _set_fan_resource(fan_resource_s *fan)
+{
+ fan->state = false;
+
+ fan->uri_path = strdup(FAN_RESOURCE_URI);
+ if (NULL == fan->uri_path) {
+ ERR("strdup(%s) Fail", FAN_RESOURCE_URI);
+ return -1;
+ }
+
+ fan->type = strdup(FAN_RESOURCE_TYPE);
+ if (NULL == fan->type) {
+ ERR("strdup(%s) Fail", FAN_RESOURCE_TYPE);
+ free(fan->uri_path);
+ return -1;
+ }
+
+ fan->ifaces = IOTCON_INTERFACE_DEFAULT;
+ fan->properties = IOTCON_HIDDEN;
+
+ return 0;
+}
+
+static void _free_fan_resource(fan_resource_s *fan)
+{
+ free(fan->type);
+ free(fan->uri_path);
+}
+
+static iotcon_resource_h _create_resource(char *uri_path, char *type, int ifaces,
+ int properties, iotcon_request_handler_cb cb, void *user_data)
+{
+ 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;
+ }
+
+ ret = iotcon_resource_create(uri_path, resource_types, ifaces, properties, cb,
+ user_data, &handle);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_create() Fail(%d)", ret);
+ iotcon_resource_types_destroy(resource_types);
+ return NULL;
+ }
+
+ iotcon_resource_types_destroy(resource_types);
+
+ return handle;
+}
+
+static int _send_response(iotcon_request_h request, iotcon_representation_h repr,
+ iotcon_interface_e iface, 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_representation(response, iface, repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_set_representation() Fail(%d)", ret);
+ iotcon_response_destroy(response);
+ 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;
+ }
+
+ /* 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 iotcon_representation_h _get_light_representation(light_resource_s *light)
+{
+ int ret;
+ iotcon_state_h state;
+ iotcon_representation_h repr;
+
+ /* create a light Representation */
+ ret = iotcon_representation_create(&repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_create() Fail(%d)", ret);
+ return NULL;
+ }
+
+ ret = iotcon_representation_set_uri_path(repr, light->uri_path);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ /* create a light state */
+ ret = iotcon_state_create(&state);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_create() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_state_set_int(state, "brightness", light->brightness);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_set_int() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ /* Set a light state into light Representation */
+ ret = iotcon_representation_set_state(repr, state);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_set_state() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ iotcon_state_destroy(state);
+
+ return repr;
+}
+
+static int _light_request_handler_get(light_resource_s *light, iotcon_request_h request)
+{
+ int ret;
+ iotcon_representation_h repr;
+
+ INFO("GET request - Light");
+
+ repr = _get_light_representation(light);
+ if (NULL == repr) {
+ ERR("_get_light_representation() Fail");
+ return -1;
+ }
+
+ ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT,
+ IOTCON_RESPONSE_RESULT_OK);
+ if (0 != ret) {
+ ERR("_send_response() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return -1;
+ }
+
+ iotcon_representation_destroy(repr);
+
+ return 0;
+}
+
+
+static iotcon_representation_h _get_fan_representation(fan_resource_s *fan)
+{
+ int ret;
+ iotcon_state_h state;
+ iotcon_representation_h repr;
+
+ /* create a fan Representation */
+ ret = iotcon_representation_create(&repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_create() Fail(%d)", ret);
+ return NULL;
+ }
+
+ ret = iotcon_representation_set_uri_path(repr, fan->uri_path);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ /* create a fan state */
+ ret = iotcon_state_create(&state);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_create() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_state_set_bool(state, "state", fan->state);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_set_bool() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ /* Set a light state into light Representation */
+ ret = iotcon_representation_set_state(repr, state);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_set_state() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ iotcon_state_destroy(state);
+
+ return repr;
+}
+
+static int _fan_request_handler_get(fan_resource_s *fan, iotcon_request_h request)
+{
+ int ret;
+ iotcon_representation_h repr;
+
+ INFO("GET request - Fan");
+
+ repr = _get_fan_representation(fan);
+ if (NULL == repr) {
+ ERR("_get_fan_representation() Fail");
+ return -1;
+ }
+
+ ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT,
+ IOTCON_RESPONSE_RESULT_OK);
+ if (0 != ret) {
+ ERR("_send_response() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return -1;
+ }
+
+ iotcon_representation_destroy(repr);
+
+ return 0;
+}
+
+static iotcon_representation_h _get_room_representation(room_resource_s *room)
+{
+ int ret;
+ iotcon_state_h state;
+ iotcon_list_h today_temp;
+ iotcon_representation_h repr, light_repr, fan_repr;
+
+ /* create a room Representation */
+ ret = iotcon_representation_create(&repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_create() Fail(%d)", ret);
+ return NULL;
+ }
+
+ ret = iotcon_representation_set_uri_path(repr, room->uri_path);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ /* create a room state */
+ ret = iotcon_state_create(&state);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_create() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_state_set_str(state, "name", room->name);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_set_str() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ /* set null */
+ ret = iotcon_state_set_null(state, "null value");
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_set_null() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_list_create(IOTCON_TYPE_INT, &today_temp);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_create() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_list_add_int(today_temp, room->today_temp[0], -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_add_int() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_list_add_int(today_temp, room->today_temp[1], -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_add_int() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_list_add_int(today_temp, room->today_temp[2], -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_add_int() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_list_add_int(today_temp, room->today_temp[3], -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_add_int() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_list_add_int(today_temp, room->today_temp[4], -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_add_int() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_state_set_list(state, "today_temp", today_temp);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_set_list() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ iotcon_list_destroy(today_temp);
+
+ /* Set a room state into room Representation */
+ ret = iotcon_representation_set_state(repr, state);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_set_state() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ iotcon_state_destroy(state);
+
+ light_repr = _get_light_representation(room->child_light);
+ if (NULL == light_repr) {
+ ERR("_get_light_representation() fail");
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_representation_append_child(repr, light_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_append_child() Fail(%d)", ret);
+ iotcon_representation_destroy(light_repr);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ iotcon_representation_destroy(light_repr);
+
+ fan_repr = _get_fan_representation(room->child_fan);
+ if (NULL == fan_repr) {
+ ERR("_get_fan_representation() fail");
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ ret = iotcon_representation_append_child(repr, fan_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_append_child() Fail(%d)", ret);
+ iotcon_representation_destroy(fan_repr);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
+
+ iotcon_representation_destroy(fan_repr);
+
+ return repr;
+}
+
+static int _room_request_handler_get(room_resource_s *room, iotcon_request_h request)
+{
+ int ret;
+ iotcon_query_h query;
+ iotcon_representation_h repr;
+ iotcon_interface_e iface = IOTCON_INTERFACE_DEFAULT;
+
+ INFO("GET request - Room");
+
+ repr = _get_room_representation(room);
+ if (NULL == repr) {
+ ERR("_get_room_representation() Fail");
+ return -1;
+ }
+
+ ret = iotcon_request_get_query(request, &query);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_query() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return -1;
+ }
+
+ if (query) {
+ ret = iotcon_query_get_interface(query, &iface);
+ if (IOTCON_ERROR_NO_DATA == ret) {
+ iface = IOTCON_INTERFACE_DEFAULT;
+ } else if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_query_get_interface() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return -1;
+ }
+ }
+
+ ret = _send_response(request, repr, iface, IOTCON_RESPONSE_RESULT_OK);
+ if (0 != ret) {
+ ERR("_send_response() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return -1;
+ }
+
+ iotcon_representation_destroy(repr);
+
+ return 0;
+}
+
+static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h request,
+ void *user_data)
+{
+ int ret;
+ iotcon_request_type_e type;
+ light_resource_s *light = user_data;
+ int iface = IOTCON_INTERFACE_DEFAULT;
+
+ RET_IF(NULL == request);
+
+ ret = iotcon_request_get_request_type(request, &type);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_types() Fail(%d)", ret);
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
+ return;
+ }
+
+ if (IOTCON_REQUEST_GET == type) {
+ ret = _light_request_handler_get(light, request);
+ if (0 != ret)
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
+ } else {
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN);
+ }
+}
+
+static void _fan_request_handler(iotcon_resource_h resource, iotcon_request_h request,
+ void *user_data)
+{
+ int ret;
+ iotcon_request_type_e type;
+ fan_resource_s *fan = user_data;
+ int iface = IOTCON_INTERFACE_DEFAULT;
+
+ RET_IF(NULL == request);
+
+ ret = iotcon_request_get_request_type(request, &type);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_types() Fail(%d)", ret);
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
+ return;
+ }
+
+ if (IOTCON_REQUEST_GET == type) {
+ ret = _fan_request_handler_get(fan, request);
+ if (0 != ret)
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
+ } else {
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN);
+ }
+}
+
+static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request,
+ void *user_data)
+{
+ FN_CALL;
+ int ret;
+ iotcon_request_type_e type;
+ char *host_address;
+ room_resource_s *room = user_data;
+ int iface = IOTCON_INTERFACE_DEFAULT;
+
+ RET_IF(NULL == request);
+
+ 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, iface, IOTCON_RESPONSE_RESULT_ERROR);
+ return;
+ }
+ INFO("host address : %s", host_address);
+
+ ret = iotcon_request_get_request_type(request, &type);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_types() Fail(%d)", ret);
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
+ return;
+ }
+
+ if (IOTCON_REQUEST_GET == type) {
+ ret = _room_request_handler_get(room, request);
+ if (0 != ret)
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
+ } else {
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN);
+ }
+}
+
+int main(int argc, char **argv)
+{
+ FN_CALL;
+ int ret;
+ GMainLoop *loop;
+ room_resource_s room = {0};
+ light_resource_s light = {0};
+ fan_resource_s fan = {0};
+
+ loop = g_main_loop_new(NULL, FALSE);
+
+ /* connect iotcon */
+ ret = iotcon_connect();
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_connect() Fail(%d)", ret);
+ return -1;
+ }
+
+ /* set resource */
+ ret = _set_room_resource(&room);
+ if (0 != ret) {
+ ERR("_set_room_resource() Fail(%d)", ret);
+ iotcon_disconnect();
+ return -1;
+ }
+
+ ret = _set_light_resource(&light);
+ if (0 != ret) {
+ ERR("_set_room_resource() Fail(%d)", ret);
+ _free_room_resource(&room);
+ iotcon_disconnect();
+ return -1;
+ }
+
+ ret = _set_fan_resource(&fan);
+ if (0 != ret) {
+ ERR("_set_room_resource() Fail(%d)", ret);
+ _free_room_resource(&room);
+ _free_light_resource(&light);
+ iotcon_disconnect();
+ return -1;
+ }
+
+ room.child_light = &light;
+ room.child_fan = &fan;
+
+ /* register room resource */
+ room.handle = _create_resource(room.uri_path, room.type, room.ifaces, room.properties,
+ _room_request_handler, &room);
+ if (NULL == room.handle) {
+ ERR("_create_resource() Fail");
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
+ iotcon_disconnect();
+ return -1;
+ }
+
+ /* register light resource */
+ light.handle = _create_resource(light.uri_path, light.type, light.ifaces,
+ light.properties, _light_request_handler, &light);
+ if (NULL == light.handle) {
+ ERR("_create_resource() Fail");
+ iotcon_resource_destroy(room.handle);
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
+ iotcon_disconnect();
+ return -1;
+ }
+
+ ret = iotcon_resource_bind_child_resource(room.handle, light.handle);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_resource_bind_child_resource() Fail");
+ iotcon_resource_destroy(light.handle);
+ iotcon_resource_destroy(room.handle);
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
+ iotcon_disconnect();
+ return -1;
+ }
+
+ /* register fan resource */
+ fan.handle = _create_resource(fan.uri_path, fan.type, fan.ifaces, fan.properties,
+ _fan_request_handler, &fan);
+ if (NULL == fan.handle) {
+ ERR("_create_resource() Fail");
+ iotcon_resource_destroy(light.handle);
+ iotcon_resource_destroy(room.handle);
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
+ iotcon_disconnect();
+ return -1;
+ }
+
+ ret = iotcon_resource_bind_child_resource(room.handle, fan.handle);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_resource_bind_child_resource() Fail");
+ iotcon_resource_destroy(fan.handle);
+ iotcon_resource_destroy(light.handle);
+ iotcon_resource_destroy(room.handle);
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
+ iotcon_disconnect();
+ return -1;
+ }
+
+ g_main_loop_run(loop);
+ g_main_loop_unref(loop);
+
+ iotcon_resource_destroy(fan.handle);
+ iotcon_resource_destroy(light.handle);
+ iotcon_resource_destroy(room.handle);
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
+
+ /* disconnect iotcon */
+ iotcon_disconnect();
+
+ return 0;
+}
+++ /dev/null
-/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * 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.
- */
-
-#include <stdlib.h>
-#include <glib.h>
-#include <iotcon.h>
-#include "test.h"
-
-static char *room_resource_device_id;
-static GList *device_id_list;
-
-#define ROOM_RESOURCE_TYPE "org.tizen.room"
-#define ROOM_RESOURCE_URI_PREFIX "/room"
-#define LIGHT_RESOURCE_URI_PREFIX "/light"
-#define FAN_RESOURCE_URI_PREFIX "/fan"
-
-static bool _get_int_list_cb(int pos, const int value, void *user_data)
-{
- DBG("%d", value);
-
- return IOTCON_FUNC_CONTINUE;
-}
-
-static void _print_repr(iotcon_representation_h recv_repr)
-{
- int i, ret, int_val;
- bool is_null, bool_val;
- char *uri_path, *str_val;
- iotcon_list_h list_val;
- iotcon_representation_h child_repr;
- iotcon_state_h recv_state, child_state;
- unsigned int key_count, children_count;
-
- INFO("GET request was successful");
-
- DBG("[ parent representation ]");
- ret = iotcon_representation_get_uri_path(recv_repr, &uri_path);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_get_uri_path() Fail(%d)", ret);
- return;
- }
- DBG("uri_path : %s", uri_path);
-
- ret = iotcon_representation_get_state(recv_repr, &recv_state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_get_state() Fail(%d)", ret);
- return;
- }
-
- ret = iotcon_state_get_keys_count(recv_state, &key_count);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
- return;
- }
-
- if (key_count) {
- ret = iotcon_state_get_str(recv_state, "name", &str_val);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_get_str() Fail(%d)", ret);
- return;
- }
- DBG("name : %s", str_val);
-
- ret = iotcon_state_get_list(recv_state, "today_temp", &list_val);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_get_list() Fail(%d)", ret);
- return;
- }
-
- DBG("today's temperature :");
- ret = iotcon_list_foreach_int(list_val, _get_int_list_cb, NULL);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_foreach_int() Fail(%d)", ret);
- return;
- }
-
- ret = iotcon_state_is_null(recv_state, "null value", &is_null);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_is_null() Fail(%d)", ret);
- return;
- }
-
- if (is_null)
- DBG("null value is null");
- }
-
- ret = iotcon_representation_get_children_count(recv_repr, &children_count);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_get_children_count() Fail(%d)", ret);
- return;
- }
-
- for (i = 0; i < children_count; i++) {
- DBG("[ child representation ]");
-
- ret = iotcon_representation_get_nth_child(recv_repr, i, &child_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_get_nth_child(%d) Fail(%d)", i, ret);
- continue;
- }
-
- ret = iotcon_representation_get_uri_path(child_repr, &uri_path);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_get_uri_path() Fail(%d)", ret);
- continue;
- }
- DBG("uri_path : %s", uri_path);
-
- ret = iotcon_representation_get_state(child_repr, &child_state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_get_state() Fail(%d)", ret);
- continue;
- }
-
- if (TEST_STR_EQUAL == strncmp(LIGHT_RESOURCE_URI_PREFIX, uri_path,
- strlen(LIGHT_RESOURCE_URI_PREFIX))) {
- ret = iotcon_state_get_keys_count(child_state, &key_count);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
- continue;
- }
-
- if (key_count) {
- ret = iotcon_state_get_int(child_state, "brightness", &int_val);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_get_int() Fail(%d)", ret);
- continue;
- }
- DBG("brightness : %d", int_val);
- }
- } else if (TEST_STR_EQUAL == strncmp(FAN_RESOURCE_URI_PREFIX, uri_path,
- strlen(FAN_RESOURCE_URI_PREFIX))) {
- ret = iotcon_state_get_keys_count(child_state, &key_count);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
- continue;
- }
- if (key_count) {
- ret = iotcon_state_get_bool(child_state, "state", &bool_val);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_get_bool() Fail(%d)", ret);
- continue;
- }
- DBG("state : %d", bool_val);
- }
- }
- }
-}
-
-static void _on_get_2nd(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 recv_repr = NULL;
-
- RETM_IF(IOTCON_ERROR_NONE != err, "Invalid err(%d)", err);
-
- ret = iotcon_response_get_result(response, &response_result);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_get_result() Fail(%d)", ret);
- return;
- }
-
- ret = iotcon_response_get_representation(response, &recv_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_get_representation() Fail(%d)", ret);
- return;
- }
-
- if (IOTCON_RESPONSE_RESULT_OK == response_result)
- _print_repr(recv_repr);
- else
- ERR("Invalid result(%d)", response_result);
-
- iotcon_remote_resource_destroy(resource);
-}
-
-static void _on_response_1st(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_query_h query_params;
- iotcon_representation_h recv_repr = NULL;
-
- RETM_IF(IOTCON_ERROR_NONE != err, "Invalid err(%d)", err);
-
- ret = iotcon_response_get_result(response, &response_result);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_get_result() Fail(%d)", ret);
- return;
- }
-
- ret = iotcon_response_get_representation(response, &recv_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_get_representation() Fail(%d)", ret);
- return;
- }
-
- if (IOTCON_RESPONSE_RESULT_OK == response_result)
- _print_repr(recv_repr);
- else
- ERR("Invalid result(%d)", response_result);
-
- ret = iotcon_query_create(&query_params);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_query_create() Fail(%d)", ret);
- return;
- }
-
- ret = iotcon_query_set_interface(query_params, IOTCON_INTERFACE_BATCH);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_query_set_interface() Fail(%d)", ret);
- iotcon_query_destroy(query_params);
- return;
- }
-
- /* send GET request again with BATCH interface */
- ret = iotcon_remote_resource_get(resource, query_params, _on_get_2nd, NULL);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_remote_resource_get() Fail(%d)", ret);
- iotcon_query_destroy(query_params);
- return;
- }
-
- iotcon_query_destroy(query_params);
-}
-
-static bool _get_res_type_fn(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 int _device_id_compare(const void *a, const void *b)
-{
- return strcmp(a, b);
-}
-
-static void _found_resource(iotcon_remote_resource_h resource, iotcon_error_e result,
- void *user_data)
-{
- GList *node;
- char *resource_host;
- char *resource_uri_path;
- char *resource_device_id;
- int ret, resource_interfaces;
- iotcon_resource_types_h resource_types;
- iotcon_remote_resource_h cloned_resource;
-
- RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result(%d)", result);
-
- if (NULL == resource)
- return;
-
- INFO("===== resource found =====");
-
- /* get the resource URI */
- ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret);
- return;
- }
-
- /* get the resource device id */
- ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret);
- return;
- }
- DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
-
- node = g_list_find_custom(device_id_list, resource_device_id, _device_id_compare);
-
- if (node && TEST_STR_EQUAL == strncmp(ROOM_RESOURCE_URI_PREFIX, resource_uri_path,
- strlen(ROOM_RESOURCE_URI_PREFIX))) {
- DBG("uri_path \"%s\" already found. skip !", resource_uri_path);
- return;
- }
-
- room_resource_device_id = strdup(resource_device_id);
- if (NULL == room_resource_device_id) {
- ERR("strdup(room_resource_device_id) Fail");
- return;
- }
-
- device_id_list = g_list_append(device_id_list, room_resource_device_id);
-
- /* get the resource host address */
- ret = iotcon_remote_resource_get_host_address(resource, &resource_host);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret);
- device_id_list = g_list_remove(device_id_list, room_resource_device_id);
- free(room_resource_device_id);
- return;
- }
- DBG("[%s] resource host : %s", resource_uri_path, resource_host);
-
- /* get the resource interfaces */
- ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
- device_id_list = g_list_remove(device_id_list, room_resource_device_id);
- free(room_resource_device_id);
- return;
- }
- if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
- DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
- if (IOTCON_INTERFACE_LINK & resource_interfaces)
- DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
- if (IOTCON_INTERFACE_BATCH & resource_interfaces)
- DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
- if (IOTCON_INTERFACE_GROUP & resource_interfaces)
- DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
-
- /* get the resource types */
- ret = iotcon_remote_resource_get_types(resource, &resource_types);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
- device_id_list = g_list_remove(device_id_list, room_resource_device_id);
- free(room_resource_device_id);
- return;
- }
- ret = iotcon_resource_types_foreach(resource_types, _get_res_type_fn,
- resource_uri_path);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_resource_types_foreach() Fail(%d)", ret);
- device_id_list = g_list_remove(device_id_list, room_resource_device_id);
- free(room_resource_device_id);
- return;
- }
-
- if (TEST_STR_EQUAL == strncmp(ROOM_RESOURCE_URI_PREFIX, resource_uri_path,
- strlen(ROOM_RESOURCE_URI_PREFIX))) {
- ret = iotcon_remote_resource_clone(resource, &cloned_resource);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_remote_resource_clone() Fail(%d)", ret);
- device_id_list = g_list_remove(device_id_list, room_resource_device_id);
- free(room_resource_device_id);
- return;
- }
-
- /* send GET request */
- ret = iotcon_remote_resource_get(cloned_resource, NULL, _on_response_1st, NULL);
- if (IOTCON_ERROR_NONE != ret)
- ERR("iotcon_remote_resource_get() Fail(%d)", ret);
- }
-
- device_id_list = g_list_remove(device_id_list, room_resource_device_id);
- free(room_resource_device_id);
-}
-
-int main(int argc, char **argv)
-{
- FN_CALL;
- int ret;
- GMainLoop *loop;
-
- loop = g_main_loop_new(NULL, FALSE);
-
- /* connect iotcon */
- ret = iotcon_connect();
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_connect() Fail(%d)", ret);
- return -1;
- }
-
- /* find room typed resources */
- ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, IOTCON_CONNECTIVITY_IPV4,
- ROOM_RESOURCE_TYPE, _found_resource, NULL);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_find_resource() Fail(%d)", ret);
- iotcon_disconnect();
- return -1;
- }
-
- g_main_loop_run(loop);
- g_main_loop_unref(loop);
-
- g_list_free_full(device_id_list, free);
-
- /* disconnect iotcon */
- iotcon_disconnect();
-
- return 0;
-}
+++ /dev/null
-/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * 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.
- */
-
-#include <stdlib.h>
-#include <glib.h>
-
-#include <iotcon.h>
-#include "test.h"
-
-#define ROOM_RESOURCE_URI "/room/1"
-#define ROOM_RESOURCE_TYPE "org.tizen.room"
-#define LIGHT_RESOURCE_URI "/light/1"
-#define LIGHT_RESOURCE_TYPE "org.tizen.light"
-#define FAN_RESOURCE_URI "/fan/1"
-#define FAN_RESOURCE_TYPE "org.tizen.fan"
-
-/* Light Resource */
-typedef struct _light_resource_s {
- int brightness;
- char *uri_path;
- char *type;
- int ifaces;
- int properties;
- iotcon_resource_h handle;
-} light_resource_s;
-
-/* Fan Resource */
-typedef struct _fan_resource_s {
- bool state;
- char *uri_path;
- char *type;
- int ifaces;
- int properties;
- iotcon_resource_h handle;
-} fan_resource_s;
-
-/* Room Resource */
-typedef struct _room_resource_s {
- char *name;
- int today_temp[5];
- char *uri_path;
- char *type;
- int ifaces;
- int properties;
- iotcon_resource_h handle;
- light_resource_s *child_light;
- fan_resource_s *child_fan;
-} room_resource_s;
-
-static int _set_room_resource(room_resource_s *room)
-{
- room->name = strdup("Michael's Room");
- if (NULL == room->name) {
- ERR("strdup() Fail");
- return -1;
- }
-
- room->today_temp[0] = 13;
- room->today_temp[1] = 19;
- room->today_temp[2] = 24;
- room->today_temp[3] = 21;
- room->today_temp[4] = 14;
-
- room->uri_path = strdup(ROOM_RESOURCE_URI);
- if (NULL == room->uri_path) {
- ERR("strdup(%s) Fail", ROOM_RESOURCE_URI);
- free(room->name);
- return -1;
- }
-
- room->type = strdup(ROOM_RESOURCE_TYPE);
- if (NULL == room->type) {
- ERR("strdup(%s) Fail", ROOM_RESOURCE_TYPE);
- free(room->uri_path);
- free(room->name);
- return -1;
- }
-
- room->ifaces = IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH;
- room->properties = IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE;
-
- return 0;
-}
-
-static void _free_room_resource(room_resource_s *room)
-{
- free(room->type);
- free(room->uri_path);
- free(room->name);
-}
-
-static int _set_light_resource(light_resource_s *light)
-{
- light->brightness = 50;
-
- light->uri_path = strdup(LIGHT_RESOURCE_URI);
- if (NULL == light->uri_path) {
- ERR("strdup(%s) Fail", LIGHT_RESOURCE_URI);
- return -1;
- }
-
- light->type = strdup(LIGHT_RESOURCE_TYPE);
- if (NULL == light->type) {
- ERR("strdup(%s) Fail", LIGHT_RESOURCE_TYPE);
- free(light->uri_path);
- return -1;
- }
-
- light->ifaces = IOTCON_INTERFACE_DEFAULT;
- light->properties = IOTCON_HIDDEN;
-
- return 0;
-}
-
-static void _free_light_resource(light_resource_s *light)
-{
- free(light->type);
- free(light->uri_path);
-}
-
-static int _set_fan_resource(fan_resource_s *fan)
-{
- fan->state = false;
-
- fan->uri_path = strdup(FAN_RESOURCE_URI);
- if (NULL == fan->uri_path) {
- ERR("strdup(%s) Fail", FAN_RESOURCE_URI);
- return -1;
- }
-
- fan->type = strdup(FAN_RESOURCE_TYPE);
- if (NULL == fan->type) {
- ERR("strdup(%s) Fail", FAN_RESOURCE_TYPE);
- free(fan->uri_path);
- return -1;
- }
-
- fan->ifaces = IOTCON_INTERFACE_DEFAULT;
- fan->properties = IOTCON_HIDDEN;
-
- return 0;
-}
-
-static void _free_fan_resource(fan_resource_s *fan)
-{
- free(fan->type);
- free(fan->uri_path);
-}
-
-static iotcon_resource_h _create_resource(char *uri_path, char *type, int ifaces,
- int properties, iotcon_request_handler_cb cb, void *user_data)
-{
- 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;
- }
-
- ret = iotcon_resource_create(uri_path, resource_types, ifaces, properties, cb,
- user_data, &handle);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_create() Fail(%d)", ret);
- iotcon_resource_types_destroy(resource_types);
- return NULL;
- }
-
- iotcon_resource_types_destroy(resource_types);
-
- return handle;
-}
-
-static int _send_response(iotcon_request_h request, iotcon_representation_h repr,
- iotcon_interface_e iface, 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_representation(response, iface, repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_set_representation() Fail(%d)", ret);
- iotcon_response_destroy(response);
- 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;
- }
-
- /* 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 iotcon_representation_h _get_light_representation(light_resource_s *light)
-{
- int ret;
- iotcon_state_h state;
- iotcon_representation_h repr;
-
- /* create a light Representation */
- ret = iotcon_representation_create(&repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_create() Fail(%d)", ret);
- return NULL;
- }
-
- ret = iotcon_representation_set_uri_path(repr, light->uri_path);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- /* create a light state */
- ret = iotcon_state_create(&state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_create() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_state_set_int(state, "brightness", light->brightness);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_int() Fail(%d)", ret);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- /* Set a light state into light Representation */
- ret = iotcon_representation_set_state(repr, state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_state() Fail(%d)", ret);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- iotcon_state_destroy(state);
-
- return repr;
-}
-
-static int _light_request_handler_get(light_resource_s *light, iotcon_request_h request)
-{
- int ret;
- iotcon_representation_h repr;
-
- INFO("GET request - Light");
-
- repr = _get_light_representation(light);
- if (NULL == repr) {
- ERR("_get_light_representation() Fail");
- return -1;
- }
-
- ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT,
- IOTCON_RESPONSE_RESULT_OK);
- if (0 != ret) {
- ERR("_send_response() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return -1;
- }
-
- iotcon_representation_destroy(repr);
-
- return 0;
-}
-
-
-static iotcon_representation_h _get_fan_representation(fan_resource_s *fan)
-{
- int ret;
- iotcon_state_h state;
- iotcon_representation_h repr;
-
- /* create a fan Representation */
- ret = iotcon_representation_create(&repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_create() Fail(%d)", ret);
- return NULL;
- }
-
- ret = iotcon_representation_set_uri_path(repr, fan->uri_path);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- /* create a fan state */
- ret = iotcon_state_create(&state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_create() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_state_set_bool(state, "state", fan->state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_bool() Fail(%d)", ret);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- /* Set a light state into light Representation */
- ret = iotcon_representation_set_state(repr, state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_state() Fail(%d)", ret);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- iotcon_state_destroy(state);
-
- return repr;
-}
-
-static int _fan_request_handler_get(fan_resource_s *fan, iotcon_request_h request)
-{
- int ret;
- iotcon_representation_h repr;
-
- INFO("GET request - Fan");
-
- repr = _get_fan_representation(fan);
- if (NULL == repr) {
- ERR("_get_fan_representation() Fail");
- return -1;
- }
-
- ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT,
- IOTCON_RESPONSE_RESULT_OK);
- if (0 != ret) {
- ERR("_send_response() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return -1;
- }
-
- iotcon_representation_destroy(repr);
-
- return 0;
-}
-
-static iotcon_representation_h _get_room_representation(room_resource_s *room)
-{
- int ret;
- iotcon_state_h state;
- iotcon_list_h today_temp;
- iotcon_representation_h repr, light_repr, fan_repr;
-
- /* create a room Representation */
- ret = iotcon_representation_create(&repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_create() Fail(%d)", ret);
- return NULL;
- }
-
- ret = iotcon_representation_set_uri_path(repr, room->uri_path);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- /* create a room state */
- ret = iotcon_state_create(&state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_create() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_state_set_str(state, "name", room->name);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_str() Fail(%d)", ret);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- /* set null */
- ret = iotcon_state_set_null(state, "null value");
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_null() Fail(%d)", ret);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_list_create(IOTCON_TYPE_INT, &today_temp);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_create() Fail(%d)", ret);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_list_add_int(today_temp, room->today_temp[0], -1);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_add_int() Fail(%d)", ret);
- iotcon_list_destroy(today_temp);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_list_add_int(today_temp, room->today_temp[1], -1);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_add_int() Fail(%d)", ret);
- iotcon_list_destroy(today_temp);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_list_add_int(today_temp, room->today_temp[2], -1);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_add_int() Fail(%d)", ret);
- iotcon_list_destroy(today_temp);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_list_add_int(today_temp, room->today_temp[3], -1);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_add_int() Fail(%d)", ret);
- iotcon_list_destroy(today_temp);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_list_add_int(today_temp, room->today_temp[4], -1);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_add_int() Fail(%d)", ret);
- iotcon_list_destroy(today_temp);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_state_set_list(state, "today_temp", today_temp);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_list() Fail(%d)", ret);
- iotcon_list_destroy(today_temp);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- iotcon_list_destroy(today_temp);
-
- /* Set a room state into room Representation */
- ret = iotcon_representation_set_state(repr, state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_state() Fail(%d)", ret);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- iotcon_state_destroy(state);
-
- light_repr = _get_light_representation(room->child_light);
- if (NULL == light_repr) {
- ERR("_get_light_representation() fail");
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_representation_append_child(repr, light_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_append_child() Fail(%d)", ret);
- iotcon_representation_destroy(light_repr);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- iotcon_representation_destroy(light_repr);
-
- fan_repr = _get_fan_representation(room->child_fan);
- if (NULL == fan_repr) {
- ERR("_get_fan_representation() fail");
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- ret = iotcon_representation_append_child(repr, fan_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_append_child() Fail(%d)", ret);
- iotcon_representation_destroy(fan_repr);
- iotcon_representation_destroy(repr);
- return NULL;
- }
-
- iotcon_representation_destroy(fan_repr);
-
- return repr;
-}
-
-static int _room_request_handler_get(room_resource_s *room, iotcon_request_h request)
-{
- int ret;
- iotcon_query_h query;
- iotcon_representation_h repr;
- iotcon_interface_e iface = IOTCON_INTERFACE_DEFAULT;
-
- INFO("GET request - Room");
-
- repr = _get_room_representation(room);
- if (NULL == repr) {
- ERR("_get_room_representation() Fail");
- return -1;
- }
-
- ret = iotcon_request_get_query(request, &query);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_request_get_query() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return -1;
- }
-
- if (query) {
- ret = iotcon_query_get_interface(query, &iface);
- if (IOTCON_ERROR_NO_DATA == ret) {
- iface = IOTCON_INTERFACE_DEFAULT;
- } else if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_query_get_interface() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return -1;
- }
- }
-
- ret = _send_response(request, repr, iface, IOTCON_RESPONSE_RESULT_OK);
- if (0 != ret) {
- ERR("_send_response() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return -1;
- }
-
- iotcon_representation_destroy(repr);
-
- return 0;
-}
-
-static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h request,
- void *user_data)
-{
- int ret;
- iotcon_request_type_e type;
- light_resource_s *light = user_data;
- int iface = IOTCON_INTERFACE_DEFAULT;
-
- RET_IF(NULL == request);
-
- ret = iotcon_request_get_request_type(request, &type);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_request_get_types() Fail(%d)", ret);
- _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
- return;
- }
-
- if (IOTCON_REQUEST_GET == type) {
- ret = _light_request_handler_get(light, request);
- if (0 != ret)
- _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
- } else {
- _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN);
- }
-}
-
-static void _fan_request_handler(iotcon_resource_h resource, iotcon_request_h request,
- void *user_data)
-{
- int ret;
- iotcon_request_type_e type;
- fan_resource_s *fan = user_data;
- int iface = IOTCON_INTERFACE_DEFAULT;
-
- RET_IF(NULL == request);
-
- ret = iotcon_request_get_request_type(request, &type);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_request_get_types() Fail(%d)", ret);
- _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
- return;
- }
-
- if (IOTCON_REQUEST_GET == type) {
- ret = _fan_request_handler_get(fan, request);
- if (0 != ret)
- _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
- } else {
- _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN);
- }
-}
-
-static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request,
- void *user_data)
-{
- FN_CALL;
- int ret;
- iotcon_request_type_e type;
- char *host_address;
- room_resource_s *room = user_data;
- int iface = IOTCON_INTERFACE_DEFAULT;
-
- RET_IF(NULL == request);
-
- 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, iface, IOTCON_RESPONSE_RESULT_ERROR);
- return;
- }
- INFO("host address : %s", host_address);
-
- ret = iotcon_request_get_request_type(request, &type);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_request_get_types() Fail(%d)", ret);
- _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
- return;
- }
-
- if (IOTCON_REQUEST_GET == type) {
- ret = _room_request_handler_get(room, request);
- if (0 != ret)
- _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
- } else {
- _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN);
- }
-}
-
-int main(int argc, char **argv)
-{
- FN_CALL;
- int ret;
- GMainLoop *loop;
- room_resource_s room = {0};
- light_resource_s light = {0};
- fan_resource_s fan = {0};
-
- loop = g_main_loop_new(NULL, FALSE);
-
- /* connect iotcon */
- ret = iotcon_connect();
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_connect() Fail(%d)", ret);
- return -1;
- }
-
- /* set resource */
- ret = _set_room_resource(&room);
- if (0 != ret) {
- ERR("_set_room_resource() Fail(%d)", ret);
- iotcon_disconnect();
- return -1;
- }
-
- ret = _set_light_resource(&light);
- if (0 != ret) {
- ERR("_set_room_resource() Fail(%d)", ret);
- _free_room_resource(&room);
- iotcon_disconnect();
- return -1;
- }
-
- ret = _set_fan_resource(&fan);
- if (0 != ret) {
- ERR("_set_room_resource() Fail(%d)", ret);
- _free_room_resource(&room);
- _free_light_resource(&light);
- iotcon_disconnect();
- return -1;
- }
-
- room.child_light = &light;
- room.child_fan = &fan;
-
- /* register room resource */
- room.handle = _create_resource(room.uri_path, room.type, room.ifaces, room.properties,
- _room_request_handler, &room);
- if (NULL == room.handle) {
- ERR("_create_resource() Fail");
- _free_fan_resource(&fan);
- _free_light_resource(&light);
- _free_room_resource(&room);
- iotcon_disconnect();
- return -1;
- }
-
- /* register light resource */
- light.handle = _create_resource(light.uri_path, light.type, light.ifaces,
- light.properties, _light_request_handler, &light);
- if (NULL == light.handle) {
- ERR("_create_resource() Fail");
- iotcon_resource_destroy(room.handle);
- _free_fan_resource(&fan);
- _free_light_resource(&light);
- _free_room_resource(&room);
- iotcon_disconnect();
- return -1;
- }
-
- ret = iotcon_resource_bind_child_resource(room.handle, light.handle);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_resource_bind_child_resource() Fail");
- iotcon_resource_destroy(light.handle);
- iotcon_resource_destroy(room.handle);
- _free_fan_resource(&fan);
- _free_light_resource(&light);
- _free_room_resource(&room);
- iotcon_disconnect();
- return -1;
- }
-
- /* register fan resource */
- fan.handle = _create_resource(fan.uri_path, fan.type, fan.ifaces, fan.properties,
- _fan_request_handler, &fan);
- if (NULL == fan.handle) {
- ERR("_create_resource() Fail");
- iotcon_resource_destroy(light.handle);
- iotcon_resource_destroy(room.handle);
- _free_fan_resource(&fan);
- _free_light_resource(&light);
- _free_room_resource(&room);
- iotcon_disconnect();
- return -1;
- }
-
- ret = iotcon_resource_bind_child_resource(room.handle, fan.handle);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_resource_bind_child_resource() Fail");
- iotcon_resource_destroy(fan.handle);
- iotcon_resource_destroy(light.handle);
- iotcon_resource_destroy(room.handle);
- _free_fan_resource(&fan);
- _free_light_resource(&light);
- _free_room_resource(&room);
- iotcon_disconnect();
- return -1;
- }
-
- g_main_loop_run(loop);
- g_main_loop_unref(loop);
-
- iotcon_resource_destroy(fan.handle);
- iotcon_resource_destroy(light.handle);
- iotcon_resource_destroy(room.handle);
- _free_fan_resource(&fan);
- _free_light_resource(&light);
- _free_room_resource(&room);
-
- /* disconnect iotcon */
- iotcon_disconnect();
-
- return 0;
-}