--- /dev/null
+YeongKyoon Lee <yeongkyoon.lee@samsung.com>
+DaiYoung Kim <daiyoung777.kim@samsung.com>
+Jinhyung Choi <jinhyung2.choi@samsung.com>
+SeokYeon Hwang <syeon.hwang@samsung.com>
+SangJin Kim <sangjin3.kim@samsung.com>
+KiTae Kim <kt920.kim@samsung.com>
+JinHyung Jo <jinhyung.jo@samsung.com>
+SungMin Ha <sungmin82.ha@samsung.com>
+MunKyu Im <munkyu.im@samsung.com>
+JiHye Kim <jihye1128.kim@samsung.com>
+GiWoong Kim <giwoong.kim@samsung.com>
+SooYoung Ha <yoosah.ha@samsnung.com>
+HyunGoo Kang <hyungoo1.kang@samsung.com>
+
--- /dev/null
+cmake_minimum_required(VERSION 2.6)
+project(emuld CXX)
+
+# to install pkgconfig setup file.
+SET(EXEC_PREFIX "\${prefix}")
+#SET(LIBDIR "\${prefix}/lib")
+SET(INCLUDEDIR "\${prefix}/include")
+SET(VERSION 1.1)
+
+set(PROJECT_MAJOR_VERSION "1")
+set(PROJECT_MINOR_VERSION "1")
+set(PROJECT_RELEASE_VERSION "0")
+set(CMAKE_VERBOSE_MAKEFILE OFF)
+
+IF("$ENV{CFLAGS}" MATCHES "-DMOBILE")
+ OPTION(USE_D_MOBILE "Use Mobile Def" ON)
+ENDIF()
+
+IF("$ENV{CFLAGS}" MATCHES "-DWEARABLE")
+ OPTION(USE_D_WEARABLE "Use Wearable Def" ON)
+ENDIF()
+
+SET(SRCS
+ src/emuld.cpp
+ src/evdi.cpp
+ src/emuld_proc.cpp
+ src/client.cpp
+ src/device.cpp
+)
+
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+
+INCLUDE(FindPkgConfig)
+
+SET(PKG_MODULE
+ vconf
+ deviced
+ dlog
+)
+
+IF(USE_D_MOBILE)
+ SET(PKG_MODULE ${PKG_MODULE}
+ pmapi
+ )
+ENDIF(USE_D_MOBILE)
+
+pkg_check_modules(PKGS REQUIRED ${PKG_MODULE})
+
+IF(USE_D_MOBILE)
+ ADD_DEFINITIONS("-DMOBILE")
+ELSEIF(USE_D_WEARABLE)
+ ADD_DEFINITIONS("-DWEARABLE")
+ENDIF()
+
+ADD_DEFINITIONS("-DENABLE_DLOG_OUT")
+ADD_DEFINITIONS(-Wall -O3 -omit-frame-pointer)
+
+ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS})
+
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${PKGS_LDFLAGS} -lpthread)
+
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin)
--- /dev/null
+Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.\r
+\r
+ Apache License\r
+ Version 2.0, January 2004\r
+ http://www.apache.org/licenses/\r
+\r
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\r
+\r
+ 1. Definitions.\r
+\r
+ "License" shall mean the terms and conditions for use, reproduction,\r
+ and distribution as defined by Sections 1 through 9 of this document.\r
+\r
+ "Licensor" shall mean the copyright owner or entity authorized by\r
+ the copyright owner that is granting the License.\r
+\r
+ "Legal Entity" shall mean the union of the acting entity and all\r
+ other entities that control, are controlled by, or are under common\r
+ control with that entity. For the purposes of this definition,\r
+ "control" means (i) the power, direct or indirect, to cause the\r
+ direction or management of such entity, whether by contract or\r
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the\r
+ outstanding shares, or (iii) beneficial ownership of such entity.\r
+\r
+ "You" (or "Your") shall mean an individual or Legal Entity\r
+ exercising permissions granted by this License.\r
+\r
+ "Source" form shall mean the preferred form for making modifications,\r
+ including but not limited to software source code, documentation\r
+ source, and configuration files.\r
+\r
+ "Object" form shall mean any form resulting from mechanical\r
+ transformation or translation of a Source form, including but\r
+ not limited to compiled object code, generated documentation,\r
+ and conversions to other media types.\r
+\r
+ "Work" shall mean the work of authorship, whether in Source or\r
+ Object form, made available under the License, as indicated by a\r
+ copyright notice that is included in or attached to the work\r
+ (an example is provided in the Appendix below).\r
+\r
+ "Derivative Works" shall mean any work, whether in Source or Object\r
+ form, that is based on (or derived from) the Work and for which the\r
+ editorial revisions, annotations, elaborations, or other modifications\r
+ represent, as a whole, an original work of authorship. For the purposes\r
+ of this License, Derivative Works shall not include works that remain\r
+ separable from, or merely link (or bind by name) to the interfaces of,\r
+ the Work and Derivative Works thereof.\r
+\r
+ "Contribution" shall mean any work of authorship, including\r
+ the original version of the Work and any modifications or additions\r
+ to that Work or Derivative Works thereof, that is intentionally\r
+ submitted to Licensor for inclusion in the Work by the copyright owner\r
+ or by an individual or Legal Entity authorized to submit on behalf of\r
+ the copyright owner. For the purposes of this definition, "submitted"\r
+ means any form of electronic, verbal, or written communication sent\r
+ to the Licensor or its representatives, including but not limited to\r
+ communication on electronic mailing lists, source code control systems,\r
+ and issue tracking systems that are managed by, or on behalf of, the\r
+ Licensor for the purpose of discussing and improving the Work, but\r
+ excluding communication that is conspicuously marked or otherwise\r
+ designated in writing by the copyright owner as "Not a Contribution."\r
+\r
+ "Contributor" shall mean Licensor and any individual or Legal Entity\r
+ on behalf of whom a Contribution has been received by Licensor and\r
+ subsequently incorporated within the Work.\r
+\r
+ 2. Grant of Copyright License. Subject to the terms and conditions of\r
+ this License, each Contributor hereby grants to You a perpetual,\r
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable\r
+ copyright license to reproduce, prepare Derivative Works of,\r
+ publicly display, publicly perform, sublicense, and distribute the\r
+ Work and such Derivative Works in Source or Object form.\r
+\r
+ 3. Grant of Patent License. Subject to the terms and conditions of\r
+ this License, each Contributor hereby grants to You a perpetual,\r
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable\r
+ (except as stated in this section) patent license to make, have made,\r
+ use, offer to sell, sell, import, and otherwise transfer the Work,\r
+ where such license applies only to those patent claims licensable\r
+ by such Contributor that are necessarily infringed by their\r
+ Contribution(s) alone or by combination of their Contribution(s)\r
+ with the Work to which such Contribution(s) was submitted. If You\r
+ institute patent litigation against any entity (including a\r
+ cross-claim or counterclaim in a lawsuit) alleging that the Work\r
+ or a Contribution incorporated within the Work constitutes direct\r
+ or contributory patent infringement, then any patent licenses\r
+ granted to You under this License for that Work shall terminate\r
+ as of the date such litigation is filed.\r
+\r
+ 4. Redistribution. You may reproduce and distribute copies of the\r
+ Work or Derivative Works thereof in any medium, with or without\r
+ modifications, and in Source or Object form, provided that You\r
+ meet the following conditions:\r
+\r
+ (a) You must give any other recipients of the Work or\r
+ Derivative Works a copy of this License; and\r
+\r
+ (b) You must cause any modified files to carry prominent notices\r
+ stating that You changed the files; and\r
+\r
+ (c) You must retain, in the Source form of any Derivative Works\r
+ that You distribute, all copyright, patent, trademark, and\r
+ attribution notices from the Source form of the Work,\r
+ excluding those notices that do not pertain to any part of\r
+ the Derivative Works; and\r
+\r
+ (d) If the Work includes a "NOTICE" text file as part of its\r
+ distribution, then any Derivative Works that You distribute must\r
+ include a readable copy of the attribution notices contained\r
+ within such NOTICE file, excluding those notices that do not\r
+ pertain to any part of the Derivative Works, in at least one\r
+ of the following places: within a NOTICE text file distributed\r
+ as part of the Derivative Works; within the Source form or\r
+ documentation, if provided along with the Derivative Works; or,\r
+ within a display generated by the Derivative Works, if and\r
+ wherever such third-party notices normally appear. The contents\r
+ of the NOTICE file are for informational purposes only and\r
+ do not modify the License. You may add Your own attribution\r
+ notices within Derivative Works that You distribute, alongside\r
+ or as an addendum to the NOTICE text from the Work, provided\r
+ that such additional attribution notices cannot be construed\r
+ as modifying the License.\r
+\r
+ You may add Your own copyright statement to Your modifications and\r
+ may provide additional or different license terms and conditions\r
+ for use, reproduction, or distribution of Your modifications, or\r
+ for any such Derivative Works as a whole, provided Your use,\r
+ reproduction, and distribution of the Work otherwise complies with\r
+ the conditions stated in this License.\r
+\r
+ 5. Submission of Contributions. Unless You explicitly state otherwise,\r
+ any Contribution intentionally submitted for inclusion in the Work\r
+ by You to the Licensor shall be under the terms and conditions of\r
+ this License, without any additional terms or conditions.\r
+ Notwithstanding the above, nothing herein shall supersede or modify\r
+ the terms of any separate license agreement you may have executed\r
+ with Licensor regarding such Contributions.\r
+\r
+ 6. Trademarks. This License does not grant permission to use the trade\r
+ names, trademarks, service marks, or product names of the Licensor,\r
+ except as required for reasonable and customary use in describing the\r
+ origin of the Work and reproducing the content of the NOTICE file.\r
+\r
+ 7. Disclaimer of Warranty. Unless required by applicable law or\r
+ agreed to in writing, Licensor provides the Work (and each\r
+ Contributor provides its Contributions) on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\r
+ implied, including, without limitation, any warranties or conditions\r
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\r
+ PARTICULAR PURPOSE. You are solely responsible for determining the\r
+ appropriateness of using or redistributing the Work and assume any\r
+ risks associated with Your exercise of permissions under this License.\r
+\r
+ 8. Limitation of Liability. In no event and under no legal theory,\r
+ whether in tort (including negligence), contract, or otherwise,\r
+ unless required by applicable law (such as deliberate and grossly\r
+ negligent acts) or agreed to in writing, shall any Contributor be\r
+ liable to You for damages, including any direct, indirect, special,\r
+ incidental, or consequential damages of any character arising as a\r
+ result of this License or out of the use or inability to use the\r
+ Work (including but not limited to damages for loss of goodwill,\r
+ work stoppage, computer failure or malfunction, or any and all\r
+ other commercial damages or losses), even if such Contributor\r
+ has been advised of the possibility of such damages.\r
+\r
+ 9. Accepting Warranty or Additional Liability. While redistributing\r
+ the Work or Derivative Works thereof, You may choose to offer,\r
+ and charge a fee for, acceptance of support, warranty, indemnity,\r
+ or other liability obligations and/or rights consistent with this\r
+ License. However, in accepting such obligations, You may act only\r
+ on Your own behalf and on Your sole responsibility, not on behalf\r
+ of any other Contributor, and only if You agree to indemnify,\r
+ defend, and hold each Contributor harmless for any liability\r
+ incurred by, or claims asserted against, such Contributor by reason\r
+ of your accepting any such warranty or additional liability.\r
+\r
+ END OF TERMS AND CONDITIONS\r
+\r
+ APPENDIX: How to apply the Apache License to your work.\r
+\r
+ To apply the Apache License to your work, attach the following\r
+ boilerplate notice, with the fields enclosed by brackets "[]"\r
+ replaced with your own identifying information. (Don't include\r
+ the brackets!) The text should be enclosed in the appropriate\r
+ comment syntax for the file format. We also recommend that a\r
+ file or class name and description of purpose be included on the\r
+ same "printed page" as the copyright notice for easier\r
+ identification within third-party archives.\r
+\r
+ Copyright [yyyy] [name of copyright owner]\r
+\r
+ Licensed under the Apache License, Version 2.0 (the "License");\r
+ you may not use this file except in compliance with the License.\r
+ You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+\r
+\r
--- /dev/null
+Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
--- /dev/null
+emuld (0.2.20) unstable; urgency=low
+
+ * add checking routine 'recv' function's return value
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.20
+
+ -- Jihye kim <jihye1128.kim@samsung.com> Mon, 04 Nov 2012 20:00:18 +0900
+
+emuld (0.2.18) unstable; urgency=low
+
+ * fixed hang up when sending packet vmodem to eventinjector
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.18
+
+ -- Sungmin ha <sungmin82.ha@samsung.com> Wed, 12 Sep 2012 20:00:18 +0900
+
+emuld (0.2.17) unstable; urgency=low
+
+ * modified for scm build
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.17
+
+ -- Sungmin ha <sungmin82.ha@samsung.com> Thu, 09 Aug 2012 21:00:18 +0900
+
+emuld (0.2.15) unstable; urgency=low
+
+ * Permission of sdcard partition changed from 755 to 777.
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.15
+
+ -- Sungmin ha <sungmin82.ha@samsung.com> Wed, 11 Jul 2012 19:38:44 +0900
+
+emuld (0.2.14) unstable; urgency=low
+
+ * modified for checking resource busy when umount sdcard
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.14
+
+ -- Sungmin ha <sungmin82.ha@samsung.com> Thu, 05 Jul 2012 21:03:00 +0900
+
+emuld (0.2.12) unstable; urgency=low
+
+ * modified for checking return value of fopen
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.12
+
+ -- Sungmin ha <sungmin82.ha@samsung.com> Fri, 22 Jun 2012 18:23:00 +0900
+
+emuld (0.2.10) unstable; urgency=low
+
+ * deleted process kill when umount
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.10
+
+ -- Sungmin ha <sungmin82.ha@samsung.com> Thu, 15 Jun 2012 16:16:00 +0900
+
+emuld (0.2.9) unstable; urgency=low
+
+ * modified vconf key path for sdcard supporting
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.9
+
+ -- Sungmin ha <sungmin82.ha@samsung.com> Thu, 14 Jun 2012 16:04:00 +0900
+
+emuld (0.2.8) unstable; urgency=low
+
+ * added sdcard mount/umount handling
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.8
+
+ -- Sungmin ha <sungmin82.ha@samsung.com> Tue, 29 May 2012 18:47:00 +0900
+
+emuld (0.2.7) unstable; urgency=low
+
+ * added exception handling
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.7
+
+ -- Sungmin ha <sungmin82.ha@samsung.com> Mon, 14 May 2012 18:10:00 +0900
+
+emuld (0.2.6) unstable; urgency=low
+
+ * modified how to get host ip
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.6
+
+ -- Sungmin ha <sungmin82.ha@samsung.com> Fri, 11 May 2012 18:05:00 +0900
+
+emuld (0.2.5) unstable; urgency=low
+
+ * add setting location replay and modify nfc.
+ * need sinchronization with event injector
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.5
+
+ -- Jihye kim <jihye1128.kim@samsung.com> Thu, 10 May 2012 11:30:00 +0900
+
+emuld (0.2.4) unstable; urgency=low
+
+ * back to the 0.2.2
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.4
+
+ -- Jihye kim <jihye1128.kim@samsung.com> Thu, 4 May 2012 11:50:00 +0900
+
+emuld (0.2.3) unstable; urgency=low
+
+ * Add making 'sdkMsg' file during post install.
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.3
+
+ -- Jihye kim <jihye1128.kim@samsung.com> Wen, 02 May 2012 14:00:00 +0900
+
+emuld (0.2.2) unstable; urgency=low
+
+ * Update changelog version.
+ * Git: slp/pkgs/e/emulator-daemon
+ * Tag: emuld_0.2.2
+
+ -- Kitae kim <kt920.kim@samsung.com> Thu, 27 Mar 2012 20:00:00 +0900
+
+emuld (0.2.1) unstable; urgency=low
+
+ * Initial Release.
+
+ -- Sungmin Ha <sungmin82.ha@samsung.com> Fri, 18 Nov 2011 09:29:52 +0900
--- /dev/null
+Source: emuld
+Section: misc
+Priority: extra
+Maintainer:Sungmin Ha <sungmin82.ha@samsung.com>,Jihye Kim <jihye1128.kim@samsung.com>,Yeongkyoon Lee <yeongkyoon.lee@samsung.com>
+Standards-Version: 0.2.20
+
+Package: emuld
+Section: misc
+Architecture: i386
+Description: emulator daemon for sdb
--- /dev/null
+usr/bin
+usr/sbin
--- /dev/null
+CMakeLists.txt
--- /dev/null
+/usr/bin/emuld
--- /dev/null
+#!/bin/sh
+chmod 777 /usr/bin/emuld
--- /dev/null
+#!/usr/bin/make -f
+# -*- makefile -*-
+# Sample debian/rules that uses debhelper.
+# This file was originally written by Joey Hess and Craig Small.
+# As a special exception, when this file is copied by dh-make into a
+# dh-make output file, you may use that output file without restriction.
+# This special exception was added by Craig Small in version 0.37 of dh-make.
+
+# Uncomment this to turn on verbose mode.
+#export DH_VERBOSE=1
+
+CFLAGS ?= -Wall -g
+CXXFLAGS ?= -Wall -g
+LDFLAGS ?=
+PREFIX ?= /usr
+DATADIR ?= /opt
+
+ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
+ CFLAGS += -O0
+ CXXFLAGS += -O0
+else
+ CFLAGS += -O2
+ CXXFLAGS += -O2
+endif
+
+LDFLAGS += -Wl,--rpath=$(PREFIX)/lib -Wl,--as-needed
+
+configure: configure-stamp
+configure-stamp:
+ dh_testdir
+ # Add here commands to configure the package.
+ CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" cmake . -DCMAKE_INSTALL_PREFIX=$(PREFIX)
+
+ touch configure-stamp
+
+build: build-stamp
+
+build-stamp: configure-stamp
+ dh_testdir
+
+ # Add here commands to compile the package.
+ $(MAKE)
+ #docbook-to-man debian/wavplayer.sgml > wavplayer.1
+
+ for f in `find $(CURDIR)/debian/ -name "*.in"`; do \
+ cat $$f > $${f%.in}; \
+ sed -i -e "s#@PREFIX@#$(PREFIX)#g" $${f%.in}; \
+ sed -i -e "s#@DATADIR@#$(DATADIR)#g" $${f%.in}; \
+ done
+
+
+ touch $@
+
+clean:
+ dh_testdir
+ dh_testroot
+ rm -f build-stamp configure-stamp
+
+ # Add here commands to clean up after the build process.
+ -$(MAKE) clean
+ rm -rf CMakeCache.txt
+ rm -rf CMakeFiles
+ rm -rf cmake_install.cmake
+ rm -rf Makefile
+ rm -rf install_manifest.txt
+# rm -rf *.so
+
+ for f in `find $(CURDIR)/debian/ -name "*.in"`; do \
+ rm -f $${f%.in}; \
+ done
+
+ dh_clean
+
+install: build
+ dh_testdir
+ dh_testroot
+ dh_clean -k
+ dh_installdirs
+
+ # Add here commands to install the package into debian/wavplayer.
+ $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
+
+
+# Build architecture-independent files here.
+binary-indep: build install
+# We have nothing to do by default.
+
+# Build architecture-dependent files here.
+binary-arch: build install
+ dh_testdir
+ dh_testroot
+ dh_installchangelogs
+ dh_installdocs
+ dh_installexamples
+ dh_install --sourcedir=debian/tmp
+# dh_installmenu
+# dh_installdebconf
+# dh_installlogrotate
+# dh_installemacsen
+# dh_installpam
+# dh_installmime
+# dh_python
+# dh_installinit
+# dh_installcron
+# dh_installinfo
+ dh_installman
+ dh_link
+ dh_strip
+ dh_compress
+ dh_fixperms
+# dh_perl
+ dh_makeshlibs
+ dh_installdeb
+ dh_shlibdeps
+ dh_gencontrol
+ dh_md5sums
+ dh_builddeb
+
+binary: binary-indep binary-arch
+.PHONY: build clean binary-indep binary-arch binary install configure
--- /dev/null
+[Unit]
+Description=Daemon for emulator
+After=system-server.service
+Requires=system-server.service
+
+[Service]
+Type=simple
+ExecStart=/usr/bin/emuld
+
+[Install]
+WantedBy=emulator.target
+
--- /dev/null
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Jinhyung Choi <jinhyung2.choi@samsnung.com>
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * Daiyoung Kim <daiyoung777.kim@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+
+#ifndef __emuld_h__
+#define __emuld_h__
+
+/* header files */
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/epoll.h>
+#include <arpa/inet.h>
+#include <signal.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/reboot.h>
+#include <stdarg.h>
+#include <sys/time.h>
+#include <pthread.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <assert.h>
+#include <errno.h>
+#include <sys/mount.h>
+#include <stdbool.h>
+#include <fcntl.h>
+#include <queue>
+#include <map>
+
+#include "emuld_common.h"
+#include "evdi_protocol.h"
+#include "evdi.h"
+
+/* definition */
+#define MAX_CLIENT 10000
+#define MAX_EVENTS 10000
+#define MAX_GETCNT 10
+#define DEFAULT_PORT 3577
+#ifdef MOBILE
+#define VMODEM_PORT 3578
+#endif
+#ifdef WEARABLE
+#define PEDOMETER_PORT 3600
+#endif
+#define SRV_IP "10.0.2.2"
+#define ID_SIZE 10
+#define HEADER_SIZE 4
+#define EMD_DEBUG
+#define POWEROFF_DURATION 2
+
+#define SUSPEND_UNLOCK 0
+#define SUSPEND_LOCK 1
+
+#define SDB_PORT_FILE "/opt/home/sdb_port.txt"
+
+enum
+{
+ fdtype_server = 0,
+ fdtype_device = 1,
+ fdtype_vmodem = 2,
+ fdtype_pedometer = 3,
+ fdtype_ij = 4,
+ fdtype_sensor = 5, //udp
+ fdtype_max = 6
+};
+
+extern pthread_t tid[MAX_CLIENT + 1];
+extern struct sockaddr_in si_sensord_other;
+extern int g_fd[fdtype_max];
+
+#if defined(ENABLE_DLOG_OUT)
+# define LOG_TAG "EMULATOR_DAEMON"
+# include <dlog/dlog.h>
+# define LOGINFO LOGI
+# define LOGERR LOGE
+# define LOGDEBUG LOGD
+#else
+# define LOGINFO(fmt, arg...) printf(fmt, arg...)
+# define LOGERR(fmt, arg...) printf(fmt, arg...)
+# define LOGDEBUG(fmt, arg...) printf(fmt, arg...)
+#endif
+
+#define IJTYPE_TELEPHONY "telephony"
+#define IJTYPE_PEDOMETER "pedometer"
+#define IJTYPE_SDCARD "sdcard"
+#define IJTYPE_SUSPEND "suspend"
+
+int parse_val(char *buff, unsigned char data, char *parsbuf);
+
+bool epoll_ctl_add(const int fd);
+
+void userpool_add(int cli_fd, unsigned short cli_port, const int fdtype);
+void userpool_delete(int cli_fd);
+
+bool is_vm_connected(void);
+#ifdef CONFIG_VMODEM
+void set_vm_connect_status(const int v);
+void* init_vm_connect(void* data);
+#endif
+
+void set_pedometer_connect_status(const int v);
+bool is_pedometer_connected(void);
+void* init_pedometer_connect(void* data);
+
+void udp_init(void);
+
+void systemcall(const char* param);
+
+int powerdown_by_force(void);
+// location
+void setting_location(char* databuf);
+
+#include <map>
+
+typedef unsigned short CliSN;
+
+struct Cli
+{
+ Cli(CliSN clisn, int fdtype, int fd, unsigned short port) :
+ clisn(clisn), fdtype(fdtype), sockfd(fd), cli_port(port) {}
+
+ CliSN clisn;
+ int fdtype;
+ int sockfd; /* client socket fds */
+ unsigned short cli_port; /* client connection port */
+};
+
+typedef std::map<CliSN, Cli*> CliMap;
+
+void clipool_add(int fd, unsigned short port, const int fdtype);
+void clipool_delete(int fd);
+void close_cli(int cli_fd);
+
+Cli* find_cli(const int fd);
+bool send_to_cli(const int fd, char* data, const int len);
+bool send_to_all_ij(char* data, const int len);
+bool is_ij_exist();
+void stop_listen(void);
+
+struct fd_info
+{
+ fd_info() : fd(-1){}
+ int fd;
+ int fdtype;
+};
+
+struct ijcommand
+{
+ enum { CMD_SIZE = 48 };
+ ijcommand() : data(NULL)
+ {
+ memset(cmd, 0, CMD_SIZE);
+ }
+ ~ijcommand()
+ {
+ if (data)
+ {
+ free(data);
+ data = NULL;
+ }
+ }
+ char cmd[CMD_SIZE];
+ char* data;
+ fd_info fdinfo;
+
+ LXT_MESSAGE msg;
+};
+
+void process_evdi_command(ijcommand* ijcmd);
+bool read_ijcmd(const int fd, ijcommand* ijcmd);
+
+void* setting_device(void* data);
+
+// msg proc
+bool msgproc_telephony(const int sockfd, ijcommand* ijcmd, const bool is_evdi);
+bool msgproc_pedometer(const int sockfd, ijcommand* ijcmd, const bool is_evdi);
+bool msgproc_sensor(const int sockfd, ijcommand* ijcmd, const bool is_evdi);
+bool msgproc_location(const int sockfd, ijcommand* ijcmd, const bool is_evdi);
+bool msgproc_system(const int sockfd, ijcommand* ijcmd, const bool is_evdi);
+bool msgproc_sdcard(const int sockfd, ijcommand* ijcmd, const bool is_evdi);
+
+#endif //__emuld_h__
--- /dev/null
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Jinhyung Choi <jinhyung2.choi@samsnung.com>
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * Daiyoung Kim <daiyoung777.kim@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+
+#ifndef __emuld_common_h__
+#define __emuld_common_h__
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <pthread.h>
+
+// define group id
+// return value to the injector
+#define STATUS 15
+
+// define action id
+#define BATTERY_LEVEL 100
+#define BATTERY_CHARGER 101
+#define USB_STATUS 102
+#define EARJACK_STATUS 103
+#define RSSI_LEVEL 104
+
+#define ACCEL_VALUE 110
+#define GYRO_VALUE 111
+#define MAG_VALUE 112
+#define LIGHT_VALUE 113
+#define PROXI_VALUE 114
+#define MOTION_VALUE 115
+
+#define LOCATION_STATUS 120
+
+
+#define PATH_SENSOR_ACCEL_XYZ "/sys/devices/virtual/sensor/accel/xyz"
+#define PATH_SENSOR_PROXI_VO "/sys/devices/virtual/sensor/proxi/vo"
+#define PATH_SENSOR_LIGHT_ADC "/sys/devices/virtual/sensor/light/adc"
+#define PATH_SENSOR_GYRO_X_RAW "/sys/devices/virtual/sensor/gyro/gyro_x_raw"
+#define PATH_SENSOR_GYRO_Y_RAW "/sys/devices/virtual/sensor/gyro/gyro_y_raw"
+#define PATH_SENSOR_GYRO_Z_RAW "/sys/devices/virtual/sensor/gyro/gyro_z_raw"
+#define PATH_SENSOR_GEO_TESLA "/sys/devices/virtual/sensor/geo/tesla"
+#define PATH_SENSOR_GYRO_X_RAW "/sys/devices/virtual/sensor/gyro/gyro_x_raw"
+#define PATH_SENSOR_GYRO_Y_RAW "/sys/devices/virtual/sensor/gyro/gyro_y_raw"
+#define PATH_SENSOR_GYRO_Z_RAW "/sys/devices/virtual/sensor/gyro/gyro_z_raw"
+
+#define PATH_BATTERY_CAPACITY "sys/class/power_supply/battery/capacity"
+#define PATH_BATTERY_CHARGER_ON "/sys/devices/platform/jack/charger_online"
+#define PATH_BATTERY_CHARGE_FULL "/sys/class/power_supply/battery/charge_full"
+#define PATH_BATTERY_CHARGE_NOW "/sys/class/power_supply/battery/charge_now"
+
+#define PATH_JACK_EARJACK "/sys/devices/platform/jack/earjack_online"
+#define PATH_JACK_USB "/sys/devices/platform/jack/usb_online"
+
+struct LXT_MESSAGE// lxt_message
+{
+ unsigned short length;
+ unsigned char group;
+ unsigned char action;
+ void *data;
+};
+
+typedef struct LXT_MESSAGE LXT_MESSAGE;
+
+// Device
+char* get_battery_level(void* , bool);
+char* get_battery_charger(void* , bool);
+char* get_usb_status(void* , bool);
+char* get_earjack_status(void* , bool);
+char* get_rssi_level(void* , bool);
+
+void device_parser(char*);
+
+// Sensor
+char* get_proximity_status(void* , bool);
+char* get_light_level(void* , bool);
+char* get_acceleration_value(void* , bool);
+char* get_gyroscope_value(void* , bool);
+char* get_magnetic_value(void* , bool);
+
+// Location
+char* get_location_status(void* , bool);
+
+// SD Card
+int is_mounted(void);
+void* mount_sdcard(void* data);
+int umount_sdcard(const int fd);
+
+void send_guest_server(char* databuf);
+
+struct _auto_mutex
+{
+ _auto_mutex(pthread_mutex_t* t)
+ {
+ _mutex = t;
+ pthread_mutex_lock(_mutex);
+
+ }
+ ~_auto_mutex()
+ {
+ pthread_mutex_unlock(_mutex);
+ }
+
+ pthread_mutex_t* _mutex;
+};
+
+#endif //
--- /dev/null
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Jinhyung Choi <jinhyung2.choi@samsnung.com>
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * Daiyoung Kim <daiyoung777.kim@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+
+#ifndef __evdi_h__
+#define __evdi_h__
+
+typedef int evdi_fd;
+
+bool init_device(evdi_fd* ret_fd);
+
+evdi_fd open_device(void);
+
+bool ijmsg_send_to_evdi(evdi_fd fd, const char* cat, const char* data, const int len);
+bool send_to_evdi(evdi_fd fd, const char* msg, const int len);
+bool msg_send_to_evdi(evdi_fd fd, const char* data, const int len);
+
+
+
+#endif
--- /dev/null
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Jinhyung Choi <jinhyung2.choi@samsnung.com>
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * Daiyoung Kim <daiyoung777.kim@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+
+#ifndef EVDI_PROTOCOL_H_
+#define EVDI_PROTOCOL_H_
+
+/* device protocol */
+
+#define __MAX_BUF_SIZE 1024
+
+enum
+{
+ route_qemu = 0,
+ route_control_server = 1,
+ route_monitor = 2
+};
+
+typedef unsigned int CSCliSN;
+
+struct msg_info {
+ char buf[__MAX_BUF_SIZE];
+
+ uint32_t route;
+ uint32_t use;
+ uint16_t count;
+ uint16_t index;
+
+ CSCliSN cclisn;
+};
+
+/* device protocol */
+
+
+#endif /* EVDI_PROTOCOL_H_ */
--- /dev/null
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Jinhyung Choi <jinhyung2.choi@samsnung.com>
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * Daiyoung Kim <daiyoung777.kim@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+
+#ifndef SYNBUF_H_
+#define SYNBUF_H_
+
+#include <stdbool.h>
+
+
+class synbuf
+{
+public:
+
+ enum
+ {
+ default_buf_size = 2048
+ };
+ synbuf()
+ : m_buf(NULL), m_size(default_buf_size), m_use(0)
+ {
+ m_buf = (char*) malloc(default_buf_size);
+ memset(m_buf, 0, default_buf_size);
+ m_readptr = m_buf;
+ }
+
+ void reset_buf()
+ {
+ freebuf();
+
+ m_buf = (char*) malloc(default_buf_size);
+ memset(m_buf, 0, default_buf_size);
+ m_readptr = m_buf;
+
+ m_size = default_buf_size;
+ m_use = 0;
+ }
+
+ int available()
+ {
+ return m_size - m_use;
+ }
+
+ char* get_readptr()
+ {
+ return m_readptr;
+ }
+
+ void set_written(const int written)
+ {
+ m_use += written;
+ }
+
+ void freebuf()
+ {
+ if (m_buf)
+ {
+ free(m_buf);
+ m_buf = NULL;
+ }
+ }
+
+ bool realloc_and_move(const int newsize, const int readed)
+ {
+ char* tmp = (char*) malloc(newsize);
+ if (!tmp)
+ return false;
+
+ int left = m_use - readed;
+ memset(tmp, 0, newsize);
+ memcpy(tmp, m_buf + readed, left);
+
+ freebuf();
+
+ m_buf = tmp;
+ m_use = left;
+ m_size = newsize;
+ m_readptr = m_buf;
+
+ return true;
+ }
+
+ bool write(const char* buf, const int len)
+ {
+ if (len >= available())
+ {
+ if (!realloc_and_move((m_size * 2), 0))
+ return false;
+ }
+
+ memcpy(m_buf + m_use, buf, len);
+ m_use += len;
+
+ return true;
+ }
+
+ int read(char* buf, const int len)
+ {
+ if (m_use < len)
+ return 0;
+
+ memcpy(buf, m_buf, len);
+
+ int left = m_use - len;
+ if (left > 0)
+ {
+ realloc_and_move(m_size, len);
+ }
+ else
+ {
+ // there is no more readable buffer, reset all variables
+ memset(m_buf, 0, m_size);
+ m_readptr = m_buf;
+ m_use = 0;
+ }
+
+ return len;
+ }
+
+
+private:
+
+
+ char* m_buf;
+ char* m_readptr;
+ int m_size;
+ int m_use;
+};
+
+
+
+#endif /* SYNBUF_H_ */
--- /dev/null
+Name: emuld
+Version: 0.5.2
+Release: 0
+Summary: Emulator daemon
+License: Apache-2.0
+Source0: %{name}-%{version}.tar.gz
+Group: SDK/Other
+
+%if ("%{_repository}" == "mobile")
+Source1001: packaging/emuld_mobile.manifest
+%endif
+
+%if ("%{_repository}" == "wearable")
+Source1002: packaging/emuld_wearable.manifest
+%endif
+
+BuildRequires: cmake
+BuildRequires: pkgconfig(vconf)
+BuildRequires: pkgconfig(deviced)
+BuildRequires: pkgconfig(dlog)
+
+%if ("%{_repository}" == "wearable")
+Requires: context-manager
+%endif
+
+%if ("%{_repository}" == "mobile")
+BuildRequires: pkgconfig(pmapi)
+%endif
+
+%description
+A emulator daemon is used for communication between guest and host
+
+%prep
+%setup -q
+
+%if ("%{_repository}" == "wearable")
+export CFLAGS+=" -DWEARABLE"
+%endif
+
+%if ("%{_repository}" == "mobile")
+export CFLAGS+=" -DMOBILE"
+%endif
+
+cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix}
+
+%build
+
+make
+
+%install
+rm -rf %{buildroot}
+
+if [ ! -d %{buildroot}/usr/lib/systemd/system/emulator.target.wants ]; then
+ mkdir -p %{buildroot}/usr/lib/systemd/system/emulator.target.wants
+fi
+
+cp emuld.service %{buildroot}/usr/lib/systemd/system/.
+ln -s ../emuld.service %{buildroot}/usr/lib/systemd/system/emulator.target.wants/emuld.service
+
+mkdir -p %{buildroot}/usr/share/license
+cp LICENSE %{buildroot}/usr/share/license/%{name}
+
+%make_install
+
+%clean
+make clean
+rm -rf CMakeCache.txt
+rm -rf CMakeFiles
+rm -rf cmake_install.cmake
+rm -rf Makefile
+rm -rf install_manifest.txt
+
+%post
+chmod 770 %{_prefix}/bin/emuld
+
+%files
+%defattr(-,root,root,-)
+
+%if ("%{_repository}" == "wearable")
+%manifest packaging/emuld_wearable.manifest
+%endif
+
+%if ("%{_repository}" == "mobile")
+%manifest packaging/emuld_mobile.manifest
+%endif
+
+%{_prefix}/bin/emuld
+/usr/share/license/%{name}
+/usr/lib/systemd/system/emuld.service
+/usr/lib/systemd/system/emulator.target.wants/emuld.service
+
+%changelog
--- /dev/null
+<manifest>
+ <request>
+ <domain name="_"/>
+ </request>
+</manifest>
--- /dev/null
+<manifest>
+ <define>
+ <domain name="emuld"/>
+ <request>
+ <smack request="context-manager" type="w" />
+ <smack request="system::use_internet" type="w" />
+ </request>
+ <permit>
+ <smack permit="system::use_internet" type="w" />
+ </permit>
+ </define>
+ <request>
+ <domain name="emuld" />
+ </request>
+</manifest>
--- /dev/null
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Jinhyung Choi <jinhyung2.choi@samsnung.com>
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * Daiyoung Kim <daiyoung777.kim@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+
+#include "emuld.h"
+#include "emuld_common.h"
+
+static pthread_mutex_t mutex_climap = PTHREAD_MUTEX_INITIALIZER;
+
+
+CliMap g_climap;
+
+void clipool_add(int fd, unsigned short port, const int fdtype)
+{
+ _auto_mutex _(&mutex_climap);
+
+ static CliSN s_id = 0;
+
+ CliSN id = s_id;
+ s_id++;
+ Cli* cli = new Cli(id, fdtype, fd, port);
+ if (!cli)
+ return;
+
+ if (!g_climap.insert(CliMap::value_type(fd, cli)).second)
+ return;
+
+ LOGINFO("clipool_add fd = %d, port = %d, type = %d", fd, port, fdtype);
+}
+
+
+void close_cli(int cli_fd)
+{
+ clipool_delete(cli_fd);
+ close(cli_fd);
+}
+
+void clipool_delete(int fd)
+{
+ _auto_mutex _(&mutex_climap);
+
+ CliMap::iterator it = g_climap.find(fd);
+
+ if (it != g_climap.end())
+ {
+ Cli* cli = it->second;
+ g_climap.erase(it);
+
+ if (cli)
+ {
+ delete cli;
+ cli = NULL;
+ }
+ }
+
+ LOGINFO("clipool_delete fd = %d", fd);
+}
+
+
+Cli* find_cli(const int fd)
+{
+ _auto_mutex _(&mutex_climap);
+
+ CliMap::iterator it = g_climap.find(fd);
+ if (it != g_climap.end())
+ return NULL;
+
+ Cli* cli = it->second;
+ return cli;
+}
+
+// for thread safe
+bool send_to_cli(const int fd, char* data, const int len)
+{
+ _auto_mutex _(&mutex_climap);
+
+ CliMap::iterator it = g_climap.find(fd);
+ if (it == g_climap.end())
+ return false;
+
+ Cli* cli = it->second;
+
+ if (send(cli->sockfd, data, len, 0) == -1)
+ return false;
+
+ return true;
+}
+
+bool send_to_all_ij(char* data, const int len)
+{
+ _auto_mutex _(&mutex_climap);
+
+ bool result = false;
+ CliMap::iterator it, itend = g_climap.end();
+
+ for (it = g_climap.begin(); it != itend; it++)
+ {
+ Cli* cli = it->second;
+
+ if (!cli)
+ continue;
+
+ int sent = send(cli->sockfd, data, len, 0);
+ result = (sent == -1) ? false : true;
+ if (sent == -1)
+ {
+ LOGERR("failed to send to ij");
+ }
+
+ LOGDEBUG("send_len: %d, err= %d", sent, errno);
+ }
+ return result;
+}
+
+bool is_ij_exist()
+{
+ _auto_mutex _(&mutex_climap);
+
+ bool result = (g_climap.size() > 0) ? true : false;
+ return result;
+}
+
+void stop_listen(void)
+{
+ pthread_mutex_destroy(&mutex_climap);;
+}
+
+
+
+
+
--- /dev/null
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Jinhyung Choi <jinhyung2.choi@samsnung.com>
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <limits.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <pthread.h>
+#include <stdarg.h>
+
+#include <vconf/vconf.h>
+#include <vconf/vconf-keys.h>
+
+#include "emuld.h"
+#include "emuld_common.h"
+
+#ifdef MOBILE
+static int battery_level = 50;
+#endif
+
+pthread_t d_tid[16];
+
+struct appdata
+{
+ void* data;
+};
+struct appdata ad;
+
+typedef struct
+{
+ int len;
+ int repeatCnt;
+ int fileCnt;
+ char *buffer;
+} FileInput_args;
+
+typedef struct
+{
+ char filename[256];
+} FileInput_files;
+
+enum sensor_type{
+ MOTION = 6,
+ USBKEYBOARD = 7,
+ BATTERYLEVEL = 8,
+ EARJACK = 9,
+ USB = 10,
+ RSSI = 11,
+ FILE_ACCEL = 14,
+ FILE_MAGNETIC = 15,
+ FILE_GYRO = 16
+};
+
+enum motion_doubletap{
+ SENSOR_MOTION_DOUBLETAP_NONE = 0,
+ SENSOR_MOTION_DOUBLETAP_DETECTION = 1
+};
+
+enum motion_shake{
+ SENSOR_MOTION_SHAKE_NONE = 0,
+ SENSOR_MOTION_SHAKE_DETECTED = 1,
+ SENSOR_MOTION_SHAKE_CONTINUING = 2,
+ SENSOR_MOTION_SHAKE_FINISHED = 3,
+ SENSOR_MOTION_SHAKE_BREAK = 4
+};
+
+enum motion_snap{
+ SENSOR_MOTION_SNAP_NONE = 0,
+ SENSOR_MOTION_SNAP_NEGATIVE_X = 1,
+ SENSOR_MOTION_SNAP_POSITIVE_X = 2,
+ SENSOR_MOTION_SNAP_NEGATIVE_Y = 3,
+ SENSOR_MOTION_SNAP_POSITIVE_Y = 4,
+ SENSOR_MOTION_SNAP_NEGATIVE_Z = 5,
+ SENSOR_MOTION_SNAP_POSITIVE_Z = 6,
+ SENSOR_MOTION_SNAP_LEFT = SENSOR_MOTION_SNAP_NEGATIVE_X,
+ SENSOR_MOTION_SNAP_RIGHT = SENSOR_MOTION_SNAP_POSITIVE_X
+};
+
+enum motion_move{
+ SENSOR_MOTION_MOVE_NONE = 0,
+ SENSOR_MOTION_MOVE_MOVETOCALL = 1
+};
+
+int check_nodes();
+
+int parse_motion_data(int len, char *buffer);
+int parse_usbkeyboard_data(int len, char *buffer);
+int parse_batterylevel_data(int len, char *buffer);
+int parse_earjack_data(int len, char *buffer);
+int parse_rssi_data(int len, char *buffer);
+
+static void system_cmd(const char* msg)
+{
+ int ret = system(msg);
+ if (ret == -1) {
+ LOGERR("system command is failed: %s", msg);
+ }
+}
+
+#ifdef WEARABLE
+
+#define DBUS_SEND_CMD "dbus-send --system --type=method_call --print-reply --reply-timeout=120000 --dest=org.tizen.system.deviced /Org/Tizen/System/DeviceD/SysNoti org.tizen.system.deviced.SysNoti."
+static void dbus_send(const char* device, const char* option)
+{
+ const char* dbus_send_cmd = DBUS_SEND_CMD;
+ char* cmd;
+
+ if (device == NULL || option == NULL)
+ return;
+
+ cmd = (char*)malloc(512);
+ if (cmd == NULL)
+ return;
+
+ memset(cmd, 0, 512);
+
+ sprintf(cmd, "%s%s string:\"%s\" %s", dbus_send_cmd, device, device, option);
+
+ system_cmd(cmd);
+ LOGINFO("dbus_send: %s", cmd);
+
+ free(cmd);
+}
+
+#define POWER_SUPPLY "power_supply"
+#define FULL "Full"
+#define CHARGING "Charging"
+#define DISCHARGING "Discharging"
+static void dbus_send_power_supply(int capacity, int charger)
+{
+ const char* power_device = POWER_SUPPLY;
+ char state [16];
+ char option [128];
+ memset(state, 0, 16);
+ memset(option, 0, 128);
+
+ if (capacity == 100 && charger == 1) {
+ memcpy(state, FULL, 4);
+ } else if (charger == 1) {
+ memcpy(state, CHARGING, 8);
+ } else {
+ memcpy(state, DISCHARGING, 11);
+ }
+
+ sprintf(option, "int32:5 string:\"%d\" string:\"%s\" string:\"Good\" string:\"%d\" string:\"1\"",
+ capacity, state, (charger + 1));
+
+ dbus_send(power_device, option);
+}
+
+#define USB_DEVICE "device_changed"
+static void dbus_send_usb(int on)
+{
+ const char* usb_device = USB_DEVICE;
+ char option [128];
+ memset(option, 0, 128);
+
+ sprintf(option, "int32:2 string:\"usb\" string:\"%d\"", on);
+
+ dbus_send(usb_device, option);
+}
+#endif
+
+int parse_motion_data(int len, char *buffer)
+{
+ int len1=0;
+ char tmpbuf[255];
+ int x;
+ char command[128];
+ memset(command, '\0', sizeof(command));
+
+ LOGDEBUG("read data: %s", buffer);
+
+ // read param count
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ /* first data */
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ x = atoi(tmpbuf);
+
+ switch(x)
+ {
+ case 1: // double tap
+ sprintf(command, "vconftool set -t int memory/private/sensor/800004 %d -i -f", SENSOR_MOTION_DOUBLETAP_DETECTION);
+ system_cmd(command);
+ // memset(command, '\0', sizeof(command));
+ // sprintf(command, "vconftool set -t int memory/private/sensor/800004 %d -i -f", SENSOR_MOTION_DOUBLETAP_NONE);
+ // system_cmd(command);
+ break;
+ case 2: // shake start
+ sprintf(command, "vconftool set -t int memory/private/sensor/800002 %d -i -f", SENSOR_MOTION_SHAKE_DETECTED);
+ system_cmd(command);
+ memset(command, '\0', sizeof(command));
+ sprintf(command, "vconftool set -t int memory/private/sensor/800002 %d -i -f", SENSOR_MOTION_SHAKE_CONTINUING);
+ system_cmd(command);
+ break;
+ case 3: // shake stop
+ sprintf(command, "vconftool set -t int memory/private/sensor/800002 %d -i -f", SENSOR_MOTION_SHAKE_FINISHED);
+ system_cmd(command);
+ break;
+ case 4: // snap x+
+ sprintf(command, "vconftool set -t int memory/private/sensor/800001 %d -i -f", SENSOR_MOTION_SNAP_POSITIVE_X);
+ system_cmd(command);
+ break;
+ case 5: // snap x-
+ sprintf(command, "vconftool set -t int memory/private/sensor/800001 %d -i -f", SENSOR_MOTION_SNAP_NEGATIVE_X);
+ system_cmd(command);
+ break;
+ case 6: // snap y+
+ sprintf(command, "vconftool set -t int memory/private/sensor/800001 %d -i -f", SENSOR_MOTION_SNAP_POSITIVE_Y);
+ system_cmd(command);
+ break;
+ case 7: // snap y-
+ sprintf(command, "vconftool set -t int memory/private/sensor/800001 %d -i -f", SENSOR_MOTION_SNAP_NEGATIVE_Y);
+ system_cmd(command);
+ break;
+ case 8: // snap z+
+ sprintf(command, "vconftool set -t int memory/private/sensor/800001 %d -i -f", SENSOR_MOTION_SNAP_POSITIVE_Z);
+ system_cmd(command);
+ break;
+ case 9: // snap z-
+ sprintf(command, "vconftool set -t int memory/private/sensor/800001 %d -i -f", SENSOR_MOTION_SNAP_NEGATIVE_Z);
+ system_cmd(command);
+ break;
+ case 10: // snap left
+ sprintf(command, "vconftool set -t int memory/private/sensor/800001 %d -i -f", SENSOR_MOTION_SNAP_NEGATIVE_X);
+ system_cmd(command);
+ break;
+ case 11: // snap right
+ sprintf(command, "vconftool set -t int memory/private/sensor/800001 %d -i -f", SENSOR_MOTION_SNAP_POSITIVE_X);
+ system_cmd(command);
+ break;
+ case 12: // move to call (direct call)
+ sprintf(command, "vconftool set -t int memory/private/sensor/800020 %d -i -f", SENSOR_MOTION_MOVE_MOVETOCALL);
+ system_cmd(command);
+ break;
+ default:
+ LOGERR("not supported activity");
+ break;
+ }
+
+ return 0;
+}
+
+int parse_usbkeyboard_data(int len, char *buffer)
+{
+ int len1=0;
+ char tmpbuf[255];
+ int x;
+
+ LOGDEBUG("read data: %s", buffer);
+
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ x = atoi(tmpbuf);
+
+ if(x == 1)
+ {
+ system_cmd("udevadm trigger --subsystem-match=input --sysname-match=event3 --action=add");
+ }
+ else if(x == 0)
+ {
+ system_cmd("udevadm trigger --subsystem-match=input --sysname-match=event3 --action=remove");
+ }
+ else
+ assert(0);
+
+ return 0;
+}
+#ifdef MOBILE
+int parse_batterylevel_data(int len, char *buffer)
+{
+ int len1=0, id = 0, ret = 0;
+ char tmpbuf[255];
+ int level = 0, charger = 0, charger_online = 0, charge_full = 0;
+ FILE* fd;
+
+ LOGDEBUG("read data: %s", buffer);
+
+ // read param count
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ /* first data */
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ id = atoi(tmpbuf);
+ if(id == 1) // level
+ {
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ level = atoi(tmpbuf);
+ battery_level = level;
+
+ if(level == 100)
+ {
+ charger = 0;
+ }
+ else
+ {
+ charger = 1;
+ }
+
+ fd = fopen(PATH_BATTERY_CAPACITY, "w");
+ if(!fd)
+ {
+ LOGERR("fopen fail");
+ return -1;
+ }
+ fprintf(fd, "%d", level);
+ fclose(fd);
+
+ fd = fopen(PATH_BATTERY_CHARGER_ON, "r");
+ if(!fd)
+ {
+ LOGERR("fopen fail");
+ return -1;
+ }
+ ret = fscanf(fd, "%d", &charger_online);
+ fclose(fd);
+ if (ret < 0)
+ {
+ LOGERR("failed to get charger_online value");
+ return -1;
+ }
+
+ LOGDEBUG("charge_online: %d", charger_online);
+
+ if(charger_online == 1 && level == 100)
+ {
+ charge_full = 1;
+ }
+ else
+ {
+ charge_full = 0;
+ }
+ LOGDEBUG("charge_full: %d", charge_full);
+
+ fd = fopen(PATH_BATTERY_CHARGE_FULL, "w");
+ if(!fd)
+ {
+ LOGERR("charge_full fopen fail");
+ return -1;
+ }
+ fprintf(fd, "%d", charge_full);
+ fclose(fd);
+
+ if(charger_online == 1)
+ {
+ fd = fopen(PATH_BATTERY_CHARGE_NOW, "w");
+ if(!fd)
+ {
+ LOGERR("charge_now fopen fail");
+ return -1;
+ }
+ fprintf(fd, "%d", charger);
+ fclose(fd);
+ }
+
+ // because time based polling
+ system_cmd("/usr/bin/sys_event device_charge_chgdet");
+ }
+ else if(id == 2)
+ {
+ /* second data */
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ charger = atoi(tmpbuf);
+ fd = fopen(PATH_BATTERY_CHARGER_ON, "w");
+ if(!fd)
+ {
+ LOGERR("charger_online fopen fail");
+ return -1;
+ }
+ fprintf(fd, "%d", charger);
+ fclose(fd);
+
+ fd = fopen(PATH_BATTERY_CHARGE_FULL, "w");
+ if(!fd)
+ {
+ LOGERR("charge_full fopen fail");
+ return -1;
+ }
+
+ if(battery_level == 100 && charger == 1)
+ {
+ fprintf(fd, "%d", 1); // charge full
+ charger = 0;
+ }
+ else
+ {
+ fprintf(fd, "%d", 0);
+ }
+ fclose(fd);
+
+ system_cmd("/usr/bin/sys_event device_charge_chgdet");
+
+ fd = fopen(PATH_BATTERY_CHARGE_NOW, "w");
+ if(!fd)
+ {
+ LOGERR("charge_now fopen fail");
+ return -1;
+ }
+ fprintf(fd, "%d", charger);
+ fclose(fd);
+
+ // because time based polling
+ system_cmd("/usr/bin/sys_event device_ta_chgdet");
+ }
+
+ return 0;
+}
+#endif
+#ifdef WEARABLE
+
+#define FILE_BATTERY_CAPACITY "/sys/class/power_supply/battery/capacity"
+#define FILE_BATTERY_CHARGER_ONLINE "/sys/devices/platform/jack/charger_online"
+#define FILE_BATTERY_CHARGE_FULL "/sys/class/power_supply/battery/charge_full"
+#define FILE_BATTERY_CHARGE_NOW "/sys/class/power_supply/battery/charge_now"
+
+static int read_from_file(const char* file_name)
+{
+ int ret;
+ FILE* fd;
+ int value;
+
+ fd = fopen(file_name, "r");
+ if(!fd)
+ {
+ LOGERR("fopen fail: %s", file_name);
+ return -1;
+ }
+
+ ret = fscanf(fd, "%d", &value);
+ fclose(fd);
+ if (ret <= 0) {
+ LOGERR("failed to get value");
+ return -1;
+ }
+
+ return value;
+}
+
+static void write_to_file(const char* file_name, int value)
+{
+ FILE* fd;
+
+ fd = fopen(file_name, "w");
+ if(!fd)
+ {
+ LOGERR("fopen fail: %s", file_name);
+ return;
+ }
+ fprintf(fd, "%d", value);
+ fclose(fd);
+}
+
+int set_battery_data(void)
+{
+ int charger_online = 0;
+ int battery_level = 0;
+
+ battery_level = read_from_file(FILE_BATTERY_CAPACITY);
+ LOGINFO("battery level: %d", battery_level);
+ if (battery_level < 0)
+ return -1;
+
+ charger_online = read_from_file(FILE_BATTERY_CHARGER_ONLINE);
+ LOGINFO("charge_online: %d", charger_online);
+ if (charger_online < 0)
+ return -1;
+
+ dbus_send_power_supply(battery_level, charger_online);
+
+ return 0;
+}
+
+#endif
+int parse_earjack_data(int len, char *buffer)
+{
+ int len1=0;
+ char tmpbuf[255];
+ int x;
+ FILE* fd;
+
+ LOGDEBUG("read data: %s", buffer);
+
+ // read param count
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ /* first data */
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ x = atoi(tmpbuf);
+
+ fd = fopen(PATH_JACK_EARJACK, "w");
+ if(!fd)
+ {
+ LOGERR("earjack_online fopen fail");
+ return -1;
+ }
+ fprintf(fd, "%d", x);
+ fclose(fd);
+
+ // because time based polling
+ system_cmd("/usr/bin/sys_event device_earjack_chgdet");
+
+ return 0;
+}
+
+#ifdef MOBILE
+int parse_usb_data(int len, char *buffer)
+{
+ int len1=0;
+ char tmpbuf[255];
+ int x;
+ FILE* fd;
+
+ LOGDEBUG("read data: %s", buffer);
+
+ // read param count
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ /* first data */
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ x = atoi(tmpbuf);
+
+ fd = fopen(PATH_JACK_USB, "w");
+ if(!fd)
+ {
+ LOGERR("usb_online fopen fail");
+ return -1;
+ }
+ fprintf(fd, "%d", x);
+ fclose(fd);
+
+ // because time based polling
+ system_cmd("/usr/bin/sys_event device_usb_chgdet");
+ return 0;
+}
+#endif
+
+#ifdef WEARABLE
+#define FILE_USB_ONLINE "/sys/devices/platform/jack/usb_online"
+int parse_usb_data(int len, char *buffer)
+{
+ int len1=0;
+ char tmpbuf[255];
+ int x;
+
+ #ifdef SENSOR_DEBUG
+ LOG("read data: %s", buffer);
+ #endif
+ // read param count
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ /* first data */
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ x = atoi(tmpbuf);
+
+ write_to_file(FILE_USB_ONLINE, x);
+
+ // because time based polling
+ dbus_send_usb(x);
+
+ return 0;
+}
+#endif
+
+int parse_rssi_data(int len, char *buffer)
+{
+ int len1=0;
+ char tmpbuf[255];
+ int x;
+ char command[128];
+ memset(command, '\0', sizeof(command));
+
+ LOGDEBUG("read data: %s", buffer);
+
+ // read param count
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ /* first data */
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ x = atoi(tmpbuf);
+
+ sprintf(command, "vconftool set -t int memory/telephony/rssi %d -i -f", x);
+ system_cmd(command);
+
+ return 0;
+}
+
+void* file_input_accel(void* param)
+{
+ FILE* srcFD;
+ FILE* dstFD;
+
+ int len1 = 0, fileCnt = 0, repeatCnt = 0, prevTime = 0, nextTime = 0, sleepTime = 0, x, y, z;
+ int waitCnt = 0;
+ double g = 9.80665;
+
+ char tmpbuf[255];
+ char token[] = ",";
+ char* ret = NULL;
+ char command[255];
+ char lineData[1024];
+
+ FileInput_args* args = (FileInput_args*)param;
+ FileInput_files fname[args->fileCnt];
+ memset(fname, '\0', sizeof(fname));
+
+ pthread_detach(pthread_self());
+
+ LOGINFO("file_input_accel start");
+
+ // save file names
+ for(fileCnt = 0; fileCnt < args->fileCnt; fileCnt++)
+ {
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(args->buffer+args->len, 0x0a, tmpbuf);
+ args->len += len1;
+
+ strcpy(fname[fileCnt].filename, tmpbuf);
+ LOGINFO("saved file name: %s", fname[fileCnt].filename);
+ }
+
+ // play files
+ for(repeatCnt = 0; repeatCnt < args->repeatCnt; repeatCnt++)
+ {
+ for(fileCnt = 0; fileCnt < args->fileCnt; fileCnt++)
+ {
+ memset(command, '\0', sizeof(command));
+ sprintf(command, "/tmp/accel/InputFiles/%s", fname[fileCnt].filename);
+ command[strlen(command) - 1] = 0x00; // erase '\n' for fopen
+ LOGINFO("fopen command: %s", command);
+
+ waitCnt = 0;
+ while(access(command, F_OK) != 0 && waitCnt < 3)
+ {
+ usleep(10000);
+ waitCnt++;
+ }
+
+ srcFD = fopen(command, "r");
+ if(!srcFD)
+ {
+ LOGINFO("fopen fail");
+ pthread_exit((void *) 0);
+ }
+
+ prevTime = 0;
+ nextTime = 0;
+
+ memset(lineData, '\0', sizeof(lineData));
+ while(fgets(lineData, 1024, srcFD) != NULL)
+ {
+ ret = strtok(lineData, token);
+ if(!ret)
+ {
+ LOGINFO("data is NULL");
+ nextTime = prevTime + 1;
+ }
+ else
+ nextTime = atoi(ret);
+
+ sleepTime = (nextTime - prevTime) * 10000;
+ if(sleepTime < 0)
+ {
+ sleepTime = 10000;
+ nextTime = prevTime + 1;
+ }
+
+ usleep(sleepTime); // convert millisecond
+ prevTime = nextTime;
+
+ ret = strtok(NULL, token);
+ if(!ret)
+ {
+ LOGINFO("x data is NULL");
+ x = 0;
+ }
+ else
+ x = (int)(atof(ret) * g * -100000);
+
+ if (x > 1961330)
+ x = 1961330;
+ if (x < -1961330)
+ x = -1961330;
+ LOGINFO("x: %d", x);
+
+ ret = strtok(NULL, token);
+ if(!ret)
+ {
+ LOGINFO("y data is NULL");
+ y = 0;
+ }
+ else
+ y = (int)(atof(ret) * g * -100000);
+
+ if (y > 1961330)
+ y = 1961330;
+ if (y < -1961330)
+ y = -1961330;
+ LOGINFO("y: %d", y);
+
+ ret = strtok(NULL, token);
+ if(!ret)
+ {
+ LOGINFO("data is NULL");
+ z = 0;
+ }
+ else
+ z = (int)(atof(ret) * g * -100000);
+
+ if (z > 1961330)
+ z = 1961330;
+ if (z < -1961330)
+ z = -1961330;
+ LOGINFO("z: %d", z);
+
+ dstFD = fopen(PATH_SENSOR_ACCEL_XYZ, "w");
+ if(!dstFD)
+ {
+ LOGINFO("fopen fail");
+ pthread_exit((void *) 0);
+ }
+ fprintf(dstFD, "%d, %d, %d",x, y, z);
+ fclose(dstFD);
+ }
+
+ fclose(srcFD);
+ }
+ }
+
+ LOGINFO("thread exit");
+ system_cmd("rm -rf /tmp/accel/InputFiles/*");
+
+ pthread_exit((void *) 0);
+}
+
+int parse_file_accel_data(int len, char *buffer)
+{
+ int len1=0, repeat = -1;
+ char tmpbuf[255];
+
+ LOGDEBUG("read data: %s", buffer);
+
+ // read start/stop
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ repeat = atoi(tmpbuf);
+
+ pthread_cancel(d_tid[0]);
+
+ if(repeat == 0) // stop
+ {
+ system_cmd("rm -rf /tmp/accel/InputFiles/*");
+ }
+ else // start
+ {
+ // read file count
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ FileInput_args args;
+ args.len = len;
+ args.repeatCnt = repeat;
+ args.fileCnt = atoi(tmpbuf);
+ args.buffer = buffer;
+ if(pthread_create(&d_tid[0], NULL, file_input_accel, &args) != 0) {
+ LOGERR("pthread create fail!");
+ }
+ }
+
+ return 0;
+}
+
+void* file_input_magnetic(void* param)
+{
+ FILE* srcFD;
+ FILE* dstFD;
+
+ int len1 = 0, fileCnt = 0, repeatCnt = 0, prevTime = 0, nextTime = 0, sleepTime = 0, x, y, z;
+ int waitCnt = 0;
+
+ char tmpbuf[255];
+ char token[] = ",";
+ char* ret = NULL;
+ char command[255];
+ char lineData[1024];
+ FileInput_args* args = (FileInput_args*) param;
+ FileInput_files fname[args->fileCnt];
+ memset(fname, '\0', sizeof(fname));
+
+ pthread_detach(pthread_self());
+
+ LOGINFO("file_input_magnetic start");
+
+ // save file names
+ for(fileCnt = 0; fileCnt < args->fileCnt; fileCnt++)
+ {
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(args->buffer+args->len, 0x0a, tmpbuf);
+ args->len += len1;
+
+ strcpy(fname[fileCnt].filename, tmpbuf);
+ LOGINFO("saved file name: %s", fname[fileCnt].filename);
+ }
+
+ // play files
+ for(repeatCnt = 0; repeatCnt < args->repeatCnt; repeatCnt++)
+ {
+ for(fileCnt = 0; fileCnt < args->fileCnt; fileCnt++)
+ {
+ memset(command, '\0', sizeof(command));
+ sprintf(command, "/tmp/geo/InputFiles/%s", fname[fileCnt].filename);
+ command[strlen(command) - 1] = 0x00; // erase '\n' for fopen
+ LOGINFO("fopen command: %s", command);
+
+ waitCnt = 0;
+ while(access(command, F_OK) != 0 && waitCnt < 3)
+ {
+ usleep(10000);
+ waitCnt++;
+ }
+
+ srcFD = fopen(command, "r");
+ if(!srcFD)
+ {
+ LOGERR("fopen fail");
+ pthread_exit((void *) 0);
+ }
+
+ prevTime = 0;
+ nextTime = 0;
+
+ memset(lineData, '\0', sizeof(lineData));
+ while(fgets(lineData, 1024, srcFD) != NULL)
+ {
+ ret = strtok(lineData, token);
+ if(!ret)
+ {
+ LOGINFO("data is NULL");
+ nextTime = prevTime + 1;
+ }
+ else
+ nextTime = atoi(ret);
+
+ sleepTime = (nextTime - prevTime) * 10000;
+ if(sleepTime < 0)
+ {
+ sleepTime = 10000;
+ nextTime = prevTime + 1;
+ }
+
+ usleep(sleepTime); // convert millisecond
+ prevTime = nextTime;
+
+ ret = strtok(NULL, token);
+ if(!ret)
+ {
+ LOGINFO("x data is NULL");
+ x = 0;
+ }
+ else
+ x = atoi(ret);
+
+ if (x > 2000)
+ x = 2000;
+ if (x < -2000)
+ x = -2000;
+ LOGINFO("x: %d", x);
+
+ ret = strtok(NULL, token);
+ if(!ret)
+ {
+ LOGINFO("y data is NULL");
+ y = 0;
+ }
+ else
+ y = atoi(ret);
+
+ if (y > 2000)
+ y = 2000;
+ if (y < -2000)
+ y = -2000;
+ LOGINFO("y: %d", y);
+
+ ret = strtok(NULL, token);
+ if(!ret)
+ {
+ LOGINFO("data is NULL");
+ z = 0;
+ }
+ else
+ z = atoi(ret);
+
+ if (z > 2000)
+ z = 2000;
+ if (z < -2000)
+ z = -2000;
+ LOGINFO("z: %d", z);
+
+ dstFD = fopen(PATH_SENSOR_GEO_TESLA, "w");
+ if(!dstFD)
+ {
+ LOGINFO("fopen fail");
+ pthread_exit((void *) 0);
+ }
+ fprintf(dstFD, "%d %d %d",x, y, z);
+ fclose(dstFD);
+ }
+
+ fclose(srcFD);
+ }
+ }
+
+ LOGINFO("thread exit");
+ system_cmd("rm -rf /tmp/geo/InputFiles/*");
+
+ pthread_exit((void *) 0);
+}
+
+int parse_file_magnetic_data(int len, char *buffer)
+{
+ int len1=0, repeat = -1;
+ char tmpbuf[255];
+
+ LOGDEBUG("read data: %s", buffer);
+
+ // read start/stop
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ repeat = atoi(tmpbuf);
+
+ pthread_cancel(d_tid[1]);
+
+ if(repeat == 0) // stop
+ {
+ system_cmd("rm -rf /tmp/geo/InputFiles/*");
+ }
+ else // start
+ {
+ // read file count
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ FileInput_args args;
+ args.len = len;
+ args.repeatCnt = repeat;
+ args.fileCnt = atoi(tmpbuf);
+ args.buffer = buffer;
+ if(pthread_create(&d_tid[1], NULL, file_input_magnetic, &args) != 0)
+ LOGINFO("pthread create fail!");
+ }
+
+ return 0;
+}
+
+void* file_input_gyro(void* param)
+{
+ FILE* srcFD;
+ FILE* dstFD;
+
+ int len1 = 0, fileCnt = 0, repeatCnt = 0, prevTime = 0, nextTime = 0, sleepTime = 0, x, y, z;
+ int waitCnt = 0;
+
+ char tmpbuf[255];
+ char token[] = ",";
+ char* ret = NULL;
+ char command[255];
+ char lineData[1024];
+ FileInput_args* args = (FileInput_args*) param;
+ FileInput_files fname[args->fileCnt];
+ memset(fname, '\0', sizeof(fname));
+
+ pthread_detach(pthread_self());
+
+ LOGINFO("file_input_gyro start");
+
+ // save file names
+ for(fileCnt = 0; fileCnt < args->fileCnt; fileCnt++)
+ {
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(args->buffer+args->len, 0x0a, tmpbuf);
+ args->len += len1;
+
+ strcpy(fname[fileCnt].filename, tmpbuf);
+ LOGINFO("saved file name: %s", fname[fileCnt].filename);
+ }
+
+ // play files
+ for(repeatCnt = 0; repeatCnt < args->repeatCnt; repeatCnt++)
+ {
+ for(fileCnt = 0; fileCnt < args->fileCnt; fileCnt++)
+ {
+ memset(command, '\0', sizeof(command));
+ sprintf(command, "/tmp/gyro/InputFiles/%s", fname[fileCnt].filename);
+ command[strlen(command) - 1] = 0x00; // erase '\n' for fopen
+ LOGINFO("fopen command: %s", command);
+
+ waitCnt = 0;
+ while(access(command, F_OK) != 0 && waitCnt < 3)
+ {
+ usleep(10000);
+ waitCnt++;
+ }
+
+ srcFD = fopen(command, "r");
+ if(!srcFD)
+ {
+ LOGINFO("fopen fail");
+ pthread_exit((void *) 0);
+ }
+
+ prevTime = 0;
+ nextTime = 0;
+
+ memset(lineData, '\0', sizeof(lineData));
+ while(fgets(lineData, 1024, srcFD) != NULL)
+ {
+ ret = strtok(lineData, token);
+ if(!ret)
+ {
+ LOGINFO("data is NULL");
+ nextTime = prevTime + 1;
+ }
+ else
+ nextTime = atoi(ret);
+
+ sleepTime = (nextTime - prevTime) * 10000;
+ if(sleepTime < 0)
+ {
+ sleepTime = 10000;
+ nextTime = prevTime + 1;
+ }
+
+ usleep(sleepTime); // convert millisecond
+ prevTime = nextTime;
+
+ ret = strtok(NULL, token);
+ if(!ret)
+ {
+ LOGINFO("x data is NULL");
+ x = 0;
+ }
+ else
+ x = (int)(atof(ret) * 1000)/17.50;
+
+ if (x > 571)
+ x = 571;
+ if (x < -571)
+ x = -571;
+ LOGINFO("x: %d", x);
+
+ ret = strtok(NULL, token);
+ if(!ret)
+ {
+ LOGINFO("y data is NULL");
+ y = 0;
+ }
+ else
+ y = (int)(atof(ret) * 1000)/17.50;
+
+ if (y > 571)
+ y = 571;
+ if (y < -571)
+ y = -571;
+ LOGINFO("y: %d", y);
+
+ ret = strtok(NULL, token);
+ if(!ret)
+ {
+ LOGINFO("data is NULL");
+ z = 0;
+ }
+ else
+ z = (int)(atof(ret) * 1000)/17.50;
+
+ if (z > 571)
+ z = 571;
+ if (z < -571)
+ z = -571;
+ LOGINFO("z: %d", z);
+
+ dstFD = fopen(PATH_SENSOR_GYRO_X_RAW, "w");
+ if(!dstFD)
+ {
+ LOGINFO("fopen fail");
+ pthread_exit((void *) 0);
+ }
+ fprintf(dstFD, "%d",x);
+ fclose(dstFD);
+
+ dstFD = fopen(PATH_SENSOR_GYRO_Y_RAW, "w");
+ if(!dstFD)
+ {
+ LOGINFO("fopen fail");
+ pthread_exit((void *) 0);
+ }
+ fprintf(dstFD, "%d",y);
+ fclose(dstFD);
+
+ dstFD = fopen(PATH_SENSOR_GYRO_Z_RAW, "w");
+ if(!dstFD)
+ {
+ LOGINFO("fopen fail");
+ pthread_exit((void *) 0);
+ }
+ fprintf(dstFD, "%d",z);
+ fclose(dstFD);
+ }
+ fclose(srcFD);
+ }
+ }
+
+ LOGINFO("thread exit");
+ system_cmd("rm -rf /tmp/gyro/InputFiles/*");
+
+ pthread_exit((void *) 0);
+}
+
+int parse_file_gyro_data(int len, char *buffer)
+{
+ int len1=0, repeat = -1;
+ char tmpbuf[255];
+
+ LOGDEBUG("read data: %s", buffer);
+
+ // read start/stop
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ repeat = atoi(tmpbuf);
+
+ pthread_cancel(d_tid[2]);
+
+ if(repeat == 0) // stop
+ {
+ system_cmd("rm -rf /tmp/gyro/InputFiles/*");
+ }
+ else // start
+ {
+ // read file count
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len1 = parse_val(buffer+len, 0x0a, tmpbuf);
+ len += len1;
+
+ FileInput_args args;
+ args.len = len;
+ args.repeatCnt = repeat;
+ args.fileCnt = atoi(tmpbuf);
+ args.buffer = buffer;
+ if(pthread_create(&d_tid[2], NULL, file_input_gyro, &args) != 0)
+ LOGINFO("pthread create fail!");
+ }
+
+ return 0;
+}
+
+void device_parser(char *buffer)
+{
+ int len = 0;
+ int ret = 0;
+ char tmpbuf[255];
+
+ LOGDEBUG("read data: %s", buffer);
+
+ // read sensor type
+ memset(tmpbuf, '\0', sizeof(tmpbuf));
+ len = parse_val(buffer, 0x0a, tmpbuf);
+
+ switch(atoi(tmpbuf))
+ {
+ case MOTION:
+ ret = parse_motion_data(len, buffer);
+ if(ret < 0)
+ LOGERR("motion parse error!");
+ break;
+ case USBKEYBOARD:
+ ret = parse_usbkeyboard_data(len, buffer);
+ if(ret < 0)
+ LOGERR("usbkeyboard parse error!");
+ break;
+ case BATTERYLEVEL:
+#ifdef MOBILE
+ ret = parse_batterylevel_data(len, buffer);
+ if(ret < 0)
+ LOGERR("batterylevel parse error!");
+#endif
+#ifdef WEARABLE
+ ret = set_battery_data();
+ if(ret < 0)
+ LOGERR("batterylevel parse error!");
+#endif
+ break;
+ case EARJACK:
+ ret = parse_earjack_data(len, buffer);
+ if(ret < 0)
+ LOGERR("earjack parse error!");
+ break;
+ case USB:
+ ret = parse_usb_data(len, buffer);
+ if(ret < 0)
+ LOGERR("usb parse error!");
+ break;
+ case RSSI:
+ ret = parse_rssi_data(len, buffer);
+ if(ret < 0)
+ LOGERR("rssi parse error!");
+ break;
+ case FILE_ACCEL:
+ ret = parse_file_accel_data(len, buffer);
+ if(ret < 0)
+ LOGERR("file_accel parse error!");
+ break;
+ case FILE_MAGNETIC:
+ ret = parse_file_magnetic_data(len, buffer);
+ if(ret < 0)
+ LOGERR("file_magnetic parse error!");
+ break;
+ case FILE_GYRO:
+ ret = parse_file_gyro_data(len, buffer);
+ if(ret < 0)
+ LOGERR("file_gyro parse error!");
+ break;
+ default:
+ break;
+ }
+}
+
+static int inline get_message(char* message, int status, int buf_len, bool is_evdi)
+{
+ if (is_evdi) {
+ sprintf(message, "%d", status);
+ return strlen(message);
+ } else {
+ // int to byte
+ message[3] = (char) (status & 0xff);
+ message[2] = (char) (status >> 8 & 0xff);
+ message[1] = (char) (status >> 16 & 0xff);
+ message[0] = (char) (status >> 24 & 0xff);
+ message[4] = '\0';
+ }
+
+ return 4;
+}
+
+static int inline get_status(const char* filename)
+{
+ int ret;
+ int status = 0;
+ FILE* fd = fopen(filename, "r");
+ if(!fd)
+ return -1;
+
+ ret = fscanf(fd, "%d", &status);
+ fclose(fd);
+
+ if (ret < 0) {
+ return ret;
+ }
+
+ return status;
+}
+
+static int inline get_file_status(char* msg, const char* filename, int buf_len, bool is_evdi)
+{
+ int status = get_status(filename);
+ if (status < 0)
+ return status;
+ return get_message(msg, status, buf_len, is_evdi);
+}
+
+static int inline get_vconf_status(char* msg, const char* key, int buf_len, bool is_evdi)
+{
+ int status;
+ int ret = vconf_get_int(key, &status);
+ if (ret != 0) {
+ LOGERR("cannot get vconf key - %s", key);
+ return -1;
+ }
+
+ return get_message(msg, status, buf_len, is_evdi);
+}
+
+char* __tmpalloc(const int size)
+{
+ char* message = (char*)malloc(sizeof(char) * size);
+ memset(message, 0, sizeof(char) * size);
+ return message;
+}
+
+char* get_usb_status(void* p, bool is_evdi)
+{
+ char* message = __tmpalloc(5);
+ int length = get_file_status(message, PATH_JACK_USB, 5, is_evdi);
+ if (length < 0){
+ LOGERR("get usb status error - %d", length);
+ length = 0;
+ }
+
+ LXT_MESSAGE* packet = (LXT_MESSAGE*)p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = length;
+ packet->group = STATUS;
+ packet->action = USB_STATUS;
+
+ return message;
+}
+
+char* get_earjack_status(void* p, bool is_evdi)
+{
+ char* message = __tmpalloc(5);
+ int length = get_file_status(message, PATH_JACK_EARJACK, 5, is_evdi);
+ if (length < 0){
+ return 0;
+ }
+
+ LXT_MESSAGE* packet = (LXT_MESSAGE*)p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = length;
+ packet->group = STATUS;
+ packet->action = EARJACK_STATUS;
+
+ return message;
+}
+
+char* get_rssi_level(void* p, bool is_evdi)
+{
+ char* message = __tmpalloc(5);
+ int length = get_vconf_status(message, "memory/telephony/rssi", 5, is_evdi);
+ if (length < 0){
+ return 0;
+ }
+
+ LXT_MESSAGE* packet = (LXT_MESSAGE*)p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = length;
+ packet->group = STATUS;
+ packet->action = RSSI_LEVEL;
+
+ return message;
+}
+
+char* get_location_status(void* p, bool is_evdi)
+{
+ int mode;
+ int ret = vconf_get_int("db/location/replay/ReplayMode", &mode);
+ if (ret != 0) {
+ return 0;
+ }
+
+ char* message = 0;
+
+ if (mode == 0)
+ { // STOP
+ message = (char*)malloc(5);
+ memset(message, 0, 5);
+
+ ret = sprintf(message, "%d", mode);
+ if (ret < 0) {
+ free(message);
+ message = 0;
+ return 0;
+ }
+ }
+ else if (mode == 1)
+ { // NMEA MODE(LOG MODE)
+ char* temp = 0;
+ temp = (char*) vconf_get_str("db/location/replay/FileName");
+ if (temp == 0) {
+ //free(temp);
+ return 0;
+ }
+
+ message = (char*)malloc(256);
+ memset(message, 0, 256);
+ ret = sprintf(message, "%d,%s", mode, temp);
+ if (ret < 0) {
+ free(message);
+ message = 0;
+ return 0;
+ }
+ } else if (mode == 2) { // MANUAL MODE
+ double latitude;
+ double logitude;
+ ret = vconf_get_dbl("db/location/replay/ManualLatitude", &latitude);
+ if (ret != 0) {
+ return 0;
+ }
+ ret = vconf_get_dbl("db/location/replay/ManualLongitude", &logitude);
+ if (ret != 0) {
+ return 0;
+ }
+ message = (char*)malloc(128);
+ memset(message, 0, 128);
+ ret = sprintf(message, "%d,%f,%f", mode, latitude, logitude);
+ if (ret < 0) {
+ free(message);
+ message = 0;
+ return 0;
+ }
+ }
+
+ LXT_MESSAGE* packet = (LXT_MESSAGE*)p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = strlen(message);
+ packet->group = STATUS;
+ packet->action = LOCATION_STATUS;
+
+ return message;
+}
+
--- /dev/null
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Jinhyung Choi <jinhyung2.choi@samsnung.com>
+ * DaiYoung Kim <daiyoung777.kim@samsnung.com>
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#include "emuld_common.h"
+#include "emuld.h"
+#include "synbuf.h"
+#ifdef MOBILE
+#include "pmapi.h"
+#endif
+
+#ifdef WEARABLE
+#include "deviced/dd-display.h"
+#endif
+
+#define PMAPI_RETRY_COUNT 3
+#define MAX_CONNECT_TRY_COUNT (60 * 3)
+#define SRV_IP "10.0.2.2"
+
+/* global definition */
+#ifdef MOBILE
+unsigned short vmodem_port = VMODEM_PORT;
+#endif
+
+#ifdef WEARABLE
+unsigned short pedometer_port = PEDOMETER_PORT;
+static int g_pedometer_connect_status;/* connection status between emuld and pedometer daemon */
+static pthread_mutex_t mutex_pedometerconnect = PTHREAD_MUTEX_INITIALIZER;
+#endif
+
+/* global server port number */
+int g_svr_port;
+
+/* connection status between emuld and vmodem */
+static int g_vm_connect_status;
+
+pthread_t tid[MAX_CLIENT + 1];
+
+/* udp socket */
+struct sockaddr_in si_sensord_other;
+
+int g_fd[fdtype_max];
+
+typedef std::queue<msg_info*> __msg_queue;
+
+__msg_queue g_msgqueue;
+
+int g_epoll_fd;
+
+static pthread_mutex_t mutex_vmconnect = PTHREAD_MUTEX_INITIALIZER;
+
+struct epoll_event g_events[MAX_EVENTS];
+
+bool exit_flag = false;
+
+/*----------------------------------------------------------------*/
+/* FUNCTION PART */
+/* ---------------------------------------------------------------*/
+
+void systemcall(const char* param)
+{
+ if (!param)
+ return;
+
+ if (system(param) == -1)
+ LOGERR("system call failure(command = %s)", param);
+}
+
+#ifdef MOBILE
+void set_lock_state(int state) {
+ int i = 0;
+ int ret = 0;
+ // Now we blocking to enter "SLEEP".
+ while(i < PMAPI_RETRY_COUNT ) {
+ if (state == SUSPEND_LOCK) {
+ ret = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
+ } else {
+ ret = pm_unlock_state(LCD_NORMAL, PM_RESET_TIMER);
+ }
+ LOGINFO("pm_lock/unlock_state() return: %d", ret);
+ if(ret == 0)
+ {
+ break;
+ }
+ ++i;
+ sleep(10);
+ }
+ if (i == PMAPI_RETRY_COUNT) {
+ LOGERR("Emulator Daemon: Failed to call pm_lock/unlock_state().");
+ }
+}
+#endif
+
+/*---------------------------------------------------------------
+function : init_data0
+io: none
+desc: initialize global client structure values
+----------------------------------------------------------------*/
+static void init_data0(void)
+{
+ register int i;
+
+ for(i = 0 ; i < fdtype_max ; i++)
+ {
+ g_fd[i] = -1;
+ }
+}
+
+bool is_vm_connected(void)
+{
+ _auto_mutex _(&mutex_vmconnect);
+
+ if (g_vm_connect_status != 1)
+ return false;
+
+ return true;
+}
+
+static void set_vm_connect_status(const int v)
+{
+ _auto_mutex _(&mutex_vmconnect);
+
+ g_vm_connect_status = v;
+}
+
+#ifdef WEARABLE
+
+bool is_pedometer_connected(void)
+{
+ _auto_mutex _(&mutex_pedometerconnect);
+
+ if (g_pedometer_connect_status != 1)
+ return false;
+
+ return true;
+}
+
+void set_pedometer_connect_status(const int v)
+{
+ _auto_mutex _(&mutex_pedometerconnect);
+
+ g_pedometer_connect_status = v;
+}
+
+void* init_pedometer_connect(void* data)
+{
+ struct sockaddr_in pedometer_addr;
+ int ret = -1;
+
+ set_pedometer_connect_status(0);
+
+ LOGINFO("init_pedometer_connect start\n");
+
+ pthread_detach(pthread_self());
+ /* Open TCP Socket */
+ if ((g_fd[fdtype_pedometer] = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ {
+ LOGERR("Server Start Fails. : Can't open stream socket \n");
+ exit(0);
+ }
+
+ /* Address Setting */
+ memset( &pedometer_addr , 0 , sizeof(pedometer_addr)) ;
+
+ pedometer_addr.sin_family = AF_INET;
+ pedometer_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
+ pedometer_addr.sin_port = htons(pedometer_port);
+
+ while (ret < 0 && !exit_flag)
+ {
+ ret = connect(g_fd[fdtype_pedometer], (struct sockaddr *)&pedometer_addr, sizeof(pedometer_addr));
+
+ LOGDEBUG("pedometer_sockfd: %d, connect ret: %d\n", g_fd[fdtype_pedometer], ret);
+
+ if(ret < 0) {
+ LOGDEBUG("connection failed to pedometer! try \n");
+ sleep(1);
+ }
+ }
+
+ epoll_ctl_add(g_fd[fdtype_pedometer]);
+
+ set_pedometer_connect_status(1);
+
+ pthread_exit((void *) 0);
+}
+
+void recv_from_pedometer(int fd)
+{
+ printf("recv_from_pedometer\n");
+
+ ijcommand ijcmd;
+ if (!read_ijcmd(fd, &ijcmd))
+ {
+ LOGERR("fail to read ijcmd\n");
+
+ set_pedometer_connect_status(0);
+
+ close(fd);
+
+ if (pthread_create(&tid[3], NULL, init_pedometer_connect, NULL) != 0)
+ {
+ LOGERR("pthread create fail!");
+ }
+ return;
+ }
+
+ LOGDEBUG("pedometer data length: %d", ijcmd.msg.length);
+ const int tmplen = HEADER_SIZE + ijcmd.msg.length;
+ char* tmp = (char*) malloc(tmplen);
+
+ if (tmp)
+ {
+ memcpy(tmp, &ijcmd.msg, HEADER_SIZE);
+ if (ijcmd.msg.length > 0)
+ memcpy(tmp + HEADER_SIZE, ijcmd.data, ijcmd.msg.length);
+
+ if(!ijmsg_send_to_evdi(g_fd[fdtype_device], IJTYPE_PEDOMETER, (const char*) tmp, tmplen)) {
+ LOGERR("msg_send_to_evdi: failed\n");
+ }
+
+ free(tmp);
+ }
+}
+
+#endif
+
+bool epoll_ctl_add(const int fd)
+{
+ struct epoll_event events;
+
+ events.events = EPOLLIN; // check In event
+ events.data.fd = fd;
+
+ if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &events) < 0 )
+ {
+ LOGERR("Epoll control fails.");
+ return false;
+ }
+
+ LOGINFO("[START] epoll events add fd success for server");
+ return true;
+}
+
+static void emuld_ready()
+{
+ char buf[16];
+
+ struct sockaddr_in si_other;
+ int s, slen=sizeof(si_other);
+ int port;
+ char *ptr;
+ char *temp_sdbport;
+ temp_sdbport = getenv("sdb_port");
+ if(temp_sdbport == NULL) {
+ LOGERR("failed to get env variable from sdb_port");
+ return;
+ }
+
+ port = strtol(temp_sdbport, &ptr, 10);
+ port = port + 3;
+
+ LOGINFO("guest_server port: %d", port);
+
+ if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1){
+ LOGERR("socket error!");
+ return;
+ }
+
+ memset((char *) &si_other, 0, sizeof(si_other));
+ si_other.sin_family = AF_INET;
+ si_other.sin_port = htons(port);
+ if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
+ LOGERR("inet_aton() failed");
+ }
+
+ memset(buf, '\0', sizeof(buf));
+
+ sprintf(buf, "5\n");
+
+ LOGINFO("send message 5\\n to guest server");
+
+ while(sendto(s, buf, sizeof(buf), 0, (struct sockaddr*)&si_other, slen) == -1)
+ {
+ LOGERR("sendto error! retry sendto");
+ usleep(1000);
+ }
+ LOGINFO("emuld is ready.");
+
+ close(s);
+
+}
+
+/*-------------------------------------------------------------
+function: init_server0
+io: input : integer - server port (must be positive)
+output: none
+desc : tcp/ip listening socket setting with input variable
+----------------------------------------------------------------*/
+
+static bool init_server0(int svr_port, int* ret_fd)
+{
+ struct sockaddr_in serv_addr;
+ int fd;
+
+ *ret_fd = -1;
+
+ /* Open TCP Socket */
+ if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ {
+ LOGERR("Server Start Fails. : Can't open stream socket");
+ return false;
+ }
+
+ /* Address Setting */
+ memset(&serv_addr , 0 , sizeof(serv_addr)) ;
+
+ serv_addr.sin_family = AF_INET;
+ serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
+ serv_addr.sin_port = htons(svr_port);
+
+ /* Set Socket Option */
+ int nSocketOpt = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &nSocketOpt, sizeof(nSocketOpt)) < 0)
+ {
+ LOGERR("Server Start Fails. : Can't set reuse address");
+ goto fail;
+ }
+
+ /* Bind Socket */
+ if (bind(fd,(struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
+ {
+ LOGERR("Server Start Fails. : Can't bind local address");
+ goto fail;
+ }
+
+ /* Listening */
+ if (listen(fd, 15) < 0) /* connection queue is 15. */
+ {
+ LOGERR("Server Start Fails. : listen failure");
+ goto fail;
+ }
+ LOGINFO("[START] Now Server listening on port %d, EMdsockfd: %d"
+ ,svr_port, fd);
+
+ /* notify to qemu that emuld is ready */
+ emuld_ready();
+
+ if (!epoll_ctl_add(fd))
+ {
+ LOGERR("Epoll control fails.");
+ goto fail;
+ }
+
+ *ret_fd = fd;
+
+ return true;
+fail:
+ close(fd);
+ return false;
+}
+/*------------------------------- end of function init_server0 */
+
+#ifdef MOBILE
+static void* init_vm_connect(void* data)
+{
+ struct sockaddr_in vm_addr;
+ int ret = -1;
+
+ set_vm_connect_status(0);
+
+ LOGINFO("init_vm_connect start");
+
+ pthread_detach(pthread_self());
+ /* Open TCP Socket */
+ if ((g_fd[fdtype_vmodem] = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ {
+ LOGERR("Server Start Fails. : Can't open stream socket.");
+ exit(0);
+ }
+
+ /* Address Setting */
+ memset( &vm_addr , 0 , sizeof(vm_addr));
+
+ vm_addr.sin_family = AF_INET;
+ vm_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
+ vm_addr.sin_port = htons(vmodem_port);
+
+ while (ret < 0 && !exit_flag)
+ {
+ ret = connect(g_fd[fdtype_vmodem], (struct sockaddr *)&vm_addr, sizeof(vm_addr));
+
+ LOGDEBUG("vm_sockfd: %d, connect ret: %d", g_fd[fdtype_vmodem], ret);
+
+ if(ret < 0) {
+ LOGERR("connection failed to vmodem! try.");
+ sleep(1);
+ }
+ }
+
+ epoll_ctl_add(g_fd[fdtype_vmodem]);
+
+ set_vm_connect_status(1);
+
+ pthread_exit((void *) 0);
+}
+#endif
+
+static bool epoll_init(void)
+{
+ g_epoll_fd = epoll_create(MAX_EVENTS); // create event pool
+ if(g_epoll_fd < 0)
+ {
+ LOGERR("Epoll create Fails.");
+ return false;
+ }
+
+ LOGINFO("[START] epoll creation success");
+ return true;
+}
+
+
+
+/*------------------------------- end of function epoll_init */
+
+
+int parse_val(char *buff, unsigned char data, char *parsbuf)
+{
+ int count=0;
+ while(1)
+ {
+ if(count > 40)
+ return -1;
+ if(buff[count] == data)
+ {
+ count++;
+ strncpy(parsbuf, buff, count);
+ return count;
+ }
+ count++;
+ }
+
+ return 0;
+}
+
+static int recv_data(int event_fd, char** r_databuf, int size)
+{
+ int recvd_size = 0;
+ int len = 0;
+ int getcnt = 0;
+ char* r_tmpbuf = NULL;
+ const int alloc_size = sizeof(char) * size + 1;
+
+ r_tmpbuf = (char*)malloc(alloc_size);
+ if(r_tmpbuf == NULL)
+ {
+ return -1;
+ }
+
+ char* databuf = (char*)malloc(alloc_size);
+ if(databuf == NULL)
+ {
+ free(r_tmpbuf);
+ *r_databuf = NULL;
+ return -1;
+ }
+
+ memset(databuf, '\0', alloc_size);
+
+ while(recvd_size < size)
+ {
+ memset(r_tmpbuf, '\0', alloc_size);
+ len = recv(event_fd, r_tmpbuf, size - recvd_size, 0);
+ if (len < 0) {
+ break;
+ }
+
+ memcpy(databuf + recvd_size, r_tmpbuf, len);
+ recvd_size += len;
+ getcnt++;
+ if(getcnt > MAX_GETCNT) {
+ break;
+ }
+ }
+ free(r_tmpbuf);
+ r_tmpbuf = NULL;
+
+ *r_databuf = databuf;
+
+ return recvd_size;
+}
+
+int read_header(int fd, LXT_MESSAGE* packet)
+{
+ char* readbuf = NULL;
+ int readed = recv_data(fd, &readbuf, HEADER_SIZE);
+ if (readed <= 0)
+ return 0;
+ memcpy((void*) packet, (void*) readbuf, HEADER_SIZE);
+
+ if (readbuf)
+ {
+ free(readbuf);
+ readbuf = NULL;
+ }
+ return readed;
+}
+
+
+bool read_ijcmd(const int fd, ijcommand* ijcmd)
+{
+ int readed;
+ readed = read_header(fd, &ijcmd->msg);
+
+ LOGDEBUG("action: %d", ijcmd->msg.action);
+ LOGDEBUG("length: %d", ijcmd->msg.length);
+
+ if (readed <= 0)
+ return false;
+
+ // TODO : this code should removed, for telephony
+ if (ijcmd->msg.length == 0)
+ {
+ if (ijcmd->msg.action == 71) // that's strange packet from telephony initialize
+ {
+ ijcmd->msg.length = 4;
+ }
+ }
+
+ if (ijcmd->msg.length <= 0)
+ return true;
+
+ if (ijcmd->msg.length > 0)
+ {
+ readed = recv_data(fd, &ijcmd->data, ijcmd->msg.length);
+ if (readed <= 0)
+ {
+ free(ijcmd->data);
+ ijcmd->data = NULL;
+ return false;
+ }
+
+ }
+ return true;
+}
+
+bool read_id(const int fd, ijcommand* ijcmd)
+{
+ char* readbuf = NULL;
+ int readed = recv_data(fd, &readbuf, ID_SIZE);
+
+ LOGDEBUG("read_id : receive size: %d", readed);
+
+ if (readed <= 0)
+ {
+ free(readbuf);
+ readbuf = NULL;
+ return false;
+ }
+
+ LOGDEBUG("identifier: %s", readbuf);
+#ifdef MOBILE
+ memset(ijcmd->cmd, '\0', sizeof(ijcmd->cmd));
+ int parselen = parse_val(readbuf, 0x0a, ijcmd->cmd);
+
+ LOGDEBUG("parse_len: %d, buf = %s, fd=%d", parselen, ijcmd->cmd, fd);
+#endif
+ if (readbuf)
+ {
+ free(readbuf);
+ readbuf = NULL;
+ }
+
+ return true;
+}
+
+#ifdef MOBILE
+void recv_from_vmodem(int fd)
+{
+ LOGDEBUG("recv_from_vmodem");
+
+ ijcommand ijcmd;
+ if (!read_ijcmd(fd, &ijcmd))
+ {
+ LOGINFO("fail to read ijcmd");
+
+ set_vm_connect_status(0);
+
+ close(fd);
+
+ if (pthread_create(&tid[0], NULL, init_vm_connect, NULL) != 0)
+ {
+ LOGERR("pthread create fail!");
+ }
+ return;
+ }
+
+ LOGDEBUG("vmodem data length: %d", ijcmd.msg.length);
+ const int tmplen = HEADER_SIZE + ijcmd.msg.length;
+ char* tmp = (char*) malloc(tmplen);
+
+ if (tmp)
+ {
+ memcpy(tmp, &ijcmd.msg, HEADER_SIZE);
+ if (ijcmd.msg.length > 0)
+ memcpy(tmp + HEADER_SIZE, ijcmd.data, ijcmd.msg.length);
+
+ if(!ijmsg_send_to_evdi(g_fd[fdtype_device], IJTYPE_TELEPHONY, (const char*) tmp, tmplen)) {
+ LOGERR("msg_send_to_evdi: failed");
+ }
+
+ free(tmp);
+ }
+
+ // send header to ij
+ if (is_ij_exist())
+ {
+ send_to_all_ij((char*) &ijcmd.msg, HEADER_SIZE);
+
+ if (ijcmd.msg.length > 0)
+ {
+ send_to_all_ij((char*) ijcmd.data, ijcmd.msg.length);
+ }
+ }
+}
+#endif
+
+void recv_from_ij(int fd)
+{
+ LOGDEBUG("recv_from_ij");
+
+ ijcommand ijcmd;
+
+ if (!read_id(fd, &ijcmd))
+ {
+ close_cli(fd);
+ return;
+ }
+
+ // TODO : if recv 0 then close client
+
+ if (!read_ijcmd(fd, &ijcmd))
+ {
+ LOGERR("fail to read ijcmd");
+ return;
+ }
+
+
+ if (strncmp(ijcmd.cmd, "telephony", 9) == 0)
+ {
+ msgproc_telephony(fd, &ijcmd, false);
+ }
+ else if (strncmp(ijcmd.cmd, "sensor", 6) == 0)
+ {
+ msgproc_sensor(fd, &ijcmd, false);
+ }
+ else if (strncmp(ijcmd.cmd, "location", 8) == 0)
+ {
+ msgproc_location(fd, &ijcmd, false);
+ }
+ else if (strncmp(ijcmd.cmd, "system", 6) == 0)
+ {
+ msgproc_system(fd, &ijcmd, false);
+ }
+ else if (strncmp(ijcmd.cmd, "sdcard", 6) == 0)
+ {
+ msgproc_sdcard(fd, &ijcmd, false);
+ }
+ else
+ {
+ LOGERR("Unknown packet: %s", ijcmd.cmd);
+ close_cli (fd);
+ }
+}
+
+static bool accept_proc(const int server_fd)
+{
+ struct sockaddr_in cli_addr;
+ int cli_sockfd;
+ int cli_len = sizeof(cli_addr);
+
+ cli_sockfd = accept(server_fd, (struct sockaddr *)&cli_addr,(socklen_t *)&cli_len);
+ if(cli_sockfd < 0)
+ {
+ LOGERR("accept error");
+ return false;
+ }
+ else
+ {
+ LOGINFO("[Accept] New client connected. fd:%d, port:%d"
+ ,cli_sockfd, cli_addr.sin_port);
+
+ clipool_add(cli_sockfd, cli_addr.sin_port, fdtype_ij);
+ epoll_ctl_add(cli_sockfd);
+ }
+ return true;
+}
+
+#ifdef MOBILE
+static void msgproc_suspend(int fd, ijcommand* ijcmd, bool evdi)
+{
+ if (ijcmd->msg.action == SUSPEND_LOCK) {
+ set_lock_state(SUSPEND_LOCK);
+ } else {
+ set_lock_state(SUSPEND_UNLOCK);
+ }
+
+ LOGINFO("[Suspend] Set lock state as %d (1: lock, other: unlock)", ijcmd->msg.action);
+}
+
+static void send_default_suspend_req(void)
+{
+ LXT_MESSAGE* packet = (LXT_MESSAGE*)malloc(sizeof(LXT_MESSAGE));
+ if(packet == NULL){
+ return;
+ }
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+
+ packet->length = 0;
+ packet->group = 5;
+ packet->action = 15;
+
+ int tmplen = HEADER_SIZE;
+ char* tmp = (char*) malloc(tmplen);
+ if (!tmp)
+ return;
+
+ memcpy(tmp, packet, HEADER_SIZE);
+
+ ijmsg_send_to_evdi(g_fd[fdtype_device], IJTYPE_SUSPEND, (const char*) tmp, tmplen);
+
+
+ if (tmp)
+ free(tmp);
+ if (packet)
+ free(packet);
+}
+#endif
+
+static synbuf g_synbuf;
+
+void process_evdi_command(ijcommand* ijcmd)
+{
+ int fd = -1;
+
+ if (strncmp(ijcmd->cmd, "sensor", 6) == 0)
+ {
+ msgproc_sensor(fd, ijcmd, true);
+ }
+#ifdef MOBILE
+ else if (strncmp(ijcmd->cmd, "telephony", 9) == 0)
+ {
+ msgproc_telephony(fd, ijcmd, true);
+ }
+ else if (strncmp(ijcmd->cmd, "suspend", 7) == 0)
+ {
+ msgproc_suspend(fd, ijcmd, true);
+ }
+#endif
+#ifdef WEARABLE
+ else if (strncmp(ijcmd->cmd, "pedometer", 9) == 0)
+ {
+ msgproc_pedometer(fd, ijcmd, true);
+ }
+#endif
+ else if (strncmp(ijcmd->cmd, "location", 8) == 0)
+ {
+ msgproc_location(fd, ijcmd, true);
+ }
+ else if (strncmp(ijcmd->cmd, "system", 6) == 0)
+ {
+ msgproc_system(fd, ijcmd, true);
+ }
+ else if (strncmp(ijcmd->cmd, "sdcard", 6) == 0)
+ {
+ msgproc_sdcard(fd, ijcmd, true);
+ }
+ else
+ {
+ LOGERR("Unknown packet: %s", ijcmd->cmd);
+ }
+}
+
+static void recv_from_evdi(evdi_fd fd)
+{
+ LOGDEBUG("recv_from_evdi");
+ int readed;
+
+ struct msg_info _msg;
+ int to_read = sizeof(struct msg_info);
+
+ memset(&_msg, 0x00, sizeof(struct msg_info));
+
+
+ while (1)
+ {
+ readed = read(fd, &_msg, to_read);
+ if (readed == -1) // TODO : error handle
+ {
+ if (errno != EAGAIN)
+ {
+ LOGERR("EAGAIN");
+ return;
+ }
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ LOGDEBUG("total readed = %d, read count = %d, index = %d, use = %d, msg = %s",
+ readed, _msg.count, _msg.index, _msg.use, _msg.buf);
+
+ g_synbuf.reset_buf();
+ g_synbuf.write(_msg.buf, _msg.use);
+
+
+ ijcommand ijcmd;
+ readed = g_synbuf.read(ijcmd.cmd, ID_SIZE);
+
+ LOGDEBUG("ij id : %s", ijcmd.cmd);
+
+ // TODO : check
+ if (readed < ID_SIZE)
+ return;
+
+ // read header
+ readed = g_synbuf.read((char*)&ijcmd.msg, HEADER_SIZE);
+ if (readed < HEADER_SIZE)
+ return;
+#ifdef MOBILE
+ int act = ijcmd.msg.action;
+ int grp = ijcmd.msg.group;
+ int len = ijcmd.msg.length;
+
+
+ LOGDEBUG("HEADER : action = %d, group = %d, length = %d", act, grp, len);
+#endif
+ if (ijcmd.msg.length > 0)
+ {
+ ijcmd.data = (char*) malloc(ijcmd.msg.length);
+ if (!ijcmd.data)
+ {
+ LOGERR("failed to allocate memory");
+ return;
+ }
+ readed = g_synbuf.read(ijcmd.data, ijcmd.msg.length);
+
+ LOGDEBUG("DATA : %s", ijcmd.data);
+
+ if (readed < ijcmd.msg.length)
+ {
+ LOGERR("received data is insufficient");
+ //return;
+ }
+ }
+
+ process_evdi_command(&ijcmd);
+}
+
+static bool server_process(void)
+{
+ int i,nfds;
+ int fd_tmp;
+
+ nfds = epoll_wait(g_epoll_fd, g_events, MAX_EVENTS, 100);
+
+ if (nfds == -1 && errno != EAGAIN && errno != EINTR)
+ {
+ LOGERR("epoll wait(%d)", errno);
+ return true;
+ }
+
+ for( i = 0 ; i < nfds ; i++ )
+ {
+ fd_tmp = g_events[i].data.fd;
+ if (fd_tmp == g_fd[fdtype_server])
+ {
+ accept_proc(fd_tmp);
+ }
+#ifdef WEARABLE
+ else if (fd_tmp == g_fd[fdtype_pedometer])
+ {
+ recv_from_pedometer(fd_tmp);
+ }
+#endif
+ else if (fd_tmp == g_fd[fdtype_device])
+ {
+ recv_from_evdi(fd_tmp);
+ }
+#ifdef MOBILE
+ else if(fd_tmp == g_fd[fdtype_vmodem])
+ {
+ recv_from_vmodem(fd_tmp);
+ }
+#endif
+ else
+ {
+ recv_from_ij(fd_tmp);
+ }
+ }
+
+ return false;
+}
+
+#ifdef WEARABLE
+void set_display_lock_state() {
+ int i = 0;
+ int ret = 0;
+ // Now we blocking to enter "SLEEP".
+ while(i < PMAPI_RETRY_COUNT ) {
+ ret = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
+ LOGINFO("display_lock_state() return: %d", ret);
+ if(ret == 0)
+ {
+ break;
+ }
+ ++i;
+ sleep(10);
+ }
+ if (i == PMAPI_RETRY_COUNT) {
+ LOGERR("Emulator Daemon: Failed to call display_lock_state().\n");
+ }
+}
+#endif
+
+int main( int argc , char *argv[])
+{
+ int state;
+
+#ifdef WEARABLE
+ int pedometer_state;
+#endif
+
+ LOGINFO("emuld start");
+ /* entry , argument check and process */
+ if(argc < 3){
+ g_svr_port = DEFAULT_PORT;
+ }else {
+ if(strcmp("-port", argv[1]) == 0 ) {
+ g_svr_port = atoi(argv[2]);
+ if(g_svr_port < 1024) {
+ LOGERR("[STOP] port number invalid : %d",g_svr_port);
+ exit(0);
+ }
+ }
+ }
+
+ init_data0();
+
+ if (!epoll_init())
+ {
+ exit(0);
+ }
+
+ if (!init_server0(g_svr_port, &g_fd[fdtype_server]))
+ {
+ close(g_epoll_fd);
+ exit(0);
+ }
+
+ if (!init_device(&g_fd[fdtype_device]))
+ {
+ close(g_epoll_fd);
+ exit(0);
+ }
+
+ LOGINFO("[START] epoll events set success for server");
+
+ set_vm_connect_status(0);
+
+#ifdef MOBILE
+ if(pthread_create(&tid[0], NULL, init_vm_connect, NULL) != 0)
+ {
+ LOGERR("pthread create fail!");
+ close(g_epoll_fd);
+ exit(0);
+ }
+
+ send_default_suspend_req();
+#endif
+
+#ifdef WEARABLE
+ if(pthread_create(&tid[3], NULL, init_pedometer_connect, NULL) != 0)
+ {
+ LOGERR("pthread create fail!");
+ close(g_epoll_fd);
+ exit(0);
+ }
+
+ set_display_lock_state();
+#endif
+ bool is_exit = false;
+
+ while(!is_exit)
+ {
+ is_exit = server_process();
+ }
+
+ exit_flag = true;
+
+ if (!is_vm_connected())
+ {
+ int status;
+ pthread_join(tid[0], (void **)&status);
+ LOGINFO("vmodem thread end %d", status);
+ }
+
+ state = pthread_mutex_destroy(&mutex_vmconnect);
+ if (state != 0)
+ {
+ LOGERR("mutex_vmconnect is failed to destroy.");
+ }
+#ifdef WEARABLE
+ if (!is_pedometer_connected())
+ {
+ int status;
+ pthread_join(tid[3], (void **)&status);
+ LOGINFO("pedometer thread end %d\n", status);
+ }
+
+ pedometer_state = pthread_mutex_destroy(&mutex_pedometerconnect);
+ if (pedometer_state != 0)
+ {
+ LOGERR("mutex_pedometerconnect is failed to destroy.");
+ }
+
+#endif
+ stop_listen();
+
+ if (g_fd[fdtype_server])
+ close(g_fd[fdtype_server]);
+
+ LOGINFO("emuld exit");
+
+ return 0;
+}
+
--- /dev/null
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Jinhyung Choi <jinhyung2.choi@samsnung.com>
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * Daiyoung Kim <daiyoung777.kim@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+/*
+ * emuld_proc.cpp
+ *
+ * Created on: 2013. 4. 12.
+ * Author: dykim
+ */
+
+#include "emuld_common.h"
+#include "emuld.h"
+#include <errno.h>
+
+#include <sys/mount.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <mntent.h>
+
+char SDpath[256];
+static struct timeval tv_start_poweroff;
+
+struct setting_device_param
+{
+ setting_device_param() : get_status_sockfd(-1), ActionID(0), is_evdi(false)
+ {
+ memset(type_cmd, 0, ID_SIZE);
+ }
+ int get_status_sockfd;
+ unsigned char ActionID;
+ bool is_evdi;
+ char type_cmd[ID_SIZE];
+};
+
+struct mount_param
+{
+ mount_param(int _fd) : fd(_fd), is_evdi(false) {}
+ int fd;
+ bool is_evdi;
+};
+
+char* get_mount_info() {
+ struct mntent *ent;
+ FILE *aFile;
+
+ aFile = setmntent("/proc/mounts", "r");
+ if (aFile == NULL) {
+ LOGERR("/proc/mounts is not exist");
+ return NULL;
+ }
+ char* mountinfo = new char[512];
+ memset(mountinfo, 0, 512);
+
+ while (NULL != (ent = getmntent(aFile))) {
+
+ if (strcmp(ent->mnt_dir, "/opt/storage/sdcard") == 0)
+ {
+ LOGDEBUG(",%s,%s,%d,%s,%d,%s",
+ ent->mnt_fsname, ent->mnt_dir, ent->mnt_freq, ent->mnt_opts, ent->mnt_passno, ent->mnt_type);
+ sprintf(mountinfo,",%s,%s,%d,%s,%d,%s\n",
+ ent->mnt_fsname, ent->mnt_dir, ent->mnt_freq, ent->mnt_opts, ent->mnt_passno, ent->mnt_type);
+ break;
+ }
+ }
+ endmntent(aFile);
+
+ return mountinfo;
+}
+
+int is_mounted()
+{
+ int ret = -1, i = 0;
+ struct stat buf;
+ char file_name[128];
+ memset(file_name, '\0', sizeof(file_name));
+
+ for(i = 0; i < 10; i++)
+ {
+ sprintf(file_name, "/dev/mmcblk%d", i);
+ ret = access(file_name, F_OK);
+ if( ret == 0 )
+ {
+ lstat(file_name, &buf);
+ if(S_ISBLK(buf.st_mode))
+ return 1;
+ else
+ return 0;
+ }
+ }
+
+ return 0;
+}
+
+void* mount_sdcard(void* data)
+{
+ mount_param* param = (mount_param*) data;
+
+ int ret = -1, i = 0;
+ struct stat buf;
+ char file_name[128], command[256];
+ memset(file_name, '\0', sizeof(file_name));
+ memset(command, '\0', sizeof(command));
+
+ LXT_MESSAGE* packet = (LXT_MESSAGE*)malloc(sizeof(LXT_MESSAGE));
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+
+ LOGINFO("start sdcard mount thread");
+
+ pthread_detach(pthread_self());
+
+ while (ret < 0)
+ {
+ for (i = 0; i < 10; i++)
+ {
+ sprintf(file_name, "/dev/mmcblk%d", i);
+ ret = access( file_name, F_OK );
+ if( ret == 0 )
+ {
+ lstat(file_name, &buf);
+ if(!S_ISBLK(buf.st_mode))
+ {
+ sprintf(command, "rm -rf %s", file_name);
+ systemcall(command);
+ }
+ else
+ break;
+ }
+ }
+
+ if (i != 10)
+ {
+ LOGDEBUG( "%s is exist", file_name);
+ packet->length = strlen(SDpath); // length
+ packet->group = 11; // sdcard
+ if (ret == 0)
+ packet->action = 1; // mounted
+ else
+ packet->action = 5; // failed
+
+ //
+ LOGDEBUG("SDpath is %s", SDpath);
+
+ const int tmplen = HEADER_SIZE + packet->length;
+ char* tmp = (char*) malloc(tmplen);
+
+ if (tmp)
+ {
+ memcpy(tmp, packet, HEADER_SIZE);
+ if (packet->length > 0)
+ {
+ memcpy(tmp + HEADER_SIZE, SDpath, packet->length);
+ }
+
+ if (param->is_evdi)
+ {
+ ijmsg_send_to_evdi(g_fd[fdtype_device], IJTYPE_SDCARD, (const char*) tmp, tmplen);
+ }
+ else
+ {
+ send_to_cli(param->fd, (char*) tmp, tmplen);
+ }
+
+ free(tmp);
+ }
+
+ //
+
+ if (ret == 0) {
+ ret = system("/usr/bin/sys_event mmcblk_add");
+ }
+
+ break;
+ }
+ else
+ {
+ LOGERR( "%s is not exist", file_name);
+ sleep(1);
+ }
+ }
+
+ if(packet)
+ {
+ free(packet);
+ packet = NULL;
+ }
+
+ if (param)
+ {
+ delete param;
+ param = NULL;
+ }
+ pthread_exit((void *) 0);
+}
+
+int umount_sdcard(const int fd, bool is_evdi)
+{
+ int ret = -1, i = 0;
+ char file_name[128];
+ memset(file_name, '\0', sizeof(file_name));
+
+ LXT_MESSAGE* packet = (LXT_MESSAGE*)malloc(sizeof(LXT_MESSAGE));
+ if(packet == NULL){
+ return ret;
+ }
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+
+ LOGINFO("start sdcard umount");
+
+ pthread_cancel(tid[1]);
+
+ for (i = 0; i < 10; i++)
+ {
+ sprintf(file_name, "/dev/mmcblk%d", i);
+ ret = access(file_name, F_OK);
+ if (ret == 0)
+ {
+ if(fd != -1)
+ {
+ packet->length = strlen(SDpath); // length
+ packet->group = 11; // sdcard
+ packet->action = 0; // unmounted
+
+ send(fd, (void*)packet, sizeof(char) * HEADER_SIZE, 0);
+
+ LOGDEBUG("SDpath is %s", SDpath);
+ send(fd, SDpath, packet->length, 0);
+ }
+ else if (is_evdi)
+ {
+ LOGDEBUG("SDpath is %s", SDpath);
+
+ packet->length = strlen(SDpath); // length
+ packet->group = 11; // sdcard
+ packet->action = 0; // unmounted
+
+ const int tmplen = HEADER_SIZE + packet->length;
+ char* tmp = (char*) malloc(tmplen);
+ if (!tmp)
+ break;
+
+ memcpy(tmp, packet, HEADER_SIZE);
+ memcpy(tmp + HEADER_SIZE, SDpath, packet->length);
+
+ ijmsg_send_to_evdi(g_fd[fdtype_device], IJTYPE_SDCARD, (const char*) tmp, tmplen);
+
+ free(tmp);
+ }
+
+ memset(SDpath, '\0', sizeof(SDpath));
+ sprintf(SDpath, "umounted");
+ ret = system("/usr/bin/sys_event mmcblk_remove");
+
+ break;
+ }
+ else
+ {
+ LOGERR( "%s is not exist", file_name);
+ }
+ }
+
+ if(packet){
+ free(packet);
+ packet = NULL;
+ }
+ return ret;
+}
+
+
+int powerdown_by_force(void)
+{
+ struct timeval now;
+ int poweroff_duration = POWEROFF_DURATION;
+
+ gettimeofday(&now, NULL);
+ /* Waiting until power off duration and displaying animation */
+ while (now.tv_sec - tv_start_poweroff.tv_sec < poweroff_duration) {
+ LOGINFO("power down wait");
+ usleep(100000);
+ gettimeofday(&now, NULL);
+ }
+
+ LOGINFO("Power off by force");
+ LOGINFO("sync");
+
+ sync();
+
+ LOGINFO("poweroff");
+
+ reboot(RB_POWER_OFF);
+
+ return 1;
+}
+
+// location event
+char command[512];
+char latitude[128];
+char longitude[128];
+void setting_location(char* databuf)
+{
+ char* s = strchr(databuf, ',');
+ memset(command, 0, 256);
+ if (s == NULL) { // SET MODE
+ int mode = atoi(databuf);
+ switch (mode) {
+ case 0: // STOP MODE
+ sprintf(command, "vconftool set -t int db/location/replay/ReplayMode 0 -f");
+ break;
+ case 1: // NMEA MODE (LOG MODE)
+ sprintf(command, "vconftool set -t int db/location/replay/ReplayMode 1 -f");
+ break;
+ case 2: // MANUAL MODE
+ sprintf(command, "vconftool set -t int db/location/replay/ReplayMode 2 -f");
+ break;
+ default:
+ LOGERR("error(%s) : stop replay mode", databuf);
+ sprintf(command, "vconftool set -t int db/location/replay/ReplayMode 0 -f");
+ break;
+ }
+ LOGDEBUG("Location Command : %s", command);
+ systemcall(command);
+ } else {
+ *s = '\0';
+ int mode = atoi(databuf);
+ if(mode == 1) { // NMEA MODE (LOG MODE)
+ sprintf(command, "vconftool set -t string db/location/replay/FileName \"%s\"", s+1);
+ LOGDEBUG("%s", command);
+ systemcall(command);
+ memset(command, 0, 256);
+ sprintf(command, "vconftool set -t int db/location/replay/ReplayMode 1 -f");
+ LOGDEBUG("%s", command);
+ systemcall(command);
+ } else if(mode == 2) {
+ memset(latitude, 0, 128);
+ memset(longitude, 0, 128);
+ char* t = strchr(s+1, ',');
+ *t = '\0';
+ strcpy(latitude, s+1);
+ strcpy(longitude, t+1);
+ //strcpy(longitude, s+1);
+ //strcpy(latitude, databuf);
+ // Latitude
+ sprintf(command, "vconftool set -t double db/location/replay/ManualLatitude %s -f", latitude);
+ LOGDEBUG("%s", command);
+ systemcall(command);
+
+ // Longitude
+ sprintf(command, "vconftool set -t double db/location/replay/ManualLongitude %s -f", longitude);
+ LOGDEBUG("%s", command);
+ systemcall(command);
+ }
+ }
+}
+
+
+void* setting_device(void* data)
+{
+ pthread_detach(pthread_self());
+
+ setting_device_param* param = (setting_device_param*) data;
+
+ if (!param)
+ return 0;
+
+ int sockfd = param->get_status_sockfd;
+
+ bool is_evdi = param->is_evdi;
+
+ char* msg = 0;
+ LXT_MESSAGE* packet = (LXT_MESSAGE*)malloc(sizeof(LXT_MESSAGE));
+
+ switch(param->ActionID)
+ {
+ case USB_STATUS:
+ msg = get_usb_status((void*)packet, is_evdi);
+ if (msg == 0) {
+ LOGERR("failed getting usb status");
+ }
+ break;
+ case EARJACK_STATUS:
+ msg = get_earjack_status((void*)packet, is_evdi);
+ if (msg == 0) {
+ LOGERR("failed getting earjack status");
+ }
+ break;
+ case RSSI_LEVEL:
+ msg = get_rssi_level((void*)packet, is_evdi);
+ if (msg == 0) {
+ LOGERR("failed getting rssi level");
+ }
+ break;
+ case MOTION_VALUE:
+ LOGERR("not support getting motion value");
+ break;
+ case LOCATION_STATUS:
+ msg = get_location_status((void*)packet, is_evdi);
+ if (msg == 0) {
+ LOGERR("failed getting location status");
+ }
+ break;
+ default:
+ LOGERR("Wrong action ID. %d", param->ActionID);
+ break;
+ }
+
+ if (sockfd != -1)
+ {
+ if (msg != 0)
+ {
+ LOGDEBUG("send data to injector");
+ }
+ else
+ {
+ LOGERR("send error message to injector");
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = 0;
+ packet->group = STATUS;
+ packet->action = param->ActionID;
+ }
+
+ send(sockfd, (void*)packet, sizeof(char) * HEADER_SIZE, 0);
+
+ if (packet->length > 0)
+ {
+ send(sockfd, msg, packet->length, 0);
+ }
+ }
+ else if (param->is_evdi)
+ {
+ if (msg == 0)
+ {
+ LOGDEBUG("send error message to injector");
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = 0;
+ packet->group = STATUS;
+ packet->action = param->ActionID;
+ }
+ else
+ {
+ LOGDEBUG("send data to injector");
+ }
+
+ const int tmplen = HEADER_SIZE + packet->length;
+ char* tmp = (char*) malloc(tmplen);
+ if (tmp)
+ {
+ memcpy(tmp, packet, HEADER_SIZE);
+ if (packet->length > 0)
+ memcpy(tmp + HEADER_SIZE, msg, packet->length);
+
+ ijmsg_send_to_evdi(g_fd[fdtype_device], param->type_cmd, (const char*) tmp, tmplen);
+
+ free(tmp);
+ }
+ }
+
+ if(msg != 0)
+ {
+ free(msg);
+ msg = 0;
+ }
+ if (packet)
+ {
+ free(packet);
+ packet = NULL;
+ }
+
+ if (param)
+ delete param;
+
+ pthread_exit((void *) 0);
+}
+
+
+// event injector message handlers
+
+bool msgproc_telephony(const int sockfd, ijcommand* ijcmd, const bool is_evdi)
+{
+ LOGDEBUG("msgproc_telephony");
+
+ if (!is_vm_connected())
+ return false;
+
+ int sent;
+ sent = send(g_fd[fdtype_vmodem], &ijcmd->msg, HEADER_SIZE, 0);
+ if (sent == -1)
+ {
+ perror("vmodem send error");
+ }
+
+ LOGDEBUG("sent to vmodem = %d, err = %d", sent, errno);
+
+ sent = send(g_fd[fdtype_vmodem], ijcmd->data, ijcmd->msg.length, 0);
+ if (sent == -1)
+ {
+ perror("vmodem send error");
+ }
+
+ LOGDEBUG("sent to vmodem = %d, err = %d", sent, errno);
+
+
+ return true;
+}
+
+#ifdef WEARABLE
+
+void print_binary(const char* data, const int len)
+{
+ int i;
+ printf("[DATA: ");
+ for(i = 0; i < len; i++) {
+ if(i == len - 1) {
+ printf("%02x]\n", data[i]);
+ } else {
+ printf("%02x,", data[i]);
+ }
+ }
+}
+
+bool msgproc_pedometer(const int sockfd, ijcommand* ijcmd, const bool is_evdi)
+{
+ int sent = 0;
+
+ if (!is_pedometer_connected())
+ return false;
+
+ print_binary(ijcmd->data, 18);
+
+ sent = send(g_fd[fdtype_pedometer], ijcmd->data, ijcmd->msg.length, 0);
+ if (sent == -1)
+ {
+ perror("pedometer send error");
+ }
+
+ LOGDEBUG("sent to pedometer daemon: %d byte", sent);
+
+ return true;
+}
+#endif
+
+bool msgproc_sensor(const int sockfd, ijcommand* ijcmd, const bool is_evdi)
+{
+ LOGDEBUG("msgproc_sensor");
+
+ if (ijcmd->msg.group == STATUS)
+ {
+ setting_device_param* param = new setting_device_param();
+ if (!param)
+ return false;
+
+ memset(param, 0, sizeof(*param));
+
+ param->get_status_sockfd = sockfd;
+ param->ActionID = ijcmd->msg.action;
+ param->is_evdi = is_evdi;
+ memcpy(param->type_cmd, ijcmd->cmd, ID_SIZE);
+
+ if (pthread_create(&tid[2], NULL, setting_device, (void*)param) != 0)
+ {
+ LOGERR("sensor pthread create fail!");
+ return false;
+ }
+
+ }
+ else
+ {
+ if (ijcmd->data != NULL && strlen(ijcmd->data) > 0) {
+ device_parser(ijcmd->data);
+ }
+ }
+ return true;
+}
+
+bool msgproc_location(const int sockfd, ijcommand* ijcmd, const bool is_evdi)
+{
+ LOGDEBUG("msgproc_location");
+ if (ijcmd->msg.group == STATUS)
+ {
+ setting_device_param* param = new setting_device_param();
+ if (!param)
+ return false;
+
+ param->get_status_sockfd = sockfd;
+ param->ActionID = ijcmd->msg.action;
+ param->is_evdi = is_evdi;
+ memcpy(param->type_cmd, ijcmd->cmd, ID_SIZE);
+
+ if (pthread_create(&tid[2], NULL, setting_device, (void*) param) != 0)
+ {
+ LOGERR("location pthread create fail!");
+ return false;
+ }
+ }
+ else
+ {
+ setting_location(ijcmd->data);
+ }
+ return true;
+}
+
+bool msgproc_system(const int sockfd, ijcommand* ijcmd, const bool is_evdi)
+{
+ LOGDEBUG("msgproc_system");
+
+ LOGINFO("/etc/rc.d/rc.shutdown, sync, reboot(RB_POWER_OFF)");
+
+ sync();
+
+ systemcall("/etc/rc.d/rc.shutdown &");
+
+ gettimeofday(&tv_start_poweroff, NULL);
+
+ powerdown_by_force();
+
+ return true;
+}
+
+bool msgproc_sdcard(const int sockfd, ijcommand* ijcmd, const bool is_evdi)
+{
+ LOGDEBUG("msgproc_sdcard");
+
+ const int tmpsize = ijcmd->msg.length;
+
+ char token[] = "\n";
+ char tmpdata[tmpsize];
+ memcpy(tmpdata, ijcmd->data, tmpsize);
+
+ char* ret = NULL;
+ ret = strtok(tmpdata, token);
+
+ LOGDEBUG("%s", ret);
+
+ int mount_val = atoi(ret);
+ int mount_status = 0;
+
+ switch (mount_val)
+ {
+ case 0: // umount
+ {
+ mount_status = umount_sdcard(sockfd, is_evdi);
+ if (mount_status == 0)
+ send_guest_server(ijcmd->data);
+ }
+ break;
+ case 1: // mount
+ {
+ memset(SDpath, '\0', sizeof(SDpath));
+ ret = strtok(NULL, token);
+ strcpy(SDpath, ret);
+ LOGDEBUG("sdcard path is %s", SDpath);
+
+ send_guest_server(ijcmd->data);
+
+ mount_param* param = new mount_param(sockfd);
+ if (!param)
+ break;
+
+ param->is_evdi = is_evdi;
+
+ if (pthread_create(&tid[1], NULL, mount_sdcard, (void*) param) != 0)
+ LOGERR("mount sdcard pthread create fail!");
+ }
+
+ break;
+ case 2: // mount status
+ {
+ mount_status = is_mounted();
+ LXT_MESSAGE* mntData = (LXT_MESSAGE*) malloc(sizeof(LXT_MESSAGE));
+ if (mntData == NULL)
+ {
+ break;
+ }
+ memset(mntData, 0, sizeof(LXT_MESSAGE));
+
+ mntData->length = strlen(SDpath); // length
+ mntData->group = 11; // sdcard
+
+ LOGDEBUG("SDpath is %s", SDpath);
+
+ switch (mount_status)
+ {
+ case 0:
+ {
+ mntData->action = 2; // umounted status
+
+ const int tmplen = HEADER_SIZE + mntData->length;
+ char* tmp = (char*) malloc(tmplen);
+
+ if (tmp)
+ {
+ memcpy(tmp, mntData, HEADER_SIZE);
+ if (mntData->length > 0)
+ {
+ memcpy(tmp + HEADER_SIZE, SDpath, mntData->length);
+ }
+
+ if (is_evdi)
+ {
+ ijmsg_send_to_evdi(g_fd[fdtype_device], IJTYPE_SDCARD, (const char*) tmp, tmplen);
+ }
+ else
+ {
+ send_to_cli(sockfd, (char*) tmp, tmplen);
+ }
+
+ free(tmp);
+ }
+
+ memset(SDpath, '\0', sizeof(SDpath));
+ sprintf(SDpath, "umounted");
+ }
+ break;
+ case 1:
+ {
+ mntData->action = 3; // mounted status
+
+ int mountinfo_size = 0;
+ char* mountinfo = get_mount_info();
+ if (mountinfo)
+ {
+ mountinfo_size = strlen(mountinfo);
+ }
+
+ const int tmplen = HEADER_SIZE + mntData->length + mountinfo_size;
+ char* tmp = (char*) malloc(tmplen);
+
+ if (tmp)
+ {
+ memcpy(tmp, mntData, HEADER_SIZE);
+ if (mntData->length > 0)
+ {
+ memcpy(tmp + HEADER_SIZE, SDpath, mntData->length);
+ }
+
+ if (mountinfo)
+ {
+ memcpy(tmp + HEADER_SIZE + mntData->length, mountinfo, mountinfo_size);
+ mntData->length += mountinfo_size;
+ memcpy(tmp, mntData, HEADER_SIZE);
+ delete mountinfo;
+ mountinfo = NULL;
+ }
+
+ if (is_evdi)
+ {
+ ijmsg_send_to_evdi(g_fd[fdtype_device], IJTYPE_SDCARD, (const char*) tmp, tmplen);
+ }
+ else
+ {
+ send_to_cli(sockfd, (char*) tmp, tmplen);
+ }
+
+ free(tmp);
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ free(mntData);
+ }
+ break;
+ default:
+ LOGERR("unknown data %s", ret);
+ break;
+ }
+ return true;
+}
+
+//sdcard event
+void send_guest_server(char* databuf)
+{
+ if (!databuf)
+ {
+ LOGERR("invalid data buf");
+ return;
+ }
+
+ char buf[32];
+ struct sockaddr_in si_other;
+ int s, slen=sizeof(si_other);
+ FILE* fd;
+ char fbuf[16];
+ int port = 3581;
+
+ fd = fopen(SDB_PORT_FILE, "r");
+ if(!fd)
+ {
+ LOGERR("fail to open %s file", SDB_PORT_FILE);
+ return;
+ }
+
+ if (fgets(fbuf, 16, fd))
+ {
+ port = atoi(fbuf) + 3;
+ }
+ else
+ {
+ LOGERR("guest port fgets failure");
+ return;
+ }
+
+ fclose(fd);
+
+ s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ if (s == -1)
+ {
+ LOGERR("socket error!");
+ return;
+ }
+
+ memset((char *) &si_other, 0, sizeof(si_other));
+ si_other.sin_family = AF_INET;
+ si_other.sin_port = htons(port);
+ if (inet_aton(SRV_IP, &si_other.sin_addr) == 0)
+ {
+ LOGERR("inet_aton() failed");
+ }
+
+ memset(buf, '\0', sizeof(buf));
+ snprintf(buf, sizeof(buf), "4\n%s", databuf);
+ LOGDEBUG("sendGuestServer msg: %s", buf);
+ if (sendto(s, buf, sizeof(buf), 0, (struct sockaddr*)&si_other, slen) == -1)
+ {
+ LOGERR("sendto error!");
+ }
+
+ close(s);
+}
+
--- /dev/null
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Jinhyung Choi <jinhyung2.choi@samsnung.com>
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * Daiyoung Kim <daiyoung777.kim@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#include "evdi.h"
+#include "emuld.h"
+
+#define DEVICE_NODE_PATH "/dev/evdi0"
+
+static pthread_mutex_t mutex_evdi = PTHREAD_MUTEX_INITIALIZER;
+
+evdi_fd open_device(void)
+{
+ evdi_fd fd;
+
+ fd = open(DEVICE_NODE_PATH, O_RDWR); //O_CREAT|O_WRONLY|O_TRUNC.
+ LOGDEBUG("evdi open fd is %d", fd);
+
+ if (fd <= 0) {
+ LOGERR("open %s fail", DEVICE_NODE_PATH);
+ return fd;
+ }
+
+ return fd;
+}
+
+bool set_nonblocking(evdi_fd fd)
+{
+ int opts;
+ opts= fcntl(fd, F_GETFL);
+ if (opts < 0)
+ {
+ LOGERR("F_GETFL fcntl failed");
+ return false;
+ }
+ opts = opts | O_NONBLOCK;
+ if (fcntl(fd, F_SETFL, opts) < 0)
+ {
+ LOGERR("NONBLOCK fcntl failed");
+ return false;
+ }
+ return true;
+}
+
+bool init_device(evdi_fd* ret_fd)
+{
+ evdi_fd fd;
+
+ *ret_fd = -1;
+
+ fd = open_device();
+ if (fd <= 0)
+ return false;
+
+ if (!set_nonblocking(fd))
+ {
+ close(fd);
+ return false;
+ }
+
+ if (!epoll_ctl_add(fd))
+ {
+ LOGERR("Epoll control fails.");
+ close(fd);
+ return false;
+ }
+
+ *ret_fd = fd;
+
+ return true;
+}
+
+bool send_to_evdi(evdi_fd fd, const char* data, const int len)
+{
+ LOGDEBUG("send to evdi client, len = %d", len);
+ int ret;
+
+ ret = write(fd, data, len);
+
+ LOGDEBUG("written bytes = %d", ret);
+
+ if (ret == -1)
+ return false;
+ return true;
+}
+
+bool ijmsg_send_to_evdi(evdi_fd fd, const char* cat, const char* data, const int len)
+{
+ _auto_mutex _(&mutex_evdi);
+
+ LOGDEBUG("ijmsg_send_to_evdi");
+
+ if (fd == -1)
+ return false;
+
+ char tmp[ID_SIZE];
+ memset(tmp, 0, ID_SIZE);
+ strncpy(tmp, cat, 10);
+
+ // TODO: need to make fragmented transmission
+ if (len + ID_SIZE > __MAX_BUF_SIZE) {
+ LOGERR("evdi message len is too large");
+ return false;
+ }
+
+ msg_info _msg;
+ memset(_msg.buf, 0, __MAX_BUF_SIZE);
+ memcpy(_msg.buf, tmp, ID_SIZE);
+ memcpy(_msg.buf + ID_SIZE, data, len);
+
+ _msg.route = route_control_server;
+ _msg.use = len + ID_SIZE;
+ _msg.count = 1;
+ _msg.index = 0;
+ _msg.cclisn = 0;
+
+ LOGDEBUG("ijmsg_send_to_evdi - %s", _msg.buf);
+
+ if (!send_to_evdi(fd, (char*) &_msg, sizeof(_msg)))
+ return false;
+
+ return true;
+}
+
+bool msg_send_to_evdi(evdi_fd fd, const char* data, const int len)
+{
+ _auto_mutex _(&mutex_evdi);
+
+ // TODO: need to make fragmented transmission
+ if (len > __MAX_BUF_SIZE)
+ {
+ LOGERR("evdi message len is too large");
+ return false;
+ }
+
+ msg_info _msg;
+ memset(_msg.buf, 0, __MAX_BUF_SIZE);
+ memcpy(_msg.buf, data, len);
+
+ _msg.route = route_control_server;
+ _msg.use = len;
+ _msg.count = 1;
+ _msg.index = 0;
+ _msg.cclisn = 0;
+
+ LOGDEBUG("msg_send_to_evdi - %s", _msg.buf);
+
+ if (!send_to_evdi(fd, (char*)&_msg, sizeof(_msg)))
+ return false;
+
+ return true;
+}
+