/emulator-plugin-geo-proc/src/geo_sim_processor.cpp
C++ | 691 lines | 504 code | 150 blank | 37 comment | 93 complexity | cc4aeec9d73e76d224715de862c27579 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-3.0, GPL-2.0, MPL-2.0, JSON, WTFPL, CC-BY-SA-4.0, CC-BY-3.0, BSD-3-Clause, LGPL-2.0, MPL-2.0-no-copyleft-exception, AGPL-1.0, 0BSD, Zlib, Unlicense, BSD-2-Clause, Apache-2.0, LGPL-3.0, ISC, MIT, CC-BY-SA-3.0, CC0-1.0, LGPL-2.1
- /*
- * emulator-plugin-geo-proc
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact:
- * 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 <geo_sim_processor.h>
- #include <vconf.h>
- geo_sim_processor::geo_sim_processor()
- : m_sensor(NULL)
- , m_filter(NULL)
- , m_raw_x(-1)
- , m_raw_y(-1)
- , m_raw_z(-1)
- , m_tesla_x(-1)
- , m_tesla_y(-1)
- , m_tesla_z(-1)
- , m_event(0)
- , m_version(1)
- , m_id(1217)
- , m_client(0)
- , m_work_err_count(0)
- , m_cal_cb_client(0)
- , m_raw_report_cb_client(0)
- , m_att_report_cb_client(0)
- {
- m_name = strdup("geo_sim_processor");
- m_cal_cb_key = strdup("memory/private/sensor/20001");
- if ((!m_name) ||(!m_cal_cb_key) ) {
- free(m_name);
- free(m_cal_cb_key);
- throw ENOMEM;
- }
-
- #ifdef HWREV_CHECK
- if (check_hw_node() != 1 ) {
- free(m_name);
- free(m_cal_cb_key);
- throw ENXIO;
- }
- #endif
- cprocessor_module::set_main(working, stopped, this);
- }
- geo_sim_processor::~geo_sim_processor()
- {
- free(m_name);
- free(m_cal_cb_key);
- }
- bool geo_sim_processor::add_input(csensor_module *sensor)
- {
- m_sensor = sensor;
- return true;
- }
- bool geo_sim_processor::add_input(cfilter_module *filter)
- {
- m_filter = filter;
- return true;
- }
- const char *geo_sim_processor::name(void)
- {
- return m_name;
- }
- int geo_sim_processor::id(void)
- {
- return m_id;
- }
- int geo_sim_processor::version(void)
- {
- return m_version;
- }
- bool geo_sim_processor::update_name(char *name)
- {
- char *new_name;
- new_name = strdup(name);
- if (!new_name) {
- ERR("No memory\n");
- return false;
- }
- free(m_name);
- m_name = new_name;
- return true;
- }
- bool geo_sim_processor::update_id(int id)
- {
- m_id = id;
- return true;
- }
- bool geo_sim_processor::update_version(int version)
- {
- m_version = version;
- return true;
- }
- cprocessor_module *geo_sim_processor::create_new(void)
- {
- #ifdef USE_ONLY_ONE_MODULE
- return (cprocessor_module*)this;
- #else
- geo_sim_processor *inst = NULL;
- bool bstate = false;
- try {
- inst = new geo_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 geo_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 (geo_sim_processor *)module;
- return ;
- }
- }
- void *geo_sim_processor::stopped(void *inst)
- {
- geo_sim_processor *processor = (geo_sim_processor*)inst;
- if (!processor) {
- ERR("There is no processor module instance at geo_sim (%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 *geo_sim_processor::working(void *inst)
- {
- geo_sim_processor *processor = (geo_sim_processor*)inst;
- unsigned long event;
- long status_event;
- int state;
-
- if (!processor) {
- ERR("There is no processor module instance at geo_sim (%s)\n", __FUNCTION__ );
- return (void*)cworker::STOPPED;
- }
- event = 0;
- status_event = 0;
-
- cfilter_module *filter;
- int raw_x;
- int raw_y;
- int raw_z;
- int tesla_x;
- int tesla_y;
- int tesla_z;
- int hdst;
- DBG("Gathering data\n");
- if (!processor->m_filter ) {
- ERR("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");
-
- raw_x = (int)filter->value("raw_x");
- raw_y = (int)filter->value("raw_y");
- raw_z = (int)filter->value("raw_z");
- tesla_x = (int)filter->value("tesla_x");
- tesla_y = (int)filter->value("tesla_y");
- tesla_z = (int)filter->value("tesla_z");
- hdst = (int)filter->value("hdst");
-
- processor->m_raw_x = raw_x;
- processor->m_raw_y = raw_y;
- processor->m_raw_z = raw_z;
- processor->m_tesla_x = tesla_x;
- processor->m_tesla_y = tesla_y;
- processor->m_tesla_z = tesla_z;
- if ( hdst != processor->m_hdst) {
- event =1 ;
- processor->m_hdst = hdst;
- status_event = (long)hdst;
- }
- if (event) {
- processor->m_event = status_event;
- if ( processor->m_cal_cb_client > 0 ) {
- DBG("event changed , set new event at key : %s , value : %d \n", processor->m_cal_cb_key , (int)(processor->m_event));
- if (processor->m_cal_cb_key) {
- state = vconf_set_int(processor->m_cal_cb_key, (int)(processor->m_event));
- if (state < 0 ) {
- ERR("Fail vconf_set_int at key : %s , value : %d\n",processor->m_cal_cb_key, (int)(processor->m_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 geo_sim_processor::value(char *port)
- {
- #ifdef TARGET
- if (!strcasecmp(port, "raw_x")) {
- return m_raw_x;
- } else if (!strcasecmp(port, "raw_y")) {
- return m_raw_y;
- } else if (!strcasecmp(port, "raw_z")) {
- return m_raw_z;
- } else if (!strcasecmp(port, "tesla_x")) {
- return m_tesla_x;
- } else if (!strcasecmp(port, "tesla_y")) {
- return m_tesla_y;
- } else if (!strcasecmp(port, "tesla_z")) {
- return m_tesla_z;
- } else if (!strcasecmp(port, "hdst")) {
- return m_hdst;
- }
- #endif
- return -1;
- }
- long geo_sim_processor::value(int id)
- {
- #ifdef TARGET
- if (id == 0) {
- return m_raw_x;
- } else if (id == 1) {
- return m_raw_y;
- } else if (id == 2) {
- return m_raw_z;
- } else if (id == 3) {
- return m_tesla_x;
- } else if (id == 4) {
- return m_tesla_y;
- } else if (id == 5) {
- return m_tesla_z;
- }
- #endif
- return -1;
- }
- bool geo_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);
- 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;
- }
- //! Before starting the processor module,
- //! We have to enable sensor
- 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 geo_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;
- }
- ret = m_filter ? m_filter->stop() : true;
- if ( ret != true ) {
- ERR("m_filter start fail\n");
- return false;
- }
- ret = m_sensor ? m_sensor->stop() : true;
- if ( ret != true ) {
- ERR("m_sensor stop fail\n");
- return false;
- }
- return true;
- }
- bool geo_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 GEOMAGNETIC_EVENT_CALIBRATION_NEEDED:
- if ( (m_cal_cb_client < 1) || ( !m_cal_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_cal_cb_key) {
- free (m_cal_cb_key);
- m_cal_cb_key = NULL;
- }
- m_cal_cb_key = strdup(dummy_key);
- if ( !m_cal_cb_key ) {
- ERR("Err No memory for event key , evt_type : %x",param->event_type);
- return false;
- }
- }
- m_cal_cb_client++;
-
- break;
-
- case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME:
- m_att_report_cb_client++;
- break;
- case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME:
- m_raw_report_cb_client++;
- break;
-
- default:
- ERR("invaild event type !!");
- return false;
- }
- return true;
- }
- bool geo_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 GEOMAGNETIC_EVENT_CALIBRATION_NEEDED:
- if ( m_cal_cb_client == 0 ) {
- ERR("There is no registed client !!");
- return false;
- }
- m_cal_cb_client--;
- if ( (m_cal_cb_client == 0) && (m_cal_cb_key!=NULL) ) {
- free (m_cal_cb_key);
- m_cal_cb_key = NULL;
- }
-
- break;
- case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME:
- m_att_report_cb_client--;
- break;
- case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME:
- m_raw_report_cb_client--;
- break;
-
- default:
- ERR("invaild event type !!");
- return false;
- }
- return true;
- }
- long geo_sim_processor::set_cmd(int type , int property , long input_value)
- {
- return -1;
- }
- int geo_sim_processor::check_hw_node(void)
- {
- int i;
- char name_node[256];
- char hw_name[50];
- FILE *fp;
- const char* orig_name = "geo_sim";
- int find_node;
- for(i=0;i<10;i++) {
- find_node = 0;
- snprintf(name_node,sizeof(name_node),"/opt/sensor/geo/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;
- }
- bool geo_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 GEOMAGNETIC_EVENT_CALIBRATION_NEEDED:
- case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME:
- case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME:
- DBG("event check ok\n");
- break;
-
- default:
- ERR("invaild event type !!");
- return false;
- }
- return true;
- }
- int geo_sim_processor::get_property(unsigned int property_level , void *property_data )
- {
- if(m_filter)
- {
- return m_filter->get_property(property_level, property_data);
- } else if(m_sensor) {
- return m_sensor->get_property(property_level, property_data);
- } else {
- ERR("no m_sensor, cannot get_property from sensor\n");
- return -1;
- }
- return 0;
- }
- int geo_sim_processor::get_struct_value(unsigned int struct_type , void *struct_values)
- {
- struct timeval sv;
- unsigned long long cur_time;
- gettimeofday(&sv, NULL);
- cur_time = MICROSECONDS(sv);
- #ifdef TARGET
- if ( struct_type == GEOMAGNETIC_ATTITUDE_DATA_SET ) {
- base_data_struct *return_values;
- return_values = (base_data_struct *)struct_values;
- return_values->data_accuracy = m_hdst;
- return_values->data_unit_idx = IDX_UNIT_DEGREE;
- return_values->time_stamp = cur_time;
- return_values->values_num = 3;
- return_values->values[0] = m_raw_x;
- return_values->values[1] = m_raw_y;
- return_values->values[2] = m_raw_z;
- DBG("raw: %d, %d, %d\n", m_raw_x, m_raw_y, m_raw_z);
- return 0;
- } else if ( struct_type == GEOMAGNETIC_RAW_DATA_SET ) {
- base_data_struct *return_values;
- return_values = (base_data_struct *)struct_values;
- return_values->data_accuracy = m_hdst;
- return_values->data_unit_idx = IDX_UNIT_MICRO_TESLA;
- return_values->time_stamp = cur_time;
- return_values->values_num = 3;
- return_values->values[0] = m_tesla_x;
- return_values->values[1] = m_tesla_y;
- return_values->values[2] = m_tesla_z;
- DBG("tesla: %d, %d, %d\n", m_tesla_x, m_tesla_y, m_tesla_z);
- return 0;
- } else
- #endif
- {
- ERR("does not support stuct_type\n");
- return -1;
- }
- }
- extern "C" cmodule *module_init(void *win, void *egl)
- {
- geo_sim_processor *inst;
- try {
- inst = new geo_sim_processor();
- } catch (int ErrNo) {
- ERR("geo_sim_processor class create fail , errno : %d , errstr : %s\n",ErrNo, strerror(ErrNo));
- return NULL;
- }
- return (cmodule*)inst;
- }
- extern "C" void module_exit(cmodule *inst)
- {
- geo_sim_processor *sample = (geo_sim_processor*)inst;
- delete sample;
- }
- //! End of a file