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 COMMUNICATION_WIFI_NETWORK_STATUS_HISTORY_MANAGER_H
17 #define COMMUNICATION_WIFI_NETWORK_STATUS_HISTORY_MANAGER_H
18 
19 #include <string>
20 #include <cstdint>
21 namespace OHOS {
22 namespace Wifi {
23 enum class NetworkStatus {
24     UNKNOWN,
25     HAS_INTERNET,
26     PORTAL,
27     NO_INTERNET
28 };
29 
30 class NetworkStatusHistoryManager {
31 public:
32 
33     /**
34      * Insert the network status records
35      * @param networkStatusHistory  historical network status records
36      * @param networkStatus target network status;
37      */
38     static void Insert(uint32_t &networkStatusHistory, NetworkStatus networkStatus);
39 
40     /**
41      * Update the network status records
42      *
43      * @param networkStatusHistory  historical network status records
44      * @param networkStatus target status;
45      */
46     static void Update(uint32_t &networkStatusHistory, NetworkStatus networkStatus);
47 
48     /**
49      * determine whether to access the internet based on historical network records;
50      *
51      * @param networkStatusHistory  historical network status records
52      * @return whether to access the internet
53      */
54     static bool IsInternetAccessByHistory(uint32_t networkStatusHistory);
55 
56     /**
57      * determine whether the access to internet recovery based on historical network records
58      *
59      * @param networkStatusHistory historical network status records
60      * @return whether the access to internet recovery
61      */
62     static bool IsAllowRecoveryByHistory(uint32_t networkStatusHistory);
63 
64     /**
65      * determine whether the network is portal based on historical network records
66      *
67      * @param networkStatusHistory
68      * @return
69      */
70     static bool IsPortalByHistory(uint32_t networkStatusHistory);
71 
72     /**
73      * determine whether the network has internet ever on historical network records
74      *
75      * @param networkStatusHistory
76      * @return
77      */
78     static bool HasInternetEverByHistory(uint32_t networkStatusHistory);
79 
80     /**
81      * determine whether the network has no networkStatus history on historical network records
82      *
83      * @param networkStatusHistory
84      * @return
85      */
86     static bool IsEmptyNetworkStatusHistory(uint32_t networkStatusHistory);
87 
88     /**
89      * to Display the networkStatus History;
90      *
91      * @param networkStatusHistory
92      * @return
93      */
94     static std::string ToString(uint32_t networkStatusHistory);
95 
96     static std::vector<int> GetCurrentNetworkStatusHistory2Array(uint32_t networkStatusHistory);
97 
98     /**
99      * get the last network status of the network status records given.
100      *
101      * @param networkHistory historical network status records
102      * @return the last network status
103      */
104     static NetworkStatus GetLastNetworkStatus(uint32_t networkHistory);
105 
106 private:
107     /* the num of enum class NetworkStatus values */
108     constexpr static int NETWORK_STATUS_NUM = 4;
109 
110     /*!
111      * count different network Status records.
112      *
113      * @param networkStatusHistory historical network status records
114      * @param counts 0: UNKNOWN,1:HAS_INTERNET,2:PORTAL,3:NO_INTERNET
115      */
116     static void CountNetworkStatus(uint32_t networkStatusHistory, int counts[NETWORK_STATUS_NUM]);
117 
118     /*!
119      * the mask to get the network status from network status history.
120      */
121     constexpr static unsigned int NETWORK_STATUS_MASK = 0b11;
122 
123     /*!
124      * the mask to limit the num of network status records.
125      */
126     constexpr static int NETWORK_STATUS_HISTORY_MAX_MASK = 0xfffff;
127 
128     /*!
129      * wifi recovery percentage
130      */
131     constexpr static double RECOVERY_PERCENTAGE = 0.8;
132 
133     /**
134      * number of bits occupied by each record
135      */
136     constexpr static int ITEM_BIT_NUM = 2;
137 };
138 }
139 }
140 #endif
141