1 /*
2  * Copyright (c) 2021-2023 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 WIFI_DRIVER_CLIENT_H
17 #define WIFI_DRIVER_CLIENT_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27 
28 #undef LOG_TAG
29 #define LOG_TAG "HDF_WIFI_CORE"
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN 0xD001566
32 
33 #ifndef IFNAMSIZ
34 #define IFNAMSIZ 16
35 #endif
36 #define ETH_ADDR_LEN 6
37 #define WIFI_24G_CHANNEL_NUM 14
38 #define WIFI_MAX_CHANNEL_NUM 24
39 #define IEEE80211G_RATES_NUM 12
40 #define IEEE80211B_RATES_NUM 4
41 #define IEEE80211A_RATES_NUM 8
42 #define SIGNAL_LEVEL_CONFFICIENT 100
43 
44 #define WIFI_POWER_MODE_SLEEPING 0
45 #define WIFI_POWER_MODE_GENERAL 1
46 #define WIFI_POWER_MODE_THROUGH_WALL 2
47 #define WIFI_POWER_MODE_NUM 3
48 
49 #define INIT_SCAN_RES_NUM 200
50 #define MAX_SSID_LEN 33 // 32 + \0
51 #define MAX_BUF_LEN 32
52 
53 typedef enum {
54     CMD_CLOSE_GO_CAC,
55     CMD_SET_GO_CSA_CHANNEL,
56     CMD_SET_GO_RADAR_DETECT,
57     CMD_ID_MCC_STA_P2P_QUOTA_TIME,
58     CMD_ID_CTRL_ROAM_CHANNEL,
59     CMD_ID_RX_REMAIN_ON_CHANNEL,
60     CMD_ID_INSTALL_WLAN_KEY,
61 } ProjectionScreenCmd;
62 
63 /* common related interface */
64 enum WifiDriverClientResultCode {
65     RET_CODE_SUCCESS = 0,
66     RET_CODE_FAILURE = -1,
67     RET_CODE_NOT_SUPPORT = -2,
68     RET_CODE_INVALID_PARAM = -3,
69     RET_CODE_MISUSE = -4, /* incorrectly API used */
70     RET_CODE_NOT_AVAILABLE = -5,
71     RET_CODE_NOMEM = -6,
72     RET_CODE_UNKNOW = -7,
73     RET_CODE_DEVICE_BUSY = -16,
74     RET_CODE_NETDOWN = -211,
75 };
76 
77 enum WifiIfType {
78     WIFI_IFTYPE_UNSPECIFIED,
79     WIFI_IFTYPE_ADHOC,
80     WIFI_IFTYPE_STATION,
81     WIFI_IFTYPE_AP,
82     WIFI_IFTYPE_AP_VLAN,
83     WIFI_IFTYPE_WDS,
84     WIFI_IFTYPE_MONITOR,
85     WIFI_IFTYPE_MESH_POINT,
86     WIFI_IFTYPE_P2P_CLIENT,
87     WIFI_IFTYPE_P2P_GO,
88     WIFI_IFTYPE_P2P_DEVICE,
89     WIFI_IFTYPE_CHBA,
90     WIFI_IFTYPE_MAX,
91 };
92 
93 typedef enum {
94     WIFI_EVENT_NEW_STA = 0,
95     WIFI_EVENT_DEL_STA,
96     WIFI_EVENT_RX_MGMT,
97     WIFI_EVENT_TX_STATUS,
98     WIFI_EVENT_SCAN_DONE,
99     WIFI_EVENT_SCAN_RESULT = 5,
100     WIFI_EVENT_CONNECT_RESULT,
101     WIFI_EVENT_DISCONNECT,
102     WIFI_EVENT_MESH_CLOSE,
103     WIFI_EVENT_NEW_PEER_CANDIDATE,
104     WIFI_EVENT_REMAIN_ON_CHANNEL = 10,
105     WIFI_EVENT_CANCEL_REMAIN_ON_CHANNEL,
106     WIFI_EVENT_CHANNEL_SWITCH,
107     WIFI_EVENT_EAPOL_RECV,
108     WIFI_EVENT_TIMEOUT_DISCONN,
109     WIFI_EVENT_RESET_DRIVER = 15,
110     WIFI_EVENT_SCAN_RESULTS,
111     WIFI_EVENT_SCAN_ABORTED,
112     WIFI_EVENT_BUTT,
113     WIFI_EVENT_ACTION_RECEIVED,
114     WIFI_EVENT_DATA_FRAME_RECEIVED,
115 } WifiEventType;
116 
117 typedef enum {
118     CMD_HID2D_MODULE_INIT,
119     CMD_SET_BATTERY_LEVEL,
120     CMD_SET_SUPP_COEX_CHAN_LIST,
121     CMD_SET_CHAN_ADJUST
122 } Hid2dCmdType;
123 
124 typedef struct {
125     int32_t reassoc;
126     uint32_t ieLen;
127     uint8_t *ie;
128     uint8_t *macAddr;
129     uint8_t resv[2];
130 } WifiNewStaInfo;
131 
132 typedef struct {
133     uint8_t *buf;
134     uint32_t len;
135     int32_t sigMbm;
136     int32_t freq;
137 } WifiRxMgmt;
138 
139 typedef struct {
140     uint8_t *buf;
141     uint32_t len;
142     uint8_t ack;
143     uint8_t resv[3];
144 } WifiTxStatus;
145 
146 typedef struct {
147     uint32_t flags;
148     uint8_t *bssid;
149     uint16_t caps;
150     uint32_t freq;
151     uint16_t beaconInt;
152     int32_t qual;
153     int32_t level;
154     uint32_t age;
155     uint64_t tsf;
156     uint32_t ieLen;
157     uint32_t beaconIeLen;
158     uint8_t *variable;
159     uint8_t *ie;
160     uint8_t *beaconIe;
161 } WifiScanResult;
162 
163 typedef struct {
164     WifiScanResult *scanResult;
165     uint32_t scanResultCapacity;
166     uint32_t num;
167 } WifiScanResults;
168 
169 typedef struct {
170     uint8_t *reqIe;
171     uint32_t reqIeLen;
172     uint8_t *respIe;
173     uint32_t respIeLen;
174     uint8_t *bssid;
175     uint8_t rsv[2];
176     uint16_t status;
177     uint16_t freq;
178 } WifiConnectResult;
179 
180 typedef struct {
181     uint8_t *ie;
182     uint16_t reason;
183     uint8_t rsv[2];
184     uint32_t ieLen;
185 } WifiDisconnect;
186 
187 typedef struct {
188     uint8_t id;
189     int32_t suite;
190     uint8_t addr[ETH_ADDR_LEN];
191     uint32_t len;
192     uint8_t buf[MAX_BUF_LEN];
193 } InstallWlanParam;
194 
195 enum WifiClientType {
196     /* 1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7 | 1<<10 | 1<<11 | 1<<13 */
197     WIFI_KERNEL_TO_WPA_CLIENT = 11519,
198     /* 1<<20 | 1<<19 | 1<<17 | 1<<16 | 1<<15 | 1<<5 | 1<<4 */
199     WIFI_KERNEL_TO_HAL_CLIENT = 1802288,
200     WIFI_CLIENT_BUTT
201 };
202 
203 typedef int32_t (*OnReceiveFunc)(uint32_t event, void *data, const char *ifName);
204 typedef int32_t (*Hid2dCallback)(const uint8_t *recvMsg, uint32_t recvMsgLen);
205 
206 int32_t WifiRegisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName);
207 void WifiUnregisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName);
208 int32_t WifiRegisterHid2dCallback(Hid2dCallback func, const char *ifName);
209 void WifiUnregisterHid2dCallback(Hid2dCallback func, const char *ifName);
210 
211 /* hal related interface */
212 #define MAX_WLAN_DEVICE 3
213 #define MAX_CHANNEL_NUM 14
214 
215 enum Ieee80211Band {
216     IEEE80211_BAND_2GHZ,  /**< 2.4 GHz */
217     IEEE80211_BAND_5GHZ,  /**< 5 GHz */
218     IEEE80211_NUM_BANDS   /**< Reserved */
219 };
220 
221 struct NetworkInfo {
222     char name[IFNAMSIZ];
223     uint8_t supportMode[WIFI_IFTYPE_MAX];
224 };
225 
226 #define MAX_IFACE_NUM 20
227 
228 struct NetworkInfoResult {
229     uint32_t nums;
230     struct NetworkInfo infos[MAX_IFACE_NUM];
231 };
232 
233 struct FreqInfoResult {
234     int32_t band;
235     uint32_t nums;
236     uint32_t *freqs;
237     uint32_t *txPower;
238 };
239 
240 #define MAX_ASSOC_STA_NUM 8
241 
242 struct AssocStaInfo {
243     uint8_t mac[ETH_ADDR_LEN];  /**< MAC address of the STA */
244 };
245 
246 struct AssocStaInfoResult {
247     uint32_t num;
248     struct AssocStaInfo infos[MAX_ASSOC_STA_NUM];
249 };
250 
251 struct NetDeviceInfo {
252     uint32_t index;
253     char ifName[IFNAMSIZ];
254     uint8_t iftype;
255     uint8_t mac[ETH_ADDR_LEN];
256 };
257 
258 #define MAX_NETDEVICE_COUNT 20
259 
260 struct NetDeviceInfoResult {
261     struct NetDeviceInfo deviceInfos[MAX_NETDEVICE_COUNT];
262 };
263 
264 struct MeasParam {
265     int32_t channelId;
266     int32_t measTime;
267 };
268 
269 struct MeasResult {
270     int32_t channelId;
271     int32_t chload;
272     int32_t noise;
273 };
274 
275 typedef struct {
276     int32_t cmdId;
277     uint32_t bufLen;
278     int8_t buf[0];
279 } ProjectionScreenParam;
280 
281 typedef struct {
282     uint32_t rxRate;
283     uint32_t txRate;
284     uint32_t flags; /* bitfield of STA_DRV_DATA_* */
285     uint8_t rxVhtmcs;
286     uint8_t txVhtmcs;
287     uint8_t rxMcs;
288     uint8_t txMcs;
289     uint8_t rxVhtNss;
290     uint8_t txVhtNss;
291 } __attribute__ ((aligned(8))) StationInfo;
292 
293 #define BSS_STATUS_ASSOCIATED 1
294 typedef struct {
295     uint8_t associatedBssid[ETH_ADDR_LEN];
296     uint32_t associatedFreq;
297 } AssociatedInfo;
298 
299 struct SignalResult {
300     int32_t currentRssi;
301     int32_t associatedFreq;
302     int32_t txBitrate;
303     int32_t rxBitrate;
304     int32_t currentNoise;
305     int32_t currentSnr;
306     int32_t currentChload;
307     int32_t currentUlDelay;
308     int32_t currentTxBytes;
309     int32_t currentRxBytes;
310     int32_t currentTxFailed;
311     int32_t currentTxPackets;
312     int32_t currentRxPackets;
313 };
314 
315 typedef struct {
316     uint8_t ssid[MAX_SSID_LEN];
317     uint32_t ssidLen;
318 } WifiDriverScanSsid;
319 
320 typedef struct {
321     bool isHidden;
322     int32_t *freqs;
323     uint32_t freqsLen;
324     WifiDriverScanSsid ssid;
325 } WifiPnoNetwork;
326 
327 typedef struct {
328     int32_t min2gRssi;
329     int32_t min5gRssi;
330     int32_t scanIntervalMs;
331     int32_t scanIterations;
332     WifiPnoNetwork *pnoNetworks;
333     uint32_t pnoNetworksLen;
334 } WifiPnoSettings;
335 
336 int32_t WifiDriverClientInit(void);
337 void WifiDriverClientDeinit(void);
338 
339 int32_t GetUsableNetworkInfo(struct NetworkInfoResult *result);
340 int32_t IsSupportCombo(uint8_t *isSupportCombo);
341 int32_t GetComboInfo(uint64_t *comboInfo, uint32_t size);
342 int32_t SetMacAddr(const char *ifName, unsigned char *mac, uint8_t len);
343 int32_t GetDevMacAddr(const char *ifName, int32_t type, uint8_t *mac, uint8_t len);
344 int32_t GetValidFreqByBand(const char *ifName, int32_t band,
345     struct FreqInfoResult *result, uint32_t size);
346 int32_t SetTxPower(const char *ifName, int32_t power);
347 int32_t GetAssociatedStas(const char *ifName, struct AssocStaInfoResult *result);
348 int32_t WifiSetCountryCode(const char *ifName, const char *code, uint32_t len);
349 int32_t SetScanMacAddr(const char *ifName, uint8_t *scanMac, uint8_t len);
350 int32_t AcquireChipId(const char *ifName, uint8_t *chipId);
351 int32_t GetIfNamesByChipId(const uint8_t chipId, char **ifNames, uint32_t *num);
352 int32_t SetResetDriver(const uint8_t chipId, const char *ifName);
353 int32_t GetNetDeviceInfo(struct NetDeviceInfoResult *netDeviceInfoResult);
354 int32_t GetCurrentPowerMode(const char *ifName, uint8_t *mode);
355 int32_t SetPowerMode(const char *ifName, uint8_t mode);
356 int32_t StartChannelMeas(const char *ifName, const struct MeasParam *measParam);
357 int32_t GetChannelMeasResult(const char *ifName, struct MeasResult *measResult);
358 int32_t SetProjectionScreenParam(const char *ifName, const ProjectionScreenParam *param);
359 int32_t SendCmdIoctl(const char *ifName, int32_t cmdId, const int8_t *paramBuf, uint32_t paramBufLen);
360 int32_t GetStationInfo(const char *ifName, StationInfo *info, const uint8_t *mac, uint32_t macLen);
361 int32_t WifiStartPnoScan(const char *ifName, const WifiPnoSettings *pnoSettings);
362 int32_t WifiStopPnoScan(const char *ifName);
363 int32_t WifiGetSignalPollInfo(const char *ifName, struct SignalResult *signalResult);
364 int32_t ClientGetApBandwidth(const char *ifName, uint8_t *bandwidth);
365 int32_t WifiSendActionFrame(const char *ifName, uint32_t txChannel, const uint8_t *frameData, uint32_t frameDataLen);
366 int32_t WifiRegisterActionFrameReceiver(const char *ifName, const uint8_t *match, uint32_t matchLen);
367 int32_t WifiSetPowerSaveMode(const char *ifName, int32_t frequency, int32_t mode);
368 int32_t WifiSetDpiMarkRule(int32_t uid, int32_t protocol, int32_t enable);
369 int32_t WifiInstallWlanExtParam(const char *ifName, const InstallWlanParam *param);
370 /* wpa related interface */
371 #define MAX_NR_CIPHER_SUITES 5
372 #define MAX_NR_AKM_SUITES 2
373 
374 typedef struct {
375     int32_t mode;
376     int32_t freq;
377     int32_t channel;
378 
379     /* for HT */
380     int32_t htEnabled;
381 
382     /*
383      * 0 = HT40 disabled, -1 = HT40 enabled,
384      * secondary channel below primary, 1 = HT40
385      * enabled, secondary channel above primary
386      */
387     int32_t secChannelOffset;
388 
389     /* for VHT */
390     int32_t vhtEnabled;
391 
392     /*
393      * valid for both HT and VHT, center_freq2 is non-zero
394      * only for bandwidth 80 and an 80+80 channel
395      */
396     int32_t centerFreq1;
397     int32_t centerFreq2;
398     int32_t bandwidth;
399     uint8_t band;
400 } WifiFreqParams;
401 
402 typedef struct {
403     uint32_t headLen;
404     uint32_t tailLen;
405     uint8_t *head;
406     uint8_t *tail;
407 } WifiBeaconData;
408 
409 typedef struct {
410     WifiFreqParams freqParams;
411     WifiBeaconData beaconData;
412     uint32_t ssidLen;
413     int32_t beaconInterval;
414     int32_t dtimPeriod;
415     uint8_t *ssid;
416     uint8_t hiddenSsid;
417     uint8_t authType;
418     uint32_t meshSsidLen;
419     uint8_t *meshSsid;
420 } WifiApSetting;
421 
422 typedef struct {
423     uint8_t *buf;
424     uint32_t len;
425 } WifiRxEapol;
426 
427 typedef struct {
428     uint32_t freq;
429     uint32_t dataLen;
430     uint8_t *data;
431     uint64_t *cookie;
432 } WifiMlmeData;
433 
434 typedef struct {
435     int32_t type;
436     uint32_t keyIdx;
437     uint32_t keyLen;
438     uint32_t seqLen;
439     uint32_t cipher;
440     uint8_t *addr;
441     uint8_t *key;
442     uint8_t *seq;
443     uint8_t def;
444     uint8_t defMgmt;
445     uint8_t defaultTypes;
446     uint8_t resv;
447 } WifiKeyExt;
448 
449 typedef struct {
450     uint8_t bssid[ETH_ADDR_LEN];
451     uint8_t iftype;
452     uint8_t resv;
453 } WifiSetMode;
454 
455 typedef struct {
456     uint16_t channel;
457     uint32_t freq;
458     uint32_t flags;
459 } WifiIeee80211Channel;
460 
461 typedef struct {
462     int32_t channelNum;          /**< Number of channels */
463     WifiIeee80211Channel iee80211Channel[WIFI_MAX_CHANNEL_NUM]; /**< WLAN channel structures */
464 }WlanBands;
465 
466 typedef struct {
467     int32_t channelNum;
468     uint16_t bitrate[12];
469     uint16_t htCapab;
470     uint8_t resv[2];
471     WlanBands bands[IEEE80211_NUM_BANDS];
472 } WifiHwFeatureData;
473 
474 typedef struct {
475     WifiDriverScanSsid *ssids;
476     int32_t *freqs;
477     uint8_t *extraIes;
478     uint8_t *bssid;
479     uint8_t numSsids;
480     uint8_t numFreqs;
481     uint8_t prefixSsidScanFlag;
482     uint8_t fastConnectFlag;
483     int32_t extraIesLen;
484 } WifiScan;
485 
486 typedef struct {
487     uint32_t wpaVersions;
488     uint32_t cipherGroup;
489     int32_t nCiphersPairwise;
490     uint32_t ciphersPairwise[MAX_NR_CIPHER_SUITES];
491     int32_t nAkmSuites;
492     uint32_t akmSuites[MAX_NR_AKM_SUITES];
493 } WifiCryptoSetting;
494 
495 typedef struct {
496     uint8_t *bssid;
497     uint8_t *ssid;
498     uint8_t *ie;
499     uint8_t *key;
500     uint8_t authType;
501     uint8_t privacy;
502     uint8_t keyLen;
503     uint8_t keyIdx;
504     uint8_t mfp;
505     uint8_t rsv[3];
506     uint32_t freq;
507     uint32_t ssidLen;
508     uint32_t ieLen;
509     WifiCryptoSetting *crypto;
510 } WifiAssociateParams;
511 
512 typedef enum {
513     WIFI_PHY_MODE_11N = 0,
514     WIFI_PHY_MODE_11G = 1,
515     WIFI_PHY_MODE_11B = 2,
516     WIFI_PHY_MODE_BUTT
517 } WifiPhyMode;
518 
519 typedef struct {
520     uint8_t status;
521     enum WifiIfType ifType;
522     WifiPhyMode mode;
523 } WifiSetNewDev;
524 
525 typedef struct {
526     uint8_t dst[ETH_ADDR_LEN];
527     uint8_t src[ETH_ADDR_LEN];
528     uint8_t bssid[ETH_ADDR_LEN];
529     uint8_t *data;
530     uint32_t dataLen;
531     uint32_t freq;
532     uint32_t wait;
533     int32_t noCck;
534 } WifiActionData;
535 
536 typedef struct {
537     uint8_t *data;
538     uint32_t dataLen;
539 } WifiDataFrame;
540 
541 typedef struct {
542     uint32_t freq;
543     uint32_t duration;
544 } WifiOnChannel;
545 
546 
547 typedef struct {
548     uint8_t ifName[IFNAMSIZ];
549     uint8_t type;
550 } WifiIfAdd;
551 
552 typedef struct {
553     uint8_t ifName[IFNAMSIZ];
554 } WifiIfRemove;
555 
556 typedef struct {
557     uint32_t ieLen;
558     uint8_t appIeType;
559     uint8_t rsv[3];
560     uint8_t *ie;
561 } WifiAppIe;
562 
563 typedef struct {
564     uint64_t drvFlags;
565 } WifiGetDrvFlags;
566 
567 int32_t WifiEapolPacketSend(const char *ifName, const uint8_t *srcAddr, const uint8_t *dstAddr, uint8_t *buf,
568     uint32_t length);
569 int32_t WifiEapolPacketReceive(const char *ifName, WifiRxEapol *rxEapol);
570 int32_t WifiEapolEnable(const char *ifName);
571 int32_t WifiEapolDisable(const char *ifName);
572 int32_t WifiCmdSetAp(const char *ifName, WifiApSetting *apsettings);
573 int32_t WifiCmdChangeBeacon(const char *ifName, WifiApSetting *apsettings);
574 int32_t WifiCmdSendMlme(const char *ifName, WifiMlmeData *mlme);
575 int32_t WifiCmdDelKey(const char *ifName, WifiKeyExt *keyExt);
576 int32_t WifiCmdNewKey(const char *ifName, WifiKeyExt *keyExt);
577 int32_t WifiCmdSetKey(const char *ifName, WifiKeyExt *keyExt);
578 int32_t WifiCmdSetMode(const char *ifName, WifiSetMode *setMode);
579 int32_t WifiCmdGetOwnMac(const char *ifName, char *buf, uint32_t len);
580 int32_t WifiCmdGetHwFeature(const char *ifName, WifiHwFeatureData *hwFeatureData);
581 int32_t WifiCmdScan(const char *ifName, WifiScan *scan);
582 int32_t WifiCmdDisconnet(const char *ifName, int32_t reasonCode);
583 int32_t WifiCmdAssoc(const char *ifName, WifiAssociateParams *assocParams);
584 int32_t WifiCmdSetNetdev(const char *ifName, WifiSetNewDev *info);
585 int32_t WifiCmdStaRemove(const char *ifName, const uint8_t *addr, uint32_t addrLen);
586 int32_t WifiCmdSendAction(const char *ifName, WifiActionData *actionData);
587 int32_t WifiCmdSetClient(uint32_t clientNum);
588 int32_t WifiCmdProbeReqReport(const char *ifName, const int32_t *report);
589 int32_t WifiCmdRemainOnChannel(const char *ifName, const WifiOnChannel *onChannel);
590 int32_t WifiCmdCancelRemainOnChannel(const char *ifName);
591 int32_t WifiCmdAddIf(const char *ifName, const WifiIfAdd *ifAdd);
592 int32_t WifiCmdRemoveIf(const char *ifName, const WifiIfRemove *ifRemove);
593 int32_t WifiCmdSetApWpsP2pIe(const char *ifName, const WifiAppIe *appIe);
594 int32_t WifiCmdGetDrvFlags(const char *ifName, WifiGetDrvFlags *params);
595 #ifdef __cplusplus
596 #if __cplusplus
597 }
598 #endif
599 #endif
600 
601 #endif /* end of wifi_driver_client.h */
602