SensorManager.cpp revision 542eace6bf9aa9cc4a2fff4886ff023b973232e2
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// LOG_TAG defined via build flag.
18#ifndef LOG_TAG
19#define LOG_TAG "HidlSensorManager"
20#endif
21#include <android-base/logging.h>
22
23#include "SensorManager.h"
24
25#include <sched.h>
26
27#include <thread>
28
29#include "EventQueue.h"
30#include "DirectReportChannel.h"
31#include "utils.h"
32
33namespace android {
34namespace frameworks {
35namespace sensorservice {
36namespace V1_0 {
37namespace implementation {
38
39using ::android::hardware::sensors::V1_0::SensorInfo;
40using ::android::hardware::sensors::V1_0::SensorsEventFormatOffset;
41using ::android::hardware::hidl_vec;
42using ::android::hardware::Void;
43using ::android::sp;
44
45SensorManager::SensorManager() {
46}
47
48SensorManager::~SensorManager() {
49    // Stops pollAll inside the thread.
50    std::unique_lock<std::mutex> lock(mLooperMutex);
51    if (mLooper != nullptr) {
52        mLooper->wake();
53    }
54}
55
56// Methods from ::android::frameworks::sensorservice::V1_0::ISensorManager follow.
57Return<void> SensorManager::getSensorList(getSensorList_cb _hidl_cb) {
58    ::android::Sensor const* const* list;
59    ssize_t count = getInternalManager().getSensorList(&list);
60    if (count < 0 || !list) {
61        LOG(ERROR) << "::android::SensorManager::getSensorList encounters " << count;
62        _hidl_cb({}, Result::UNKNOWN_ERROR);
63        return Void();
64    }
65    hidl_vec<SensorInfo> ret;
66    ret.resize(static_cast<size_t>(count));
67    for (ssize_t i = 0; i < count; ++i) {
68        ret[i] = convertSensor(*list[i]);
69    }
70    _hidl_cb(ret, Result::OK);
71    return Void();
72}
73
74Return<void> SensorManager::getDefaultSensor(SensorType type, getDefaultSensor_cb _hidl_cb) {
75    ::android::Sensor const* sensor = getInternalManager().getDefaultSensor(static_cast<int>(type));
76    if (!sensor) {
77        _hidl_cb({}, Result::NOT_EXIST);
78        return Void();
79    }
80    _hidl_cb(convertSensor(*sensor), Result::OK);
81    return Void();
82}
83
84template<typename Callback>
85void createDirectChannel(::android::SensorManager& manager, size_t size, int type,
86        const native_handle_t* handle, const Callback& _hidl_cb) {
87
88    int channelId = manager.createDirectChannel(
89        size, type, handle);
90    if (channelId < 0) {
91        _hidl_cb(nullptr, convertResult(channelId));
92        return;
93    }
94    if (channelId == 0) {
95        _hidl_cb(nullptr, Result::UNKNOWN_ERROR);
96        return;
97    }
98
99    _hidl_cb(sp<IDirectReportChannel>(new DirectReportChannel(manager, channelId)),
100            Result::OK);
101}
102
103Return<void> SensorManager::createAshmemDirectChannel(
104        const hidl_memory& mem, uint64_t size,
105        createAshmemDirectChannel_cb _hidl_cb) {
106    if (size > mem.size() || size < (uint64_t)SensorsEventFormatOffset::TOTAL_LENGTH) {
107        _hidl_cb(nullptr, Result::BAD_VALUE);
108        return Void();
109    }
110
111    createDirectChannel(getInternalManager(), size, SENSOR_DIRECT_MEM_TYPE_ASHMEM,
112            mem.handle(), _hidl_cb);
113
114    return Void();
115}
116
117Return<void> SensorManager::createGrallocDirectChannel(
118        const hidl_handle& buffer, uint64_t size,
119        createGrallocDirectChannel_cb _hidl_cb) {
120
121    createDirectChannel(getInternalManager(), size, SENSOR_DIRECT_MEM_TYPE_GRALLOC,
122            buffer.getNativeHandle(), _hidl_cb);
123
124    return Void();
125}
126
127/* One global looper for all event queues created from this SensorManager. */
128sp<::android::Looper> SensorManager::getLooper() {
129    std::unique_lock<std::mutex> lock(mLooperMutex);
130    if (mLooper == nullptr) {
131        std::condition_variable looperSet;
132
133        std::thread{[&mutex = mLooperMutex, &looper = mLooper, &looperSet] {
134
135            struct sched_param p = {0};
136            p.sched_priority = 10;
137            if (sched_setscheduler(0 /* current thread*/, SCHED_FIFO, &p) != 0) {
138                LOG(WARNING) << "Could not use SCHED_FIFO for looper thread: "
139                        << strerror(errno);
140            }
141
142            std::unique_lock<std::mutex> lock(mutex);
143            looper = Looper::prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS /* opts */);
144            lock.unlock();
145
146            looperSet.notify_one();
147            int pollResult = looper->pollAll(-1 /* timeout */);
148            if (pollResult != ALOOPER_POLL_WAKE) {
149                LOG(ERROR) << "Looper::pollAll returns unexpected " << pollResult;
150            }
151            LOG(INFO) << "Looper thread is terminated.";
152        }}.detach();
153        looperSet.wait(lock, [this]{ return this->mLooper != nullptr; });
154    }
155    return mLooper;
156}
157
158::android::SensorManager& SensorManager::getInternalManager() {
159    std::lock_guard<std::mutex> lock(mInternalManagerMutex);
160    if (mInternalManager == nullptr) {
161        mInternalManager = &::android::SensorManager::getInstanceForPackage(
162                String16(ISensorManager::descriptor));
163    }
164    return *mInternalManager;
165}
166
167Return<void> SensorManager::createEventQueue(
168        const sp<IEventQueueCallback> &callback, createEventQueue_cb _hidl_cb) {
169    if (callback == nullptr) {
170        _hidl_cb(nullptr, Result::BAD_VALUE);
171        return Void();
172    }
173
174    sp<::android::Looper> looper = getLooper();
175    sp<::android::SensorEventQueue> internalQueue = getInternalManager().createEventQueue();
176    if (internalQueue == nullptr) {
177        LOG(WARNING) << "::android::SensorManager::createEventQueue returns nullptr.";
178        _hidl_cb(nullptr, Result::UNKNOWN_ERROR);
179        return Void();
180    }
181
182    sp<IEventQueue> queue = new EventQueue(callback, looper, internalQueue);
183    _hidl_cb(queue, Result::OK);
184
185    return Void();
186}
187
188}  // namespace implementation
189}  // namespace V1_0
190}  // namespace sensorservice
191}  // namespace frameworks
192}  // namespace android
193