1 /* Medfield sensor plugins
2 * Copyright (C) 2013 Intel Corporation
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.
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.
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
23 #include <linux/input.h>
25 #include "baseprocessor.h"
31 BaseProcessor::BaseProcessor():
38 set_main(started,0,this);
39 memset(&mValues,0,sizeof(mValues));
42 BaseProcessor::~BaseProcessor()
48 bool BaseProcessor::add_input(csensor_module *sensor)
54 bool BaseProcessor::add_input(cfilter_module *filter)
60 cprocessor_module *BaseProcessor::create_new(void)
66 void BaseProcessor::destroy(cprocessor_module *module)
71 const char *BaseProcessor::name(void)
77 int BaseProcessor::version(void)
83 int BaseProcessor::id(void)
89 bool BaseProcessor::update_name(char *name)
96 bool BaseProcessor::update_version(int version)
103 bool BaseProcessor::update_id(int id)
110 long BaseProcessor::value(char *port)
116 long BaseProcessor::value(int id)
123 bool BaseProcessor::add_callback_func(cmd_reg_t *param)
125 DbgPrint("%s",mName.c_str());
127 if (!check_callback_event(param))
130 mCallbacks.push_back(*param);
137 bool operator==(const cmd_reg_t &a, const cmd_reg_t &b)
139 return (a.type == b.type && a.event_type == b.event_type &&
140 a.interval == b.interval);
144 bool BaseProcessor::remove_callback_func(cmd_reg_t *toRemove)
147 std::vector<cmd_reg_t>::iterator iter;
149 iter = std::find(mCallbacks.begin(),mCallbacks.end(),*toRemove);
151 if (iter == mCallbacks.end())
154 mCallbacks.erase(iter,iter);
160 bool BaseProcessor::check_callback_event(cmd_reg_t *param)
166 std::vector<int>::const_iterator iter;
168 for (iter = mSupportedEvents.begin(); iter != mSupportedEvents.end(); iter++) {
169 if ((*iter) == param->event_type)
173 DbgPrint("Requested event was not found");
177 long BaseProcessor::set_cmd(int type , int property , long input_value)
184 int BaseProcessor::get_property(unsigned int property_level , void *property_struct)
186 DbgPrint("Name %s level %d ",mName.c_str(), property_level);
188 if (!property_struct)
191 base_property_struct *result = (base_property_struct*) property_struct;
192 return get_property(property_level, *result );
195 int BaseProcessor::get_property(unsigned int property_level , base_property_struct &result)
200 int BaseProcessor::get_struct_value(unsigned int struct_type , void *struct_values)
207 data_accuracy accuracy = ACCURACY_UNDEFINED;
208 data_unit_idx_t unit = IDX_UNDEFINED_UNIT;
210 if (!fill_values(struct_type, count, unit, accuracy)) {
211 DbgPrint("Fill values returned false");
212 mValueMutex.unlock();
216 base_data_struct *result = (base_data_struct *)struct_values;
218 for (int i = 0; i < count; i++) {
219 result->values[i] = this->mValues[i];
220 DbgPrint("result [%d] is %f",i, result->values[i] );
222 result->values_num = count;
223 result->data_accuracy = accuracy;
226 mValueMutex.unlock();
233 void* BaseProcessor::started(void *ctx)
236 return ((BaseProcessor *) ctx)->started();
239 void* BaseProcessor::started()
242 if (mFd != -1 && mInputEventCount) {
243 input_event *events = new input_event[mInputEventCount];
244 const ssize_t readByteCount = read(mFd, events, mInputEventCount * sizeof(input_event));
245 DbgPrint("%s Readed %d bytes",mName.c_str(),readByteCount);
247 //Ignore partial event reads
248 if (readByteCount > 0 && readByteCount % sizeof(input_event) == 0) {
249 const int readEventCount = readByteCount / sizeof(input_event);
250 std::vector <input_event *> eventVector;
251 DbgPrint("Count of events is %d",readEventCount);
252 for (int i = 0; i < readEventCount; i++) {
253 eventVector.push_back(&(events[i]));
258 process_input_events(eventVector);
259 mValueMutex.unlock();
264 return (void *) cworker::STARTED;
267 void BaseProcessor::process_input_events(const std::vector<input_event*> &events)
269 std::vector<input_event *>::const_iterator iterator;
270 for (iterator = events.begin(); iterator != events.end(); iterator++) {
271 const input_event& event = (**iterator);
275 DbgPrint("%s sensor got input code %d value %d\n",mName.c_str(), event.code, event.value);
276 if (event.code < sizeof(mValues)/sizeof(float))
277 mValues[event.code] = event.value;
281 DbgPrint("Syn event");
285 DbgPrint("Unknown event type %d", event.type);
291 bool BaseProcessor::start(void)
300 return cprocessor_module::start();
303 bool BaseProcessor::stop(void)
312 return cprocessor_module::stop();
315 bool BaseProcessor::enable()
317 writeToSysfs("enable","1",1);
322 bool BaseProcessor::disable()
324 writeToSysfs("enable","0",1);
329 bool BaseProcessor::find_input_device_by_name(const char *inputName)
331 const char *dirname = "/dev/input";
332 char devname[PATH_MAX];
341 dir = opendir(dirname);
345 memset(devname,0,PATH_MAX);
346 strcpy(devname, dirname);
347 filename = devname + strlen(devname);
349 strcpy(devname, dirname);
350 filename = devname + strlen(devname);
353 while ((de = readdir(dir))) {
354 if (de->d_type == DT_DIR)
357 strcpy(filename, de->d_name);
358 filename[strlen(de->d_name)] = 0;
360 mFd = open(devname,O_RDONLY);
362 DbgPrint("Not opened %s",devname);
367 if (ioctl(mFd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
369 DbgPrint("Ioctl error");
371 DbgPrint("Found device %s: %s",devname,name);
373 if (!strcmp(name, inputName)) {
374 DbgPrint("Found input device for sensor %s: %s",devname,name);
387 const char *BaseProcessor::find_device_from_udev(const char *driver)
389 udev_device *dev = NULL;
391 udev_enumerate *enumerator = NULL;
393 struct udev_list_entry *devices = NULL, *dev_list_entry = NULL;
394 const char *devicePath = "";
404 DbgPrint("Failed to initialize udev %d",errno);
408 enumerator = udev_enumerate_new(udev);
411 DbgPrint("Failed to initialize enumerator");
415 //This class is only meant to be used for sensors
416 //which are connected by i2c bus
417 udev_enumerate_add_match_subsystem(enumerator,"i2c");
418 udev_enumerate_add_match_property(enumerator,"DRIVER",driver);
420 if (udev_enumerate_scan_devices(enumerator) != 0) {
421 DbgPrint("Failed to scan devices");
422 goto release_enumerator;
425 devices = udev_enumerate_get_list_entry(enumerator);
427 udev_list_entry_foreach(dev_list_entry, devices) {
429 const char *path = udev_list_entry_get_name(dev_list_entry);
430 DbgPrint("Found device named: %s value",path);
432 dev = udev_device_new_from_syspath(udev,path);
435 devicePath = udev_device_get_devpath(dev);
436 udev_device_unref(dev);
442 mSysfsPath += devicePath;
447 udev_enumerate_unref(enumerator);
455 bool BaseProcessor::writeToSysfs(const char *filename, const char *buffer, int count)
457 DbgPrint("%s",name());
459 if (!mSysfsPath.empty()) {
460 std::string fullpath = mSysfsPath;
461 fullpath.append("/");
462 fullpath.append(filename);
463 const char *cpath = fullpath.c_str();
465 DbgPrint("Trying to open sysfs file entry %s",cpath);
466 int fd = open(cpath,O_WRONLY);
468 DbgPrint("Sysfs file entry opened");
469 int writeCount = write(fd,buffer,count);
470 if (writeCount == count) {
477 DbgPrint("There was error opening sysfs file %s",strerror(errno));