1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <fcntl.h> 18 #include <errno.h> 19 #include <math.h> 20 #include <poll.h> 21 #include <unistd.h> 22 #include <dirent.h> 23 #include <sys/select.h> 24 25 #include <linux/max9635.h> 26 27 #include <cutils/log.h> 28 29 #include "LightSensor.h" 30 31 /*****************************************************************************/ 32 33 LightSensor::LightSensor() 34 : SensorBase(LIGHTING_DEVICE_NAME, "max9635_als"), 35 mEnabled(0), 36 mInputReader(4), 37 mHasPendingEvent(false) 38 { 39 mPendingEvent.version = sizeof(sensors_event_t); 40 mPendingEvent.sensor = ID_L; 41 mPendingEvent.type = SENSOR_TYPE_LIGHT; 42 memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data)); 43 } 44 45 LightSensor::~LightSensor() { 46 } 47 48 int LightSensor::enable(int32_t, int en) { 49 int err; 50 en = en ? 1 : 0; 51 if(mEnabled != en) { 52 if (en) { 53 open_device(); 54 } 55 err = ioctl(dev_fd, MAX9635_IOCTL_SET_ENABLE,&en); 56 err = err<0 ? -errno : 0; 57 LOGE_IF(err, "MAX9635_IOCTL_SET_ENABLE failed (%s)", strerror(-err)); 58 if (!err) { 59 mEnabled = en; 60 } 61 if (!en) { 62 close_device(); 63 } 64 } 65 return 0; 66 } 67 68 bool LightSensor::hasPendingEvents() const { 69 return mHasPendingEvent; 70 } 71 72 int LightSensor::readEvents(sensors_event_t* data, int count) 73 { 74 if (count < 1) 75 return -EINVAL; 76 77 if (mHasPendingEvent) { 78 mHasPendingEvent = false; 79 mPendingEvent.timestamp = getTimestamp(); 80 *data = mPendingEvent; 81 return mEnabled ? 1 : 0; 82 } 83 84 ssize_t n = mInputReader.fill(data_fd); 85 if (n < 0) 86 return n; 87 88 int numEventReceived = 0; 89 input_event const* event; 90 91 while (count && mInputReader.readEvent(&event)) { 92 int type = event->type; 93 if (type == EV_MSC) { 94 if (event->code == EVENT_TYPE_LIGHT) { 95 mPendingEvent.light = indexToValue(event->value); 96 } 97 } else if (type == EV_SYN) { 98 mPendingEvent.timestamp = timevalToNano(event->time); 99 if (mEnabled) { 100 *data++ = mPendingEvent; 101 count--; 102 numEventReceived++; 103 } 104 } else { 105 if (type == 4 && event->code == 3) { 106 // weird, not sure why we're getting this all the time 107 } else { 108 LOGE("LightSensor: unknown event (type=%d, code=%d)", 109 type, event->code); 110 } 111 } 112 mInputReader.next(); 113 } 114 115 return numEventReceived; 116 } 117 118 float LightSensor::indexToValue(size_t index) const 119 { 120 return float(index); 121 } 122