diff options
author | Mathias Agopian <mathias@google.com> | 2010-07-14 16:32:04 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2010-07-14 16:32:04 -0700 |
commit | c9a11088e503b9e3ae52a3f671b2d21f5cd54f06 (patch) | |
tree | d6841ee8f8cf79e7079bfb0f53dc053cc5bd670d /libs | |
parent | 38eea8bf990540360b45b963195a7766c30b55d2 (diff) | |
parent | b957b9d63c88efd3a961759424987b99219adeed (diff) | |
download | frameworks_base-c9a11088e503b9e3ae52a3f671b2d21f5cd54f06.zip frameworks_base-c9a11088e503b9e3ae52a3f671b2d21f5cd54f06.tar.gz frameworks_base-c9a11088e503b9e3ae52a3f671b2d21f5cd54f06.tar.bz2 |
Merge "first step at implementing the native sensor support" into gingerbread
Diffstat (limited to 'libs')
-rw-r--r-- | libs/gui/Android.mk | 25 | ||||
-rw-r--r-- | libs/gui/ISensorEventConnection.cpp | 108 | ||||
-rw-r--r-- | libs/gui/ISensorServer.cpp | 100 | ||||
-rw-r--r-- | libs/gui/Sensor.cpp | 165 | ||||
-rw-r--r-- | libs/gui/SensorChannel.cpp | 93 | ||||
-rw-r--r-- | libs/gui/SensorEventQueue.cpp | 97 | ||||
-rw-r--r-- | libs/gui/SensorManager.cpp | 68 |
7 files changed, 656 insertions, 0 deletions
diff --git a/libs/gui/Android.mk b/libs/gui/Android.mk new file mode 100644 index 0000000..249558a --- /dev/null +++ b/libs/gui/Android.mk @@ -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) diff --git a/libs/gui/ISensorEventConnection.cpp b/libs/gui/ISensorEventConnection.cpp new file mode 100644 index 0000000..3e9d456 --- /dev/null +++ b/libs/gui/ISensorEventConnection.cpp @@ -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 diff --git a/libs/gui/ISensorServer.cpp b/libs/gui/ISensorServer.cpp new file mode 100644 index 0000000..c6177bc --- /dev/null +++ b/libs/gui/ISensorServer.cpp @@ -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 diff --git a/libs/gui/Sensor.cpp b/libs/gui/Sensor.cpp new file mode 100644 index 0000000..1fdd285 --- /dev/null +++ b/libs/gui/Sensor.cpp @@ -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 diff --git a/libs/gui/SensorChannel.cpp b/libs/gui/SensorChannel.cpp new file mode 100644 index 0000000..147e1c2 --- /dev/null +++ b/libs/gui/SensorChannel.cpp @@ -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 diff --git a/libs/gui/SensorEventQueue.cpp b/libs/gui/SensorEventQueue.cpp new file mode 100644 index 0000000..f922ac4 --- /dev/null +++ b/libs/gui/SensorEventQueue.cpp @@ -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 + diff --git a/libs/gui/SensorManager.cpp b/libs/gui/SensorManager.cpp new file mode 100644 index 0000000..cd89285 --- /dev/null +++ b/libs/gui/SensorManager.cpp @@ -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 |