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