Tizen 2.0 Release
[sdk/emulator-plugin-geo-filter.git] / src / geo_sim_filter.cpp
1 /*
2  * emulator-plugin-geo-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 <geo_sim_filter.h>
57
58
59
60 geo_sim_filter::geo_sim_filter()
61 : m_sensor(NULL)
62 , m_id(1126)
63 , m_version(1)
64 , m_x(-1)
65 , m_y(-1)
66 , m_z(-1)
67 , m_fired_time(0)
68 , m_polling_interval(500000)
69 {
70         m_name = strdup("geo_sim_filter");
71         if (!m_name) {
72                 throw ENOMEM;
73         }
74         
75 #ifdef HWREV_CHECK
76         if (check_hw_node() != 1 ) {
77                 free(m_name);
78                 throw ENXIO;
79         }
80 #endif
81
82 }
83
84
85
86 geo_sim_filter::~geo_sim_filter()
87 {
88         free(m_name);
89 }
90
91
92
93 const char *geo_sim_filter::name(void)
94 {
95         return m_name;
96 }
97
98
99
100 int geo_sim_filter::version(void)
101 {
102         return m_version;
103 }
104
105
106
107 int geo_sim_filter::id(void)
108 {
109         return m_id;
110 }
111
112
113
114 bool geo_sim_filter::is_data_ready(bool wait)
115 {
116         unsigned long long cur_time;
117         unsigned long elapsed_time;
118         struct timeval sv;
119
120
121         gettimeofday(&sv, NULL);
122         cur_time = MICROSECONDS(sv);
123
124         elapsed_time = (unsigned long)(cur_time - m_fired_time);
125         if (elapsed_time < m_polling_interval) {
126                 DBG("Waiting\n");
127                 if (wait) {
128                         usleep(m_polling_interval - elapsed_time);
129                         m_fired_time = cur_time + (m_polling_interval-elapsed_time);
130
131                 }
132         } else {
133                 DBG("Re-firing %llu %llu\n", cur_time, m_fired_time);
134                 DBG("elapsed_time %lu polling_interval %ld\n", elapsed_time, m_polling_interval);
135                 m_fired_time = cur_time;
136         }
137         
138         return m_sensor ? m_sensor->is_data_ready(wait) : false;
139 }
140
141
142
143 long geo_sim_filter::value(const char *port)
144 {
145         return m_sensor ? m_sensor->value(port) : -1;
146 }
147
148
149
150 long geo_sim_filter::value(int id)
151 {
152         return m_sensor ? m_sensor->value(id) : -1;
153 }
154
155
156
157 bool geo_sim_filter::update_name(char *name)
158 {
159         char *new_name;
160
161         new_name = strdup(name);
162         if (!new_name) {
163                 ERR("No memory\n");
164                 return false;
165         }
166
167         free(m_name);
168         m_name = new_name;
169         return true;
170 }
171
172
173
174 bool geo_sim_filter::update_version(int ver)
175 {
176         m_version = ver;
177         return true;
178 }
179
180
181
182 bool geo_sim_filter::update_id(int id)
183 {
184         m_id = id;
185         return true;
186 }
187
188
189
190 void geo_sim_filter::reset(void)
191 {
192 }
193
194
195
196 bool geo_sim_filter::add_input(csensor_module *module)
197 {
198         m_sensor = module;
199         return true;
200 }
201
202
203
204 cfilter_module *geo_sim_filter::create_new(void)
205 {       
206 #ifdef USE_ONLY_ONE_MODULE
207         return (cfilter_module*)this;
208 #else
209         geo_sim_filter *inst;
210         bool bstate = false;
211         
212         try {
213                 inst = new geo_sim_filter;
214         } catch (...) {
215                 ERR("No memory\n");
216                 return NULL;
217         }
218
219         bstate = cmodule::add_to_list((cmodule *)inst);
220         if ( !bstate ) {
221                 ERR("Creat and add_to_list fail");
222                 return NULL;
223         }       
224
225         return (cfilter_module*)inst;
226 #endif
227 }
228
229
230
231 void geo_sim_filter::destroy(cfilter_module *module)
232 {
233         bool bstate = false;
234
235         bstate = cmodule::del_from_list((cmodule *)module);
236
237         if ( !bstate ) {
238                 ERR("Destory and del_from_list fail");
239                 delete (geo_sim_filter *)module;
240                 return ;
241         }       
242
243 }
244
245
246
247 bool geo_sim_filter::start(void)
248 {
249         return m_sensor ? m_sensor->start() : false;
250 }
251
252
253
254 bool geo_sim_filter::stop(void)
255 {
256         return m_sensor ? m_sensor->stop() : false;
257 }
258
259 int geo_sim_filter::get_sensor_type(void)
260 {
261         if (m_sensor) {
262                 return m_sensor->get_sensor_type();
263         }
264         else {
265                 return ID_LUMINANT;
266         }       
267 }
268
269 int geo_sim_filter::check_hw_node(void)
270 {
271         int i;
272         char name_node[256];
273         char hw_name[50];
274         FILE *fp;
275         const char* orig_name = "geo_sim";
276         int find_node;
277
278
279         for(i=0;i<10;i++) {
280                 find_node = 0;
281                 snprintf(name_node,sizeof(name_node),"/opt/sensor/geo/name");
282                 
283                 fp = fopen(name_node, "r");
284                 if (!fp) {
285                         DBG("Failed to open a sys_node or there is no node : %s\n",name_node);                  
286                         break;
287                 }
288                 
289                 if ( fscanf(fp, "%s", hw_name) < 0) {
290                         fclose(fp);
291                         ERR("Failed to collect data\n");
292                         return -1;
293                 }
294                 fclose(fp);
295
296                 if (!strcasecmp(hw_name, orig_name )) {
297                         find_node =1;           
298                         break;
299                 }                                               
300         }
301                 
302         return find_node;       
303 }
304
305 long geo_sim_filter::polling_interval(void)
306 {
307         return (unsigned long long)m_polling_interval /1000llu ;
308 }
309
310 bool geo_sim_filter::update_polling_interval(unsigned long val)
311 {
312         DBG("Update polling interval %lu\n", val);
313         cfilter_module::lock();
314         m_polling_interval = (unsigned long long)val * 1000llu;
315         cfilter_module::unlock();
316         return true;
317 }
318
319 int geo_sim_filter::get_property(unsigned int property_level , void *property_data)
320 {
321         if(m_sensor) {
322                 return m_sensor->get_property(property_level , property_data);
323         }
324         else {
325                 return -1;
326         }
327 }
328
329 int geo_sim_filter::get_struct_value(unsigned int struct_type , void *struct_values)
330 {
331
332         if (m_sensor) {
333                 return m_sensor->get_struct_value(struct_type , struct_values);
334         }
335         else {
336                 ERR("no m_sensor , cannot get_struct_value from sensor\n");
337                 return -1;
338         }       
339
340 }
341
342 extern "C" cmodule *module_init(void *win, void *egl)
343 {
344         geo_sim_filter *sample;
345
346         try {
347                 sample = new geo_sim_filter;
348         } catch (int ErrNo) {
349                 ERR("geo_sim_filter class create fail , errno : %d , errstr : %s\n",ErrNo, strerror(ErrNo));
350                 return NULL;
351         }
352
353         return (cmodule*)sample;
354 }
355
356
357
358 extern "C"  void module_exit(cmodule *inst)
359 {
360         geo_sim_filter *sample = (geo_sim_filter*)inst;
361         delete sample;
362 }
363
364
365
366 //! End of a file