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