sensor_request.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include <algorithm>
  17. #include "chre/core/sensor_request.h"
  18. #include "chre/platform/assert.h"
  19. #include "chre/platform/fatal_error.h"
  20. namespace chre {
  21. namespace {
  22. Nanoseconds getBatchInterval(const SensorRequest& request) {
  23. // With capping in SensorRequest constructor, interval + latency < UINT64_MAX.
  24. // When the return value is default, request latency (instead of batch
  25. // interval) will be used to compute the merged latency.
  26. if (request.getInterval() == Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT)
  27. || request.getLatency() == Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT)) {
  28. return Nanoseconds(CHRE_SENSOR_BATCH_INTERVAL_DEFAULT);
  29. } else {
  30. return request.getInterval() + request.getLatency();
  31. }
  32. }
  33. } // namespace
  34. SensorRequest::SensorRequest()
  35. : SensorRequest(SensorMode::Off,
  36. Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT),
  37. Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT)) {}
  38. SensorRequest::SensorRequest(SensorMode mode, Nanoseconds interval,
  39. Nanoseconds latency)
  40. : SensorRequest(kInvalidInstanceId, mode, interval, latency) {}
  41. SensorRequest::SensorRequest(uint32_t instanceId, SensorMode mode,
  42. Nanoseconds interval, Nanoseconds latency)
  43. : mInstanceId(instanceId), mInterval(interval), mLatency(latency),
  44. mMode(mode) {
  45. // cap non-default interval/latency to ensure no overflow in CHRE internal
  46. // operations.
  47. if (interval != Nanoseconds(CHRE_SENSOR_INTERVAL_DEFAULT)) {
  48. mInterval = std::min(interval, Nanoseconds(kMaxIntervalLatencyNs));
  49. }
  50. if (latency != Nanoseconds(CHRE_SENSOR_LATENCY_DEFAULT)) {
  51. mLatency = std::min(latency, Nanoseconds(kMaxIntervalLatencyNs));
  52. }
  53. }
  54. bool SensorRequest::isEquivalentTo(const SensorRequest& request) const {
  55. return (mMode == request.mMode
  56. && mInterval == request.mInterval
  57. && mLatency == request.mLatency);
  58. }
  59. bool SensorRequest::mergeWith(const SensorRequest& request) {
  60. bool attributesChanged = false;
  61. if (request.mMode != SensorMode::Off) {
  62. // Calculate minimum batch interval before mInterval is modified.
  63. Nanoseconds batchInterval = std::min(getBatchInterval(*this),
  64. getBatchInterval(request));
  65. if (request.mInterval < mInterval) {
  66. mInterval = request.mInterval;
  67. attributesChanged = true;
  68. }
  69. if (batchInterval == Nanoseconds(CHRE_SENSOR_BATCH_INTERVAL_DEFAULT)) {
  70. // If batchInterval is default, it can't be effectively calculated.
  71. // Use request.mLatency for more aggressive latency merging in this case.
  72. Nanoseconds latency = request.mLatency;
  73. if (latency < mLatency) {
  74. mLatency = latency;
  75. attributesChanged = true;
  76. }
  77. } else {
  78. Nanoseconds latency = (batchInterval - mInterval);
  79. // Note that while batchInterval can only shrink after merging, latency
  80. // can grow if the merged interval is lower.
  81. // Also, it's guaranteed that latency <= kMaxIntervalLatencyNs.
  82. if (latency != mLatency) {
  83. mLatency = latency;
  84. attributesChanged = true;
  85. }
  86. }
  87. // Compute the highest priority mode. Active continuous is the highest
  88. // priority and passive one-shot is the lowest.
  89. SensorMode maximalSensorMode = SensorMode::Off;
  90. if (mMode == SensorMode::ActiveContinuous
  91. || request.mMode == SensorMode::ActiveContinuous) {
  92. maximalSensorMode = SensorMode::ActiveContinuous;
  93. } else if (mMode == SensorMode::ActiveOneShot
  94. || request.mMode == SensorMode::ActiveOneShot) {
  95. maximalSensorMode = SensorMode::ActiveOneShot;
  96. } else if (mMode == SensorMode::PassiveContinuous
  97. || request.mMode == SensorMode::PassiveContinuous) {
  98. maximalSensorMode = SensorMode::PassiveContinuous;
  99. } else if (mMode == SensorMode::PassiveOneShot
  100. || request.mMode == SensorMode::PassiveOneShot) {
  101. maximalSensorMode = SensorMode::PassiveOneShot;
  102. } else {
  103. CHRE_ASSERT(false);
  104. }
  105. if (mMode != maximalSensorMode) {
  106. mMode = maximalSensorMode;
  107. attributesChanged = true;
  108. }
  109. }
  110. return attributesChanged;
  111. }
  112. } // namespace chre