137a09cd7Sopenharmony_ci/*
237a09cd7Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
337a09cd7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
437a09cd7Sopenharmony_ci * you may not use this file except in compliance with the License.
537a09cd7Sopenharmony_ci * You may obtain a copy of the License at
637a09cd7Sopenharmony_ci *
737a09cd7Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
837a09cd7Sopenharmony_ci *
937a09cd7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1037a09cd7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1137a09cd7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1237a09cd7Sopenharmony_ci * See the License for the specific language governing permissions and
1337a09cd7Sopenharmony_ci * limitations under the License.
1437a09cd7Sopenharmony_ci */
1537a09cd7Sopenharmony_ci
1637a09cd7Sopenharmony_ci#include "thermal_srv_stub.h"
1737a09cd7Sopenharmony_ci
1837a09cd7Sopenharmony_ci#include "message_parcel.h"
1937a09cd7Sopenharmony_ci#include "string_ex.h"
2037a09cd7Sopenharmony_ci
2137a09cd7Sopenharmony_ci#include "ithermal_action_callback.h"
2237a09cd7Sopenharmony_ci#include "ithermal_temp_callback.h"
2337a09cd7Sopenharmony_ci#include "ithermal_level_callback.h"
2437a09cd7Sopenharmony_ci#include "thermal_common.h"
2537a09cd7Sopenharmony_ci#include "thermal_srv_ipc_interface_code.h"
2637a09cd7Sopenharmony_ci#include "thermal_srv_sensor_info.h"
2737a09cd7Sopenharmony_ci#include "xcollie/xcollie.h"
2837a09cd7Sopenharmony_ci
2937a09cd7Sopenharmony_cinamespace OHOS {
3037a09cd7Sopenharmony_cinamespace PowerMgr {
3137a09cd7Sopenharmony_cinamespace {
3237a09cd7Sopenharmony_ciconstexpr int PARAM_MAX_NUM = 10;
3337a09cd7Sopenharmony_ci}
3437a09cd7Sopenharmony_ciint ThermalSrvStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
3537a09cd7Sopenharmony_ci{
3637a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC,
3737a09cd7Sopenharmony_ci        "ThermalSrvStub::OnRemoteRequest, cmd = %{public}d, flags = %{public}d",
3837a09cd7Sopenharmony_ci        code, option.GetFlags());
3937a09cd7Sopenharmony_ci    std::u16string descriptor = ThermalSrvStub::GetDescriptor();
4037a09cd7Sopenharmony_ci    std::u16string remoteDescriptor = data.ReadInterfaceToken();
4137a09cd7Sopenharmony_ci    if (descriptor != remoteDescriptor) {
4237a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "ThermalSrvStub::OnRemoteRequest failed, descriptor is not matched!");
4337a09cd7Sopenharmony_ci        return E_GET_THERMAL_SERVICE_FAILED;
4437a09cd7Sopenharmony_ci    }
4537a09cd7Sopenharmony_ci    const int DFX_DELAY_S = 60;
4637a09cd7Sopenharmony_ci    int id = HiviewDFX::XCollie::GetInstance().SetTimer("ThermalSrvStub", DFX_DELAY_S, nullptr, nullptr,
4737a09cd7Sopenharmony_ci        HiviewDFX::XCOLLIE_FLAG_LOG);
4837a09cd7Sopenharmony_ci    int32_t ret = CheckRequestCode(code, data, reply, option);
4937a09cd7Sopenharmony_ci    HiviewDFX::XCollie::GetInstance().CancelTimer(id);
5037a09cd7Sopenharmony_ci    return ret;
5137a09cd7Sopenharmony_ci}
5237a09cd7Sopenharmony_ci
5337a09cd7Sopenharmony_ciint32_t ThermalSrvStub::CheckRequestCode(const uint32_t code, MessageParcel& data, MessageParcel& reply,
5437a09cd7Sopenharmony_ci    MessageOption& option)
5537a09cd7Sopenharmony_ci{
5637a09cd7Sopenharmony_ci    switch (code) {
5737a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::REG_THERMAL_TEMP_CALLBACK): {
5837a09cd7Sopenharmony_ci            return SubscribeThermalTempCallbackStub(data);
5937a09cd7Sopenharmony_ci        }
6037a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::UNREG_THERMAL_TEMP_CALLBACK): {
6137a09cd7Sopenharmony_ci            return UnSubscribeThermalTempCallbackStub(data);
6237a09cd7Sopenharmony_ci        }
6337a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::REG_THERMAL_LEVEL_CALLBACK): {
6437a09cd7Sopenharmony_ci            return SubscribeThermalLevelCallbackStub(data);
6537a09cd7Sopenharmony_ci        }
6637a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::UNREG_THERMAL_LEVEL_CALLBACK): {
6737a09cd7Sopenharmony_ci            return UnSubscribeThermalLevelCallbackStub(data);
6837a09cd7Sopenharmony_ci        }
6937a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::REG_THERMAL_ACTION_CALLBACK): {
7037a09cd7Sopenharmony_ci            return SubscribeThermalActionCallbackStub(data);
7137a09cd7Sopenharmony_ci        }
7237a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::UNREG_THERMAL_ACTION_CALLBACK): {
7337a09cd7Sopenharmony_ci            return UnSubscribeThermalActionCallbackStub(data);
7437a09cd7Sopenharmony_ci        }
7537a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::GET_SENSOR_INFO): {
7637a09cd7Sopenharmony_ci            return GetThermalSrvSensorInfoStub(data, reply);
7737a09cd7Sopenharmony_ci        }
7837a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::GET_TEMP_LEVEL): {
7937a09cd7Sopenharmony_ci            return GetThermalevelStub(reply);
8037a09cd7Sopenharmony_ci        }
8137a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::GET_THERMAL_INFO): {
8237a09cd7Sopenharmony_ci            return GetThermalInfoStub(reply);
8337a09cd7Sopenharmony_ci        }
8437a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::SET_SCENE): {
8537a09cd7Sopenharmony_ci            return SetSceneStub(data);
8637a09cd7Sopenharmony_ci        }
8737a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::UPDATE_THERMAL_STATE): {
8837a09cd7Sopenharmony_ci            return UpdateThermalStateStub(data);
8937a09cd7Sopenharmony_ci        }
9037a09cd7Sopenharmony_ci        case static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::SHELL_DUMP): {
9137a09cd7Sopenharmony_ci            return ShellDumpStub(data, reply);
9237a09cd7Sopenharmony_ci        }
9337a09cd7Sopenharmony_ci        default: {
9437a09cd7Sopenharmony_ci            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
9537a09cd7Sopenharmony_ci        }
9637a09cd7Sopenharmony_ci    }
9737a09cd7Sopenharmony_ci}
9837a09cd7Sopenharmony_ci
9937a09cd7Sopenharmony_ciint32_t ThermalSrvStub::SubscribeThermalTempCallbackStub(MessageParcel& data)
10037a09cd7Sopenharmony_ci{
10137a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
10237a09cd7Sopenharmony_ci    sptr<IRemoteObject> obj = data.ReadRemoteObject();
10337a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR_THERMAL);
10437a09cd7Sopenharmony_ci    sptr<IThermalTempCallback> callback = iface_cast<IThermalTempCallback>(obj);
10537a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR_THERMAL);
10637a09cd7Sopenharmony_ci    std::vector<std::string> typeList;
10737a09cd7Sopenharmony_ci    if (!data.ReadStringVector(&typeList)) {
10837a09cd7Sopenharmony_ci        THERMAL_HILOGI(COMP_SVC, "failed to read type list");
10937a09cd7Sopenharmony_ci        return ERR_INVALID_VALUE;
11037a09cd7Sopenharmony_ci    }
11137a09cd7Sopenharmony_ci    SubscribeThermalTempCallback(typeList, callback);
11237a09cd7Sopenharmony_ci    return ERR_OK;
11337a09cd7Sopenharmony_ci}
11437a09cd7Sopenharmony_ci
11537a09cd7Sopenharmony_ciint32_t ThermalSrvStub::UnSubscribeThermalTempCallbackStub(MessageParcel& data)
11637a09cd7Sopenharmony_ci{
11737a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
11837a09cd7Sopenharmony_ci    sptr<IRemoteObject> obj = data.ReadRemoteObject();
11937a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR_THERMAL);
12037a09cd7Sopenharmony_ci    sptr<IThermalTempCallback> callback = iface_cast<IThermalTempCallback>(obj);
12137a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR_THERMAL);
12237a09cd7Sopenharmony_ci    UnSubscribeThermalTempCallback(callback);
12337a09cd7Sopenharmony_ci    return ERR_OK;
12437a09cd7Sopenharmony_ci}
12537a09cd7Sopenharmony_ci
12637a09cd7Sopenharmony_ciint32_t ThermalSrvStub::SubscribeThermalLevelCallbackStub(MessageParcel& data)
12737a09cd7Sopenharmony_ci{
12837a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "SubscribeThermalLevelCallbackStub Enter");
12937a09cd7Sopenharmony_ci    sptr<IRemoteObject> obj = data.ReadRemoteObject();
13037a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR_THERMAL);
13137a09cd7Sopenharmony_ci    sptr<IThermalLevelCallback> callback = iface_cast<IThermalLevelCallback>(obj);
13237a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR_THERMAL);
13337a09cd7Sopenharmony_ci    SubscribeThermalLevelCallback(callback);
13437a09cd7Sopenharmony_ci    return ERR_OK;
13537a09cd7Sopenharmony_ci}
13637a09cd7Sopenharmony_ci
13737a09cd7Sopenharmony_ciint32_t ThermalSrvStub::UnSubscribeThermalLevelCallbackStub(MessageParcel& data)
13837a09cd7Sopenharmony_ci{
13937a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
14037a09cd7Sopenharmony_ci    sptr<IRemoteObject> obj = data.ReadRemoteObject();
14137a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR_THERMAL);
14237a09cd7Sopenharmony_ci    sptr<IThermalLevelCallback> callback = iface_cast<IThermalLevelCallback>(obj);
14337a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR_THERMAL);
14437a09cd7Sopenharmony_ci    UnSubscribeThermalLevelCallback(callback);
14537a09cd7Sopenharmony_ci    return ERR_OK;
14637a09cd7Sopenharmony_ci}
14737a09cd7Sopenharmony_ci
14837a09cd7Sopenharmony_ciint32_t ThermalSrvStub::SubscribeThermalActionCallbackStub(MessageParcel& data)
14937a09cd7Sopenharmony_ci{
15037a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
15137a09cd7Sopenharmony_ci    sptr<IRemoteObject> obj = data.ReadRemoteObject();
15237a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR_THERMAL);
15337a09cd7Sopenharmony_ci    sptr<IThermalActionCallback> callback = iface_cast<IThermalActionCallback>(obj);
15437a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR_THERMAL);
15537a09cd7Sopenharmony_ci
15637a09cd7Sopenharmony_ci    std::vector<std::string> actionList;
15737a09cd7Sopenharmony_ci    if (!data.ReadStringVector(&actionList)) {
15837a09cd7Sopenharmony_ci        THERMAL_HILOGI(COMP_SVC, "failed to read action list");
15937a09cd7Sopenharmony_ci        return ERR_INVALID_VALUE;
16037a09cd7Sopenharmony_ci    }
16137a09cd7Sopenharmony_ci
16237a09cd7Sopenharmony_ci    std::string desc;
16337a09cd7Sopenharmony_ci    THERMAL_READ_PARCEL_WITH_RET(data, String, desc, E_READ_PARCEL_ERROR_THERMAL);
16437a09cd7Sopenharmony_ci
16537a09cd7Sopenharmony_ci    SubscribeThermalActionCallback(actionList, desc, callback);
16637a09cd7Sopenharmony_ci    return ERR_OK;
16737a09cd7Sopenharmony_ci}
16837a09cd7Sopenharmony_ci
16937a09cd7Sopenharmony_ciint32_t ThermalSrvStub::UnSubscribeThermalActionCallbackStub(MessageParcel& data)
17037a09cd7Sopenharmony_ci{
17137a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
17237a09cd7Sopenharmony_ci    sptr<IRemoteObject> obj = data.ReadRemoteObject();
17337a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR_THERMAL);
17437a09cd7Sopenharmony_ci    sptr<IThermalActionCallback> callback = iface_cast<IThermalActionCallback>(obj);
17537a09cd7Sopenharmony_ci    THERMAL_RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR_THERMAL);
17637a09cd7Sopenharmony_ci    UnSubscribeThermalActionCallback(callback);
17737a09cd7Sopenharmony_ci    return ERR_OK;
17837a09cd7Sopenharmony_ci}
17937a09cd7Sopenharmony_ci
18037a09cd7Sopenharmony_ciint32_t ThermalSrvStub::GetThermalSrvSensorInfoStub(MessageParcel& data, MessageParcel& reply)
18137a09cd7Sopenharmony_ci{
18237a09cd7Sopenharmony_ci    ThermalSrvSensorInfo sensorInfo;
18337a09cd7Sopenharmony_ci    uint32_t type = 0;
18437a09cd7Sopenharmony_ci
18537a09cd7Sopenharmony_ci    THERMAL_READ_PARCEL_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR_THERMAL);
18637a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "type is %{public}d", type);
18737a09cd7Sopenharmony_ci    bool ret = GetThermalSrvSensorInfo(static_cast<SensorType>(type), sensorInfo);
18837a09cd7Sopenharmony_ci    if (ret) {
18937a09cd7Sopenharmony_ci        if (!reply.WriteParcelable(&sensorInfo)) {
19037a09cd7Sopenharmony_ci            THERMAL_HILOGE(COMP_SVC, "write failed");
19137a09cd7Sopenharmony_ci            return -1;
19237a09cd7Sopenharmony_ci        }
19337a09cd7Sopenharmony_ci    }
19437a09cd7Sopenharmony_ci    return ERR_OK;
19537a09cd7Sopenharmony_ci}
19637a09cd7Sopenharmony_ci
19737a09cd7Sopenharmony_ciint32_t ThermalSrvStub::GetThermalevelStub(MessageParcel& reply)
19837a09cd7Sopenharmony_ci{
19937a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
20037a09cd7Sopenharmony_ci    ThermalLevel level;
20137a09cd7Sopenharmony_ci    GetThermalLevel(level);
20237a09cd7Sopenharmony_ci    THERMAL_WRITE_PARCEL_WITH_RET(reply, Uint32, static_cast<uint32_t>(level), ERR_OK);
20337a09cd7Sopenharmony_ci    return ERR_OK;
20437a09cd7Sopenharmony_ci}
20537a09cd7Sopenharmony_ci
20637a09cd7Sopenharmony_ciint32_t ThermalSrvStub::GetThermalInfoStub(MessageParcel& reply)
20737a09cd7Sopenharmony_ci{
20837a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
20937a09cd7Sopenharmony_ci    bool ret = false;
21037a09cd7Sopenharmony_ci    ret = GetThermalInfo();
21137a09cd7Sopenharmony_ci    if (!reply.WriteBool(ret)) {
21237a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_FWK, "WriteBool fail");
21337a09cd7Sopenharmony_ci        return E_READ_PARCEL_ERROR_THERMAL;
21437a09cd7Sopenharmony_ci    }
21537a09cd7Sopenharmony_ci    return ERR_OK;
21637a09cd7Sopenharmony_ci}
21737a09cd7Sopenharmony_ci
21837a09cd7Sopenharmony_ciint32_t ThermalSrvStub::SetSceneStub(MessageParcel& data)
21937a09cd7Sopenharmony_ci{
22037a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
22137a09cd7Sopenharmony_ci    std::string scene;
22237a09cd7Sopenharmony_ci
22337a09cd7Sopenharmony_ci    THERMAL_READ_PARCEL_WITH_RET(data, String, scene, E_READ_PARCEL_ERROR_THERMAL);
22437a09cd7Sopenharmony_ci    SetScene(scene);
22537a09cd7Sopenharmony_ci    return ERR_OK;
22637a09cd7Sopenharmony_ci}
22737a09cd7Sopenharmony_ci
22837a09cd7Sopenharmony_ciint32_t ThermalSrvStub::UpdateThermalStateStub(MessageParcel& data)
22937a09cd7Sopenharmony_ci{
23037a09cd7Sopenharmony_ci    THERMAL_HILOGD(COMP_SVC, "Enter");
23137a09cd7Sopenharmony_ci    std::string tag;
23237a09cd7Sopenharmony_ci    std::string val;
23337a09cd7Sopenharmony_ci    bool isImmed = false;
23437a09cd7Sopenharmony_ci    THERMAL_READ_PARCEL_WITH_RET(data, String, tag, E_READ_PARCEL_ERROR_THERMAL);
23537a09cd7Sopenharmony_ci    THERMAL_READ_PARCEL_WITH_RET(data, String, val, E_READ_PARCEL_ERROR_THERMAL);
23637a09cd7Sopenharmony_ci    THERMAL_READ_PARCEL_WITH_RET(data, Bool, isImmed, E_READ_PARCEL_ERROR_THERMAL);
23737a09cd7Sopenharmony_ci    UpdateThermalState(tag, val, isImmed);
23837a09cd7Sopenharmony_ci    return ERR_OK;
23937a09cd7Sopenharmony_ci}
24037a09cd7Sopenharmony_ci
24137a09cd7Sopenharmony_ciint32_t ThermalSrvStub::ShellDumpStub(MessageParcel& data, MessageParcel& reply)
24237a09cd7Sopenharmony_ci{
24337a09cd7Sopenharmony_ci    uint32_t argc;
24437a09cd7Sopenharmony_ci    std::vector<std::string> args;
24537a09cd7Sopenharmony_ci
24637a09cd7Sopenharmony_ci    if (!data.ReadUint32(argc)) {
24737a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "Readback fail!");
24837a09cd7Sopenharmony_ci        return E_READ_PARCEL_ERROR_THERMAL;
24937a09cd7Sopenharmony_ci    }
25037a09cd7Sopenharmony_ci
25137a09cd7Sopenharmony_ci    if (argc >= PARAM_MAX_NUM) {
25237a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "params exceed limit");
25337a09cd7Sopenharmony_ci        return E_EXCEED_PARAM_LIMIT;
25437a09cd7Sopenharmony_ci    }
25537a09cd7Sopenharmony_ci
25637a09cd7Sopenharmony_ci    for (uint32_t i = 0; i < argc; i++) {
25737a09cd7Sopenharmony_ci        std::string arg = data.ReadString();
25837a09cd7Sopenharmony_ci        if (arg.empty()) {
25937a09cd7Sopenharmony_ci            THERMAL_HILOGE(COMP_SVC, "read value fail:%{public}d", i);
26037a09cd7Sopenharmony_ci            return E_READ_PARCEL_ERROR_THERMAL;
26137a09cd7Sopenharmony_ci        }
26237a09cd7Sopenharmony_ci        args.push_back(arg);
26337a09cd7Sopenharmony_ci    }
26437a09cd7Sopenharmony_ci
26537a09cd7Sopenharmony_ci    std::string ret = ShellDump(args, argc);
26637a09cd7Sopenharmony_ci    if (!reply.WriteString(ret)) {
26737a09cd7Sopenharmony_ci        THERMAL_HILOGE(COMP_SVC, "PowerMgrStub:: Dump Writeback Fail!");
26837a09cd7Sopenharmony_ci        return E_READ_PARCEL_ERROR_THERMAL;
26937a09cd7Sopenharmony_ci    }
27037a09cd7Sopenharmony_ci    return ERR_OK;
27137a09cd7Sopenharmony_ci}
27237a09cd7Sopenharmony_ci} // namespace PowerMgr
27337a09cd7Sopenharmony_ci} // namespace OHOS
274