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
48 #ifdef CHRE_TELEMETRY_SUPPORT_ENABLED
49 EventLoopManagerSingleton::get()->getTelemetryManager().onPalOpenFailure(
50 TelemetryManager::PalType::SENSOR);
51 #endif // CHRE_TELEMETRY_SUPPORT_ENABLED
52
53 mSensorApi = nullptr;
54 } else {
55 LOGD("Opened Sensor PAL version 0x%08" PRIx32, mSensorApi->moduleVersion);
56 }
57 } else {
58 LOGW("Requested Sensor PAL (version 0x%08" PRIx32 ") not found",
59 CHRE_PAL_SENSOR_API_CURRENT_VERSION);
60 }
61 }
62
getSensors()63 DynamicVector<Sensor> PlatformSensorManager::getSensors() {
64 DynamicVector<Sensor> sensors;
65 const struct chreSensorInfo *palSensors = nullptr;
66 uint32_t arraySize;
67 if (mSensorApi != nullptr) {
68 if (!mSensorApi->getSensors(&palSensors, &arraySize) || arraySize == 0) {
69 LOGE("Failed to query the platform for sensors");
70 } else if (!sensors.reserve(arraySize)) {
71 LOG_OOM();
72 } else {
73 for (uint32_t i = 0; i < arraySize; i++) {
74 const struct chreSensorInfo *sensor = &palSensors[i];
75 sensors.push_back(Sensor());
76 sensors[i].initBase(sensor, i /* sensorHandle */);
77 if (sensor->sensorName != nullptr) {
78 LOGD("Found sensor: %s", sensor->sensorName);
79 } else {
80 LOGD("Sensor at index %" PRIu32 " has type %" PRIu8, i,
81 sensor->sensorType);
82 }
83 }
84 }
85 }
86 return sensors;
87 }
88
configureSensor(Sensor & sensor,const SensorRequest & request)89 bool PlatformSensorManager::configureSensor(Sensor &sensor,
90 const SensorRequest &request) {
91 bool success = false;
92 if (mSensorApi != nullptr) {
93 success = mSensorApi->configureSensor(
94 sensor.getSensorHandle(),
95 getConfigureModeFromSensorMode(request.getMode()),
96 request.getInterval().toRawNanoseconds(),
97 request.getLatency().toRawNanoseconds());
98 }
99 return success;
100 }
101
configureBiasEvents(const Sensor & sensor,bool enable,uint64_t latencyNs)102 bool PlatformSensorManager::configureBiasEvents(const Sensor &sensor,
103 bool enable,
104 uint64_t latencyNs) {
105 bool success = false;
106 if (mSensorApi != nullptr) {
107 success = mSensorApi->configureBiasEvents(sensor.getSensorHandle(), enable,
108 latencyNs);
109 }
110 return success;
111 }
112
getThreeAxisBias(const Sensor & sensor,struct chreSensorThreeAxisData * bias) const113 bool PlatformSensorManager::getThreeAxisBias(
114 const Sensor &sensor, struct chreSensorThreeAxisData *bias) const {
115 bool success = false;
116 if (mSensorApi != nullptr) {
117 success = mSensorApi->getThreeAxisBias(sensor.getSensorHandle(), bias);
118 }
119 return success;
120 }
121
flush(const Sensor & sensor,uint32_t * flushRequestId)122 bool PlatformSensorManager::flush(const Sensor &sensor,
123 uint32_t *flushRequestId) {
124 bool success = false;
125 if (mSensorApi != nullptr) {
126 success = mSensorApi->flush(sensor.getSensorHandle(), flushRequestId);
127 }
128 return success;
129 }
130
samplingStatusUpdateCallback(uint32_t sensorHandle,struct chreSensorSamplingStatus * status)131 void PlatformSensorManagerBase::samplingStatusUpdateCallback(
132 uint32_t sensorHandle, struct chreSensorSamplingStatus *status) {
133 EventLoopManagerSingleton::get()
134 ->getSensorRequestManager()
135 .handleSamplingStatusUpdate(sensorHandle, status);
136 }
137
dataEventCallback(uint32_t sensorHandle,void * data)138 void PlatformSensorManagerBase::dataEventCallback(uint32_t sensorHandle,
139 void *data) {
140 EventLoopManagerSingleton::get()
141 ->getSensorRequestManager()
142 .handleSensorDataEvent(sensorHandle, data);
143 }
144
biasEventCallback(uint32_t sensorHandle,void * biasData)145 void PlatformSensorManagerBase::biasEventCallback(uint32_t sensorHandle,
146 void *biasData) {
147 EventLoopManagerSingleton::get()->getSensorRequestManager().handleBiasEvent(
148 sensorHandle, biasData);
149 }
150
flushCompleteCallback(uint32_t sensorHandle,uint32_t flushRequestId,uint8_t errorCode)151 void PlatformSensorManagerBase::flushCompleteCallback(uint32_t sensorHandle,
152 uint32_t flushRequestId,
153 uint8_t errorCode) {
154 EventLoopManagerSingleton::get()
155 ->getSensorRequestManager()
156 .handleFlushCompleteEvent(sensorHandle, flushRequestId, errorCode);
157 }
158
getTargetGroupId(const Nanoapp &) const159 uint16_t PlatformSensorManager::getTargetGroupId(
160 const Nanoapp & /*nanoapp*/) const {
161 // Target group IDs are not supported for PALs so always assume 1 since
162 // all sensors group masks are 0xFFFF.
163 return 1;
164 }
165
releaseSamplingStatusUpdate(struct chreSensorSamplingStatus * status)166 void PlatformSensorManager::releaseSamplingStatusUpdate(
167 struct chreSensorSamplingStatus *status) {
168 mSensorApi->releaseSamplingStatusEvent(status);
169 }
170
releaseSensorDataEvent(void * data)171 void PlatformSensorManager::releaseSensorDataEvent(void *data) {
172 mSensorApi->releaseSensorDataEvent(data);
173 }
174
releaseBiasEvent(void * biasData)175 void PlatformSensorManager::releaseBiasEvent(void *biasData) {
176 mSensorApi->releaseBiasEvent(biasData);
177 }
178
179 } // namespace chre
180