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