1 /*
2  * Copyright (C) 2021 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_SCAN_SERVICE_H
16 #define OHOS_WIFI_SCAN_SERVICE_H
17 
18 #include <string>
19 #include <map>
20 #include <mutex>
21 #include <ctime>
22 #include <set>
23 #include <math.h>
24 #include <unordered_set>
25 #include "iscan_service_callbacks.h"
26 #include "define.h"
27 #ifndef OHOS_ARCH_LITE
28 #include "wifi_country_code_manager.h"
29 #endif
30 #include "wifi_scan_msg.h"
31 #include "wifi_errcode.h"
32 #include "scan_common.h"
33 #include "scan_monitor.h"
34 #include "scan_state_machine.h"
35 #include "ienhance_service.h"
36 
37 namespace OHOS {
38 namespace Wifi {
39 const int DISCONNECTED_SCAN_INTERVAL = 20 * 60 * 1000;
40 const int RESTART_PNO_SCAN_TIME = 5 * 1000;
41 const int RESTART_SYSTEM_SCAN_TIME = 2 * 1000;
42 const int FREQS_24G_MAX_VALUE = 2500;
43 const int FREQS_5G_MIN_VALUE = 5000;
44 const int SECOND_TO_MICRO_SECOND = 1000000;
45 const int MAX_PNO_SCAN_FAILED_NUM = 5;
46 const int MAX_SYSTEM_SCAN_FAILED_NUM = 5;
47 const int DOUBLE_SCAN_INTERVAL = 2;
48 const int SYSTEM_SCAN_INIT_TIME = 10;
49 const int SYSTEM_SCAN_INTERVAL_ONE_HOUR = 60 * 60;
50 const int SYSTEM_SCAN_INTERVAL_FIVE_MINUTE = 5 * 60;
51 const int SYSTEM_SCAN_INTERVAL_160_SECOND = 160;
52 const int SYSTEM_SCAN_INTERVAL_10_SECOND = 10;
53 const int SYSTEM_SCAN_INTERVAL_30_SECOND = 30;
54 const int SYSTEM_SCAN_INTERVAL_60_SECOND = 60;
55 const int SYSTEM_SCAN_COUNT_3_TIMES = 3;
56 const int DEFAULT_PNO_SCAN_INTERVAL = 300;
57 
58 const int TONE_PER_SYM_11ABG = 48;
59 const int TONE_PER_SYM_11N_20MHZ = 52;
60 const int TONE_PER_SYM_11N_40MHZ = 108;
61 const int TONE_PER_SYM_11AC_20MHZ = 52;
62 const int TONE_PER_SYM_11AC_40MHZ = 108;
63 const int TONE_PER_SYM_11AC_80MHZ = 234;
64 const int TONE_PER_SYM_11AC_160MHZ = 468;
65 const int TONE_PER_SYM_11AX_20MHZ = 234;
66 const int TONE_PER_SYM_11AX_40MHZ = 468;
67 const int TONE_PER_SYM_11AX_80MHZ = 980;
68 const int TONE_PER_SYM_11AX_160MHZ = 1960;
69 
70 const int SYM_DURATION_11ABG_NS = 4000;
71 // 11n OFDM symbol duration in ns with 0.4us guard interval
72 const int SYM_DURATION_11N_NS = 3600;
73 // 11ac OFDM symbol duration in ns with 0.4us guard interval
74 const int SYM_DURATION_11AC_NS = 3600;
75 const int SYM_DURATION_11AX_NS = 13600;
76 const int MICRO_TO_NANO_RATIO = 1000;
77 
78 const int BIT_PER_TONE_SCALE = 1000;
79 const int MAX_BITS_PER_TONE_11ABG = (int) round((6 * 3.0 * BIT_PER_TONE_SCALE) / 4.0);
80 const int MAX_BITS_PER_TONE_11N = (int) round((6 * 5.0 * BIT_PER_TONE_SCALE) / 6.0);
81 const int MAX_BITS_PER_TONE_11AC = (int) round((8 * 5.0 * BIT_PER_TONE_SCALE) / 6.0);
82 const int MAX_BITS_PER_TONE_11AX = (int) round((10 * 5.0 * BIT_PER_TONE_SCALE) / 6.0);
83 
84 const int TWO_DB = 3;
85 const int SNR_BIT_PER_TONE_HIGH_SNR_SCALE = BIT_PER_TONE_SCALE / TWO_DB;
86 const int SNR_BIT_PER_TONE_LUT_MIN = -10; // minimum snrDb supported by LUT
87 const int SNR_BIT_PER_TONE_LUT_MAX = 9;   // maximum snrDb supported by LUT
88 const int SNR_BIT_PER_TONE_LUT[] = {0, 171, 212, 262, 323, 396, 484, 586,
89                                           706, 844, 1000, 1176, 1370, 1583, 1812, 2058, 2317, 2588, 2870, 3161};
90 const int NOISE_FLOOR_20MHZ_DBM = -96;
91 
92 const int SNR_MARGIN_DB = 16;
93 const int MAX_NUM_SPATIAL_STREAM_11AX = 8;
94 const int MAX_NUM_SPATIAL_STREAM_11AC = 8;
95 const int MAX_NUM_SPATIAL_STREAM_11N = 4;
96 const int MAX_NUM_SPATIAL_STREAM_11ABG = 1;
97 
98 const int B_MODE_MAX_MBPS = 11;
99 const int MAX_CHANNEL_UTILIZATION = 255;
100 const int MAX_RSSI = 200;
101 
102 const int MAX_RX_SPATIAL_STREAMS = 2;
103 const int MAX_TX_SPATIAL_STREAMS = 2;
104 
105 inline const int LOCATOR_SA_UID = 1021;
106 
107 int WifiMaxThroughput(int wifiStandard, bool is11bMode,
108                       WifiChannelWidth channelWidth, int rssiDbm, int maxNumSpatialStream, int channelUtilization);
109 
110 class ScanService {
111     FRIEND_GTEST(ScanService);
112 public:
113     explicit ScanService(int instId = 0);
114     virtual ~ScanService();
115     /**
116      * @Description  Initializing the Scan Service.
117      *
118      * @param scanSerivceCallbacks Callback function registered with the wifiManager[in].
119      * @return success: true, failed: false
120      */
121     virtual bool InitScanService(const IScanSerivceCallbacks &scanSerivceCallbacks);
122     /**
123      * @Description Stopping the Scan Service.
124      *
125      */
126     virtual void UnInitScanService();
127     /**
128      * @Description Registers the callback function of the scanning module to the interface service.
129      *
130      * @param scanSerivceCallbacks callback function
131      */
132     virtual void RegisterScanCallbacks(const IScanSerivceCallbacks &iScanSerivceCallbacks);
133     /**
134      * @Description Start a complete Wi-Fi scan.
135      *
136      * @param externFlag - Externally initiated scanning[in]
137      * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED
138      */
139     virtual ErrCode Scan(ScanType scanType);
140     /**
141      * @Description Start Wi-Fi scanning based on specified parameters.
142      *
143      * @param params - Scan specified parameters[in]
144      * @return success: WIFI_OPT_SUCCESS, failed: WIFI_OPT_FAILED
145      */
146     virtual ErrCode ScanWithParam(const WifiScanParams &params, ScanType scanType);
147     /**
148      * @Description Disable/Restore the scanning operation.
149      *
150      * * @param params - disable or not.
151      * @return WIFI_OPT_SUCCESS: success, WIFI_OPT_FAILED: failed
152      */
153     ErrCode DisableScan(bool disable);
154     /**
155      * @Description Start/Stop pno scan
156      *
157      * @param isStartAction - true:start pno scan; false:stop pno scan
158      * @param periodMs - pno scan interval
159      * @param suspendReason - pno scan suspent reason
160      * @return WIFI_OPT_SUCCESS: success, WIFI_OPT_FAILED: failed
161      */
162     ErrCode StartWifiPnoScan(bool isStartAction, int periodMs, int suspendReason);
163     /**
164      * @Description Starting a Single Scan.
165      *
166      * @param scanConfig - Scanning parameters[in]
167      * @return success: true, failed: false
168      */
169     bool SingleScan(ScanConfig &scanConfig);
170     /**
171      * @Description  Start PNO scanning
172      * @return success: true, failed: false
173      */
174     bool BeginPnoScan();
175     /**
176      * @Description Start PNO scanning
177      *
178      * @param pnoScanConfig - PNO scanning parameters[in]
179      * @param interScanConfig - common scanning parameters[in]
180      * @return success: true, failed: false
181      */
182     bool PnoScan(const PnoScanConfig &pnoScanConfig, const InterScanConfig &interScanConfig);
183     /**
184      * @Description Disable PNO scanning.
185      *
186      */
187     void EndPnoScan();
188     /**
189      * @Description Stop pno scan and clear local resource.
190      */
191     void StopPnoScan();
192     /**
193      * @Description The system scans and selects a scanning mode
194      *              based on the current screen status and STA status.
195      *
196      * @param scanAtOnce - Whether to start scanning immediately[in]
197      */
198     virtual void SystemScanProcess(bool scanAtOnce);
199     /**
200      * @Description Status reported by the state machine.
201      *
202      * @param scanStatusReport - Structure of the reported status.[in]
203      */
204     virtual void HandleScanStatusReport(ScanStatusReport &scanStatusReport);
205     /**
206      * @Description Internal event reporting and processing.
207      *
208      * @param innerEvent - Internal event[in]
209      */
210     void HandleInnerEventReport(ScanInnerEventType innerEvent);
211     /**
212      * @Description Screen State (On/Off) Change Handler
213      *
214      */
215     virtual void HandleScreenStatusChanged();
216     /**
217      * @Description STA status change processing
218      *
219      * @param state - STA state[in]
220      */
221     virtual void HandleStaStatusChanged(int status);
222     /**
223      * @Description Network quality change processing
224      *
225      * @param state - Network quality[in]
226      */
227     virtual void HandleNetworkQualityChanged(int status);
228     /**
229      * @Description movingfreeze status change processing
230      *
231      */
232     virtual void HandleMovingFreezeChanged();
233     /**
234      * @Description custom scene status change processing
235      *
236      * @param customScene custom scene[in]
237      * @param customSceneStatus custom scene status[in]
238      */
239     virtual void HandleCustomStatusChanged(int customScene, int customSceneStatus);
240     /**
241      * @Description Get custom scene state.
242      *
243      * @param customState custom scene state map[out]
244      * @return
245      */
246     virtual void HandleGetCustomSceneState(std::map<int, time_t>& sceneMap) const;
247     /**
248      * @Description Handle auto connect state.
249      *
250      * @param success auto connect state[in]
251      * @return
252      */
253     virtual void HandleAutoConnectStateChanged(bool success);
254     /**
255      * @Description Query and save the scan control policy.
256      *
257      */
258     virtual void GetScanControlInfo();
259     /**
260      * @Description When scanning control changes, the count data needs to be cleared.
261      *
262      */
263     void ClearScanControlValue();
264     /**
265      * @Description When scanning control changes, the count data needs to be cleared.
266      *
267      */
268     virtual void SetStaCurrentTime();
269     /**
270      * @Description Set EnhanceService to Scan Service.
271      *
272      * @param enhanceService IEnhanceService object
273      * @return void
274      */
275     virtual void SetEnhanceService(IEnhanceService* enhanceService);
276     /**
277      * @Description Init chipset info.
278      */
279     virtual void InitChipsetInfo();
280     /**
281      * @Description  SetNetworkInterfaceUpDown
282      *
283      * @Output: Return operating results to Interface Service after set iface up dwon
284                successfully through callback function instead of returning
285                result immediately.
286      * @Return success: WIFI_OPT_SUCCESS  fail: WIFI_OPT_FAILED
287      */
288     virtual ErrCode SetNetworkInterfaceUpDown(bool upDown);
289 
290 private:
291     using ScanConfigMap = std::map<int, StoreScanConfig>;
292     using ScanInfoHandlerMap = std::map<std::string, ScanInfoHandler>;
293     using PnoScanInfoHandlerMap = std::map<std::string, PnoScanInfoHandler>;
294 
295     std::shared_mutex mScanCallbackMutex;
296     IScanSerivceCallbacks mScanSerivceCallbacks;
297     ScanStateMachine *pScanStateMachine;             /* Scanning state machine pointer */
298     ScanMonitor *pScanMonitor;                       /* Scanning Monitor Pointer */
299     bool scanStartedFlag;                            /* The scanning is started */
300     ScanInfoHandlerMap scanInfoHandlerMap;              /* Map of obtaining the scanning result */
301     PnoScanInfoHandlerMap pnoScanInfoHandlerMap;        /* Map of obtaining PNO scanning results */
302     ScanConfigMap scanConfigMap;                     /* Save Scan Configuration */
303     int scanConfigStoreIndex;                        /* Index for saving the scan configuration */
304     int64_t pnoScanStartTime;                        /* PNO scanning start time */
305     int staStatus;                                   /* STA state */
306     bool isPnoScanBegined;                           /* The PNO scanning has been started */
307     bool autoNetworkSelection;                       /* Automatic network selection */
308     int64_t lastSystemScanTime;                      /* Last System Scan Time */
309     int pnoScanFailedNum;                            /* Number of PNO Scan Failures */
310     std::atomic<int> systemScanFailedNum;
311     ScanControlInfo scanControlInfo;                 /* Scan Control Policy */
312     std::atomic<bool> disableScanFlag {false};       /* Disable Scan Flag. */
313     std::vector<int> freqs2G;                        /* The support frequencys for 2.4G */
314     std::vector<int> freqs5G;                        /* The support frequencys for 5G */
315     std::vector<int> freqsDfs;                       /* The support frequencys for DFS */
316     SystemScanIntervalMode systemScanIntervalMode;   /* Store system scan data */
317     PnoScanIntervalMode pnoScanIntervalMode;         /* Store pno scan data */
318     time_t customCurrentTime; /* Indicates the time when the STA enters the Customer-defined scenario */
319     std::vector<SingleAppForbid> appForbidList; /* Store extern app scan data */
320     /*
321      * If the number of consecutive count times is less than the value of
322      * interval, the user is added to the blocklist and cannot be scanned.
323      */
324     std::vector<int> scanBlocklist;
325     /* Stores data that is scanned and controlled regardless of applications. */
326     std::vector<SingleAppForbid> fullAppForbidList;
327     std::map<int, time_t> customSceneTimeMap; /* Record the time when a scene is entered. */
328     std::vector<std::string> scan_thermal_trust_list;
329     std::vector<std::string> scan_frequency_trust_list;
330     std::vector<std::string> scan_screen_off_trust_list;
331     std::vector<std::string> scan_gps_block_list;
332     std::vector<std::string> scan_hid2d_list;
333     int customSceneForbidCount;
334     mutable std::mutex scanConfigMapMutex;
335     mutable std::mutex scanControlInfoMutex;
336     bool scanTrustMode;                              /* scan trustlist mode */
337     std::unordered_set<int> scanTrustSceneIds;       /* scan scene ids */
338     bool lastFreezeState;                            /* last freeze state. */
339     bool isAbsFreezeScaned;                          /* scanned in freeze state. */
340     int scanResultBackup;                            /* scan result backup. */
341     IEnhanceService *mEnhanceService;                /* EnhanceService handle */
342     int m_instId;
343     int lastNetworkQuality;
344     int chipsetCategory;
345     int chipsetFeatrureCapability;
346     bool isChipsetInfoObtained;
347     /**
348      * @Description Obtains the frequency of a specified band.
349      *
350      * @param band - Scan frequency bands. Obtain the frequency list based on the frequency bands.[in]
351      * @param freqs - Frequency list[out]
352      * @return success: true, failed: false
353      */
354     bool GetBandFreqs(ScanBandType band, std::vector<int> &freqs);
355     /**
356      * @Description Enter the scanning message body.
357      *
358      * @param interMessage - Message pointer[in]
359      * @param interConfig - Scan Configuration[in]
360      * @return success: true, failed: false
361      */
362     bool AddScanMessageBody(InternalMessagePtr interMessage, const InterScanConfig &interConfig);
363     /**
364      * @Description Save Request Configuration
365      *
366      * @param scanConfig - Scanning parameters[in]
367      * @param interConfig - Internal Scanning Parameters[in]
368      * @return success: Saved request index, failed: MAX_SCAN_CONFIG_STORE_INDEX
369      */
370     int StoreRequestScanConfig(const ScanConfig &scanConfig, const InterScanConfig &interConfig);
371 
372     int GetWifiMaxSupportedMaxSpeed(const InterScanInfo &scanInfo, const int &maxNumberSpatialStreams);
373     /**
374      * @Description Convert InterScanInfo to WifiScanInfo
375      *
376      * @param scanInfo - Scanning Result[in]
377      * @param interConfig - Internal Scanning Result[in]
378      */
379     void ConvertScanInfo(WifiScanInfo &scanInfo, const InterScanInfo &interInfo);
380     /**
381      * @Description Merge scan result
382      *
383      * @param results - Scanning Result Cache[in]
384      * @param storeInfoList - New Scanning Result[in]
385      */
386     void MergeScanResult(std::vector<WifiScanInfo> &results, std::vector<WifiScanInfo> &storeInfoList);
387     /**
388      * @Description Try to restore saved network
389      */
390     void TryToRestoreSavedNetwork();
391     /**
392      * @Description Save the scanning result in the configuration center.
393      *
394      * @param scanConfig - scan Config[in]
395      * @param scanInfoList - scan result list[in]
396      * @return success: true, failed: false
397      */
398     bool StoreFullScanInfo(const StoreScanConfig &scanConfig, std::vector<InterScanInfo> &scanInfoList);
399     /**
400      * @Description Saves the scanning result of specified parameters in the configuration center.
401      *
402      * @param scanConfig - scan Config[in]
403      * @param scanInfoList - scan result list[in]
404      * @return success: true, failed: false
405      */
406     bool StoreUserScanInfo(const StoreScanConfig &scanConfig, std::vector<InterScanInfo> &scanInfoList);
407 
408     void ReportScanStartEvent();
409     void ReportScanStopEvent();
410     void ReportScanFinishEvent(int event);
411     /**
412      * @Description Sends the scanning result to the interface service,
413      *              which then sends the scanning result to the connection
414      *              management module for processing.
415      *
416      * @param scanInfoList - scan result list[in]
417      */
418     void ReportScanInfos(std::vector<InterScanInfo> &interScanList);
419 
420     /**
421      * @Description Sends the store scanning result to the interface service,
422      *              which then sends the store scanning result to the connection
423      *              management module for processing.
424      *
425      * @param scanInfoList - scan result list[in]
426      */
427     void ReportStoreScanInfos(std::vector<InterScanInfo> &interScanList);
428 
429     /**
430      * @Description Enter the PNO scanning message body.
431      *
432      * @param interMessage - Message pointer[in]
433      * @param pnoScanConfig - PNO Scanning Configuration[in]
434      * @return success: true, failed: false
435      */
436     bool AddPnoScanMessageBody(InternalMessagePtr interMessage, const PnoScanConfig &pnoScanConfig);
437     /**
438      * @Description Stopping System Scanning
439      *
440      */
441     void StopSystemScan();
442     /**
443      * @Description Start the periodic scanning.
444      *
445      * @param scanAtOnce - Whether to start a scan immediately[in]
446      */
447     void StartSystemTimerScan(bool scanAtOnce);
448     /**
449      * @Description System scanning timer expiration processing.
450      *
451      */
452     void HandleSystemScanTimeout();
453     /**
454      * @Description Detected disconnections at intervals and started scanning.
455      *
456      */
457     void DisconnectedTimerScan();
458     /**
459      * @Description Detected disconnections at intervals and started scanning.
460      *
461      */
462     void HandleDisconnectedScanTimeout();
463     /**
464      * @Description Callback function for obtaining the scanning result
465      *
466      * @param requestIndexList - Request Index List[in]
467      * @param scanInfoList - Scan Info List[in]
468      */
469     void HandleCommonScanInfo(std::vector<int> &requestIndexList, std::vector<InterScanInfo> &scanInfoList);
470     /**
471      * @Description Handle scanning result
472      *
473      * @param requestIndexList - Request Index List[in]
474      * @param scanInfoList - Scan Info List[in]
475      * @param fullScanStored - Full scan stored [in]
476      */
477     void HandleScanResults(std::vector<int> &requestIndexList, std::vector<InterScanInfo> &scanInfoList,
478         bool &fullScanStored);
479     /**
480      * @Description Common scanning failure processing
481      *
482      * @param requestIndexList - Request Index List[in]
483      */
484     void HandleCommonScanFailed(std::vector<int> &requestIndexList);
485     /**
486      * @Description Callback function for obtaining the PNO scanning result
487      *
488      * @param scanInfoList - Scan Info List[in]
489      */
490     void HandlePnoScanInfo(std::vector<InterScanInfo> &scanInfoList);
491     /**
492      * @Description PNO scanning failed, Restart after a delay.
493      *
494      */
495     void RestartPnoScanTimeOut();
496     ErrCode ScanControlInner(ScanType scanType);
497     /**
498      * @Description System scanning failed, Restart after a delay.
499      */
500     void RestartSystemScanTimeOut();
501     /**
502      * @Description Determines whether external scanning is allowed based on the scanning policy.
503      *
504      * @return success: true, failed: false
505      */
506     ErrCode AllowExternScan();
507     /**
508      * @Description Determines whether native external scanning is allowed based on the scanning policy.
509      *
510      * @return success: true, failed: false
511      */
512     ErrCode AllowNativeExternScan();
513     /**
514      * @Description Determine whether to allow scheduled system scanning.
515      *
516      * @return success: true, failed: false
517      */
518     ErrCode AllowSystemTimerScan();
519     /**
520      * @Description Determines whether to allow PNO scanning based on the scanning policy.
521      *
522      * @return success: true, failed: false
523      */
524     ErrCode AllowPnoScan();
525     /**
526      * @Description Determines whether to allow scanning based on the scanning type..
527      *
528      * @param scanType - scan type: 0 - Extern; 1 - SystemTimer 2 Pno
529      * @return true: allow, false: not allowed.
530      */
531     ErrCode AllowScanByType(ScanType scanType);
532     /**
533      * @Description Set the current mode to trust list mode.
534      *
535      */
536     void SetScanTrustMode();
537     /**
538      * @Description Reset to non scan trust list mode.
539      *
540      */
541     void ResetToNonTrustMode();
542     /**
543      * @Description Is it the trust list mode?
544      *
545      * @param sceneId - current scene id[out].
546      * @return true: success, false: failed
547      */
548     bool IsScanTrustMode() const;
549     /**
550      * @Description Add the scene id to trust list.
551      * Assume that the scene id of moving freeze is -1.
552      *
553      * @param sceneId - scene id.
554      */
555     void AddScanTrustSceneId(int sceneId);
556     /**
557      * @Description Clear trust list.
558      *
559      */
560     void ClearScanTrustSceneIds();
561     /**
562      * @Description Is sceneId in trust list.
563      *
564      * @param sceneId - scene id.
565      */
566     bool IsInScanTrust(int sceneId) const;
567     /**
568      * @Description Is it the moving freeze state?
569      *
570      * @param appRunMode - current scan mode.
571      * @return true: success, false: failed
572      */
573     bool IsMovingFreezeState(ScanMode appRunMode) const;
574     /**
575      * @Description Whether scanned in moving freeze state.?
576      *
577      */
578     bool IsMovingFreezeScaned() const;
579     /**
580      * @Description Apply trustlists scanning policies.
581      *
582      * @param scanType - scan type: 0 - Extern; 1 - SystemTimer 2 Pno
583      * @return true: success, false: failed
584      */
585     ErrCode ApplyTrustListPolicy(ScanType scanType);
586     /* *
587      * @Description Apply trustlists and moving freeze scanning policies.
588      *
589      * @param scanType - type: 0 - Extern; 1 - SystemTimer 2 Pno
590      * @return WIFI_OPT_SUCCESS: success,
591      * WIFI_OPT_FAILED: general scan control fail,
592      * WIFI_OPT_MOVING_FREEZE_CTRL: moving freeze scan control fail.
593      */
594     ErrCode ApplyScanPolices(ScanType type);
595     /* *
596      * @Description  Obtains the current screen.
597      *
598      * @return success: ScanScene, failed: SCAN_SCENE_MAX
599      */
600     int GetStaScene();
601 
602     /**
603      * @Description Determines whether externally initiated scanning is being processed.
604      *
605      * @return success: true, failed: false
606      */
607     bool IsExternScanning() const;
608     /**
609      * @Description Indicates whether scanning with parameter.
610      *
611      * @return success: true, failed: false
612      */
613     bool IsScanningWithParam();
614     /**
615      * @Description Adjust the frequency band and frequency based on the scanning policy.
616      *
617      * @param scanBand - scan band[in]
618      * @param freqs - scan frequency[in]
619      */
620     void GetAllowBandFreqsControlInfo(ScanBandType &scanBand, std::vector<int> &freqs);
621     /**
622      * @Description Do not adjust the frequency band when the 2.4 GHz frequency band is used.
623      *
624      * @param scanBand - scan band[in]
625      * @return ScanBandType - scan band
626      */
627     ScanBandType ConvertBandNotAllow24G(ScanBandType scanBand);
628     /**
629      * @Description Do not adjust the frequency band when the 5 GHz frequency band is used.
630      *
631      * @param scanBand - scan band[in]
632      * @return ScanBandType - scan band
633      */
634     ScanBandType ConvertBandNotAllow5G(ScanBandType scanBand);
635     /**
636      * @Description Delete the 2.4 GHz frequency from the frequency list.
637      *
638      * @param freqs - frequency[in]
639      */
640     void Delete24GhzFreqs(std::vector<int> &freqs);
641     /**
642      * @Description Delete the 5 GHz frequency from the frequency list.
643      *
644      * @param freqs - frequency list[in]
645      */
646     void Delete5GhzFreqs(std::vector<int> &freqs);
647     /**
648      * @Description Get the ssid of saved networks.
649      *
650      * @param savedNetworkSsid - ssid of saved networks[out]
651      * @return success: true, failed: false
652      */
653     bool GetSavedNetworkSsidList(std::vector<std::string> &savedNetworkSsid);
654     /**
655      * @Description Get the ssid of saved hidden networks.
656      *
657      * @param hiddenNetworkSsid - ssid of hidden networks[out]
658      * @return success: true, failed: false
659      */
660     bool GetHiddenNetworkSsidList(std::vector<std::string> &hiddenNetworkSsid);
661     /**
662      * @Description ScanForbidMap control mode specific implementation for specific scanning mode.
663      *
664      * @param isStaScene - Indicates whether the STA scenario is used.[in]
665      * @param scanScene - scan scene[in][in]
666      * @param scanMode - scan mode[in][in]
667      * @return success: true, failed: false
668      */
669     bool AllowScanByForbidMap(int scanScene, ScanMode scanMode, time_t currentTime);
670     /**
671      * @Description Check whether the scan mode can be used during scanning under the forbid mode control.
672      *
673      * @param scanMode scan mode[in]
674      * @return true - success
675      * @return false  - failed
676      */
677     bool AllowScanDuringScanning(ScanMode scanMode) const;
678     /**
679      * @Description
680      *
681      * @param staScene sta scan scene[in]
682      * @param scanMode scan mode[in]
683      * @return true - success
684      * @return false  - failed
685      */
686     bool AllowScanDuringStaScene(int staScene, ScanMode scanMode);
687     /**
688      * @Description Check whether the scan mode can be used during custom scene off under the forbid mode control.
689      *
690      * @param scanMode [in]
691      * @return true - success
692      * @return false  - failed
693      */
694     bool AllowScanDuringCustomScene(ScanMode scanMode);
695     /**
696      * @Description Check whether the scan mode can be used under the interval mode control.
697      *
698      * @param appId App type for external requests to scan.[in]
699      * @param scanScene scan scene[in]
700      * @param scanMode scna mode[in]
701      * @return true - success
702      * @return false  - failed
703      */
704     bool AllowExternScanByIntervalMode(int appId, int scanScene, ScanMode scanMode);
705 #ifdef SUPPORT_SCAN_CONTROL
706     /**
707      * @Description Determines whether to allow system scan based on scanInterval control mode.
708      *
709      * @param staScene - sta scene.[in]
710      * @param interval - system scan interval[in]
711      * @param count - Total number of times that the scanning interval is multiplied by 2[in]
712      * @return success: true, failed: false
713      */
714     bool SystemScanByInterval(int staScene, int &interval, int &count);
715 #else
716     /**
717      * @Description Determines whether to allow system scan based on scanInterval control mode.
718      *
719      * @param expScanCount - Number of scan.[in]
720      * @param interval - system scan interval[in]
721      * @param count - Total number of times that the scanning interval is multiplied by 2[in]
722      * @return success: true, failed: false
723      */
724     bool SystemScanByInterval(int &expScanCount, int &interval, int &count);
725 #endif
726     /**
727      * @Description Determines whether to allow pno scan based on scanInterval control mode.
728      *
729      * @param fixedScanCount pno scan count[in]
730      * @param fixedScanTime pno scan time[in]
731      * @param interval pno scan interval[in]
732      * @param count pno scan max count[in]
733      * @return true - success
734      * @return false  - failed
735      */
736     bool PnoScanByInterval(int &fixedScanCount, time_t &fixedScanTime, int interval, int count);
737     /**
738      * @Description Determines whether to allow extern scan based on scanInterval control mode.
739      *
740      * @param appId ID of the app to be scanned[in]
741      * @param singleAppForbid Stored External Scan Parameters[in]
742      * @return true
743      * @return false
744      */
745     bool ExternScanByInterval(int appId, SingleAppForbid &singleAppForbid);
746     /**
747      * @Description Determine whether external scanning of a single application can be distinguished.
748      *
749      * @param appId ID of the app to be scanned[in]
750      * @param scanIntervalMode scan control policy parameters[in]
751      * @return true
752      * @return false
753      */
754     bool AllowSingleAppScanByInterval(int appId, ScanIntervalMode scanIntervalMode);
755     /**
756      * @Description Check whether external scanning is allowed regardless of a single application.
757      *
758      * @param appId ID of the app to be scanned[in]
759      * @param scanIntervalMode intervalMode scan control policy parameters[in]
760      * @return true
761      * @return false
762      */
763     bool AllowFullAppScanByInterval(int appId, ScanIntervalMode scanIntervalMode);
764     /**
765      * @Description Determines whether external scanning is allowed in scanning control in INTERVAL_FIXED mode.
766      *
767      * @param fixedScanCount INTERVAL_FIXED intervalMode scan time[in]
768      * @param fixedScanTime INTERVAL_FIXED intervalMode scan time[in]
769      * @param interval scan interval[in]
770      * @param count scan count[in]
771      * @return true
772      * @return false
773      */
774     bool AllowScanByIntervalFixed(int &fixedScanCount, time_t &fixedScanTime, int &interval, int &count);
775     /**
776      * @Description Determines whether external scanning is allowed in scanning control in INTERVAL_EXP mode.
777      *
778      * @param expScanCount INTERVAL_EXP intervalMode scan count[in]
779      * @param interval scan interval[in]
780      * @param count scan count[in]
781      * @return true
782      * @return false
783      */
784     bool AllowScanByIntervalExp(int &expScanCount, int &interval, int &count);
785     /**
786      * @Description Determines whether external scanning is allowed in scanning control in INTERVAL_CONTINUE mode.
787      *
788      * @param continueScanTime INTERVAL_CONTINUE intervalMode scan time[in]
789      * @param lessThanIntervalCount INTERVAL_CONTINUE intervalMode scan count[in]
790      * @param interval scan interval[in]
791      * @param count scan count[in]
792      * @return true
793      * @return false
794      */
795     bool AllowScanByIntervalContinue(time_t &continueScanTime, int &lessThanIntervalCount, int &interval, int &count);
796     /**
797      * @Description Determines whether external scanning is allowed in scanning control in INTERVAL_BLOCKLIST mode.
798      *
799      * @param appId ID of the app to be scanned[in]
800      * @param blockListScanTime INTERVAL_BLOCKLIST intervalMode scan time[in]
801      * @param lessThanIntervalCount INTERVAL_BLOCKLIST intervalMode scan count[in]
802      * @param interval scan interval[in]
803      * @param count scan count[in]
804      * @return true
805      * @return false
806      */
807     bool AllowScanByIntervalBlocklist(
808         int appId, time_t &blockListScanTime, int &lessThanIntervalCount, int &interval, int &count);
809 
810         /**
811      * @Description Determines whether scanning is allowed by disable scan control.
812      *
813      * @return true: allow, false: not allowed.
814      */
815     bool AllowScanByDisableScanCtrl();
816     /**
817      * @Description Determines whether scanning is allowed in movingfreeze mode.
818      *
819      * @param appRunMode scan mode
820      * @return true: allow, false: not allowed.
821      */
822     bool AllowScanByMovingFreeze(ScanMode appRunMode);
823     /**
824      * @Description Determines whether scanning is allowed in hid2d state.
825      *
826      * @return true: allow, false: not allowed.
827      */
828     bool AllowScanByHid2dState();
829 
830     /**
831      * @Description Is the app in the trustlist?
832      *
833      * @param trustList trustlist[in]
834      * @param sceneId scene id[in]
835      * @param appPackageName app package name[in]
836      * @return true: in the trustlist, false: not in the trustlist.
837      */
838     bool IsPackageInTrustList(const std::string& trustList, int sceneId, const std::string &appPackageName) const;
839     /* *
840      * @Description all scan check at custom check.
841      *
842      * @param customIter custom iterator[in]
843      * @param scanMode scene mode[in]
844      * @return true: allow, false: not allowed.
845      */
846     bool AllowCustomSceneCheck(const std::map<int, time_t>::const_iterator &customIter, ScanMode scanMode);
847     /* *
848      * @Description Is app in the filter list.
849      *
850      * @param packageFilter packageFilter[in]
851      * @return true: int the list, false: not in the list.
852      */
853     bool IsAppInFilterList(const std::vector<std::string> &packageFilter) const;
854     /* *
855      * @Description adjust system scan interval when sta connected.
856      *
857      * @param interval scan interval[in]
858      */
859     void SystemScanConnectedPolicy(int &interval);
860     /* *
861      * @Description adjust system scan interval when sta disconnected.
862      *
863      * @param interval scan interval[in]
864      * @param count adjust count[in]
865      */
866     void SystemScanDisconnectedPolicy(int &interval, int &count);
867 #ifndef OHOS_ARCH_LITE
868     class WifiCountryCodeChangeObserver : public IWifiCountryCodeChangeListener {
869     public:
WifiCountryCodeChangeObserver(const std::string & name,StateMachine & stateMachineObj)870         WifiCountryCodeChangeObserver(const std::string &name, StateMachine &stateMachineObj)
871             : IWifiCountryCodeChangeListener(name, stateMachineObj) {}
872         ~WifiCountryCodeChangeObserver() override = default;
873         ErrCode OnWifiCountryCodeChanged(const std::string &wifiCountryCode) override;
874         std::string GetListenerModuleName() override;
875     };
876     std::shared_ptr<IWifiCountryCodeChangeListener> m_scanObserver;
877 #endif
878 };
879 }  // namespace Wifi
880 }  // namespace OHOS
881 
882 #endif
883