From 589ce85ee4174829cfedce91b6b2509d2a4002eb Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Tue, 13 Jul 2010 22:21:56 -0700 Subject: [PATCH] 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 --- include/gui/ISensorEventConnection.h | 57 +++++++++ include/gui/ISensorServer.h | 57 +++++++++ include/gui/Sensor.h | 88 ++++++++++++++ include/gui/SensorChannel.h | 53 +++++++++ include/gui/SensorEventQueue.h | 71 ++++++++++++ include/gui/SensorManager.h | 63 ++++++++++ libs/gui/Android.mk | 25 ++++ libs/gui/ISensorEventConnection.cpp | 108 ++++++++++++++++++ libs/gui/ISensorServer.cpp | 100 ++++++++++++++++ libs/gui/Sensor.cpp | 165 +++++++++++++++++++++++++++ libs/gui/SensorChannel.cpp | 93 +++++++++++++++ libs/gui/SensorEventQueue.cpp | 97 ++++++++++++++++ libs/gui/SensorManager.cpp | 68 +++++++++++ 13 files changed, 1045 insertions(+) create mode 100644 include/gui/ISensorEventConnection.h create mode 100644 include/gui/ISensorServer.h create mode 100644 include/gui/Sensor.h create mode 100644 include/gui/SensorChannel.h create mode 100644 include/gui/SensorEventQueue.h create mode 100644 include/gui/SensorManager.h create mode 100644 libs/gui/Android.mk create mode 100644 libs/gui/ISensorEventConnection.cpp create mode 100644 libs/gui/ISensorServer.cpp create mode 100644 libs/gui/Sensor.cpp create mode 100644 libs/gui/SensorChannel.cpp create mode 100644 libs/gui/SensorEventQueue.cpp create mode 100644 libs/gui/SensorManager.cpp diff --git a/include/gui/ISensorEventConnection.h b/include/gui/ISensorEventConnection.h new file mode 100644 index 000000000..ed4e4cc72 --- /dev/null +++ b/include/gui/ISensorEventConnection.h @@ -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 +#include + +#include +#include + +#include + +namespace android { +// ---------------------------------------------------------------------------- + +class SensorChannel; + +class ISensorEventConnection : public IInterface +{ +public: + DECLARE_META_INTERFACE(SensorEventConnection); + + virtual sp 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 +{ +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 diff --git a/include/gui/ISensorServer.h b/include/gui/ISensorServer.h new file mode 100644 index 000000000..3e0507633 --- /dev/null +++ b/include/gui/ISensorServer.h @@ -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 +#include + +#include +#include + +#include + +namespace android { +// ---------------------------------------------------------------------------- + +class Sensor; +class ISensorEventConnection; + +class ISensorServer : public IInterface +{ +public: + DECLARE_META_INTERFACE(SensorServer); + + virtual Vector getSensorList()= 0; + virtual sp createSensorEventConnection() = 0; +}; + +// ---------------------------------------------------------------------------- + +class BnSensorServer : public BnInterface +{ +public: + virtual status_t onTransact( uint32_t code, + const Parcel& data, + Parcel* reply, + uint32_t flags = 0); +}; + +// ---------------------------------------------------------------------------- +}; // namespace android + +#endif // ANDROID_GUI_ISENSORSERVER_H diff --git a/include/gui/Sensor.h b/include/gui/Sensor.h new file mode 100644 index 000000000..86a16f14f --- /dev/null +++ b/include/gui/Sensor.h @@ -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 +#include + +#include +#include +#include + +#include + +#include + +// ---------------------------------------------------------------------------- +// 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 diff --git a/include/gui/SensorChannel.h b/include/gui/SensorChannel.h new file mode 100644 index 000000000..bb546186a --- /dev/null +++ b/include/gui/SensorChannel.h @@ -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 +#include + +#include +#include + + +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 diff --git a/include/gui/SensorEventQueue.h b/include/gui/SensorEventQueue.h new file mode 100644 index 000000000..d8d8128e7 --- /dev/null +++ b/include/gui/SensorEventQueue.h @@ -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 +#include + +#include +#include +#include + +#include + +// ---------------------------------------------------------------------------- + +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& 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 mSensorEventConnection; + sp mSensorChannel; +}; + +// ---------------------------------------------------------------------------- +}; // namespace android + +#endif // ANDROID_SENSOR_EVENT_QUEUE_H diff --git a/include/gui/SensorManager.h b/include/gui/SensorManager.h new file mode 100644 index 000000000..0d65334ce --- /dev/null +++ b/include/gui/SensorManager.h @@ -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 +#include + +#include +#include +#include +#include + +#include + +// ---------------------------------------------------------------------------- +// Concrete types for the NDK +struct ASensorManager { }; + +// ---------------------------------------------------------------------------- +namespace android { +// ---------------------------------------------------------------------------- + +class ISensorServer; +class Sensor; +class SensorEventQueue; + +// ---------------------------------------------------------------------------- + +class SensorManager : public ASensorManager, public Singleton +{ +public: + SensorManager(); + ~SensorManager(); + + ssize_t getSensorList(Sensor**) const; + Sensor* getDefaultSensor(int type); + sp createEventQueue(); + +private: + sp mSensorServer; + Sensor* mSensorList; + Vector mSensors; +}; + +// ---------------------------------------------------------------------------- +}; // namespace android + +#endif // ANDROID_GUI_SENSOR_MANAGER_H diff --git a/libs/gui/Android.mk b/libs/gui/Android.mk new file mode 100644 index 000000000..249558ac8 --- /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 000000000..3e9d456fd --- /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 +#include + +#include +#include +#include + +#include +#include + +#include +#include + +namespace android { +// ---------------------------------------------------------------------------- + +enum { + GET_SENSOR_CHANNEL = IBinder::FIRST_CALL_TRANSACTION, + ENABLE_DISABLE, + SET_EVENT_RATE +}; + +class BpSensorEventConnection : public BpInterface +{ +public: + BpSensorEventConnection(const sp& impl) + : BpInterface(impl) + { + } + + virtual sp 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 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 000000000..c6177bc71 --- /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 +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +namespace android { +// ---------------------------------------------------------------------------- + +enum { + GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION, + CREATE_SENSOR_EVENT_CONNECTION, +}; + +class BpSensorServer : public BpInterface +{ +public: + BpSensorServer(const sp& impl) + : BpInterface(impl) + { + } + + virtual Vector getSensorList() + { + Parcel data, reply; + remote()->transact(GET_SENSOR_LIST, data, &reply); + Sensor s; + Vector v; + int32_t n = reply.readInt32(); + v.setCapacity(n); + while (n--) { + reply.read(static_cast(s)); + v.add(s); + } + return v; + } + + virtual sp createSensorEventConnection() + { + Parcel data, reply; + remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply); + return interface_cast(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 v(getSensorList()); + size_t n = v.size(); + reply->writeInt32(n); + for (size_t i=0 ; iwrite(static_cast(v[i])); + } + return NO_ERROR; + } break; + case CREATE_SENSOR_EVENT_CONNECTION: { + CHECK_INTERFACE(ISensorServer, data, reply); + sp 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 000000000..1fdd28582 --- /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 +#include + +#include +#include +#include + +#include + +#include + +// ---------------------------------------------------------------------------- +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(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(static_cast(buffer) + offset) = value; + return sizeof(float); +} + +static inline +size_t write(void* buffer, size_t offset, int32_t value) { + *reinterpret_cast(static_cast(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(buffer) + offset, len); + return (len + 3) & ~3; +} + +static inline +size_t read(void const* buffer, size_t offset, float* value) { + *value = *reinterpret_cast(static_cast(buffer) + offset); + return sizeof(float); +} + +static inline +size_t read(void const* buffer, size_t offset, int32_t* value) { + *value = *reinterpret_cast(static_cast(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 000000000..147e1c2bb --- /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 +#include + +#include +#include + +#include + +#include + +#include + +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 000000000..f922ac4b0 --- /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 +#include + +#include +#include + +#include +#include +#include +#include + +#include + +// ---------------------------------------------------------------------------- +namespace android { +// ---------------------------------------------------------------------------- + +SensorEventQueue::SensorEventQueue(const sp& 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 000000000..cd892851f --- /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 +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +// ---------------------------------------------------------------------------- +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 SensorManager::createEventQueue() +{ + sp result = new SensorEventQueue( + mSensorServer->createSensorEventConnection()); + return result; +} + +// ---------------------------------------------------------------------------- +}; // namespace android