Tizen 2.0 Release
[adaptation/intel_mfld/sensor-plugins-mfld-blackbay.git] / src / proxiprocessor.cpp
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 #include "proxiprocessor.h"
19 #include "sensor.h"
20 #include "log.h"
21 #include "fcntl.h"
22 #include "poll.h"
23 #include <vconf.h>
24 #include <errno.h>
25
26 const char *PROXI_CHANGED_KEY = DEFAULT_SENSOR_KEY_PREFIX"80001";
27
28 ProxiProcessor::ProxiProcessor()
29 {
30      DbgPrint("%p",this);
31      mName = "proxi";
32      mSupportedEvents.push_back(PROXIMITY_EVENT_CHANGE_STATE);
33      mSupportedEvents.push_back(PROXIMITY_EVENT_STATE_REPORT_ON_TIME);
34 }
35
36 bool ProxiProcessor::fill_values(unsigned int type, int &count,
37                                  data_unit_idx_t &unit, data_accuracy &accuracy)
38 {
39      DbgPrint("%u",type);
40
41
42      switch (type) {
43      case PROXIMITY_BASE_DATA_SET:
44           count = 1;
45           accuracy = ACCURACY_GOOD;
46           unit = IDX_UNIT_STATE_ON_OFF;
47           return true;
48      }
49
50      return false;
51 }
52
53 int ProxiProcessor::get_property(unsigned int property_level,
54                                  base_property_struct &result)
55 {
56      strcpy(result.sensor_name,"ltr502");
57      strcpy(result.sensor_vendor, "Lite-On");
58
59      if (property_level == PROXIMITY_BASE_DATA_SET) {
60           result.sensor_unit_idx = IDX_UNIT_STATE_ON_OFF;
61           result.sensor_min_range = 0;
62           result.sensor_max_range = 1;
63           result.sensor_resolution = 6;
64      } else {
65           return -1;
66      }
67
68      return 0;
69 }
70
71 bool ProxiProcessor::enable()
72 {
73      DbgPrint("");
74      if (mFd <=  0) {
75           mFd = open("/dev/ltr502als_psensor", O_RDONLY);
76      }
77
78      if (mFd > 0) {
79           ioctl(mFd,0,1);
80           return true;
81      }
82      DbgPrint("Opening sensor file failed");
83
84      return false;
85 }
86
87 bool ProxiProcessor::disable()
88 {
89      if (mFd > 0) {
90           ioctl(mFd,0,0);
91           return true;
92      }
93
94      return false;
95 }
96
97 void* ProxiProcessor::started()
98 {
99      pollfd pollInfo;
100      pollInfo.fd = mFd;
101      pollInfo.events = POLLIN;
102      if (poll(&pollInfo,1,-1)){
103           DbgPrint("Poll completed");
104           int value;
105           int result = read(mFd,&value,sizeof(int));
106           if (result > 0) {
107             mValueMutex.lock();
108             mValues[0] = value != 0 ? 1 : 0;
109             mValueMutex.unlock();
110             vconf_set_int(PROXI_CHANGED_KEY,value);
111
112             DbgPrint("Readed value %d",value);
113           }
114           else {
115               DbgPrint("File read error %d",errno);
116           }
117      }
118
119      return (void *) cworker::STARTED;
120 }