1 /*
2 * Copyright (C) 2019 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 "chre/platform/platform_sensor_manager.h"
18
19 #include <cinttypes>
20
21 #include "chre/core/event_loop_manager.h"
22 #include "chre/platform/log.h"
23 #include "chre/platform/shared/pal_system_api.h"
24
25 namespace chre {
26
27 const chrePalSensorCallbacks PlatformSensorManagerBase::sSensorCallbacks = {
28 PlatformSensorManager::samplingStatusUpdateCallback,
29 PlatformSensorManager::dataEventCallback,
30 PlatformSensorManager::biasEventCallback,
31 PlatformSensorManager::flushCompleteCallback,
32 };
33
~PlatformSensorManager()34 PlatformSensorManager::~PlatformSensorManager() {
35 if (mSensorApi != nullptr) {
36 LOGD("Platform sensor manager closing");
37 mSensorApi->close();
38 LOGD("Platform sensor manager closed");
39 }
40 }
41
init()42 void PlatformSensorManager::init() {
43 mSensorApi = chrePalSensorGetApi(CHRE_PAL_SENSOR_API_CURRENT_VERSION);
44 if (mSensorApi != nullptr) {
45 if (!mSensorApi->open(&gChrePalSystemApi, &sSensorCallbacks)) {
46 LOGE("Sensor PAL open returned false");
47 mSensorApi = nullptr;
48 } else {
49 LOGD("Opened Sensor PAL version 0x%08" PRIx32, mSensorApi->moduleVersion);
50 }
51 } else {
52 LOGW("Requested Sensor PAL (version 0x%08" PRIx32 ") not found",
53 CHRE_PAL_SENSOR_API_CURRENT_VERSION);
54 }
55 }
56
getSensors()57 DynamicVector<Sensor> PlatformSensorManager::getSensors() {
58 DynamicVector<Sensor> sensors;
59 struct chreSensorInfo *palSensors = nullptr;
60 uint32_t arraySize;
61 if (mSensorApi != nullptr) {
62 if (!mSensorApi->getSensors(&palSensors, &arraySize) || arraySize == 0) {
63 LOGE("Failed to query the platform for sensors");
64 } else if (!sensors.reserve(arraySize)) {
65 LOG_OOM();
66 } else {
67 for (uint32_t i = 0; i < arraySize; i++) {
68 struct chreSensorInfo *sensor = &palSensors[i];
69 sensors.push_back(Sensor());
70 sensors[i].initBase(sensor, i /* sensorHandle */);
71 if (sensor->sensorName != nullptr) {
72 LOGD("Found sensor: %s", sensor->sensorName);
73 } else {
74 LOGD("Sensor at index %" PRIu32 " has type %" PRIu8, i,
75 sensor->sensorType);
76 }
77 }
78 }
79 }
80 return sensors;
81 }
82
configureSensor(Sensor & sensor,const SensorRequest & request)83 bool PlatformSensorManager::configureSensor(Sensor &sensor,
84 const SensorRequest &request) {
85 bool success = false;
86 if (mSensorApi != nullptr) {
87 success = mSensorApi->configureSensor(
88 sensor.getSensorHandle(),
89 getConfigureModeFromSensorMode(request.getMode()),
90 request.getInterval().toRawNanoseconds(),
91 request.getLatency().toRawNanoseconds());
92 }
93 return success;
94 }
95
configureBiasEvents(const Sensor & sensor,bool enable,uint64_t latencyNs)96 bool PlatformSensorManager::configureBiasEvents(const Sensor &sensor,
97 bool enable,
98 uint64_t latencyNs) {
99 bool success = false;
100 if (mSensorApi != nullptr) {
101 success = mSensorApi->configureBiasEvents(sensor.getSensorHandle(), enable,
102 latencyNs);
103 }
104 return success;
105 }
106
getThreeAxisBias(const Sensor & sensor,struct chreSensorThreeAxisData * bias) const107 bool PlatformSensorManager::getThreeAxisBias(
108 const Sensor &sensor, struct chreSensorThreeAxisData *bias) const {
109 bool success = false;
110 if (mSensorApi != nullptr) {
111 success = mSensorApi->getThreeAxisBias(sensor.getSensorHandle(), bias);
112 }
113 return success;
114 }
115
flush(const Sensor & sensor,uint32_t * flushRequestId)116 bool PlatformSensorManager::flush(const Sensor &sensor,
117 uint32_t *flushRequestId) {
118 bool success = false;
119 if (mSensorApi != nullptr) {
120 success = mSensorApi->flush(sensor.getSensorHandle(), flushRequestId);
121 }
122 return success;
123 }
124
samplingStatusUpdateCallback(uint32_t sensorHandle,struct chreSensorSamplingStatus * status)125 void PlatformSensorManagerBase::samplingStatusUpdateCallback(
126 uint32_t sensorHandle, struct chreSensorSamplingStatus *status) {
127 EventLoopManagerSingleton::get()
128 ->getSensorRequestManager()
129 .handleSamplingStatusUpdate(sensorHandle, status);
130 }
131
dataEventCallback(uint32_t sensorHandle,void * data)132 void PlatformSensorManagerBase::dataEventCallback(uint32_t sensorHandle,
133 void *data) {
134 EventLoopManagerSingleton::get()
135 ->getSensorRequestManager()
136 .handleSensorDataEvent(sensorHandle, data);
137 }
138
biasEventCallback(uint32_t sensorHandle,void * biasData)139 void PlatformSensorManagerBase::biasEventCallback(uint32_t sensorHandle,
140 void *biasData) {
141 EventLoopManagerSingleton::get()->getSensorRequestManager().handleBiasEvent(
142 sensorHandle, biasData);
143 }
144
flushCompleteCallback(uint32_t sensorHandle,uint32_t flushRequestId,uint8_t errorCode)145 void PlatformSensorManagerBase::flushCompleteCallback(uint32_t sensorHandle,
146 uint32_t flushRequestId,
147 uint8_t errorCode) {
148 EventLoopManagerSingleton::get()
149 ->getSensorRequestManager()
150 .handleFlushCompleteEvent(sensorHandle, flushRequestId, errorCode);
151 }
152
getTargetGroupId(const Nanoapp &) const153 uint16_t PlatformSensorManager::getTargetGroupId(
154 const Nanoapp & /*nanoapp*/) const {
155 // Target group IDs are not supported for PALs so always assume 1 since
156 // all sensors group masks are 0xFFFF.
157 return 1;
158 }
159
releaseSamplingStatusUpdate(struct chreSensorSamplingStatus * status)160 void PlatformSensorManager::releaseSamplingStatusUpdate(
161 struct chreSensorSamplingStatus *status) {
162 mSensorApi->releaseSamplingStatusEvent(status);
163 }
164
releaseSensorDataEvent(void * data)165 void PlatformSensorManager::releaseSensorDataEvent(void *data) {
166 mSensorApi->releaseSensorDataEvent(data);
167 }
168
releaseBiasEvent(void * biasData)169 void PlatformSensorManager::releaseBiasEvent(void *biasData) {
170 mSensorApi->releaseBiasEvent(biasData);
171 }
172
173 } // namespace chre
174