PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/emulator-plugin-geo-proc/src/geo_sim_processor.cpp

https://review.tizen.org/git/
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
  1. /*
  2. * emulator-plugin-geo-proc
  3. *
  4. * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
  5. *
  6. * Contact:
  7. * Sungmin Ha <sungmin82.ha@samsung.com>
  8. * DongKyun Yun <dk77.yun@samsung.com>
  9. *
  10. * This library is free software; you can redistribute it and/or modify it under
  11. * the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation; either version 2.1 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful, but WITHOUT ANY
  16. * WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library; if not, write to the Free Software Foundation, Inc., 51
  22. * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Contributors:
  25. * - S-Core Co., Ltd
  26. *
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <errno.h>
  32. #include <dlfcn.h>
  33. #include <pthread.h>
  34. #include <string.h>
  35. #include <sys/un.h>
  36. #include <netinet/in.h>
  37. #include <math.h>
  38. #include <sys/select.h>
  39. #include <sys/time.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <fcntl.h>
  43. #include <linux/input.h>
  44. #include <common.h>
  45. #include <cobject_type.h>
  46. #include <clist.h>
  47. #include <cmutex.h>
  48. #include <cmodule.h>
  49. #include <csync.h>
  50. #include <cworker.h>
  51. #include <cpacket.h>
  52. #include <csock.h>
  53. #include <sf_common.h>
  54. #include <csensor_module.h>
  55. #include <cfilter_module.h>
  56. #include <cprocessor_module.h>
  57. #include <geo_sim_processor.h>
  58. #include <vconf.h>
  59. geo_sim_processor::geo_sim_processor()
  60. : m_sensor(NULL)
  61. , m_filter(NULL)
  62. , m_raw_x(-1)
  63. , m_raw_y(-1)
  64. , m_raw_z(-1)
  65. , m_tesla_x(-1)
  66. , m_tesla_y(-1)
  67. , m_tesla_z(-1)
  68. , m_event(0)
  69. , m_version(1)
  70. , m_id(1217)
  71. , m_client(0)
  72. , m_work_err_count(0)
  73. , m_cal_cb_client(0)
  74. , m_raw_report_cb_client(0)
  75. , m_att_report_cb_client(0)
  76. {
  77. m_name = strdup("geo_sim_processor");
  78. m_cal_cb_key = strdup("memory/private/sensor/20001");
  79. if ((!m_name) ||(!m_cal_cb_key) ) {
  80. free(m_name);
  81. free(m_cal_cb_key);
  82. throw ENOMEM;
  83. }
  84. #ifdef HWREV_CHECK
  85. if (check_hw_node() != 1 ) {
  86. free(m_name);
  87. free(m_cal_cb_key);
  88. throw ENXIO;
  89. }
  90. #endif
  91. cprocessor_module::set_main(working, stopped, this);
  92. }
  93. geo_sim_processor::~geo_sim_processor()
  94. {
  95. free(m_name);
  96. free(m_cal_cb_key);
  97. }
  98. bool geo_sim_processor::add_input(csensor_module *sensor)
  99. {
  100. m_sensor = sensor;
  101. return true;
  102. }
  103. bool geo_sim_processor::add_input(cfilter_module *filter)
  104. {
  105. m_filter = filter;
  106. return true;
  107. }
  108. const char *geo_sim_processor::name(void)
  109. {
  110. return m_name;
  111. }
  112. int geo_sim_processor::id(void)
  113. {
  114. return m_id;
  115. }
  116. int geo_sim_processor::version(void)
  117. {
  118. return m_version;
  119. }
  120. bool geo_sim_processor::update_name(char *name)
  121. {
  122. char *new_name;
  123. new_name = strdup(name);
  124. if (!new_name) {
  125. ERR("No memory\n");
  126. return false;
  127. }
  128. free(m_name);
  129. m_name = new_name;
  130. return true;
  131. }
  132. bool geo_sim_processor::update_id(int id)
  133. {
  134. m_id = id;
  135. return true;
  136. }
  137. bool geo_sim_processor::update_version(int version)
  138. {
  139. m_version = version;
  140. return true;
  141. }
  142. cprocessor_module *geo_sim_processor::create_new(void)
  143. {
  144. #ifdef USE_ONLY_ONE_MODULE
  145. return (cprocessor_module*)this;
  146. #else
  147. geo_sim_processor *inst = NULL;
  148. bool bstate = false;
  149. try {
  150. inst = new geo_sim_processor;
  151. } catch (...) {
  152. ERR("No Memory\n");
  153. return NULL;
  154. }
  155. bstate = cmodule::add_to_list((cmodule *)inst);
  156. if ( !bstate ) {
  157. ERR("Creat and add_to_list fail");
  158. return NULL;
  159. }
  160. return (cprocessor_module*)inst;
  161. #endif
  162. }
  163. void geo_sim_processor::destroy(cprocessor_module *module)
  164. {
  165. bool bstate = false;
  166. bstate = cmodule::del_from_list((cmodule *)module);
  167. if ( !bstate ) {
  168. ERR("Destory and del_from_list fail");
  169. delete (geo_sim_processor *)module;
  170. return ;
  171. }
  172. }
  173. void *geo_sim_processor::stopped(void *inst)
  174. {
  175. geo_sim_processor *processor = (geo_sim_processor*)inst;
  176. if (!processor) {
  177. ERR("There is no processor module instance at geo_sim (%s)\n", __FUNCTION__ );
  178. return (void*)NULL;
  179. }
  180. processor->wakeup_all_client();
  181. DBG(">>>>>>>>Wait signal %lx , %s\n", pthread_self(), processor->m_name);
  182. processor->m_sync.wait();
  183. DBG(">>>>>>>>>Signal received %lx, %s\n", pthread_self(), processor->m_name);
  184. return (void*)NULL;
  185. }
  186. void *geo_sim_processor::working(void *inst)
  187. {
  188. geo_sim_processor *processor = (geo_sim_processor*)inst;
  189. unsigned long event;
  190. long status_event;
  191. int state;
  192. if (!processor) {
  193. ERR("There is no processor module instance at geo_sim (%s)\n", __FUNCTION__ );
  194. return (void*)cworker::STOPPED;
  195. }
  196. event = 0;
  197. status_event = 0;
  198. cfilter_module *filter;
  199. int raw_x;
  200. int raw_y;
  201. int raw_z;
  202. int tesla_x;
  203. int tesla_y;
  204. int tesla_z;
  205. int hdst;
  206. DBG("Gathering data\n");
  207. if (!processor->m_filter ) {
  208. ERR("Filter is not added\n");
  209. return (void*)cworker::STOPPED;
  210. }
  211. //! Implementation dependent
  212. filter = (cfilter_module*)processor->m_filter;
  213. DBG("Invoke is_data_ready\n");
  214. if (filter->is_data_ready(true) == false) {
  215. ERR("Data ready has failed\n");
  216. processor->m_work_err_count++;
  217. // if ( processor->m_work_err_count > 10 ) {
  218. // ERR("Too many error counted stop processor");
  219. // return (void*)cworker::STOPPED;
  220. // }
  221. return (void*)cworker::STARTED;
  222. }
  223. DBG("Data is ready now\n");
  224. raw_x = (int)filter->value("raw_x");
  225. raw_y = (int)filter->value("raw_y");
  226. raw_z = (int)filter->value("raw_z");
  227. tesla_x = (int)filter->value("tesla_x");
  228. tesla_y = (int)filter->value("tesla_y");
  229. tesla_z = (int)filter->value("tesla_z");
  230. hdst = (int)filter->value("hdst");
  231. processor->m_raw_x = raw_x;
  232. processor->m_raw_y = raw_y;
  233. processor->m_raw_z = raw_z;
  234. processor->m_tesla_x = tesla_x;
  235. processor->m_tesla_y = tesla_y;
  236. processor->m_tesla_z = tesla_z;
  237. if ( hdst != processor->m_hdst) {
  238. event =1 ;
  239. processor->m_hdst = hdst;
  240. status_event = (long)hdst;
  241. }
  242. if (event) {
  243. processor->m_event = status_event;
  244. if ( processor->m_cal_cb_client > 0 ) {
  245. DBG("event changed , set new event at key : %s , value : %d \n", processor->m_cal_cb_key , (int)(processor->m_event));
  246. if (processor->m_cal_cb_key) {
  247. state = vconf_set_int(processor->m_cal_cb_key, (int)(processor->m_event));
  248. if (state < 0 ) {
  249. ERR("Fail vconf_set_int at key : %s , value : %d\n",processor->m_cal_cb_key, (int)(processor->m_event));
  250. }
  251. }
  252. }
  253. } else {
  254. DBG("No event changed\n");
  255. }
  256. processor->wakeup_all_client();
  257. //! TODO: How can I get the polling interval?
  258. //! TODO: When we get a polling interval, try read data in that interval :D
  259. return (void*)cworker::STARTED;
  260. }
  261. long geo_sim_processor::value(char *port)
  262. {
  263. #ifdef TARGET
  264. if (!strcasecmp(port, "raw_x")) {
  265. return m_raw_x;
  266. } else if (!strcasecmp(port, "raw_y")) {
  267. return m_raw_y;
  268. } else if (!strcasecmp(port, "raw_z")) {
  269. return m_raw_z;
  270. } else if (!strcasecmp(port, "tesla_x")) {
  271. return m_tesla_x;
  272. } else if (!strcasecmp(port, "tesla_y")) {
  273. return m_tesla_y;
  274. } else if (!strcasecmp(port, "tesla_z")) {
  275. return m_tesla_z;
  276. } else if (!strcasecmp(port, "hdst")) {
  277. return m_hdst;
  278. }
  279. #endif
  280. return -1;
  281. }
  282. long geo_sim_processor::value(int id)
  283. {
  284. #ifdef TARGET
  285. if (id == 0) {
  286. return m_raw_x;
  287. } else if (id == 1) {
  288. return m_raw_y;
  289. } else if (id == 2) {
  290. return m_raw_z;
  291. } else if (id == 3) {
  292. return m_tesla_x;
  293. } else if (id == 4) {
  294. return m_tesla_y;
  295. } else if (id == 5) {
  296. return m_tesla_z;
  297. }
  298. #endif
  299. return -1;
  300. }
  301. bool geo_sim_processor::start(void)
  302. {
  303. bool ret;
  304. m_client ++;
  305. if (m_client > 1) {
  306. DBG("%s processor fake starting\n",m_name);
  307. return true;
  308. }
  309. DBG("%s processor real starting\n",m_name);
  310. ret = m_sensor ? m_sensor->start() : true;
  311. if ( ret != true ) {
  312. ERR("m_sensor start fail\n");
  313. return false;
  314. }
  315. ret = m_filter ? m_filter->start() : true;
  316. if ( ret != true ) {
  317. ERR("m_filter start fail\n");
  318. return false;
  319. }
  320. //! Before starting the processor module,
  321. //! We have to enable sensor
  322. ret = cprocessor_module::start();
  323. if (ret == true) {
  324. DBG("Signal send %s\n", m_name);
  325. m_sync.send_event();
  326. DBG("Signal sent\n");
  327. }
  328. return ret;
  329. }
  330. bool geo_sim_processor::stop(void)
  331. {
  332. bool ret;
  333. m_client --;
  334. if (m_client > 0) {
  335. DBG("%s processor fake Stopping\n",m_name);
  336. return true;
  337. }
  338. DBG("%s processor real Stopping\n",m_name);
  339. m_client = 0;
  340. ret = cprocessor_module::stop();
  341. if ( ret != true ) {
  342. ERR("cprocessor_module::stop()\n");
  343. return false;
  344. }
  345. ret = m_filter ? m_filter->stop() : true;
  346. if ( ret != true ) {
  347. ERR("m_filter start fail\n");
  348. return false;
  349. }
  350. ret = m_sensor ? m_sensor->stop() : true;
  351. if ( ret != true ) {
  352. ERR("m_sensor stop fail\n");
  353. return false;
  354. }
  355. return true;
  356. }
  357. bool geo_sim_processor::add_callback_func(cmd_reg_t * param)
  358. {
  359. char dummy_key[MAX_KEY_LEN];
  360. if ( param->type != REG_ADD ) {
  361. ERR("invaild cmd type !!");
  362. return false;
  363. }
  364. switch ( param->event_type ) {
  365. case GEOMAGNETIC_EVENT_CALIBRATION_NEEDED:
  366. if ( (m_cal_cb_client < 1) || ( !m_cal_cb_key ) ) {
  367. memset(dummy_key,'\0',MAX_KEY_LEN);
  368. snprintf(dummy_key,(MAX_KEY_LEN-1),"%s%x",DEFAULT_SENSOR_KEY_PREFIX,param->event_type);
  369. if (m_cal_cb_key) {
  370. free (m_cal_cb_key);
  371. m_cal_cb_key = NULL;
  372. }
  373. m_cal_cb_key = strdup(dummy_key);
  374. if ( !m_cal_cb_key ) {
  375. ERR("Err No memory for event key , evt_type : %x",param->event_type);
  376. return false;
  377. }
  378. }
  379. m_cal_cb_client++;
  380. break;
  381. case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME:
  382. m_att_report_cb_client++;
  383. break;
  384. case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME:
  385. m_raw_report_cb_client++;
  386. break;
  387. default:
  388. ERR("invaild event type !!");
  389. return false;
  390. }
  391. return true;
  392. }
  393. bool geo_sim_processor::remove_callback_func(cmd_reg_t * param)
  394. {
  395. if ( param->type != REG_DEL ) {
  396. ERR("invaild cmd type !!");
  397. return false;
  398. }
  399. switch ( param->event_type ) {
  400. case GEOMAGNETIC_EVENT_CALIBRATION_NEEDED:
  401. if ( m_cal_cb_client == 0 ) {
  402. ERR("There is no registed client !!");
  403. return false;
  404. }
  405. m_cal_cb_client--;
  406. if ( (m_cal_cb_client == 0) && (m_cal_cb_key!=NULL) ) {
  407. free (m_cal_cb_key);
  408. m_cal_cb_key = NULL;
  409. }
  410. break;
  411. case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME:
  412. m_att_report_cb_client--;
  413. break;
  414. case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME:
  415. m_raw_report_cb_client--;
  416. break;
  417. default:
  418. ERR("invaild event type !!");
  419. return false;
  420. }
  421. return true;
  422. }
  423. long geo_sim_processor::set_cmd(int type , int property , long input_value)
  424. {
  425. return -1;
  426. }
  427. int geo_sim_processor::check_hw_node(void)
  428. {
  429. int i;
  430. char name_node[256];
  431. char hw_name[50];
  432. FILE *fp;
  433. const char* orig_name = "geo_sim";
  434. int find_node;
  435. for(i=0;i<10;i++) {
  436. find_node = 0;
  437. snprintf(name_node,sizeof(name_node),"/opt/sensor/geo/name");
  438. fp = fopen(name_node, "r");
  439. if (!fp) {
  440. DBG("Failed to open a sys_node or there is no node : %s\n",name_node);
  441. break;
  442. }
  443. if ( fscanf(fp, "%s", hw_name) < 0) {
  444. fclose(fp);
  445. ERR("Failed to collect data\n");
  446. return -1;
  447. }
  448. fclose(fp);
  449. if (!strcasecmp(hw_name, orig_name )) {
  450. find_node =1;
  451. break;
  452. }
  453. }
  454. return find_node;
  455. }
  456. bool geo_sim_processor::check_callback_event(cmd_reg_t *param)
  457. {
  458. if ( param->type != REG_CHK ) {
  459. ERR("invaild cmd type !!");
  460. return false;
  461. }
  462. switch ( param->event_type ) {
  463. case GEOMAGNETIC_EVENT_CALIBRATION_NEEDED:
  464. case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME:
  465. case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME:
  466. DBG("event check ok\n");
  467. break;
  468. default:
  469. ERR("invaild event type !!");
  470. return false;
  471. }
  472. return true;
  473. }
  474. int geo_sim_processor::get_property(unsigned int property_level , void *property_data )
  475. {
  476. if(m_filter)
  477. {
  478. return m_filter->get_property(property_level, property_data);
  479. } else if(m_sensor) {
  480. return m_sensor->get_property(property_level, property_data);
  481. } else {
  482. ERR("no m_sensor, cannot get_property from sensor\n");
  483. return -1;
  484. }
  485. return 0;
  486. }
  487. int geo_sim_processor::get_struct_value(unsigned int struct_type , void *struct_values)
  488. {
  489. struct timeval sv;
  490. unsigned long long cur_time;
  491. gettimeofday(&sv, NULL);
  492. cur_time = MICROSECONDS(sv);
  493. #ifdef TARGET
  494. if ( struct_type == GEOMAGNETIC_ATTITUDE_DATA_SET ) {
  495. base_data_struct *return_values;
  496. return_values = (base_data_struct *)struct_values;
  497. return_values->data_accuracy = m_hdst;
  498. return_values->data_unit_idx = IDX_UNIT_DEGREE;
  499. return_values->time_stamp = cur_time;
  500. return_values->values_num = 3;
  501. return_values->values[0] = m_raw_x;
  502. return_values->values[1] = m_raw_y;
  503. return_values->values[2] = m_raw_z;
  504. DBG("raw: %d, %d, %d\n", m_raw_x, m_raw_y, m_raw_z);
  505. return 0;
  506. } else if ( struct_type == GEOMAGNETIC_RAW_DATA_SET ) {
  507. base_data_struct *return_values;
  508. return_values = (base_data_struct *)struct_values;
  509. return_values->data_accuracy = m_hdst;
  510. return_values->data_unit_idx = IDX_UNIT_MICRO_TESLA;
  511. return_values->time_stamp = cur_time;
  512. return_values->values_num = 3;
  513. return_values->values[0] = m_tesla_x;
  514. return_values->values[1] = m_tesla_y;
  515. return_values->values[2] = m_tesla_z;
  516. DBG("tesla: %d, %d, %d\n", m_tesla_x, m_tesla_y, m_tesla_z);
  517. return 0;
  518. } else
  519. #endif
  520. {
  521. ERR("does not support stuct_type\n");
  522. return -1;
  523. }
  524. }
  525. extern "C" cmodule *module_init(void *win, void *egl)
  526. {
  527. geo_sim_processor *inst;
  528. try {
  529. inst = new geo_sim_processor();
  530. } catch (int ErrNo) {
  531. ERR("geo_sim_processor class create fail , errno : %d , errstr : %s\n",ErrNo, strerror(ErrNo));
  532. return NULL;
  533. }
  534. return (cmodule*)inst;
  535. }
  536. extern "C" void module_exit(cmodule *inst)
  537. {
  538. geo_sim_processor *sample = (geo_sim_processor*)inst;
  539. delete sample;
  540. }
  541. //! End of a file