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