Initial version 84/138884/2
authorJin Yoon <jinny.yoon@samsung.com>
Fri, 14 Jul 2017 06:46:07 +0000 (15:46 +0900)
committerJin Yoon <jinny.yoon@samsung.com>
Fri, 14 Jul 2017 06:47:47 +0000 (15:47 +0900)
Change-Id: I4793992b12b75b9fd8cbb44404cd4160f162f06b

CMakeLists.txt [new file with mode: 0644]
inc/connectivity.h [new file with mode: 0644]
inc/connectivity_internal.h [new file with mode: 0644]
inc/log.h [new file with mode: 0644]
org.tizen.position-finder-client.manifest [new file with mode: 0755]
packaging/org.tizen.position-finder-client.spec [new file with mode: 0644]
res/iotcon-test-svr-db-client.dat [new file with mode: 0644]
shared/res/position_finder_client.png [new file with mode: 0755]
src/connectivity.c [new file with mode: 0644]
src/control.c [new file with mode: 0644]
tizen-manifest.xml [new file with mode: 0644]

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..df415a6
--- /dev/null
@@ -0,0 +1,48 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(position-finder-client C)
+
+SET(INSTALL_EXEC_PREFIX "${INSTALL_PREFIX}/bin")
+SET(INSTALL_RESDIR "${INSTALL_PREFIX}/res")
+SET(CMAKE_VERBOSE_MAKEFILE 0)
+
+SET(PROJECT_ROOT_DIR "${CMAKE_SOURCE_DIR}")
+SET(PROJECT_RESOURCES_DIR "${PROJECT_ROOT_DIR}/res")
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(APP_PKGS REQUIRED
+       dlog
+       aul
+       capi-appfw-application
+       capi-appfw-service-application
+       capi-system-peripheral-io
+       iotcon
+       eina
+)
+
+FOREACH (flag ${APP_PKGS_CFLAGS})
+    SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Winline -g -fno-builtin-malloc -fPIE")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
+
+INCLUDE_DIRECTORIES(${PROJECT_ROOT_DIR}/inc)
+
+ADD_EXECUTABLE(${PROJECT_NAME}
+       ${PROJECT_ROOT_DIR}/src/connectivity.c
+       ${PROJECT_ROOT_DIR}/src/control.c
+)
+
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} -lm)
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${APP_PKGS_LDFLAGS})
+
+# Install
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${INSTALL_EXEC_PREFIX})
+
+INSTALL(FILES ${PROJECT_ROOT_DIR}/tizen-manifest.xml DESTINATION ${SYS_PACKAGES_DIR} RENAME org.tizen.position-finder-client.xml)
+INSTALL(DIRECTORY DESTINATION ${PREFIX}/data)
+INSTALL(FILES ${PROJECT_ROOT_DIR}/shared/res/position_finder_client.png DESTINATION ${SYS_ICONS_DIR})
+INSTALL(FILES ${PROJECT_ROOT_DIR}/res/iotcon-test-svr-db-client.dat DESTINATION ${INSTALL_RESDIR})
+
+# End of a file
diff --git a/inc/connectivity.h b/inc/connectivity.h
new file mode 100644 (file)
index 0000000..00e3a2b
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Jin Yoon <jinny.yoon@samsung.com>
+ *          Geunsun Lee <gs86.lee@samsung.com>
+ *          Eunyoung Lee <ey928.lee@samsung.com>
+ *          Junkyu Han <junkyu.han@samsung.com>
+ *
+ * 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.
+ */
+
+#ifndef __POSITION_FINDER_CONNECTIVITY_H__
+#define __POSITION_FINDER_CONNECTIVITY_H__
+
+#include "connectivity_internal.h"
+
+typedef struct _connectivity_resource_s connectivity_resource_s;
+
+extern int connectivity_find_resource(void);
+
+#endif /* __POSITION_FINDER_CONNECTIVITY_H__ */
diff --git a/inc/connectivity_internal.h b/inc/connectivity_internal.h
new file mode 100644 (file)
index 0000000..af21943
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Jin Yoon <jinny.yoon@samsung.com>
+ *          Geunsun Lee <gs86.lee@samsung.com>
+ *          Eunyoung Lee <ey928.lee@samsung.com>
+ *          Junkyu Han <junkyu.han@samsung.com>
+ *
+ * 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.
+ */
+
+#ifndef __POSITION_FINDER_CONNECTIVITY_H__
+#define __POSITION_FINDER_CONNECTIVITY_H__
+
+extern int connectivity_init(void);
+extern int connectivity_fini(void);
+
+#endif /* __POSITION_FINDER_CONNECTIVITY_H__ */
diff --git a/inc/log.h b/inc/log.h
new file mode 100644 (file)
index 0000000..4d48c05
--- /dev/null
+++ b/inc/log.h
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Jin Yoon <jinny.yoon@samsung.com>
+ *          Geunsun Lee <gs86.lee@samsung.com>
+ *          Eunyoung Lee <ey928.lee@samsung.com>
+ *          Junkyu Han <junkyu.han@samsung.com>
+ *
+ * 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.
+ */
+
+#ifndef __POSITION_FINDER_CLIENT_H__
+#define __POSITION_FINDER_CLIENT_H__
+
+#include <dlog.h>
+
+#ifdef  LOG_TAG
+#undef  LOG_TAG
+#endif
+#define LOG_TAG "POSITION_FINDER_CLIENT"
+
+#if !defined(_D)
+#define _D(fmt, arg...) dlog_print(DLOG_DEBUG, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg)
+#endif
+
+#if !defined(_I)
+#define _I(fmt, arg...) dlog_print(DLOG_INFO, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg)
+#endif
+
+#if !defined(_W)
+#define _W(fmt, arg...) dlog_print(DLOG_WARN, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg)
+#endif
+
+#if !defined(_E)
+#define _E(fmt, arg...) dlog_print(DLOG_ERROR, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg)
+#endif
+
+#define retvm_if(expr, val, fmt, arg...) do { \
+       if(expr) { \
+               _E(fmt, ##arg); \
+               _E("(%s) -> %s() return", #expr, __FUNCTION__); \
+               return val; \
+       } \
+} while (0)
+
+#define retv_if(expr, val) do { \
+       if(expr) { \
+               _E("(%s) -> %s() return", #expr, __FUNCTION__); \
+               return (val); \
+       } \
+} while (0)
+
+#define retm_if(expr, fmt, arg...) do { \
+       if(expr) { \
+               _E(fmt, ##arg); \
+               _E("(%s) -> %s() return", #expr, __FUNCTION__); \
+               return; \
+       } \
+} while (0)
+
+#define ret_if(expr) do { \
+       if(expr) { \
+               _E("(%s) -> %s() return", #expr, __FUNCTION__); \
+               return; \
+       } \
+} while (0)
+
+#define goto_if(expr, val) do { \
+       if(expr) { \
+               _E("(%s) -> goto", #expr); \
+               goto val; \
+       } \
+} while (0)
+
+#define break_if(expr) { \
+       if(expr) { \
+               _E("(%s) -> break", #expr); \
+               break; \
+       } \
+}
+
+#define continue_if(expr) { \
+       if(expr) { \
+               _E("(%s) -> continue", #expr); \
+               continue; \
+       } \
+}
+
+
+
+#endif /* __POSITION_FINDER_CLIENT_H__ */
diff --git a/org.tizen.position-finder-client.manifest b/org.tizen.position-finder-client.manifest
new file mode 100755 (executable)
index 0000000..a76fdba
--- /dev/null
@@ -0,0 +1,5 @@
+<manifest>
+       <request>
+               <domain name="_" />
+       </request>
+</manifest>
diff --git a/packaging/org.tizen.position-finder-client.spec b/packaging/org.tizen.position-finder-client.spec
new file mode 100644 (file)
index 0000000..f56d031
--- /dev/null
@@ -0,0 +1,76 @@
+Name:       org.tizen.position-finder-client
+%define alias org.tizen.position-finder-client
+Summary:    Position Finder Client
+Group:      Applications/Core Applications
+Version:    0.0.1
+Release:    1
+License:    Apache-2.0
+Provides:   org.tizen.position-finder-client = %{version}-%{release}
+Source0:    %{name}-%{version}.tar.gz
+
+BuildRequires:  cmake
+BuildRequires:  hash-signer
+BuildRequires:  pkgconfig(capi-appfw-application)
+BuildRequires:  pkgconfig(dlog)
+BuildRequires:  pkgconfig(libtzplatform-config)
+BuildRequires:  pkgconfig(capi-appfw-service-application)
+BuildRequires: pkgconfig(capi-system-peripheral-io)
+BuildRequires: pkgconfig(iotcon)
+BuildRequires: pkgconfig(eina)
+
+%description
+Thing Illumination
+
+%prep
+%setup -q
+
+%build
+
+%define _pkg_dir %{TZ_SYS_RO_APP}/%{alias}
+%define _pkg_shared_dir %{_pkg_dir}/shared
+%define _pkg_data_dir %{_pkg_dir}/data
+%define _sys_icons_dir %{_pkg_shared_dir}/res
+%define _sys_packages_dir %{TZ_SYS_RO_PACKAGES}
+%define _sys_license_dir %{TZ_SYS_SHARE}/license
+
+%ifarch %{arm}
+export CFLAGS="$CFLAGS -DTIZEN_BUILD_TARGET"
+export CXXFLAGS="$CXXFLAGS -DTIZEN_BUILD_TARGET"
+export FFLAGS="$FFLAGS -DTIZEN_BUILD_TARGET"
+%else
+export CFLAGS="$CFLAGS -DTIZEN_BUILD_EMULATOR"
+export CXXFLAGS="$CXXFLAGS -DTIZEN_BUILD_EMULATOR"
+export FFLAGS="$FFLAGS -DTIZEN_BUILD_EMULATOR"
+%endif
+
+cmake . -DINSTALL_PREFIX=%{_pkg_dir} \
+       -DSYS_ICONS_DIR=%{_sys_icons_dir} \
+       -DSYS_PACKAGES_DIR=%{_sys_packages_dir}
+make %{?jobs:-j%jobs}
+
+%install
+%make_install
+
+%define tizen_sign 1
+%define tizen_sign_base %{_pkg_dir}
+%define tizen_sign_level platform
+%define tizen_author_sign 1
+%define tizen_dist_sign 1
+#%find_lang position-finder-client
+
+%post
+/sbin/ldconfig
+chsmack -a "User::App::Shared" %{_pkg_dir}/res/*.dat
+chmod 666 %{_pkg_dir}/res/*.dat
+
+%postun -p /sbin/ldconfig
+
+%files
+%manifest org.tizen.position-finder-client.manifest
+%{_pkg_dir}/res/*.dat
+%defattr(-,root,root,-)
+%{_pkg_dir}/bin/position-finder-client
+%{_sys_packages_dir}/org.tizen.position-finder-client.xml
+%{_sys_icons_dir}/position_finder_client.png
+%{_pkg_dir}/author-signature.xml
+%{_pkg_dir}/signature1.xml
diff --git a/res/iotcon-test-svr-db-client.dat b/res/iotcon-test-svr-db-client.dat
new file mode 100644 (file)
index 0000000..9d1379a
Binary files /dev/null and b/res/iotcon-test-svr-db-client.dat differ
diff --git a/shared/res/position_finder_client.png b/shared/res/position_finder_client.png
new file mode 100755 (executable)
index 0000000..9765b1b
Binary files /dev/null and b/shared/res/position_finder_client.png differ
diff --git a/src/connectivity.c b/src/connectivity.c
new file mode 100644 (file)
index 0000000..8b29131
--- /dev/null
@@ -0,0 +1,685 @@
+/*
+ * Copyright (c) 2015 - 2016 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 <tizen.h>
+#include <service_app.h>
+#include <iotcon.h>
+#include <Eina.h>
+
+#include "log.h"
+#include "connectivity.h"
+
+#define DOOR_RESOURCE_URI_PREFIX "/door"
+#define DOOR_RESOURCE_TYPE "org.tizen.door"
+
+struct _connectivity_resource_s {
+       char *device_id;
+       char *host_address;
+       char *device_name;
+       char *uri_path;
+       iotcon_presence_h presence;
+       iotcon_remote_resource_h resource;
+};
+
+static struct {
+       Eina_List *list;
+} connectivity_info = {
+       .list = NULL,
+};
+
+static void _response_query_cb(iotcon_remote_resource_h resource, iotcon_error_e err,
+               iotcon_request_type_e request_type, iotcon_response_h response, void *user_data);
+
+static void _observe_cb(iotcon_remote_resource_h resource, iotcon_error_e err, int sequence_number, iotcon_response_h response, void *user_data)
+{
+       int ret = -1;
+       bool opened = false;
+       iotcon_attributes_h attributes = NULL;
+       iotcon_representation_h repr = NULL;
+       iotcon_response_result_e response_result;
+
+       ret_if(IOTCON_ERROR_NONE != err);
+
+       ret = iotcon_response_get_result(response, &response_result);
+       if (IOTCON_ERROR_NONE != ret) {
+               _E("iotcon_response_get_result() Fail(%d)", ret);
+               return;
+       }
+
+       if (IOTCON_RESPONSE_OK != response_result) {
+               _E("_on_response_observe Response error(%d)", response_result);
+               return;
+       }
+
+       ret = iotcon_response_get_representation(response, &repr);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       ret = iotcon_representation_get_attributes(repr, &attributes);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       ret = iotcon_attributes_get_bool(attributes, "opened", &opened);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       if (opened) _I("[Door] opened.");
+       else _I("[Door] closed.");
+}
+
+static void _response_delete_query(iotcon_remote_resource_h resource, iotcon_response_h response, void *user_data)
+{
+       int ret = -1;
+       iotcon_response_result_e response_result;
+
+       ret = iotcon_response_get_result(response, &response_result);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       if (IOTCON_RESPONSE_OK != response_result
+                       && IOTCON_RESPONSE_RESOURCE_DELETED != response_result) {
+               _E("_response_delete_query Response error(%d)", response_result);
+               return;
+       }
+
+       _I("DELETE request was successful");
+       /* FIXME : need to free resources? */
+       iotcon_remote_resource_destroy(resource);
+}
+
+static void _response_post_query(iotcon_remote_resource_h resource, iotcon_response_h response, void *user_data)
+{
+       int ret = -1;
+       char *host = NULL, *created_uri_path = NULL;
+       iotcon_connectivity_type_e connectivity_type;
+       iotcon_response_result_e response_result;
+       iotcon_attributes_h recv_attributes = NULL;
+       iotcon_resource_types_h types = NULL;
+       iotcon_resource_interfaces_h ifaces = NULL;
+       iotcon_remote_resource_h new_door_resource = NULL;
+       iotcon_representation_h recv_repr = NULL;
+
+       ret = iotcon_response_get_result(response, &response_result);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       if (IOTCON_RESPONSE_RESOURCE_CREATED != response_result) {
+               _E("_response_post_query Response error(%d)", response_result);
+               return;
+       }
+
+       _I("POST request was successful");
+
+       ret = iotcon_response_get_representation(response, &recv_repr);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       ret = iotcon_representation_get_attributes(recv_repr, &recv_attributes);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       ret = iotcon_attributes_get_str(recv_attributes, "createduripath", &created_uri_path);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       _D("New resource created : %s", created_uri_path);
+
+       ret = iotcon_remote_resource_get_host_address(resource, &host);
+       if (IOTCON_ERROR_NONE != ret) {
+               _E("iotcon_remote_resource_get_host_address() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_remote_resource_get_connectivity_type(resource, &connectivity_type);
+       if (IOTCON_ERROR_NONE != ret) {
+               _E("iotcon_remote_resource_get_connectivity_type() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_remote_resource_get_types(resource, &types);
+       if (IOTCON_ERROR_NONE != ret) {
+               _E("iotcon_remote_resource_get_types() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_remote_resource_get_interfaces(resource, &ifaces);
+       if (IOTCON_ERROR_NONE != ret) {
+               _E("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_remote_resource_create(host, connectivity_type, created_uri_path,
+                       IOTCON_RESOURCE_SECURE, types, ifaces, &new_door_resource);
+       if (IOTCON_ERROR_NONE != ret) {
+               _E("iotcon_remote_resource_create() Fail(%d)", ret);
+               return;
+       }
+
+       ret = iotcon_remote_resource_delete(new_door_resource, _response_query_cb, NULL);
+       if (IOTCON_ERROR_NONE != ret) {
+               _E("iotcon_remote_resource_delete() Fail(%d)", ret);
+               iotcon_remote_resource_destroy(new_door_resource);
+               return;
+       }
+}
+
+static void _response_put_query(iotcon_remote_resource_h resource, iotcon_response_h response, void *user_data)
+{
+       int ret = -1;
+       iotcon_response_result_e response_result;
+       iotcon_representation_h send_repr = NULL;
+
+       ret = iotcon_response_get_result(response, &response_result);
+       if (IOTCON_ERROR_NONE != ret) {
+               _E("iotcon_response_get_result() Fail(%d)", ret);
+               return;
+       }
+
+       if (IOTCON_RESPONSE_RESOURCE_CHANGED != response_result) {
+               _E("_response_put_query Response error(%d)", response_result);
+               return;
+       }
+
+       _I("PUT request was successful");
+
+       ret = iotcon_representation_create(&send_repr);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       ret = iotcon_remote_resource_post(resource, send_repr, NULL, _response_query_cb, NULL);
+       goto_if(IOTCON_ERROR_NONE != ret, out);
+
+out:
+       iotcon_representation_destroy(send_repr);
+}
+
+static void _response_get_query(iotcon_remote_resource_h resource, iotcon_response_h response, void *user_data)
+{
+       bool opened = true;
+       char *resource_host = NULL;
+       int ret = -1;
+       iotcon_response_result_e response_result;
+       iotcon_representation_h send_repr = NULL;
+       iotcon_representation_h recv_repr = NULL;
+       iotcon_attributes_h send_attributes = NULL;
+       iotcon_attributes_h recv_attributes = NULL;
+
+       ret = iotcon_response_get_result(response, &response_result);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       if (IOTCON_RESPONSE_OK != response_result) {
+               _E("_response_get_query response error(%d)", response_result);
+               return;
+       }
+
+       iotcon_remote_resource_get_host_address(resource, &resource_host);
+       _I("Resource host : %s", resource_host);
+
+       ret = iotcon_response_get_representation(response, &recv_repr);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       ret = iotcon_representation_get_attributes(recv_repr, &recv_attributes);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       ret = iotcon_attributes_get_bool(recv_attributes, "opened", &opened);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       ret = iotcon_representation_create(&send_repr);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       ret = iotcon_attributes_create(&send_attributes);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_attributes_add_bool(send_attributes, "opened", !opened);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_representation_set_attributes(send_repr, send_attributes);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_remote_resource_put(resource, send_repr, NULL, _response_query_cb, NULL);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       iotcon_attributes_destroy(send_attributes);
+       iotcon_representation_destroy(send_repr);
+
+       return;
+
+error:
+       if (send_attributes) iotcon_attributes_destroy(send_attributes);
+       if (send_repr) iotcon_representation_destroy(send_repr);
+
+}
+
+static void _presence_cb(iotcon_presence_h presence, iotcon_error_e err,
+               iotcon_presence_response_h response, void *user_data)
+{
+       int ret = -1;
+       char *host_address = NULL;
+       char *resource_type = NULL;
+       iotcon_presence_result_e result;
+       iotcon_presence_trigger_e trigger;
+       iotcon_connectivity_type_e connectivity_type;
+
+       ret_if(IOTCON_ERROR_NONE != err);
+
+       ret = iotcon_presence_response_get_result(response, &result);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       _I("Presence callback's result : %d", result);
+
+       if (IOTCON_PRESENCE_OK == result) {
+               ret = iotcon_presence_response_get_trigger(response, &trigger);
+               ret_if(IOTCON_ERROR_NONE != ret);
+               _I("trigger : %d", trigger);
+       }
+
+       ret = iotcon_presence_response_get_host_address(response, &host_address);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       ret = iotcon_presence_response_get_connectivity_type(response, &connectivity_type);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       ret = iotcon_presence_response_get_resource_type(response, &resource_type);
+       ret_if(IOTCON_ERROR_NONE != ret);
+
+       _I("Host address : %s", host_address);
+       _I("Resource type : %s", resource_type);
+}
+
+static void _response_query_cb(iotcon_remote_resource_h resource,
+               iotcon_error_e err,
+               iotcon_request_type_e request_type,
+               iotcon_response_h response,
+               void *user_data)
+{
+       ret_if(IOTCON_ERROR_NONE != err);
+
+       _I("Request type[%d]", request_type);
+
+       switch (request_type) {
+       case IOTCON_REQUEST_GET:
+               _response_get_query(resource, response, user_data);
+               break;
+       case IOTCON_REQUEST_PUT:
+               _response_put_query(resource, response, user_data);
+               break;
+       case IOTCON_REQUEST_POST:
+               _response_post_query(resource, response, user_data);
+               break;
+       case IOTCON_REQUEST_DELETE:
+               _response_delete_query(resource, response, user_data);
+               break;
+       default:
+               _E("Invalid request type (%d)", request_type);
+       }
+}
+
+/* return values : -1 error, 0 not exist, 1 exist */
+static int _exist_device_id_in_list(iotcon_remote_resource_h resource)
+{
+       Eina_List *l = NULL, *ln = NULL;
+       connectivity_resource_s *temp = NULL;
+       char *device_id = NULL;
+       int ret = -1;
+
+       retv_if(!resource, -1);
+
+       ret = iotcon_remote_resource_get_device_id(resource, &device_id);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       EINA_LIST_FOREACH_SAFE(connectivity_info.list, l, ln, temp) {
+               continue_if(!temp->device_id);
+               if (!strncmp(temp->device_id, device_id, strlen(device_id))) {
+                       _D("\"%s\" already found. skip !", device_id);
+                       return 1;
+               }
+       }
+
+       _I("Resource[%s] is not in the list", device_id);
+
+       return 0;
+}
+
+static int _add_resource_info_into_list(connectivity_resource_s *info)
+{
+       retv_if(!info, -1);
+       connectivity_info.list = eina_list_append(connectivity_info.list, info);
+       return 0;
+}
+
+#if 0
+static connectivity_resource_s *_pop_device_id_from_list(const char *device_id)
+{
+       Eina_List *l = NULL, *ln = NULL;
+       connectivity_resource_s *temp = NULL;
+       connectivity_resource_s *info = NULL;
+
+       retv_if(!device_id, NULL);
+
+       EINA_LIST_FOREACH_SAFE(connectivity_info.list, l, ln, temp) {
+               continue_if(!temp->device_id);
+               if (!strncmp(temp->device_id, device_id, strlen(device_id))) {
+                       connectivity_info.list = eina_list_remove(connectivity_info.list, temp);
+                       info = temp;
+                       break;
+               }
+       }
+
+       return info;
+}
+#endif
+
+static bool _get_res_iface_cb(const char *string, void *user_data)
+{
+       char *resource_uri_path = user_data;
+       _I("[%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;
+       _I("[%s] resource type : %s", resource_uri_path, string);
+       return IOTCON_FUNC_CONTINUE;
+}
+
+static int _retrieve_resource_info(iotcon_remote_resource_h resource, connectivity_resource_s *info)
+{
+       int ret = -1;
+       char *device_id = NULL;
+       char *host_address = NULL;
+       char *device_name = NULL;
+       char *uri_path = NULL;
+       iotcon_resource_interfaces_h resource_interfaces;
+       iotcon_resource_types_h resource_types;
+       iotcon_connectivity_type_e connectivity_type;
+
+       retv_if(!info, -1);
+
+       ret = iotcon_remote_resource_get_device_id(resource, &device_id);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_remote_resource_get_host_address(resource, &host_address);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_remote_resource_get_device_name(resource, &device_name);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_remote_resource_get_uri_path(resource, &uri_path);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_remote_resource_get_connectivity_type(resource, &connectivity_type);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_resource_interfaces_foreach(resource_interfaces, _get_res_iface_cb, uri_path);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_remote_resource_get_types(resource, &resource_types);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb, uri_path);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_remote_resource_clone(resource, &info->resource);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       _I("Resource Addr[%s/%s] is in Device[%s]", host_address, uri_path, device_name);
+
+       info->device_id = strdup(device_id);
+       goto_if(!info->device_id, error);
+
+       info->host_address = strdup(host_address);
+       goto_if(!info->host_address, error);
+
+       info->device_name = strdup(device_name);
+       goto_if(!info->device_name, error);
+
+       info->uri_path = strdup(uri_path);
+       goto_if(!info->uri_path, error);
+
+       return 0;
+
+error:
+       if (info->resource) {
+               iotcon_remote_resource_destroy(info->resource);
+               info->resource = NULL;
+       }
+       if (info->uri_path) {
+               free(info->uri_path);
+               info->uri_path = NULL;
+       }
+       if (info->device_name) {
+               free(info->device_name);
+               info->device_name = NULL;
+       }
+       if (info->host_address) {
+               free(info->host_address);
+               info->host_address = NULL;
+       }
+       if (info->device_id) {
+               free(info->device_id);
+               info->device_id = NULL;
+       }
+
+       return -1;
+}
+
+static void _free_resource_info(connectivity_resource_s *info)
+{
+       ret_if(!info);
+
+       if (info->presence)
+               iotcon_remove_presence_cb(info->presence);
+
+       if (info->resource)
+               iotcon_remote_resource_destroy(info->resource);
+
+       if (info->uri_path)
+               free(info->uri_path);
+
+       if (info->device_name)
+               free(info->device_name);
+
+       if (info->host_address)
+               free(info->host_address);
+
+       if (info->device_id)
+               free(info->device_id);
+
+       free(info);
+}
+
+static void _free_resource_info_list(void)
+{
+       connectivity_resource_s *info = NULL;
+
+       EINA_LIST_FREE(connectivity_info.list, info) {
+               _free_resource_info(info);
+       }
+}
+
+
+static int _register_presence(iotcon_remote_resource_h resource, connectivity_resource_s *info)
+{
+       int ret = -1;
+       char *resource_host = NULL;
+       iotcon_connectivity_type_e connectivity_type;
+
+       ret = iotcon_remote_resource_get_host_address(resource, &resource_host);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_remote_resource_get_connectivity_type(resource, &connectivity_type);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       /* FIXME : We need to remove presence_cb when not needed */
+       ret = iotcon_add_presence_cb(resource_host,
+                       connectivity_type,
+                       DOOR_RESOURCE_TYPE,
+                       _presence_cb,
+                       NULL,
+                       &info->presence);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       return 0;
+}
+
+#if 0
+static void _unregister_presence(connectivity_resource_s *info)
+{
+       ret_if(!info);
+       iotcon_remove_presence_cb(info->presence);
+       info->presence = NULL;
+}
+#endif
+
+static int _register_observe(connectivity_resource_s *info)
+{
+       int ret = -1;
+
+       /* resource is cloned data */
+       ret = iotcon_remote_resource_observe_register(info->resource,
+                       IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER,
+                       NULL,
+                       _observe_cb,
+                       NULL);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       return 0;
+}
+
+#if 0
+static void _unregister_observe(connectivity_resource_s *info)
+{
+       ret_if(!info);
+       iotcon_remote_resource_observe_deregister(info->resource);
+}
+#endif
+
+static int _request_query(connectivity_resource_s *info)
+{
+       int ret = -1;
+       iotcon_query_h query = NULL;
+
+       ret = iotcon_query_create(&query);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_query_add(query, "query_key", "query_value");
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_remote_resource_get(info->resource, query, _response_query_cb, NULL);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       iotcon_query_destroy(query);
+       return 0;
+
+error:
+       iotcon_query_destroy(query);
+       return -1;
+}
+
+static bool _found_resource_cb(iotcon_remote_resource_h resource, iotcon_error_e result, void *user_data)
+{
+       int ret = -1;
+       connectivity_resource_s *info = NULL;
+
+       _D("HELLO");
+       /* We should know where we get wrong results */
+       /* Can attackers send wrong messages to stop this? */
+       retv_if(IOTCON_ERROR_NONE != result, IOTCON_FUNC_STOP);
+       if (!resource) return IOTCON_FUNC_CONTINUE;
+
+       _I("Resource is found");
+
+       ret = _exist_device_id_in_list(resource);
+       retv_if(-1 == ret, IOTCON_FUNC_CONTINUE);
+       if (1 == ret) return IOTCON_FUNC_CONTINUE;
+
+       info = calloc(1, sizeof(connectivity_resource_s));
+       retv_if(!info, IOTCON_FUNC_CONTINUE);
+
+       ret = _add_resource_info_into_list(info);
+       goto_if(-1 == ret, error);
+
+       ret = _retrieve_resource_info(resource, info);
+       goto_if(-1 == ret, error);
+
+       /* FIXME : We have to develop these routines more for meaningful usecases */
+       /* We can think several scenarios like intentionally closing servers. */
+       ret = _register_presence(resource, info);
+       goto_if(-1 == ret, error);
+
+       if (-1 == _register_observe(info)) {
+               _E("Cannot observe [%s:%s:%s:%s]", info->device_id, info->host_address, info->device_name, info->uri_path);
+       }
+
+       if (-1 == _request_query(info)) {
+               _E("Cannot request query");
+       }
+
+       return IOTCON_FUNC_CONTINUE;
+
+error:
+       connectivity_info.list = eina_list_remove(connectivity_info.list, info);
+       if (info) _free_resource_info(info);
+       return IOTCON_FUNC_CONTINUE;
+}
+
+int connectivity_init(void)
+{
+       int ret = -1;
+
+       ret = iotcon_initialize("/usr/apps/org.tizen.position-finder-client/res/iotcon-test-svr-db-client.dat");
+       if (IOTCON_ERROR_NONE != ret) {
+               _E("iotcon_initialize() Fail(%d)", ret);
+               return -1;
+       }
+
+       return 0;
+}
+
+int connectivity_fini(void)
+{
+       /* deinitialize iotcon */
+       _free_resource_info_list();
+       iotcon_deinitialize();
+       return 0;
+}
+
+int connectivity_find_resource(void)
+{
+       int ret = -1;
+       iotcon_query_h query = NULL;
+
+       _D("HELLO");
+
+       ret = iotcon_query_create(&query);
+       retv_if(IOTCON_ERROR_NONE != ret, -1);
+
+       ret = iotcon_query_set_resource_type(query, DOOR_RESOURCE_TYPE);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS,
+                       IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP,
+                       query,
+                       _found_resource_cb,
+                       NULL);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       _D("HELLO");
+       iotcon_query_destroy(query);
+       return 0;
+
+error:
+       iotcon_query_destroy(query);
+       return -1;
+}
diff --git a/src/control.c b/src/control.c
new file mode 100644 (file)
index 0000000..d308805
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2015 - 2016 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 <tizen.h>
+#include <service_app.h>
+#include <iotcon.h>
+#include <Eina.h>
+
+#include "log.h"
+#include "connectivity.h"
+
+static void _start_internal_function(void);
+static void _stop_internal_function(void);
+
+bool service_app_create(void *data)
+{
+       int ret = -1;
+
+       _start_internal_function();
+
+       connectivity_find_resource();
+
+    return true;
+}
+
+void service_app_terminate(void *data)
+{
+       _stop_internal_function();
+}
+
+void service_app_control(app_control_h app_control, void *data)
+{
+    // Todo: add your code here.
+}
+
+static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
+{
+       /*APP_EVENT_LANGUAGE_CHANGED*/
+}
+
+static void service_app_region_changed(app_event_info_h event_info, void *user_data)
+{
+       /*APP_EVENT_REGION_FORMAT_CHANGED*/
+}
+
+static void service_app_low_battery(app_event_info_h event_info, void *user_data)
+{
+       /*APP_EVENT_LOW_BATTERY*/
+}
+
+static void service_app_low_memory(app_event_info_h event_info, void *user_data)
+{
+       /*APP_EVENT_LOW_MEMORY*/
+}
+
+int main(int argc, char* argv[])
+{
+    char ad[50] = {0, };
+       service_app_lifecycle_callback_s event_callback;
+       app_event_handler_h handlers[5] = {NULL, };
+       int ret = 0;
+
+       event_callback.create = service_app_create;
+       event_callback.terminate = service_app_terminate;
+       event_callback.app_control = service_app_control;
+
+       service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
+       service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
+       service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
+       service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
+
+       ret = service_app_main(argc, argv, &event_callback, ad);
+
+       return ret;
+}
+
+/* Do not modify codes under this comment */
+static void _start_internal_function(void)
+{
+       int ret = -1;
+       ret = connectivity_init();
+       ret_if(IOTCON_ERROR_NONE != ret);
+}
+
+static void _stop_internal_function(void)
+{
+       _I("Terminating...");
+       connectivity_fini();
+}
diff --git a/tizen-manifest.xml b/tizen-manifest.xml
new file mode 100644 (file)
index 0000000..f32f3c9
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<manifest xmlns="http://tizen.org/ns/packages" api-version="3.0" package="org.tizen.position-finder-client" version="1.0.0">
+    <profile name="mobile"/>
+    <service-application appid="org.tizen.position-finder-client" auto-restart="false" exec="position-finder-client" multiple="false" nodisplay="false" on-boot="false" taskmanage="true" type="capp">
+        <label>Position Filnder Client</label>
+        <icon>position_finder_client.png</icon>
+    </service-application>
+    <privileges>
+        <privilege>http://tizen.org/privilege/network.get</privilege>
+               <privilege>http://tizen.org/privilege/internet</privilege>
+    </privileges>
+</manifest>