1c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong/*
2c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong * Copyright (C) 2017 The Android Open Source Project
3c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong *
4c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong * Licensed under the Apache License, Version 2.0 (the "License");
5c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong * you may not use this file except in compliance with the License.
6c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong * You may obtain a copy of the License at
7c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong *
8c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong *      http://www.apache.org/licenses/LICENSE-2.0
9c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong *
10c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong * Unless required by applicable law or agreed to in writing, software
11c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong * distributed under the License is distributed on an "AS IS" BASIS,
12c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong * See the License for the specific language governing permissions and
14c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong * limitations under the License.
15c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong */
16c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong
17c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong#include "utils.h"
18c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong
1995c7a0636763c0861d46425709befe90a3919c04Yifan Hong#include <sensors/convert.h>
2095c7a0636763c0861d46425709befe90a3919c04Yifan Hong
21c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hongnamespace android {
22c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hongnamespace frameworks {
23c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hongnamespace sensorservice {
24c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hongnamespace V1_0 {
25c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hongnamespace implementation {
26c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong
27c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hongusing ::android::Sensor;
28c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hongusing ::android::hardware::hidl_string;
29c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hongusing ::android::hardware::sensors::V1_0::SensorInfo;
30c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong
3195c7a0636763c0861d46425709befe90a3919c04Yifan HongSensorInfo convertSensor(const Sensor& src) {
32c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    SensorInfo dst;
33c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    const String8& name = src.getName();
34c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    const String8& vendor = src.getVendor();
35c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.name = hidl_string{name.string(), name.size()};
36c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.vendor = hidl_string{vendor.string(), vendor.size()};
37c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.version = src.getVersion();
38c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.sensorHandle = src.getHandle();
39c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.type = static_cast<::android::hardware::sensors::V1_0::SensorType>(
40c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong            src.getType());
4195c7a0636763c0861d46425709befe90a3919c04Yifan Hong    // maxRange uses maxValue because ::android::Sensor wraps the
42c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    // internal sensor_t in this way.
43c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.maxRange = src.getMaxValue();
44c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.resolution = src.getResolution();
45c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.power = src.getPowerUsage();
46c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.minDelay = src.getMinDelay();
47c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.fifoReservedEventCount = src.getFifoReservedEventCount();
48c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.fifoMaxEventCount = src.getFifoMaxEventCount();
49c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.typeAsString = src.getStringType();
50c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.requiredPermission = src.getRequiredPermission();
51c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.maxDelay = src.getMaxDelay();
52c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    dst.flags = src.getFlags();
53c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    return dst;
54c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong}
55c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong
56c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan HongResult convertResult(status_t status) {
57c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    switch (status) {
58c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong        case OK:
59c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong            return Result::OK;
60c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong        case NAME_NOT_FOUND:
61c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong            return Result::NOT_EXIST;
62c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong        case NO_MEMORY:
63c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong            return Result::NO_MEMORY;
64c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong        case NO_INIT:
65c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong            return Result::NO_INIT;
66ecf108002757b0207d3bff47a73fcac1df58c8ecYifan Hong        case PERMISSION_DENIED:
67ecf108002757b0207d3bff47a73fcac1df58c8ecYifan Hong            return Result::PERMISSION_DENIED;
68c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong        case BAD_VALUE:
69c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong            return Result::BAD_VALUE;
70c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong        case INVALID_OPERATION:
71c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong            return Result::INVALID_OPERATION;
72c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong        default:
73c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong            return Result::UNKNOWN_ERROR;
74c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong    }
75c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong}
76c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong
7795c7a0636763c0861d46425709befe90a3919c04Yifan Hong::android::hardware::sensors::V1_0::Event convertEvent(const ::ASensorEvent& src) {
7895c7a0636763c0861d46425709befe90a3919c04Yifan Hong    ::android::hardware::sensors::V1_0::Event dst;
7995c7a0636763c0861d46425709befe90a3919c04Yifan Hong    ::android::hardware::sensors::V1_0::implementation::convertFromSensorEvent(
8095c7a0636763c0861d46425709befe90a3919c04Yifan Hong            reinterpret_cast<const sensors_event_t&>(src), &dst);
8195c7a0636763c0861d46425709befe90a3919c04Yifan Hong    return dst;
8295c7a0636763c0861d46425709befe90a3919c04Yifan Hong}
8395c7a0636763c0861d46425709befe90a3919c04Yifan Hong
84c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong}  // namespace implementation
85c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong}  // namespace V1_0
86c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong}  // namespace sensorservice
87c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong}  // namespace frameworks
88c34f57fed0ad09c6d7361a39580da20b466d46d1Yifan Hong}  // namespace android
89