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 #ifndef NEGOTIATE_MESSAGE_H
16 #define NEGOTIATE_MESSAGE_H
17 
18 #include <map>
19 #include <set>
20 #include "info_container.h"
21 #include "ipv4_info.h"
22 #include "interface_info.h"
23 #include "link_info.h"
24 
25 #include "wifi_direct_types.h"
26 
27 namespace OHOS::SoftBus {
28 enum class LegacyCommandType {
29     CMD_INVALID = -1,
30     /* v1 cmd */
31     CMD_DISCONNECT_V1_REQ = 5,
32     CMD_CONN_V1_REQ = 8,
33     CMD_CONN_V1_RESP = 9,
34     CMD_REUSE_REQ = 12,
35     CMD_CTRL_CHL_HANDSHAKE = 13,
36     CMD_GC_WIFI_CONFIG_CHANGED = 17,
37     CMD_REUSE_RESP = 19,
38 
39     CMD_PC_GET_INTERFACE_INFO_REQ = 30,
40     CMD_PC_GET_INTERFACE_INFO_RESP = 31,
41     CMD_FORCE_DISCONNECT_V1_REQ = 32,
42 };
43 
44 enum class LegacyContentType {
45     INVALID = -1,
46     GO_INFO = 1,
47     GC_INFO = 2,
48     RESULT = 3,
49 };
50 
51 enum class LegacyResult {
52     OK = 0,
53     V1_ERROR_CONNECTED_WITH_MISMATCHED_ROLE = -1,
54     V1_ERROR_IF_NOT_AVAILABLE = -25,
55 };
56 
57 enum class NegotiateMessageType {
58     CMD_INVALID = -1,
59     /* v1 cmd is defined by LegacyCommandType*/
60 
61     /* v2 cmd */
62     CMD_CONN_V2_REQ_1 = 21,
63     CMD_CONN_V2_REQ_2 = 22,
64     CMD_CONN_V2_REQ_3 = 23,
65     CMD_CONN_V2_RESP_1 = 24,
66     CMD_CONN_V2_RESP_2 = 25,
67     CMD_CONN_V2_RESP_3 = 26,
68     CMD_DISCONNECT_V2_REQ = 27,
69     CMD_DISCONNECT_V2_RESP = 28,
70     CMD_CLIENT_JOIN_FAIL_NOTIFY = 29,
71     /* 30-49 is for LegacyCommandType*/
72 
73     CMD_TRIGGER_REQ = 50,
74     CMD_TRIGGER_RESP = 51,
75     CMD_AUTH_LISTEN_RESP = 52,
76     CMD_RENEGOTIATE_REQ = 53,
77     CMD_RENEGOTIATE_RESP = 54,
78     CMD_AUTH_HAND_SHAKE = 55,
79     CMD_AUTH_HAND_SHAKE_RSP = 56,
80     CMD_DETECT_LINK_REQ = 57,
81     CMD_DETECT_LINK_RSP = 58,
82     CMD_FORCE_DISCONNECT_REQ = 59,
83 
84     CMD_V3_REQ = 100,
85     CMD_V3_RSP = 101,
86     CMD_V3_CUSTOM_PORT_REQ = 102,
87     CMD_V3_CUSTOM_PORT_RSP = 103,
88 };
89 
90 enum class NegotiateMessageKey {
91     MSG_TYPE = 0,
92     SESSION_ID = 1,
93     WIFI_CFG_INFO = 2,
94     IPV4_INFO_ARRAY = 3,
95     PREFER_LINK_MODE = 4,
96     IS_MODE_STRICT = 5,
97     PREFER_LINK_BANDWIDTH = 6,
98     IS_BRIDGE_SUPPORTED = 7,
99     LINK_INFO = 8,
100     RESULT_CODE = 9,
101     INTERFACE_INFO_ARRAY = 10,
102     REMOTE_DEVICE_ID = 11,
103     NEGO_CHANNEL = 12,
104     EXTRA_DATA_ARRAY = 13,
105     INNER_LINK = 14,
106     WIFI_CFG_TYPE = 15,
107     IS_PROXY_ENABLE = 16,
108     CHANNEL_5G_LIST = 17,
109     CHANNEL_5G_SCORE = 18,
110     CHALLENGE_CODE = 19,
111     NEW_PTK_FRAME = 20,
112 
113     /* old p2p */
114     GC_CHANNEL_LIST = 200,
115     STATION_FREQUENCY = 201,
116     ROLE = 202,
117     EXPECTED_ROLE = 203,
118     VERSION = 204,
119     GC_IP = 205,
120     WIDE_BAND_SUPPORTED = 206,
121     GROUP_CONFIG = 207,
122     MAC = 208,
123     BRIDGE_SUPPORTED = 209,
124     GO_IP = 210,
125     GO_MAC = 211,
126     GO_PORT = 212,
127     IP = 213,
128     RESULT = 214,
129     CONTENT_TYPE = 215,
130     GC_MAC = 216,
131     SELF_WIFI_CONFIG = 217,
132     GC_CHANNEL_SCORE = 218,
133     COMMAND_TYPE = 219,
134     INTERFACE_NAME = 220,
135 };
136 
137 class NegotiateMessage : public Serializable, public InfoContainer<NegotiateMessageKey> {
138 public:
139     NegotiateMessage();
140     explicit NegotiateMessage(NegotiateMessageType type);
141     explicit NegotiateMessage(LegacyCommandType type);
142     ~NegotiateMessage() override;
143 
144     int Marshalling(WifiDirectProtocol &protocol, std::vector<uint8_t> &output) const override;
145     int Unmarshalling(WifiDirectProtocol &protocol, const std::vector<uint8_t> &input) override;
146 
147     void SetMessageType(NegotiateMessageType value);
148     void SetMessageType(LegacyCommandType value);
149     NegotiateMessageType GetMessageType() const;
150     std::string MessageTypeToString() const;
151 
152     static constexpr int SESSION_ID_INVALID = -1;
153     void SetSessionId(uint32_t value);
154     uint32_t GetSessionId() const;
155 
156     void SetWifiConfigInfo(const std::vector<uint8_t> &value);
157     std::vector<uint8_t> GetWifiConfigInfo() const;
158 
159     void SetIpv4InfoArray(const std::vector<Ipv4Info> &value);
160     std::vector<Ipv4Info> GetIpv4InfoArray() const;
161 
162     void SetPreferLinkMode(LinkInfo::LinkMode value);
163     LinkInfo::LinkMode GetPreferLinkMode() const;
164 
165     void SetIsModeStrict(bool value);
166     bool GetIsModeStrict() const;
167 
168     void SetPreferLinkBandWidth(int value);
169     int GetPreferLinkBandWidth() const;
170 
171     void SetIsBridgeSupported(bool value);
172     bool GetIsBridgeSupported() const;
173 
174     void SetLinkInfo(const LinkInfo &value);
175     LinkInfo GetLinkInfo() const;
176 
177     static constexpr int RESULT_CODE_INVALID = -1;
178     void SetResultCode(int value);
179     int GetResultCode() const;
180 
181     void SetInterfaceInfoArray(const std::vector<InterfaceInfo> &value);
182     std::vector<InterfaceInfo> GetInterfaceInfoArray() const;
183 
184     void SetRemoteDeviceId(const std::string &value);
185     std::string GetRemoteDeviceId() const;
186 
187     void SetExtraData(const std::vector<uint8_t> &value);
188     std::vector<uint8_t> GetExtraData() const;
189 
190     void SetIsProxyEnable(bool value);
191     bool GetIsProxyEnable() const;
192 
193     void Set5GChannelList(const std::string &value);
194     std::string Get5GChannelList() const;
195 
196     void Set5GChannelScore(const std::string &value);
197     std::string Get5GChannelScore() const;
198 
199     void SetChallengeCode(uint32_t value);
200     uint32_t GetChallengeCode() const;
201 
202     void SetNewPtkFrame(bool value);
203     bool GetNewPtkFrame() const;
204 
205     void SetLegacyP2pGcChannelList(const std::string &value);
206     std::string GetLegacyP2pGcChannelList() const;
207 
208     void SetLegacyP2pStationFrequency(int value);
209     int GetLegacyP2pStationFrequency() const;
210 
211     void SetLegacyP2pRole(WifiDirectRole value);
212     WifiDirectRole GetLegacyP2pRole() const;
213 
214     void SetLegacyP2pExpectedRole(WifiDirectRole value);
215     WifiDirectRole GetLegacyP2pExpectedRole() const;
216 
217     void SetLegacyP2pVersion(int value);
218     int GetLegacyP2pVersion() const;
219 
220     void SetLegacyP2pGcIp(const std::string &value);
221     std::string GetLegacyP2pGcIp() const;
222 
223     void SetLegacyP2pWideBandSupported(bool value);
224     bool GetLegacyP2pWideBandSupported() const;
225 
226     void SetLegacyP2pGroupConfig(const std::string &value);
227     std::string GetLegacyP2pGroupConfig() const;
228 
229     void SetLegacyP2pMac(const std::string &value);
230     std::string GetLegacyP2pMac() const;
231 
232     void SetLegacyP2pBridgeSupport(bool value);
233     bool GetLegacyP2pBridgeSupport() const;
234 
235     void SetLegacyP2pGoIp(const std::string &value);
236     std::string GetLegacyP2pGoIp() const;
237 
238     void SetLegacyP2pGoMac(const std::string &value);
239     std::string GetLegacyP2pGoMac() const;
240 
241     void SetLegacyP2pGoPort(int value);
242     int GetLegacyP2pGoPort() const;
243 
244     void SetLegacyP2pIp(const std::string &value);
245     std::string GetLegacyP2pIp() const;
246 
247     void SetLegacyP2pResult(LegacyResult value);
248     LegacyResult GetLegacyP2pResult() const;
249 
250     void SetLegacyP2pContentType(LegacyContentType value);
251     LegacyContentType GetLegacyP2pContentType() const;
252 
253     void SetLegacyP2pGcMac(const std::string &value);
254     std::string GetLegacyP2pGcMac() const;
255 
256     void SetLegacyP2pWifiConfigInfo(const std::string &value);
257     std::string GetLegacyP2pWifiConfigInfo() const;
258 
259     void SetLegacyP2pCommandType(LegacyCommandType value);
260     LegacyCommandType GetLegacyP2pCommandType() const;
261 
262     void SetLegacyInterfaceName(const std::string &value);
263     std::string GetLegacyInterfaceName() const;
264 
265     static std::map<NegotiateMessageKey, std::string> keyStringTable_;
266 
267 private:
268     void MarshallingIpv4Array(WifiDirectProtocol &protocol) const ;
269     void MarshallingInterfaceArray(WifiDirectProtocol &protocol) const ;
270     void MarshallingLinkInfo(WifiDirectProtocol &protocol) const ;
271 
272     void UnmarshallingIpv4Array(uint8_t *data, size_t size);
273     void UnmarshallingInterfaceArray(WifiDirectProtocol &protocol, uint8_t *data, size_t size);
274     void UnmarshallingLinkInfo(WifiDirectProtocol &protocol, uint8_t *data, size_t size);
275 
276     static std::set<NegotiateMessageKey> keyIgnoreTable_;
277 };
278 }
279 #endif
280