1 /* 2 * Copyright (c) 2024 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 #ifndef OHOS_PIN_HOLDER_H 17 #define OHOS_PIN_HOLDER_H 18 19 #include "dm_timer.h" 20 #include "idevice_manager_service_listener.h" 21 #include "pin_holder_session.h" 22 #include "pinholder_session_callback.h" 23 24 #include <atomic> 25 #include <map> 26 #include <mutex> 27 28 namespace OHOS { 29 namespace DistributedHardware { 30 typedef enum PinHolderState { 31 SOURCE_INIT = 1, 32 SOURCE_CREATE, 33 SOURCE_DESTROY, 34 SINK_INIT = 20, 35 SINK_CREATE, 36 SINK_DESTROY, 37 } PinHolderState; 38 39 typedef enum DestroyState { 40 STATE_UNKNOW = 0x0, 41 STATE_REMOTE_WRONG = 0x1, 42 STATE_TIME_OUT = 0x2, 43 } DestroyState; 44 45 class PinHolder final : public IPinholderSessionCallback, 46 public std::enable_shared_from_this<PinHolder> { 47 public: 48 PinHolder(std::shared_ptr<IDeviceManagerServiceListener> listener); 49 ~PinHolder(); 50 int32_t RegisterPinHolderCallback(const std::string &pkgName); 51 int32_t CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId, 52 DmPinType pinType, const std::string &payload); 53 int32_t DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId, 54 DmPinType pinType, const std::string &payload); 55 int32_t NotifyPinHolderEvent(const std::string &pkgName, const std::string &event); 56 public: 57 void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result); 58 void OnSessionClosed(int32_t sessionId); 59 void OnDataReceived(int32_t sessionId, std::string message); 60 61 private: 62 int32_t CreateGeneratePinHolderMsg(); 63 int32_t ParseMsgType(const std::string &message); 64 void ProcessCloseSessionMsg(const std::string &message); 65 void ProcessCreateMsg(const std::string &message); 66 void ProcessCreateRespMsg(const std::string &message); 67 void ProcessDestroyMsg(const std::string &message); 68 void ProcessDestroyResMsg(const std::string &message); 69 void ProcessChangeMsg(const std::string &message); 70 void ProcessChangeRespMsg(const std::string &message); 71 void CloseSession(const std::string &name); 72 void GetPeerDeviceId(int32_t sessionId, std::string &udidHash); 73 int32_t CheckTargetIdVaild(const PeerTargetId &targetId); 74 private: 75 std::shared_ptr<IDeviceManagerServiceListener> listener_ = nullptr; 76 std::shared_ptr<PinHolderSession> session_ = nullptr; 77 std::shared_ptr<DmTimer> timer_ = nullptr; 78 79 std::string registerPkgName_ = ""; 80 std::string remoteDeviceId_ = ""; 81 std::string payload_ = ""; 82 DmPinType pinType_ = NUMBER_PIN_CODE; 83 PinHolderState sinkState_; 84 PinHolderState sourceState_; 85 int32_t sessionId_ = -1; 86 bool isRemoteSupported_ = false; 87 std::atomic<bool> isDestroy_ {false}; 88 DestroyState destroyState_ = STATE_UNKNOW; 89 }; 90 } 91 } 92 #endif // OHOS_PIN_HOLDER_H