Tizen 2.1 base
[platform/adaptation/emulator/emulator-plugin-accel-filter.git] / src / accel_sim_filter.cpp
1 /*
2  * emulator-plugin-accel-filter
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  * SooYoung Ha <yoosah.ha@samsnung.com>
8  * Sungmin Ha <sungmin82.ha@samsung.com>
9  * DongKyun Yun <dk77.yun@samsung.com>
10  * 
11  * This library is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the
13  * Free Software Foundation; either version 2.1 of the License, or (at your option)
14  * any later version.
15  * 
16  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19  * License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this library; if not, write to the Free Software Foundation, Inc., 51
23  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  *
25  * Contributors:
26  * - S-Core Co., Ltd
27  * 
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <dlfcn.h>
35 #include <pthread.h>
36 #include <string.h>
37 #include <sys/un.h>
38 #include <netinet/in.h>
39 #include <sys/time.h>
40
41
42 #include <common.h>
43 #include <cobject_type.h>
44 #include <clist.h>
45 #include <cmutex.h>
46 #include <cmodule.h>
47
48 #include <cpacket.h>
49 #include <csync.h>
50 #include <cworker.h>
51 #include <csock.h>
52 #include <sf_common.h>
53
54 #include <csensor_module.h>
55 #include <cfilter_module.h>
56 #include <accel_sim_filter.h>
57
58 accel_sim_filter::accel_sim_filter()
59 : m_sensor(NULL)
60 , m_id(11)
61 , m_version(1)
62 , m_x(-1)
63 , m_y(-1)
64 , m_z(-1)
65 , m_fired_time(0)
66 , m_polling_interval(100000)
67 {
68         m_name = strdup("accel_sim_filter");
69         if (!m_name) {
70                 throw ENOMEM;
71         }
72         
73 #ifdef HWREV_CHECK
74         if (check_hw_node() != 1 ) {
75                 free(m_name);
76                 throw ENXIO;
77         }
78 #endif
79
80 }
81
82
83
84 accel_sim_filter::~accel_sim_filter()
85 {
86         free(m_name);
87 }
88
89
90
91 const char *accel_sim_filter::name(void)
92 {
93         return m_name;
94 }
95
96
97
98 int accel_sim_filter::version(void)
99 {
100         return m_version;
101 }
102
103
104
105 int accel_sim_filter::id(void)
106 {
107         return m_id;
108 }
109
110
111
112 bool accel_sim_filter::is_data_ready(bool wait)
113 {
114         unsigned long long cur_time;
115         unsigned long elapsed_time;
116         struct timeval sv;
117
118         DbgPrint("Is data ready: m_sensor %p\n", m_sensor);
119
120         gettimeofday(&sv, NULL);
121         cur_time = MICROSECONDS(sv);
122
123         elapsed_time = (unsigned long)(cur_time - m_fired_time);
124         if (elapsed_time < m_polling_interval) {
125                 DbgPrint("Waiting\n");
126                 if (wait) {
127                         /*
128                         struct timespec ts;
129                         ts.tv_sec = 0;
130                         ts.tv_nsec= (m_polling_interval - elapsed_time) * 1000llu;
131                         nanosleep(&ts, NULL);
132                         */
133                         usleep(m_polling_interval - elapsed_time);
134                         cfilter_module::lock();
135                         m_fired_time = cur_time + (m_polling_interval-elapsed_time);
136                         cfilter_module::unlock();
137
138                 } else {
139                         DbgPrint("Re-firing %llu %llu\n", cur_time, m_fired_time);
140                         DbgPrint("elapsed_time %lu polling_interval %ld\n", elapsed_time, m_polling_interval);
141                         cfilter_module::lock();
142                         m_fired_time = cur_time;
143                         cfilter_module::unlock();                       
144                 }
145         } else {
146                 DbgPrint("Re-firing %llu %llu\n", cur_time, m_fired_time);
147                 DbgPrint("elapsed_time %lu polling_interval %ld\n", elapsed_time, m_polling_interval);
148                 cfilter_module::lock();
149                 m_fired_time = cur_time;
150                 cfilter_module::unlock();
151         }
152         
153         return m_sensor ? m_sensor->is_data_ready(false) : true;
154 }
155
156
157
158 long accel_sim_filter::value(const char *port)
159 {
160         long value = 600;
161         if (m_sensor) {
162                 //! Get the "floor" value. based on unit 100 
163                 value = m_sensor->value(port);          
164         }
165         return (-value);
166 }
167
168
169
170 long accel_sim_filter::value(int id)
171 {
172         long value = 600;
173
174         if (m_sensor) {
175                 value = m_sensor->value(id);
176
177         }
178
179         return (-value);
180 }
181
182
183
184 bool accel_sim_filter::update_name(char *name)
185 {
186         char *new_name;
187
188         new_name = strdup(name);
189         if (!new_name) {
190                 DbgPrint("No memory\n");
191                 return false;
192         }
193
194         free(m_name);
195         m_name = new_name;
196         return true;
197 }
198
199
200
201 bool accel_sim_filter::update_version(int ver)
202 {
203         m_version = ver;
204         return true;
205 }
206
207
208
209 bool accel_sim_filter::update_id(int id)
210 {
211         m_id = id;
212         return true;
213 }
214
215
216
217 void accel_sim_filter::reset(void)
218 {
219 }
220
221
222
223 bool accel_sim_filter::add_input(csensor_module *module)
224 {
225         m_sensor = module;
226         return true;
227 }
228
229
230
231 cfilter_module *accel_sim_filter::create_new(void)
232 {       
233 #ifdef USE_ONLY_ONE_MODULE
234         return (cfilter_module*)this;
235 #else
236         accel_sim_filter *inst;
237         bool bstate = false;
238         
239         try {
240                 inst = new accel_sim_filter;
241         } catch (...) {
242                 DbgPrint("No memory\n");
243                 return NULL;
244         }
245
246         bstate = cmodule::add_to_list((cmodule *)inst);
247         if ( !bstate ) {
248                 DbgPrint("Creat and add_to_list fail");
249                 return NULL;
250         }       
251
252         return (cfilter_module*)inst;
253 #endif
254 }
255
256
257
258 void accel_sim_filter::destroy(cfilter_module *module)
259 {
260         bool bstate = false;
261
262         bstate = cmodule::del_from_list((cmodule *)module);
263
264         if ( !bstate ) {
265                 DbgPrint("Destory and del_from_list fail");
266                 delete (accel_sim_filter *)module;
267                 return ;
268         }       
269
270 }
271
272
273
274 bool accel_sim_filter::start(void)
275 {
276         return m_sensor ? m_sensor->start() : false;
277 }
278
279
280
281 bool accel_sim_filter::stop(void)
282 {
283         return m_sensor ? m_sensor->stop() : false;
284 }
285
286 int accel_sim_filter::get_sensor_type(void)
287 {
288         if (m_sensor) {
289                 return m_sensor->get_sensor_type();
290         }
291         else {
292                 return ID_UNKNOWN;
293         }       
294 }
295
296 long accel_sim_filter::polling_interval(void)
297 {
298         return (unsigned long long)m_polling_interval /1000llu ;
299 }
300
301
302
303 bool accel_sim_filter::update_polling_interval(unsigned long val)
304 {
305         DBG("Update polling interval %lu\n", val);
306         cfilter_module::lock();
307         m_polling_interval = (unsigned long long)val * 1000llu;
308         cfilter_module::unlock();
309         return true;
310 }
311
312 int accel_sim_filter::get_property(unsigned int property_level , void *property_data)
313 {
314         if(m_sensor) {
315                 return m_sensor->get_property(property_level , property_data);
316         }
317         else {
318                 return -1;
319         }
320 }
321
322 int accel_sim_filter::get_struct_value(unsigned int struct_type , void *struct_values)
323 {
324         int state;
325         
326         if (m_sensor) {
327                 if ( (struct_type & 0xFFFF) == 0x0001 ) {
328                         base_data_struct sensor_struct_value;
329                         base_data_struct *return_struct_value = NULL;
330                         state =  m_sensor->get_struct_value(struct_type , (void *)&sensor_struct_value);
331                         if ( state < 0 ) {
332                                 ERR("m_sensor get_struct_value fail\n");
333                                 return -1;
334                         }
335                         return_struct_value = (base_data_struct *)struct_values;
336                         if ( !return_struct_value ) {
337                                 ERR("Error return struct_values point fail(null)\n");
338                                 return -1;
339                         }
340                         return_struct_value->data_accuracy = sensor_struct_value.data_accuracy;
341                         return_struct_value->data_unit_idx = IDX_UNIT_METRE_PER_SECOND_SQUARED;
342                         return_struct_value->time_stamp = sensor_struct_value.time_stamp;
343                         return_struct_value->values_num = 3;
344                         return_struct_value->values[0] = (sensor_struct_value.values[0] / 100000);
345                         return_struct_value->values[1] = (sensor_struct_value.values[1] / 100000);
346                         return_struct_value->values[2] = (sensor_struct_value.values[2] / 100000);
347
348                         DBG("accel_sim_filter , struct_value , x : %f , y : %f , z :%f\n",return_struct_value->values[0] , return_struct_value->values[1] , return_struct_value->values[2]);
349                         
350                 } else {
351                         ERR("does not support struct_type : %d\n",struct_type);
352                         return -1;
353                 }
354         }
355         else {
356                 ERR("no m_sensor , cannot get_struct_value from sensor\n");
357                 return -1;
358         }       
359
360         return 0;
361 }
362
363 int accel_sim_filter::check_hw_node(void)
364 {
365         int i;
366         char name_node[256];
367         char hw_name[50];
368         FILE *fp;
369         const char* orig_name = "accel_sim";
370         int find_node;
371
372         for(i=0;i<10;i++) {
373                 find_node = 0;
374                 snprintf(name_node,sizeof(name_node),"/opt/sensor/accel/name");
375                 
376                 fp = fopen(name_node, "r");
377                 if (!fp) {
378                         DBG("Failed to open a sys_node or there is no node : %s\n",name_node);                  
379                         break;
380                 }
381                 
382                 if ( fscanf(fp, "%s", hw_name) < 0) {
383                         fclose(fp);
384                         ERR("Failed to collect data\n");
385                         return -1;
386                 }
387                 fclose(fp);
388
389                 if (!strcasecmp(hw_name, orig_name )) {
390                         find_node =1;
391                         break;
392                 }                               
393                 
394         }
395
396         return find_node;       
397 }
398
399 cmodule *module_init(void *win, void *egl)
400 {
401         accel_sim_filter *sample;
402
403         try {
404                 sample = new accel_sim_filter;
405         } catch (int ErrNo) {
406                 ERR("accel_sim_filter class create fail , errno : %d , errstr : %s\n",ErrNo, strerror(ErrNo));
407                 return NULL;
408         }
409
410         return (cmodule*)sample;
411 }
412
413
414
415 void module_exit(cmodule *inst)
416 {
417         accel_sim_filter *sample = (accel_sim_filter*)inst;
418         delete sample;
419 }
420
421
422
423 //! End of a file