• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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_WIFI_P2P_MSG_H
17 #define OHOS_WIFI_P2P_MSG_H
18 
19 #include <string>
20 #include <vector>
21 #include <climits>
22 #include "securec.h"
23 
24 namespace OHOS {
25 namespace Wifi {
26 constexpr int WIFI_STR_MAC_LENGTH = 17;
27 constexpr int MAX_PASSPHRASE_LENGTH = 127;
28 constexpr int DEVICE_NAME_LENGTH = 32;
29 
30 enum class P2pGroupStatus { GS_CREATING, GS_CREATED, GS_STARTED, GS_REMOVING, GS_INVALID };
31 enum class P2pServiceStatus : unsigned char {
32     PSRS_SUCCESS,
33     PSRS_SERVICE_PROTOCOL_NOT_AVAILABLE,
34     PSRS_REQUESTED_INFORMATION_NOT_AVAILABLE,
35     PSRS_BAD_REQUEST,
36 };
37 
38 enum class P2pServicerProtocolType : unsigned char {
39     SERVICE_TYPE_ALL = 0,
40     SERVICE_TYPE_BONJOUR = 1,
41     SERVICE_TYPE_UP_NP = 2,
42     SERVICE_TYPE_WS_DISCOVERY = 3,
43     SERVICE_TYPE_VENDOR_SPECIFIC = 255,
44 };
45 
46 enum class P2pActionCallback : unsigned char {
47     DiscoverDevices,
48     StopDiscoverDevices,
49     DiscoverServices,
50     StopDiscoverServices,
51     PutLocalP2pService,
52     DeleteLocalP2pService,
53     RequestService,
54     StartP2pListen,
55     StopP2pListen,
56     CreateGroup,
57     RemoveGroup,
58     DeleteGroup,
59     P2pConnect,
60     P2pCancelConnect,
61     P2pSetDeviceName,
62     CreateHid2dGroup,
63     Hid2dConnect,
64     UNKNOWN
65 };
66 
67 enum class P2pState {
68     P2P_STATE_NONE = 0,
69     P2P_STATE_IDLE,
70     P2P_STATE_STARTING,
71     P2P_STATE_STARTED,
72     P2P_STATE_CLOSING,
73     P2P_STATE_CLOSED,
74 };
75 
76 enum class P2pDiscoverState {
77     P2P_DISCOVER_NONE = 0,
78     P2P_DISCOVER_STARTING,
79     P2P_DISCOVER_CLOSED,
80 };
81 
82 enum class P2pConnectedState {
83     P2P_DISCONNECTED = 0,
84     P2P_CONNECTED,
85 };
86 
87 enum class P2pWfdInfoType {
88     WFD_SOURCE = 0x00,
89     PRIMARY_SINK = 0x01,
90     SECONDARY_SINK= 0x10,
91     SOURCE_OR_PRIMARY_SINK= 0x11
92 };
93 enum class P2pDeviceType {
94     DEVICE_TYPE = 0x3,
95     COUPLED_SINK_SUPPORT_AT_SOURCE = 0x4,
96     COUPLED_SINK_SUPPORT_AT_SINK = 0x8,
97     SESSION_AVAILABLE = 0x30,
98     SESSION_AVAILABLE_BIT1 = 0x10,
99     SESSION_AVAILABLE_BIT2 = 0x20
100 };
101 
102 enum class P2pDeviceStatus { PDS_CONNECTED, PDS_INVITED, PDS_FAILED, PDS_AVAILABLE, PDS_UNAVAILABLE };
103 
104 enum class WpsMethod { WPS_METHOD_PBC, WPS_METHOD_DISPLAY, WPS_METHOD_KEYPAD, WPS_METHOD_LABEL, WPS_METHOD_INVALID };
105 
106 enum class WpsConfigMethod {
107     WPS_CFG_INVALID = 0,
108     WPS_CFG_DISPLAY = 0x0008,
109     WPS_CFG_PUSHBUTTON = 0x0080,
110     WPS_CFG_KEYPAD = 0x0100,
111 };
112 
113 enum class P2pDeviceCapability {
114     PDC_SERVICE_DISCOVERY = 1,
115     PDC_CLIENT_DISCOVERABILITY = 1 << 1,
116     PDC_STARTED_CONCURRENT_OPER = 1 << 2,
117     PDC_REMOVING_INFRA_MANAGED = 1 << 3,
118     PDC_DEVICE_LIMIT = 1 << 4,
119     PDC_INVITATION_PROCEDURE = 1 << 5
120 };
121 
122 enum class P2pGroupCapability {
123     PGC_GROUP_OWNER = 1,
124     PGC_PERSISTENT_GROUP = 1 << 1,
125     PGC_GROUP_LIMIT = 1 << 2,
126     PGC_INTRA_BSS_DIST = 1 << 3,
127     PGC_CROSS_CONN = 1 << 4,
128     PGC_PERSISTENT_RECONN = 1 << 5,
129     PGC_GROUP_FORMATION = 1 << 6,
130     PGC_IP_ADDR_ALLOC = 1 << 7
131 };
132 
133 enum class GroupOwnerBand { GO_BAND_AUTO, GO_BAND_2GHZ, GO_BAND_5GHZ };
134 
135 const int MAX_WFD_SUBELEMS = 12;
136 const char DeviceInfoSubelemLenHex[] = {"0006"};
137 class WifiP2pWfdInfo {
138 public:
WifiP2pWfdInfo()139     WifiP2pWfdInfo() : wfdEnabled(false), deviceInfo(0), ctrlPort(0), maxThroughput(0)
140     {}
WifiP2pWfdInfo(int info,int port,int throughput)141     WifiP2pWfdInfo(int info, int port, int throughput)
142         : wfdEnabled(true), deviceInfo(info), ctrlPort(port), maxThroughput(throughput)
143     {}
~WifiP2pWfdInfo()144     ~WifiP2pWfdInfo()
145     {}
146     void SetWfdEnabled(bool value);
147     bool GetWfdEnabled() const;
148     void SetDeviceInfo(int info);
149     int GetDeviceInfo() const;
150     void SetCtrlPort(int port);
151     int GetCtrlPort() const;
152     void SetMaxThroughput(int throughput);
153     int GetMaxThroughput() const;
154     bool isSessionAvailable();
155     void setSessionAvailable(bool enabled);
156     void GetDeviceInfoElement(std::string &subelement);
157 
158 private:
159     bool wfdEnabled;
160     int deviceInfo;
161     int ctrlPort;
162     int maxThroughput;
163 };
164 
165 class WifiP2pDevice {
166 public:
WifiP2pDevice()167     WifiP2pDevice()
168         : deviceName(""),
169           networkName(""),
170           mDeviceAddress(""),
171           primaryDeviceType(""),
172           secondaryDeviceType(""),
173           status(P2pDeviceStatus::PDS_UNAVAILABLE),
174           supportWpsConfigMethods(0),
175           deviceCapabilitys(0),
176           groupCapabilitys(0)
177     {}
~WifiP2pDevice()178     ~WifiP2pDevice()
179     {}
180     void SetDeviceName(const std::string &setDeviceName);
181     const std::string &GetDeviceName() const;
182     void SetNetworkName(const std::string &name);
183     const std::string &GetNetworkName() const;
184     void SetDeviceAddress(const std::string &deviceAddress);
185     const std::string &GetDeviceAddress() const;
186     void SetPrimaryDeviceType(const std::string &setPrimaryDeviceType);
187     const std::string &GetPrimaryDeviceType() const;
188     void SetSecondaryDeviceType(const std::string &deviceType);
189     const std::string &GetSecondaryDeviceType() const;
190     void SetP2pDeviceStatus(P2pDeviceStatus setStatus);
191     P2pDeviceStatus GetP2pDeviceStatus() const;
192     void SetWfdInfo(const WifiP2pWfdInfo &info);
193     const WifiP2pWfdInfo &GetWfdInfo() const;
194     void SetWpsConfigMethod(unsigned int wpsConfigMethod);
195     unsigned int GetWpsConfigMethod() const;
196     void SetDeviceCapabilitys(int capabilitys);
197     int GetDeviceCapabilitys() const;
198     void SetGroupCapabilitys(int capabilitys);
199     int GetGroupCapabilitys() const;
200     bool IsGroupOwner() const;
201     bool IsGroupLimit() const;
202     bool IsDeviceLimit() const;
203     bool Isinviteable() const;
204     bool IsValid() const;
205     bool operator==(const WifiP2pDevice &cmp) const;
206     bool operator!=(const WifiP2pDevice &cmp) const;
207     bool WpsPbcSupported() const;
208     bool WpsDisplaySupported() const;
209     bool WpKeypadSupported() const;
210 
211 private:
212     std::string deviceName; /* the value range is 0 to 32 characters. */
213     std::string networkName; /* oper_ssid of peer device */
214     std::string mDeviceAddress; /* the device MAC address, the length is 17 characters. */
215     std::string primaryDeviceType;
216     std::string secondaryDeviceType;
217     P2pDeviceStatus status;
218     WifiP2pWfdInfo wfdInfo;
219     unsigned int supportWpsConfigMethods;
220     int deviceCapabilitys;
221     int groupCapabilitys;
222 };
223 
224 const int TEMPORARY_NET_ID = -1;
225 const int PERSISTENT_NET_ID = -2;
226 const int INVALID_NET_ID = -999;
227 class WifiP2pGroupInfo {
228 public:
WifiP2pGroupInfo()229     WifiP2pGroupInfo()
230         : isP2pGroupOwner(false),
231           networkId(INVALID_NET_ID),
232           frequency(0),
233           isP2pPersistent(0),
234           groupStatus(P2pGroupStatus::GS_INVALID),
235           explicitGroup(false)
236     {}
~WifiP2pGroupInfo()237     ~WifiP2pGroupInfo()
238     {}
239     bool operator==(const WifiP2pGroupInfo &group) const;
240     bool operator!=(const WifiP2pGroupInfo &group) const;
241     void SetIsGroupOwner(bool isGroupOwner);
242     bool IsGroupOwner() const;
243     void SetOwner(const WifiP2pDevice &setOwner);
244     const WifiP2pDevice &GetOwner() const;
245     void SetPassphrase(const std::string &setPassphrase);
246     const std::string &GetPassphrase() const;
247     void SetInterface(const std::string &setInterface);
248     const std::string &GetInterface() const;
249     void SetGroupName(const std::string &newGroupName);
250     const std::string &GetGroupName() const;
251     void SetFrequency(int setFrequency);
252     int GetFrequency() const;
253     void SetIsPersistent(bool isPersistent);
254     bool IsPersistent() const;
255     void SetP2pGroupStatus(P2pGroupStatus newGroupStatus);
256     P2pGroupStatus GetP2pGroupStatus() const;
257     void SetNetworkId(int nwId);
258     const int &GetNetworkId() const;
259     void SetGoIpAddress(const std::string &ipAddr);
260     const std::string &GetGoIpAddress() const;
261     void AddClientDevice(const WifiP2pDevice &clientDevice);
262     void RemoveClientDevice(const WifiP2pDevice &clientDevice);
263     bool IsContainsDevice(const WifiP2pDevice &clientDevice) const;
264     bool IsClientDevicesEmpty() const;
265     const std::vector<WifiP2pDevice> &GetClientDevices() const;
266     void SetClientDevices(const std::vector<WifiP2pDevice> &devices);
267     void ClearClientDevices();
268     bool IsExplicitGroup(void) const;
269     void SetExplicitGroup(bool isExplicit);
270 
271 private:
272     WifiP2pDevice owner;
273     bool isP2pGroupOwner;
274     std::string passphrase; /* the value ranges from 8 to 63. */
275     std::string interface;
276     std::string groupName;
277     int networkId;
278     int frequency; /* for example : freq=2412 to select 2.4 GHz channel 1.(Based on 2.4 GHz or 5 GHz) */
279     bool isP2pPersistent;
280     P2pGroupStatus groupStatus;
281     std::vector<WifiP2pDevice> clientDevices;
282     std::string goIpAddress;
283     bool explicitGroup;
284 };
285 
286 class WpsInfo {
287 public:
WpsInfo()288     WpsInfo() : mWpsMethod(WpsMethod::WPS_METHOD_INVALID), bssid(""), pin("")
289     {}
~WpsInfo()290     ~WpsInfo()
291     {}
292     void SetWpsMethod(WpsMethod wpsMethod);
293     WpsMethod GetWpsMethod() const;
294 
295     void SetBssid(const std::string &setBssid);
296     const std::string &GetBssid() const;
297     void SetPin(const std::string &setPin);
298     const std::string &GetPin() const;
299 
300 private:
301     WpsMethod mWpsMethod;
302     std::string bssid; /* the length is 17 characters. */
303     std::string pin; /* the length is 4 or 8 characters. */
304 };
305 
306 const int AUTO_GROUP_OWNER_VALUE = -1;
307 const int MIN_GROUP_OWNER_VALUE = 0;
308 const int MAX_GROUP_OWNER_VALUE = 15;
309 class WifiP2pConfig {
310 public:
WifiP2pConfig()311     WifiP2pConfig()
312         : mDeviceAddress(""),
313           goBand(GroupOwnerBand::GO_BAND_AUTO),
314           netId(-1),
315           passphrase(""),
316           groupOwnerIntent(AUTO_GROUP_OWNER_VALUE),
317           groupName("")
318     {}
WifiP2pConfig(const WifiP2pConfig & config)319     WifiP2pConfig(const WifiP2pConfig &config)
320         : mDeviceAddress(config.GetDeviceAddress()),
321           goBand(config.GetGoBand()),
322           netId(config.GetNetId()),
323           passphrase(config.GetPassphrase()),
324           groupOwnerIntent(config.GetGroupOwnerIntent()),
325           groupName(config.GetGroupName())
326     {}
~WifiP2pConfig()327     ~WifiP2pConfig()
328     {}
329     void SetDeviceAddress(const std::string &deviceAddress);
330     const std::string &GetDeviceAddress() const;
331     void SetGoBand(GroupOwnerBand setGoBand);
332     GroupOwnerBand GetGoBand() const;
333     void SetNetId(int setNetId);
334     int GetNetId() const;
335     void SetPassphrase(const std::string &newPassphrase);
336     const std::string &GetPassphrase() const;
337     void SetGroupOwnerIntent(int intent);
338     int GetGroupOwnerIntent() const;
339     void SetGroupName(const std::string &setGroupName);
340     const std::string &GetGroupName() const;
341 
342 private:
343     std::string mDeviceAddress; /* the device MAC address, the length is 17 characters. */
344     GroupOwnerBand goBand;
345     int netId; /* network id, when -2 means persistent and -1 means temporary, else need >= 0 */
346     std::string passphrase; /* the value ranges from 8 to 63. */
347     int groupOwnerIntent; /* the value is -1.(A value of -1 indicates the system can choose an appropriate value.) */
348     std::string groupName; /* the value ranges from 1 to 32. */
349 };
350 
351 class WifiP2pConfigInternal : public WifiP2pConfig {
352 public:
WifiP2pConfigInternal()353     WifiP2pConfigInternal(): WifiP2pConfig()
354     {
355         wpsInfo.SetWpsMethod(WpsMethod::WPS_METHOD_INVALID);
356     }
WifiP2pConfigInternal(WifiP2pConfig config)357     WifiP2pConfigInternal(WifiP2pConfig config): WifiP2pConfig(config)
358     {
359         wpsInfo.SetWpsMethod(WpsMethod::WPS_METHOD_INVALID);
360     }
~WifiP2pConfigInternal()361     ~WifiP2pConfigInternal()
362     {}
SetWpsInfo(const WpsInfo & info)363     inline void SetWpsInfo(const WpsInfo &info)
364     {
365         wpsInfo = info;
366     }
GetWpsInfo()367     inline const WpsInfo &GetWpsInfo() const
368     {
369         return wpsInfo;
370     }
371 
372 private:
373     WpsInfo wpsInfo;
374 };
375 
376 class WifiP2pLinkedInfo {
377 public:
WifiP2pLinkedInfo()378     WifiP2pLinkedInfo() : connectState(P2pConnectedState::P2P_DISCONNECTED), isP2pGroupOwner(false)
379     {}
~WifiP2pLinkedInfo()380     ~WifiP2pLinkedInfo()
381     {}
382     void SetConnectState(P2pConnectedState setConnectState);
383     P2pConnectedState GetConnectState() const;
384     void SetIsGroupOwner(bool isGroupOwner);
385     const bool &IsGroupOwner() const;
386     void SetIsGroupOwnerAddress(const std::string &setGroupOwnerAddress);
387     const std::string &GetGroupOwnerAddress() const;
388 
389 private:
390     P2pConnectedState connectState;
391     bool isP2pGroupOwner;
392     std::string groupOwnerAddress; /* the length is 17 characters. */
393 };
394 
395 const int SERVICE_TLV_LENGTH_SIZE = 2;
396 const int PROTOCOL_SIZE = 1;
397 const int TRANSACTION_ID_SIZE = 1;
398 const int SERVICE_STATUS_SIZE = 1;
399 
400 class WifiP2pServiceRequest {
401 public:
WifiP2pServiceRequest()402     WifiP2pServiceRequest() : mProtocolType(P2pServicerProtocolType::SERVICE_TYPE_ALL), mTransactionId(0)
403     {}
WifiP2pServiceRequest(P2pServicerProtocolType protocolType,const std::string & data)404     WifiP2pServiceRequest(P2pServicerProtocolType protocolType, const std::string &data)
405         : mProtocolType(protocolType),
406           mTransactionId(0)
407     {
408         for (unsigned long i = 0; i < data.length(); ++i) {
409             mQuery.push_back(data.at(i));
410         }
411     }
~WifiP2pServiceRequest()412     ~WifiP2pServiceRequest()
413     {}
414     void SetProtocolType(P2pServicerProtocolType serviceProtocolType);
415     P2pServicerProtocolType GetProtocolType() const;
416     void SetTransactionId(unsigned char transactionId);
417     int GetTransactionId() const;
418     void SetQuery(const std::vector<unsigned char> &query);
419     const std::vector<unsigned char> &GetQuery() const;
420 
421     std::vector<unsigned char> GetTlv() const;
422 
423     bool operator==(const WifiP2pServiceRequest &cmp) const;
424 
425 private:
426     P2pServicerProtocolType mProtocolType;
427     unsigned char mTransactionId;
428     std::vector<unsigned char> mQuery;
429 };
430 
431 class WifiP2pServiceResponse {
432 public:
WifiP2pServiceResponse()433     WifiP2pServiceResponse()
434         : mProtocolType(P2pServicerProtocolType::SERVICE_TYPE_ALL),
435           mTransactionId(0),
436           mServiceStatus(P2pServiceStatus::PSRS_BAD_REQUEST)
437     {}
WifiP2pServiceResponse(P2pServicerProtocolType protocolType,P2pServiceStatus serviceStatus,unsigned char transactionId,const std::vector<unsigned char> data)438     WifiP2pServiceResponse(P2pServicerProtocolType protocolType, P2pServiceStatus serviceStatus,
439         unsigned char transactionId, const std::vector<unsigned char> data)
440         : mProtocolType(protocolType), mTransactionId(transactionId), mServiceStatus(serviceStatus), responseData(data)
441     {}
~WifiP2pServiceResponse()442     ~WifiP2pServiceResponse()
443     {}
444     void SetProtocolType(P2pServicerProtocolType serviceProtocolType);
445     P2pServicerProtocolType GetProtocolType() const;
446     void SetTransactionId(unsigned char transactionId);
447     unsigned char GetTransactionId() const;
448     void SetServiceStatus(P2pServiceStatus serviceStatus);
449     P2pServiceStatus GetServiceStatus() const;
450     void SetServiceName(const std::string &name);
451     const std::string &GetServiceName() const;
452     void SetData(const std::vector<unsigned char> &data);
453     const std::vector<unsigned char> &GetData() const;
454     std::vector<unsigned char> GetTlv() const;
455     bool operator==(const WifiP2pServiceResponse &cmp) const;
456 
457 protected:
458     P2pServicerProtocolType mProtocolType;
459     unsigned char mTransactionId;
460     P2pServiceStatus mServiceStatus;
461     std::string mSvrName;
462     std::vector<unsigned char> responseData;
463 };
464 
465 class WifiP2pServiceInfo {
466 public:
WifiP2pServiceInfo()467     WifiP2pServiceInfo()
468         : mDeviceAddress("00:00:00:00:00:00"), mProtocolType(P2pServicerProtocolType::SERVICE_TYPE_VENDOR_SPECIFIC)
469     {}
WifiP2pServiceInfo(std::vector<std::string> queryList)470     explicit WifiP2pServiceInfo(std::vector<std::string> queryList) : mQueryList(queryList)
471     {}
~WifiP2pServiceInfo()472     ~WifiP2pServiceInfo()
473     {}
474     void SetServiceName(const std::string &name);
475     const std::string &GetServiceName() const;
476     void SetDeviceAddress(const std::string &deviceAddress);
477     const std::string &GetDeviceAddress() const;
478     void SetServicerProtocolType(P2pServicerProtocolType type);
479     P2pServicerProtocolType GetServicerProtocolType() const;
480     void SetQueryList(const std::vector<std::string> &queryList);
481     const std::vector<std::string> &GetQueryList() const;
482     bool operator==(const WifiP2pServiceInfo &cmp) const;
483     /**
484      * @Description - Pack all data into a P2P service request packet based on the data interface.
485      * @return - WifiP2pServiceRequest
486      */
487     WifiP2pServiceRequest BuildRequest();
488     P2pServiceStatus ProcessServiceRequest(
489         const std::vector<unsigned char> &Query, std::vector<unsigned char> &data) const;
490     void ProcessServiceResponse(const std::vector<unsigned char> &data) const;
491     static std::string Bin2HexStr(std::vector<unsigned char> data);
492     static std::string Bin2HexStr(std::string data);
493 
494 private:
495     std::string serviceName;
496     std::string mDeviceAddress;
497     P2pServicerProtocolType mProtocolType;
498     std::vector<std::string> mQueryList;
499 };
500 
501 class P2pVendorConfig {
502 public:
P2pVendorConfig()503     P2pVendorConfig() : randomMacSupport(false), isAutoListen(false), primaryDeviceType("10-0050F204-5")
504     {}
~P2pVendorConfig()505     ~P2pVendorConfig()
506     {}
507     bool GetRandomMacSupport() const;
508     void SetRandomMacSupport(bool support);
509     bool GetIsAutoListen() const;
510     void SetIsAutoListen(bool autoListen);
511     const std::string &GetDeviceName() const;
512     void SetDeviceName(const std::string &name);
513     const std::string &GetPrimaryDeviceType() const;
514     void SetPrimaryDeviceType(const std::string &setPrimaryDeviceType);
515     const std::string &GetSecondaryDeviceType() const;
516     void SetSecondaryDeviceType(const std::string &setSecondaryDeviceType);
517 
518 private:
519     bool randomMacSupport;
520     bool isAutoListen;
521     std::string deviceName;
522     std::string primaryDeviceType;
523     std::string secondaryDeviceType;
524 };
525 }  // namespace Wifi
526 }  // namespace OHOS
527 #endif
528