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 
20 #include "SensorServiceUtils.h"
21 #include <utils/Thread.h>
22 #include <iomanip>
23 #include <sstream>
24 
25 namespace android {
26 
27 class SensorService;
28 
29 class SensorService::SensorRegistrationInfo : public SensorServiceUtil::Dumpable {
30 public:
SensorRegistrationInfo()31     SensorRegistrationInfo() : mPackageName() {
32         mSensorHandle = mSamplingRateUs = mMaxReportLatencyUs = INT32_MIN;
33         mHour = mMin = mSec = INT8_MIN;
34         mActivated = false;
35     }
36 
SensorRegistrationInfo(int32_t handle,const String8 & packageName,int64_t samplingRateNs,int64_t maxReportLatencyNs,bool activate)37     SensorRegistrationInfo(int32_t handle, const String8 &packageName,
38                            int64_t samplingRateNs, int64_t maxReportLatencyNs, bool activate) {
39         mSensorHandle = handle;
40         mPackageName = packageName;
41 
42         mSamplingRateUs = static_cast<int64_t>(samplingRateNs/1000);
43         mMaxReportLatencyUs = static_cast<int64_t>(maxReportLatencyNs/1000);
44         mActivated = activate;
45 
46         IPCThreadState *thread = IPCThreadState::self();
47         mPid = (thread != nullptr) ? thread->getCallingPid() : -1;
48         mUid = (thread != nullptr) ? thread->getCallingUid() : -1;
49 
50         time_t rawtime = time(nullptr);
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);
55     }
56 
isSentinel(const SensorRegistrationInfo & info)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
dump()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;
85     int64_t mSamplingRateUs;
86     int64_t mMaxReportLatencyUs;
87     bool mActivated;
88     int8_t mHour, mMin, mSec;
89 
90 };
91 
92 } // namespace android;
93 
94 #endif // ANDROID_SENSOR_REGISTRATION_INFO_H
95 
96 
97