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 OHOS_WIFI_RANDOM_MAC_HELPER_H
16 #define OHOS_WIFI_RANDOM_MAC_HELPER_H
17 #include <string>
18 #include <random>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include "wifi_common_util.h"
22 #include "wifi_msg.h"
23 
24 namespace OHOS {
25 namespace Wifi {
26 
27 class WifiRandomMacHelper {
28 public:
29 
30     /**
31      * @Description Generate random number
32      *
33      * @return long int
34      */
35     static long int GetRandom();
36 
37     /**
38      * @Description generate a MAC address
39      *
40      * @param randomMacAddr - random MAC address[out]
41      */
42     static void GenerateRandomMacAddress(std::string &randomMacAddr);
43 
44     /**
45      * @Description generate a MAC address
46      *
47      * @param peerBssid - real MAC address[in]
48      * @param randomMacAddr - random MAC address[out]
49      */
50     static void GenerateRandomMacAddressByBssid(std::string peerBssid, std::string &randomMacAddr);
51 
52     /**
53      * @Description generate a MAC address
54      *
55      * @param random - unsigned long long value mac address[in]
56      * @param randomMacAddr - random MAC address[out]
57      * @return 0 - success
58      */
59     static int GenerateRandomMacAddressByLong(unsigned long long random, std::string &randomMacAddr);
60 
61 #ifdef SUPPORT_LOCAL_RANDOM_MAC
62     /**
63      * @Description generate a MAC address
64      *
65      * @param content - content for generate MAC address[in]
66      * @param randomMacAddr - random MAC address[out]
67      * @return 0 - success
68      */
69     static int CalculateRandomMacForWifiDeviceConfig(const std::string &content, std::string &randomMacAddr);
70 #endif
71 
72     /**
73      * @Description generate a MAC address
74      *
75      * @param peerBssid - real MAC address[in]
76      * @param randomMacAddr - random MAC address[out]
77      */
78     static void LongLongToBytes(unsigned long long value, std::vector<uint8_t> &outPlant);
79 
80     /**
81      * @Description convert uint8_t array to unsigned long long value
82      *
83      * @param bytes - uint8_t array bytes[in]
84      * @return unsigned long long value
85      */
86     static unsigned long long BytesToLonglong(const std::vector<uint8_t> &bytes);
87 
88     /**
89      * @Description convert uint8_t array to unsigned long long mac address
90      *
91      * @param bytes - uint8_t array bytes[in]
92      * @return unsigned long long value of mac address
93      */
94     static unsigned long long LongAddrFromByteAddr(std::vector<uint8_t> &bytes);
95 
96     /**
97      * @Description generate a MAC address from unsigned long long value
98      *
99      * @param addr - addr[in]
100      * @param randomMacAddr - random MAC address[out]
101      * @return 0 - success
102      */
103     static int StringAddrFromLongAddr(unsigned long long addr, std::string &randomMacAddr);
104     /**
105      * @Description a debug tool for BytesArrayToString
106      *
107      * @param bytes - uint8_t array
108      * @return std::string - formated string
109      */
110     static std::string BytesArrayToString(const std::vector<uint8_t> &bytes);
111 
112     /**
113     * @Description  change random mac for wifi2
114     *for example, input mac is 00:1a:2b:3c:4d:5e, this func will change pos 3 byte mac from "3c" to "bc" by
115     *performing an XOR operation with 0x80
116     * @param wifi2RandomMac - wifi2RandomMac
117     */
118     static bool GetWifi2RandomMac(std::string &wifi2RandomMac);
119 };
120 }
121 }
122 #endif