1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci#include "wlan_common_fuzzer.h"
16094332d3Sopenharmony_ci
17094332d3Sopenharmony_ci#define WLAN_FREQ_MAX_NUM 35
18094332d3Sopenharmony_ci#define ETH_ADDR_LEN 6
19094332d3Sopenharmony_ci#define BITS_NUM_24 24
20094332d3Sopenharmony_ci#define BITS_NUM_16 16
21094332d3Sopenharmony_ci#define BITS_NUM_8 8
22094332d3Sopenharmony_ci
23094332d3Sopenharmony_cistatic uint32_t g_wlanTestSize = 0;
24094332d3Sopenharmony_ci
25094332d3Sopenharmony_ciuint32_t SetWlanDataSize(const uint32_t *dataSize)
26094332d3Sopenharmony_ci{
27094332d3Sopenharmony_ci    if (dataSize != nullptr) {
28094332d3Sopenharmony_ci        g_wlanTestSize = *dataSize;
29094332d3Sopenharmony_ci        return HDF_SUCCESS;
30094332d3Sopenharmony_ci    }
31094332d3Sopenharmony_ci    HDF_LOGE("%{public}s: set data size failed!", __FUNCTION__);
32094332d3Sopenharmony_ci    return HDF_FAILURE;
33094332d3Sopenharmony_ci}
34094332d3Sopenharmony_ci
35094332d3Sopenharmony_ciuint32_t GetWlanDataSize(uint32_t *dataSize)
36094332d3Sopenharmony_ci{
37094332d3Sopenharmony_ci    if (dataSize != nullptr) {
38094332d3Sopenharmony_ci        *dataSize = g_wlanTestSize;
39094332d3Sopenharmony_ci        return HDF_SUCCESS;
40094332d3Sopenharmony_ci    }
41094332d3Sopenharmony_ci    HDF_LOGE("%{public}s: get data size failed!", __FUNCTION__);
42094332d3Sopenharmony_ci    return HDF_FAILURE;
43094332d3Sopenharmony_ci}
44094332d3Sopenharmony_ci
45094332d3Sopenharmony_ciuint32_t Convert2Uint32(const uint8_t *ptr)
46094332d3Sopenharmony_ci{
47094332d3Sopenharmony_ci    if (ptr == nullptr) {
48094332d3Sopenharmony_ci        return 0;
49094332d3Sopenharmony_ci    }
50094332d3Sopenharmony_ci    /*
51094332d3Sopenharmony_ci     * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
52094332d3Sopenharmony_ci     * and the third digit no left
53094332d3Sopenharmony_ci     */
54094332d3Sopenharmony_ci    return (ptr[0] << BITS_NUM_24) | (ptr[1] << BITS_NUM_16) | (ptr[2] << BITS_NUM_8) | (ptr[3]);
55094332d3Sopenharmony_ci}
56094332d3Sopenharmony_ci
57094332d3Sopenharmony_cibool PreProcessRawData(const uint8_t *rawData, size_t size, uint8_t *tmpRawData, size_t tmpRawDataSize)
58094332d3Sopenharmony_ci{
59094332d3Sopenharmony_ci    if (rawData == nullptr || tmpRawData == nullptr) {
60094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: rawData or tmpRawData is nullptr!", __FUNCTION__);
61094332d3Sopenharmony_ci        return false;
62094332d3Sopenharmony_ci    }
63094332d3Sopenharmony_ci    uint32_t dataSize = size - OFFSET;
64094332d3Sopenharmony_ci    if (memcpy_s(tmpRawData, tmpRawDataSize, rawData + OFFSET, dataSize) != EOK) {
65094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: memcpy_s failed!", __FUNCTION__);
66094332d3Sopenharmony_ci        return false;
67094332d3Sopenharmony_ci    }
68094332d3Sopenharmony_ci    if (SetWlanDataSize(&dataSize) != HDF_SUCCESS) {
69094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: set data size failed!", __FUNCTION__);
70094332d3Sopenharmony_ci        return false;
71094332d3Sopenharmony_ci    }
72094332d3Sopenharmony_ci    return true;
73094332d3Sopenharmony_ci}
74094332d3Sopenharmony_ci
75094332d3Sopenharmony_civoid FuzzGetChipId(struct IWlanInterface *interface, const uint8_t *rawData)
76094332d3Sopenharmony_ci{
77094332d3Sopenharmony_ci    uint8_t chipId = 0;
78094332d3Sopenharmony_ci    struct HdfFeatureInfo feature;
79094332d3Sopenharmony_ci    feature.ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
80094332d3Sopenharmony_ci    feature.type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_ci    interface->GetChipId(interface, &feature, &chipId);
83094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
84094332d3Sopenharmony_ci}
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_civoid FuzzGetDeviceMacAddress(struct IWlanInterface *interface, const uint8_t *rawData)
87094332d3Sopenharmony_ci{
88094332d3Sopenharmony_ci    uint8_t mac[ETH_ADDR_LEN] = {0};
89094332d3Sopenharmony_ci    uint32_t macLen = ETH_ADDR_LEN;
90094332d3Sopenharmony_ci    struct HdfFeatureInfo feature;
91094332d3Sopenharmony_ci    feature.ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
92094332d3Sopenharmony_ci    feature.type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
93094332d3Sopenharmony_ci
94094332d3Sopenharmony_ci    interface->GetDeviceMacAddress(interface, &feature, mac, &macLen,
95094332d3Sopenharmony_ci        *const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(rawData)));
96094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
97094332d3Sopenharmony_ci}
98094332d3Sopenharmony_ci
99094332d3Sopenharmony_civoid FuzzGetFeatureType(struct IWlanInterface *interface, const uint8_t *rawData)
100094332d3Sopenharmony_ci{
101094332d3Sopenharmony_ci    int32_t featureType;
102094332d3Sopenharmony_ci    struct HdfFeatureInfo feature;
103094332d3Sopenharmony_ci    feature.ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
104094332d3Sopenharmony_ci    feature.type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
105094332d3Sopenharmony_ci
106094332d3Sopenharmony_ci    interface->GetFeatureType(interface, &feature, &featureType);
107094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
108094332d3Sopenharmony_ci}
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_civoid FuzzGetFreqsWithBand(struct IWlanInterface *interface, const uint8_t *rawData)
111094332d3Sopenharmony_ci{
112094332d3Sopenharmony_ci    struct HdfFeatureInfo feature;
113094332d3Sopenharmony_ci    struct HdfWifiInfo wifiInfo;
114094332d3Sopenharmony_ci    int32_t freq[WLAN_FREQ_MAX_NUM] = {0};
115094332d3Sopenharmony_ci    uint32_t freqLen = WLAN_FREQ_MAX_NUM;
116094332d3Sopenharmony_ci    feature.ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
117094332d3Sopenharmony_ci    feature.type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
118094332d3Sopenharmony_ci    wifiInfo.band = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
119094332d3Sopenharmony_ci    wifiInfo.size = *const_cast<uint32_t *>(reinterpret_cast<const uint32_t *>(rawData));
120094332d3Sopenharmony_ci
121094332d3Sopenharmony_ci    interface->GetFreqsWithBand(interface, &feature, &wifiInfo, freq, &freqLen);
122094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
123094332d3Sopenharmony_ci}
124094332d3Sopenharmony_ci
125094332d3Sopenharmony_civoid FuzzGetNetworkIfaceName(struct IWlanInterface *interface, const uint8_t *rawData)
126094332d3Sopenharmony_ci{
127094332d3Sopenharmony_ci    char ifNames[IFNAMSIZ] = {0};
128094332d3Sopenharmony_ci    struct HdfFeatureInfo feature;
129094332d3Sopenharmony_ci    feature.ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
130094332d3Sopenharmony_ci    feature.type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
131094332d3Sopenharmony_ci
132094332d3Sopenharmony_ci    interface->GetNetworkIfaceName(interface, &feature, ifNames, IFNAMSIZ);
133094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
134094332d3Sopenharmony_ci}
135094332d3Sopenharmony_ci
136094332d3Sopenharmony_civoid FuzzSetMacAddress(struct IWlanInterface *interface, const uint8_t *rawData)
137094332d3Sopenharmony_ci{
138094332d3Sopenharmony_ci    struct HdfFeatureInfo feature;
139094332d3Sopenharmony_ci    feature.ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
140094332d3Sopenharmony_ci    feature.type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
141094332d3Sopenharmony_ci    const uint8_t *mac = rawData;
142094332d3Sopenharmony_ci    uint32_t macLen = ETH_ADDR_LEN;
143094332d3Sopenharmony_ci
144094332d3Sopenharmony_ci    interface->SetMacAddress(interface, &feature, mac, macLen);
145094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
146094332d3Sopenharmony_ci}
147094332d3Sopenharmony_ci
148094332d3Sopenharmony_civoid FuzzSetTxPower(struct IWlanInterface *interface, const uint8_t *rawData)
149094332d3Sopenharmony_ci{
150094332d3Sopenharmony_ci    struct HdfFeatureInfo feature;
151094332d3Sopenharmony_ci    feature.ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
152094332d3Sopenharmony_ci    feature.type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
153094332d3Sopenharmony_ci    int32_t power = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
154094332d3Sopenharmony_ci
155094332d3Sopenharmony_ci    interface->SetTxPower(interface, &feature, power);
156094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
157094332d3Sopenharmony_ci}
158094332d3Sopenharmony_ci
159094332d3Sopenharmony_civoid FuzzGetPowerMode(struct IWlanInterface *interface, const uint8_t *rawData)
160094332d3Sopenharmony_ci{
161094332d3Sopenharmony_ci    uint8_t mode = 0;
162094332d3Sopenharmony_ci    struct HdfFeatureInfo feature;
163094332d3Sopenharmony_ci    feature.ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
164094332d3Sopenharmony_ci    feature.type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
165094332d3Sopenharmony_ci
166094332d3Sopenharmony_ci    interface->GetPowerMode(interface, &feature, &mode);
167094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
168094332d3Sopenharmony_ci}
169094332d3Sopenharmony_ci
170094332d3Sopenharmony_civoid FuzzSetPowerMode(struct IWlanInterface *interface, const uint8_t *rawData)
171094332d3Sopenharmony_ci{
172094332d3Sopenharmony_ci    struct HdfFeatureInfo feature;
173094332d3Sopenharmony_ci    feature.ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
174094332d3Sopenharmony_ci    feature.type = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
175094332d3Sopenharmony_ci    uint8_t mode = *const_cast<uint8_t *>(rawData);
176094332d3Sopenharmony_ci
177094332d3Sopenharmony_ci    interface->SetPowerMode(interface, &feature, mode);
178094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
179094332d3Sopenharmony_ci}
180094332d3Sopenharmony_ci
181094332d3Sopenharmony_civoid FuzzGetIfNamesByChipId(struct IWlanInterface *interface, const uint8_t *rawData)
182094332d3Sopenharmony_ci{
183094332d3Sopenharmony_ci    uint32_t num = 0;
184094332d3Sopenharmony_ci    char ifNames[IFNAMSIZ] = {0};
185094332d3Sopenharmony_ci    uint8_t chipId = *const_cast<uint8_t *>(rawData);
186094332d3Sopenharmony_ci
187094332d3Sopenharmony_ci    interface->GetIfNamesByChipId(interface, chipId, ifNames, IFNAMSIZ, &num);
188094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
189094332d3Sopenharmony_ci}
190094332d3Sopenharmony_ci
191094332d3Sopenharmony_civoid FuzzResetDriver(struct IWlanInterface *interface, const uint8_t *rawData)
192094332d3Sopenharmony_ci{
193094332d3Sopenharmony_ci    uint8_t chipId = *const_cast<uint8_t *>(rawData);
194094332d3Sopenharmony_ci    const char *ifName = reinterpret_cast<const char *>(rawData);
195094332d3Sopenharmony_ci
196094332d3Sopenharmony_ci    interface->ResetDriver(interface, chipId, ifName);
197094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
198094332d3Sopenharmony_ci}
199094332d3Sopenharmony_ci
200094332d3Sopenharmony_civoid FuzzStartChannelMeas(struct IWlanInterface *interface, const uint8_t *rawData)
201094332d3Sopenharmony_ci{
202094332d3Sopenharmony_ci    const char *ifName = reinterpret_cast<const char *>(rawData);
203094332d3Sopenharmony_ci    struct MeasChannelParam measChannelParam;
204094332d3Sopenharmony_ci    measChannelParam.channelId = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
205094332d3Sopenharmony_ci    measChannelParam.measTime = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
206094332d3Sopenharmony_ci
207094332d3Sopenharmony_ci    interface->StartChannelMeas(interface, ifName, &measChannelParam);
208094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
209094332d3Sopenharmony_ci}
210094332d3Sopenharmony_ci
211094332d3Sopenharmony_civoid FuzzSetProjectionScreenParam(struct IWlanInterface *interface, const uint8_t *rawData)
212094332d3Sopenharmony_ci{
213094332d3Sopenharmony_ci    const char *ifName = reinterpret_cast<const char *>(rawData);
214094332d3Sopenharmony_ci    struct ProjectionScreenCmdParam param;
215094332d3Sopenharmony_ci    param.buf = const_cast<int8_t *>(reinterpret_cast<const int8_t *>(rawData));
216094332d3Sopenharmony_ci    param.bufLen = g_wlanTestSize;
217094332d3Sopenharmony_ci    param.cmdId = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
218094332d3Sopenharmony_ci
219094332d3Sopenharmony_ci    interface->SetProjectionScreenParam(interface, ifName, &param);
220094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
221094332d3Sopenharmony_ci}
222094332d3Sopenharmony_ci
223094332d3Sopenharmony_civoid FuzzWifiSendCmdIoctl(struct IWlanInterface *interface, const uint8_t *rawData)
224094332d3Sopenharmony_ci{
225094332d3Sopenharmony_ci    const char *ifName = reinterpret_cast<const char *>(rawData);
226094332d3Sopenharmony_ci    int32_t cmdId = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
227094332d3Sopenharmony_ci    int8_t *paramBuf = const_cast<int8_t *>(reinterpret_cast<const int8_t *>(rawData));
228094332d3Sopenharmony_ci
229094332d3Sopenharmony_ci    interface->WifiSendCmdIoctl(interface, ifName, cmdId, paramBuf, g_wlanTestSize);
230094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
231094332d3Sopenharmony_ci}
232094332d3Sopenharmony_ci
233094332d3Sopenharmony_civoid FuzzGetFeatureByIfName(struct IWlanInterface *interface, const uint8_t *rawData)
234094332d3Sopenharmony_ci{
235094332d3Sopenharmony_ci    struct HdfFeatureInfo featureInfo;
236094332d3Sopenharmony_ci    const char *ifName = reinterpret_cast<const char *>(rawData);
237094332d3Sopenharmony_ci
238094332d3Sopenharmony_ci    interface->GetFeatureByIfName(interface, ifName, &featureInfo);
239094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
240094332d3Sopenharmony_ci}
241094332d3Sopenharmony_ci
242094332d3Sopenharmony_civoid FuzzGetStaInfo(struct IWlanInterface *interface, const uint8_t *rawData)
243094332d3Sopenharmony_ci{
244094332d3Sopenharmony_ci    struct WifiStationInfo info;
245094332d3Sopenharmony_ci    const char *ifName = reinterpret_cast<const char *>(rawData);
246094332d3Sopenharmony_ci    const uint8_t *mac = rawData;
247094332d3Sopenharmony_ci    uint32_t macLen = ETH_ADDR_LEN;
248094332d3Sopenharmony_ci
249094332d3Sopenharmony_ci    interface->GetStaInfo(interface, ifName, &info, mac, macLen);
250094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
251094332d3Sopenharmony_ci}
252094332d3Sopenharmony_ci
253094332d3Sopenharmony_civoid FuzzResetToFactoryMacAddress(struct IWlanInterface *interface, const uint8_t *rawData)
254094332d3Sopenharmony_ci{
255094332d3Sopenharmony_ci    const char *ifName = const_cast<char *>(reinterpret_cast<const char *>(rawData));
256094332d3Sopenharmony_ci    interface->ResetToFactoryMacAddress(interface, ifName);
257094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
258094332d3Sopenharmony_ci}
259094332d3Sopenharmony_ci
260094332d3Sopenharmony_civoid FuzzGetChannelMeasResult(struct IWlanInterface *interface, const uint8_t *rawData)
261094332d3Sopenharmony_ci{
262094332d3Sopenharmony_ci    const char *ifName = reinterpret_cast<const char *>(rawData);
263094332d3Sopenharmony_ci    struct MeasChannelResult measChannelResult = {0};
264094332d3Sopenharmony_ci
265094332d3Sopenharmony_ci    interface->GetChannelMeasResult(interface, ifName, &measChannelResult);
266094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: success", __FUNCTION__);
267094332d3Sopenharmony_ci}
268