aboutsummaryrefslogtreecommitdiff
path: root/platform/shared/platform_sensor_manager.cc
blob: e137992ea4d962e81a6f337a6958531fdc8cc686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "chre/platform/platform_sensor_manager.h"

#include <cinttypes>

#include "chre/core/event_loop_manager.h"
#include "chre/platform/log.h"
#include "chre/platform/shared/pal_system_api.h"

namespace chre {

const chrePalSensorCallbacks PlatformSensorManagerBase::sSensorCallbacks = {
    PlatformSensorManager::samplingStatusUpdateCallback,
    PlatformSensorManager::dataEventCallback,
    PlatformSensorManager::biasEventCallback,
    PlatformSensorManager::flushCompleteCallback,
};

PlatformSensorManager::~PlatformSensorManager() {
  if (mSensorApi != nullptr) {
    LOGD("Platform sensor manager closing");
    mSensorApi->close();
    LOGD("Platform sensor manager closed");
  }
}

void PlatformSensorManager::init() {
  mSensorApi = chrePalSensorGetApi(CHRE_PAL_SENSOR_API_CURRENT_VERSION);
  if (mSensorApi != nullptr) {
    if (!mSensorApi->open(&gChrePalSystemApi, &sSensorCallbacks)) {
      LOGE("Sensor PAL open returned false");
      mSensorApi = nullptr;
    } else {
      LOGD("Opened Sensor PAL version 0x%08" PRIx32, mSensorApi->moduleVersion);
    }
  } else {
    LOGW("Requested Sensor PAL (version 0x%08" PRIx32 ") not found",
         CHRE_PAL_SENSOR_API_CURRENT_VERSION);
  }
}

DynamicVector<Sensor> PlatformSensorManager::getSensors() {
  DynamicVector<Sensor> sensors;
  struct chreSensorInfo *palSensors = nullptr;
  uint32_t arraySize;
  if (mSensorApi != nullptr) {
    if (!mSensorApi->getSensors(&palSensors, &arraySize) || arraySize == 0) {
      LOGE("Failed to query the platform for sensors");
    } else if (!sensors.reserve(arraySize)) {
      LOG_OOM();
    } else {
      for (uint32_t i = 0; i < arraySize; i++) {
        struct chreSensorInfo *sensor = &palSensors[i];
        sensors.push_back(Sensor());
        sensors[i].initBase(sensor, i /* sensorHandle */);
        if (sensor->sensorName != nullptr) {
          LOGD("Found sensor: %s", sensor->sensorName);
        } else {
          LOGD("Sensor at index %" PRIu32 " has type %" PRIu8, i,
               sensor->sensorType);
        }
      }
    }
  }
  return sensors;
}

bool PlatformSensorManager::configureSensor(Sensor &sensor,
                                            const SensorRequest &request) {
  bool success = false;
  if (mSensorApi != nullptr) {
    success = mSensorApi->configureSensor(
        sensor.getSensorHandle(),
        getConfigureModeFromSensorMode(request.getMode()),
        request.getInterval().toRawNanoseconds(),
        request.getLatency().toRawNanoseconds());
  }
  return success;
}

bool PlatformSensorManager::configureBiasEvents(const Sensor &sensor,
                                                bool enable,
                                                uint64_t latencyNs) {
  bool success = false;
  if (mSensorApi != nullptr) {
    success = mSensorApi->configureBiasEvents(sensor.getSensorHandle(), enable,
                                              latencyNs);
  }
  return success;
}

bool PlatformSensorManager::getThreeAxisBias(
    const Sensor &sensor, struct chreSensorThreeAxisData *bias) const {
  bool success = false;
  if (mSensorApi != nullptr) {
    success = mSensorApi->getThreeAxisBias(sensor.getSensorHandle(), bias);
  }
  return success;
}

bool PlatformSensorManager::flush(const Sensor &sensor,
                                  uint32_t *flushRequestId) {
  bool success = false;
  if (mSensorApi != nullptr) {
    success = mSensorApi->flush(sensor.getSensorHandle(), flushRequestId);
  }
  return success;
}

void PlatformSensorManagerBase::samplingStatusUpdateCallback(
    uint32_t sensorHandle, struct chreSensorSamplingStatus *status) {
  EventLoopManagerSingleton::get()
      ->getSensorRequestManager()
      .handleSamplingStatusUpdate(sensorHandle, status);
}

void PlatformSensorManagerBase::dataEventCallback(uint32_t sensorHandle,
                                                  void *data) {
  EventLoopManagerSingleton::get()
      ->getSensorRequestManager()
      .handleSensorDataEvent(sensorHandle, data);
}

void PlatformSensorManagerBase::biasEventCallback(uint32_t sensorHandle,
                                                  void *biasData) {
  EventLoopManagerSingleton::get()->getSensorRequestManager().handleBiasEvent(
      sensorHandle, biasData);
}

void PlatformSensorManagerBase::flushCompleteCallback(uint32_t sensorHandle,
                                                      uint32_t flushRequestId,
                                                      uint8_t errorCode) {
  EventLoopManagerSingleton::get()
      ->getSensorRequestManager()
      .handleFlushCompleteEvent(sensorHandle, flushRequestId, errorCode);
}

void PlatformSensorManager::releaseSamplingStatusUpdate(
    struct chreSensorSamplingStatus *status) {
  mSensorApi->releaseSamplingStatusEvent(status);
}

void PlatformSensorManager::releaseSensorDataEvent(void *data) {
  mSensorApi->releaseSensorDataEvent(data);
}

void PlatformSensorManager::releaseBiasEvent(void *biasData) {
  mSensorApi->releaseBiasEvent(biasData);
}

}  // namespace chre