• 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/sensor.h"
18 
19 #include "chre/core/event_loop_manager.h"
20 #include "chre_api/chre/version.h"
21 
22 namespace chre {
23 Mutex Sensor::mSamplingStatusMutex;
24 
Sensor(Sensor && other)25 Sensor::Sensor(Sensor &&other)
26     : PlatformSensor(std::move(other)), mFlushRequestPending(false) {
27   *this = std::move(other);
28 }
29 
operator =(Sensor && other)30 Sensor &Sensor::operator=(Sensor &&other) {
31   PlatformSensor::operator=(std::move(other));
32 
33   mSensorRequests = std::move(other.mSensorRequests);
34 
35   mFlushRequestTimerHandle = other.mFlushRequestTimerHandle;
36   other.mFlushRequestTimerHandle = CHRE_TIMER_INVALID;
37 
38   mFlushRequestPending = other.mFlushRequestPending.load();
39   other.mFlushRequestPending = false;
40 
41   mLastEvent = other.mLastEvent;
42   other.mLastEvent = nullptr;
43 
44   mLastEventValid = other.mLastEventValid;
45   other.mLastEventValid = false;
46 
47   return *this;
48 }
49 
~Sensor()50 Sensor::~Sensor() {
51   if (mLastEvent != nullptr) {
52     LOGD("Releasing lastEvent: sensor %s, size %zu", getSensorName(),
53          getLastEventSize());
54     memoryFree(mLastEvent);
55   }
56 }
57 
init()58 void Sensor::init() {
59   size_t lastEventSize = getLastEventSize();
60   if (lastEventSize > 0) {
61     mLastEvent = static_cast<ChreSensorData *>(memoryAlloc(lastEventSize));
62     if (mLastEvent == nullptr) {
63       FATAL_ERROR("Failed to allocate last event memory for %s",
64                   getSensorName());
65     }
66   }
67 }
68 
populateSensorInfo(struct chreSensorInfo * info,uint32_t targetApiVersion) const69 void Sensor::populateSensorInfo(struct chreSensorInfo *info,
70                                 uint32_t targetApiVersion) const {
71   info->sensorType = getSensorType();
72   info->isOnChange = isOnChange();
73   info->isOneShot = isOneShot();
74   info->reportsBiasEvents = reportsBiasEvents();
75   info->supportsPassiveMode = supportsPassiveMode();
76   info->unusedFlags = 0;
77   info->sensorName = getSensorName();
78 
79   // minInterval was added in CHRE API v1.1 - do not attempt to populate for
80   // nanoapps targeting v1.0 as their struct will not be large enough
81   if (targetApiVersion >= CHRE_API_VERSION_1_1) {
82     info->minInterval = getMinInterval();
83   }
84 }
85 
clearPendingFlushRequest()86 void Sensor::clearPendingFlushRequest() {
87   cancelPendingFlushRequestTimer();
88   mFlushRequestPending = false;
89 }
90 
cancelPendingFlushRequestTimer()91 void Sensor::cancelPendingFlushRequestTimer() {
92   if (mFlushRequestTimerHandle != CHRE_TIMER_INVALID) {
93     EventLoopManagerSingleton::get()->cancelDelayedCallback(
94         mFlushRequestTimerHandle);
95     mFlushRequestTimerHandle = CHRE_TIMER_INVALID;
96   }
97 }
98 
setLastEvent(ChreSensorData * event)99 void Sensor::setLastEvent(ChreSensorData *event) {
100   if (event == nullptr) {
101     mLastEventValid = false;
102   } else {
103     CHRE_ASSERT(event->header.readingCount > 0);
104 
105     SensorTypeHelpers::getLastSample(getSensorType(), event, mLastEvent);
106     mLastEventValid = true;
107   }
108 }
109 
getSamplingStatus(struct chreSensorSamplingStatus * status) const110 bool Sensor::getSamplingStatus(struct chreSensorSamplingStatus *status) const {
111   CHRE_ASSERT(status != nullptr);
112   LockGuard<Mutex> mLock(mSamplingStatusMutex);
113 
114   memcpy(status, &mSamplingStatus, sizeof(*status));
115   return true;
116 }
117 
setSamplingStatus(const struct chreSensorSamplingStatus & status)118 void Sensor::setSamplingStatus(const struct chreSensorSamplingStatus &status) {
119   LockGuard<Mutex> mLock(mSamplingStatusMutex);
120   mSamplingStatus = status;
121 }
122 
123 }  // namespace chre
124