xref: /aosp_15_r20/system/chre/platform/shared/chre_api_sensor.cc (revision 84e339476a462649f82315436d70fd732297a399)
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 #include "chre_api/chre/sensor.h"
18 
19 #include "chre/core/event_loop_manager.h"
20 #include "chre/core/sensor_request.h"
21 #include "chre/util/macros.h"
22 #include "chre/util/time.h"
23 
24 using chre::EventLoopManager;
25 using chre::EventLoopManagerSingleton;
26 using chre::Nanoapp;
27 using chre::Nanoseconds;
28 using chre::SensorMode;
29 using chre::SensorRequest;
30 
31 using chre::getSensorModeFromEnum;
32 
chreSensorFindDefault(uint8_t sensorType,uint32_t * handle)33 DLL_EXPORT bool chreSensorFindDefault(uint8_t sensorType, uint32_t *handle) {
34   return chreSensorFind(sensorType, CHRE_SENSOR_INDEX_DEFAULT, handle);
35 }
36 
chreSensorFind(uint8_t sensorType,uint8_t sensorIndex,uint32_t * handle)37 DLL_EXPORT bool chreSensorFind(uint8_t sensorType, uint8_t sensorIndex,
38                                uint32_t *handle) {
39 #if CHRE_SENSORS_SUPPORT_ENABLED
40   Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
41   return EventLoopManagerSingleton::get()
42       ->getSensorRequestManager()
43       .getSensorHandleForNanoapp(sensorType, sensorIndex, *nanoapp, handle);
44 #else  // CHRE_SENSORS_SUPPORT_ENABLED
45   UNUSED_VAR(sensorType);
46   UNUSED_VAR(sensorIndex);
47   UNUSED_VAR(handle);
48   return false;
49 #endif
50 }
51 
chreGetSensorInfo(uint32_t sensorHandle,struct chreSensorInfo * info)52 DLL_EXPORT bool chreGetSensorInfo(uint32_t sensorHandle,
53                                   struct chreSensorInfo *info) {
54 #ifdef CHRE_SENSORS_SUPPORT_ENABLED
55   CHRE_ASSERT(info);
56 
57   Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
58 
59   bool success = false;
60   if (info != nullptr) {
61     success = EventLoopManagerSingleton::get()
62                   ->getSensorRequestManager()
63                   .getSensorInfo(sensorHandle, *nanoapp, info);
64   }
65   return success;
66 #else   // CHRE_SENSORS_SUPPORT_ENABLED
67   UNUSED_VAR(sensorHandle);
68   UNUSED_VAR(info);
69   return false;
70 #endif  // CHRE_SENSORS_SUPPORT_ENABLED
71 }
72 
chreGetSensorSamplingStatus(uint32_t sensorHandle,struct chreSensorSamplingStatus * status)73 DLL_EXPORT bool chreGetSensorSamplingStatus(
74     uint32_t sensorHandle, struct chreSensorSamplingStatus *status) {
75 #ifdef CHRE_SENSORS_SUPPORT_ENABLED
76   CHRE_ASSERT(status);
77 
78   bool success = false;
79   if (status != nullptr) {
80     success = EventLoopManagerSingleton::get()
81                   ->getSensorRequestManager()
82                   .getSensorSamplingStatus(sensorHandle, status);
83   }
84   return success;
85 #else   // CHRE_SENSORS_SUPPORT_ENABLED
86   UNUSED_VAR(sensorHandle);
87   UNUSED_VAR(status);
88   return false;
89 #endif  // CHRE_SENSORS_SUPPORT_ENABLED
90 }
91 
chreSensorConfigure(uint32_t sensorHandle,enum chreSensorConfigureMode mode,uint64_t interval,uint64_t latency)92 DLL_EXPORT bool chreSensorConfigure(uint32_t sensorHandle,
93                                     enum chreSensorConfigureMode mode,
94                                     uint64_t interval, uint64_t latency) {
95 #ifdef CHRE_SENSORS_SUPPORT_ENABLED
96   Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
97   SensorMode sensorMode = getSensorModeFromEnum(mode);
98   SensorRequest sensorRequest(nanoapp->getInstanceId(), sensorMode,
99                               Nanoseconds(interval), Nanoseconds(latency));
100   return EventLoopManagerSingleton::get()
101       ->getSensorRequestManager()
102       .setSensorRequest(nanoapp, sensorHandle, sensorRequest);
103 #else   // CHRE_SENSORS_SUPPORT_ENABLED
104   UNUSED_VAR(sensorHandle);
105   UNUSED_VAR(mode);
106   UNUSED_VAR(interval);
107   UNUSED_VAR(latency);
108   return false;
109 #endif  // CHRE_SENSORS_SUPPORT_ENABLED
110 }
111 
chreSensorConfigureBiasEvents(uint32_t sensorHandle,bool enable)112 DLL_EXPORT bool chreSensorConfigureBiasEvents(uint32_t sensorHandle,
113                                               bool enable) {
114 #ifdef CHRE_SENSORS_SUPPORT_ENABLED
115   Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
116   return EventLoopManagerSingleton::get()
117       ->getSensorRequestManager()
118       .configureBiasEvents(nanoapp, sensorHandle, enable);
119 #else   // CHRE_SENSORS_SUPPORT_ENABLED
120   UNUSED_VAR(sensorHandle);
121   UNUSED_VAR(enable);
122   return false;
123 #endif  // CHRE_SENSORS_SUPPORT_ENABLED
124 }
125 
chreSensorGetThreeAxisBias(uint32_t sensorHandle,struct chreSensorThreeAxisData * bias)126 DLL_EXPORT bool chreSensorGetThreeAxisBias(
127     uint32_t sensorHandle, struct chreSensorThreeAxisData *bias) {
128 #ifdef CHRE_SENSORS_SUPPORT_ENABLED
129   return EventLoopManagerSingleton::get()
130       ->getSensorRequestManager()
131       .getThreeAxisBias(sensorHandle, bias);
132 #else   // CHRE_SENSORS_SUPPORT_ENABLED
133   UNUSED_VAR(sensorHandle);
134   UNUSED_VAR(bias);
135   return false;
136 #endif  // CHRE_SENSORS_SUPPORT_ENABLED
137 }
138 
chreSensorFlushAsync(uint32_t sensorHandle,const void * cookie)139 DLL_EXPORT bool chreSensorFlushAsync(uint32_t sensorHandle,
140                                      const void *cookie) {
141 #ifdef CHRE_SENSORS_SUPPORT_ENABLED
142   Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
143   return EventLoopManagerSingleton::get()->getSensorRequestManager().flushAsync(
144       nanoapp, sensorHandle, cookie);
145 #else   // CHRE_SENSORS_SUPPORT_ENABLED
146   UNUSED_VAR(sensorHandle);
147   UNUSED_VAR(cookie);
148   return false;
149 #endif  // CHRE_SENSORS_SUPPORT_ENABLED
150 }
151