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