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/platform/context.h"
20#include "chre/util/time.h"
21#include "chre_api/chre/sensor.h"
22
23using chre::EventLoopManager;
24using chre::EventLoopManagerSingleton;
25using chre::Nanoseconds;
26using chre::SensorMode;
27using chre::SensorRequest;
28using chre::SensorType;
29
30using chre::getSensorModeFromEnum;
31using chre::getSensorTypeFromUnsignedInt;
32
33bool chreSensorFindDefault(uint8_t sensorType, uint32_t *handle) {
34  SensorType validatedSensorType = getSensorTypeFromUnsignedInt(sensorType);
35  return (validatedSensorType != SensorType::Unknown
36      && EventLoopManagerSingleton::get()->getSensorRequestManager()
37          .getSensorHandle(validatedSensorType, handle));
38}
39
40bool chreGetSensorInfo(uint32_t sensorHandle, struct chreSensorInfo *info) {
41  CHRE_ASSERT(info);
42
43  chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
44
45  bool success = false;
46  if (info != nullptr) {
47    success = EventLoopManagerSingleton::get()->getSensorRequestManager().
48        getSensorInfo(sensorHandle, nanoapp, info);
49  }
50  return success;
51}
52
53bool chreSensorConfigure(uint32_t sensorHandle,
54                         enum chreSensorConfigureMode mode,
55                         uint64_t interval, uint64_t latency) {
56  chre::Nanoapp *nanoapp = EventLoopManager::validateChreApiCall(__func__);
57  SensorMode sensorMode = getSensorModeFromEnum(mode);
58  SensorRequest sensorRequest(nanoapp, sensorMode, Nanoseconds(interval),
59                              Nanoseconds(latency));
60  return EventLoopManagerSingleton::get()->getSensorRequestManager()
61      .setSensorRequest(nanoapp, sensorHandle, sensorRequest);
62}
63