blob: cb85df9bb04d193b6eaaa7d9267e91fa96458246 [file] [log] [blame]
Mathias Agopian589ce852010-07-13 22:21:56 -07001/*
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// ----------------------------------------------------------------------------
29namespace android {
30// ----------------------------------------------------------------------------
31
32Sensor::Sensor()
33 : mHandle(0), mType(0),
34 mMinValue(0), mMaxValue(0), mResolution(0),
35 mPower(0)
36{
37}
38
Mathias Agopiana7352c92010-07-14 23:41:37 -070039Sensor::Sensor(struct sensor_t const* hwSensor)
40{
41 mName = hwSensor->name;
42 mVendor = hwSensor->vendor;
43 mHandle = hwSensor->handle;
44 mType = hwSensor->type;
45 mMinValue = 0; // FIXME: minValue
46 mMaxValue = hwSensor->maxRange; // FIXME: maxValue
47 mResolution = hwSensor->resolution;
48 mPower = hwSensor->power;
49}
50
Mathias Agopian589ce852010-07-13 22:21:56 -070051Sensor::~Sensor()
52{
53}
54
55const String8& Sensor::getName() const {
56 return mName;
57}
58
59const String8& Sensor::getVendor() const {
60 return mVendor;
61}
62
63int32_t Sensor::getHandle() const {
64 return mHandle;
65}
66
67int32_t Sensor::getType() const {
68 return mType;
69}
70
71float Sensor::getMinValue() const {
72 return mMinValue;
73}
74
75float Sensor::getMaxValue() const {
76 return mMaxValue;
77}
78
79float Sensor::getResolution() const {
80 return mResolution;
81}
82
83float Sensor::getPowerUsage() const {
84 return mPower;
85}
86
87size_t Sensor::getFlattenedSize() const
88{
89 return sizeof(int32_t) + ((mName.length() + 3) & ~3) +
90 sizeof(int32_t) + ((mVendor.length() + 3) & ~3) +
91 sizeof(int32_t) * 2 +
Mathias Agopiand03d9ce2010-07-22 18:57:41 -070092 sizeof(float) * 4;
Mathias Agopian589ce852010-07-13 22:21:56 -070093}
94
95size_t Sensor::getFdCount() const
96{
97 return 0;
98}
99
100static inline
101size_t write(void* buffer, size_t offset, const String8& value) {
102 memcpy(static_cast<char*>(buffer) + offset, value.string(), value.length());
103 return (value.length() + 3) & ~3;
104}
105
106static inline
107size_t write(void* buffer, size_t offset, float value) {
108 *reinterpret_cast<float*>(static_cast<char*>(buffer) + offset) = value;
109 return sizeof(float);
110}
111
112static inline
113size_t write(void* buffer, size_t offset, int32_t value) {
114 *reinterpret_cast<int32_t*>(static_cast<char*>(buffer) + offset) = value;
115 return sizeof(int32_t);
116}
117
118status_t Sensor::flatten(void* buffer, size_t size,
119 int fds[], size_t count) const
120{
121 if (size < Sensor::getFlattenedSize())
122 return -ENOMEM;
123
124 size_t offset = 0;
125 offset += write(buffer, offset, int32_t(mName.length()));
126 offset += write(buffer, offset, mName);
127 offset += write(buffer, offset, int32_t(mVendor.length()));
128 offset += write(buffer, offset, mVendor);
129 offset += write(buffer, offset, mHandle);
130 offset += write(buffer, offset, mType);
131 offset += write(buffer, offset, mMinValue);
132 offset += write(buffer, offset, mMaxValue);
133 offset += write(buffer, offset, mResolution);
134 offset += write(buffer, offset, mPower);
135
136 return NO_ERROR;
137}
138
139static inline
140size_t read(void const* buffer, size_t offset, String8* value, int32_t len) {
141 value->setTo(static_cast<char const*>(buffer) + offset, len);
142 return (len + 3) & ~3;
143}
144
145static inline
146size_t read(void const* buffer, size_t offset, float* value) {
147 *value = *reinterpret_cast<float const*>(static_cast<char const*>(buffer) + offset);
148 return sizeof(float);
149}
150
151static inline
152size_t read(void const* buffer, size_t offset, int32_t* value) {
153 *value = *reinterpret_cast<int32_t const*>(static_cast<char const*>(buffer) + offset);
154 return sizeof(int32_t);
155}
156
157status_t Sensor::unflatten(void const* buffer, size_t size,
158 int fds[], size_t count)
159{
160 int32_t len;
161 size_t offset = 0;
162 offset += read(buffer, offset, &len);
163 offset += read(buffer, offset, &mName, len);
164 offset += read(buffer, offset, &len);
165 offset += read(buffer, offset, &mVendor, len);
166 offset += read(buffer, offset, &mHandle);
167 offset += read(buffer, offset, &mType);
168 offset += read(buffer, offset, &mMinValue);
169 offset += read(buffer, offset, &mMaxValue);
170 offset += read(buffer, offset, &mResolution);
171 offset += read(buffer, offset, &mPower);
172
173 return NO_ERROR;
174}
175
176// ----------------------------------------------------------------------------
177}; // namespace android