1 /*
2 * Copyright (C) 2016 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 #ifndef _SENSORS_H_
18 #define _SENSORS_H_
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 #include <plat/inc/taggedPtr.h>
24 #include <variant/inc/variant.h>
25 #include <eventnums.h>
26 #include <sensType.h>
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include "toolchain.h"
30
31 // Specify the maximum number of sensors that can be registered. Allow it to be
32 // overriden on a per-device basis.
33 #ifndef MAX_REGISTERED_SENSORS
34 #define MAX_REGISTERED_SENSORS 32
35 #endif // MAX_REGISTERED_SENSORS
36
37 #define MAX_MIN_SAMPLES 3000
38
39 enum NumAxis {
40 NUM_AXIS_EMBEDDED = 0, // data = (uint32_t)evtData
41 NUM_AXIS_ONE = 1, // data is in struct SingleAxisDataEvent format
42 NUM_AXIS_THREE = 3, // data is in struct TripleAxisDataEvent format
43 };
44
45 struct SensorFirstSample
46 {
47 uint8_t numSamples;
48 uint8_t numFlushes;
49 uint8_t biasCurrent : 1;
50 uint8_t biasPresent : 1;
51 uint8_t biasSample : 6;
52 uint8_t interrupt;
53 };
54
55 // NUM_AXIS_EMBEDDED data format
56 union EmbeddedDataPoint {
57 uint32_t idata;
58 float fdata;
59 void *vptr;
60 };
61
62 // NUM_AXIS_ONE data format
63 SET_PACKED_STRUCT_MODE_ON
64 struct SingleAxisDataPoint {
65 union {
66 uint32_t deltaTime; //delta since last sample, for 0th sample this is firstSample
67 struct SensorFirstSample firstSample;
68 };
69 union {
70 float fdata;
71 int32_t idata;
72 };
73 } ATTRIBUTE_PACKED;
74 SET_PACKED_STRUCT_MODE_OFF
75
76 struct SingleAxisDataEvent {
77 uint64_t referenceTime;
78 struct SingleAxisDataPoint samples[];
79 };
80
81 // NUM_AXIS_THREE data format
82 SET_PACKED_STRUCT_MODE_ON
83 struct TripleAxisDataPoint {
84 union {
85 uint32_t deltaTime; //delta since last sample, for 0th sample this is firstSample
86 struct SensorFirstSample firstSample;
87 };
88 union {
89 float x;
90 int32_t ix;
91 };
92 union {
93 float y;
94 int32_t iy;
95 };
96 union {
97 float z;
98 int32_t iz;
99 };
100 } ATTRIBUTE_PACKED;
101 SET_PACKED_STRUCT_MODE_OFF
102
103 struct TripleAxisDataEvent {
104 uint64_t referenceTime;
105 struct TripleAxisDataPoint samples[];
106 };
107
108 SET_PACKED_STRUCT_MODE_ON
109 struct RawTripleAxisDataPoint {
110 union {
111 uint32_t deltaTime; //delta since last sample, for 0th sample this is firstSample
112 struct SensorFirstSample firstSample;
113 };
114 int16_t ix;
115 int16_t iy;
116 int16_t iz;
117 } ATTRIBUTE_PACKED;
118 SET_PACKED_STRUCT_MODE_OFF
119
120 struct RawTripleAxisDataEvent {
121 uint64_t referenceTime;
122 struct RawTripleAxisDataPoint samples[];
123 };
124
125 struct UserSensorEventHdr { //all user sensor events start with this struct
126 TaggedPtr marshallCbk;
127 };
128
129 #define SENSOR_DATA_EVENT_FLUSH (void *)0xFFFFFFFF // flush for all data
130
131 struct SensorPowerEvent {
132 void *callData;
133 bool on;
134 };
135
136 struct SensorSetRateEvent {
137 void *callData;
138 uint32_t rate;
139 uint64_t latency;
140 };
141
142 struct SensorCfgDataEvent {
143 void *callData;
144 void *data;
145 };
146
147 struct SensorSendDirectEventEvent {
148 void *callData;
149 uint32_t tid;
150 };
151
152 struct SensorMarshallUserEventEvent {
153 void *callData;
154 uint32_t origEvtType;
155 void *origEvtData;
156 TaggedPtr evtFreeingInfo;
157 };
158
159
160
161
162 struct SensorOps {
163 bool (*sensorPower)(bool on, void *); /* -> SENSOR_INTERNAL_EVT_POWER_STATE_CHG (success) */
164 bool (*sensorFirmwareUpload)(void *); /* -> SENSOR_INTERNAL_EVT_FW_STATE_CHG (rate or 0 if fail) */
165 bool (*sensorSetRate)(uint32_t rate, uint64_t latency, void *);
166 /* -> SENSOR_INTERNAL_EVT_RATE_CHG (rate) */
167 bool (*sensorFlush)(void *); //trigger a measurement for ondemand sensors (if supported)
168 bool (*sensorTriggerOndemand)(void *);
169 bool (*sensorCalibrate)(void *);
170 bool (*sensorCfgData)(void *cfgData, void *);
171
172 bool (*sensorSendOneDirectEvt)(void *, uint32_t tid); //resend last state (if known), only for onchange-supporting sensors, to bring on a new client
173
174 // Marshall yourEvt for sending to host. Send a EVT_MARSHALLED_SENSOR_DATA event with marshalled data.
175 // Always send event, even on error, free the passed-in event using osFreeRetainedEvent
176 bool (*sensorMarshallData)(uint32_t yourEvtType, const void *yourEvtData, TaggedPtr *evtFreeingInfoP, void *);
177 bool (*sensorSelfTest)(void *);
178 };
179
180 enum SensorInfoFlags1 {
181 SENSOR_INFO_FLAGS1_BIAS = (1 << 0),
182 SENSOR_INFO_FLAGS1_RAW = (1 << 1),
183
184 // Indicates that this sensor's events are for local consumption within the
185 // hub only, i.e. they should not be transmitted to the host
186 SENSOR_INFO_FLAGS1_LOCAL_ONLY = (1 << 2),
187 };
188
189 struct SensorInfo {
190 const char *sensorName; /* sensors.c code does not use this */
191
192 /* Specify a list of rates supported in sensorSetRate, using a 0 to mark the
193 end of the list.
194
195 If SENSOR_RATE_ONCHANGE is included in this list, then sensor events
196 should only be sent on data changes, regardless of any underlying
197 sampling rate. In this case, the sensorSendOneDirectEvt callback will be
198 invoked on each call to sensorRequest() to send new clients initial data.
199
200 If SENSOR_RATE_ONDEMAND is included in this list, then the
201 sensorTriggerOndemand callback must be implemented.
202
203 If this list contains only explicit rates in Hz, then sensorRequests with
204 SENSOR_RATE_ONCHANGE or ONDEMAND will be rejected.
205
206 If NULL, the expectation is that rate is not applicable/configurable, and
207 only SENSOR_RATE_ONCHANGE or SENSOR_RATE_ONDEMAND will be accepted, but
208 neither on-change semantics or on-demand support is implied. */
209 const uint32_t *supportedRates;
210
211 uint8_t sensorType;
212 uint8_t numAxis; /* enum NumAxis */
213 uint8_t interrupt; /* interrupt to generate to AP */
214 uint8_t flags1; /* enum SensorInfoFlags1 */
215 uint16_t minSamples; /* minimum host fifo size (in # of samples) */
216 uint8_t biasType;
217 uint8_t rawType;
218 float rawScale;
219 };
220
221
222 /*
223 * Sensor rate is encoded as a 32-bit integer as number of samples it can
224 * provide per 1024 seconds, allowing representations of all useful values
225 * well. This define is to be used for static values only please, as GCC
226 * will convert it into a const int at compile time. Do not use this at
227 * runtime please. A few Magic values exist at both ends of the range
228 * 0 is used as a list sentinel and high numbers for special abilities.
229 */
230 #define SENSOR_RATE_ONDEMAND 0xFFFFFF00UL
231 #define SENSOR_RATE_ONCHANGE 0xFFFFFF01UL
232 #define SENSOR_RATE_ONESHOT 0xFFFFFF02UL
233 #define SENSOR_HZ(_hz) ((uint32_t)((_hz) * 1024.0f))
234
235 /*
236 * Sensor latency is a 64-bit integer specifying the allowable delay in ns
237 * that data can be buffered.
238 */
239 #define SENSOR_LATENCY_NODATA 0xFFFFFFFFFFFFFF00ULL
240
241 /*
242 * sensors module api
243 */
244 bool sensorsInit(void);
245
246 /*
247 * Api for sensor drivers
248 */
249 #define SENSOR_INTERNAL_EVT_POWER_STATE_CHG 0
250 #define SENSOR_INTERNAL_EVT_FW_STATE_CHG 1
251 #define SENSOR_INTERNAL_EVT_RATE_CHG 2
252
253 uint32_t sensorRegister(const struct SensorInfo *si, const struct SensorOps *ops, void *callData, bool initComplete); /* returns handle, copy is not made */
254 uint32_t sensorRegisterAsApp(const struct SensorInfo *si, uint32_t tid, void *callData, bool initComplete); /* returns handle, copy is not made */
255 bool sensorRegisterInitComplete(uint32_t handle);
256 bool sensorUnregister(uint32_t handle); /* your job to be sure it is off already */
257 bool sensorSignalInternalEvt(uint32_t handle, uint32_t intEvtNum, uint32_t value1, uint64_t value2);
258
259 #define sensorGetMyEventType(_sensorType) (EVT_NO_FIRST_SENSOR_EVENT + (_sensorType))
260
261
262 /*
263 * api for using sensors (enum is not synced with sensor sub/unsub, this is ok since we do not expect a lot of dynamic sub/unsub)
264 */
265 const struct SensorInfo* sensorFind(uint32_t sensorType, uint32_t idx, uint32_t *handleP); //enumerate all sensors of a type
266 bool sensorRequest(uint32_t clientTid, uint32_t sensorHandle, uint32_t rate, uint64_t latency);
267 bool sensorRequestRateChange(uint32_t clientTid, uint32_t sensorHandle, uint32_t newRate, uint64_t newLatency);
268 bool sensorRelease(uint32_t clientTid, uint32_t sensorHandle);
269 bool sensorTriggerOndemand(uint32_t clientTid, uint32_t sensorHandle);
270 bool sensorFlush(uint32_t sensorHandle);
271 bool sensorCalibrate(uint32_t sensorHandle);
272 bool sensorSelfTest(uint32_t sensorHandle);
273 bool sensorCfgData(uint32_t sensorHandle, void* cfgData);
274 uint32_t sensorGetCurRate(uint32_t sensorHandle);
275 uint64_t sensorGetCurLatency(uint32_t sensorHandle);
276 uint64_t sensorGetTime(void);
277 bool sensorGetInitComplete(uint32_t sensorHandle); // DO NOT poll on this value
278 bool sensorMarshallEvent(uint32_t sensorHandle, uint32_t evtType, void *evtData, TaggedPtr *evtFreeingInfoP);
279 int sensorUnregisterAll(uint32_t tid);
280
281 /*
282 * convenience funcs
283 */
sensorTimerLookupCommon(const uint32_t * supportedRates,const uint64_t * timerVals,uint32_t wantedRate)284 static inline uint64_t sensorTimerLookupCommon(const uint32_t *supportedRates, const uint64_t *timerVals, uint32_t wantedRate)
285 {
286 uint32_t rate;
287
288 while ((rate = *supportedRates++) != 0) {
289 if (rate == wantedRate)
290 return *timerVals;
291 timerVals++;
292 }
293
294 return 0;
295 }
296
297
298 #ifdef __cplusplus
299 }
300 #endif
301
302 #endif
303