Tizen 2.0 Release
[adaptation/intel_mfld/sensor-plugins-mfld-blackbay.git] / src / lightprocessor.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 "lightprocessor.h"
19 #include "sensor.h"
20 #include "log.h"
21 #include "fcntl.h"
22 #include "poll.h"
23 #include "vconf.h"
24
25 const char *LIGHT_INDEX_KEY = DEFAULT_SENSOR_KEY_PREFIX"40001";
26
27 LightProcessor::LightProcessor()
28 {
29      DbgPrint("%p",this);
30      mName = "light";
31
32      mSupportedEvents.push_back(LIGHT_EVENT_CHANGE_LEVEL);
33      mSupportedEvents.push_back(LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME);
34      mSupportedEvents.push_back(LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME);
35 }
36
37 bool LightProcessor::fill_values(unsigned int type, int &count,
38                                  data_unit_idx_t &unit, data_accuracy &accuracy)
39 {
40      DbgPrint("%u",type);
41
42
43
44      switch (type) {
45      case LIGHT_LUX_DATA_SET:
46           count = 1;
47           mValues[0] = mLuxValue;
48           accuracy = ACCURACY_GOOD;
49           unit = IDX_UNIT_LUX;
50           break;
51      case LIGHT_BASE_DATA_SET:
52           count = 1;
53           mValues[0] = mIndex;
54           accuracy = ACCURACY_NORMAL;
55           unit = IDX_UNIT_LEVEL_1_TO_10;
56           return true;
57      default:
58           return false;
59      }
60
61      return true;
62 }
63
64 int LightProcessor::get_property(unsigned int property_level,
65                                  base_property_struct &result)
66 {
67      strcpy(result.sensor_name,"ltr502");
68      strcpy(result.sensor_vendor,"Lite-On");
69
70      if (property_level == LIGHT_BASE_DATA_SET) {
71           result.sensor_unit_idx = IDX_UNIT_LEVEL_1_TO_10;
72           result.sensor_min_range = 1;
73           result.sensor_max_range = 10;
74           result.sensor_resolution = 0.0002;
75      } else if (property_level == LIGHT_LUX_DATA_SET) {
76           result.sensor_unit_idx = IDX_UNIT_LUX;
77           result.sensor_min_range = 0;
78           result.sensor_max_range = 10000;
79           result.sensor_resolution = 1;
80      } else {
81           return -1;
82      }
83
84      return 0;
85 }
86
87 bool LightProcessor::enable()
88 {
89      DbgPrint("");
90      if (mFd <=  0) {
91           mFd = open("/dev/ltr502als_lsensor", O_RDONLY);
92      }
93
94      if (mFd > 0) {
95           ioctl(mFd,0,1);
96           return true;
97      }
98
99      DbgPrint("Opening sensor file failed");
100
101      return false;
102 }
103
104 bool LightProcessor::disable()
105 {
106      if (mFd > 0) {
107           ioctl(mFd,0,0);
108           return true;
109      }
110
111      return false;
112 }
113
114 static int luxToIndex(int luminance)
115 {
116      static const int luxValues[10] = {
117           10, 50, 100, 150, 250,
118           300, 400, 1000, 2000, 3000
119      };
120
121      for (unsigned int i = 0; i < sizeof(luxValues)/sizeof(int); i++) {
122           if (luxValues[i] >= luminance)
123                return i+1;
124      }
125
126      return 10;
127 }
128
129 void* LightProcessor::started()
130 {
131      DbgPrint();
132      pollfd pollInfo;
133      pollInfo.fd = mFd;
134      pollInfo.events = POLLIN;
135      poll(&pollInfo,1,2000);
136
137      if (pollInfo.revents & POLLIN) {
138           DbgPrint("HAVE POLLIN");
139           int value;
140           int readCount = read(mFd,&value,sizeof(int));
141           if (readCount != sizeof(int)) {
142                return (void *) cworker::STARTED;
143           }
144
145           int oldIndex = mIndex;
146           int currentIndex = luxToIndex(value);
147
148           mValueMutex.lock();
149           mLuxValue = value;
150           mIndex = currentIndex;
151           mValueMutex.unlock();
152
153           if (currentIndex != oldIndex) {
154                vconf_set_int(LIGHT_INDEX_KEY, currentIndex);
155                DbgPrint("Notifying change old %d new %d", oldIndex, currentIndex);
156           }
157
158           DbgPrint("Readed value %d",value);
159      }
160
161      return (void *) cworker::STARTED;
162 }