blob: 6928034d6e28de395923182cbb287b94f2df7a03 [file] [log] [blame]
Aaron Whyteab6ec382013-10-22 17:17:17 -07001/*
2 * Copyright (C) 2013 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 <hardware/sensors.h>
18#include <algorithm>
19#include <pthread.h>
Aaron Whyteab6ec382013-10-22 17:17:17 -070020#include <linux/input.h>
Aaron Whyteab6ec382013-10-22 17:17:17 -070021#include <cutils/log.h>
22
23#include "SensorEventQueue.h"
24
25SensorEventQueue::SensorEventQueue(int capacity) {
26 mCapacity = capacity;
Aaron Whyte92863c12013-10-28 17:18:06 -070027
Aaron Whyteab6ec382013-10-22 17:17:17 -070028 mStart = 0;
29 mSize = 0;
30 mData = new sensors_event_t[mCapacity];
Aaron Whyteab6ec382013-10-22 17:17:17 -070031 pthread_cond_init(&mSpaceAvailableCondition, NULL);
Aaron Whyteab6ec382013-10-22 17:17:17 -070032}
33
34SensorEventQueue::~SensorEventQueue() {
35 delete[] mData;
36 mData = NULL;
Aaron Whyteab6ec382013-10-22 17:17:17 -070037 pthread_cond_destroy(&mSpaceAvailableCondition);
Aaron Whyteab6ec382013-10-22 17:17:17 -070038}
39
40int SensorEventQueue::getWritableRegion(int requestedLength, sensors_event_t** out) {
Aaron Whyte92863c12013-10-28 17:18:06 -070041 if (mSize == mCapacity || requestedLength <= 0) {
Aaron Whyteab6ec382013-10-22 17:17:17 -070042 *out = NULL;
43 return 0;
44 }
45 // Start writing after the last readable record.
46 int firstWritable = (mStart + mSize) % mCapacity;
47
48 int lastWritable = firstWritable + requestedLength - 1;
49
50 // Don't go past the end of the data array.
51 if (lastWritable > mCapacity - 1) {
52 lastWritable = mCapacity - 1;
53 }
54 // Don't go into the readable region.
55 if (firstWritable < mStart && lastWritable >= mStart) {
56 lastWritable = mStart - 1;
57 }
58 *out = &mData[firstWritable];
59 return lastWritable - firstWritable + 1;
60}
61
62void SensorEventQueue::markAsWritten(int count) {
63 mSize += count;
Aaron Whyteab6ec382013-10-22 17:17:17 -070064}
65
66int SensorEventQueue::getSize() {
67 return mSize;
68}
69
70sensors_event_t* SensorEventQueue::peek() {
Aaron Whyte92863c12013-10-28 17:18:06 -070071 if (mSize == 0) return NULL;
Aaron Whyteab6ec382013-10-22 17:17:17 -070072 return &mData[mStart];
73}
74
75void SensorEventQueue::dequeue() {
Aaron Whyte92863c12013-10-28 17:18:06 -070076 if (mSize == 0) return;
77 if (mSize == mCapacity) {
78 pthread_cond_broadcast(&mSpaceAvailableCondition);
79 }
Aaron Whyteab6ec382013-10-22 17:17:17 -070080 mSize--;
81 mStart = (mStart + 1) % mCapacity;
Aaron Whyte92863c12013-10-28 17:18:06 -070082}
83
Aaron Whytec69f3a72013-10-29 15:17:01 -070084// returns true if it waited, or false if it was a no-op.
85bool SensorEventQueue::waitForSpace(pthread_mutex_t* mutex) {
86 bool waited = false;
Aaron Whyte92863c12013-10-28 17:18:06 -070087 while (mSize == mCapacity) {
Aaron Whytec69f3a72013-10-29 15:17:01 -070088 waited = true;
Aaron Whyte92863c12013-10-28 17:18:06 -070089 pthread_cond_wait(&mSpaceAvailableCondition, mutex);
90 }
Aaron Whytec69f3a72013-10-29 15:17:01 -070091 return waited;
Aaron Whyteab6ec382013-10-22 17:17:17 -070092}