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 #pragma once
18 
19 #include <stdint.h>
20 #include <sys/types.h>
21 
22 #include <utils/Errors.h>
23 #include <utils/Flattenable.h>
24 #include <utils/String8.h>
25 #include <utils/Timers.h>
26 
27 // FIXME: including from android/ breaks layering, as libandroid ultimately depends on libsensors
28 #include <android/sensor.h>
29 
30 #include <hardware/sensors.h>
31 
32 // ----------------------------------------------------------------------------
33 // Concrete types for the NDK
34 struct ASensor { };
35 
36 // ----------------------------------------------------------------------------
37 namespace android {
38 // ----------------------------------------------------------------------------
39 
40 class Parcel;
41 
42 // ----------------------------------------------------------------------------
43 
44 class Sensor : public ASensor, public LightFlattenable<Sensor>
45 {
46 public:
47     enum {
48         TYPE_ACCELEROMETER  = ASENSOR_TYPE_ACCELEROMETER,
49         TYPE_MAGNETIC_FIELD = ASENSOR_TYPE_MAGNETIC_FIELD,
50         TYPE_GYROSCOPE      = ASENSOR_TYPE_GYROSCOPE,
51         TYPE_LIGHT          = ASENSOR_TYPE_LIGHT,
52         TYPE_PROXIMITY      = ASENSOR_TYPE_PROXIMITY
53     };
54 
55     struct uuid_t{
56         union {
57             uint8_t b[16];
58             int64_t i64[2];
59         };
uuid_tuuid_t60         explicit uuid_t(const uint8_t (&uuid)[16]) { memcpy(b, uuid, sizeof(b));}
uuid_tuuid_t61         uuid_t() : b{0} {}
62     };
63 
64     Sensor(const Sensor&) = default;
65     Sensor& operator=(const Sensor&) = default;
66 
67     explicit Sensor(const char * name = "");
68     explicit Sensor(struct sensor_t const* hwSensor, int halVersion = 0);
69     Sensor(struct sensor_t const& hwSensor, const uuid_t& uuid, int halVersion = 0);
70     ~Sensor();
71 
72     const String8& getName() const;
73     const String8& getVendor() const;
74     int32_t getHandle() const;
75     int32_t getType() const;
76     float getMinValue() const;
77     float getMaxValue() const;
78     float getResolution() const;
79     float getPowerUsage() const;
80     int32_t getMinDelay() const;
81     nsecs_t getMinDelayNs() const;
82     int32_t getVersion() const;
83     uint32_t getFifoReservedEventCount() const;
84     uint32_t getFifoMaxEventCount() const;
85     const String8& getStringType() const;
86     const String8& getRequiredPermission() const;
87     bool isRequiredPermissionRuntime() const;
88     int32_t getRequiredAppOp() const;
89     int32_t getMaxDelay() const;
90     uint32_t getFlags() const;
91     bool isWakeUpSensor() const;
92     bool isDynamicSensor() const;
93     bool isDataInjectionSupported() const;
94     bool hasAdditionalInfo() const;
95     int32_t getHighestDirectReportRateLevel() const;
96     bool isDirectChannelTypeSupported(int32_t sharedMemType) const;
97     int32_t getReportingMode() const;
98 
99     // Note that after setId() has been called, getUuid() no longer
100     // returns the UUID.
101     // TODO(b/29547335): Remove getUuid(), add getUuidIndex(), and
102     //     make sure setId() doesn't change the UuidIndex.
103     const uuid_t& getUuid() const;
104     int32_t getId() const;
105     void setId(int32_t id);
106 
107     // LightFlattenable protocol
isFixedSize()108     inline bool isFixedSize() const { return false; }
109     size_t getFlattenedSize() const;
110     status_t flatten(void* buffer, size_t size) const;
111     status_t unflatten(void const* buffer, size_t size);
112 
113 private:
114     String8 mName;
115     String8 mVendor;
116     int32_t mHandle;
117     int32_t mType;
118     float   mMinValue;
119     float   mMaxValue;
120     float   mResolution;
121     float   mPower;
122     int32_t mMinDelay;
123     int32_t mVersion;
124     uint32_t mFifoReservedEventCount;
125     uint32_t mFifoMaxEventCount;
126     String8 mStringType;
127     String8 mRequiredPermission;
128     bool mRequiredPermissionRuntime = false;
129     int32_t mRequiredAppOp;
130     int32_t mMaxDelay;
131     uint32_t mFlags;
132     // TODO(b/29547335): Get rid of this field and replace with an index.
133     //     The index will be into a separate global vector of UUIDs.
134     //     Also add an mId field (and change flatten/unflatten appropriately).
135     uuid_t  mUuid;
136     static void flattenString8(void*& buffer, size_t& size, const String8& string8);
137     static bool unflattenString8(void const*& buffer, size_t& size, String8& outputString8);
138 };
139 
140 // ----------------------------------------------------------------------------
141 }; // namespace android
142