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 AUTH_NEGOTIATE_CHANNEL_H
17 #define AUTH_NEGOTIATE_CHANNEL_H
18 
19 #include <future>
20 #include <map>
21 #include <mutex>
22 #include <memory>
23 
24 #include "timer.h"
25 #include "auth_interface.h"
26 #include "softbus_common.h"
27 
28 #include "wifi_direct_initiator.h"
29 #include "channel/negotiate_channel.h"
30 
31 namespace OHOS::SoftBus {
32 struct AuthOpenEvent {
33     int reason_;
34     AuthHandle handle_;
35 };
36 
37 struct AuthExceptionEvent {
38     int32_t error_;
39     AuthHandle handle_;
40 };
41 
42 class AuthNegotiateChannel : public NegotiateChannel, public std::enable_shared_from_this<AuthNegotiateChannel> {
43 public:
44     struct OpenParam {
45         AuthLinkType type;
46         std::string remoteUuid;
47         std::string remoteIp;
48         int remotePort;
49         ListenerModule module;
50     };
51 
52     static void Init();
53     static std::pair<int, ListenerModule> StartListening(AuthLinkType type, const std::string &localIp, int port);
54     static void StopListening(AuthLinkType type, ListenerModule module);
55     static int OpenConnection(const OpenParam &param, const std::shared_ptr<AuthNegotiateChannel> &channel);
56     static void StopCustomListening();
57 
58     static void ProcessDetectLinkRequest(const std::shared_ptr<AuthNegotiateChannel> &channel);
59     static void ProcessDetectLinkResponse(AuthHandle handle, const NegotiateMessage &response);
60     void OnWaitDetectResponseTimeout();
61 
62     explicit AuthNegotiateChannel(const AuthHandle &handle);
63     ~AuthNegotiateChannel() override;
64 
65     bool operator==(const AuthNegotiateChannel &other) const;
66     bool IsMeta() const;
67     void SetClose();
68 
69     int SendMessage(const NegotiateMessage &msg) const override;
70     NegotiateMessage SendMessageAndWaitResponse(const NegotiateMessage &msg);
71     std::string GetRemoteDeviceId() const override;
72 
73 private:
74     static void OnConnOpened(uint32_t requestId, AuthHandle authHandle);
75     static void OnConnOpenFailed(uint32_t requestId, int32_t reason);
76 
77     class Initiator {
78     public:
Initiator()79         Initiator()
80         {
81             WifiDirectInitiator::GetInstance().Add(AuthNegotiateChannel::Init);
82         }
83     };
84 
85     static inline Initiator initiator_;
86     static inline std::recursive_mutex lock_;
87     static inline std::map<uint32_t, std::string> requestIdToDeviceIdMap_;
88 
89     static inline std::recursive_mutex channelLock_;
90     static inline std::map<int64_t, std::shared_ptr<AuthNegotiateChannel>> authIdToChannelMap_;
91     static Utils::Timer timer_;
92 
93     AuthHandle handle_;
94     std::shared_ptr<std::promise<NegotiateMessage>> promise_;
95     uint32_t timerId_;
96     bool close_;
97 };
98 } // namespace OHOS::SoftBus
99 #endif
100