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