179a732c7Sopenharmony_ci/*
279a732c7Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
379a732c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
479a732c7Sopenharmony_ci * you may not use this file except in compliance with the License.
579a732c7Sopenharmony_ci * You may obtain a copy of the License at
679a732c7Sopenharmony_ci *
779a732c7Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
879a732c7Sopenharmony_ci *
979a732c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1079a732c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1179a732c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1279a732c7Sopenharmony_ci * See the License for the specific language governing permissions and
1379a732c7Sopenharmony_ci * limitations under the License.
1479a732c7Sopenharmony_ci */
1579a732c7Sopenharmony_ci
1679a732c7Sopenharmony_ci#include "pin_holder.h"
1779a732c7Sopenharmony_ci
1879a732c7Sopenharmony_ci#include "dm_anonymous.h"
1979a732c7Sopenharmony_ci#include "dm_crypto.h"
2079a732c7Sopenharmony_ci#include "dm_log.h"
2179a732c7Sopenharmony_ci#include "dm_radar_helper.h"
2279a732c7Sopenharmony_ci#include "nlohmann/json.hpp"
2379a732c7Sopenharmony_ci
2479a732c7Sopenharmony_cinamespace OHOS {
2579a732c7Sopenharmony_cinamespace DistributedHardware {
2679a732c7Sopenharmony_ciconstexpr int32_t SESSION_SIDE_SERVER = 0;
2779a732c7Sopenharmony_ciconstexpr int32_t SESSION_ID_INVALID = -1;
2879a732c7Sopenharmony_ciconstexpr int32_t REPLY_SUCCESS = 0;
2979a732c7Sopenharmony_ciconstexpr int32_t REPLY_FAILED = -1;
3079a732c7Sopenharmony_ci
3179a732c7Sopenharmony_ciconstexpr int32_t MSG_TYPE_CREATE_PIN_HOLDER = 600;
3279a732c7Sopenharmony_ciconstexpr int32_t MSG_TYPE_CREATE_PIN_HOLDER_RESP = 601;
3379a732c7Sopenharmony_ciconstexpr int32_t MSG_TYPE_DESTROY_PIN_HOLDER = 650;
3479a732c7Sopenharmony_ciconstexpr int32_t MSG_TYPE_DESTROY_PIN_HOLDER_RESP = 651;
3579a732c7Sopenharmony_ciconstexpr int32_t MSG_TYPE_PIN_HOLDER_CHANGE = 700;
3679a732c7Sopenharmony_ciconstexpr int32_t MSG_TYPE_PIN_HOLDER_CHANGE_RESP = 701;
3779a732c7Sopenharmony_ci
3879a732c7Sopenharmony_ciconstexpr const char* PINHOLDER_CREATE_TIMEOUT_TASK = "deviceManagerTimer:pinholdercreate";
3979a732c7Sopenharmony_ciconstexpr int32_t PIN_HOLDER_SESSION_CREATE_TIMEOUT = 60;
4079a732c7Sopenharmony_ci
4179a732c7Sopenharmony_ciconstexpr const char* TAG_PIN_TYPE = "PIN_TYPE";
4279a732c7Sopenharmony_ciconstexpr const char* TAG_PAYLOAD = "PAYLOAD";
4379a732c7Sopenharmony_ciconstexpr const char* TAG_REPLY = "REPLY";
4479a732c7Sopenharmony_ciconstexpr const char* TAG_REMOTE_DEVICE_ID = "REMOTE_DEVICE_ID";
4579a732c7Sopenharmony_ci
4679a732c7Sopenharmony_ciconstexpr int32_t DM_OK = 0;
4779a732c7Sopenharmony_ciconstexpr int32_t ERR_DM_FAILED = 96929744;
4879a732c7Sopenharmony_ciconstexpr int32_t ERR_DM_TIME_OUT = 96929745;
4979a732c7Sopenharmony_ciconstexpr const char* TAG_MSG_TYPE = "MSG_TYPE";
5079a732c7Sopenharmony_ciconstexpr const char* TAG_DM_VERSION = "DM_VERSION";
5179a732c7Sopenharmony_ciconstexpr const char* DM_CONNECTION_DISCONNECTED = "DM_CONNECTION_DISCONNECTED";
5279a732c7Sopenharmony_ciconstexpr int32_t DEVICE_UUID_LENGTH = 65;
5379a732c7Sopenharmony_ciconstexpr int32_t ERR_DM_INPUT_PARA_INVALID = 96929749;
5479a732c7Sopenharmony_ciconstexpr int32_t ERR_DM_BIND_PEER_UNSUPPORTED = 96929802;
5579a732c7Sopenharmony_ciPinHolder::PinHolder(std::shared_ptr<IDeviceManagerServiceListener> listener): listener_(listener)
5679a732c7Sopenharmony_ci{
5779a732c7Sopenharmony_ci    if (session_ == nullptr) {
5879a732c7Sopenharmony_ci        session_ = std::make_shared<PinHolderSession>();
5979a732c7Sopenharmony_ci    }
6079a732c7Sopenharmony_ci    if (timer_ == nullptr) {
6179a732c7Sopenharmony_ci        timer_ = std::make_shared<DmTimer>();
6279a732c7Sopenharmony_ci    }
6379a732c7Sopenharmony_ci    sinkState_ = SINK_INIT;
6479a732c7Sopenharmony_ci    sourceState_ = SOURCE_INIT;
6579a732c7Sopenharmony_ci}
6679a732c7Sopenharmony_ci
6779a732c7Sopenharmony_ciPinHolder::~PinHolder()
6879a732c7Sopenharmony_ci{
6979a732c7Sopenharmony_ci    if (session_ != nullptr) {
7079a732c7Sopenharmony_ci        session_->UnRegisterSessionCallback();
7179a732c7Sopenharmony_ci        session_ = nullptr;
7279a732c7Sopenharmony_ci    }
7379a732c7Sopenharmony_ci    if (timer_ != nullptr) {
7479a732c7Sopenharmony_ci        timer_->DeleteAll();
7579a732c7Sopenharmony_ci        timer_ = nullptr;
7679a732c7Sopenharmony_ci    }
7779a732c7Sopenharmony_ci}
7879a732c7Sopenharmony_ci
7979a732c7Sopenharmony_ciint32_t PinHolder::RegisterPinHolderCallback(const std::string &pkgName)
8079a732c7Sopenharmony_ci{
8179a732c7Sopenharmony_ci    if (session_ == nullptr) {
8279a732c7Sopenharmony_ci        LOGE("RegisterPinHolderCallback session is nullptr.");
8379a732c7Sopenharmony_ci        return ERR_DM_FAILED;
8479a732c7Sopenharmony_ci    }
8579a732c7Sopenharmony_ci    registerPkgName_ = pkgName;
8679a732c7Sopenharmony_ci    session_->RegisterSessionCallback(shared_from_this());
8779a732c7Sopenharmony_ci    return DM_OK;
8879a732c7Sopenharmony_ci}
8979a732c7Sopenharmony_ci
9079a732c7Sopenharmony_ciint32_t PinHolder::CreatePinHolder(const std::string &pkgName,
9179a732c7Sopenharmony_ci    const PeerTargetId &targetId, DmPinType pinType, const std::string &payload)
9279a732c7Sopenharmony_ci{
9379a732c7Sopenharmony_ci    LOGI("CreatePinHolder.");
9479a732c7Sopenharmony_ci    if (registerPkgName_.empty() || registerPkgName_ != pkgName) {
9579a732c7Sopenharmony_ci        LOGE("CreatePinHolder pkgName: %{public}s is not register callback.", pkgName.c_str());
9679a732c7Sopenharmony_ci        return ERR_DM_FAILED;
9779a732c7Sopenharmony_ci    }
9879a732c7Sopenharmony_ci    int32_t ret = CheckTargetIdVaild(targetId);
9979a732c7Sopenharmony_ci    if (ret != DM_OK) {
10079a732c7Sopenharmony_ci        LOGE("CreatePinHolder targetId is invalid.");
10179a732c7Sopenharmony_ci        return ret;
10279a732c7Sopenharmony_ci    }
10379a732c7Sopenharmony_ci    if (listener_ == nullptr || session_ == nullptr) {
10479a732c7Sopenharmony_ci        LOGE("CreatePinHolder listener or session is nullptr.");
10579a732c7Sopenharmony_ci        return ERR_DM_FAILED;
10679a732c7Sopenharmony_ci    }
10779a732c7Sopenharmony_ci    if (sourceState_ != SOURCE_INIT) {
10879a732c7Sopenharmony_ci        LOGE("CreatePinHolder failed, state is %{public}d.", sourceState_);
10979a732c7Sopenharmony_ci        return ERR_DM_FAILED;
11079a732c7Sopenharmony_ci    }
11179a732c7Sopenharmony_ci    if (sessionId_ != SESSION_ID_INVALID) {
11279a732c7Sopenharmony_ci        LOGI("CreatePinHolder session already create, sessionId: %{public}d.", sessionId_);
11379a732c7Sopenharmony_ci        CreateGeneratePinHolderMsg();
11479a732c7Sopenharmony_ci        return DM_OK;
11579a732c7Sopenharmony_ci    }
11679a732c7Sopenharmony_ci
11779a732c7Sopenharmony_ci    sessionId_ = session_->OpenSessionServer(targetId);
11879a732c7Sopenharmony_ci    int32_t stageRes =
11979a732c7Sopenharmony_ci        sessionId_ > 0 ? static_cast<int32_t>(StageRes::STAGE_SUCC) : static_cast<int32_t>(StageRes::STAGE_FAIL);
12079a732c7Sopenharmony_ci    DmRadarHelper::GetInstance().ReportCreatePinHolder(
12179a732c7Sopenharmony_ci        registerPkgName_, sessionId_, targetId.deviceId, sessionId_, stageRes);
12279a732c7Sopenharmony_ci    if (sessionId_ < 0) {
12379a732c7Sopenharmony_ci        LOGE("[SOFTBUS]open session error, sessionId: %{public}d.", sessionId_);
12479a732c7Sopenharmony_ci        listener_->OnCreateResult(registerPkgName_, sessionId_);
12579a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::CREATE_RESULT,
12679a732c7Sopenharmony_ci            sessionId_, "");
12779a732c7Sopenharmony_ci        sessionId_ = SESSION_ID_INVALID;
12879a732c7Sopenharmony_ci        return sessionId_;
12979a732c7Sopenharmony_ci    }
13079a732c7Sopenharmony_ci    pinType_ = pinType;
13179a732c7Sopenharmony_ci    payload_ = payload;
13279a732c7Sopenharmony_ci    return DM_OK;
13379a732c7Sopenharmony_ci}
13479a732c7Sopenharmony_ci
13579a732c7Sopenharmony_ciint32_t PinHolder::DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId, DmPinType pinType,
13679a732c7Sopenharmony_ci    const std::string &payload)
13779a732c7Sopenharmony_ci{
13879a732c7Sopenharmony_ci    LOGI("DestroyPinHolder.");
13979a732c7Sopenharmony_ci    if (listener_ == nullptr || session_ == nullptr) {
14079a732c7Sopenharmony_ci        LOGE("DestroyPinHolder listener or session is nullptr.");
14179a732c7Sopenharmony_ci        return ERR_DM_FAILED;
14279a732c7Sopenharmony_ci    }
14379a732c7Sopenharmony_ci    if (registerPkgName_.empty() || pkgName != registerPkgName_) {
14479a732c7Sopenharmony_ci        LOGE("DestroyPinHolder pkgName: %{public}s is not register callback.", pkgName.c_str());
14579a732c7Sopenharmony_ci        return ERR_DM_FAILED;
14679a732c7Sopenharmony_ci    }
14779a732c7Sopenharmony_ci    int32_t ret = CheckTargetIdVaild(targetId);
14879a732c7Sopenharmony_ci    if (ret != DM_OK) {
14979a732c7Sopenharmony_ci        LOGE("DestroyPinHolder targetId is invalid.");
15079a732c7Sopenharmony_ci        return ret;
15179a732c7Sopenharmony_ci    }
15279a732c7Sopenharmony_ci    if (sessionId_ == SESSION_ID_INVALID) {
15379a732c7Sopenharmony_ci        LOGI("DestroyPinHolder session already destroy.");
15479a732c7Sopenharmony_ci        listener_->OnDestroyResult(registerPkgName_, ret);
15579a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY_RESULT, ret, "");
15679a732c7Sopenharmony_ci        return ret;
15779a732c7Sopenharmony_ci    }
15879a732c7Sopenharmony_ci    if (sourceState_ != SOURCE_CREATE) {
15979a732c7Sopenharmony_ci        LOGE("DestroyPinHolder failed, state is %{public}d.", sourceState_);
16079a732c7Sopenharmony_ci        return ERR_DM_FAILED;
16179a732c7Sopenharmony_ci    }
16279a732c7Sopenharmony_ci    if (timer_ != nullptr) {
16379a732c7Sopenharmony_ci        timer_->DeleteTimer(PINHOLDER_CREATE_TIMEOUT_TASK);
16479a732c7Sopenharmony_ci    }
16579a732c7Sopenharmony_ci    nlohmann::json jsonObj;
16679a732c7Sopenharmony_ci    jsonObj[TAG_MSG_TYPE] = MSG_TYPE_DESTROY_PIN_HOLDER;
16779a732c7Sopenharmony_ci    jsonObj[TAG_PIN_TYPE] = pinType;
16879a732c7Sopenharmony_ci    jsonObj[TAG_PAYLOAD] = payload;
16979a732c7Sopenharmony_ci    pinType_ = pinType;
17079a732c7Sopenharmony_ci    std::string message = jsonObj.dump();
17179a732c7Sopenharmony_ci    LOGI("DestroyPinHolder, message type is: %{public}d, pin type is: %{public}d.", MSG_TYPE_DESTROY_PIN_HOLDER,
17279a732c7Sopenharmony_ci        pinType);
17379a732c7Sopenharmony_ci    ret = session_->SendData(sessionId_, message);
17479a732c7Sopenharmony_ci    int32_t stageRes =
17579a732c7Sopenharmony_ci        ret == DM_OK ? static_cast<int32_t>(StageRes::STAGE_SUCC) : static_cast<int32_t>(StageRes::STAGE_FAIL);
17679a732c7Sopenharmony_ci    DmRadarHelper::GetInstance().ReportDestroyPinHolder(registerPkgName_, targetId.deviceId, ret, stageRes);
17779a732c7Sopenharmony_ci    if (ret != DM_OK) {
17879a732c7Sopenharmony_ci        LOGE("[SOFTBUS]SendBytes failed, ret: %{public}d.", ret);
17979a732c7Sopenharmony_ci        listener_->OnDestroyResult(registerPkgName_, ERR_DM_FAILED);
18079a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY_RESULT, ERR_DM_FAILED, "");
18179a732c7Sopenharmony_ci        return ret;
18279a732c7Sopenharmony_ci    }
18379a732c7Sopenharmony_ci    return ret;
18479a732c7Sopenharmony_ci}
18579a732c7Sopenharmony_ci
18679a732c7Sopenharmony_ciint32_t PinHolder::CreateGeneratePinHolderMsg()
18779a732c7Sopenharmony_ci{
18879a732c7Sopenharmony_ci    if (listener_ == nullptr || session_ == nullptr) {
18979a732c7Sopenharmony_ci        LOGE("CreateGeneratePinHolderMsg listener or session is nullptr.");
19079a732c7Sopenharmony_ci        return ERR_DM_FAILED;
19179a732c7Sopenharmony_ci    }
19279a732c7Sopenharmony_ci
19379a732c7Sopenharmony_ci    timer_->DeleteAll();
19479a732c7Sopenharmony_ci    timer_->StartTimer(std::string(PINHOLDER_CREATE_TIMEOUT_TASK), PIN_HOLDER_SESSION_CREATE_TIMEOUT,
19579a732c7Sopenharmony_ci        [this] (std::string name) {
19679a732c7Sopenharmony_ci            PinHolder::CloseSession(name);
19779a732c7Sopenharmony_ci        });
19879a732c7Sopenharmony_ci    nlohmann::json jsonObj;
19979a732c7Sopenharmony_ci    jsonObj[TAG_PIN_TYPE] = pinType_;
20079a732c7Sopenharmony_ci    jsonObj[TAG_PAYLOAD] = payload_;
20179a732c7Sopenharmony_ci    jsonObj[TAG_MSG_TYPE] = MSG_TYPE_CREATE_PIN_HOLDER;
20279a732c7Sopenharmony_ci    jsonObj[TAG_DM_VERSION] = "";
20379a732c7Sopenharmony_ci    std::string message = jsonObj.dump();
20479a732c7Sopenharmony_ci    LOGI("CreateGeneratePinHolderMsg, message type is: %{public}d, pin type is: %{public}d.",
20579a732c7Sopenharmony_ci        MSG_TYPE_CREATE_PIN_HOLDER, pinType_);
20679a732c7Sopenharmony_ci    int32_t ret = session_->SendData(sessionId_, message);
20779a732c7Sopenharmony_ci    int32_t bizStage = static_cast<int32_t>(PinHolderStage::SEND_CREATE_PIN_HOLDER_MSG);
20879a732c7Sopenharmony_ci    DmRadarHelper::GetInstance().ReportSendOrReceiveHolderMsg(bizStage,
20979a732c7Sopenharmony_ci        std::string("CreateGeneratePinHolderMsg"), "");
21079a732c7Sopenharmony_ci    if (ret != DM_OK) {
21179a732c7Sopenharmony_ci        LOGE("[SOFTBUS]SendBytes failed, ret: %{public}d.", ret);
21279a732c7Sopenharmony_ci        listener_->OnCreateResult(registerPkgName_, ret);
21379a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::CREATE_RESULT, ret, "");
21479a732c7Sopenharmony_ci        return ret;
21579a732c7Sopenharmony_ci    }
21679a732c7Sopenharmony_ci    return ret;
21779a732c7Sopenharmony_ci}
21879a732c7Sopenharmony_ci
21979a732c7Sopenharmony_ciint32_t PinHolder::ParseMsgType(const std::string &message)
22079a732c7Sopenharmony_ci{
22179a732c7Sopenharmony_ci    nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
22279a732c7Sopenharmony_ci    if (jsonObject.is_discarded()) {
22379a732c7Sopenharmony_ci        LOGE("ParseMsgType DecodeRequest jsonStr error");
22479a732c7Sopenharmony_ci        return ERR_DM_FAILED;
22579a732c7Sopenharmony_ci    }
22679a732c7Sopenharmony_ci    if (!IsInt32(jsonObject, TAG_MSG_TYPE)) {
22779a732c7Sopenharmony_ci        LOGE("ParseMsgType err json string.");
22879a732c7Sopenharmony_ci        return ERR_DM_FAILED;
22979a732c7Sopenharmony_ci    }
23079a732c7Sopenharmony_ci    int32_t msgType = jsonObject[TAG_MSG_TYPE].get<int32_t>();
23179a732c7Sopenharmony_ci    return msgType;
23279a732c7Sopenharmony_ci}
23379a732c7Sopenharmony_ci
23479a732c7Sopenharmony_civoid PinHolder::ProcessCreateMsg(const std::string &message)
23579a732c7Sopenharmony_ci{
23679a732c7Sopenharmony_ci    if (listener_ == nullptr || session_ == nullptr) {
23779a732c7Sopenharmony_ci        LOGE("ProcessCreateMsg listener or session is nullptr.");
23879a732c7Sopenharmony_ci        return;
23979a732c7Sopenharmony_ci    }
24079a732c7Sopenharmony_ci    nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
24179a732c7Sopenharmony_ci    if (jsonObject.is_discarded()) {
24279a732c7Sopenharmony_ci        LOGE("ProcessCreateMsg DecodeRequest jsonStr error");
24379a732c7Sopenharmony_ci        return;
24479a732c7Sopenharmony_ci    }
24579a732c7Sopenharmony_ci    if (!IsInt32(jsonObject, TAG_PIN_TYPE) || !IsString(jsonObject, TAG_PAYLOAD)) {
24679a732c7Sopenharmony_ci        LOGE("ProcessCreateMsg err json string.");
24779a732c7Sopenharmony_ci        return;
24879a732c7Sopenharmony_ci    }
24979a732c7Sopenharmony_ci    DmPinType pinType = static_cast<DmPinType>(jsonObject[TAG_PIN_TYPE].get<int32_t>());
25079a732c7Sopenharmony_ci    std::string payload = jsonObject[TAG_PAYLOAD].get<std::string>();
25179a732c7Sopenharmony_ci    isRemoteSupported_ = jsonObject.contains(TAG_DM_VERSION);
25279a732c7Sopenharmony_ci    int32_t bizStage = static_cast<int32_t>(PinHolderStage::RECEIVE_CREATE_PIN_HOLDER_MSG);
25379a732c7Sopenharmony_ci    DmRadarHelper::GetInstance().ReportSendOrReceiveHolderMsg(bizStage, std::string("ProcessCreateMsg"), "");
25479a732c7Sopenharmony_ci    nlohmann::json jsonObj;
25579a732c7Sopenharmony_ci    jsonObj[TAG_MSG_TYPE] = MSG_TYPE_CREATE_PIN_HOLDER_RESP;
25679a732c7Sopenharmony_ci    if (sinkState_ != SINK_INIT) {
25779a732c7Sopenharmony_ci        jsonObj[TAG_REPLY] = REPLY_FAILED;
25879a732c7Sopenharmony_ci    } else {
25979a732c7Sopenharmony_ci        jsonObj[TAG_REPLY] = REPLY_SUCCESS;
26079a732c7Sopenharmony_ci        sinkState_ = SINK_CREATE;
26179a732c7Sopenharmony_ci        sourceState_ = SOURCE_CREATE;
26279a732c7Sopenharmony_ci        listener_->OnPinHolderCreate(registerPkgName_, remoteDeviceId_, pinType, payload);
26379a732c7Sopenharmony_ci        nlohmann::json jsonContent;
26479a732c7Sopenharmony_ci        jsonContent[TAG_PIN_TYPE] = pinType;
26579a732c7Sopenharmony_ci        jsonContent[TAG_PAYLOAD] = payload;
26679a732c7Sopenharmony_ci        jsonContent[TAG_REMOTE_DEVICE_ID] = remoteDeviceId_;
26779a732c7Sopenharmony_ci        std::string content = jsonContent.dump();
26879a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::CREATE, DM_OK, content);
26979a732c7Sopenharmony_ci    }
27079a732c7Sopenharmony_ci    jsonObj[TAG_DM_VERSION] = "";
27179a732c7Sopenharmony_ci
27279a732c7Sopenharmony_ci    std::string msg = jsonObj.dump();
27379a732c7Sopenharmony_ci    LOGI("ProcessCreateMsg, message type is: %{public}d.", MSG_TYPE_CREATE_PIN_HOLDER_RESP);
27479a732c7Sopenharmony_ci    int32_t ret = session_->SendData(sessionId_, msg);
27579a732c7Sopenharmony_ci    if (ret != DM_OK) {
27679a732c7Sopenharmony_ci        LOGE("[SOFTBUS]SendBytes failed, ret: %{public}d.", ret);
27779a732c7Sopenharmony_ci        return;
27879a732c7Sopenharmony_ci    }
27979a732c7Sopenharmony_ci}
28079a732c7Sopenharmony_ci
28179a732c7Sopenharmony_civoid PinHolder::ProcessCreateRespMsg(const std::string &message)
28279a732c7Sopenharmony_ci{
28379a732c7Sopenharmony_ci    nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
28479a732c7Sopenharmony_ci    if (jsonObject.is_discarded()) {
28579a732c7Sopenharmony_ci        LOGE("ProcessCreateRespMsg DecodeRequest jsonStr error.");
28679a732c7Sopenharmony_ci        return;
28779a732c7Sopenharmony_ci    }
28879a732c7Sopenharmony_ci    if (!IsInt32(jsonObject, TAG_REPLY)) {
28979a732c7Sopenharmony_ci        LOGE("ProcessCreateRespMsg err json string.");
29079a732c7Sopenharmony_ci        return;
29179a732c7Sopenharmony_ci    }
29279a732c7Sopenharmony_ci    isRemoteSupported_ = jsonObject.contains(TAG_DM_VERSION);
29379a732c7Sopenharmony_ci    int32_t reply = jsonObject[TAG_REPLY].get<int32_t>();
29479a732c7Sopenharmony_ci    if (listener_ == nullptr || session_ == nullptr) {
29579a732c7Sopenharmony_ci        LOGE("ProcessCreateRespMsg listener or session is nullptr.");
29679a732c7Sopenharmony_ci        return;
29779a732c7Sopenharmony_ci    }
29879a732c7Sopenharmony_ci    if (reply == REPLY_SUCCESS) {
29979a732c7Sopenharmony_ci        listener_->OnCreateResult(registerPkgName_, DM_OK);
30079a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::CREATE_RESULT, DM_OK, "");
30179a732c7Sopenharmony_ci        sourceState_ = SOURCE_CREATE;
30279a732c7Sopenharmony_ci        sinkState_ = SINK_CREATE;
30379a732c7Sopenharmony_ci    } else {
30479a732c7Sopenharmony_ci        LOGE("ProcessCreateRespMsg remote state is wrong.");
30579a732c7Sopenharmony_ci        listener_->OnCreateResult(registerPkgName_, ERR_DM_FAILED);
30679a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::CREATE_RESULT, ERR_DM_FAILED, "");
30779a732c7Sopenharmony_ci        session_->CloseSessionServer(sessionId_);
30879a732c7Sopenharmony_ci        sessionId_ = SESSION_ID_INVALID;
30979a732c7Sopenharmony_ci        destroyState_ = STATE_REMOTE_WRONG;
31079a732c7Sopenharmony_ci    }
31179a732c7Sopenharmony_ci}
31279a732c7Sopenharmony_ci
31379a732c7Sopenharmony_civoid PinHolder::ProcessDestroyMsg(const std::string &message)
31479a732c7Sopenharmony_ci{
31579a732c7Sopenharmony_ci    if (listener_ == nullptr || session_ == nullptr) {
31679a732c7Sopenharmony_ci        LOGE("ProcessDestroyMsg listener or session is nullptr.");
31779a732c7Sopenharmony_ci        return;
31879a732c7Sopenharmony_ci    }
31979a732c7Sopenharmony_ci    nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
32079a732c7Sopenharmony_ci    if (jsonObject.is_discarded()) {
32179a732c7Sopenharmony_ci        LOGE("ProcessDestroyMsg DecodeRequest jsonStr error.");
32279a732c7Sopenharmony_ci        return;
32379a732c7Sopenharmony_ci    }
32479a732c7Sopenharmony_ci    if (!IsInt32(jsonObject, TAG_PIN_TYPE) || !IsString(jsonObject, TAG_PAYLOAD)) {
32579a732c7Sopenharmony_ci        LOGE("ProcessDestroyMsg err json string.");
32679a732c7Sopenharmony_ci        return;
32779a732c7Sopenharmony_ci    }
32879a732c7Sopenharmony_ci    DmPinType pinType = static_cast<DmPinType>(jsonObject[TAG_PIN_TYPE].get<int32_t>());
32979a732c7Sopenharmony_ci    std::string payload = jsonObject[TAG_PAYLOAD].get<std::string>();
33079a732c7Sopenharmony_ci    int32_t bizStage = static_cast<int32_t>(PinHolderStage::RECEIVE_DESTROY_PIN_HOLDER_MSG);
33179a732c7Sopenharmony_ci    DmRadarHelper::GetInstance().ReportSendOrReceiveHolderMsg(bizStage, std::string("ProcessDestroyMsg"), "");
33279a732c7Sopenharmony_ci    nlohmann::json jsonObj;
33379a732c7Sopenharmony_ci    jsonObj[TAG_MSG_TYPE] = MSG_TYPE_DESTROY_PIN_HOLDER_RESP;
33479a732c7Sopenharmony_ci    if (sinkState_ != SINK_CREATE) {
33579a732c7Sopenharmony_ci        jsonObj[TAG_REPLY] = REPLY_FAILED;
33679a732c7Sopenharmony_ci    } else {
33779a732c7Sopenharmony_ci        jsonObj[TAG_REPLY] = REPLY_SUCCESS;
33879a732c7Sopenharmony_ci        sinkState_ = SINK_INIT;
33979a732c7Sopenharmony_ci        sourceState_ = SOURCE_INIT;
34079a732c7Sopenharmony_ci        if (!isDestroy_.load()) {
34179a732c7Sopenharmony_ci            listener_->OnPinHolderDestroy(registerPkgName_, pinType, payload);
34279a732c7Sopenharmony_ci            nlohmann::json jsonContent;
34379a732c7Sopenharmony_ci            jsonContent[TAG_PIN_TYPE] = pinType;
34479a732c7Sopenharmony_ci            jsonContent[TAG_PAYLOAD] = payload;
34579a732c7Sopenharmony_ci            std::string content = jsonContent.dump();
34679a732c7Sopenharmony_ci            listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, DM_OK, content);
34779a732c7Sopenharmony_ci            isDestroy_.store(true);
34879a732c7Sopenharmony_ci        }
34979a732c7Sopenharmony_ci    }
35079a732c7Sopenharmony_ci
35179a732c7Sopenharmony_ci    std::string msg = jsonObj.dump();
35279a732c7Sopenharmony_ci    LOGI("ProcessDestroyMsg, message type is: %{public}d.", MSG_TYPE_DESTROY_PIN_HOLDER_RESP);
35379a732c7Sopenharmony_ci    int32_t ret = session_->SendData(sessionId_, msg);
35479a732c7Sopenharmony_ci    if (ret != DM_OK) {
35579a732c7Sopenharmony_ci        LOGE("[SOFTBUS]SendBytes failed, ret: %{public}d.", ret);
35679a732c7Sopenharmony_ci        return;
35779a732c7Sopenharmony_ci    }
35879a732c7Sopenharmony_ci}
35979a732c7Sopenharmony_ci
36079a732c7Sopenharmony_civoid PinHolder::CloseSession(const std::string &name)
36179a732c7Sopenharmony_ci{
36279a732c7Sopenharmony_ci    LOGI("PinHolder::CloseSession start timer name %{public}s.", name.c_str());
36379a732c7Sopenharmony_ci    if (session_ == nullptr) {
36479a732c7Sopenharmony_ci        LOGE("CloseSession session is nullptr.");
36579a732c7Sopenharmony_ci        return;
36679a732c7Sopenharmony_ci    }
36779a732c7Sopenharmony_ci    nlohmann::json jsonObj;
36879a732c7Sopenharmony_ci    jsonObj[DM_CONNECTION_DISCONNECTED] = true;
36979a732c7Sopenharmony_ci    std::string payload = jsonObj.dump();
37079a732c7Sopenharmony_ci    if (listener_ != nullptr && !isDestroy_.load()) {
37179a732c7Sopenharmony_ci        listener_->OnPinHolderDestroy(registerPkgName_, pinType_, payload);
37279a732c7Sopenharmony_ci        nlohmann::json jsonContent;
37379a732c7Sopenharmony_ci        jsonContent[TAG_PIN_TYPE] = pinType_;
37479a732c7Sopenharmony_ci        jsonContent[TAG_PAYLOAD] = payload;
37579a732c7Sopenharmony_ci        std::string content = jsonContent.dump();
37679a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, ERR_DM_TIME_OUT, content);
37779a732c7Sopenharmony_ci        isDestroy_.store(true);
37879a732c7Sopenharmony_ci    }
37979a732c7Sopenharmony_ci    session_->CloseSessionServer(sessionId_);
38079a732c7Sopenharmony_ci    timer_->DeleteAll();
38179a732c7Sopenharmony_ci    destroyState_ = STATE_TIME_OUT;
38279a732c7Sopenharmony_ci    sessionId_ = SESSION_ID_INVALID;
38379a732c7Sopenharmony_ci    sinkState_ = SINK_INIT;
38479a732c7Sopenharmony_ci    sourceState_ = SOURCE_INIT;
38579a732c7Sopenharmony_ci    remoteDeviceId_ = "";
38679a732c7Sopenharmony_ci    isRemoteSupported_ = false;
38779a732c7Sopenharmony_ci}
38879a732c7Sopenharmony_ci
38979a732c7Sopenharmony_civoid PinHolder::ProcessDestroyResMsg(const std::string &message)
39079a732c7Sopenharmony_ci{
39179a732c7Sopenharmony_ci    nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
39279a732c7Sopenharmony_ci    if (jsonObject.is_discarded()) {
39379a732c7Sopenharmony_ci        LOGE("ProcessDestroyResMsg DecodeRequest jsonStr error.");
39479a732c7Sopenharmony_ci        return;
39579a732c7Sopenharmony_ci    }
39679a732c7Sopenharmony_ci    if (!IsInt32(jsonObject, TAG_REPLY)) {
39779a732c7Sopenharmony_ci        LOGE("ProcessDestroyResMsg err json string.");
39879a732c7Sopenharmony_ci        return;
39979a732c7Sopenharmony_ci    }
40079a732c7Sopenharmony_ci    int32_t reply = jsonObject[TAG_REPLY].get<int32_t>();
40179a732c7Sopenharmony_ci    if (listener_ == nullptr || session_ == nullptr) {
40279a732c7Sopenharmony_ci        LOGE("ProcessDestroyResMsg listener or session is nullptr.");
40379a732c7Sopenharmony_ci        return;
40479a732c7Sopenharmony_ci    }
40579a732c7Sopenharmony_ci    if (reply == REPLY_SUCCESS) {
40679a732c7Sopenharmony_ci        listener_->OnDestroyResult(registerPkgName_, DM_OK);
40779a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY_RESULT, DM_OK, "");
40879a732c7Sopenharmony_ci        sourceState_ = SOURCE_INIT;
40979a732c7Sopenharmony_ci        sinkState_ = SINK_INIT;
41079a732c7Sopenharmony_ci        timer_->DeleteAll();
41179a732c7Sopenharmony_ci    } else {
41279a732c7Sopenharmony_ci        LOGE("ProcessDestroyResMsg remote state is wrong.");
41379a732c7Sopenharmony_ci        listener_->OnDestroyResult(registerPkgName_, ERR_DM_FAILED);
41479a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY_RESULT, ERR_DM_FAILED, "");
41579a732c7Sopenharmony_ci    }
41679a732c7Sopenharmony_ci    session_->CloseSessionServer(sessionId_);
41779a732c7Sopenharmony_ci    sessionId_ = SESSION_ID_INVALID;
41879a732c7Sopenharmony_ci    remoteDeviceId_ = "";
41979a732c7Sopenharmony_ci}
42079a732c7Sopenharmony_ci
42179a732c7Sopenharmony_civoid PinHolder::OnDataReceived(int32_t sessionId, std::string message)
42279a732c7Sopenharmony_ci{
42379a732c7Sopenharmony_ci    int32_t msgType = ParseMsgType(message);
42479a732c7Sopenharmony_ci    LOGI("OnDataReceived, msgType: %{public}d.", msgType);
42579a732c7Sopenharmony_ci
42679a732c7Sopenharmony_ci    switch (msgType) {
42779a732c7Sopenharmony_ci        case MSG_TYPE_CREATE_PIN_HOLDER:
42879a732c7Sopenharmony_ci            ProcessCreateMsg(message);
42979a732c7Sopenharmony_ci            break;
43079a732c7Sopenharmony_ci        case MSG_TYPE_CREATE_PIN_HOLDER_RESP:
43179a732c7Sopenharmony_ci            ProcessCreateRespMsg(message);
43279a732c7Sopenharmony_ci            break;
43379a732c7Sopenharmony_ci        case MSG_TYPE_DESTROY_PIN_HOLDER:
43479a732c7Sopenharmony_ci            ProcessDestroyMsg(message);
43579a732c7Sopenharmony_ci            break;
43679a732c7Sopenharmony_ci        case MSG_TYPE_DESTROY_PIN_HOLDER_RESP:
43779a732c7Sopenharmony_ci            ProcessDestroyResMsg(message);
43879a732c7Sopenharmony_ci            break;
43979a732c7Sopenharmony_ci        case MSG_TYPE_PIN_HOLDER_CHANGE:
44079a732c7Sopenharmony_ci            ProcessChangeMsg(message);
44179a732c7Sopenharmony_ci            break;
44279a732c7Sopenharmony_ci        case MSG_TYPE_PIN_HOLDER_CHANGE_RESP:
44379a732c7Sopenharmony_ci            ProcessChangeRespMsg(message);
44479a732c7Sopenharmony_ci            break;
44579a732c7Sopenharmony_ci        default:
44679a732c7Sopenharmony_ci            break;
44779a732c7Sopenharmony_ci    }
44879a732c7Sopenharmony_ci}
44979a732c7Sopenharmony_ci
45079a732c7Sopenharmony_civoid PinHolder::GetPeerDeviceId(int32_t sessionId, std::string &udidHash)
45179a732c7Sopenharmony_ci{
45279a732c7Sopenharmony_ci    char peerDeviceId[DEVICE_UUID_LENGTH] = {0};
45379a732c7Sopenharmony_ci    int32_t ret = ::GetPeerDeviceId(sessionId, &peerDeviceId[0], DEVICE_UUID_LENGTH);
45479a732c7Sopenharmony_ci    if (ret != DM_OK) {
45579a732c7Sopenharmony_ci        LOGE("[SOFTBUS]GetPeerDeviceId failed for session: %{public}d.", sessionId);
45679a732c7Sopenharmony_ci        udidHash = "";
45779a732c7Sopenharmony_ci        return;
45879a732c7Sopenharmony_ci    }
45979a732c7Sopenharmony_ci    std::string deviceId = peerDeviceId;
46079a732c7Sopenharmony_ci    char udidHashTmp[DM_MAX_DEVICE_ID_LEN] = {0};
46179a732c7Sopenharmony_ci    if (Crypto::GetUdidHash(deviceId, reinterpret_cast<uint8_t *>(udidHashTmp)) != DM_OK) {
46279a732c7Sopenharmony_ci        LOGE("get udidhash by udid: %{public}s failed.", GetAnonyString(deviceId).c_str());
46379a732c7Sopenharmony_ci        udidHash = "";
46479a732c7Sopenharmony_ci        return;
46579a732c7Sopenharmony_ci    }
46679a732c7Sopenharmony_ci    udidHash = udidHashTmp;
46779a732c7Sopenharmony_ci    LOGI("GetPeerDeviceId udid hash: %{public}s success.", GetAnonyString(udidHash).c_str());
46879a732c7Sopenharmony_ci}
46979a732c7Sopenharmony_ci
47079a732c7Sopenharmony_civoid PinHolder::OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result)
47179a732c7Sopenharmony_ci{
47279a732c7Sopenharmony_ci    isDestroy_.store(false);
47379a732c7Sopenharmony_ci    destroyState_ = STATE_UNKNOW;
47479a732c7Sopenharmony_ci    char peerDeviceId[DEVICE_UUID_LENGTH] = {0};
47579a732c7Sopenharmony_ci    int32_t ret = ::GetPeerDeviceId(sessionId, &peerDeviceId[0], DEVICE_UUID_LENGTH);
47679a732c7Sopenharmony_ci    if (ret != DM_OK) {
47779a732c7Sopenharmony_ci        LOGE("[SOFTBUS]GetPeerDeviceId failed for session: %{public}d.", sessionId);
47879a732c7Sopenharmony_ci    }
47979a732c7Sopenharmony_ci    LOGI("OnSessionOpened, peerDeviceId: %{public}s.", GetAnonyString(peerDeviceId).c_str());
48079a732c7Sopenharmony_ci    DmRadarHelper::GetInstance().ReportSendOrReceiveHolderMsg(static_cast<int32_t>(PinHolderStage::SESSION_OPENED),
48179a732c7Sopenharmony_ci        std::string("OnSessionOpened"), std::string(peerDeviceId));
48279a732c7Sopenharmony_ci    sessionId_ = sessionId;
48379a732c7Sopenharmony_ci    if (sessionSide == SESSION_SIDE_SERVER) {
48479a732c7Sopenharmony_ci        LOGI("[SOFTBUS]onSesssionOpened success, side is sink. sessionId: %{public}d.", sessionId);
48579a732c7Sopenharmony_ci        GetPeerDeviceId(sessionId, remoteDeviceId_);
48679a732c7Sopenharmony_ci        return;
48779a732c7Sopenharmony_ci    }
48879a732c7Sopenharmony_ci    if (result == DM_OK) {
48979a732c7Sopenharmony_ci        CreateGeneratePinHolderMsg();
49079a732c7Sopenharmony_ci        return;
49179a732c7Sopenharmony_ci    }
49279a732c7Sopenharmony_ci    LOGE("[SOFTBUS]onSesssionOpened failed. sessionId: %{public}d.", sessionId);
49379a732c7Sopenharmony_ci    sessionId_ = SESSION_ID_INVALID;
49479a732c7Sopenharmony_ci    if (listener_ != nullptr) {
49579a732c7Sopenharmony_ci        listener_->OnCreateResult(registerPkgName_, result);
49679a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::CREATE_RESULT, result, "");
49779a732c7Sopenharmony_ci    }
49879a732c7Sopenharmony_ci    return;
49979a732c7Sopenharmony_ci}
50079a732c7Sopenharmony_ci
50179a732c7Sopenharmony_civoid PinHolder::OnSessionClosed(int32_t sessionId)
50279a732c7Sopenharmony_ci{
50379a732c7Sopenharmony_ci    LOGI("[SOFTBUS]OnSessionClosed sessionId: %{public}d.", sessionId);
50479a732c7Sopenharmony_ci    sessionId_ = SESSION_ID_INVALID;
50579a732c7Sopenharmony_ci    sinkState_ = SINK_INIT;
50679a732c7Sopenharmony_ci    sourceState_ = SOURCE_INIT;
50779a732c7Sopenharmony_ci    remoteDeviceId_ = "";
50879a732c7Sopenharmony_ci    isRemoteSupported_ = false;
50979a732c7Sopenharmony_ci    nlohmann::json jsonObj;
51079a732c7Sopenharmony_ci    jsonObj[DM_CONNECTION_DISCONNECTED] = true;
51179a732c7Sopenharmony_ci    std::string payload = jsonObj.dump();
51279a732c7Sopenharmony_ci    if (listener_ != nullptr && !isDestroy_.load()) {
51379a732c7Sopenharmony_ci        listener_->OnPinHolderDestroy(registerPkgName_, pinType_, payload);
51479a732c7Sopenharmony_ci        nlohmann::json jsonContent;
51579a732c7Sopenharmony_ci        jsonContent[TAG_PIN_TYPE] = pinType_;
51679a732c7Sopenharmony_ci        jsonContent[TAG_PAYLOAD] = payload;
51779a732c7Sopenharmony_ci        std::string content = jsonContent.dump();
51879a732c7Sopenharmony_ci        if (destroyState_ == STATE_UNKNOW) {
51979a732c7Sopenharmony_ci            listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, sessionId, content);
52079a732c7Sopenharmony_ci        } else if (destroyState_ == STATE_REMOTE_WRONG) {
52179a732c7Sopenharmony_ci            listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, ERR_DM_FAILED, content);
52279a732c7Sopenharmony_ci        } else if (destroyState_ == STATE_TIME_OUT) {
52379a732c7Sopenharmony_ci            listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, ERR_DM_TIME_OUT, content);
52479a732c7Sopenharmony_ci        }
52579a732c7Sopenharmony_ci        isDestroy_.store(true);
52679a732c7Sopenharmony_ci    }
52779a732c7Sopenharmony_ci    if (timer_ != nullptr) {
52879a732c7Sopenharmony_ci        timer_->DeleteAll();
52979a732c7Sopenharmony_ci    }
53079a732c7Sopenharmony_ci    return;
53179a732c7Sopenharmony_ci}
53279a732c7Sopenharmony_ci
53379a732c7Sopenharmony_ciint32_t PinHolder::CheckTargetIdVaild(const PeerTargetId &targetId)
53479a732c7Sopenharmony_ci{
53579a732c7Sopenharmony_ci    if (targetId.deviceId.empty() && targetId.brMac.empty() && targetId.bleMac.empty() && targetId.wifiIp.empty()) {
53679a732c7Sopenharmony_ci        LOGE("CheckTargetIdVaild failed. targetId is empty.");
53779a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
53879a732c7Sopenharmony_ci    }
53979a732c7Sopenharmony_ci    return DM_OK;
54079a732c7Sopenharmony_ci}
54179a732c7Sopenharmony_ci
54279a732c7Sopenharmony_ciint32_t PinHolder::NotifyPinHolderEvent(const std::string &pkgName, const std::string &event)
54379a732c7Sopenharmony_ci{
54479a732c7Sopenharmony_ci    LOGI("NotifyPinHolderEvent.");
54579a732c7Sopenharmony_ci    if (listener_ == nullptr || session_ == nullptr) {
54679a732c7Sopenharmony_ci        LOGE("NotifyPinHolderEvent listener or session is nullptr.");
54779a732c7Sopenharmony_ci        return ERR_DM_FAILED;
54879a732c7Sopenharmony_ci    }
54979a732c7Sopenharmony_ci    if (registerPkgName_.empty() || pkgName != registerPkgName_ || event.empty()) {
55079a732c7Sopenharmony_ci        LOGE("NotifyPinHolderEvent pkgName: %{public}s is not register callback.", pkgName.c_str());
55179a732c7Sopenharmony_ci        return ERR_DM_FAILED;
55279a732c7Sopenharmony_ci    }
55379a732c7Sopenharmony_ci    if (sessionId_ == SESSION_ID_INVALID) {
55479a732c7Sopenharmony_ci        LOGE("NotifyPinHolderEvent session invalid.");
55579a732c7Sopenharmony_ci        return ERR_DM_FAILED;
55679a732c7Sopenharmony_ci    }
55779a732c7Sopenharmony_ci    if (!isRemoteSupported_) {
55879a732c7Sopenharmony_ci        LOGE("NotifyPinHolderEvent failed, remote not support.");
55979a732c7Sopenharmony_ci        return ERR_DM_BIND_PEER_UNSUPPORTED;
56079a732c7Sopenharmony_ci    }
56179a732c7Sopenharmony_ci    nlohmann::json jsonObject = nlohmann::json::parse(event, nullptr, false);
56279a732c7Sopenharmony_ci    if (jsonObject.is_discarded() || !IsInt32(jsonObject, TAG_PIN_TYPE)) {
56379a732c7Sopenharmony_ci        LOGE("ProcessChangeMsg DecodeRequest jsonStr error.");
56479a732c7Sopenharmony_ci        return ERR_DM_FAILED;
56579a732c7Sopenharmony_ci    }
56679a732c7Sopenharmony_ci    timer_->DeleteAll();
56779a732c7Sopenharmony_ci    timer_->StartTimer(std::string(PINHOLDER_CREATE_TIMEOUT_TASK), PIN_HOLDER_SESSION_CREATE_TIMEOUT,
56879a732c7Sopenharmony_ci        [this] (std::string name) {
56979a732c7Sopenharmony_ci            PinHolder::CloseSession(name);
57079a732c7Sopenharmony_ci        });
57179a732c7Sopenharmony_ci    DmPinType pinType = static_cast<DmPinType>(jsonObject[TAG_PIN_TYPE].get<int32_t>());
57279a732c7Sopenharmony_ci    nlohmann::json jsonObj;
57379a732c7Sopenharmony_ci    jsonObj[TAG_MSG_TYPE] = MSG_TYPE_PIN_HOLDER_CHANGE;
57479a732c7Sopenharmony_ci    jsonObj[TAG_PIN_TYPE] = pinType;
57579a732c7Sopenharmony_ci    std::string message = jsonObj.dump();
57679a732c7Sopenharmony_ci    LOGI("NotifyPinHolderEvent, message type is: %{public}d, pin type is: %{public}d.",
57779a732c7Sopenharmony_ci        MSG_TYPE_PIN_HOLDER_CHANGE, pinType);
57879a732c7Sopenharmony_ci    int32_t ret = session_->SendData(sessionId_, message);
57979a732c7Sopenharmony_ci    if (ret != DM_OK) {
58079a732c7Sopenharmony_ci        LOGE("[SOFTBUS]SendBytes failed, ret: %{public}d.", ret);
58179a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::PIN_TYPE_CHANGE_RESULT, ERR_DM_FAILED, "");
58279a732c7Sopenharmony_ci        return ERR_DM_FAILED;
58379a732c7Sopenharmony_ci    }
58479a732c7Sopenharmony_ci    return ret;
58579a732c7Sopenharmony_ci}
58679a732c7Sopenharmony_ci
58779a732c7Sopenharmony_civoid PinHolder::ProcessChangeMsg(const std::string &message)
58879a732c7Sopenharmony_ci{
58979a732c7Sopenharmony_ci    if (listener_ == nullptr || session_ == nullptr) {
59079a732c7Sopenharmony_ci        LOGE("ProcessChangeMsg listener or session is nullptr.");
59179a732c7Sopenharmony_ci        return;
59279a732c7Sopenharmony_ci    }
59379a732c7Sopenharmony_ci    nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
59479a732c7Sopenharmony_ci    if (jsonObject.is_discarded()) {
59579a732c7Sopenharmony_ci        LOGE("ProcessChangeMsg DecodeRequest jsonStr error.");
59679a732c7Sopenharmony_ci        return;
59779a732c7Sopenharmony_ci    }
59879a732c7Sopenharmony_ci    if (!IsInt32(jsonObject, TAG_PIN_TYPE)) {
59979a732c7Sopenharmony_ci        LOGE("ProcessChangeMsg err json string.");
60079a732c7Sopenharmony_ci        return;
60179a732c7Sopenharmony_ci    }
60279a732c7Sopenharmony_ci    DmPinType pinType = static_cast<DmPinType>(jsonObject[TAG_PIN_TYPE].get<int32_t>());
60379a732c7Sopenharmony_ci
60479a732c7Sopenharmony_ci    nlohmann::json jsonObj;
60579a732c7Sopenharmony_ci    jsonObj[TAG_MSG_TYPE] = MSG_TYPE_PIN_HOLDER_CHANGE_RESP;
60679a732c7Sopenharmony_ci    if (sinkState_ != SINK_CREATE) {
60779a732c7Sopenharmony_ci        jsonObj[TAG_REPLY] = REPLY_FAILED;
60879a732c7Sopenharmony_ci    } else {
60979a732c7Sopenharmony_ci        jsonObj[TAG_REPLY] = REPLY_SUCCESS;
61079a732c7Sopenharmony_ci        nlohmann::json jsonContent;
61179a732c7Sopenharmony_ci        jsonContent[TAG_PIN_TYPE] = pinType;
61279a732c7Sopenharmony_ci        std::string content = jsonContent.dump();
61379a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::PIN_TYPE_CHANGE, DM_OK, content);
61479a732c7Sopenharmony_ci        timer_->DeleteAll();
61579a732c7Sopenharmony_ci        timer_->StartTimer(std::string(PINHOLDER_CREATE_TIMEOUT_TASK), PIN_HOLDER_SESSION_CREATE_TIMEOUT,
61679a732c7Sopenharmony_ci            [this] (std::string name) {
61779a732c7Sopenharmony_ci                PinHolder::CloseSession(name);
61879a732c7Sopenharmony_ci            });
61979a732c7Sopenharmony_ci    }
62079a732c7Sopenharmony_ci
62179a732c7Sopenharmony_ci    std::string msg = jsonObj.dump();
62279a732c7Sopenharmony_ci    LOGI("ProcessChangeMsg, message type is: %{public}d.", MSG_TYPE_PIN_HOLDER_CHANGE_RESP);
62379a732c7Sopenharmony_ci    int32_t ret = session_->SendData(sessionId_, msg);
62479a732c7Sopenharmony_ci    if (ret != DM_OK) {
62579a732c7Sopenharmony_ci        LOGE("[SOFTBUS]SendBytes failed, ret: %{public}d.", ret);
62679a732c7Sopenharmony_ci        return;
62779a732c7Sopenharmony_ci    }
62879a732c7Sopenharmony_ci}
62979a732c7Sopenharmony_ci
63079a732c7Sopenharmony_civoid PinHolder::ProcessChangeRespMsg(const std::string &message)
63179a732c7Sopenharmony_ci{
63279a732c7Sopenharmony_ci    nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
63379a732c7Sopenharmony_ci    if (jsonObject.is_discarded()) {
63479a732c7Sopenharmony_ci        LOGE("ProcessChangeRespMsg DecodeRequest jsonStr error.");
63579a732c7Sopenharmony_ci        return;
63679a732c7Sopenharmony_ci    }
63779a732c7Sopenharmony_ci    if (!IsInt32(jsonObject, TAG_REPLY)) {
63879a732c7Sopenharmony_ci        LOGE("ProcessChangeRespMsg err json string.");
63979a732c7Sopenharmony_ci        return;
64079a732c7Sopenharmony_ci    }
64179a732c7Sopenharmony_ci    int32_t reply = jsonObject[TAG_REPLY].get<int32_t>();
64279a732c7Sopenharmony_ci    if (listener_ == nullptr || session_ == nullptr) {
64379a732c7Sopenharmony_ci        LOGE("ProcessChangeRespMsg listener or session is nullptr.");
64479a732c7Sopenharmony_ci        return;
64579a732c7Sopenharmony_ci    }
64679a732c7Sopenharmony_ci    if (reply == REPLY_SUCCESS) {
64779a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::PIN_TYPE_CHANGE_RESULT, DM_OK, "");
64879a732c7Sopenharmony_ci    } else {
64979a732c7Sopenharmony_ci        LOGE("ProcessChangeRespMsg remote state is wrong.");
65079a732c7Sopenharmony_ci        listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::PIN_TYPE_CHANGE_RESULT, ERR_DM_FAILED, "");
65179a732c7Sopenharmony_ci    }
65279a732c7Sopenharmony_ci}
65379a732c7Sopenharmony_ci} // namespace DistributedHardware
65479a732c7Sopenharmony_ci} // namespace OHOS