summaryrefslogtreecommitdiff
path: root/src/accel_sim_processor.cpp
diff options
context:
space:
mode:
authorSehong Na <sehong.na@samsung.com>2014-05-31 13:00:18 +0900
committerSehong Na <sehong.na@samsung.com>2014-05-31 13:00:18 +0900
commitdf393f11932e6715f010c1c0822592cec5ae3407 (patch)
treea3379253a6c3441a0afcc412ed99cbb9ea4bd11f /src/accel_sim_processor.cpp
downloademulator-plugin-accel-proc-tizen_2.3.tar.gz
emulator-plugin-accel-proc-tizen_2.3.tar.bz2
emulator-plugin-accel-proc-tizen_2.3.zip
Diffstat (limited to 'src/accel_sim_processor.cpp')
-rw-r--r--src/accel_sim_processor.cpp1058
1 files changed, 1058 insertions, 0 deletions
diff --git a/src/accel_sim_processor.cpp b/src/accel_sim_processor.cpp
new file mode 100644
index 0000000..0329770
--- /dev/null
+++ b/src/accel_sim_processor.cpp
@@ -0,0 +1,1058 @@
+/*
+ * emulator-plugin-accel-proc
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * DongKyun Yun <dk77.yun@samsung.com>
+ *
+ * This library is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <dlfcn.h>
+#include <pthread.h>
+#include <string.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <math.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <linux/input.h>
+
+
+#include <common.h>
+#include <cobject_type.h>
+#include <clist.h>
+#include <cmutex.h>
+#include <cmodule.h>
+#include <csync.h>
+#include <cworker.h>
+#include <cpacket.h>
+#include <csock.h>
+#include <sf_common.h>
+
+#include <csensor_module.h>
+#include <cfilter_module.h>
+#include <cprocessor_module.h>
+#include <accel_sim_processor.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#include <vconf.h>
+
+#define DEBUG
+#define SRV_IP "10.0.2.2"
+#define RADIAN_VALUE 57.29747
+#define GRAVITY_VALUE 0.0098
+#define BINARY_TO_GRAVITY 0.001
+#define LOW_FILTER_ALPHA 0.8
+#define LOW_FILTER_ALPHA_R 0.2
+
+const char *accel_sim_processor::LCD_TYPE_NODE = "/sys/class/graphics/fb0/virtual_size";
+
+const char *accel_sim_processor::LCD_TYPE_KEY = "memory/private/sensor/lcd_type";
+
+accel_sim_processor::accel_sim_processor()
+: m_sensor(NULL)
+, m_filter(NULL)
+, m_x(-1)
+, m_y(-1)
+, m_z(-1)
+, m_gravity_x(-1)
+, m_gravity_y(-1)
+, m_gravity_z(-1)
+, m_event(0)
+, m_new_event(0)
+, m_version(1)
+, m_id(0x04BE)
+, m_acc_theta(-1)
+, m_acc_pitch(-1)
+, m_client(0)
+#ifdef SIMULATOR
+, m_handle_simul_node(-1)
+, m_run_state(-1)
+#endif
+, m_work_err_count(0)
+, m_ms_unit_upscale_value(100000)
+, m_ms_unit_upscale_x(-1)
+, m_ms_unit_upscale_y(-1)
+, m_ms_unit_upscale_z(-1)
+, m_rotation_cb_client(0)
+, m_data_report_cb_client(0)
+, m_orientation_report_cb_client(0)
+, m_linear_acceleration_report_cb_client(0)
+, m_gravity_report_cb_client(0)
+{
+ m_name = strdup("accel_sim_processor");
+ m_rotation_cb_key = strdup("memory/private/sensor/10001");
+ if ((!m_name) ||(!m_rotation_cb_key) ) {
+ free(m_name);
+ free(m_rotation_cb_key);
+ throw ENOMEM;
+ }
+
+ m_lcd_type = check_lcd_type();
+
+#ifdef SIMULATOR
+ FD_ZERO(&m_readfds);
+ FD_ZERO(&m_writefds);
+ FD_ZERO(&m_exceptfds);
+ FD_ZERO(&m_standby_fds);
+#endif
+
+#ifdef HWREV_CHECK
+ if (check_hw_node() != 1 ) {
+ free(m_name);
+ free(m_rotation_cb_key);
+ throw ENXIO;
+ }
+#endif
+
+ cprocessor_module::set_main(working, stopped, this);
+}
+
+
+
+accel_sim_processor::~accel_sim_processor()
+{
+ free(m_name);
+ free(m_rotation_cb_key);
+
+#ifdef SIMULATOR
+ if ( m_run_state != -1 ) {
+ m_run_state = -1;
+ usleep(100000);
+ }
+ if (m_handle_simul_node != -1) {
+ FD_CLR(m_handle_simul_node, &m_readfds);
+ close(m_handle_simul_node);
+ }
+#endif
+}
+
+
+
+bool accel_sim_processor::add_input(csensor_module *sensor)
+{
+ m_sensor = sensor;
+ return true;
+}
+
+
+
+bool accel_sim_processor::add_input(cfilter_module *filter)
+{
+ m_filter = filter;
+ return true;
+}
+
+
+
+const char *accel_sim_processor::name(void)
+{
+ return m_name;
+}
+
+
+
+int accel_sim_processor::id(void)
+{
+ return m_id;
+}
+
+
+
+int accel_sim_processor::version(void)
+{
+ return m_version;
+}
+
+
+
+bool accel_sim_processor::update_name(char *name)
+{
+ char *new_name;
+ new_name = strdup(name);
+ if (!new_name) {
+ DbgPrint("No memory\n");
+ return false;
+ }
+
+ free(m_name);
+ m_name = new_name;
+ return true;
+}
+
+
+
+bool accel_sim_processor::update_id(int id)
+{
+ m_id = id;
+ return true;
+}
+
+
+
+bool accel_sim_processor::update_version(int version)
+{
+ m_version = version;
+ return true;
+}
+
+
+
+cprocessor_module *accel_sim_processor::create_new(void)
+{
+#ifdef USE_ONLY_ONE_MODULE
+ return (cprocessor_module*)this;
+#else
+ accel_sim_processor *inst = NULL;
+ bool bstate = false;
+
+ try {
+ inst = new accel_sim_processor;
+ } catch (...) {
+ ERR("No Memory\n");
+ return NULL;
+ }
+
+ bstate = cmodule::add_to_list((cmodule *)inst);
+ if ( !bstate ) {
+ ERR("Creat and add_to_list fail");
+ return NULL;
+ }
+
+ return (cprocessor_module*)inst;
+#endif
+}
+
+
+
+void accel_sim_processor::destroy(cprocessor_module *module)
+{
+ bool bstate = false;
+
+ bstate = cmodule::del_from_list((cmodule *)module);
+
+ if ( !bstate ) {
+ ERR("Destory and del_from_list fail");
+ delete (accel_sim_processor *)module;
+ return ;
+ }
+}
+
+
+
+void *accel_sim_processor::stopped(void *inst)
+{
+
+ accel_sim_processor *processor = (accel_sim_processor*)inst;
+
+ if (!processor) {
+ ERR("There is no processor module instance at accel_sim_processor (%s)\n", __FUNCTION__ );
+ return (void*)NULL;
+ }
+
+ processor->wakeup_all_client();
+
+ DBG(">>>>>>>>Wait signal %lx , %s\n", pthread_self(), processor->m_name);
+ processor->m_sync.wait();
+
+ DBG(">>>>>>>>>Signal received %lx, %s\n", pthread_self(), processor->m_name);
+ return (void*)NULL;
+}
+
+
+
+void *accel_sim_processor::working(void *inst)
+{
+ unsigned long event;
+ unsigned long status_event;
+ long new_event = -1;
+ int state;
+
+#ifdef SIMULATOR
+ int fd_count = -1;
+ int poll_handle = -1;
+
+ fd_set writefds;
+ fd_set exceptfds;
+ fd_set standby_fds;
+ struct timeval select_time;
+#else
+ cfilter_module *filter;
+ long x;
+ long y;
+ long z;
+ double norm_z;
+ double atan_value;
+ int acc_theta , acc_pitch;
+#endif
+
+ struct sockaddr_in si_other;
+// int s; // for socket, but not be used now
+ int i, slen=sizeof(si_other);
+ char buf[16];
+ FILE* fd;
+ char fbuf[16];
+ int port;
+ fd = fopen("/opt/home/sdb_port.txt", "r");
+ if(!fd)
+ port = 3581;
+ else
+ {
+ fgets(fbuf, 16, fd);
+ fclose(fd);
+ port = atoi(fbuf) + 3;
+ }
+
+ memset(buf, '\0', sizeof(buf));
+
+ /* socket doesn't be used now
+ if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
+ ERR("socket error! (%s)\n", __FUNCTION__ );
+ */
+
+ 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) {
+ fprintf(stderr, "inet_aton() failed\n");
+ }
+
+ accel_sim_processor *processor = (accel_sim_processor*)inst;
+ if (!processor) {
+ ERR("There is no processor module instance at accel (%s)\n", __FUNCTION__ );
+ return (void*)cworker::STOPPED;
+ }
+
+#ifdef TARGET
+ DBG("Gathering data\n");
+ if (!processor->m_sensor && !processor->m_filter) {
+ ERR("Sensor or filter is not added\n");
+ return (void*)cworker::STOPPED;
+ }
+
+ //! Implementation dependent
+ filter = (cfilter_module*)processor->m_filter;
+
+ DBG("Invoke is_data_ready\n");
+
+ if (filter->is_data_ready(true) == false) {
+ ERR("Data ready has failed\n");
+ processor->m_work_err_count++;
+// if ( processor->m_work_err_count > 10 ) {
+// ERR("Too many error counted stop processor");
+// return (void*)cworker::STOPPED;
+// }
+ return (void*)cworker::STARTED;
+ }
+
+ DBG("Data is ready now\n");
+
+ x = filter->value("x") * -1;
+ y = filter->value("y") * -1;
+ z = filter->value("z") * -1;
+
+ DBG("Read %ld %ld %ld\n", x, y, z);
+ DBG("Prev-Read %ld %ld %ld\n", processor->m_x, processor->m_y, processor->m_z);
+
+ atan_value = atan2(x, y);
+ acc_theta = (int)(atan_value * (180.0/M_PI) + 360)%360;
+
+ if ( z > 1024 ) {
+ norm_z = 1.0;
+ }
+ else if ( z < -1024 ) {
+ norm_z = -1.0;
+ }
+ else {
+ norm_z = ((double)z)/1024;
+ }
+ acc_pitch = (int)( acos(norm_z) *(180.0/M_PI));
+#endif
+
+ event = 0;
+ status_event = 0;
+
+#ifdef SIMULATOR
+
+ if (processor->m_run_state != 1) {
+ ERR("Not run_flag setted");
+ return (void*)cworker::STOPPED;
+ }
+
+ poll_handle = processor->m_handle_simul_node;
+
+ if ( poll_handle != -1 ) {
+ writefds = processor->m_writefds;
+ exceptfds = processor->m_exceptfds;
+ standby_fds = processor->m_readfds;
+
+ select_time.tv_sec = 0;
+ select_time.tv_usec = 100000;
+
+ fd_count = select(poll_handle+1, &standby_fds, &writefds, &exceptfds, &select_time);
+
+ } else {
+ ERR("Invaild simul_node handle , handle : %d", poll_handle);
+ return (void*)cworker::STOPPED;
+ }
+
+ if (FD_ISSET(poll_handle, &standby_fds)) {
+ struct input_event simul_event;
+ DbgPrint("Simulator device detection!");
+ if(fd_count>0)
+ {
+ DbgPrint("FD_COUNT = %d",fd_count);
+
+ state = read(poll_handle, &simul_event, sizeof(simul_event));
+ if(state>0)
+ {
+ switch(simul_event.code)
+ {
+ case SIMUL_ROTATE_0:
+ status_event = (PORTRAIT_TOP|HEAD_CENTER);
+// if (!(processor->m_event & (PORTRAIT_TOP|HEAD_CENTER))) {
+ if ((processor->m_new_event) != ROTATION_HEAD_ROTATE_0) {
+ event = (PORTRAIT_TOP|HEAD_CENTER);
+ new_event = ROTATION_HEAD_ROTATE_0;
+ }
+ break;
+ case SIMUL_ROTATE_90:
+ status_event = (LANDSCAPE|HEAD_LEFT);
+// if (!(processor->m_event & (LANDSCAPE|HEAD_LEFT))) {
+ if ((processor->m_new_event) != ROTATION_HEAD_ROTATE_90) {
+ event = (LANDSCAPE|HEAD_LEFT);
+ new_event =ROTATION_HEAD_ROTATE_90;
+ }
+ break;
+ case SIMUL_ROTATE_180:
+ status_event = (PORTRAIT_BTM|HEAD_CENTER);
+// if (!(processor->m_event & (PORTRAIT_BTM|HEAD_CENTER))) {
+ if ((processor->m_new_event) != ROTATION_HEAD_ROTATE_180) {
+ event = (PORTRAIT_BTM|HEAD_CENTER);
+ new_event =ROTATION_HEAD_ROTATE_180;
+ }
+ break;
+ case SIMUL_ROTATE_270:
+ status_event = (LANDSCAPE|HEAD_RIGHT);
+// if (!(processor->m_event & (LANDSCAPE|HEAD_RIGHT))) {
+ if ((processor->m_new_event) != ROTATION_HEAD_ROTATE_270) {
+ event = (LANDSCAPE|HEAD_RIGHT);
+ new_event =ROTATION_HEAD_ROTATE_270;
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ else
+ {
+ DbgPrint("error read fail , state = %d",state);
+ }
+
+// FD_CLR(poll_handle, &standby_fds);
+ }
+
+ }
+#endif
+
+
+#ifdef TARGET
+ if( (acc_pitch>PITCH_MIN) && (acc_pitch<PITCH_MAX) ) {
+ if ((acc_theta >= 330 && acc_theta <=359) || (acc_theta >=0 && acc_theta <=30))
+ {
+ status_event = (PORTRAIT_TOP|HEAD_CENTER);
+ if(processor->m_event != status_event)
+ {
+ event = (PORTRAIT_TOP|HEAD_CENTER);
+ new_event = ROTATION_HEAD_ROTATE_0;
+ }
+// if (!(processor->m_event & (PORTRAIT_TOP|HEAD_CENTER))) {
+ if ((processor->m_new_event) != ROTATION_HEAD_ROTATE_0) {
+ event = (PORTRAIT_TOP|HEAD_CENTER);
+ new_event = ROTATION_HEAD_ROTATE_0;
+
+ // sprintf(buf, "2\n%d\n", 0);
+ // if(sendto(s, buf, sizeof(buf), 0, (struct sockaddr*)&si_other, slen) == -1)
+ // ERR("sendto error!\n");
+ }
+
+ }
+ else if(acc_theta >= 60 && acc_theta <=120)
+ {
+ status_event = (LANDSCAPE|HEAD_LEFT);
+ if(processor->m_event != status_event)
+ {
+ event = (LANDSCAPE|HEAD_LEFT);
+ new_event =ROTATION_HEAD_ROTATE_90;
+ }
+// if (!(processor->m_event & (LANDSCAPE|HEAD_LEFT))) {
+ if ((processor->m_new_event) != ROTATION_HEAD_ROTATE_90) {
+ event = (LANDSCAPE|HEAD_LEFT);
+ new_event =ROTATION_HEAD_ROTATE_90;
+
+ // sprintf(buf, "2\n%d\n", 90);
+ // if(sendto(s, buf, sizeof(buf), 0, (struct sockaddr*)&si_other, slen) == -1)
+ // ERR("sendto error!\n");
+ }
+ }
+ else if(acc_theta >=150 && acc_theta <= 210)
+ {
+ status_event = (PORTRAIT_BTM|HEAD_CENTER);
+ if(processor->m_event != status_event)
+ {
+ event = (PORTRAIT_BTM|HEAD_CENTER);
+ new_event = ROTATION_HEAD_ROTATE_180;
+ }
+// if (!(processor->m_event & (PORTRAIT_BTM|HEAD_CENTER))) {
+ if ((processor->m_new_event) != ROTATION_HEAD_ROTATE_180) {
+ event = (PORTRAIT_BTM|HEAD_CENTER);
+ new_event = ROTATION_HEAD_ROTATE_180;
+
+ // sprintf(buf, "2\n%d\n", 180);
+ // if(sendto(s, buf, sizeof(buf), 0, (struct sockaddr*)&si_other, slen) == -1)
+ // ERR("sendto error!\n");
+ }
+
+ }
+ else if(acc_theta >=240 && acc_theta <= 300)
+ {
+ status_event = (LANDSCAPE|HEAD_RIGHT);
+ if(processor->m_event != status_event)
+ {
+ event = (LANDSCAPE|HEAD_RIGHT);
+ new_event =ROTATION_HEAD_ROTATE_270;
+ }
+// if (!(processor->m_event & (LANDSCAPE|HEAD_RIGHT))) {
+ if ((processor->m_new_event) != ROTATION_HEAD_ROTATE_270) {
+ event = (LANDSCAPE|HEAD_RIGHT);
+ new_event =ROTATION_HEAD_ROTATE_270;
+
+ // sprintf(buf, "2\n%d\n", 270);
+ // if(sendto(s, buf, sizeof(buf), 0, (struct sockaddr*)&si_other, slen) == -1)
+ // ERR("sendto error!\n");
+ }
+
+ }
+
+ }
+// close(s);
+
+ DBG("check event_value :%lu, status_event : %lu , acc_pitch : %d , acc_theta : %d \n",event , status_event ,acc_pitch , acc_theta );
+
+ processor->m_x = x;
+ processor->m_y = y;
+ processor->m_z = z;
+ processor->m_acc_theta = acc_theta ;
+ processor->m_acc_pitch = acc_pitch ;
+
+ processor->m_ms_unit_upscale_x = ((double)x * 0.004 * (9.81))*processor->m_ms_unit_upscale_value;
+ processor->m_ms_unit_upscale_y = ((double)y * 0.004 * (9.81))*processor->m_ms_unit_upscale_value;
+ processor->m_ms_unit_upscale_z = ((double)z * 0.004 * (9.81))*processor->m_ms_unit_upscale_value;
+#endif
+
+ if (event)
+ {
+ processor->m_event = status_event;
+ processor->m_new_event = new_event;
+ if ( processor->m_rotation_cb_client > 0 ) {
+ DBG("event changed , set new event at key : %s , value : %d \n", processor->m_rotation_cb_key , (int)(processor->m_new_event));
+ if ( processor->m_rotation_cb_key ) {
+ state = vconf_set_int(processor->m_rotation_cb_key, (int)(processor->m_new_event));
+ if (state < 0 ) {
+ ERR("Fail vconf_set_int at key : %s , value : %d\n",processor->m_rotation_cb_key, (int)(processor->m_new_event));
+ }
+ }
+ }
+ }
+ else
+ {
+ DBG("No event changed\n");
+ }
+
+ processor->wakeup_all_client();
+
+ //! TODO: How can I get the polling interval?
+ //! TODO: When we get a polling interval, try read data in that interval :D
+ return (void*)cworker::STARTED;
+}
+
+
+
+long accel_sim_processor::value(char *port)
+{
+ if (!strcasecmp(port, "acc_cod_x")) {
+ return m_x;
+ } else if (!strcasecmp(port, "acc_cod_y")) {
+ return m_y;
+ } else if (!strcasecmp(port, "acc_cod_z")) {
+ return m_z;
+ } else if (!strcasecmp(port, "acc_theta")) {
+ return m_acc_theta;
+ } else if (!strcasecmp(port, "acc_pitch")) {
+ return m_acc_pitch;
+ } else if (!strcasecmp(port, "acc_state")) {
+ return m_event;
+ }
+
+ return -1;
+}
+
+
+
+long accel_sim_processor::value(int id)
+{
+ if (id == 0) {
+ return m_x;
+ } else if (id == 1) {
+ return m_y;
+ } else if (id == 2) {
+ return m_z;
+ } else if (id == 10) {
+ return m_acc_theta;
+ } else if (id == 11) {
+ return m_acc_pitch;
+ } else if (id == 12) {
+ return m_new_event;
+ } else if (id == 13) {
+ return m_ms_unit_upscale_value;
+ } else if (id == 14) {
+ return m_ms_unit_upscale_x;
+ } else if (id == 15) {
+ return m_ms_unit_upscale_y;
+ } else if (id == 16) {
+ return m_ms_unit_upscale_z;
+ }
+
+ return -1;
+}
+
+
+
+bool accel_sim_processor::start(void)
+{
+ bool ret;
+
+ m_client ++;
+ if (m_client > 1) {
+ DBG("%s processor fake starting\n",m_name);
+ return true;
+ }
+
+ DBG("%s processor real starting\n",m_name);
+
+#ifdef SIMULATOR
+ m_handle_simul_node = open(SIMUL_NODE,O_RDONLY);
+ if (m_handle_simul_node<0) {
+ DbgPrint("Acceletor handle open fail for SIMULATOR(handle:%d) \n",m_handle_simul_node);
+ return false;
+ }
+
+ FD_SET(m_handle_simul_node, &m_readfds);
+ m_run_state =1;
+#endif
+ //! Before starting the processor module,
+ //! We have to enable sensor
+ ret = m_sensor ? m_sensor->start() : true;
+ if ( ret != true ) {
+ ERR("m_sensor start fail\n");
+ return false;
+ }
+
+ ret = m_filter ? m_filter->start() : true;
+ if ( ret != true ) {
+ ERR("m_filter start fail\n");
+ return false;
+ }
+
+ ret = cprocessor_module::start();
+
+ if (ret == true) {
+ DBG("Signal send %s\n", m_name);
+ m_sync.send_event();
+ DBG("Signal sent\n");
+ }
+
+ return ret;
+}
+
+
+
+bool accel_sim_processor::stop(void)
+{
+ bool ret;
+
+ m_client --;
+ if (m_client > 0) {
+ DBG("%s processor fake Stopping\n",m_name);
+ return true;
+ }
+
+ DBG("%s processor real Stopping\n",m_name);
+
+ m_client = 0;
+
+ ret = cprocessor_module::stop();
+ if ( ret != true ) {
+ ERR("cprocessor_module::stop()\n");
+ return false;
+ }
+
+#ifdef SIMULATOR
+ int state = -1;
+
+ m_run_state = -1;
+
+ usleep(100000);
+
+ FD_CLR(m_handle_simul_node, &m_readfds);
+
+ state = close(m_handle_simul_node);
+ if ( state < 0 ) {
+ DbgPrint("Acceletor handle close fail for SIMULATOR(state:%d) \n",state);
+ return false;
+ }
+
+#endif
+
+ ret = m_filter ? m_filter->stop() : true;
+ if ( ret != true ) {
+ ERR("m_filter stop fail\n");
+ return false;
+ }
+
+ ret = m_sensor ? m_sensor->stop() : true;
+ if ( ret != true ) {
+ ERR("m_sensor stop fail\n");
+ return false;
+ }
+
+
+ return ret;
+}
+
+bool accel_sim_processor::add_callback_func(cmd_reg_t * param)
+{
+ char dummy_key[MAX_KEY_LEN];
+
+ if ( param->type != REG_ADD ) {
+ ERR("invaild cmd type !!");
+ return false;
+ }
+
+ switch ( param->event_type ) {
+ case ACCELEROMETER_EVENT_ROTATION_CHECK:
+ if ( (m_rotation_cb_client < 1) || ( !m_rotation_cb_key ) ) {
+ memset(dummy_key,'\0',MAX_KEY_LEN);
+ snprintf(dummy_key,(MAX_KEY_LEN-1),"%s%x",DEFAULT_SENSOR_KEY_PREFIX,param->event_type);
+ if (m_rotation_cb_key) {
+ free (m_rotation_cb_key);
+ m_rotation_cb_key = NULL;
+ }
+ m_rotation_cb_key = strdup(dummy_key);
+ if ( !m_rotation_cb_key ) {
+ ERR("Err No memory for event key , evt_type : %x",param->event_type);
+ return false;
+ }
+ }
+ m_rotation_cb_client++;
+
+ break;
+ case ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME:
+ m_data_report_cb_client++;
+ break;
+ case ACCELEROMETER_EVENT_ORIENTATION_DATA_REPORT_ON_TIME:
+ m_orientation_report_cb_client++;
+ break;
+ case ACCELEROMETER_EVENT_LINEAR_ACCELERATION_DATA_REPORT_ON_TIME:
+ m_linear_acceleration_report_cb_client++;
+ break;
+ case ACCELEROMETER_EVENT_GRAVITY_DATA_REPORT_ON_TIME:
+ m_gravity_report_cb_client++;
+ break;
+ default:
+ ERR("invaild event type !!");
+ return false;
+ }
+
+ return true;
+}
+
+
+bool accel_sim_processor::remove_callback_func(cmd_reg_t * param)
+{
+ if ( param->type != REG_DEL ) {
+ ERR("invaild cmd type !!");
+ return false;
+ }
+
+ switch ( param->event_type ) {
+ case ACCELEROMETER_EVENT_ROTATION_CHECK:
+ if ( m_rotation_cb_client == 0 ) {
+ ERR("There is no registed client !!");
+ return false;
+ }
+ m_rotation_cb_client--;
+ if ( (m_rotation_cb_client == 0) && (m_rotation_cb_key!=NULL) ) {
+ free (m_rotation_cb_key);
+ m_rotation_cb_key = NULL;
+ }
+
+ break;
+ case ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME:
+ m_data_report_cb_client--;
+ break;
+ case ACCELEROMETER_EVENT_ORIENTATION_DATA_REPORT_ON_TIME:
+ if(m_orientation_report_cb_client > 0)
+ m_orientation_report_cb_client--;
+ break;
+ case ACCELEROMETER_EVENT_LINEAR_ACCELERATION_DATA_REPORT_ON_TIME:
+ if(m_linear_acceleration_report_cb_client > 0)
+ m_linear_acceleration_report_cb_client--;
+ break;
+ case ACCELEROMETER_EVENT_GRAVITY_DATA_REPORT_ON_TIME:
+ if(m_gravity_report_cb_client > 0)
+ m_gravity_report_cb_client--;
+ break;
+ default:
+ ERR("invaild event type !!");
+ return false;
+ }
+
+ return true;
+}
+
+bool accel_sim_processor::check_callback_event(cmd_reg_t *param)
+{
+ if ( param->type != REG_CHK ) {
+ ERR("invaild cmd type !!");
+ return false;
+ }
+
+ switch ( param->event_type ) {
+ case ACCELEROMETER_EVENT_ROTATION_CHECK:
+ case ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME:
+ case ACCELEROMETER_EVENT_ORIENTATION_DATA_REPORT_ON_TIME:
+ case ACCELEROMETER_EVENT_LINEAR_ACCELERATION_DATA_REPORT_ON_TIME:
+ case ACCELEROMETER_EVENT_GRAVITY_DATA_REPORT_ON_TIME:
+ DBG("event check ok\n");
+ break;
+
+ default:
+ ERR("invaild event type !!");
+ return false;
+ }
+
+ return true;
+}
+
+long accel_sim_processor::set_cmd(int type , int property , long input_value)
+{
+ return -1;
+}
+
+int accel_sim_processor::get_property(unsigned int property_level , void *property_data )
+{
+ int result = -1;
+ base_property_struct *return_property;
+ return_property = (base_property_struct *)property_data;
+
+ if (m_sensor) {
+ result = m_sensor->get_property(ACCELEROMETER_BASE_DATA_SET , return_property);
+ if(result == 0)
+ {
+ if(property_level == ACCELEROMETER_BASE_DATA_SET)
+ {
+ return result;
+ }
+ else if(property_level == ACCELEROMETER_ORIENTATION_DATA_SET)
+ {
+ return_property->sensor_unit_idx = IDX_UNIT_DEGREE;
+ return_property->sensor_min_range = -180;
+ return_property->sensor_max_range = 360;
+ return_property->sensor_resolution = 1;
+ }
+ else if(property_level == ACCELEROMETER_LINEAR_ACCELERATION_DATA_SET)
+ {
+ return_property->sensor_unit_idx = IDX_UNIT_METRE_PER_SECOND_SQUARED;
+ return_property->sensor_min_range = -20.0;
+ return_property->sensor_max_range = 20.0;
+ return_property->sensor_resolution = 0.1;
+ }
+ else if(property_level == ACCELEROMETER_GRAVITY_DATA_SET)
+ {
+ return_property->sensor_unit_idx = IDX_UNIT_METRE_PER_SECOND_SQUARED;
+ return_property->sensor_min_range = -2.0;
+ return_property->sensor_max_range = 2.0;
+ return_property->sensor_resolution = 0.01;
+ }
+ else
+ {
+ ERR("cannot get_property from sensor\n");
+ return -1;
+ }
+ return result;
+ }
+ } else {
+ ERR("no m_sensor , cannot get_property from sensor\n");
+ }
+ return -1;
+}
+
+int accel_sim_processor::get_struct_value(unsigned int struct_type , void *struct_values)
+{
+ int state;
+ base_data_struct sensor_struct_data;
+ base_data_struct *return_struct_data = NULL;
+
+ state = m_filter ? m_filter->get_struct_value(ACCELEROMETER_BASE_DATA_SET , &sensor_struct_data) : -1;
+ if (state<0) {
+ ERR("Error , m_filter get struct_data fail\n");
+ return -1;
+ }
+
+ if ( struct_type == ACCELEROMETER_BASE_DATA_SET ) {
+ return_struct_data = (base_data_struct *)struct_values;
+ return_struct_data->data_accuracy = sensor_struct_data.data_accuracy;
+ return_struct_data->data_unit_idx = IDX_UNIT_METRE_PER_SECOND_SQUARED;
+ return_struct_data->values_num = 3;
+ return_struct_data->values[0] = sensor_struct_data.values[0];
+ return_struct_data->values[1] = sensor_struct_data.values[1];
+ return_struct_data->values[2] = sensor_struct_data.values[2];
+ } else if ( struct_type == ACCELEROMETER_ORIENTATION_DATA_SET ) {
+ return_struct_data = (base_data_struct *)struct_values;
+ return_struct_data->data_accuracy = sensor_struct_data.data_accuracy;
+ return_struct_data->data_unit_idx = IDX_UNIT_DEGREE;
+ return_struct_data->values_num = 3;
+ return_struct_data->values[0] = (int)(atan2(sensor_struct_data.values[0], sensor_struct_data.values[1]) * RADIAN_VALUE + 180) % 360;
+ return_struct_data->values[1] = (int)(atan2(sensor_struct_data.values[1], sensor_struct_data.values[2]) * RADIAN_VALUE ) % 180;
+ return_struct_data->values[2] = (int)(atan2(sensor_struct_data.values[0], sensor_struct_data.values[2]) * RADIAN_VALUE ) % 180;
+
+ if(return_struct_data->values[2] > 90)
+ return_struct_data->values[2] = 180 - return_struct_data->values[2];
+ else if (return_struct_data->values[2] < -90)
+ return_struct_data->values[2] = -180 - return_struct_data->values[2];
+ } else if (struct_type == ACCELEROMETER_LINEAR_ACCELERATION_DATA_SET) {
+ return_struct_data = (base_data_struct *)struct_values;
+ return_struct_data->data_accuracy = sensor_struct_data.data_accuracy;
+ return_struct_data->data_unit_idx = IDX_UNIT_METRE_PER_SECOND_SQUARED;
+ return_struct_data->values_num = 3;
+ m_gravity_x = (LOW_FILTER_ALPHA * m_gravity_x) + (LOW_FILTER_ALPHA_R * sensor_struct_data.values[0]);
+ m_gravity_y = (LOW_FILTER_ALPHA * m_gravity_y) + (LOW_FILTER_ALPHA_R * sensor_struct_data.values[1]);
+ m_gravity_z = (LOW_FILTER_ALPHA * m_gravity_z) + (LOW_FILTER_ALPHA_R * sensor_struct_data.values[2]);
+ return_struct_data->values[0] = (sensor_struct_data.values[0] - m_gravity_x);
+ return_struct_data->values[1] = (sensor_struct_data.values[1] - m_gravity_y);
+ return_struct_data->values[2] = (sensor_struct_data.values[2] - m_gravity_z);
+ } else if (struct_type == ACCELEROMETER_GRAVITY_DATA_SET) {
+ return_struct_data = (base_data_struct *)struct_values;
+ return_struct_data->data_accuracy = sensor_struct_data.data_accuracy;
+ return_struct_data->data_unit_idx = IDX_UNIT_METRE_PER_SECOND_SQUARED;
+ return_struct_data->values_num = 3;
+ return_struct_data->values[0] = sensor_struct_data.values[0] / GRAVITY_VALUE * BINARY_TO_GRAVITY;
+ return_struct_data->values[1] = sensor_struct_data.values[1] / GRAVITY_VALUE * BINARY_TO_GRAVITY;
+ return_struct_data->values[2] = sensor_struct_data.values[2] / GRAVITY_VALUE * BINARY_TO_GRAVITY;
+ } else {
+ ERR("does not support stuct_type\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+int accel_sim_processor::check_lcd_type(void)
+{
+ int type=0;
+
+ return type;
+}
+
+int accel_sim_processor::check_hw_node(void)
+{
+ int i;
+ char name_node[256];
+ char hw_name[50];
+ FILE *fp;
+ const char* orig_name = "accel_sim";
+ int find_node;
+
+ for(i=0;i<10;i++) {
+ find_node = 0;
+ snprintf(name_node,sizeof(name_node),"/sys/devices/virtual/sensor/accel/name");
+
+ fp = fopen(name_node, "r");
+ if (!fp) {
+ DBG("Failed to open a sys_node or there is no node : %s\n",name_node);
+ break;
+ }
+
+ if ( fscanf(fp, "%s", hw_name) < 0) {
+ fclose(fp);
+ ERR("Failed to collect data\n");
+ return -1;
+ }
+ fclose(fp);
+
+ if (!strcasecmp(hw_name, orig_name )) {
+ find_node =1;
+ break;
+ }
+
+ }
+
+ return find_node;
+}
+
+
+cmodule *module_init(void *win, void *egl)
+{
+ accel_sim_processor *inst;
+
+ try {
+ inst = new accel_sim_processor();
+ } catch (int ErrNo) {
+ ERR("accel_sim_processor class create fail , errno : %d , errstr : %s\n",ErrNo, strerror(ErrNo));
+ return NULL;
+ }
+
+ return (cmodule*)inst;
+}
+
+
+
+void module_exit(cmodule *inst)
+{
+ accel_sim_processor *sample = (accel_sim_processor*)inst;
+ delete sample;
+}
+
+
+
+//! End of a file