10cc8f809924706c7d683da30605f432635dd5bb6Peng Xu/*
20cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * Copyright (C) 2016 The Android Open Source Project
30cc8f809924706c7d683da30605f432635dd5bb6Peng Xu *
40cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * Licensed under the Apache License, Version 2.0 (the "License");
50cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * you may not use this file except in compliance with the License.
60cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * You may obtain a copy of the License at
70cc8f809924706c7d683da30605f432635dd5bb6Peng Xu *
80cc8f809924706c7d683da30605f432635dd5bb6Peng Xu *      http://www.apache.org/licenses/LICENSE-2.0
90cc8f809924706c7d683da30605f432635dd5bb6Peng Xu *
100cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * Unless required by applicable law or agreed to in writing, software
110cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * distributed under the License is distributed on an "AS IS" BASIS,
120cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * See the License for the specific language governing permissions and
140cc8f809924706c7d683da30605f432635dd5bb6Peng Xu * limitations under the License.
150cc8f809924706c7d683da30605f432635dd5bb6Peng Xu */
160cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
170cc8f809924706c7d683da30605f432635dd5bb6Peng Xu#include "SensorList.h"
180cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
190cc8f809924706c7d683da30605f432635dd5bb6Peng Xu#include <hardware/sensors.h>
200cc8f809924706c7d683da30605f432635dd5bb6Peng Xu#include <utils/String8.h>
210cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
226a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu#include <cinttypes>
236a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu
240cc8f809924706c7d683da30605f432635dd5bb6Peng Xunamespace android {
250cc8f809924706c7d683da30605f432635dd5bb6Peng Xunamespace SensorServiceUtil {
260cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
270cc8f809924706c7d683da30605f432635dd5bb6Peng Xuconst Sensor SensorList::mNonSensor = Sensor("unknown");
280cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
290cc8f809924706c7d683da30605f432635dd5bb6Peng Xubool SensorList::add(
300cc8f809924706c7d683da30605f432635dd5bb6Peng Xu        int handle, SensorInterface* si, bool isForDebug, bool isVirtual) {
310cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    std::lock_guard<std::mutex> lk(mLock);
320cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    if (handle == si->getSensor().getHandle() &&
330cc8f809924706c7d683da30605f432635dd5bb6Peng Xu        mUsedHandle.insert(handle).second) {
340cc8f809924706c7d683da30605f432635dd5bb6Peng Xu        // will succeed as the mUsedHandle does not have this handle
350cc8f809924706c7d683da30605f432635dd5bb6Peng Xu        mHandleMap.emplace(handle, Entry(si, isForDebug, isVirtual));
360cc8f809924706c7d683da30605f432635dd5bb6Peng Xu        return true;
370cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    }
380cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    // handle exist already or handle mismatch
390cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    return false;
400cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}
410cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
420cc8f809924706c7d683da30605f432635dd5bb6Peng Xubool SensorList::remove(int handle) {
430cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    std::lock_guard<std::mutex> lk(mLock);
440cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    auto entry = mHandleMap.find(handle);
450cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    if (entry != mHandleMap.end()) {
460cc8f809924706c7d683da30605f432635dd5bb6Peng Xu        mHandleMap.erase(entry);
470cc8f809924706c7d683da30605f432635dd5bb6Peng Xu        return true;
480cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    }
490cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    return false;
500cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}
510cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
520cc8f809924706c7d683da30605f432635dd5bb6Peng XuString8 SensorList::getName(int handle) const {
530cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    return getOne<String8>(
540cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            handle, [] (const Entry& e) -> String8 {return e.si->getSensor().getName();},
550cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            mNonSensor.getName());
560cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}
570cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
58755c451c7861a029e26e5f16e319b629169e656dPeng Xusp<SensorInterface> SensorList::getInterface(int handle) const {
59755c451c7861a029e26e5f16e319b629169e656dPeng Xu    return getOne<sp<SensorInterface>>(
60755c451c7861a029e26e5f16e319b629169e656dPeng Xu            handle, [] (const Entry& e) -> sp<SensorInterface> {return e.si;}, nullptr);
610cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}
620cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
630cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
640cc8f809924706c7d683da30605f432635dd5bb6Peng Xubool SensorList::isNewHandle(int handle) const {
650cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    std::lock_guard<std::mutex> lk(mLock);
660cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    return mUsedHandle.find(handle) == mUsedHandle.end();
670cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}
680cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
690cc8f809924706c7d683da30605f432635dd5bb6Peng Xuconst Vector<Sensor> SensorList::getUserSensors() const {
700cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    // lock in forEachEntry
710cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    Vector<Sensor> sensors;
720cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    forEachEntry(
730cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            [&sensors] (const Entry& e) -> bool {
740cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                if (!e.isForDebug && !e.si->getSensor().isDynamicSensor()) {
750cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                    sensors.add(e.si->getSensor());
760cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                }
770cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                return true;
780cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            });
790cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    return sensors;
800cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}
810cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
820cc8f809924706c7d683da30605f432635dd5bb6Peng Xuconst Vector<Sensor> SensorList::getUserDebugSensors() const {
830cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    // lock in forEachEntry
840cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    Vector<Sensor> sensors;
850cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    forEachEntry(
860cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            [&sensors] (const Entry& e) -> bool {
870cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                if (!e.si->getSensor().isDynamicSensor()) {
880cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                    sensors.add(e.si->getSensor());
890cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                }
900cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                return true;
910cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            });
920cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    return sensors;
930cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}
940cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
950cc8f809924706c7d683da30605f432635dd5bb6Peng Xuconst Vector<Sensor> SensorList::getDynamicSensors() const {
960cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    // lock in forEachEntry
970cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    Vector<Sensor> sensors;
980cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    forEachEntry(
990cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            [&sensors] (const Entry& e) -> bool {
1000cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                if (!e.isForDebug && e.si->getSensor().isDynamicSensor()) {
1010cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                    sensors.add(e.si->getSensor());
1020cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                }
1030cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                return true;
1040cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            });
1050cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    return sensors;
1060cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}
1070cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
1080cc8f809924706c7d683da30605f432635dd5bb6Peng Xuconst Vector<Sensor> SensorList::getVirtualSensors() const {
1090cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    // lock in forEachEntry
1100cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    Vector<Sensor> sensors;
1110cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    forEachEntry(
1120cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            [&sensors] (const Entry& e) -> bool {
1130cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                if (e.isVirtual) {
1140cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                    sensors.add(e.si->getSensor());
1150cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                }
1160cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                return true;
1170cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            });
1180cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    return sensors;
1190cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}
1200cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
1210cc8f809924706c7d683da30605f432635dd5bb6Peng Xustd::string SensorList::dump() const {
1220cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    String8 result;
1230cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
1240cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    forEachSensor([&result] (const Sensor& s) -> bool {
1250cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            result.appendFormat(
1266a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu                    "%#010x) %-25s | %-15s | ver: %" PRId32 " | type: %20s(%" PRId32
127e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                        ") | perm: %s\n",
1286a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu                    s.getHandle(),
1290cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                    s.getName().string(),
1300cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                    s.getVendor().string(),
1310cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                    s.getVersion(),
1320cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                    s.getStringType().string(),
1336a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu                    s.getType(),
1346a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu                    s.getRequiredPermission().size() ? s.getRequiredPermission().string() : "n/a");
1350cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
136e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu            result.append("\t");
1370cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            const int reportingMode = s.getReportingMode();
1380cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            if (reportingMode == AREPORTING_MODE_CONTINUOUS) {
139e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                result.append("continuous | ");
1400cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            } else if (reportingMode == AREPORTING_MODE_ON_CHANGE) {
141e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                result.append("on-change | ");
1420cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            } else if (reportingMode == AREPORTING_MODE_ONE_SHOT) {
143e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                result.append("one-shot | ");
1440cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            } else if (reportingMode == AREPORTING_MODE_SPECIAL_TRIGGER) {
145e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                result.append("special-trigger | ");
1460cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            } else {
147e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                result.append("unknown-mode | ");
1480cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            }
1490cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
1500cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            if (s.getMaxDelay() > 0) {
1510cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                result.appendFormat("minRate=%.2fHz | ", 1e6f / s.getMaxDelay());
1520cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            } else {
1536a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu                result.appendFormat("maxDelay=%" PRId32 "us | ", s.getMaxDelay());
1540cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            }
1550cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
1560cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            if (s.getMinDelay() > 0) {
1570cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                result.appendFormat("maxRate=%.2fHz | ", 1e6f / s.getMinDelay());
1580cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            } else {
1596a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu                result.appendFormat("minDelay=%" PRId32 "us | ", s.getMinDelay());
1600cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            }
1610cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
1620cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            if (s.getFifoMaxEventCount() > 0) {
1636a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu                result.appendFormat("FIFO (max,reserved) = (%" PRIu32 ", %" PRIu32 ") events | ",
1646a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu                        s.getFifoMaxEventCount(),
1656a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu                        s.getFifoReservedEventCount());
1660cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            } else {
1670cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                result.append("no batching | ");
1680cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            }
1690cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
1700cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            if (s.isWakeUpSensor()) {
1710cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                result.appendFormat("wakeUp | ");
1720cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            } else {
1730cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                result.appendFormat("non-wakeUp | ");
1740cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            }
1750cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
1766a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu            if (s.isDynamicSensor()) {
1776a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu                result.appendFormat("dynamic, ");
1786a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu            }
1796a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu            if (s.hasAdditionalInfo()) {
1806a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu                result.appendFormat("has-additional-info, ");
1816a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu            }
1820cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            result.append("\n");
183e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu
184e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu            if (s.getHighestDirectReportRateLevel() > SENSOR_DIRECT_RATE_STOP) {
185e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                result.appendFormat("\thighest rate level = %d, support shared mem: ",
186e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                        s.getHighestDirectReportRateLevel());
187e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                if (s.isDirectChannelTypeSupported(SENSOR_DIRECT_MEM_TYPE_ASHMEM)) {
188e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                    result.append("ashmem, ");
189e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                }
190e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                if (s.isDirectChannelTypeSupported(SENSOR_DIRECT_MEM_TYPE_GRALLOC)) {
191e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                    result.append("gralloc, ");
192e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                }
193d9c8a864e193d7ae3d0a04c1aa7ff15da955fb0fPeng Xu                result.appendFormat("flag =0x%08x", static_cast<int>(s.getFlags()));
194e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu                result.append("\n");
195e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu            }
1960cc8f809924706c7d683da30605f432635dd5bb6Peng Xu            return true;
1970cc8f809924706c7d683da30605f432635dd5bb6Peng Xu        });
1980cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    return std::string(result.string());
1990cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}
2000cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
2010cc8f809924706c7d683da30605f432635dd5bb6Peng XuSensorList::~SensorList() {
2020cc8f809924706c7d683da30605f432635dd5bb6Peng Xu}
2030cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
2040cc8f809924706c7d683da30605f432635dd5bb6Peng Xu} // namespace SensorServiceUtil
2050cc8f809924706c7d683da30605f432635dd5bb6Peng Xu} // namespace android
2060cc8f809924706c7d683da30605f432635dd5bb6Peng Xu
207