| 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 |  | 
| Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 20 | #include "SensorServiceUtils.h" | 
 | 21 | #include <utils/Thread.h> | 
 | 22 | #include <iomanip> | 
 | 23 | #include <sstream> | 
 | 24 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 25 | namespace android { | 
 | 26 |  | 
 | 27 | class SensorService; | 
 | 28 |  | 
| Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 29 | class SensorService::SensorRegistrationInfo : public SensorServiceUtil::Dumpable { | 
 | 30 | public: | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 31 |     SensorRegistrationInfo() : mPackageName() { | 
 | 32 |         mSensorHandle = mSamplingRateUs = mMaxReportLatencyUs = INT32_MIN; | 
| Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 33 |         mHour = mMin = mSec = INT8_MIN; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 34 |         mActivated = false; | 
 | 35 |     } | 
 | 36 |  | 
| Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 37 |     SensorRegistrationInfo(int32_t handle, const String8 &packageName, | 
| Julius D'souza | c35b820 | 2017-12-11 17:33:04 -0800 | [diff] [blame] | 38 |                            int64_t samplingRateNs, int64_t maxReportLatencyNs, bool activate) { | 
| Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 39 |         mSensorHandle = handle; | 
 | 40 |         mPackageName = packageName; | 
 | 41 |  | 
| Julius D'souza | c35b820 | 2017-12-11 17:33:04 -0800 | [diff] [blame] | 42 |         mSamplingRateUs = static_cast<int64_t>(samplingRateNs/1000); | 
 | 43 |         mMaxReportLatencyUs = static_cast<int64_t>(maxReportLatencyNs/1000); | 
| Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 44 |         mActivated = activate; | 
 | 45 |  | 
 | 46 |         IPCThreadState *thread = IPCThreadState::self(); | 
 | 47 |         mPid = (thread != nullptr) ? thread->getCallingPid() : -1; | 
 | 48 |         mUid = (thread != nullptr) ? thread->getCallingUid() : -1; | 
 | 49 |  | 
| Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 50 |         time_t rawtime = time(nullptr); | 
| Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 51 |         struct tm * timeinfo = localtime(&rawtime); | 
 | 52 |         mHour = static_cast<int8_t>(timeinfo->tm_hour); | 
 | 53 |         mMin = static_cast<int8_t>(timeinfo->tm_min); | 
 | 54 |         mSec = static_cast<int8_t>(timeinfo->tm_sec); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 55 |     } | 
| Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 56 |  | 
 | 57 |     static bool isSentinel(const SensorRegistrationInfo& info) { | 
 | 58 |        return (info.mHour == INT8_MIN && | 
 | 59 |                info.mMin == INT8_MIN && | 
 | 60 |                info.mSec == INT8_MIN); | 
 | 61 |     } | 
 | 62 |  | 
 | 63 |     // Dumpable interface | 
 | 64 |     virtual std::string dump() const override { | 
 | 65 |         std::ostringstream ss; | 
 | 66 |         ss << std::setfill('0') << std::setw(2) << static_cast<int>(mHour) << ":" | 
 | 67 |            << std::setw(2) << static_cast<int>(mMin) << ":" | 
 | 68 |            << std::setw(2) << static_cast<int>(mSec) | 
 | 69 |            << (mActivated ? " +" : " -") | 
 | 70 |            << " 0x" << std::hex << std::setw(8) << mSensorHandle << std::dec | 
 | 71 |            << std::setfill(' ') << " pid=" << std::setw(5) << mPid | 
 | 72 |            << " uid=" << std::setw(5) << mUid << " package=" << mPackageName; | 
 | 73 |         if (mActivated) { | 
 | 74 |            ss  << " samplingPeriod=" << mSamplingRateUs << "us" | 
 | 75 |                << " batchingPeriod=" << mMaxReportLatencyUs << "us"; | 
 | 76 |         }; | 
 | 77 |         return ss.str(); | 
 | 78 |     } | 
 | 79 |  | 
 | 80 | private: | 
 | 81 |     int32_t mSensorHandle; | 
 | 82 |     String8 mPackageName; | 
 | 83 |     pid_t   mPid; | 
 | 84 |     uid_t   mUid; | 
| Julius D'souza | c35b820 | 2017-12-11 17:33:04 -0800 | [diff] [blame] | 85 |     int64_t mSamplingRateUs; | 
 | 86 |     int64_t mMaxReportLatencyUs; | 
| Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 87 |     bool mActivated; | 
 | 88 |     int8_t mHour, mMin, mSec; | 
 | 89 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 90 | }; | 
 | 91 |  | 
 | 92 | } // namespace android; | 
 | 93 |  | 
 | 94 | #endif // ANDROID_SENSOR_REGISTRATION_INFO_H | 
 | 95 |  | 
 | 96 |  |