1/*
2 * Copyright (C) 2017 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 "utils.h"
18
19#include <sensors/convert.h>
20
21namespace android {
22namespace frameworks {
23namespace sensorservice {
24namespace V1_0 {
25namespace implementation {
26
27using ::android::Sensor;
28using ::android::hardware::hidl_string;
29using ::android::hardware::sensors::V1_0::SensorInfo;
30
31SensorInfo convertSensor(const Sensor& src) {
32    SensorInfo dst;
33    const String8& name = src.getName();
34    const String8& vendor = src.getVendor();
35    dst.name = hidl_string{name.string(), name.size()};
36    dst.vendor = hidl_string{vendor.string(), vendor.size()};
37    dst.version = src.getVersion();
38    dst.sensorHandle = src.getHandle();
39    dst.type = static_cast<::android::hardware::sensors::V1_0::SensorType>(
40            src.getType());
41    // maxRange uses maxValue because ::android::Sensor wraps the
42    // internal sensor_t in this way.
43    dst.maxRange = src.getMaxValue();
44    dst.resolution = src.getResolution();
45    dst.power = src.getPowerUsage();
46    dst.minDelay = src.getMinDelay();
47    dst.fifoReservedEventCount = src.getFifoReservedEventCount();
48    dst.fifoMaxEventCount = src.getFifoMaxEventCount();
49    dst.typeAsString = src.getStringType();
50    dst.requiredPermission = src.getRequiredPermission();
51    dst.maxDelay = src.getMaxDelay();
52    dst.flags = src.getFlags();
53    return dst;
54}
55
56Result convertResult(status_t status) {
57    switch (status) {
58        case OK:
59            return Result::OK;
60        case NAME_NOT_FOUND:
61            return Result::NOT_EXIST;
62        case NO_MEMORY:
63            return Result::NO_MEMORY;
64        case NO_INIT:
65            return Result::NO_INIT;
66        case PERMISSION_DENIED:
67            return Result::PERMISSION_DENIED;
68        case BAD_VALUE:
69            return Result::BAD_VALUE;
70        case INVALID_OPERATION:
71            return Result::INVALID_OPERATION;
72        default:
73            return Result::UNKNOWN_ERROR;
74    }
75}
76
77::android::hardware::sensors::V1_0::Event convertEvent(const ::ASensorEvent& src) {
78    ::android::hardware::sensors::V1_0::Event dst;
79    ::android::hardware::sensors::V1_0::implementation::convertFromSensorEvent(
80            reinterpret_cast<const sensors_event_t&>(src), &dst);
81    return dst;
82}
83
84}  // namespace implementation
85}  // namespace V1_0
86}  // namespace sensorservice
87}  // namespace frameworks
88}  // namespace android
89