Tizen 2.0 Release
[adaptation/intel_mfld/sensor-plugins-mfld-blackbay.git] / inc / baseprocessor.h
1 /* Medfield sensor plugins
2  * Copyright (C) 2013 Intel Corporation
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; version 2.1.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301USA
16  */
17
18 #ifndef BASEPROCESSOR_H
19 #define BASEPROCESSOR_H
20
21 #include <sys/un.h>
22 #include <netinet/in.h>
23 #include <pthread.h>
24 #include <cobject_type.h>
25 #include <clist.h>
26 #include <cmutex.h>
27 #include <sf_common.h>
28 #include <cmodule.h>
29 #include <cworker.h>
30 #include <csensor_module.h>
31 #include <cfilter_module.h>
32 #include <csensor_module.h>
33 #include <cprocessor_module.h>
34 #include "sensor.h"
35 #include <vector>
36 #include <linux/input.h>
37 #include <string>
38
39 class BaseProcessor: public cprocessor_module {
40 public:
41      BaseProcessor();
42      virtual ~BaseProcessor();
43
44      virtual bool add_input(csensor_module *sensor);
45      virtual bool add_input(cfilter_module *filter);
46
47      virtual cprocessor_module *create_new(void);
48      virtual void destroy(cprocessor_module *module);
49
50      virtual const char *name(void);
51      virtual int version(void);
52      virtual int id(void);
53
54      virtual bool update_name(char *name);
55      virtual bool update_version(int version);
56      virtual bool update_id(int id);
57
58      virtual long value(char *port);
59      virtual long value(int id);
60
61      virtual bool add_callback_func(cmd_reg_t *param);
62      virtual bool remove_callback_func(cmd_reg_t *param);
63      virtual bool check_callback_event(cmd_reg_t *param);
64
65      virtual long set_cmd(int type , int property , long input_value);
66      virtual int get_property(unsigned int property_level , void *property_struct);
67      virtual int get_property(unsigned int property_level, base_property_struct &result);
68
69      /**
70       * @brief get_struct_value Base implementation will call fill_values with struct_type
71       * and copy values from mValues to client response struct
72       * @param struct_type type which should be returned.
73       * @param struct_values values should be put inside this. Type is really base_data_struct *
74       * @return
75       */
76      virtual int get_struct_value(unsigned int struct_type , void *struct_values);
77
78      /**
79       * @brief fill_values
80       * @param type which values should be returned. These are defined in sensor headers
81       * @param count this should be filled by implementation and set to amount of values
82       * @param unit unit of values. Must be filled by implementation
83       * @param accuracy accuracy of values. Must be filled by implementation
84       * returned by sensor. Values should be put to mValues array. This function will be
85       * called with mValueMutex held, so its safe to change values in array.
86       * @return true if reading ok, false otherwise
87       */
88      virtual bool fill_values(unsigned int type, int &count,
89                               data_unit_idx_t &unit, data_accuracy &accuracy) = 0;
90
91      /**
92       * @brief process_input_event this function will be called with readed input event.
93       * Base implementation handles events of EV_ABS and EV_REL type and puts values
94       * of these events to mValues array by event->code. This function is called with mValueMutex
95       * held.
96       * @param event event which was readed
97       */
98      virtual void process_input_events(const std::vector <input_event *> &events);
99
100      /**
101       * @brief start this function will be called by sensor framework when it wants to start
102       * this processor. Base implementation increases mStartCount and only calls
103       * cprocessor::module::start if this is first start. Before cprocessor::module::start
104       * is called enable() will be called (only in case first start).
105       * @return true if processor is started successfully or was previously started, false
106       * if starting fails
107       */
108      virtual bool start(void);
109
110
111      /**
112       * @brief stop this function will be called by sensor framework when it wants to start
113       * this processor. Base implementation increases mStartCount and only calls
114       * cprocessor::module::start if this is last stop
115       * @return true if processor is stopped successfully or was previously stopped, false
116       * if stopping fails
117       */
118      virtual bool stop(void);
119
120      /**
121       * @brief enable This function will be called when sensor should be started. Base implementation
122       * tries to find enable file from mSysfsPath path. If it finds it, writes 1 to it.
123       * Base implementation always returns true.
124       * @return true if enabling succeeds otherwise false.
125       *
126       */
127      virtual bool enable();
128
129      /**
130       * @brief disable This function will be called when sensor should be disabled. Base implementation
131       * tries to find enable file from mSysfsPath path. If it finds it, writes 0 to it.
132       * Base implementation always returns true.
133       * @return true if disabling succeeds otherwise false.
134       */
135      virtual bool disable();
136
137      /**
138       * @brief started this function will be called by sensor framework thread main loop
139       * this static function will call started() implementation of this class
140       * @return value which has been returned by started() function of this class.
141       */
142      static void *started(void *);
143
144 protected:
145      /**
146       * @brief started this function will be called by started static function. This
147       * function should do sensors data processing. Base implementation reads input events from
148       * mFd file descriptor and feeds them to process_input_event
149       * @return
150       */
151      virtual void *started();
152
153      /**
154       * @brief find_input_device_by_name Search and open input device for sensor.
155       * Goes through devices under /dev/input and opens device which name matches
156       * to given parameter. Doesn't recurse to subdirs.
157       * @param inputName name returned by EVIOCGNAME ioctl for wanted device
158       * @return true if device found false otherwise
159       */
160      bool find_input_device_by_name(const char *inputName);
161
162      /**
163       * @brief find_input_device_by_name Search and open input device for sensor.
164       * This function uses sysfs to find device. This function fills mSysfsPath if
165       * device is found
166       * @param driver name of the driver for wanted input device
167       * @return true if device found false otherwise
168       */
169      const char* find_device_from_udev(const char *driver);
170
171      /**
172       * @brief writeToSysfs Writes information to device specific node in sysfs
173       * @param filename filename to write in sysfs. Not including path whics is taken from mSysfsPath
174       * @see find_device_from_udev
175       * @param buffer buffer to write
176       * @param count count of bytes to write
177       * @return false if mSysfsPath is empty or opening of file fails
178       */
179      bool writeToSysfs(const char *filename, const char *buffer, int count);
180
181      /**Id of sensor */
182      int mId;
183
184      /** Name of sensor*/
185      std::string mName;
186
187      /**Sensor driver version*/
188      int mVersion;
189
190      /**Values read from sensor, fill_values function should use this*/
191      float  mValues[6];
192
193      /** Mutex which protects mValues array*/
194      cmutex mValueMutex;
195
196      /** How many times this sensor has been started*/
197      int mStartCount;
198
199      /** Callback functions registered*/
200      std::vector <cmd_reg_t> mCallbacks;
201
202      /** File descriptor associated with sensor*/
203      int mFd;
204
205      /** How many input events are expected from sensor*/
206      unsigned int mInputEventCount;
207
208      /** Path in sysfs to this sensor*/
209      std::string mSysfsPath;
210
211      /** Supported callback events*/
212      std::vector <int> mSupportedEvents;
213 };
214
215 #endif