Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010 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 <stdint.h> |
| 18 | #include <sys/types.h> |
| 19 | |
| 20 | #include <utils/Errors.h> |
| 21 | #include <utils/String8.h> |
| 22 | #include <utils/Flattenable.h> |
| 23 | |
| 24 | #include <hardware/sensors.h> |
| 25 | |
| 26 | #include <gui/Sensor.h> |
| 27 | |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | namespace android { |
| 30 | // ---------------------------------------------------------------------------- |
| 31 | |
| 32 | Sensor::Sensor() |
| 33 | : mHandle(0), mType(0), |
| 34 | mMinValue(0), mMaxValue(0), mResolution(0), |
| 35 | mPower(0) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | Sensor::~Sensor() |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | const String8& Sensor::getName() const { |
| 44 | return mName; |
| 45 | } |
| 46 | |
| 47 | const String8& Sensor::getVendor() const { |
| 48 | return mVendor; |
| 49 | } |
| 50 | |
| 51 | int32_t Sensor::getHandle() const { |
| 52 | return mHandle; |
| 53 | } |
| 54 | |
| 55 | int32_t Sensor::getType() const { |
| 56 | return mType; |
| 57 | } |
| 58 | |
| 59 | float Sensor::getMinValue() const { |
| 60 | return mMinValue; |
| 61 | } |
| 62 | |
| 63 | float Sensor::getMaxValue() const { |
| 64 | return mMaxValue; |
| 65 | } |
| 66 | |
| 67 | float Sensor::getResolution() const { |
| 68 | return mResolution; |
| 69 | } |
| 70 | |
| 71 | float Sensor::getPowerUsage() const { |
| 72 | return mPower; |
| 73 | } |
| 74 | |
| 75 | size_t Sensor::getFlattenedSize() const |
| 76 | { |
| 77 | return sizeof(int32_t) + ((mName.length() + 3) & ~3) + |
| 78 | sizeof(int32_t) + ((mVendor.length() + 3) & ~3) + |
| 79 | sizeof(int32_t) * 2 + |
| 80 | sizeof(float) * 3; |
| 81 | } |
| 82 | |
| 83 | size_t Sensor::getFdCount() const |
| 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static inline |
| 89 | size_t write(void* buffer, size_t offset, const String8& value) { |
| 90 | memcpy(static_cast<char*>(buffer) + offset, value.string(), value.length()); |
| 91 | return (value.length() + 3) & ~3; |
| 92 | } |
| 93 | |
| 94 | static inline |
| 95 | size_t write(void* buffer, size_t offset, float value) { |
| 96 | *reinterpret_cast<float*>(static_cast<char*>(buffer) + offset) = value; |
| 97 | return sizeof(float); |
| 98 | } |
| 99 | |
| 100 | static inline |
| 101 | size_t write(void* buffer, size_t offset, int32_t value) { |
| 102 | *reinterpret_cast<int32_t*>(static_cast<char*>(buffer) + offset) = value; |
| 103 | return sizeof(int32_t); |
| 104 | } |
| 105 | |
| 106 | status_t Sensor::flatten(void* buffer, size_t size, |
| 107 | int fds[], size_t count) const |
| 108 | { |
| 109 | if (size < Sensor::getFlattenedSize()) |
| 110 | return -ENOMEM; |
| 111 | |
| 112 | size_t offset = 0; |
| 113 | offset += write(buffer, offset, int32_t(mName.length())); |
| 114 | offset += write(buffer, offset, mName); |
| 115 | offset += write(buffer, offset, int32_t(mVendor.length())); |
| 116 | offset += write(buffer, offset, mVendor); |
| 117 | offset += write(buffer, offset, mHandle); |
| 118 | offset += write(buffer, offset, mType); |
| 119 | offset += write(buffer, offset, mMinValue); |
| 120 | offset += write(buffer, offset, mMaxValue); |
| 121 | offset += write(buffer, offset, mResolution); |
| 122 | offset += write(buffer, offset, mPower); |
| 123 | |
| 124 | return NO_ERROR; |
| 125 | } |
| 126 | |
| 127 | static inline |
| 128 | size_t read(void const* buffer, size_t offset, String8* value, int32_t len) { |
| 129 | value->setTo(static_cast<char const*>(buffer) + offset, len); |
| 130 | return (len + 3) & ~3; |
| 131 | } |
| 132 | |
| 133 | static inline |
| 134 | size_t read(void const* buffer, size_t offset, float* value) { |
| 135 | *value = *reinterpret_cast<float const*>(static_cast<char const*>(buffer) + offset); |
| 136 | return sizeof(float); |
| 137 | } |
| 138 | |
| 139 | static inline |
| 140 | size_t read(void const* buffer, size_t offset, int32_t* value) { |
| 141 | *value = *reinterpret_cast<int32_t const*>(static_cast<char const*>(buffer) + offset); |
| 142 | return sizeof(int32_t); |
| 143 | } |
| 144 | |
| 145 | status_t Sensor::unflatten(void const* buffer, size_t size, |
| 146 | int fds[], size_t count) |
| 147 | { |
| 148 | int32_t len; |
| 149 | size_t offset = 0; |
| 150 | offset += read(buffer, offset, &len); |
| 151 | offset += read(buffer, offset, &mName, len); |
| 152 | offset += read(buffer, offset, &len); |
| 153 | offset += read(buffer, offset, &mVendor, len); |
| 154 | offset += read(buffer, offset, &mHandle); |
| 155 | offset += read(buffer, offset, &mType); |
| 156 | offset += read(buffer, offset, &mMinValue); |
| 157 | offset += read(buffer, offset, &mMaxValue); |
| 158 | offset += read(buffer, offset, &mResolution); |
| 159 | offset += read(buffer, offset, &mPower); |
| 160 | |
| 161 | return NO_ERROR; |
| 162 | } |
| 163 | |
| 164 | // ---------------------------------------------------------------------------- |
| 165 | }; // namespace android |