diff options
author | Haamed Gheibi <haamed@google.com> | 2022-02-17 15:24:46 -0800 |
---|---|---|
committer | Haamed Gheibi <haamed@google.com> | 2022-02-22 09:46:46 -0800 |
commit | 75f1bda1bc422e3882dbbeed4be03b24f863b2ba (patch) | |
tree | ab7c399debcc86a00607387d37dc1cdf48abc500 /modules/sensors/dynamic_sensor/HidRawSensor.cpp | |
parent | 202706ad45c6b0c0efff99c9f1de34003e80927f (diff) | |
parent | c142f2613ef9982d59dca254d7a625bdf595f70c (diff) |
Merge SP2A.220305.013
Bug: 220074017
Change-Id: Ia2198b401a3a907c785316e0ef4c00bf27fa59b1
Diffstat (limited to 'modules/sensors/dynamic_sensor/HidRawSensor.cpp')
-rw-r--r-- | modules/sensors/dynamic_sensor/HidRawSensor.cpp | 186 |
1 files changed, 126 insertions, 60 deletions
diff --git a/modules/sensors/dynamic_sensor/HidRawSensor.cpp b/modules/sensors/dynamic_sensor/HidRawSensor.cpp index f3c8a27b..8aaf2d4f 100644 --- a/modules/sensors/dynamic_sensor/HidRawSensor.cpp +++ b/modules/sensors/dynamic_sensor/HidRawSensor.cpp @@ -19,6 +19,8 @@ #include <utils/Errors.h> #include "HidLog.h" +#include <HidUtils.h> + #include <algorithm> #include <cfloat> #include <codecvt> @@ -490,13 +492,8 @@ bool HidRawSensor::populateFeatureValueFromFeatureReport( } break; case SENSOR_DESCRIPTION: - if (!r.isByteAligned() || r.bitSize != 16 || r.count < 1 - || (r.bitOffset / 8 + r.count * 2) > buffer.size() ) { - // invalid description - break; - } if (decodeString(r, buffer, &str)) { - mFeatureInfo.isAndroidCustom = detectAndroidCustomSensor(str); + detectSensorFromDescription(str); } break; default: @@ -581,26 +578,34 @@ bool HidRawSensor::validateFeatureValueAndBuildSensor() { bool HidRawSensor::decodeString( const HidParser::ReportItem &report, const std::vector<uint8_t> &buffer, std::string *d) { - if (!report.isByteAligned() || report.bitSize != 16 || report.count < 1) { + if (!report.isByteAligned() || + (report.bitSize != 8 && report.bitSize != 16) || report.count < 1) { return false; } + size_t charSize = report.bitSize / 8; size_t offset = report.bitOffset / 8; - if (offset + report.count * 2 > buffer.size()) { + if (offset + report.count * charSize > buffer.size()) { return false; } - std::vector<uint16_t> data(report.count); - auto i = data.begin(); - auto j = buffer.begin() + offset; - for ( ; i != data.end(); ++i, j += sizeof(uint16_t)) { - // hid specified little endian - *i = *j + (*(j + 1) << 8); + if (charSize == 1) { + *d = std::string(buffer.begin() + offset, + buffer.begin() + offset + report.count); + } else { + std::vector<uint16_t> data(report.count); + auto i = data.begin(); + auto j = buffer.begin() + offset; + for ( ; i != data.end(); ++i, j += sizeof(uint16_t)) { + // hid specified little endian + *i = *j + (*(j + 1) << 8); + } + std::wstring wstr(data.begin(), data.end()); + + std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; + *d = converter.to_bytes(wstr); } - std::wstring wstr(data.begin(), data.end()); - std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; - *d = converter.to_bytes(wstr); return true; } @@ -619,6 +624,28 @@ std::vector<std::string> split(const std::string &text, char sep) { return tokens; } +void HidRawSensor::detectSensorFromDescription(const std::string &description) { + if (detectAndroidHeadTrackerSensor(description) || + detectAndroidCustomSensor(description)) { + mFeatureInfo.isAndroidCustom = true; + } +} + +bool HidRawSensor::detectAndroidHeadTrackerSensor( + const std::string &description) { + if (description.find("#AndroidHeadTracker#1.") != 0) { + return false; + } + + mFeatureInfo.type = SENSOR_TYPE_DEVICE_PRIVATE_BASE; + mFeatureInfo.typeString = CUSTOM_TYPE_PREFIX + "headtracker"; + mFeatureInfo.reportModeFlag = SENSOR_FLAG_CONTINUOUS_MODE; + mFeatureInfo.permission = ""; + mFeatureInfo.isWakeUp = false; + + return true; +} + bool HidRawSensor::detectAndroidCustomSensor(const std::string &description) { size_t nullPosition = description.find('\0'); if (nullPosition == std::string::npos) { @@ -784,50 +811,83 @@ bool HidRawSensor::detectAndroidCustomSensor(const std::string &description) { } bool HidRawSensor::findSensorControlUsage(const std::vector<HidParser::ReportPacket> &packets) { + using namespace Hid::Sensor::PowerStateUsage; using namespace Hid::Sensor::PropertyUsage; - using namespace Hid::Sensor::RawMinMax; + using namespace Hid::Sensor::ReportingStateUsage; //REPORTING_STATE const HidParser::ReportItem *reportingState = find(packets, REPORTING_STATE, HidParser::REPORT_TYPE_FEATURE); - if (reportingState == nullptr - || !reportingState->isByteAligned() - || reportingState->bitSize != 8 - || reportingState->minRaw != REPORTING_STATE_MIN - || reportingState->maxRaw != REPORTING_STATE_MAX) { + if (reportingState == nullptr) { LOG_W << "Cannot find valid reporting state feature" << LOG_ENDL; } else { mReportingStateId = reportingState->id; - mReportingStateOffset = reportingState->bitOffset / 8; + mReportingStateBitOffset = reportingState->bitOffset; + mReportingStateBitSize = reportingState->bitSize; + + mReportingStateDisableIndex = -1; + mReportingStateEnableIndex = -1; + for (unsigned i = 0; i < reportingState->usageVector.size(); ++i) { + if (reportingState->usageVector[i] == REPORTING_STATE_NO_EVENTS) { + mReportingStateDisableIndex = i; + } + if (reportingState->usageVector[i] == REPORTING_STATE_ALL_EVENTS) { + mReportingStateEnableIndex = i; + } + } + if (mReportingStateDisableIndex < 0) { + LOG_W << "Cannot find reporting state to disable sensor" + << LOG_ENDL; + mReportingStateId = -1; + } + if (mReportingStateEnableIndex < 0) { + LOG_W << "Cannot find reporting state to enable sensor" << LOG_ENDL; + mReportingStateId = -1; + } } //POWER_STATE const HidParser::ReportItem *powerState = find(packets, POWER_STATE, HidParser::REPORT_TYPE_FEATURE); - if (powerState == nullptr - || !powerState->isByteAligned() - || powerState->bitSize != 8 - || powerState->minRaw != POWER_STATE_MIN - || powerState->maxRaw != POWER_STATE_MAX) { + if (powerState == nullptr) { LOG_W << "Cannot find valid power state feature" << LOG_ENDL; } else { mPowerStateId = powerState->id; - mPowerStateOffset = powerState->bitOffset / 8; + mPowerStateBitOffset = powerState->bitOffset; + mPowerStateBitSize = powerState->bitSize; + + mPowerStateOffIndex = -1; + mPowerStateOnIndex = -1; + for (unsigned i = 0; i < powerState->usageVector.size(); ++i) { + if (powerState->usageVector[i] == POWER_STATE_D4_POWER_OFF) { + mPowerStateOffIndex = i; + } + if (powerState->usageVector[i] == POWER_STATE_D0_FULL_POWER) { + mPowerStateOnIndex = i; + } + } + if (mPowerStateOffIndex < 0) { + LOG_W << "Cannot find power state to power off sensor" + << LOG_ENDL; + mPowerStateId = -1; + } + if (mPowerStateOnIndex < 0) { + LOG_W << "Cannot find power state to power on sensor" << LOG_ENDL; + mPowerStateId = -1; + } } //REPORT_INTERVAL const HidParser::ReportItem *reportInterval = find(packets, REPORT_INTERVAL, HidParser::REPORT_TYPE_FEATURE); if (reportInterval == nullptr - || !reportInterval->isByteAligned() - || reportInterval->minRaw < 0 - || (reportInterval->bitSize != 16 && reportInterval->bitSize != 32)) { + || reportInterval->minRaw < 0) { LOG_W << "Cannot find valid report interval feature" << LOG_ENDL; } else { mReportIntervalId = reportInterval->id; - mReportIntervalOffset = reportInterval->bitOffset / 8; - mReportIntervalSize = reportInterval->bitSize / 8; + mReportIntervalBitOffset = reportInterval->bitOffset; + mReportIntervalBitSize = reportInterval->bitSize; mFeatureInfo.minDelay = std::max(static_cast<int64_t>(1), reportInterval->minRaw) * 1000; mFeatureInfo.maxDelay = std::min(static_cast<int64_t>(1000000), @@ -846,7 +906,6 @@ void HidRawSensor::getUuid(uint8_t* uuid) const { } int HidRawSensor::enable(bool enable) { - using namespace Hid::Sensor::StateValue; SP(HidDevice) device = PROMOTE(mDevice); if (device == nullptr) { @@ -863,8 +922,11 @@ int HidRawSensor::enable(bool enable) { setPowerOk = false; uint8_t id = static_cast<uint8_t>(mPowerStateId); if (device->getFeature(id, &buffer) - && buffer.size() > mPowerStateOffset) { - buffer[mPowerStateOffset] = enable ? POWER_STATE_FULL_POWER : POWER_STATE_POWER_OFF; + && (8 * buffer.size()) >= + (mPowerStateBitOffset + mPowerStateBitSize)) { + uint8_t index = enable ? mPowerStateOnIndex : mPowerStateOffIndex; + HidUtil::copyBits(&index, &(buffer[0]), buffer.size(), + 0, mPowerStateBitOffset, mPowerStateBitSize); setPowerOk = device->setFeature(id, buffer); } else { LOG_E << "enable: changing POWER STATE failed" << LOG_ENDL; @@ -876,9 +938,12 @@ int HidRawSensor::enable(bool enable) { setReportingOk = false; uint8_t id = static_cast<uint8_t>(mReportingStateId); if (device->getFeature(id, &buffer) - && buffer.size() > mReportingStateOffset) { - buffer[mReportingStateOffset] - = enable ? REPORTING_STATE_ALL_EVENT : REPORTING_STATE_NO_EVENT; + && (8 * buffer.size()) > + (mReportingStateBitOffset + mReportingStateBitSize)) { + uint8_t index = enable ? mReportingStateEnableIndex : + mReportingStateDisableIndex; + HidUtil::copyBits(&index, &(buffer[0]), buffer.size(),0, + mReportingStateBitOffset, mReportingStateBitSize); setReportingOk = device->setFeature(id, buffer); } else { LOG_E << "enable: changing REPORTING STATE failed" << LOG_ENDL; @@ -911,22 +976,15 @@ int HidRawSensor::batch(int64_t samplingPeriod, int64_t batchingPeriod) { ok = false; uint8_t id = static_cast<uint8_t>(mReportIntervalId); if (device->getFeature(id, &buffer) - && buffer.size() >= mReportIntervalOffset + mReportIntervalSize) { + && (8 * buffer.size()) >= + (mReportIntervalBitOffset + mReportIntervalBitSize)) { int64_t periodMs = samplingPeriod / 1000000; //ns -> ms - switch (mReportIntervalSize) { - case sizeof(uint16_t): - periodMs = std::min(periodMs, static_cast<int64_t>(UINT16_MAX)); - buffer[mReportIntervalOffset] = periodMs & 0xFF; - buffer[mReportIntervalOffset + 1] = (periodMs >> 8) & 0xFF; - break; - case sizeof(uint32_t): - periodMs = std::min(periodMs, static_cast<int64_t>(UINT32_MAX)); - buffer[mReportIntervalOffset] = periodMs & 0xFF; - buffer[mReportIntervalOffset + 1] = (periodMs >> 8) & 0xFF; - buffer[mReportIntervalOffset + 2] = (periodMs >> 16) & 0xFF; - buffer[mReportIntervalOffset + 3] = (periodMs >> 24) & 0xFF; - break; - } + int64_t maxPeriodMs = + (1LL << std::min(mReportIntervalBitSize, 63U)) - 1; + periodMs = std::min(periodMs, maxPeriodMs); + HidUtil::copyBits(&periodMs, &(buffer[0]), buffer.size(), + 0, mReportIntervalBitOffset, + mReportIntervalBitSize); ok = device->setFeature(id, buffer); } } @@ -1019,7 +1077,11 @@ std::string HidRawSensor::dump() const { ss << " Power state "; if (mPowerStateId >= 0) { ss << "found, id: " << mPowerStateId - << " offset: " << mPowerStateOffset << LOG_ENDL; + << " bit offset: " << mPowerStateBitOffset + << " bit size: " << mPowerStateBitSize + << " power off index: " << mPowerStateOffIndex + << " power on index: " << mPowerStateOnIndex + << LOG_ENDL; } else { ss << "not found" << LOG_ENDL; } @@ -1027,7 +1089,11 @@ std::string HidRawSensor::dump() const { ss << " Reporting state "; if (mReportingStateId >= 0) { ss << "found, id: " << mReportingStateId - << " offset: " << mReportingStateOffset << LOG_ENDL; + << " bit offset: " << mReportingStateBitOffset + << " bit size: " << mReportingStateBitSize + << " disable index: " << mReportingStateDisableIndex + << " enable index: " << mReportingStateEnableIndex + << LOG_ENDL; } else { ss << "not found" << LOG_ENDL; } @@ -1035,8 +1101,8 @@ std::string HidRawSensor::dump() const { ss << " Report interval "; if (mReportIntervalId >= 0) { ss << "found, id: " << mReportIntervalId - << " offset: " << mReportIntervalOffset - << " size: " << mReportIntervalSize << LOG_ENDL; + << " bit offset: " << mReportIntervalBitOffset + << " bit size: " << mReportIntervalBitSize << LOG_ENDL; } else { ss << "not found" << LOG_ENDL; } |