1 /*
2  * Copyright (c) 2022-2023 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 "thermal_action_callback_stub.h"
17 #include <message_parcel.h>
18 
19 #include "constants.h"
20 #include "thermal_action_callback_ipc_interface_code.h"
21 #include "thermal_common.h"
22 #include "xcollie/xcollie.h"
23 
24 namespace OHOS {
25 namespace PowerMgr {
26 constexpr uint32_t MAX_ACTION_MAP_NUM = 2000;
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)27 int ThermalActionCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
28     MessageOption &option)
29 {
30     THERMAL_HILOGD(COMP_SVC,
31         "ThermalActionCallbackStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
32     std::u16string descripter = ThermalActionCallbackStub::GetDescriptor();
33     std::u16string remoteDescripter = data.ReadInterfaceToken();
34     if (descripter != remoteDescripter) {
35         THERMAL_HILOGE(COMP_SVC, "ThermalTempCallbackStub::OnRemoteRequest failed, \
36             descriptor is not matched!");
37         return E_GET_THERMAL_SERVICE_FAILED;
38     }
39     const int DFX_DELAY_S = 60;
40     int id = HiviewDFX::XCollie::GetInstance().SetTimer("ThermalActionCallbackStub", DFX_DELAY_S, nullptr, nullptr,
41         HiviewDFX::XCOLLIE_FLAG_LOG);
42     int ret = ERR_OK;
43     if (code == static_cast<uint32_t>(PowerMgr::ThermalActionCallbackInterfaceCode::THERMAL_ACTION_CHANGED)) {
44         ret = OnThermalActionChangedStub(data);
45     } else {
46         ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
47     }
48     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
49     return ret;
50 }
51 
OnThermalActionChangedStub(MessageParcel & data)52 int32_t ThermalActionCallbackStub::OnThermalActionChangedStub(MessageParcel& data)
53 {
54     std::map<std::string, std::string> actionMapCb;
55     uint32_t size = 0;
56     THERMAL_READ_PARCEL_WITH_RET(data, Uint32, size, E_READ_PARCEL_ERROR_THERMAL);
57     if (size <= 0 || size > MAX_ACTION_MAP_NUM) {
58         THERMAL_HILOGW(COMP_SVC, "size exceed limit, size=%{public}d", size);
59         return E_EXCEED_PARAM_LIMIT;
60     }
61     for (uint32_t i = 0; i < size; i++) {
62         std::string type;
63         std::string action;
64         THERMAL_READ_PARCEL_WITH_RET(data, String, type, E_READ_PARCEL_ERROR_THERMAL);
65         THERMAL_READ_PARCEL_WITH_RET(data, String, action, E_READ_PARCEL_ERROR_THERMAL);
66         actionMapCb[type] = action;
67     }
68     OnThermalActionChanged(actionMapCb);
69     return ERR_OK;
70 }
71 } // namespace PowerMgr
72 } // namespace OHOS
73