• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int32_t UnRegisterPinHolderCallback(const std::string &pkgName);
52 public:
53     void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result);
54     void OnSessionClosed(int32_t sessionId);
55     void OnDataReceived(int32_t sessionId, std::string message);
56 
57 private:
58     int32_t CreateGeneratePinHolderMsg();
59     int32_t ParseMsgType(const std::string &message);
60     void ProcessCloseSessionMsg(const std::string &message);
61     void ProcessCreateMsg(const std::string &message);
62     void ProcessCreateRespMsg(const std::string &message);
63     void ProcessDestroyMsg(const std::string &message);
64     void ProcessDestroyResMsg(const std::string &message);
65     void ProcessChangeMsg(const std::string &message);
66     void ProcessChangeRespMsg(const std::string &message);
67     void CloseSession(const std::string &name);
68     void GetPeerDeviceId(int32_t sessionId, std::string &udidHash);
69     int32_t CheckTargetIdVaild(const PeerTargetId &targetId);
70 private:
71     std::shared_ptr<IDeviceManagerServiceListener> listener_ = nullptr;
72     std::shared_ptr<PinHolderSession> session_ = nullptr;
73     std::shared_ptr<DmTimer> timer_ = nullptr;
74 
75     std::string remoteDeviceId_ = "";
76     std::string payload_ = "";
77     DmPinType pinType_ = NUMBER_PIN_CODE;
78     PinHolderState sinkState_;
79     PinHolderState sourceState_;
80     int32_t sessionId_ = -1;
81     bool isRemoteSupported_ = false;
82     std::atomic<bool> isDestroy_ {false};
83     DestroyState destroyState_ = STATE_UNKNOW;
84     ProcessInfo processInfo_;
85 };
86 }
87 }
88 #endif // OHOS_PIN_HOLDER_H