Files
android_hardware_libhardware/modules/sensors/dynamic_sensor/RingBuffer.cpp
Peng Xu 57d8af7f5e Dynamic sensor manager module - framework
Library to handle dynamic sensor connection. There are two way to use
this: as hal extension or standalone hal module.

In hal extension mode: add libdynamic_sensor_ext in dependency of hal,
instantiate DynamicSensorManager with appropriate parameters. Then
for all sensor requests, if the handle is owned by dynamic sensor
manager, forward the request.

In standalone mode, add sensor.dynamic_sensor_hal into device make
file. Usually, this also means multihal is necessary. Add
sensor.dynamic_sensor_hal into multihal configuration file.

This CL implements the dynamic sensor manager framework. Sensor daemon
will be added in follow up CL.

Test: test compile (functionality test done in a follow up CL)

Change-Id: I3b96ee135d8dbe3e199af01bed4b61637358803e
2017-03-07 20:50:56 -08:00

105 lines
2.4 KiB
C++

/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "RingBuffer.h"
#include <stdlib.h>
#include <string.h>
namespace android {
RingBuffer::RingBuffer(size_t size)
: mSize(size),
mData((sensors_event_t *)malloc(sizeof(sensors_event_t) * mSize)),
mReadPos(0),
mWritePos(0) {
}
RingBuffer::~RingBuffer() {
free(mData);
mData = NULL;
}
ssize_t RingBuffer::write(const sensors_event_t *ev, size_t size) {
Mutex::Autolock autoLock(mLock);
size_t numAvailableToRead = mWritePos - mReadPos;
size_t numAvailableToWrite = mSize - numAvailableToRead;
if (size > numAvailableToWrite) {
size = numAvailableToWrite;
}
size_t writePos = (mWritePos % mSize);
size_t copy = mSize - writePos;
if (copy > size) {
copy = size;
}
memcpy(&mData[writePos], ev, copy * sizeof(sensors_event_t));
if (size > copy) {
memcpy(mData, &ev[copy], (size - copy) * sizeof(sensors_event_t));
}
mWritePos += size;
if (numAvailableToRead == 0 && size > 0) {
mNotEmptyCondition.broadcast();
}
return size;
}
ssize_t RingBuffer::read(sensors_event_t *ev, size_t size) {
Mutex::Autolock autoLock(mLock);
size_t numAvailableToRead;
for (;;) {
numAvailableToRead = mWritePos - mReadPos;
if (numAvailableToRead > 0) {
break;
}
mNotEmptyCondition.wait(mLock);
}
if (size > numAvailableToRead) {
size = numAvailableToRead;
}
size_t readPos = (mReadPos % mSize);
size_t copy = mSize - readPos;
if (copy > size) {
copy = size;
}
memcpy(ev, &mData[readPos], copy * sizeof(sensors_event_t));
if (size > copy) {
memcpy(&ev[copy], mData, (size - copy) * sizeof(sensors_event_t));
}
mReadPos += size;
return size;
}
} // namespace android