first step at implementing the native sensor support

in this commit:
- implemented the C stub
- implemented the binder interfaces involved
- implemented most of the C++ client side

missing:
- SensorManager cannot connect to the SensorServer yet
(because there is no SensorServer yet)

Change-Id: I75010cbeef31c98d6fa62fd5d388dcef87c2636b
This commit is contained in:
Mathias Agopian 2010-07-13 22:21:56 -07:00
parent cee6103d08
commit 589ce85ee4
13 changed files with 1045 additions and 0 deletions

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2010 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.
*/
#ifndef ANDROID_GUI_ISENSOR_EVENT_CONNECTION_H
#define ANDROID_GUI_ISENSOR_EVENT_CONNECTION_H
#include <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <binder/IInterface.h>
namespace android {
// ----------------------------------------------------------------------------
class SensorChannel;
class ISensorEventConnection : public IInterface
{
public:
DECLARE_META_INTERFACE(SensorEventConnection);
virtual sp<SensorChannel> getSensorChannel() const = 0;
virtual status_t enableDisable(int handle, bool enabled) = 0;
virtual status_t setEventRate(int handle, nsecs_t ns) = 0;
};
// ----------------------------------------------------------------------------
class BnSensorEventConnection : public BnInterface<ISensorEventConnection>
{
public:
virtual status_t onTransact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0);
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_GUI_ISENSOR_EVENT_CONNECTION_H

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2010 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.
*/
#ifndef ANDROID_GUI_ISENSORSERVER_H
#define ANDROID_GUI_ISENSORSERVER_H
#include <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <binder/IInterface.h>
namespace android {
// ----------------------------------------------------------------------------
class Sensor;
class ISensorEventConnection;
class ISensorServer : public IInterface
{
public:
DECLARE_META_INTERFACE(SensorServer);
virtual Vector<Sensor> getSensorList()= 0;
virtual sp<ISensorEventConnection> createSensorEventConnection() = 0;
};
// ----------------------------------------------------------------------------
class BnSensorServer : public BnInterface<ISensorServer>
{
public:
virtual status_t onTransact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0);
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_GUI_ISENSORSERVER_H

88
include/gui/Sensor.h Normal file
View File

@ -0,0 +1,88 @@
/*
* Copyright (C) 2010 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.
*/
#ifndef ANDROID_GUI_SENSOR_H
#define ANDROID_GUI_SENSOR_H
#include <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/String8.h>
#include <utils/Flattenable.h>
#include <hardware/sensors.h>
#include <android/sensor.h>
// ----------------------------------------------------------------------------
// Concrete types for the NDK
struct ASensor { };
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
class Parcel;
// ----------------------------------------------------------------------------
class Sensor : public ASensor, public Flattenable
{
public:
enum {
TYPE_ACCELEROMETER = ASENSOR_TYPE_ACCELEROMETER,
TYPE_MAGNETIC_FIELD = ASENSOR_TYPE_MAGNETIC_FIELD,
TYPE_GYROSCOPE = ASENSOR_TYPE_GYROSCOPE,
TYPE_LIGHT = ASENSOR_TYPE_LIGHT,
TYPE_PROXIMITY = ASENSOR_TYPE_PROXIMITY
};
Sensor();
virtual ~Sensor();
const String8& getName() const;
const String8& getVendor() const;
int32_t getHandle() const;
int32_t getType() const;
float getMinValue() const;
float getMaxValue() const;
float getResolution() const;
float getPowerUsage() const;
// Flattenable interface
virtual size_t getFlattenedSize() const;
virtual size_t getFdCount() const;
virtual status_t flatten(void* buffer, size_t size,
int fds[], size_t count) const;
virtual status_t unflatten(void const* buffer, size_t size,
int fds[], size_t count);
private:
String8 mName;
String8 mVendor;
int32_t mHandle;
int32_t mType;
float mMinValue;
float mMaxValue;
float mResolution;
float mPower;
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_GUI_SENSOR_H

View File

@ -0,0 +1,53 @@
/*
* Copyright (C) 2010 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.
*/
#ifndef ANDROID_GUI_SENSOR_CHANNEL_H
#define ANDROID_GUI_SENSOR_CHANNEL_H
#include <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
namespace android {
// ----------------------------------------------------------------------------
class Parcel;
class SensorChannel : public RefBase
{
public:
SensorChannel();
SensorChannel(const Parcel& data);
virtual ~SensorChannel();
int getFd() const;
ssize_t write(void const* vaddr, size_t size);
ssize_t read(void* vaddr, size_t size);
status_t writeToParcel(Parcel* reply) const;
private:
int mSendFd;
mutable int mReceiveFd;
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_GUI_SENSOR_CHANNEL_H

View File

@ -0,0 +1,71 @@
/*
* Copyright (C) 2010 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.
*/
#ifndef ANDROID_SENSOR_EVENT_QUEUE_H
#define ANDROID_SENSOR_EVENT_QUEUE_H
#include <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <utils/Timers.h>
#include <gui/SensorChannel.h>
// ----------------------------------------------------------------------------
struct ALooper;
struct ASensorEvent;
// Concrete types for the NDK
struct ASensorEventQueue {
ALooper* looper;
};
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
class ISensorEventConnection;
class Sensor;
// ----------------------------------------------------------------------------
class SensorEventQueue : public ASensorEventQueue, public RefBase
{
public:
SensorEventQueue(const sp<ISensorEventConnection>& connection);
virtual ~SensorEventQueue();
virtual void onFirstRef();
int getFd() const;
ssize_t write(ASensorEvent const* events, size_t numEvents);
ssize_t read(ASensorEvent* events, size_t numEvents);
status_t enableSensor(Sensor const* sensor) const;
status_t disableSensor(Sensor const* sensor) const;
status_t setEventRate(Sensor const* sensor, nsecs_t ns) const;
private:
sp<ISensorEventConnection> mSensorEventConnection;
sp<SensorChannel> mSensorChannel;
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_SENSOR_EVENT_QUEUE_H

View File

@ -0,0 +1,63 @@
/*
* Copyright (C) 2010 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.
*/
#ifndef ANDROID_GUI_SENSOR_MANAGER_H
#define ANDROID_GUI_SENSOR_MANAGER_H
#include <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <utils/Singleton.h>
#include <utils/Vector.h>
#include <gui/SensorEventQueue.h>
// ----------------------------------------------------------------------------
// Concrete types for the NDK
struct ASensorManager { };
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
class ISensorServer;
class Sensor;
class SensorEventQueue;
// ----------------------------------------------------------------------------
class SensorManager : public ASensorManager, public Singleton<SensorManager>
{
public:
SensorManager();
~SensorManager();
ssize_t getSensorList(Sensor**) const;
Sensor* getDefaultSensor(int type);
sp<SensorEventQueue> createEventQueue();
private:
sp<ISensorServer> mSensorServer;
Sensor* mSensorList;
Vector<Sensor> mSensors;
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_GUI_SENSOR_MANAGER_H

25
libs/gui/Android.mk Normal file
View File

@ -0,0 +1,25 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
ISensorEventConnection.cpp \
ISensorServer.cpp \
Sensor.cpp \
SensorChannel.cpp \
SensorEventQueue.cpp \
SensorManager.cpp
LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
libbinder \
libhardware \
libhardware_legacy
LOCAL_MODULE:= libgui
ifeq ($(TARGET_SIMULATOR),true)
LOCAL_LDLIBS += -lpthread
endif
include $(BUILD_SHARED_LIBRARY)

View File

@ -0,0 +1,108 @@
/*
* Copyright (C) 2010 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 <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <utils/Timers.h>
#include <binder/Parcel.h>
#include <binder/IInterface.h>
#include <gui/ISensorEventConnection.h>
#include <gui/SensorChannel.h>
namespace android {
// ----------------------------------------------------------------------------
enum {
GET_SENSOR_CHANNEL = IBinder::FIRST_CALL_TRANSACTION,
ENABLE_DISABLE,
SET_EVENT_RATE
};
class BpSensorEventConnection : public BpInterface<ISensorEventConnection>
{
public:
BpSensorEventConnection(const sp<IBinder>& impl)
: BpInterface<ISensorEventConnection>(impl)
{
}
virtual sp<SensorChannel> getSensorChannel() const
{
Parcel data, reply;
remote()->transact(GET_SENSOR_CHANNEL, data, &reply);
return new SensorChannel(reply);
}
virtual status_t enableDisable(int handle, bool enabled)
{
Parcel data, reply;
data.writeInt32(handle);
data.writeInt32(enabled);
remote()->transact(ENABLE_DISABLE, data, &reply);
return reply.readInt32();
}
virtual status_t setEventRate(int handle, nsecs_t ns)
{
Parcel data, reply;
data.writeInt32(handle);
data.writeInt64(ns);
remote()->transact(SET_EVENT_RATE, data, &reply);
return reply.readInt32();
}
};
IMPLEMENT_META_INTERFACE(SensorEventConnection, "android.gui.SensorEventConnection");
// ----------------------------------------------------------------------------
status_t BnSensorEventConnection::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch(code) {
case GET_SENSOR_CHANNEL: {
CHECK_INTERFACE(ISensorEventConnection, data, reply);
sp<SensorChannel> channel(getSensorChannel());
channel->writeToParcel(reply);
return NO_ERROR;
} break;
case ENABLE_DISABLE: {
CHECK_INTERFACE(ISensorEventConnection, data, reply);
int handle = data.readInt32();
int enabled = data.readInt32();
status_t result = enableDisable(handle, enabled);
reply->writeInt32(result);
return NO_ERROR;
} break;
case SET_EVENT_RATE: {
CHECK_INTERFACE(ISensorEventConnection, data, reply);
int handle = data.readInt32();
int ns = data.readInt64();
status_t result = setEventRate(handle, ns);
reply->writeInt32(result);
return NO_ERROR;
} break;
}
return BBinder::onTransact(code, data, reply, flags);
}
// ----------------------------------------------------------------------------
}; // namespace android

100
libs/gui/ISensorServer.cpp Normal file
View File

@ -0,0 +1,100 @@
/*
* Copyright (C) 2010 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 <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <utils/Vector.h>
#include <utils/Timers.h>
#include <binder/Parcel.h>
#include <binder/IInterface.h>
#include <gui/Sensor.h>
#include <gui/ISensorServer.h>
#include <gui/ISensorEventConnection.h>
namespace android {
// ----------------------------------------------------------------------------
enum {
GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
CREATE_SENSOR_EVENT_CONNECTION,
};
class BpSensorServer : public BpInterface<ISensorServer>
{
public:
BpSensorServer(const sp<IBinder>& impl)
: BpInterface<ISensorServer>(impl)
{
}
virtual Vector<Sensor> getSensorList()
{
Parcel data, reply;
remote()->transact(GET_SENSOR_LIST, data, &reply);
Sensor s;
Vector<Sensor> v;
int32_t n = reply.readInt32();
v.setCapacity(n);
while (n--) {
reply.read(static_cast<Flattenable&>(s));
v.add(s);
}
return v;
}
virtual sp<ISensorEventConnection> createSensorEventConnection()
{
Parcel data, reply;
remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
}
};
IMPLEMENT_META_INTERFACE(SensorServer, "android.gui.SensorServer");
// ----------------------------------------------------------------------
status_t BnSensorServer::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch(code) {
case GET_SENSOR_LIST: {
CHECK_INTERFACE(ISensorServer, data, reply);
Vector<Sensor> v(getSensorList());
size_t n = v.size();
reply->writeInt32(n);
for (size_t i=0 ; i<n ; i++) {
reply->write(static_cast<const Flattenable&>(v[i]));
}
return NO_ERROR;
} break;
case CREATE_SENSOR_EVENT_CONNECTION: {
CHECK_INTERFACE(ISensorServer, data, reply);
sp<ISensorEventConnection> connection(createSensorEventConnection());
reply->writeStrongBinder(connection->asBinder());
return NO_ERROR;
} break;
}
return BBinder::onTransact(code, data, reply, flags);
}
// ----------------------------------------------------------------------------
}; // namespace android

165
libs/gui/Sensor.cpp Normal file
View File

@ -0,0 +1,165 @@
/*
* Copyright (C) 2010 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 <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/String8.h>
#include <utils/Flattenable.h>
#include <hardware/sensors.h>
#include <gui/Sensor.h>
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
Sensor::Sensor()
: mHandle(0), mType(0),
mMinValue(0), mMaxValue(0), mResolution(0),
mPower(0)
{
}
Sensor::~Sensor()
{
}
const String8& Sensor::getName() const {
return mName;
}
const String8& Sensor::getVendor() const {
return mVendor;
}
int32_t Sensor::getHandle() const {
return mHandle;
}
int32_t Sensor::getType() const {
return mType;
}
float Sensor::getMinValue() const {
return mMinValue;
}
float Sensor::getMaxValue() const {
return mMaxValue;
}
float Sensor::getResolution() const {
return mResolution;
}
float Sensor::getPowerUsage() const {
return mPower;
}
size_t Sensor::getFlattenedSize() const
{
return sizeof(int32_t) + ((mName.length() + 3) & ~3) +
sizeof(int32_t) + ((mVendor.length() + 3) & ~3) +
sizeof(int32_t) * 2 +
sizeof(float) * 3;
}
size_t Sensor::getFdCount() const
{
return 0;
}
static inline
size_t write(void* buffer, size_t offset, const String8& value) {
memcpy(static_cast<char*>(buffer) + offset, value.string(), value.length());
return (value.length() + 3) & ~3;
}
static inline
size_t write(void* buffer, size_t offset, float value) {
*reinterpret_cast<float*>(static_cast<char*>(buffer) + offset) = value;
return sizeof(float);
}
static inline
size_t write(void* buffer, size_t offset, int32_t value) {
*reinterpret_cast<int32_t*>(static_cast<char*>(buffer) + offset) = value;
return sizeof(int32_t);
}
status_t Sensor::flatten(void* buffer, size_t size,
int fds[], size_t count) const
{
if (size < Sensor::getFlattenedSize())
return -ENOMEM;
size_t offset = 0;
offset += write(buffer, offset, int32_t(mName.length()));
offset += write(buffer, offset, mName);
offset += write(buffer, offset, int32_t(mVendor.length()));
offset += write(buffer, offset, mVendor);
offset += write(buffer, offset, mHandle);
offset += write(buffer, offset, mType);
offset += write(buffer, offset, mMinValue);
offset += write(buffer, offset, mMaxValue);
offset += write(buffer, offset, mResolution);
offset += write(buffer, offset, mPower);
return NO_ERROR;
}
static inline
size_t read(void const* buffer, size_t offset, String8* value, int32_t len) {
value->setTo(static_cast<char const*>(buffer) + offset, len);
return (len + 3) & ~3;
}
static inline
size_t read(void const* buffer, size_t offset, float* value) {
*value = *reinterpret_cast<float const*>(static_cast<char const*>(buffer) + offset);
return sizeof(float);
}
static inline
size_t read(void const* buffer, size_t offset, int32_t* value) {
*value = *reinterpret_cast<int32_t const*>(static_cast<char const*>(buffer) + offset);
return sizeof(int32_t);
}
status_t Sensor::unflatten(void const* buffer, size_t size,
int fds[], size_t count)
{
int32_t len;
size_t offset = 0;
offset += read(buffer, offset, &len);
offset += read(buffer, offset, &mName, len);
offset += read(buffer, offset, &len);
offset += read(buffer, offset, &mVendor, len);
offset += read(buffer, offset, &mHandle);
offset += read(buffer, offset, &mType);
offset += read(buffer, offset, &mMinValue);
offset += read(buffer, offset, &mMaxValue);
offset += read(buffer, offset, &mResolution);
offset += read(buffer, offset, &mPower);
return NO_ERROR;
}
// ----------------------------------------------------------------------------
}; // namespace android

View File

@ -0,0 +1,93 @@
/*
* Copyright (C) 2010 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 <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <utils/Errors.h>
#include <binder/Parcel.h>
#include <gui/SensorChannel.h>
namespace android {
// ----------------------------------------------------------------------------
SensorChannel::SensorChannel()
: mSendFd(-1), mReceiveFd(-1)
{
int fds[2];
if (pipe(fds) == 0) {
mReceiveFd = fds[0];
mSendFd = fds[1];
fcntl(mReceiveFd, F_SETFL, O_NONBLOCK);
fcntl(mSendFd, F_SETFL, O_NONBLOCK);
}
}
SensorChannel::SensorChannel(const Parcel& data)
: mSendFd(-1), mReceiveFd(-1)
{
mReceiveFd = dup(data.readFileDescriptor());
fcntl(mReceiveFd, F_SETFL, O_NONBLOCK);
}
SensorChannel::~SensorChannel()
{
if (mSendFd >= 0)
close(mSendFd);
if (mReceiveFd >= 0)
close(mReceiveFd);
}
int SensorChannel::getFd() const
{
return mReceiveFd;
}
ssize_t SensorChannel::write(void const* vaddr, size_t size)
{
ssize_t len = ::write(mSendFd, vaddr, size);
if (len < 0)
return -errno;
return len;
}
ssize_t SensorChannel::read(void* vaddr, size_t size)
{
ssize_t len = ::read(mReceiveFd, vaddr, size);
if (len < 0)
return -errno;
return len;
}
status_t SensorChannel::writeToParcel(Parcel* reply) const
{
if (mReceiveFd < 0)
return -EINVAL;
status_t result = reply->writeDupFileDescriptor(mReceiveFd);
close(mReceiveFd);
mReceiveFd = -1;
return result;
}
// ----------------------------------------------------------------------------
}; // namespace android

View File

@ -0,0 +1,97 @@
/*
* Copyright (C) 2010 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 <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <gui/Sensor.h>
#include <gui/SensorChannel.h>
#include <gui/SensorEventQueue.h>
#include <gui/ISensorEventConnection.h>
#include <android/sensor.h>
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
: mSensorEventConnection(connection)
{
}
SensorEventQueue::~SensorEventQueue()
{
}
void SensorEventQueue::onFirstRef()
{
mSensorChannel = mSensorEventConnection->getSensorChannel();
}
int SensorEventQueue::getFd() const
{
return mSensorChannel->getFd();
}
ssize_t SensorEventQueue::write(ASensorEvent const* events, size_t numEvents)
{
ssize_t size = mSensorChannel->write(events, numEvents * sizeof(events[0]));
if (size >= 0) {
if (size % sizeof(events[0])) {
// partial write!!! should never happen.
return -EINVAL;
}
// returns number of events written
size /= sizeof(events[0]);
}
return size;
}
ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents)
{
ssize_t size = mSensorChannel->read(events, numEvents*sizeof(events[0]));
if (size >= 0) {
if (size % sizeof(events[0])) {
// partial write!!! should never happen.
return -EINVAL;
}
// returns number of events read
size /= sizeof(events[0]);
}
return size;
}
status_t SensorEventQueue::enableSensor(Sensor const* sensor) const
{
return mSensorEventConnection->enableDisable(sensor->getHandle(), true);
}
status_t SensorEventQueue::disableSensor(Sensor const* sensor) const
{
return mSensorEventConnection->enableDisable(sensor->getHandle(), false);
}
status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const
{
return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
}
// ----------------------------------------------------------------------------
}; // namespace android

View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 2010 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 <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <utils/Singleton.h>
#include <gui/ISensorServer.h>
#include <gui/ISensorEventConnection.h>
#include <gui/Sensor.h>
#include <gui/SensorManager.h>
#include <gui/SensorEventQueue.h>
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
ANDROID_SINGLETON_STATIC_INSTANCE(SensorManager)
SensorManager::SensorManager()
: mSensorList(0)
{
mSensors = mSensorServer->getSensorList();
// TODO: needs implementation
}
SensorManager::~SensorManager()
{
// TODO: needs implementation
}
ssize_t SensorManager::getSensorList(Sensor** list) const
{
*list = mSensorList;
return mSensors.size();
}
Sensor* SensorManager::getDefaultSensor(int type)
{
// TODO: needs implementation
return mSensorList;
}
sp<SensorEventQueue> SensorManager::createEventQueue()
{
sp<SensorEventQueue> result = new SensorEventQueue(
mSensorServer->createSensorEventConnection());
return result;
}
// ----------------------------------------------------------------------------
}; // namespace android