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