1 /*
2  * Copyright (C) 2010 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 #define LOG_TAG "Sensors"
18 
19 #include <algorithm>
20 #include <stdint.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <linux/errno.h>
24 
25 #include <utils/Errors.h>
26 #include <utils/RefBase.h>
27 #include <utils/Looper.h>
28 
29 #include <gui/Sensor.h>
30 #include <gui/BitTube.h>
31 #include <gui/SensorEventQueue.h>
32 #include <gui/ISensorEventConnection.h>
33 
34 #include <android/sensor.h>
35 
36 using std::min;
37 
38 // ----------------------------------------------------------------------------
39 namespace android {
40 // ----------------------------------------------------------------------------
41 
SensorEventQueue(const sp<ISensorEventConnection> & connection)42 SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
43     : mSensorEventConnection(connection), mRecBuffer(NULL), mAvailable(0), mConsumed(0),
44       mNumAcksToSend(0) {
45     mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT];
46 }
47 
~SensorEventQueue()48 SensorEventQueue::~SensorEventQueue() {
49     delete [] mRecBuffer;
50 }
51 
onFirstRef()52 void SensorEventQueue::onFirstRef()
53 {
54     mSensorChannel = mSensorEventConnection->getSensorChannel();
55 }
56 
getFd() const57 int SensorEventQueue::getFd() const
58 {
59     return mSensorChannel->getFd();
60 }
61 
62 
write(const sp<BitTube> & tube,ASensorEvent const * events,size_t numEvents)63 ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
64         ASensorEvent const* events, size_t numEvents) {
65     return BitTube::sendObjects(tube, events, numEvents);
66 }
67 
read(ASensorEvent * events,size_t numEvents)68 ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
69     if (mAvailable == 0) {
70         ssize_t err = BitTube::recvObjects(mSensorChannel,
71                 mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT);
72         if (err < 0) {
73             return err;
74         }
75         mAvailable = static_cast<size_t>(err);
76         mConsumed = 0;
77     }
78     size_t count = min(numEvents, mAvailable);
79     memcpy(events, mRecBuffer + mConsumed, count * sizeof(ASensorEvent));
80     mAvailable -= count;
81     mConsumed += count;
82     return static_cast<ssize_t>(count);
83 }
84 
getLooper() const85 sp<Looper> SensorEventQueue::getLooper() const
86 {
87     Mutex::Autolock _l(mLock);
88     if (mLooper == 0) {
89         mLooper = new Looper(true);
90         mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
91     }
92     return mLooper;
93 }
94 
waitForEvent() const95 status_t SensorEventQueue::waitForEvent() const
96 {
97     const int fd = getFd();
98     sp<Looper> looper(getLooper());
99 
100     int events;
101     int32_t result;
102     do {
103         result = looper->pollOnce(-1, NULL, &events, NULL);
104         if (result == ALOOPER_POLL_ERROR) {
105             ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
106             result = -EPIPE; // unknown error, so we make up one
107             break;
108         }
109         if (events & ALOOPER_EVENT_HANGUP) {
110             // the other-side has died
111             ALOGE("SensorEventQueue::waitForEvent error HANGUP");
112             result = -EPIPE; // unknown error, so we make up one
113             break;
114         }
115     } while (result != fd);
116 
117     return  (result == fd) ? status_t(NO_ERROR) : result;
118 }
119 
wake() const120 status_t SensorEventQueue::wake() const
121 {
122     sp<Looper> looper(getLooper());
123     looper->wake();
124     return NO_ERROR;
125 }
126 
enableSensor(Sensor const * sensor) const127 status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
128     return mSensorEventConnection->enableDisable(sensor->getHandle(), true, 0, 0, false);
129 }
130 
disableSensor(Sensor const * sensor) const131 status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
132     return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, false);
133 }
134 
enableSensor(int32_t handle,int32_t samplingPeriodUs,int maxBatchReportLatencyUs,int reservedFlags) const135 status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
136                                         int maxBatchReportLatencyUs, int reservedFlags) const {
137     return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
138                                                  us2ns(maxBatchReportLatencyUs), reservedFlags);
139 }
140 
flush() const141 status_t SensorEventQueue::flush() const {
142     return mSensorEventConnection->flush();
143 }
144 
disableSensor(int32_t handle) const145 status_t SensorEventQueue::disableSensor(int32_t handle) const {
146     return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
147 }
148 
setEventRate(Sensor const * sensor,nsecs_t ns) const149 status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
150     return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
151 }
152 
injectSensorEvent(const ASensorEvent & event)153 status_t SensorEventQueue::injectSensorEvent(const ASensorEvent& event) {
154     do {
155         // Blocking call.
156         ssize_t size = ::send(mSensorChannel->getFd(), &event, sizeof(event), MSG_NOSIGNAL);
157         if (size >= 0) {
158             return NO_ERROR;
159         } else if (size < 0 && errno == EAGAIN) {
160             // If send is returning a "Try again" error, sleep for 100ms and try again. In all
161             // other cases log a failure and exit.
162             usleep(100000);
163         } else {
164             ALOGE("injectSensorEvent failure %s %zd", strerror(errno), size);
165             return INVALID_OPERATION;
166         }
167     } while (true);
168 }
169 
sendAck(const ASensorEvent * events,int count)170 void SensorEventQueue::sendAck(const ASensorEvent* events, int count) {
171     for (int i = 0; i < count; ++i) {
172         if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) {
173             ++mNumAcksToSend;
174         }
175     }
176     // Send mNumAcksToSend to acknowledge for the wake up sensor events received.
177     if (mNumAcksToSend > 0) {
178         ssize_t size = ::send(mSensorChannel->getFd(), &mNumAcksToSend, sizeof(mNumAcksToSend),
179                 MSG_DONTWAIT | MSG_NOSIGNAL);
180         if (size < 0) {
181             ALOGE("sendAck failure %zd %d", size, mNumAcksToSend);
182         } else {
183             mNumAcksToSend = 0;
184         }
185     }
186     return;
187 }
188 
189 // ----------------------------------------------------------------------------
190 }; // namespace android
191 
192