• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "socperf_stub.h"
17 #include <cstdint>              // for int32_t
18 #include <iosfwd>               // for string
19 #include <string>               // for basic_string, operator!=, u16string
20 #include <vector>               // for vector
21 #include "accesstoken_kit.h"
22 #include "ipc_skeleton.h"
23 #include "tokenid_kit.h"
24 #include "socperf_ipc_interface_code.h"
25 #include "socperf_log.h"
26 
27 namespace OHOS {
28 namespace SOCPERF {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)29 int32_t SocPerfStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
30     MessageParcel &reply, MessageOption &option)
31 {
32     auto remoteDescriptor = data.ReadInterfaceToken();
33     if (GetDescriptor() != remoteDescriptor || !HasPerfPermission()) {
34         return ERR_INVALID_STATE;
35     }
36     switch (code) {
37         case static_cast<uint32_t>(SocPerfInterfaceCode::TRANS_IPC_ID_PERF_REQUEST): {
38             int32_t cmdId = data.ReadInt32();
39             std::string msg = data.ReadString();
40             PerfRequest(cmdId, msg);
41             break;
42         }
43         case static_cast<uint32_t>(SocPerfInterfaceCode::TRANS_IPC_ID_PERF_REQUEST_EX): {
44             int32_t cmdId = data.ReadInt32();
45             bool onOffTag = data.ReadBool();
46             std::string msg = data.ReadString();
47             PerfRequestEx(cmdId, onOffTag, msg);
48             break;
49         }
50         case static_cast<uint32_t>(SocPerfInterfaceCode::TRANS_IPC_ID_POWER_LIMIT_BOOST_FREQ): {
51             bool onOffTag = data.ReadBool();
52             std::string msg = data.ReadString();
53             PowerLimitBoost(onOffTag, msg);
54             break;
55         }
56         case static_cast<uint32_t>(SocPerfInterfaceCode::TRANS_IPC_ID_THERMAL_LIMIT_BOOST_FREQ): {
57             bool onOffTag = data.ReadBool();
58             std::string msg = data.ReadString();
59             ThermalLimitBoost(onOffTag, msg);
60             break;
61         }
62         case static_cast<uint32_t>(SocPerfInterfaceCode::TRANS_IPC_ID_LIMIT_REQUEST): {
63             int32_t clientId = data.ReadInt32();
64             std::vector<int32_t> tags;
65             data.ReadInt32Vector(&tags);
66             std::vector<int64_t> configs;
67             data.ReadInt64Vector(&configs);
68             std::string msg = data.ReadString();
69             LimitRequest(clientId, tags, configs, msg);
70             break;
71         }
72         default:
73             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74     }
75     return ERR_OK;
76 }
77 
HasPerfPermission()78 bool SocPerfStub::HasPerfPermission()
79 {
80     uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
81     auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(accessToken);
82     if (int(tokenType) == OHOS::Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
83         uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
84         if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId)) {
85             SOC_PERF_LOGE("Invalid Permission to SocPerf, token[%{public}u] tokenType[%{public}d]",
86                 accessToken, (int)tokenType);
87             return false;
88         }
89     }
90     return true;
91 }
92 } // namespace SOCPERF
93 } // namespace OHOS
94