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 = 63; 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 FormGroup, 57 RemoveGroup, 58 DeleteGroup, 59 P2pConnect, 60 P2pDisConnect, 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 const int MAX_WFD_SUBELEMS = 12; 135 const char DeviceInfoSubelemLenHex[] = {"0006"}; 136 class WifiP2pWfdInfo { 137 public: WifiP2pWfdInfo()138 WifiP2pWfdInfo() : wfdEnabled(false), deviceInfo(0), ctrlPort(0), maxThroughput(0) 139 {} WifiP2pWfdInfo(int info,int port,int throughput)140 WifiP2pWfdInfo(int info, int port, int throughput) 141 : wfdEnabled(true), deviceInfo(info), ctrlPort(port), maxThroughput(throughput) 142 {} ~WifiP2pWfdInfo()143 ~WifiP2pWfdInfo() 144 {} 145 void SetWfdEnabled(bool value); 146 bool GetWfdEnabled() const; 147 void SetDeviceInfo(int info); 148 int GetDeviceInfo() const; 149 void SetCtrlPort(int port); 150 int GetCtrlPort() const; 151 void SetMaxThroughput(int throughput); 152 int GetMaxThroughput() const; 153 bool isSessionAvailable(); 154 void setSessionAvailable(bool enabled); 155 void GetDeviceInfoElement(std::string &subelement); 156 157 private: 158 bool wfdEnabled; 159 int deviceInfo; 160 int ctrlPort; 161 int maxThroughput; 162 }; 163 164 class WifiP2pDevice { 165 public: WifiP2pDevice()166 WifiP2pDevice() 167 : deviceName(""), 168 mDeviceAddress(""), 169 primaryDeviceType(""), 170 secondaryDeviceType(""), 171 status(P2pDeviceStatus::PDS_UNAVAILABLE), 172 supportWpsConfigMethods(0), 173 deviceCapabilitys(0), 174 groupCapabilitys(0) 175 {} ~WifiP2pDevice()176 ~WifiP2pDevice() 177 {} 178 void SetDeviceName(const std::string &setDeviceName); 179 const std::string &GetDeviceName() const; 180 void SetDeviceAddress(const std::string &deviceAddress); 181 const std::string &GetDeviceAddress() const; 182 void SetPrimaryDeviceType(const std::string &setPrimaryDeviceType); 183 const std::string &GetPrimaryDeviceType() const; 184 void SetSecondaryDeviceType(const std::string &deviceType); 185 const std::string &GetSecondaryDeviceType() const; 186 void SetP2pDeviceStatus(P2pDeviceStatus setStatus); 187 P2pDeviceStatus GetP2pDeviceStatus() const; 188 void SetWfdInfo(const WifiP2pWfdInfo &info); 189 const WifiP2pWfdInfo &GetWfdInfo() const; 190 void SetWpsConfigMethod(unsigned int wpsConfigMethod); 191 unsigned int GetWpsConfigMethod() const; 192 void SetDeviceCapabilitys(int capabilitys); 193 int GetDeviceCapabilitys() const; 194 void SetGroupCapabilitys(int capabilitys); 195 int GetGroupCapabilitys() const; 196 bool IsGroupOwner() const; 197 bool IsGroupLimit() const; 198 bool IsDeviceLimit() const; 199 bool Isinviteable() const; 200 bool IsValid() const; 201 bool operator==(const WifiP2pDevice &cmp) const; 202 bool operator!=(const WifiP2pDevice &cmp) const; 203 bool WpsPbcSupported() const; 204 bool WpsDisplaySupported() const; 205 bool WpKeypadSupported() const; 206 bool isGroupOwner() const; 207 208 private: 209 std::string deviceName; /* the value range is 0 to 32 characters. */ 210 std::string mDeviceAddress; /* the device MAC address, the length is 17 characters. */ 211 std::string primaryDeviceType; 212 std::string secondaryDeviceType; 213 P2pDeviceStatus status; 214 WifiP2pWfdInfo wfdInfo; 215 unsigned int supportWpsConfigMethods; 216 int deviceCapabilitys; 217 int groupCapabilitys; 218 }; 219 220 const int TEMPORARY_NET_ID = -1; 221 const int PERSISTENT_NET_ID = -2; 222 const int INVALID_NET_ID = -999; 223 class WifiP2pGroupInfo { 224 public: WifiP2pGroupInfo()225 WifiP2pGroupInfo() 226 : isP2pGroupOwner(false), 227 networkId(INVALID_NET_ID), 228 frequency(0), 229 isP2pPersistent(0), 230 groupStatus(P2pGroupStatus::GS_INVALID) 231 {} ~WifiP2pGroupInfo()232 ~WifiP2pGroupInfo() 233 {} 234 bool operator==(const WifiP2pGroupInfo &group) const; 235 bool operator!=(const WifiP2pGroupInfo &group) const; 236 void SetIsGroupOwner(bool isGroupOwner); 237 bool IsGroupOwner() const; 238 void SetOwner(const WifiP2pDevice &setOwner); 239 const WifiP2pDevice &GetOwner() const; 240 void SetPassphrase(const std::string &setPassphrase); 241 const std::string &GetPassphrase() const; 242 void SetInterface(const std::string &setInterface); 243 const std::string &GetInterface() const; 244 void SetGroupName(const std::string &newGroupName); 245 const std::string &GetGroupName() const; 246 void SetFrequency(int setFrequency); 247 int GetFrequency() const; 248 void SetIsPersistent(bool isPersistent); 249 bool IsPersistent() const; 250 void SetP2pGroupStatus(P2pGroupStatus newGroupStatus); 251 P2pGroupStatus GetP2pGroupStatus() const; 252 void SetNetworkId(int nwId); 253 const int &GetNetworkId() const; 254 void SetGoIpAddress(const std::string &ipAddr); 255 const std::string &GetGoIpAddress() const; 256 void AddClientDevice(const WifiP2pDevice &clientDevice); 257 void RemoveClientDevice(const WifiP2pDevice &clientDevice); 258 bool IsContainsDevice(const WifiP2pDevice &clientDevice) const; 259 bool IsClientDevicesEmpty() const; 260 const std::vector<WifiP2pDevice> &GetClientDevices() const; 261 void SetClientDevices(const std::vector<WifiP2pDevice> &devices); 262 void ClearClientDevices(); 263 264 private: 265 WifiP2pDevice owner; 266 bool isP2pGroupOwner; 267 std::string passphrase; /* the value ranges from 8 to 63. */ 268 std::string interface; 269 std::string groupName; 270 int networkId; 271 int frequency; /* for example : freq=2412 to select 2.4 GHz channel 1.(Based on 2.4 GHz or 5 GHz) */ 272 bool isP2pPersistent; 273 P2pGroupStatus groupStatus; 274 std::vector<WifiP2pDevice> clientDevices; 275 std::string goIpAddress; 276 }; 277 278 class WpsInfo { 279 public: WpsInfo()280 WpsInfo() : mWpsMethod(WpsMethod::WPS_METHOD_INVALID), bssid(""), pin("") 281 {} ~WpsInfo()282 ~WpsInfo() 283 {} 284 void SetWpsMethod(WpsMethod wpsMethod); 285 WpsMethod GetWpsMethod() const; 286 287 void SetBssid(const std::string &setBssid); 288 const std::string &GetBssid() const; 289 void SetPin(const std::string &setPin); 290 const std::string &GetPin() const; 291 292 private: 293 WpsMethod mWpsMethod; 294 std::string bssid; /* the length is 17 characters. */ 295 std::string pin; /* the length is 4 or 8 characters. */ 296 }; 297 298 const int AUTO_GROUP_OWNER_VALUE = -1; 299 const int MIN_GROUP_OWNER_INTENT = 0; 300 const int MAX_GROUP_OWNER_INTENT = 15; 301 class WifiP2pConfig { 302 public: WifiP2pConfig()303 WifiP2pConfig() 304 : mDeviceAddress(""), 305 goBand(GroupOwnerBand::GO_BAND_AUTO), 306 netId(-1), 307 passphrase(""), 308 groupOwnerIntent(AUTO_GROUP_OWNER_VALUE), 309 groupName("") 310 {} WifiP2pConfig(const WifiP2pConfig & config)311 WifiP2pConfig(const WifiP2pConfig &config) 312 : mDeviceAddress(config.GetDeviceAddress()), 313 goBand(config.GetGoBand()), 314 netId(config.GetNetId()), 315 passphrase(config.GetPassphrase()), 316 groupOwnerIntent(config.GetGroupOwnerIntent()), 317 groupName(config.GetGroupName()) 318 {} ~WifiP2pConfig()319 ~WifiP2pConfig() 320 {} 321 void SetDeviceAddress(const std::string &deviceAddress); 322 const std::string &GetDeviceAddress() const; 323 void SetGoBand(GroupOwnerBand setGoBand); 324 GroupOwnerBand GetGoBand() const; 325 void SetNetId(int setNetId); 326 int GetNetId() const; 327 void SetPassphrase(const std::string &newPassphrase); 328 const std::string &GetPassphrase() const; 329 void SetGroupOwnerIntent(int intent); 330 int GetGroupOwnerIntent() const; 331 void SetGroupName(const std::string &setGroupName); 332 const std::string &GetGroupName() const; 333 334 private: 335 std::string mDeviceAddress; /* the device MAC address, the length is 17 characters. */ 336 GroupOwnerBand goBand; 337 int netId; /* network id, when -2 means persistent and -1 means temporary, else need >= 0 */ 338 std::string passphrase; /* the value ranges from 8 to 63. */ 339 int groupOwnerIntent; /* the value is -1.(A value of -1 indicates the system can choose an appropriate value.) */ 340 std::string groupName; /* the value ranges from 1 to 32. */ 341 }; 342 343 class WifiP2pConfigInternal : public WifiP2pConfig { 344 public: WifiP2pConfigInternal()345 WifiP2pConfigInternal(): WifiP2pConfig() 346 { 347 wpsInfo.SetWpsMethod(WpsMethod::WPS_METHOD_INVALID); 348 } WifiP2pConfigInternal(WifiP2pConfig config)349 WifiP2pConfigInternal(WifiP2pConfig config): WifiP2pConfig(config) 350 { 351 wpsInfo.SetWpsMethod(WpsMethod::WPS_METHOD_INVALID); 352 } ~WifiP2pConfigInternal()353 ~WifiP2pConfigInternal() 354 {} SetWpsInfo(const WpsInfo & info)355 inline void SetWpsInfo(const WpsInfo &info) 356 { 357 wpsInfo = info; 358 } GetWpsInfo()359 inline const WpsInfo &GetWpsInfo() const 360 { 361 return wpsInfo; 362 } 363 364 private: 365 WpsInfo wpsInfo; 366 }; 367 368 class WifiP2pLinkedInfo { 369 public: WifiP2pLinkedInfo()370 WifiP2pLinkedInfo() : connectState(P2pConnectedState::P2P_DISCONNECTED), isP2pGroupOwner(false) 371 {} ~WifiP2pLinkedInfo()372 ~WifiP2pLinkedInfo() 373 {} 374 void SetConnectState(P2pConnectedState setConnectState); 375 P2pConnectedState GetConnectState() const; 376 void SetIsGroupOwner(bool isGroupOwner); 377 const bool &IsGroupOwner() const; 378 void SetIsGroupOwnerAddress(const std::string &setGroupOwnerAddress); 379 const std::string &GetGroupOwnerAddress() const; 380 381 private: 382 P2pConnectedState connectState; 383 bool isP2pGroupOwner; 384 std::string groupOwnerAddress; /* the length is 17 characters. */ 385 }; 386 387 const int SERVICE_TLV_LENGTH_SIZE = 2; 388 const int PROTOCOL_SIZE = 1; 389 const int TRANSACTION_ID_SIZE = 1; 390 const int SERVICE_STATUS_SIZE = 1; 391 392 class WifiP2pServiceRequest { 393 public: WifiP2pServiceRequest()394 WifiP2pServiceRequest() : mProtocolType(P2pServicerProtocolType::SERVICE_TYPE_ALL), mTransactionId(0) 395 {} WifiP2pServiceRequest(P2pServicerProtocolType protocolType,const std::string & data)396 WifiP2pServiceRequest(P2pServicerProtocolType protocolType, const std::string &data) 397 : mProtocolType(protocolType), 398 mTransactionId(0) 399 { 400 for (unsigned long i = 0; i < data.length(); ++i) { 401 mQuery.push_back(data.at(i)); 402 } 403 } ~WifiP2pServiceRequest()404 ~WifiP2pServiceRequest() 405 {} 406 void SetProtocolType(P2pServicerProtocolType serviceProtocolType); 407 P2pServicerProtocolType GetProtocolType() const; 408 void SetTransactionId(unsigned char transactionId); 409 int GetTransactionId() const; 410 void SetQuery(const std::vector<unsigned char> &query); 411 const std::vector<unsigned char> &GetQuery() const; 412 413 std::vector<unsigned char> GetTlv() const; 414 415 bool operator==(const WifiP2pServiceRequest &cmp) const; 416 417 private: 418 P2pServicerProtocolType mProtocolType; 419 unsigned char mTransactionId; 420 std::vector<unsigned char> mQuery; 421 }; 422 423 class WifiP2pServiceResponse { 424 public: WifiP2pServiceResponse()425 WifiP2pServiceResponse() 426 : mProtocolType(P2pServicerProtocolType::SERVICE_TYPE_ALL), 427 mTransactionId(0), 428 mServiceStatus(P2pServiceStatus::PSRS_BAD_REQUEST) 429 {} WifiP2pServiceResponse(P2pServicerProtocolType protocolType,P2pServiceStatus serviceStatus,unsigned char transactionId,const std::vector<unsigned char> data)430 WifiP2pServiceResponse(P2pServicerProtocolType protocolType, P2pServiceStatus serviceStatus, 431 unsigned char transactionId, const std::vector<unsigned char> data) 432 : mProtocolType(protocolType), mTransactionId(transactionId), mServiceStatus(serviceStatus), responseData(data) 433 {} ~WifiP2pServiceResponse()434 ~WifiP2pServiceResponse() 435 {} 436 void SetProtocolType(P2pServicerProtocolType serviceProtocolType); 437 P2pServicerProtocolType GetProtocolType() const; 438 void SetTransactionId(unsigned char transactionId); 439 unsigned char GetTransactionId() const; 440 void SetServiceStatus(P2pServiceStatus serviceStatus); 441 P2pServiceStatus GetServiceStatus() const; 442 void SetServiceName(const std::string &name); 443 const std::string &GetServiceName() const; 444 void SetData(const std::vector<unsigned char> &data); 445 const std::vector<unsigned char> &GetData() const; 446 std::vector<unsigned char> GetTlv() const; 447 bool operator==(const WifiP2pServiceResponse &cmp) const; 448 449 protected: 450 P2pServicerProtocolType mProtocolType; 451 unsigned char mTransactionId; 452 P2pServiceStatus mServiceStatus; 453 std::string mSvrName; 454 std::vector<unsigned char> responseData; 455 }; 456 457 class WifiP2pServiceInfo { 458 public: WifiP2pServiceInfo()459 WifiP2pServiceInfo() 460 : mDeviceAddress("00:00:00:00:00:00"), mProtocolType(P2pServicerProtocolType::SERVICE_TYPE_VENDOR_SPECIFIC) 461 {} WifiP2pServiceInfo(std::vector<std::string> queryList)462 explicit WifiP2pServiceInfo(std::vector<std::string> queryList) : mQueryList(queryList) 463 {} ~WifiP2pServiceInfo()464 ~WifiP2pServiceInfo() 465 {} 466 void SetServiceName(const std::string &name); 467 const std::string &GetServiceName() const; 468 void SetDeviceAddress(const std::string &deviceAddress); 469 const std::string &GetDeviceAddress() const; 470 void SetServicerProtocolType(P2pServicerProtocolType type); 471 P2pServicerProtocolType GetServicerProtocolType() const; 472 void SetQueryList(const std::vector<std::string> &queryList); 473 const std::vector<std::string> &GetQueryList() const; 474 bool operator==(const WifiP2pServiceInfo &cmp) const; 475 /** 476 * @Description - Pack all data into a P2P service request packet based on the data interface. 477 * @return - WifiP2pServiceRequest 478 */ 479 WifiP2pServiceRequest BuildRequest(); 480 P2pServiceStatus ProcessServiceRequest( 481 const std::vector<unsigned char> &Query, std::vector<unsigned char> &data) const; 482 void ProcessServiceResponse(const std::vector<unsigned char> &data) const; 483 static std::string Bin2HexStr(std::vector<unsigned char> data); 484 static std::string Bin2HexStr(std::string data); 485 486 private: 487 std::string serviceName; 488 std::string mDeviceAddress; 489 P2pServicerProtocolType mProtocolType; 490 std::vector<std::string> mQueryList; 491 }; 492 493 class P2pVendorConfig { 494 public: P2pVendorConfig()495 P2pVendorConfig() : randomMacSupport(false), isAutoListen(false), primaryDeviceType("10-0050F204-5") 496 {} ~P2pVendorConfig()497 ~P2pVendorConfig() 498 {} 499 bool GetRandomMacSupport() const; 500 void SetRandomMacSupport(bool support); 501 bool GetIsAutoListen() const; 502 void SetIsAutoListen(bool autoListen); 503 const std::string &GetDeviceName() const; 504 void SetDeviceName(const std::string &name); 505 const std::string &GetPrimaryDeviceType() const; 506 void SetPrimaryDeviceType(const std::string &setPrimaryDeviceType); 507 const std::string &GetSecondaryDeviceType() const; 508 void SetSecondaryDeviceType(const std::string &setSecondaryDeviceType); 509 510 private: 511 bool randomMacSupport; 512 bool isAutoListen; 513 std::string deviceName; 514 std::string primaryDeviceType; 515 std::string secondaryDeviceType; 516 }; 517 } // namespace Wifi 518 } // namespace OHOS 519 #endif 520