blob: 1fdd285823d6c05fa51afbfe46cdb4a2fa034f5d [file] [log] [blame]
Mathias Agopianb957b9d2010-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
39Sensor::~Sensor()
40{
41}
42
43const String8& Sensor::getName() const {
44 return mName;
45}
46
47const String8& Sensor::getVendor() const {
48 return mVendor;
49}
50
51int32_t Sensor::getHandle() const {
52 return mHandle;
53}
54
55int32_t Sensor::getType() const {
56 return mType;
57}
58
59float Sensor::getMinValue() const {
60 return mMinValue;
61}
62
63float Sensor::getMaxValue() const {
64 return mMaxValue;
65}
66
67float Sensor::getResolution() const {
68 return mResolution;
69}
70
71float Sensor::getPowerUsage() const {
72 return mPower;
73}
74
75size_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
83size_t Sensor::getFdCount() const
84{
85 return 0;
86}
87
88static inline
89size_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
94static inline
95size_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
100static inline
101size_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
106status_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
127static inline
128size_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
133static inline
134size_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
139static inline
140size_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
145status_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