• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <ctime>
21 #include <iomanip>
22 #include <sstream>
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"
28 
29 namespace android {
30 
31 class SensorService;
32 
33 class SensorService::SensorRegistrationInfo : public SensorServiceUtil::Dumpable {
34 public:
SensorRegistrationInfo()35     SensorRegistrationInfo() : mPackageName() {
36         mSensorHandle = mSamplingRateUs = mMaxReportLatencyUs = INT32_MIN;
37         mRealtimeSec = 0;
38         mActivated = false;
39     }
40 
SensorRegistrationInfo(int32_t handle,const String8 & packageName,int64_t samplingRateNs,int64_t maxReportLatencyNs,bool activate)41     SensorRegistrationInfo(int32_t handle, const String8 &packageName,
42                            int64_t samplingRateNs, int64_t maxReportLatencyNs, bool activate) {
43         mSensorHandle = handle;
44         mPackageName = packageName;
45 
46         mSamplingRateUs = static_cast<int64_t>(samplingRateNs/1000);
47         mMaxReportLatencyUs = static_cast<int64_t>(maxReportLatencyNs/1000);
48         mActivated = activate;
49 
50         IPCThreadState *thread = IPCThreadState::self();
51         mPid = (thread != nullptr) ? thread->getCallingPid() : -1;
52         mUid = (thread != nullptr) ? thread->getCallingUid() : -1;
53 
54         timespec curTime;
55         clock_gettime(CLOCK_REALTIME_COARSE, &curTime);
56         mRealtimeSec = curTime.tv_sec;
57     }
58 
isSentinel(const SensorRegistrationInfo & info)59     static bool isSentinel(const SensorRegistrationInfo& info) {
60        return (info.mSensorHandle == INT32_MIN && info.mRealtimeSec == 0);
61     }
62 
63     // Dumpable interface
dump()64     virtual std::string dump() const override {
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 
70         std::ostringstream ss;
71         ss << std::setfill('0') << std::setw(2) << static_cast<int>(hour) << ":"
72            << std::setw(2) << static_cast<int>(min) << ":"
73            << std::setw(2) << static_cast<int>(sec)
74            << (mActivated ? " +" : " -")
75            << " 0x" << std::hex << std::setw(8) << mSensorHandle << std::dec
76            << std::setfill(' ') << " pid=" << std::setw(5) << mPid
77            << " uid=" << std::setw(5) << mUid << " package=" << mPackageName;
78         if (mActivated) {
79            ss  << " samplingPeriod=" << mSamplingRateUs << "us"
80                << " batchingPeriod=" << mMaxReportLatencyUs << "us";
81         };
82         return ss.str();
83     }
84 
85     /**
86      * Dump debugging information as android.service.SensorRegistrationInfoProto protobuf message
87      * using ProtoOutputStream.
88      *
89      * See proto definition and some notes about ProtoOutputStream in
90      * frameworks/base/core/proto/android/service/sensor_service.proto
91      */
dump(util::ProtoOutputStream * proto)92     virtual void dump(util::ProtoOutputStream* proto) const override {
93         using namespace service::SensorRegistrationInfoProto;
94         proto->write(TIMESTAMP_SEC, int64_t(mRealtimeSec));
95         proto->write(SENSOR_HANDLE, mSensorHandle);
96         proto->write(PACKAGE_NAME, std::string(mPackageName.string()));
97         proto->write(PID, int32_t(mPid));
98         proto->write(UID, int32_t(mUid));
99         proto->write(SAMPLING_RATE_US, mSamplingRateUs);
100         proto->write(MAX_REPORT_LATENCY_US, mMaxReportLatencyUs);
101         proto->write(ACTIVATED, mActivated);
102     }
103 
104 private:
105     int32_t mSensorHandle;
106     String8 mPackageName;
107     pid_t   mPid;
108     uid_t   mUid;
109     int64_t mSamplingRateUs;
110     int64_t mMaxReportLatencyUs;
111     bool mActivated;
112     time_t mRealtimeSec;
113 };
114 
115 } // namespace android;
116 
117 #endif // ANDROID_SENSOR_REGISTRATION_INFO_H
118 
119 
120