Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | #ifndef ANDROID_SENSOR_REGISTRATION_INFO_H |
| 18 | #define ANDROID_SENSOR_REGISTRATION_INFO_H |
| 19 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 20 | #include <ctime> |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 21 | #include <iomanip> |
| 22 | #include <sstream> |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 23 | #include <utils/Thread.h> |
| 24 | |
| 25 | #include <android/util/ProtoOutputStream.h> |
| 26 | #include <frameworks/base/core/proto/android/service/sensor_service.proto.h> |
| 27 | #include "SensorServiceUtils.h" |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 28 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 29 | namespace android { |
| 30 | |
| 31 | class SensorService; |
| 32 | |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 33 | class SensorService::SensorRegistrationInfo : public SensorServiceUtil::Dumpable { |
| 34 | public: |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 35 | SensorRegistrationInfo() : mPackageName() { |
| 36 | mSensorHandle = mSamplingRateUs = mMaxReportLatencyUs = INT32_MIN; |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 37 | mRealtimeSec = 0; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 38 | mActivated = false; |
| 39 | } |
| 40 | |
Rocky Fang | e7fd954 | 2024-08-06 00:19:01 +0000 | [diff] [blame] | 41 | SensorRegistrationInfo(int32_t handle, const String8& packageName, int64_t samplingRateNs, |
| 42 | int64_t maxReportLatencyNs, bool activate, status_t registerStatus) { |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 43 | mSensorHandle = handle; |
| 44 | mPackageName = packageName; |
Rocky Fang | e7fd954 | 2024-08-06 00:19:01 +0000 | [diff] [blame] | 45 | mRegisteredStatus = registerStatus; |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 46 | |
Julius D'souza | c35b820 | 2017-12-11 17:33:04 -0800 | [diff] [blame] | 47 | mSamplingRateUs = static_cast<int64_t>(samplingRateNs/1000); |
| 48 | mMaxReportLatencyUs = static_cast<int64_t>(maxReportLatencyNs/1000); |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 49 | mActivated = activate; |
| 50 | |
| 51 | IPCThreadState *thread = IPCThreadState::self(); |
| 52 | mPid = (thread != nullptr) ? thread->getCallingPid() : -1; |
| 53 | mUid = (thread != nullptr) ? thread->getCallingUid() : -1; |
| 54 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 55 | timespec curTime; |
| 56 | clock_gettime(CLOCK_REALTIME_COARSE, &curTime); |
| 57 | mRealtimeSec = curTime.tv_sec; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 58 | } |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 59 | |
| 60 | static bool isSentinel(const SensorRegistrationInfo& info) { |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 61 | return (info.mSensorHandle == INT32_MIN && info.mRealtimeSec == 0); |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Rocky Fang | e7fd954 | 2024-08-06 00:19:01 +0000 | [diff] [blame] | 64 | std::string dump(SensorService* sensorService) const { |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 65 | struct tm* timeinfo = localtime(&mRealtimeSec); |
| 66 | const int8_t hour = static_cast<int8_t>(timeinfo->tm_hour); |
| 67 | const int8_t min = static_cast<int8_t>(timeinfo->tm_min); |
| 68 | const int8_t sec = static_cast<int8_t>(timeinfo->tm_sec); |
| 69 | |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 70 | std::ostringstream ss; |
Rocky Fang | e7fd954 | 2024-08-06 00:19:01 +0000 | [diff] [blame] | 71 | ss << std::setfill('0') << std::setw(2) << static_cast<int>(hour) << ":" << std::setw(2) |
| 72 | << static_cast<int>(min) << ":" << std::setw(2) << static_cast<int>(sec) |
| 73 | << (mActivated ? " +" : " -") << " 0x" << std::hex << std::setw(8) << mSensorHandle; |
| 74 | ss << std::dec << std::setfill(' ') << " pid=" << std::setw(5) << mPid |
| 75 | << " uid=" << std::setw(5) << mUid; |
| 76 | |
| 77 | std::string samplingRate = |
| 78 | mActivated ? std::to_string(mSamplingRateUs) : " N/A "; |
| 79 | std::string batchingInterval = |
| 80 | mActivated ? std::to_string(mMaxReportLatencyUs) : " N/A "; |
| 81 | ss << " samplingPeriod=" << std::setfill(' ') << std::setw(8) |
| 82 | << samplingRate << "us" << " batchingPeriod=" << std::setfill(' ') |
| 83 | << std::setw(8) << batchingInterval << "us" |
| 84 | << " result=" << statusToString(mRegisteredStatus) |
| 85 | << " (sensor, package): (" << std::setfill(' ') << std::left |
| 86 | << std::setw(27); |
| 87 | |
| 88 | if (sensorService != nullptr) { |
| 89 | ss << sensorService->getSensorName(mSensorHandle); |
| 90 | } else { |
| 91 | ss << "null"; |
| 92 | } |
| 93 | ss << ", " << mPackageName << ")"; |
| 94 | |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 95 | return ss.str(); |
| 96 | } |
| 97 | |
Rocky Fang | e7fd954 | 2024-08-06 00:19:01 +0000 | [diff] [blame] | 98 | // Dumpable interface |
| 99 | virtual std::string dump() const override { return dump(static_cast<SensorService*>(nullptr)); } |
| 100 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 101 | /** |
| 102 | * Dump debugging information as android.service.SensorRegistrationInfoProto protobuf message |
| 103 | * using ProtoOutputStream. |
| 104 | * |
| 105 | * See proto definition and some notes about ProtoOutputStream in |
| 106 | * frameworks/base/core/proto/android/service/sensor_service.proto |
| 107 | */ |
| 108 | virtual void dump(util::ProtoOutputStream* proto) const override { |
| 109 | using namespace service::SensorRegistrationInfoProto; |
| 110 | proto->write(TIMESTAMP_SEC, int64_t(mRealtimeSec)); |
| 111 | proto->write(SENSOR_HANDLE, mSensorHandle); |
Tomasz Wasilczyk | 83aa0ab | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 112 | proto->write(PACKAGE_NAME, std::string(mPackageName.c_str())); |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 113 | proto->write(PID, int32_t(mPid)); |
| 114 | proto->write(UID, int32_t(mUid)); |
| 115 | proto->write(SAMPLING_RATE_US, mSamplingRateUs); |
| 116 | proto->write(MAX_REPORT_LATENCY_US, mMaxReportLatencyUs); |
| 117 | proto->write(ACTIVATED, mActivated); |
| 118 | } |
| 119 | |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 120 | private: |
| 121 | int32_t mSensorHandle; |
| 122 | String8 mPackageName; |
| 123 | pid_t mPid; |
| 124 | uid_t mUid; |
Julius D'souza | c35b820 | 2017-12-11 17:33:04 -0800 | [diff] [blame] | 125 | int64_t mSamplingRateUs; |
| 126 | int64_t mMaxReportLatencyUs; |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 127 | bool mActivated; |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 128 | time_t mRealtimeSec; |
Rocky Fang | e7fd954 | 2024-08-06 00:19:01 +0000 | [diff] [blame] | 129 | status_t mRegisteredStatus; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | } // namespace android; |
| 133 | |
| 134 | #endif // ANDROID_SENSOR_REGISTRATION_INFO_H |
| 135 | |
| 136 | |