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 WIFI_DIRECT_UTILS_H 16 #define WIFI_DIRECT_UTILS_H 17 18 #include <cinttypes> 19 #include <condition_variable> 20 #include <vector> 21 #include <string> 22 23 #include "data/ipv4_info.h" 24 #include "wifi_direct_types.h" 25 #include "data/link_info.h" 26 #include "data/inner_link.h" 27 28 namespace OHOS::SoftBus { 29 static constexpr int FREQUENCY_2G_FIRST = 2412; 30 static constexpr int FREQUENCY_2G_LAST = 2472; 31 static constexpr int FREQUENCY_5G_FIRST = 5170; 32 static constexpr int FREQUENCY_5G_LAST = 5825; 33 static constexpr int CHANNEL_2G_FIRST = 1; 34 static constexpr int CHANNEL_2G_LAST = 13; 35 static constexpr int CHANNEL_5G_FIRST = 34; 36 static constexpr int CHANNEL_5G_LAST = 165; 37 static constexpr int FREQUENCY_STEP = 5; 38 static constexpr int CHANNEL_INVALID = -1; 39 static constexpr int FREQUENCY_INVALID = -1; 40 41 class WifiDirectUtils { 42 public: 43 static std::vector<std::string> SplitString(const std::string &s, const std::string &delimiter); 44 45 static uint32_t BytesToInt(const std::vector<uint8_t> &data); 46 static uint32_t BytesToInt(const uint8_t *data, uint32_t size); 47 static void IntToBytes(uint32_t data, uint32_t len, std::vector<uint8_t> &out); 48 static std::string ToString(const std::vector<uint8_t> &input); 49 static std::vector<uint8_t> ToBinary(const std::string &input); 50 51 static bool Is2GBand(int frequency); 52 static bool Is5GBand(int frequency); 53 static int ChannelToFrequency(int channel); 54 static int FrequencyToChannel(int frequency); 55 56 static std::string NetworkIdToUuid(const std::string &networkId); 57 static std::string UuidToNetworkId(const std::string &uuid); 58 static std::string GetLocalNetworkId(); 59 static std::string GetLocalUuid(); 60 static std::vector<uint8_t> GetLocalPtk(const std::string &remoteNetworkId); 61 static std::vector<uint8_t> GetRemotePtk(const std::string &remoteNetworkId); 62 static bool IsRemoteSupportTlv(const std::string &remoteDeviceId); 63 static bool IsLocalSupportTlv(); 64 static void SetLocalWifiDirectMac(const std::string &mac); 65 static bool IsDeviceOnline(const std::string &remoteNetworkId); 66 67 static std::vector<uint8_t> MacStringToArray(const std::string &macString); 68 static std::string MacArrayToString(const std::vector<uint8_t> &macArray); 69 static std::string MacArrayToString(const uint8_t *mac, int size); 70 static std::vector<uint8_t> GetInterfaceMacAddr(const std::string &interface); 71 static std::string GetInterfaceIpv6Addr(const std::string &name); 72 73 static std::vector<Ipv4Info> GetLocalIpv4Infos(); 74 75 static int CompareIgnoreCase(const std::string &left, const std::string &right); 76 77 static bool SupportHml(); 78 static bool SupportHmlTwo(); 79 static int32_t GetInterfaceIpString(const std::string &interface, std::string &ip); 80 static bool IsInChannelList(int32_t channel, const std::vector<int> &channelArray); 81 static int32_t IpStringToIntArray(const char *addrString, uint32_t *addrArray, size_t addrArraySize); 82 83 static std::string ChannelListToString(const std::vector<int> &channels); 84 static std::vector<int> StringToChannelList(std::string channels); 85 86 static WifiDirectRole ToWifiDirectRole(LinkInfo::LinkMode mode); 87 static void ShowLinkInfoList(const std::string &banana, const std::vector<LinkInfo> &inkList); 88 89 static constexpr int BAND_WIDTH_80M_NUMBER = 80 << 20; 90 static constexpr int BAND_WIDTH_160M_NUMBER = 160 << 20; 91 static WifiDirectBandWidth BandWidthNumberToEnum(int bandWidth); 92 static int BandWidthEnumToNumber(WifiDirectBandWidth bandWidth); 93 static int GetRecommendChannelFromLnn(const std::string &networkId); 94 static void SerialFlowEnter(); 95 static void SerialFlowExit(); 96 static void ParallelFlowEnter(); 97 static void ParallelFlowExit(); 98 static uint32_t CalculateStringLength(const char *str, uint32_t size); 99 static void SyncLnnInfoForP2p(WifiDirectRole role, const std::string &localMac, const std::string &goMac); 100 static bool IsDfsChannel(const int &frequency); 101 static bool CheckLinkAtDfsChannelConflict(const std::string &remoteDeviceId, InnerLink::LinkType type); 102 static int32_t GetOsType(const char *networkId); 103 static int32_t GetDeviceType(const char *networkId); 104 static int32_t GetDeviceType(); 105 106 private: 107 static inline std::mutex serialParallelLock_; 108 static inline std::condition_variable serialParallelCv_; 109 static inline int serialCount_ = 0; 110 static inline int parallelCount_ = 0; 111 }; 112 } 113 114 #endif 115