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#ifndef OHOS_PIN_HOLDER_H 1779a732c7Sopenharmony_ci#define OHOS_PIN_HOLDER_H 1879a732c7Sopenharmony_ci 1979a732c7Sopenharmony_ci#include "dm_timer.h" 2079a732c7Sopenharmony_ci#include "idevice_manager_service_listener.h" 2179a732c7Sopenharmony_ci#include "pin_holder_session.h" 2279a732c7Sopenharmony_ci#include "pinholder_session_callback.h" 2379a732c7Sopenharmony_ci 2479a732c7Sopenharmony_ci#include <atomic> 2579a732c7Sopenharmony_ci#include <map> 2679a732c7Sopenharmony_ci#include <mutex> 2779a732c7Sopenharmony_ci 2879a732c7Sopenharmony_cinamespace OHOS { 2979a732c7Sopenharmony_cinamespace DistributedHardware { 3079a732c7Sopenharmony_citypedef enum PinHolderState { 3179a732c7Sopenharmony_ci SOURCE_INIT = 1, 3279a732c7Sopenharmony_ci SOURCE_CREATE, 3379a732c7Sopenharmony_ci SOURCE_DESTROY, 3479a732c7Sopenharmony_ci SINK_INIT = 20, 3579a732c7Sopenharmony_ci SINK_CREATE, 3679a732c7Sopenharmony_ci SINK_DESTROY, 3779a732c7Sopenharmony_ci} PinHolderState; 3879a732c7Sopenharmony_ci 3979a732c7Sopenharmony_citypedef enum DestroyState { 4079a732c7Sopenharmony_ci STATE_UNKNOW = 0x0, 4179a732c7Sopenharmony_ci STATE_REMOTE_WRONG = 0x1, 4279a732c7Sopenharmony_ci STATE_TIME_OUT = 0x2, 4379a732c7Sopenharmony_ci} DestroyState; 4479a732c7Sopenharmony_ci 4579a732c7Sopenharmony_ciclass PinHolder final : public IPinholderSessionCallback, 4679a732c7Sopenharmony_ci public std::enable_shared_from_this<PinHolder> { 4779a732c7Sopenharmony_cipublic: 4879a732c7Sopenharmony_ci PinHolder(std::shared_ptr<IDeviceManagerServiceListener> listener); 4979a732c7Sopenharmony_ci ~PinHolder(); 5079a732c7Sopenharmony_ci int32_t RegisterPinHolderCallback(const std::string &pkgName); 5179a732c7Sopenharmony_ci int32_t CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId, 5279a732c7Sopenharmony_ci DmPinType pinType, const std::string &payload); 5379a732c7Sopenharmony_ci int32_t DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId, 5479a732c7Sopenharmony_ci DmPinType pinType, const std::string &payload); 5579a732c7Sopenharmony_ci int32_t NotifyPinHolderEvent(const std::string &pkgName, const std::string &event); 5679a732c7Sopenharmony_cipublic: 5779a732c7Sopenharmony_ci void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result); 5879a732c7Sopenharmony_ci void OnSessionClosed(int32_t sessionId); 5979a732c7Sopenharmony_ci void OnDataReceived(int32_t sessionId, std::string message); 6079a732c7Sopenharmony_ci 6179a732c7Sopenharmony_ciprivate: 6279a732c7Sopenharmony_ci int32_t CreateGeneratePinHolderMsg(); 6379a732c7Sopenharmony_ci int32_t ParseMsgType(const std::string &message); 6479a732c7Sopenharmony_ci void ProcessCreateMsg(const std::string &message); 6579a732c7Sopenharmony_ci void ProcessCreateRespMsg(const std::string &message); 6679a732c7Sopenharmony_ci void ProcessDestroyMsg(const std::string &message); 6779a732c7Sopenharmony_ci void ProcessDestroyResMsg(const std::string &message); 6879a732c7Sopenharmony_ci void ProcessChangeMsg(const std::string &message); 6979a732c7Sopenharmony_ci void ProcessChangeRespMsg(const std::string &message); 7079a732c7Sopenharmony_ci void CloseSession(const std::string &name); 7179a732c7Sopenharmony_ci void GetPeerDeviceId(int32_t sessionId, std::string &udidHash); 7279a732c7Sopenharmony_ci int32_t CheckTargetIdVaild(const PeerTargetId &targetId); 7379a732c7Sopenharmony_ciprivate: 7479a732c7Sopenharmony_ci std::shared_ptr<IDeviceManagerServiceListener> listener_ = nullptr; 7579a732c7Sopenharmony_ci std::shared_ptr<PinHolderSession> session_ = nullptr; 7679a732c7Sopenharmony_ci std::shared_ptr<DmTimer> timer_ = nullptr; 7779a732c7Sopenharmony_ci 7879a732c7Sopenharmony_ci std::string registerPkgName_ = ""; 7979a732c7Sopenharmony_ci std::string remoteDeviceId_ = ""; 8079a732c7Sopenharmony_ci std::string payload_ = ""; 8179a732c7Sopenharmony_ci DmPinType pinType_ = NUMBER_PIN_CODE; 8279a732c7Sopenharmony_ci PinHolderState sinkState_; 8379a732c7Sopenharmony_ci PinHolderState sourceState_; 8479a732c7Sopenharmony_ci int32_t sessionId_ = -1; 8579a732c7Sopenharmony_ci bool isRemoteSupported_ = false; 8679a732c7Sopenharmony_ci std::atomic<bool> isDestroy_ {false}; 8779a732c7Sopenharmony_ci DestroyState destroyState_ = STATE_UNKNOW; 8879a732c7Sopenharmony_ci}; 8979a732c7Sopenharmony_ci} 9079a732c7Sopenharmony_ci} 9179a732c7Sopenharmony_ci#endif // OHOS_PIN_HOLDER_H