• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Sensor()32 Sensor::Sensor()
33     : mHandle(0), mType(0),
34       mMinValue(0), mMaxValue(0), mResolution(0),
35       mPower(0), mMinDelay(0)
36 {
37 }
38 
Sensor(struct sensor_t const * hwSensor)39 Sensor::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     mMinDelay = hwSensor->minDelay;
50 }
51 
~Sensor()52 Sensor::~Sensor()
53 {
54 }
55 
getName() const56 const String8& Sensor::getName() const {
57     return mName;
58 }
59 
getVendor() const60 const String8& Sensor::getVendor() const {
61     return mVendor;
62 }
63 
getHandle() const64 int32_t Sensor::getHandle() const {
65     return mHandle;
66 }
67 
getType() const68 int32_t Sensor::getType() const {
69     return mType;
70 }
71 
getMinValue() const72 float Sensor::getMinValue() const {
73     return mMinValue;
74 }
75 
getMaxValue() const76 float Sensor::getMaxValue() const {
77     return mMaxValue;
78 }
79 
getResolution() const80 float Sensor::getResolution() const {
81     return mResolution;
82 }
83 
getPowerUsage() const84 float Sensor::getPowerUsage() const {
85     return mPower;
86 }
87 
getMinDelay() const88 int32_t Sensor::getMinDelay() const {
89     return mMinDelay;
90 }
91 
getMinDelayNs() const92 nsecs_t Sensor::getMinDelayNs() const {
93     return getMinDelay() * 1000;
94 }
95 
getVersion() const96 int32_t Sensor::getVersion() const {
97     return mVersion;
98 }
99 
getFlattenedSize() const100 size_t Sensor::getFlattenedSize() const
101 {
102     return  sizeof(int32_t) + ((mName.length() + 3) & ~3) +
103             sizeof(int32_t) + ((mVendor.length() + 3) & ~3) +
104             sizeof(int32_t) * 2 +
105             sizeof(float) * 4 +
106             sizeof(int32_t);
107 }
108 
getFdCount() const109 size_t Sensor::getFdCount() const
110 {
111     return 0;
112 }
113 
114 static inline
write(void * buffer,size_t offset,const String8 & value)115 size_t write(void* buffer, size_t offset, const String8& value) {
116     memcpy(static_cast<char*>(buffer) + offset, value.string(), value.length());
117     return (value.length() + 3) & ~3;
118 }
119 
120 static inline
write(void * buffer,size_t offset,float value)121 size_t write(void* buffer, size_t offset, float value) {
122     *reinterpret_cast<float*>(static_cast<char*>(buffer) + offset) = value;
123     return sizeof(float);
124 }
125 
126 static inline
write(void * buffer,size_t offset,int32_t value)127 size_t write(void* buffer, size_t offset, int32_t value) {
128     *reinterpret_cast<int32_t*>(static_cast<char*>(buffer) + offset) = value;
129     return sizeof(int32_t);
130 }
131 
flatten(void * buffer,size_t size,int fds[],size_t count) const132 status_t Sensor::flatten(void* buffer, size_t size,
133         int fds[], size_t count) const
134 {
135     if (size < Sensor::getFlattenedSize())
136         return -ENOMEM;
137 
138     size_t offset = 0;
139     offset += write(buffer, offset, int32_t(mName.length()));
140     offset += write(buffer, offset, mName);
141     offset += write(buffer, offset, int32_t(mVendor.length()));
142     offset += write(buffer, offset, mVendor);
143     offset += write(buffer, offset, mHandle);
144     offset += write(buffer, offset, mType);
145     offset += write(buffer, offset, mMinValue);
146     offset += write(buffer, offset, mMaxValue);
147     offset += write(buffer, offset, mResolution);
148     offset += write(buffer, offset, mPower);
149     offset += write(buffer, offset, mMinDelay);
150 
151     return NO_ERROR;
152 }
153 
154 static inline
read(void const * buffer,size_t offset,String8 * value,int32_t len)155 size_t read(void const* buffer, size_t offset, String8* value, int32_t len) {
156     value->setTo(static_cast<char const*>(buffer) + offset, len);
157     return (len + 3) & ~3;
158 }
159 
160 static inline
read(void const * buffer,size_t offset,float * value)161 size_t read(void const* buffer, size_t offset, float* value) {
162     *value = *reinterpret_cast<float const*>(static_cast<char const*>(buffer) + offset);
163     return sizeof(float);
164 }
165 
166 static inline
read(void const * buffer,size_t offset,int32_t * value)167 size_t read(void const* buffer, size_t offset, int32_t* value) {
168     *value = *reinterpret_cast<int32_t const*>(static_cast<char const*>(buffer) + offset);
169     return sizeof(int32_t);
170 }
171 
unflatten(void const * buffer,size_t size,int fds[],size_t count)172 status_t Sensor::unflatten(void const* buffer, size_t size,
173         int fds[], size_t count)
174 {
175     int32_t len;
176     size_t offset = 0;
177     offset += read(buffer, offset, &len);
178     offset += read(buffer, offset, &mName, len);
179     offset += read(buffer, offset, &len);
180     offset += read(buffer, offset, &mVendor, len);
181     offset += read(buffer, offset, &mHandle);
182     offset += read(buffer, offset, &mType);
183     offset += read(buffer, offset, &mMinValue);
184     offset += read(buffer, offset, &mMaxValue);
185     offset += read(buffer, offset, &mResolution);
186     offset += read(buffer, offset, &mPower);
187     offset += read(buffer, offset, &mMinDelay);
188 
189     return NO_ERROR;
190 }
191 
192 // ----------------------------------------------------------------------------
193 }; // namespace android
194