1589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian/*
2589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Copyright (C) 2010 The Android Open Source Project
3589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *
4589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * you may not use this file except in compliance with the License.
6589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * You may obtain a copy of the License at
7589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *
8589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *
10589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Unless required by applicable law or agreed to in writing, software
11589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * See the License for the specific language governing permissions and
14589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * limitations under the License.
15589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian */
16589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
17589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <stdint.h>
18589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <sys/types.h>
19589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
20589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/Errors.h>
21589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/String8.h>
22589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/Flattenable.h>
23589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
24589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <hardware/sensors.h>
25589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
26589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/Sensor.h>
27589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
28589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ----------------------------------------------------------------------------
29589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiannamespace android {
30589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ----------------------------------------------------------------------------
31589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
32589ce85ee4174829cfedce91b6b2509d2a4002ebMathias AgopianSensor::Sensor()
33589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    : mHandle(0), mType(0),
34589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian      mMinValue(0), mMaxValue(0), mResolution(0),
35a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopian      mPower(0), mMinDelay(0)
36589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
37589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
38589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
39a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias AgopianSensor::Sensor(struct sensor_t const* hwSensor)
40a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian{
41a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    mName = hwSensor->name;
42a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    mVendor = hwSensor->vendor;
432ebc4d6d947ef488198b8777d9190c93904b4b9aMathias Agopian    mVersion = hwSensor->version;
44a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    mHandle = hwSensor->handle;
45a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    mType = hwSensor->type;
46a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    mMinValue = 0;                      // FIXME: minValue
47a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    mMaxValue = hwSensor->maxRange;     // FIXME: maxValue
48a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    mResolution = hwSensor->resolution;
49a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    mPower = hwSensor->power;
50a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopian    mMinDelay = hwSensor->minDelay;
51a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian}
52a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian
53589ce85ee4174829cfedce91b6b2509d2a4002ebMathias AgopianSensor::~Sensor()
54589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
55589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
56589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
57589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianconst String8& Sensor::getName() const {
58589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return mName;
59589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
60589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
61589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianconst String8& Sensor::getVendor() const {
62589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return mVendor;
63589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
64589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
65589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianint32_t Sensor::getHandle() const {
66589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return mHandle;
67589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
68589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
69589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianint32_t Sensor::getType() const {
70589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return mType;
71589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
72589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
73589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianfloat Sensor::getMinValue() const {
74589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return mMinValue;
75589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
76589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
77589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianfloat Sensor::getMaxValue() const {
78589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return mMaxValue;
79589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
80589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
81589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianfloat Sensor::getResolution() const {
82589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return mResolution;
83589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
84589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
85589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianfloat Sensor::getPowerUsage() const {
86589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return mPower;
87589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
88589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
89a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopianint32_t Sensor::getMinDelay() const {
90a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopian    return mMinDelay;
91a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopian}
92a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopian
93b62013f6aafee08cfad25a2712dc32264e44f783Mathias Agopiannsecs_t Sensor::getMinDelayNs() const {
94b62013f6aafee08cfad25a2712dc32264e44f783Mathias Agopian    return getMinDelay() * 1000;
95b62013f6aafee08cfad25a2712dc32264e44f783Mathias Agopian}
96b62013f6aafee08cfad25a2712dc32264e44f783Mathias Agopian
97b62013f6aafee08cfad25a2712dc32264e44f783Mathias Agopianint32_t Sensor::getVersion() const {
98b62013f6aafee08cfad25a2712dc32264e44f783Mathias Agopian    return mVersion;
99b62013f6aafee08cfad25a2712dc32264e44f783Mathias Agopian}
100b62013f6aafee08cfad25a2712dc32264e44f783Mathias Agopian
1018683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopiansize_t Sensor::getSize() const
102589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
103589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return  sizeof(int32_t) + ((mName.length() + 3) & ~3) +
104589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian            sizeof(int32_t) + ((mVendor.length() + 3) & ~3) +
1052ebc4d6d947ef488198b8777d9190c93904b4b9aMathias Agopian            sizeof(int32_t) * 3 +
106a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopian            sizeof(float) * 4 +
107a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopian            sizeof(int32_t);
108589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
109589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
110589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianstatic inline
111589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiansize_t write(void* buffer, size_t offset, const String8& value) {
112589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    memcpy(static_cast<char*>(buffer) + offset, value.string(), value.length());
113589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return (value.length() + 3) & ~3;
114589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
115589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
116589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianstatic inline
117589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiansize_t write(void* buffer, size_t offset, float value) {
118589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    *reinterpret_cast<float*>(static_cast<char*>(buffer) + offset) = value;
119589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return sizeof(float);
120589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
121589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
122589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianstatic inline
123589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiansize_t write(void* buffer, size_t offset, int32_t value) {
124589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    *reinterpret_cast<int32_t*>(static_cast<char*>(buffer) + offset) = value;
125589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return sizeof(int32_t);
126589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
127589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
1288683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopianstatus_t Sensor::flatten(void* buffer) const
129589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
130589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    size_t offset = 0;
131589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += write(buffer, offset, int32_t(mName.length()));
132589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += write(buffer, offset, mName);
133589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += write(buffer, offset, int32_t(mVendor.length()));
134589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += write(buffer, offset, mVendor);
1352ebc4d6d947ef488198b8777d9190c93904b4b9aMathias Agopian    offset += write(buffer, offset, mVersion);
136589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += write(buffer, offset, mHandle);
137589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += write(buffer, offset, mType);
138589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += write(buffer, offset, mMinValue);
139589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += write(buffer, offset, mMaxValue);
140589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += write(buffer, offset, mResolution);
141589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += write(buffer, offset, mPower);
142a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopian    offset += write(buffer, offset, mMinDelay);
143589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return NO_ERROR;
144589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
145589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
146589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianstatic inline
147589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiansize_t read(void const* buffer, size_t offset, String8* value, int32_t len) {
148589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    value->setTo(static_cast<char const*>(buffer) + offset, len);
149589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return (len + 3) & ~3;
150589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
151589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
152589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianstatic inline
153589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiansize_t read(void const* buffer, size_t offset, float* value) {
154589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    *value = *reinterpret_cast<float const*>(static_cast<char const*>(buffer) + offset);
155589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return sizeof(float);
156589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
157589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
158589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianstatic inline
159589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiansize_t read(void const* buffer, size_t offset, int32_t* value) {
160589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    *value = *reinterpret_cast<int32_t const*>(static_cast<char const*>(buffer) + offset);
161589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return sizeof(int32_t);
162589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
163589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
1648683fca395d01734ec7946e9f0595ec5d7b754c6Mathias Agopianstatus_t Sensor::unflatten(void const* buffer, size_t size)
165589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
166589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    int32_t len;
167589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    size_t offset = 0;
168589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += read(buffer, offset, &len);
169589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += read(buffer, offset, &mName, len);
170589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += read(buffer, offset, &len);
171589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += read(buffer, offset, &mVendor, len);
1722ebc4d6d947ef488198b8777d9190c93904b4b9aMathias Agopian    offset += read(buffer, offset, &mVersion);
173589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += read(buffer, offset, &mHandle);
174589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += read(buffer, offset, &mType);
175589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += read(buffer, offset, &mMinValue);
176589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += read(buffer, offset, &mMaxValue);
177589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += read(buffer, offset, &mResolution);
178589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    offset += read(buffer, offset, &mPower);
179a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopian    offset += read(buffer, offset, &mMinDelay);
180589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return NO_ERROR;
181589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
182589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
183589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ----------------------------------------------------------------------------
184589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}; // namespace android
185