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 <map> 25 #include <mutex> 26 27 namespace OHOS { 28 namespace DistributedHardware { 29 typedef enum PinHolderState { 30 SOURCE_INIT = 1, 31 SOURCE_CREATE, 32 SOURCE_DESTROY, 33 SINK_INIT = 20, 34 SINK_CREATE, 35 SINK_DESTROY, 36 } PinHolderState; 37 38 class PinHolder final : public IPinholderSessionCallback, 39 public std::enable_shared_from_this<PinHolder> { 40 public: 41 PinHolder(std::shared_ptr<IDeviceManagerServiceListener> listener); 42 ~PinHolder(); 43 int32_t RegisterPinHolderCallback(const std::string &pkgName); 44 int32_t CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId, 45 DmPinType pinType, const std::string &payload); 46 int32_t DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId, 47 DmPinType pinType, const std::string &payload); 48 public: 49 void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result); 50 void OnSessionClosed(int32_t sessionId); 51 void OnDataReceived(int32_t sessionId, std::string message); 52 53 private: 54 int32_t CreateGeneratePinHolderMsg(); 55 int32_t ParseMsgType(const std::string &message); 56 void ProcessCreateMsg(const std::string &message); 57 void ProcessCreateRespMsg(const std::string &message); 58 void ProcessDestroyMsg(const std::string &message); 59 void ProcessDestroyResMsg(const std::string &message); 60 void CloseSession(const std::string &name); 61 void GetPeerDeviceId(int32_t sessionId, std::string &udidHash); 62 int32_t CheckTargetIdVaild(const PeerTargetId &targetId); 63 private: 64 std::shared_ptr<IDeviceManagerServiceListener> listener_ = nullptr; 65 std::shared_ptr<PinHolderSession> session_ = nullptr; 66 std::shared_ptr<DmTimer> timer_ = nullptr; 67 68 std::string registerPkgName_ = ""; 69 std::string remoteDeviceId_ = ""; 70 std::string payload_ = ""; 71 DmPinType pinType_; 72 PinHolderState sinkState_; 73 PinHolderState sourceState_; 74 int32_t sessionId_ = -1; 75 }; 76 } 77 } 78 #endif // OHOS_PIN_HOLDER_H