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 INNER_LINK_H
17 #define INNER_LINK_H
18 
19 #include <memory>
20 
21 #include "softbus_conn_interface.h"
22 
23 #include "channel/negotiate_channel.h"
24 #include "data/info_container.h"
25 #include "wifi_direct_types.h"
26 
27 namespace OHOS::SoftBus {
28 constexpr int PROTECT_DURATION_MS = 2000;
29 
30 enum class InnerLinKey {
31     LINK_TYPE = 1,
32     STATE = 2,
33     LOCAL_INTERFACE = 3,
34     LOCAL_BASE_MAC = 4,
35     LOCAL_DYNAMIC_MAC = 5,
36     LOCAL_IPV4 = 6,
37     REMOTE_INTERFACE = 7,
38     REMOTE_BASE_MAC = 8,
39     REMOTE_DYNAMIC_MAC = 9,
40     REMOTE_IPV4 = 10,
41     IS_BEING_USED_BY_LOCAL = 11,
42     IS_BEING_USED_BY_REMOTE = 12,
43     FREQUENCY = 13,
44     STATE_CHANGE_TIME = 14,
45     REMOTE_DEVICE_ID = 15,
46     NEGOTIATION_CHANNEL = 16,
47     LOCAL_PORT = 17,
48     LISTENER_MODULE_ID = 18,
49     LOCAL_IPV6 = 19,
50     REMOTE_IPV6 = 20,
51     HAS_PTK = 21,
52     CUSTOM_PORT = 22,
53     NEW_PTK_FRAME = 23,
54     LOCAL_CUSTOM_PORT = 24,
55     REMOTE_CUSTOM_PORT = 25,
56     IS_LEGACY_REUSED = 26,
57 };
58 
59 struct LinkIdStruct {
60     int id;
61     int pid;
62     uint32_t requestId;
63 };
64 
65 class InnerLink : public InfoContainer<InnerLinKey> {
66 public:
67     enum class LinkType {
68         INVALID_TYPE,
69         P2P,
70         HML,
71     };
72 
73     enum class LinkState {
74         INVALID_STATE = -1,
75         DISCONNECTED = 0,
76         CONNECTED = 1,
77         CONNECTING = 2,
78         DISCONNECTING = 3
79     };
80 
81     explicit InnerLink(const std::string &remoteMac);
82     InnerLink(LinkType type, const std::string &remoteDeviceId);
83     ~InnerLink();
84 
85     InnerLink::LinkType GetLinkType() const;
86     void SetLinkType(InnerLink::LinkType type);
87 
88     InnerLink::LinkState GetState() const;
89     void SetState(LinkState newState);
90 
91     std::string GetLocalInterface() const;
92     void SetLocalInterface(const std::string &interface);
93 
94     std::string GetLocalBaseMac() const;
95     void SetLocalBaseMac(const std::string &mac);
96 
97     std::string GetLocalDynamicMac() const;
98     void SetLocalDynamicMac(const std::string &mac);
99 
100     std::string GetLocalIpv4() const;
101     void SetLocalIpv4(const std::string &ip);
102 
103     std::string GetRemoteInterface() const;
104     void SetRemoteInterface(const std::string &interface);
105 
106     std::string GetRemoteBaseMac() const;
107     void SetRemoteBaseMac(const std::string &mac);
108 
109     std::string GetRemoteDynamicMac() const;
110     void SetRemoteDynamicMac(const std::string &mac);
111 
112     std::string GetRemoteIpv4() const;
113     void SetRemoteIpv4(const std::string &ip);
114 
115     bool IsBeingUsedByLocal() const;
116     // setter is supported implicitly by link methods below
117 
118     bool IsBeingUsedByRemote() const;
119     void SetBeingUsedByRemote(bool value);
120 
121     int GetFrequency() const;
122     void SetFrequency(int frequency);
123 
124     std::string GetRemoteDeviceId() const;
125     void SetRemoteDeviceId(const std::string &deviceId);
126 
127     std::shared_ptr<NegotiateChannel> GetNegotiateChannel() const;
128     void SetNegotiateChannel(const std::shared_ptr<NegotiateChannel> &channel);
129 
130     int GetLocalPort() const;
131     void SetLocalPort(int port);
132 
133     ListenerModule GetListenerModule() const;
134     void SetListenerModule(ListenerModule module);
135 
136     std::string GetLocalIpv6() const;
137     void SetLocalIpv6(const std::string &value);
138 
139     std::string GetRemoteIpv6() const;
140     void SetRemoteIpv6(const std::string &value);
141 
142     bool HasPtk() const;
143     void SetPtk(bool value);
144 
145     int32_t GetLocalCustomPort() const;
146     void SetLocalCustomPort(int32_t value);
147     static void StopCustomListen(int32_t localCustomPort);
148 
149     int32_t GetRemoteCustomPort() const;
150     void SetRemoteCustomPort(int32_t value);
151 
152     bool GetNewPtkFrame() const;
153     void SetNewPtkFrame(bool value);
154 
155     bool GetLegacyReused() const;
156     void SetLegacyReused(bool value);
157 
158     void GenerateLink(uint32_t requestId, int pid, WifiDirectLink &link, bool ipv4);
159     void RemoveId(int linkId);
160     bool IsContainId(int linkId) const;
161 
162     size_t GetReference() const;
163     bool IsProtected() const;
164 
165     void Dump() const;
166 
167 private:
168     void AddId(int linkId, uint32_t requestId, int pid);
169     std::map<int, std::shared_ptr<LinkIdStruct>> linkIds_;
170     std::shared_ptr<NegotiateChannel> channel_;
171 };
172 
173 using InnerLinkBasicInfo = struct {
174     bool isBeingUsedByRemote;
175     InnerLink::LinkState state;
176     InnerLink::LinkType linkType;
177     int freq;
178     std::string remoteDeviceId;
179     std::string remoteIpv4;
180     std::string remoteBaseMac;
181 };
182 } // namespace OHOS::SoftBus
183 #endif
184